xref: /freebsd/sys/dev/ofw/openfirmio.c (revision 3047fefe49f57a673de8df152c199de12ec2c6d3)
1 /*	$NetBSD: openfirmio.c,v 1.4 2002/09/06 13:23:19 gehenna 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. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	@(#)openfirm.c	8.1 (Berkeley) 6/11/93
45  *
46  * $FreeBSD$
47  */
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/conf.h>
52 #include <sys/errno.h>
53 #include <sys/fcntl.h>
54 #include <sys/ioccom.h>
55 #include <sys/kernel.h>
56 #include <sys/malloc.h>
57 #include <sys/module.h>
58 
59 #include <dev/ofw/openfirmio.h>
60 
61 static dev_t openfirm_dev;
62 
63 static d_ioctl_t openfirm_ioctl;
64 
65 #define CDEV_MAJOR	177
66 #define	OPENFIRM_MINOR	0
67 
68 static struct cdevsw openfirm_cdevsw = {
69 	/* open */	nullopen,
70 	/* close */	nullclose,
71 	/* read */	noread,
72 	/* write */	nowrite,
73 	/* ioctl */	openfirm_ioctl,
74 	/* poll */	nopoll,
75 	/* mmap */	nommap,
76 	/* strategy */	nostrategy,
77 	/* name */	"openfirm",
78 	/* maj */	CDEV_MAJOR,
79 	/* dump */	nodump,
80 	/* psize */	nopsize,
81 	/* flags */	0,
82 	/* kqfilter */	nokqfilter
83 };
84 
85 static phandle_t lastnode;	/* speed hack */
86 
87 static int openfirm_checkid(phandle_t, phandle_t);
88 static int openfirm_getstr(int, const char *, char **);
89 
90 /* Maximum accepted name length. */
91 #define	OFW_NAME_MAX	8191
92 
93 /*
94  * Verify target ID is valid (exists in the OPENPROM tree), as
95  * listed from node ID sid forward.
96  */
97 static int
98 openfirm_checkid(phandle_t sid, phandle_t tid)
99 {
100 
101 	for (; sid != 0; sid = OF_peer(sid))
102 		if (sid == tid || openfirm_checkid(OF_child(sid), tid))
103 			return (1);
104 
105 	return (0);
106 }
107 
108 static int
109 openfirm_getstr(int len, const char *user, char **cpp)
110 {
111 	int error;
112 	char *cp;
113 
114 	/* Reject obvious bogus requests */
115 	if ((u_int)len > OFW_NAME_MAX)
116 		return (ENAMETOOLONG);
117 
118 	*cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
119 	if (cp == NULL)
120 		return (ENOMEM);
121 	error = copyin(user, cp, len);
122 	cp[len] = '\0';
123 	return (error);
124 }
125 
126 int
127 openfirm_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags,
128     struct thread *td)
129 {
130 	struct ofiocdesc *of;
131 	phandle_t node;
132 	int len, ok, error;
133 	char *name, *value;
134 	char newname[32];
135 
136 	of = (struct ofiocdesc *)data;
137 	switch (cmd) {
138 	case OFIOCGETOPTNODE:
139 		*(phandle_t *) data = OF_finddevice("/options");
140 		return (0);
141 	case OFIOCGET:
142 #if 0
143 	case OFIOCSET:
144 #endif
145 	case OFIOCNEXTPROP:
146 	case OFIOCFINDDEVICE:
147 		node = of->of_nodeid;
148 		break;
149 	case OFIOCGETNEXT:
150 	case OFIOCGETCHILD:
151 		node = *(phandle_t *)data;
152 		break;
153 	default:
154 		return (ENOTTY);
155 	}
156 
157 	if (node != 0 && node != lastnode) {
158 		/* Not an easy one, must search for it */
159 		ok = openfirm_checkid(OF_peer(0), node);
160 		if (!ok)
161 			return (EINVAL);
162 		lastnode = node;
163 	}
164 
165 	name = value = NULL;
166 	error = 0;
167 	switch (cmd) {
168 
169 	case OFIOCGET:
170 		if ((flags & FREAD) == 0)
171 			return (EBADF);
172 		if (node == 0)
173 			return (EINVAL);
174 		error = openfirm_getstr(of->of_namelen, of->of_name, &name);
175 		if (error)
176 			break;
177 		len = OF_getproplen(node, name);
178 		if (len > of->of_buflen) {
179 			error = ENOMEM;
180 			break;
181 		}
182 		of->of_buflen = len;
183 		/* -1 means no entry; 0 means no value */
184 		if (len <= 0)
185 			break;
186 		value = malloc(len, M_TEMP, M_WAITOK);
187 		if (value == NULL) {
188 			error = ENOMEM;
189 			break;
190 		}
191 		len = OF_getprop(node, name, (void *)value, len);
192 		error = copyout(value, of->of_buf, len);
193 		break;
194 
195 #if 0
196 	case OFIOCSET:
197 		if ((flags & FWRITE) == 0)
198 			return (EBADF);
199 		if (node == 0)
200 			return (EINVAL);
201 		error = openfirm_getstr(of->of_namelen, of->of_name, &name);
202 		if (error)
203 			break;
204 		error = openfirm_getstr(of->of_buflen, of->of_buf, &value);
205 		if (error)
206 			break;
207 		len = OF_setprop(node, name, value, of->of_buflen);
208 		if (len != of->of_buflen)
209 			error = EINVAL;
210 		break;
211 #endif
212 
213 	case OFIOCNEXTPROP:
214 		if ((flags & FREAD) == 0)
215 			return (EBADF);
216 		if (node == 0 || of->of_buflen < 0)
217 			return (EINVAL);
218 		if (of->of_namelen != 0) {
219 			error = openfirm_getstr(of->of_namelen, of->of_name,
220 			    &name);
221 			if (error)
222 				break;
223 		}
224 		ok = OF_nextprop(node, name, newname);
225 		if (ok == 0) {
226 			error = ENOENT;
227 			break;
228 		}
229 		if (ok == -1) {
230 			error = EINVAL;
231 			break;
232 		}
233 		len = strlen(newname) + 1;
234 		if (len > of->of_buflen)
235 			len = of->of_buflen;
236 		else
237 			of->of_buflen = len;
238 		error = copyout(newname, of->of_buf, len);
239 		break;
240 
241 	case OFIOCGETNEXT:
242 		if ((flags & FREAD) == 0)
243 			return (EBADF);
244 		node = OF_peer(node);
245 		*(phandle_t *)data = lastnode = node;
246 		break;
247 
248 	case OFIOCGETCHILD:
249 		if ((flags & FREAD) == 0)
250 			return (EBADF);
251 		if (node == 0)
252 			return (EINVAL);
253 		node = OF_child(node);
254 		*(phandle_t *)data = lastnode = node;
255 		break;
256 
257 	case OFIOCFINDDEVICE:
258 		if ((flags & FREAD) == 0)
259 			return (EBADF);
260 		error = openfirm_getstr(of->of_namelen, of->of_name, &name);
261 		if (error)
262 			break;
263 		node = OF_finddevice(name);
264 		if (node == 0 || node == -1) {
265 			error = ENOENT;
266 			break;
267 		}
268 		of->of_nodeid = lastnode = node;
269 		break;
270 	}
271 
272 	if (name != NULL)
273 		free(name, M_TEMP);
274 	if (value != NULL)
275 		free(value, M_TEMP);
276 
277 	return (error);
278 }
279 
280 static int
281 openfirm_modevent(module_t mod, int type, void *data)
282 {
283 	switch(type) {
284 	case MOD_LOAD:
285 		if (bootverbose)
286 			printf("openfirm: <OpenFirmware control device>\n");
287 		/*
288 		 * Allow only root access by default; this device may allow
289 		 * users to peek into firmware passwords, and likely to crash
290 		 * the machine on some boxen due to firmware quirks.
291 		 */
292 		openfirm_dev = make_dev(&openfirm_cdevsw, OPENFIRM_MINOR,
293 		    UID_ROOT, GID_WHEEL, 0600, "openfirm");
294 		return 0;
295 
296 	case MOD_UNLOAD:
297 		destroy_dev(openfirm_dev);
298 		return 0;
299 
300 	case MOD_SHUTDOWN:
301 		return 0;
302 
303 	default:
304 		return EOPNOTSUPP;
305 	}
306 }
307 
308 DEV_MODULE(openfirm, openfirm_modevent, NULL);
309