improve freeldr debug prints:
[reactos.git] / reactos / boot / freeldr / freeldr / disk / disk.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
23 #undef UNIMPLEMENTED
24 #define UNIMPLEMENTED BugCheck((DPRINT_WARNING, "Unimplemented\n"));
25
26 static BOOLEAN bReportError = TRUE;
27
28 /////////////////////////////////////////////////////////////////////////////////////////////
29 // FUNCTIONS
30 /////////////////////////////////////////////////////////////////////////////////////////////
31
32 VOID DiskReportError (BOOLEAN bError)
33 {
34 bReportError = bError;
35 }
36
37 VOID DiskError(PCSTR ErrorString, ULONG ErrorCode)
38 {
39 CHAR ErrorCodeString[200];
40
41 if (bReportError == FALSE)
42 return;
43
44 sprintf(ErrorCodeString, "%s\n\nError Code: 0x%lx\nError: %s", ErrorString, ErrorCode, DiskGetErrorCodeString(ErrorCode));
45
46 DPRINTM(DPRINT_DISK, "%s\n", ErrorCodeString);
47
48 UiMessageBox(ErrorCodeString);
49 }
50
51 PCSTR DiskGetErrorCodeString(ULONG ErrorCode)
52 {
53 switch (ErrorCode)
54 {
55 case 0x00: return "no error";
56 case 0x01: return "bad command passed to driver";
57 case 0x02: return "address mark not found or bad sector";
58 case 0x03: return "diskette write protect error";
59 case 0x04: return "sector not found";
60 case 0x05: return "fixed disk reset failed";
61 case 0x06: return "diskette changed or removed";
62 case 0x07: return "bad fixed disk parameter table";
63 case 0x08: return "DMA overrun";
64 case 0x09: return "DMA access across 64k boundary";
65 case 0x0A: return "bad fixed disk sector flag";
66 case 0x0B: return "bad fixed disk cylinder";
67 case 0x0C: return "unsupported track/invalid media";
68 case 0x0D: return "invalid number of sectors on fixed disk format";
69 case 0x0E: return "fixed disk controlled data address mark detected";
70 case 0x0F: return "fixed disk DMA arbitration level out of range";
71 case 0x10: return "ECC/CRC error on disk read";
72 case 0x11: return "recoverable fixed disk data error, data fixed by ECC";
73 case 0x20: return "controller error (NEC for floppies)";
74 case 0x40: return "seek failure";
75 case 0x80: return "time out, drive not ready";
76 case 0xAA: return "fixed disk drive not ready";
77 case 0xBB: return "fixed disk undefined error";
78 case 0xCC: return "fixed disk write fault on selected drive";
79 case 0xE0: return "fixed disk status error/Error reg = 0";
80 case 0xFF: return "sense operation failed";
81
82 default: return "unknown error code";
83 }
84 }
85
86 // This function is in arch/i386/i386disk.c
87 //BOOLEAN DiskReadLogicalSectors(ULONG DriveNumber, U64 SectorNumber, ULONG SectorCount, PVOID Buffer)
88
89 BOOLEAN DiskIsDriveRemovable(ULONG DriveNumber)
90 {
91 // Hard disks use drive numbers >= 0x80
92 // So if the drive number indicates a hard disk
93 // then return FALSE
94 // 0x49 is our magic ramdisk drive, so return FALSE for that too
95 if ((DriveNumber >= 0x80) || (DriveNumber == 0x49))
96 {
97 return FALSE;
98 }
99
100 // Drive is a floppy diskette so return TRUE
101 return TRUE;
102 }
103
104 BOOLEAN DiskGetBootVolume(PULONG DriveNumber, PULONGLONG StartSector, PULONGLONG SectorCount, int *FsType)
105 {
106 PARTITION_TABLE_ENTRY PartitionTableEntry;
107 UCHAR VolumeType;
108 ULONG ActivePartition;
109
110 DPRINTM(DPRINT_FILESYSTEM, "FsOpenVolume() DriveNumber: 0x%x PartitionNumber: 0x%x\n", BootDrive, BootPartition);
111
112 // Check and see if it is a floppy drive
113 // If so then just assume FAT12 file system type
114 if (DiskIsDriveRemovable(BootDrive))
115 {
116 DPRINTM(DPRINT_FILESYSTEM, "Drive is a floppy diskette drive. Assuming FAT12 file system.\n");
117
118 *DriveNumber = BootDrive;
119 *StartSector = 0;
120 *SectorCount = 2 * 80 * 18; /* FIXME hardcoded for 1.44 Mb */
121 *FsType = FS_FAT;
122 return TRUE;
123 }
124
125 // Check for ISO9660 file system type
126 if (BootDrive >= 0x80 && FsRecIsIso9660(BootDrive))
127 {
128 DPRINTM(DPRINT_FILESYSTEM, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n");
129
130 *DriveNumber = BootDrive;
131 *StartSector = 0;
132 *SectorCount = 0;
133 *FsType = FS_ISO9660;
134 return TRUE;
135 }
136
137 // Get the requested partition entry
138 if (BootPartition == 0)
139 {
140 // Partition requested was zero which means the boot partition
141 if (! DiskGetActivePartitionEntry(BootDrive, &PartitionTableEntry, &ActivePartition))
142 {
143 /* Try partition-less disk */
144 *StartSector = 0;
145 *SectorCount = 0;
146 }
147 /* Check for valid partition */
148 else if (PartitionTableEntry.SystemIndicator == PARTITION_ENTRY_UNUSED)
149 {
150 return FALSE;
151 }
152 else
153 {
154 *StartSector = PartitionTableEntry.SectorCountBeforePartition;
155 *SectorCount = PartitionTableEntry.PartitionSectorCount;
156 }
157 }
158 else if (0xff == BootPartition)
159 {
160 /* Partition-less disk */
161 *StartSector = 0;
162 *SectorCount = 0;
163 }
164 else
165 {
166 // Get requested partition
167 if (! MachDiskGetPartitionEntry(BootDrive, BootPartition, &PartitionTableEntry))
168 {
169 return FALSE;
170 }
171 /* Check for valid partition */
172 else if (PartitionTableEntry.SystemIndicator == PARTITION_ENTRY_UNUSED)
173 {
174 return FALSE;
175 }
176 else
177 {
178 *StartSector = PartitionTableEntry.SectorCountBeforePartition;
179 *SectorCount = PartitionTableEntry.PartitionSectorCount;
180 }
181 }
182
183 // Try to recognize the file system
184 if (!FsRecognizeVolume(BootDrive, *StartSector, &VolumeType))
185 {
186 return FALSE;
187 }
188
189 *DriveNumber = BootDrive;
190
191 switch (VolumeType)
192 {
193 case PARTITION_FAT_12:
194 case PARTITION_FAT_16:
195 case PARTITION_HUGE:
196 case PARTITION_XINT13:
197 case PARTITION_FAT32:
198 case PARTITION_FAT32_XINT13:
199 *FsType = FS_FAT;
200 return TRUE;
201 case PARTITION_EXT2:
202 *FsType = FS_EXT2;
203 return TRUE;
204 case PARTITION_NTFS:
205 *FsType = FS_NTFS;
206 return TRUE;
207 default:
208 *FsType = 0;
209 return FALSE;
210 }
211
212 return TRUE;
213 }
214
215 VOID
216 DiskGetBootDevice(PULONG BootDevice)
217 {
218 ((char *)BootDevice)[0] = (char)BootDrive;
219 ((char *)BootDevice)[1] = (char)BootPartition;
220 }
221
222 BOOLEAN
223 DiskBootingFromFloppy(VOID)
224 {
225 return BootDrive < 0x80;
226 }
227
228 #define IsRecognizedPartition(P) \
229 ((P) == PARTITION_FAT_12 || \
230 (P) == PARTITION_FAT_16 || \
231 (P) == PARTITION_HUGE || \
232 (P) == PARTITION_IFS || \
233 (P) == PARTITION_EXT2 || \
234 (P) == PARTITION_FAT32 || \
235 (P) == PARTITION_FAT32_XINT13 || \
236 (P) == PARTITION_XINT13)
237
238 #define IsContainerPartition(P) \
239 ((P) == PARTITION_EXTENDED || \
240 (P) == PARTITION_XINT13_EXTENDED)
241
242 BOOLEAN DiskGetSystemVolume(char *SystemPath,
243 char *RemainingPath,
244 PULONG Device,
245 PULONG DriveNumber,
246 PULONGLONG StartSector,
247 PULONGLONG SectorCount,
248 int *FsType)
249 {
250 ULONG PartitionNumber;
251 PARTITION_TABLE_ENTRY PartitionTableEntry;
252 UCHAR VolumeType;
253 CHAR BootPath[256];
254 unsigned i, RosPartition;
255
256 /*
257 * Verify system path
258 */
259 if (!DissectArcPath(SystemPath, BootPath, DriveNumber, &PartitionNumber))
260 {
261 return FALSE;
262 }
263 if (NULL != RemainingPath)
264 {
265 strcpy(RemainingPath, BootPath);
266 }
267
268 /* 0xff -> no partition table present, use whole device */
269 if (0xff == PartitionNumber)
270 {
271 PartitionTableEntry.SectorCountBeforePartition = 0;
272 i = 0xff;
273 }
274 else
275 {
276 /* recalculate the boot partition for freeldr */
277 i = 0;
278 RosPartition = 0;
279 while (1)
280 {
281 if (!MachDiskGetPartitionEntry(*DriveNumber, ++i, &PartitionTableEntry))
282 {
283 return FALSE;
284 }
285 if (!IsContainerPartition(PartitionTableEntry.SystemIndicator) &&
286 PartitionTableEntry.SystemIndicator != PARTITION_ENTRY_UNUSED)
287 {
288 if (++RosPartition == PartitionNumber)
289 {
290 break;
291 }
292 }
293 }
294 }
295
296 /* Check for ISO9660 file system type */
297 if (*DriveNumber >= 0x80 && FsRecIsIso9660(*DriveNumber))
298 {
299 DPRINTM(DPRINT_FILESYSTEM, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n");
300
301 if (NULL != Device)
302 {
303 ((char *)Device)[0] = (char)(*DriveNumber);
304 ((char *)Device)[1] = (char)i;
305 }
306 *StartSector = 0;
307 *SectorCount = 0;
308 *FsType = FS_ISO9660;
309 return TRUE;
310 }
311
312 if (!FsRecognizeVolume(*DriveNumber, PartitionTableEntry.SectorCountBeforePartition, &VolumeType))
313 {
314 return FALSE;
315 }
316
317 if (NULL != Device)
318 {
319 ((char *)Device)[0] = (char)(*DriveNumber);
320 ((char *)Device)[1] = (char)i;
321 }
322 *StartSector = PartitionTableEntry.SectorCountBeforePartition;
323 *SectorCount = PartitionTableEntry.PartitionSectorCount;
324
325 switch (VolumeType)
326 {
327 case PARTITION_FAT_12:
328 case PARTITION_FAT_16:
329 case PARTITION_HUGE:
330 case PARTITION_XINT13:
331 case PARTITION_FAT32:
332 case PARTITION_FAT32_XINT13:
333 *FsType = FS_FAT;
334 return TRUE;
335 case PARTITION_EXT2:
336 *FsType = FS_EXT2;
337 return TRUE;
338 case PARTITION_NTFS:
339 *FsType = FS_NTFS;
340 return TRUE;
341 default:
342 *FsType = 0;
343 return FALSE;
344 }
345
346 return FALSE;
347 }
348
349 BOOLEAN
350 DiskGetBootPath(char *BootPath, unsigned Size)
351 {
352 static char Path[] = "multi(0)disk(0)";
353 char Device[4];
354
355 _itoa(BootDrive, Device, 10);
356 if (Size <= sizeof(Path) + 6 + strlen(Device))
357 {
358 return FALSE;
359 }
360 strcpy(BootPath, Path);
361 strcat(BootPath, MachDiskBootingFromFloppy() ? "fdisk" : "cdrom");
362 strcat(strcat(strcat(BootPath, "("), Device), ")");
363
364 return TRUE;
365 }
366
367 BOOLEAN
368 DiskNormalizeSystemPath(char *SystemPath, unsigned Size)
369 {
370 CHAR BootPath[256];
371 ULONG PartitionNumber;
372 ULONG DriveNumber;
373 PARTITION_TABLE_ENTRY PartEntry;
374 char *p;
375
376 if (!DissectArcPath(SystemPath, BootPath, &DriveNumber, &PartitionNumber))
377 {
378 return FALSE;
379 }
380
381 if (0 != PartitionNumber)
382 {
383 return TRUE;
384 }
385
386 if (! DiskGetActivePartitionEntry(DriveNumber,
387 &PartEntry,
388 &PartitionNumber) ||
389 PartitionNumber < 1 || 9 < PartitionNumber)
390 {
391 return FALSE;
392 }
393
394 p = SystemPath;
395 while ('\0' != *p && 0 != _strnicmp(p, "partition(", 10)) {
396 p++;
397 }
398 p = strchr(p, ')');
399 if (NULL == p || '0' != *(p - 1)) {
400 return FALSE;
401 }
402 *(p - 1) = '0' + PartitionNumber;
403
404 return TRUE;
405 }
406
407
408 // This function is in arch/i386/i386disk.c
409 //VOID DiskStopFloppyMotor(VOID)
410
411 // This function is in arch/i386/i386disk.c
412 //ULONG DiskGetCacheableBlockCount(ULONG DriveNumber)