devopen.c revision 1.1.8.2 1 1.1.8.2 jruoho /* $NetBSD: devopen.c,v 1.1.8.2 2011/06/06 09:05:21 jruoho Exp $ */
2 1.1.8.2 jruoho
3 1.1.8.2 jruoho /*-
4 1.1.8.2 jruoho * Copyright (c) 1992, 1993
5 1.1.8.2 jruoho * The Regents of the University of California. All rights reserved.
6 1.1.8.2 jruoho *
7 1.1.8.2 jruoho * This code is derived from software contributed to Berkeley by
8 1.1.8.2 jruoho * Ralph Campbell.
9 1.1.8.2 jruoho *
10 1.1.8.2 jruoho * Redistribution and use in source and binary forms, with or without
11 1.1.8.2 jruoho * modification, are permitted provided that the following conditions
12 1.1.8.2 jruoho * are met:
13 1.1.8.2 jruoho * 1. Redistributions of source code must retain the above copyright
14 1.1.8.2 jruoho * notice, this list of conditions and the following disclaimer.
15 1.1.8.2 jruoho * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.8.2 jruoho * notice, this list of conditions and the following disclaimer in the
17 1.1.8.2 jruoho * documentation and/or other materials provided with the distribution.
18 1.1.8.2 jruoho * 3. Neither the name of the University nor the names of its contributors
19 1.1.8.2 jruoho * may be used to endorse or promote products derived from this software
20 1.1.8.2 jruoho * without specific prior written permission.
21 1.1.8.2 jruoho *
22 1.1.8.2 jruoho * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1.8.2 jruoho * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1.8.2 jruoho * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1.8.2 jruoho * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1.8.2 jruoho * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1.8.2 jruoho * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1.8.2 jruoho * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1.8.2 jruoho * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1.8.2 jruoho * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1.8.2 jruoho * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1.8.2 jruoho * SUCH DAMAGE.
33 1.1.8.2 jruoho *
34 1.1.8.2 jruoho * @(#)devopen.c 8.1 (Berkeley) 6/10/93
35 1.1.8.2 jruoho */
36 1.1.8.2 jruoho
37 1.1.8.2 jruoho #include <lib/libsa/stand.h>
38 1.1.8.2 jruoho #include <lib/libkern/libkern.h>
39 1.1.8.2 jruoho
40 1.1.8.2 jruoho /*
41 1.1.8.2 jruoho * Decode the string 'fname', open the device and return the remaining
42 1.1.8.2 jruoho * file name if any.
43 1.1.8.2 jruoho */
44 1.1.8.2 jruoho int
45 1.1.8.2 jruoho devopen(struct open_file *f,
46 1.1.8.2 jruoho const char *fname,
47 1.1.8.2 jruoho char **file) /* out */
48 1.1.8.2 jruoho {
49 1.1.8.2 jruoho int ctlr = 0, unit = 0, part = 0;
50 1.1.8.2 jruoho int c, rc;
51 1.1.8.2 jruoho char device_name[20];
52 1.1.8.2 jruoho const char *cp;
53 1.1.8.2 jruoho char *ncp;
54 1.1.8.2 jruoho #if !defined(LIBSA_SINGLE_DEVICE)
55 1.1.8.2 jruoho int i;
56 1.1.8.2 jruoho struct devsw *dp;
57 1.1.8.2 jruoho #endif
58 1.1.8.2 jruoho
59 1.1.8.2 jruoho cp = fname;
60 1.1.8.2 jruoho ncp = device_name;
61 1.1.8.2 jruoho
62 1.1.8.2 jruoho /* Require the form CTRL/DEVNAME(UNIT,PART)/FILENAME e.g. '0/ace(0,0)/netbsd' '0/tftp(0,0)/netbsd'
63 1.1.8.2 jruoho */
64 1.1.8.2 jruoho
65 1.1.8.2 jruoho /* get controller number */
66 1.1.8.2 jruoho if ((c = *cp) >= '0' && c <= '9') {
67 1.1.8.2 jruoho ctlr = c - '0';
68 1.1.8.2 jruoho c = *++cp;
69 1.1.8.2 jruoho } else
70 1.1.8.2 jruoho return (ENXIO);
71 1.1.8.2 jruoho
72 1.1.8.2 jruoho if (c != '/')
73 1.1.8.2 jruoho return (ENXIO);
74 1.1.8.2 jruoho c = *++cp;
75 1.1.8.2 jruoho
76 1.1.8.2 jruoho /* get device name */
77 1.1.8.2 jruoho while ((c = *cp) != '\0') {
78 1.1.8.2 jruoho if ((c == '(') || (c == '/')) {
79 1.1.8.2 jruoho cp++;
80 1.1.8.2 jruoho break;
81 1.1.8.2 jruoho }
82 1.1.8.2 jruoho if (ncp < device_name + sizeof(device_name) - 1)
83 1.1.8.2 jruoho *ncp++ = c;
84 1.1.8.2 jruoho cp++;
85 1.1.8.2 jruoho }
86 1.1.8.2 jruoho if (ncp == device_name)
87 1.1.8.2 jruoho return (ENXIO);
88 1.1.8.2 jruoho
89 1.1.8.2 jruoho /* get device number */
90 1.1.8.2 jruoho if ((c = *cp) >= '0' && c <= '9') {
91 1.1.8.2 jruoho unit = c - '0';
92 1.1.8.2 jruoho c = *++cp;
93 1.1.8.2 jruoho } else
94 1.1.8.2 jruoho return (ENXIO);
95 1.1.8.2 jruoho
96 1.1.8.2 jruoho if (c == ',') {
97 1.1.8.2 jruoho /* get partition number */
98 1.1.8.2 jruoho if ((c = *++cp) >= '0' && c <= '9') {
99 1.1.8.2 jruoho part = c - '0';
100 1.1.8.2 jruoho c = *++cp;
101 1.1.8.2 jruoho } else
102 1.1.8.2 jruoho return (ENXIO);
103 1.1.8.2 jruoho } else
104 1.1.8.2 jruoho return (ENXIO);
105 1.1.8.2 jruoho
106 1.1.8.2 jruoho if (c == ')')
107 1.1.8.2 jruoho cp++;
108 1.1.8.2 jruoho else
109 1.1.8.2 jruoho return (ENXIO);
110 1.1.8.2 jruoho
111 1.1.8.2 jruoho *ncp = '\0';
112 1.1.8.2 jruoho
113 1.1.8.2 jruoho #ifdef LIBSA_SINGLE_DEVICE
114 1.1.8.2 jruoho rc = DEV_OPEN(dp)(f, ctlr, unit, part);
115 1.1.8.2 jruoho #else /* !LIBSA_SINGLE_DEVICE */
116 1.1.8.2 jruoho for (dp = devsw, i = 0; i < ndevs; dp++, i++)
117 1.1.8.2 jruoho if (dp->dv_name && strcmp(device_name, dp->dv_name) == 0)
118 1.1.8.2 jruoho goto fnd;
119 1.1.8.2 jruoho printf("Unknown device '%s'\nKnown devices are:", device_name);
120 1.1.8.2 jruoho for (dp = devsw, i = 0; i < ndevs; dp++, i++)
121 1.1.8.2 jruoho if (dp->dv_name)
122 1.1.8.2 jruoho printf(" %s", dp->dv_name);
123 1.1.8.2 jruoho printf("\n");
124 1.1.8.2 jruoho return (ENXIO);
125 1.1.8.2 jruoho
126 1.1.8.2 jruoho fnd:
127 1.1.8.2 jruoho #ifdef BOOTNET
128 1.1.8.2 jruoho if (strcmp(device_name, "tftp") == 0)
129 1.1.8.2 jruoho rc = (dp->dv_open)(f, device_name);
130 1.1.8.2 jruoho else
131 1.1.8.2 jruoho #endif /* BOOTNET */
132 1.1.8.2 jruoho rc = (dp->dv_open)(f, ctlr, unit, part, cp);
133 1.1.8.2 jruoho #endif /* !LIBSA_SINGLE_DEVICE */
134 1.1.8.2 jruoho if (rc)
135 1.1.8.2 jruoho return (rc);
136 1.1.8.2 jruoho
137 1.1.8.2 jruoho #ifndef LIBSA_SINGLE_DEVICE
138 1.1.8.2 jruoho f->f_dev = dp;
139 1.1.8.2 jruoho #endif
140 1.1.8.2 jruoho if (file && *cp != '\0')
141 1.1.8.2 jruoho *file = (char *)cp; /* XXX */
142 1.1.8.2 jruoho return (0);
143 1.1.8.2 jruoho }
144