3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
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.
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.
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.
24 #define UNIMPLEMENTED BugCheck((DPRINT_WARNING, "Unimplemented\n"));
26 static BOOLEAN bReportError
= TRUE
;
28 /////////////////////////////////////////////////////////////////////////////////////////////
30 /////////////////////////////////////////////////////////////////////////////////////////////
32 VOID
DiskReportError (BOOLEAN bError
)
34 bReportError
= bError
;
37 VOID
DiskError(PCSTR ErrorString
, ULONG ErrorCode
)
39 CHAR ErrorCodeString
[200];
41 if (bReportError
== FALSE
)
44 sprintf(ErrorCodeString
, "%s\n\nError Code: 0x%lx\nError: %s", ErrorString
, ErrorCode
, DiskGetErrorCodeString(ErrorCode
));
46 DPRINTM(DPRINT_DISK
, "%s\n", ErrorCodeString
);
48 UiMessageBox(ErrorCodeString
);
51 PCSTR
DiskGetErrorCodeString(ULONG ErrorCode
)
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";
82 default: return "unknown error code";
86 // This function is in arch/i386/i386disk.c
87 //BOOLEAN DiskReadLogicalSectors(ULONG DriveNumber, U64 SectorNumber, ULONG SectorCount, PVOID Buffer)
89 BOOLEAN
DiskIsDriveRemovable(ULONG DriveNumber
)
91 // Hard disks use drive numbers >= 0x80
92 // So if the drive number indicates a hard disk
94 // 0x49 is our magic ramdisk drive, so return FALSE for that too
95 if ((DriveNumber
>= 0x80) || (DriveNumber
== 0x49))
100 // Drive is a floppy diskette so return TRUE
104 BOOLEAN
DiskGetBootVolume(PULONG DriveNumber
, PULONGLONG StartSector
, PULONGLONG SectorCount
, int *FsType
)
106 PARTITION_TABLE_ENTRY PartitionTableEntry
;
108 ULONG ActivePartition
;
110 DPRINTM(DPRINT_FILESYSTEM
, "FsOpenVolume() DriveNumber: 0x%x PartitionNumber: 0x%x\n", BootDrive
, BootPartition
);
112 // Check and see if it is a floppy drive
113 // If so then just assume FAT12 file system type
114 if (DiskIsDriveRemovable(BootDrive
))
116 DPRINTM(DPRINT_FILESYSTEM
, "Drive is a floppy diskette drive. Assuming FAT12 file system.\n");
118 *DriveNumber
= BootDrive
;
120 *SectorCount
= 2 * 80 * 18; /* FIXME hardcoded for 1.44 Mb */
125 // Check for ISO9660 file system type
126 if (BootDrive
>= 0x80 && FsRecIsIso9660(BootDrive
))
128 DPRINTM(DPRINT_FILESYSTEM
, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n");
130 *DriveNumber
= BootDrive
;
133 *FsType
= FS_ISO9660
;
137 // Get the requested partition entry
138 if (BootPartition
== 0)
140 // Partition requested was zero which means the boot partition
141 if (! DiskGetActivePartitionEntry(BootDrive
, &PartitionTableEntry
, &ActivePartition
))
143 /* Try partition-less disk */
147 /* Check for valid partition */
148 else if (PartitionTableEntry
.SystemIndicator
== PARTITION_ENTRY_UNUSED
)
154 *StartSector
= PartitionTableEntry
.SectorCountBeforePartition
;
155 *SectorCount
= PartitionTableEntry
.PartitionSectorCount
;
158 else if (0xff == BootPartition
)
160 /* Partition-less disk */
166 // Get requested partition
167 if (! MachDiskGetPartitionEntry(BootDrive
, BootPartition
, &PartitionTableEntry
))
171 /* Check for valid partition */
172 else if (PartitionTableEntry
.SystemIndicator
== PARTITION_ENTRY_UNUSED
)
178 *StartSector
= PartitionTableEntry
.SectorCountBeforePartition
;
179 *SectorCount
= PartitionTableEntry
.PartitionSectorCount
;
183 // Try to recognize the file system
184 if (!FsRecognizeVolume(BootDrive
, *StartSector
, &VolumeType
))
189 *DriveNumber
= BootDrive
;
193 case PARTITION_FAT_12
:
194 case PARTITION_FAT_16
:
196 case PARTITION_XINT13
:
197 case PARTITION_FAT32
:
198 case PARTITION_FAT32_XINT13
:
216 DiskGetBootDevice(PULONG BootDevice
)
218 ((char *)BootDevice
)[0] = (char)BootDrive
;
219 ((char *)BootDevice
)[1] = (char)BootPartition
;
223 DiskBootingFromFloppy(VOID
)
225 return BootDrive
< 0x80;
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)
238 #define IsContainerPartition(P) \
239 ((P) == PARTITION_EXTENDED || \
240 (P) == PARTITION_XINT13_EXTENDED)
242 BOOLEAN
DiskGetSystemVolume(char *SystemPath
,
246 PULONGLONG StartSector
,
247 PULONGLONG SectorCount
,
250 ULONG PartitionNumber
;
251 PARTITION_TABLE_ENTRY PartitionTableEntry
;
254 unsigned i
, RosPartition
;
259 if (!DissectArcPath(SystemPath
, BootPath
, DriveNumber
, &PartitionNumber
))
263 if (NULL
!= RemainingPath
)
265 strcpy(RemainingPath
, BootPath
);
268 /* 0xff -> no partition table present, use whole device */
269 if (0xff == PartitionNumber
)
271 PartitionTableEntry
.SectorCountBeforePartition
= 0;
276 /* recalculate the boot partition for freeldr */
281 if (!MachDiskGetPartitionEntry(*DriveNumber
, ++i
, &PartitionTableEntry
))
285 if (!IsContainerPartition(PartitionTableEntry
.SystemIndicator
) &&
286 PartitionTableEntry
.SystemIndicator
!= PARTITION_ENTRY_UNUSED
)
288 if (++RosPartition
== PartitionNumber
)
296 /* Check for ISO9660 file system type */
297 if (*DriveNumber
>= 0x80 && FsRecIsIso9660(*DriveNumber
))
299 DPRINTM(DPRINT_FILESYSTEM
, "Drive is a cdrom drive. Assuming ISO-9660 file system.\n");
303 ((char *)Device
)[0] = (char)(*DriveNumber
);
304 ((char *)Device
)[1] = (char)i
;
308 *FsType
= FS_ISO9660
;
312 if (!FsRecognizeVolume(*DriveNumber
, PartitionTableEntry
.SectorCountBeforePartition
, &VolumeType
))
319 ((char *)Device
)[0] = (char)(*DriveNumber
);
320 ((char *)Device
)[1] = (char)i
;
322 *StartSector
= PartitionTableEntry
.SectorCountBeforePartition
;
323 *SectorCount
= PartitionTableEntry
.PartitionSectorCount
;
327 case PARTITION_FAT_12
:
328 case PARTITION_FAT_16
:
330 case PARTITION_XINT13
:
331 case PARTITION_FAT32
:
332 case PARTITION_FAT32_XINT13
:
350 DiskGetBootPath(char *BootPath
, unsigned Size
)
352 static char Path
[] = "multi(0)disk(0)";
355 _itoa(BootDrive
, Device
, 10);
356 if (Size
<= sizeof(Path
) + 6 + strlen(Device
))
360 strcpy(BootPath
, Path
);
361 strcat(BootPath
, MachDiskBootingFromFloppy() ? "fdisk" : "cdrom");
362 strcat(strcat(strcat(BootPath
, "("), Device
), ")");
368 DiskNormalizeSystemPath(char *SystemPath
, unsigned Size
)
371 ULONG PartitionNumber
;
373 PARTITION_TABLE_ENTRY PartEntry
;
376 if (!DissectArcPath(SystemPath
, BootPath
, &DriveNumber
, &PartitionNumber
))
381 if (0 != PartitionNumber
)
386 if (! DiskGetActivePartitionEntry(DriveNumber
,
389 PartitionNumber
< 1 || 9 < PartitionNumber
)
395 while ('\0' != *p
&& 0 != _strnicmp(p
, "partition(", 10)) {
399 if (NULL
== p
|| '0' != *(p
- 1)) {
402 *(p
- 1) = '0' + PartitionNumber
;
408 // This function is in arch/i386/i386disk.c
409 //VOID DiskStopFloppyMotor(VOID)
411 // This function is in arch/i386/i386disk.c
412 //ULONG DiskGetCacheableBlockCount(ULONG DriveNumber)