xref: /netbsd-src/sys/arch/hp300/stand/common/devopen.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: devopen.c,v 1.4 2002/08/04 00:44:58 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     /* first form */
156     if (*s == '(') {
157 	/* lookup device and get index */
158 	if ((*dev = devlookup(fname, s - fname)) < 0)
159 	    goto baddev;
160 
161 	/* tokenize device ident */
162 	args[0] = ++s;
163 	for (args[0] = s, i = 1; *s && *s != ')'; s++) {
164 	    if (*s == ',')
165 		args[i++] = ++s;
166 	}
167 	switch(i) {
168 	case 4:
169 	    *adapt = atoi(args[0]);
170 	    *ctlr  = atoi(args[1]);
171 	    *unit  = atoi(args[2]);
172 	    *part  = atoi(args[3]);
173 	    break;
174 	case 3:
175 	    *ctlr  = atoi(args[0]);
176 	    *unit  = atoi(args[1]);
177 	    *part  = atoi(args[2]);
178 	    break;
179 	case 2:
180 	    *unit  = atoi(args[0]);
181 	    *part  = atoi(args[1]);
182 	    break;
183 	case 1:
184 	    *part  = atoi(args[0]);
185 	    break;
186 	case 0:
187 	    break;
188 	}
189 	*file = ++s;
190     }
191 
192     /* second form */
193     else if (*s == ':') {
194 	int temp;
195 
196 	/* isolate device */
197 	for (s = (char *)fname; *s != ':' && !isdigit(*s); s++);
198 
199 	/* lookup device and get index */
200 	if ((*dev = devlookup(fname, s - fname)) < 0)
201 	    goto baddev;
202 
203 	/* isolate unit */
204 	if ((temp = atoi(s)) > 255)
205 	    goto bad;
206 	*adapt = temp / 8;
207 	*ctlr = temp % 8;
208 	for (; isdigit(*s); s++);
209 
210 	/* translate partition */
211 	if (!ispart(*s))
212 	    goto bad;
213 
214 	*part = *s++ - 'a';
215 	if (*s != ':')
216 	    goto bad;
217 	*file = ++s;
218     }
219 
220     /* no device present */
221     else
222 	*file = (char *)fname;
223 
224     /* return the remaining unparsed part as the file to boot */
225     return(0);
226 
227  bad:
228     usage();
229 
230  baddev:
231     return(-1);
232 }
233 
234 
235 devopen(f, fname, file)
236 	struct open_file *f;
237 	const char *fname;
238 	char **file;
239 {
240 	int n, error;
241 	int dev, adapt, ctlr, unit, part;
242 	struct devsw *dp = &devsw[0];
243 
244 	dev   = B_TYPE(bootdev);
245 	adapt = B_ADAPTOR(bootdev);
246 	ctlr  = B_CONTROLLER(bootdev);
247 	unit  = B_UNIT(bootdev);
248 	part  = B_PARTITION(bootdev);
249 
250 	if (error = devparse(fname, &dev, &adapt, &ctlr, &unit, &part, file))
251 	    return(error);
252 
253 	/*
254 	 * Set up filesystem type based on what device we're opening.
255 	 */
256 	switch (dev) {
257 	case 0:		/* ct */
258 		bcopy(file_system_rawfs, file_system, sizeof(struct fs_ops));
259 		break;
260 
261 	case 2:		/* rd */
262 	case 4:		/* sd */
263 		bcopy(file_system_ufs, file_system, sizeof(struct fs_ops));
264 		break;
265 
266 	case 6:		/* le */
267 		bcopy(file_system_nfs, file_system, sizeof(struct fs_ops));
268 		break;
269 
270 	default:
271 		/* XXX what else should we do here? */
272 		printf("WARNING: BOGUS BOOT DEV TYPE 0x%x!\n", dev);
273 		return (EIO);
274 	}
275 
276 	dp = &devsw[dev];
277 
278 	if (!dp->dv_open)
279 		return(ENODEV);
280 
281 	f->f_dev = dp;
282 
283 	if ((error = (*dp->dv_open)(f, adapt, ctlr, part)) == 0) {
284 		if ((error =
285 		    (*punitsw[dev].p_punit)(adapt, ctlr, &unit)) != 0) {
286 			goto bad;
287 		}
288 		opendev = MAKEBOOTDEV(dev, adapt, ctlr, unit, part);
289 		return(0);
290 	}
291 
292  bad:
293 	printf("%s(%d,%d,%d,%d): %s\n", devsw[dev].dv_name,
294 	    adapt, ctlr, unit, part, strerror(error));
295 
296 	return(error);
297 }
298