1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
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
17 static struct version_info
{
19 int last_comp_version
;
24 FTF_FULLPATH
|FTF_VARALIGN
|FTF_NAMEPROPS
},
26 FTF_FULLPATH
|FTF_VARALIGN
|FTF_NAMEPROPS
|FTF_BOOTCPUID
},
28 FTF_FULLPATH
|FTF_VARALIGN
|FTF_NAMEPROPS
|FTF_BOOTCPUID
|FTF_STRTABSIZE
},
30 FTF_BOOTCPUID
|FTF_STRTABSIZE
|FTF_NOPS
},
31 {17, 16, FDT_V17_SIZE
,
32 FTF_BOOTCPUID
|FTF_STRTABSIZE
|FTF_STRUCTSIZE
|FTF_NOPS
},
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
);
45 static void bin_emit_cell(void *e
, cell_t val
)
47 struct data
*dtbuf
= e
;
49 *dtbuf
= data_append_cell(*dtbuf
, val
);
52 static void bin_emit_string(void *e
, const char *str
, int len
)
54 struct data
*dtbuf
= e
;
59 *dtbuf
= data_append_data(*dtbuf
, str
, len
);
60 *dtbuf
= data_append_byte(*dtbuf
, '\0');
63 static void bin_emit_align(void *e
, int a
)
65 struct data
*dtbuf
= e
;
67 *dtbuf
= data_append_align(*dtbuf
, a
);
70 static void bin_emit_data(void *e
, struct data d
)
72 struct data
*dtbuf
= e
;
74 *dtbuf
= data_append_data(*dtbuf
, d
.val
, d
.len
);
77 static void bin_emit_beginnode(void *e
, struct label
*labels
)
79 bin_emit_cell(e
, FDT_BEGIN_NODE
);
82 static void bin_emit_endnode(void *e
, struct label
*labels
)
84 bin_emit_cell(e
, FDT_END_NODE
);
87 static void bin_emit_property(void *e
, struct label
*labels
)
89 bin_emit_cell(e
, FDT_PROP
);
92 static 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
,
102 static void emit_label(FILE *f
, const char *prefix
, const char *label
)
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
);
109 static void emit_offset_label(FILE *f
, const char *label
, int offset
)
111 fprintf(f
, "\t.globl\t%s\n", label
);
112 fprintf(f
, "%s\t= . + %d\n", label
, offset
);
115 #define ASM_EMIT_BELONG(f, fmt, ...) \
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__); \
123 static void asm_emit_cell(void *e
, cell_t val
)
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);
133 static void asm_emit_string(void *e
, const char *str
, int len
)
138 fprintf(f
, "\t.asciz\t\"%.*s\"\n", len
, str
);
140 fprintf(f
, "\t.asciz\t\"%s\"\n", str
);
143 static void asm_emit_align(void *e
, int a
)
147 fprintf(f
, "\t.balign\t%d, 0\n", a
);
150 static void asm_emit_data(void *e
, struct data d
)
153 unsigned int off
= 0;
154 struct marker
*m
= d
.markers
;
156 for_each_marker_of_type(m
, LABEL
)
157 emit_offset_label(f
, m
->ref
, m
->offset
);
159 while ((d
.len
- off
) >= sizeof(uint32_t)) {
160 asm_emit_cell(e
, dtb_ld32(d
.val
+ off
));
161 off
+= sizeof(uint32_t);
164 while ((d
.len
- off
) >= 1) {
165 fprintf(f
, "\t.byte\t0x%hhx\n", d
.val
[off
]);
169 assert(off
== d
.len
);
172 static void asm_emit_beginnode(void *e
, struct label
*labels
)
177 for_each_label(labels
, l
) {
178 fprintf(f
, "\t.globl\t%s\n", l
->label
);
179 fprintf(f
, "%s:\n", l
->label
);
181 fprintf(f
, "\t/* FDT_BEGIN_NODE */\n");
182 asm_emit_cell(e
, FDT_BEGIN_NODE
);
185 static void asm_emit_endnode(void *e
, struct label
*labels
)
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
);
198 static void asm_emit_property(void *e
, struct label
*labels
)
203 for_each_label(labels
, l
) {
204 fprintf(f
, "\t.globl\t%s\n", l
->label
);
205 fprintf(f
, "%s:\n", l
->label
);
207 fprintf(f
, "\t/* FDT_PROP */\n");
208 asm_emit_cell(e
, FDT_PROP
);
211 static 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
,
221 static int stringtable_insert(struct data
*d
, const char *str
)
225 /* FIXME: do this more efficiently? */
227 for (i
= 0; i
< d
->len
; i
++) {
228 if (streq(str
, d
->val
+ i
))
232 *d
= data_append_data(*d
, str
, strlen(str
)+1);
236 static void flatten_tree(struct node
*tree
, struct emitter
*emit
,
237 void *etarget
, struct data
*strbuf
,
238 struct version_info
*vi
)
240 struct property
*prop
;
242 bool seen_name_prop
= false;
247 emit
->beginnode(etarget
, tree
->labels
);
249 if (vi
->flags
& FTF_FULLPATH
)
250 emit
->string(etarget
, tree
->fullpath
, 0);
252 emit
->string(etarget
, tree
->name
, 0);
254 emit
->align(etarget
, sizeof(cell_t
));
256 for_each_property(tree
, prop
) {
259 if (streq(prop
->name
, "name"))
260 seen_name_prop
= true;
262 nameoff
= stringtable_insert(strbuf
, prop
->name
);
264 emit
->property(etarget
, prop
->labels
);
265 emit
->cell(etarget
, prop
->val
.len
);
266 emit
->cell(etarget
, nameoff
);
268 if ((vi
->flags
& FTF_VARALIGN
) && (prop
->val
.len
>= 8))
269 emit
->align(etarget
, 8);
271 emit
->data(etarget
, prop
->val
);
272 emit
->align(etarget
, sizeof(cell_t
));
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"));
280 if ((vi
->flags
& FTF_VARALIGN
) && ((tree
->basenamelen
+1) >= 8))
281 emit
->align(etarget
, 8);
283 emit
->string(etarget
, tree
->name
, tree
->basenamelen
);
284 emit
->align(etarget
, sizeof(cell_t
));
287 for_each_child(tree
, child
) {
288 flatten_tree(child
, emit
, etarget
, strbuf
, vi
);
291 emit
->endnode(etarget
, tree
->labels
);
294 static struct data
flatten_reserve_list(struct reserve_info
*reservelist
,
295 struct version_info
*vi
)
297 struct reserve_info
*re
;
298 struct data d
= empty_data
;
301 for (re
= reservelist
; re
; re
= re
->next
) {
302 d
= data_append_re(d
, re
->address
, re
->size
);
305 * Add additional reserved slots if the user asked for them.
307 for (j
= 0; j
< reservenum
; j
++) {
308 d
= data_append_re(d
, 0, 0);
314 static void make_fdt_header(struct fdt_header
*fdt
,
315 struct version_info
*vi
,
316 int reservesize
, int dtsize
, int strsize
,
321 reservesize
+= sizeof(struct fdt_reserve_entry
);
323 memset(fdt
, 0xff, sizeof(*fdt
));
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
);
329 /* Reserve map should be doubleword aligned */
330 reserve_off
= ALIGN(vi
->hdr_size
, 8);
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
336 fdt
->totalsize
= cpu_to_fdt32(reserve_off
+ reservesize
+ dtsize
+ strsize
);
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
);
346 void dt_to_blob(FILE *f
, struct dt_info
*dti
, int version
)
348 struct version_info
*vi
= NULL
;
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
;
357 for (i
= 0; i
< ARRAY_SIZE(version_table
); i
++) {
358 if (version_table
[i
].version
== version
)
359 vi
= &version_table
[i
];
362 die("Unknown device tree blob version %d\n", version
);
364 flatten_tree(dti
->dt
, &bin_emitter
, &dtbuf
, &strbuf
, vi
);
365 bin_emit_cell(&dtbuf
, FDT_END
);
367 reservebuf
= flatten_reserve_list(dti
->reservelist
, vi
);
370 make_fdt_header(&fdt
, vi
, reservebuf
.len
, dtbuf
.len
, strbuf
.len
,
371 dti
->boot_cpuid_phys
);
374 * If the user asked for more space than is used, adjust the totalsize.
377 padlen
= minsize
- fdt32_to_cpu(fdt
.totalsize
);
382 "Warning: blob size %"PRIu32
" >= minimum size %d\n",
383 fdt32_to_cpu(fdt
.totalsize
), minsize
);
391 padlen
= ALIGN(fdt32_to_cpu(fdt
.totalsize
) + padlen
, alignsize
)
392 - fdt32_to_cpu(fdt
.totalsize
);
395 int tsize
= fdt32_to_cpu(fdt
.totalsize
);
397 fdt
.totalsize
= cpu_to_fdt32(tsize
);
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.
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
);
413 * If the user asked for more space than is used, pad out the blob.
416 blob
= data_append_zeroes(blob
, padlen
);
418 if (fwrite(blob
.val
, blob
.len
, 1, f
) != 1) {
420 die("Error writing device tree blob: %s\n",
423 die("Short write on device tree blob\n");
427 * data_merge() frees the right-hand element so only the blob
428 * remains to be freed.
433 static void dump_stringtable_asm(FILE *f
, struct data strbuf
)
440 while (p
< (strbuf
.val
+ strbuf
.len
)) {
442 fprintf(f
, "\t.asciz \"%s\"\n", p
);
447 void dt_to_asm(FILE *f
, struct dt_info
*dti
, int version
)
449 struct version_info
*vi
= NULL
;
451 struct data strbuf
= empty_data
;
452 struct reserve_info
*re
;
453 const char *symprefix
= "dt";
455 for (i
= 0; i
< ARRAY_SIZE(version_table
); i
++) {
456 if (version_table
[i
].version
== version
)
457 vi
= &version_table
[i
];
460 die("Unknown device tree blob version %d\n", version
);
462 fprintf(f
, "/* autogenerated by dtc, do not edit */\n\n");
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
);
485 if (vi
->flags
& FTF_BOOTCPUID
) {
486 fprintf(f
, "\t/* boot_cpuid_phys */\n");
487 asm_emit_cell(f
, dti
->boot_cpuid_phys
);
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
);
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
);
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.
508 asm_emit_align(f
, 8);
509 emit_label(f
, symprefix
, "reserve_map");
511 fprintf(f
, "/* Memory reserve map from source file */\n");
514 * Use .long on high and low halves of u64s to avoid .quad
515 * as it appears .quad isn't available in some assemblers.
517 for (re
= dti
->reservelist
; re
; re
= re
->next
) {
520 for_each_label(re
->labels
, l
) {
521 fprintf(f
, "\t.globl\t%s\n", l
->label
);
522 fprintf(f
, "%s:\n", l
->label
);
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));
530 for (i
= 0; i
< reservenum
; i
++) {
531 fprintf(f
, "\t.long\t0, 0\n\t.long\t0, 0\n");
534 fprintf(f
, "\t.long\t0, 0\n\t.long\t0, 0\n");
536 emit_label(f
, symprefix
, "struct_start");
537 flatten_tree(dti
->dt
, &asm_emitter
, f
, &strbuf
, vi
);
539 fprintf(f
, "\t/* FDT_END */\n");
540 asm_emit_cell(f
, FDT_END
);
541 emit_label(f
, symprefix
, "struct_end");
543 emit_label(f
, symprefix
, "strings_start");
544 dump_stringtable_asm(f
, strbuf
);
545 emit_label(f
, symprefix
, "strings_end");
547 emit_label(f
, symprefix
, "blob_end");
550 * If the user asked for more space than is used, pad it out.
553 fprintf(f
, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n",
554 minsize
, symprefix
, symprefix
);
557 fprintf(f
, "\t.space\t%d, 0\n", padsize
);
560 asm_emit_align(f
, alignsize
);
561 emit_label(f
, symprefix
, "blob_abs_end");
567 char *base
, *limit
, *ptr
;
570 static void inbuf_init(struct inbuf
*inb
, void *base
, void *limit
)
574 inb
->ptr
= inb
->base
;
577 static void flat_read_chunk(struct inbuf
*inb
, void *p
, int len
)
579 if ((inb
->ptr
+ len
) > inb
->limit
)
580 die("Premature end of data parsing flat device tree\n");
582 memcpy(p
, inb
->ptr
, len
);
587 static uint32_t flat_read_word(struct inbuf
*inb
)
591 assert(((inb
->ptr
- inb
->base
) % sizeof(val
)) == 0);
593 flat_read_chunk(inb
, &val
, sizeof(val
));
595 return fdt32_to_cpu(val
);
598 static void flat_realign(struct inbuf
*inb
, int align
)
600 int off
= inb
->ptr
- inb
->base
;
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");
607 static const char *flat_read_string(struct inbuf
*inb
)
610 const char *p
= inb
->ptr
;
615 die("Premature end of data parsing flat device tree\n");
617 } while ((*p
++) != '\0');
623 flat_realign(inb
, sizeof(uint32_t));
628 static struct data
flat_read_data(struct inbuf
*inb
, int len
)
630 struct data d
= empty_data
;
635 d
= data_grow_for(d
, len
);
638 flat_read_chunk(inb
, d
.val
, len
);
640 flat_realign(inb
, sizeof(uint32_t));
645 static char *flat_read_stringtable(struct inbuf
*inb
, int offset
)
649 p
= inb
->base
+ offset
;
651 if (p
>= inb
->limit
|| p
< inb
->base
)
652 die("String offset %d overruns string table\n",
661 return xstrdup(inb
->base
+ offset
);
664 static struct property
*flat_read_property(struct inbuf
*dtbuf
,
665 struct inbuf
*strbuf
, int flags
)
667 uint32_t proplen
, stroff
;
671 proplen
= flat_read_word(dtbuf
);
672 stroff
= flat_read_word(dtbuf
);
674 name
= flat_read_stringtable(strbuf
, stroff
);
676 if ((flags
& FTF_VARALIGN
) && (proplen
>= 8))
677 flat_realign(dtbuf
, 8);
679 val
= flat_read_data(dtbuf
, proplen
);
681 return build_property(name
, val
, NULL
);
685 static struct reserve_info
*flat_read_mem_reserve(struct inbuf
*inb
)
687 struct reserve_info
*reservelist
= NULL
;
688 struct reserve_info
*new;
689 struct fdt_reserve_entry re
;
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.
695 * First pass, count entries.
698 uint64_t address
, size
;
700 flat_read_chunk(inb
, &re
, sizeof(re
));
701 address
= fdt64_to_cpu(re
.address
);
702 size
= fdt64_to_cpu(re
.size
);
706 new = build_reserve_entry(address
, size
);
707 reservelist
= add_reserve_entry(reservelist
, new);
714 static const char *nodename_from_path(const char *ppath
, const char *cpath
)
718 plen
= strlen(ppath
);
720 if (!strstarts(cpath
, ppath
))
721 die("Path \"%s\" is not valid as a child of \"%s\"\n",
724 /* root node is a special case */
725 if (!streq(ppath
, "/"))
731 static struct node
*unflatten_tree(struct inbuf
*dtbuf
,
732 struct inbuf
*strbuf
,
733 const char *parent_flatname
, int flags
)
736 const char *flatname
;
739 node
= build_node(NULL
, NULL
, NULL
);
741 flatname
= flat_read_string(dtbuf
);
743 if (flags
& FTF_FULLPATH
)
744 node
->name
= xstrdup(nodename_from_path(parent_flatname
,
747 node
->name
= xstrdup(flatname
);
750 struct property
*prop
;
753 val
= flat_read_word(dtbuf
);
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
);
764 child
= unflatten_tree(dtbuf
,strbuf
, flatname
, flags
);
765 add_child(node
, child
);
772 die("Premature FDT_END in device tree blob\n");
776 if (!(flags
& FTF_NOPS
))
777 fprintf(stderr
, "Warning: NOP tag found in flat tree"
784 die("Invalid opcode word %08x in device tree blob\n",
787 } while (val
!= FDT_END_NODE
);
793 struct dt_info
*dt_from_blob(const char *fname
)
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
;
801 struct fdt_header
*fdt
;
803 struct inbuf dtbuf
, strbuf
;
804 struct inbuf memresvbuf
;
806 struct reserve_info
*reservelist
;
811 f
= srcfile_relative_open(fname
, NULL
);
813 rc
= fread(&magic_buf
, sizeof(magic_buf
), 1, f
);
815 die("Error reading DT blob magic number: %s\n",
819 die("EOF reading DT blob magic number\n");
821 die("Mysterious short read reading magic number\n");
824 magic
= fdt32_to_cpu(magic_buf
);
825 if (magic
!= FDT_MAGIC
)
826 die("Blob has incorrect magic number\n");
828 rc
= fread(&totalsize_buf
, sizeof(totalsize_buf
), 1, f
);
830 die("Error reading DT blob size: %s\n", strerror(errno
));
833 die("EOF reading DT blob size\n");
835 die("Mysterious short read reading blob size\n");
838 totalsize
= fdt32_to_cpu(totalsize_buf
);
839 if (totalsize
< FDT_V1_SIZE
)
840 die("DT blob size (%d) is too small\n", totalsize
);
842 blob
= xmalloc(totalsize
);
844 fdt
= (struct fdt_header
*)blob
;
845 fdt
->magic
= cpu_to_fdt32(magic
);
846 fdt
->totalsize
= cpu_to_fdt32(totalsize
);
848 sizeleft
= totalsize
- sizeof(magic
) - sizeof(totalsize
);
849 p
= blob
+ sizeof(magic
) + sizeof(totalsize
);
853 die("EOF before reading %d bytes of DT blob\n",
856 rc
= fread(p
, 1, sizeleft
, f
);
858 die("Error reading DT blob: %s\n",
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
);
871 if (off_mem_rsvmap
>= totalsize
)
872 die("Mem Reserve structure offset exceeds total size\n");
874 if (off_dt
>= totalsize
)
875 die("DT structure offset exceeds total size\n");
877 if (off_str
> totalsize
)
878 die("String table offset exceeds total size\n");
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
);
886 inbuf_init(&strbuf
, blob
+ off_str
, blob
+ totalsize
);
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");
896 flags
|= FTF_FULLPATH
| FTF_NAMEPROPS
| FTF_VARALIGN
;
901 inbuf_init(&memresvbuf
,
902 blob
+ off_mem_rsvmap
, blob
+ totalsize
);
903 inbuf_init(&dtbuf
, blob
+ off_dt
, blob
+ totalsize
);
905 reservelist
= flat_read_mem_reserve(&memresvbuf
);
907 val
= flat_read_word(&dtbuf
);
909 if (val
!= FDT_BEGIN_NODE
)
910 die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val
);
912 tree
= unflatten_tree(&dtbuf
, &strbuf
, "", flags
);
914 val
= flat_read_word(&dtbuf
);
916 die("Device tree blob doesn't end with FDT_END\n");
922 return build_dt_info(DTSF_V1
, reservelist
, tree
, boot_cpuid_phys
);