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 <dev/ofw/openfirm.h>
30 #include <dev/ofw/openfirmio.h>
43 /* Constants controlling the layout of the output. */
47 #define CHARSPERLINE 60
48 #define BYTESPERLINE (CHARSPERLINE / 3)
50 static void usage(void);
51 static void ofw_indent(int);
52 static void ofw_dump_properties(int, phandle_t, int, int, int);
53 static void ofw_dump_property(int fd, phandle_t n, int level,
54 const char *prop, int raw, int str);
55 static void ofw_dump(int, const char *, int, int, const char *, int, int);
62 "usage: ofwdump -a [-p | -P property] [-R | -S]\n"
63 " ofwdump [-p | -P property] [-r] [-R | -S] [--] nodes\n");
68 main(int argc, char *argv[])
71 int aflag, pflag, rflag, Rflag, Sflag;
74 aflag = pflag = rflag = Rflag = Sflag = 0;
76 while ((opt = getopt(argc, argv, "-aprP:RS")) != -1) {
117 fd = ofw_open(O_RDONLY);
121 ofw_dump(fd, NULL, rflag, pflag, Parg, Rflag, Sflag);
124 * For the sake of scripts, usage() is not called here if
127 for (i = 0; i < argc; i++)
128 ofw_dump(fd, argv[i], rflag, pflag, Parg, Rflag, Sflag);
135 ofw_indent(int level)
139 for (i = 0; i < level; i++)
144 ofw_dump_properties(int fd, phandle_t n, int level, int raw, int str)
149 for (nlen = ofw_firstprop(fd, n, prop, sizeof(prop)); nlen != 0;
150 nlen = ofw_nextprop(fd, n, prop, prop, sizeof(prop)))
151 ofw_dump_property(fd, n, level, prop, raw, str);
155 ofw_dump_property(int fd, phandle_t n, int level, const char *prop, int raw,
158 static void *pbuf = NULL;
159 static char *visbuf = NULL;
160 static char printbuf[CHARSPERLINE + 1];
161 static int pblen = 0, vblen = 0;
162 int len, i, j, max, vlen;
164 len = ofw_getprop_alloc(fd, n, prop, &pbuf, &pblen, 1);
168 write(STDOUT_FILENO, pbuf, len);
170 printf("%.*s\n", len, (char *)pbuf);
172 ofw_indent(level * LVLINDENT + NAMEINDENT);
173 printf("%s:\n", prop);
175 for (i = 0; i < len; i += BYTESPERLINE) {
177 max = max > BYTESPERLINE ? BYTESPERLINE : max;
178 ofw_indent(level * LVLINDENT + DUMPINDENT);
179 for (j = 0; j < max; j++)
181 ((unsigned char *)pbuf)[i + j]);
185 * strvis() and print if it looks like it is
188 if (((char *)pbuf)[len - 1] == '\0' &&
189 strlen(pbuf) == (unsigned)len - 1) {
190 if (vblen < (len - 1) * 4 + 1) {
193 vblen = (OFIOCMAXVALUE + len) * 4 + 1;
194 if ((visbuf = malloc(vblen)) == NULL)
198 vlen = strvis(visbuf, pbuf, VIS_TAB | VIS_NL);
199 for (i = 0; i < vlen; i += CHARSPERLINE) {
200 ofw_indent(level * LVLINDENT +
202 strlcpy(printbuf, &visbuf[i],
204 printf("'%s'\n", printbuf);
211 ofw_dump_node(int fd, phandle_t n, int level, int rec, int prop,
212 const char *pmatch, int raw, int str)
214 static void *nbuf = NULL;
215 static int nblen = 0;
220 ofw_indent(level * LVLINDENT);
221 printf("Node %#lx", (unsigned long)n);
222 plen = ofw_getprop_alloc(fd, n, "name", &nbuf, &nblen, 1);
224 printf(": %.*s\n", (int)plen, (char *)nbuf);
230 ofw_dump_property(fd, n, level, pmatch, raw, str);
232 ofw_dump_properties(fd, n, level, raw, str);
235 for (c = ofw_child(fd, n); c != 0; c = ofw_peer(fd, c)) {
236 ofw_dump_node(fd, c, level + 1, rec, prop, pmatch,
243 ofw_dump(int fd, const char *start, int rec, int prop, const char *pmatch,
248 n = start == NULL ? ofw_root(fd) : ofw_finddevice(fd, start);
249 ofw_dump_node(fd, n, 0, rec, prop, pmatch, raw, str);