xref: /freebsd/usr.sbin/ofwdump/ofwdump.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*-
2  * Copyright (c) 2002 by Thomas Moestl <tmm@FreeBSD.org>.
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 ``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.
24  */
25 
26 #include <sys/cdefs.h>
27 #include <dev/ofw/openfirm.h>
28 #include <dev/ofw/openfirmio.h>
29 
30 #include <err.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sysexits.h>
36 #include <unistd.h>
37 #include <vis.h>
38 
39 #include "ofw_util.h"
40 
41 /* Constants controlling the layout of the output. */
42 #define	LVLINDENT	2
43 #define	NAMEINDENT	2
44 #define	DUMPINDENT	4
45 #define	CHARSPERLINE	60
46 #define	BYTESPERLINE	(CHARSPERLINE / 3)
47 
48 static void	usage(void);
49 static void	ofw_indent(int);
50 static void	ofw_dump_properties(int, phandle_t, int, int, int);
51 static void	ofw_dump_property(int fd, phandle_t n, int level,
52 		    const char *prop, int raw, int str);
53 static void	ofw_dump(int, const char *, int, int, const char *, int, int);
54 
55 static void
56 usage(void)
57 {
58 
59 	fprintf(stderr,
60 	    "usage: ofwdump -a [-p | -P property] [-R | -S]\n"
61 	    "       ofwdump [-p | -P property] [-r] [-R | -S] [--] nodes\n");
62 	exit(EX_USAGE);
63 }
64 
65 int
66 main(int argc, char *argv[])
67 {
68 	int opt, i, fd;
69 	int aflag, pflag, rflag, Rflag, Sflag;
70 	char *Parg;
71 
72 	aflag = pflag = rflag = Rflag = Sflag = 0;
73 	Parg = NULL;
74 	while ((opt = getopt(argc, argv, "-aprP:RS")) != -1) {
75 		if (opt == '-')
76 			break;
77 		switch (opt) {
78 		case 'a':
79 			aflag = 1;
80 			rflag = 1;
81 			break;
82 		case 'p':
83 			if (Parg != NULL)
84 				usage();
85 			pflag = 1;
86 			break;
87 		case 'r':
88 			rflag = 1;
89 			break;
90 		case 'P':
91 			if (pflag)
92 				usage();
93 			pflag = 1;
94 			Parg = optarg;
95 			break;
96 		case 'R':
97 			if (Sflag)
98 				usage();
99 			Rflag = 1;
100 			break;
101 		case 'S':
102 			if (Rflag)
103 				usage();
104 			Sflag = 1;
105 			break;
106 		case '?':
107 		default:
108 			usage();
109 			/* NOTREACHED */
110 		}
111 	}
112 	argc -= optind;
113 	argv += optind;
114 
115 	fd = ofw_open(O_RDONLY);
116 	if (aflag) {
117 		if (argc != 0)
118 			usage();
119 		ofw_dump(fd, NULL, rflag, pflag, Parg, Rflag, Sflag);
120 	} else {
121 		/*
122 		 * For the sake of scripts, usage() is not called here if
123 		 * argc == 0.
124 		 */
125 		for (i = 0; i < argc; i++)
126 			ofw_dump(fd, argv[i], rflag, pflag, Parg, Rflag, Sflag);
127 	}
128 	ofw_close(fd);
129 	return (EX_OK);
130 }
131 
132 static void
133 ofw_indent(int level)
134 {
135 	int i;
136 
137 	for (i = 0; i < level; i++)
138 		putchar(' ');
139 }
140 
141 static void
142 ofw_dump_properties(int fd, phandle_t n, int level, int raw, int str)
143 {
144 	int nlen;
145 	char prop[OFIOCSUGGPROPNAMELEN];
146 
147 	for (nlen = ofw_firstprop(fd, n, prop, sizeof(prop)); nlen != 0;
148 	     nlen = ofw_nextprop(fd, n, prop, prop, sizeof(prop)))
149 		ofw_dump_property(fd, n, level, prop, raw, str);
150 }
151 
152 static void
153 ofw_dump_property(int fd, phandle_t n, int level, const char *prop, int raw,
154     int str)
155 {
156 	static void *pbuf = NULL;
157 	static char *visbuf = NULL;
158 	static char printbuf[CHARSPERLINE + 1];
159 	static int pblen = 0, vblen = 0;
160 	int len, i, j, max, vlen;
161 
162 	len = ofw_getprop_alloc(fd, n, prop, &pbuf, &pblen, 1);
163 	if (len < 0)
164 		return;
165 	if (raw)
166 		write(STDOUT_FILENO, pbuf, len);
167 	else if (str)
168 		printf("%.*s\n", len, (char *)pbuf);
169 	else {
170 		ofw_indent(level * LVLINDENT + NAMEINDENT);
171 		printf("%s:\n", prop);
172 		/* Print in hex. */
173 		for (i = 0; i < len; i += BYTESPERLINE) {
174 			max = len - i;
175 			max = max > BYTESPERLINE ? BYTESPERLINE : max;
176 			ofw_indent(level * LVLINDENT + DUMPINDENT);
177 			for (j = 0; j < max; j++)
178 				printf("%02x ",
179 				    ((unsigned char *)pbuf)[i + j]);
180 			printf("\n");
181 		}
182 		/*
183 		 * strvis() and print if it looks like it is
184 		 * zero-terminated.
185 		 */
186 		if (((char *)pbuf)[len - 1] == '\0' &&
187 		    strlen(pbuf) == (unsigned)len - 1) {
188 			if (vblen < (len - 1) * 4 + 1) {
189 				if (visbuf != NULL)
190 					free(visbuf);
191 				vblen = (OFIOCMAXVALUE + len) * 4 + 1;
192 					if ((visbuf = malloc(vblen)) == NULL)
193 						err(EX_OSERR,
194 						    "malloc() failed");
195 			}
196 			vlen = strvis(visbuf, pbuf, VIS_TAB | VIS_NL);
197 			for (i = 0; i < vlen; i += CHARSPERLINE) {
198 				ofw_indent(level * LVLINDENT +
199 				    DUMPINDENT);
200 				strlcpy(printbuf, &visbuf[i],
201 				    sizeof(printbuf));
202 				printf("'%s'\n", printbuf);
203 			}
204 		}
205 	}
206 }
207 
208 static void
209 ofw_dump_node(int fd, phandle_t n, int level, int rec, int prop,
210     const char *pmatch, int raw, int str)
211 {
212 	static void *nbuf = NULL;
213 	static int nblen = 0;
214 	int plen;
215 	phandle_t c;
216 
217 	if (!(raw || str)) {
218 		ofw_indent(level * LVLINDENT);
219 		printf("Node %#lx", (unsigned long)n);
220 		plen = ofw_getprop_alloc(fd, n, "name", &nbuf, &nblen, 1);
221 		if (plen > 0)
222 			printf(": %.*s\n", (int)plen, (char *)nbuf);
223 		else
224 			putchar('\n');
225 	}
226 	if (prop) {
227 		if (pmatch)
228 			ofw_dump_property(fd, n, level, pmatch, raw, str);
229 		else
230 			ofw_dump_properties(fd, n, level, raw, str);
231 	}
232 	if (rec) {
233 		for (c = ofw_child(fd, n); c != 0; c = ofw_peer(fd, c)) {
234 			ofw_dump_node(fd, c, level + 1, rec, prop, pmatch,
235 			    raw, str);
236 		}
237 	}
238 }
239 
240 static void
241 ofw_dump(int fd, const char *start, int rec, int prop, const char *pmatch,
242     int raw, int str)
243 {
244 	phandle_t n;
245 
246 	n = start == NULL ? ofw_root(fd) : ofw_finddevice(fd, start);
247 	ofw_dump_node(fd, n, 0, rec, prop, pmatch, raw, str);
248 }
249