1 /* $NetBSD: devopen.c,v 1.8 2005/12/11 12:17:04 christos Exp $ */
4 * Copyright (c) 1993 John Brezak
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <lib/libsa/stand.h>
32 #include <lib/libkern/libkern.h>
33 #include <sys/param.h>
34 #include <sys/reboot.h>
36 #define ispart(c) ((c) >= 'a' && (c) <= 'h')
38 static int atoi(char *);
39 static int devlookup(char *);
40 static int devparse(const char *, int *, int *, int *, int *, int *, char **);
49 val
= val
* 10 + (*cp
++ - '0');
56 struct devsw
*dp
= devsw
;
59 for (i
= 0; i
< ndevs
; i
++, dp
++)
60 if (dp
->dv_name
&& strcmp(dp
->dv_name
, d
) == 0)
63 printf("No such device - Configured devices are:\n");
64 for (dp
= devsw
, i
= 0; i
< ndevs
; i
++, dp
++)
66 printf(" %s", dp
->dv_name
);
72 * Parse a device spec in one of two forms.
73 * dev(ctlr, unit, part)file
76 devparse(const char *fname
, int *dev
, int *adapt
, int *ctlr
, int *unit
,
77 int *part
, char **file
)
81 extern char nametmp
[];
83 /* get device name and make lower case */
84 strcpy(nametmp
, (char *)fname
);
85 for (s
= nametmp
; *s
&& *s
!= '('; s
++)
86 if (isupper(*s
)) *s
= tolower(*s
);
89 /* lookup device and get index */
91 if ((*dev
= devlookup(nametmp
)) < 0)
94 /* tokenize device ident */
95 for (++s
, flag
= 0, argc
= 0; *s
&& *s
!= ')'; s
++) {
113 *part
= atoi(args
[2]);
116 *unit
= atoi(args
[1]);
119 *ctlr
= atoi(args
[0]);
124 /* no device present */
125 *file
= (char *)fname
;
134 devopen(struct open_file
*f
, const char *fname
, char **file
)
137 int dev
= 0, ctlr
= 0, unit
= 0, part
= 0;
139 struct devsw
*dp
= &devsw
[0];
142 devparse(fname
, &dev
, &adapt
, &ctlr
, &unit
, &part
, file
)) != 0)
150 if ((error
= (*dp
->dv_open
)(f
, ctlr
, unit
, part
)) == 0)
153 printf("%s(%d,%d,%d): %s\n", devsw
[dev
].dv_name
,
154 ctlr
, unit
, part
, strerror(error
));