xref: /netbsd-src/sys/dev/fdt/fdt_ddb.c (revision c901baed96cedf11c49b6661314a7ee295198df5)
1*c901baedSskrll /*	$NetBSD: fdt_ddb.c,v 1.2 2021/03/06 13:21:26 skrll Exp $	*/
2155f2770Sskrll 
3155f2770Sskrll /*-
4155f2770Sskrll  * Copyright (c) 2020 The NetBSD Foundation, Inc.
5155f2770Sskrll  * All rights reserved.
6155f2770Sskrll  *
7155f2770Sskrll  * This code is derived from software contributed to The NetBSD Foundation
8155f2770Sskrll  * by Nick Hudson
9155f2770Sskrll  *
10155f2770Sskrll  * Redistribution and use in source and binary forms, with or without
11155f2770Sskrll  * modification, are permitted provided that the following conditions
12155f2770Sskrll  * are met:
13155f2770Sskrll  * 1. Redistributions of source code must retain the above copyright
14155f2770Sskrll  *    notice, this list of conditions and the following disclaimer.
15155f2770Sskrll  * 2. Redistributions in binary form must reproduce the above copyright
16155f2770Sskrll  *    notice, this list of conditions and the following disclaimer in the
17155f2770Sskrll  *    documentation and/or other materials provided with the distribution.
18155f2770Sskrll  *
19155f2770Sskrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20155f2770Sskrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21155f2770Sskrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22155f2770Sskrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23155f2770Sskrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24155f2770Sskrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25155f2770Sskrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26155f2770Sskrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27155f2770Sskrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28155f2770Sskrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29155f2770Sskrll  * POSSIBILITY OF SUCH DAMAGE.
30155f2770Sskrll  */
31155f2770Sskrll 
32155f2770Sskrll 
33155f2770Sskrll #include <sys/cdefs.h>
34*c901baedSskrll __KERNEL_RCSID(0, "$NetBSD: fdt_ddb.c,v 1.2 2021/03/06 13:21:26 skrll Exp $");
35155f2770Sskrll 
36155f2770Sskrll #include <sys/param.h>
37155f2770Sskrll 
38155f2770Sskrll #include <libfdt.h>
39155f2770Sskrll #include <dev/fdt/fdt_ddb.h>
40155f2770Sskrll #include <dev/fdt/fdtvar.h>
41155f2770Sskrll 
42155f2770Sskrll #define FDT_MAX_DEPTH	16
43155f2770Sskrll 
44155f2770Sskrll static bool
fdt_isprint(const void * data,int len)45155f2770Sskrll fdt_isprint(const void *data, int len)
46155f2770Sskrll {
47155f2770Sskrll 	const uint8_t *c = (const uint8_t *)data;
48155f2770Sskrll 
49155f2770Sskrll 	if (len == 0)
50155f2770Sskrll 		return false;
51155f2770Sskrll 
52*c901baedSskrll 	/* Count consecutive zeroes */
53*c901baedSskrll 	int cz = 0;
54155f2770Sskrll 	for (size_t j = 0; j < len; j++) {
55*c901baedSskrll 		if (c[j] == '\0')
56*c901baedSskrll 			cz++;
57*c901baedSskrll 		else if (isprint(c[j]))
58*c901baedSskrll 			cz = 0;
59*c901baedSskrll 		else
60*c901baedSskrll 			return false;
61*c901baedSskrll 		if (cz > 1)
62155f2770Sskrll 			return false;
63155f2770Sskrll 	}
64155f2770Sskrll 	return true;
65155f2770Sskrll }
66155f2770Sskrll 
67155f2770Sskrll static void
68155f2770Sskrll fdt_print_properties(const void *fdt, int node,
69155f2770Sskrll     void (*pr)(const char *, ...) __printflike(1, 2))
70155f2770Sskrll {
71155f2770Sskrll 	int property;
72155f2770Sskrll 
fdt_for_each_property_offset(property,fdt,node)73155f2770Sskrll 	fdt_for_each_property_offset(property, fdt, node) {
74155f2770Sskrll 		int len;
75155f2770Sskrll 		const struct fdt_property *prop =
76155f2770Sskrll 		    fdt_get_property_by_offset(fdt, property, &len);
77155f2770Sskrll 		const char *name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
78155f2770Sskrll 
79155f2770Sskrll 		pr("    %s", name);
80155f2770Sskrll 		if (len == 0) {
81155f2770Sskrll 			pr("\n");
82155f2770Sskrll 			continue;
83155f2770Sskrll 		}
84155f2770Sskrll 		if (fdt_isprint(prop->data, len)) {
85155f2770Sskrll 			const uint8_t *c = (const uint8_t *)prop->data;
86155f2770Sskrll 
87155f2770Sskrll 			pr(" = \"");
88155f2770Sskrll 			for (size_t j = 0; j < len; j++) {
89155f2770Sskrll 				if (c[j] == '\0') {
90155f2770Sskrll 					if (j + 1 != len)
91155f2770Sskrll 						pr("\", \"");
92155f2770Sskrll 				} else
93155f2770Sskrll 					pr("%c", c[j]);
94155f2770Sskrll 			}
95155f2770Sskrll 			pr("\"\n");
96155f2770Sskrll 			continue;
97155f2770Sskrll 		}
98155f2770Sskrll 		if ((len % 4) == 0) {
99155f2770Sskrll 			const uint32_t *cell = (const uint32_t *)prop->data;
100155f2770Sskrll 			size_t count = len / sizeof(uint32_t);
101155f2770Sskrll 
102155f2770Sskrll 			pr(" = <");
103155f2770Sskrll 			for (size_t j = 0; j < count; j++) {
104155f2770Sskrll 				pr("%#" PRIx32 "%s", fdt32_to_cpu(cell[j]),
105155f2770Sskrll 				    (j != count - 1) ? " " : "");
106155f2770Sskrll 			}
107155f2770Sskrll 			pr(">\n");
108155f2770Sskrll 		} else {
109155f2770Sskrll 			const uint8_t *byte = (const uint8_t *)prop->data;
110155f2770Sskrll 
111155f2770Sskrll 			pr(" = [");
112155f2770Sskrll 			for (size_t j = 0; j < len; j++) {
113155f2770Sskrll 				pr("%02x%s", byte[j],
114155f2770Sskrll 				   (j != len - 1) ? " " : "");
115155f2770Sskrll 			}
116155f2770Sskrll 			pr("]\n");
117155f2770Sskrll 		}
118155f2770Sskrll 	}
119155f2770Sskrll }
120155f2770Sskrll 
121155f2770Sskrll 
122155f2770Sskrll void
123155f2770Sskrll fdt_print(const void *addr, bool full,
124155f2770Sskrll     void (*pr)(const char *, ...) __printflike(1, 2))
125155f2770Sskrll {
126155f2770Sskrll 	const void *fdt = addr;
127155f2770Sskrll 	const char *pname[FDT_MAX_DEPTH] = { NULL };
128155f2770Sskrll 
129155f2770Sskrll 	int error = fdt_check_header(fdt);
130155f2770Sskrll 	if (error) {
131155f2770Sskrll 		pr("Invalid FDT at %p\n", fdt);
132155f2770Sskrll 		return;
133155f2770Sskrll 	}
134155f2770Sskrll 
135155f2770Sskrll 	int depth = 0;
136155f2770Sskrll 	for (int node = fdt_path_offset(fdt, "/");
137155f2770Sskrll 	     node >= 0 && depth >= 0;
138155f2770Sskrll 	     node = fdt_next_node(fdt, node, &depth)) {
139155f2770Sskrll 		const char *name = fdt_get_name(fdt, node, NULL);
140155f2770Sskrll 
141155f2770Sskrll 		if (depth > FDT_MAX_DEPTH) {
142155f2770Sskrll 			pr("max depth exceeded: %d\n", depth);
143155f2770Sskrll 			continue;
144155f2770Sskrll 		}
145155f2770Sskrll 		pname[depth] = name;
146155f2770Sskrll 		/*
147155f2770Sskrll 		 * change conditional for when alternative root nodes
148155f2770Sskrll 		 * can be specified
149155f2770Sskrll 		 */
150155f2770Sskrll 		if (depth == 0)
151155f2770Sskrll 			pr("/");
152155f2770Sskrll 		for (size_t i = 1; i <= depth; i++) {
153155f2770Sskrll 			if (pname[i] == NULL)
154155f2770Sskrll 				break;
155155f2770Sskrll 			pr("/%s", pname[i]);
156155f2770Sskrll 		}
157155f2770Sskrll 		pr("\n");
158155f2770Sskrll 		if (!full)
159155f2770Sskrll 			continue;
160155f2770Sskrll 		fdt_print_properties(fdt, node, pr);
161155f2770Sskrll 	}
162155f2770Sskrll }
163