xref: /netbsd-src/sys/arch/hp300/stand/common/devopen.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: devopen.c,v 1.6 2003/11/14 16:52:40 tsutsui 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 static void usage(void);
80 static int devlookup(const char * ,int);
81 static int devparse(const char *, int *, int*, int*, int*, int*, char **);
82 
83 int
84 atoi(char *cp)
85 {
86 	int val = 0;
87 
88 	while (isdigit(*cp))
89 		val = val * 10 + (*cp++ - '0');
90 	return val;
91 }
92 
93 void
94 usage(void)
95 {
96 
97 	printf("Usage: device(adaptor, controller, drive, partition)file\n"
98 	    "       <device><unit><partitionletter>:file\n");
99 }
100 
101 static int
102 devlookup(d, len)
103 	const char *d;
104 	int len;
105 {
106 	struct devsw *dp = devsw;
107 	int i;
108 
109 	for (i = 0; i < ndevs; i++, dp++) {
110 		if (dp->dv_name && strncmp(dp->dv_name, d, len) == 0) {
111 			/*
112 			 * Set the filesystem and startup up according to
113 			 * the device being opened.
114 			 */
115 			switch (i) {
116 			case 0:	/* ct */
117 				memcpy(file_system, file_system_rawfs,
118 				    sizeof(struct fs_ops));
119 				break;
120 
121 			case 2:	/* rd */
122 			case 4:	/* sd */
123 				memcpy(file_system, file_system_ufs,
124 				    sizeof(struct fs_ops));
125 				break;
126 
127 			case 6:	/* le */
128 				memcpy(file_system, file_system_nfs,
129 				    sizeof(struct fs_ops));
130 				break;
131 
132 			default:
133 				/* Agh!  What happened?! */
134 				goto bad;
135 			}
136 		return i;
137 		}
138 	}
139 
140  bad:
141 	printf("No such device - Configured devices are:\n");
142 	for (dp = devsw, i = 0; i < ndevs; i++, dp++)
143 		if (dp->dv_name)
144 			printf(" %s", dp->dv_name);
145 	printf("\n");
146 	errno = ENODEV;
147 	return -1;
148 }
149 
150 /*
151  * Parse a device spec in one of two forms.
152  *
153  * dev(adapt, ctlr, unit, part)file
154  * [A-Za-z]*[0-9]*[A-Za-z]:file
155  *    dev   unit  part
156  */
157 static int
158 devparse(fname, dev, adapt, ctlr, unit, part, file)
159 	const char *fname;
160 	int *dev, *adapt, *ctlr, *unit, *part;
161 	char **file;
162 {
163 	int i;
164 	char *s, *args[4];
165 
166 	/* get device name */
167 	for (s = (char *)fname; *s && *s != '/' && *s != ':' && *s != '('; s++)
168 		;
169 
170 	/* first form */
171 	if (*s == '(') {
172 		/* lookup device and get index */
173 		if ((*dev = devlookup(fname, s - fname)) < 0)
174 			goto baddev;
175 
176 		/* tokenize device ident */
177 		args[0] = ++s;
178 		for (args[0] = s, i = 1; *s && *s != ')'; s++) {
179 			if (*s == ',')
180 				args[i++] = ++s;
181 		}
182 		switch(i) {
183 		case 4:
184 			*adapt = atoi(args[0]);
185 			*ctlr  = atoi(args[1]);
186 			*unit  = atoi(args[2]);
187 			*part  = atoi(args[3]);
188 			break;
189 		case 3:
190 			*ctlr  = atoi(args[0]);
191 			*unit  = atoi(args[1]);
192 			*part  = atoi(args[2]);
193 			break;
194 		case 2:
195 			*unit  = atoi(args[0]);
196 			*part  = atoi(args[1]);
197 			break;
198 		case 1:
199 			*part  = atoi(args[0]);
200 			break;
201 		case 0:
202 			break;
203 		}
204 		*file = ++s;
205 	}
206 
207 	/* second form */
208 	else if (*s == ':') {
209 		int temp;
210 
211 		/* isolate device */
212 		for (s = (char *)fname; *s != ':' && !isdigit(*s); s++);
213 
214 			/* lookup device and get index */
215 			if ((*dev = devlookup(fname, s - fname)) < 0)
216 				goto baddev;
217 
218 		/* isolate unit */
219 		if ((temp = atoi(s)) > 255)
220 			goto bad;
221 		*adapt = temp / 8;
222 		*ctlr = temp % 8;
223 		for (; isdigit(*s); s++)
224 			;
225 
226 		/* translate partition */
227 		if (!ispart(*s))
228 			goto bad;
229 
230 		*part = *s++ - 'a';
231 		if (*s != ':')
232 			goto bad;
233 		*file = ++s;
234 	}
235 
236 	/* no device present */
237 	else
238 		*file = (char *)fname;
239 
240 	/* return the remaining unparsed part as the file to boot */
241 	return 0;
242 
243  bad:
244 	usage();
245 
246  baddev:
247 	return -1;
248 }
249 
250 
251 int
252 devopen(f, fname, file)
253 	struct open_file *f;
254 	const char *fname;
255 	char **file;
256 {
257 	int error;
258 	int dev, adapt, ctlr, unit, part;
259 	struct devsw *dp = &devsw[0];
260 
261 	dev   = B_TYPE(bootdev);
262 	adapt = B_ADAPTOR(bootdev);
263 	ctlr  = B_CONTROLLER(bootdev);
264 	unit  = B_UNIT(bootdev);
265 	part  = B_PARTITION(bootdev);
266 
267 	if ((error = devparse(fname, &dev, &adapt, &ctlr, &unit, &part, file))
268 	    != 0)
269 		return error;
270 
271 	/*
272 	 * Set up filesystem type based on what device we're opening.
273 	 */
274 	switch (dev) {
275 	case 0:		/* ct */
276 		memcpy(file_system, file_system_rawfs, sizeof(struct fs_ops));
277 		break;
278 
279 	case 2:		/* rd */
280 	case 4:		/* sd */
281 		memcpy(file_system, file_system_ufs, sizeof(struct fs_ops));
282 		break;
283 
284 	case 6:		/* le */
285 		memcpy(file_system, file_system_nfs, sizeof(struct fs_ops));
286 		break;
287 
288 	default:
289 		/* XXX what else should we do here? */
290 		printf("WARNING: BOGUS BOOT DEV TYPE 0x%x!\n", dev);
291 		return EIO;
292 	}
293 
294 	dp = &devsw[dev];
295 
296 	if (!dp->dv_open)
297 		return ENODEV;
298 
299 	f->f_dev = dp;
300 
301 	if ((error = (*dp->dv_open)(f, adapt, ctlr, part)) == 0) {
302 		if ((error =
303 		    (*punitsw[dev].p_punit)(adapt, ctlr, &unit)) != 0) {
304 			goto bad;
305 		}
306 		opendev = MAKEBOOTDEV(dev, adapt, ctlr, unit, part);
307 		return 0;
308 	}
309 
310  bad:
311 	printf("%s(%d,%d,%d,%d): %s\n", devsw[dev].dv_name,
312 	    adapt, ctlr, unit, part, strerror(error));
313 
314 	return error;
315 }
316