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