2 * fdtdump.c - Contributed by Pantelis Antoniou <pantelis.antoniou AT gmail.com>
13 #include <libfdt_env.h>
18 #define FDT_MAGIC_SIZE 4
19 #define MAX_VERSION 17
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)))
25 static const char *tagname(uint32_t tag)
27 static const char * const names[] = {
28 #define TN(t) [t] = #t
36 if (tag < ARRAY_SIZE(names))
42 #define dumpf(fmt, args...) \
43 do { if (debug) printf("// " fmt, ## args); } while (0)
45 static void dump_blob(void *blob, bool debug)
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);
59 const char *p, *s, *t;
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));
77 printf("// boot_cpuid_phys:\t0x%x\n",
78 fdt32_to_cpu(bph->boot_cpuid_phys));
81 printf("// size_dt_strings:\t0x%x\n",
82 fdt32_to_cpu(bph->size_dt_strings));
84 printf("// size_dt_struct:\t0x%x\n",
85 fdt32_to_cpu(bph->size_dt_struct));
89 addr = fdt64_to_cpu(p_rsvmap[i].address);
90 size = fdt64_to_cpu(p_rsvmap[i].size);
91 if (addr == 0 && size == 0)
94 printf("/memreserve/ %#llx %#llx;\n",
95 (unsigned long long)addr, (unsigned long long)size);
99 while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {
101 dumpf("%04zx: tag: 0x%08x (%s)\n",
102 (uintptr_t)p - blob_off - 4, tag, tagname(tag));
104 if (tag == FDT_BEGIN_NODE) {
106 p = PALIGN(p + strlen(s) + 1, 4);
111 printf("%*s%s {\n", depth * shift, "", s);
117 if (tag == FDT_END_NODE) {
120 printf("%*s};\n", depth * shift, "");
124 if (tag == FDT_NOP) {
125 printf("%*s// [NOP]\n", depth * shift, "");
129 if (tag != FDT_PROP) {
130 fprintf(stderr, "%*s ** Unknown tag 0x%08x\n", depth * shift, "", tag);
133 sz = fdt32_to_cpu(GET_CELL(p));
134 s = p_strings + fdt32_to_cpu(GET_CELL(p));
135 if (version < 16 && sz >= 8)
139 p = PALIGN(p + sz, 4);
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);
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
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
163 static bool valid_header(char *p, off_t len)
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)
177 int main(int argc, char *argv[])
186 while ((opt = util_getopt_long()) != EOF) {
188 case_USAGE_COMMON_FLAGS
198 if (optind != argc - 1)
199 usage("missing input filename");
202 buf = utilfdt_read_len(file, &len);
204 die("could not read: %s\n", file);
206 /* try and locate an embedded fdt in a bigger blob */
208 unsigned char smagic[FDT_MAGIC_SIZE];
210 char *endp = buf + len;
212 fdt_set_magic(smagic, FDT_MAGIC);
214 /* poor man's memmem */
215 while ((endp - p) >= FDT_MAGIC_SIZE) {
216 p = memchr(p, smagic[0], endp - p - FDT_MAGIC_SIZE);
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))
225 printf("%s: skipping fdt magic at offset %#zx\n",
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);
234 } else if (!valid_header(buf, len))
235 die("%s: header is not valid\n", file);
237 dump_blob(buf, debug);