blob: fcf71541d8a7e543766f45077ebfc1184c7d6cab [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
David Gibson63dc9c72007-09-18 11:44:04 +10004 *
David Gibsonfc14dad2005-06-08 17:18:34 +10005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
David Gibson63dc9c72007-09-18 11:44:04 +100014 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
David Gibsonfc14dad2005-06-08 17:18:34 +100019 */
20
21#include "dtc.h"
David Gibsona742aad2008-05-16 13:22:09 +100022#include "srcpos.h"
David Gibsonfc14dad2005-06-08 17:18:34 +100023
24#define FTF_FULLPATH 0x1
25#define FTF_VARALIGN 0x2
26#define FTF_NAMEPROPS 0x4
27#define FTF_BOOTCPUID 0x8
28#define FTF_STRTABSIZE 0x10
David Gibson46c88df2007-03-14 11:02:40 +110029#define FTF_STRUCTSIZE 0x20
Milton Millerce243222007-06-09 23:21:31 -050030#define FTF_NOPS 0x40
David Gibsonfc14dad2005-06-08 17:18:34 +100031
David Gibson230f2532005-08-29 12:48:02 +100032static struct version_info {
David Gibsonfc14dad2005-06-08 17:18:34 +100033 int version;
34 int last_comp_version;
35 int hdr_size;
36 int flags;
37} version_table[] = {
David Gibsonfb7c7ac2007-09-26 13:11:05 +100038 {1, 1, FDT_V1_SIZE,
David Gibsonfc14dad2005-06-08 17:18:34 +100039 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS},
David Gibsonfb7c7ac2007-09-26 13:11:05 +100040 {2, 1, FDT_V2_SIZE,
David Gibsonfc14dad2005-06-08 17:18:34 +100041 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID},
David Gibsonfb7c7ac2007-09-26 13:11:05 +100042 {3, 1, FDT_V3_SIZE,
David Gibsonfc14dad2005-06-08 17:18:34 +100043 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID|FTF_STRTABSIZE},
David Gibsonfb7c7ac2007-09-26 13:11:05 +100044 {16, 16, FDT_V3_SIZE,
Milton Millerce243222007-06-09 23:21:31 -050045 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_NOPS},
David Gibsonfb7c7ac2007-09-26 13:11:05 +100046 {17, 16, FDT_V17_SIZE,
Milton Millerce243222007-06-09 23:21:31 -050047 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
David Gibsonfc14dad2005-06-08 17:18:34 +100048};
49
50struct emitter {
51 void (*cell)(void *, cell_t);
Nicolas Iooss9ffdf602017-03-04 14:26:43 +010052 void (*string)(void *, const char *, int);
David Gibsonfc14dad2005-06-08 17:18:34 +100053 void (*align)(void *, int);
54 void (*data)(void *, struct data);
David Gibson05898c62010-02-24 18:22:17 +110055 void (*beginnode)(void *, struct label *labels);
56 void (*endnode)(void *, struct label *labels);
57 void (*property)(void *, struct label *labels);
David Gibsonfc14dad2005-06-08 17:18:34 +100058};
59
60static void bin_emit_cell(void *e, cell_t val)
61{
62 struct data *dtbuf = e;
63
64 *dtbuf = data_append_cell(*dtbuf, val);
65}
66
Nicolas Iooss9ffdf602017-03-04 14:26:43 +010067static void bin_emit_string(void *e, const char *str, int len)
David Gibsonfc14dad2005-06-08 17:18:34 +100068{
69 struct data *dtbuf = e;
70
71 if (len == 0)
72 len = strlen(str);
73
74 *dtbuf = data_append_data(*dtbuf, str, len);
75 *dtbuf = data_append_byte(*dtbuf, '\0');
76}
77
78static void bin_emit_align(void *e, int a)
79{
80 struct data *dtbuf = e;
81
82 *dtbuf = data_append_align(*dtbuf, a);
83}
84
85static void bin_emit_data(void *e, struct data d)
86{
87 struct data *dtbuf = e;
88
89 *dtbuf = data_append_data(*dtbuf, d.val, d.len);
90}
91
David Gibson05898c62010-02-24 18:22:17 +110092static void bin_emit_beginnode(void *e, struct label *labels)
David Gibsonfc14dad2005-06-08 17:18:34 +100093{
David Gibsonfb7c7ac2007-09-26 13:11:05 +100094 bin_emit_cell(e, FDT_BEGIN_NODE);
David Gibsonfc14dad2005-06-08 17:18:34 +100095}
96
David Gibson05898c62010-02-24 18:22:17 +110097static void bin_emit_endnode(void *e, struct label *labels)
David Gibsonfc14dad2005-06-08 17:18:34 +100098{
David Gibsonfb7c7ac2007-09-26 13:11:05 +100099 bin_emit_cell(e, FDT_END_NODE);
David Gibsonfc14dad2005-06-08 17:18:34 +1000100}
101
David Gibson05898c62010-02-24 18:22:17 +1100102static void bin_emit_property(void *e, struct label *labels)
David Gibsonfc14dad2005-06-08 17:18:34 +1000103{
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000104 bin_emit_cell(e, FDT_PROP);
David Gibsonfc14dad2005-06-08 17:18:34 +1000105}
106
David Gibson230f2532005-08-29 12:48:02 +1000107static struct emitter bin_emitter = {
David Gibsonfc14dad2005-06-08 17:18:34 +1000108 .cell = bin_emit_cell,
109 .string = bin_emit_string,
110 .align = bin_emit_align,
111 .data = bin_emit_data,
112 .beginnode = bin_emit_beginnode,
113 .endnode = bin_emit_endnode,
David Gibson63dc9c72007-09-18 11:44:04 +1000114 .property = bin_emit_property,
David Gibsonfc14dad2005-06-08 17:18:34 +1000115};
116
David Gibson92cb9a22007-12-04 14:26:15 +1100117static void emit_label(FILE *f, const char *prefix, const char *label)
David Gibson4102d842005-06-16 14:36:37 +1000118{
119 fprintf(f, "\t.globl\t%s_%s\n", prefix, label);
120 fprintf(f, "%s_%s:\n", prefix, label);
121 fprintf(f, "_%s_%s:\n", prefix, label);
122}
123
David Gibson92cb9a22007-12-04 14:26:15 +1100124static void emit_offset_label(FILE *f, const char *label, int offset)
Milton Miller6a99b132007-07-07 01:18:51 -0500125{
126 fprintf(f, "\t.globl\t%s\n", label);
127 fprintf(f, "%s\t= . + %d\n", label, offset);
128}
129
David Gibson26d93f62009-01-08 11:47:55 +1100130#define ASM_EMIT_BELONG(f, fmt, ...) \
131 { \
132 fprintf((f), "\t.byte\t((" fmt ") >> 24) & 0xff\n", __VA_ARGS__); \
133 fprintf((f), "\t.byte\t((" fmt ") >> 16) & 0xff\n", __VA_ARGS__); \
134 fprintf((f), "\t.byte\t((" fmt ") >> 8) & 0xff\n", __VA_ARGS__); \
135 fprintf((f), "\t.byte\t(" fmt ") & 0xff\n", __VA_ARGS__); \
136 }
137
David Gibsonfc14dad2005-06-08 17:18:34 +1000138static void asm_emit_cell(void *e, cell_t val)
139{
140 FILE *f = e;
141
David Gibson26d93f62009-01-08 11:47:55 +1100142 fprintf(f, "\t.byte 0x%02x; .byte 0x%02x; .byte 0x%02x; .byte 0x%02x\n",
143 (val >> 24) & 0xff, (val >> 16) & 0xff,
144 (val >> 8) & 0xff, val & 0xff);
David Gibsonfc14dad2005-06-08 17:18:34 +1000145}
146
Nicolas Iooss9ffdf602017-03-04 14:26:43 +0100147static void asm_emit_string(void *e, const char *str, int len)
David Gibsonfc14dad2005-06-08 17:18:34 +1000148{
149 FILE *f = e;
David Gibsonfc14dad2005-06-08 17:18:34 +1000150
Nicolas Iooss9ffdf602017-03-04 14:26:43 +0100151 if (len != 0)
152 fprintf(f, "\t.string\t\"%.*s\"\n", len, str);
153 else
154 fprintf(f, "\t.string\t\"%s\"\n", str);
David Gibsonfc14dad2005-06-08 17:18:34 +1000155}
156
157static void asm_emit_align(void *e, int a)
158{
159 FILE *f = e;
160
David Gibsonb31b2712009-11-12 13:30:02 +1100161 fprintf(f, "\t.balign\t%d, 0\n", a);
David Gibsonfc14dad2005-06-08 17:18:34 +1000162}
163
164static void asm_emit_data(void *e, struct data d)
165{
166 FILE *f = e;
167 int off = 0;
David Gibson5ac97df2008-02-28 20:58:28 +1100168 struct marker *m = d.markers;
Milton Miller6a99b132007-07-07 01:18:51 -0500169
David Gibson5ac97df2008-02-28 20:58:28 +1100170 for_each_marker_of_type(m, LABEL)
171 emit_offset_label(f, m->ref, m->offset);
David Gibsonfc14dad2005-06-08 17:18:34 +1000172
David Gibson53359012008-06-25 13:53:07 +1000173 while ((d.len - off) >= sizeof(uint32_t)) {
David Gibsonbad5b282017-03-06 12:08:53 +1100174 asm_emit_cell(e, fdt32_to_cpu(*((fdt32_t *)(d.val+off))));
David Gibson53359012008-06-25 13:53:07 +1000175 off += sizeof(uint32_t);
David Gibsonfc14dad2005-06-08 17:18:34 +1000176 }
177
David Gibsonc8c374b2008-06-25 14:27:53 +1000178 while ((d.len - off) >= 1) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000179 fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]);
180 off += 1;
181 }
182
183 assert(off == d.len);
184}
185
David Gibson05898c62010-02-24 18:22:17 +1100186static void asm_emit_beginnode(void *e, struct label *labels)
David Gibsonfc14dad2005-06-08 17:18:34 +1000187{
188 FILE *f = e;
David Gibson05898c62010-02-24 18:22:17 +1100189 struct label *l;
David Gibsonfc14dad2005-06-08 17:18:34 +1000190
David Gibson05898c62010-02-24 18:22:17 +1100191 for_each_label(labels, l) {
192 fprintf(f, "\t.globl\t%s\n", l->label);
193 fprintf(f, "%s:\n", l->label);
David Gibson4102d842005-06-16 14:36:37 +1000194 }
David Gibson26d93f62009-01-08 11:47:55 +1100195 fprintf(f, "\t/* FDT_BEGIN_NODE */\n");
196 asm_emit_cell(e, FDT_BEGIN_NODE);
David Gibsonfc14dad2005-06-08 17:18:34 +1000197}
198
David Gibson05898c62010-02-24 18:22:17 +1100199static void asm_emit_endnode(void *e, struct label *labels)
David Gibsonfc14dad2005-06-08 17:18:34 +1000200{
201 FILE *f = e;
David Gibson05898c62010-02-24 18:22:17 +1100202 struct label *l;
David Gibsonfc14dad2005-06-08 17:18:34 +1000203
David Gibson26d93f62009-01-08 11:47:55 +1100204 fprintf(f, "\t/* FDT_END_NODE */\n");
205 asm_emit_cell(e, FDT_END_NODE);
David Gibson05898c62010-02-24 18:22:17 +1100206 for_each_label(labels, l) {
207 fprintf(f, "\t.globl\t%s_end\n", l->label);
208 fprintf(f, "%s_end:\n", l->label);
David Gibson4102d842005-06-16 14:36:37 +1000209 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000210}
211
David Gibson05898c62010-02-24 18:22:17 +1100212static void asm_emit_property(void *e, struct label *labels)
David Gibsonfc14dad2005-06-08 17:18:34 +1000213{
214 FILE *f = e;
David Gibson05898c62010-02-24 18:22:17 +1100215 struct label *l;
David Gibsonfc14dad2005-06-08 17:18:34 +1000216
David Gibson05898c62010-02-24 18:22:17 +1100217 for_each_label(labels, l) {
218 fprintf(f, "\t.globl\t%s\n", l->label);
219 fprintf(f, "%s:\n", l->label);
David Gibson4102d842005-06-16 14:36:37 +1000220 }
David Gibson26d93f62009-01-08 11:47:55 +1100221 fprintf(f, "\t/* FDT_PROP */\n");
222 asm_emit_cell(e, FDT_PROP);
David Gibsonfc14dad2005-06-08 17:18:34 +1000223}
224
David Gibson230f2532005-08-29 12:48:02 +1000225static struct emitter asm_emitter = {
David Gibsonfc14dad2005-06-08 17:18:34 +1000226 .cell = asm_emit_cell,
227 .string = asm_emit_string,
228 .align = asm_emit_align,
229 .data = asm_emit_data,
230 .beginnode = asm_emit_beginnode,
231 .endnode = asm_emit_endnode,
David Gibson63dc9c72007-09-18 11:44:04 +1000232 .property = asm_emit_property,
David Gibsonfc14dad2005-06-08 17:18:34 +1000233};
234
David Gibson92cb9a22007-12-04 14:26:15 +1100235static int stringtable_insert(struct data *d, const char *str)
David Gibsonfc14dad2005-06-08 17:18:34 +1000236{
237 int i;
238
239 /* FIXME: do this more efficiently? */
240
241 for (i = 0; i < d->len; i++) {
242 if (streq(str, d->val + i))
243 return i;
244 }
245
246 *d = data_append_data(*d, str, strlen(str)+1);
David Gibsona6c69572005-07-11 17:09:42 +1000247 return i;
David Gibsonfc14dad2005-06-08 17:18:34 +1000248}
249
250static void flatten_tree(struct node *tree, struct emitter *emit,
251 void *etarget, struct data *strbuf,
252 struct version_info *vi)
253{
254 struct property *prop;
255 struct node *child;
David Gibson17625372013-10-28 21:06:53 +1100256 bool seen_name_prop = false;
David Gibsonfc14dad2005-06-08 17:18:34 +1000257
Stephen Warren45013d82012-08-07 22:50:15 -0600258 if (tree->deleted)
259 return;
260
David Gibson05898c62010-02-24 18:22:17 +1100261 emit->beginnode(etarget, tree->labels);
David Gibsonfc14dad2005-06-08 17:18:34 +1000262
263 if (vi->flags & FTF_FULLPATH)
264 emit->string(etarget, tree->fullpath, 0);
265 else
266 emit->string(etarget, tree->name, 0);
267
268 emit->align(etarget, sizeof(cell_t));
269
270 for_each_property(tree, prop) {
271 int nameoff;
272
273 if (streq(prop->name, "name"))
David Gibson17625372013-10-28 21:06:53 +1100274 seen_name_prop = true;
David Gibsonfc14dad2005-06-08 17:18:34 +1000275
276 nameoff = stringtable_insert(strbuf, prop->name);
277
David Gibson05898c62010-02-24 18:22:17 +1100278 emit->property(etarget, prop->labels);
David Gibsonfc14dad2005-06-08 17:18:34 +1000279 emit->cell(etarget, prop->val.len);
280 emit->cell(etarget, nameoff);
281
282 if ((vi->flags & FTF_VARALIGN) && (prop->val.len >= 8))
283 emit->align(etarget, 8);
284
285 emit->data(etarget, prop->val);
286 emit->align(etarget, sizeof(cell_t));
287 }
288
289 if ((vi->flags & FTF_NAMEPROPS) && !seen_name_prop) {
David Gibson4102d842005-06-16 14:36:37 +1000290 emit->property(etarget, NULL);
David Gibsonfc14dad2005-06-08 17:18:34 +1000291 emit->cell(etarget, tree->basenamelen+1);
292 emit->cell(etarget, stringtable_insert(strbuf, "name"));
293
294 if ((vi->flags & FTF_VARALIGN) && ((tree->basenamelen+1) >= 8))
295 emit->align(etarget, 8);
296
297 emit->string(etarget, tree->name, tree->basenamelen);
David Gibson41916132005-08-25 15:39:09 +1000298 emit->align(etarget, sizeof(cell_t));
David Gibsonfc14dad2005-06-08 17:18:34 +1000299 }
300
301 for_each_child(tree, child) {
302 flatten_tree(child, emit, etarget, strbuf, vi);
303 }
304
David Gibson05898c62010-02-24 18:22:17 +1100305 emit->endnode(etarget, tree->labels);
David Gibsonfc14dad2005-06-08 17:18:34 +1000306}
307
David Gibsonf040d952005-10-24 18:18:38 +1000308static struct data flatten_reserve_list(struct reserve_info *reservelist,
309 struct version_info *vi)
310{
311 struct reserve_info *re;
312 struct data d = empty_data;
Jerry Van Baren4384b232007-04-04 22:04:33 -0400313 int j;
David Gibsonf040d952005-10-24 18:18:38 +1000314
315 for (re = reservelist; re; re = re->next) {
David Gibson49300f22017-03-06 12:04:45 +1100316 d = data_append_re(d, re->address, re->size);
David Gibsonf040d952005-10-24 18:18:38 +1000317 }
Jerry Van Baren4384b232007-04-04 22:04:33 -0400318 /*
319 * Add additional reserved slots if the user asked for them.
320 */
321 for (j = 0; j < reservenum; j++) {
David Gibson49300f22017-03-06 12:04:45 +1100322 d = data_append_re(d, 0, 0);
Jerry Van Baren4384b232007-04-04 22:04:33 -0400323 }
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400324
David Gibsonf040d952005-10-24 18:18:38 +1000325 return d;
326}
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400327
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000328static void make_fdt_header(struct fdt_header *fdt,
329 struct version_info *vi,
330 int reservesize, int dtsize, int strsize,
331 int boot_cpuid_phys)
David Gibsonfc14dad2005-06-08 17:18:34 +1000332{
David Gibson47f23de2005-07-11 17:19:26 +1000333 int reserve_off;
David Gibsonf040d952005-10-24 18:18:38 +1000334
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000335 reservesize += sizeof(struct fdt_reserve_entry);
David Gibsonfc14dad2005-06-08 17:18:34 +1000336
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000337 memset(fdt, 0xff, sizeof(*fdt));
David Gibsonfc14dad2005-06-08 17:18:34 +1000338
David Gibsonc8c374b2008-06-25 14:27:53 +1000339 fdt->magic = cpu_to_fdt32(FDT_MAGIC);
340 fdt->version = cpu_to_fdt32(vi->version);
341 fdt->last_comp_version = cpu_to_fdt32(vi->last_comp_version);
David Gibsonfc14dad2005-06-08 17:18:34 +1000342
David Gibson47f23de2005-07-11 17:19:26 +1000343 /* Reserve map should be doubleword aligned */
344 reserve_off = ALIGN(vi->hdr_size, 8);
345
David Gibsonc8c374b2008-06-25 14:27:53 +1000346 fdt->off_mem_rsvmap = cpu_to_fdt32(reserve_off);
347 fdt->off_dt_struct = cpu_to_fdt32(reserve_off + reservesize);
348 fdt->off_dt_strings = cpu_to_fdt32(reserve_off + reservesize
David Gibsonfc14dad2005-06-08 17:18:34 +1000349 + dtsize);
David Gibsonc8c374b2008-06-25 14:27:53 +1000350 fdt->totalsize = cpu_to_fdt32(reserve_off + reservesize + dtsize + strsize);
Jerry Van Baren4384b232007-04-04 22:04:33 -0400351
David Gibsonfc14dad2005-06-08 17:18:34 +1000352 if (vi->flags & FTF_BOOTCPUID)
David Gibsonc8c374b2008-06-25 14:27:53 +1000353 fdt->boot_cpuid_phys = cpu_to_fdt32(boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000354 if (vi->flags & FTF_STRTABSIZE)
David Gibsonc8c374b2008-06-25 14:27:53 +1000355 fdt->size_dt_strings = cpu_to_fdt32(strsize);
David Gibson46c88df2007-03-14 11:02:40 +1100356 if (vi->flags & FTF_STRUCTSIZE)
David Gibsonc8c374b2008-06-25 14:27:53 +1000357 fdt->size_dt_struct = cpu_to_fdt32(dtsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000358}
359
David Gibson00fbb862016-05-31 11:58:42 +1000360void dt_to_blob(FILE *f, struct dt_info *dti, int version)
David Gibsonfc14dad2005-06-08 17:18:34 +1000361{
362 struct version_info *vi = NULL;
363 int i;
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400364 struct data blob = empty_data;
365 struct data reservebuf = empty_data;
366 struct data dtbuf = empty_data;
367 struct data strbuf = empty_data;
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000368 struct fdt_header fdt;
Kumar Gala2b7dc8d2007-11-28 10:21:12 -0600369 int padlen = 0;
David Gibsonfc14dad2005-06-08 17:18:34 +1000370
371 for (i = 0; i < ARRAY_SIZE(version_table); i++) {
372 if (version_table[i].version == version)
373 vi = &version_table[i];
374 }
375 if (!vi)
376 die("Unknown device tree blob version %d\n", version);
377
David Gibson00fbb862016-05-31 11:58:42 +1000378 flatten_tree(dti->dt, &bin_emitter, &dtbuf, &strbuf, vi);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000379 bin_emit_cell(&dtbuf, FDT_END);
David Gibsonfc14dad2005-06-08 17:18:34 +1000380
David Gibson00fbb862016-05-31 11:58:42 +1000381 reservebuf = flatten_reserve_list(dti->reservelist, vi);
David Gibsonf040d952005-10-24 18:18:38 +1000382
David Gibsonf0517db2005-07-15 17:14:24 +1000383 /* Make header */
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000384 make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len,
David Gibson00fbb862016-05-31 11:58:42 +1000385 dti->boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000386
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400387 /*
Jerry Van Baren7ea144f2007-04-19 22:22:35 -0400388 * If the user asked for more space than is used, adjust the totalsize.
389 */
Kumar Gala2b7dc8d2007-11-28 10:21:12 -0600390 if (minsize > 0) {
David Gibsonc8c374b2008-06-25 14:27:53 +1000391 padlen = minsize - fdt32_to_cpu(fdt.totalsize);
Tim Wang874f4052016-07-18 15:56:53 +0800392 if (padlen < 0) {
393 padlen = 0;
394 if (quiet < 1)
395 fprintf(stderr,
396 "Warning: blob size %d >= minimum size %d\n",
397 fdt32_to_cpu(fdt.totalsize), minsize);
398 }
Jerry Van Baren7ea144f2007-04-19 22:22:35 -0400399 }
400
Kumar Gala2b7dc8d2007-11-28 10:21:12 -0600401 if (padsize > 0)
402 padlen = padsize;
403
Tim Wang874f4052016-07-18 15:56:53 +0800404 if (alignsize > 0)
405 padlen = ALIGN(fdt32_to_cpu(fdt.totalsize) + padlen, alignsize)
406 - fdt32_to_cpu(fdt.totalsize);
407
Kumar Gala80c72a82007-12-04 17:36:08 -0600408 if (padlen > 0) {
David Gibsonc8c374b2008-06-25 14:27:53 +1000409 int tsize = fdt32_to_cpu(fdt.totalsize);
Kumar Gala80c72a82007-12-04 17:36:08 -0600410 tsize += padlen;
David Gibsonc8c374b2008-06-25 14:27:53 +1000411 fdt.totalsize = cpu_to_fdt32(tsize);
Kumar Gala80c72a82007-12-04 17:36:08 -0600412 }
413
Jerry Van Baren7ea144f2007-04-19 22:22:35 -0400414 /*
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400415 * Assemble the blob: start with the header, add with alignment
416 * the reserve buffer, add the reserve map terminating zeroes,
417 * the device tree itself, and finally the strings.
418 */
David Gibsona266e5c2008-02-28 20:55:37 +1100419 blob = data_append_data(blob, &fdt, vi->hdr_size);
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400420 blob = data_append_align(blob, 8);
421 blob = data_merge(blob, reservebuf);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000422 blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry));
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400423 blob = data_merge(blob, dtbuf);
424 blob = data_merge(blob, strbuf);
David Gibson47f23de2005-07-11 17:19:26 +1000425
David Gibsonf0517db2005-07-15 17:14:24 +1000426 /*
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400427 * If the user asked for more space than is used, pad out the blob.
David Gibsonf0517db2005-07-15 17:14:24 +1000428 */
Kumar Gala80c72a82007-12-04 17:36:08 -0600429 if (padlen > 0)
Jerry Van Baren7ea144f2007-04-19 22:22:35 -0400430 blob = data_append_zeroes(blob, padlen);
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400431
David Gibson0783d7e2008-11-07 11:41:11 +1100432 if (fwrite(blob.val, blob.len, 1, f) != 1) {
433 if (ferror(f))
434 die("Error writing device tree blob: %s\n",
435 strerror(errno));
436 else
437 die("Short write on device tree blob\n");
438 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000439
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400440 /*
441 * data_merge() frees the right-hand element so only the blob
442 * remains to be freed.
443 */
444 data_free(blob);
David Gibsonfc14dad2005-06-08 17:18:34 +1000445}
446
David Gibson230f2532005-08-29 12:48:02 +1000447static void dump_stringtable_asm(FILE *f, struct data strbuf)
David Gibsonfc14dad2005-06-08 17:18:34 +1000448{
David Gibson92cb9a22007-12-04 14:26:15 +1100449 const char *p;
David Gibsonfc14dad2005-06-08 17:18:34 +1000450 int len;
451
452 p = strbuf.val;
453
454 while (p < (strbuf.val + strbuf.len)) {
455 len = strlen(p);
456 fprintf(f, "\t.string \"%s\"\n", p);
457 p += len+1;
458 }
459}
460
David Gibson00fbb862016-05-31 11:58:42 +1000461void dt_to_asm(FILE *f, struct dt_info *dti, int version)
David Gibsonfc14dad2005-06-08 17:18:34 +1000462{
463 struct version_info *vi = NULL;
464 int i;
465 struct data strbuf = empty_data;
David Gibsonf040d952005-10-24 18:18:38 +1000466 struct reserve_info *re;
David Gibson92cb9a22007-12-04 14:26:15 +1100467 const char *symprefix = "dt";
David Gibsonfc14dad2005-06-08 17:18:34 +1000468
469 for (i = 0; i < ARRAY_SIZE(version_table); i++) {
470 if (version_table[i].version == version)
471 vi = &version_table[i];
472 }
473 if (!vi)
474 die("Unknown device tree blob version %d\n", version);
475
476 fprintf(f, "/* autogenerated by dtc, do not edit */\n\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000477
478 emit_label(f, symprefix, "blob_start");
479 emit_label(f, symprefix, "header");
David Gibson26d93f62009-01-08 11:47:55 +1100480 fprintf(f, "\t/* magic */\n");
481 asm_emit_cell(f, FDT_MAGIC);
482 fprintf(f, "\t/* totalsize */\n");
483 ASM_EMIT_BELONG(f, "_%s_blob_abs_end - _%s_blob_start",
David Gibsonfc14dad2005-06-08 17:18:34 +1000484 symprefix, symprefix);
David Gibson26d93f62009-01-08 11:47:55 +1100485 fprintf(f, "\t/* off_dt_struct */\n");
486 ASM_EMIT_BELONG(f, "_%s_struct_start - _%s_blob_start",
487 symprefix, symprefix);
488 fprintf(f, "\t/* off_dt_strings */\n");
489 ASM_EMIT_BELONG(f, "_%s_strings_start - _%s_blob_start",
490 symprefix, symprefix);
491 fprintf(f, "\t/* off_mem_rsvmap */\n");
492 ASM_EMIT_BELONG(f, "_%s_reserve_map - _%s_blob_start",
493 symprefix, symprefix);
494 fprintf(f, "\t/* version */\n");
495 asm_emit_cell(f, vi->version);
496 fprintf(f, "\t/* last_comp_version */\n");
497 asm_emit_cell(f, vi->last_comp_version);
David Gibsonfc14dad2005-06-08 17:18:34 +1000498
David Gibson26d93f62009-01-08 11:47:55 +1100499 if (vi->flags & FTF_BOOTCPUID) {
500 fprintf(f, "\t/* boot_cpuid_phys */\n");
David Gibson00fbb862016-05-31 11:58:42 +1000501 asm_emit_cell(f, dti->boot_cpuid_phys);
David Gibson26d93f62009-01-08 11:47:55 +1100502 }
503
504 if (vi->flags & FTF_STRTABSIZE) {
505 fprintf(f, "\t/* size_dt_strings */\n");
506 ASM_EMIT_BELONG(f, "_%s_strings_end - _%s_strings_start",
507 symprefix, symprefix);
508 }
509
510 if (vi->flags & FTF_STRUCTSIZE) {
511 fprintf(f, "\t/* size_dt_struct */\n");
512 ASM_EMIT_BELONG(f, "_%s_struct_end - _%s_struct_start",
Milton Miller81fda8a2007-07-07 01:18:47 -0500513 symprefix, symprefix);
David Gibson26d93f62009-01-08 11:47:55 +1100514 }
Milton Miller81fda8a2007-07-07 01:18:47 -0500515
David Gibsonf0517db2005-07-15 17:14:24 +1000516 /*
517 * Reserve map entries.
518 * Align the reserve map to a doubleword boundary.
519 * Each entry is an (address, size) pair of u64 values.
David Gibsonf0517db2005-07-15 17:14:24 +1000520 * Always supply a zero-sized temination entry.
521 */
David Gibson47f23de2005-07-11 17:19:26 +1000522 asm_emit_align(f, 8);
David Gibsonfc14dad2005-06-08 17:18:34 +1000523 emit_label(f, symprefix, "reserve_map");
David Gibsonf0517db2005-07-15 17:14:24 +1000524
David Gibsonf040d952005-10-24 18:18:38 +1000525 fprintf(f, "/* Memory reserve map from source file */\n");
Jon Loeliger05ae3d82006-04-19 11:58:45 -0500526
527 /*
528 * Use .long on high and low halfs of u64s to avoid .quad
529 * as it appears .quad isn't available in some assemblers.
530 */
David Gibson00fbb862016-05-31 11:58:42 +1000531 for (re = dti->reservelist; re; re = re->next) {
David Gibson05898c62010-02-24 18:22:17 +1100532 struct label *l;
533
534 for_each_label(re->labels, l) {
535 fprintf(f, "\t.globl\t%s\n", l->label);
536 fprintf(f, "%s:\n", l->label);
Milton Millerd4290332007-07-07 01:18:49 -0500537 }
David Gibson49300f22017-03-06 12:04:45 +1100538 ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->address >> 32));
David Gibson26d93f62009-01-08 11:47:55 +1100539 ASM_EMIT_BELONG(f, "0x%08x",
David Gibson49300f22017-03-06 12:04:45 +1100540 (unsigned int)(re->address & 0xffffffff));
541 ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->size >> 32));
542 ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->size & 0xffffffff));
David Gibsonfc14dad2005-06-08 17:18:34 +1000543 }
Jerry Van Barenca25e542007-04-17 18:14:41 -0400544 for (i = 0; i < reservenum; i++) {
545 fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
546 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000547
Jon Loeliger05ae3d82006-04-19 11:58:45 -0500548 fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000549
David Gibsonfc14dad2005-06-08 17:18:34 +1000550 emit_label(f, symprefix, "struct_start");
David Gibson00fbb862016-05-31 11:58:42 +1000551 flatten_tree(dti->dt, &asm_emitter, f, &strbuf, vi);
David Gibson26d93f62009-01-08 11:47:55 +1100552
553 fprintf(f, "\t/* FDT_END */\n");
554 asm_emit_cell(f, FDT_END);
David Gibsonfc14dad2005-06-08 17:18:34 +1000555 emit_label(f, symprefix, "struct_end");
556
557 emit_label(f, symprefix, "strings_start");
558 dump_stringtable_asm(f, strbuf);
559 emit_label(f, symprefix, "strings_end");
560
561 emit_label(f, symprefix, "blob_end");
562
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400563 /*
564 * If the user asked for more space than is used, pad it out.
565 */
566 if (minsize > 0) {
567 fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n",
568 minsize, symprefix, symprefix);
569 }
Kumar Gala2b7dc8d2007-11-28 10:21:12 -0600570 if (padsize > 0) {
571 fprintf(f, "\t.space\t%d, 0\n", padsize);
572 }
Tim Wang874f4052016-07-18 15:56:53 +0800573 if (alignsize > 0)
574 asm_emit_align(f, alignsize);
Jerry Van Baren86c01ee2007-04-18 21:59:51 -0400575 emit_label(f, symprefix, "blob_abs_end");
576
David Gibsonfc14dad2005-06-08 17:18:34 +1000577 data_free(strbuf);
578}
579
580struct inbuf {
581 char *base, *limit, *ptr;
582};
583
584static void inbuf_init(struct inbuf *inb, void *base, void *limit)
585{
586 inb->base = base;
587 inb->limit = limit;
588 inb->ptr = inb->base;
589}
590
591static void flat_read_chunk(struct inbuf *inb, void *p, int len)
592{
593 if ((inb->ptr + len) > inb->limit)
594 die("Premature end of data parsing flat device tree\n");
595
596 memcpy(p, inb->ptr, len);
597
598 inb->ptr += len;
599}
600
David Gibson53359012008-06-25 13:53:07 +1000601static uint32_t flat_read_word(struct inbuf *inb)
David Gibsonfc14dad2005-06-08 17:18:34 +1000602{
David Gibsonbad5b282017-03-06 12:08:53 +1100603 fdt32_t val;
David Gibsonfc14dad2005-06-08 17:18:34 +1000604
605 assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
606
607 flat_read_chunk(inb, &val, sizeof(val));
608
David Gibsonc8c374b2008-06-25 14:27:53 +1000609 return fdt32_to_cpu(val);
David Gibsonfc14dad2005-06-08 17:18:34 +1000610}
611
612static void flat_realign(struct inbuf *inb, int align)
613{
614 int off = inb->ptr - inb->base;
615
616 inb->ptr = inb->base + ALIGN(off, align);
617 if (inb->ptr > inb->limit)
618 die("Premature end of data parsing flat device tree\n");
619}
620
621static char *flat_read_string(struct inbuf *inb)
622{
623 int len = 0;
David Gibson92cb9a22007-12-04 14:26:15 +1100624 const char *p = inb->ptr;
David Gibsonfc14dad2005-06-08 17:18:34 +1000625 char *str;
626
627 do {
628 if (p >= inb->limit)
629 die("Premature end of data parsing flat device tree\n");
630 len++;
631 } while ((*p++) != '\0');
632
Jon Loeliger879e4d22008-10-03 11:12:33 -0500633 str = xstrdup(inb->ptr);
David Gibsonfc14dad2005-06-08 17:18:34 +1000634
635 inb->ptr += len;
636
David Gibson53359012008-06-25 13:53:07 +1000637 flat_realign(inb, sizeof(uint32_t));
David Gibsonfc14dad2005-06-08 17:18:34 +1000638
639 return str;
640}
641
642static struct data flat_read_data(struct inbuf *inb, int len)
643{
644 struct data d = empty_data;
645
646 if (len == 0)
647 return empty_data;
648
649 d = data_grow_for(d, len);
650 d.len = len;
651
652 flat_read_chunk(inb, d.val, len);
653
David Gibson53359012008-06-25 13:53:07 +1000654 flat_realign(inb, sizeof(uint32_t));
David Gibsonfc14dad2005-06-08 17:18:34 +1000655
656 return d;
657}
658
659static char *flat_read_stringtable(struct inbuf *inb, int offset)
660{
David Gibson92cb9a22007-12-04 14:26:15 +1100661 const char *p;
David Gibsonfc14dad2005-06-08 17:18:34 +1000662
663 p = inb->base + offset;
664 while (1) {
David Gibson4ddf7c02005-08-19 16:11:11 +1000665 if (p >= inb->limit || p < inb->base)
David Gibson7ee3ffd2005-07-11 16:45:57 +1000666 die("String offset %d overruns string table\n",
667 offset);
David Gibsonfc14dad2005-06-08 17:18:34 +1000668
669 if (*p == '\0')
670 break;
671
672 p++;
673 }
674
Jon Loeliger879e4d22008-10-03 11:12:33 -0500675 return xstrdup(inb->base + offset);
David Gibsonfc14dad2005-06-08 17:18:34 +1000676}
677
David Gibson230f2532005-08-29 12:48:02 +1000678static struct property *flat_read_property(struct inbuf *dtbuf,
679 struct inbuf *strbuf, int flags)
David Gibsonfc14dad2005-06-08 17:18:34 +1000680{
David Gibson53359012008-06-25 13:53:07 +1000681 uint32_t proplen, stroff;
David Gibsonfc14dad2005-06-08 17:18:34 +1000682 char *name;
683 struct data val;
684
685 proplen = flat_read_word(dtbuf);
686 stroff = flat_read_word(dtbuf);
687
688 name = flat_read_stringtable(strbuf, stroff);
689
690 if ((flags & FTF_VARALIGN) && (proplen >= 8))
691 flat_realign(dtbuf, 8);
692
693 val = flat_read_data(dtbuf, proplen);
694
David Gibson05898c62010-02-24 18:22:17 +1100695 return build_property(name, val);
David Gibsonfc14dad2005-06-08 17:18:34 +1000696}
697
David Gibsonf0517db2005-07-15 17:14:24 +1000698
David Gibsonf040d952005-10-24 18:18:38 +1000699static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
David Gibsonf0517db2005-07-15 17:14:24 +1000700{
David Gibsonf040d952005-10-24 18:18:38 +1000701 struct reserve_info *reservelist = NULL;
702 struct reserve_info *new;
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000703 struct fdt_reserve_entry re;
David Gibsonf0517db2005-07-15 17:14:24 +1000704
705 /*
706 * Each entry is a pair of u64 (addr, size) values for 4 cell_t's.
707 * List terminates at an entry with size equal to zero.
708 *
709 * First pass, count entries.
710 */
David Gibson6c0f3672005-08-29 13:36:15 +1000711 while (1) {
David Gibsonbad5b282017-03-06 12:08:53 +1100712 uint64_t address, size;
713
David Gibson6c0f3672005-08-29 13:36:15 +1000714 flat_read_chunk(inb, &re, sizeof(re));
David Gibsonbad5b282017-03-06 12:08:53 +1100715 address = fdt64_to_cpu(re.address);
716 size = fdt64_to_cpu(re.size);
717 if (size == 0)
David Gibson6c0f3672005-08-29 13:36:15 +1000718 break;
David Gibsonf0517db2005-07-15 17:14:24 +1000719
David Gibsonbad5b282017-03-06 12:08:53 +1100720 new = build_reserve_entry(address, size);
David Gibsonf040d952005-10-24 18:18:38 +1000721 reservelist = add_reserve_entry(reservelist, new);
David Gibson6c0f3672005-08-29 13:36:15 +1000722 }
David Gibsonf0517db2005-07-15 17:14:24 +1000723
David Gibsonf040d952005-10-24 18:18:38 +1000724 return reservelist;
David Gibsonf0517db2005-07-15 17:14:24 +1000725}
726
727
David Gibson92cb9a22007-12-04 14:26:15 +1100728static char *nodename_from_path(const char *ppath, const char *cpath)
David Gibsonfc14dad2005-06-08 17:18:34 +1000729{
David Gibsonfc14dad2005-06-08 17:18:34 +1000730 int plen;
731
David Gibsonb2de5182008-02-29 16:51:28 +1100732 plen = strlen(ppath);
David Gibsonfc14dad2005-06-08 17:18:34 +1000733
David Gibsonb2de5182008-02-29 16:51:28 +1100734 if (!strneq(ppath, cpath, plen))
735 die("Path \"%s\" is not valid as a child of \"%s\"\n",
736 cpath, ppath);
David Gibsonfc14dad2005-06-08 17:18:34 +1000737
David Gibsonb2de5182008-02-29 16:51:28 +1100738 /* root node is a special case */
739 if (!streq(ppath, "/"))
740 plen++;
David Gibsonfc14dad2005-06-08 17:18:34 +1000741
Jon Loeliger879e4d22008-10-03 11:12:33 -0500742 return xstrdup(cpath + plen);
David Gibsonfc14dad2005-06-08 17:18:34 +1000743}
744
745static struct node *unflatten_tree(struct inbuf *dtbuf,
746 struct inbuf *strbuf,
David Gibsonb2de5182008-02-29 16:51:28 +1100747 const char *parent_flatname, int flags)
David Gibsonfc14dad2005-06-08 17:18:34 +1000748{
749 struct node *node;
David Gibsonb2de5182008-02-29 16:51:28 +1100750 char *flatname;
David Gibson53359012008-06-25 13:53:07 +1000751 uint32_t val;
David Gibsonfc14dad2005-06-08 17:18:34 +1000752
753 node = build_node(NULL, NULL);
754
David Gibsonb2de5182008-02-29 16:51:28 +1100755 flatname = flat_read_string(dtbuf);
David Gibsonfc14dad2005-06-08 17:18:34 +1000756
David Gibsonb2de5182008-02-29 16:51:28 +1100757 if (flags & FTF_FULLPATH)
758 node->name = nodename_from_path(parent_flatname, flatname);
759 else
760 node->name = flatname;
David Gibsonfc14dad2005-06-08 17:18:34 +1000761
762 do {
763 struct property *prop;
764 struct node *child;
765
766 val = flat_read_word(dtbuf);
767 switch (val) {
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000768 case FDT_PROP:
David Gibson592ea582007-09-04 10:43:03 +1000769 if (node->children)
770 fprintf(stderr, "Warning: Flat tree input has "
771 "subnodes preceding a property.\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000772 prop = flat_read_property(dtbuf, strbuf, flags);
773 add_property(node, prop);
774 break;
775
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000776 case FDT_BEGIN_NODE:
David Gibsonb2de5182008-02-29 16:51:28 +1100777 child = unflatten_tree(dtbuf,strbuf, flatname, flags);
David Gibsonfc14dad2005-06-08 17:18:34 +1000778 add_child(node, child);
779 break;
780
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000781 case FDT_END_NODE:
David Gibsonfc14dad2005-06-08 17:18:34 +1000782 break;
783
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000784 case FDT_END:
785 die("Premature FDT_END in device tree blob\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000786 break;
787
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000788 case FDT_NOP:
David Gibson07387742007-06-26 11:30:47 +1000789 if (!(flags & FTF_NOPS))
790 fprintf(stderr, "Warning: NOP tag found in flat tree"
791 " version <16\n");
Milton Millerce243222007-06-09 23:21:31 -0500792
David Gibson07387742007-06-26 11:30:47 +1000793 /* Ignore */
Milton Millerce243222007-06-09 23:21:31 -0500794 break;
795
David Gibsonfc14dad2005-06-08 17:18:34 +1000796 default:
797 die("Invalid opcode word %08x in device tree blob\n",
798 val);
799 }
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000800 } while (val != FDT_END_NODE);
David Gibsonfc14dad2005-06-08 17:18:34 +1000801
Jean-Christophe Duboiscb9241a2016-07-11 00:16:52 +0200802 if (node->name != flatname) {
803 free(flatname);
804 }
805
David Gibsonfc14dad2005-06-08 17:18:34 +1000806 return node;
807}
808
David Gibsonf0517db2005-07-15 17:14:24 +1000809
David Gibson00fbb862016-05-31 11:58:42 +1000810struct dt_info *dt_from_blob(const char *fname)
David Gibsonfc14dad2005-06-08 17:18:34 +1000811{
David Gibsond68cb362009-12-08 14:24:42 +1100812 FILE *f;
David Gibsonbad5b282017-03-06 12:08:53 +1100813 fdt32_t magic_buf, totalsize_buf;
David Gibson53359012008-06-25 13:53:07 +1000814 uint32_t magic, totalsize, version, size_dt, boot_cpuid_phys;
815 uint32_t off_dt, off_str, off_mem_rsvmap;
David Gibsonfc14dad2005-06-08 17:18:34 +1000816 int rc;
817 char *blob;
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000818 struct fdt_header *fdt;
David Gibsonfc14dad2005-06-08 17:18:34 +1000819 char *p;
820 struct inbuf dtbuf, strbuf;
David Gibsonf0517db2005-07-15 17:14:24 +1000821 struct inbuf memresvbuf;
David Gibsonfc14dad2005-06-08 17:18:34 +1000822 int sizeleft;
David Gibsonf040d952005-10-24 18:18:38 +1000823 struct reserve_info *reservelist;
David Gibsonfc14dad2005-06-08 17:18:34 +1000824 struct node *tree;
David Gibson53359012008-06-25 13:53:07 +1000825 uint32_t val;
David Gibsonfc14dad2005-06-08 17:18:34 +1000826 int flags = 0;
827
David Gibsond68cb362009-12-08 14:24:42 +1100828 f = srcfile_relative_open(fname, NULL);
David Gibsona742aad2008-05-16 13:22:09 +1000829
David Gibsonbad5b282017-03-06 12:08:53 +1100830 rc = fread(&magic_buf, sizeof(magic_buf), 1, f);
David Gibsond68cb362009-12-08 14:24:42 +1100831 if (ferror(f))
David Gibsonfc14dad2005-06-08 17:18:34 +1000832 die("Error reading DT blob magic number: %s\n",
833 strerror(errno));
834 if (rc < 1) {
David Gibsond68cb362009-12-08 14:24:42 +1100835 if (feof(f))
David Gibsonfc14dad2005-06-08 17:18:34 +1000836 die("EOF reading DT blob magic number\n");
837 else
838 die("Mysterious short read reading magic number\n");
839 }
840
David Gibsonbad5b282017-03-06 12:08:53 +1100841 magic = fdt32_to_cpu(magic_buf);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000842 if (magic != FDT_MAGIC)
David Gibsonfc14dad2005-06-08 17:18:34 +1000843 die("Blob has incorrect magic number\n");
844
David Gibsonbad5b282017-03-06 12:08:53 +1100845 rc = fread(&totalsize_buf, sizeof(totalsize_buf), 1, f);
David Gibsond68cb362009-12-08 14:24:42 +1100846 if (ferror(f))
David Gibsonfc14dad2005-06-08 17:18:34 +1000847 die("Error reading DT blob size: %s\n", strerror(errno));
848 if (rc < 1) {
David Gibsond68cb362009-12-08 14:24:42 +1100849 if (feof(f))
David Gibsonfc14dad2005-06-08 17:18:34 +1000850 die("EOF reading DT blob size\n");
851 else
852 die("Mysterious short read reading blob size\n");
853 }
854
David Gibsonbad5b282017-03-06 12:08:53 +1100855 totalsize = fdt32_to_cpu(totalsize_buf);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000856 if (totalsize < FDT_V1_SIZE)
David Gibsonfc14dad2005-06-08 17:18:34 +1000857 die("DT blob size (%d) is too small\n", totalsize);
858
859 blob = xmalloc(totalsize);
860
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000861 fdt = (struct fdt_header *)blob;
David Gibsonc8c374b2008-06-25 14:27:53 +1000862 fdt->magic = cpu_to_fdt32(magic);
863 fdt->totalsize = cpu_to_fdt32(totalsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000864
865 sizeleft = totalsize - sizeof(magic) - sizeof(totalsize);
866 p = blob + sizeof(magic) + sizeof(totalsize);
867
868 while (sizeleft) {
David Gibsond68cb362009-12-08 14:24:42 +1100869 if (feof(f))
David Gibsonfc14dad2005-06-08 17:18:34 +1000870 die("EOF before reading %d bytes of DT blob\n",
871 totalsize);
872
David Gibsond68cb362009-12-08 14:24:42 +1100873 rc = fread(p, 1, sizeleft, f);
874 if (ferror(f))
David Gibsonfc14dad2005-06-08 17:18:34 +1000875 die("Error reading DT blob: %s\n",
876 strerror(errno));
877
878 sizeleft -= rc;
879 p += rc;
880 }
881
David Gibsonc8c374b2008-06-25 14:27:53 +1000882 off_dt = fdt32_to_cpu(fdt->off_dt_struct);
883 off_str = fdt32_to_cpu(fdt->off_dt_strings);
884 off_mem_rsvmap = fdt32_to_cpu(fdt->off_mem_rsvmap);
885 version = fdt32_to_cpu(fdt->version);
886 boot_cpuid_phys = fdt32_to_cpu(fdt->boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000887
David Gibsonf0517db2005-07-15 17:14:24 +1000888 if (off_mem_rsvmap >= totalsize)
889 die("Mem Reserve structure offset exceeds total size\n");
890
David Gibsonfc14dad2005-06-08 17:18:34 +1000891 if (off_dt >= totalsize)
892 die("DT structure offset exceeds total size\n");
893
894 if (off_str > totalsize)
895 die("String table offset exceeds total size\n");
896
David Gibsonfc14dad2005-06-08 17:18:34 +1000897 if (version >= 3) {
David Gibsonc8c374b2008-06-25 14:27:53 +1000898 uint32_t size_str = fdt32_to_cpu(fdt->size_dt_strings);
Anton Blanchard2e53f9d2016-01-03 08:43:35 +1100899 if ((off_str+size_str < off_str) || (off_str+size_str > totalsize))
David Gibsonfc14dad2005-06-08 17:18:34 +1000900 die("String table extends past total size\n");
David Gibsona266e5c2008-02-28 20:55:37 +1100901 inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str);
902 } else {
903 inbuf_init(&strbuf, blob + off_str, blob + totalsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000904 }
David Gibson46c88df2007-03-14 11:02:40 +1100905
906 if (version >= 17) {
David Gibsonc8c374b2008-06-25 14:27:53 +1000907 size_dt = fdt32_to_cpu(fdt->size_dt_struct);
Anton Blanchard2e53f9d2016-01-03 08:43:35 +1100908 if ((off_dt+size_dt < off_dt) || (off_dt+size_dt > totalsize))
David Gibson46c88df2007-03-14 11:02:40 +1100909 die("Structure block extends past total size\n");
910 }
David Gibson63dc9c72007-09-18 11:44:04 +1000911
David Gibson46c88df2007-03-14 11:02:40 +1100912 if (version < 16) {
David Gibsonfc14dad2005-06-08 17:18:34 +1000913 flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN;
Milton Millerce243222007-06-09 23:21:31 -0500914 } else {
915 flags |= FTF_NOPS;
David Gibsonfc14dad2005-06-08 17:18:34 +1000916 }
917
David Gibsonf0517db2005-07-15 17:14:24 +1000918 inbuf_init(&memresvbuf,
919 blob + off_mem_rsvmap, blob + totalsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000920 inbuf_init(&dtbuf, blob + off_dt, blob + totalsize);
David Gibsonfc14dad2005-06-08 17:18:34 +1000921
David Gibsonf040d952005-10-24 18:18:38 +1000922 reservelist = flat_read_mem_reserve(&memresvbuf);
David Gibsonf0517db2005-07-15 17:14:24 +1000923
David Gibsonfc14dad2005-06-08 17:18:34 +1000924 val = flat_read_word(&dtbuf);
925
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000926 if (val != FDT_BEGIN_NODE)
927 die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val);
David Gibsonfc14dad2005-06-08 17:18:34 +1000928
929 tree = unflatten_tree(&dtbuf, &strbuf, "", flags);
930
931 val = flat_read_word(&dtbuf);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000932 if (val != FDT_END)
933 die("Device tree blob doesn't end with FDT_END\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000934
935 free(blob);
936
David Gibsond68cb362009-12-08 14:24:42 +1100937 fclose(f);
David Gibsona742aad2008-05-16 13:22:09 +1000938
David Gibson00fbb862016-05-31 11:58:42 +1000939 return build_dt_info(DTSF_V1, reservelist, tree, boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000940}