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