]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ofw/openpromio.c
MFV r322232: 8426 mark immutable buffer arguments as such in abd.h
[FreeBSD/FreeBSD.git] / sys / dev / ofw / openpromio.c
1 /*-
2  * Copyright (c) 2003 Jake Burkholder.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/conf.h>
34 #include <sys/errno.h>
35 #include <sys/fcntl.h>
36 #include <sys/ioccom.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40
41 #include <dev/ofw/openfirm.h>
42 #include <dev/ofw/openpromio.h>
43
44 /*
45  * This provides a Solaris compatible character device interface to
46  * Open Firmware.  It exists entirely for compatibility with software
47  * like X11, and only the features that are actually needed for that
48  * are implemented.  The interface sucks too much to actually use,
49  * new code should use the /dev/openfirm device.
50  */
51
52 static d_open_t openprom_open;
53 static d_close_t openprom_close;
54 static d_ioctl_t openprom_ioctl;
55
56 static int openprom_modevent(module_t mode, int type, void *data);
57 static int openprom_node_valid(phandle_t node);
58 static int openprom_node_search(phandle_t root, phandle_t node);
59
60 static struct cdevsw openprom_cdevsw = {
61         .d_version =    D_VERSION,
62         .d_flags =      D_NEEDGIANT,
63         .d_open =       openprom_open,
64         .d_close =      openprom_close,
65         .d_ioctl =      openprom_ioctl,
66         .d_name =       "openprom",
67 };
68
69 static int openprom_is_open;
70 static struct cdev *openprom_dev;
71 static phandle_t openprom_node;
72
73 static int
74 openprom_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
75 {
76
77         if (openprom_is_open != 0)
78                 return (EBUSY);
79         openprom_is_open = 1;
80         return (0);
81 }
82
83 static int
84 openprom_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
85 {
86
87         openprom_is_open = 0;
88         return (0);
89 }
90
91 static int
92 openprom_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
93     struct thread *td)
94 {
95         struct openpromio *oprom;
96         phandle_t node;
97         uint32_t len;
98         size_t done;
99         int proplen;
100         char *prop;
101         char *buf;
102         int error;
103
104         if ((flags & FREAD) == 0)
105                 return (EPERM);
106
107         prop = buf = NULL;
108         error = 0;
109         switch (cmd) {
110         case OPROMCHILD:
111         case OPROMNEXT:
112                 if (data == NULL || *(void **)data == NULL)
113                         return (EINVAL);
114                 oprom = *(void **)data;
115                 error = copyin(&oprom->oprom_size, &len, sizeof(len));
116                 if (error != 0)
117                         break;
118                 if (len != sizeof(node)) {
119                         error = EINVAL;
120                         break;
121                 }
122                 error = copyin(&oprom->oprom_array, &node, sizeof(node));
123                 if (error != 0)
124                         break;
125                 error = openprom_node_valid(node);
126                 if (error != 0)
127                         break;
128                 switch (cmd) {
129                 case OPROMCHILD:
130                         node = OF_child(node);
131                         break;
132                 case OPROMNEXT:
133                         node = OF_peer(node);
134                         break;
135                 }
136                 error = copyout(&node, &oprom->oprom_array, sizeof(node));
137                 if (error != 0)
138                         break;
139                 openprom_node = node;
140                 break;
141         case OPROMGETPROP:
142         case OPROMNXTPROP:
143                 if (data == NULL || *(void **)data == NULL)
144                         return (EINVAL);
145                 oprom = *(void **)data;
146                 error = copyin(&oprom->oprom_size, &len, sizeof(len));
147                 if (error != 0)
148                         break;
149                 if (len > OPROMMAXPARAM) {
150                         error = EINVAL;
151                         break;
152                 }
153                 prop = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
154                 error = copyinstr(&oprom->oprom_array, prop, len, &done);
155                 if (error != 0)
156                         break;
157                 buf = malloc(OPROMMAXPARAM, M_TEMP, M_WAITOK | M_ZERO);
158                 node = openprom_node;
159                 switch (cmd) {
160                 case OPROMGETPROP:
161                         proplen = OF_getproplen(node, prop);
162                         if (proplen > OPROMMAXPARAM) {
163                                 error = EINVAL;
164                                 break;
165                         }
166                         error = OF_getprop(node, prop, buf, proplen);
167                         break;
168                 case OPROMNXTPROP:
169                         error = OF_nextprop(node, prop, buf, OPROMMAXPARAM);
170                         proplen = strlen(buf);
171                         break;
172                 }
173                 if (error != -1) {
174                         error = copyout(&proplen, &oprom->oprom_size,
175                             sizeof(proplen));
176                         if (error == 0)
177                                 error = copyout(buf, &oprom->oprom_array,
178                                     proplen + 1);
179                 } else
180                         error = EINVAL;
181                 break;
182         default:
183                 error = ENOIOCTL;
184                 break;
185         }
186
187         if (prop != NULL)
188                 free(prop, M_TEMP);
189         if (buf != NULL)
190                 free(buf, M_TEMP);
191
192         return (error);
193 }
194
195 static int
196 openprom_node_valid(phandle_t node)
197 {
198
199         if (node == 0)
200                 return (0);
201         return (openprom_node_search(OF_peer(0), node));
202 }
203
204 static int
205 openprom_node_search(phandle_t root, phandle_t node)
206 {
207         phandle_t child;
208
209         if (root == node)
210                 return (0);
211         for (child = OF_child(root); child != 0 && child != -1;
212             child = OF_peer(child))
213                 if (openprom_node_search(child, node) == 0)
214                         return (0);
215         return (EINVAL);
216 }
217
218 static int
219 openprom_modevent(module_t mode, int type, void *data)
220 {
221
222         switch (type) {
223         case MOD_LOAD:
224                 openprom_dev = make_dev(&openprom_cdevsw, 0, UID_ROOT,
225                     GID_WHEEL, 0600, "openprom");
226                 return (0);
227         case MOD_UNLOAD:
228                 destroy_dev(openprom_dev);
229                 return (0);
230         default:
231                 return (EOPNOTSUPP);
232         }
233 }
234
235 DEV_MODULE(openprom, openprom_modevent, NULL);