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.
28 UCHAR PacketSize
; // 00h - Size of packet (10h or 18h)
29 UCHAR Reserved
; // 01h - Reserved (0)
30 USHORT LBABlockCount
; // 02h - Number of blocks to transfer (max 007Fh for Phoenix EDD)
31 USHORT TransferBufferOffset
; // 04h - Transfer buffer offset (seg:off)
32 USHORT TransferBufferSegment
; // Transfer buffer segment (seg:off)
33 ULONGLONG LBAStartBlock
; // 08h - Starting absolute block number
34 //ULONGLONG TransferBuffer64; // 10h - (EDD-3.0, optional) 64-bit flat address of transfer buffer
35 // used if DWORD at 04h is FFFFh:FFFFh
36 // Commented since some earlier BIOSes refuse to work with
37 // such extended structure
38 } I386_DISK_ADDRESS_PACKET
, *PI386_DISK_ADDRESS_PACKET
;
41 /////////////////////////////////////////////////////////////////////////////////////////////
43 /////////////////////////////////////////////////////////////////////////////////////////////
45 static BOOLEAN
PcDiskResetController(ULONG DriveNumber
)
50 DPRINTM(DPRINT_DISK
, "PcDiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber
);
52 // BIOS Int 13h, function 0 - Reset disk system
54 // DL = drive (if bit 7 is set both hard disks and floppy disks reset)
57 // CF clear if successful
60 RegsIn
.b
.dl
= DriveNumber
;
62 // Reset the disk controller
63 Int386(0x13, &RegsIn
, &RegsOut
);
65 return INT386_SUCCESS(RegsOut
);
68 static BOOLEAN
PcDiskReadLogicalSectorsLBA(ULONG DriveNumber
, ULONGLONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
73 PI386_DISK_ADDRESS_PACKET Packet
= (PI386_DISK_ADDRESS_PACKET
)(BIOSCALLBUFFER
);
75 DPRINTM(DPRINT_DISK
, "PcDiskReadLogicalSectorsLBA() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
77 // BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
78 RegsIn
.b
.ah
= 0x42; // Subfunction 42h
79 RegsIn
.b
.dl
= DriveNumber
; // Drive number in DL (0 - floppy, 0x80 - harddisk)
80 RegsIn
.x
.ds
= BIOSCALLBUFSEGMENT
; // DS:SI -> disk address packet
81 RegsIn
.w
.si
= BIOSCALLBUFOFFSET
;
83 // Setup disk address packet
84 RtlZeroMemory(Packet
, sizeof(I386_DISK_ADDRESS_PACKET
));
85 Packet
->PacketSize
= sizeof(I386_DISK_ADDRESS_PACKET
);
87 Packet
->LBABlockCount
= SectorCount
;
88 Packet
->TransferBufferOffset
= ((ULONG_PTR
)Buffer
) & 0x0F;
89 Packet
->TransferBufferSegment
= ((ULONG_PTR
)Buffer
) >> 4;
90 Packet
->LBAStartBlock
= SectorNumber
;
92 // BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
94 // CF clear if successful
98 // disk address packet's block count field set to the
99 // number of blocks successfully transferred
102 for (RetryCount
=0; RetryCount
<3; RetryCount
++)
104 Int386(0x13, &RegsIn
, &RegsOut
);
106 // If it worked return TRUE
107 if (INT386_SUCCESS(RegsOut
))
111 // If it was a corrected ECC error then the data is still good
112 else if (RegsOut
.b
.ah
== 0x11)
116 // If it failed the do the next retry
119 PcDiskResetController(DriveNumber
);
125 // If we get here then the read failed
126 DiskError("Disk Read Failed in LBA mode", RegsOut
.b
.ah
);
131 static BOOLEAN
PcDiskReadLogicalSectorsCHS(ULONG DriveNumber
, ULONGLONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
133 ULONG PhysicalSector
;
136 GEOMETRY DriveGeometry
;
137 ULONG NumberOfSectorsToRead
;
142 DPRINTM(DPRINT_DISK
, "PcDiskReadLogicalSectorsCHS()\n");
145 // Get the drive geometry
147 if (!MachDiskGetDriveGeometry(DriveNumber
, &DriveGeometry
) ||
148 DriveGeometry
.Sectors
== 0 ||
149 DriveGeometry
.Heads
== 0)
158 // Calculate the physical disk offsets
160 PhysicalSector
= 1 + (SectorNumber
% DriveGeometry
.Sectors
);
161 PhysicalHead
= (SectorNumber
/ DriveGeometry
.Sectors
) % DriveGeometry
.Heads
;
162 PhysicalTrack
= (SectorNumber
/ DriveGeometry
.Sectors
) / DriveGeometry
.Heads
;
165 // Calculate how many sectors we need to read this round
167 if (PhysicalSector
> 1)
169 if (SectorCount
>= (DriveGeometry
.Sectors
- (PhysicalSector
- 1)))
170 NumberOfSectorsToRead
= (DriveGeometry
.Sectors
- (PhysicalSector
- 1));
172 NumberOfSectorsToRead
= SectorCount
;
176 if (SectorCount
>= DriveGeometry
.Sectors
)
177 NumberOfSectorsToRead
= DriveGeometry
.Sectors
;
179 NumberOfSectorsToRead
= SectorCount
;
183 // Make sure the read is within the geometry boundaries
185 if ((PhysicalHead
>= DriveGeometry
.Heads
) ||
186 (PhysicalTrack
>= DriveGeometry
.Cylinders
) ||
187 ((NumberOfSectorsToRead
+ PhysicalSector
) > (DriveGeometry
.Sectors
+ 1)) ||
188 (PhysicalSector
> DriveGeometry
.Sectors
))
190 DiskError("Disk read exceeds drive geometry limits.", 0);
194 // BIOS Int 13h, function 2 - Read Disk Sectors
196 // AL = number of sectors to read (must be nonzero)
197 // CH = low eight bits of cylinder number
198 // CL = sector number 1-63 (bits 0-5)
199 // high two bits of cylinder (bits 6-7, hard disk only)
201 // DL = drive number (bit 7 set for hard disk)
202 // ES:BX -> data buffer
205 // if AH = 11h (corrected ECC error), AL = burst length
206 // CF clear if successful
208 // AL = number of sectors transferred
209 // (only valid if CF set for some BIOSes)
211 RegsIn
.b
.al
= NumberOfSectorsToRead
;
212 RegsIn
.b
.ch
= (PhysicalTrack
& 0xFF);
213 RegsIn
.b
.cl
= (PhysicalSector
+ ((PhysicalTrack
& 0x300) >> 2));
214 RegsIn
.b
.dh
= PhysicalHead
;
215 RegsIn
.b
.dl
= DriveNumber
;
216 RegsIn
.w
.es
= ((ULONG_PTR
)Buffer
) >> 4;
217 RegsIn
.w
.bx
= ((ULONG_PTR
)Buffer
) & 0x0F;
223 for (RetryCount
=0; RetryCount
<3; RetryCount
++)
225 Int386(0x13, &RegsIn
, &RegsOut
);
227 // If it worked break out
228 if (INT386_SUCCESS(RegsOut
))
232 // If it was a corrected ECC error then the data is still good
233 else if (RegsOut
.b
.ah
== 0x11)
237 // If it failed the do the next retry
240 PcDiskResetController(DriveNumber
);
246 // If we retried 3 times then fail
249 DiskError("Disk Read Failed in CHS mode, after retrying 3 times", RegsOut
.b
.ah
);
253 // I have learned that not all bioses return
254 // the sector read count in the AL register (at least mine doesn't)
255 // even if the sectors were read correctly. So instead
256 // of checking the sector read count we will rely solely
257 // on the carry flag being set on error
259 Buffer
= (PVOID
)((ULONG_PTR
)Buffer
+ (NumberOfSectorsToRead
* DriveGeometry
.BytesPerSector
));
260 SectorCount
-= NumberOfSectorsToRead
;
261 SectorNumber
+= NumberOfSectorsToRead
;
267 BOOLEAN
PcDiskReadLogicalSectors(ULONG DriveNumber
, ULONGLONG SectorNumber
, ULONG SectorCount
, PVOID Buffer
)
270 DPRINTM(DPRINT_DISK
, "PcDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
273 // Check to see if it is a fixed disk drive
274 // If so then check to see if Int13 extensions work
275 // If they do then use them, otherwise default back to BIOS calls
277 if ((DriveNumber
>= 0x80) && DiskInt13ExtensionsSupported(DriveNumber
))
279 DPRINTM(DPRINT_DISK
, "Using Int 13 Extensions for read. DiskInt13ExtensionsSupported(%d) = %s\n", DriveNumber
, DiskInt13ExtensionsSupported(DriveNumber
) ? "TRUE" : "FALSE");
282 // LBA is easy, nothing to calculate
285 return PcDiskReadLogicalSectorsLBA(DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
289 // LBA is not supported default to the CHS calls
290 return PcDiskReadLogicalSectorsCHS(DriveNumber
, SectorNumber
, SectorCount
, Buffer
);
297 PcDiskGetDriveGeometry(ULONG DriveNumber
, PGEOMETRY Geometry
)
303 DPRINTM(DPRINT_DISK
, "DiskGetDriveGeometry()\n");
305 /* BIOS Int 13h, function 08h - Get drive parameters
307 * DL = drive (bit 7 set for hard disk)
308 * ES:DI = 0000h:0000h to guard against BIOS bugs
312 * CF clear if successful
314 * AL = 00h on at least some BIOSes
315 * BL = drive type (AT/PS2 floppies only)
316 * CH = low eight bits of maximum cylinder number
317 * CL = maximum sector number (bits 5-0)
318 * high two bits of maximum cylinder number (bits 7-6)
319 * DH = maximum head number
320 * DL = number of drives
321 * ES:DI -> drive parameter table (floppies only)
324 RegsIn
.b
.dl
= DriveNumber
;
325 RegsIn
.w
.es
= 0x0000;
326 RegsIn
.w
.di
= 0x0000;
328 /* Get drive parameters */
329 Int386(0x13, &RegsIn
, &RegsOut
);
331 if (! INT386_SUCCESS(RegsOut
))
336 Cylinders
= (RegsOut
.b
.cl
& 0xC0) << 2;
337 Cylinders
+= RegsOut
.b
.ch
;
339 Geometry
->Cylinders
= Cylinders
;
340 Geometry
->Heads
= RegsOut
.b
.dh
+ 1;
341 Geometry
->Sectors
= RegsOut
.b
.cl
& 0x3F;
342 Geometry
->BytesPerSector
= 512; /* Just assume 512 bytes per sector */
348 PcDiskGetCacheableBlockCount(ULONG DriveNumber
)
352 /* If LBA is supported then the block size will be 64 sectors (32k)
353 * If not then the block size is the size of one track */
354 if (DiskInt13ExtensionsSupported(DriveNumber
))
358 /* Get the disk geometry
359 * If this fails then we will just return 1 sector to be safe */
360 else if (! PcDiskGetDriveGeometry(DriveNumber
, &Geometry
))
366 return Geometry
.Sectors
;