]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ofw/openfirmio.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304659, and update
[FreeBSD/FreeBSD.git] / sys / dev / ofw / openfirmio.c
1 /*      $NetBSD: openfirmio.c,v 1.4 2002/09/06 13:23:19 gehenna Exp $ */
2
3 #include <sys/cdefs.h>
4 __FBSDID("$FreeBSD$");
5
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,
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[32];
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
153         case OFIOCGET:
154         case OFIOCGETPROPLEN:
155                 if (node == 0)
156                         return (EINVAL);
157                 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
158                 if (error)
159                         break;
160                 len = OF_getproplen(node, name);
161                 if (cmd == OFIOCGETPROPLEN) {
162                         of->of_buflen = len;
163                         break;
164                 }
165                 if (len > of->of_buflen) {
166                         error = ENOMEM;
167                         break;
168                 }
169                 of->of_buflen = len;
170                 /* -1 means no entry; 0 means no value. */
171                 if (len <= 0)
172                         break;
173                 value = malloc(len, M_TEMP, M_WAITOK);
174                 len = OF_getprop(node, name, (void *)value, len);
175                 error = copyout(value, of->of_buf, len);
176                 break;
177
178         case OFIOCSET:
179                 /*
180                  * Note: Text string values for at least the /options node
181                  * have to be null-terminated and the length parameter must
182                  * include this terminating null.  However, like OF_getprop(),
183                  * OF_setprop() will return the actual length of the text
184                  * string, i.e. omitting the terminating null.
185                  */
186                 if ((flags & FWRITE) == 0)
187                         return (EBADF);
188                 if (node == 0)
189                         return (EINVAL);
190                 if ((u_int)of->of_buflen > OFIOCMAXVALUE)
191                         return (ENAMETOOLONG);
192                 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
193                 if (error)
194                         break;
195                 value = malloc(of->of_buflen, M_TEMP, M_WAITOK);
196                 error = copyin(of->of_buf, value, of->of_buflen);
197                 if (error)
198                         break;
199                 len = OF_setprop(node, name, value, of->of_buflen);
200                 if (len < 0)
201                         error = EINVAL;
202                 of->of_buflen = len;
203                 break;
204
205         case OFIOCNEXTPROP:
206                 if (node == 0 || of->of_buflen < 0)
207                         return (EINVAL);
208                 if (of->of_namelen != 0) {
209                         error = openfirm_getstr(of->of_namelen, of->of_name,
210                             &name);
211                         if (error)
212                                 break;
213                 }
214                 ok = OF_nextprop(node, name, newname, sizeof(newname));
215                 if (ok == 0) {
216                         error = ENOENT;
217                         break;
218                 }
219                 if (ok == -1) {
220                         error = EINVAL;
221                         break;
222                 }
223                 len = strlen(newname) + 1;
224                 if (len > of->of_buflen)
225                         len = of->of_buflen;
226                 else
227                         of->of_buflen = len;
228                 error = copyout(newname, of->of_buf, len);
229                 break;
230
231         case OFIOCGETNEXT:
232                 node = OF_peer(node);
233                 *(phandle_t *)data = lastnode = node;
234                 break;
235
236         case OFIOCGETCHILD:
237                 if (node == 0)
238                         return (EINVAL);
239                 node = OF_child(node);
240                 *(phandle_t *)data = lastnode = node;
241                 break;
242
243         case OFIOCFINDDEVICE:
244                 error = openfirm_getstr(of->of_namelen, of->of_name, &name);
245                 if (error)
246                         break;
247                 node = OF_finddevice(name);
248                 if (node == 0 || node == -1) {
249                         error = ENOENT;
250                         break;
251                 }
252                 of->of_nodeid = lastnode = node;
253                 break;
254         }
255
256         if (name != NULL)
257                 free(name, M_TEMP);
258         if (value != NULL)
259                 free(value, M_TEMP);
260
261         return (error);
262 }
263
264 static int
265 openfirm_modevent(module_t mod, int type, void *data)
266 {
267
268         switch(type) {
269         case MOD_LOAD:
270                 if (bootverbose)
271                         printf("openfirm: <Open Firmware control device>\n");
272                 /*
273                  * Allow only root access by default; this device may allow
274                  * users to peek into firmware passwords, and likely to crash
275                  * the machine on some boxen due to firmware quirks.
276                  */
277                 openfirm_dev = make_dev(&openfirm_cdevsw, OPENFIRM_MINOR,
278                     UID_ROOT, GID_WHEEL, 0600, "openfirm");
279                 return 0;
280
281         case MOD_UNLOAD:
282                 destroy_dev(openfirm_dev);
283                 return 0;
284
285         case MOD_SHUTDOWN:
286                 return 0;
287
288         default:
289                 return EOPNOTSUPP;
290         }
291 }
292
293 DEV_MODULE(openfirm, openfirm_modevent, NULL);