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, const char *, int,
54 static void ofw_dump(int, const char *, int, int, const char *, int, int);
61 "usage: ofwdump -a [-p | -P property] [-R | -S]\n"
62 " ofwdump [-p | -P property] [-r] [-R | -S] [--] nodes\n");
67 main(int argc, char *argv[])
70 int aflag, pflag, rflag, Rflag, Sflag;
73 aflag = pflag = rflag = Rflag = Sflag = 0;
75 while ((opt = getopt(argc, argv, "-aprP:RS")) != -1) {
116 fd = ofw_open(O_RDONLY);
120 ofw_dump(fd, NULL, rflag, pflag, Parg, Rflag, Sflag);
123 * For the sake of scripts, usage() is not called here if
126 for (i = 0; i < argc; i++)
127 ofw_dump(fd, argv[i], rflag, pflag, Parg, Rflag, Sflag);
134 ofw_indent(int level)
138 for (i = 0; i < level; i++)
143 ofw_dump_properties(int fd, phandle_t n, int level, const char *pmatch, int raw,
146 static void *pbuf = NULL;
147 static char *visbuf = NULL;
148 static char printbuf[CHARSPERLINE + 1];
149 static int pblen = 0, vblen = 0;
151 int nlen, len, i, j, max, vlen;
153 for (nlen = ofw_firstprop(fd, n, prop, sizeof(prop)); nlen != 0;
154 nlen = ofw_nextprop(fd, n, prop, prop, sizeof(prop))) {
155 if (pmatch != NULL && strcmp(pmatch, prop) != 0)
157 len = ofw_getprop_alloc(fd, n, prop, &pbuf, &pblen, 1);
161 write(STDOUT_FILENO, pbuf, len);
163 printf("%.*s\n", len, (char *)pbuf);
165 ofw_indent(level * LVLINDENT + NAMEINDENT);
166 printf("%s:\n", prop);
168 for (i = 0; i < len; i += BYTESPERLINE) {
170 max = max > BYTESPERLINE ? BYTESPERLINE : max;
171 ofw_indent(level * LVLINDENT + DUMPINDENT);
172 for (j = 0; j < max; j++)
174 ((unsigned char *)pbuf)[i + j]);
178 * strvis() and print if it looks like it is
181 if (((char *)pbuf)[len - 1] == '\0' &&
182 strlen(pbuf) == (unsigned)len - 1) {
183 if (vblen < (len - 1) * 4 + 1) {
186 vblen = (OFIOCMAXVALUE + len) * 4 + 1;
187 if ((visbuf = malloc(vblen)) == NULL)
191 vlen = strvis(visbuf, pbuf, VIS_TAB | VIS_NL);
192 for (i = 0; i < vlen; i += CHARSPERLINE) {
193 ofw_indent(level * LVLINDENT +
195 strlcpy(printbuf, &visbuf[i],
197 printf("'%s'\n", printbuf);
205 ofw_dump_node(int fd, phandle_t n, int level, int rec, int prop,
206 const char *pmatch, int raw, int str)
208 static void *nbuf = NULL;
209 static int nblen = 0;
214 ofw_indent(level * LVLINDENT);
215 printf("Node %#lx", (unsigned long)n);
216 plen = ofw_getprop_alloc(fd, n, "name", &nbuf, &nblen, 1);
218 printf(": %.*s\n", (int)plen, (char *)nbuf);
223 ofw_dump_properties(fd, n, level, pmatch, raw, str);
225 for (c = ofw_child(fd, n); c != 0; c = ofw_peer(fd, c)) {
226 ofw_dump_node(fd, c, level + 1, rec, prop, pmatch,
233 ofw_dump(int fd, const char *start, int rec, int prop, const char *pmatch,
238 n = start == NULL ? ofw_root(fd) : ofw_finddevice(fd, start);
239 ofw_dump_node(fd, n, 0, rec, prop, pmatch, raw, str);