xref: /freebsd/sys/dev/ofw/openfirmio.c (revision 29363fb446372cb3f10bc98664e9767c53fbb457)
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>
4098ca2bdSWarner Losh /*-
57282444bSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
67282444bSPedro F. Giffuni  *
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.
27fbbd9655SWarner Losh  * 3. 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  */
44de540622SThomas Moestl 
45de540622SThomas Moestl #include <sys/param.h>
46de540622SThomas Moestl #include <sys/systm.h>
47de540622SThomas Moestl #include <sys/conf.h>
48de540622SThomas Moestl #include <sys/errno.h>
49de540622SThomas Moestl #include <sys/fcntl.h>
50de540622SThomas Moestl #include <sys/ioccom.h>
51de540622SThomas Moestl #include <sys/kernel.h>
52de540622SThomas Moestl #include <sys/malloc.h>
53de540622SThomas Moestl #include <sys/module.h>
54de540622SThomas Moestl 
55de540622SThomas Moestl #include <dev/ofw/openfirmio.h>
56de540622SThomas Moestl 
5789c9c53dSPoul-Henning Kamp static struct cdev *openfirm_dev;
58de540622SThomas Moestl 
59de540622SThomas Moestl static d_ioctl_t openfirm_ioctl;
60de540622SThomas Moestl 
61de540622SThomas Moestl #define	OPENFIRM_MINOR	0
62de540622SThomas Moestl 
63de540622SThomas Moestl static struct cdevsw openfirm_cdevsw = {
64dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
65*fbeb4ccaSMark Johnston 	.d_flags =	D_NEEDGIANT | D_GIANTOK,
667ac40f5fSPoul-Henning Kamp 	.d_ioctl =	openfirm_ioctl,
677ac40f5fSPoul-Henning Kamp 	.d_name =	"openfirm",
68de540622SThomas Moestl };
69de540622SThomas Moestl 
70de540622SThomas Moestl static phandle_t lastnode;	/* speed hack */
71de540622SThomas Moestl 
72de540622SThomas Moestl static int openfirm_checkid(phandle_t, phandle_t);
73de540622SThomas Moestl static int openfirm_getstr(int, const char *, char **);
74de540622SThomas Moestl 
75de540622SThomas Moestl /*
76de540622SThomas Moestl  * Verify target ID is valid (exists in the OPENPROM tree), as
77de540622SThomas Moestl  * listed from node ID sid forward.
78de540622SThomas Moestl  */
79de540622SThomas Moestl static int
openfirm_checkid(phandle_t sid,phandle_t tid)80de540622SThomas Moestl openfirm_checkid(phandle_t sid, phandle_t tid)
81de540622SThomas Moestl {
82de540622SThomas Moestl 
83de540622SThomas Moestl 	for (; sid != 0; sid = OF_peer(sid))
84de540622SThomas Moestl 		if (sid == tid || openfirm_checkid(OF_child(sid), tid))
85de540622SThomas Moestl 			return (1);
86de540622SThomas Moestl 
87de540622SThomas Moestl 	return (0);
88de540622SThomas Moestl }
89de540622SThomas Moestl 
90de540622SThomas Moestl static int
openfirm_getstr(int len,const char * user,char ** cpp)91de540622SThomas Moestl openfirm_getstr(int len, const char *user, char **cpp)
92de540622SThomas Moestl {
93de540622SThomas Moestl 	int error;
94de540622SThomas Moestl 	char *cp;
95de540622SThomas Moestl 
96481d6b54SMarius Strobl 	/* Reject obvious bogus requests. */
97073e8552SMarius Strobl 	if ((u_int)len > OFIOCMAXNAME)
98de540622SThomas Moestl 		return (ENAMETOOLONG);
99de540622SThomas Moestl 
100a163d034SWarner Losh 	*cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
101de540622SThomas Moestl 	error = copyin(user, cp, len);
102de540622SThomas Moestl 	cp[len] = '\0';
103de540622SThomas Moestl 	return (error);
104de540622SThomas Moestl }
105de540622SThomas Moestl 
106de540622SThomas Moestl int
openfirm_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int flags,struct thread * td)10789c9c53dSPoul-Henning Kamp openfirm_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
108de540622SThomas Moestl     struct thread *td)
109de540622SThomas Moestl {
110de540622SThomas Moestl 	struct ofiocdesc *of;
111de540622SThomas Moestl 	phandle_t node;
112de540622SThomas Moestl 	int len, ok, error;
113de540622SThomas Moestl 	char *name, *value;
11426869ad1SBrandon Bergren 	char newname[OFIOCSUGGPROPNAMELEN];
115de540622SThomas Moestl 
116933c9a14SThomas Moestl 	if ((flags & FREAD) == 0)
117933c9a14SThomas Moestl 		return (EBADF);
118933c9a14SThomas Moestl 
119de540622SThomas Moestl 	of = (struct ofiocdesc *)data;
120de540622SThomas Moestl 	switch (cmd) {
121de540622SThomas Moestl 	case OFIOCGETOPTNODE:
122de540622SThomas Moestl 		*(phandle_t *) data = OF_finddevice("/options");
123de540622SThomas Moestl 		return (0);
124de540622SThomas Moestl 	case OFIOCGET:
125de540622SThomas Moestl 	case OFIOCSET:
126de540622SThomas Moestl 	case OFIOCNEXTPROP:
127de540622SThomas Moestl 	case OFIOCFINDDEVICE:
128933c9a14SThomas Moestl 	case OFIOCGETPROPLEN:
129de540622SThomas Moestl 		node = of->of_nodeid;
130de540622SThomas Moestl 		break;
131de540622SThomas Moestl 	case OFIOCGETNEXT:
132de540622SThomas Moestl 	case OFIOCGETCHILD:
133de540622SThomas Moestl 		node = *(phandle_t *)data;
134de540622SThomas Moestl 		break;
135de540622SThomas Moestl 	default:
136933c9a14SThomas Moestl 		return (ENOIOCTL);
137de540622SThomas Moestl 	}
138de540622SThomas Moestl 
139de540622SThomas Moestl 	if (node != 0 && node != lastnode) {
140481d6b54SMarius Strobl 		/* Not an easy one, we must search for it. */
141de540622SThomas Moestl 		ok = openfirm_checkid(OF_peer(0), node);
142de540622SThomas Moestl 		if (!ok)
143de540622SThomas Moestl 			return (EINVAL);
144de540622SThomas Moestl 		lastnode = node;
145de540622SThomas Moestl 	}
146de540622SThomas Moestl 
147de540622SThomas Moestl 	name = value = NULL;
148de540622SThomas Moestl 	error = 0;
149de540622SThomas Moestl 	switch (cmd) {
150de540622SThomas Moestl 	case OFIOCGET:
151933c9a14SThomas Moestl 	case OFIOCGETPROPLEN:
152de540622SThomas Moestl 		if (node == 0)
153de540622SThomas Moestl 			return (EINVAL);
154de540622SThomas Moestl 		error = openfirm_getstr(of->of_namelen, of->of_name, &name);
155de540622SThomas Moestl 		if (error)
156de540622SThomas Moestl 			break;
157de540622SThomas Moestl 		len = OF_getproplen(node, name);
158933c9a14SThomas Moestl 		if (cmd == OFIOCGETPROPLEN) {
159933c9a14SThomas Moestl 			of->of_buflen = len;
160933c9a14SThomas Moestl 			break;
161933c9a14SThomas Moestl 		}
162de540622SThomas Moestl 		if (len > of->of_buflen) {
163de540622SThomas Moestl 			error = ENOMEM;
164de540622SThomas Moestl 			break;
165de540622SThomas Moestl 		}
166de540622SThomas Moestl 		of->of_buflen = len;
167481d6b54SMarius Strobl 		/* -1 means no entry; 0 means no value. */
168de540622SThomas Moestl 		if (len <= 0)
169de540622SThomas Moestl 			break;
170a163d034SWarner Losh 		value = malloc(len, M_TEMP, M_WAITOK);
171de540622SThomas Moestl 		len = OF_getprop(node, name, (void *)value, len);
172de540622SThomas Moestl 		error = copyout(value, of->of_buf, len);
173de540622SThomas Moestl 		break;
174de540622SThomas Moestl 
175de540622SThomas Moestl 	case OFIOCSET:
176073e8552SMarius Strobl 		/*
177073e8552SMarius Strobl 		 * Note: Text string values for at least the /options node
178e70415b3SMarius Strobl 		 * have to be null-terminated and the length parameter must
179073e8552SMarius Strobl 		 * include this terminating null.  However, like OF_getprop(),
180e70415b3SMarius Strobl 		 * OF_setprop() will return the actual length of the text
181073e8552SMarius Strobl 		 * string, i.e. omitting the terminating null.
182073e8552SMarius Strobl 		 */
183de540622SThomas Moestl 		if ((flags & FWRITE) == 0)
184de540622SThomas Moestl 			return (EBADF);
185de540622SThomas Moestl 		if (node == 0)
186de540622SThomas Moestl 			return (EINVAL);
187073e8552SMarius Strobl 		if ((u_int)of->of_buflen > OFIOCMAXVALUE)
188073e8552SMarius Strobl 			return (ENAMETOOLONG);
189de540622SThomas Moestl 		error = openfirm_getstr(of->of_namelen, of->of_name, &name);
190de540622SThomas Moestl 		if (error)
191de540622SThomas Moestl 			break;
192073e8552SMarius Strobl 		value = malloc(of->of_buflen, M_TEMP, M_WAITOK);
193073e8552SMarius Strobl 		error = copyin(of->of_buf, value, of->of_buflen);
194de540622SThomas Moestl 		if (error)
195de540622SThomas Moestl 			break;
196de540622SThomas Moestl 		len = OF_setprop(node, name, value, of->of_buflen);
197073e8552SMarius Strobl 		if (len < 0)
198de540622SThomas Moestl 			error = EINVAL;
199073e8552SMarius Strobl 		of->of_buflen = len;
200de540622SThomas Moestl 		break;
201de540622SThomas Moestl 
202de540622SThomas Moestl 	case OFIOCNEXTPROP:
203de540622SThomas Moestl 		if (node == 0 || of->of_buflen < 0)
204de540622SThomas Moestl 			return (EINVAL);
205de540622SThomas Moestl 		if (of->of_namelen != 0) {
206de540622SThomas Moestl 			error = openfirm_getstr(of->of_namelen, of->of_name,
207de540622SThomas Moestl 			    &name);
208de540622SThomas Moestl 			if (error)
209de540622SThomas Moestl 				break;
210de540622SThomas Moestl 		}
21191416fb2SNathan Whitehorn 		ok = OF_nextprop(node, name, newname, sizeof(newname));
212de540622SThomas Moestl 		if (ok == 0) {
213de540622SThomas Moestl 			error = ENOENT;
214de540622SThomas Moestl 			break;
215de540622SThomas Moestl 		}
216de540622SThomas Moestl 		if (ok == -1) {
217de540622SThomas Moestl 			error = EINVAL;
218de540622SThomas Moestl 			break;
219de540622SThomas Moestl 		}
220de540622SThomas Moestl 		len = strlen(newname) + 1;
22126869ad1SBrandon Bergren 		if (len > of->of_buflen) {
22226869ad1SBrandon Bergren 			/*
22326869ad1SBrandon Bergren 			 * Passed buffer was insufficient.
22426869ad1SBrandon Bergren 			 *
22526869ad1SBrandon Bergren 			 * Instead of returning an error here, truncate the
22626869ad1SBrandon Bergren 			 * property name to fit the buffer.
22726869ad1SBrandon Bergren 			 *
22826869ad1SBrandon Bergren 			 * This allows us to retain compatibility with old
22926869ad1SBrandon Bergren 			 * tools which always pass a 32 character buffer.
23026869ad1SBrandon Bergren 			 */
231de540622SThomas Moestl 			len = of->of_buflen;
23226869ad1SBrandon Bergren 			newname[len - 1] = '\0';
23326869ad1SBrandon Bergren 		}
234de540622SThomas Moestl 		else
235de540622SThomas Moestl 			of->of_buflen = len;
236de540622SThomas Moestl 		error = copyout(newname, of->of_buf, len);
237de540622SThomas Moestl 		break;
238de540622SThomas Moestl 
239de540622SThomas Moestl 	case OFIOCGETNEXT:
240de540622SThomas Moestl 		node = OF_peer(node);
241de540622SThomas Moestl 		*(phandle_t *)data = lastnode = node;
242de540622SThomas Moestl 		break;
243de540622SThomas Moestl 
244de540622SThomas Moestl 	case OFIOCGETCHILD:
245de540622SThomas Moestl 		if (node == 0)
246de540622SThomas Moestl 			return (EINVAL);
247de540622SThomas Moestl 		node = OF_child(node);
248de540622SThomas Moestl 		*(phandle_t *)data = lastnode = node;
249de540622SThomas Moestl 		break;
250de540622SThomas Moestl 
251de540622SThomas Moestl 	case OFIOCFINDDEVICE:
252de540622SThomas Moestl 		error = openfirm_getstr(of->of_namelen, of->of_name, &name);
253de540622SThomas Moestl 		if (error)
254de540622SThomas Moestl 			break;
255de540622SThomas Moestl 		node = OF_finddevice(name);
256108117ccSOleksandr Tymoshenko 		if (node == -1) {
257de540622SThomas Moestl 			error = ENOENT;
258de540622SThomas Moestl 			break;
259de540622SThomas Moestl 		}
260de540622SThomas Moestl 		of->of_nodeid = lastnode = node;
261de540622SThomas Moestl 		break;
262de540622SThomas Moestl 	}
263de540622SThomas Moestl 
264de540622SThomas Moestl 	if (name != NULL)
265de540622SThomas Moestl 		free(name, M_TEMP);
266de540622SThomas Moestl 	if (value != NULL)
267de540622SThomas Moestl 		free(value, M_TEMP);
268de540622SThomas Moestl 
269de540622SThomas Moestl 	return (error);
270de540622SThomas Moestl }
271de540622SThomas Moestl 
272de540622SThomas Moestl static int
openfirm_modevent(module_t mod,int type,void * data)273de540622SThomas Moestl openfirm_modevent(module_t mod, int type, void *data)
274de540622SThomas Moestl {
275481d6b54SMarius Strobl 
276de540622SThomas Moestl 	switch(type) {
277de540622SThomas Moestl 	case MOD_LOAD:
278de540622SThomas Moestl 		if (bootverbose)
279de540622SThomas Moestl 			printf("openfirm: <Open Firmware control device>\n");
280de540622SThomas Moestl 		/*
281de540622SThomas Moestl 		 * Allow only root access by default; this device may allow
282de540622SThomas Moestl 		 * users to peek into firmware passwords, and likely to crash
283de540622SThomas Moestl 		 * the machine on some boxen due to firmware quirks.
284de540622SThomas Moestl 		 */
285de540622SThomas Moestl 		openfirm_dev = make_dev(&openfirm_cdevsw, OPENFIRM_MINOR,
286de540622SThomas Moestl 		    UID_ROOT, GID_WHEEL, 0600, "openfirm");
287de540622SThomas Moestl 		return 0;
288de540622SThomas Moestl 
289de540622SThomas Moestl 	case MOD_UNLOAD:
290de540622SThomas Moestl 		destroy_dev(openfirm_dev);
291de540622SThomas Moestl 		return 0;
292de540622SThomas Moestl 
293de540622SThomas Moestl 	case MOD_SHUTDOWN:
294de540622SThomas Moestl 		return 0;
295de540622SThomas Moestl 
296de540622SThomas Moestl 	default:
297de540622SThomas Moestl 		return EOPNOTSUPP;
298de540622SThomas Moestl 	}
299de540622SThomas Moestl }
300de540622SThomas Moestl 
301de540622SThomas Moestl DEV_MODULE(openfirm, openfirm_modevent, NULL);
302