devopen.c revision 1.9.20.1 1 1.9.20.1 rmind /* $NetBSD: devopen.c,v 1.9.20.1 2011/03/05 20:49:46 rmind Exp $ */
2 1.1 sakamoto
3 1.1 sakamoto /*-
4 1.1 sakamoto * Copyright (c) 1993 John Brezak
5 1.1 sakamoto * All rights reserved.
6 1.1 sakamoto *
7 1.1 sakamoto * Redistribution and use in source and binary forms, with or without
8 1.1 sakamoto * modification, are permitted provided that the following conditions
9 1.1 sakamoto * are met:
10 1.1 sakamoto * 1. Redistributions of source code must retain the above copyright
11 1.1 sakamoto * notice, this list of conditions and the following disclaimer.
12 1.1 sakamoto * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 sakamoto * notice, this list of conditions and the following disclaimer in the
14 1.1 sakamoto * documentation and/or other materials provided with the distribution.
15 1.1 sakamoto * 3. The name of the author may not be used to endorse or promote products
16 1.1 sakamoto * derived from this software without specific prior written permission.
17 1.5 junyoung *
18 1.1 sakamoto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19 1.1 sakamoto * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 sakamoto * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 sakamoto * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 1.1 sakamoto * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 1.1 sakamoto * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 sakamoto * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 sakamoto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 1.1 sakamoto * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 1.1 sakamoto * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 sakamoto * POSSIBILITY OF SUCH DAMAGE.
29 1.1 sakamoto */
30 1.1 sakamoto
31 1.6 junyoung #include <lib/libsa/stand.h>
32 1.4 sakamoto #include <lib/libkern/libkern.h>
33 1.1 sakamoto #include <sys/param.h>
34 1.1 sakamoto #include <sys/reboot.h>
35 1.1 sakamoto
36 1.9 kiyohara static int devparse(const char *, int *, int *, int *, int *, int *, char **);
37 1.9 kiyohara
38 1.9 kiyohara
39 1.1 sakamoto /*
40 1.9.20.1 rmind * Parse a device spec.
41 1.9.20.1 rmind * i.e.
42 1.9.20.1 rmind * /dev/disk/floppy
43 1.9.20.1 rmind * /dev/disk/ide/0/master/0
44 1.9.20.1 rmind * /dev/disk/ide/0/slave/0
45 1.9.20.1 rmind * /dev/disk/scsi/0/0/0
46 1.9.20.1 rmind * /dev/disk/scsi/0/3/0
47 1.1 sakamoto */
48 1.9 kiyohara static int
49 1.9.20.1 rmind devparse(const char *fname, int *dev, int *ctlr, int *unit, int *lunit,
50 1.5 junyoung int *part, char **file)
51 1.1 sakamoto {
52 1.9.20.1 rmind int i;
53 1.9.20.1 rmind char devdir[] = "/dev/disk/";
54 1.9.20.1 rmind char floppy[] = "floppy";
55 1.9.20.1 rmind char ide[] = "ide";
56 1.9.20.1 rmind char scsi[] = "scsi";
57 1.9.20.1 rmind char *p;
58 1.9.20.1 rmind
59 1.9.20.1 rmind if (strncmp(fname, devdir, strlen(devdir)) != 0)
60 1.9.20.1 rmind return EINVAL;
61 1.9.20.1 rmind p = __UNCONST(fname) + strlen(devdir);
62 1.9.20.1 rmind
63 1.9.20.1 rmind if (strncmp(p, floppy, strlen(floppy)) == 0) {
64 1.9.20.1 rmind p += strlen(floppy);
65 1.9.20.1 rmind for (i = 0; devsw[i].dv_name != NULL; i++)
66 1.9.20.1 rmind if (strcmp(devsw[i].dv_name, "fd") == 0) {
67 1.9.20.1 rmind *dev = i;
68 1.9.20.1 rmind *ctlr = 0;
69 1.9.20.1 rmind *unit = 1;
70 1.9.20.1 rmind *lunit = 0;
71 1.9.20.1 rmind break;
72 1.1 sakamoto }
73 1.9.20.1 rmind } else if (strncmp(p, ide, strlen(ide)) == 0) {
74 1.9.20.1 rmind char master[] = "master";
75 1.9.20.1 rmind char slave[] = "slave";
76 1.9.20.1 rmind
77 1.9.20.1 rmind p += strlen(ide);
78 1.9.20.1 rmind if (*p++ != '/' ||
79 1.9.20.1 rmind !isdigit(*p++) ||
80 1.9.20.1 rmind *p++ != '/')
81 1.9.20.1 rmind return EINVAL;
82 1.9.20.1 rmind *ctlr = *(p - 2) - '0';
83 1.9.20.1 rmind if (strncmp(p, master, strlen(master)) == 0) {
84 1.9.20.1 rmind *unit = 0;
85 1.9.20.1 rmind p += strlen(master);
86 1.9.20.1 rmind } else if (strncmp(p, slave, strlen(slave)) == 0) {
87 1.9.20.1 rmind *unit = 1;
88 1.9.20.1 rmind p += strlen(slave);
89 1.9.20.1 rmind } else
90 1.9.20.1 rmind return EINVAL;
91 1.9.20.1 rmind if (*p++ != '/' ||
92 1.9.20.1 rmind !isdigit(*p++) ||
93 1.9.20.1 rmind *p++ != '_' ||
94 1.9.20.1 rmind !isdigit(*p++))
95 1.9.20.1 rmind return EINVAL;
96 1.9.20.1 rmind *lunit = *(p - 3) - '0';
97 1.9.20.1 rmind *part = *(p - 1) - '0';
98 1.9.20.1 rmind for (i = 0; devsw[i].dv_name != NULL; i++)
99 1.9.20.1 rmind if (strcmp(devsw[i].dv_name, "wd") == 0) {
100 1.9.20.1 rmind *dev = i;
101 1.9.20.1 rmind break;
102 1.9.20.1 rmind }
103 1.9.20.1 rmind if (devsw[i].dv_name == NULL)
104 1.9.20.1 rmind return EINVAL;
105 1.9.20.1 rmind } else if (strncmp(p, scsi, strlen(scsi)) == 0) {
106 1.9.20.1 rmind p += strlen(scsi);
107 1.9.20.1 rmind if (*p++ != '/' ||
108 1.9.20.1 rmind !isdigit(*p++) ||
109 1.9.20.1 rmind *p++ != '/' ||
110 1.9.20.1 rmind !isdigit(*p++) ||
111 1.9.20.1 rmind *p++ != '/' ||
112 1.9.20.1 rmind !isdigit(*p++) ||
113 1.9.20.1 rmind *p++ != '_' ||
114 1.9.20.1 rmind !isdigit(*p++))
115 1.9.20.1 rmind return EINVAL;
116 1.9.20.1 rmind *ctlr = *(p - 7) - '0';
117 1.9.20.1 rmind *unit = *(p - 5) - '0';
118 1.9.20.1 rmind *lunit = *(p - 3) - '0';
119 1.9.20.1 rmind *part = *(p - 1) - '0';
120 1.9.20.1 rmind for (i = 0; devsw[i].dv_name != NULL; i++)
121 1.9.20.1 rmind if (strcmp(devsw[i].dv_name, "sd") == 0) {
122 1.9.20.1 rmind *dev = i;
123 1.9.20.1 rmind break;
124 1.9.20.1 rmind }
125 1.9.20.1 rmind if (devsw[i].dv_name == NULL)
126 1.9.20.1 rmind return EINVAL;
127 1.1 sakamoto }
128 1.1 sakamoto
129 1.9.20.1 rmind if (*p++ != ':')
130 1.9.20.1 rmind return EINVAL;
131 1.9.20.1 rmind *file = p;
132 1.9.20.1 rmind return 0;
133 1.1 sakamoto }
134 1.1 sakamoto
135 1.3 sakamoto int
136 1.5 junyoung devopen(struct open_file *f, const char *fname, char **file)
137 1.1 sakamoto {
138 1.3 sakamoto int error;
139 1.9.20.1 rmind int dev = 0, ctlr = 0, unit = 0, lunit = 0, part = 0;
140 1.1 sakamoto struct devsw *dp = &devsw[0];
141 1.9.20.1 rmind extern struct devsw pseudo_devsw;
142 1.1 sakamoto
143 1.9.20.1 rmind **file = '\0';
144 1.9.20.1 rmind error = devparse(fname, &dev, &ctlr, &unit, &lunit, &part, file);
145 1.9.20.1 rmind if (error == 0) {
146 1.9.20.1 rmind dp = &devsw[dev];
147 1.9.20.1 rmind if (!dp->dv_open)
148 1.9.20.1 rmind return ENODEV;
149 1.9.20.1 rmind } else {
150 1.9.20.1 rmind if (strcmp(fname, "in") == 0)
151 1.9.20.1 rmind /* special case: kernel in memory */
152 1.9.20.1 rmind dp = &pseudo_devsw;
153 1.9.20.1 rmind else
154 1.9.20.1 rmind return error;
155 1.9.20.1 rmind }
156 1.1 sakamoto
157 1.1 sakamoto f->f_dev = dp;
158 1.9.20.1 rmind if ((error = (*dp->dv_open)(f, ctlr, unit, lunit, part)) == 0)
159 1.5 junyoung return 0;
160 1.1 sakamoto
161 1.9.20.1 rmind printf("%s %s\n", fname, strerror(error));
162 1.1 sakamoto
163 1.5 junyoung return error;
164 1.1 sakamoto }
165