1 /* $NetBSD: devopen.c,v 1.9 2005/12/11 12:17:19 christos Exp $ */
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
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>
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 **);
81 while (isdigit((unsigned char)*cp
))
82 val
= val
* 10 + (*cp
++ - '0');
90 printf("Usage: device(adaptor, controller, drive, partition)file\n"
91 " <device><unit><partitionletter>:file\n");
95 devlookup(const char *d
, int len
)
97 struct devsw
*dp
= devsw
;
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.
108 memcpy(file_system
, file_system_rawfs
,
109 sizeof(struct fs_ops
));
114 memcpy(file_system
, file_system_ufs
,
115 sizeof(struct fs_ops
));
119 memcpy(file_system
, file_system_nfs
,
120 sizeof(struct fs_ops
));
124 /* Agh! What happened?! */
132 printf("No such device - Configured devices are:\n");
133 for (dp
= devsw
, i
= 0; i
< ndevs
; i
++, dp
++)
135 printf(" %s", dp
->dv_name
);
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
149 devparse(const char *fname
, int *dev
, int *adapt
, int *ctlr
, int *unit
,
150 int *part
, char **file
)
155 /* get device name */
156 for (s
= (char *)fname
; *s
&& *s
!= '/' && *s
!= ':' && *s
!= '('; s
++)
161 /* lookup device and get index */
162 if ((*dev
= devlookup(fname
, s
- fname
)) < 0)
165 /* tokenize device ident */
167 for (args
[0] = s
, i
= 1; *s
&& *s
!= ')'; s
++) {
173 *adapt
= atoi(args
[0]);
174 *ctlr
= atoi(args
[1]);
175 *unit
= atoi(args
[2]);
176 *part
= atoi(args
[3]);
179 *ctlr
= atoi(args
[0]);
180 *unit
= atoi(args
[1]);
181 *part
= atoi(args
[2]);
184 *unit
= atoi(args
[0]);
185 *part
= atoi(args
[1]);
188 *part
= atoi(args
[0]);
197 else if (*s
== ':') {
201 for (s
= (char *)fname
; *s
!= ':' && !isdigit(*s
); s
++);
203 /* lookup device and get index */
204 if ((*dev
= devlookup(fname
, s
- fname
)) < 0)
208 if ((temp
= atoi(s
)) > 255)
212 for (; isdigit(*s
); s
++)
215 /* translate partition */
225 /* no device present */
227 *file
= (char *)fname
;
229 /* return the remaining unparsed part as the file to boot */
241 devopen(struct open_file
*f
, const char *fname
, char **file
)
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
))
258 * Set up filesystem type based on what device we're opening.
262 memcpy(file_system
, file_system_rawfs
, sizeof(struct fs_ops
));
267 memcpy(file_system
, file_system_ufs
, sizeof(struct fs_ops
));
271 memcpy(file_system
, file_system_nfs
, sizeof(struct fs_ops
));
275 /* XXX what else should we do here? */
276 printf("WARNING: BOGUS BOOT DEV TYPE 0x%x!\n", dev
);
287 if ((error
= (*dp
->dv_open
)(f
, adapt
, ctlr
, part
)) == 0) {
289 (*punitsw
[dev
].p_punit
)(adapt
, ctlr
, &unit
)) != 0) {
292 opendev
= MAKEBOOTDEV(dev
, adapt
, ctlr
, unit
, part
);
297 printf("%s(%d,%d,%d,%d): %s\n", devsw
[dev
].dv_name
,
298 adapt
, ctlr
, unit
, part
, strerror(error
));