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