c0eeb0b7d41a5f5036478077e74487750412d14e
[reactos.git] / reactos / boot / freeldr / freeldr / arch / i386 / pcdisk.c
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 #include <freeldr.h>
21
22 #define NDEBUG
23 #include <debug.h>
24
25 #include <pshpack2.h>
26 typedef struct
27 {
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;
39 #include <poppack.h>
40
41 /////////////////////////////////////////////////////////////////////////////////////////////
42 // FUNCTIONS
43 /////////////////////////////////////////////////////////////////////////////////////////////
44
45 static BOOLEAN PcDiskResetController(ULONG DriveNumber)
46 {
47 REGS RegsIn;
48 REGS RegsOut;
49
50 DPRINTM(DPRINT_DISK, "PcDiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber);
51
52 // BIOS Int 13h, function 0 - Reset disk system
53 // AH = 00h
54 // DL = drive (if bit 7 is set both hard disks and floppy disks reset)
55 // Return:
56 // AH = status
57 // CF clear if successful
58 // CF set on error
59 RegsIn.b.ah = 0x00;
60 RegsIn.b.dl = DriveNumber;
61
62 // Reset the disk controller
63 Int386(0x13, &RegsIn, &RegsOut);
64
65 return INT386_SUCCESS(RegsOut);
66 }
67
68 static BOOLEAN PcDiskReadLogicalSectorsLBA(ULONG DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
69 {
70 REGS RegsIn;
71 REGS RegsOut;
72 ULONG RetryCount;
73 PI386_DISK_ADDRESS_PACKET Packet = (PI386_DISK_ADDRESS_PACKET)(BIOSCALLBUFFER);
74
75 DPRINTM(DPRINT_DISK, "PcDiskReadLogicalSectorsLBA() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer);
76
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;
82
83 // Setup disk address packet
84 RtlZeroMemory(Packet, sizeof(I386_DISK_ADDRESS_PACKET));
85 Packet->PacketSize = sizeof(I386_DISK_ADDRESS_PACKET);
86 Packet->Reserved = 0;
87 Packet->LBABlockCount = SectorCount;
88 Packet->TransferBufferOffset = ((ULONG_PTR)Buffer) & 0x0F;
89 Packet->TransferBufferSegment = ((ULONG_PTR)Buffer) >> 4;
90 Packet->LBAStartBlock = SectorNumber;
91
92 // BIOS int 0x13, function 42h - IBM/MS INT 13 Extensions - EXTENDED READ
93 // Return:
94 // CF clear if successful
95 // AH = 00h
96 // CF set on error
97 // AH = error code
98 // disk address packet's block count field set to the
99 // number of blocks successfully transferred
100
101 // Retry 3 times
102 for (RetryCount=0; RetryCount<3; RetryCount++)
103 {
104 Int386(0x13, &RegsIn, &RegsOut);
105
106 // If it worked return TRUE
107 if (INT386_SUCCESS(RegsOut))
108 {
109 return TRUE;
110 }
111 // If it was a corrected ECC error then the data is still good
112 else if (RegsOut.b.ah == 0x11)
113 {
114 return TRUE;
115 }
116 // If it failed the do the next retry
117 else
118 {
119 PcDiskResetController(DriveNumber);
120
121 continue;
122 }
123 }
124
125 // If we get here then the read failed
126 DiskError("Disk Read Failed in LBA mode", RegsOut.b.ah);
127
128 return FALSE;
129 }
130
131 static BOOLEAN PcDiskReadLogicalSectorsCHS(ULONG DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
132 {
133 ULONG PhysicalSector;
134 ULONG PhysicalHead;
135 ULONG PhysicalTrack;
136 GEOMETRY DriveGeometry;
137 ULONG NumberOfSectorsToRead;
138 REGS RegsIn;
139 REGS RegsOut;
140 ULONG RetryCount;
141
142 DPRINTM(DPRINT_DISK, "PcDiskReadLogicalSectorsCHS()\n");
143
144 //
145 // Get the drive geometry
146 //
147 if (!MachDiskGetDriveGeometry(DriveNumber, &DriveGeometry) ||
148 DriveGeometry.Sectors == 0 ||
149 DriveGeometry.Heads == 0)
150 {
151 return FALSE;
152 }
153
154 while (SectorCount)
155 {
156
157 //
158 // Calculate the physical disk offsets
159 //
160 PhysicalSector = 1 + (SectorNumber % DriveGeometry.Sectors);
161 PhysicalHead = (SectorNumber / DriveGeometry.Sectors) % DriveGeometry.Heads;
162 PhysicalTrack = (SectorNumber / DriveGeometry.Sectors) / DriveGeometry.Heads;
163
164 //
165 // Calculate how many sectors we need to read this round
166 //
167 if (PhysicalSector > 1)
168 {
169 if (SectorCount >= (DriveGeometry.Sectors - (PhysicalSector - 1)))
170 NumberOfSectorsToRead = (DriveGeometry.Sectors - (PhysicalSector - 1));
171 else
172 NumberOfSectorsToRead = SectorCount;
173 }
174 else
175 {
176 if (SectorCount >= DriveGeometry.Sectors)
177 NumberOfSectorsToRead = DriveGeometry.Sectors;
178 else
179 NumberOfSectorsToRead = SectorCount;
180 }
181
182 //
183 // Make sure the read is within the geometry boundaries
184 //
185 if ((PhysicalHead >= DriveGeometry.Heads) ||
186 (PhysicalTrack >= DriveGeometry.Cylinders) ||
187 ((NumberOfSectorsToRead + PhysicalSector) > (DriveGeometry.Sectors + 1)) ||
188 (PhysicalSector > DriveGeometry.Sectors))
189 {
190 DiskError("Disk read exceeds drive geometry limits.", 0);
191 return FALSE;
192 }
193
194 // BIOS Int 13h, function 2 - Read Disk Sectors
195 // AH = 02h
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)
200 // DH = head number
201 // DL = drive number (bit 7 set for hard disk)
202 // ES:BX -> data buffer
203 // Return:
204 // CF set on error
205 // if AH = 11h (corrected ECC error), AL = burst length
206 // CF clear if successful
207 // AH = status
208 // AL = number of sectors transferred
209 // (only valid if CF set for some BIOSes)
210 RegsIn.b.ah = 0x02;
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;
218
219 //
220 // Perform the read
221 // Retry 3 times
222 //
223 for (RetryCount=0; RetryCount<3; RetryCount++)
224 {
225 Int386(0x13, &RegsIn, &RegsOut);
226
227 // If it worked break out
228 if (INT386_SUCCESS(RegsOut))
229 {
230 break;
231 }
232 // If it was a corrected ECC error then the data is still good
233 else if (RegsOut.b.ah == 0x11)
234 {
235 break;
236 }
237 // If it failed the do the next retry
238 else
239 {
240 PcDiskResetController(DriveNumber);
241
242 continue;
243 }
244 }
245
246 // If we retried 3 times then fail
247 if (RetryCount >= 3)
248 {
249 DiskError("Disk Read Failed in CHS mode, after retrying 3 times", RegsOut.b.ah);
250 return FALSE;
251 }
252
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
258
259 Buffer = (PVOID)((ULONG_PTR)Buffer + (NumberOfSectorsToRead * DriveGeometry.BytesPerSector));
260 SectorCount -= NumberOfSectorsToRead;
261 SectorNumber += NumberOfSectorsToRead;
262 }
263
264 return TRUE;
265 }
266
267 static BOOLEAN PcDiskInt13ExtensionsSupported(ULONG DriveNumber)
268 {
269 static ULONG LastDriveNumber = 0xffffffff;
270 static BOOLEAN LastSupported;
271 REGS RegsIn;
272 REGS RegsOut;
273
274 DPRINTM(DPRINT_DISK, "PcDiskInt13ExtensionsSupported()\n");
275
276 if (DriveNumber == LastDriveNumber)
277 {
278 DPRINTM(DPRINT_DISK, "Using cached value %s for drive 0x%x\n", LastSupported ? "TRUE" : "FALSE", DriveNumber);
279 return LastSupported;
280 }
281
282 // Some BIOSes report that extended disk access functions are not supported
283 // when booting from a CD (e.g. Phoenix BIOS v6.00PG and Insyde BIOS shipping
284 // with Intel Macs). Therefore we just return TRUE if we're booting from a CD -
285 // we can assume that all El Torito capable BIOSes support INT 13 extensions.
286 // We simply detect whether we're booting from CD by checking whether the drive
287 // number is >= 0x90. It's 0x90 on the Insyde BIOS, and 0x9F on most other BIOSes.
288 if (DriveNumber >= 0x90)
289 {
290 LastSupported = TRUE;
291 return TRUE;
292 }
293
294 LastDriveNumber = DriveNumber;
295
296 // IBM/MS INT 13 Extensions - INSTALLATION CHECK
297 // AH = 41h
298 // BX = 55AAh
299 // DL = drive (80h-FFh)
300 // Return:
301 // CF set on error (extensions not supported)
302 // AH = 01h (invalid function)
303 // CF clear if successful
304 // BX = AA55h if installed
305 // AH = major version of extensions
306 // 01h = 1.x
307 // 20h = 2.0 / EDD-1.0
308 // 21h = 2.1 / EDD-1.1
309 // 30h = EDD-3.0
310 // AL = internal use
311 // CX = API subset support bitmap
312 // DH = extension version (v2.0+ ??? -- not present in 1.x)
313 //
314 // Bitfields for IBM/MS INT 13 Extensions API support bitmap
315 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
316 // Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
317 // Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
318 // extended drive parameter table is valid
319 // Bits 3-15 reserved
320 RegsIn.b.ah = 0x41;
321 RegsIn.w.bx = 0x55AA;
322 RegsIn.b.dl = DriveNumber;
323
324 // Reset the disk controller
325 Int386(0x13, &RegsIn, &RegsOut);
326
327 if (!INT386_SUCCESS(RegsOut))
328 {
329 // CF set on error (extensions not supported)
330 LastSupported = FALSE;
331 return FALSE;
332 }
333
334 if (RegsOut.w.bx != 0xAA55)
335 {
336 // BX = AA55h if installed
337 LastSupported = FALSE;
338 return FALSE;
339 }
340
341 if (!(RegsOut.w.cx & 0x0001))
342 {
343 // CX = API subset support bitmap
344 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
345 printf("Suspicious API subset support bitmap 0x%x on device 0x%lx\n", RegsOut.w.cx, DriveNumber);
346 LastSupported = FALSE;
347 return FALSE;
348 }
349
350 LastSupported = TRUE;
351 return TRUE;
352 }
353
354 BOOLEAN PcDiskReadLogicalSectors(ULONG DriveNumber, ULONGLONG SectorNumber, ULONG SectorCount, PVOID Buffer)
355 {
356
357 DPRINTM(DPRINT_DISK, "PcDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64d SectorCount: %d Buffer: 0x%x\n", DriveNumber, SectorNumber, SectorCount, Buffer);
358
359 //
360 // Check to see if it is a fixed disk drive
361 // If so then check to see if Int13 extensions work
362 // If they do then use them, otherwise default back to BIOS calls
363 //
364 if ((DriveNumber >= 0x80) && PcDiskInt13ExtensionsSupported(DriveNumber))
365 {
366 DPRINTM(DPRINT_DISK, "Using Int 13 Extensions for read. PcDiskInt13ExtensionsSupported(%d) = %s\n", DriveNumber, PcDiskInt13ExtensionsSupported(DriveNumber) ? "TRUE" : "FALSE");
367
368 //
369 // LBA is easy, nothing to calculate
370 // Just do the read
371 //
372 return PcDiskReadLogicalSectorsLBA(DriveNumber, SectorNumber, SectorCount, Buffer);
373 }
374 else
375 {
376 // LBA is not supported default to the CHS calls
377 return PcDiskReadLogicalSectorsCHS(DriveNumber, SectorNumber, SectorCount, Buffer);
378 }
379
380 return TRUE;
381 }
382
383 BOOLEAN
384 PcDiskGetDriveGeometry(ULONG DriveNumber, PGEOMETRY Geometry)
385 {
386 REGS RegsIn;
387 REGS RegsOut;
388 ULONG Cylinders;
389
390 DPRINTM(DPRINT_DISK, "DiskGetDriveGeometry()\n");
391
392 /* BIOS Int 13h, function 08h - Get drive parameters
393 * AH = 08h
394 * DL = drive (bit 7 set for hard disk)
395 * ES:DI = 0000h:0000h to guard against BIOS bugs
396 * Return:
397 * CF set on error
398 * AH = status (07h)
399 * CF clear if successful
400 * AH = 00h
401 * AL = 00h on at least some BIOSes
402 * BL = drive type (AT/PS2 floppies only)
403 * CH = low eight bits of maximum cylinder number
404 * CL = maximum sector number (bits 5-0)
405 * high two bits of maximum cylinder number (bits 7-6)
406 * DH = maximum head number
407 * DL = number of drives
408 * ES:DI -> drive parameter table (floppies only)
409 */
410 RegsIn.b.ah = 0x08;
411 RegsIn.b.dl = DriveNumber;
412 RegsIn.w.es = 0x0000;
413 RegsIn.w.di = 0x0000;
414
415 /* Get drive parameters */
416 Int386(0x13, &RegsIn, &RegsOut);
417
418 if (! INT386_SUCCESS(RegsOut))
419 {
420 return FALSE;
421 }
422
423 Cylinders = (RegsOut.b.cl & 0xC0) << 2;
424 Cylinders += RegsOut.b.ch;
425 Cylinders++;
426 Geometry->Cylinders = Cylinders;
427 Geometry->Heads = RegsOut.b.dh + 1;
428 Geometry->Sectors = RegsOut.b.cl & 0x3F;
429 Geometry->BytesPerSector = 512; /* Just assume 512 bytes per sector */
430
431 return TRUE;
432 }
433
434 ULONG
435 PcDiskGetCacheableBlockCount(ULONG DriveNumber)
436 {
437 GEOMETRY Geometry;
438
439 /* If LBA is supported then the block size will be 64 sectors (32k)
440 * If not then the block size is the size of one track */
441 if (DiskInt13ExtensionsSupported(DriveNumber))
442 {
443 return 64;
444 }
445 /* Get the disk geometry
446 * If this fails then we will just return 1 sector to be safe */
447 else if (! PcDiskGetDriveGeometry(DriveNumber, &Geometry))
448 {
449 return 1;
450 }
451 else
452 {
453 return Geometry.Sectors;
454 }
455 }
456
457 /* EOF */