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