blob: 16fa3071cd8f9173f0d7d2a312e580c1f05c8624 [file] [log] [blame]
Nicholas Flintham1e3d3112013-04-10 10:48:38 +01001/*
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
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
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 for (j = 0; j < reservenum; j++) {
325 d = data_append_re(d, &null_re);
326 }
327
328 return d;
329}
330
331static void make_fdt_header(struct fdt_header *fdt,
332 struct version_info *vi,
333 int reservesize, int dtsize, int strsize,
334 int boot_cpuid_phys)
335{
336 int reserve_off;
337
338 reservesize += sizeof(struct fdt_reserve_entry);
339
340 memset(fdt, 0xff, sizeof(*fdt));
341
342 fdt->magic = cpu_to_fdt32(FDT_MAGIC);
343 fdt->version = cpu_to_fdt32(vi->version);
344 fdt->last_comp_version = cpu_to_fdt32(vi->last_comp_version);
345
346
347 reserve_off = ALIGN(vi->hdr_size, 8);
348
349 fdt->off_mem_rsvmap = cpu_to_fdt32(reserve_off);
350 fdt->off_dt_struct = cpu_to_fdt32(reserve_off + reservesize);
351 fdt->off_dt_strings = cpu_to_fdt32(reserve_off + reservesize
352 + dtsize);
353 fdt->totalsize = cpu_to_fdt32(reserve_off + reservesize + dtsize + strsize);
354
355 if (vi->flags & FTF_BOOTCPUID)
356 fdt->boot_cpuid_phys = cpu_to_fdt32(boot_cpuid_phys);
357 if (vi->flags & FTF_STRTABSIZE)
358 fdt->size_dt_strings = cpu_to_fdt32(strsize);
359 if (vi->flags & FTF_STRUCTSIZE)
360 fdt->size_dt_struct = cpu_to_fdt32(dtsize);
361}
362
363void dt_to_blob(FILE *f, struct boot_info *bi, int version)
364{
365 struct version_info *vi = NULL;
366 int i;
367 struct data blob = empty_data;
368 struct data reservebuf = empty_data;
369 struct data dtbuf = empty_data;
370 struct data strbuf = empty_data;
371 struct fdt_header fdt;
372 int padlen = 0;
373
374 for (i = 0; i < ARRAY_SIZE(version_table); i++) {
375 if (version_table[i].version == version)
376 vi = &version_table[i];
377 }
378 if (!vi)
379 die("Unknown device tree blob version %d\n", version);
380
381 flatten_tree(bi->dt, &bin_emitter, &dtbuf, &strbuf, vi);
382 bin_emit_cell(&dtbuf, FDT_END);
383
384 reservebuf = flatten_reserve_list(bi->reservelist, vi);
385
386
387 make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len,
388 bi->boot_cpuid_phys);
389
390 if (minsize > 0) {
391 padlen = minsize - fdt32_to_cpu(fdt.totalsize);
392 if ((padlen < 0) && (quiet < 1))
393 fprintf(stderr,
394 "Warning: blob size %d >= minimum size %d\n",
395 fdt32_to_cpu(fdt.totalsize), minsize);
396 }
397
398 if (padsize > 0)
399 padlen = padsize;
400
401 if (padlen > 0) {
402 int tsize = fdt32_to_cpu(fdt.totalsize);
403 tsize += padlen;
404 fdt.totalsize = cpu_to_fdt32(tsize);
405 }
406
407 blob = data_append_data(blob, &fdt, vi->hdr_size);
408 blob = data_append_align(blob, 8);
409 blob = data_merge(blob, reservebuf);
410 blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry));
411 blob = data_merge(blob, dtbuf);
412 blob = data_merge(blob, strbuf);
413
414 if (padlen > 0)
415 blob = data_append_zeroes(blob, padlen);
416
417 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 }
424
425 data_free(blob);
426}
427
428static void dump_stringtable_asm(FILE *f, struct data strbuf)
429{
430 const char *p;
431 int len;
432
433 p = strbuf.val;
434
435 while (p < (strbuf.val + strbuf.len)) {
436 len = strlen(p);
437 fprintf(f, "\t.string \"%s\"\n", p);
438 p += len+1;
439 }
440}
441
442void dt_to_asm(FILE *f, struct boot_info *bi, int version)
443{
444 struct version_info *vi = NULL;
445 int i;
446 struct data strbuf = empty_data;
447 struct reserve_info *re;
448 const char *symprefix = "dt";
449
450 for (i = 0; i < ARRAY_SIZE(version_table); i++) {
451 if (version_table[i].version == version)
452 vi = &version_table[i];
453 }
454 if (!vi)
455 die("Unknown device tree blob version %d\n", version);
456
457 fprintf(f, "/* autogenerated by dtc, do not edit */\n\n");
458
459 emit_label(f, symprefix, "blob_start");
460 emit_label(f, symprefix, "header");
461 fprintf(f, "\t/* magic */\n");
462 asm_emit_cell(f, FDT_MAGIC);
463 fprintf(f, "\t/* totalsize */\n");
464 ASM_EMIT_BELONG(f, "_%s_blob_abs_end - _%s_blob_start",
465 symprefix, symprefix);
466 fprintf(f, "\t/* off_dt_struct */\n");
467 ASM_EMIT_BELONG(f, "_%s_struct_start - _%s_blob_start",
468 symprefix, symprefix);
469 fprintf(f, "\t/* off_dt_strings */\n");
470 ASM_EMIT_BELONG(f, "_%s_strings_start - _%s_blob_start",
471 symprefix, symprefix);
472 fprintf(f, "\t/* off_mem_rsvmap */\n");
473 ASM_EMIT_BELONG(f, "_%s_reserve_map - _%s_blob_start",
474 symprefix, symprefix);
475 fprintf(f, "\t/* version */\n");
476 asm_emit_cell(f, vi->version);
477 fprintf(f, "\t/* last_comp_version */\n");
478 asm_emit_cell(f, vi->last_comp_version);
479
480 if (vi->flags & FTF_BOOTCPUID) {
481 fprintf(f, "\t/* boot_cpuid_phys */\n");
482 asm_emit_cell(f, bi->boot_cpuid_phys);
483 }
484
485 if (vi->flags & FTF_STRTABSIZE) {
486 fprintf(f, "\t/* size_dt_strings */\n");
487 ASM_EMIT_BELONG(f, "_%s_strings_end - _%s_strings_start",
488 symprefix, symprefix);
489 }
490
491 if (vi->flags & FTF_STRUCTSIZE) {
492 fprintf(f, "\t/* size_dt_struct */\n");
493 ASM_EMIT_BELONG(f, "_%s_struct_end - _%s_struct_start",
494 symprefix, symprefix);
495 }
496
497 asm_emit_align(f, 8);
498 emit_label(f, symprefix, "reserve_map");
499
500 fprintf(f, "/* Memory reserve map from source file */\n");
501
502 for (re = bi->reservelist; re; re = re->next) {
503 struct label *l;
504
505 for_each_label(re->labels, l) {
506 fprintf(f, "\t.globl\t%s\n", l->label);
507 fprintf(f, "%s:\n", l->label);
508 }
509 ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.address >> 32));
510 ASM_EMIT_BELONG(f, "0x%08x",
511 (unsigned int)(re->re.address & 0xffffffff));
512 ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.size >> 32));
513 ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.size & 0xffffffff));
514 }
515 for (i = 0; i < reservenum; i++) {
516 fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
517 }
518
519 fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
520
521 emit_label(f, symprefix, "struct_start");
522 flatten_tree(bi->dt, &asm_emitter, f, &strbuf, vi);
523
524 fprintf(f, "\t/* FDT_END */\n");
525 asm_emit_cell(f, FDT_END);
526 emit_label(f, symprefix, "struct_end");
527
528 emit_label(f, symprefix, "strings_start");
529 dump_stringtable_asm(f, strbuf);
530 emit_label(f, symprefix, "strings_end");
531
532 emit_label(f, symprefix, "blob_end");
533
534 if (minsize > 0) {
535 fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n",
536 minsize, symprefix, symprefix);
537 }
538 if (padsize > 0) {
539 fprintf(f, "\t.space\t%d, 0\n", padsize);
540 }
541 emit_label(f, symprefix, "blob_abs_end");
542
543 data_free(strbuf);
544}
545
546struct inbuf {
547 char *base, *limit, *ptr;
548};
549
550static void inbuf_init(struct inbuf *inb, void *base, void *limit)
551{
552 inb->base = base;
553 inb->limit = limit;
554 inb->ptr = inb->base;
555}
556
557static void flat_read_chunk(struct inbuf *inb, void *p, int len)
558{
559 if ((inb->ptr + len) > inb->limit)
560 die("Premature end of data parsing flat device tree\n");
561
562 memcpy(p, inb->ptr, len);
563
564 inb->ptr += len;
565}
566
567static uint32_t flat_read_word(struct inbuf *inb)
568{
569 uint32_t val;
570
571 assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
572
573 flat_read_chunk(inb, &val, sizeof(val));
574
575 return fdt32_to_cpu(val);
576}
577
578static void flat_realign(struct inbuf *inb, int align)
579{
580 int off = inb->ptr - inb->base;
581
582 inb->ptr = inb->base + ALIGN(off, align);
583 if (inb->ptr > inb->limit)
584 die("Premature end of data parsing flat device tree\n");
585}
586
587static char *flat_read_string(struct inbuf *inb)
588{
589 int len = 0;
590 const char *p = inb->ptr;
591 char *str;
592
593 do {
594 if (p >= inb->limit)
595 die("Premature end of data parsing flat device tree\n");
596 len++;
597 } while ((*p++) != '\0');
598
599 str = xstrdup(inb->ptr);
600
601 inb->ptr += len;
602
603 flat_realign(inb, sizeof(uint32_t));
604
605 return str;
606}
607
608static struct data flat_read_data(struct inbuf *inb, int len)
609{
610 struct data d = empty_data;
611
612 if (len == 0)
613 return empty_data;
614
615 d = data_grow_for(d, len);
616 d.len = len;
617
618 flat_read_chunk(inb, d.val, len);
619
620 flat_realign(inb, sizeof(uint32_t));
621
622 return d;
623}
624
625static char *flat_read_stringtable(struct inbuf *inb, int offset)
626{
627 const char *p;
628
629 p = inb->base + offset;
630 while (1) {
631 if (p >= inb->limit || p < inb->base)
632 die("String offset %d overruns string table\n",
633 offset);
634
635 if (*p == '\0')
636 break;
637
638 p++;
639 }
640
641 return xstrdup(inb->base + offset);
642}
643
644static struct property *flat_read_property(struct inbuf *dtbuf,
645 struct inbuf *strbuf, int flags)
646{
647 uint32_t proplen, stroff;
648 char *name;
649 struct data val;
650
651 proplen = flat_read_word(dtbuf);
652 stroff = flat_read_word(dtbuf);
653
654 name = flat_read_stringtable(strbuf, stroff);
655
656 if ((flags & FTF_VARALIGN) && (proplen >= 8))
657 flat_realign(dtbuf, 8);
658
659 val = flat_read_data(dtbuf, proplen);
660
661 return build_property(name, val);
662}
663
664
665static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
666{
667 struct reserve_info *reservelist = NULL;
668 struct reserve_info *new;
669 struct fdt_reserve_entry re;
670
671 while (1) {
672 flat_read_chunk(inb, &re, sizeof(re));
673 re.address = fdt64_to_cpu(re.address);
674 re.size = fdt64_to_cpu(re.size);
675 if (re.size == 0)
676 break;
677
678 new = build_reserve_entry(re.address, re.size);
679 reservelist = add_reserve_entry(reservelist, new);
680 }
681
682 return reservelist;
683}
684
685
686static char *nodename_from_path(const char *ppath, const char *cpath)
687{
688 int plen;
689
690 plen = strlen(ppath);
691
692 if (!strneq(ppath, cpath, plen))
693 die("Path \"%s\" is not valid as a child of \"%s\"\n",
694 cpath, ppath);
695
696
697 if (!streq(ppath, "/"))
698 plen++;
699
700 return xstrdup(cpath + plen);
701}
702
703static struct node *unflatten_tree(struct inbuf *dtbuf,
704 struct inbuf *strbuf,
705 const char *parent_flatname, int flags)
706{
707 struct node *node;
708 char *flatname;
709 uint32_t val;
710
711 node = build_node(NULL, NULL);
712
713 flatname = flat_read_string(dtbuf);
714
715 if (flags & FTF_FULLPATH)
716 node->name = nodename_from_path(parent_flatname, flatname);
717 else
718 node->name = flatname;
719
720 do {
721 struct property *prop;
722 struct node *child;
723
724 val = flat_read_word(dtbuf);
725 switch (val) {
726 case FDT_PROP:
727 if (node->children)
728 fprintf(stderr, "Warning: Flat tree input has "
729 "subnodes preceding a property.\n");
730 prop = flat_read_property(dtbuf, strbuf, flags);
731 add_property(node, prop);
732 break;
733
734 case FDT_BEGIN_NODE:
735 child = unflatten_tree(dtbuf,strbuf, flatname, flags);
736 add_child(node, child);
737 break;
738
739 case FDT_END_NODE:
740 break;
741
742 case FDT_END:
743 die("Premature FDT_END in device tree blob\n");
744 break;
745
746 case FDT_NOP:
747 if (!(flags & FTF_NOPS))
748 fprintf(stderr, "Warning: NOP tag found in flat tree"
749 " version <16\n");
750
751
752 break;
753
754 default:
755 die("Invalid opcode word %08x in device tree blob\n",
756 val);
757 }
758 } while (val != FDT_END_NODE);
759
760 return node;
761}
762
763
764struct boot_info *dt_from_blob(const char *fname)
765{
766 FILE *f;
767 uint32_t magic, totalsize, version, size_dt, boot_cpuid_phys;
768 uint32_t off_dt, off_str, off_mem_rsvmap;
769 int rc;
770 char *blob;
771 struct fdt_header *fdt;
772 char *p;
773 struct inbuf dtbuf, strbuf;
774 struct inbuf memresvbuf;
775 int sizeleft;
776 struct reserve_info *reservelist;
777 struct node *tree;
778 uint32_t val;
779 int flags = 0;
780
781 f = srcfile_relative_open(fname, NULL);
782
783 rc = fread(&magic, sizeof(magic), 1, f);
784 if (ferror(f))
785 die("Error reading DT blob magic number: %s\n",
786 strerror(errno));
787 if (rc < 1) {
788 if (feof(f))
789 die("EOF reading DT blob magic number\n");
790 else
791 die("Mysterious short read reading magic number\n");
792 }
793
794 magic = fdt32_to_cpu(magic);
795 if (magic != FDT_MAGIC)
796 die("Blob has incorrect magic number\n");
797
798 rc = fread(&totalsize, sizeof(totalsize), 1, f);
799 if (ferror(f))
800 die("Error reading DT blob size: %s\n", strerror(errno));
801 if (rc < 1) {
802 if (feof(f))
803 die("EOF reading DT blob size\n");
804 else
805 die("Mysterious short read reading blob size\n");
806 }
807
808 totalsize = fdt32_to_cpu(totalsize);
809 if (totalsize < FDT_V1_SIZE)
810 die("DT blob size (%d) is too small\n", totalsize);
811
812 blob = xmalloc(totalsize);
813
814 fdt = (struct fdt_header *)blob;
815 fdt->magic = cpu_to_fdt32(magic);
816 fdt->totalsize = cpu_to_fdt32(totalsize);
817
818 sizeleft = totalsize - sizeof(magic) - sizeof(totalsize);
819 p = blob + sizeof(magic) + sizeof(totalsize);
820
821 while (sizeleft) {
822 if (feof(f))
823 die("EOF before reading %d bytes of DT blob\n",
824 totalsize);
825
826 rc = fread(p, 1, sizeleft, f);
827 if (ferror(f))
828 die("Error reading DT blob: %s\n",
829 strerror(errno));
830
831 sizeleft -= rc;
832 p += rc;
833 }
834
835 off_dt = fdt32_to_cpu(fdt->off_dt_struct);
836 off_str = fdt32_to_cpu(fdt->off_dt_strings);
837 off_mem_rsvmap = fdt32_to_cpu(fdt->off_mem_rsvmap);
838 version = fdt32_to_cpu(fdt->version);
839 boot_cpuid_phys = fdt32_to_cpu(fdt->boot_cpuid_phys);
840
841 if (off_mem_rsvmap >= totalsize)
842 die("Mem Reserve structure offset exceeds total size\n");
843
844 if (off_dt >= totalsize)
845 die("DT structure offset exceeds total size\n");
846
847 if (off_str > totalsize)
848 die("String table offset exceeds total size\n");
849
850 if (version >= 3) {
851 uint32_t size_str = fdt32_to_cpu(fdt->size_dt_strings);
852 if (off_str+size_str > totalsize)
853 die("String table extends past total size\n");
854 inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str);
855 } else {
856 inbuf_init(&strbuf, blob + off_str, blob + totalsize);
857 }
858
859 if (version >= 17) {
860 size_dt = fdt32_to_cpu(fdt->size_dt_struct);
861 if (off_dt+size_dt > totalsize)
862 die("Structure block extends past total size\n");
863 }
864
865 if (version < 16) {
866 flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN;
867 } else {
868 flags |= FTF_NOPS;
869 }
870
871 inbuf_init(&memresvbuf,
872 blob + off_mem_rsvmap, blob + totalsize);
873 inbuf_init(&dtbuf, blob + off_dt, blob + totalsize);
874
875 reservelist = flat_read_mem_reserve(&memresvbuf);
876
877 val = flat_read_word(&dtbuf);
878
879 if (val != FDT_BEGIN_NODE)
880 die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val);
881
882 tree = unflatten_tree(&dtbuf, &strbuf, "", flags);
883
884 val = flat_read_word(&dtbuf);
885 if (val != FDT_END)
886 die("Device tree blob doesn't end with FDT_END\n");
887
888 free(blob);
889
890 fclose(f);
891
892 return build_boot_info(reservelist, tree, boot_cpuid_phys);
893}