2 * fdtdump.c - Contributed by Pantelis Antoniou <pantelis.antoniou AT gmail.com>
13 #include <libfdt_env.h>
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)))
22 static const char *tagname(uint32_t tag)
24 static const char * const names[] = {
33 if (tag < ARRAY_SIZE(names))
39 #define dumpf(fmt, args...) \
40 do { if (debug) printf("// " fmt, ## args); } while (0)
42 static void dump_blob(void *blob, bool debug)
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);
56 const char *p, *s, *t;
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));
74 printf("// boot_cpuid_phys:\t0x%x\n",
75 fdt32_to_cpu(bph->boot_cpuid_phys));
78 printf("// size_dt_strings:\t0x%x\n",
79 fdt32_to_cpu(bph->size_dt_strings));
81 printf("// size_dt_struct:\t0x%x\n",
82 fdt32_to_cpu(bph->size_dt_struct));
86 addr = fdt64_to_cpu(p_rsvmap[i].address);
87 size = fdt64_to_cpu(p_rsvmap[i].size);
88 if (addr == 0 && size == 0)
91 printf("/memreserve/ %llx %llx;\n",
92 (unsigned long long)addr, (unsigned long long)size);
96 while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {
98 dumpf("%04zx: tag: 0x%08x (%s)\n",
99 (uintptr_t)p - blob_off - 4, tag, tagname(tag));
101 if (tag == FDT_BEGIN_NODE) {
103 p = PALIGN(p + strlen(s) + 1, 4);
108 printf("%*s%s {\n", depth * shift, "", s);
114 if (tag == FDT_END_NODE) {
117 printf("%*s};\n", depth * shift, "");
121 if (tag == FDT_NOP) {
122 printf("%*s// [NOP]\n", depth * shift, "");
126 if (tag != FDT_PROP) {
127 fprintf(stderr, "%*s ** Unknown tag 0x%08x\n", depth * shift, "", tag);
130 sz = fdt32_to_cpu(GET_CELL(p));
131 s = p_strings + fdt32_to_cpu(GET_CELL(p));
132 if (version < 16 && sz >= 8)
136 p = PALIGN(p + sz, 4);
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);
146 /* Usage related data. */
147 static const char usage_synopsis[] = "fdtdump [options] <file>";
148 static const char usage_short_opts[] = "ds" USAGE_COMMON_SHORT_OPTS;
149 static struct option const usage_long_opts[] = {
150 {"debug", no_argument, NULL, 'd'},
151 {"scan", no_argument, NULL, 's'},
152 USAGE_COMMON_LONG_OPTS
154 static 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
160 int main(int argc, char *argv[])
169 while ((opt = util_getopt_long()) != EOF) {
171 case_USAGE_COMMON_FLAGS
181 if (optind != argc - 1)
182 usage("missing input filename");
185 buf = utilfdt_read_len(file, &len);
187 die("could not read: %s\n", file);
189 /* try and locate an embedded fdt in a bigger blob */
191 unsigned char smagic[4];
193 char *endp = buf + len;
195 fdt_set_magic(smagic, FDT_MAGIC);
197 /* poor man's memmem */
199 p = memchr(p, smagic[0], endp - p - 4);
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)
213 printf("%s: skipping fdt magic at offset %#zx\n",
219 die("%s: could not locate fdt magic\n", file);
220 printf("%s: found fdt at offset %#zx\n", file, p - buf);
224 dump_blob(buf, debug);