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