xref: /openbmc/linux/scripts/dtc/flattree.c (revision 6f05afcbb031722ec1eff77dde188ff2edf8940e)
19fffb55fSDavid Gibson /*
29fffb55fSDavid Gibson  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
39fffb55fSDavid Gibson  *
49fffb55fSDavid Gibson  *
59fffb55fSDavid Gibson  * This program is free software; you can redistribute it and/or
69fffb55fSDavid Gibson  * modify it under the terms of the GNU General Public License as
79fffb55fSDavid Gibson  * published by the Free Software Foundation; either version 2 of the
89fffb55fSDavid Gibson  * License, or (at your option) any later version.
99fffb55fSDavid Gibson  *
109fffb55fSDavid Gibson  *  This program is distributed in the hope that it will be useful,
119fffb55fSDavid Gibson  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
129fffb55fSDavid Gibson  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139fffb55fSDavid Gibson  *  General Public License for more details.
149fffb55fSDavid Gibson  *
159fffb55fSDavid Gibson  *  You should have received a copy of the GNU General Public License
169fffb55fSDavid Gibson  *  along with this program; if not, write to the Free Software
179fffb55fSDavid Gibson  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
189fffb55fSDavid Gibson  *                                                                   USA
199fffb55fSDavid Gibson  */
209fffb55fSDavid Gibson 
219fffb55fSDavid Gibson #include "dtc.h"
229fffb55fSDavid Gibson #include "srcpos.h"
239fffb55fSDavid Gibson 
249fffb55fSDavid Gibson #define FTF_FULLPATH	0x1
259fffb55fSDavid Gibson #define FTF_VARALIGN	0x2
269fffb55fSDavid Gibson #define FTF_NAMEPROPS	0x4
279fffb55fSDavid Gibson #define FTF_BOOTCPUID	0x8
289fffb55fSDavid Gibson #define FTF_STRTABSIZE	0x10
299fffb55fSDavid Gibson #define FTF_STRUCTSIZE	0x20
309fffb55fSDavid Gibson #define FTF_NOPS	0x40
319fffb55fSDavid Gibson 
329fffb55fSDavid Gibson static struct version_info {
339fffb55fSDavid Gibson 	int version;
349fffb55fSDavid Gibson 	int last_comp_version;
359fffb55fSDavid Gibson 	int hdr_size;
369fffb55fSDavid Gibson 	int flags;
379fffb55fSDavid Gibson } version_table[] = {
389fffb55fSDavid Gibson 	{1, 1, FDT_V1_SIZE,
399fffb55fSDavid Gibson 	 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS},
409fffb55fSDavid Gibson 	{2, 1, FDT_V2_SIZE,
419fffb55fSDavid Gibson 	 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID},
429fffb55fSDavid Gibson 	{3, 1, FDT_V3_SIZE,
439fffb55fSDavid Gibson 	 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID|FTF_STRTABSIZE},
449fffb55fSDavid Gibson 	{16, 16, FDT_V3_SIZE,
459fffb55fSDavid Gibson 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_NOPS},
469fffb55fSDavid Gibson 	{17, 16, FDT_V17_SIZE,
479fffb55fSDavid Gibson 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
489fffb55fSDavid Gibson };
499fffb55fSDavid Gibson 
509fffb55fSDavid Gibson struct emitter {
519fffb55fSDavid Gibson 	void (*cell)(void *, cell_t);
529fffb55fSDavid Gibson 	void (*string)(void *, char *, int);
539fffb55fSDavid Gibson 	void (*align)(void *, int);
549fffb55fSDavid Gibson 	void (*data)(void *, struct data);
55658f29a5SJohn Bonesio 	void (*beginnode)(void *, struct label *labels);
56658f29a5SJohn Bonesio 	void (*endnode)(void *, struct label *labels);
57658f29a5SJohn Bonesio 	void (*property)(void *, struct label *labels);
589fffb55fSDavid Gibson };
599fffb55fSDavid Gibson 
609fffb55fSDavid Gibson static void bin_emit_cell(void *e, cell_t val)
619fffb55fSDavid Gibson {
629fffb55fSDavid Gibson 	struct data *dtbuf = e;
639fffb55fSDavid Gibson 
649fffb55fSDavid Gibson 	*dtbuf = data_append_cell(*dtbuf, val);
659fffb55fSDavid Gibson }
669fffb55fSDavid Gibson 
679fffb55fSDavid Gibson static void bin_emit_string(void *e, char *str, int len)
689fffb55fSDavid Gibson {
699fffb55fSDavid Gibson 	struct data *dtbuf = e;
709fffb55fSDavid Gibson 
719fffb55fSDavid Gibson 	if (len == 0)
729fffb55fSDavid Gibson 		len = strlen(str);
739fffb55fSDavid Gibson 
749fffb55fSDavid Gibson 	*dtbuf = data_append_data(*dtbuf, str, len);
759fffb55fSDavid Gibson 	*dtbuf = data_append_byte(*dtbuf, '\0');
769fffb55fSDavid Gibson }
779fffb55fSDavid Gibson 
789fffb55fSDavid Gibson static void bin_emit_align(void *e, int a)
799fffb55fSDavid Gibson {
809fffb55fSDavid Gibson 	struct data *dtbuf = e;
819fffb55fSDavid Gibson 
829fffb55fSDavid Gibson 	*dtbuf = data_append_align(*dtbuf, a);
839fffb55fSDavid Gibson }
849fffb55fSDavid Gibson 
859fffb55fSDavid Gibson static void bin_emit_data(void *e, struct data d)
869fffb55fSDavid Gibson {
879fffb55fSDavid Gibson 	struct data *dtbuf = e;
889fffb55fSDavid Gibson 
899fffb55fSDavid Gibson 	*dtbuf = data_append_data(*dtbuf, d.val, d.len);
909fffb55fSDavid Gibson }
919fffb55fSDavid Gibson 
92658f29a5SJohn Bonesio static void bin_emit_beginnode(void *e, struct label *labels)
939fffb55fSDavid Gibson {
949fffb55fSDavid Gibson 	bin_emit_cell(e, FDT_BEGIN_NODE);
959fffb55fSDavid Gibson }
969fffb55fSDavid Gibson 
97658f29a5SJohn Bonesio static void bin_emit_endnode(void *e, struct label *labels)
989fffb55fSDavid Gibson {
999fffb55fSDavid Gibson 	bin_emit_cell(e, FDT_END_NODE);
1009fffb55fSDavid Gibson }
1019fffb55fSDavid Gibson 
102658f29a5SJohn Bonesio static void bin_emit_property(void *e, struct label *labels)
1039fffb55fSDavid Gibson {
1049fffb55fSDavid Gibson 	bin_emit_cell(e, FDT_PROP);
1059fffb55fSDavid Gibson }
1069fffb55fSDavid Gibson 
1079fffb55fSDavid Gibson static struct emitter bin_emitter = {
1089fffb55fSDavid Gibson 	.cell = bin_emit_cell,
1099fffb55fSDavid Gibson 	.string = bin_emit_string,
1109fffb55fSDavid Gibson 	.align = bin_emit_align,
1119fffb55fSDavid Gibson 	.data = bin_emit_data,
1129fffb55fSDavid Gibson 	.beginnode = bin_emit_beginnode,
1139fffb55fSDavid Gibson 	.endnode = bin_emit_endnode,
1149fffb55fSDavid Gibson 	.property = bin_emit_property,
1159fffb55fSDavid Gibson };
1169fffb55fSDavid Gibson 
1179fffb55fSDavid Gibson static void emit_label(FILE *f, const char *prefix, const char *label)
1189fffb55fSDavid Gibson {
1199fffb55fSDavid Gibson 	fprintf(f, "\t.globl\t%s_%s\n", prefix, label);
1209fffb55fSDavid Gibson 	fprintf(f, "%s_%s:\n", prefix, label);
1219fffb55fSDavid Gibson 	fprintf(f, "_%s_%s:\n", prefix, label);
1229fffb55fSDavid Gibson }
1239fffb55fSDavid Gibson 
1249fffb55fSDavid Gibson static void emit_offset_label(FILE *f, const char *label, int offset)
1259fffb55fSDavid Gibson {
1269fffb55fSDavid Gibson 	fprintf(f, "\t.globl\t%s\n", label);
1279fffb55fSDavid Gibson 	fprintf(f, "%s\t= . + %d\n", label, offset);
1289fffb55fSDavid Gibson }
1299fffb55fSDavid Gibson 
130658f29a5SJohn Bonesio #define ASM_EMIT_BELONG(f, fmt, ...) \
131658f29a5SJohn Bonesio 	{ \
132658f29a5SJohn Bonesio 		fprintf((f), "\t.byte\t((" fmt ") >> 24) & 0xff\n", __VA_ARGS__); \
133658f29a5SJohn Bonesio 		fprintf((f), "\t.byte\t((" fmt ") >> 16) & 0xff\n", __VA_ARGS__); \
134658f29a5SJohn Bonesio 		fprintf((f), "\t.byte\t((" fmt ") >> 8) & 0xff\n", __VA_ARGS__); \
135658f29a5SJohn Bonesio 		fprintf((f), "\t.byte\t(" fmt ") & 0xff\n", __VA_ARGS__); \
136658f29a5SJohn Bonesio 	}
137658f29a5SJohn Bonesio 
1389fffb55fSDavid Gibson static void asm_emit_cell(void *e, cell_t val)
1399fffb55fSDavid Gibson {
1409fffb55fSDavid Gibson 	FILE *f = e;
1419fffb55fSDavid Gibson 
142658f29a5SJohn Bonesio 	fprintf(f, "\t.byte 0x%02x; .byte 0x%02x; .byte 0x%02x; .byte 0x%02x\n",
143658f29a5SJohn Bonesio 		(val >> 24) & 0xff, (val >> 16) & 0xff,
144658f29a5SJohn Bonesio 		(val >> 8) & 0xff, val & 0xff);
1459fffb55fSDavid Gibson }
1469fffb55fSDavid Gibson 
1479fffb55fSDavid Gibson static void asm_emit_string(void *e, char *str, int len)
1489fffb55fSDavid Gibson {
1499fffb55fSDavid Gibson 	FILE *f = e;
1509fffb55fSDavid Gibson 	char c = 0;
1519fffb55fSDavid Gibson 
1529fffb55fSDavid Gibson 	if (len != 0) {
1539fffb55fSDavid Gibson 		/* XXX: ewww */
1549fffb55fSDavid Gibson 		c = str[len];
1559fffb55fSDavid Gibson 		str[len] = '\0';
1569fffb55fSDavid Gibson 	}
1579fffb55fSDavid Gibson 
1589fffb55fSDavid Gibson 	fprintf(f, "\t.string\t\"%s\"\n", str);
1599fffb55fSDavid Gibson 
1609fffb55fSDavid Gibson 	if (len != 0) {
1619fffb55fSDavid Gibson 		str[len] = c;
1629fffb55fSDavid Gibson 	}
1639fffb55fSDavid Gibson }
1649fffb55fSDavid Gibson 
1659fffb55fSDavid Gibson static void asm_emit_align(void *e, int a)
1669fffb55fSDavid Gibson {
1679fffb55fSDavid Gibson 	FILE *f = e;
1689fffb55fSDavid Gibson 
169658f29a5SJohn Bonesio 	fprintf(f, "\t.balign\t%d, 0\n", a);
1709fffb55fSDavid Gibson }
1719fffb55fSDavid Gibson 
1729fffb55fSDavid Gibson static void asm_emit_data(void *e, struct data d)
1739fffb55fSDavid Gibson {
1749fffb55fSDavid Gibson 	FILE *f = e;
1759fffb55fSDavid Gibson 	int off = 0;
1769fffb55fSDavid Gibson 	struct marker *m = d.markers;
1779fffb55fSDavid Gibson 
1789fffb55fSDavid Gibson 	for_each_marker_of_type(m, LABEL)
1799fffb55fSDavid Gibson 		emit_offset_label(f, m->ref, m->offset);
1809fffb55fSDavid Gibson 
1819fffb55fSDavid Gibson 	while ((d.len - off) >= sizeof(uint32_t)) {
182658f29a5SJohn Bonesio 		asm_emit_cell(e, fdt32_to_cpu(*((uint32_t *)(d.val+off))));
1839fffb55fSDavid Gibson 		off += sizeof(uint32_t);
1849fffb55fSDavid Gibson 	}
1859fffb55fSDavid Gibson 
1869fffb55fSDavid Gibson 	while ((d.len - off) >= 1) {
1879fffb55fSDavid Gibson 		fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]);
1889fffb55fSDavid Gibson 		off += 1;
1899fffb55fSDavid Gibson 	}
1909fffb55fSDavid Gibson 
1919fffb55fSDavid Gibson 	assert(off == d.len);
1929fffb55fSDavid Gibson }
1939fffb55fSDavid Gibson 
194658f29a5SJohn Bonesio static void asm_emit_beginnode(void *e, struct label *labels)
1959fffb55fSDavid Gibson {
1969fffb55fSDavid Gibson 	FILE *f = e;
197658f29a5SJohn Bonesio 	struct label *l;
1989fffb55fSDavid Gibson 
199658f29a5SJohn Bonesio 	for_each_label(labels, l) {
200658f29a5SJohn Bonesio 		fprintf(f, "\t.globl\t%s\n", l->label);
201658f29a5SJohn Bonesio 		fprintf(f, "%s:\n", l->label);
2029fffb55fSDavid Gibson 	}
203658f29a5SJohn Bonesio 	fprintf(f, "\t/* FDT_BEGIN_NODE */\n");
204658f29a5SJohn Bonesio 	asm_emit_cell(e, FDT_BEGIN_NODE);
2059fffb55fSDavid Gibson }
2069fffb55fSDavid Gibson 
207658f29a5SJohn Bonesio static void asm_emit_endnode(void *e, struct label *labels)
2089fffb55fSDavid Gibson {
2099fffb55fSDavid Gibson 	FILE *f = e;
210658f29a5SJohn Bonesio 	struct label *l;
2119fffb55fSDavid Gibson 
212658f29a5SJohn Bonesio 	fprintf(f, "\t/* FDT_END_NODE */\n");
213658f29a5SJohn Bonesio 	asm_emit_cell(e, FDT_END_NODE);
214658f29a5SJohn Bonesio 	for_each_label(labels, l) {
215658f29a5SJohn Bonesio 		fprintf(f, "\t.globl\t%s_end\n", l->label);
216658f29a5SJohn Bonesio 		fprintf(f, "%s_end:\n", l->label);
2179fffb55fSDavid Gibson 	}
2189fffb55fSDavid Gibson }
2199fffb55fSDavid Gibson 
220658f29a5SJohn Bonesio static void asm_emit_property(void *e, struct label *labels)
2219fffb55fSDavid Gibson {
2229fffb55fSDavid Gibson 	FILE *f = e;
223658f29a5SJohn Bonesio 	struct label *l;
2249fffb55fSDavid Gibson 
225658f29a5SJohn Bonesio 	for_each_label(labels, l) {
226658f29a5SJohn Bonesio 		fprintf(f, "\t.globl\t%s\n", l->label);
227658f29a5SJohn Bonesio 		fprintf(f, "%s:\n", l->label);
2289fffb55fSDavid Gibson 	}
229658f29a5SJohn Bonesio 	fprintf(f, "\t/* FDT_PROP */\n");
230658f29a5SJohn Bonesio 	asm_emit_cell(e, FDT_PROP);
2319fffb55fSDavid Gibson }
2329fffb55fSDavid Gibson 
2339fffb55fSDavid Gibson static struct emitter asm_emitter = {
2349fffb55fSDavid Gibson 	.cell = asm_emit_cell,
2359fffb55fSDavid Gibson 	.string = asm_emit_string,
2369fffb55fSDavid Gibson 	.align = asm_emit_align,
2379fffb55fSDavid Gibson 	.data = asm_emit_data,
2389fffb55fSDavid Gibson 	.beginnode = asm_emit_beginnode,
2399fffb55fSDavid Gibson 	.endnode = asm_emit_endnode,
2409fffb55fSDavid Gibson 	.property = asm_emit_property,
2419fffb55fSDavid Gibson };
2429fffb55fSDavid Gibson 
2439fffb55fSDavid Gibson static int stringtable_insert(struct data *d, const char *str)
2449fffb55fSDavid Gibson {
2459fffb55fSDavid Gibson 	int i;
2469fffb55fSDavid Gibson 
2479fffb55fSDavid Gibson 	/* FIXME: do this more efficiently? */
2489fffb55fSDavid Gibson 
2499fffb55fSDavid Gibson 	for (i = 0; i < d->len; i++) {
2509fffb55fSDavid Gibson 		if (streq(str, d->val + i))
2519fffb55fSDavid Gibson 			return i;
2529fffb55fSDavid Gibson 	}
2539fffb55fSDavid Gibson 
2549fffb55fSDavid Gibson 	*d = data_append_data(*d, str, strlen(str)+1);
2559fffb55fSDavid Gibson 	return i;
2569fffb55fSDavid Gibson }
2579fffb55fSDavid Gibson 
2589fffb55fSDavid Gibson static void flatten_tree(struct node *tree, struct emitter *emit,
2599fffb55fSDavid Gibson 			 void *etarget, struct data *strbuf,
2609fffb55fSDavid Gibson 			 struct version_info *vi)
2619fffb55fSDavid Gibson {
2629fffb55fSDavid Gibson 	struct property *prop;
2639fffb55fSDavid Gibson 	struct node *child;
26447605971SRob Herring 	bool seen_name_prop = false;
2659fffb55fSDavid Gibson 
266cd296721SStephen Warren 	if (tree->deleted)
267cd296721SStephen Warren 		return;
268cd296721SStephen Warren 
269658f29a5SJohn Bonesio 	emit->beginnode(etarget, tree->labels);
2709fffb55fSDavid Gibson 
2719fffb55fSDavid Gibson 	if (vi->flags & FTF_FULLPATH)
2729fffb55fSDavid Gibson 		emit->string(etarget, tree->fullpath, 0);
2739fffb55fSDavid Gibson 	else
2749fffb55fSDavid Gibson 		emit->string(etarget, tree->name, 0);
2759fffb55fSDavid Gibson 
2769fffb55fSDavid Gibson 	emit->align(etarget, sizeof(cell_t));
2779fffb55fSDavid Gibson 
2789fffb55fSDavid Gibson 	for_each_property(tree, prop) {
2799fffb55fSDavid Gibson 		int nameoff;
2809fffb55fSDavid Gibson 
2819fffb55fSDavid Gibson 		if (streq(prop->name, "name"))
28247605971SRob Herring 			seen_name_prop = true;
2839fffb55fSDavid Gibson 
2849fffb55fSDavid Gibson 		nameoff = stringtable_insert(strbuf, prop->name);
2859fffb55fSDavid Gibson 
286658f29a5SJohn Bonesio 		emit->property(etarget, prop->labels);
2879fffb55fSDavid Gibson 		emit->cell(etarget, prop->val.len);
2889fffb55fSDavid Gibson 		emit->cell(etarget, nameoff);
2899fffb55fSDavid Gibson 
2909fffb55fSDavid Gibson 		if ((vi->flags & FTF_VARALIGN) && (prop->val.len >= 8))
2919fffb55fSDavid Gibson 			emit->align(etarget, 8);
2929fffb55fSDavid Gibson 
2939fffb55fSDavid Gibson 		emit->data(etarget, prop->val);
2949fffb55fSDavid Gibson 		emit->align(etarget, sizeof(cell_t));
2959fffb55fSDavid Gibson 	}
2969fffb55fSDavid Gibson 
2979fffb55fSDavid Gibson 	if ((vi->flags & FTF_NAMEPROPS) && !seen_name_prop) {
2989fffb55fSDavid Gibson 		emit->property(etarget, NULL);
2999fffb55fSDavid Gibson 		emit->cell(etarget, tree->basenamelen+1);
3009fffb55fSDavid Gibson 		emit->cell(etarget, stringtable_insert(strbuf, "name"));
3019fffb55fSDavid Gibson 
3029fffb55fSDavid Gibson 		if ((vi->flags & FTF_VARALIGN) && ((tree->basenamelen+1) >= 8))
3039fffb55fSDavid Gibson 			emit->align(etarget, 8);
3049fffb55fSDavid Gibson 
3059fffb55fSDavid Gibson 		emit->string(etarget, tree->name, tree->basenamelen);
3069fffb55fSDavid Gibson 		emit->align(etarget, sizeof(cell_t));
3079fffb55fSDavid Gibson 	}
3089fffb55fSDavid Gibson 
3099fffb55fSDavid Gibson 	for_each_child(tree, child) {
3109fffb55fSDavid Gibson 		flatten_tree(child, emit, etarget, strbuf, vi);
3119fffb55fSDavid Gibson 	}
3129fffb55fSDavid Gibson 
313658f29a5SJohn Bonesio 	emit->endnode(etarget, tree->labels);
3149fffb55fSDavid Gibson }
3159fffb55fSDavid Gibson 
3169fffb55fSDavid Gibson static struct data flatten_reserve_list(struct reserve_info *reservelist,
3179fffb55fSDavid Gibson 				 struct version_info *vi)
3189fffb55fSDavid Gibson {
3199fffb55fSDavid Gibson 	struct reserve_info *re;
3209fffb55fSDavid Gibson 	struct data d = empty_data;
3219fffb55fSDavid Gibson 	static struct fdt_reserve_entry null_re = {0,0};
3229fffb55fSDavid Gibson 	int    j;
3239fffb55fSDavid Gibson 
3249fffb55fSDavid Gibson 	for (re = reservelist; re; re = re->next) {
3259fffb55fSDavid Gibson 		d = data_append_re(d, &re->re);
3269fffb55fSDavid Gibson 	}
3279fffb55fSDavid Gibson 	/*
3289fffb55fSDavid Gibson 	 * Add additional reserved slots if the user asked for them.
3299fffb55fSDavid Gibson 	 */
3309fffb55fSDavid Gibson 	for (j = 0; j < reservenum; j++) {
3319fffb55fSDavid Gibson 		d = data_append_re(d, &null_re);
3329fffb55fSDavid Gibson 	}
3339fffb55fSDavid Gibson 
3349fffb55fSDavid Gibson 	return d;
3359fffb55fSDavid Gibson }
3369fffb55fSDavid Gibson 
3379fffb55fSDavid Gibson static void make_fdt_header(struct fdt_header *fdt,
3389fffb55fSDavid Gibson 			    struct version_info *vi,
3399fffb55fSDavid Gibson 			    int reservesize, int dtsize, int strsize,
3409fffb55fSDavid Gibson 			    int boot_cpuid_phys)
3419fffb55fSDavid Gibson {
3429fffb55fSDavid Gibson 	int reserve_off;
3439fffb55fSDavid Gibson 
3449fffb55fSDavid Gibson 	reservesize += sizeof(struct fdt_reserve_entry);
3459fffb55fSDavid Gibson 
3469fffb55fSDavid Gibson 	memset(fdt, 0xff, sizeof(*fdt));
3479fffb55fSDavid Gibson 
3489fffb55fSDavid Gibson 	fdt->magic = cpu_to_fdt32(FDT_MAGIC);
3499fffb55fSDavid Gibson 	fdt->version = cpu_to_fdt32(vi->version);
3509fffb55fSDavid Gibson 	fdt->last_comp_version = cpu_to_fdt32(vi->last_comp_version);
3519fffb55fSDavid Gibson 
3529fffb55fSDavid Gibson 	/* Reserve map should be doubleword aligned */
3539fffb55fSDavid Gibson 	reserve_off = ALIGN(vi->hdr_size, 8);
3549fffb55fSDavid Gibson 
3559fffb55fSDavid Gibson 	fdt->off_mem_rsvmap = cpu_to_fdt32(reserve_off);
3569fffb55fSDavid Gibson 	fdt->off_dt_struct = cpu_to_fdt32(reserve_off + reservesize);
3579fffb55fSDavid Gibson 	fdt->off_dt_strings = cpu_to_fdt32(reserve_off + reservesize
3589fffb55fSDavid Gibson 					  + dtsize);
3599fffb55fSDavid Gibson 	fdt->totalsize = cpu_to_fdt32(reserve_off + reservesize + dtsize + strsize);
3609fffb55fSDavid Gibson 
3619fffb55fSDavid Gibson 	if (vi->flags & FTF_BOOTCPUID)
3629fffb55fSDavid Gibson 		fdt->boot_cpuid_phys = cpu_to_fdt32(boot_cpuid_phys);
3639fffb55fSDavid Gibson 	if (vi->flags & FTF_STRTABSIZE)
3649fffb55fSDavid Gibson 		fdt->size_dt_strings = cpu_to_fdt32(strsize);
3659fffb55fSDavid Gibson 	if (vi->flags & FTF_STRUCTSIZE)
3669fffb55fSDavid Gibson 		fdt->size_dt_struct = cpu_to_fdt32(dtsize);
3679fffb55fSDavid Gibson }
3689fffb55fSDavid Gibson 
369*6f05afcbSRob Herring void dt_to_blob(FILE *f, struct dt_info *dti, int version)
3709fffb55fSDavid Gibson {
3719fffb55fSDavid Gibson 	struct version_info *vi = NULL;
3729fffb55fSDavid Gibson 	int i;
3739fffb55fSDavid Gibson 	struct data blob       = empty_data;
3749fffb55fSDavid Gibson 	struct data reservebuf = empty_data;
3759fffb55fSDavid Gibson 	struct data dtbuf      = empty_data;
3769fffb55fSDavid Gibson 	struct data strbuf     = empty_data;
3779fffb55fSDavid Gibson 	struct fdt_header fdt;
3789fffb55fSDavid Gibson 	int padlen = 0;
3799fffb55fSDavid Gibson 
3809fffb55fSDavid Gibson 	for (i = 0; i < ARRAY_SIZE(version_table); i++) {
3819fffb55fSDavid Gibson 		if (version_table[i].version == version)
3829fffb55fSDavid Gibson 			vi = &version_table[i];
3839fffb55fSDavid Gibson 	}
3849fffb55fSDavid Gibson 	if (!vi)
3859fffb55fSDavid Gibson 		die("Unknown device tree blob version %d\n", version);
3869fffb55fSDavid Gibson 
387*6f05afcbSRob Herring 	flatten_tree(dti->dt, &bin_emitter, &dtbuf, &strbuf, vi);
3889fffb55fSDavid Gibson 	bin_emit_cell(&dtbuf, FDT_END);
3899fffb55fSDavid Gibson 
390*6f05afcbSRob Herring 	reservebuf = flatten_reserve_list(dti->reservelist, vi);
3919fffb55fSDavid Gibson 
3929fffb55fSDavid Gibson 	/* Make header */
3939fffb55fSDavid Gibson 	make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len,
394*6f05afcbSRob Herring 			dti->boot_cpuid_phys);
3959fffb55fSDavid Gibson 
3969fffb55fSDavid Gibson 	/*
3979fffb55fSDavid Gibson 	 * If the user asked for more space than is used, adjust the totalsize.
3989fffb55fSDavid Gibson 	 */
3999fffb55fSDavid Gibson 	if (minsize > 0) {
4009fffb55fSDavid Gibson 		padlen = minsize - fdt32_to_cpu(fdt.totalsize);
401*6f05afcbSRob Herring 		if (padlen < 0) {
402*6f05afcbSRob Herring 			padlen = 0;
403*6f05afcbSRob Herring 			if (quiet < 1)
4049fffb55fSDavid Gibson 				fprintf(stderr,
4059fffb55fSDavid Gibson 					"Warning: blob size %d >= minimum size %d\n",
4069fffb55fSDavid Gibson 					fdt32_to_cpu(fdt.totalsize), minsize);
4079fffb55fSDavid Gibson 		}
408*6f05afcbSRob Herring 	}
4099fffb55fSDavid Gibson 
4109fffb55fSDavid Gibson 	if (padsize > 0)
4119fffb55fSDavid Gibson 		padlen = padsize;
4129fffb55fSDavid Gibson 
413*6f05afcbSRob Herring 	if (alignsize > 0)
414*6f05afcbSRob Herring 		padlen = ALIGN(fdt32_to_cpu(fdt.totalsize) + padlen, alignsize)
415*6f05afcbSRob Herring 			- fdt32_to_cpu(fdt.totalsize);
416*6f05afcbSRob Herring 
4179fffb55fSDavid Gibson 	if (padlen > 0) {
4189fffb55fSDavid Gibson 		int tsize = fdt32_to_cpu(fdt.totalsize);
4199fffb55fSDavid Gibson 		tsize += padlen;
4209fffb55fSDavid Gibson 		fdt.totalsize = cpu_to_fdt32(tsize);
4219fffb55fSDavid Gibson 	}
4229fffb55fSDavid Gibson 
4239fffb55fSDavid Gibson 	/*
4249fffb55fSDavid Gibson 	 * Assemble the blob: start with the header, add with alignment
4259fffb55fSDavid Gibson 	 * the reserve buffer, add the reserve map terminating zeroes,
4269fffb55fSDavid Gibson 	 * the device tree itself, and finally the strings.
4279fffb55fSDavid Gibson 	 */
4289fffb55fSDavid Gibson 	blob = data_append_data(blob, &fdt, vi->hdr_size);
4299fffb55fSDavid Gibson 	blob = data_append_align(blob, 8);
4309fffb55fSDavid Gibson 	blob = data_merge(blob, reservebuf);
4319fffb55fSDavid Gibson 	blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry));
4329fffb55fSDavid Gibson 	blob = data_merge(blob, dtbuf);
4339fffb55fSDavid Gibson 	blob = data_merge(blob, strbuf);
4349fffb55fSDavid Gibson 
4359fffb55fSDavid Gibson 	/*
4369fffb55fSDavid Gibson 	 * If the user asked for more space than is used, pad out the blob.
4379fffb55fSDavid Gibson 	 */
4389fffb55fSDavid Gibson 	if (padlen > 0)
4399fffb55fSDavid Gibson 		blob = data_append_zeroes(blob, padlen);
4409fffb55fSDavid Gibson 
441658f29a5SJohn Bonesio 	if (fwrite(blob.val, blob.len, 1, f) != 1) {
4429fffb55fSDavid Gibson 		if (ferror(f))
443658f29a5SJohn Bonesio 			die("Error writing device tree blob: %s\n",
444658f29a5SJohn Bonesio 			    strerror(errno));
445658f29a5SJohn Bonesio 		else
446658f29a5SJohn Bonesio 			die("Short write on device tree blob\n");
447658f29a5SJohn Bonesio 	}
4489fffb55fSDavid Gibson 
4499fffb55fSDavid Gibson 	/*
4509fffb55fSDavid Gibson 	 * data_merge() frees the right-hand element so only the blob
4519fffb55fSDavid Gibson 	 * remains to be freed.
4529fffb55fSDavid Gibson 	 */
4539fffb55fSDavid Gibson 	data_free(blob);
4549fffb55fSDavid Gibson }
4559fffb55fSDavid Gibson 
4569fffb55fSDavid Gibson static void dump_stringtable_asm(FILE *f, struct data strbuf)
4579fffb55fSDavid Gibson {
4589fffb55fSDavid Gibson 	const char *p;
4599fffb55fSDavid Gibson 	int len;
4609fffb55fSDavid Gibson 
4619fffb55fSDavid Gibson 	p = strbuf.val;
4629fffb55fSDavid Gibson 
4639fffb55fSDavid Gibson 	while (p < (strbuf.val + strbuf.len)) {
4649fffb55fSDavid Gibson 		len = strlen(p);
4659fffb55fSDavid Gibson 		fprintf(f, "\t.string \"%s\"\n", p);
4669fffb55fSDavid Gibson 		p += len+1;
4679fffb55fSDavid Gibson 	}
4689fffb55fSDavid Gibson }
4699fffb55fSDavid Gibson 
470*6f05afcbSRob Herring void dt_to_asm(FILE *f, struct dt_info *dti, int version)
4719fffb55fSDavid Gibson {
4729fffb55fSDavid Gibson 	struct version_info *vi = NULL;
4739fffb55fSDavid Gibson 	int i;
4749fffb55fSDavid Gibson 	struct data strbuf = empty_data;
4759fffb55fSDavid Gibson 	struct reserve_info *re;
4769fffb55fSDavid Gibson 	const char *symprefix = "dt";
4779fffb55fSDavid Gibson 
4789fffb55fSDavid Gibson 	for (i = 0; i < ARRAY_SIZE(version_table); i++) {
4799fffb55fSDavid Gibson 		if (version_table[i].version == version)
4809fffb55fSDavid Gibson 			vi = &version_table[i];
4819fffb55fSDavid Gibson 	}
4829fffb55fSDavid Gibson 	if (!vi)
4839fffb55fSDavid Gibson 		die("Unknown device tree blob version %d\n", version);
4849fffb55fSDavid Gibson 
4859fffb55fSDavid Gibson 	fprintf(f, "/* autogenerated by dtc, do not edit */\n\n");
4869fffb55fSDavid Gibson 
4879fffb55fSDavid Gibson 	emit_label(f, symprefix, "blob_start");
4889fffb55fSDavid Gibson 	emit_label(f, symprefix, "header");
489658f29a5SJohn Bonesio 	fprintf(f, "\t/* magic */\n");
490658f29a5SJohn Bonesio 	asm_emit_cell(f, FDT_MAGIC);
491658f29a5SJohn Bonesio 	fprintf(f, "\t/* totalsize */\n");
492658f29a5SJohn Bonesio 	ASM_EMIT_BELONG(f, "_%s_blob_abs_end - _%s_blob_start",
4939fffb55fSDavid Gibson 			symprefix, symprefix);
494658f29a5SJohn Bonesio 	fprintf(f, "\t/* off_dt_struct */\n");
495658f29a5SJohn Bonesio 	ASM_EMIT_BELONG(f, "_%s_struct_start - _%s_blob_start",
4969fffb55fSDavid Gibson 		symprefix, symprefix);
497658f29a5SJohn Bonesio 	fprintf(f, "\t/* off_dt_strings */\n");
498658f29a5SJohn Bonesio 	ASM_EMIT_BELONG(f, "_%s_strings_start - _%s_blob_start",
4999fffb55fSDavid Gibson 		symprefix, symprefix);
500658f29a5SJohn Bonesio 	fprintf(f, "\t/* off_mem_rsvmap */\n");
501658f29a5SJohn Bonesio 	ASM_EMIT_BELONG(f, "_%s_reserve_map - _%s_blob_start",
5029fffb55fSDavid Gibson 		symprefix, symprefix);
503658f29a5SJohn Bonesio 	fprintf(f, "\t/* version */\n");
504658f29a5SJohn Bonesio 	asm_emit_cell(f, vi->version);
505658f29a5SJohn Bonesio 	fprintf(f, "\t/* last_comp_version */\n");
506658f29a5SJohn Bonesio 	asm_emit_cell(f, vi->last_comp_version);
5079fffb55fSDavid Gibson 
508658f29a5SJohn Bonesio 	if (vi->flags & FTF_BOOTCPUID) {
509658f29a5SJohn Bonesio 		fprintf(f, "\t/* boot_cpuid_phys */\n");
510*6f05afcbSRob Herring 		asm_emit_cell(f, dti->boot_cpuid_phys);
511658f29a5SJohn Bonesio 	}
5129fffb55fSDavid Gibson 
513658f29a5SJohn Bonesio 	if (vi->flags & FTF_STRTABSIZE) {
514658f29a5SJohn Bonesio 		fprintf(f, "\t/* size_dt_strings */\n");
515658f29a5SJohn Bonesio 		ASM_EMIT_BELONG(f, "_%s_strings_end - _%s_strings_start",
5169fffb55fSDavid Gibson 				symprefix, symprefix);
517658f29a5SJohn Bonesio 	}
5189fffb55fSDavid Gibson 
519658f29a5SJohn Bonesio 	if (vi->flags & FTF_STRUCTSIZE) {
520658f29a5SJohn Bonesio 		fprintf(f, "\t/* size_dt_struct */\n");
521658f29a5SJohn Bonesio 		ASM_EMIT_BELONG(f, "_%s_struct_end - _%s_struct_start",
5229fffb55fSDavid Gibson 			symprefix, symprefix);
523658f29a5SJohn Bonesio 	}
5249fffb55fSDavid Gibson 
5259fffb55fSDavid Gibson 	/*
5269fffb55fSDavid Gibson 	 * Reserve map entries.
5279fffb55fSDavid Gibson 	 * Align the reserve map to a doubleword boundary.
5289fffb55fSDavid Gibson 	 * Each entry is an (address, size) pair of u64 values.
5299fffb55fSDavid Gibson 	 * Always supply a zero-sized temination entry.
5309fffb55fSDavid Gibson 	 */
5319fffb55fSDavid Gibson 	asm_emit_align(f, 8);
5329fffb55fSDavid Gibson 	emit_label(f, symprefix, "reserve_map");
5339fffb55fSDavid Gibson 
5349fffb55fSDavid Gibson 	fprintf(f, "/* Memory reserve map from source file */\n");
5359fffb55fSDavid Gibson 
5369fffb55fSDavid Gibson 	/*
5379fffb55fSDavid Gibson 	 * Use .long on high and low halfs of u64s to avoid .quad
5389fffb55fSDavid Gibson 	 * as it appears .quad isn't available in some assemblers.
5399fffb55fSDavid Gibson 	 */
540*6f05afcbSRob Herring 	for (re = dti->reservelist; re; re = re->next) {
541658f29a5SJohn Bonesio 		struct label *l;
542658f29a5SJohn Bonesio 
543658f29a5SJohn Bonesio 		for_each_label(re->labels, l) {
544658f29a5SJohn Bonesio 			fprintf(f, "\t.globl\t%s\n", l->label);
545658f29a5SJohn Bonesio 			fprintf(f, "%s:\n", l->label);
5469fffb55fSDavid Gibson 		}
547658f29a5SJohn Bonesio 		ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.address >> 32));
548658f29a5SJohn Bonesio 		ASM_EMIT_BELONG(f, "0x%08x",
5499fffb55fSDavid Gibson 				(unsigned int)(re->re.address & 0xffffffff));
550658f29a5SJohn Bonesio 		ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.size >> 32));
551658f29a5SJohn Bonesio 		ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.size & 0xffffffff));
5529fffb55fSDavid Gibson 	}
5539fffb55fSDavid Gibson 	for (i = 0; i < reservenum; i++) {
5549fffb55fSDavid Gibson 		fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
5559fffb55fSDavid Gibson 	}
5569fffb55fSDavid Gibson 
5579fffb55fSDavid Gibson 	fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
5589fffb55fSDavid Gibson 
5599fffb55fSDavid Gibson 	emit_label(f, symprefix, "struct_start");
560*6f05afcbSRob Herring 	flatten_tree(dti->dt, &asm_emitter, f, &strbuf, vi);
561658f29a5SJohn Bonesio 
562658f29a5SJohn Bonesio 	fprintf(f, "\t/* FDT_END */\n");
563658f29a5SJohn Bonesio 	asm_emit_cell(f, FDT_END);
5649fffb55fSDavid Gibson 	emit_label(f, symprefix, "struct_end");
5659fffb55fSDavid Gibson 
5669fffb55fSDavid Gibson 	emit_label(f, symprefix, "strings_start");
5679fffb55fSDavid Gibson 	dump_stringtable_asm(f, strbuf);
5689fffb55fSDavid Gibson 	emit_label(f, symprefix, "strings_end");
5699fffb55fSDavid Gibson 
5709fffb55fSDavid Gibson 	emit_label(f, symprefix, "blob_end");
5719fffb55fSDavid Gibson 
5729fffb55fSDavid Gibson 	/*
5739fffb55fSDavid Gibson 	 * If the user asked for more space than is used, pad it out.
5749fffb55fSDavid Gibson 	 */
5759fffb55fSDavid Gibson 	if (minsize > 0) {
5769fffb55fSDavid Gibson 		fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n",
5779fffb55fSDavid Gibson 			minsize, symprefix, symprefix);
5789fffb55fSDavid Gibson 	}
5799fffb55fSDavid Gibson 	if (padsize > 0) {
5809fffb55fSDavid Gibson 		fprintf(f, "\t.space\t%d, 0\n", padsize);
5819fffb55fSDavid Gibson 	}
582*6f05afcbSRob Herring 	if (alignsize > 0)
583*6f05afcbSRob Herring 		asm_emit_align(f, alignsize);
5849fffb55fSDavid Gibson 	emit_label(f, symprefix, "blob_abs_end");
5859fffb55fSDavid Gibson 
5869fffb55fSDavid Gibson 	data_free(strbuf);
5879fffb55fSDavid Gibson }
5889fffb55fSDavid Gibson 
5899fffb55fSDavid Gibson struct inbuf {
5909fffb55fSDavid Gibson 	char *base, *limit, *ptr;
5919fffb55fSDavid Gibson };
5929fffb55fSDavid Gibson 
5939fffb55fSDavid Gibson static void inbuf_init(struct inbuf *inb, void *base, void *limit)
5949fffb55fSDavid Gibson {
5959fffb55fSDavid Gibson 	inb->base = base;
5969fffb55fSDavid Gibson 	inb->limit = limit;
5979fffb55fSDavid Gibson 	inb->ptr = inb->base;
5989fffb55fSDavid Gibson }
5999fffb55fSDavid Gibson 
6009fffb55fSDavid Gibson static void flat_read_chunk(struct inbuf *inb, void *p, int len)
6019fffb55fSDavid Gibson {
6029fffb55fSDavid Gibson 	if ((inb->ptr + len) > inb->limit)
6039fffb55fSDavid Gibson 		die("Premature end of data parsing flat device tree\n");
6049fffb55fSDavid Gibson 
6059fffb55fSDavid Gibson 	memcpy(p, inb->ptr, len);
6069fffb55fSDavid Gibson 
6079fffb55fSDavid Gibson 	inb->ptr += len;
6089fffb55fSDavid Gibson }
6099fffb55fSDavid Gibson 
6109fffb55fSDavid Gibson static uint32_t flat_read_word(struct inbuf *inb)
6119fffb55fSDavid Gibson {
6129fffb55fSDavid Gibson 	uint32_t val;
6139fffb55fSDavid Gibson 
6149fffb55fSDavid Gibson 	assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
6159fffb55fSDavid Gibson 
6169fffb55fSDavid Gibson 	flat_read_chunk(inb, &val, sizeof(val));
6179fffb55fSDavid Gibson 
6189fffb55fSDavid Gibson 	return fdt32_to_cpu(val);
6199fffb55fSDavid Gibson }
6209fffb55fSDavid Gibson 
6219fffb55fSDavid Gibson static void flat_realign(struct inbuf *inb, int align)
6229fffb55fSDavid Gibson {
6239fffb55fSDavid Gibson 	int off = inb->ptr - inb->base;
6249fffb55fSDavid Gibson 
6259fffb55fSDavid Gibson 	inb->ptr = inb->base + ALIGN(off, align);
6269fffb55fSDavid Gibson 	if (inb->ptr > inb->limit)
6279fffb55fSDavid Gibson 		die("Premature end of data parsing flat device tree\n");
6289fffb55fSDavid Gibson }
6299fffb55fSDavid Gibson 
6309fffb55fSDavid Gibson static char *flat_read_string(struct inbuf *inb)
6319fffb55fSDavid Gibson {
6329fffb55fSDavid Gibson 	int len = 0;
6339fffb55fSDavid Gibson 	const char *p = inb->ptr;
6349fffb55fSDavid Gibson 	char *str;
6359fffb55fSDavid Gibson 
6369fffb55fSDavid Gibson 	do {
6379fffb55fSDavid Gibson 		if (p >= inb->limit)
6389fffb55fSDavid Gibson 			die("Premature end of data parsing flat device tree\n");
6399fffb55fSDavid Gibson 		len++;
6409fffb55fSDavid Gibson 	} while ((*p++) != '\0');
6419fffb55fSDavid Gibson 
642658f29a5SJohn Bonesio 	str = xstrdup(inb->ptr);
6439fffb55fSDavid Gibson 
6449fffb55fSDavid Gibson 	inb->ptr += len;
6459fffb55fSDavid Gibson 
6469fffb55fSDavid Gibson 	flat_realign(inb, sizeof(uint32_t));
6479fffb55fSDavid Gibson 
6489fffb55fSDavid Gibson 	return str;
6499fffb55fSDavid Gibson }
6509fffb55fSDavid Gibson 
6519fffb55fSDavid Gibson static struct data flat_read_data(struct inbuf *inb, int len)
6529fffb55fSDavid Gibson {
6539fffb55fSDavid Gibson 	struct data d = empty_data;
6549fffb55fSDavid Gibson 
6559fffb55fSDavid Gibson 	if (len == 0)
6569fffb55fSDavid Gibson 		return empty_data;
6579fffb55fSDavid Gibson 
6589fffb55fSDavid Gibson 	d = data_grow_for(d, len);
6599fffb55fSDavid Gibson 	d.len = len;
6609fffb55fSDavid Gibson 
6619fffb55fSDavid Gibson 	flat_read_chunk(inb, d.val, len);
6629fffb55fSDavid Gibson 
6639fffb55fSDavid Gibson 	flat_realign(inb, sizeof(uint32_t));
6649fffb55fSDavid Gibson 
6659fffb55fSDavid Gibson 	return d;
6669fffb55fSDavid Gibson }
6679fffb55fSDavid Gibson 
6689fffb55fSDavid Gibson static char *flat_read_stringtable(struct inbuf *inb, int offset)
6699fffb55fSDavid Gibson {
6709fffb55fSDavid Gibson 	const char *p;
6719fffb55fSDavid Gibson 
6729fffb55fSDavid Gibson 	p = inb->base + offset;
6739fffb55fSDavid Gibson 	while (1) {
6749fffb55fSDavid Gibson 		if (p >= inb->limit || p < inb->base)
6759fffb55fSDavid Gibson 			die("String offset %d overruns string table\n",
6769fffb55fSDavid Gibson 			    offset);
6779fffb55fSDavid Gibson 
6789fffb55fSDavid Gibson 		if (*p == '\0')
6799fffb55fSDavid Gibson 			break;
6809fffb55fSDavid Gibson 
6819fffb55fSDavid Gibson 		p++;
6829fffb55fSDavid Gibson 	}
6839fffb55fSDavid Gibson 
684658f29a5SJohn Bonesio 	return xstrdup(inb->base + offset);
6859fffb55fSDavid Gibson }
6869fffb55fSDavid Gibson 
6879fffb55fSDavid Gibson static struct property *flat_read_property(struct inbuf *dtbuf,
6889fffb55fSDavid Gibson 					   struct inbuf *strbuf, int flags)
6899fffb55fSDavid Gibson {
6909fffb55fSDavid Gibson 	uint32_t proplen, stroff;
6919fffb55fSDavid Gibson 	char *name;
6929fffb55fSDavid Gibson 	struct data val;
6939fffb55fSDavid Gibson 
6949fffb55fSDavid Gibson 	proplen = flat_read_word(dtbuf);
6959fffb55fSDavid Gibson 	stroff = flat_read_word(dtbuf);
6969fffb55fSDavid Gibson 
6979fffb55fSDavid Gibson 	name = flat_read_stringtable(strbuf, stroff);
6989fffb55fSDavid Gibson 
6999fffb55fSDavid Gibson 	if ((flags & FTF_VARALIGN) && (proplen >= 8))
7009fffb55fSDavid Gibson 		flat_realign(dtbuf, 8);
7019fffb55fSDavid Gibson 
7029fffb55fSDavid Gibson 	val = flat_read_data(dtbuf, proplen);
7039fffb55fSDavid Gibson 
704658f29a5SJohn Bonesio 	return build_property(name, val);
7059fffb55fSDavid Gibson }
7069fffb55fSDavid Gibson 
7079fffb55fSDavid Gibson 
7089fffb55fSDavid Gibson static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
7099fffb55fSDavid Gibson {
7109fffb55fSDavid Gibson 	struct reserve_info *reservelist = NULL;
7119fffb55fSDavid Gibson 	struct reserve_info *new;
7129fffb55fSDavid Gibson 	struct fdt_reserve_entry re;
7139fffb55fSDavid Gibson 
7149fffb55fSDavid Gibson 	/*
7159fffb55fSDavid Gibson 	 * Each entry is a pair of u64 (addr, size) values for 4 cell_t's.
7169fffb55fSDavid Gibson 	 * List terminates at an entry with size equal to zero.
7179fffb55fSDavid Gibson 	 *
7189fffb55fSDavid Gibson 	 * First pass, count entries.
7199fffb55fSDavid Gibson 	 */
7209fffb55fSDavid Gibson 	while (1) {
7219fffb55fSDavid Gibson 		flat_read_chunk(inb, &re, sizeof(re));
7229fffb55fSDavid Gibson 		re.address  = fdt64_to_cpu(re.address);
7239fffb55fSDavid Gibson 		re.size = fdt64_to_cpu(re.size);
7249fffb55fSDavid Gibson 		if (re.size == 0)
7259fffb55fSDavid Gibson 			break;
7269fffb55fSDavid Gibson 
727658f29a5SJohn Bonesio 		new = build_reserve_entry(re.address, re.size);
7289fffb55fSDavid Gibson 		reservelist = add_reserve_entry(reservelist, new);
7299fffb55fSDavid Gibson 	}
7309fffb55fSDavid Gibson 
7319fffb55fSDavid Gibson 	return reservelist;
7329fffb55fSDavid Gibson }
7339fffb55fSDavid Gibson 
7349fffb55fSDavid Gibson 
7359fffb55fSDavid Gibson static char *nodename_from_path(const char *ppath, const char *cpath)
7369fffb55fSDavid Gibson {
7379fffb55fSDavid Gibson 	int plen;
7389fffb55fSDavid Gibson 
7399fffb55fSDavid Gibson 	plen = strlen(ppath);
7409fffb55fSDavid Gibson 
7419fffb55fSDavid Gibson 	if (!strneq(ppath, cpath, plen))
7429fffb55fSDavid Gibson 		die("Path \"%s\" is not valid as a child of \"%s\"\n",
7439fffb55fSDavid Gibson 		    cpath, ppath);
7449fffb55fSDavid Gibson 
7459fffb55fSDavid Gibson 	/* root node is a special case */
7469fffb55fSDavid Gibson 	if (!streq(ppath, "/"))
7479fffb55fSDavid Gibson 		plen++;
7489fffb55fSDavid Gibson 
749658f29a5SJohn Bonesio 	return xstrdup(cpath + plen);
7509fffb55fSDavid Gibson }
7519fffb55fSDavid Gibson 
7529fffb55fSDavid Gibson static struct node *unflatten_tree(struct inbuf *dtbuf,
7539fffb55fSDavid Gibson 				   struct inbuf *strbuf,
7549fffb55fSDavid Gibson 				   const char *parent_flatname, int flags)
7559fffb55fSDavid Gibson {
7569fffb55fSDavid Gibson 	struct node *node;
7579fffb55fSDavid Gibson 	char *flatname;
7589fffb55fSDavid Gibson 	uint32_t val;
7599fffb55fSDavid Gibson 
7609fffb55fSDavid Gibson 	node = build_node(NULL, NULL);
7619fffb55fSDavid Gibson 
7629fffb55fSDavid Gibson 	flatname = flat_read_string(dtbuf);
7639fffb55fSDavid Gibson 
7649fffb55fSDavid Gibson 	if (flags & FTF_FULLPATH)
7659fffb55fSDavid Gibson 		node->name = nodename_from_path(parent_flatname, flatname);
7669fffb55fSDavid Gibson 	else
7679fffb55fSDavid Gibson 		node->name = flatname;
7689fffb55fSDavid Gibson 
7699fffb55fSDavid Gibson 	do {
7709fffb55fSDavid Gibson 		struct property *prop;
7719fffb55fSDavid Gibson 		struct node *child;
7729fffb55fSDavid Gibson 
7739fffb55fSDavid Gibson 		val = flat_read_word(dtbuf);
7749fffb55fSDavid Gibson 		switch (val) {
7759fffb55fSDavid Gibson 		case FDT_PROP:
7769fffb55fSDavid Gibson 			if (node->children)
7779fffb55fSDavid Gibson 				fprintf(stderr, "Warning: Flat tree input has "
7789fffb55fSDavid Gibson 					"subnodes preceding a property.\n");
7799fffb55fSDavid Gibson 			prop = flat_read_property(dtbuf, strbuf, flags);
7809fffb55fSDavid Gibson 			add_property(node, prop);
7819fffb55fSDavid Gibson 			break;
7829fffb55fSDavid Gibson 
7839fffb55fSDavid Gibson 		case FDT_BEGIN_NODE:
7849fffb55fSDavid Gibson 			child = unflatten_tree(dtbuf,strbuf, flatname, flags);
7859fffb55fSDavid Gibson 			add_child(node, child);
7869fffb55fSDavid Gibson 			break;
7879fffb55fSDavid Gibson 
7889fffb55fSDavid Gibson 		case FDT_END_NODE:
7899fffb55fSDavid Gibson 			break;
7909fffb55fSDavid Gibson 
7919fffb55fSDavid Gibson 		case FDT_END:
7929fffb55fSDavid Gibson 			die("Premature FDT_END in device tree blob\n");
7939fffb55fSDavid Gibson 			break;
7949fffb55fSDavid Gibson 
7959fffb55fSDavid Gibson 		case FDT_NOP:
7969fffb55fSDavid Gibson 			if (!(flags & FTF_NOPS))
7979fffb55fSDavid Gibson 				fprintf(stderr, "Warning: NOP tag found in flat tree"
7989fffb55fSDavid Gibson 					" version <16\n");
7999fffb55fSDavid Gibson 
8009fffb55fSDavid Gibson 			/* Ignore */
8019fffb55fSDavid Gibson 			break;
8029fffb55fSDavid Gibson 
8039fffb55fSDavid Gibson 		default:
8049fffb55fSDavid Gibson 			die("Invalid opcode word %08x in device tree blob\n",
8059fffb55fSDavid Gibson 			    val);
8069fffb55fSDavid Gibson 		}
8079fffb55fSDavid Gibson 	} while (val != FDT_END_NODE);
8089fffb55fSDavid Gibson 
809*6f05afcbSRob Herring 	if (node->name != flatname) {
810*6f05afcbSRob Herring 		free(flatname);
811*6f05afcbSRob Herring 	}
812*6f05afcbSRob Herring 
8139fffb55fSDavid Gibson 	return node;
8149fffb55fSDavid Gibson }
8159fffb55fSDavid Gibson 
8169fffb55fSDavid Gibson 
817*6f05afcbSRob Herring struct dt_info *dt_from_blob(const char *fname)
8189fffb55fSDavid Gibson {
819658f29a5SJohn Bonesio 	FILE *f;
8209fffb55fSDavid Gibson 	uint32_t magic, totalsize, version, size_dt, boot_cpuid_phys;
8219fffb55fSDavid Gibson 	uint32_t off_dt, off_str, off_mem_rsvmap;
8229fffb55fSDavid Gibson 	int rc;
8239fffb55fSDavid Gibson 	char *blob;
8249fffb55fSDavid Gibson 	struct fdt_header *fdt;
8259fffb55fSDavid Gibson 	char *p;
8269fffb55fSDavid Gibson 	struct inbuf dtbuf, strbuf;
8279fffb55fSDavid Gibson 	struct inbuf memresvbuf;
8289fffb55fSDavid Gibson 	int sizeleft;
8299fffb55fSDavid Gibson 	struct reserve_info *reservelist;
8309fffb55fSDavid Gibson 	struct node *tree;
8319fffb55fSDavid Gibson 	uint32_t val;
8329fffb55fSDavid Gibson 	int flags = 0;
8339fffb55fSDavid Gibson 
834658f29a5SJohn Bonesio 	f = srcfile_relative_open(fname, NULL);
8359fffb55fSDavid Gibson 
836658f29a5SJohn Bonesio 	rc = fread(&magic, sizeof(magic), 1, f);
837658f29a5SJohn Bonesio 	if (ferror(f))
8389fffb55fSDavid Gibson 		die("Error reading DT blob magic number: %s\n",
8399fffb55fSDavid Gibson 		    strerror(errno));
8409fffb55fSDavid Gibson 	if (rc < 1) {
841658f29a5SJohn Bonesio 		if (feof(f))
8429fffb55fSDavid Gibson 			die("EOF reading DT blob magic number\n");
8439fffb55fSDavid Gibson 		else
8449fffb55fSDavid Gibson 			die("Mysterious short read reading magic number\n");
8459fffb55fSDavid Gibson 	}
8469fffb55fSDavid Gibson 
8479fffb55fSDavid Gibson 	magic = fdt32_to_cpu(magic);
8489fffb55fSDavid Gibson 	if (magic != FDT_MAGIC)
8499fffb55fSDavid Gibson 		die("Blob has incorrect magic number\n");
8509fffb55fSDavid Gibson 
851658f29a5SJohn Bonesio 	rc = fread(&totalsize, sizeof(totalsize), 1, f);
852658f29a5SJohn Bonesio 	if (ferror(f))
8539fffb55fSDavid Gibson 		die("Error reading DT blob size: %s\n", strerror(errno));
8549fffb55fSDavid Gibson 	if (rc < 1) {
855658f29a5SJohn Bonesio 		if (feof(f))
8569fffb55fSDavid Gibson 			die("EOF reading DT blob size\n");
8579fffb55fSDavid Gibson 		else
8589fffb55fSDavid Gibson 			die("Mysterious short read reading blob size\n");
8599fffb55fSDavid Gibson 	}
8609fffb55fSDavid Gibson 
8619fffb55fSDavid Gibson 	totalsize = fdt32_to_cpu(totalsize);
8629fffb55fSDavid Gibson 	if (totalsize < FDT_V1_SIZE)
8639fffb55fSDavid Gibson 		die("DT blob size (%d) is too small\n", totalsize);
8649fffb55fSDavid Gibson 
8659fffb55fSDavid Gibson 	blob = xmalloc(totalsize);
8669fffb55fSDavid Gibson 
8679fffb55fSDavid Gibson 	fdt = (struct fdt_header *)blob;
8689fffb55fSDavid Gibson 	fdt->magic = cpu_to_fdt32(magic);
8699fffb55fSDavid Gibson 	fdt->totalsize = cpu_to_fdt32(totalsize);
8709fffb55fSDavid Gibson 
8719fffb55fSDavid Gibson 	sizeleft = totalsize - sizeof(magic) - sizeof(totalsize);
8729fffb55fSDavid Gibson 	p = blob + sizeof(magic)  + sizeof(totalsize);
8739fffb55fSDavid Gibson 
8749fffb55fSDavid Gibson 	while (sizeleft) {
875658f29a5SJohn Bonesio 		if (feof(f))
8769fffb55fSDavid Gibson 			die("EOF before reading %d bytes of DT blob\n",
8779fffb55fSDavid Gibson 			    totalsize);
8789fffb55fSDavid Gibson 
879658f29a5SJohn Bonesio 		rc = fread(p, 1, sizeleft, f);
880658f29a5SJohn Bonesio 		if (ferror(f))
8819fffb55fSDavid Gibson 			die("Error reading DT blob: %s\n",
8829fffb55fSDavid Gibson 			    strerror(errno));
8839fffb55fSDavid Gibson 
8849fffb55fSDavid Gibson 		sizeleft -= rc;
8859fffb55fSDavid Gibson 		p += rc;
8869fffb55fSDavid Gibson 	}
8879fffb55fSDavid Gibson 
8889fffb55fSDavid Gibson 	off_dt = fdt32_to_cpu(fdt->off_dt_struct);
8899fffb55fSDavid Gibson 	off_str = fdt32_to_cpu(fdt->off_dt_strings);
8909fffb55fSDavid Gibson 	off_mem_rsvmap = fdt32_to_cpu(fdt->off_mem_rsvmap);
8919fffb55fSDavid Gibson 	version = fdt32_to_cpu(fdt->version);
8929fffb55fSDavid Gibson 	boot_cpuid_phys = fdt32_to_cpu(fdt->boot_cpuid_phys);
8939fffb55fSDavid Gibson 
8949fffb55fSDavid Gibson 	if (off_mem_rsvmap >= totalsize)
8959fffb55fSDavid Gibson 		die("Mem Reserve structure offset exceeds total size\n");
8969fffb55fSDavid Gibson 
8979fffb55fSDavid Gibson 	if (off_dt >= totalsize)
8989fffb55fSDavid Gibson 		die("DT structure offset exceeds total size\n");
8999fffb55fSDavid Gibson 
9009fffb55fSDavid Gibson 	if (off_str > totalsize)
9019fffb55fSDavid Gibson 		die("String table offset exceeds total size\n");
9029fffb55fSDavid Gibson 
9039fffb55fSDavid Gibson 	if (version >= 3) {
9049fffb55fSDavid Gibson 		uint32_t size_str = fdt32_to_cpu(fdt->size_dt_strings);
905b9937347SRob Herring 		if ((off_str+size_str < off_str) || (off_str+size_str > totalsize))
9069fffb55fSDavid Gibson 			die("String table extends past total size\n");
9079fffb55fSDavid Gibson 		inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str);
9089fffb55fSDavid Gibson 	} else {
9099fffb55fSDavid Gibson 		inbuf_init(&strbuf, blob + off_str, blob + totalsize);
9109fffb55fSDavid Gibson 	}
9119fffb55fSDavid Gibson 
9129fffb55fSDavid Gibson 	if (version >= 17) {
9139fffb55fSDavid Gibson 		size_dt = fdt32_to_cpu(fdt->size_dt_struct);
914b9937347SRob Herring 		if ((off_dt+size_dt < off_dt) || (off_dt+size_dt > totalsize))
9159fffb55fSDavid Gibson 			die("Structure block extends past total size\n");
9169fffb55fSDavid Gibson 	}
9179fffb55fSDavid Gibson 
9189fffb55fSDavid Gibson 	if (version < 16) {
9199fffb55fSDavid Gibson 		flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN;
9209fffb55fSDavid Gibson 	} else {
9219fffb55fSDavid Gibson 		flags |= FTF_NOPS;
9229fffb55fSDavid Gibson 	}
9239fffb55fSDavid Gibson 
9249fffb55fSDavid Gibson 	inbuf_init(&memresvbuf,
9259fffb55fSDavid Gibson 		   blob + off_mem_rsvmap, blob + totalsize);
9269fffb55fSDavid Gibson 	inbuf_init(&dtbuf, blob + off_dt, blob + totalsize);
9279fffb55fSDavid Gibson 
9289fffb55fSDavid Gibson 	reservelist = flat_read_mem_reserve(&memresvbuf);
9299fffb55fSDavid Gibson 
9309fffb55fSDavid Gibson 	val = flat_read_word(&dtbuf);
9319fffb55fSDavid Gibson 
9329fffb55fSDavid Gibson 	if (val != FDT_BEGIN_NODE)
9339fffb55fSDavid Gibson 		die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val);
9349fffb55fSDavid Gibson 
9359fffb55fSDavid Gibson 	tree = unflatten_tree(&dtbuf, &strbuf, "", flags);
9369fffb55fSDavid Gibson 
9379fffb55fSDavid Gibson 	val = flat_read_word(&dtbuf);
9389fffb55fSDavid Gibson 	if (val != FDT_END)
9399fffb55fSDavid Gibson 		die("Device tree blob doesn't end with FDT_END\n");
9409fffb55fSDavid Gibson 
9419fffb55fSDavid Gibson 	free(blob);
9429fffb55fSDavid Gibson 
943658f29a5SJohn Bonesio 	fclose(f);
9449fffb55fSDavid Gibson 
945*6f05afcbSRob Herring 	return build_dt_info(DTSF_V1, reservelist, tree, boot_cpuid_phys);
9469fffb55fSDavid Gibson }
947