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>
|
|
|
|
|
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
|
|
|
|
|
2005-08-29 06:58:27 +02:00
|
|
|
#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
|
|
|
|
#define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a))))
|
2009-11-17 07:00:53 +01:00
|
|
|
#define GET_CELL(p) (p += 4, *((const uint32_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");
|
2008-06-26 02:43:06 +02:00
|
|
|
printf("// magic:\t\t0x%x\n", fdt32_to_cpu(bph->magic));
|
2008-01-03 05:48:43 +01:00
|
|
|
printf("// totalsize:\t\t0x%x (%d)\n", totalsize, totalsize);
|
|
|
|
printf("// off_dt_struct:\t0x%x\n", off_dt);
|
|
|
|
printf("// off_dt_strings:\t0x%x\n", off_str);
|
|
|
|
printf("// off_mem_rsvmap:\t0x%x\n", off_mem_rsvmap);
|
|
|
|
printf("// version:\t\t%d\n", version);
|
|
|
|
printf("// last_comp_version:\t%d\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)
|
|
|
|
printf("// boot_cpuid_phys:\t0x%x\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)
|
|
|
|
printf("// size_dt_strings:\t0x%x\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)
|
|
|
|
printf("// size_dt_struct:\t0x%x\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;
|
|
|
|
|
2014-06-18 09:00:22 +02:00
|
|
|
printf("/memreserve/ %#llx %#llx;\n",
|
2005-08-29 06:58:27 +02:00
|
|
|
(unsigned long long)addr, (unsigned long long)size);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-04-16 04:13:17 +02:00
|
|
|
dumpf("%04zx: tag: 0x%08x (%s)\n",
|
|
|
|
(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) {
|
2005-08-29 06:58:27 +02:00
|
|
|
fprintf(stderr, "%*s ** Unknown tag 0x%08x\n", depth * shift, "", tag);
|
|
|
|
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);
|
|
|
|
|
2013-04-16 04:13:17 +02:00
|
|
|
dumpf("%04zx: string: %s\n", (uintptr_t)s - blob_off, s);
|
|
|
|
dumpf("%04zx: 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
|
|
|
|
|
|
|
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;
|
|
|
|
off_t len;
|
2005-08-29 06:58:27 +02:00
|
|
|
|
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];
|
|
|
|
|
2013-04-16 04:13:13 +02:00
|
|
|
buf = utilfdt_read_len(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;
|
|
|
|
|
|
|
|
fdt_set_magic(smagic, FDT_MAGIC);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
fdt32_t max_version = 17;
|
|
|
|
if (fdt_version(p) <= max_version &&
|
|
|
|
fdt_last_comp_version(p) < max_version &&
|
|
|
|
fdt_totalsize(p) < this_len &&
|
|
|
|
fdt_off_dt_struct(p) < this_len &&
|
|
|
|
fdt_off_dt_strings(p) < this_len)
|
|
|
|
break;
|
2013-04-16 04:13:17 +02:00
|
|
|
if (debug)
|
|
|
|
printf("%s: skipping fdt magic at offset %#zx\n",
|
|
|
|
file, p - buf);
|
2013-04-16 04:13:13 +02:00
|
|
|
}
|
|
|
|
++p;
|
|
|
|
}
|
2016-07-13 02:31:13 +02:00
|
|
|
if (!p || ((endp - p) < FDT_MAGIC_SIZE))
|
2013-04-16 04:13:13 +02:00
|
|
|
die("%s: could not locate fdt magic\n", file);
|
|
|
|
printf("%s: found fdt at offset %#zx\n", file, p - buf);
|
|
|
|
buf = p;
|
|
|
|
}
|
|
|
|
|
2013-04-16 04:13:17 +02:00
|
|
|
dump_blob(buf, debug);
|
2005-08-29 06:58:27 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|