fdtdump.c revision 261215
1/*
2 * fdtdump.c - Contributed by Pantelis Antoniou <pantelis.antoniou AT gmail.com>
3 */
4
5#include <stdbool.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <ctype.h>
11
12#include <libfdt.h>
13#include <libfdt_env.h>
14#include <fdt.h>
15
16#include "util.h"
17
18#define ALIGN(x, a)	(((x) + ((a) - 1)) & ~((a) - 1))
19#define PALIGN(p, a)	((void *)(ALIGN((unsigned long)(p), (a))))
20#define GET_CELL(p)	(p += 4, *((const uint32_t *)(p-4)))
21
22static const char *tagname(uint32_t tag)
23{
24	static const char * const names[] = {
25#define TN(t) [t] #t
26		TN(FDT_BEGIN_NODE),
27		TN(FDT_END_NODE),
28		TN(FDT_PROP),
29		TN(FDT_NOP),
30		TN(FDT_END),
31#undef TN
32	};
33	if (tag < ARRAY_SIZE(names))
34		if (names[tag])
35			return names[tag];
36	return "FDT_???";
37}
38
39#define dumpf(fmt, args...) \
40	do { if (debug) printf("// " fmt, ## args); } while (0)
41
42static void dump_blob(void *blob, bool debug)
43{
44	uintptr_t blob_off = (uintptr_t)blob;
45	struct fdt_header *bph = blob;
46	uint32_t off_mem_rsvmap = fdt32_to_cpu(bph->off_mem_rsvmap);
47	uint32_t off_dt = fdt32_to_cpu(bph->off_dt_struct);
48	uint32_t off_str = fdt32_to_cpu(bph->off_dt_strings);
49	struct fdt_reserve_entry *p_rsvmap =
50		(struct fdt_reserve_entry *)((char *)blob + off_mem_rsvmap);
51	const char *p_struct = (const char *)blob + off_dt;
52	const char *p_strings = (const char *)blob + off_str;
53	uint32_t version = fdt32_to_cpu(bph->version);
54	uint32_t totalsize = fdt32_to_cpu(bph->totalsize);
55	uint32_t tag;
56	const char *p, *s, *t;
57	int depth, sz, shift;
58	int i;
59	uint64_t addr, size;
60
61	depth = 0;
62	shift = 4;
63
64	printf("/dts-v1/;\n");
65	printf("// magic:\t\t0x%x\n", fdt32_to_cpu(bph->magic));
66	printf("// totalsize:\t\t0x%x (%d)\n", totalsize, totalsize);
67	printf("// off_dt_struct:\t0x%x\n", off_dt);
68	printf("// off_dt_strings:\t0x%x\n", off_str);
69	printf("// off_mem_rsvmap:\t0x%x\n", off_mem_rsvmap);
70	printf("// version:\t\t%d\n", version);
71	printf("// last_comp_version:\t%d\n",
72	       fdt32_to_cpu(bph->last_comp_version));
73	if (version >= 2)
74		printf("// boot_cpuid_phys:\t0x%x\n",
75		       fdt32_to_cpu(bph->boot_cpuid_phys));
76
77	if (version >= 3)
78		printf("// size_dt_strings:\t0x%x\n",
79		       fdt32_to_cpu(bph->size_dt_strings));
80	if (version >= 17)
81		printf("// size_dt_struct:\t0x%x\n",
82		       fdt32_to_cpu(bph->size_dt_struct));
83	printf("\n");
84
85	for (i = 0; ; i++) {
86		addr = fdt64_to_cpu(p_rsvmap[i].address);
87		size = fdt64_to_cpu(p_rsvmap[i].size);
88		if (addr == 0 && size == 0)
89			break;
90
91		printf("/memreserve/ %llx %llx;\n",
92		       (unsigned long long)addr, (unsigned long long)size);
93	}
94
95	p = p_struct;
96	while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {
97
98		dumpf("%04zx: tag: 0x%08x (%s)\n",
99		        (uintptr_t)p - blob_off - 4, tag, tagname(tag));
100
101		if (tag == FDT_BEGIN_NODE) {
102			s = p;
103			p = PALIGN(p + strlen(s) + 1, 4);
104
105			if (*s == '\0')
106				s = "/";
107
108			printf("%*s%s {\n", depth * shift, "", s);
109
110			depth++;
111			continue;
112		}
113
114		if (tag == FDT_END_NODE) {
115			depth--;
116
117			printf("%*s};\n", depth * shift, "");
118			continue;
119		}
120
121		if (tag == FDT_NOP) {
122			printf("%*s// [NOP]\n", depth * shift, "");
123			continue;
124		}
125
126		if (tag != FDT_PROP) {
127			fprintf(stderr, "%*s ** Unknown tag 0x%08x\n", depth * shift, "", tag);
128			break;
129		}
130		sz = fdt32_to_cpu(GET_CELL(p));
131		s = p_strings + fdt32_to_cpu(GET_CELL(p));
132		if (version < 16 && sz >= 8)
133			p = PALIGN(p, 8);
134		t = p;
135
136		p = PALIGN(p + sz, 4);
137
138		dumpf("%04zx: string: %s\n", (uintptr_t)s - blob_off, s);
139		dumpf("%04zx: value\n", (uintptr_t)t - blob_off);
140		printf("%*s%s", depth * shift, "", s);
141		utilfdt_print_data(t, sz);
142		printf(";\n");
143	}
144}
145
146/* Usage related data. */
147static const char usage_synopsis[] = "fdtdump [options] <file>";
148static const char usage_short_opts[] = "ds" USAGE_COMMON_SHORT_OPTS;
149static struct option const usage_long_opts[] = {
150	{"debug",            no_argument, NULL, 'd'},
151	{"scan",             no_argument, NULL, 's'},
152	USAGE_COMMON_LONG_OPTS
153};
154static const char * const usage_opts_help[] = {
155	"Dump debug information while decoding the file",
156	"Scan for an embedded fdt in file",
157	USAGE_COMMON_OPTS_HELP
158};
159
160int main(int argc, char *argv[])
161{
162	int opt;
163	const char *file;
164	char *buf;
165	bool debug = false;
166	bool scan = false;
167	off_t len;
168
169	while ((opt = util_getopt_long()) != EOF) {
170		switch (opt) {
171		case_USAGE_COMMON_FLAGS
172
173		case 'd':
174			debug = true;
175			break;
176		case 's':
177			scan = true;
178			break;
179		}
180	}
181	if (optind != argc - 1)
182		usage("missing input filename");
183	file = argv[optind];
184
185	buf = utilfdt_read_len(file, &len);
186	if (!buf)
187		die("could not read: %s\n", file);
188
189	/* try and locate an embedded fdt in a bigger blob */
190	if (scan) {
191		unsigned char smagic[4];
192		char *p = buf;
193		char *endp = buf + len;
194
195		fdt_set_magic(smagic, FDT_MAGIC);
196
197		/* poor man's memmem */
198		while (true) {
199			p = memchr(p, smagic[0], endp - p - 4);
200			if (!p)
201				break;
202			if (fdt_magic(p) == FDT_MAGIC) {
203				/* try and validate the main struct */
204				off_t this_len = endp - p;
205				fdt32_t max_version = 17;
206				if (fdt_version(p) <= max_version &&
207				    fdt_last_comp_version(p) < max_version &&
208				    fdt_totalsize(p) < this_len &&
209				    fdt_off_dt_struct(p) < this_len &&
210					fdt_off_dt_strings(p) < this_len)
211					break;
212				if (debug)
213					printf("%s: skipping fdt magic at offset %#zx\n",
214						file, p - buf);
215			}
216			++p;
217		}
218		if (!p)
219			die("%s: could not locate fdt magic\n", file);
220		printf("%s: found fdt at offset %#zx\n", file, p - buf);
221		buf = p;
222	}
223
224	dump_blob(buf, debug);
225
226	return 0;
227}
228