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