2019-06-20 23:19:43 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-08-29 06:58:27 +02:00
|
|
|
/*
|
2011-10-25 23:29:24 +02:00
|
|
|
* fdtdump.c - Contributed by Pantelis Antoniou <pantelis.antoniou AT gmail.com>
|
2005-08-29 06:58:27 +02:00
|
|
|
*/
|
|
|
|
|
2013-04-16 04:13:13 +02:00
|
|
|
#include <stdbool.h>
|
2005-08-29 06:58:27 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2009-11-17 07:00:53 +01:00
|
|
|
#include <stdlib.h>
|
2005-08-29 06:58:27 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2018-06-05 01:55:15 +02:00
|
|
|
#include <inttypes.h>
|
2005-08-29 06:58:27 +02:00
|
|
|
|
2013-04-16 04:13:13 +02:00
|
|
|
#include <libfdt.h>
|
2008-06-26 02:43:06 +02:00
|
|
|
#include <libfdt_env.h>
|
2012-11-14 01:34:09 +01:00
|
|
|
#include <fdt.h>
|
2005-08-29 06:58:27 +02:00
|
|
|
|
2011-07-05 21:02:49 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2016-07-13 02:31:13 +02:00
|
|
|
#define FDT_MAGIC_SIZE 4
|
2021-06-11 19:10:34 +02:00
|
|
|
#define MAX_VERSION 17U
|
2016-07-13 02:31:13 +02:00
|
|
|
|
2005-08-29 06:58:27 +02:00
|
|
|
#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
|
2021-08-25 14:13:50 +02:00
|
|
|
#define PALIGN(p, a) ((void *)(ALIGN((uintptr_t)(p), (a))))
|
2017-03-06 02:08:53 +01:00
|
|
|
#define GET_CELL(p) (p += 4, *((const fdt32_t *)(p-4)))
|
2005-08-29 06:58:27 +02:00
|
|
|
|
2013-04-16 04:13:17 +02:00
|
|
|
static const char *tagname(uint32_t tag)
|
2005-08-29 06:58:27 +02:00
|
|
|
{
|
2013-04-16 04:13:17 +02:00
|
|
|
static const char * const names[] = {
|
2014-10-18 00:22:11 +02:00
|
|
|
#define TN(t) [t] = #t
|
2013-04-16 04:13:17 +02:00
|
|
|
TN(FDT_BEGIN_NODE),
|
|
|
|
TN(FDT_END_NODE),
|
|
|
|
TN(FDT_PROP),
|
|
|
|
TN(FDT_NOP),
|
|
|
|
TN(FDT_END),
|
|
|
|
#undef TN
|
|
|
|
};
|
|
|
|
if (tag < ARRAY_SIZE(names))
|
|
|
|
if (names[tag])
|
|
|
|
return names[tag];
|
|
|
|
return "FDT_???";
|
|
|
|
}
|
|
|
|
|
|
|
|
#define dumpf(fmt, args...) \
|
|
|
|
do { if (debug) printf("// " fmt, ## args); } while (0)
|
|
|
|
|
|
|
|
static void dump_blob(void *blob, bool debug)
|
|
|
|
{
|
|
|
|
uintptr_t blob_off = (uintptr_t)blob;
|
2007-09-26 05:11:05 +02:00
|
|
|
struct fdt_header *bph = blob;
|
2008-06-26 02:43:06 +02:00
|
|
|
uint32_t off_mem_rsvmap = fdt32_to_cpu(bph->off_mem_rsvmap);
|
|
|
|
uint32_t off_dt = fdt32_to_cpu(bph->off_dt_struct);
|
|
|
|
uint32_t off_str = fdt32_to_cpu(bph->off_dt_strings);
|
2007-09-26 05:11:05 +02:00
|
|
|
struct fdt_reserve_entry *p_rsvmap =
|
2008-07-07 02:10:48 +02:00
|
|
|
(struct fdt_reserve_entry *)((char *)blob + off_mem_rsvmap);
|
2009-11-17 07:00:53 +01:00
|
|
|
const char *p_struct = (const char *)blob + off_dt;
|
|
|
|
const char *p_strings = (const char *)blob + off_str;
|
2008-06-26 02:43:06 +02:00
|
|
|
uint32_t version = fdt32_to_cpu(bph->version);
|
|
|
|
uint32_t totalsize = fdt32_to_cpu(bph->totalsize);
|
2005-08-29 06:58:27 +02:00
|
|
|
uint32_t tag;
|
2009-11-17 07:00:53 +01:00
|
|
|
const char *p, *s, *t;
|
2005-08-29 06:58:27 +02:00
|
|
|
int depth, sz, shift;
|
|
|
|
int i;
|
|
|
|
uint64_t addr, size;
|
|
|
|
|
|
|
|
depth = 0;
|
|
|
|
shift = 4;
|
|
|
|
|
2009-11-17 07:00:53 +01:00
|
|
|
printf("/dts-v1/;\n");
|
2018-06-05 01:55:15 +02:00
|
|
|
printf("// magic:\t\t0x%"PRIx32"\n", fdt32_to_cpu(bph->magic));
|
|
|
|
printf("// totalsize:\t\t0x%"PRIx32" (%"PRIu32")\n",
|
|
|
|
totalsize, totalsize);
|
|
|
|
printf("// off_dt_struct:\t0x%"PRIx32"\n", off_dt);
|
|
|
|
printf("// off_dt_strings:\t0x%"PRIx32"\n", off_str);
|
|
|
|
printf("// off_mem_rsvmap:\t0x%"PRIx32"\n", off_mem_rsvmap);
|
|
|
|
printf("// version:\t\t%"PRIu32"\n", version);
|
|
|
|
printf("// last_comp_version:\t%"PRIu32"\n",
|
2008-06-26 02:43:06 +02:00
|
|
|
fdt32_to_cpu(bph->last_comp_version));
|
2008-01-03 05:48:43 +01:00
|
|
|
if (version >= 2)
|
2018-06-05 01:55:15 +02:00
|
|
|
printf("// boot_cpuid_phys:\t0x%"PRIx32"\n",
|
2008-06-26 02:43:06 +02:00
|
|
|
fdt32_to_cpu(bph->boot_cpuid_phys));
|
2008-01-03 05:48:43 +01:00
|
|
|
|
|
|
|
if (version >= 3)
|
2018-06-05 01:55:15 +02:00
|
|
|
printf("// size_dt_strings:\t0x%"PRIx32"\n",
|
2008-06-26 02:43:06 +02:00
|
|
|
fdt32_to_cpu(bph->size_dt_strings));
|
2008-01-03 05:48:43 +01:00
|
|
|
if (version >= 17)
|
2018-06-05 01:55:15 +02:00
|
|
|
printf("// size_dt_struct:\t0x%"PRIx32"\n",
|
2008-06-26 02:43:06 +02:00
|
|
|
fdt32_to_cpu(bph->size_dt_struct));
|
2008-01-03 05:48:43 +01:00
|
|
|
printf("\n");
|
|
|
|
|
2005-08-29 06:58:27 +02:00
|
|
|
for (i = 0; ; i++) {
|
2008-06-26 02:43:06 +02:00
|
|
|
addr = fdt64_to_cpu(p_rsvmap[i].address);
|
|
|
|
size = fdt64_to_cpu(p_rsvmap[i].size);
|
2005-08-29 06:58:27 +02:00
|
|
|
if (addr == 0 && size == 0)
|
|
|
|
break;
|
|
|
|
|
2018-06-05 01:55:15 +02:00
|
|
|
printf("/memreserve/ %#"PRIx64" %#"PRIx64";\n",
|
|
|
|
addr, size);
|
2005-08-29 06:58:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p = p_struct;
|
2008-06-26 02:43:06 +02:00
|
|
|
while ((tag = fdt32_to_cpu(GET_CELL(p))) != FDT_END) {
|
2005-08-29 06:58:27 +02:00
|
|
|
|
2019-01-02 12:30:13 +01:00
|
|
|
dumpf("%04"PRIxPTR": tag: 0x%08"PRIx32" (%s)\n",
|
2013-04-16 04:13:17 +02:00
|
|
|
(uintptr_t)p - blob_off - 4, tag, tagname(tag));
|
2005-08-29 06:58:27 +02:00
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
if (tag == FDT_BEGIN_NODE) {
|
2005-08-29 06:58:27 +02:00
|
|
|
s = p;
|
|
|
|
p = PALIGN(p + strlen(s) + 1, 4);
|
|
|
|
|
|
|
|
if (*s == '\0')
|
|
|
|
s = "/";
|
|
|
|
|
|
|
|
printf("%*s%s {\n", depth * shift, "", s);
|
|
|
|
|
|
|
|
depth++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
if (tag == FDT_END_NODE) {
|
2005-08-29 06:58:27 +02:00
|
|
|
depth--;
|
|
|
|
|
|
|
|
printf("%*s};\n", depth * shift, "");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
if (tag == FDT_NOP) {
|
2005-08-29 06:58:27 +02:00
|
|
|
printf("%*s// [NOP]\n", depth * shift, "");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-09-26 05:11:05 +02:00
|
|
|
if (tag != FDT_PROP) {
|
2018-06-05 01:55:15 +02:00
|
|
|
fprintf(stderr, "%*s ** Unknown tag 0x%08"PRIx32"\n", depth * shift, "", tag);
|
2005-08-29 06:58:27 +02:00
|
|
|
break;
|
|
|
|
}
|
2008-06-26 02:43:06 +02:00
|
|
|
sz = fdt32_to_cpu(GET_CELL(p));
|
|
|
|
s = p_strings + fdt32_to_cpu(GET_CELL(p));
|
2007-03-14 01:02:40 +01:00
|
|
|
if (version < 16 && sz >= 8)
|
2005-08-29 06:58:27 +02:00
|
|
|
p = PALIGN(p, 8);
|
|
|
|
t = p;
|
|
|
|
|
|
|
|
p = PALIGN(p + sz, 4);
|
|
|
|
|
2019-01-02 12:30:13 +01:00
|
|
|
dumpf("%04"PRIxPTR": string: %s\n", (uintptr_t)s - blob_off, s);
|
|
|
|
dumpf("%04"PRIxPTR": value\n", (uintptr_t)t - blob_off);
|
2005-08-29 06:58:27 +02:00
|
|
|
printf("%*s%s", depth * shift, "", s);
|
2013-01-21 21:59:16 +01:00
|
|
|
utilfdt_print_data(t, sz);
|
2005-08-29 06:58:27 +02:00
|
|
|
printf(";\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 04:13:12 +02:00
|
|
|
/* Usage related data. */
|
|
|
|
static const char usage_synopsis[] = "fdtdump [options] <file>";
|
2013-04-16 04:13:17 +02:00
|
|
|
static const char usage_short_opts[] = "ds" USAGE_COMMON_SHORT_OPTS;
|
2013-04-16 04:13:12 +02:00
|
|
|
static struct option const usage_long_opts[] = {
|
2013-04-16 04:13:17 +02:00
|
|
|
{"debug", no_argument, NULL, 'd'},
|
2013-04-16 04:13:13 +02:00
|
|
|
{"scan", no_argument, NULL, 's'},
|
2013-04-16 04:13:12 +02:00
|
|
|
USAGE_COMMON_LONG_OPTS
|
|
|
|
};
|
|
|
|
static const char * const usage_opts_help[] = {
|
2013-04-16 04:13:17 +02:00
|
|
|
"Dump debug information while decoding the file",
|
2013-04-16 04:13:13 +02:00
|
|
|
"Scan for an embedded fdt in file",
|
2013-04-16 04:13:12 +02:00
|
|
|
USAGE_COMMON_OPTS_HELP
|
|
|
|
};
|
2005-08-29 06:58:27 +02:00
|
|
|
|
2021-06-11 19:10:34 +02:00
|
|
|
static bool valid_header(char *p, size_t len)
|
2016-12-22 00:59:06 +01:00
|
|
|
{
|
|
|
|
if (len < sizeof(struct fdt_header) ||
|
|
|
|
fdt_magic(p) != FDT_MAGIC ||
|
|
|
|
fdt_version(p) > MAX_VERSION ||
|
2017-04-18 04:52:08 +02:00
|
|
|
fdt_last_comp_version(p) > MAX_VERSION ||
|
2016-12-22 00:59:06 +01:00
|
|
|
fdt_totalsize(p) >= len ||
|
|
|
|
fdt_off_dt_struct(p) >= len ||
|
|
|
|
fdt_off_dt_strings(p) >= len)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-08-29 06:58:27 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-04-16 04:13:12 +02:00
|
|
|
int opt;
|
|
|
|
const char *file;
|
2009-11-17 07:00:53 +01:00
|
|
|
char *buf;
|
2013-04-16 04:13:17 +02:00
|
|
|
bool debug = false;
|
2013-04-16 04:13:13 +02:00
|
|
|
bool scan = false;
|
2018-03-17 04:53:23 +01:00
|
|
|
size_t len;
|
2005-08-29 06:58:27 +02:00
|
|
|
|
2017-04-18 05:05:08 +02:00
|
|
|
fprintf(stderr, "\n"
|
|
|
|
"**** fdtdump is a low-level debugging tool, not meant for general use.\n"
|
|
|
|
"**** If you want to decompile a dtb, you probably want\n"
|
|
|
|
"**** dtc -I dtb -O dts <filename>\n\n"
|
|
|
|
);
|
2013-04-16 04:13:12 +02:00
|
|
|
while ((opt = util_getopt_long()) != EOF) {
|
|
|
|
switch (opt) {
|
|
|
|
case_USAGE_COMMON_FLAGS
|
2013-04-16 04:13:13 +02:00
|
|
|
|
2013-04-16 04:13:17 +02:00
|
|
|
case 'd':
|
|
|
|
debug = true;
|
|
|
|
break;
|
2013-04-16 04:13:13 +02:00
|
|
|
case 's':
|
|
|
|
scan = true;
|
|
|
|
break;
|
2013-04-16 04:13:12 +02:00
|
|
|
}
|
2005-08-29 06:58:27 +02:00
|
|
|
}
|
2013-04-16 04:13:12 +02:00
|
|
|
if (optind != argc - 1)
|
2013-05-24 10:04:43 +02:00
|
|
|
usage("missing input filename");
|
2013-04-16 04:13:12 +02:00
|
|
|
file = argv[optind];
|
|
|
|
|
2018-03-16 08:27:03 +01:00
|
|
|
buf = utilfdt_read(file, &len);
|
2013-04-16 04:13:12 +02:00
|
|
|
if (!buf)
|
|
|
|
die("could not read: %s\n", file);
|
2005-08-29 06:58:27 +02:00
|
|
|
|
2013-04-16 04:13:13 +02:00
|
|
|
/* try and locate an embedded fdt in a bigger blob */
|
|
|
|
if (scan) {
|
2016-07-13 02:31:13 +02:00
|
|
|
unsigned char smagic[FDT_MAGIC_SIZE];
|
2013-04-16 04:13:13 +02:00
|
|
|
char *p = buf;
|
|
|
|
char *endp = buf + len;
|
|
|
|
|
2021-01-06 04:52:26 +01:00
|
|
|
fdt32_st(smagic, FDT_MAGIC);
|
2013-04-16 04:13:13 +02:00
|
|
|
|
|
|
|
/* poor man's memmem */
|
2016-07-13 02:31:13 +02:00
|
|
|
while ((endp - p) >= FDT_MAGIC_SIZE) {
|
|
|
|
p = memchr(p, smagic[0], endp - p - FDT_MAGIC_SIZE);
|
2013-04-16 04:13:13 +02:00
|
|
|
if (!p)
|
|
|
|
break;
|
|
|
|
if (fdt_magic(p) == FDT_MAGIC) {
|
|
|
|
/* try and validate the main struct */
|
|
|
|
off_t this_len = endp - p;
|
2016-12-22 00:59:06 +01:00
|
|
|
if (valid_header(p, this_len))
|
2013-04-16 04:13:13 +02:00
|
|
|
break;
|
2013-04-16 04:13:17 +02:00
|
|
|
if (debug)
|
2018-10-22 12:14:44 +02:00
|
|
|
printf("%s: skipping fdt magic at offset %#tx\n",
|
2013-04-16 04:13:17 +02:00
|
|
|
file, p - buf);
|
2013-04-16 04:13:13 +02:00
|
|
|
}
|
|
|
|
++p;
|
|
|
|
}
|
2021-06-11 19:10:34 +02:00
|
|
|
if (!p || (size_t)(endp - p) < sizeof(struct fdt_header))
|
2013-04-16 04:13:13 +02:00
|
|
|
die("%s: could not locate fdt magic\n", file);
|
2018-10-22 12:14:44 +02:00
|
|
|
printf("%s: found fdt at offset %#tx\n", file, p - buf);
|
2013-04-16 04:13:13 +02:00
|
|
|
buf = p;
|
2016-12-22 00:59:06 +01:00
|
|
|
} else if (!valid_header(buf, len))
|
|
|
|
die("%s: header is not valid\n", file);
|
2013-04-16 04:13:13 +02:00
|
|
|
|
2013-04-16 04:13:17 +02:00
|
|
|
dump_blob(buf, debug);
|
2005-08-29 06:58:27 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|