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