e9579a0beabf333c02b2b03819232f82ae5fa840
[reactos.git] / 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef _M_ARM
21 #include <freeldr.h>
22
23 #include <debug.h>
24
25 DBG_DEFAULT_CHANNEL(DISK);
26
27 CHAR FrldrBootPath[MAX_PATH] = "";
28
29 static BOOLEAN bReportError = TRUE;
30
31 /* FUNCTIONS *****************************************************************/
32
33 VOID DiskReportError(BOOLEAN bError)
34 {
35 bReportError = bError;
36 }
37
38 VOID DiskError(PCSTR ErrorString, ULONG ErrorCode)
39 {
40 CHAR ErrorCodeString[200];
41
42 if (bReportError == FALSE)
43 return;
44
45 sprintf(ErrorCodeString, "%s\n\nError Code: 0x%lx\nError: %s",
46 ErrorString, ErrorCode, DiskGetErrorCodeString(ErrorCode));
47
48 TRACE("%s\n", ErrorCodeString);
49
50 UiMessageBox(ErrorCodeString);
51 }
52
53 PCSTR DiskGetErrorCodeString(ULONG ErrorCode)
54 {
55 switch (ErrorCode)
56 {
57 case 0x00: return "no error";
58 case 0x01: return "bad command passed to driver";
59 case 0x02: return "address mark not found or bad sector";
60 case 0x03: return "diskette write protect error";
61 case 0x04: return "sector not found";
62 case 0x05: return "fixed disk reset failed";
63 case 0x06: return "diskette changed or removed";
64 case 0x07: return "bad fixed disk parameter table";
65 case 0x08: return "DMA overrun";
66 case 0x09: return "DMA access across 64k boundary";
67 case 0x0A: return "bad fixed disk sector flag";
68 case 0x0B: return "bad fixed disk cylinder";
69 case 0x0C: return "unsupported track/invalid media";
70 case 0x0D: return "invalid number of sectors on fixed disk format";
71 case 0x0E: return "fixed disk controlled data address mark detected";
72 case 0x0F: return "fixed disk DMA arbitration level out of range";
73 case 0x10: return "ECC/CRC error on disk read";
74 case 0x11: return "recoverable fixed disk data error, data fixed by ECC";
75 case 0x20: return "controller error (NEC for floppies)";
76 case 0x40: return "seek failure";
77 case 0x80: return "time out, drive not ready";
78 case 0xAA: return "fixed disk drive not ready";
79 case 0xBB: return "fixed disk undefined error";
80 case 0xCC: return "fixed disk write fault on selected drive";
81 case 0xE0: return "fixed disk status error/Error reg = 0";
82 case 0xFF: return "sense operation failed";
83
84 default: return "unknown error code";
85 }
86 }
87
88 BOOLEAN DiskIsDriveRemovable(UCHAR DriveNumber)
89 {
90 /*
91 * Hard disks use drive numbers >= 0x80 . So if the drive number
92 * indicates a hard disk then return FALSE.
93 * 0x49 is our magic ramdisk drive, so return FALSE for that too.
94 */
95 if ((DriveNumber >= 0x80) || (DriveNumber == 0x49))
96 return FALSE;
97
98 /* The drive is a floppy diskette so return TRUE */
99 return TRUE;
100 }
101
102 BOOLEAN DiskGetBootPath(OUT PCHAR BootPath, IN ULONG Size)
103 {
104 if (*FrldrBootPath)
105 goto Done;
106
107 if (Size)
108 BootPath[0] = ANSI_NULL;
109
110 /* 0x49 is our magic ramdisk drive, so try to detect it first */
111 if (FrldrBootDrive == 0x49)
112 {
113 /* This is the ramdisk. See ArmDiskGetBootPath too... */
114 // sprintf(FrldrBootPath, "ramdisk(%u)", 0);
115 strcpy(FrldrBootPath, "ramdisk(0)");
116 }
117 else if (FrldrBootDrive < 0x80)
118 {
119 /* This is a floppy */
120 sprintf(FrldrBootPath, "multi(0)disk(0)fdisk(%u)", FrldrBootDrive);
121 }
122 else if (FrldrBootPartition == 0xFF)
123 {
124 /* Boot Partition 0xFF is the magic value that indicates booting from CD-ROM (see isoboot.S) */
125 sprintf(FrldrBootPath, "multi(0)disk(0)cdrom(%u)", FrldrBootDrive - 0x80);
126 }
127 else
128 {
129 ULONG BootPartition;
130 PARTITION_TABLE_ENTRY PartitionEntry;
131
132 /* This is a hard disk */
133 if (!DiskGetBootPartitionEntry(FrldrBootDrive, &PartitionEntry, &BootPartition))
134 {
135 ERR("Failed to get boot partition entry\n");
136 return FALSE;
137 }
138
139 FrldrBootPartition = BootPartition;
140
141 sprintf(FrldrBootPath, "multi(0)disk(0)rdisk(%u)partition(%lu)",
142 FrldrBootDrive - 0x80, FrldrBootPartition);
143 }
144
145 Done:
146 /* Copy back the buffer */
147 if (Size < strlen(FrldrBootPath) + 1)
148 return FALSE;
149 strncpy(BootPath, FrldrBootPath, Size);
150 return TRUE;
151 }
152
153 #endif