xref: /openbmc/linux/scripts/dtc/flattree.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
112869ecdSRob Herring // SPDX-License-Identifier: GPL-2.0-or-later
29fffb55fSDavid Gibson /*
39fffb55fSDavid Gibson  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
49fffb55fSDavid Gibson  */
59fffb55fSDavid Gibson 
69fffb55fSDavid Gibson #include "dtc.h"
79fffb55fSDavid Gibson #include "srcpos.h"
89fffb55fSDavid Gibson 
99fffb55fSDavid Gibson #define FTF_FULLPATH	0x1
109fffb55fSDavid Gibson #define FTF_VARALIGN	0x2
119fffb55fSDavid Gibson #define FTF_NAMEPROPS	0x4
129fffb55fSDavid Gibson #define FTF_BOOTCPUID	0x8
139fffb55fSDavid Gibson #define FTF_STRTABSIZE	0x10
149fffb55fSDavid Gibson #define FTF_STRUCTSIZE	0x20
159fffb55fSDavid Gibson #define FTF_NOPS	0x40
169fffb55fSDavid Gibson 
179fffb55fSDavid Gibson static struct version_info {
189fffb55fSDavid Gibson 	int version;
199fffb55fSDavid Gibson 	int last_comp_version;
209fffb55fSDavid Gibson 	int hdr_size;
219fffb55fSDavid Gibson 	int flags;
229fffb55fSDavid Gibson } version_table[] = {
239fffb55fSDavid Gibson 	{1, 1, FDT_V1_SIZE,
249fffb55fSDavid Gibson 	 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS},
259fffb55fSDavid Gibson 	{2, 1, FDT_V2_SIZE,
269fffb55fSDavid Gibson 	 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID},
279fffb55fSDavid Gibson 	{3, 1, FDT_V3_SIZE,
289fffb55fSDavid Gibson 	 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID|FTF_STRTABSIZE},
299fffb55fSDavid Gibson 	{16, 16, FDT_V3_SIZE,
309fffb55fSDavid Gibson 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_NOPS},
319fffb55fSDavid Gibson 	{17, 16, FDT_V17_SIZE,
329fffb55fSDavid Gibson 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
339fffb55fSDavid Gibson };
349fffb55fSDavid Gibson 
359fffb55fSDavid Gibson struct emitter {
369fffb55fSDavid Gibson 	void (*cell)(void *, cell_t);
3789d12310SRob Herring 	void (*string)(void *, const char *, int);
389fffb55fSDavid Gibson 	void (*align)(void *, int);
399fffb55fSDavid Gibson 	void (*data)(void *, struct data);
40658f29a5SJohn Bonesio 	void (*beginnode)(void *, struct label *labels);
41658f29a5SJohn Bonesio 	void (*endnode)(void *, struct label *labels);
42658f29a5SJohn Bonesio 	void (*property)(void *, struct label *labels);
439fffb55fSDavid Gibson };
449fffb55fSDavid Gibson 
bin_emit_cell(void * e,cell_t val)459fffb55fSDavid Gibson static void bin_emit_cell(void *e, cell_t val)
469fffb55fSDavid Gibson {
479fffb55fSDavid Gibson 	struct data *dtbuf = e;
489fffb55fSDavid Gibson 
499fffb55fSDavid Gibson 	*dtbuf = data_append_cell(*dtbuf, val);
509fffb55fSDavid Gibson }
519fffb55fSDavid Gibson 
bin_emit_string(void * e,const char * str,int len)5289d12310SRob Herring static void bin_emit_string(void *e, const char *str, int len)
539fffb55fSDavid Gibson {
549fffb55fSDavid Gibson 	struct data *dtbuf = e;
559fffb55fSDavid Gibson 
569fffb55fSDavid Gibson 	if (len == 0)
579fffb55fSDavid Gibson 		len = strlen(str);
589fffb55fSDavid Gibson 
599fffb55fSDavid Gibson 	*dtbuf = data_append_data(*dtbuf, str, len);
609fffb55fSDavid Gibson 	*dtbuf = data_append_byte(*dtbuf, '\0');
619fffb55fSDavid Gibson }
629fffb55fSDavid Gibson 
bin_emit_align(void * e,int a)639fffb55fSDavid Gibson static void bin_emit_align(void *e, int a)
649fffb55fSDavid Gibson {
659fffb55fSDavid Gibson 	struct data *dtbuf = e;
669fffb55fSDavid Gibson 
679fffb55fSDavid Gibson 	*dtbuf = data_append_align(*dtbuf, a);
689fffb55fSDavid Gibson }
699fffb55fSDavid Gibson 
bin_emit_data(void * e,struct data d)709fffb55fSDavid Gibson static void bin_emit_data(void *e, struct data d)
719fffb55fSDavid Gibson {
729fffb55fSDavid Gibson 	struct data *dtbuf = e;
739fffb55fSDavid Gibson 
749fffb55fSDavid Gibson 	*dtbuf = data_append_data(*dtbuf, d.val, d.len);
759fffb55fSDavid Gibson }
769fffb55fSDavid Gibson 
bin_emit_beginnode(void * e,struct label * labels)77658f29a5SJohn Bonesio static void bin_emit_beginnode(void *e, struct label *labels)
789fffb55fSDavid Gibson {
799fffb55fSDavid Gibson 	bin_emit_cell(e, FDT_BEGIN_NODE);
809fffb55fSDavid Gibson }
819fffb55fSDavid Gibson 
bin_emit_endnode(void * e,struct label * labels)82658f29a5SJohn Bonesio static void bin_emit_endnode(void *e, struct label *labels)
839fffb55fSDavid Gibson {
849fffb55fSDavid Gibson 	bin_emit_cell(e, FDT_END_NODE);
859fffb55fSDavid Gibson }
869fffb55fSDavid Gibson 
bin_emit_property(void * e,struct label * labels)87658f29a5SJohn Bonesio static void bin_emit_property(void *e, struct label *labels)
889fffb55fSDavid Gibson {
899fffb55fSDavid Gibson 	bin_emit_cell(e, FDT_PROP);
909fffb55fSDavid Gibson }
919fffb55fSDavid Gibson 
929fffb55fSDavid Gibson static struct emitter bin_emitter = {
939fffb55fSDavid Gibson 	.cell = bin_emit_cell,
949fffb55fSDavid Gibson 	.string = bin_emit_string,
959fffb55fSDavid Gibson 	.align = bin_emit_align,
969fffb55fSDavid Gibson 	.data = bin_emit_data,
979fffb55fSDavid Gibson 	.beginnode = bin_emit_beginnode,
989fffb55fSDavid Gibson 	.endnode = bin_emit_endnode,
999fffb55fSDavid Gibson 	.property = bin_emit_property,
1009fffb55fSDavid Gibson };
1019fffb55fSDavid Gibson 
emit_label(FILE * f,const char * prefix,const char * label)1029fffb55fSDavid Gibson static void emit_label(FILE *f, const char *prefix, const char *label)
1039fffb55fSDavid Gibson {
1049fffb55fSDavid Gibson 	fprintf(f, "\t.globl\t%s_%s\n", prefix, label);
1059fffb55fSDavid Gibson 	fprintf(f, "%s_%s:\n", prefix, label);
1069fffb55fSDavid Gibson 	fprintf(f, "_%s_%s:\n", prefix, label);
1079fffb55fSDavid Gibson }
1089fffb55fSDavid Gibson 
emit_offset_label(FILE * f,const char * label,int offset)1099fffb55fSDavid Gibson static void emit_offset_label(FILE *f, const char *label, int offset)
1109fffb55fSDavid Gibson {
1119fffb55fSDavid Gibson 	fprintf(f, "\t.globl\t%s\n", label);
1129fffb55fSDavid Gibson 	fprintf(f, "%s\t= . + %d\n", label, offset);
1139fffb55fSDavid Gibson }
1149fffb55fSDavid Gibson 
115658f29a5SJohn Bonesio #define ASM_EMIT_BELONG(f, fmt, ...) \
116658f29a5SJohn Bonesio 	{ \
117658f29a5SJohn Bonesio 		fprintf((f), "\t.byte\t((" fmt ") >> 24) & 0xff\n", __VA_ARGS__); \
118658f29a5SJohn Bonesio 		fprintf((f), "\t.byte\t((" fmt ") >> 16) & 0xff\n", __VA_ARGS__); \
119658f29a5SJohn Bonesio 		fprintf((f), "\t.byte\t((" fmt ") >> 8) & 0xff\n", __VA_ARGS__); \
120658f29a5SJohn Bonesio 		fprintf((f), "\t.byte\t(" fmt ") & 0xff\n", __VA_ARGS__); \
121658f29a5SJohn Bonesio 	}
122658f29a5SJohn Bonesio 
asm_emit_cell(void * e,cell_t val)1239fffb55fSDavid Gibson static void asm_emit_cell(void *e, cell_t val)
1249fffb55fSDavid Gibson {
1259fffb55fSDavid Gibson 	FILE *f = e;
1269fffb55fSDavid Gibson 
127*a77725a9SRob Herring 	fprintf(f, "\t.byte\t0x%02x\n" "\t.byte\t0x%02x\n"
128*a77725a9SRob Herring 		"\t.byte\t0x%02x\n" "\t.byte\t0x%02x\n",
129658f29a5SJohn Bonesio 		(val >> 24) & 0xff, (val >> 16) & 0xff,
130658f29a5SJohn Bonesio 		(val >> 8) & 0xff, val & 0xff);
1319fffb55fSDavid Gibson }
1329fffb55fSDavid Gibson 
asm_emit_string(void * e,const char * str,int len)13389d12310SRob Herring static void asm_emit_string(void *e, const char *str, int len)
1349fffb55fSDavid Gibson {
1359fffb55fSDavid Gibson 	FILE *f = e;
1369fffb55fSDavid Gibson 
13789d12310SRob Herring 	if (len != 0)
138*a77725a9SRob Herring 		fprintf(f, "\t.asciz\t\"%.*s\"\n", len, str);
13989d12310SRob Herring 	else
140*a77725a9SRob Herring 		fprintf(f, "\t.asciz\t\"%s\"\n", str);
1419fffb55fSDavid Gibson }
1429fffb55fSDavid Gibson 
asm_emit_align(void * e,int a)1439fffb55fSDavid Gibson static void asm_emit_align(void *e, int a)
1449fffb55fSDavid Gibson {
1459fffb55fSDavid Gibson 	FILE *f = e;
1469fffb55fSDavid Gibson 
147658f29a5SJohn Bonesio 	fprintf(f, "\t.balign\t%d, 0\n", a);
1489fffb55fSDavid Gibson }
1499fffb55fSDavid Gibson 
asm_emit_data(void * e,struct data d)1509fffb55fSDavid Gibson static void asm_emit_data(void *e, struct data d)
1519fffb55fSDavid Gibson {
1529fffb55fSDavid Gibson 	FILE *f = e;
15379edff12SRob Herring 	unsigned int off = 0;
1549fffb55fSDavid Gibson 	struct marker *m = d.markers;
1559fffb55fSDavid Gibson 
1569fffb55fSDavid Gibson 	for_each_marker_of_type(m, LABEL)
1579fffb55fSDavid Gibson 		emit_offset_label(f, m->ref, m->offset);
1589fffb55fSDavid Gibson 
1599fffb55fSDavid Gibson 	while ((d.len - off) >= sizeof(uint32_t)) {
1603eb619b2SRob Herring 		asm_emit_cell(e, dtb_ld32(d.val + off));
1619fffb55fSDavid Gibson 		off += sizeof(uint32_t);
1629fffb55fSDavid Gibson 	}
1639fffb55fSDavid Gibson 
1649fffb55fSDavid Gibson 	while ((d.len - off) >= 1) {
1659fffb55fSDavid Gibson 		fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]);
1669fffb55fSDavid Gibson 		off += 1;
1679fffb55fSDavid Gibson 	}
1689fffb55fSDavid Gibson 
1699fffb55fSDavid Gibson 	assert(off == d.len);
1709fffb55fSDavid Gibson }
1719fffb55fSDavid Gibson 
asm_emit_beginnode(void * e,struct label * labels)172658f29a5SJohn Bonesio static void asm_emit_beginnode(void *e, struct label *labels)
1739fffb55fSDavid Gibson {
1749fffb55fSDavid Gibson 	FILE *f = e;
175658f29a5SJohn Bonesio 	struct label *l;
1769fffb55fSDavid Gibson 
177658f29a5SJohn Bonesio 	for_each_label(labels, l) {
178658f29a5SJohn Bonesio 		fprintf(f, "\t.globl\t%s\n", l->label);
179658f29a5SJohn Bonesio 		fprintf(f, "%s:\n", l->label);
1809fffb55fSDavid Gibson 	}
181658f29a5SJohn Bonesio 	fprintf(f, "\t/* FDT_BEGIN_NODE */\n");
182658f29a5SJohn Bonesio 	asm_emit_cell(e, FDT_BEGIN_NODE);
1839fffb55fSDavid Gibson }
1849fffb55fSDavid Gibson 
asm_emit_endnode(void * e,struct label * labels)185658f29a5SJohn Bonesio static void asm_emit_endnode(void *e, struct label *labels)
1869fffb55fSDavid Gibson {
1879fffb55fSDavid Gibson 	FILE *f = e;
188658f29a5SJohn Bonesio 	struct label *l;
1899fffb55fSDavid Gibson 
190658f29a5SJohn Bonesio 	fprintf(f, "\t/* FDT_END_NODE */\n");
191658f29a5SJohn Bonesio 	asm_emit_cell(e, FDT_END_NODE);
192658f29a5SJohn Bonesio 	for_each_label(labels, l) {
193658f29a5SJohn Bonesio 		fprintf(f, "\t.globl\t%s_end\n", l->label);
194658f29a5SJohn Bonesio 		fprintf(f, "%s_end:\n", l->label);
1959fffb55fSDavid Gibson 	}
1969fffb55fSDavid Gibson }
1979fffb55fSDavid Gibson 
asm_emit_property(void * e,struct label * labels)198658f29a5SJohn Bonesio static void asm_emit_property(void *e, struct label *labels)
1999fffb55fSDavid Gibson {
2009fffb55fSDavid Gibson 	FILE *f = e;
201658f29a5SJohn Bonesio 	struct label *l;
2029fffb55fSDavid Gibson 
203658f29a5SJohn Bonesio 	for_each_label(labels, l) {
204658f29a5SJohn Bonesio 		fprintf(f, "\t.globl\t%s\n", l->label);
205658f29a5SJohn Bonesio 		fprintf(f, "%s:\n", l->label);
2069fffb55fSDavid Gibson 	}
207658f29a5SJohn Bonesio 	fprintf(f, "\t/* FDT_PROP */\n");
208658f29a5SJohn Bonesio 	asm_emit_cell(e, FDT_PROP);
2099fffb55fSDavid Gibson }
2109fffb55fSDavid Gibson 
2119fffb55fSDavid Gibson static struct emitter asm_emitter = {
2129fffb55fSDavid Gibson 	.cell = asm_emit_cell,
2139fffb55fSDavid Gibson 	.string = asm_emit_string,
2149fffb55fSDavid Gibson 	.align = asm_emit_align,
2159fffb55fSDavid Gibson 	.data = asm_emit_data,
2169fffb55fSDavid Gibson 	.beginnode = asm_emit_beginnode,
2179fffb55fSDavid Gibson 	.endnode = asm_emit_endnode,
2189fffb55fSDavid Gibson 	.property = asm_emit_property,
2199fffb55fSDavid Gibson };
2209fffb55fSDavid Gibson 
stringtable_insert(struct data * d,const char * str)2219fffb55fSDavid Gibson static int stringtable_insert(struct data *d, const char *str)
2229fffb55fSDavid Gibson {
22379edff12SRob Herring 	unsigned int i;
2249fffb55fSDavid Gibson 
2259fffb55fSDavid Gibson 	/* FIXME: do this more efficiently? */
2269fffb55fSDavid Gibson 
2279fffb55fSDavid Gibson 	for (i = 0; i < d->len; i++) {
2289fffb55fSDavid Gibson 		if (streq(str, d->val + i))
2299fffb55fSDavid Gibson 			return i;
2309fffb55fSDavid Gibson 	}
2319fffb55fSDavid Gibson 
2329fffb55fSDavid Gibson 	*d = data_append_data(*d, str, strlen(str)+1);
2339fffb55fSDavid Gibson 	return i;
2349fffb55fSDavid Gibson }
2359fffb55fSDavid Gibson 
flatten_tree(struct node * tree,struct emitter * emit,void * etarget,struct data * strbuf,struct version_info * vi)2369fffb55fSDavid Gibson static void flatten_tree(struct node *tree, struct emitter *emit,
2379fffb55fSDavid Gibson 			 void *etarget, struct data *strbuf,
2389fffb55fSDavid Gibson 			 struct version_info *vi)
2399fffb55fSDavid Gibson {
2409fffb55fSDavid Gibson 	struct property *prop;
2419fffb55fSDavid Gibson 	struct node *child;
24247605971SRob Herring 	bool seen_name_prop = false;
2439fffb55fSDavid Gibson 
244cd296721SStephen Warren 	if (tree->deleted)
245cd296721SStephen Warren 		return;
246cd296721SStephen Warren 
247658f29a5SJohn Bonesio 	emit->beginnode(etarget, tree->labels);
2489fffb55fSDavid Gibson 
2499fffb55fSDavid Gibson 	if (vi->flags & FTF_FULLPATH)
2509fffb55fSDavid Gibson 		emit->string(etarget, tree->fullpath, 0);
2519fffb55fSDavid Gibson 	else
2529fffb55fSDavid Gibson 		emit->string(etarget, tree->name, 0);
2539fffb55fSDavid Gibson 
2549fffb55fSDavid Gibson 	emit->align(etarget, sizeof(cell_t));
2559fffb55fSDavid Gibson 
2569fffb55fSDavid Gibson 	for_each_property(tree, prop) {
2579fffb55fSDavid Gibson 		int nameoff;
2589fffb55fSDavid Gibson 
2599fffb55fSDavid Gibson 		if (streq(prop->name, "name"))
26047605971SRob Herring 			seen_name_prop = true;
2619fffb55fSDavid Gibson 
2629fffb55fSDavid Gibson 		nameoff = stringtable_insert(strbuf, prop->name);
2639fffb55fSDavid Gibson 
264658f29a5SJohn Bonesio 		emit->property(etarget, prop->labels);
2659fffb55fSDavid Gibson 		emit->cell(etarget, prop->val.len);
2669fffb55fSDavid Gibson 		emit->cell(etarget, nameoff);
2679fffb55fSDavid Gibson 
2689fffb55fSDavid Gibson 		if ((vi->flags & FTF_VARALIGN) && (prop->val.len >= 8))
2699fffb55fSDavid Gibson 			emit->align(etarget, 8);
2709fffb55fSDavid Gibson 
2719fffb55fSDavid Gibson 		emit->data(etarget, prop->val);
2729fffb55fSDavid Gibson 		emit->align(etarget, sizeof(cell_t));
2739fffb55fSDavid Gibson 	}
2749fffb55fSDavid Gibson 
2759fffb55fSDavid Gibson 	if ((vi->flags & FTF_NAMEPROPS) && !seen_name_prop) {
2769fffb55fSDavid Gibson 		emit->property(etarget, NULL);
2779fffb55fSDavid Gibson 		emit->cell(etarget, tree->basenamelen+1);
2789fffb55fSDavid Gibson 		emit->cell(etarget, stringtable_insert(strbuf, "name"));
2799fffb55fSDavid Gibson 
2809fffb55fSDavid Gibson 		if ((vi->flags & FTF_VARALIGN) && ((tree->basenamelen+1) >= 8))
2819fffb55fSDavid Gibson 			emit->align(etarget, 8);
2829fffb55fSDavid Gibson 
2839fffb55fSDavid Gibson 		emit->string(etarget, tree->name, tree->basenamelen);
2849fffb55fSDavid Gibson 		emit->align(etarget, sizeof(cell_t));
2859fffb55fSDavid Gibson 	}
2869fffb55fSDavid Gibson 
2879fffb55fSDavid Gibson 	for_each_child(tree, child) {
2889fffb55fSDavid Gibson 		flatten_tree(child, emit, etarget, strbuf, vi);
2899fffb55fSDavid Gibson 	}
2909fffb55fSDavid Gibson 
291658f29a5SJohn Bonesio 	emit->endnode(etarget, tree->labels);
2929fffb55fSDavid Gibson }
2939fffb55fSDavid Gibson 
flatten_reserve_list(struct reserve_info * reservelist,struct version_info * vi)2949fffb55fSDavid Gibson static struct data flatten_reserve_list(struct reserve_info *reservelist,
2959fffb55fSDavid Gibson 				 struct version_info *vi)
2969fffb55fSDavid Gibson {
2979fffb55fSDavid Gibson 	struct reserve_info *re;
2989fffb55fSDavid Gibson 	struct data d = empty_data;
299*a77725a9SRob Herring 	unsigned int j;
3009fffb55fSDavid Gibson 
3019fffb55fSDavid Gibson 	for (re = reservelist; re; re = re->next) {
30289d12310SRob Herring 		d = data_append_re(d, re->address, re->size);
3039fffb55fSDavid Gibson 	}
3049fffb55fSDavid Gibson 	/*
3059fffb55fSDavid Gibson 	 * Add additional reserved slots if the user asked for them.
3069fffb55fSDavid Gibson 	 */
3079fffb55fSDavid Gibson 	for (j = 0; j < reservenum; j++) {
30889d12310SRob Herring 		d = data_append_re(d, 0, 0);
3099fffb55fSDavid Gibson 	}
3109fffb55fSDavid Gibson 
3119fffb55fSDavid Gibson 	return d;
3129fffb55fSDavid Gibson }
3139fffb55fSDavid Gibson 
make_fdt_header(struct fdt_header * fdt,struct version_info * vi,int reservesize,int dtsize,int strsize,int boot_cpuid_phys)3149fffb55fSDavid Gibson static void make_fdt_header(struct fdt_header *fdt,
3159fffb55fSDavid Gibson 			    struct version_info *vi,
3169fffb55fSDavid Gibson 			    int reservesize, int dtsize, int strsize,
3179fffb55fSDavid Gibson 			    int boot_cpuid_phys)
3189fffb55fSDavid Gibson {
3199fffb55fSDavid Gibson 	int reserve_off;
3209fffb55fSDavid Gibson 
3219fffb55fSDavid Gibson 	reservesize += sizeof(struct fdt_reserve_entry);
3229fffb55fSDavid Gibson 
3239fffb55fSDavid Gibson 	memset(fdt, 0xff, sizeof(*fdt));
3249fffb55fSDavid Gibson 
3259fffb55fSDavid Gibson 	fdt->magic = cpu_to_fdt32(FDT_MAGIC);
3269fffb55fSDavid Gibson 	fdt->version = cpu_to_fdt32(vi->version);
3279fffb55fSDavid Gibson 	fdt->last_comp_version = cpu_to_fdt32(vi->last_comp_version);
3289fffb55fSDavid Gibson 
3299fffb55fSDavid Gibson 	/* Reserve map should be doubleword aligned */
3309fffb55fSDavid Gibson 	reserve_off = ALIGN(vi->hdr_size, 8);
3319fffb55fSDavid Gibson 
3329fffb55fSDavid Gibson 	fdt->off_mem_rsvmap = cpu_to_fdt32(reserve_off);
3339fffb55fSDavid Gibson 	fdt->off_dt_struct = cpu_to_fdt32(reserve_off + reservesize);
3349fffb55fSDavid Gibson 	fdt->off_dt_strings = cpu_to_fdt32(reserve_off + reservesize
3359fffb55fSDavid Gibson 					  + dtsize);
3369fffb55fSDavid Gibson 	fdt->totalsize = cpu_to_fdt32(reserve_off + reservesize + dtsize + strsize);
3379fffb55fSDavid Gibson 
3389fffb55fSDavid Gibson 	if (vi->flags & FTF_BOOTCPUID)
3399fffb55fSDavid Gibson 		fdt->boot_cpuid_phys = cpu_to_fdt32(boot_cpuid_phys);
3409fffb55fSDavid Gibson 	if (vi->flags & FTF_STRTABSIZE)
3419fffb55fSDavid Gibson 		fdt->size_dt_strings = cpu_to_fdt32(strsize);
3429fffb55fSDavid Gibson 	if (vi->flags & FTF_STRUCTSIZE)
3439fffb55fSDavid Gibson 		fdt->size_dt_struct = cpu_to_fdt32(dtsize);
3449fffb55fSDavid Gibson }
3459fffb55fSDavid Gibson 
dt_to_blob(FILE * f,struct dt_info * dti,int version)3466f05afcbSRob Herring void dt_to_blob(FILE *f, struct dt_info *dti, int version)
3479fffb55fSDavid Gibson {
3489fffb55fSDavid Gibson 	struct version_info *vi = NULL;
34979edff12SRob Herring 	unsigned int i;
3509fffb55fSDavid Gibson 	struct data blob       = empty_data;
3519fffb55fSDavid Gibson 	struct data reservebuf = empty_data;
3529fffb55fSDavid Gibson 	struct data dtbuf      = empty_data;
3539fffb55fSDavid Gibson 	struct data strbuf     = empty_data;
3549fffb55fSDavid Gibson 	struct fdt_header fdt;
3559fffb55fSDavid Gibson 	int padlen = 0;
3569fffb55fSDavid Gibson 
3579fffb55fSDavid Gibson 	for (i = 0; i < ARRAY_SIZE(version_table); i++) {
3589fffb55fSDavid Gibson 		if (version_table[i].version == version)
3599fffb55fSDavid Gibson 			vi = &version_table[i];
3609fffb55fSDavid Gibson 	}
3619fffb55fSDavid Gibson 	if (!vi)
3629fffb55fSDavid Gibson 		die("Unknown device tree blob version %d\n", version);
3639fffb55fSDavid Gibson 
3646f05afcbSRob Herring 	flatten_tree(dti->dt, &bin_emitter, &dtbuf, &strbuf, vi);
3659fffb55fSDavid Gibson 	bin_emit_cell(&dtbuf, FDT_END);
3669fffb55fSDavid Gibson 
3676f05afcbSRob Herring 	reservebuf = flatten_reserve_list(dti->reservelist, vi);
3689fffb55fSDavid Gibson 
3699fffb55fSDavid Gibson 	/* Make header */
3709fffb55fSDavid Gibson 	make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len,
3716f05afcbSRob Herring 			dti->boot_cpuid_phys);
3729fffb55fSDavid Gibson 
3739fffb55fSDavid Gibson 	/*
3749fffb55fSDavid Gibson 	 * If the user asked for more space than is used, adjust the totalsize.
3759fffb55fSDavid Gibson 	 */
3769fffb55fSDavid Gibson 	if (minsize > 0) {
3779fffb55fSDavid Gibson 		padlen = minsize - fdt32_to_cpu(fdt.totalsize);
3786f05afcbSRob Herring 		if (padlen < 0) {
3796f05afcbSRob Herring 			padlen = 0;
3806f05afcbSRob Herring 			if (quiet < 1)
3819fffb55fSDavid Gibson 				fprintf(stderr,
382f858927fSRob Herring 					"Warning: blob size %"PRIu32" >= minimum size %d\n",
3839fffb55fSDavid Gibson 					fdt32_to_cpu(fdt.totalsize), minsize);
3849fffb55fSDavid Gibson 		}
3856f05afcbSRob Herring 	}
3869fffb55fSDavid Gibson 
3879fffb55fSDavid Gibson 	if (padsize > 0)
3889fffb55fSDavid Gibson 		padlen = padsize;
3899fffb55fSDavid Gibson 
3906f05afcbSRob Herring 	if (alignsize > 0)
3916f05afcbSRob Herring 		padlen = ALIGN(fdt32_to_cpu(fdt.totalsize) + padlen, alignsize)
3926f05afcbSRob Herring 			- fdt32_to_cpu(fdt.totalsize);
3936f05afcbSRob Herring 
3949fffb55fSDavid Gibson 	if (padlen > 0) {
3959fffb55fSDavid Gibson 		int tsize = fdt32_to_cpu(fdt.totalsize);
3969fffb55fSDavid Gibson 		tsize += padlen;
3979fffb55fSDavid Gibson 		fdt.totalsize = cpu_to_fdt32(tsize);
3989fffb55fSDavid Gibson 	}
3999fffb55fSDavid Gibson 
4009fffb55fSDavid Gibson 	/*
4019fffb55fSDavid Gibson 	 * Assemble the blob: start with the header, add with alignment
4029fffb55fSDavid Gibson 	 * the reserve buffer, add the reserve map terminating zeroes,
4039fffb55fSDavid Gibson 	 * the device tree itself, and finally the strings.
4049fffb55fSDavid Gibson 	 */
4059fffb55fSDavid Gibson 	blob = data_append_data(blob, &fdt, vi->hdr_size);
4069fffb55fSDavid Gibson 	blob = data_append_align(blob, 8);
4079fffb55fSDavid Gibson 	blob = data_merge(blob, reservebuf);
4089fffb55fSDavid Gibson 	blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry));
4099fffb55fSDavid Gibson 	blob = data_merge(blob, dtbuf);
4109fffb55fSDavid Gibson 	blob = data_merge(blob, strbuf);
4119fffb55fSDavid Gibson 
4129fffb55fSDavid Gibson 	/*
4139fffb55fSDavid Gibson 	 * If the user asked for more space than is used, pad out the blob.
4149fffb55fSDavid Gibson 	 */
4159fffb55fSDavid Gibson 	if (padlen > 0)
4169fffb55fSDavid Gibson 		blob = data_append_zeroes(blob, padlen);
4179fffb55fSDavid Gibson 
418658f29a5SJohn Bonesio 	if (fwrite(blob.val, blob.len, 1, f) != 1) {
4199fffb55fSDavid Gibson 		if (ferror(f))
420658f29a5SJohn Bonesio 			die("Error writing device tree blob: %s\n",
421658f29a5SJohn Bonesio 			    strerror(errno));
422658f29a5SJohn Bonesio 		else
423658f29a5SJohn Bonesio 			die("Short write on device tree blob\n");
424658f29a5SJohn Bonesio 	}
4259fffb55fSDavid Gibson 
4269fffb55fSDavid Gibson 	/*
4279fffb55fSDavid Gibson 	 * data_merge() frees the right-hand element so only the blob
4289fffb55fSDavid Gibson 	 * remains to be freed.
4299fffb55fSDavid Gibson 	 */
4309fffb55fSDavid Gibson 	data_free(blob);
4319fffb55fSDavid Gibson }
4329fffb55fSDavid Gibson 
dump_stringtable_asm(FILE * f,struct data strbuf)4339fffb55fSDavid Gibson static void dump_stringtable_asm(FILE *f, struct data strbuf)
4349fffb55fSDavid Gibson {
4359fffb55fSDavid Gibson 	const char *p;
4369fffb55fSDavid Gibson 	int len;
4379fffb55fSDavid Gibson 
4389fffb55fSDavid Gibson 	p = strbuf.val;
4399fffb55fSDavid Gibson 
4409fffb55fSDavid Gibson 	while (p < (strbuf.val + strbuf.len)) {
4419fffb55fSDavid Gibson 		len = strlen(p);
442*a77725a9SRob Herring 		fprintf(f, "\t.asciz \"%s\"\n", p);
4439fffb55fSDavid Gibson 		p += len+1;
4449fffb55fSDavid Gibson 	}
4459fffb55fSDavid Gibson }
4469fffb55fSDavid Gibson 
dt_to_asm(FILE * f,struct dt_info * dti,int version)4476f05afcbSRob Herring void dt_to_asm(FILE *f, struct dt_info *dti, int version)
4489fffb55fSDavid Gibson {
4499fffb55fSDavid Gibson 	struct version_info *vi = NULL;
45079edff12SRob Herring 	unsigned int i;
4519fffb55fSDavid Gibson 	struct data strbuf = empty_data;
4529fffb55fSDavid Gibson 	struct reserve_info *re;
4539fffb55fSDavid Gibson 	const char *symprefix = "dt";
4549fffb55fSDavid Gibson 
4559fffb55fSDavid Gibson 	for (i = 0; i < ARRAY_SIZE(version_table); i++) {
4569fffb55fSDavid Gibson 		if (version_table[i].version == version)
4579fffb55fSDavid Gibson 			vi = &version_table[i];
4589fffb55fSDavid Gibson 	}
4599fffb55fSDavid Gibson 	if (!vi)
4609fffb55fSDavid Gibson 		die("Unknown device tree blob version %d\n", version);
4619fffb55fSDavid Gibson 
4629fffb55fSDavid Gibson 	fprintf(f, "/* autogenerated by dtc, do not edit */\n\n");
4639fffb55fSDavid Gibson 
4649fffb55fSDavid Gibson 	emit_label(f, symprefix, "blob_start");
4659fffb55fSDavid Gibson 	emit_label(f, symprefix, "header");
466658f29a5SJohn Bonesio 	fprintf(f, "\t/* magic */\n");
467658f29a5SJohn Bonesio 	asm_emit_cell(f, FDT_MAGIC);
468658f29a5SJohn Bonesio 	fprintf(f, "\t/* totalsize */\n");
469658f29a5SJohn Bonesio 	ASM_EMIT_BELONG(f, "_%s_blob_abs_end - _%s_blob_start",
4709fffb55fSDavid Gibson 			symprefix, symprefix);
471658f29a5SJohn Bonesio 	fprintf(f, "\t/* off_dt_struct */\n");
472658f29a5SJohn Bonesio 	ASM_EMIT_BELONG(f, "_%s_struct_start - _%s_blob_start",
4739fffb55fSDavid Gibson 		symprefix, symprefix);
474658f29a5SJohn Bonesio 	fprintf(f, "\t/* off_dt_strings */\n");
475658f29a5SJohn Bonesio 	ASM_EMIT_BELONG(f, "_%s_strings_start - _%s_blob_start",
4769fffb55fSDavid Gibson 		symprefix, symprefix);
477658f29a5SJohn Bonesio 	fprintf(f, "\t/* off_mem_rsvmap */\n");
478658f29a5SJohn Bonesio 	ASM_EMIT_BELONG(f, "_%s_reserve_map - _%s_blob_start",
4799fffb55fSDavid Gibson 		symprefix, symprefix);
480658f29a5SJohn Bonesio 	fprintf(f, "\t/* version */\n");
481658f29a5SJohn Bonesio 	asm_emit_cell(f, vi->version);
482658f29a5SJohn Bonesio 	fprintf(f, "\t/* last_comp_version */\n");
483658f29a5SJohn Bonesio 	asm_emit_cell(f, vi->last_comp_version);
4849fffb55fSDavid Gibson 
485658f29a5SJohn Bonesio 	if (vi->flags & FTF_BOOTCPUID) {
486658f29a5SJohn Bonesio 		fprintf(f, "\t/* boot_cpuid_phys */\n");
4876f05afcbSRob Herring 		asm_emit_cell(f, dti->boot_cpuid_phys);
488658f29a5SJohn Bonesio 	}
4899fffb55fSDavid Gibson 
490658f29a5SJohn Bonesio 	if (vi->flags & FTF_STRTABSIZE) {
491658f29a5SJohn Bonesio 		fprintf(f, "\t/* size_dt_strings */\n");
492658f29a5SJohn Bonesio 		ASM_EMIT_BELONG(f, "_%s_strings_end - _%s_strings_start",
4939fffb55fSDavid Gibson 				symprefix, symprefix);
494658f29a5SJohn Bonesio 	}
4959fffb55fSDavid Gibson 
496658f29a5SJohn Bonesio 	if (vi->flags & FTF_STRUCTSIZE) {
497658f29a5SJohn Bonesio 		fprintf(f, "\t/* size_dt_struct */\n");
498658f29a5SJohn Bonesio 		ASM_EMIT_BELONG(f, "_%s_struct_end - _%s_struct_start",
4999fffb55fSDavid Gibson 			symprefix, symprefix);
500658f29a5SJohn Bonesio 	}
5019fffb55fSDavid Gibson 
5029fffb55fSDavid Gibson 	/*
5039fffb55fSDavid Gibson 	 * Reserve map entries.
5049fffb55fSDavid Gibson 	 * Align the reserve map to a doubleword boundary.
5059fffb55fSDavid Gibson 	 * Each entry is an (address, size) pair of u64 values.
5069fffb55fSDavid Gibson 	 * Always supply a zero-sized temination entry.
5079fffb55fSDavid Gibson 	 */
5089fffb55fSDavid Gibson 	asm_emit_align(f, 8);
5099fffb55fSDavid Gibson 	emit_label(f, symprefix, "reserve_map");
5109fffb55fSDavid Gibson 
5119fffb55fSDavid Gibson 	fprintf(f, "/* Memory reserve map from source file */\n");
5129fffb55fSDavid Gibson 
5139fffb55fSDavid Gibson 	/*
5149bb9c6a1SRob Herring 	 * Use .long on high and low halves of u64s to avoid .quad
5159fffb55fSDavid Gibson 	 * as it appears .quad isn't available in some assemblers.
5169fffb55fSDavid Gibson 	 */
5176f05afcbSRob Herring 	for (re = dti->reservelist; re; re = re->next) {
518658f29a5SJohn Bonesio 		struct label *l;
519658f29a5SJohn Bonesio 
520658f29a5SJohn Bonesio 		for_each_label(re->labels, l) {
521658f29a5SJohn Bonesio 			fprintf(f, "\t.globl\t%s\n", l->label);
522658f29a5SJohn Bonesio 			fprintf(f, "%s:\n", l->label);
5239fffb55fSDavid Gibson 		}
52489d12310SRob Herring 		ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->address >> 32));
525658f29a5SJohn Bonesio 		ASM_EMIT_BELONG(f, "0x%08x",
52689d12310SRob Herring 				(unsigned int)(re->address & 0xffffffff));
52789d12310SRob Herring 		ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->size >> 32));
52889d12310SRob Herring 		ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->size & 0xffffffff));
5299fffb55fSDavid Gibson 	}
5309fffb55fSDavid Gibson 	for (i = 0; i < reservenum; i++) {
5319fffb55fSDavid Gibson 		fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
5329fffb55fSDavid Gibson 	}
5339fffb55fSDavid Gibson 
5349fffb55fSDavid Gibson 	fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
5359fffb55fSDavid Gibson 
5369fffb55fSDavid Gibson 	emit_label(f, symprefix, "struct_start");
5376f05afcbSRob Herring 	flatten_tree(dti->dt, &asm_emitter, f, &strbuf, vi);
538658f29a5SJohn Bonesio 
539658f29a5SJohn Bonesio 	fprintf(f, "\t/* FDT_END */\n");
540658f29a5SJohn Bonesio 	asm_emit_cell(f, FDT_END);
5419fffb55fSDavid Gibson 	emit_label(f, symprefix, "struct_end");
5429fffb55fSDavid Gibson 
5439fffb55fSDavid Gibson 	emit_label(f, symprefix, "strings_start");
5449fffb55fSDavid Gibson 	dump_stringtable_asm(f, strbuf);
5459fffb55fSDavid Gibson 	emit_label(f, symprefix, "strings_end");
5469fffb55fSDavid Gibson 
5479fffb55fSDavid Gibson 	emit_label(f, symprefix, "blob_end");
5489fffb55fSDavid Gibson 
5499fffb55fSDavid Gibson 	/*
5509fffb55fSDavid Gibson 	 * If the user asked for more space than is used, pad it out.
5519fffb55fSDavid Gibson 	 */
5529fffb55fSDavid Gibson 	if (minsize > 0) {
5539fffb55fSDavid Gibson 		fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n",
5549fffb55fSDavid Gibson 			minsize, symprefix, symprefix);
5559fffb55fSDavid Gibson 	}
5569fffb55fSDavid Gibson 	if (padsize > 0) {
5579fffb55fSDavid Gibson 		fprintf(f, "\t.space\t%d, 0\n", padsize);
5589fffb55fSDavid Gibson 	}
5596f05afcbSRob Herring 	if (alignsize > 0)
5606f05afcbSRob Herring 		asm_emit_align(f, alignsize);
5619fffb55fSDavid Gibson 	emit_label(f, symprefix, "blob_abs_end");
5629fffb55fSDavid Gibson 
5639fffb55fSDavid Gibson 	data_free(strbuf);
5649fffb55fSDavid Gibson }
5659fffb55fSDavid Gibson 
5669fffb55fSDavid Gibson struct inbuf {
5679fffb55fSDavid Gibson 	char *base, *limit, *ptr;
5689fffb55fSDavid Gibson };
5699fffb55fSDavid Gibson 
inbuf_init(struct inbuf * inb,void * base,void * limit)5709fffb55fSDavid Gibson static void inbuf_init(struct inbuf *inb, void *base, void *limit)
5719fffb55fSDavid Gibson {
5729fffb55fSDavid Gibson 	inb->base = base;
5739fffb55fSDavid Gibson 	inb->limit = limit;
5749fffb55fSDavid Gibson 	inb->ptr = inb->base;
5759fffb55fSDavid Gibson }
5769fffb55fSDavid Gibson 
flat_read_chunk(struct inbuf * inb,void * p,int len)5779fffb55fSDavid Gibson static void flat_read_chunk(struct inbuf *inb, void *p, int len)
5789fffb55fSDavid Gibson {
5799fffb55fSDavid Gibson 	if ((inb->ptr + len) > inb->limit)
5809fffb55fSDavid Gibson 		die("Premature end of data parsing flat device tree\n");
5819fffb55fSDavid Gibson 
5829fffb55fSDavid Gibson 	memcpy(p, inb->ptr, len);
5839fffb55fSDavid Gibson 
5849fffb55fSDavid Gibson 	inb->ptr += len;
5859fffb55fSDavid Gibson }
5869fffb55fSDavid Gibson 
flat_read_word(struct inbuf * inb)5879fffb55fSDavid Gibson static uint32_t flat_read_word(struct inbuf *inb)
5889fffb55fSDavid Gibson {
58989d12310SRob Herring 	fdt32_t val;
5909fffb55fSDavid Gibson 
5919fffb55fSDavid Gibson 	assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
5929fffb55fSDavid Gibson 
5939fffb55fSDavid Gibson 	flat_read_chunk(inb, &val, sizeof(val));
5949fffb55fSDavid Gibson 
5959fffb55fSDavid Gibson 	return fdt32_to_cpu(val);
5969fffb55fSDavid Gibson }
5979fffb55fSDavid Gibson 
flat_realign(struct inbuf * inb,int align)5989fffb55fSDavid Gibson static void flat_realign(struct inbuf *inb, int align)
5999fffb55fSDavid Gibson {
6009fffb55fSDavid Gibson 	int off = inb->ptr - inb->base;
6019fffb55fSDavid Gibson 
6029fffb55fSDavid Gibson 	inb->ptr = inb->base + ALIGN(off, align);
6039fffb55fSDavid Gibson 	if (inb->ptr > inb->limit)
6049fffb55fSDavid Gibson 		die("Premature end of data parsing flat device tree\n");
6059fffb55fSDavid Gibson }
6069fffb55fSDavid Gibson 
flat_read_string(struct inbuf * inb)6079fffb55fSDavid Gibson static char *flat_read_string(struct inbuf *inb)
6089fffb55fSDavid Gibson {
6099fffb55fSDavid Gibson 	int len = 0;
6109fffb55fSDavid Gibson 	const char *p = inb->ptr;
6119fffb55fSDavid Gibson 	char *str;
6129fffb55fSDavid Gibson 
6139fffb55fSDavid Gibson 	do {
6149fffb55fSDavid Gibson 		if (p >= inb->limit)
6159fffb55fSDavid Gibson 			die("Premature end of data parsing flat device tree\n");
6169fffb55fSDavid Gibson 		len++;
6179fffb55fSDavid Gibson 	} while ((*p++) != '\0');
6189fffb55fSDavid Gibson 
619658f29a5SJohn Bonesio 	str = xstrdup(inb->ptr);
6209fffb55fSDavid Gibson 
6219fffb55fSDavid Gibson 	inb->ptr += len;
6229fffb55fSDavid Gibson 
6239fffb55fSDavid Gibson 	flat_realign(inb, sizeof(uint32_t));
6249fffb55fSDavid Gibson 
6259fffb55fSDavid Gibson 	return str;
6269fffb55fSDavid Gibson }
6279fffb55fSDavid Gibson 
flat_read_data(struct inbuf * inb,int len)6289fffb55fSDavid Gibson static struct data flat_read_data(struct inbuf *inb, int len)
6299fffb55fSDavid Gibson {
6309fffb55fSDavid Gibson 	struct data d = empty_data;
6319fffb55fSDavid Gibson 
6329fffb55fSDavid Gibson 	if (len == 0)
6339fffb55fSDavid Gibson 		return empty_data;
6349fffb55fSDavid Gibson 
6359fffb55fSDavid Gibson 	d = data_grow_for(d, len);
6369fffb55fSDavid Gibson 	d.len = len;
6379fffb55fSDavid Gibson 
6389fffb55fSDavid Gibson 	flat_read_chunk(inb, d.val, len);
6399fffb55fSDavid Gibson 
6409fffb55fSDavid Gibson 	flat_realign(inb, sizeof(uint32_t));
6419fffb55fSDavid Gibson 
6429fffb55fSDavid Gibson 	return d;
6439fffb55fSDavid Gibson }
6449fffb55fSDavid Gibson 
flat_read_stringtable(struct inbuf * inb,int offset)6459fffb55fSDavid Gibson static char *flat_read_stringtable(struct inbuf *inb, int offset)
6469fffb55fSDavid Gibson {
6479fffb55fSDavid Gibson 	const char *p;
6489fffb55fSDavid Gibson 
6499fffb55fSDavid Gibson 	p = inb->base + offset;
6509fffb55fSDavid Gibson 	while (1) {
6519fffb55fSDavid Gibson 		if (p >= inb->limit || p < inb->base)
6529fffb55fSDavid Gibson 			die("String offset %d overruns string table\n",
6539fffb55fSDavid Gibson 			    offset);
6549fffb55fSDavid Gibson 
6559fffb55fSDavid Gibson 		if (*p == '\0')
6569fffb55fSDavid Gibson 			break;
6579fffb55fSDavid Gibson 
6589fffb55fSDavid Gibson 		p++;
6599fffb55fSDavid Gibson 	}
6609fffb55fSDavid Gibson 
661658f29a5SJohn Bonesio 	return xstrdup(inb->base + offset);
6629fffb55fSDavid Gibson }
6639fffb55fSDavid Gibson 
flat_read_property(struct inbuf * dtbuf,struct inbuf * strbuf,int flags)6649fffb55fSDavid Gibson static struct property *flat_read_property(struct inbuf *dtbuf,
6659fffb55fSDavid Gibson 					   struct inbuf *strbuf, int flags)
6669fffb55fSDavid Gibson {
6679fffb55fSDavid Gibson 	uint32_t proplen, stroff;
6689fffb55fSDavid Gibson 	char *name;
6699fffb55fSDavid Gibson 	struct data val;
6709fffb55fSDavid Gibson 
6719fffb55fSDavid Gibson 	proplen = flat_read_word(dtbuf);
6729fffb55fSDavid Gibson 	stroff = flat_read_word(dtbuf);
6739fffb55fSDavid Gibson 
6749fffb55fSDavid Gibson 	name = flat_read_stringtable(strbuf, stroff);
6759fffb55fSDavid Gibson 
6769fffb55fSDavid Gibson 	if ((flags & FTF_VARALIGN) && (proplen >= 8))
6779fffb55fSDavid Gibson 		flat_realign(dtbuf, 8);
6789fffb55fSDavid Gibson 
6799fffb55fSDavid Gibson 	val = flat_read_data(dtbuf, proplen);
6809fffb55fSDavid Gibson 
681c2e7075cSRob Herring 	return build_property(name, val, NULL);
6829fffb55fSDavid Gibson }
6839fffb55fSDavid Gibson 
6849fffb55fSDavid Gibson 
flat_read_mem_reserve(struct inbuf * inb)6859fffb55fSDavid Gibson static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
6869fffb55fSDavid Gibson {
6879fffb55fSDavid Gibson 	struct reserve_info *reservelist = NULL;
6889fffb55fSDavid Gibson 	struct reserve_info *new;
6899fffb55fSDavid Gibson 	struct fdt_reserve_entry re;
6909fffb55fSDavid Gibson 
6919fffb55fSDavid Gibson 	/*
6929fffb55fSDavid Gibson 	 * Each entry is a pair of u64 (addr, size) values for 4 cell_t's.
6939fffb55fSDavid Gibson 	 * List terminates at an entry with size equal to zero.
6949fffb55fSDavid Gibson 	 *
6959fffb55fSDavid Gibson 	 * First pass, count entries.
6969fffb55fSDavid Gibson 	 */
6979fffb55fSDavid Gibson 	while (1) {
69889d12310SRob Herring 		uint64_t address, size;
69989d12310SRob Herring 
7009fffb55fSDavid Gibson 		flat_read_chunk(inb, &re, sizeof(re));
70189d12310SRob Herring 		address  = fdt64_to_cpu(re.address);
70289d12310SRob Herring 		size = fdt64_to_cpu(re.size);
70389d12310SRob Herring 		if (size == 0)
7049fffb55fSDavid Gibson 			break;
7059fffb55fSDavid Gibson 
70689d12310SRob Herring 		new = build_reserve_entry(address, size);
7079fffb55fSDavid Gibson 		reservelist = add_reserve_entry(reservelist, new);
7089fffb55fSDavid Gibson 	}
7099fffb55fSDavid Gibson 
7109fffb55fSDavid Gibson 	return reservelist;
7119fffb55fSDavid Gibson }
7129fffb55fSDavid Gibson 
7139fffb55fSDavid Gibson 
nodename_from_path(const char * ppath,const char * cpath)7149fffb55fSDavid Gibson static char *nodename_from_path(const char *ppath, const char *cpath)
7159fffb55fSDavid Gibson {
7169fffb55fSDavid Gibson 	int plen;
7179fffb55fSDavid Gibson 
7189fffb55fSDavid Gibson 	plen = strlen(ppath);
7199fffb55fSDavid Gibson 
7209130ba88SRob Herring 	if (!strstarts(cpath, ppath))
7219fffb55fSDavid Gibson 		die("Path \"%s\" is not valid as a child of \"%s\"\n",
7229fffb55fSDavid Gibson 		    cpath, ppath);
7239fffb55fSDavid Gibson 
7249fffb55fSDavid Gibson 	/* root node is a special case */
7259fffb55fSDavid Gibson 	if (!streq(ppath, "/"))
7269fffb55fSDavid Gibson 		plen++;
7279fffb55fSDavid Gibson 
728658f29a5SJohn Bonesio 	return xstrdup(cpath + plen);
7299fffb55fSDavid Gibson }
7309fffb55fSDavid Gibson 
unflatten_tree(struct inbuf * dtbuf,struct inbuf * strbuf,const char * parent_flatname,int flags)7319fffb55fSDavid Gibson static struct node *unflatten_tree(struct inbuf *dtbuf,
7329fffb55fSDavid Gibson 				   struct inbuf *strbuf,
7339fffb55fSDavid Gibson 				   const char *parent_flatname, int flags)
7349fffb55fSDavid Gibson {
7359fffb55fSDavid Gibson 	struct node *node;
7369fffb55fSDavid Gibson 	char *flatname;
7379fffb55fSDavid Gibson 	uint32_t val;
7389fffb55fSDavid Gibson 
739c2e7075cSRob Herring 	node = build_node(NULL, NULL, NULL);
7409fffb55fSDavid Gibson 
7419fffb55fSDavid Gibson 	flatname = flat_read_string(dtbuf);
7429fffb55fSDavid Gibson 
7439fffb55fSDavid Gibson 	if (flags & FTF_FULLPATH)
7449fffb55fSDavid Gibson 		node->name = nodename_from_path(parent_flatname, flatname);
7459fffb55fSDavid Gibson 	else
7469fffb55fSDavid Gibson 		node->name = flatname;
7479fffb55fSDavid Gibson 
7489fffb55fSDavid Gibson 	do {
7499fffb55fSDavid Gibson 		struct property *prop;
7509fffb55fSDavid Gibson 		struct node *child;
7519fffb55fSDavid Gibson 
7529fffb55fSDavid Gibson 		val = flat_read_word(dtbuf);
7539fffb55fSDavid Gibson 		switch (val) {
7549fffb55fSDavid Gibson 		case FDT_PROP:
7559fffb55fSDavid Gibson 			if (node->children)
7569fffb55fSDavid Gibson 				fprintf(stderr, "Warning: Flat tree input has "
7579fffb55fSDavid Gibson 					"subnodes preceding a property.\n");
7589fffb55fSDavid Gibson 			prop = flat_read_property(dtbuf, strbuf, flags);
7599fffb55fSDavid Gibson 			add_property(node, prop);
7609fffb55fSDavid Gibson 			break;
7619fffb55fSDavid Gibson 
7629fffb55fSDavid Gibson 		case FDT_BEGIN_NODE:
7639fffb55fSDavid Gibson 			child = unflatten_tree(dtbuf,strbuf, flatname, flags);
7649fffb55fSDavid Gibson 			add_child(node, child);
7659fffb55fSDavid Gibson 			break;
7669fffb55fSDavid Gibson 
7679fffb55fSDavid Gibson 		case FDT_END_NODE:
7689fffb55fSDavid Gibson 			break;
7699fffb55fSDavid Gibson 
7709fffb55fSDavid Gibson 		case FDT_END:
7719fffb55fSDavid Gibson 			die("Premature FDT_END in device tree blob\n");
7729fffb55fSDavid Gibson 			break;
7739fffb55fSDavid Gibson 
7749fffb55fSDavid Gibson 		case FDT_NOP:
7759fffb55fSDavid Gibson 			if (!(flags & FTF_NOPS))
7769fffb55fSDavid Gibson 				fprintf(stderr, "Warning: NOP tag found in flat tree"
7779fffb55fSDavid Gibson 					" version <16\n");
7789fffb55fSDavid Gibson 
7799fffb55fSDavid Gibson 			/* Ignore */
7809fffb55fSDavid Gibson 			break;
7819fffb55fSDavid Gibson 
7829fffb55fSDavid Gibson 		default:
7839fffb55fSDavid Gibson 			die("Invalid opcode word %08x in device tree blob\n",
7849fffb55fSDavid Gibson 			    val);
7859fffb55fSDavid Gibson 		}
7869fffb55fSDavid Gibson 	} while (val != FDT_END_NODE);
7879fffb55fSDavid Gibson 
7886f05afcbSRob Herring 	if (node->name != flatname) {
7896f05afcbSRob Herring 		free(flatname);
7906f05afcbSRob Herring 	}
7916f05afcbSRob Herring 
7929fffb55fSDavid Gibson 	return node;
7939fffb55fSDavid Gibson }
7949fffb55fSDavid Gibson 
7959fffb55fSDavid Gibson 
dt_from_blob(const char * fname)7966f05afcbSRob Herring struct dt_info *dt_from_blob(const char *fname)
7979fffb55fSDavid Gibson {
798658f29a5SJohn Bonesio 	FILE *f;
79989d12310SRob Herring 	fdt32_t magic_buf, totalsize_buf;
8009fffb55fSDavid Gibson 	uint32_t magic, totalsize, version, size_dt, boot_cpuid_phys;
8019fffb55fSDavid Gibson 	uint32_t off_dt, off_str, off_mem_rsvmap;
8029fffb55fSDavid Gibson 	int rc;
8039fffb55fSDavid Gibson 	char *blob;
8049fffb55fSDavid Gibson 	struct fdt_header *fdt;
8059fffb55fSDavid Gibson 	char *p;
8069fffb55fSDavid Gibson 	struct inbuf dtbuf, strbuf;
8079fffb55fSDavid Gibson 	struct inbuf memresvbuf;
8089fffb55fSDavid Gibson 	int sizeleft;
8099fffb55fSDavid Gibson 	struct reserve_info *reservelist;
8109fffb55fSDavid Gibson 	struct node *tree;
8119fffb55fSDavid Gibson 	uint32_t val;
8129fffb55fSDavid Gibson 	int flags = 0;
8139fffb55fSDavid Gibson 
814658f29a5SJohn Bonesio 	f = srcfile_relative_open(fname, NULL);
8159fffb55fSDavid Gibson 
81689d12310SRob Herring 	rc = fread(&magic_buf, sizeof(magic_buf), 1, f);
817658f29a5SJohn Bonesio 	if (ferror(f))
8189fffb55fSDavid Gibson 		die("Error reading DT blob magic number: %s\n",
8199fffb55fSDavid Gibson 		    strerror(errno));
8209fffb55fSDavid Gibson 	if (rc < 1) {
821658f29a5SJohn Bonesio 		if (feof(f))
8229fffb55fSDavid Gibson 			die("EOF reading DT blob magic number\n");
8239fffb55fSDavid Gibson 		else
8249fffb55fSDavid Gibson 			die("Mysterious short read reading magic number\n");
8259fffb55fSDavid Gibson 	}
8269fffb55fSDavid Gibson 
82789d12310SRob Herring 	magic = fdt32_to_cpu(magic_buf);
8289fffb55fSDavid Gibson 	if (magic != FDT_MAGIC)
8299fffb55fSDavid Gibson 		die("Blob has incorrect magic number\n");
8309fffb55fSDavid Gibson 
83189d12310SRob Herring 	rc = fread(&totalsize_buf, sizeof(totalsize_buf), 1, f);
832658f29a5SJohn Bonesio 	if (ferror(f))
8339fffb55fSDavid Gibson 		die("Error reading DT blob size: %s\n", strerror(errno));
8349fffb55fSDavid Gibson 	if (rc < 1) {
835658f29a5SJohn Bonesio 		if (feof(f))
8369fffb55fSDavid Gibson 			die("EOF reading DT blob size\n");
8379fffb55fSDavid Gibson 		else
8389fffb55fSDavid Gibson 			die("Mysterious short read reading blob size\n");
8399fffb55fSDavid Gibson 	}
8409fffb55fSDavid Gibson 
84189d12310SRob Herring 	totalsize = fdt32_to_cpu(totalsize_buf);
8429fffb55fSDavid Gibson 	if (totalsize < FDT_V1_SIZE)
8439fffb55fSDavid Gibson 		die("DT blob size (%d) is too small\n", totalsize);
8449fffb55fSDavid Gibson 
8459fffb55fSDavid Gibson 	blob = xmalloc(totalsize);
8469fffb55fSDavid Gibson 
8479fffb55fSDavid Gibson 	fdt = (struct fdt_header *)blob;
8489fffb55fSDavid Gibson 	fdt->magic = cpu_to_fdt32(magic);
8499fffb55fSDavid Gibson 	fdt->totalsize = cpu_to_fdt32(totalsize);
8509fffb55fSDavid Gibson 
8519fffb55fSDavid Gibson 	sizeleft = totalsize - sizeof(magic) - sizeof(totalsize);
8529fffb55fSDavid Gibson 	p = blob + sizeof(magic)  + sizeof(totalsize);
8539fffb55fSDavid Gibson 
8549fffb55fSDavid Gibson 	while (sizeleft) {
855658f29a5SJohn Bonesio 		if (feof(f))
8569fffb55fSDavid Gibson 			die("EOF before reading %d bytes of DT blob\n",
8579fffb55fSDavid Gibson 			    totalsize);
8589fffb55fSDavid Gibson 
859658f29a5SJohn Bonesio 		rc = fread(p, 1, sizeleft, f);
860658f29a5SJohn Bonesio 		if (ferror(f))
8619fffb55fSDavid Gibson 			die("Error reading DT blob: %s\n",
8629fffb55fSDavid Gibson 			    strerror(errno));
8639fffb55fSDavid Gibson 
8649fffb55fSDavid Gibson 		sizeleft -= rc;
8659fffb55fSDavid Gibson 		p += rc;
8669fffb55fSDavid Gibson 	}
8679fffb55fSDavid Gibson 
8689fffb55fSDavid Gibson 	off_dt = fdt32_to_cpu(fdt->off_dt_struct);
8699fffb55fSDavid Gibson 	off_str = fdt32_to_cpu(fdt->off_dt_strings);
8709fffb55fSDavid Gibson 	off_mem_rsvmap = fdt32_to_cpu(fdt->off_mem_rsvmap);
8719fffb55fSDavid Gibson 	version = fdt32_to_cpu(fdt->version);
8729fffb55fSDavid Gibson 	boot_cpuid_phys = fdt32_to_cpu(fdt->boot_cpuid_phys);
8739fffb55fSDavid Gibson 
8749fffb55fSDavid Gibson 	if (off_mem_rsvmap >= totalsize)
8759fffb55fSDavid Gibson 		die("Mem Reserve structure offset exceeds total size\n");
8769fffb55fSDavid Gibson 
8779fffb55fSDavid Gibson 	if (off_dt >= totalsize)
8789fffb55fSDavid Gibson 		die("DT structure offset exceeds total size\n");
8799fffb55fSDavid Gibson 
8809fffb55fSDavid Gibson 	if (off_str > totalsize)
8819fffb55fSDavid Gibson 		die("String table offset exceeds total size\n");
8829fffb55fSDavid Gibson 
8839fffb55fSDavid Gibson 	if (version >= 3) {
8849fffb55fSDavid Gibson 		uint32_t size_str = fdt32_to_cpu(fdt->size_dt_strings);
885b9937347SRob Herring 		if ((off_str+size_str < off_str) || (off_str+size_str > totalsize))
8869fffb55fSDavid Gibson 			die("String table extends past total size\n");
8879fffb55fSDavid Gibson 		inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str);
8889fffb55fSDavid Gibson 	} else {
8899fffb55fSDavid Gibson 		inbuf_init(&strbuf, blob + off_str, blob + totalsize);
8909fffb55fSDavid Gibson 	}
8919fffb55fSDavid Gibson 
8929fffb55fSDavid Gibson 	if (version >= 17) {
8939fffb55fSDavid Gibson 		size_dt = fdt32_to_cpu(fdt->size_dt_struct);
894b9937347SRob Herring 		if ((off_dt+size_dt < off_dt) || (off_dt+size_dt > totalsize))
8959fffb55fSDavid Gibson 			die("Structure block extends past total size\n");
8969fffb55fSDavid Gibson 	}
8979fffb55fSDavid Gibson 
8989fffb55fSDavid Gibson 	if (version < 16) {
8999fffb55fSDavid Gibson 		flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN;
9009fffb55fSDavid Gibson 	} else {
9019fffb55fSDavid Gibson 		flags |= FTF_NOPS;
9029fffb55fSDavid Gibson 	}
9039fffb55fSDavid Gibson 
9049fffb55fSDavid Gibson 	inbuf_init(&memresvbuf,
9059fffb55fSDavid Gibson 		   blob + off_mem_rsvmap, blob + totalsize);
9069fffb55fSDavid Gibson 	inbuf_init(&dtbuf, blob + off_dt, blob + totalsize);
9079fffb55fSDavid Gibson 
9089fffb55fSDavid Gibson 	reservelist = flat_read_mem_reserve(&memresvbuf);
9099fffb55fSDavid Gibson 
9109fffb55fSDavid Gibson 	val = flat_read_word(&dtbuf);
9119fffb55fSDavid Gibson 
9129fffb55fSDavid Gibson 	if (val != FDT_BEGIN_NODE)
9139fffb55fSDavid Gibson 		die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val);
9149fffb55fSDavid Gibson 
9159fffb55fSDavid Gibson 	tree = unflatten_tree(&dtbuf, &strbuf, "", flags);
9169fffb55fSDavid Gibson 
9179fffb55fSDavid Gibson 	val = flat_read_word(&dtbuf);
9189fffb55fSDavid Gibson 	if (val != FDT_END)
9199fffb55fSDavid Gibson 		die("Device tree blob doesn't end with FDT_END\n");
9209fffb55fSDavid Gibson 
9219fffb55fSDavid Gibson 	free(blob);
9229fffb55fSDavid Gibson 
923658f29a5SJohn Bonesio 	fclose(f);
9249fffb55fSDavid Gibson 
9256f05afcbSRob Herring 	return build_dt_info(DTSF_V1, reservelist, tree, boot_cpuid_phys);
9269fffb55fSDavid Gibson }
927