blob: bd6977eedcb860a16ff8f6424f00cc150bd0877b [file] [log] [blame]
Rob Herring12869ec2019-06-21 08:18:32 -06001// SPDX-License-Identifier: GPL-2.0-or-later
David Gibsona4da2e32007-12-18 15:06:42 +11002/*
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
David Gibsona4da2e32007-12-18 15:06:42 +11004 */
5
6#include "dtc.h"
David Gibsoned95d742008-08-07 12:24:17 +10007#include "srcpos.h"
David Gibsona4da2e32007-12-18 15:06:42 +11008
9#define FTF_FULLPATH 0x1
10#define FTF_VARALIGN 0x2
11#define FTF_NAMEPROPS 0x4
12#define FTF_BOOTCPUID 0x8
13#define FTF_STRTABSIZE 0x10
14#define FTF_STRUCTSIZE 0x20
15#define FTF_NOPS 0x40
16
17static struct version_info {
18 int version;
19 int last_comp_version;
20 int hdr_size;
21 int flags;
22} version_table[] = {
23 {1, 1, FDT_V1_SIZE,
24 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS},
25 {2, 1, FDT_V2_SIZE,
26 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID},
27 {3, 1, FDT_V3_SIZE,
28 FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID|FTF_STRTABSIZE},
29 {16, 16, FDT_V3_SIZE,
30 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_NOPS},
31 {17, 16, FDT_V17_SIZE,
32 FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE|FTF_NOPS},
33};
34
35struct emitter {
36 void (*cell)(void *, cell_t);
Rob Herring89d12312017-03-21 09:01:08 -050037 void (*string)(void *, const char *, int);
David Gibsona4da2e32007-12-18 15:06:42 +110038 void (*align)(void *, int);
39 void (*data)(void *, struct data);
John Bonesio658f29a2010-11-17 15:28:20 -080040 void (*beginnode)(void *, struct label *labels);
41 void (*endnode)(void *, struct label *labels);
42 void (*property)(void *, struct label *labels);
David Gibsona4da2e32007-12-18 15:06:42 +110043};
44
45static void bin_emit_cell(void *e, cell_t val)
46{
47 struct data *dtbuf = e;
48
49 *dtbuf = data_append_cell(*dtbuf, val);
50}
51
Rob Herring89d12312017-03-21 09:01:08 -050052static void bin_emit_string(void *e, const char *str, int len)
David Gibsona4da2e32007-12-18 15:06:42 +110053{
54 struct data *dtbuf = e;
55
56 if (len == 0)
57 len = strlen(str);
58
59 *dtbuf = data_append_data(*dtbuf, str, len);
60 *dtbuf = data_append_byte(*dtbuf, '\0');
61}
62
63static void bin_emit_align(void *e, int a)
64{
65 struct data *dtbuf = e;
66
67 *dtbuf = data_append_align(*dtbuf, a);
68}
69
70static void bin_emit_data(void *e, struct data d)
71{
72 struct data *dtbuf = e;
73
74 *dtbuf = data_append_data(*dtbuf, d.val, d.len);
75}
76
John Bonesio658f29a2010-11-17 15:28:20 -080077static void bin_emit_beginnode(void *e, struct label *labels)
David Gibsona4da2e32007-12-18 15:06:42 +110078{
79 bin_emit_cell(e, FDT_BEGIN_NODE);
80}
81
John Bonesio658f29a2010-11-17 15:28:20 -080082static void bin_emit_endnode(void *e, struct label *labels)
David Gibsona4da2e32007-12-18 15:06:42 +110083{
84 bin_emit_cell(e, FDT_END_NODE);
85}
86
John Bonesio658f29a2010-11-17 15:28:20 -080087static void bin_emit_property(void *e, struct label *labels)
David Gibsona4da2e32007-12-18 15:06:42 +110088{
89 bin_emit_cell(e, FDT_PROP);
90}
91
92static struct emitter bin_emitter = {
93 .cell = bin_emit_cell,
94 .string = bin_emit_string,
95 .align = bin_emit_align,
96 .data = bin_emit_data,
97 .beginnode = bin_emit_beginnode,
98 .endnode = bin_emit_endnode,
99 .property = bin_emit_property,
100};
101
102static void emit_label(FILE *f, const char *prefix, const char *label)
103{
104 fprintf(f, "\t.globl\t%s_%s\n", prefix, label);
105 fprintf(f, "%s_%s:\n", prefix, label);
106 fprintf(f, "_%s_%s:\n", prefix, label);
107}
108
109static void emit_offset_label(FILE *f, const char *label, int offset)
110{
111 fprintf(f, "\t.globl\t%s\n", label);
112 fprintf(f, "%s\t= . + %d\n", label, offset);
113}
114
John Bonesio658f29a2010-11-17 15:28:20 -0800115#define ASM_EMIT_BELONG(f, fmt, ...) \
116 { \
117 fprintf((f), "\t.byte\t((" fmt ") >> 24) & 0xff\n", __VA_ARGS__); \
118 fprintf((f), "\t.byte\t((" fmt ") >> 16) & 0xff\n", __VA_ARGS__); \
119 fprintf((f), "\t.byte\t((" fmt ") >> 8) & 0xff\n", __VA_ARGS__); \
120 fprintf((f), "\t.byte\t(" fmt ") & 0xff\n", __VA_ARGS__); \
121 }
122
David Gibsona4da2e32007-12-18 15:06:42 +1100123static void asm_emit_cell(void *e, cell_t val)
124{
125 FILE *f = e;
126
John Bonesio658f29a2010-11-17 15:28:20 -0800127 fprintf(f, "\t.byte 0x%02x; .byte 0x%02x; .byte 0x%02x; .byte 0x%02x\n",
128 (val >> 24) & 0xff, (val >> 16) & 0xff,
129 (val >> 8) & 0xff, val & 0xff);
David Gibsona4da2e32007-12-18 15:06:42 +1100130}
131
Rob Herring89d12312017-03-21 09:01:08 -0500132static void asm_emit_string(void *e, const char *str, int len)
David Gibsona4da2e32007-12-18 15:06:42 +1100133{
134 FILE *f = e;
David Gibsona4da2e32007-12-18 15:06:42 +1100135
Rob Herring89d12312017-03-21 09:01:08 -0500136 if (len != 0)
137 fprintf(f, "\t.string\t\"%.*s\"\n", len, str);
138 else
139 fprintf(f, "\t.string\t\"%s\"\n", str);
David Gibsona4da2e32007-12-18 15:06:42 +1100140}
141
142static void asm_emit_align(void *e, int a)
143{
144 FILE *f = e;
145
John Bonesio658f29a2010-11-17 15:28:20 -0800146 fprintf(f, "\t.balign\t%d, 0\n", a);
David Gibsona4da2e32007-12-18 15:06:42 +1100147}
148
149static void asm_emit_data(void *e, struct data d)
150{
151 FILE *f = e;
152 int off = 0;
David Gibsoned95d742008-08-07 12:24:17 +1000153 struct marker *m = d.markers;
David Gibsona4da2e32007-12-18 15:06:42 +1100154
David Gibsoned95d742008-08-07 12:24:17 +1000155 for_each_marker_of_type(m, LABEL)
156 emit_offset_label(f, m->ref, m->offset);
David Gibsona4da2e32007-12-18 15:06:42 +1100157
David Gibsoned95d742008-08-07 12:24:17 +1000158 while ((d.len - off) >= sizeof(uint32_t)) {
Rob Herring89d12312017-03-21 09:01:08 -0500159 asm_emit_cell(e, fdt32_to_cpu(*((fdt32_t *)(d.val+off))));
David Gibsoned95d742008-08-07 12:24:17 +1000160 off += sizeof(uint32_t);
David Gibsona4da2e32007-12-18 15:06:42 +1100161 }
162
David Gibsoned95d742008-08-07 12:24:17 +1000163 while ((d.len - off) >= 1) {
David Gibsona4da2e32007-12-18 15:06:42 +1100164 fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]);
165 off += 1;
166 }
167
168 assert(off == d.len);
169}
170
John Bonesio658f29a2010-11-17 15:28:20 -0800171static void asm_emit_beginnode(void *e, struct label *labels)
David Gibsona4da2e32007-12-18 15:06:42 +1100172{
173 FILE *f = e;
John Bonesio658f29a2010-11-17 15:28:20 -0800174 struct label *l;
David Gibsona4da2e32007-12-18 15:06:42 +1100175
John Bonesio658f29a2010-11-17 15:28:20 -0800176 for_each_label(labels, l) {
177 fprintf(f, "\t.globl\t%s\n", l->label);
178 fprintf(f, "%s:\n", l->label);
David Gibsona4da2e32007-12-18 15:06:42 +1100179 }
John Bonesio658f29a2010-11-17 15:28:20 -0800180 fprintf(f, "\t/* FDT_BEGIN_NODE */\n");
181 asm_emit_cell(e, FDT_BEGIN_NODE);
David Gibsona4da2e32007-12-18 15:06:42 +1100182}
183
John Bonesio658f29a2010-11-17 15:28:20 -0800184static void asm_emit_endnode(void *e, struct label *labels)
David Gibsona4da2e32007-12-18 15:06:42 +1100185{
186 FILE *f = e;
John Bonesio658f29a2010-11-17 15:28:20 -0800187 struct label *l;
David Gibsona4da2e32007-12-18 15:06:42 +1100188
John Bonesio658f29a2010-11-17 15:28:20 -0800189 fprintf(f, "\t/* FDT_END_NODE */\n");
190 asm_emit_cell(e, FDT_END_NODE);
191 for_each_label(labels, l) {
192 fprintf(f, "\t.globl\t%s_end\n", l->label);
193 fprintf(f, "%s_end:\n", l->label);
David Gibsona4da2e32007-12-18 15:06:42 +1100194 }
195}
196
John Bonesio658f29a2010-11-17 15:28:20 -0800197static void asm_emit_property(void *e, struct label *labels)
David Gibsona4da2e32007-12-18 15:06:42 +1100198{
199 FILE *f = e;
John Bonesio658f29a2010-11-17 15:28:20 -0800200 struct label *l;
David Gibsona4da2e32007-12-18 15:06:42 +1100201
John Bonesio658f29a2010-11-17 15:28:20 -0800202 for_each_label(labels, l) {
203 fprintf(f, "\t.globl\t%s\n", l->label);
204 fprintf(f, "%s:\n", l->label);
David Gibsona4da2e32007-12-18 15:06:42 +1100205 }
John Bonesio658f29a2010-11-17 15:28:20 -0800206 fprintf(f, "\t/* FDT_PROP */\n");
207 asm_emit_cell(e, FDT_PROP);
David Gibsona4da2e32007-12-18 15:06:42 +1100208}
209
210static struct emitter asm_emitter = {
211 .cell = asm_emit_cell,
212 .string = asm_emit_string,
213 .align = asm_emit_align,
214 .data = asm_emit_data,
215 .beginnode = asm_emit_beginnode,
216 .endnode = asm_emit_endnode,
217 .property = asm_emit_property,
218};
219
220static int stringtable_insert(struct data *d, const char *str)
221{
222 int i;
223
224 /* FIXME: do this more efficiently? */
225
226 for (i = 0; i < d->len; i++) {
227 if (streq(str, d->val + i))
228 return i;
229 }
230
231 *d = data_append_data(*d, str, strlen(str)+1);
232 return i;
233}
234
235static void flatten_tree(struct node *tree, struct emitter *emit,
236 void *etarget, struct data *strbuf,
237 struct version_info *vi)
238{
239 struct property *prop;
240 struct node *child;
Rob Herring47605972015-04-29 16:00:05 -0500241 bool seen_name_prop = false;
David Gibsona4da2e32007-12-18 15:06:42 +1100242
Stephen Warrencd296722012-09-28 21:25:59 +0000243 if (tree->deleted)
244 return;
245
John Bonesio658f29a2010-11-17 15:28:20 -0800246 emit->beginnode(etarget, tree->labels);
David Gibsona4da2e32007-12-18 15:06:42 +1100247
248 if (vi->flags & FTF_FULLPATH)
249 emit->string(etarget, tree->fullpath, 0);
250 else
251 emit->string(etarget, tree->name, 0);
252
253 emit->align(etarget, sizeof(cell_t));
254
255 for_each_property(tree, prop) {
256 int nameoff;
257
258 if (streq(prop->name, "name"))
Rob Herring47605972015-04-29 16:00:05 -0500259 seen_name_prop = true;
David Gibsona4da2e32007-12-18 15:06:42 +1100260
261 nameoff = stringtable_insert(strbuf, prop->name);
262
John Bonesio658f29a2010-11-17 15:28:20 -0800263 emit->property(etarget, prop->labels);
David Gibsona4da2e32007-12-18 15:06:42 +1100264 emit->cell(etarget, prop->val.len);
265 emit->cell(etarget, nameoff);
266
267 if ((vi->flags & FTF_VARALIGN) && (prop->val.len >= 8))
268 emit->align(etarget, 8);
269
270 emit->data(etarget, prop->val);
271 emit->align(etarget, sizeof(cell_t));
272 }
273
274 if ((vi->flags & FTF_NAMEPROPS) && !seen_name_prop) {
275 emit->property(etarget, NULL);
276 emit->cell(etarget, tree->basenamelen+1);
277 emit->cell(etarget, stringtable_insert(strbuf, "name"));
278
279 if ((vi->flags & FTF_VARALIGN) && ((tree->basenamelen+1) >= 8))
280 emit->align(etarget, 8);
281
282 emit->string(etarget, tree->name, tree->basenamelen);
283 emit->align(etarget, sizeof(cell_t));
284 }
285
286 for_each_child(tree, child) {
287 flatten_tree(child, emit, etarget, strbuf, vi);
288 }
289
John Bonesio658f29a2010-11-17 15:28:20 -0800290 emit->endnode(etarget, tree->labels);
David Gibsona4da2e32007-12-18 15:06:42 +1100291}
292
293static struct data flatten_reserve_list(struct reserve_info *reservelist,
294 struct version_info *vi)
295{
296 struct reserve_info *re;
297 struct data d = empty_data;
David Gibsona4da2e32007-12-18 15:06:42 +1100298 int j;
299
300 for (re = reservelist; re; re = re->next) {
Rob Herring89d12312017-03-21 09:01:08 -0500301 d = data_append_re(d, re->address, re->size);
David Gibsona4da2e32007-12-18 15:06:42 +1100302 }
303 /*
304 * Add additional reserved slots if the user asked for them.
305 */
306 for (j = 0; j < reservenum; j++) {
Rob Herring89d12312017-03-21 09:01:08 -0500307 d = data_append_re(d, 0, 0);
David Gibsona4da2e32007-12-18 15:06:42 +1100308 }
309
310 return d;
311}
312
313static void make_fdt_header(struct fdt_header *fdt,
314 struct version_info *vi,
315 int reservesize, int dtsize, int strsize,
316 int boot_cpuid_phys)
317{
318 int reserve_off;
319
320 reservesize += sizeof(struct fdt_reserve_entry);
321
322 memset(fdt, 0xff, sizeof(*fdt));
323
David Gibsoned95d742008-08-07 12:24:17 +1000324 fdt->magic = cpu_to_fdt32(FDT_MAGIC);
325 fdt->version = cpu_to_fdt32(vi->version);
326 fdt->last_comp_version = cpu_to_fdt32(vi->last_comp_version);
David Gibsona4da2e32007-12-18 15:06:42 +1100327
328 /* Reserve map should be doubleword aligned */
329 reserve_off = ALIGN(vi->hdr_size, 8);
330
David Gibsoned95d742008-08-07 12:24:17 +1000331 fdt->off_mem_rsvmap = cpu_to_fdt32(reserve_off);
332 fdt->off_dt_struct = cpu_to_fdt32(reserve_off + reservesize);
333 fdt->off_dt_strings = cpu_to_fdt32(reserve_off + reservesize
David Gibsona4da2e32007-12-18 15:06:42 +1100334 + dtsize);
David Gibsoned95d742008-08-07 12:24:17 +1000335 fdt->totalsize = cpu_to_fdt32(reserve_off + reservesize + dtsize + strsize);
David Gibsona4da2e32007-12-18 15:06:42 +1100336
337 if (vi->flags & FTF_BOOTCPUID)
David Gibsoned95d742008-08-07 12:24:17 +1000338 fdt->boot_cpuid_phys = cpu_to_fdt32(boot_cpuid_phys);
David Gibsona4da2e32007-12-18 15:06:42 +1100339 if (vi->flags & FTF_STRTABSIZE)
David Gibsoned95d742008-08-07 12:24:17 +1000340 fdt->size_dt_strings = cpu_to_fdt32(strsize);
David Gibsona4da2e32007-12-18 15:06:42 +1100341 if (vi->flags & FTF_STRUCTSIZE)
David Gibsoned95d742008-08-07 12:24:17 +1000342 fdt->size_dt_struct = cpu_to_fdt32(dtsize);
David Gibsona4da2e32007-12-18 15:06:42 +1100343}
344
Rob Herring6f05afc2017-01-04 10:45:20 -0600345void dt_to_blob(FILE *f, struct dt_info *dti, int version)
David Gibsona4da2e32007-12-18 15:06:42 +1100346{
347 struct version_info *vi = NULL;
348 int i;
349 struct data blob = empty_data;
350 struct data reservebuf = empty_data;
351 struct data dtbuf = empty_data;
352 struct data strbuf = empty_data;
353 struct fdt_header fdt;
354 int padlen = 0;
355
356 for (i = 0; i < ARRAY_SIZE(version_table); i++) {
357 if (version_table[i].version == version)
358 vi = &version_table[i];
359 }
360 if (!vi)
361 die("Unknown device tree blob version %d\n", version);
362
Rob Herring6f05afc2017-01-04 10:45:20 -0600363 flatten_tree(dti->dt, &bin_emitter, &dtbuf, &strbuf, vi);
David Gibsona4da2e32007-12-18 15:06:42 +1100364 bin_emit_cell(&dtbuf, FDT_END);
365
Rob Herring6f05afc2017-01-04 10:45:20 -0600366 reservebuf = flatten_reserve_list(dti->reservelist, vi);
David Gibsona4da2e32007-12-18 15:06:42 +1100367
368 /* Make header */
369 make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len,
Rob Herring6f05afc2017-01-04 10:45:20 -0600370 dti->boot_cpuid_phys);
David Gibsona4da2e32007-12-18 15:06:42 +1100371
372 /*
373 * If the user asked for more space than is used, adjust the totalsize.
374 */
375 if (minsize > 0) {
David Gibsoned95d742008-08-07 12:24:17 +1000376 padlen = minsize - fdt32_to_cpu(fdt.totalsize);
Rob Herring6f05afc2017-01-04 10:45:20 -0600377 if (padlen < 0) {
378 padlen = 0;
379 if (quiet < 1)
380 fprintf(stderr,
Rob Herringf8589272018-09-13 08:59:25 -0500381 "Warning: blob size %"PRIu32" >= minimum size %d\n",
Rob Herring6f05afc2017-01-04 10:45:20 -0600382 fdt32_to_cpu(fdt.totalsize), minsize);
383 }
David Gibsona4da2e32007-12-18 15:06:42 +1100384 }
385
386 if (padsize > 0)
387 padlen = padsize;
388
Rob Herring6f05afc2017-01-04 10:45:20 -0600389 if (alignsize > 0)
390 padlen = ALIGN(fdt32_to_cpu(fdt.totalsize) + padlen, alignsize)
391 - fdt32_to_cpu(fdt.totalsize);
392
David Gibsona4da2e32007-12-18 15:06:42 +1100393 if (padlen > 0) {
David Gibsoned95d742008-08-07 12:24:17 +1000394 int tsize = fdt32_to_cpu(fdt.totalsize);
David Gibsona4da2e32007-12-18 15:06:42 +1100395 tsize += padlen;
David Gibsoned95d742008-08-07 12:24:17 +1000396 fdt.totalsize = cpu_to_fdt32(tsize);
David Gibsona4da2e32007-12-18 15:06:42 +1100397 }
398
399 /*
400 * Assemble the blob: start with the header, add with alignment
401 * the reserve buffer, add the reserve map terminating zeroes,
402 * the device tree itself, and finally the strings.
403 */
David Gibsoned95d742008-08-07 12:24:17 +1000404 blob = data_append_data(blob, &fdt, vi->hdr_size);
David Gibsona4da2e32007-12-18 15:06:42 +1100405 blob = data_append_align(blob, 8);
406 blob = data_merge(blob, reservebuf);
407 blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry));
408 blob = data_merge(blob, dtbuf);
409 blob = data_merge(blob, strbuf);
410
411 /*
412 * If the user asked for more space than is used, pad out the blob.
413 */
414 if (padlen > 0)
415 blob = data_append_zeroes(blob, padlen);
416
John Bonesio658f29a2010-11-17 15:28:20 -0800417 if (fwrite(blob.val, blob.len, 1, f) != 1) {
418 if (ferror(f))
419 die("Error writing device tree blob: %s\n",
420 strerror(errno));
421 else
422 die("Short write on device tree blob\n");
423 }
David Gibsona4da2e32007-12-18 15:06:42 +1100424
425 /*
426 * data_merge() frees the right-hand element so only the blob
427 * remains to be freed.
428 */
429 data_free(blob);
430}
431
432static void dump_stringtable_asm(FILE *f, struct data strbuf)
433{
434 const char *p;
435 int len;
436
437 p = strbuf.val;
438
439 while (p < (strbuf.val + strbuf.len)) {
440 len = strlen(p);
441 fprintf(f, "\t.string \"%s\"\n", p);
442 p += len+1;
443 }
444}
445
Rob Herring6f05afc2017-01-04 10:45:20 -0600446void dt_to_asm(FILE *f, struct dt_info *dti, int version)
David Gibsona4da2e32007-12-18 15:06:42 +1100447{
448 struct version_info *vi = NULL;
449 int i;
450 struct data strbuf = empty_data;
451 struct reserve_info *re;
452 const char *symprefix = "dt";
453
454 for (i = 0; i < ARRAY_SIZE(version_table); i++) {
455 if (version_table[i].version == version)
456 vi = &version_table[i];
457 }
458 if (!vi)
459 die("Unknown device tree blob version %d\n", version);
460
461 fprintf(f, "/* autogenerated by dtc, do not edit */\n\n");
David Gibsona4da2e32007-12-18 15:06:42 +1100462
463 emit_label(f, symprefix, "blob_start");
464 emit_label(f, symprefix, "header");
John Bonesio658f29a2010-11-17 15:28:20 -0800465 fprintf(f, "\t/* magic */\n");
466 asm_emit_cell(f, FDT_MAGIC);
467 fprintf(f, "\t/* totalsize */\n");
468 ASM_EMIT_BELONG(f, "_%s_blob_abs_end - _%s_blob_start",
David Gibsona4da2e32007-12-18 15:06:42 +1100469 symprefix, symprefix);
John Bonesio658f29a2010-11-17 15:28:20 -0800470 fprintf(f, "\t/* off_dt_struct */\n");
471 ASM_EMIT_BELONG(f, "_%s_struct_start - _%s_blob_start",
472 symprefix, symprefix);
473 fprintf(f, "\t/* off_dt_strings */\n");
474 ASM_EMIT_BELONG(f, "_%s_strings_start - _%s_blob_start",
475 symprefix, symprefix);
476 fprintf(f, "\t/* off_mem_rsvmap */\n");
477 ASM_EMIT_BELONG(f, "_%s_reserve_map - _%s_blob_start",
478 symprefix, symprefix);
479 fprintf(f, "\t/* version */\n");
480 asm_emit_cell(f, vi->version);
481 fprintf(f, "\t/* last_comp_version */\n");
482 asm_emit_cell(f, vi->last_comp_version);
David Gibsona4da2e32007-12-18 15:06:42 +1100483
John Bonesio658f29a2010-11-17 15:28:20 -0800484 if (vi->flags & FTF_BOOTCPUID) {
485 fprintf(f, "\t/* boot_cpuid_phys */\n");
Rob Herring6f05afc2017-01-04 10:45:20 -0600486 asm_emit_cell(f, dti->boot_cpuid_phys);
John Bonesio658f29a2010-11-17 15:28:20 -0800487 }
488
489 if (vi->flags & FTF_STRTABSIZE) {
490 fprintf(f, "\t/* size_dt_strings */\n");
491 ASM_EMIT_BELONG(f, "_%s_strings_end - _%s_strings_start",
492 symprefix, symprefix);
493 }
494
495 if (vi->flags & FTF_STRUCTSIZE) {
496 fprintf(f, "\t/* size_dt_struct */\n");
497 ASM_EMIT_BELONG(f, "_%s_struct_end - _%s_struct_start",
David Gibsona4da2e32007-12-18 15:06:42 +1100498 symprefix, symprefix);
John Bonesio658f29a2010-11-17 15:28:20 -0800499 }
David Gibsona4da2e32007-12-18 15:06:42 +1100500
501 /*
502 * Reserve map entries.
503 * Align the reserve map to a doubleword boundary.
504 * Each entry is an (address, size) pair of u64 values.
505 * Always supply a zero-sized temination entry.
506 */
507 asm_emit_align(f, 8);
508 emit_label(f, symprefix, "reserve_map");
509
510 fprintf(f, "/* Memory reserve map from source file */\n");
511
512 /*
Rob Herring9bb9c6a2019-06-12 07:05:52 -0600513 * Use .long on high and low halves of u64s to avoid .quad
David Gibsona4da2e32007-12-18 15:06:42 +1100514 * as it appears .quad isn't available in some assemblers.
515 */
Rob Herring6f05afc2017-01-04 10:45:20 -0600516 for (re = dti->reservelist; re; re = re->next) {
John Bonesio658f29a2010-11-17 15:28:20 -0800517 struct label *l;
518
519 for_each_label(re->labels, l) {
520 fprintf(f, "\t.globl\t%s\n", l->label);
521 fprintf(f, "%s:\n", l->label);
David Gibsona4da2e32007-12-18 15:06:42 +1100522 }
Rob Herring89d12312017-03-21 09:01:08 -0500523 ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->address >> 32));
John Bonesio658f29a2010-11-17 15:28:20 -0800524 ASM_EMIT_BELONG(f, "0x%08x",
Rob Herring89d12312017-03-21 09:01:08 -0500525 (unsigned int)(re->address & 0xffffffff));
526 ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->size >> 32));
527 ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->size & 0xffffffff));
David Gibsona4da2e32007-12-18 15:06:42 +1100528 }
529 for (i = 0; i < reservenum; i++) {
530 fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
531 }
532
533 fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
534
535 emit_label(f, symprefix, "struct_start");
Rob Herring6f05afc2017-01-04 10:45:20 -0600536 flatten_tree(dti->dt, &asm_emitter, f, &strbuf, vi);
John Bonesio658f29a2010-11-17 15:28:20 -0800537
538 fprintf(f, "\t/* FDT_END */\n");
539 asm_emit_cell(f, FDT_END);
David Gibsona4da2e32007-12-18 15:06:42 +1100540 emit_label(f, symprefix, "struct_end");
541
542 emit_label(f, symprefix, "strings_start");
543 dump_stringtable_asm(f, strbuf);
544 emit_label(f, symprefix, "strings_end");
545
546 emit_label(f, symprefix, "blob_end");
547
548 /*
549 * If the user asked for more space than is used, pad it out.
550 */
551 if (minsize > 0) {
552 fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n",
553 minsize, symprefix, symprefix);
554 }
555 if (padsize > 0) {
556 fprintf(f, "\t.space\t%d, 0\n", padsize);
557 }
Rob Herring6f05afc2017-01-04 10:45:20 -0600558 if (alignsize > 0)
559 asm_emit_align(f, alignsize);
David Gibsona4da2e32007-12-18 15:06:42 +1100560 emit_label(f, symprefix, "blob_abs_end");
561
562 data_free(strbuf);
563}
564
565struct inbuf {
566 char *base, *limit, *ptr;
567};
568
569static void inbuf_init(struct inbuf *inb, void *base, void *limit)
570{
571 inb->base = base;
572 inb->limit = limit;
573 inb->ptr = inb->base;
574}
575
576static void flat_read_chunk(struct inbuf *inb, void *p, int len)
577{
578 if ((inb->ptr + len) > inb->limit)
579 die("Premature end of data parsing flat device tree\n");
580
581 memcpy(p, inb->ptr, len);
582
583 inb->ptr += len;
584}
585
David Gibsoned95d742008-08-07 12:24:17 +1000586static uint32_t flat_read_word(struct inbuf *inb)
David Gibsona4da2e32007-12-18 15:06:42 +1100587{
Rob Herring89d12312017-03-21 09:01:08 -0500588 fdt32_t val;
David Gibsona4da2e32007-12-18 15:06:42 +1100589
590 assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
591
592 flat_read_chunk(inb, &val, sizeof(val));
593
David Gibsoned95d742008-08-07 12:24:17 +1000594 return fdt32_to_cpu(val);
David Gibsona4da2e32007-12-18 15:06:42 +1100595}
596
597static void flat_realign(struct inbuf *inb, int align)
598{
599 int off = inb->ptr - inb->base;
600
601 inb->ptr = inb->base + ALIGN(off, align);
602 if (inb->ptr > inb->limit)
603 die("Premature end of data parsing flat device tree\n");
604}
605
606static char *flat_read_string(struct inbuf *inb)
607{
608 int len = 0;
609 const char *p = inb->ptr;
610 char *str;
611
612 do {
613 if (p >= inb->limit)
614 die("Premature end of data parsing flat device tree\n");
615 len++;
616 } while ((*p++) != '\0');
617
John Bonesio658f29a2010-11-17 15:28:20 -0800618 str = xstrdup(inb->ptr);
David Gibsona4da2e32007-12-18 15:06:42 +1100619
620 inb->ptr += len;
621
David Gibsoned95d742008-08-07 12:24:17 +1000622 flat_realign(inb, sizeof(uint32_t));
David Gibsona4da2e32007-12-18 15:06:42 +1100623
624 return str;
625}
626
627static struct data flat_read_data(struct inbuf *inb, int len)
628{
629 struct data d = empty_data;
630
631 if (len == 0)
632 return empty_data;
633
634 d = data_grow_for(d, len);
635 d.len = len;
636
637 flat_read_chunk(inb, d.val, len);
638
David Gibsoned95d742008-08-07 12:24:17 +1000639 flat_realign(inb, sizeof(uint32_t));
David Gibsona4da2e32007-12-18 15:06:42 +1100640
641 return d;
642}
643
644static char *flat_read_stringtable(struct inbuf *inb, int offset)
645{
646 const char *p;
647
648 p = inb->base + offset;
649 while (1) {
650 if (p >= inb->limit || p < inb->base)
651 die("String offset %d overruns string table\n",
652 offset);
653
654 if (*p == '\0')
655 break;
656
657 p++;
658 }
659
John Bonesio658f29a2010-11-17 15:28:20 -0800660 return xstrdup(inb->base + offset);
David Gibsona4da2e32007-12-18 15:06:42 +1100661}
662
663static struct property *flat_read_property(struct inbuf *dtbuf,
664 struct inbuf *strbuf, int flags)
665{
David Gibsoned95d742008-08-07 12:24:17 +1000666 uint32_t proplen, stroff;
David Gibsona4da2e32007-12-18 15:06:42 +1100667 char *name;
668 struct data val;
669
670 proplen = flat_read_word(dtbuf);
671 stroff = flat_read_word(dtbuf);
672
673 name = flat_read_stringtable(strbuf, stroff);
674
675 if ((flags & FTF_VARALIGN) && (proplen >= 8))
676 flat_realign(dtbuf, 8);
677
678 val = flat_read_data(dtbuf, proplen);
679
Rob Herringc2e70752018-11-28 18:37:35 -0600680 return build_property(name, val, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +1100681}
682
683
684static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
685{
686 struct reserve_info *reservelist = NULL;
687 struct reserve_info *new;
David Gibsona4da2e32007-12-18 15:06:42 +1100688 struct fdt_reserve_entry re;
689
690 /*
691 * Each entry is a pair of u64 (addr, size) values for 4 cell_t's.
692 * List terminates at an entry with size equal to zero.
693 *
694 * First pass, count entries.
695 */
David Gibsona4da2e32007-12-18 15:06:42 +1100696 while (1) {
Rob Herring89d12312017-03-21 09:01:08 -0500697 uint64_t address, size;
698
David Gibsona4da2e32007-12-18 15:06:42 +1100699 flat_read_chunk(inb, &re, sizeof(re));
Rob Herring89d12312017-03-21 09:01:08 -0500700 address = fdt64_to_cpu(re.address);
701 size = fdt64_to_cpu(re.size);
702 if (size == 0)
David Gibsona4da2e32007-12-18 15:06:42 +1100703 break;
704
Rob Herring89d12312017-03-21 09:01:08 -0500705 new = build_reserve_entry(address, size);
David Gibsona4da2e32007-12-18 15:06:42 +1100706 reservelist = add_reserve_entry(reservelist, new);
707 }
708
709 return reservelist;
710}
711
712
713static char *nodename_from_path(const char *ppath, const char *cpath)
714{
David Gibsona4da2e32007-12-18 15:06:42 +1100715 int plen;
716
David Gibsoned95d742008-08-07 12:24:17 +1000717 plen = strlen(ppath);
David Gibsona4da2e32007-12-18 15:06:42 +1100718
Rob Herring9130ba82018-02-27 17:40:38 -0600719 if (!strstarts(cpath, ppath))
David Gibsoned95d742008-08-07 12:24:17 +1000720 die("Path \"%s\" is not valid as a child of \"%s\"\n",
721 cpath, ppath);
David Gibsona4da2e32007-12-18 15:06:42 +1100722
David Gibsoned95d742008-08-07 12:24:17 +1000723 /* root node is a special case */
724 if (!streq(ppath, "/"))
725 plen++;
David Gibsona4da2e32007-12-18 15:06:42 +1100726
John Bonesio658f29a2010-11-17 15:28:20 -0800727 return xstrdup(cpath + plen);
David Gibsona4da2e32007-12-18 15:06:42 +1100728}
729
730static struct node *unflatten_tree(struct inbuf *dtbuf,
731 struct inbuf *strbuf,
David Gibsoned95d742008-08-07 12:24:17 +1000732 const char *parent_flatname, int flags)
David Gibsona4da2e32007-12-18 15:06:42 +1100733{
734 struct node *node;
David Gibsoned95d742008-08-07 12:24:17 +1000735 char *flatname;
736 uint32_t val;
David Gibsona4da2e32007-12-18 15:06:42 +1100737
Rob Herringc2e70752018-11-28 18:37:35 -0600738 node = build_node(NULL, NULL, NULL);
David Gibsona4da2e32007-12-18 15:06:42 +1100739
David Gibsoned95d742008-08-07 12:24:17 +1000740 flatname = flat_read_string(dtbuf);
David Gibsona4da2e32007-12-18 15:06:42 +1100741
David Gibsoned95d742008-08-07 12:24:17 +1000742 if (flags & FTF_FULLPATH)
743 node->name = nodename_from_path(parent_flatname, flatname);
744 else
745 node->name = flatname;
David Gibsona4da2e32007-12-18 15:06:42 +1100746
747 do {
748 struct property *prop;
749 struct node *child;
750
751 val = flat_read_word(dtbuf);
752 switch (val) {
753 case FDT_PROP:
754 if (node->children)
755 fprintf(stderr, "Warning: Flat tree input has "
756 "subnodes preceding a property.\n");
757 prop = flat_read_property(dtbuf, strbuf, flags);
758 add_property(node, prop);
759 break;
760
761 case FDT_BEGIN_NODE:
David Gibsoned95d742008-08-07 12:24:17 +1000762 child = unflatten_tree(dtbuf,strbuf, flatname, flags);
David Gibsona4da2e32007-12-18 15:06:42 +1100763 add_child(node, child);
764 break;
765
766 case FDT_END_NODE:
767 break;
768
769 case FDT_END:
770 die("Premature FDT_END in device tree blob\n");
771 break;
772
773 case FDT_NOP:
774 if (!(flags & FTF_NOPS))
775 fprintf(stderr, "Warning: NOP tag found in flat tree"
776 " version <16\n");
777
778 /* Ignore */
779 break;
780
781 default:
782 die("Invalid opcode word %08x in device tree blob\n",
783 val);
784 }
785 } while (val != FDT_END_NODE);
786
Rob Herring6f05afc2017-01-04 10:45:20 -0600787 if (node->name != flatname) {
788 free(flatname);
789 }
790
David Gibsona4da2e32007-12-18 15:06:42 +1100791 return node;
792}
793
794
Rob Herring6f05afc2017-01-04 10:45:20 -0600795struct dt_info *dt_from_blob(const char *fname)
David Gibsona4da2e32007-12-18 15:06:42 +1100796{
John Bonesio658f29a2010-11-17 15:28:20 -0800797 FILE *f;
Rob Herring89d12312017-03-21 09:01:08 -0500798 fdt32_t magic_buf, totalsize_buf;
David Gibsoned95d742008-08-07 12:24:17 +1000799 uint32_t magic, totalsize, version, size_dt, boot_cpuid_phys;
800 uint32_t off_dt, off_str, off_mem_rsvmap;
David Gibsona4da2e32007-12-18 15:06:42 +1100801 int rc;
802 char *blob;
803 struct fdt_header *fdt;
804 char *p;
805 struct inbuf dtbuf, strbuf;
806 struct inbuf memresvbuf;
807 int sizeleft;
808 struct reserve_info *reservelist;
809 struct node *tree;
David Gibsoned95d742008-08-07 12:24:17 +1000810 uint32_t val;
David Gibsona4da2e32007-12-18 15:06:42 +1100811 int flags = 0;
812
John Bonesio658f29a2010-11-17 15:28:20 -0800813 f = srcfile_relative_open(fname, NULL);
David Gibsoned95d742008-08-07 12:24:17 +1000814
Rob Herring89d12312017-03-21 09:01:08 -0500815 rc = fread(&magic_buf, sizeof(magic_buf), 1, f);
John Bonesio658f29a2010-11-17 15:28:20 -0800816 if (ferror(f))
David Gibsona4da2e32007-12-18 15:06:42 +1100817 die("Error reading DT blob magic number: %s\n",
818 strerror(errno));
819 if (rc < 1) {
John Bonesio658f29a2010-11-17 15:28:20 -0800820 if (feof(f))
David Gibsona4da2e32007-12-18 15:06:42 +1100821 die("EOF reading DT blob magic number\n");
822 else
823 die("Mysterious short read reading magic number\n");
824 }
825
Rob Herring89d12312017-03-21 09:01:08 -0500826 magic = fdt32_to_cpu(magic_buf);
David Gibsona4da2e32007-12-18 15:06:42 +1100827 if (magic != FDT_MAGIC)
828 die("Blob has incorrect magic number\n");
829
Rob Herring89d12312017-03-21 09:01:08 -0500830 rc = fread(&totalsize_buf, sizeof(totalsize_buf), 1, f);
John Bonesio658f29a2010-11-17 15:28:20 -0800831 if (ferror(f))
David Gibsona4da2e32007-12-18 15:06:42 +1100832 die("Error reading DT blob size: %s\n", strerror(errno));
833 if (rc < 1) {
John Bonesio658f29a2010-11-17 15:28:20 -0800834 if (feof(f))
David Gibsona4da2e32007-12-18 15:06:42 +1100835 die("EOF reading DT blob size\n");
836 else
837 die("Mysterious short read reading blob size\n");
838 }
839
Rob Herring89d12312017-03-21 09:01:08 -0500840 totalsize = fdt32_to_cpu(totalsize_buf);
David Gibsona4da2e32007-12-18 15:06:42 +1100841 if (totalsize < FDT_V1_SIZE)
842 die("DT blob size (%d) is too small\n", totalsize);
843
844 blob = xmalloc(totalsize);
845
846 fdt = (struct fdt_header *)blob;
David Gibsoned95d742008-08-07 12:24:17 +1000847 fdt->magic = cpu_to_fdt32(magic);
848 fdt->totalsize = cpu_to_fdt32(totalsize);
David Gibsona4da2e32007-12-18 15:06:42 +1100849
850 sizeleft = totalsize - sizeof(magic) - sizeof(totalsize);
851 p = blob + sizeof(magic) + sizeof(totalsize);
852
853 while (sizeleft) {
John Bonesio658f29a2010-11-17 15:28:20 -0800854 if (feof(f))
David Gibsona4da2e32007-12-18 15:06:42 +1100855 die("EOF before reading %d bytes of DT blob\n",
856 totalsize);
857
John Bonesio658f29a2010-11-17 15:28:20 -0800858 rc = fread(p, 1, sizeleft, f);
859 if (ferror(f))
David Gibsona4da2e32007-12-18 15:06:42 +1100860 die("Error reading DT blob: %s\n",
861 strerror(errno));
862
863 sizeleft -= rc;
864 p += rc;
865 }
866
David Gibsoned95d742008-08-07 12:24:17 +1000867 off_dt = fdt32_to_cpu(fdt->off_dt_struct);
868 off_str = fdt32_to_cpu(fdt->off_dt_strings);
869 off_mem_rsvmap = fdt32_to_cpu(fdt->off_mem_rsvmap);
870 version = fdt32_to_cpu(fdt->version);
871 boot_cpuid_phys = fdt32_to_cpu(fdt->boot_cpuid_phys);
David Gibsona4da2e32007-12-18 15:06:42 +1100872
873 if (off_mem_rsvmap >= totalsize)
874 die("Mem Reserve structure offset exceeds total size\n");
875
876 if (off_dt >= totalsize)
877 die("DT structure offset exceeds total size\n");
878
879 if (off_str > totalsize)
880 die("String table offset exceeds total size\n");
881
David Gibsona4da2e32007-12-18 15:06:42 +1100882 if (version >= 3) {
David Gibsoned95d742008-08-07 12:24:17 +1000883 uint32_t size_str = fdt32_to_cpu(fdt->size_dt_strings);
Rob Herringb9937342016-03-04 08:56:58 -0600884 if ((off_str+size_str < off_str) || (off_str+size_str > totalsize))
David Gibsona4da2e32007-12-18 15:06:42 +1100885 die("String table extends past total size\n");
David Gibsoned95d742008-08-07 12:24:17 +1000886 inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str);
887 } else {
888 inbuf_init(&strbuf, blob + off_str, blob + totalsize);
David Gibsona4da2e32007-12-18 15:06:42 +1100889 }
890
891 if (version >= 17) {
David Gibsoned95d742008-08-07 12:24:17 +1000892 size_dt = fdt32_to_cpu(fdt->size_dt_struct);
Rob Herringb9937342016-03-04 08:56:58 -0600893 if ((off_dt+size_dt < off_dt) || (off_dt+size_dt > totalsize))
David Gibsona4da2e32007-12-18 15:06:42 +1100894 die("Structure block extends past total size\n");
895 }
896
897 if (version < 16) {
898 flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN;
899 } else {
900 flags |= FTF_NOPS;
901 }
902
903 inbuf_init(&memresvbuf,
904 blob + off_mem_rsvmap, blob + totalsize);
905 inbuf_init(&dtbuf, blob + off_dt, blob + totalsize);
David Gibsona4da2e32007-12-18 15:06:42 +1100906
907 reservelist = flat_read_mem_reserve(&memresvbuf);
908
909 val = flat_read_word(&dtbuf);
910
911 if (val != FDT_BEGIN_NODE)
912 die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val);
913
914 tree = unflatten_tree(&dtbuf, &strbuf, "", flags);
915
916 val = flat_read_word(&dtbuf);
917 if (val != FDT_END)
918 die("Device tree blob doesn't end with FDT_END\n");
919
920 free(blob);
921
John Bonesio658f29a2010-11-17 15:28:20 -0800922 fclose(f);
David Gibsoned95d742008-08-07 12:24:17 +1000923
Rob Herring6f05afc2017-01-04 10:45:20 -0600924 return build_dt_info(DTSF_V1, reservelist, tree, boot_cpuid_phys);
David Gibsona4da2e32007-12-18 15:06:42 +1100925}