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