David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 1 | /* |
Mike Frysinger | 8f459c5 | 2011-10-25 17:29:24 -0400 | [diff] [blame] | 2 | * fdtdump.c - Contributed by Pantelis Antoniou <pantelis.antoniou AT gmail.com> |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 3 | */ |
| 4 | |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 5 | #include <stdbool.h> |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 6 | #include <stdint.h> |
| 7 | #include <stdio.h> |
David Gibson | 0ef2105 | 2009-11-17 17:00:53 +1100 | [diff] [blame] | 8 | #include <stdlib.h> |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 9 | #include <string.h> |
| 10 | #include <ctype.h> |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 11 | |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 12 | #include <libfdt.h> |
David Gibson | 11d7100 | 2008-06-26 10:43:06 +1000 | [diff] [blame] | 13 | #include <libfdt_env.h> |
Kim Phillips | 20b866a | 2012-11-13 18:34:09 -0600 | [diff] [blame] | 14 | #include <fdt.h> |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 15 | |
Simon Glass | 492f9d5 | 2011-07-05 12:02:49 -0700 | [diff] [blame] | 16 | #include "util.h" |
| 17 | |
Jean-Christophe Dubois | e24d39a | 2016-07-13 02:31:13 +0200 | [diff] [blame] | 18 | #define FDT_MAGIC_SIZE 4 |
Heinrich Schuchardt | 0931cea | 2016-12-22 00:59:06 +0100 | [diff] [blame] | 19 | #define MAX_VERSION 17 |
Jean-Christophe Dubois | e24d39a | 2016-07-13 02:31:13 +0200 | [diff] [blame] | 20 | |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 21 | #define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1)) |
| 22 | #define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a)))) |
David Gibson | bad5b28 | 2017-03-06 12:08:53 +1100 | [diff] [blame] | 23 | #define GET_CELL(p) (p += 4, *((const fdt32_t *)(p-4))) |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 24 | |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 25 | static const char *tagname(uint32_t tag) |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 26 | { |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 27 | static const char * const names[] = { |
Phil Elwell | 242c264 | 2014-10-17 23:22:11 +0100 | [diff] [blame] | 28 | #define TN(t) [t] = #t |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 29 | TN(FDT_BEGIN_NODE), |
| 30 | TN(FDT_END_NODE), |
| 31 | TN(FDT_PROP), |
| 32 | TN(FDT_NOP), |
| 33 | TN(FDT_END), |
| 34 | #undef TN |
| 35 | }; |
| 36 | if (tag < ARRAY_SIZE(names)) |
| 37 | if (names[tag]) |
| 38 | return names[tag]; |
| 39 | return "FDT_???"; |
| 40 | } |
| 41 | |
| 42 | #define dumpf(fmt, args...) \ |
| 43 | do { if (debug) printf("// " fmt, ## args); } while (0) |
| 44 | |
| 45 | static void dump_blob(void *blob, bool debug) |
| 46 | { |
| 47 | uintptr_t blob_off = (uintptr_t)blob; |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 48 | struct fdt_header *bph = blob; |
David Gibson | 11d7100 | 2008-06-26 10:43:06 +1000 | [diff] [blame] | 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); |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 52 | struct fdt_reserve_entry *p_rsvmap = |
David Gibson | 36786db | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 53 | (struct fdt_reserve_entry *)((char *)blob + off_mem_rsvmap); |
David Gibson | 0ef2105 | 2009-11-17 17:00:53 +1100 | [diff] [blame] | 54 | const char *p_struct = (const char *)blob + off_dt; |
| 55 | const char *p_strings = (const char *)blob + off_str; |
David Gibson | 11d7100 | 2008-06-26 10:43:06 +1000 | [diff] [blame] | 56 | uint32_t version = fdt32_to_cpu(bph->version); |
| 57 | uint32_t totalsize = fdt32_to_cpu(bph->totalsize); |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 58 | uint32_t tag; |
David Gibson | 0ef2105 | 2009-11-17 17:00:53 +1100 | [diff] [blame] | 59 | const char *p, *s, *t; |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 60 | int depth, sz, shift; |
| 61 | int i; |
| 62 | uint64_t addr, size; |
| 63 | |
| 64 | depth = 0; |
| 65 | shift = 4; |
| 66 | |
David Gibson | 0ef2105 | 2009-11-17 17:00:53 +1100 | [diff] [blame] | 67 | printf("/dts-v1/;\n"); |
David Gibson | 11d7100 | 2008-06-26 10:43:06 +1000 | [diff] [blame] | 68 | printf("// magic:\t\t0x%x\n", fdt32_to_cpu(bph->magic)); |
David Gibson | 3bb78bf | 2008-01-03 15:48:43 +1100 | [diff] [blame] | 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", |
David Gibson | 11d7100 | 2008-06-26 10:43:06 +1000 | [diff] [blame] | 75 | fdt32_to_cpu(bph->last_comp_version)); |
David Gibson | 3bb78bf | 2008-01-03 15:48:43 +1100 | [diff] [blame] | 76 | if (version >= 2) |
| 77 | printf("// boot_cpuid_phys:\t0x%x\n", |
David Gibson | 11d7100 | 2008-06-26 10:43:06 +1000 | [diff] [blame] | 78 | fdt32_to_cpu(bph->boot_cpuid_phys)); |
David Gibson | 3bb78bf | 2008-01-03 15:48:43 +1100 | [diff] [blame] | 79 | |
| 80 | if (version >= 3) |
| 81 | printf("// size_dt_strings:\t0x%x\n", |
David Gibson | 11d7100 | 2008-06-26 10:43:06 +1000 | [diff] [blame] | 82 | fdt32_to_cpu(bph->size_dt_strings)); |
David Gibson | 3bb78bf | 2008-01-03 15:48:43 +1100 | [diff] [blame] | 83 | if (version >= 17) |
| 84 | printf("// size_dt_struct:\t0x%x\n", |
David Gibson | 11d7100 | 2008-06-26 10:43:06 +1000 | [diff] [blame] | 85 | fdt32_to_cpu(bph->size_dt_struct)); |
David Gibson | 3bb78bf | 2008-01-03 15:48:43 +1100 | [diff] [blame] | 86 | printf("\n"); |
| 87 | |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 88 | for (i = 0; ; i++) { |
David Gibson | 11d7100 | 2008-06-26 10:43:06 +1000 | [diff] [blame] | 89 | addr = fdt64_to_cpu(p_rsvmap[i].address); |
| 90 | size = fdt64_to_cpu(p_rsvmap[i].size); |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 91 | if (addr == 0 && size == 0) |
| 92 | break; |
| 93 | |
Simon Glass | dfcfb7f | 2014-06-18 01:00:22 -0600 | [diff] [blame] | 94 | printf("/memreserve/ %#llx %#llx;\n", |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 95 | (unsigned long long)addr, (unsigned long long)size); |
| 96 | } |
| 97 | |
| 98 | p = p_struct; |
David Gibson | 11d7100 | 2008-06-26 10:43:06 +1000 | [diff] [blame] | 99 | while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) { |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 100 | |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 101 | dumpf("%04zx: tag: 0x%08x (%s)\n", |
| 102 | (uintptr_t)p - blob_off - 4, tag, tagname(tag)); |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 103 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 104 | if (tag == FDT_BEGIN_NODE) { |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 105 | s = p; |
| 106 | p = PALIGN(p + strlen(s) + 1, 4); |
| 107 | |
| 108 | if (*s == '\0') |
| 109 | s = "/"; |
| 110 | |
| 111 | printf("%*s%s {\n", depth * shift, "", s); |
| 112 | |
| 113 | depth++; |
| 114 | continue; |
| 115 | } |
| 116 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 117 | if (tag == FDT_END_NODE) { |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 118 | depth--; |
| 119 | |
| 120 | printf("%*s};\n", depth * shift, ""); |
| 121 | continue; |
| 122 | } |
| 123 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 124 | if (tag == FDT_NOP) { |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 125 | printf("%*s// [NOP]\n", depth * shift, ""); |
| 126 | continue; |
| 127 | } |
| 128 | |
David Gibson | fb7c7ac | 2007-09-26 13:11:05 +1000 | [diff] [blame] | 129 | if (tag != FDT_PROP) { |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 130 | fprintf(stderr, "%*s ** Unknown tag 0x%08x\n", depth * shift, "", tag); |
| 131 | break; |
| 132 | } |
David Gibson | 11d7100 | 2008-06-26 10:43:06 +1000 | [diff] [blame] | 133 | sz = fdt32_to_cpu(GET_CELL(p)); |
| 134 | s = p_strings + fdt32_to_cpu(GET_CELL(p)); |
David Gibson | 46c88df | 2007-03-14 11:02:40 +1100 | [diff] [blame] | 135 | if (version < 16 && sz >= 8) |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 136 | p = PALIGN(p, 8); |
| 137 | t = p; |
| 138 | |
| 139 | p = PALIGN(p + sz, 4); |
| 140 | |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 141 | dumpf("%04zx: string: %s\n", (uintptr_t)s - blob_off, s); |
| 142 | dumpf("%04zx: value\n", (uintptr_t)t - blob_off); |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 143 | printf("%*s%s", depth * shift, "", s); |
Simon Glass | d20391d | 2013-01-21 12:59:16 -0800 | [diff] [blame] | 144 | utilfdt_print_data(t, sz); |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 145 | printf(";\n"); |
| 146 | } |
| 147 | } |
| 148 | |
Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 149 | /* Usage related data. */ |
| 150 | static const char usage_synopsis[] = "fdtdump [options] <file>"; |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 151 | static const char usage_short_opts[] = "ds" USAGE_COMMON_SHORT_OPTS; |
Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 152 | static struct option const usage_long_opts[] = { |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 153 | {"debug", no_argument, NULL, 'd'}, |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 154 | {"scan", no_argument, NULL, 's'}, |
Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 155 | USAGE_COMMON_LONG_OPTS |
| 156 | }; |
| 157 | static const char * const usage_opts_help[] = { |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 158 | "Dump debug information while decoding the file", |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 159 | "Scan for an embedded fdt in file", |
Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 160 | USAGE_COMMON_OPTS_HELP |
| 161 | }; |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 162 | |
Heinrich Schuchardt | 0931cea | 2016-12-22 00:59:06 +0100 | [diff] [blame] | 163 | static bool valid_header(char *p, off_t len) |
| 164 | { |
| 165 | if (len < sizeof(struct fdt_header) || |
| 166 | fdt_magic(p) != FDT_MAGIC || |
| 167 | fdt_version(p) > MAX_VERSION || |
David Gibson | c225884 | 2017-04-18 12:52:08 +1000 | [diff] [blame] | 168 | fdt_last_comp_version(p) > MAX_VERSION || |
Heinrich Schuchardt | 0931cea | 2016-12-22 00:59:06 +0100 | [diff] [blame] | 169 | fdt_totalsize(p) >= len || |
| 170 | fdt_off_dt_struct(p) >= len || |
| 171 | fdt_off_dt_strings(p) >= len) |
| 172 | return 0; |
| 173 | else |
| 174 | return 1; |
| 175 | } |
| 176 | |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 177 | int main(int argc, char *argv[]) |
| 178 | { |
Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 179 | int opt; |
| 180 | const char *file; |
David Gibson | 0ef2105 | 2009-11-17 17:00:53 +1100 | [diff] [blame] | 181 | char *buf; |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 182 | bool debug = false; |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 183 | bool scan = false; |
| 184 | off_t len; |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 185 | |
David Gibson | 548aea2 | 2017-04-18 13:05:08 +1000 | [diff] [blame] | 186 | fprintf(stderr, "\n" |
| 187 | "**** fdtdump is a low-level debugging tool, not meant for general use.\n" |
| 188 | "**** If you want to decompile a dtb, you probably want\n" |
| 189 | "**** dtc -I dtb -O dts <filename>\n\n" |
| 190 | ); |
Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 191 | while ((opt = util_getopt_long()) != EOF) { |
| 192 | switch (opt) { |
| 193 | case_USAGE_COMMON_FLAGS |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 194 | |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 195 | case 'd': |
| 196 | debug = true; |
| 197 | break; |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 198 | case 's': |
| 199 | scan = true; |
| 200 | break; |
Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 201 | } |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 202 | } |
Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 203 | if (optind != argc - 1) |
Mike Frysinger | b9e8065 | 2013-05-24 18:04:43 +1000 | [diff] [blame] | 204 | usage("missing input filename"); |
Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 205 | file = argv[optind]; |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 206 | |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 207 | buf = utilfdt_read_len(file, &len); |
Mike Frysinger | be8d1c8 | 2013-04-15 22:13:12 -0400 | [diff] [blame] | 208 | if (!buf) |
| 209 | die("could not read: %s\n", file); |
| 210 | |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 211 | /* try and locate an embedded fdt in a bigger blob */ |
| 212 | if (scan) { |
Jean-Christophe Dubois | e24d39a | 2016-07-13 02:31:13 +0200 | [diff] [blame] | 213 | unsigned char smagic[FDT_MAGIC_SIZE]; |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 214 | char *p = buf; |
| 215 | char *endp = buf + len; |
| 216 | |
| 217 | fdt_set_magic(smagic, FDT_MAGIC); |
| 218 | |
| 219 | /* poor man's memmem */ |
Jean-Christophe Dubois | e24d39a | 2016-07-13 02:31:13 +0200 | [diff] [blame] | 220 | while ((endp - p) >= FDT_MAGIC_SIZE) { |
| 221 | p = memchr(p, smagic[0], endp - p - FDT_MAGIC_SIZE); |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 222 | if (!p) |
| 223 | break; |
| 224 | if (fdt_magic(p) == FDT_MAGIC) { |
| 225 | /* try and validate the main struct */ |
| 226 | off_t this_len = endp - p; |
Heinrich Schuchardt | 0931cea | 2016-12-22 00:59:06 +0100 | [diff] [blame] | 227 | if (valid_header(p, this_len)) |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 228 | break; |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 229 | if (debug) |
| 230 | printf("%s: skipping fdt magic at offset %#zx\n", |
| 231 | file, p - buf); |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 232 | } |
| 233 | ++p; |
| 234 | } |
Heinrich Schuchardt | 0931cea | 2016-12-22 00:59:06 +0100 | [diff] [blame] | 235 | if (!p || endp - p < sizeof(struct fdt_header)) |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 236 | die("%s: could not locate fdt magic\n", file); |
| 237 | printf("%s: found fdt at offset %#zx\n", file, p - buf); |
| 238 | buf = p; |
Heinrich Schuchardt | 0931cea | 2016-12-22 00:59:06 +0100 | [diff] [blame] | 239 | } else if (!valid_header(buf, len)) |
| 240 | die("%s: header is not valid\n", file); |
Mike Frysinger | fdc7387 | 2013-04-15 22:13:13 -0400 | [diff] [blame] | 241 | |
Mike Frysinger | 8ec013a | 2013-04-15 22:13:17 -0400 | [diff] [blame] | 242 | dump_blob(buf, debug); |
David Gibson | b2543fc | 2005-08-29 14:58:27 +1000 | [diff] [blame] | 243 | |
| 244 | return 0; |
| 245 | } |