xref: /freebsd/sys/dev/ofw/openfirmio.c (revision a163d034fadcfb4a25ca34a2ba5f491c47b6ff69)
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 CDEV_MAJOR	177
66de540622SThomas Moestl #define	OPENFIRM_MINOR	0
67de540622SThomas Moestl 
68de540622SThomas Moestl static struct cdevsw openfirm_cdevsw = {
69de540622SThomas Moestl 	/* open */	nullopen,
70de540622SThomas Moestl 	/* close */	nullclose,
71de540622SThomas Moestl 	/* read */	noread,
72de540622SThomas Moestl 	/* write */	nowrite,
73de540622SThomas Moestl 	/* ioctl */	openfirm_ioctl,
74de540622SThomas Moestl 	/* poll */	nopoll,
75de540622SThomas Moestl 	/* mmap */	nommap,
76de540622SThomas Moestl 	/* strategy */	nostrategy,
77de540622SThomas Moestl 	/* name */	"openfirm",
78de540622SThomas Moestl 	/* maj */	CDEV_MAJOR,
79de540622SThomas Moestl 	/* dump */	nodump,
80de540622SThomas Moestl 	/* psize */	nopsize,
81de540622SThomas Moestl 	/* flags */	0,
82de540622SThomas Moestl 	/* kqfilter */	nokqfilter
83de540622SThomas Moestl };
84de540622SThomas Moestl 
85de540622SThomas Moestl static phandle_t lastnode;	/* speed hack */
86de540622SThomas Moestl 
87de540622SThomas Moestl static int openfirm_checkid(phandle_t, phandle_t);
88de540622SThomas Moestl static int openfirm_getstr(int, const char *, char **);
89de540622SThomas Moestl 
90de540622SThomas Moestl /* Maximum accepted name length. */
91de540622SThomas Moestl #define	OFW_NAME_MAX	8191
92de540622SThomas Moestl 
93de540622SThomas Moestl /*
94de540622SThomas Moestl  * Verify target ID is valid (exists in the OPENPROM tree), as
95de540622SThomas Moestl  * listed from node ID sid forward.
96de540622SThomas Moestl  */
97de540622SThomas Moestl static int
98de540622SThomas Moestl openfirm_checkid(phandle_t sid, phandle_t tid)
99de540622SThomas Moestl {
100de540622SThomas Moestl 
101de540622SThomas Moestl 	for (; sid != 0; sid = OF_peer(sid))
102de540622SThomas Moestl 		if (sid == tid || openfirm_checkid(OF_child(sid), tid))
103de540622SThomas Moestl 			return (1);
104de540622SThomas Moestl 
105de540622SThomas Moestl 	return (0);
106de540622SThomas Moestl }
107de540622SThomas Moestl 
108de540622SThomas Moestl static int
109de540622SThomas Moestl openfirm_getstr(int len, const char *user, char **cpp)
110de540622SThomas Moestl {
111de540622SThomas Moestl 	int error;
112de540622SThomas Moestl 	char *cp;
113de540622SThomas Moestl 
114de540622SThomas Moestl 	/* Reject obvious bogus requests */
115de540622SThomas Moestl 	if ((u_int)len > OFW_NAME_MAX)
116de540622SThomas Moestl 		return (ENAMETOOLONG);
117de540622SThomas Moestl 
118a163d034SWarner Losh 	*cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
119de540622SThomas Moestl 	if (cp == NULL)
120de540622SThomas Moestl 		return (ENOMEM);
121de540622SThomas Moestl 	error = copyin(user, cp, len);
122de540622SThomas Moestl 	cp[len] = '\0';
123de540622SThomas Moestl 	return (error);
124de540622SThomas Moestl }
125de540622SThomas Moestl 
126de540622SThomas Moestl int
127de540622SThomas Moestl openfirm_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags,
128de540622SThomas Moestl     struct thread *td)
129de540622SThomas Moestl {
130de540622SThomas Moestl 	struct ofiocdesc *of;
131de540622SThomas Moestl 	phandle_t node;
132de540622SThomas Moestl 	int len, ok, error;
133de540622SThomas Moestl 	char *name, *value;
134de540622SThomas Moestl 	char newname[32];
135de540622SThomas Moestl 
136de540622SThomas Moestl 	of = (struct ofiocdesc *)data;
137de540622SThomas Moestl 	switch (cmd) {
138de540622SThomas Moestl 	case OFIOCGETOPTNODE:
139de540622SThomas Moestl 		*(phandle_t *) data = OF_finddevice("/options");
140de540622SThomas Moestl 		return (0);
141de540622SThomas Moestl 	case OFIOCGET:
142de540622SThomas Moestl #if 0
143de540622SThomas Moestl 	case OFIOCSET:
144de540622SThomas Moestl #endif
145de540622SThomas Moestl 	case OFIOCNEXTPROP:
146de540622SThomas Moestl 	case OFIOCFINDDEVICE:
147de540622SThomas Moestl 		node = of->of_nodeid;
148de540622SThomas Moestl 		break;
149de540622SThomas Moestl 	case OFIOCGETNEXT:
150de540622SThomas Moestl 	case OFIOCGETCHILD:
151de540622SThomas Moestl 		node = *(phandle_t *)data;
152de540622SThomas Moestl 		break;
153de540622SThomas Moestl 	default:
154de540622SThomas Moestl 		return (ENOTTY);
155de540622SThomas Moestl 	}
156de540622SThomas Moestl 
157de540622SThomas Moestl 	if (node != 0 && node != lastnode) {
158de540622SThomas Moestl 		/* Not an easy one, must search for it */
159de540622SThomas Moestl 		ok = openfirm_checkid(OF_peer(0), node);
160de540622SThomas Moestl 		if (!ok)
161de540622SThomas Moestl 			return (EINVAL);
162de540622SThomas Moestl 		lastnode = node;
163de540622SThomas Moestl 	}
164de540622SThomas Moestl 
165de540622SThomas Moestl 	name = value = NULL;
166de540622SThomas Moestl 	error = 0;
167de540622SThomas Moestl 	switch (cmd) {
168de540622SThomas Moestl 
169de540622SThomas Moestl 	case OFIOCGET:
170de540622SThomas Moestl 		if ((flags & FREAD) == 0)
171de540622SThomas Moestl 			return (EBADF);
172de540622SThomas Moestl 		if (node == 0)
173de540622SThomas Moestl 			return (EINVAL);
174de540622SThomas Moestl 		error = openfirm_getstr(of->of_namelen, of->of_name, &name);
175de540622SThomas Moestl 		if (error)
176de540622SThomas Moestl 			break;
177de540622SThomas Moestl 		len = OF_getproplen(node, name);
178de540622SThomas Moestl 		if (len > of->of_buflen) {
179de540622SThomas Moestl 			error = ENOMEM;
180de540622SThomas Moestl 			break;
181de540622SThomas Moestl 		}
182de540622SThomas Moestl 		of->of_buflen = len;
183de540622SThomas Moestl 		/* -1 means no entry; 0 means no value */
184de540622SThomas Moestl 		if (len <= 0)
185de540622SThomas Moestl 			break;
186a163d034SWarner Losh 		value = malloc(len, M_TEMP, M_WAITOK);
187de540622SThomas Moestl 		if (value == NULL) {
188de540622SThomas Moestl 			error = ENOMEM;
189de540622SThomas Moestl 			break;
190de540622SThomas Moestl 		}
191de540622SThomas Moestl 		len = OF_getprop(node, name, (void *)value, len);
192de540622SThomas Moestl 		error = copyout(value, of->of_buf, len);
193de540622SThomas Moestl 		break;
194de540622SThomas Moestl 
195de540622SThomas Moestl #if 0
196de540622SThomas Moestl 	case OFIOCSET:
197de540622SThomas Moestl 		if ((flags & FWRITE) == 0)
198de540622SThomas Moestl 			return (EBADF);
199de540622SThomas Moestl 		if (node == 0)
200de540622SThomas Moestl 			return (EINVAL);
201de540622SThomas Moestl 		error = openfirm_getstr(of->of_namelen, of->of_name, &name);
202de540622SThomas Moestl 		if (error)
203de540622SThomas Moestl 			break;
204de540622SThomas Moestl 		error = openfirm_getstr(of->of_buflen, of->of_buf, &value);
205de540622SThomas Moestl 		if (error)
206de540622SThomas Moestl 			break;
207de540622SThomas Moestl 		len = OF_setprop(node, name, value, of->of_buflen);
208de540622SThomas Moestl 		if (len != of->of_buflen)
209de540622SThomas Moestl 			error = EINVAL;
210de540622SThomas Moestl 		break;
211de540622SThomas Moestl #endif
212de540622SThomas Moestl 
213de540622SThomas Moestl 	case OFIOCNEXTPROP:
214de540622SThomas Moestl 		if ((flags & FREAD) == 0)
215de540622SThomas Moestl 			return (EBADF);
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 		if ((flags & FREAD) == 0)
243de540622SThomas Moestl 			return (EBADF);
244de540622SThomas Moestl 		node = OF_peer(node);
245de540622SThomas Moestl 		*(phandle_t *)data = lastnode = node;
246de540622SThomas Moestl 		break;
247de540622SThomas Moestl 
248de540622SThomas Moestl 	case OFIOCGETCHILD:
249de540622SThomas Moestl 		if ((flags & FREAD) == 0)
250de540622SThomas Moestl 			return (EBADF);
251de540622SThomas Moestl 		if (node == 0)
252de540622SThomas Moestl 			return (EINVAL);
253de540622SThomas Moestl 		node = OF_child(node);
254de540622SThomas Moestl 		*(phandle_t *)data = lastnode = node;
255de540622SThomas Moestl 		break;
256de540622SThomas Moestl 
257de540622SThomas Moestl 	case OFIOCFINDDEVICE:
258de540622SThomas Moestl 		if ((flags & FREAD) == 0)
259de540622SThomas Moestl 			return (EBADF);
260de540622SThomas Moestl 		error = openfirm_getstr(of->of_namelen, of->of_name, &name);
261de540622SThomas Moestl 		if (error)
262de540622SThomas Moestl 			break;
263de540622SThomas Moestl 		node = OF_finddevice(name);
264de540622SThomas Moestl 		if (node == 0 || node == -1) {
265de540622SThomas Moestl 			error = ENOENT;
266de540622SThomas Moestl 			break;
267de540622SThomas Moestl 		}
268de540622SThomas Moestl 		of->of_nodeid = lastnode = node;
269de540622SThomas Moestl 		break;
270de540622SThomas Moestl 	}
271de540622SThomas Moestl 
272de540622SThomas Moestl 	if (name != NULL)
273de540622SThomas Moestl 		free(name, M_TEMP);
274de540622SThomas Moestl 	if (value != NULL)
275de540622SThomas Moestl 		free(value, M_TEMP);
276de540622SThomas Moestl 
277de540622SThomas Moestl 	return (error);
278de540622SThomas Moestl }
279de540622SThomas Moestl 
280de540622SThomas Moestl static int
281de540622SThomas Moestl openfirm_modevent(module_t mod, int type, void *data)
282de540622SThomas Moestl {
283de540622SThomas Moestl 	switch(type) {
284de540622SThomas Moestl 	case MOD_LOAD:
285de540622SThomas Moestl 		if (bootverbose)
286de540622SThomas Moestl 			printf("openfirm: <OpenFirmware control device>\n");
287de540622SThomas Moestl 		/*
288de540622SThomas Moestl 		 * Allow only root access by default; this device may allow
289de540622SThomas Moestl 		 * users to peek into firmware passwords, and likely to crash
290de540622SThomas Moestl 		 * the machine on some boxen due to firmware quirks.
291de540622SThomas Moestl 		 */
292de540622SThomas Moestl 		openfirm_dev = make_dev(&openfirm_cdevsw, OPENFIRM_MINOR,
293de540622SThomas Moestl 		    UID_ROOT, GID_WHEEL, 0600, "openfirm");
294de540622SThomas Moestl 		return 0;
295de540622SThomas Moestl 
296de540622SThomas Moestl 	case MOD_UNLOAD:
297de540622SThomas Moestl 		destroy_dev(openfirm_dev);
298de540622SThomas Moestl 		return 0;
299de540622SThomas Moestl 
300de540622SThomas Moestl 	case MOD_SHUTDOWN:
301de540622SThomas Moestl 		return 0;
302de540622SThomas Moestl 
303de540622SThomas Moestl 	default:
304de540622SThomas Moestl 		return EOPNOTSUPP;
305de540622SThomas Moestl 	}
306de540622SThomas Moestl }
307de540622SThomas Moestl 
308de540622SThomas Moestl DEV_MODULE(openfirm, openfirm_modevent, NULL);
309