2 * ftdump.c - Contributed by Pantelis Antoniou <pantelis.antoniou AT gmail.com>
12 #include <libfdt_env.h>
14 #define FTDUMP_BUF_SIZE 65536
16 #define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
17 #define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a))))
18 #define GET_CELL(p) (p += 4, *((const uint32_t *)(p-4)))
20 static int is_printable_string(const void *data, int len)
25 /* zero length is not */
29 /* must terminate with zero */
30 if (s[len - 1] != '\0')
34 while (*s && isprint(*s))
37 /* not zero, or not done yet */
38 if (*s != '\0' || (s + 1 - ss) < len)
44 static void print_data(const char *data, int len)
49 /* no data, don't print */
53 if (is_printable_string(data, len)) {
54 printf(" = \"%s\"", (const char *)data);
55 } else if ((len % 4) == 0) {
57 for (i = 0; i < len; i += 4)
58 printf("0x%08x%s", fdt32_to_cpu(GET_CELL(p)),
59 i < (len - 4) ? " " : "");
63 for (i = 0; i < len; i++)
64 printf("%02x%s", *p++, i < len - 1 ? " " : "");
69 static void dump_blob(void *blob)
71 struct fdt_header *bph = blob;
72 uint32_t off_mem_rsvmap = fdt32_to_cpu(bph->off_mem_rsvmap);
73 uint32_t off_dt = fdt32_to_cpu(bph->off_dt_struct);
74 uint32_t off_str = fdt32_to_cpu(bph->off_dt_strings);
75 struct fdt_reserve_entry *p_rsvmap =
76 (struct fdt_reserve_entry *)((char *)blob + off_mem_rsvmap);
77 const char *p_struct = (const char *)blob + off_dt;
78 const char *p_strings = (const char *)blob + off_str;
79 uint32_t version = fdt32_to_cpu(bph->version);
80 uint32_t totalsize = fdt32_to_cpu(bph->totalsize);
82 const char *p, *s, *t;
90 printf("/dts-v1/;\n");
91 printf("// magic:\t\t0x%x\n", fdt32_to_cpu(bph->magic));
92 printf("// totalsize:\t\t0x%x (%d)\n", totalsize, totalsize);
93 printf("// off_dt_struct:\t0x%x\n", off_dt);
94 printf("// off_dt_strings:\t0x%x\n", off_str);
95 printf("// off_mem_rsvmap:\t0x%x\n", off_mem_rsvmap);
96 printf("// version:\t\t%d\n", version);
97 printf("// last_comp_version:\t%d\n",
98 fdt32_to_cpu(bph->last_comp_version));
100 printf("// boot_cpuid_phys:\t0x%x\n",
101 fdt32_to_cpu(bph->boot_cpuid_phys));
104 printf("// size_dt_strings:\t0x%x\n",
105 fdt32_to_cpu(bph->size_dt_strings));
107 printf("// size_dt_struct:\t0x%x\n",
108 fdt32_to_cpu(bph->size_dt_struct));
112 addr = fdt64_to_cpu(p_rsvmap[i].address);
113 size = fdt64_to_cpu(p_rsvmap[i].size);
114 if (addr == 0 && size == 0)
117 printf("/memreserve/ %llx %llx;\n",
118 (unsigned long long)addr, (unsigned long long)size);
122 while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {
124 /* printf("tag: 0x%08x (%d)\n", tag, p - p_struct); */
126 if (tag == FDT_BEGIN_NODE) {
128 p = PALIGN(p + strlen(s) + 1, 4);
133 printf("%*s%s {\n", depth * shift, "", s);
139 if (tag == FDT_END_NODE) {
142 printf("%*s};\n", depth * shift, "");
146 if (tag == FDT_NOP) {
147 printf("%*s// [NOP]\n", depth * shift, "");
151 if (tag != FDT_PROP) {
152 fprintf(stderr, "%*s ** Unknown tag 0x%08x\n", depth * shift, "", tag);
155 sz = fdt32_to_cpu(GET_CELL(p));
156 s = p_strings + fdt32_to_cpu(GET_CELL(p));
157 if (version < 16 && sz >= 8)
161 p = PALIGN(p + sz, 4);
163 printf("%*s%s", depth * shift, "", s);
170 int main(int argc, char *argv[])
177 fprintf(stderr, "supply input filename\n");
181 if (strcmp(argv[1], "-") == 0) {
184 fp = fopen(argv[1], "rb");
186 fprintf(stderr, "unable to open %s\n", argv[1]);
191 buf = malloc(FTDUMP_BUF_SIZE);
193 fprintf(stderr, "Couldn't allocate %d byte buffer\n", FTDUMP_BUF_SIZE);
197 size = fread(buf, 1, FTDUMP_BUF_SIZE, fp);
198 if (size == FTDUMP_BUF_SIZE) {
199 fprintf(stderr, "file too large (maximum is %d bytes)\n", FTDUMP_BUF_SIZE);