No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hp300 / stand / common / devopen.c
blobd4cf883643135a4d9e463a0473a59a3242532b95
1 /* $NetBSD: devopen.c,v 1.9 2005/12/11 12:17:19 christos Exp $ */
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 /*-
33 * Copyright (c) 1993 John Brezak
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. The name of the author may not be used to endorse or promote products
45 * derived from this software without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
49 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
51 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
52 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
56 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
60 #include <sys/param.h>
61 #include <sys/reboot.h>
63 #include <lib/libkern/libkern.h>
65 #include <lib/libsa/stand.h>
66 #include <hp300/stand/common/samachdep.h>
68 u_int opendev;
70 #define ispart(c) ((c) >= 'a' && (c) <= 'h')
72 static void usage(void);
73 static int devlookup(const char * ,int);
74 static int devparse(const char *, int *, int*, int*, int*, int*, char **);
76 int
77 atoi(char *cp)
79 int val = 0;
81 while (isdigit((unsigned char)*cp))
82 val = val * 10 + (*cp++ - '0');
83 return val;
86 void
87 usage(void)
90 printf("Usage: device(adaptor, controller, drive, partition)file\n"
91 " <device><unit><partitionletter>:file\n");
94 static int
95 devlookup(const char *d, int len)
97 struct devsw *dp = devsw;
98 int i;
100 for (i = 0; i < ndevs; i++, dp++) {
101 if (dp->dv_name && strncmp(dp->dv_name, d, len) == 0) {
103 * Set the filesystem and startup up according to
104 * the device being opened.
106 switch (i) {
107 case 0: /* ct */
108 memcpy(file_system, file_system_rawfs,
109 sizeof(struct fs_ops));
110 break;
112 case 2: /* rd */
113 case 4: /* sd */
114 memcpy(file_system, file_system_ufs,
115 sizeof(struct fs_ops));
116 break;
118 case 6: /* le */
119 memcpy(file_system, file_system_nfs,
120 sizeof(struct fs_ops));
121 break;
123 default:
124 /* Agh! What happened?! */
125 goto bad;
127 return i;
131 bad:
132 printf("No such device - Configured devices are:\n");
133 for (dp = devsw, i = 0; i < ndevs; i++, dp++)
134 if (dp->dv_name)
135 printf(" %s", dp->dv_name);
136 printf("\n");
137 errno = ENODEV;
138 return -1;
142 * Parse a device spec in one of two forms.
144 * dev(adapt, ctlr, unit, part)file
145 * [A-Za-z]*[0-9]*[A-Za-z]:file
146 * dev unit part
148 static int
149 devparse(const char *fname, int *dev, int *adapt, int *ctlr, int *unit,
150 int *part, char **file)
152 int i;
153 char *s, *args[4];
155 /* get device name */
156 for (s = (char *)fname; *s && *s != '/' && *s != ':' && *s != '('; s++)
159 /* first form */
160 if (*s == '(') {
161 /* lookup device and get index */
162 if ((*dev = devlookup(fname, s - fname)) < 0)
163 goto baddev;
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;
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;
193 *file = ++s;
196 /* second form */
197 else if (*s == ':') {
198 int temp;
200 /* isolate device */
201 for (s = (char *)fname; *s != ':' && !isdigit(*s); s++);
203 /* lookup device and get index */
204 if ((*dev = devlookup(fname, s - fname)) < 0)
205 goto baddev;
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++)
215 /* translate partition */
216 if (!ispart(*s))
217 goto bad;
219 *part = *s++ - 'a';
220 if (*s != ':')
221 goto bad;
222 *file = ++s;
225 /* no device present */
226 else
227 *file = (char *)fname;
229 /* return the remaining unparsed part as the file to boot */
230 return 0;
232 bad:
233 usage();
235 baddev:
236 return -1;
241 devopen(struct open_file *f, const char *fname, char **file)
243 int error;
244 int dev, adapt, ctlr, unit, part;
245 struct devsw *dp = &devsw[0];
247 dev = B_TYPE(bootdev);
248 adapt = B_ADAPTOR(bootdev);
249 ctlr = B_CONTROLLER(bootdev);
250 unit = B_UNIT(bootdev);
251 part = B_PARTITION(bootdev);
253 if ((error = devparse(fname, &dev, &adapt, &ctlr, &unit, &part, file))
254 != 0)
255 return error;
258 * Set up filesystem type based on what device we're opening.
260 switch (dev) {
261 case 0: /* ct */
262 memcpy(file_system, file_system_rawfs, sizeof(struct fs_ops));
263 break;
265 case 2: /* rd */
266 case 4: /* sd */
267 memcpy(file_system, file_system_ufs, sizeof(struct fs_ops));
268 break;
270 case 6: /* le */
271 memcpy(file_system, file_system_nfs, sizeof(struct fs_ops));
272 break;
274 default:
275 /* XXX what else should we do here? */
276 printf("WARNING: BOGUS BOOT DEV TYPE 0x%x!\n", dev);
277 return EIO;
280 dp = &devsw[dev];
282 if (!dp->dv_open)
283 return ENODEV;
285 f->f_dev = dp;
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;
292 opendev = MAKEBOOTDEV(dev, adapt, ctlr, unit, part);
293 return 0;
296 bad:
297 printf("%s(%d,%d,%d,%d): %s\n", devsw[dev].dv_name,
298 adapt, ctlr, unit, part, strerror(error));
300 return error;