Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include "dtc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "srcpos.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #define FTF_FULLPATH	0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define FTF_VARALIGN	0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #define FTF_NAMEPROPS	0x4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #define FTF_BOOTCPUID	0x8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define FTF_STRTABSIZE	0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #define FTF_STRUCTSIZE	0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define FTF_NOPS	0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static struct version_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	int version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	int last_comp_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	int hdr_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) } version_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	{1, 1, FDT_V1_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	{2, 1, FDT_V2_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	{3, 1, FDT_V3_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID|FTF_STRTABSIZE},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	{16, 16, FDT_V3_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_NOPS},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	{17, 16, FDT_V17_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) struct emitter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	void (*cell)(void *, cell_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	void (*string)(void *, const char *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	void (*align)(void *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	void (*data)(void *, struct data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	void (*beginnode)(void *, struct label *labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	void (*endnode)(void *, struct label *labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	void (*property)(void *, struct label *labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static void bin_emit_cell(void *e, cell_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct data *dtbuf = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	*dtbuf = data_append_cell(*dtbuf, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static void bin_emit_string(void *e, const char *str, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct data *dtbuf = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		len = strlen(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	*dtbuf = data_append_data(*dtbuf, str, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	*dtbuf = data_append_byte(*dtbuf, '\0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static void bin_emit_align(void *e, int a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct data *dtbuf = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	*dtbuf = data_append_align(*dtbuf, a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static void bin_emit_data(void *e, struct data d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct data *dtbuf = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	*dtbuf = data_append_data(*dtbuf, d.val, d.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static void bin_emit_beginnode(void *e, struct label *labels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	bin_emit_cell(e, FDT_BEGIN_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static void bin_emit_endnode(void *e, struct label *labels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	bin_emit_cell(e, FDT_END_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static void bin_emit_property(void *e, struct label *labels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	bin_emit_cell(e, FDT_PROP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static struct emitter bin_emitter = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	.cell = bin_emit_cell,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	.string = bin_emit_string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	.align = bin_emit_align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	.data = bin_emit_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	.beginnode = bin_emit_beginnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	.endnode = bin_emit_endnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	.property = bin_emit_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static void emit_label(FILE *f, const char *prefix, const char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	fprintf(f, "\t.globl\t%s_%s\n", prefix, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	fprintf(f, "%s_%s:\n", prefix, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	fprintf(f, "_%s_%s:\n", prefix, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static void emit_offset_label(FILE *f, const char *label, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	fprintf(f, "\t.globl\t%s\n", label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	fprintf(f, "%s\t= . + %d\n", label, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #define ASM_EMIT_BELONG(f, fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	{ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		fprintf((f), "\t.byte\t((" fmt ") >> 24) & 0xff\n", __VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		fprintf((f), "\t.byte\t((" fmt ") >> 16) & 0xff\n", __VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		fprintf((f), "\t.byte\t((" fmt ") >> 8) & 0xff\n", __VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		fprintf((f), "\t.byte\t(" fmt ") & 0xff\n", __VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static void asm_emit_cell(void *e, cell_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	FILE *f = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	fprintf(f, "\t.byte 0x%02x; .byte 0x%02x; .byte 0x%02x; .byte 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		(val >> 24) & 0xff, (val >> 16) & 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		(val >> 8) & 0xff, val & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void asm_emit_string(void *e, const char *str, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	FILE *f = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (len != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		fprintf(f, "\t.string\t\"%.*s\"\n", len, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		fprintf(f, "\t.string\t\"%s\"\n", str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static void asm_emit_align(void *e, int a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	FILE *f = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	fprintf(f, "\t.balign\t%d, 0\n", a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static void asm_emit_data(void *e, struct data d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	FILE *f = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	unsigned int off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct marker *m = d.markers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	for_each_marker_of_type(m, LABEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		emit_offset_label(f, m->ref, m->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	while ((d.len - off) >= sizeof(uint32_t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		asm_emit_cell(e, dtb_ld32(d.val + off));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		off += sizeof(uint32_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	while ((d.len - off) >= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		off += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	assert(off == d.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static void asm_emit_beginnode(void *e, struct label *labels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	FILE *f = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct label *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	for_each_label(labels, l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		fprintf(f, "\t.globl\t%s\n", l->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		fprintf(f, "%s:\n", l->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	fprintf(f, "\t/* FDT_BEGIN_NODE */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	asm_emit_cell(e, FDT_BEGIN_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static void asm_emit_endnode(void *e, struct label *labels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	FILE *f = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct label *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	fprintf(f, "\t/* FDT_END_NODE */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	asm_emit_cell(e, FDT_END_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	for_each_label(labels, l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		fprintf(f, "\t.globl\t%s_end\n", l->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		fprintf(f, "%s_end:\n", l->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void asm_emit_property(void *e, struct label *labels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	FILE *f = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct label *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	for_each_label(labels, l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		fprintf(f, "\t.globl\t%s\n", l->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		fprintf(f, "%s:\n", l->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	fprintf(f, "\t/* FDT_PROP */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	asm_emit_cell(e, FDT_PROP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static struct emitter asm_emitter = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	.cell = asm_emit_cell,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	.string = asm_emit_string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	.align = asm_emit_align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	.data = asm_emit_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	.beginnode = asm_emit_beginnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	.endnode = asm_emit_endnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	.property = asm_emit_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static int stringtable_insert(struct data *d, const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	/* FIXME: do this more efficiently? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	for (i = 0; i < d->len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		if (streq(str, d->val + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	*d = data_append_data(*d, str, strlen(str)+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static void flatten_tree(struct node *tree, struct emitter *emit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			 void *etarget, struct data *strbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			 struct version_info *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	bool seen_name_prop = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (tree->deleted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	emit->beginnode(etarget, tree->labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (vi->flags & FTF_FULLPATH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		emit->string(etarget, tree->fullpath, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		emit->string(etarget, tree->name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	emit->align(etarget, sizeof(cell_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	for_each_property(tree, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		int nameoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		if (streq(prop->name, "name"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			seen_name_prop = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		nameoff = stringtable_insert(strbuf, prop->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		emit->property(etarget, prop->labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		emit->cell(etarget, prop->val.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		emit->cell(etarget, nameoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		if ((vi->flags & FTF_VARALIGN) && (prop->val.len >= 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			emit->align(etarget, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		emit->data(etarget, prop->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		emit->align(etarget, sizeof(cell_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if ((vi->flags & FTF_NAMEPROPS) && !seen_name_prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		emit->property(etarget, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		emit->cell(etarget, tree->basenamelen+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		emit->cell(etarget, stringtable_insert(strbuf, "name"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		if ((vi->flags & FTF_VARALIGN) && ((tree->basenamelen+1) >= 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			emit->align(etarget, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		emit->string(etarget, tree->name, tree->basenamelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		emit->align(etarget, sizeof(cell_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	for_each_child(tree, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		flatten_tree(child, emit, etarget, strbuf, vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	emit->endnode(etarget, tree->labels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static struct data flatten_reserve_list(struct reserve_info *reservelist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 				 struct version_info *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	struct reserve_info *re;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	struct data d = empty_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	int    j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	for (re = reservelist; re; re = re->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		d = data_append_re(d, re->address, re->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	 * Add additional reserved slots if the user asked for them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	for (j = 0; j < reservenum; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		d = data_append_re(d, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	return d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static void make_fdt_header(struct fdt_header *fdt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			    struct version_info *vi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			    int reservesize, int dtsize, int strsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			    int boot_cpuid_phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	int reserve_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	reservesize += sizeof(struct fdt_reserve_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	memset(fdt, 0xff, sizeof(*fdt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	fdt->magic = cpu_to_fdt32(FDT_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	fdt->version = cpu_to_fdt32(vi->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	fdt->last_comp_version = cpu_to_fdt32(vi->last_comp_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	/* Reserve map should be doubleword aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	reserve_off = ALIGN(vi->hdr_size, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	fdt->off_mem_rsvmap = cpu_to_fdt32(reserve_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	fdt->off_dt_struct = cpu_to_fdt32(reserve_off + reservesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	fdt->off_dt_strings = cpu_to_fdt32(reserve_off + reservesize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 					  + dtsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	fdt->totalsize = cpu_to_fdt32(reserve_off + reservesize + dtsize + strsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	if (vi->flags & FTF_BOOTCPUID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		fdt->boot_cpuid_phys = cpu_to_fdt32(boot_cpuid_phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (vi->flags & FTF_STRTABSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		fdt->size_dt_strings = cpu_to_fdt32(strsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (vi->flags & FTF_STRUCTSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		fdt->size_dt_struct = cpu_to_fdt32(dtsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) void dt_to_blob(FILE *f, struct dt_info *dti, int version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	struct version_info *vi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	struct data blob       = empty_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	struct data reservebuf = empty_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	struct data dtbuf      = empty_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	struct data strbuf     = empty_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	struct fdt_header fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	int padlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	for (i = 0; i < ARRAY_SIZE(version_table); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		if (version_table[i].version == version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			vi = &version_table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	if (!vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		die("Unknown device tree blob version %d\n", version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	flatten_tree(dti->dt, &bin_emitter, &dtbuf, &strbuf, vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	bin_emit_cell(&dtbuf, FDT_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	reservebuf = flatten_reserve_list(dti->reservelist, vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	/* Make header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			dti->boot_cpuid_phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	 * If the user asked for more space than is used, adjust the totalsize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if (minsize > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		padlen = minsize - fdt32_to_cpu(fdt.totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		if (padlen < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			padlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			if (quiet < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 				fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 					"Warning: blob size %"PRIu32" >= minimum size %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 					fdt32_to_cpu(fdt.totalsize), minsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (padsize > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		padlen = padsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (alignsize > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		padlen = ALIGN(fdt32_to_cpu(fdt.totalsize) + padlen, alignsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			- fdt32_to_cpu(fdt.totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (padlen > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		int tsize = fdt32_to_cpu(fdt.totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		tsize += padlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		fdt.totalsize = cpu_to_fdt32(tsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	 * Assemble the blob: start with the header, add with alignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	 * the reserve buffer, add the reserve map terminating zeroes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	 * the device tree itself, and finally the strings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	blob = data_append_data(blob, &fdt, vi->hdr_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	blob = data_append_align(blob, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	blob = data_merge(blob, reservebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	blob = data_merge(blob, dtbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	blob = data_merge(blob, strbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	 * If the user asked for more space than is used, pad out the blob.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	if (padlen > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		blob = data_append_zeroes(blob, padlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	if (fwrite(blob.val, blob.len, 1, f) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		if (ferror(f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			die("Error writing device tree blob: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			    strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			die("Short write on device tree blob\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	 * data_merge() frees the right-hand element so only the blob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	 * remains to be freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	data_free(blob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static void dump_stringtable_asm(FILE *f, struct data strbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	const char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	p = strbuf.val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	while (p < (strbuf.val + strbuf.len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		len = strlen(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		fprintf(f, "\t.string \"%s\"\n", p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		p += len+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) void dt_to_asm(FILE *f, struct dt_info *dti, int version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	struct version_info *vi = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	struct data strbuf = empty_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	struct reserve_info *re;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	const char *symprefix = "dt";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	for (i = 0; i < ARRAY_SIZE(version_table); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		if (version_table[i].version == version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			vi = &version_table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	if (!vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		die("Unknown device tree blob version %d\n", version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	fprintf(f, "/* autogenerated by dtc, do not edit */\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	emit_label(f, symprefix, "blob_start");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	emit_label(f, symprefix, "header");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	fprintf(f, "\t/* magic */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	asm_emit_cell(f, FDT_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	fprintf(f, "\t/* totalsize */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	ASM_EMIT_BELONG(f, "_%s_blob_abs_end - _%s_blob_start",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 			symprefix, symprefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	fprintf(f, "\t/* off_dt_struct */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	ASM_EMIT_BELONG(f, "_%s_struct_start - _%s_blob_start",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		symprefix, symprefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	fprintf(f, "\t/* off_dt_strings */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	ASM_EMIT_BELONG(f, "_%s_strings_start - _%s_blob_start",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		symprefix, symprefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	fprintf(f, "\t/* off_mem_rsvmap */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	ASM_EMIT_BELONG(f, "_%s_reserve_map - _%s_blob_start",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		symprefix, symprefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	fprintf(f, "\t/* version */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	asm_emit_cell(f, vi->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	fprintf(f, "\t/* last_comp_version */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	asm_emit_cell(f, vi->last_comp_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	if (vi->flags & FTF_BOOTCPUID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		fprintf(f, "\t/* boot_cpuid_phys */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		asm_emit_cell(f, dti->boot_cpuid_phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	if (vi->flags & FTF_STRTABSIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		fprintf(f, "\t/* size_dt_strings */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		ASM_EMIT_BELONG(f, "_%s_strings_end - _%s_strings_start",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 				symprefix, symprefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	if (vi->flags & FTF_STRUCTSIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		fprintf(f, "\t/* size_dt_struct */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		ASM_EMIT_BELONG(f, "_%s_struct_end - _%s_struct_start",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			symprefix, symprefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	 * Reserve map entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	 * Align the reserve map to a doubleword boundary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	 * Each entry is an (address, size) pair of u64 values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	 * Always supply a zero-sized temination entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	asm_emit_align(f, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	emit_label(f, symprefix, "reserve_map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	fprintf(f, "/* Memory reserve map from source file */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	 * Use .long on high and low halves of u64s to avoid .quad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	 * as it appears .quad isn't available in some assemblers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	for (re = dti->reservelist; re; re = re->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		struct label *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		for_each_label(re->labels, l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			fprintf(f, "\t.globl\t%s\n", l->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 			fprintf(f, "%s:\n", l->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->address >> 32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		ASM_EMIT_BELONG(f, "0x%08x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 				(unsigned int)(re->address & 0xffffffff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->size >> 32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->size & 0xffffffff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	for (i = 0; i < reservenum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	emit_label(f, symprefix, "struct_start");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	flatten_tree(dti->dt, &asm_emitter, f, &strbuf, vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	fprintf(f, "\t/* FDT_END */\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	asm_emit_cell(f, FDT_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	emit_label(f, symprefix, "struct_end");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	emit_label(f, symprefix, "strings_start");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	dump_stringtable_asm(f, strbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	emit_label(f, symprefix, "strings_end");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	emit_label(f, symprefix, "blob_end");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	 * If the user asked for more space than is used, pad it out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	if (minsize > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 			minsize, symprefix, symprefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	if (padsize > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		fprintf(f, "\t.space\t%d, 0\n", padsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	if (alignsize > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		asm_emit_align(f, alignsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	emit_label(f, symprefix, "blob_abs_end");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	data_free(strbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) struct inbuf {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	char *base, *limit, *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static void inbuf_init(struct inbuf *inb, void *base, void *limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	inb->base = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	inb->limit = limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	inb->ptr = inb->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static void flat_read_chunk(struct inbuf *inb, void *p, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	if ((inb->ptr + len) > inb->limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		die("Premature end of data parsing flat device tree\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	memcpy(p, inb->ptr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	inb->ptr += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) static uint32_t flat_read_word(struct inbuf *inb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	fdt32_t val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	flat_read_chunk(inb, &val, sizeof(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	return fdt32_to_cpu(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) static void flat_realign(struct inbuf *inb, int align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	int off = inb->ptr - inb->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	inb->ptr = inb->base + ALIGN(off, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	if (inb->ptr > inb->limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		die("Premature end of data parsing flat device tree\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) static char *flat_read_string(struct inbuf *inb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	int len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	const char *p = inb->ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		if (p >= inb->limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 			die("Premature end of data parsing flat device tree\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	} while ((*p++) != '\0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	str = xstrdup(inb->ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	inb->ptr += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	flat_realign(inb, sizeof(uint32_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	return str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) static struct data flat_read_data(struct inbuf *inb, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	struct data d = empty_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		return empty_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	d = data_grow_for(d, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	d.len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	flat_read_chunk(inb, d.val, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	flat_realign(inb, sizeof(uint32_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	return d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) static char *flat_read_stringtable(struct inbuf *inb, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	const char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	p = inb->base + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		if (p >= inb->limit || p < inb->base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 			die("String offset %d overruns string table\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 			    offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		if (*p == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	return xstrdup(inb->base + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) static struct property *flat_read_property(struct inbuf *dtbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 					   struct inbuf *strbuf, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	uint32_t proplen, stroff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	struct data val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	proplen = flat_read_word(dtbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	stroff = flat_read_word(dtbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	name = flat_read_stringtable(strbuf, stroff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	if ((flags & FTF_VARALIGN) && (proplen >= 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		flat_realign(dtbuf, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	val = flat_read_data(dtbuf, proplen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	return build_property(name, val, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	struct reserve_info *reservelist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	struct reserve_info *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	struct fdt_reserve_entry re;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	 * Each entry is a pair of u64 (addr, size) values for 4 cell_t's.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	 * List terminates at an entry with size equal to zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	 * First pass, count entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		uint64_t address, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		flat_read_chunk(inb, &re, sizeof(re));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		address  = fdt64_to_cpu(re.address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		size = fdt64_to_cpu(re.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		if (size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		new = build_reserve_entry(address, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		reservelist = add_reserve_entry(reservelist, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	return reservelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) static char *nodename_from_path(const char *ppath, const char *cpath)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	int plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	plen = strlen(ppath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	if (!strstarts(cpath, ppath))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 		die("Path \"%s\" is not valid as a child of \"%s\"\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 		    cpath, ppath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	/* root node is a special case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	if (!streq(ppath, "/"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		plen++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	return xstrdup(cpath + plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) static struct node *unflatten_tree(struct inbuf *dtbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 				   struct inbuf *strbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 				   const char *parent_flatname, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	struct node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	char *flatname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	uint32_t val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	node = build_node(NULL, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	flatname = flat_read_string(dtbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	if (flags & FTF_FULLPATH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		node->name = nodename_from_path(parent_flatname, flatname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 		node->name = flatname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		struct node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		val = flat_read_word(dtbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 		case FDT_PROP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 			if (node->children)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 				fprintf(stderr, "Warning: Flat tree input has "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 					"subnodes preceding a property.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 			prop = flat_read_property(dtbuf, strbuf, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 			add_property(node, prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 		case FDT_BEGIN_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 			child = unflatten_tree(dtbuf,strbuf, flatname, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 			add_child(node, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 		case FDT_END_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 		case FDT_END:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 			die("Premature FDT_END in device tree blob\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 		case FDT_NOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 			if (!(flags & FTF_NOPS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 				fprintf(stderr, "Warning: NOP tag found in flat tree"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 					" version <16\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 			/* Ignore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 			die("Invalid opcode word %08x in device tree blob\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 			    val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	} while (val != FDT_END_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	if (node->name != flatname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 		free(flatname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) struct dt_info *dt_from_blob(const char *fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	fdt32_t magic_buf, totalsize_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	uint32_t magic, totalsize, version, size_dt, boot_cpuid_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	uint32_t off_dt, off_str, off_mem_rsvmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	char *blob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	struct fdt_header *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	struct inbuf dtbuf, strbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	struct inbuf memresvbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	int sizeleft;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	struct reserve_info *reservelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	struct node *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	uint32_t val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 	int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 	f = srcfile_relative_open(fname, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	rc = fread(&magic_buf, sizeof(magic_buf), 1, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	if (ferror(f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 		die("Error reading DT blob magic number: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 		    strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	if (rc < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 		if (feof(f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 			die("EOF reading DT blob magic number\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 			die("Mysterious short read reading magic number\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	magic = fdt32_to_cpu(magic_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	if (magic != FDT_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 		die("Blob has incorrect magic number\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	rc = fread(&totalsize_buf, sizeof(totalsize_buf), 1, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	if (ferror(f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 		die("Error reading DT blob size: %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 	if (rc < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 		if (feof(f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 			die("EOF reading DT blob size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 			die("Mysterious short read reading blob size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 	totalsize = fdt32_to_cpu(totalsize_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 	if (totalsize < FDT_V1_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 		die("DT blob size (%d) is too small\n", totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	blob = xmalloc(totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	fdt = (struct fdt_header *)blob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	fdt->magic = cpu_to_fdt32(magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	fdt->totalsize = cpu_to_fdt32(totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 	sizeleft = totalsize - sizeof(magic) - sizeof(totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	p = blob + sizeof(magic)  + sizeof(totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 	while (sizeleft) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 		if (feof(f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 			die("EOF before reading %d bytes of DT blob\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 			    totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 		rc = fread(p, 1, sizeleft, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 		if (ferror(f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 			die("Error reading DT blob: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 			    strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 		sizeleft -= rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 		p += rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	off_dt = fdt32_to_cpu(fdt->off_dt_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 	off_str = fdt32_to_cpu(fdt->off_dt_strings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	off_mem_rsvmap = fdt32_to_cpu(fdt->off_mem_rsvmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 	version = fdt32_to_cpu(fdt->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 	boot_cpuid_phys = fdt32_to_cpu(fdt->boot_cpuid_phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	if (off_mem_rsvmap >= totalsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 		die("Mem Reserve structure offset exceeds total size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	if (off_dt >= totalsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 		die("DT structure offset exceeds total size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 	if (off_str > totalsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 		die("String table offset exceeds total size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 	if (version >= 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 		uint32_t size_str = fdt32_to_cpu(fdt->size_dt_strings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 		if ((off_str+size_str < off_str) || (off_str+size_str > totalsize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 			die("String table extends past total size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 		inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 		inbuf_init(&strbuf, blob + off_str, blob + totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	if (version >= 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 		size_dt = fdt32_to_cpu(fdt->size_dt_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 		if ((off_dt+size_dt < off_dt) || (off_dt+size_dt > totalsize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 			die("Structure block extends past total size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 	if (version < 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 		flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 		flags |= FTF_NOPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 	inbuf_init(&memresvbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 		   blob + off_mem_rsvmap, blob + totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 	inbuf_init(&dtbuf, blob + off_dt, blob + totalsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 	reservelist = flat_read_mem_reserve(&memresvbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	val = flat_read_word(&dtbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 	if (val != FDT_BEGIN_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 		die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 	tree = unflatten_tree(&dtbuf, &strbuf, "", flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 	val = flat_read_word(&dtbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 	if (val != FDT_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 		die("Device tree blob doesn't end with FDT_END\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 	free(blob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 	fclose(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 	return build_dt_info(DTSF_V1, reservelist, tree, boot_cpuid_phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }