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