devopen.c revision 1.1.4.2 1 1.1.4.2 rmind /* $NetBSD: devopen.c,v 1.1.4.2 2011/03/05 20:51:16 rmind Exp $ */
2 1.1.4.2 rmind
3 1.1.4.2 rmind /*-
4 1.1.4.2 rmind * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.1.4.2 rmind * All rights reserved.
6 1.1.4.2 rmind *
7 1.1.4.2 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.1.4.2 rmind * by Rolf Grossmann.
9 1.1.4.2 rmind *
10 1.1.4.2 rmind * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 rmind * modification, are permitted provided that the following conditions
12 1.1.4.2 rmind * are met:
13 1.1.4.2 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 rmind * documentation and/or other materials provided with the distribution.
18 1.1.4.2 rmind *
19 1.1.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.4.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.4.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.4.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.4.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.4.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.4.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.4.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.4.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.4.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.4.2 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1.4.2 rmind */
31 1.1.4.2 rmind
32 1.1.4.2 rmind #include <lib/libsa/stand.h>
33 1.1.4.2 rmind #include <lib/libkern/libkern.h>
34 1.1.4.2 rmind
35 1.1.4.2 rmind #include "boot.h"
36 1.1.4.2 rmind
37 1.1.4.2 rmind extern const struct fs_ops file_system_nfs;
38 1.1.4.2 rmind
39 1.1.4.2 rmind /*
40 1.1.4.2 rmind * Parse a device spec.
41 1.1.4.2 rmind *
42 1.1.4.2 rmind * Format:
43 1.1.4.2 rmind * [device:][filename]
44 1.1.4.2 rmind */
45 1.1.4.2 rmind int
46 1.1.4.2 rmind devparse(const char *fname, int *dev, uint8_t *unit, uint8_t *part,
47 1.1.4.2 rmind const char **file)
48 1.1.4.2 rmind {
49 1.1.4.2 rmind const char *col;
50 1.1.4.2 rmind
51 1.1.4.2 rmind *unit = 0; /* default to wd0a */
52 1.1.4.2 rmind *part = 0;
53 1.1.4.2 rmind *dev = 0;
54 1.1.4.2 rmind *file = DEFKERNELNAME;
55 1.1.4.2 rmind
56 1.1.4.2 rmind if (fname == NULL)
57 1.1.4.2 rmind return 0;
58 1.1.4.2 rmind
59 1.1.4.2 rmind if ((col = strchr(fname, ':')) != NULL) {
60 1.1.4.2 rmind int devlen;
61 1.1.4.2 rmind uint8_t i, u, p;
62 1.1.4.2 rmind struct devsw *dp;
63 1.1.4.2 rmind char devname[MAXDEVNAME];
64 1.1.4.2 rmind
65 1.1.4.2 rmind devlen = col - fname;
66 1.1.4.2 rmind if (devlen > MAXDEVNAME)
67 1.1.4.2 rmind return EINVAL;
68 1.1.4.2 rmind
69 1.1.4.2 rmind #define isnum(c) (((c) >= '0') && ((c) <= '9'))
70 1.1.4.2 rmind #define isalpha(c) (((c) >= 'a') && ((c) <= 'z'))
71 1.1.4.2 rmind
72 1.1.4.2 rmind /* extract device name */
73 1.1.4.2 rmind for (i = 0; isalpha(fname[i]) && (i < devlen); i++)
74 1.1.4.2 rmind devname[i] = fname[i];
75 1.1.4.2 rmind devname[i] = '\0';
76 1.1.4.2 rmind
77 1.1.4.2 rmind /* parse [disk][unit][part] (ex. wd0a) strings */
78 1.1.4.2 rmind if (!isnum(fname[i]))
79 1.1.4.2 rmind return EUNIT;
80 1.1.4.2 rmind
81 1.1.4.2 rmind /* device number */
82 1.1.4.2 rmind for (u = 0; isnum(fname[i]) && (i < devlen); i++)
83 1.1.4.2 rmind u = u * 10 + (fname[i] - '0');
84 1.1.4.2 rmind
85 1.1.4.2 rmind if (!isalpha(fname[i]))
86 1.1.4.2 rmind return EPART;
87 1.1.4.2 rmind
88 1.1.4.2 rmind /* partition number */
89 1.1.4.2 rmind p = 0;
90 1.1.4.2 rmind if (i < devlen)
91 1.1.4.2 rmind p = fname[i++] - 'a';
92 1.1.4.2 rmind
93 1.1.4.2 rmind if (i != devlen)
94 1.1.4.2 rmind return ENXIO;
95 1.1.4.2 rmind
96 1.1.4.2 rmind /* check device name */
97 1.1.4.2 rmind for (dp = devsw, i = 0; i < ndevs; dp++, i++) {
98 1.1.4.2 rmind if (dp->dv_name && !strcmp(devname, dp->dv_name))
99 1.1.4.2 rmind break;
100 1.1.4.2 rmind }
101 1.1.4.2 rmind
102 1.1.4.2 rmind if (i >= ndevs)
103 1.1.4.2 rmind return ENXIO;
104 1.1.4.2 rmind
105 1.1.4.2 rmind *unit = u;
106 1.1.4.2 rmind *part = p;
107 1.1.4.2 rmind *dev = i;
108 1.1.4.2 rmind fname = ++col;
109 1.1.4.2 rmind }
110 1.1.4.2 rmind
111 1.1.4.2 rmind if (*fname)
112 1.1.4.2 rmind *file = fname;
113 1.1.4.2 rmind
114 1.1.4.2 rmind return 0;
115 1.1.4.2 rmind }
116 1.1.4.2 rmind
117 1.1.4.2 rmind int
118 1.1.4.2 rmind devopen(struct open_file *f, const char *fname, char **file)
119 1.1.4.2 rmind {
120 1.1.4.2 rmind struct devsw *dp;
121 1.1.4.2 rmind uint8_t unit, part;
122 1.1.4.2 rmind int dev, error;
123 1.1.4.2 rmind
124 1.1.4.2 rmind DPRINTF(("devopen(%s)\n", fname));
125 1.1.4.2 rmind
126 1.1.4.2 rmind if ((error = devparse(fname, &dev, &unit, &part,
127 1.1.4.2 rmind (const char **)file)) != 0)
128 1.1.4.2 rmind return error;
129 1.1.4.2 rmind
130 1.1.4.2 rmind dp = &devsw[dev];
131 1.1.4.2 rmind if ((void *)dp->dv_open == (void *)nodev)
132 1.1.4.2 rmind return ENXIO;
133 1.1.4.2 rmind
134 1.1.4.2 rmind f->f_dev = dp;
135 1.1.4.2 rmind
136 1.1.4.2 rmind if ((error = (*dp->dv_open)(f, unit, part)) != 0)
137 1.1.4.2 rmind printf("%s%d%c: %d = %s\n", devsw[dev].dv_name,
138 1.1.4.2 rmind unit, 'a' + part, error, strerror(error));
139 1.1.4.2 rmind
140 1.1.4.2 rmind return error;
141 1.1.4.2 rmind }
142