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