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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 U8 PacketSize
; // 00h - Size of packet (10h or 18h)
31 U8 Reserved
; // 01h - Reserved (0)
32 U16 LBABlockCount
; // 02h - Number of blocks to transfer (max 007Fh for Phoenix EDD)
33 U16 TransferBufferOffset
; // 04h - Transfer buffer offset (seg:off)
34 U16 TransferBufferSegment
; // Transfer buffer segment (seg:off)
35 U64 LBAStartBlock
; // 08h - Starting absolute block number
36 U64 TransferBuffer64
; // 10h - (EDD-3.0, optional) 64-bit flat address of transfer buffer
37 // used if DWORD at 04h is FFFFh:FFFFh
38 } PACKED I386_DISK_ADDRESS_PACKET
, *PI386_DISK_ADDRESS_PACKET
;
40 /////////////////////////////////////////////////////////////////////////////////////////////
42 /////////////////////////////////////////////////////////////////////////////////////////////
46 BOOL
DiskReadLogicalSectors(U32 DriveNumber
, U64 SectorNumber
, U32 SectorCount
, PVOID Buffer
)
49 DbgPrint((DPRINT_DISK
, "DiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber
, SectorNumber
, SectorCount
, Buffer
));
52 // Check to see if it is a fixed disk drive
53 // If so then check to see if Int13 extensions work
54 // If they do then use them, otherwise default back to BIOS calls
56 if ((DriveNumber
>= 0x80) && DiskInt13ExtensionsSupported(DriveNumber
))
58 DbgPrint((DPRINT_DISK
, "Using Int 13 Extensions for read. DiskInt13ExtensionsSupported(%d) = %s\n", DriveNumber
, DiskInt13ExtensionsSupported(DriveNumber
) ? "TRUE" : "FALSE"));
61 // LBA is easy, nothing to calculate
64 return DiskReadLogicalSectorsLBA(DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
68 // LBA is not supported default to the CHS calls
69 return DiskReadLogicalSectorsCHS(DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
75 BOOL
DiskReadLogicalSectorsLBA(U32 DriveNumber
, U64 SectorNumber
, U32 SectorCount
, PVOID Buffer
)
80 PI386_DISK_ADDRESS_PACKET Packet
= (PI386_DISK_ADDRESS_PACKET
)(BIOSCALLBUFFER
);
82 DbgPrint((DPRINT_DISK
, "DiskReadLogicalSectorsLBA() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber
, SectorNumber
, SectorCount
, Buffer
));
84 // BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
85 RegsIn
.b
.ah
= 0x42; // Subfunction 42h
86 RegsIn
.b
.dl
= DriveNumber
; // Drive number in DL (0 - floppy, 0x80 - harddisk)
87 RegsIn
.x
.ds
= BIOSCALLBUFSEGMENT
; // DS:SI -> disk address packet
88 RegsIn
.w
.si
= BIOSCALLBUFOFFSET
;
90 // Setup disk address packet
91 RtlZeroMemory(Packet
, sizeof(I386_DISK_ADDRESS_PACKET
));
92 Packet
->PacketSize
= sizeof(I386_DISK_ADDRESS_PACKET
);
94 Packet
->LBABlockCount
= SectorCount
;
95 Packet
->TransferBufferOffset
= ((U32
)Buffer
) & 0x0F;
96 Packet
->TransferBufferSegment
= ((U32
)Buffer
) >> 4;
97 Packet
->LBAStartBlock
= SectorNumber
;
98 Packet
->TransferBuffer64
= 0;
100 // BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
102 // CF clear if successful
106 // disk address packet's block count field set to the
107 // number of blocks successfully transferred
110 for (RetryCount
=0; RetryCount
<3; RetryCount
++)
112 Int386(0x13, &RegsIn
, &RegsOut
);
114 // If it worked return TRUE
115 if (INT386_SUCCESS(RegsOut
))
119 // If it was a corrected ECC error then the data is still good
120 else if (RegsOut
.b
.ah
== 0x11)
124 // If it failed the do the next retry
127 DiskResetController(DriveNumber
);
133 // If we get here then the read failed
134 DiskError("Disk Read Failed", RegsOut
.b
.ah
);
139 BOOL
DiskReadLogicalSectorsCHS(U32 DriveNumber
, U64 SectorNumber
, U32 SectorCount
, PVOID Buffer
)
144 GEOMETRY DriveGeometry
;
145 U32 NumberOfSectorsToRead
;
150 DbgPrint((DPRINT_DISK
, "DiskReadLogicalSectorsCHS()\n"));
153 // Get the drive geometry
155 if (!DiskGetDriveGeometry(DriveNumber
, &DriveGeometry
))
164 // Calculate the physical disk offsets
166 PhysicalSector
= 1 + (SectorNumber
% DriveGeometry
.Sectors
);
167 PhysicalHead
= (SectorNumber
/ DriveGeometry
.Sectors
) % DriveGeometry
.Heads
;
168 PhysicalTrack
= (SectorNumber
/ DriveGeometry
.Sectors
) / DriveGeometry
.Heads
;
171 // Calculate how many sectors we need to read this round
173 if (PhysicalSector
> 1)
175 if (SectorCount
>= (DriveGeometry
.Sectors
- (PhysicalSector
- 1)))
176 NumberOfSectorsToRead
= (DriveGeometry
.Sectors
- (PhysicalSector
- 1));
178 NumberOfSectorsToRead
= SectorCount
;
182 if (SectorCount
>= DriveGeometry
.Sectors
)
183 NumberOfSectorsToRead
= DriveGeometry
.Sectors
;
185 NumberOfSectorsToRead
= SectorCount
;
189 // Make sure the read is within the geometry boundaries
191 if ((PhysicalHead
>= DriveGeometry
.Heads
) ||
192 (PhysicalTrack
>= DriveGeometry
.Cylinders
) ||
193 ((NumberOfSectorsToRead
+ PhysicalSector
) > (DriveGeometry
.Sectors
+ 1)) ||
194 (PhysicalSector
> DriveGeometry
.Sectors
))
196 DiskError("Disk read exceeds drive geometry limits.", 0);
200 // BIOS Int 13h, function 2 - Read Disk Sectors
202 // AL = number of sectors to read (must be nonzero)
203 // CH = low eight bits of cylinder number
204 // CL = sector number 1-63 (bits 0-5)
205 // high two bits of cylinder (bits 6-7, hard disk only)
207 // DL = drive number (bit 7 set for hard disk)
208 // ES:BX -> data buffer
211 // if AH = 11h (corrected ECC error), AL = burst length
212 // CF clear if successful
214 // AL = number of sectors transferred
215 // (only valid if CF set for some BIOSes)
217 RegsIn
.b
.al
= NumberOfSectorsToRead
;
218 RegsIn
.b
.ch
= (PhysicalTrack
& 0xFF);
219 RegsIn
.b
.cl
= (PhysicalSector
+ ((PhysicalTrack
& 0x300) >> 2));
220 RegsIn
.b
.dh
= PhysicalHead
;
221 RegsIn
.b
.dl
= DriveNumber
;
222 RegsIn
.w
.es
= ((U32
)Buffer
) >> 4;
223 RegsIn
.w
.bx
= ((U32
)Buffer
) & 0x0F;
229 for (RetryCount
=0; RetryCount
<3; RetryCount
++)
231 Int386(0x13, &RegsIn
, &RegsOut
);
233 // If it worked break out
234 if (INT386_SUCCESS(RegsOut
))
238 // If it was a corrected ECC error then the data is still good
239 else if (RegsOut
.b
.ah
== 0x11)
243 // If it failed the do the next retry
246 DiskResetController(DriveNumber
);
252 // If we retried 3 times then fail
255 DiskError("Disk Read Failed", RegsOut
.b
.ah
);
259 // I have learned that not all bioses return
260 // the sector read count in the AL register (at least mine doesn't)
261 // even if the sectors were read correctly. So instead
262 // of checking the sector read count we will rely solely
263 // on the carry flag being set on error
265 Buffer
+= (NumberOfSectorsToRead
* DriveGeometry
.BytesPerSector
);
266 SectorCount
-= NumberOfSectorsToRead
;
267 SectorNumber
+= NumberOfSectorsToRead
;
273 BOOL
DiskResetController(U32 DriveNumber
)
278 DbgPrint((DPRINT_DISK
, "DiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber
));
280 // BIOS Int 13h, function 0 - Reset disk system
282 // DL = drive (if bit 7 is set both hard disks and floppy disks reset)
285 // CF clear if successful
288 RegsIn
.b
.dl
= DriveNumber
;
290 // Reset the disk controller
291 Int386(0x13, &RegsIn
, &RegsOut
);
293 return INT386_SUCCESS(RegsOut
);
296 BOOL
DiskInt13ExtensionsSupported(U32 DriveNumber
)
301 DbgPrint((DPRINT_DISK
, "DiskInt13ExtensionsSupported()\n"));
303 // IBM/MS INT 13 Extensions - INSTALLATION CHECK
306 // DL = drive (80h-FFh)
308 // CF set on error (extensions not supported)
309 // AH = 01h (invalid function)
310 // CF clear if successful
311 // BX = AA55h if installed
312 // AH = major version of extensions
314 // 20h = 2.0 / EDD-1.0
315 // 21h = 2.1 / EDD-1.1
318 // CX = API subset support bitmap
319 // DH = extension version (v2.0+ ??? -- not present in 1.x)
321 // Bitfields for IBM/MS INT 13 Extensions API support bitmap
322 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
323 // Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
324 // Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
325 // extended drive parameter table is valid
326 // Bits 3-15 reserved
328 RegsIn
.w
.bx
= 0x55AA;
329 RegsIn
.b
.dl
= DriveNumber
;
331 // Reset the disk controller
332 Int386(0x13, &RegsIn
, &RegsOut
);
334 if (!INT386_SUCCESS(RegsOut
))
336 // CF set on error (extensions not supported)
340 if (RegsOut
.w
.bx
!= 0xAA55)
342 // BX = AA55h if installed
347 // The original check is too strict because some BIOSes report that
348 // extended disk access functions are not suported when booting
349 // from a CD (e.g. Phoenix BIOS v6.00PG). Argh!
351 if (!(RegsOut
.w
.cx
& 0x0001))
353 // CX = API subset support bitmap
354 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
359 // Use this relaxed check instead
360 if (RegsOut
.w
.cx
== 0x0000)
362 // CX = API subset support bitmap
369 VOID
DiskStopFloppyMotor(VOID
)
371 WRITE_PORT_UCHAR((PUCHAR
)0x3F2, 0);
374 BOOL
DiskGetDriveParameters(U32 DriveNumber
, PGEOMETRY Geometry
)
380 DbgPrint((DPRINT_DISK
, "DiskGetDriveParameters()\n"));
382 // BIOS Int 13h, function 08h - Get drive parameters
384 // DL = drive (bit 7 set for hard disk)
385 // ES:DI = 0000h:0000h to guard against BIOS bugs
389 // CF clear if successful
391 // AL = 00h on at least some BIOSes
392 // BL = drive type (AT/PS2 floppies only)
393 // CH = low eight bits of maximum cylinder number
394 // CL = maximum sector number (bits 5-0)
395 // high two bits of maximum cylinder number (bits 7-6)
396 // DH = maximum head number
397 // DL = number of drives
398 // ES:DI -> drive parameter table (floppies only)
400 RegsIn
.b
.dl
= DriveNumber
;
401 RegsIn
.w
.es
= 0x0000;
402 RegsIn
.w
.di
= 0x0000;
404 // Get drive parameters
405 Int386(0x13, &RegsIn
, &RegsOut
);
407 if (!INT386_SUCCESS(RegsOut
))
412 Cylinders
= (RegsOut
.b
.cl
& 0xC0) << 2;
413 Cylinders
+= RegsOut
.b
.ch
;
415 Geometry
->Cylinders
= Cylinders
;
416 Geometry
->Heads
= RegsOut
.b
.dh
+ 1;
417 Geometry
->Sectors
= RegsOut
.b
.cl
& 0x3F;
418 Geometry
->BytesPerSector
= 512; // Just assume 512 bytes per sector
423 BOOL
DiskGetExtendedDriveParameters(U32 DriveNumber
, PVOID Buffer
, U16 BufferSize
)
427 PU16 Ptr
= (PU16
)(BIOSCALLBUFFER
);
429 DbgPrint((DPRINT_DISK
, "DiskGetExtendedDriveParameters()\n"));
431 // Initialize transfer buffer
434 // BIOS Int 13h, function 48h - Get drive parameters
436 // DL = drive (bit 7 set for hard disk)
437 // DS:SI = result buffer
441 // CF clear if successful
443 // DS:SI -> result buffer
445 RegsIn
.b
.dl
= DriveNumber
;
446 RegsIn
.x
.ds
= BIOSCALLBUFSEGMENT
; // DS:SI -> result buffer
447 RegsIn
.w
.si
= BIOSCALLBUFOFFSET
;
449 // Get drive parameters
450 Int386(0x13, &RegsIn
, &RegsOut
);
452 if (!INT386_SUCCESS(RegsOut
))
457 memcpy(Buffer
, Ptr
, BufferSize
);
464 U32
DiskGetCacheableBlockCount(U32 DriveNumber
)
468 // Get the disk geometry
469 // If this fails then we will just return 1 sector to be safe
470 if (!DiskGetDriveParameters(DriveNumber
, &Geometry
))
475 // If LBA is supported then the block size will be 64 sectors (32k)
476 // If not then the block size is the size of one track
477 if (DiskInt13ExtensionsSupported(DriveNumber
))
483 return Geometry
.Sectors
;
487 #endif // defined __i386__