Fixed the calculation of the boot disk number in i386DiskGetSystemVolume.
[reactos.git] / reactos / boot / freeldr / freeldr / arch / i386 / i386disk.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 #include "debug.h"
22 #include "i386.h"
23 #include "fsrec.h"
24
25 /////////////////////////////////////////////////////////////////////////////////////////////
26 // FUNCTIONS
27 /////////////////////////////////////////////////////////////////////////////////////////////
28
29 #ifdef __i386__
30
31 BOOL DiskResetController(ULONG DriveNumber)
32 {
33 REGS RegsIn;
34 REGS RegsOut;
35
36 DbgPrint((DPRINT_DISK, "DiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber));
37
38 // BIOS Int 13h, function 0 - Reset disk system
39 // AH = 00h
40 // DL = drive (if bit 7 is set both hard disks and floppy disks reset)
41 // Return:
42 // AH = status
43 // CF clear if successful
44 // CF set on error
45 RegsIn.b.ah = 0x00;
46 RegsIn.b.dl = DriveNumber;
47
48 // Reset the disk controller
49 Int386(0x13, &RegsIn, &RegsOut);
50
51 return INT386_SUCCESS(RegsOut);
52 }
53
54 BOOL DiskInt13ExtensionsSupported(ULONG DriveNumber)
55 {
56 REGS RegsIn;
57 REGS RegsOut;
58
59 DbgPrint((DPRINT_DISK, "DiskInt13ExtensionsSupported()\n"));
60
61 // IBM/MS INT 13 Extensions - INSTALLATION CHECK
62 // AH = 41h
63 // BX = 55AAh
64 // DL = drive (80h-FFh)
65 // Return:
66 // CF set on error (extensions not supported)
67 // AH = 01h (invalid function)
68 // CF clear if successful
69 // BX = AA55h if installed
70 // AH = major version of extensions
71 // 01h = 1.x
72 // 20h = 2.0 / EDD-1.0
73 // 21h = 2.1 / EDD-1.1
74 // 30h = EDD-3.0
75 // AL = internal use
76 // CX = API subset support bitmap
77 // DH = extension version (v2.0+ ??? -- not present in 1.x)
78 //
79 // Bitfields for IBM/MS INT 13 Extensions API support bitmap
80 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
81 // Bit 1, removable drive controller functions (AH=45h,46h,48h,49h,INT 15/AH=52h) supported
82 // Bit 2, enhanced disk drive (EDD) functions (AH=48h,AH=4Eh) supported
83 // extended drive parameter table is valid
84 // Bits 3-15 reserved
85 RegsIn.b.ah = 0x41;
86 RegsIn.w.bx = 0x55AA;
87 RegsIn.b.dl = DriveNumber;
88
89 // Reset the disk controller
90 Int386(0x13, &RegsIn, &RegsOut);
91
92 if (!INT386_SUCCESS(RegsOut))
93 {
94 // CF set on error (extensions not supported)
95 return FALSE;
96 }
97
98 if (RegsOut.w.bx != 0xAA55)
99 {
100 // BX = AA55h if installed
101 return FALSE;
102 }
103
104 // Note:
105 // The original check is too strict because some BIOSes report that
106 // extended disk access functions are not suported when booting
107 // from a CD (e.g. Phoenix BIOS v6.00PG). Argh!
108 #if 0
109 if (!(RegsOut.w.cx & 0x0001))
110 {
111 // CX = API subset support bitmap
112 // Bit 0, extended disk access functions (AH=42h-44h,47h,48h) supported
113 return FALSE;
114 }
115 #endif
116
117 // Use this relaxed check instead
118 if (RegsOut.w.cx == 0x0000)
119 {
120 // CX = API subset support bitmap
121 return FALSE;
122 }
123
124 return TRUE;
125 }
126
127 VOID DiskStopFloppyMotor(VOID)
128 {
129 WRITE_PORT_UCHAR((PUCHAR)0x3F2, 0);
130 }
131
132 BOOL DiskGetExtendedDriveParameters(ULONG DriveNumber, PVOID Buffer, USHORT BufferSize)
133 {
134 REGS RegsIn;
135 REGS RegsOut;
136 PUSHORT Ptr = (PUSHORT)(BIOSCALLBUFFER);
137
138 DbgPrint((DPRINT_DISK, "DiskGetExtendedDriveParameters()\n"));
139
140 // Initialize transfer buffer
141 *Ptr = BufferSize;
142
143 // BIOS Int 13h, function 48h - Get drive parameters
144 // AH = 48h
145 // DL = drive (bit 7 set for hard disk)
146 // DS:SI = result buffer
147 // Return:
148 // CF set on error
149 // AH = status (07h)
150 // CF clear if successful
151 // AH = 00h
152 // DS:SI -> result buffer
153 RegsIn.b.ah = 0x48;
154 RegsIn.b.dl = DriveNumber;
155 RegsIn.x.ds = BIOSCALLBUFSEGMENT; // DS:SI -> result buffer
156 RegsIn.w.si = BIOSCALLBUFOFFSET;
157
158 // Get drive parameters
159 Int386(0x13, &RegsIn, &RegsOut);
160
161 if (!INT386_SUCCESS(RegsOut))
162 {
163 return FALSE;
164 }
165
166 memcpy(Buffer, Ptr, BufferSize);
167
168 DbgPrint((DPRINT_DISK, "size of buffer: %x\n", Ptr[0]));
169 DbgPrint((DPRINT_DISK, "information flags: %x\n", Ptr[1]));
170 DbgPrint((DPRINT_DISK, "number of physical cylinders on drive: %u\n", *(PULONG)&Ptr[2]));
171 DbgPrint((DPRINT_DISK, "number of physical heads on drive: %u\n", *(PULONG)&Ptr[4]));
172 DbgPrint((DPRINT_DISK, "number of physical sectors per track: %u\n", *(PULONG)&Ptr[6]));
173 DbgPrint((DPRINT_DISK, "total number of sectors on drive: %I64u\n", *(unsigned long long*)&Ptr[8]));
174 DbgPrint((DPRINT_DISK, "bytes per sector: %u\n", Ptr[12]));
175 if (Ptr[0] >= 0x1e)
176 {
177 DbgPrint((DPRINT_HB, "EED configuration parameters: %x:%x\n", Ptr[13], Ptr[14]));
178 if (Ptr[13] != 0xffff && Ptr[14] != 0xffff)
179 {
180 PUCHAR SpecPtr = (PUCHAR)((Ptr[13] << 4) + Ptr[14]);
181 DbgPrint((DPRINT_DISK, "SpecPtr: %x\n", SpecPtr));
182 DbgPrint((DPRINT_DISK, "physical I/O port base address: %x\n", *(PUSHORT)&SpecPtr[0]));
183 DbgPrint((DPRINT_DISK, "disk-drive control port address: %x\n", *(PUSHORT)&SpecPtr[2]));
184 DbgPrint((DPRINT_DISK, "drive flags: %x\n", SpecPtr[4]));
185 DbgPrint((DPRINT_DISK, "proprietary information: %x\n", SpecPtr[5]));
186 DbgPrint((DPRINT_DISK, "IRQ for drive: %u\n", SpecPtr[6]));
187 DbgPrint((DPRINT_DISK, "sector count for multi-sector transfers: %u\n", SpecPtr[7]));
188 DbgPrint((DPRINT_DISK, "DMA control: %x\n", SpecPtr[8]));
189 DbgPrint((DPRINT_DISK, "programmed I/O control: %x\n", SpecPtr[9]));
190 DbgPrint((DPRINT_DISK, "drive options: %x\n", *(PUSHORT)&SpecPtr[10]));
191 }
192 }
193 if (Ptr[0] >= 0x42)
194 {
195 DbgPrint((DPRINT_HB, "signature: %x\n", Ptr[15]));
196 }
197
198 return TRUE;
199 }
200
201 BOOL i386DiskGetBootVolume(PULONG DriveNumber, PULONGLONG StartSector, PULONGLONG SectorCount, int *FsType)
202 {
203 PARTITION_TABLE_ENTRY PartitionTableEntry;
204 UCHAR VolumeType;
205
206 DbgPrint((DPRINT_FILESYSTEM, "FsOpenVolume() DriveNumber: 0x%x PartitionNumber: 0x%x\n", i386BootDrive, i386BootPartition));
207
208 // Check and see if it is a floppy drive
209 // If so then just assume FAT12 file system type
210 if (DiskIsDriveRemovable(i386BootDrive))
211 {
212 DbgPrint((DPRINT_FILESYSTEM, "Drive is a floppy diskette drive. Assuming FAT12 file system.\n"));
213
214 *DriveNumber = i386BootDrive;
215 *StartSector = 0;
216 *SectorCount = 2 * 80 * 18; /* FIXME hardcoded for 1.44 Mb */
217 *FsType = FS_FAT;
218 return TRUE;
219 }
220
221 // Check for ISO9660 file system type
222 if (i386BootDrive >= 0x80 && FsRecIsIso9660(i386BootDrive))
223 {
224 DbgPrint((DPRINT_FILESYSTEM, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n"));
225
226 *DriveNumber = i386BootDrive;
227 *StartSector = 0;
228 *SectorCount = 0;
229 *FsType = FS_ISO9660;
230 return TRUE;
231 }
232
233 // Get the requested partition entry
234 if (i386BootPartition == 0)
235 {
236 // Partition requested was zero which means the boot partition
237 if (! DiskGetActivePartitionEntry(i386BootDrive, &PartitionTableEntry))
238 {
239 /* Try partition-less disk */
240 *StartSector = 0;
241 *SectorCount = 0;
242 }
243 /* Check for valid partition */
244 else if (PartitionTableEntry.SystemIndicator == PARTITION_ENTRY_UNUSED)
245 {
246 return FALSE;
247 }
248 else
249 {
250 *StartSector = PartitionTableEntry.SectorCountBeforePartition;
251 *SectorCount = PartitionTableEntry.PartitionSectorCount;
252 }
253 }
254 else if (0xff == i386BootPartition)
255 {
256 /* Partition-less disk */
257 *StartSector = 0;
258 *SectorCount = 0;
259 }
260 else
261 {
262 // Get requested partition
263 if (! MachDiskGetPartitionEntry(i386BootDrive, i386BootPartition, &PartitionTableEntry))
264 {
265 return FALSE;
266 }
267 /* Check for valid partition */
268 else if (PartitionTableEntry.SystemIndicator == PARTITION_ENTRY_UNUSED)
269 {
270 return FALSE;
271 }
272 else
273 {
274 *StartSector = PartitionTableEntry.SectorCountBeforePartition;
275 *SectorCount = PartitionTableEntry.PartitionSectorCount;
276 }
277 }
278
279 // Try to recognize the file system
280 if (!FsRecognizeVolume(i386BootDrive, *StartSector, &VolumeType))
281 {
282 return FALSE;
283 }
284
285 *DriveNumber = i386BootDrive;
286
287 switch (VolumeType)
288 {
289 case PARTITION_FAT_12:
290 case PARTITION_FAT_16:
291 case PARTITION_HUGE:
292 case PARTITION_XINT13:
293 case PARTITION_FAT32:
294 case PARTITION_FAT32_XINT13:
295 *FsType = FS_FAT;
296 return TRUE;
297 case PARTITION_EXT2:
298 *FsType = FS_EXT2;
299 return TRUE;
300 case PARTITION_NTFS:
301 *FsType = FS_NTFS;
302 return TRUE;
303 default:
304 *FsType = 0;
305 return FALSE;
306 }
307
308 return TRUE;
309 }
310
311 VOID
312 i386DiskGetBootDevice(PULONG BootDevice)
313 {
314 ((char *)BootDevice)[0] = (char)i386BootDrive;
315 ((char *)BootDevice)[1] = (char)i386BootPartition;
316 }
317
318 BOOL
319 i386DiskBootingFromFloppy(VOID)
320 {
321 return i386BootDrive < 0x80;
322 }
323
324 #define IsRecognizedPartition(P) \
325 ((P) == PARTITION_FAT_12 || \
326 (P) == PARTITION_FAT_16 || \
327 (P) == PARTITION_HUGE || \
328 (P) == PARTITION_IFS || \
329 (P) == PARTITION_EXT2 || \
330 (P) == PARTITION_FAT32 || \
331 (P) == PARTITION_FAT32_XINT13 || \
332 (P) == PARTITION_XINT13)
333
334 #define IsContainerPartition(P) \
335 ((P) == PARTITION_EXTENDED || \
336 (P) == PARTITION_XINT13_EXTENDED)
337
338 BOOL i386DiskGetSystemVolume(char *SystemPath,
339 char *RemainingPath,
340 PULONG Device,
341 PULONG DriveNumber,
342 PULONGLONG StartSector,
343 PULONGLONG SectorCount,
344 int *FsType)
345 {
346 ULONG PartitionNumber;
347 PARTITION_TABLE_ENTRY PartitionTableEntry;
348 UCHAR VolumeType;
349 CHAR BootPath[256];
350 unsigned i, RosPartition;
351
352 /*
353 * Verify system path
354 */
355 if (!DissectArcPath(SystemPath, BootPath, DriveNumber, &PartitionNumber))
356 {
357 return FALSE;
358 }
359 if (NULL != RemainingPath)
360 {
361 strcpy(RemainingPath, BootPath);
362 }
363
364 /* 0xff -> no partition table present, use whole device */
365 if (0xff == PartitionNumber)
366 {
367 PartitionTableEntry.SectorCountBeforePartition = 0;
368 i = 0xff;
369 }
370 else
371 {
372 /* recalculate the boot partition for freeldr */
373 i = 0;
374 RosPartition = 0;
375 while (1)
376 {
377 if (!MachDiskGetPartitionEntry(*DriveNumber, ++i, &PartitionTableEntry))
378 {
379 return FALSE;
380 }
381 if (!IsContainerPartition(PartitionTableEntry.SystemIndicator) &&
382 PartitionTableEntry.SystemIndicator != PARTITION_ENTRY_UNUSED)
383 {
384 if (++RosPartition == PartitionNumber)
385 {
386 break;
387 }
388 }
389 }
390 }
391
392 /* Check for ISO9660 file system type */
393 if (*DriveNumber >= 0x80 && FsRecIsIso9660(*DriveNumber))
394 {
395 DbgPrint((DPRINT_FILESYSTEM, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n"));
396
397 if (NULL != Device)
398 {
399 ((char *)Device)[0] = (char)(*DriveNumber);
400 ((char *)Device)[1] = (char)i;
401 }
402 *StartSector = 0;
403 *SectorCount = 0;
404 *FsType = FS_ISO9660;
405 return TRUE;
406 }
407
408 if (!FsRecognizeVolume(*DriveNumber, PartitionTableEntry.SectorCountBeforePartition, &VolumeType))
409 {
410 return FALSE;
411 }
412
413 if (NULL != Device)
414 {
415 ((char *)Device)[0] = (char)(*DriveNumber);
416 ((char *)Device)[1] = (char)i;
417 }
418 *StartSector = PartitionTableEntry.SectorCountBeforePartition;
419 *SectorCount = PartitionTableEntry.PartitionSectorCount;
420
421 switch (VolumeType)
422 {
423 case PARTITION_FAT_12:
424 case PARTITION_FAT_16:
425 case PARTITION_HUGE:
426 case PARTITION_XINT13:
427 case PARTITION_FAT32:
428 case PARTITION_FAT32_XINT13:
429 *FsType = FS_FAT;
430 return TRUE;
431 case PARTITION_EXT2:
432 *FsType = FS_EXT2;
433 return TRUE;
434 case PARTITION_NTFS:
435 *FsType = FS_NTFS;
436 return TRUE;
437 default:
438 *FsType = 0;
439 return FALSE;
440 }
441
442 return FALSE;
443 }
444
445 BOOL
446 i386DiskGetBootPath(char *BootPath, unsigned Size)
447 {
448 static char Path[] = "multi(0)disk(0)";
449 char Device[4];
450
451 itoa(i386BootDrive, Device, 10);
452 if (Size <= sizeof(Path) + 6 + strlen(Device))
453 {
454 return FALSE;
455 }
456 strcpy(BootPath, Path);
457 strcat(BootPath, MachDiskBootingFromFloppy() ? "fdisk" : "cdrom");
458 strcat(strcat(strcat(BootPath, "("), Device), ")");
459
460 return TRUE;
461 }
462
463 #endif /* defined __i386__ */
464
465 /* EOF */