Sync with trunk head
[reactos.git] / boot / freeldr / freeldr / reactos / arcname.c
1 /*
2 * FreeLoader - arcname.c
3 *
4 * Copyright (C) 2001 Brian Palmer <brianp@sginet.com>
5 * Copyright (C) 2001 Eric Kohl
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include <freeldr.h>
23
24 BOOLEAN DissectArcPath(CHAR *ArcPath, CHAR *BootPath, ULONG* BootDrive, ULONG* BootPartition)
25 {
26 char *p;
27
28 //
29 // Detect ramdisk path
30 //
31 if (_strnicmp(ArcPath, "ramdisk(0)", 10) == 0)
32 {
33 //
34 // Magic value for ramdisks
35 //
36 *BootDrive = 0x49;
37 *BootPartition = 1;
38
39 //
40 // Get the path
41 //
42 p = ArcPath + 11;
43 strcpy(BootPath, p);
44 return TRUE;
45 }
46
47 if (_strnicmp(ArcPath, "multi(0)disk(0)", 15) != 0)
48 return FALSE;
49
50 p = ArcPath + 15;
51 if (_strnicmp(p, "fdisk(", 6) == 0)
52 {
53 /*
54 * floppy disk path:
55 * multi(0)disk(0)fdisk(x)\path
56 */
57 p = p + 6;
58 *BootDrive = atoi(p);
59 p = strchr(p, ')');
60 if (p == NULL)
61 return FALSE;
62 p++;
63 *BootPartition = 0xff;
64 }
65 else if (_strnicmp(p, "cdrom(", 6) == 0)
66 {
67 /*
68 * cdrom path:
69 * multi(0)disk(0)cdrom(x)\path
70 */
71 p = p + 6;
72 *BootDrive = atoi(p);
73 p = strchr(p, ')');
74 if (p == NULL)
75 return FALSE;
76 p++;
77 *BootPartition = 0xff;
78 }
79 else if (_strnicmp(p, "rdisk(", 6) == 0)
80 {
81 /*
82 * hard disk path:
83 * multi(0)disk(0)rdisk(x)partition(y)\path
84 */
85 p = p + 6;
86 *BootDrive = atoi(p) + 0x80;
87 p = strchr(p, ')');
88 if ((p == NULL) || (_strnicmp(p, ")partition(", 11) != 0))
89 return FALSE;
90 p = p + 11;
91 *BootPartition = atoi(p);
92 p = strchr(p, ')');
93 if (p == NULL)
94 return FALSE;
95 p++;
96 }
97 else
98 {
99 return FALSE;
100 }
101
102 strcpy(BootPath, p);
103
104 return TRUE;
105 }
106
107 /* PathSyntax: scsi() = 0, multi() = 1, ramdisk() = 2 */
108 BOOLEAN
109 DissectArcPath2(
110 IN CHAR* ArcPath,
111 OUT ULONG* x,
112 OUT ULONG* y,
113 OUT ULONG* z,
114 OUT ULONG* Partition,
115 OUT ULONG *PathSyntax)
116 {
117 /* Detect ramdisk() */
118 if (_strnicmp(ArcPath, "ramdisk(0)", 10) == 0)
119 {
120 *x = *y = *z = 0;
121 *Partition = 1;
122 *PathSyntax = 2;
123 return TRUE;
124 }
125 /* Detect scsi()disk()rdisk()partition() */
126 else if (sscanf(ArcPath, "scsi(%lu)disk(%lu)rdisk(%lu)partition(%lu)", x, y, z, Partition) == 4)
127 {
128 *PathSyntax = 0;
129 return TRUE;
130 }
131 /* Detect scsi()cdrom()fdisk() */
132 else if (sscanf(ArcPath, "scsi(%lu)cdrom(%lu)fdisk(%lu)", x, y, z) == 3)
133 {
134 *Partition = 0;
135 *PathSyntax = 0;
136 return TRUE;
137 }
138 /* Detect multi()disk()rdisk()partition() */
139 else if (sscanf(ArcPath, "multi(%lu)disk(%lu)rdisk(%lu)partition(%lu)", x, y, z, Partition) == 4)
140 {
141 *PathSyntax = 1;
142 return TRUE;
143 }
144 /* Detect multi()disk()cdrom() */
145 else if (sscanf(ArcPath, "multi(%lu)disk(%lu)cdrom(%lu)", x, y, z) == 3)
146 {
147 *Partition = 1;
148 *PathSyntax = 1;
149 return TRUE;
150 }
151 /* Detect multi()disk()fdisk() */
152 else if (sscanf(ArcPath, "multi(%lu)disk(%lu)fdisk(%lu)", x, y, z) == 3)
153 {
154 *Partition = 1;
155 *PathSyntax = 1;
156 return TRUE;
157 }
158
159 /* Unknown syntax */
160 return FALSE;
161 }
162
163 VOID ConstructArcPath(PCHAR ArcPath, PCHAR SystemFolder, ULONG Disk, ULONG Partition)
164 {
165 char tmp[50];
166
167 strcpy(ArcPath, "multi(0)disk(0)");
168
169 if (Disk < 0x80)
170 {
171 /*
172 * floppy disk path:
173 * multi(0)disk(0)fdisk(x)\path
174 */
175 sprintf(tmp, "fdisk(%d)", (int) Disk);
176 strcat(ArcPath, tmp);
177 }
178 else
179 {
180 /*
181 * hard disk path:
182 * multi(0)disk(0)rdisk(x)partition(y)\path
183 */
184 sprintf(tmp, "rdisk(%d)partition(%d)", (int) (Disk - 0x80), (int) Partition);
185 strcat(ArcPath, tmp);
186 }
187
188 if (SystemFolder[0] == '\\' || SystemFolder[0] == '/')
189 {
190 strcat(ArcPath, SystemFolder);
191 }
192 else
193 {
194 strcat(ArcPath, "\\");
195 strcat(ArcPath, SystemFolder);
196 }
197 }
198
199 ULONG ConvertArcNameToBiosDriveNumber(PCHAR ArcPath)
200 {
201 char * p;
202 ULONG DriveNumber = 0;
203
204 if (_strnicmp(ArcPath, "multi(0)disk(0)", 15) != 0)
205 return 0;
206
207 p = ArcPath + 15;
208 if (_strnicmp(p, "fdisk(", 6) == 0)
209 {
210 /*
211 * floppy disk path:
212 * multi(0)disk(0)fdisk(x)\path
213 */
214 p = p + 6;
215 DriveNumber = atoi(p);
216 }
217 else if (_strnicmp(p, "rdisk(", 6) == 0)
218 {
219 /*
220 * hard disk path:
221 * multi(0)disk(0)rdisk(x)partition(y)\path
222 */
223 p = p + 6;
224 DriveNumber = atoi(p) + 0x80;
225 }
226
227 return DriveNumber;
228 }