1 /* $NetBSD: openfirmio.c,v 1.4 2002/09/06 13:23:19 gehenna Exp $ */
2
3 #include <sys/cdefs.h>
4 /*-
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Copyright (c) 1992, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * This software was developed by the Computer Systems Engineering group
11 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
12 * contributed to Berkeley.
13 *
14 * All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Lawrence Berkeley Laboratory.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
42 *
43 */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/conf.h>
48 #include <sys/errno.h>
49 #include <sys/fcntl.h>
50 #include <sys/ioccom.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/module.h>
54
55 #include <dev/ofw/openfirmio.h>
56
57 static struct cdev *openfirm_dev;
58
59 static d_ioctl_t openfirm_ioctl;
60
61 #define OPENFIRM_MINOR 0
62
63 static struct cdevsw openfirm_cdevsw = {
64 .d_version = D_VERSION,
65 .d_flags = D_NEEDGIANT | D_GIANTOK,
66 .d_ioctl = openfirm_ioctl,
67 .d_name = "openfirm",
68 };
69
70 static phandle_t lastnode; /* speed hack */
71
72 static int openfirm_checkid(phandle_t, phandle_t);
73 static int openfirm_getstr(int, const char *, char **);
74
75 /*
76 * Verify target ID is valid (exists in the OPENPROM tree), as
77 * listed from node ID sid forward.
78 */
79 static int
openfirm_checkid(phandle_t sid,phandle_t tid)80 openfirm_checkid(phandle_t sid, phandle_t tid)
81 {
82
83 for (; sid != 0; sid = OF_peer(sid))
84 if (sid == tid || openfirm_checkid(OF_child(sid), tid))
85 return (1);
86
87 return (0);
88 }
89
90 static int
openfirm_getstr(int len,const char * user,char ** cpp)91 openfirm_getstr(int len, const char *user, char **cpp)
92 {
93 int error;
94 char *cp;
95
96 /* Reject obvious bogus requests. */
97 if ((u_int)len > OFIOCMAXNAME)
98 return (ENAMETOOLONG);
99
100 *cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
101 error = copyin(user, cp, len);
102 cp[len] = '\0';
103 return (error);
104 }
105
106 int
openfirm_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int flags,struct thread * td)107 openfirm_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
108 struct thread *td)
109 {
110 struct ofiocdesc *of;
111 phandle_t node;
112 int len, ok, error;
113 char *name, *value;
114 char newname[OFIOCSUGGPROPNAMELEN];
115
116 if ((flags & FREAD) == 0)
117 return (EBADF);
118
119 of = (struct ofiocdesc *)data;
120 switch (cmd) {
121 case OFIOCGETOPTNODE:
122 *(phandle_t *) data = OF_finddevice("/options");
123 return (0);
124 case OFIOCGET:
125 case OFIOCSET:
126 case OFIOCNEXTPROP:
127 case OFIOCFINDDEVICE:
128 case OFIOCGETPROPLEN:
129 node = of->of_nodeid;
130 break;
131 case OFIOCGETNEXT:
132 case OFIOCGETCHILD:
133 node = *(phandle_t *)data;
134 break;
135 default:
136 return (ENOIOCTL);
137 }
138
139 if (node != 0 && node != lastnode) {
140 /* Not an easy one, we must search for it. */
141 ok = openfirm_checkid(OF_peer(0), node);
142 if (!ok)
143 return (EINVAL);
144 lastnode = node;
145 }
146
147 name = value = NULL;
148 error = 0;
149 switch (cmd) {
150 case OFIOCGET:
151 case OFIOCGETPROPLEN:
152 if (node == 0)
153 return (EINVAL);
154 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
155 if (error)
156 break;
157 len = OF_getproplen(node, name);
158 if (cmd == OFIOCGETPROPLEN) {
159 of->of_buflen = len;
160 break;
161 }
162 if (len > of->of_buflen) {
163 error = ENOMEM;
164 break;
165 }
166 of->of_buflen = len;
167 /* -1 means no entry; 0 means no value. */
168 if (len <= 0)
169 break;
170 value = malloc(len, M_TEMP, M_WAITOK);
171 len = OF_getprop(node, name, (void *)value, len);
172 error = copyout(value, of->of_buf, len);
173 break;
174
175 case OFIOCSET:
176 /*
177 * Note: Text string values for at least the /options node
178 * have to be null-terminated and the length parameter must
179 * include this terminating null. However, like OF_getprop(),
180 * OF_setprop() will return the actual length of the text
181 * string, i.e. omitting the terminating null.
182 */
183 if ((flags & FWRITE) == 0)
184 return (EBADF);
185 if (node == 0)
186 return (EINVAL);
187 if ((u_int)of->of_buflen > OFIOCMAXVALUE)
188 return (ENAMETOOLONG);
189 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
190 if (error)
191 break;
192 value = malloc(of->of_buflen, M_TEMP, M_WAITOK);
193 error = copyin(of->of_buf, value, of->of_buflen);
194 if (error)
195 break;
196 len = OF_setprop(node, name, value, of->of_buflen);
197 if (len < 0)
198 error = EINVAL;
199 of->of_buflen = len;
200 break;
201
202 case OFIOCNEXTPROP:
203 if (node == 0 || of->of_buflen < 0)
204 return (EINVAL);
205 if (of->of_namelen != 0) {
206 error = openfirm_getstr(of->of_namelen, of->of_name,
207 &name);
208 if (error)
209 break;
210 }
211 ok = OF_nextprop(node, name, newname, sizeof(newname));
212 if (ok == 0) {
213 error = ENOENT;
214 break;
215 }
216 if (ok == -1) {
217 error = EINVAL;
218 break;
219 }
220 len = strlen(newname) + 1;
221 if (len > of->of_buflen) {
222 /*
223 * Passed buffer was insufficient.
224 *
225 * Instead of returning an error here, truncate the
226 * property name to fit the buffer.
227 *
228 * This allows us to retain compatibility with old
229 * tools which always pass a 32 character buffer.
230 */
231 len = of->of_buflen;
232 newname[len - 1] = '\0';
233 }
234 else
235 of->of_buflen = len;
236 error = copyout(newname, of->of_buf, len);
237 break;
238
239 case OFIOCGETNEXT:
240 node = OF_peer(node);
241 *(phandle_t *)data = lastnode = node;
242 break;
243
244 case OFIOCGETCHILD:
245 if (node == 0)
246 return (EINVAL);
247 node = OF_child(node);
248 *(phandle_t *)data = lastnode = node;
249 break;
250
251 case OFIOCFINDDEVICE:
252 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
253 if (error)
254 break;
255 node = OF_finddevice(name);
256 if (node == -1) {
257 error = ENOENT;
258 break;
259 }
260 of->of_nodeid = lastnode = node;
261 break;
262 }
263
264 if (name != NULL)
265 free(name, M_TEMP);
266 if (value != NULL)
267 free(value, M_TEMP);
268
269 return (error);
270 }
271
272 static int
openfirm_modevent(module_t mod,int type,void * data)273 openfirm_modevent(module_t mod, int type, void *data)
274 {
275
276 switch(type) {
277 case MOD_LOAD:
278 if (bootverbose)
279 printf("openfirm: <Open Firmware control device>\n");
280 /*
281 * Allow only root access by default; this device may allow
282 * users to peek into firmware passwords, and likely to crash
283 * the machine on some boxen due to firmware quirks.
284 */
285 openfirm_dev = make_dev(&openfirm_cdevsw, OPENFIRM_MINOR,
286 UID_ROOT, GID_WHEEL, 0600, "openfirm");
287 return 0;
288
289 case MOD_UNLOAD:
290 destroy_dev(openfirm_dev);
291 return 0;
292
293 case MOD_SHUTDOWN:
294 return 0;
295
296 default:
297 return EOPNOTSUPP;
298 }
299 }
300
301 DEV_MODULE(openfirm, openfirm_modevent, NULL);
302