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