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