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