b7ce3295497660ed27198b35dc6f884a28c07643
[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 #include <debug.h>
23
24 #undef UNIMPLEMENTED
25 #define UNIMPLEMENTED BugCheck((DPRINT_WARNING, "Unimplemented\n"));
26
27 static BOOLEAN bReportError = TRUE;
28
29 /////////////////////////////////////////////////////////////////////////////////////////////
30 // FUNCTIONS
31 /////////////////////////////////////////////////////////////////////////////////////////////
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", ErrorString, ErrorCode, DiskGetErrorCodeString(ErrorCode));
46
47 DPRINTM(DPRINT_DISK, "%s\n", ErrorCodeString);
48
49 UiMessageBox(ErrorCodeString);
50 }
51
52 PCSTR DiskGetErrorCodeString(ULONG ErrorCode)
53 {
54 switch (ErrorCode)
55 {
56 case 0x00: return "no error";
57 case 0x01: return "bad command passed to driver";
58 case 0x02: return "address mark not found or bad sector";
59 case 0x03: return "diskette write protect error";
60 case 0x04: return "sector not found";
61 case 0x05: return "fixed disk reset failed";
62 case 0x06: return "diskette changed or removed";
63 case 0x07: return "bad fixed disk parameter table";
64 case 0x08: return "DMA overrun";
65 case 0x09: return "DMA access across 64k boundary";
66 case 0x0A: return "bad fixed disk sector flag";
67 case 0x0B: return "bad fixed disk cylinder";
68 case 0x0C: return "unsupported track/invalid media";
69 case 0x0D: return "invalid number of sectors on fixed disk format";
70 case 0x0E: return "fixed disk controlled data address mark detected";
71 case 0x0F: return "fixed disk DMA arbitration level out of range";
72 case 0x10: return "ECC/CRC error on disk read";
73 case 0x11: return "recoverable fixed disk data error, data fixed by ECC";
74 case 0x20: return "controller error (NEC for floppies)";
75 case 0x40: return "seek failure";
76 case 0x80: return "time out, drive not ready";
77 case 0xAA: return "fixed disk drive not ready";
78 case 0xBB: return "fixed disk undefined error";
79 case 0xCC: return "fixed disk write fault on selected drive";
80 case 0xE0: return "fixed disk status error/Error reg = 0";
81 case 0xFF: return "sense operation failed";
82
83 default: return "unknown error code";
84 }
85 }
86
87 // This function is in arch/i386/i386disk.c
88 //BOOLEAN DiskReadLogicalSectors(ULONG DriveNumber, U64 SectorNumber, ULONG SectorCount, PVOID Buffer)
89
90 BOOLEAN DiskIsDriveRemovable(ULONG DriveNumber)
91 {
92 // Hard disks use drive numbers >= 0x80
93 // So if the drive number indicates a hard disk
94 // then return FALSE
95 // 0x49 is our magic ramdisk drive, so return FALSE for that too
96 if ((DriveNumber >= 0x80) || (DriveNumber == 0x49))
97 {
98 return FALSE;
99 }
100
101 // Drive is a floppy diskette so return TRUE
102 return TRUE;
103 }
104
105 BOOLEAN
106 DiskGetBootPath(char *BootPath, unsigned Size)
107 {
108 static char Path[] = "multi(0)disk(0)";
109 char Device[4];
110 char Partition[4];
111 PARTITION_TABLE_ENTRY PartitionEntry;
112 MASTER_BOOT_RECORD MasterBootRecord;
113
114 if (BootDrive < 0x80)
115 {
116 /* This is a floppy */
117
118 if (Size <= sizeof(Path) + 7 + strlen(Device))
119 {
120 return FALSE;
121 }
122
123 strcpy(BootPath, Path);
124
125 strcat(BootPath, "fdisk");
126
127 _itoa(BootDrive, Device, 10);
128 strcat(BootPath, "(");
129 strcat(BootPath, Device);
130 strcat(BootPath, ")");
131 }
132 /* FIXME */
133 else if (DiskReadBootRecord(BootDrive, 0, &MasterBootRecord))
134 {
135 /* This is a hard disk */
136
137 if (!DiskGetActivePartitionEntry(BootDrive, &PartitionEntry, &BootPartition))
138 {
139 DbgPrint("Invalid active partition information\n");
140 return FALSE;
141 }
142
143 if (Size <= sizeof(Path) + 18 + strlen(Device) + strlen(Partition))
144 {
145 return FALSE;
146 }
147
148 strcpy(BootPath, Path);
149
150 strcat(BootPath, "rdisk");
151
152 _itoa(BootDrive - 0x80, Device, 10);
153 strcat(BootPath, "(");
154 strcat(BootPath, Device);
155 strcat(BootPath, ")");
156
157 _itoa(BootPartition, Partition, 10);
158 strcat(BootPath, "partition(");
159 strcat(BootPath, Partition);
160 strcat(BootPath, ")");
161 }
162 else
163 {
164 /* This is a CD-ROM drive */
165
166 if (Size <= sizeof(Path) + 7 + strlen(Device))
167 {
168 return FALSE;
169 }
170
171 strcpy(BootPath, Path);
172
173 strcat(BootPath, "cdrom");
174
175 _itoa(BootDrive - 0x80, Device, 10);
176 strcat(BootPath, "(");
177 strcat(BootPath, Device);
178 strcat(BootPath, ")");
179 }
180
181 return TRUE;
182 }
183
184 // This function is in arch/i386/i386disk.c
185 //VOID DiskStopFloppyMotor(VOID)
186
187 // This function is in arch/i386/i386disk.c
188 //ULONG DiskGetCacheableBlockCount(ULONG DriveNumber)
189
190 #endif