1 /* $NetBSD: openfirmio.c,v 1.4 2002/09/06 13:23:19 gehenna Exp $ */
7 * Copyright (c) 1992, 1993
8 * The Regents of the University of California. All rights reserved.
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.
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.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
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 * 4. 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.
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
43 * @(#)openfirm.c 8.1 (Berkeley) 6/11/93
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/errno.h>
51 #include <sys/fcntl.h>
52 #include <sys/ioccom.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/module.h>
57 #include <dev/ofw/openfirmio.h>
59 static struct cdev *openfirm_dev;
61 static d_ioctl_t openfirm_ioctl;
63 #define OPENFIRM_MINOR 0
65 static struct cdevsw openfirm_cdevsw = {
66 .d_version = D_VERSION,
67 .d_flags = D_NEEDGIANT,
68 .d_ioctl = openfirm_ioctl,
72 static phandle_t lastnode; /* speed hack */
74 static int openfirm_checkid(phandle_t, phandle_t);
75 static int openfirm_getstr(int, const char *, char **);
78 * Verify target ID is valid (exists in the OPENPROM tree), as
79 * listed from node ID sid forward.
82 openfirm_checkid(phandle_t sid, phandle_t tid)
85 for (; sid != 0; sid = OF_peer(sid))
86 if (sid == tid || openfirm_checkid(OF_child(sid), tid))
93 openfirm_getstr(int len, const char *user, char **cpp)
98 /* Reject obvious bogus requests. */
99 if ((u_int)len > OFIOCMAXNAME)
100 return (ENAMETOOLONG);
102 *cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
105 error = copyin(user, cp, len);
111 openfirm_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
114 struct ofiocdesc *of;
120 if ((flags & FREAD) == 0)
123 of = (struct ofiocdesc *)data;
125 case OFIOCGETOPTNODE:
126 *(phandle_t *) data = OF_finddevice("/options");
131 case OFIOCFINDDEVICE:
132 case OFIOCGETPROPLEN:
133 node = of->of_nodeid;
137 node = *(phandle_t *)data;
143 if (node != 0 && node != lastnode) {
144 /* Not an easy one, we must search for it. */
145 ok = openfirm_checkid(OF_peer(0), node);
156 case OFIOCGETPROPLEN:
159 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
162 len = OF_getproplen(node, name);
163 if (cmd == OFIOCGETPROPLEN) {
167 if (len > of->of_buflen) {
172 /* -1 means no entry; 0 means no value. */
175 value = malloc(len, M_TEMP, M_WAITOK);
180 len = OF_getprop(node, name, (void *)value, len);
181 error = copyout(value, of->of_buf, len);
186 * Note: Text string values for at least the /options node
187 * have to be null-terminated and the length parameter must
188 * include this terminating null. However, like OF_getprop(),
189 * OF_setprop() will return the actual length of the text
190 * string, i.e. omitting the terminating null.
192 if ((flags & FWRITE) == 0)
196 if ((u_int)of->of_buflen > OFIOCMAXVALUE)
197 return (ENAMETOOLONG);
198 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
201 value = malloc(of->of_buflen, M_TEMP, M_WAITOK);
206 error = copyin(of->of_buf, value, of->of_buflen);
209 len = OF_setprop(node, name, value, of->of_buflen);
216 if (node == 0 || of->of_buflen < 0)
218 if (of->of_namelen != 0) {
219 error = openfirm_getstr(of->of_namelen, of->of_name,
224 ok = OF_nextprop(node, name, newname, sizeof(newname));
233 len = strlen(newname) + 1;
234 if (len > of->of_buflen)
238 error = copyout(newname, of->of_buf, len);
242 node = OF_peer(node);
243 *(phandle_t *)data = lastnode = node;
249 node = OF_child(node);
250 *(phandle_t *)data = lastnode = node;
253 case OFIOCFINDDEVICE:
254 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
257 node = OF_finddevice(name);
258 if (node == 0 || node == -1) {
262 of->of_nodeid = lastnode = node;
275 openfirm_modevent(module_t mod, int type, void *data)
281 printf("openfirm: <Open Firmware control device>\n");
283 * Allow only root access by default; this device may allow
284 * users to peek into firmware passwords, and likely to crash
285 * the machine on some boxen due to firmware quirks.
287 openfirm_dev = make_dev(&openfirm_cdevsw, OPENFIRM_MINOR,
288 UID_ROOT, GID_WHEEL, 0600, "openfirm");
292 destroy_dev(openfirm_dev);
303 DEV_MODULE(openfirm, openfirm_modevent, NULL);