ReactOS 0.4.16-dev-1786-gfe497a2
pcdisk.c
Go to the documentation of this file.
1/*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20// #if defined(__i386__) || defined(_M_AMD64)
21
22#include <freeldr.h>
23
24#include <debug.h>
26
27/* Enable this line if you want to support multi-drive caching (increases FreeLdr size!) */
28// #define CACHE_MULTI_DRIVES
29
30#include <pshpack1.h>
31
32typedef struct
33{
34 UCHAR PacketSize; // 00h - Size of packet (10h or 18h)
35 UCHAR Reserved; // 01h - Reserved (0)
36 USHORT LBABlockCount; // 02h - Number of blocks to transfer (max 007Fh for Phoenix EDD)
37 USHORT TransferBufferOffset; // 04h - Transfer buffer offset (seg:off)
38 USHORT TransferBufferSegment; // Transfer buffer segment (seg:off)
39 ULONGLONG LBAStartBlock; // 08h - Starting absolute block number
40// ULONGLONG TransferBuffer64; // 10h - (EDD-3.0, optional) 64-bit flat address of transfer buffer
41 // used if DWORD at 04h is FFFFh:FFFFh
42 // Commented since some earlier BIOSes refuse to work with
43 // such extended structure
45
46typedef struct
47{
48 UCHAR PacketSize; // 00h - Size of packet in bytes (13h)
49 UCHAR MediaType; // 01h - Boot media type (see #00282)
50 UCHAR DriveNumber; /* 02h - Drive number:
51 * 00h Floppy image
52 * 80h Bootable hard disk
53 * 81h-FFh Nonbootable or no emulation
54 */
55 UCHAR Controller; // 03h - CD-ROM controller number
56 ULONG LBAImage; // 04h - Logical Block Address of disk image to emulate
57 USHORT DeviceSpec; /* 08h - Device specification (see also #00282)
58 * (IDE) Bit 0:
59 * Drive is slave instead of master
60 * (SCSI) Bits 7-0:
61 * LUN and PUN
62 * Bits 15-8:
63 * Bus number
64 */
65 USHORT Buffer; // 0Ah - Segment of 3K buffer for caching CD-ROM reads
66 USHORT LoadSeg; // 0Ch - Load segment for initial boot image.
67 // If 0000h, load at segment 07C0h.
68 USHORT SectorCount; // 0Eh - Number of 512-byte virtual sectors to load
69 // (only valid for AH=4Ch).
70 UCHAR CHSGeometry[3]; /* 10h - Low byte of cylinder count (for INT 13/AH=08h)
71 * 11h - Sector count, high bits of cylinder count (for INT 13/AH=08h)
72 * 12h - Head count (for INT 13/AH=08h)
73 */
76
77/*
78 * Extended disk geometry (Int13 / AH=48h)
79 * See also ntdddisk.h DISK_EX_INT13_INFO
80 */
81typedef struct _EXTENDED_GEOMETRY
82{
92
93#include <poppack.h>
94
95typedef struct _PC_DISK_DRIVE
96{
97 /* Disk geometry (legacy BIOS and INT13 extended) */
100
101 /* TRUE when INT 13h extensions are supported */
103
104 /*
105 * 'IsRemovable' flag: TRUE when the drive is removable (e.g. floppy, CD-ROM...).
106 * In that case some of the cached information might need to be refreshed regularly.
107 */
109
110#ifdef CACHE_MULTI_DRIVES
111 /*
112 * 'Initialized' flag: if TRUE then the drive has been initialized;
113 * if FALSE then it needs to be initialized; if its high bit is set
114 * then there has been an error; don't try to use it.
115 */
117#endif
119
120#ifdef CACHE_MULTI_DRIVES
121/* Cache of all possible PC disk drives */
122// Maximum number of disks is 0x100, indexed from 0x00 to 0xFF.
123static PC_DISK_DRIVE PcDiskDrive[0x100];
124#else
125/* Cached data for the last-accessed PC disk drive */
126// We use a USHORT so that we can initialize it with a drive number that cannot exist
127// on the system (they are <= 0xFF), therefore forcing drive caching on first access.
128static USHORT LastDriveNumber = 0xFFFF;
130#endif /* CACHE_MULTI_DRIVES */
131
132/* DISK IO ERROR SUPPORT *****************************************************/
133
134static LONG lReportError = 0; // >= 0: display errors; < 0: hide errors.
135
137{
138 /* Set the reference count */
139 if (bShowError) ++lReportError;
140 else --lReportError;
141 return lReportError;
142}
143
145{
146 switch (ErrorCode)
147 {
148 case 0x00: return "no error";
149 case 0x01: return "bad command passed to driver";
150 case 0x02: return "address mark not found or bad sector";
151 case 0x03: return "diskette write protect error";
152 case 0x04: return "sector not found";
153 case 0x05: return "fixed disk reset failed";
154 case 0x06: return "diskette changed or removed";
155 case 0x07: return "bad fixed disk parameter table";
156 case 0x08: return "DMA overrun";
157 case 0x09: return "DMA access across 64k boundary";
158 case 0x0A: return "bad fixed disk sector flag";
159 case 0x0B: return "bad fixed disk cylinder";
160 case 0x0C: return "unsupported track/invalid media";
161 case 0x0D: return "invalid number of sectors on fixed disk format";
162 case 0x0E: return "fixed disk controlled data address mark detected";
163 case 0x0F: return "fixed disk DMA arbitration level out of range";
164 case 0x10: return "ECC/CRC error on disk read";
165 case 0x11: return "recoverable fixed disk data error, data fixed by ECC";
166 case 0x20: return "controller error (NEC for floppies)";
167 case 0x40: return "seek failure";
168 case 0x80: return "time out, drive not ready";
169 case 0xAA: return "fixed disk drive not ready";
170 case 0xBB: return "fixed disk undefined error";
171 case 0xCC: return "fixed disk write fault on selected drive";
172 case 0xE0: return "fixed disk status error/Error reg = 0";
173 case 0xFF: return "sense operation failed";
174
175 default: return "unknown error code";
176 }
177}
178
179static VOID DiskError(PCSTR ErrorString, ULONG ErrorCode)
180{
181 CHAR ErrorCodeString[200];
182
183 if (lReportError < 0)
184 return;
185
186 sprintf(ErrorCodeString, "%s\n\nError Code: 0x%lx\nError: %s",
188
189 ERR("%s\n", ErrorCodeString);
190 UiMessageBox(ErrorCodeString);
191}
192
193/* FUNCTIONS *****************************************************************/
194
196{
197 REGS RegsIn, RegsOut;
198
199 WARN("DiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber);
200
201 /*
202 * BIOS Int 13h, function 0 - Reset disk system
203 * AH = 00h
204 * DL = drive (if bit 7 is set both hard disks and floppy disks reset)
205 * Return:
206 * AH = status
207 * CF clear if successful
208 * CF set on error
209 */
210 RegsIn.b.ah = 0x00;
211 RegsIn.b.dl = DriveNumber;
212
213 /* Reset the disk controller */
214 Int386(0x13, &RegsIn, &RegsOut);
215
216 return INT386_SUCCESS(RegsOut);
217}
218
219static BOOLEAN
221{
222 /*
223 * Hard disks use drive numbers >= 0x80 . So if the drive number
224 * indicates a hard disk then return FALSE.
225 * 0x49 is our magic ramdisk drive, so return FALSE for that too.
226 */
227 if ((DriveNumber >= 0x80) || (DriveNumber == 0x49))
228 return FALSE;
229
230 /* The drive is a floppy diskette so return TRUE */
231 return TRUE;
232}
233
234static BOOLEAN
236{
237 REGS RegsIn, RegsOut;
238
239 /*
240 * Some BIOSes report that extended disk access functions are not supported
241 * when booting from a CD (e.g. Phoenix BIOS v6.00PG and Insyde BIOS shipping
242 * with Intel Macs). Therefore we just return TRUE if we're booting from a CD
243 * - we can assume that all El Torito capable BIOSes support INT 13 extensions.
244 * We simply detect whether we're booting from CD by checking whether the drive
245 * number is >= 0x8A. It's 0x90 on the Insyde BIOS, and 0x9F on most other BIOSes.
246 */
247 if (DriveNumber >= 0x8A)
248 return TRUE;
249
250 /*
251 * IBM/MS INT 13 Extensions - INSTALLATION CHECK
252 * AH = 41h
253 * BX = 55AAh
254 * DL = drive (80h-FFh)
255 * Return:
256 * CF set on error (extensions not supported)
257 * AH = 01h (invalid function)
258 * CF clear if successful
259 * BX = AA55h if installed
260 * AH = major version of extensions
261 * 01h = 1.x
262 * 20h = 2.0 / EDD-1.0
263 * 21h = 2.1 / EDD-1.1
264 * 30h = EDD-3.0
265 * AL = internal use
266 * CX = API subset support bitmap
267 * DH = extension version (v2.0+ ??? -- not present in 1.x)
268 *
269 * Bitfields for IBM/MS INT 13 Extensions API support bitmap
270 * Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
271 * Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
272 * Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
273 * extended drive parameter table is valid
274 * Bits 3-15 reserved
275 */
276 RegsIn.b.ah = 0x41;
277 RegsIn.w.bx = 0x55AA;
278 RegsIn.b.dl = DriveNumber;
279
280 /* Reset the disk controller */
281 Int386(0x13, &RegsIn, &RegsOut);
282 if (!INT386_SUCCESS(RegsOut))
283 {
284 /* CF set on error (extensions not supported) */
285 return FALSE;
286 }
287
288 if (RegsOut.w.bx != 0xAA55)
289 {
290 /* BX = AA55h if installed */
291 return FALSE;
292 }
293
294#if DBG
295 TRACE("Drive 0x%x: INT 13h Extended version: 0x%02x, API bitmap: 0x%04x\n",
296 DriveNumber, RegsOut.b.ah, RegsOut.w.cx);
297#endif
298 if (!(RegsOut.w.cx & 0x0001))
299 {
300 /*
301 * CX = API subset support bitmap.
302 * Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported.
303 */
304 WARN("Drive 0x%x: Suspicious API subset support bitmap 0x%04x\n",
305 DriveNumber, RegsOut.w.cx);
306 return FALSE;
307 }
308
309 return TRUE;
310}
311
312static BOOLEAN
314 _In_ UCHAR DriveNumber,
315 _In_ PPC_DISK_DRIVE DiskDrive,
318{
320 REGS RegsIn, RegsOut;
321
322 TRACE("DiskGetExtendedDriveParameters(0x%x)\n", DriveNumber);
323
324 if (!DiskDrive->Int13ExtensionsSupported || (BufferSize < sizeof(*Ptr)))
325 return FALSE;
326
327 /*
328 * Initialize the transfer buffer.
329 * NOTE: Zeroing out the buffer also helps avoiding the bug where Dell
330 * machines using PhoenixBIOS 4.0 Release 6.0 fail to correctly handle
331 * this function if Ptr->Flags is not 0 on entry.
332 */
333 RtlZeroMemory(Ptr, sizeof(*Ptr)); // BufferSize;
334 Ptr->Size = BufferSize;
335
336 /*
337 * BIOS Int 13h, function 48h - Get drive parameters
338 * AH = 48h
339 * DL = drive (bit 7 set for hard disk)
340 * DS:SI = result buffer
341 * Return:
342 * CF set on error
343 * AH = status (07h)
344 * CF clear if successful
345 * AH = 00h
346 * DS:SI -> result buffer
347 */
348 RegsIn.b.ah = 0x48;
349 RegsIn.b.dl = DriveNumber;
350 RegsIn.x.ds = BIOSCALLBUFSEGMENT;
351 RegsIn.w.si = BIOSCALLBUFOFFSET;
352
353 /* Get drive parameters */
354 Int386(0x13, &RegsIn, &RegsOut);
355 if (!INT386_SUCCESS(RegsOut))
356 return FALSE;
357
359
360#if DBG
361 TRACE("Size of buffer: 0x%x\n", Ptr->Size);
362 TRACE("Information flags: 0x%x\n", Ptr->Flags);
363 TRACE("Number of physical cylinders on drive: %u\n", Ptr->Cylinders);
364 TRACE("Number of physical heads on drive: %u\n", Ptr->Heads);
365 TRACE("Number of physical sectors per track: %u\n", Ptr->SectorsPerTrack);
366 TRACE("Total number of sectors on drive: %I64u\n", Ptr->Sectors);
367 TRACE("Bytes per sector: %u\n", Ptr->BytesPerSector);
368 if (Ptr->Size >= 0x1E)
369 {
370 // LOWORD(Ptr->PDPTE): offset, HIWORD(Ptr->PDPTE): segment
371 USHORT Off = (USHORT)(Ptr->PDPTE & 0xFFFF); // ((PUSHORT)&Ptr->PDPTE)[0];
372 USHORT Seg = (USHORT)((Ptr->PDPTE >> 16) & 0xFFFF); // ((PUSHORT)&Ptr->PDPTE)[1];
373 TRACE("EDD configuration parameters (DPTE): %x:%x\n", Seg, Off);
374
375 /* The DPTE pointer is valid if it's != FFFF:FFFF (per the Enhanced Disk
376 * Drive Specification), but also, when it's != 0000:0000 (broken BIOSes) */
377 if (Ptr->PDPTE != 0xFFFFFFFF && Ptr->PDPTE != 0)
378 {
379 PUCHAR SpecPtr = (PUCHAR)(ULONG_PTR)((Seg << 4) + Off);
380 TRACE("SpecPtr: 0x%x\n", SpecPtr);
381 TRACE("Physical I/O port base address: 0x%x\n", *(PUSHORT)&SpecPtr[0]);
382 TRACE("Disk-drive control port address: 0x%x\n", *(PUSHORT)&SpecPtr[2]);
383 TRACE("Head register upper nibble: 0x%x\n", SpecPtr[4]);
384 TRACE("BIOS Vendor-specific: 0x%x\n", SpecPtr[5]);
385 TRACE("IRQ for drive: %u\n", SpecPtr[6]);
386 TRACE("Sector count for multi-sector transfers: %u\n", SpecPtr[7]);
387 TRACE("DMA control: 0x%x\n", SpecPtr[8]);
388 TRACE("Programmed I/O control: 0x%x\n", SpecPtr[9]);
389 TRACE("Drive options: 0x%x\n", *(PUSHORT)&SpecPtr[10]);
390 }
391 }
392 if (Ptr->Size >= 0x42)
393 {
394 TRACE("Signature: 0x%x\n", ((PUSHORT)Ptr)[15]);
395 }
396#endif // DBG
397
398 return TRUE;
399}
400
401static BOOLEAN
403 IN UCHAR DriveNumber,
404 IN PPC_DISK_DRIVE DiskDrive)
405{
407 REGS RegsIn, RegsOut;
408 ULONG Cylinders;
409
410 /* Get the extended geometry first */
411 RtlZeroMemory(&DiskDrive->ExtGeometry, sizeof(DiskDrive->ExtGeometry));
412 Success = DiskGetExtendedDriveParameters(DriveNumber, DiskDrive,
413 &DiskDrive->ExtGeometry,
414 sizeof(DiskDrive->ExtGeometry));
415 if (Success)
416 {
417 TRACE("DiskGetExtendedDriveParameters(0x%x) returned:\n"
418 "Cylinders : 0x%x\n"
419 "Heads : 0x%x\n"
420 "Sects/Track: 0x%x\n"
421 "Total Sects: 0x%llx\n"
422 "Bytes/Sect : 0x%x\n",
423 DriveNumber,
424 DiskDrive->ExtGeometry.Cylinders,
425 DiskDrive->ExtGeometry.Heads,
426 DiskDrive->ExtGeometry.SectorsPerTrack,
427 DiskDrive->ExtGeometry.Sectors,
428 DiskDrive->ExtGeometry.BytesPerSector);
429 }
430
431 /* Now try the legacy geometry */
432 RtlZeroMemory(&DiskDrive->Geometry, sizeof(DiskDrive->Geometry));
433
434 /*
435 * BIOS Int 13h, function 08h - Get drive parameters
436 * AH = 08h
437 * DL = drive (bit 7 set for hard disk)
438 * ES:DI = 0000h:0000h to guard against BIOS bugs
439 * Return:
440 * CF set on error
441 * AH = status (07h)
442 * CF clear if successful
443 * AH = 00h
444 * AL = 00h on at least some BIOSes
445 * BL = drive type (AT/PS2 floppies only)
446 * CH = low eight bits of maximum cylinder number
447 * CL = maximum sector number (bits 5-0)
448 * high two bits of maximum cylinder number (bits 7-6)
449 * DH = maximum head number
450 * DL = number of drives
451 * ES:DI -> drive parameter table (floppies only)
452 */
453 RegsIn.b.ah = 0x08;
454 RegsIn.b.dl = DriveNumber;
455 RegsIn.w.es = 0x0000;
456 RegsIn.w.di = 0x0000;
457
458 /* Get drive parameters */
459 Int386(0x13, &RegsIn, &RegsOut);
460 if (!INT386_SUCCESS(RegsOut))
461 {
462 /* We failed, return the result of the previous call (extended geometry) */
463 return Success;
464 }
465 /* OR it with the old result, so that we return TRUE whenever either call succeeded */
466 Success |= TRUE;
467
468 Cylinders = (RegsOut.b.cl & 0xC0) << 2;
469 Cylinders += RegsOut.b.ch;
470 Cylinders++;
471 DiskDrive->Geometry.Cylinders = Cylinders;
472 DiskDrive->Geometry.Heads = RegsOut.b.dh + 1;
473 DiskDrive->Geometry.SectorsPerTrack = RegsOut.b.cl & 0x3F;
474 DiskDrive->Geometry.BytesPerSector = 512; /* Just assume 512 bytes per sector */
475
476 DiskDrive->Geometry.Sectors = (ULONGLONG)DiskDrive->Geometry.Cylinders *
477 DiskDrive->Geometry.Heads *
478 DiskDrive->Geometry.SectorsPerTrack;
479
480 TRACE("Regular Int13h(0x%x) returned:\n"
481 "Cylinders : 0x%x\n"
482 "Heads : 0x%x\n"
483 "Sects/Track: 0x%x\n"
484 "Total Sects: 0x%llx\n"
485 "Bytes/Sect : 0x%x\n",
486 DriveNumber,
487 DiskDrive->Geometry.Cylinders,
488 DiskDrive->Geometry.Heads,
489 DiskDrive->Geometry.SectorsPerTrack,
490 DiskDrive->Geometry.Sectors,
491 DiskDrive->Geometry.BytesPerSector);
492
493 return Success;
494}
495
496static BOOLEAN
498 IN UCHAR DriveNumber,
499 IN OUT PPC_DISK_DRIVE DiskDrive)
500{
501 DiskDrive->IsRemovable = DiskIsDriveRemovable(DriveNumber);
502
503 /*
504 * Check to see if it is a fixed disk drive.
505 * If so then check to see if INT 13h extensions work.
506 * If they do then use them, otherwise default back to BIOS calls.
507 */
508 DiskDrive->Int13ExtensionsSupported = DiskInt13ExtensionsSupported(DriveNumber);
509
510 if (!InitDriveGeometry(DriveNumber, DiskDrive))
511 return FALSE;
512
513 TRACE("\n"
514 "DriveNumber: 0x%x\n"
515 "IsRemovable = %s\n"
516 "Int13ExtensionsSupported = %s\n",
517 DriveNumber,
518 DiskDrive->IsRemovable ? "TRUE" : "FALSE",
519 DiskDrive->Int13ExtensionsSupported ? "TRUE" : "FALSE");
520
521 return TRUE;
522}
523
524static inline
527{
528#ifdef CACHE_MULTI_DRIVES
529 PPC_DISK_DRIVE DiskDrive;
530
531 ASSERT((0 <= DriveNumber) && (DriveNumber < RTL_NUMBER_OF(PcDiskDrive)));
532
533 /* Retrieve a slot */
534 DiskDrive = &PcDiskDrive[DriveNumber];
535
536 /* If the drive has not been initialized before... */
537 if (!DiskDrive->Initialized)
538 {
539 /* ... try to initialize it now. */
540 if (!PcDiskDriveInit(DriveNumber, DiskDrive))
541 {
542 /*
543 * If we failed, there is no drive at this number
544 * and flag it as such (set its high bit).
545 */
546 DiskDrive->Initialized |= 0x80;
547 return NULL;
548 }
549 DiskDrive->Initialized = TRUE;
550 }
551 else if (DiskDrive->Initialized & 0x80)
552 {
553 /*
554 * The disk failed to be initialized previously, reset its flag to give
555 * it chance to be initialized again later, but just fail for the moment.
556 */
557 DiskDrive->Initialized = FALSE;
558 return NULL;
559 }
560
561 return DiskDrive;
562#else
563 static PC_DISK_DRIVE NewDiskDrive;
564
565 ASSERT((0 <= DriveNumber) && (DriveNumber <= 0xFF));
566
567 /* Update cached information */
568
569 /* If the drive has not been accessed last before... */
570 if ((USHORT)DriveNumber != LastDriveNumber)
571 {
572 /* ... try to (re-)initialize and cache it now. */
573 RtlZeroMemory(&NewDiskDrive, sizeof(NewDiskDrive));
574 if (!PcDiskDriveInit(DriveNumber, &NewDiskDrive))
575 {
576 /*
577 * If we failed, there is no drive at this number.
578 * Keep the last-accessed valid drive cached.
579 */
580 return NULL;
581 }
582 /* We succeeded, cache the drive data */
583 PcDiskDrive = NewDiskDrive;
584 LastDriveNumber = (USHORT)DriveNumber;
585 }
586
587 return &PcDiskDrive;
588#endif /* CACHE_MULTI_DRIVES */
589}
590
591static BOOLEAN
593 IN UCHAR DriveNumber,
594 IN ULONGLONG SectorNumber,
597{
599 REGS RegsIn, RegsOut;
600 ULONG RetryCount;
601
602 /* Setup disk address packet */
603 RtlZeroMemory(Packet, sizeof(*Packet));
604 Packet->PacketSize = sizeof(*Packet);
605 Packet->Reserved = 0;
606 // Packet->LBABlockCount set in the loop.
607 Packet->TransferBufferOffset = ((ULONG_PTR)Buffer) & 0x0F;
608 Packet->TransferBufferSegment = (USHORT)(((ULONG_PTR)Buffer) >> 4);
609 Packet->LBAStartBlock = SectorNumber;
610
611 /*
612 * BIOS Int 13h, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
613 * Return:
614 * CF clear if successful
615 * AH = 00h
616 * CF set on error
617 * AH = error code
618 * Disk address packet's block count field set to the
619 * number of blocks successfully transferred.
620 */
621 RegsIn.b.ah = 0x42;
622 RegsIn.b.dl = DriveNumber; // Drive number in DL (0 - floppy, 0x80 - harddisk)
623 RegsIn.x.ds = BIOSCALLBUFSEGMENT; // DS:SI -> disk address packet
624 RegsIn.w.si = BIOSCALLBUFOFFSET;
625
626 /* Retry 3 times */
627 for (RetryCount = 0; RetryCount < 3; ++RetryCount)
628 {
629 /* Restore the number of blocks to transfer, since it gets reset
630 * on failure with the number of blocks that were successfully
631 * transferred (and which could be zero). */
632 Packet->LBABlockCount = (USHORT)SectorCount;
633 ASSERT(Packet->LBABlockCount == SectorCount);
634
635 Int386(0x13, &RegsIn, &RegsOut);
636
637 /* If it worked, or if it was a corrected ECC error
638 * and the data is still good, return success */
639 if (INT386_SUCCESS(RegsOut) || (RegsOut.b.ah == 0x11))
640 return TRUE;
641
642 /* It failed, do the next retry */
643 DiskResetController(DriveNumber);
644 }
645
646 /* If we get here then the read failed */
647 DiskError("Disk Read Failed in LBA mode", RegsOut.b.ah);
648 ERR("Disk Read Failed in LBA mode: %x (%s) (DriveNumber: 0x%x SectorNumber: %I64u SectorCount: %u)\n",
649 RegsOut.b.ah, DiskGetErrorCodeString(RegsOut.b.ah),
650 DriveNumber, SectorNumber, SectorCount);
651
652 return FALSE;
653}
654
655static BOOLEAN
657 IN UCHAR DriveNumber,
658 IN PPC_DISK_DRIVE DiskDrive,
659 IN ULONGLONG SectorNumber,
662{
663 UCHAR PhysicalSector;
664 UCHAR PhysicalHead;
665 ULONG PhysicalTrack;
666 GEOMETRY DriveGeometry;
667 ULONG NumberOfSectorsToRead;
668 REGS RegsIn, RegsOut;
669 ULONG RetryCount;
670
671 DriveGeometry = DiskDrive->Geometry;
672 if (DriveGeometry.SectorsPerTrack == 0 || DriveGeometry.Heads == 0)
673 return FALSE;
674
675 while (SectorCount > 0)
676 {
677 /*
678 * Calculate the physical disk offsets.
679 * Note: DriveGeometry.SectorsPerTrack < 64
680 */
681 PhysicalSector = 1 + (UCHAR)(SectorNumber % DriveGeometry.SectorsPerTrack);
682 PhysicalHead = (UCHAR)((SectorNumber / DriveGeometry.SectorsPerTrack) % DriveGeometry.Heads);
683 PhysicalTrack = (ULONG)((SectorNumber / DriveGeometry.SectorsPerTrack) / DriveGeometry.Heads);
684
685 /* Calculate how many sectors we need to read this round */
686 if (PhysicalSector > 1)
687 {
688 NumberOfSectorsToRead = min(SectorCount,
689 (DriveGeometry.SectorsPerTrack - (PhysicalSector - 1)));
690 }
691 else
692 {
693 NumberOfSectorsToRead = min(SectorCount, DriveGeometry.SectorsPerTrack);
694 }
695
696 /* Make sure the read is within the geometry boundaries */
697 if ((PhysicalHead >= DriveGeometry.Heads) ||
698 (PhysicalTrack >= DriveGeometry.Cylinders) ||
699 ((NumberOfSectorsToRead + PhysicalSector) > (DriveGeometry.SectorsPerTrack + 1)) ||
700 (PhysicalSector > DriveGeometry.SectorsPerTrack))
701 {
702 DiskError("Disk read exceeds drive geometry limits.", 0);
703 return FALSE;
704 }
705
706 /*
707 * BIOS Int 13h, function 2 - Read Disk Sectors
708 * AH = 02h
709 * AL = number of sectors to read (must be nonzero)
710 * CH = low eight bits of cylinder number
711 * CL = sector number 1-63 (bits 0-5)
712 * high two bits of cylinder (bits 6-7, hard disk only)
713 * DH = head number
714 * DL = drive number (bit 7 set for hard disk)
715 * ES:BX -> data buffer
716 * Return:
717 * CF set on error
718 * if AH = 11h (corrected ECC error), AL = burst length
719 * CF clear if successful
720 * AH = status
721 * AL = number of sectors transferred
722 * (only valid if CF set for some BIOSes)
723 */
724 RegsIn.b.ah = 0x02;
725 RegsIn.b.al = (UCHAR)NumberOfSectorsToRead;
726 RegsIn.b.ch = (PhysicalTrack & 0xFF);
727 RegsIn.b.cl = (UCHAR)(PhysicalSector + ((PhysicalTrack & 0x300) >> 2));
728 RegsIn.b.dh = PhysicalHead;
729 RegsIn.b.dl = DriveNumber;
730 RegsIn.w.es = (USHORT)(((ULONG_PTR)Buffer) >> 4);
731 RegsIn.w.bx = ((ULONG_PTR)Buffer) & 0x0F;
732
733 /* Perform the read. Retry 3 times. */
734 for (RetryCount = 0; RetryCount < 3; ++RetryCount)
735 {
736 Int386(0x13, &RegsIn, &RegsOut);
737
738 /* If it worked, or if it was a corrected ECC error
739 * and the data is still good, break out */
740 if (INT386_SUCCESS(RegsOut) || (RegsOut.b.ah == 0x11))
741 break;
742
743 /* It failed, do the next retry */
744 DiskResetController(DriveNumber);
745 }
746
747 /* If we retried 3 times then fail */
748 if (RetryCount >= 3)
749 {
750 DiskError("Disk Read Failed in CHS mode, after retrying 3 times", RegsOut.b.ah);
751 ERR("Disk Read Failed in CHS mode, after retrying 3 times: %x (%s) (DriveNumber: 0x%x SectorNumber: %I64u SectorCount: %u)\n",
752 RegsOut.b.ah, DiskGetErrorCodeString(RegsOut.b.ah),
753 DriveNumber, SectorNumber, SectorCount);
754 return FALSE;
755 }
756
757 /*
758 * I have learned that not all BIOSes return
759 * the sector read count in the AL register (at least mine doesn't)
760 * even if the sectors were read correctly. So instead
761 * of checking the sector read count we will rely solely
762 * on the carry flag being set on error.
763 */
764
765 Buffer = (PVOID)((ULONG_PTR)Buffer + (NumberOfSectorsToRead * DriveGeometry.BytesPerSector));
766 SectorCount -= NumberOfSectorsToRead;
767 SectorNumber += NumberOfSectorsToRead;
768 }
769
770 return TRUE;
771}
772
775 IN UCHAR DriveNumber,
776 IN ULONGLONG SectorNumber,
779{
780 PPC_DISK_DRIVE DiskDrive;
781
782 TRACE("PcDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64u SectorCount: %u Buffer: 0x%x\n",
783 DriveNumber, SectorNumber, SectorCount, Buffer);
784
785 /* 16-bit BIOS addressing limitation */
786 ASSERT(((ULONG_PTR)Buffer) <= 0xFFFFF);
787
788 DiskDrive = PcDiskDriveNumberToDrive(DriveNumber);
789 if (!DiskDrive)
790 return FALSE;
791
792 if ((DriveNumber >= 0x80) && DiskDrive->Int13ExtensionsSupported)
793 {
794 /* LBA is easy, nothing to calculate. Just do the read. */
795 TRACE("--> Using LBA\n");
796 return PcDiskReadLogicalSectorsLBA(DriveNumber, SectorNumber, SectorCount, Buffer);
797 }
798 else
799 {
800 /* LBA is not supported, default to CHS */
801 TRACE("--> Using CHS\n");
802 return PcDiskReadLogicalSectorsCHS(DriveNumber, DiskDrive, SectorNumber, SectorCount, Buffer);
803 }
804}
805
806#if defined(__i386__) || defined(_M_AMD64)
808{
809 WRITE_PORT_UCHAR((PUCHAR)0x3F2, 0); // DOR_FDC_ENABLE | DOR_DMA_IO_INTERFACE_ENABLE 0x0C // we changed 0x0C->0 to workaround CORE-16469
810}
811#endif // defined __i386__ || defined(_M_AMD64)
812
815{
816 PPC_DISK_DRIVE DiskDrive;
817
818 TRACE("PcDiskGetDriveGeometry(0x%x)\n", DriveNumber);
819
820 DiskDrive = PcDiskDriveNumberToDrive(DriveNumber);
821 if (!DiskDrive)
822 return FALSE;
823
824 /* Try to get the extended geometry first */
825 if (DiskDrive->ExtGeometry.Size == sizeof(DiskDrive->ExtGeometry))
826 {
827 /* Extended geometry has been initialized, return it */
828 Geometry->Cylinders = DiskDrive->ExtGeometry.Cylinders;
829 Geometry->Heads = DiskDrive->ExtGeometry.Heads;
830 Geometry->SectorsPerTrack = DiskDrive->ExtGeometry.SectorsPerTrack;
831 Geometry->BytesPerSector = DiskDrive->ExtGeometry.BytesPerSector;
832 Geometry->Sectors = DiskDrive->ExtGeometry.Sectors;
833 }
834 else
835 /* Fall back to legacy BIOS geometry */
836 {
837 *Geometry = DiskDrive->Geometry;
838 }
839
840 return TRUE;
841}
842
843ULONG
845{
846 PPC_DISK_DRIVE DiskDrive;
847
848 DiskDrive = PcDiskDriveNumberToDrive(DriveNumber);
849 if (!DiskDrive)
850 return 1; // Unknown count.
851
852 /*
853 * If LBA is supported then the block size will be 64 sectors (32k).
854 * If not then the block size is the size of one track.
855 */
856 if (DiskDrive->Int13ExtensionsSupported)
857 return 64;
858 else
859 return DiskDrive->Geometry.SectorsPerTrack;
860}
861
862/* EOF */
unsigned char BOOLEAN
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
#define __cdecl
Definition: accygwin.h:79
#define WARN(fmt,...)
Definition: precomp.h:61
#define ERR(fmt,...)
Definition: precomp.h:57
#define DBG_DEFAULT_CHANNEL(ch)
Definition: debug.h:106
VOID UiMessageBox(_In_ PCSTR Format,...)
Definition: ui.c:359
Definition: bufpool.h:45
#define BufferSize
Definition: mmc.h:75
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ULONG_PTR
Definition: config.h:101
@ Success
Definition: eventcreate.c:712
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
#define ASSERT(a)
Definition: mode.c:44
#define sprintf(buf, format,...)
Definition: sprintf.c:55
#define min(a, b)
Definition: monoChain.cc:55
_In_ NDIS_HANDLE _In_ PNDIS_PACKET Packet
Definition: ndis.h:1549
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
@ Initialized
Definition: ketypes.h:388
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
ULONG SectorCount
Definition: part_xbox.c:31
VOID __cdecl DiskStopFloppyMotor(VOID)
Definition: pc98hw.c:1235
#define WRITE_PORT_UCHAR(p, d)
Definition: pc98vid.h:21
#define INT386_SUCCESS(regs)
Definition: pcbios.h:179
int __cdecl Int386(int ivec, REGS *in, REGS *out)
static BOOLEAN DiskInt13ExtensionsSupported(IN UCHAR DriveNumber)
Definition: pcdisk.c:235
static LONG lReportError
Definition: pcdisk.c:134
BOOLEAN DiskResetController(UCHAR DriveNumber)
Definition: pcdisk.c:195
struct _EXTENDED_GEOMETRY EXTENDED_GEOMETRY
static PCSTR DiskGetErrorCodeString(ULONG ErrorCode)
Definition: pcdisk.c:144
struct _EXTENDED_GEOMETRY * PEXTENDED_GEOMETRY
static BOOLEAN PcDiskReadLogicalSectorsLBA(IN UCHAR DriveNumber, IN ULONGLONG SectorNumber, IN ULONG SectorCount, OUT PVOID Buffer)
Definition: pcdisk.c:592
static BOOLEAN DiskGetExtendedDriveParameters(_In_ UCHAR DriveNumber, _In_ PPC_DISK_DRIVE DiskDrive, _Out_ PVOID Buffer, _In_ USHORT BufferSize)
Definition: pcdisk.c:313
static BOOLEAN DiskIsDriveRemovable(UCHAR DriveNumber)
Definition: pcdisk.c:220
struct _PC_DISK_DRIVE PC_DISK_DRIVE
LONG DiskReportError(BOOLEAN bShowError)
Definition: pcdisk.c:136
static USHORT LastDriveNumber
Definition: pcdisk.c:128
static BOOLEAN PcDiskReadLogicalSectorsCHS(IN UCHAR DriveNumber, IN PPC_DISK_DRIVE DiskDrive, IN ULONGLONG SectorNumber, IN ULONG SectorCount, OUT PVOID Buffer)
Definition: pcdisk.c:656
static PC_DISK_DRIVE PcDiskDrive
Definition: pcdisk.c:129
struct I386_DISK_ADDRESS_PACKET * PI386_DISK_ADDRESS_PACKET
BOOLEAN PcDiskReadLogicalSectors(IN UCHAR DriveNumber, IN ULONGLONG SectorNumber, IN ULONG SectorCount, OUT PVOID Buffer)
Definition: pcdisk.c:774
struct I386_CDROM_SPEC_PACKET * PI386_CDROM_SPEC_PACKET
struct _PC_DISK_DRIVE * PPC_DISK_DRIVE
static BOOLEAN InitDriveGeometry(IN UCHAR DriveNumber, IN PPC_DISK_DRIVE DiskDrive)
Definition: pcdisk.c:402
static PPC_DISK_DRIVE PcDiskDriveNumberToDrive(IN UCHAR DriveNumber)
Definition: pcdisk.c:526
static BOOLEAN PcDiskDriveInit(IN UCHAR DriveNumber, IN OUT PPC_DISK_DRIVE DiskDrive)
Definition: pcdisk.c:497
ULONG PcDiskGetCacheableBlockCount(UCHAR DriveNumber)
Definition: pcdisk.c:844
static VOID DiskError(PCSTR ErrorString, ULONG ErrorCode)
Definition: pcdisk.c:179
BOOLEAN PcDiskGetDriveGeometry(UCHAR DriveNumber, PGEOMETRY Geometry)
Definition: pcdisk.c:814
long LONG
Definition: pedump.c:60
unsigned short USHORT
Definition: pedump.c:61
#define TRACE(s)
Definition: solgame.cpp:4
unsigned char ch
Definition: pcbios.h:138
unsigned char dl
Definition: pcbios.h:140
unsigned char cl
Definition: pcbios.h:137
unsigned char al
Definition: pcbios.h:131
unsigned char ah
Definition: pcbios.h:132
unsigned char dh
Definition: pcbios.h:141
unsigned short ds
Definition: pcbios.h:100
USHORT TransferBufferSegment
Definition: pcdisk.c:38
ULONGLONG LBAStartBlock
Definition: pcdisk.c:39
USHORT TransferBufferOffset
Definition: pcdisk.c:37
unsigned short si
Definition: pcbios.h:116
unsigned short es
Definition: pcbios.h:121
unsigned short di
Definition: pcbios.h:117
unsigned short cx
Definition: pcbios.h:113
unsigned short bx
Definition: pcbios.h:112
ULONGLONG Sectors
Definition: pcdisk.c:88
USHORT Flags
Definition: pcdisk.c:84
USHORT BytesPerSector
Definition: pcdisk.c:89
ULONG SectorsPerTrack
Definition: pcdisk.c:87
ULONG Cylinders
Definition: pcdisk.c:85
Definition: disk.h:26
ULONG BytesPerSector
Number of bytes per sector.
Definition: disk.h:30
ULONG Cylinders
Number of cylinders on the disk.
Definition: disk.h:27
ULONGLONG Sectors
Total number of disk sectors/LBA blocks.
Definition: disk.h:31
ULONG SectorsPerTrack
Number of sectors per track.
Definition: disk.h:29
ULONG Heads
Number of heads on the disk.
Definition: disk.h:28
EXTENDED_GEOMETRY ExtGeometry
Definition: pcdisk.c:99
BOOLEAN Int13ExtensionsSupported
Definition: pcdisk.c:102
GEOMETRY Geometry
Definition: pcdisk.c:98
BOOLEAN IsRemovable
Definition: pcdisk.c:108
void * PVOID
Definition: typedefs.h:50
uint16_t * PUSHORT
Definition: typedefs.h:56
const char * PCSTR
Definition: typedefs.h:52
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
uint64_t ULONGLONG
Definition: typedefs.h:67
#define OUT
Definition: typedefs.h:40
Definition: pcbios.h:159
DWORDREGS x
Definition: pcbios.h:160
BYTEREGS b
Definition: pcbios.h:163
WORDREGS w
Definition: pcbios.h:162
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254
#define BIOSCALLBUFSEGMENT
Definition: x86common.h:21
#define BIOSCALLBUFOFFSET
Definition: x86common.h:22
#define BIOSCALLBUFFER
Definition: x86common.h:12
unsigned char UCHAR
Definition: xmlstorage.h:181
char CHAR
Definition: xmlstorage.h:175