1 // SPDX-License-Identifier: GPL-2.0
3 * fdtdump.c - Contributed by Pantelis Antoniou <pantelis.antoniou AT gmail.com>
13 #include <libfdt_env.h>
17 #define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
18 #define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a))))
19 #define GET_CELL(p) (p += 4, *((const uint32_t *)(p-4)))
21 static void print_data(const char *data
, int len
)
26 /* no data, don't print */
30 if (util_is_printable_string(data
, len
)) {
31 printf(" = \"%s\"", (const char *)data
);
32 } else if ((len
% 4) == 0) {
34 for (i
= 0; i
< len
; i
+= 4)
35 printf("0x%08x%s", fdt32_to_cpu(GET_CELL(p
)),
36 i
< (len
- 4) ? " " : "");
40 for (i
= 0; i
< len
; i
++)
41 printf("%02x%s", *p
++, i
< len
- 1 ? " " : "");
46 static void dump_blob(void *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 /* printf("tag: 0x%08x (%d)\n", tag, p - p_struct); */
103 if (tag
== FDT_BEGIN_NODE
) {
105 p
= PALIGN(p
+ strlen(s
) + 1, 4);
110 printf("%*s%s {\n", depth
* shift
, "", s
);
116 if (tag
== FDT_END_NODE
) {
119 printf("%*s};\n", depth
* shift
, "");
123 if (tag
== FDT_NOP
) {
124 printf("%*s// [NOP]\n", depth
* shift
, "");
128 if (tag
!= FDT_PROP
) {
129 fprintf(stderr
, "%*s ** Unknown tag 0x%08x\n", depth
* shift
, "", tag
);
132 sz
= fdt32_to_cpu(GET_CELL(p
));
133 s
= p_strings
+ fdt32_to_cpu(GET_CELL(p
));
134 if (version
< 16 && sz
>= 8)
138 p
= PALIGN(p
+ sz
, 4);
140 printf("%*s%s", depth
* shift
, "", s
);
147 int main(int argc
, char *argv
[])
152 fprintf(stderr
, "supply input filename\n");
156 buf
= utilfdt_read(argv
[1]);