devopen.c revision 1.1 1 /* $NetBSD: devopen.c,v 1.1 1998/01/16 04:17:44 sakamoto Exp $ */
2
3 /*-
4 * Copyright (c) 1993 John Brezak
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
17 *
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.
29 */
30
31 #include "stand.h"
32
33 #include <sys/param.h>
34 #include <sys/reboot.h>
35
36 #define ispart(c) ((c) >= 'a' && (c) <= 'h')
37
38 atoi(cp)
39 char *cp;
40 {
41 int val = 0;
42
43 while (isdigit(*cp))
44 val = val * 10 + (*cp++ - '0');
45 return (val);
46 }
47
48 devlookup(d)
49 char *d;
50 {
51 struct devsw *dp = devsw;
52 int i;
53
54 for (i = 0; i < ndevs; i++, dp++)
55 if (dp->dv_name && strcmp(dp->dv_name, d) == 0)
56 return (i);
57
58 printf("No such device - Configured devices are:\n");
59 for (dp = devsw, i = 0; i < ndevs; i++, dp++)
60 if (dp->dv_name)
61 printf(" %s", dp->dv_name);
62 printf("\n");
63 return (ENODEV);
64 }
65
66 /*
67 * Parse a device spec in one of two forms.
68 * dev(ctlr, unit, part)file
69 */
70 devparse(fname, dev, adapt, ctlr, unit, part, file)
71 const char *fname;
72 int *dev;
73 int *adapt;
74 int *ctlr;
75 int *unit;
76 int *part;
77 char **file;
78 {
79 int argc, flag;
80 char *s, *args[3];
81 extern char nametmp[];
82
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);
87
88 if (*s == '(') {
89 /* lookup device and get index */
90 *s = NULL;
91 if ((*dev = devlookup(nametmp, s - nametmp)) < 0)
92 goto baddev;
93
94 /* tokenize device ident */
95 for (++s, flag = 0, argc = 0; *s && *s != ')'; s++) {
96 if (*s != ',') {
97 if (!flag) {
98 flag++;
99 args[argc++] = s;
100 }
101 } else {
102 if (flag) {
103 *s = NULL;
104 flag = 0;
105 }
106 }
107 }
108 if (*s == ')')
109 *s = NULL;
110
111 switch (argc) {
112 case 3:
113 *part = atoi(args[2]);
114 /* FALL THROUGH */
115 case 2:
116 *unit = atoi(args[1]);
117 /* FALL THROUGH */
118 case 1:
119 *ctlr = atoi(args[0]);
120 break;
121 }
122 *file = ++s;
123 } else {
124 /* no device present */
125 *file = (char *)fname;
126 }
127 return (0);
128
129 baddev:
130 return (EINVAL);
131 }
132
133 devopen(f, fname, file)
134 struct open_file *f;
135 const char *fname;
136 char **file;
137 {
138 int n, error;
139 int dev = 0, ctlr = 0, unit = 0, part = 0;
140 int adapt = 0;
141 struct devsw *dp = &devsw[0];
142
143 if (error = devparse(fname, &dev, &adapt, &ctlr, &unit, &part, file))
144 return (error);
145
146 dp = &devsw[dev];
147 if (!dp->dv_open)
148 return (ENODEV);
149
150 f->f_dev = dp;
151 if ((error = (*dp->dv_open)(f, ctlr, unit, part)) == 0)
152 return (0);
153
154 printf("%s(%d,%d,%d): %s\n", devsw[dev].dv_name,
155 ctlr, unit, part, strerror(error));
156
157 return (error);
158 }
159