devopen.c revision 1.5 1 /* $NetBSD: devopen.c,v 1.5 2008/03/02 06:17:41 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Rolf Grossmann.
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 #include <lib/libsa/stand.h>
40 #include <lib/libkern/libkern.h>
41
42 #include "boot.h"
43
44 /*
45 * Parse a device spec.
46 *
47 * Format:
48 * [device:][filename]
49 */
50 int
51 devparse(const char *fname, int *dev, uint8_t *unit, uint8_t *part,
52 const char **file)
53 {
54 const char *col;
55
56 *unit = 0; /* default to wd0a */
57 *part = 0;
58 *dev = 0;
59 *file = DEFKERNELNAME;
60
61 if (fname == NULL)
62 return 0;
63
64 if ((col = strchr(fname, ':')) != NULL) {
65 int devlen;
66 uint8_t i, u, p;
67 struct devsw *dp;
68 char devname[MAXDEVNAME];
69
70 devlen = col - fname;
71 if (devlen > MAXDEVNAME)
72 return EINVAL;
73
74 #define isnum(c) (((c) >= '0') && ((c) <= '9'))
75 #define isalpha(c) (((c) >= 'a') && ((c) <= 'z'))
76
77 /* extract device name */
78 for (i = 0; isalpha(fname[i]) && (i < devlen); i++)
79 devname[i] = fname[i];
80 devname[i] = '\0';
81
82 if (strcmp(devname, "nfs") == 0) {
83 /* no unit number or partition suffix on netboot */
84 u = 0;
85 p = 0;
86 } else {
87 /* parse [disk][unit][part] (ex. wd0a) strings */
88 if (!isnum(fname[i]))
89 return EUNIT;
90
91 /* device number */
92 for (u = 0; isnum(fname[i]) && (i < devlen); i++)
93 u = u * 10 + (fname[i] - '0');
94
95 if (!isalpha(fname[i]))
96 return EPART;
97
98 /* partition number */
99 p = 0;
100 if (i < devlen)
101 p = fname[i++] - 'a';
102
103 if (i != devlen)
104 return ENXIO;
105 }
106
107 /* check device name */
108 for (dp = devsw, i = 0; i < ndevs; dp++, i++) {
109 if (dp->dv_name && !strcmp(devname, dp->dv_name))
110 break;
111 }
112
113 if (i >= ndevs)
114 return ENXIO;
115
116 *unit = u;
117 *part = p;
118 *dev = i;
119 fname = ++col;
120 }
121
122 if (*fname)
123 *file = fname;
124
125 return 0;
126 }
127
128 int
129 devopen(struct open_file *f, const char *fname, char **file)
130 {
131 struct devsw *dp;
132 uint8_t unit, part;
133 int dev, error;
134
135 DPRINTF(("devopen(%s)\n", fname));
136
137 if ((error = devparse(fname, &dev, &unit, &part,
138 (const char **)file)) != 0)
139 return error;
140
141 dp = &devsw[dev];
142 if ((void *)dp->dv_open == (void *)nodev)
143 return ENXIO;
144
145 f->f_dev = dp;
146
147 if ((error = (*dp->dv_open)(f, unit, part)) != 0)
148 printf("%s%d%c: %d = %s\n", devsw[dev].dv_name,
149 unit, 'a' + part, error, strerror(error));
150
151 return error;
152 }
153