2 * Copyright (c) 2002 by Thomas Moestl <tmm@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
32 #include <dev/ofw/openfirmio.h>
43 #include "pathnames.h"
46 #define OFW_IOCTL(fd, cmd, val) do { \
47 if (ioctl(fd, cmd, val) == -1) \
48 err(EX_IOERR, "ioctl(..., " #cmd ", ...) failed"); \
56 if ((fd = open(PATH_DEV_OPENFIRM, mode)) == -1)
57 err(EX_UNAVAILABLE, "could not open " PATH_DEV_OPENFIRM);
72 return (ofw_peer(fd, 0));
80 OFW_IOCTL(fd, OFIOCGETOPTNODE, &rv);
85 ofw_peer(int fd, phandle_t node)
90 OFW_IOCTL(fd, OFIOCGETNEXT, &rv);
95 ofw_child(int fd, phandle_t node)
100 OFW_IOCTL(fd, OFIOCGETCHILD, &rv);
105 ofw_finddevice(int fd, const char *name)
110 d.of_namelen = strlen(name);
114 if (ioctl(fd, OFIOCFINDDEVICE, &d) == -1) {
116 err(EX_UNAVAILABLE, "Node '%s' not found", name);
119 "ioctl(..., OFIOCFINDDEVICE, ...) failed");
121 return (d.of_nodeid);
125 ofw_firstprop(int fd, phandle_t node, char *buf, int buflen)
128 return (ofw_nextprop(fd, node, NULL, buf, buflen));
132 ofw_nextprop(int fd, phandle_t node, const char *prev, char *buf, int buflen)
137 d.of_namelen = prev != NULL ? strlen(prev) : 0;
139 d.of_buflen = buflen;
141 if (ioctl(fd, OFIOCNEXTPROP, &d) == -1) {
145 err(EX_IOERR, "ioctl(..., OFIOCNEXTPROP, ...) failed");
147 return (d.of_buflen);
155 if ((p = malloc(size)) == NULL)
156 err(EX_OSERR, "malloc() failed");
161 ofw_getprop(int fd, phandle_t node, const char *name, void *buf, int buflen)
166 d.of_namelen = strlen(name);
168 d.of_buflen = buflen;
170 OFW_IOCTL(fd, OFIOCGET, &d);
171 return (d.of_buflen);
175 ofw_setprop(int fd, phandle_t node, const char *name, const void *buf,
181 d.of_namelen = strlen(name);
183 d.of_buflen = buflen;
184 d.of_buf = ofw_malloc(buflen);
185 memcpy(d.of_buf, buf, buflen);
186 OFW_IOCTL(fd, OFIOCSET, &d);
188 return (d.of_buflen);
192 ofw_getproplen(int fd, phandle_t node, const char *name)
197 d.of_namelen = strlen(name);
199 OFW_IOCTL(fd, OFIOCGETPROPLEN, &d);
200 return (d.of_buflen);
204 ofw_getprop_alloc(int fd, phandle_t node, const char *name, void **buf,
205 int *buflen, int reserve)
211 len = ofw_getproplen(fd, node, name);
214 if (*buflen < len + reserve) {
217 *buflen = len + reserve + OFIOCMAXVALUE;
218 *buf = ofw_malloc(*buflen);
221 d.of_namelen = strlen(name);
223 d.of_buflen = *buflen - reserve;
225 rv = ioctl(fd, OFIOCGET, &d);
226 } while (rv == -1 && errno == ENOMEM);
228 err(EX_IOERR, "ioctl(..., OFIOCGET, ...) failed");
229 return (d.of_buflen);