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