2
* ftdump.c - Contributed by Pantelis Antoniou <pantelis.antoniou AT gmail.com>
11
#include <libfdt_env.h>
13
#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
14
#define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a))))
15
#define GET_CELL(p) (p += 4, *((uint32_t *)(p-4)))
17
static int is_printable_string(const void *data, int len)
22
/* zero length is not */
26
/* must terminate with zero */
27
if (s[len - 1] != '\0')
31
while (*s && isprint(*s))
34
/* not zero, or not done yet */
35
if (*s != '\0' || (s + 1 - ss) < len)
41
static void print_data(const void *data, int len)
46
/* no data, don't print */
50
if (is_printable_string(data, len)) {
51
printf(" = \"%s\"", (const char *)data);
52
} else if ((len % 4) == 0) {
54
for (i = 0; i < len; i += 4)
55
printf("%08x%s", *((const uint32_t *)data + i),
56
i < (len - 4) ? " " : "");
60
for (i = 0, s = data; i < len; i++)
61
printf("%02x%s", s[i], i < len - 1 ? " " : "");
66
static void dump_blob(void *blob)
68
struct fdt_header *bph = blob;
69
uint32_t off_mem_rsvmap = fdt32_to_cpu(bph->off_mem_rsvmap);
70
uint32_t off_dt = fdt32_to_cpu(bph->off_dt_struct);
71
uint32_t off_str = fdt32_to_cpu(bph->off_dt_strings);
72
struct fdt_reserve_entry *p_rsvmap =
73
(struct fdt_reserve_entry *)((char *)blob + off_mem_rsvmap);
74
char *p_struct = (char *)blob + off_dt;
75
char *p_strings = (char *)blob + off_str;
76
uint32_t version = fdt32_to_cpu(bph->version);
77
uint32_t totalsize = fdt32_to_cpu(bph->totalsize);
88
printf("// magic:\t\t0x%x\n", fdt32_to_cpu(bph->magic));
89
printf("// totalsize:\t\t0x%x (%d)\n", totalsize, totalsize);
90
printf("// off_dt_struct:\t0x%x\n", off_dt);
91
printf("// off_dt_strings:\t0x%x\n", off_str);
92
printf("// off_mem_rsvmap:\t0x%x\n", off_mem_rsvmap);
93
printf("// version:\t\t%d\n", version);
94
printf("// last_comp_version:\t%d\n",
95
fdt32_to_cpu(bph->last_comp_version));
97
printf("// boot_cpuid_phys:\t0x%x\n",
98
fdt32_to_cpu(bph->boot_cpuid_phys));
101
printf("// size_dt_strings:\t0x%x\n",
102
fdt32_to_cpu(bph->size_dt_strings));
104
printf("// size_dt_struct:\t0x%x\n",
105
fdt32_to_cpu(bph->size_dt_struct));
109
addr = fdt64_to_cpu(p_rsvmap[i].address);
110
size = fdt64_to_cpu(p_rsvmap[i].size);
111
if (addr == 0 && size == 0)
114
printf("/memreserve/ %llx %llx;\n",
115
(unsigned long long)addr, (unsigned long long)size);
119
while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {
121
/* printf("tag: 0x%08x (%d)\n", tag, p - p_struct); */
123
if (tag == FDT_BEGIN_NODE) {
125
p = PALIGN(p + strlen(s) + 1, 4);
130
printf("%*s%s {\n", depth * shift, "", s);
136
if (tag == FDT_END_NODE) {
139
printf("%*s};\n", depth * shift, "");
143
if (tag == FDT_NOP) {
144
printf("%*s// [NOP]\n", depth * shift, "");
148
if (tag != FDT_PROP) {
149
fprintf(stderr, "%*s ** Unknown tag 0x%08x\n", depth * shift, "", tag);
152
sz = fdt32_to_cpu(GET_CELL(p));
153
s = p_strings + fdt32_to_cpu(GET_CELL(p));
154
if (version < 16 && sz >= 8)
158
p = PALIGN(p + sz, 4);
160
printf("%*s%s", depth * shift, "", s);
167
int main(int argc, char *argv[])
170
char buf[16384]; /* 16k max */
174
fprintf(stderr, "supply input filename\n");
178
fp = fopen(argv[1], "rb");
180
fprintf(stderr, "unable to open %s\n", argv[1]);
184
size = fread(buf, 1, sizeof(buf), fp);
185
if (size == sizeof(buf)) { /* too large */
186
fprintf(stderr, "file too large\n");