remove whitespace from end of lines
[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 return TRUE;
169 }
170
171 BOOL i386DiskGetBootVolume(PULONG DriveNumber, PULONGLONG StartSector, PULONGLONG SectorCount, int *FsType)
172 {
173 PARTITION_TABLE_ENTRY PartitionTableEntry;
174 UCHAR VolumeType;
175
176 DbgPrint((DPRINT_FILESYSTEM, "FsOpenVolume() DriveNumber: 0x%x PartitionNumber: 0x%x\n", i386BootDrive, i386BootPartition));
177
178 // Check and see if it is a floppy drive
179 // If so then just assume FAT12 file system type
180 if (DiskIsDriveRemovable(i386BootDrive))
181 {
182 DbgPrint((DPRINT_FILESYSTEM, "Drive is a floppy diskette drive. Assuming FAT12 file system.\n"));
183
184 *DriveNumber = i386BootDrive;
185 *StartSector = 0;
186 *SectorCount = 2 * 80 * 18; /* FIXME hardcoded for 1.44 Mb */
187 *FsType = FS_FAT;
188 return TRUE;
189 }
190
191 // Check for ISO9660 file system type
192 if (i386BootDrive >= 0x80 && FsRecIsIso9660(i386BootDrive))
193 {
194 DbgPrint((DPRINT_FILESYSTEM, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n"));
195
196 *DriveNumber = i386BootDrive;
197 *StartSector = 0;
198 *SectorCount = 0;
199 *FsType = FS_ISO9660;
200 return TRUE;
201 }
202
203 // Get the requested partition entry
204 if (i386BootPartition == 0)
205 {
206 // Partition requested was zero which means the boot partition
207 if (! DiskGetActivePartitionEntry(i386BootDrive, &PartitionTableEntry))
208 {
209 return FALSE;
210 }
211 }
212 else
213 {
214 // Get requested partition
215 if (! MachDiskGetPartitionEntry(i386BootDrive, i386BootPartition, &PartitionTableEntry))
216 {
217 return FALSE;
218 }
219 }
220
221 // Check for valid partition
222 if (PartitionTableEntry.SystemIndicator == PARTITION_ENTRY_UNUSED)
223 {
224 return FALSE;
225 }
226
227 // Try to recognize the file system
228 if (!FsRecognizeVolume(i386BootDrive, PartitionTableEntry.SectorCountBeforePartition, &VolumeType))
229 {
230 return FALSE;
231 }
232
233 *DriveNumber = i386BootDrive;
234 *StartSector = PartitionTableEntry.SectorCountBeforePartition;
235 *SectorCount = PartitionTableEntry.PartitionSectorCount;
236
237 //switch (PartitionTableEntry.SystemIndicator)
238 switch (VolumeType)
239 {
240 case PARTITION_FAT_12:
241 case PARTITION_FAT_16:
242 case PARTITION_HUGE:
243 case PARTITION_XINT13:
244 case PARTITION_FAT32:
245 case PARTITION_FAT32_XINT13:
246 *FsType = FS_FAT;
247 return TRUE;
248 case PARTITION_EXT2:
249 *FsType = FS_EXT2;
250 return TRUE;
251 case PARTITION_NTFS:
252 *FsType = FS_NTFS;
253 return TRUE;
254 default:
255 *FsType = 0;
256 return FALSE;
257 }
258
259 return TRUE;
260 }
261
262 VOID
263 i386DiskGetBootDevice(PULONG BootDevice)
264 {
265 ((char *)BootDevice)[0] = (char)i386BootDrive;
266 ((char *)BootDevice)[1] = (char)i386BootPartition;
267 }
268
269 BOOL
270 i386DiskBootingFromFloppy(VOID)
271 {
272 return i386BootDrive < 0x80;
273 }
274
275 #define IsRecognizedPartition(P) \
276 ((P) == PARTITION_FAT_12 || \
277 (P) == PARTITION_FAT_16 || \
278 (P) == PARTITION_HUGE || \
279 (P) == PARTITION_IFS || \
280 (P) == PARTITION_EXT2 || \
281 (P) == PARTITION_FAT32 || \
282 (P) == PARTITION_FAT32_XINT13 || \
283 (P) == PARTITION_XINT13)
284
285 BOOL i386DiskGetSystemVolume(char *SystemPath,
286 char *RemainingPath,
287 PULONG Device,
288 PULONG DriveNumber,
289 PULONGLONG StartSector,
290 PULONGLONG SectorCount,
291 int *FsType)
292 {
293 ULONG PartitionNumber;
294 PARTITION_TABLE_ENTRY PartitionTableEntry;
295 UCHAR VolumeType;
296 CHAR BootPath[256];
297 unsigned i, RosPartition;
298
299 /*
300 * Verify system path
301 */
302 if (!DissectArcPath(SystemPath, BootPath, DriveNumber, &PartitionNumber))
303 {
304 return FALSE;
305 }
306 if (NULL != RemainingPath)
307 {
308 strcpy(RemainingPath, BootPath);
309 }
310
311 /* 0xff -> no partition table present, use whole device */
312 if (0xff == PartitionNumber)
313 {
314 PartitionTableEntry.SectorCountBeforePartition = 0;
315 i = 0xff;
316 }
317 else
318 {
319 /* recalculate the boot partition for freeldr */
320 i = 0;
321 RosPartition = 0;
322 while (1)
323 {
324 if (!MachDiskGetPartitionEntry(*DriveNumber, ++i, &PartitionTableEntry))
325 {
326 return FALSE;
327 }
328 if (IsRecognizedPartition(PartitionTableEntry.SystemIndicator))
329 {
330 if (++RosPartition == PartitionNumber)
331 {
332 break;
333 }
334 }
335 }
336 }
337
338 /* Check for ISO9660 file system type */
339 if (*DriveNumber >= 0x80 && FsRecIsIso9660(*DriveNumber))
340 {
341 DbgPrint((DPRINT_FILESYSTEM, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n"));
342
343 if (NULL != Device)
344 {
345 ((char *)Device)[0] = (char)(*DriveNumber);
346 ((char *)Device)[1] = (char)i;
347 }
348 *StartSector = 0;
349 *SectorCount = 0;
350 *FsType = FS_ISO9660;
351 return TRUE;
352 }
353
354 if (!FsRecognizeVolume(*DriveNumber, PartitionTableEntry.SectorCountBeforePartition, &VolumeType))
355 {
356 return FALSE;
357 }
358
359 if (NULL != Device)
360 {
361 ((char *)Device)[0] = (char)(*DriveNumber);
362 ((char *)Device)[1] = (char)i;
363 }
364 *StartSector = PartitionTableEntry.SectorCountBeforePartition;
365 *SectorCount = PartitionTableEntry.PartitionSectorCount;
366
367 switch (VolumeType)
368 {
369 case PARTITION_FAT_12:
370 case PARTITION_FAT_16:
371 case PARTITION_HUGE:
372 case PARTITION_XINT13:
373 case PARTITION_FAT32:
374 case PARTITION_FAT32_XINT13:
375 *FsType = FS_FAT;
376 return TRUE;
377 case PARTITION_EXT2:
378 *FsType = FS_EXT2;
379 return TRUE;
380 case PARTITION_NTFS:
381 *FsType = FS_NTFS;
382 return TRUE;
383 default:
384 *FsType = 0;
385 return FALSE;
386 }
387
388 return FALSE;
389 }
390
391 BOOL
392 i386DiskGetBootPath(char *BootPath, unsigned Size)
393 {
394 static char Path[] = "multi(0)disk(0)";
395 char Device[4];
396
397 itoa(i386BootDrive, Device, 10);
398 if (Size <= sizeof(Path) + 6 + strlen(Device))
399 {
400 return FALSE;
401 }
402 strcpy(BootPath, Path);
403 strcat(BootPath, MachDiskBootingFromFloppy() ? "fdisk" : "cdrom");
404 strcat(strcat(strcat(BootPath, "("), Device), ")");
405
406 return TRUE;
407 }
408
409 #endif /* defined __i386__ */
410
411 /* EOF */