openfirmio.c revision 1.11.92.1 1 /* $NetBSD: openfirmio.c,v 1.11.92.1 2014/05/18 17:45:39 rmind Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)openfirm.c 8.1 (Berkeley) 6/11/93
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: openfirmio.c,v 1.11.92.1 2014/05/18 17:45:39 rmind Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/errno.h>
49 #include <sys/fcntl.h>
50 #include <sys/ioctl.h>
51 #include <sys/malloc.h>
52 #include <sys/conf.h>
53 #include <sys/device.h>
54 #include <sys/event.h>
55
56 #include <dev/ofw/openfirm.h>
57 #include <dev/ofw/openfirmio.h>
58
59 static int lastnode; /* speed hack */
60
61 static int openfirmcheckid (int, int);
62 static int openfirmgetstr (int, char *, char **);
63
64 void openfirmattach (int);
65
66 dev_type_ioctl(openfirmioctl);
67
68 const struct cdevsw openfirm_cdevsw = {
69 .d_open = nullopen,
70 .d_close = nullclose,
71 .d_read = noread,
72 .d_write = nowrite,
73 .d_ioctl = openfirmioctl,
74 .d_stop = nostop,
75 .d_tty = notty,
76 .d_poll = nopoll,
77 .d_mmap = nommap,
78 .d_kqfilter = nokqfilter,
79 .d_flag = 0
80 };
81
82 void
83 openfirmattach(int num)
84 {
85 /* nothing */
86 }
87
88 /*
89 * Verify target ID is valid (exists in the OPENPROM tree), as
90 * listed from node ID sid forward.
91 */
92 static int
93 openfirmcheckid(int sid, int tid)
94 {
95
96 for (; sid != 0; sid = OF_peer(sid))
97 if (sid == tid || openfirmcheckid(OF_child(sid), tid))
98 return (1);
99
100 return (0);
101 }
102
103 static int
104 openfirmgetstr(int len, char *user, char **cpp)
105 {
106 int error;
107 char *cp;
108
109 /* Reject obvious bogus requests */
110 if ((u_int)len > (8 * 1024) - 1)
111 return (ENAMETOOLONG);
112
113 *cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
114 error = copyin(user, cp, len);
115 cp[len] = '\0';
116 return (error);
117 }
118
119 int
120 openfirmioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
121 {
122 struct ofiocdesc *of;
123 int node, len, ok, error, s;
124 char *name, *value;
125
126 if (cmd == OFIOCGETOPTNODE) {
127 s = splhigh();
128 *(int *) data = OF_finddevice("/options");
129 splx(s);
130 return (0);
131 }
132
133 /* Verify node id */
134 of = (struct ofiocdesc *)data;
135 node = of->of_nodeid;
136 if (node != 0 && node != lastnode) {
137 /* Not an easy one, must search for it */
138 s = splhigh();
139 ok = openfirmcheckid(OF_peer(0), node);
140 splx(s);
141 if (!ok)
142 return (EINVAL);
143 lastnode = node;
144 }
145
146 name = value = NULL;
147 error = 0;
148 switch (cmd) {
149
150 case OFIOCGET:
151 if ((flags & FREAD) == 0)
152 return (EBADF);
153 if (node == 0)
154 return (EINVAL);
155 error = openfirmgetstr(of->of_namelen, of->of_name, &name);
156 if (error)
157 break;
158 s = splhigh();
159 len = OF_getproplen(node, name);
160 splx(s);
161 if (len > of->of_buflen) {
162 error = ENOMEM;
163 break;
164 }
165 of->of_buflen = len;
166 /* -1 means no entry; 0 means no value */
167 if (len <= 0)
168 break;
169 value = malloc(len, M_TEMP, M_WAITOK);
170 if (value == NULL) {
171 error = ENOMEM;
172 break;
173 }
174 s = splhigh();
175 len = OF_getprop(node, name, (void *)value, len);
176 splx(s);
177 error = copyout(value, of->of_buf, len);
178 break;
179
180
181 case OFIOCSET:
182 if ((flags & FWRITE) == 0)
183 return (EBADF);
184 if (node == 0)
185 return (EINVAL);
186 error = openfirmgetstr(of->of_namelen, of->of_name, &name);
187 if (error)
188 break;
189 error = openfirmgetstr(of->of_buflen, of->of_buf, &value);
190 if (error)
191 break;
192 s = splhigh();
193 len = OF_setprop(node, name, value, of->of_buflen + 1);
194 splx(s);
195
196 /*
197 * XXX
198 * some OF implementations return the buffer length including
199 * the trailing zero ( like macppc ) and some without ( like
200 * FirmWorks OF used in Shark )
201 */
202 if ((len != (of->of_buflen + 1)) && (len != of->of_buflen))
203 error = EINVAL;
204 break;
205
206 case OFIOCNEXTPROP: {
207 char newname[32];
208 if ((flags & FREAD) == 0)
209 return (EBADF);
210 if (node == 0)
211 return (EINVAL);
212 if (of->of_namelen != 0) {
213 error = openfirmgetstr(of->of_namelen, of->of_name,
214 &name);
215 if (error)
216 break;
217 }
218 s = splhigh();
219 ok = OF_nextprop(node, name, newname);
220 splx(s);
221 if (ok == 0) {
222 error = ENOENT;
223 break;
224 }
225 if (ok == -1) {
226 error = EINVAL;
227 break;
228 }
229 len = strlen(newname);
230 if (len > of->of_buflen)
231 len = of->of_buflen;
232 else
233 of->of_buflen = len;
234 error = copyout(newname, of->of_buf, len);
235 break;
236 }
237
238 case OFIOCGETNEXT:
239 if ((flags & FREAD) == 0)
240 return (EBADF);
241 s = splhigh();
242 node = OF_peer(node);
243 splx(s);
244 *(int *)data = lastnode = node;
245 break;
246
247 case OFIOCGETCHILD:
248 if ((flags & FREAD) == 0)
249 return (EBADF);
250 if (node == 0)
251 return (EINVAL);
252 s = splhigh();
253 node = OF_child(node);
254 splx(s);
255 *(int *)data = lastnode = node;
256 break;
257
258 case OFIOCFINDDEVICE:
259 if ((flags & FREAD) == 0)
260 return (EBADF);
261 error = openfirmgetstr(of->of_namelen, of->of_name, &name);
262 if (error)
263 break;
264 node = OF_finddevice(name);
265 if (node == 0 || node == -1) {
266 error = ENOENT;
267 break;
268 }
269 of->of_nodeid = lastnode = node;
270 break;
271
272 default:
273 return (ENOTTY);
274 }
275
276 if (name)
277 free(name, M_TEMP);
278 if (value)
279 free(value, M_TEMP);
280
281 return (error);
282 }
283