devopen.c revision 1.3 1 /* $NetBSD: devopen.c,v 1.3 2001/05/13 00:01:45 gmcgarry Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1993 John Brezak
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. The name of the author may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
56 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
57 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
58 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
59 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
60 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
62 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
63 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
65 */
66
67 #include <sys/param.h>
68 #include <sys/reboot.h>
69
70 #include <lib/libkern/libkern.h>
71
72 #include <lib/libsa/stand.h>
73 #include <hp300/stand/common/samachdep.h>
74
75 u_int opendev;
76
77 #define ispart(c) ((c) >= 'a' && (c) <= 'h')
78
79 atoi(char *cp)
80 {
81 int val = 0;
82 while(isdigit(*cp))
83 val = val * 10 + (*cp++ - '0');
84 return(val);
85 }
86
87 usage()
88 {
89 printf("\
90 Usage: device(adaptor, controller, drive, partition)file\n\
91 <device><unit><partitionletter>:file\n\
92 ");
93 }
94
95 devlookup(d, len)
96 const char *d;
97 int len;
98 {
99 struct devsw *dp = devsw;
100 int i;
101
102 for (i = 0; i < ndevs; i++, dp++) {
103 if (dp->dv_name && strncmp(dp->dv_name, d, len) == 0) {
104 /*
105 * Set the filesystem and startup up according to the device
106 * being opened.
107 */
108 switch (i) {
109 case 0: /* ct */
110 bcopy(file_system_rawfs, file_system, sizeof(struct fs_ops));
111 break;
112
113 case 2: /* rd */
114 case 4: /* sd */
115 bcopy(file_system_ufs, file_system, sizeof(struct fs_ops));
116 break;
117
118 case 6: /* le */
119 bcopy(file_system_nfs, file_system, sizeof(struct fs_ops));
120 break;
121
122 default:
123 /* Agh! What happened?! */
124 goto bad;
125 }
126 return(i);
127 }
128 }
129
130 bad:
131 printf("No such device - Configured devices are:\n");
132 for (dp = devsw, i = 0; i < ndevs; i++, dp++)
133 if (dp->dv_name)
134 printf(" %s", dp->dv_name);
135 printf("\n");
136 errno = ENODEV;
137 return(-1);
138 }
139
140 /*
141 * Parse a device spec in one of two forms.
142 *
143 * dev(adapt, ctlr, unit, part)file
144 * [A-Za-z]*[0-9]*[A-Za-z]:file
145 * dev unit part
146 */
147 devparse(fname, dev, adapt, ctlr, unit, part, file)
148 const char *fname;
149 int *dev, *adapt, *ctlr, *unit, *part;
150 char **file;
151 {
152 int *argp, i;
153 char *s, *args[4];
154
155 /* get device name and make lower case */
156 for (s = (char *)fname; *s && *s != '/' && *s != ':' && *s != '('; s++)
157 if (isupper(*s)) *s = tolower(*s);
158
159 /* first form */
160 if (*s == '(') {
161 /* lookup device and get index */
162 if ((*dev = devlookup(fname, s - fname)) < 0)
163 goto baddev;
164
165 /* tokenize device ident */
166 args[0] = ++s;
167 for (args[0] = s, i = 1; *s && *s != ')'; s++) {
168 if (*s == ',')
169 args[i++] = ++s;
170 }
171 switch(i) {
172 case 4:
173 *adapt = atoi(args[0]);
174 *ctlr = atoi(args[1]);
175 *unit = atoi(args[2]);
176 *part = atoi(args[3]);
177 break;
178 case 3:
179 *ctlr = atoi(args[0]);
180 *unit = atoi(args[1]);
181 *part = atoi(args[2]);
182 break;
183 case 2:
184 *unit = atoi(args[0]);
185 *part = atoi(args[1]);
186 break;
187 case 1:
188 *part = atoi(args[0]);
189 break;
190 case 0:
191 break;
192 }
193 *file = ++s;
194 }
195
196 /* second form */
197 else if (*s == ':') {
198 int temp;
199
200 /* isolate device */
201 for (s = (char *)fname; *s != ':' && !isdigit(*s); s++);
202
203 /* lookup device and get index */
204 if ((*dev = devlookup(fname, s - fname)) < 0)
205 goto baddev;
206
207 /* isolate unit */
208 if ((temp = atoi(s)) > 255)
209 goto bad;
210 *adapt = temp / 8;
211 *ctlr = temp % 8;
212 for (; isdigit(*s); s++);
213
214 /* translate partition */
215 if (!ispart(*s))
216 goto bad;
217
218 *part = *s++ - 'a';
219 if (*s != ':')
220 goto bad;
221 *file = ++s;
222 }
223
224 /* no device present */
225 else
226 *file = (char *)fname;
227
228 /* return the remaining unparsed part as the file to boot */
229 return(0);
230
231 bad:
232 usage();
233
234 baddev:
235 return(-1);
236 }
237
238
239 devopen(f, fname, file)
240 struct open_file *f;
241 const char *fname;
242 char **file;
243 {
244 int n, error;
245 int dev, adapt, ctlr, unit, part;
246 struct devsw *dp = &devsw[0];
247
248 dev = B_TYPE(bootdev);
249 adapt = B_ADAPTOR(bootdev);
250 ctlr = B_CONTROLLER(bootdev);
251 unit = B_UNIT(bootdev);
252 part = B_PARTITION(bootdev);
253
254 if (error = devparse(fname, &dev, &adapt, &ctlr, &unit, &part, file))
255 return(error);
256
257 /*
258 * Set up filesystem type based on what device we're opening.
259 */
260 switch (dev) {
261 case 0: /* ct */
262 bcopy(file_system_rawfs, file_system, sizeof(struct fs_ops));
263 break;
264
265 case 2: /* rd */
266 case 4: /* sd */
267 bcopy(file_system_ufs, file_system, sizeof(struct fs_ops));
268 break;
269
270 case 6: /* le */
271 bcopy(file_system_nfs, file_system, sizeof(struct fs_ops));
272 break;
273
274 default:
275 /* XXX what else should we do here? */
276 printf("WARNING: BOGUS BOOT DEV TYPE 0x%x!\n", dev);
277 return (EIO);
278 }
279
280 dp = &devsw[dev];
281
282 if (!dp->dv_open)
283 return(ENODEV);
284
285 f->f_dev = dp;
286
287 if ((error = (*dp->dv_open)(f, adapt, ctlr, part)) == 0) {
288 if ((error =
289 (*punitsw[dev].p_punit)(adapt, ctlr, &unit)) != 0) {
290 goto bad;
291 }
292 opendev = MAKEBOOTDEV(dev, adapt, ctlr, unit, part);
293 return(0);
294 }
295
296 bad:
297 printf("%s(%d,%d,%d,%d): %s\n", devsw[dev].dv_name,
298 adapt, ctlr, unit, part, strerror(error));
299
300 return(error);
301 }
302