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