devopen.c revision 1.5 1 1.5 tsutsui /* $NetBSD: devopen.c,v 1.5 2011/04/11 14:00:02 tsutsui Exp $ */
2 1.1 minoura
3 1.1 minoura /*
4 1.1 minoura * Copyright (c) 2001 Minoura Makoto
5 1.1 minoura * All rights reserved.
6 1.1 minoura *
7 1.1 minoura * Redistribution and use in source and binary forms, with or without
8 1.1 minoura * modification, are permitted provided that the following conditions
9 1.1 minoura * are met:
10 1.1 minoura * 1. Redistributions of source code must retain the above copyright
11 1.1 minoura * notice, this list of conditions and the following disclaimer.
12 1.1 minoura * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 minoura * notice, this list of conditions and the following disclaimer in the
14 1.1 minoura * documentation and/or other materials provided with the distribution.
15 1.1 minoura *
16 1.1 minoura * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 minoura * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 minoura * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 minoura * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 minoura * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 minoura * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 minoura * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 minoura * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 minoura * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 minoura * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 minoura * SUCH DAMAGE.
27 1.1 minoura */
28 1.1 minoura
29 1.1 minoura #include <sys/param.h>
30 1.1 minoura #include <sys/disklabel.h>
31 1.1 minoura #include <lib/libkern/libkern.h>
32 1.1 minoura #include <lib/libsa/stand.h>
33 1.1 minoura #include "libx68k.h"
34 1.1 minoura
35 1.1 minoura extern struct devspec devspec[]; /* defined in conf.c */
36 1.2 minoura int devopen_open_dir = 0;
37 1.1 minoura
38 1.1 minoura /*
39 1.1 minoura * Parse a device spec.
40 1.1 minoura *
41 1.1 minoura * sd<unit><part>:<file>
42 1.1 minoura * unit - 0-7
43 1.1 minoura * part - a-p
44 1.1 minoura */
45 1.1 minoura int
46 1.1 minoura devparse(const char *fname, int *dev, int *unit, int *part, char **file)
47 1.1 minoura {
48 1.1 minoura char const *s;
49 1.1 minoura int i;
50 1.1 minoura
51 1.1 minoura s = fname;
52 1.1 minoura for (i = 0; devspec[i].ds_name != 0; i++) {
53 1.1 minoura if (strncmp (devspec[i].ds_name, s,
54 1.1 minoura strlen(devspec[i].ds_name)) == 0)
55 1.1 minoura break;
56 1.1 minoura }
57 1.1 minoura
58 1.1 minoura if (devspec[i].ds_name == 0)
59 1.4 isaki return ENODEV;
60 1.1 minoura s += strlen(devspec[i].ds_name);
61 1.1 minoura *dev = devspec[i].ds_dev;
62 1.1 minoura
63 1.1 minoura *unit = *s++ - '0';
64 1.1 minoura if (*unit < 0 || *unit > devspec[i].ds_maxunit)
65 1.1 minoura /* bad unit */
66 1.4 isaki return ENODEV;
67 1.1 minoura *part = *s++ - 'a';
68 1.1 minoura if (*part < 0 || *part > MAXPARTITIONS)
69 1.1 minoura /* bad partition */
70 1.4 isaki return ENODEV;
71 1.1 minoura
72 1.1 minoura if (*s++ != ':')
73 1.4 isaki return ENODEV;
74 1.1 minoura
75 1.2 minoura if (*s == '/') {
76 1.1 minoura s++;
77 1.2 minoura if (devopen_open_dir && *s == 0)
78 1.2 minoura s--;
79 1.2 minoura }
80 1.3 skrll *file = __UNCONST(s);
81 1.1 minoura
82 1.1 minoura return 0;
83 1.4 isaki }
84 1.1 minoura
85 1.1 minoura int
86 1.1 minoura devopen(struct open_file *f, const char *fname, char **file)
87 1.1 minoura {
88 1.1 minoura int error;
89 1.1 minoura int dev, unit, part;
90 1.1 minoura struct devsw *dp = &devsw[0];
91 1.1 minoura
92 1.1 minoura error = devparse(fname, &dev, &unit, &part, file);
93 1.1 minoura if (error)
94 1.5 tsutsui return error;
95 1.4 isaki
96 1.1 minoura dp = &devsw[dev];
97 1.4 isaki
98 1.5 tsutsui if (dp->dv_open == NULL)
99 1.4 isaki return ENODEV;
100 1.1 minoura
101 1.1 minoura f->f_dev = dp;
102 1.4 isaki
103 1.1 minoura if ((error = (*dp->dv_open)(f, unit, part)) == 0)
104 1.5 tsutsui return 0;
105 1.4 isaki
106 1.4 isaki return error;
107 1.4 isaki }
108