3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
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.
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.
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.
20 // #if defined(__i386__) || defined(_M_AMD64)
25 DBG_DEFAULT_CHANNEL(DISK
);
27 /* Enable this line if you want to support multi-drive caching (increases FreeLdr size!) */
28 // #define CACHE_MULTI_DRIVES
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
44 } I386_DISK_ADDRESS_PACKET
, *PI386_DISK_ADDRESS_PACKET
;
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:
52 * 80h Bootable hard disk
53 * 81h-FFh Nonbootable or no emulation
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)
59 * Drive is slave instead of master
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)
75 } I386_CDROM_SPEC_PACKET
, *PI386_CDROM_SPEC_PACKET
;
79 typedef struct _PC_DISK_DRIVE
81 /* Disk geometry (legacy BIOS and INT13 extended) */
83 EXTENDED_GEOMETRY ExtGeometry
;
85 /* TRUE when INT 13h extensions are supported */
86 BOOLEAN Int13ExtensionsSupported
;
89 * 'IsRemovable' flag: TRUE when the drive is removable (e.g. floppy, CD-ROM...).
90 * In that case some of the cached information might need to be refreshed regularly.
94 #ifdef CACHE_MULTI_DRIVES
96 * 'Initialized' flag: if TRUE then the drive has been initialized;
97 * if FALSE then it needs to be initialized; if its high bit is set
98 * then there has been an error; don't try to use it.
102 } PC_DISK_DRIVE
, *PPC_DISK_DRIVE
;
104 #ifdef CACHE_MULTI_DRIVES
105 /* Cache of all possible PC disk drives */
106 // Maximum number of disks is 0x100, indexed from 0x00 to 0xFF.
107 static PC_DISK_DRIVE PcDiskDrive
[0x100];
109 /* Cached data for the last-accessed PC disk drive */
110 // We use a USHORT so that we can initialize it with a drive number that cannot exist
111 // on the system (they are <= 0xFF), therefore forcing drive caching on first access.
112 static USHORT LastDriveNumber
= 0xFFFF;
113 static PC_DISK_DRIVE PcDiskDrive
;
114 #endif /* CACHE_MULTI_DRIVES */
116 /* DISK IO ERROR SUPPORT *****************************************************/
118 static LONG lReportError
= 0; // >= 0: display errors; < 0: hide errors.
120 LONG
DiskReportError(BOOLEAN bShowError
)
122 /* Set the reference count */
123 if (bShowError
) ++lReportError
;
128 static PCSTR
DiskGetErrorCodeString(ULONG ErrorCode
)
132 case 0x00: return "no error";
133 case 0x01: return "bad command passed to driver";
134 case 0x02: return "address mark not found or bad sector";
135 case 0x03: return "diskette write protect error";
136 case 0x04: return "sector not found";
137 case 0x05: return "fixed disk reset failed";
138 case 0x06: return "diskette changed or removed";
139 case 0x07: return "bad fixed disk parameter table";
140 case 0x08: return "DMA overrun";
141 case 0x09: return "DMA access across 64k boundary";
142 case 0x0A: return "bad fixed disk sector flag";
143 case 0x0B: return "bad fixed disk cylinder";
144 case 0x0C: return "unsupported track/invalid media";
145 case 0x0D: return "invalid number of sectors on fixed disk format";
146 case 0x0E: return "fixed disk controlled data address mark detected";
147 case 0x0F: return "fixed disk DMA arbitration level out of range";
148 case 0x10: return "ECC/CRC error on disk read";
149 case 0x11: return "recoverable fixed disk data error, data fixed by ECC";
150 case 0x20: return "controller error (NEC for floppies)";
151 case 0x40: return "seek failure";
152 case 0x80: return "time out, drive not ready";
153 case 0xAA: return "fixed disk drive not ready";
154 case 0xBB: return "fixed disk undefined error";
155 case 0xCC: return "fixed disk write fault on selected drive";
156 case 0xE0: return "fixed disk status error/Error reg = 0";
157 case 0xFF: return "sense operation failed";
159 default: return "unknown error code";
163 static VOID
DiskError(PCSTR ErrorString
, ULONG ErrorCode
)
165 CHAR ErrorCodeString
[200];
167 if (lReportError
< 0)
170 sprintf(ErrorCodeString
, "%s\n\nError Code: 0x%lx\nError: %s",
171 ErrorString
, ErrorCode
, DiskGetErrorCodeString(ErrorCode
));
173 ERR("%s\n", ErrorCodeString
);
175 UiMessageBox(ErrorCodeString
);
178 /* FUNCTIONS *****************************************************************/
180 BOOLEAN
DiskResetController(UCHAR DriveNumber
)
182 REGS RegsIn
, RegsOut
;
184 WARN("DiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber
);
187 * BIOS Int 13h, function 0 - Reset disk system
189 * DL = drive (if bit 7 is set both hard disks and floppy disks reset)
192 * CF clear if successful
196 RegsIn
.b
.dl
= DriveNumber
;
198 /* Reset the disk controller */
199 Int386(0x13, &RegsIn
, &RegsOut
);
201 return INT386_SUCCESS(RegsOut
);
205 DiskIsDriveRemovable(UCHAR DriveNumber
)
208 * Hard disks use drive numbers >= 0x80 . So if the drive number
209 * indicates a hard disk then return FALSE.
210 * 0x49 is our magic ramdisk drive, so return FALSE for that too.
212 if ((DriveNumber
>= 0x80) || (DriveNumber
== 0x49))
215 /* The drive is a floppy diskette so return TRUE */
220 DiskInt13ExtensionsSupported(IN UCHAR DriveNumber
)
222 REGS RegsIn
, RegsOut
;
225 * Some BIOSes report that extended disk access functions are not supported
226 * when booting from a CD (e.g. Phoenix BIOS v6.00PG and Insyde BIOS shipping
227 * with Intel Macs). Therefore we just return TRUE if we're booting from a CD
228 * - we can assume that all El Torito capable BIOSes support INT 13 extensions.
229 * We simply detect whether we're booting from CD by checking whether the drive
230 * number is >= 0x8A. It's 0x90 on the Insyde BIOS, and 0x9F on most other BIOSes.
232 if (DriveNumber
>= 0x8A)
236 * IBM/MS INT 13 Extensions - INSTALLATION CHECK
239 * DL = drive (80h-FFh)
241 * CF set on error (extensions not supported)
242 * AH = 01h (invalid function)
243 * CF clear if successful
244 * BX = AA55h if installed
245 * AH = major version of extensions
247 * 20h = 2.0 / EDD-1.0
248 * 21h = 2.1 / EDD-1.1
251 * CX = API subset support bitmap
252 * DH = extension version (v2.0+ ??? -- not present in 1.x)
254 * Bitfields for IBM/MS INT 13 Extensions API support bitmap
255 * Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
256 * Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
257 * Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
258 * extended drive parameter table is valid
262 RegsIn
.w
.bx
= 0x55AA;
263 RegsIn
.b
.dl
= DriveNumber
;
265 /* Reset the disk controller */
266 Int386(0x13, &RegsIn
, &RegsOut
);
267 if (!INT386_SUCCESS(RegsOut
))
269 /* CF set on error (extensions not supported) */
273 if (RegsOut
.w
.bx
!= 0xAA55)
275 /* BX = AA55h if installed */
279 if (!(RegsOut
.w
.cx
& 0x0001))
282 * CX = API subset support bitmap.
283 * Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported.
285 WARN("Suspicious API subset support bitmap 0x%x on device 0x%lx\n",
286 RegsOut
.w
.cx
, DriveNumber
);
294 DiskGetExtendedDriveParameters(
295 IN UCHAR DriveNumber
,
296 IN PPC_DISK_DRIVE DiskDrive
,
298 IN USHORT BufferSize
)
300 REGS RegsIn
, RegsOut
;
301 PUSHORT Ptr
= (PUSHORT
)(BIOSCALLBUFFER
);
303 TRACE("DiskGetExtendedDriveParameters(0x%x)\n", DriveNumber
);
305 if (!DiskDrive
->Int13ExtensionsSupported
)
308 /* Initialize transfer buffer */
312 * BIOS Int 13h, function 48h - Get drive parameters
314 * DL = drive (bit 7 set for hard disk)
315 * DS:SI = result buffer
319 * CF clear if successful
321 * DS:SI -> result buffer
324 RegsIn
.b
.dl
= DriveNumber
;
325 RegsIn
.x
.ds
= BIOSCALLBUFSEGMENT
; // DS:SI -> result buffer
326 RegsIn
.w
.si
= BIOSCALLBUFOFFSET
;
328 /* Get drive parameters */
329 Int386(0x13, &RegsIn
, &RegsOut
);
330 if (!INT386_SUCCESS(RegsOut
))
333 RtlCopyMemory(Buffer
, Ptr
, BufferSize
);
336 TRACE("size of buffer: %x\n", Ptr
[0]);
337 TRACE("information flags: %x\n", Ptr
[1]);
338 TRACE("number of physical cylinders on drive: %u\n", *(PULONG
)&Ptr
[2]);
339 TRACE("number of physical heads on drive: %u\n", *(PULONG
)&Ptr
[4]);
340 TRACE("number of physical sectors per track: %u\n", *(PULONG
)&Ptr
[6]);
341 TRACE("total number of sectors on drive: %I64u\n", *(unsigned long long*)&Ptr
[8]);
342 TRACE("bytes per sector: %u\n", Ptr
[12]);
345 TRACE("EED configuration parameters: %x:%x\n", Ptr
[13], Ptr
[14]);
346 if (Ptr
[13] != 0xffff && Ptr
[14] != 0xffff)
348 PUCHAR SpecPtr
= (PUCHAR
)(ULONG_PTR
)((Ptr
[13] << 4) + Ptr
[14]);
349 TRACE("SpecPtr: %x\n", SpecPtr
);
350 TRACE("physical I/O port base address: %x\n", *(PUSHORT
)&SpecPtr
[0]);
351 TRACE("disk-drive control port address: %x\n", *(PUSHORT
)&SpecPtr
[2]);
352 TRACE("drive flags: %x\n", SpecPtr
[4]);
353 TRACE("proprietary information: %x\n", SpecPtr
[5]);
354 TRACE("IRQ for drive: %u\n", SpecPtr
[6]);
355 TRACE("sector count for multi-sector transfers: %u\n", SpecPtr
[7]);
356 TRACE("DMA control: %x\n", SpecPtr
[8]);
357 TRACE("programmed I/O control: %x\n", SpecPtr
[9]);
358 TRACE("drive options: %x\n", *(PUSHORT
)&SpecPtr
[10]);
363 TRACE("signature: %x\n", Ptr
[15]);
372 IN UCHAR DriveNumber
,
373 IN PPC_DISK_DRIVE DiskDrive
)
376 REGS RegsIn
, RegsOut
;
379 /* Get the extended geometry first */
380 DiskDrive
->ExtGeometry
.Size
= sizeof(DiskDrive
->ExtGeometry
);
381 Success
= DiskGetExtendedDriveParameters(DriveNumber
, DiskDrive
,
382 &DiskDrive
->ExtGeometry
,
383 DiskDrive
->ExtGeometry
.Size
);
386 /* Failed, zero it out */
387 RtlZeroMemory(&DiskDrive
->ExtGeometry
, sizeof(DiskDrive
->ExtGeometry
));
391 TRACE("DiskGetExtendedDriveParameters(0x%x) returned:\n"
394 "Sects/Track: 0x%x\n"
395 "Bytes/Sect : 0x%x\n",
397 DiskDrive
->ExtGeometry
.Cylinders
,
398 DiskDrive
->ExtGeometry
.Heads
,
399 DiskDrive
->ExtGeometry
.SectorsPerTrack
,
400 DiskDrive
->ExtGeometry
.BytesPerSector
);
403 /* Now try the legacy geometry */
404 RtlZeroMemory(&DiskDrive
->Geometry
, sizeof(DiskDrive
->Geometry
));
407 * BIOS Int 13h, function 08h - Get drive parameters
409 * DL = drive (bit 7 set for hard disk)
410 * ES:DI = 0000h:0000h to guard against BIOS bugs
414 * CF clear if successful
416 * AL = 00h on at least some BIOSes
417 * BL = drive type (AT/PS2 floppies only)
418 * CH = low eight bits of maximum cylinder number
419 * CL = maximum sector number (bits 5-0)
420 * high two bits of maximum cylinder number (bits 7-6)
421 * DH = maximum head number
422 * DL = number of drives
423 * ES:DI -> drive parameter table (floppies only)
426 RegsIn
.b
.dl
= DriveNumber
;
427 RegsIn
.w
.es
= 0x0000;
428 RegsIn
.w
.di
= 0x0000;
430 /* Get drive parameters */
431 Int386(0x13, &RegsIn
, &RegsOut
);
432 if (!INT386_SUCCESS(RegsOut
))
434 /* We failed, return the result of the previous call (extended geometry) */
437 /* OR it with the old result, so that we return TRUE whenever either call succeeded */
440 Cylinders
= (RegsOut
.b
.cl
& 0xC0) << 2;
441 Cylinders
+= RegsOut
.b
.ch
;
443 DiskDrive
->Geometry
.Cylinders
= Cylinders
;
444 DiskDrive
->Geometry
.Heads
= RegsOut
.b
.dh
+ 1;
445 DiskDrive
->Geometry
.Sectors
= RegsOut
.b
.cl
& 0x3F;
446 DiskDrive
->Geometry
.BytesPerSector
= 512; /* Just assume 512 bytes per sector */
448 TRACE("Regular Int13h(0x%x) returned:\n"
451 "Sects/Track: 0x%x (original 0x%x)\n"
452 "Bytes/Sect : 0x%x\n",
454 DiskDrive
->Geometry
.Cylinders
,
455 DiskDrive
->Geometry
.Heads
,
456 DiskDrive
->Geometry
.Sectors
, RegsOut
.b
.cl
,
457 DiskDrive
->Geometry
.BytesPerSector
);
464 IN UCHAR DriveNumber
,
465 IN OUT PPC_DISK_DRIVE DiskDrive
)
467 DiskDrive
->IsRemovable
= DiskIsDriveRemovable(DriveNumber
);
470 * Check to see if it is a fixed disk drive.
471 * If so then check to see if INT 13h extensions work.
472 * If they do then use them, otherwise default back to BIOS calls.
474 DiskDrive
->Int13ExtensionsSupported
= DiskInt13ExtensionsSupported(DriveNumber
);
476 if (!InitDriveGeometry(DriveNumber
, DiskDrive
))
480 "DriveNumber: 0x%x\n"
482 "Int13ExtensionsSupported = %s\n",
484 DiskDrive
->IsRemovable
? "TRUE" : "FALSE",
485 DiskDrive
->Int13ExtensionsSupported
? "TRUE" : "FALSE");
492 PcDiskDriveNumberToDrive(IN UCHAR DriveNumber
)
494 #ifdef CACHE_MULTI_DRIVES
495 PPC_DISK_DRIVE DiskDrive
;
497 ASSERT((0 <= DriveNumber
) && (DriveNumber
< RTL_NUMBER_OF(PcDiskDrive
)));
499 /* Retrieve a slot */
500 DiskDrive
= &PcDiskDrive
[DriveNumber
];
502 /* If the drive has not been initialized before... */
503 if (!DiskDrive
->Initialized
)
505 /* ... try to initialize it now. */
506 if (!PcDiskDriveInit(DriveNumber
, DiskDrive
))
509 * If we failed, there is no drive at this number
510 * and flag it as such (set its high bit).
512 DiskDrive
->Initialized
|= 0x80;
515 DiskDrive
->Initialized
= TRUE
;
517 else if (DiskDrive
->Initialized
& 0x80)
520 * The disk failed to be initialized previously, reset its flag to give
521 * it chance to be initialized again later, but just fail for the moment.
523 DiskDrive
->Initialized
= FALSE
;
529 static PC_DISK_DRIVE NewDiskDrive
;
531 ASSERT((0 <= DriveNumber
) && (DriveNumber
<= 0xFF));
533 /* Update cached information */
535 /* If the drive has not been accessed last before... */
536 if ((USHORT
)DriveNumber
!= LastDriveNumber
)
538 /* ... try to (re-)initialize and cache it now. */
539 RtlZeroMemory(&NewDiskDrive
, sizeof(NewDiskDrive
));
540 if (!PcDiskDriveInit(DriveNumber
, &NewDiskDrive
))
543 * If we failed, there is no drive at this number.
544 * Keep the last-accessed valid drive cached.
548 /* We succeeded, cache the drive data */
549 PcDiskDrive
= NewDiskDrive
;
550 LastDriveNumber
= (USHORT
)DriveNumber
;
554 #endif /* CACHE_MULTI_DRIVES */
558 PcDiskReadLogicalSectorsLBA(
559 IN UCHAR DriveNumber
,
560 IN ULONGLONG SectorNumber
,
561 IN ULONG SectorCount
,
564 REGS RegsIn
, RegsOut
;
566 PI386_DISK_ADDRESS_PACKET Packet
= (PI386_DISK_ADDRESS_PACKET
)(BIOSCALLBUFFER
);
568 /* Setup disk address packet */
569 RtlZeroMemory(Packet
, sizeof(*Packet
));
570 Packet
->PacketSize
= sizeof(*Packet
);
571 Packet
->Reserved
= 0;
572 Packet
->LBABlockCount
= (USHORT
)SectorCount
;
573 ASSERT(Packet
->LBABlockCount
== SectorCount
);
574 Packet
->TransferBufferOffset
= ((ULONG_PTR
)Buffer
) & 0x0F;
575 Packet
->TransferBufferSegment
= (USHORT
)(((ULONG_PTR
)Buffer
) >> 4);
576 Packet
->LBAStartBlock
= SectorNumber
;
579 * BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
581 * CF clear if successful
585 * Disk address packet's block count field set to the
586 * number of blocks successfully transferred.
588 RegsIn
.b
.ah
= 0x42; // Subfunction 42h
589 RegsIn
.b
.dl
= DriveNumber
; // Drive number in DL (0 - floppy, 0x80 - harddisk)
590 RegsIn
.x
.ds
= BIOSCALLBUFSEGMENT
; // DS:SI -> disk address packet
591 RegsIn
.w
.si
= BIOSCALLBUFOFFSET
;
594 for (RetryCount
= 0; RetryCount
< 3; ++RetryCount
)
596 Int386(0x13, &RegsIn
, &RegsOut
);
598 /* If it worked return TRUE */
599 if (INT386_SUCCESS(RegsOut
))
603 /* If it was a corrected ECC error then the data is still good */
604 else if (RegsOut
.b
.ah
== 0x11)
608 /* If it failed then do the next retry */
611 DiskResetController(DriveNumber
);
616 /* If we get here then the read failed */
617 DiskError("Disk Read Failed in LBA mode", RegsOut
.b
.ah
);
618 ERR("Disk Read Failed in LBA mode: %x (%s) (DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d)\n",
619 RegsOut
.b
.ah
, DiskGetErrorCodeString(RegsOut
.b
.ah
),
620 DriveNumber
, SectorNumber
, SectorCount
);
626 PcDiskReadLogicalSectorsCHS(
627 IN UCHAR DriveNumber
,
628 IN PPC_DISK_DRIVE DiskDrive
,
629 IN ULONGLONG SectorNumber
,
630 IN ULONG SectorCount
,
633 UCHAR PhysicalSector
;
636 GEOMETRY DriveGeometry
;
637 ULONG NumberOfSectorsToRead
;
638 REGS RegsIn
, RegsOut
;
641 DriveGeometry
= DiskDrive
->Geometry
;
642 if (DriveGeometry
.Sectors
== 0 || DriveGeometry
.Heads
== 0)
645 while (SectorCount
> 0)
648 * Calculate the physical disk offsets.
649 * Note: DriveGeometry.Sectors < 64
651 PhysicalSector
= 1 + (UCHAR
)(SectorNumber
% DriveGeometry
.Sectors
);
652 PhysicalHead
= (UCHAR
)((SectorNumber
/ DriveGeometry
.Sectors
) % DriveGeometry
.Heads
);
653 PhysicalTrack
= (ULONG
)((SectorNumber
/ DriveGeometry
.Sectors
) / DriveGeometry
.Heads
);
655 /* Calculate how many sectors we need to read this round */
656 if (PhysicalSector
> 1)
658 if (SectorCount
>= (DriveGeometry
.Sectors
- (PhysicalSector
- 1)))
659 NumberOfSectorsToRead
= (DriveGeometry
.Sectors
- (PhysicalSector
- 1));
661 NumberOfSectorsToRead
= SectorCount
;
665 if (SectorCount
>= DriveGeometry
.Sectors
)
666 NumberOfSectorsToRead
= DriveGeometry
.Sectors
;
668 NumberOfSectorsToRead
= SectorCount
;
671 /* Make sure the read is within the geometry boundaries */
672 if ((PhysicalHead
>= DriveGeometry
.Heads
) ||
673 (PhysicalTrack
>= DriveGeometry
.Cylinders
) ||
674 ((NumberOfSectorsToRead
+ PhysicalSector
) > (DriveGeometry
.Sectors
+ 1)) ||
675 (PhysicalSector
> DriveGeometry
.Sectors
))
677 DiskError("Disk read exceeds drive geometry limits.", 0);
682 * BIOS Int 13h, function 2 - Read Disk Sectors
684 * AL = number of sectors to read (must be nonzero)
685 * CH = low eight bits of cylinder number
686 * CL = sector number 1-63 (bits 0-5)
687 * high two bits of cylinder (bits 6-7, hard disk only)
689 * DL = drive number (bit 7 set for hard disk)
690 * ES:BX -> data buffer
693 * if AH = 11h (corrected ECC error), AL = burst length
694 * CF clear if successful
696 * AL = number of sectors transferred
697 * (only valid if CF set for some BIOSes)
700 RegsIn
.b
.al
= (UCHAR
)NumberOfSectorsToRead
;
701 RegsIn
.b
.ch
= (PhysicalTrack
& 0xFF);
702 RegsIn
.b
.cl
= (UCHAR
)(PhysicalSector
+ ((PhysicalTrack
& 0x300) >> 2));
703 RegsIn
.b
.dh
= PhysicalHead
;
704 RegsIn
.b
.dl
= DriveNumber
;
705 RegsIn
.w
.es
= (USHORT
)(((ULONG_PTR
)Buffer
) >> 4);
706 RegsIn
.w
.bx
= ((ULONG_PTR
)Buffer
) & 0x0F;
708 /* Perform the read. Retry 3 times. */
709 for (RetryCount
= 0; RetryCount
< 3; ++RetryCount
)
711 Int386(0x13, &RegsIn
, &RegsOut
);
713 /* If it worked break out */
714 if (INT386_SUCCESS(RegsOut
))
718 /* If it was a corrected ECC error then the data is still good */
719 else if (RegsOut
.b
.ah
== 0x11)
723 /* If it failed then do the next retry */
726 DiskResetController(DriveNumber
);
731 /* If we retried 3 times then fail */
734 DiskError("Disk Read Failed in CHS mode, after retrying 3 times", RegsOut
.b
.ah
);
735 ERR("Disk Read Failed in CHS mode, after retrying 3 times: %x (%s) (DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d)\n",
736 RegsOut
.b
.ah
, DiskGetErrorCodeString(RegsOut
.b
.ah
),
737 DriveNumber
, SectorNumber
, SectorCount
);
742 * I have learned that not all BIOSes return
743 * the sector read count in the AL register (at least mine doesn't)
744 * even if the sectors were read correctly. So instead
745 * of checking the sector read count we will rely solely
746 * on the carry flag being set on error.
749 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ (NumberOfSectorsToRead
* DriveGeometry
.BytesPerSector
));
750 SectorCount
-= NumberOfSectorsToRead
;
751 SectorNumber
+= NumberOfSectorsToRead
;
758 PcDiskReadLogicalSectors(
759 IN UCHAR DriveNumber
,
760 IN ULONGLONG SectorNumber
,
761 IN ULONG SectorCount
,
764 PPC_DISK_DRIVE DiskDrive
;
766 TRACE("PcDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n",
767 DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
769 /* 16-bit BIOS addressing limitation */
770 ASSERT(((ULONG_PTR
)Buffer
) <= 0xFFFFF);
772 DiskDrive
= PcDiskDriveNumberToDrive(DriveNumber
);
776 if ((DriveNumber
>= 0x80) && DiskDrive
->Int13ExtensionsSupported
)
778 /* LBA is easy, nothing to calculate. Just do the read. */
779 TRACE("--> Using LBA\n");
780 return PcDiskReadLogicalSectorsLBA(DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
784 /* LBA is not supported, default to CHS */
785 TRACE("--> Using CHS\n");
786 return PcDiskReadLogicalSectorsCHS(DriveNumber
, DiskDrive
, SectorNumber
, SectorCount
, Buffer
);
790 #if defined(__i386__) || defined(_M_AMD64)
791 VOID __cdecl
DiskStopFloppyMotor(VOID
)
793 WRITE_PORT_UCHAR((PUCHAR
)0x3F2, 0x0C); // DOR_FDC_ENABLE | DOR_DMA_IO_INTERFACE_ENABLE
795 #endif // defined __i386__ || defined(_M_AMD64)
798 PcDiskGetDriveGeometry(UCHAR DriveNumber
, PGEOMETRY Geometry
)
800 PPC_DISK_DRIVE DiskDrive
;
802 TRACE("PcDiskGetDriveGeometry(0x%x)\n", DriveNumber
);
804 DiskDrive
= PcDiskDriveNumberToDrive(DriveNumber
);
808 /* Try to get the extended geometry first */
809 if (DiskDrive
->ExtGeometry
.Size
== sizeof(DiskDrive
->ExtGeometry
))
811 /* Extended geometry has been initialized, return it */
812 Geometry
->Cylinders
= DiskDrive
->ExtGeometry
.Cylinders
;
813 Geometry
->Heads
= DiskDrive
->ExtGeometry
.Heads
;
814 Geometry
->Sectors
= DiskDrive
->ExtGeometry
.SectorsPerTrack
;
815 Geometry
->BytesPerSector
= DiskDrive
->ExtGeometry
.BytesPerSector
;
818 /* Fall back to legacy BIOS geometry */
820 *Geometry
= DiskDrive
->Geometry
;
827 PcDiskGetCacheableBlockCount(UCHAR DriveNumber
)
829 PPC_DISK_DRIVE DiskDrive
;
831 DiskDrive
= PcDiskDriveNumberToDrive(DriveNumber
);
833 return 1; // Unknown count.
836 * If LBA is supported then the block size will be 64 sectors (32k).
837 * If not then the block size is the size of one track.
839 if (DiskDrive
->Int13ExtensionsSupported
)
842 return DiskDrive
->Geometry
.Sectors
;