]>
Commit | Line | Data |
---|---|---|
a4da2e3e DG |
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" | |
ed95d745 | 22 | #include "srcpos.h" |
a4da2e3e DG |
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 | ||
32 | static 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 | ||
50 | struct emitter { | |
51 | void (*cell)(void *, cell_t); | |
52 | void (*string)(void *, char *, int); | |
53 | void (*align)(void *, int); | |
54 | void (*data)(void *, struct data); | |
658f29a5 JB |
55 | void (*beginnode)(void *, struct label *labels); |
56 | void (*endnode)(void *, struct label *labels); | |
57 | void (*property)(void *, struct label *labels); | |
a4da2e3e DG |
58 | }; |
59 | ||
60 | static 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 | ||
67 | static 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 | ||
78 | static void bin_emit_align(void *e, int a) | |
79 | { | |
80 | struct data *dtbuf = e; | |
81 | ||
82 | *dtbuf = data_append_align(*dtbuf, a); | |
83 | } | |
84 | ||
85 | static 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 | ||
658f29a5 | 92 | static void bin_emit_beginnode(void *e, struct label *labels) |
a4da2e3e DG |
93 | { |
94 | bin_emit_cell(e, FDT_BEGIN_NODE); | |
95 | } | |
96 | ||
658f29a5 | 97 | static void bin_emit_endnode(void *e, struct label *labels) |
a4da2e3e DG |
98 | { |
99 | bin_emit_cell(e, FDT_END_NODE); | |
100 | } | |
101 | ||
658f29a5 | 102 | static void bin_emit_property(void *e, struct label *labels) |
a4da2e3e DG |
103 | { |
104 | bin_emit_cell(e, FDT_PROP); | |
105 | } | |
106 | ||
107 | static 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 | ||
117 | static 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 | ||
124 | static 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 | ||
658f29a5 JB |
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 | ||
a4da2e3e DG |
138 | static void asm_emit_cell(void *e, cell_t val) |
139 | { | |
140 | FILE *f = e; | |
141 | ||
658f29a5 JB |
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); | |
a4da2e3e DG |
145 | } |
146 | ||
147 | static 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 | ||
165 | static void asm_emit_align(void *e, int a) | |
166 | { | |
167 | FILE *f = e; | |
168 | ||
658f29a5 | 169 | fprintf(f, "\t.balign\t%d, 0\n", a); |
a4da2e3e DG |
170 | } |
171 | ||
172 | static void asm_emit_data(void *e, struct data d) | |
173 | { | |
174 | FILE *f = e; | |
175 | int off = 0; | |
ed95d745 | 176 | struct marker *m = d.markers; |
a4da2e3e | 177 | |
ed95d745 DG |
178 | for_each_marker_of_type(m, LABEL) |
179 | emit_offset_label(f, m->ref, m->offset); | |
a4da2e3e | 180 | |
ed95d745 | 181 | while ((d.len - off) >= sizeof(uint32_t)) { |
658f29a5 | 182 | asm_emit_cell(e, fdt32_to_cpu(*((uint32_t *)(d.val+off)))); |
ed95d745 | 183 | off += sizeof(uint32_t); |
a4da2e3e DG |
184 | } |
185 | ||
ed95d745 | 186 | while ((d.len - off) >= 1) { |
a4da2e3e DG |
187 | fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]); |
188 | off += 1; | |
189 | } | |
190 | ||
191 | assert(off == d.len); | |
192 | } | |
193 | ||
658f29a5 | 194 | static void asm_emit_beginnode(void *e, struct label *labels) |
a4da2e3e DG |
195 | { |
196 | FILE *f = e; | |
658f29a5 | 197 | struct label *l; |
a4da2e3e | 198 | |
658f29a5 JB |
199 | for_each_label(labels, l) { |
200 | fprintf(f, "\t.globl\t%s\n", l->label); | |
201 | fprintf(f, "%s:\n", l->label); | |
a4da2e3e | 202 | } |
658f29a5 JB |
203 | fprintf(f, "\t/* FDT_BEGIN_NODE */\n"); |
204 | asm_emit_cell(e, FDT_BEGIN_NODE); | |
a4da2e3e DG |
205 | } |
206 | ||
658f29a5 | 207 | static void asm_emit_endnode(void *e, struct label *labels) |
a4da2e3e DG |
208 | { |
209 | FILE *f = e; | |
658f29a5 | 210 | struct label *l; |
a4da2e3e | 211 | |
658f29a5 JB |
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); | |
a4da2e3e DG |
217 | } |
218 | } | |
219 | ||
658f29a5 | 220 | static void asm_emit_property(void *e, struct label *labels) |
a4da2e3e DG |
221 | { |
222 | FILE *f = e; | |
658f29a5 | 223 | struct label *l; |
a4da2e3e | 224 | |
658f29a5 JB |
225 | for_each_label(labels, l) { |
226 | fprintf(f, "\t.globl\t%s\n", l->label); | |
227 | fprintf(f, "%s:\n", l->label); | |
a4da2e3e | 228 | } |
658f29a5 JB |
229 | fprintf(f, "\t/* FDT_PROP */\n"); |
230 | asm_emit_cell(e, FDT_PROP); | |
a4da2e3e DG |
231 | } |
232 | ||
233 | static 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 | ||
243 | static 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 | ||
53e5e540 PE |
258 | static void emit_local_fixups(struct node *tree, struct emitter *emit, |
259 | void *etarget, struct data *strbuf, struct version_info *vi, | |
260 | struct node *node) | |
261 | { | |
262 | struct fixup_entry *fe, *fen; | |
263 | struct node *child; | |
264 | int nameoff, count; | |
265 | cell_t *buf; | |
266 | struct data d; | |
267 | ||
268 | if (node->emit_local_fixup_node) { | |
269 | ||
270 | /* emit the external fixups (do not emit /) */ | |
271 | if (node != tree) { | |
272 | emit->beginnode(etarget, NULL); | |
273 | emit->string(etarget, node->name, 0); | |
274 | emit->align(etarget, sizeof(cell_t)); | |
275 | } | |
276 | ||
277 | for_each_local_fixup_entry(tree, fe) { | |
278 | if (fe->node != node || fe->local_fixup_generated) | |
279 | continue; | |
280 | ||
281 | /* count the number of fixup entries */ | |
282 | count = 0; | |
283 | for_each_local_fixup_entry(tree, fen) { | |
284 | if (fen->prop != fe->prop) | |
285 | continue; | |
286 | fen->local_fixup_generated = true; | |
287 | count++; | |
288 | } | |
289 | ||
290 | /* allocate buffer */ | |
291 | buf = xmalloc(count * sizeof(cell_t)); | |
292 | ||
293 | /* collect all the offsets in buffer */ | |
294 | count = 0; | |
295 | for_each_local_fixup_entry(tree, fen) { | |
296 | if (fen->prop != fe->prop) | |
297 | continue; | |
298 | fen->local_fixup_generated = true; | |
299 | buf[count++] = cpu_to_fdt32(fen->offset); | |
300 | } | |
301 | d = empty_data; | |
302 | d.len = count * sizeof(cell_t); | |
303 | d.val = (char *)buf; | |
304 | ||
305 | nameoff = stringtable_insert(strbuf, fe->prop->name); | |
306 | emit->property(etarget, fe->prop->labels); | |
307 | emit->cell(etarget, count * sizeof(cell_t)); | |
308 | emit->cell(etarget, nameoff); | |
309 | ||
310 | if ((vi->flags & FTF_VARALIGN) && | |
311 | (count * sizeof(cell_t)) >= 8) | |
312 | emit->align(etarget, 8); | |
313 | ||
314 | emit->data(etarget, d); | |
315 | emit->align(etarget, sizeof(cell_t)); | |
316 | ||
317 | free(buf); | |
318 | } | |
319 | } | |
320 | ||
321 | for_each_child(node, child) | |
322 | emit_local_fixups(tree, emit, etarget, strbuf, vi, child); | |
323 | ||
324 | if (node->emit_local_fixup_node && node != tree) | |
325 | emit->endnode(etarget, tree->labels); | |
326 | } | |
327 | ||
328 | static void emit_symbols_node(struct node *tree, struct emitter *emit, | |
329 | void *etarget, struct data *strbuf, | |
330 | struct version_info *vi) | |
331 | { | |
332 | struct symbol *sym; | |
333 | int nameoff, vallen; | |
334 | ||
335 | /* do nothing if no symbols */ | |
336 | if (!tree->symbols) | |
337 | return; | |
338 | ||
339 | emit->beginnode(etarget, NULL); | |
340 | emit->string(etarget, "__symbols__", 0); | |
341 | emit->align(etarget, sizeof(cell_t)); | |
342 | ||
343 | for_each_symbol(tree, sym) { | |
344 | ||
345 | vallen = strlen(sym->node->fullpath); | |
346 | ||
347 | nameoff = stringtable_insert(strbuf, sym->label->label); | |
348 | ||
349 | emit->property(etarget, NULL); | |
350 | emit->cell(etarget, vallen + 1); | |
351 | emit->cell(etarget, nameoff); | |
352 | ||
353 | if ((vi->flags & FTF_VARALIGN) && vallen >= 8) | |
354 | emit->align(etarget, 8); | |
355 | ||
356 | emit->string(etarget, sym->node->fullpath, | |
357 | strlen(sym->node->fullpath)); | |
358 | emit->align(etarget, sizeof(cell_t)); | |
359 | } | |
360 | ||
361 | emit->endnode(etarget, NULL); | |
362 | } | |
363 | ||
364 | static void emit_local_fixups_node(struct node *tree, struct emitter *emit, | |
365 | void *etarget, struct data *strbuf, | |
366 | struct version_info *vi) | |
367 | { | |
368 | struct fixup_entry *fe; | |
369 | struct node *node; | |
370 | ||
371 | /* do nothing if no local fixups */ | |
372 | if (!tree->local_fixups) | |
373 | return; | |
374 | ||
375 | /* mark all nodes that need a local fixup generated (and parents) */ | |
376 | for_each_local_fixup_entry(tree, fe) { | |
377 | node = fe->node; | |
378 | while (node != NULL && !node->emit_local_fixup_node) { | |
379 | node->emit_local_fixup_node = true; | |
380 | node = node->parent; | |
381 | } | |
382 | } | |
383 | ||
384 | /* emit the local fixups node now */ | |
385 | emit->beginnode(etarget, NULL); | |
386 | emit->string(etarget, "__local_fixups__", 0); | |
387 | emit->align(etarget, sizeof(cell_t)); | |
388 | ||
389 | emit_local_fixups(tree, emit, etarget, strbuf, vi, tree); | |
390 | ||
391 | emit->endnode(etarget, tree->labels); | |
392 | } | |
393 | ||
394 | static void emit_fixups_node(struct node *tree, struct emitter *emit, | |
395 | void *etarget, struct data *strbuf, | |
396 | struct version_info *vi) | |
397 | { | |
398 | struct fixup *f; | |
399 | struct fixup_entry *fe; | |
400 | char *name, *s; | |
401 | const char *fullpath; | |
402 | int namesz, nameoff, vallen; | |
403 | ||
404 | /* do nothing if no fixups */ | |
405 | if (!tree->fixups) | |
406 | return; | |
407 | ||
408 | /* emit the external fixups */ | |
409 | emit->beginnode(etarget, NULL); | |
410 | emit->string(etarget, "__fixups__", 0); | |
411 | emit->align(etarget, sizeof(cell_t)); | |
412 | ||
413 | for_each_fixup(tree, f) { | |
414 | ||
415 | namesz = 0; | |
416 | for_each_fixup_entry(f, fe) { | |
417 | fullpath = fe->node->fullpath; | |
418 | if (fullpath[0] == '\0') | |
419 | fullpath = "/"; | |
420 | namesz += strlen(fullpath) + 1; | |
421 | namesz += strlen(fe->prop->name) + 1; | |
422 | namesz += 32; /* space for :<number> + '\0' */ | |
423 | } | |
424 | ||
425 | name = xmalloc(namesz); | |
426 | ||
427 | s = name; | |
428 | for_each_fixup_entry(f, fe) { | |
429 | fullpath = fe->node->fullpath; | |
430 | if (fullpath[0] == '\0') | |
431 | fullpath = "/"; | |
432 | snprintf(s, name + namesz - s, "%s:%s:%d", fullpath, | |
433 | fe->prop->name, fe->offset); | |
434 | s += strlen(s) + 1; | |
435 | } | |
436 | ||
437 | nameoff = stringtable_insert(strbuf, f->ref); | |
438 | vallen = s - name - 1; | |
439 | ||
440 | emit->property(etarget, NULL); | |
441 | emit->cell(etarget, vallen + 1); | |
442 | emit->cell(etarget, nameoff); | |
443 | ||
444 | if ((vi->flags & FTF_VARALIGN) && vallen >= 8) | |
445 | emit->align(etarget, 8); | |
446 | ||
447 | emit->string(etarget, name, vallen); | |
448 | emit->align(etarget, sizeof(cell_t)); | |
449 | ||
450 | free(name); | |
451 | } | |
452 | ||
453 | emit->endnode(etarget, tree->labels); | |
454 | } | |
455 | ||
a4da2e3e DG |
456 | static void flatten_tree(struct node *tree, struct emitter *emit, |
457 | void *etarget, struct data *strbuf, | |
458 | struct version_info *vi) | |
459 | { | |
460 | struct property *prop; | |
461 | struct node *child; | |
47605971 | 462 | bool seen_name_prop = false; |
a4da2e3e | 463 | |
cd296721 SW |
464 | if (tree->deleted) |
465 | return; | |
466 | ||
658f29a5 | 467 | emit->beginnode(etarget, tree->labels); |
a4da2e3e DG |
468 | |
469 | if (vi->flags & FTF_FULLPATH) | |
470 | emit->string(etarget, tree->fullpath, 0); | |
471 | else | |
472 | emit->string(etarget, tree->name, 0); | |
473 | ||
474 | emit->align(etarget, sizeof(cell_t)); | |
475 | ||
476 | for_each_property(tree, prop) { | |
477 | int nameoff; | |
478 | ||
479 | if (streq(prop->name, "name")) | |
47605971 | 480 | seen_name_prop = true; |
a4da2e3e DG |
481 | |
482 | nameoff = stringtable_insert(strbuf, prop->name); | |
483 | ||
658f29a5 | 484 | emit->property(etarget, prop->labels); |
a4da2e3e DG |
485 | emit->cell(etarget, prop->val.len); |
486 | emit->cell(etarget, nameoff); | |
487 | ||
488 | if ((vi->flags & FTF_VARALIGN) && (prop->val.len >= 8)) | |
489 | emit->align(etarget, 8); | |
490 | ||
491 | emit->data(etarget, prop->val); | |
492 | emit->align(etarget, sizeof(cell_t)); | |
493 | } | |
494 | ||
495 | if ((vi->flags & FTF_NAMEPROPS) && !seen_name_prop) { | |
496 | emit->property(etarget, NULL); | |
497 | emit->cell(etarget, tree->basenamelen+1); | |
498 | emit->cell(etarget, stringtable_insert(strbuf, "name")); | |
499 | ||
500 | if ((vi->flags & FTF_VARALIGN) && ((tree->basenamelen+1) >= 8)) | |
501 | emit->align(etarget, 8); | |
502 | ||
503 | emit->string(etarget, tree->name, tree->basenamelen); | |
504 | emit->align(etarget, sizeof(cell_t)); | |
505 | } | |
506 | ||
507 | for_each_child(tree, child) { | |
508 | flatten_tree(child, emit, etarget, strbuf, vi); | |
509 | } | |
510 | ||
53e5e540 PE |
511 | emit_symbols_node(tree, emit, etarget, strbuf, vi); |
512 | emit_local_fixups_node(tree, emit, etarget, strbuf, vi); | |
513 | emit_fixups_node(tree, emit, etarget, strbuf, vi); | |
514 | ||
658f29a5 | 515 | emit->endnode(etarget, tree->labels); |
a4da2e3e DG |
516 | } |
517 | ||
518 | static struct data flatten_reserve_list(struct reserve_info *reservelist, | |
519 | struct version_info *vi) | |
520 | { | |
521 | struct reserve_info *re; | |
522 | struct data d = empty_data; | |
523 | static struct fdt_reserve_entry null_re = {0,0}; | |
524 | int j; | |
525 | ||
526 | for (re = reservelist; re; re = re->next) { | |
527 | d = data_append_re(d, &re->re); | |
528 | } | |
529 | /* | |
530 | * Add additional reserved slots if the user asked for them. | |
531 | */ | |
532 | for (j = 0; j < reservenum; j++) { | |
533 | d = data_append_re(d, &null_re); | |
534 | } | |
535 | ||
536 | return d; | |
537 | } | |
538 | ||
539 | static void make_fdt_header(struct fdt_header *fdt, | |
540 | struct version_info *vi, | |
541 | int reservesize, int dtsize, int strsize, | |
542 | int boot_cpuid_phys) | |
543 | { | |
544 | int reserve_off; | |
545 | ||
546 | reservesize += sizeof(struct fdt_reserve_entry); | |
547 | ||
548 | memset(fdt, 0xff, sizeof(*fdt)); | |
549 | ||
ed95d745 DG |
550 | fdt->magic = cpu_to_fdt32(FDT_MAGIC); |
551 | fdt->version = cpu_to_fdt32(vi->version); | |
552 | fdt->last_comp_version = cpu_to_fdt32(vi->last_comp_version); | |
a4da2e3e DG |
553 | |
554 | /* Reserve map should be doubleword aligned */ | |
555 | reserve_off = ALIGN(vi->hdr_size, 8); | |
556 | ||
ed95d745 DG |
557 | fdt->off_mem_rsvmap = cpu_to_fdt32(reserve_off); |
558 | fdt->off_dt_struct = cpu_to_fdt32(reserve_off + reservesize); | |
559 | fdt->off_dt_strings = cpu_to_fdt32(reserve_off + reservesize | |
a4da2e3e | 560 | + dtsize); |
ed95d745 | 561 | fdt->totalsize = cpu_to_fdt32(reserve_off + reservesize + dtsize + strsize); |
a4da2e3e DG |
562 | |
563 | if (vi->flags & FTF_BOOTCPUID) | |
ed95d745 | 564 | fdt->boot_cpuid_phys = cpu_to_fdt32(boot_cpuid_phys); |
a4da2e3e | 565 | if (vi->flags & FTF_STRTABSIZE) |
ed95d745 | 566 | fdt->size_dt_strings = cpu_to_fdt32(strsize); |
a4da2e3e | 567 | if (vi->flags & FTF_STRUCTSIZE) |
ed95d745 | 568 | fdt->size_dt_struct = cpu_to_fdt32(dtsize); |
a4da2e3e DG |
569 | } |
570 | ||
ed95d745 | 571 | void dt_to_blob(FILE *f, struct boot_info *bi, int version) |
a4da2e3e DG |
572 | { |
573 | struct version_info *vi = NULL; | |
574 | int i; | |
575 | struct data blob = empty_data; | |
576 | struct data reservebuf = empty_data; | |
577 | struct data dtbuf = empty_data; | |
578 | struct data strbuf = empty_data; | |
579 | struct fdt_header fdt; | |
580 | int padlen = 0; | |
581 | ||
582 | for (i = 0; i < ARRAY_SIZE(version_table); i++) { | |
583 | if (version_table[i].version == version) | |
584 | vi = &version_table[i]; | |
585 | } | |
586 | if (!vi) | |
587 | die("Unknown device tree blob version %d\n", version); | |
588 | ||
589 | flatten_tree(bi->dt, &bin_emitter, &dtbuf, &strbuf, vi); | |
590 | bin_emit_cell(&dtbuf, FDT_END); | |
591 | ||
592 | reservebuf = flatten_reserve_list(bi->reservelist, vi); | |
593 | ||
594 | /* Make header */ | |
595 | make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len, | |
ed95d745 | 596 | bi->boot_cpuid_phys); |
a4da2e3e DG |
597 | |
598 | /* | |
599 | * If the user asked for more space than is used, adjust the totalsize. | |
600 | */ | |
601 | if (minsize > 0) { | |
ed95d745 | 602 | padlen = minsize - fdt32_to_cpu(fdt.totalsize); |
a4da2e3e DG |
603 | if ((padlen < 0) && (quiet < 1)) |
604 | fprintf(stderr, | |
605 | "Warning: blob size %d >= minimum size %d\n", | |
ed95d745 | 606 | fdt32_to_cpu(fdt.totalsize), minsize); |
a4da2e3e DG |
607 | } |
608 | ||
609 | if (padsize > 0) | |
610 | padlen = padsize; | |
611 | ||
612 | if (padlen > 0) { | |
ed95d745 | 613 | int tsize = fdt32_to_cpu(fdt.totalsize); |
a4da2e3e | 614 | tsize += padlen; |
ed95d745 | 615 | fdt.totalsize = cpu_to_fdt32(tsize); |
a4da2e3e DG |
616 | } |
617 | ||
618 | /* | |
619 | * Assemble the blob: start with the header, add with alignment | |
620 | * the reserve buffer, add the reserve map terminating zeroes, | |
621 | * the device tree itself, and finally the strings. | |
622 | */ | |
ed95d745 | 623 | blob = data_append_data(blob, &fdt, vi->hdr_size); |
a4da2e3e DG |
624 | blob = data_append_align(blob, 8); |
625 | blob = data_merge(blob, reservebuf); | |
626 | blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry)); | |
627 | blob = data_merge(blob, dtbuf); | |
628 | blob = data_merge(blob, strbuf); | |
629 | ||
630 | /* | |
631 | * If the user asked for more space than is used, pad out the blob. | |
632 | */ | |
633 | if (padlen > 0) | |
634 | blob = data_append_zeroes(blob, padlen); | |
635 | ||
658f29a5 JB |
636 | if (fwrite(blob.val, blob.len, 1, f) != 1) { |
637 | if (ferror(f)) | |
638 | die("Error writing device tree blob: %s\n", | |
639 | strerror(errno)); | |
640 | else | |
641 | die("Short write on device tree blob\n"); | |
642 | } | |
a4da2e3e DG |
643 | |
644 | /* | |
645 | * data_merge() frees the right-hand element so only the blob | |
646 | * remains to be freed. | |
647 | */ | |
648 | data_free(blob); | |
649 | } | |
650 | ||
651 | static void dump_stringtable_asm(FILE *f, struct data strbuf) | |
652 | { | |
653 | const char *p; | |
654 | int len; | |
655 | ||
656 | p = strbuf.val; | |
657 | ||
658 | while (p < (strbuf.val + strbuf.len)) { | |
659 | len = strlen(p); | |
660 | fprintf(f, "\t.string \"%s\"\n", p); | |
661 | p += len+1; | |
662 | } | |
663 | } | |
664 | ||
ed95d745 | 665 | void dt_to_asm(FILE *f, struct boot_info *bi, int version) |
a4da2e3e DG |
666 | { |
667 | struct version_info *vi = NULL; | |
668 | int i; | |
669 | struct data strbuf = empty_data; | |
670 | struct reserve_info *re; | |
671 | const char *symprefix = "dt"; | |
672 | ||
673 | for (i = 0; i < ARRAY_SIZE(version_table); i++) { | |
674 | if (version_table[i].version == version) | |
675 | vi = &version_table[i]; | |
676 | } | |
677 | if (!vi) | |
678 | die("Unknown device tree blob version %d\n", version); | |
679 | ||
680 | fprintf(f, "/* autogenerated by dtc, do not edit */\n\n"); | |
a4da2e3e DG |
681 | |
682 | emit_label(f, symprefix, "blob_start"); | |
683 | emit_label(f, symprefix, "header"); | |
658f29a5 JB |
684 | fprintf(f, "\t/* magic */\n"); |
685 | asm_emit_cell(f, FDT_MAGIC); | |
686 | fprintf(f, "\t/* totalsize */\n"); | |
687 | ASM_EMIT_BELONG(f, "_%s_blob_abs_end - _%s_blob_start", | |
688 | symprefix, symprefix); | |
689 | fprintf(f, "\t/* off_dt_struct */\n"); | |
690 | ASM_EMIT_BELONG(f, "_%s_struct_start - _%s_blob_start", | |
a4da2e3e | 691 | symprefix, symprefix); |
658f29a5 JB |
692 | fprintf(f, "\t/* off_dt_strings */\n"); |
693 | ASM_EMIT_BELONG(f, "_%s_strings_start - _%s_blob_start", | |
a4da2e3e | 694 | symprefix, symprefix); |
658f29a5 JB |
695 | fprintf(f, "\t/* off_mem_rsvmap */\n"); |
696 | ASM_EMIT_BELONG(f, "_%s_reserve_map - _%s_blob_start", | |
a4da2e3e | 697 | symprefix, symprefix); |
658f29a5 JB |
698 | fprintf(f, "\t/* version */\n"); |
699 | asm_emit_cell(f, vi->version); | |
700 | fprintf(f, "\t/* last_comp_version */\n"); | |
701 | asm_emit_cell(f, vi->last_comp_version); | |
702 | ||
703 | if (vi->flags & FTF_BOOTCPUID) { | |
704 | fprintf(f, "\t/* boot_cpuid_phys */\n"); | |
705 | asm_emit_cell(f, bi->boot_cpuid_phys); | |
706 | } | |
a4da2e3e | 707 | |
658f29a5 JB |
708 | if (vi->flags & FTF_STRTABSIZE) { |
709 | fprintf(f, "\t/* size_dt_strings */\n"); | |
710 | ASM_EMIT_BELONG(f, "_%s_strings_end - _%s_strings_start", | |
711 | symprefix, symprefix); | |
712 | } | |
a4da2e3e | 713 | |
658f29a5 JB |
714 | if (vi->flags & FTF_STRUCTSIZE) { |
715 | fprintf(f, "\t/* size_dt_struct */\n"); | |
716 | ASM_EMIT_BELONG(f, "_%s_struct_end - _%s_struct_start", | |
a4da2e3e | 717 | symprefix, symprefix); |
658f29a5 | 718 | } |
a4da2e3e DG |
719 | |
720 | /* | |
721 | * Reserve map entries. | |
722 | * Align the reserve map to a doubleword boundary. | |
723 | * Each entry is an (address, size) pair of u64 values. | |
724 | * Always supply a zero-sized temination entry. | |
725 | */ | |
726 | asm_emit_align(f, 8); | |
727 | emit_label(f, symprefix, "reserve_map"); | |
728 | ||
729 | fprintf(f, "/* Memory reserve map from source file */\n"); | |
730 | ||
731 | /* | |
732 | * Use .long on high and low halfs of u64s to avoid .quad | |
733 | * as it appears .quad isn't available in some assemblers. | |
734 | */ | |
735 | for (re = bi->reservelist; re; re = re->next) { | |
658f29a5 JB |
736 | struct label *l; |
737 | ||
738 | for_each_label(re->labels, l) { | |
739 | fprintf(f, "\t.globl\t%s\n", l->label); | |
740 | fprintf(f, "%s:\n", l->label); | |
a4da2e3e | 741 | } |
658f29a5 JB |
742 | ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.address >> 32)); |
743 | ASM_EMIT_BELONG(f, "0x%08x", | |
744 | (unsigned int)(re->re.address & 0xffffffff)); | |
745 | ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.size >> 32)); | |
746 | ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.size & 0xffffffff)); | |
a4da2e3e DG |
747 | } |
748 | for (i = 0; i < reservenum; i++) { | |
749 | fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n"); | |
750 | } | |
751 | ||
752 | fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n"); | |
753 | ||
754 | emit_label(f, symprefix, "struct_start"); | |
755 | flatten_tree(bi->dt, &asm_emitter, f, &strbuf, vi); | |
658f29a5 JB |
756 | |
757 | fprintf(f, "\t/* FDT_END */\n"); | |
758 | asm_emit_cell(f, FDT_END); | |
a4da2e3e DG |
759 | emit_label(f, symprefix, "struct_end"); |
760 | ||
761 | emit_label(f, symprefix, "strings_start"); | |
762 | dump_stringtable_asm(f, strbuf); | |
763 | emit_label(f, symprefix, "strings_end"); | |
764 | ||
765 | emit_label(f, symprefix, "blob_end"); | |
766 | ||
767 | /* | |
768 | * If the user asked for more space than is used, pad it out. | |
769 | */ | |
770 | if (minsize > 0) { | |
771 | fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n", | |
772 | minsize, symprefix, symprefix); | |
773 | } | |
774 | if (padsize > 0) { | |
775 | fprintf(f, "\t.space\t%d, 0\n", padsize); | |
776 | } | |
777 | emit_label(f, symprefix, "blob_abs_end"); | |
778 | ||
779 | data_free(strbuf); | |
780 | } | |
781 | ||
782 | struct inbuf { | |
783 | char *base, *limit, *ptr; | |
784 | }; | |
785 | ||
786 | static void inbuf_init(struct inbuf *inb, void *base, void *limit) | |
787 | { | |
788 | inb->base = base; | |
789 | inb->limit = limit; | |
790 | inb->ptr = inb->base; | |
791 | } | |
792 | ||
793 | static void flat_read_chunk(struct inbuf *inb, void *p, int len) | |
794 | { | |
795 | if ((inb->ptr + len) > inb->limit) | |
796 | die("Premature end of data parsing flat device tree\n"); | |
797 | ||
798 | memcpy(p, inb->ptr, len); | |
799 | ||
800 | inb->ptr += len; | |
801 | } | |
802 | ||
ed95d745 | 803 | static uint32_t flat_read_word(struct inbuf *inb) |
a4da2e3e | 804 | { |
ed95d745 | 805 | uint32_t val; |
a4da2e3e DG |
806 | |
807 | assert(((inb->ptr - inb->base) % sizeof(val)) == 0); | |
808 | ||
809 | flat_read_chunk(inb, &val, sizeof(val)); | |
810 | ||
ed95d745 | 811 | return fdt32_to_cpu(val); |
a4da2e3e DG |
812 | } |
813 | ||
814 | static void flat_realign(struct inbuf *inb, int align) | |
815 | { | |
816 | int off = inb->ptr - inb->base; | |
817 | ||
818 | inb->ptr = inb->base + ALIGN(off, align); | |
819 | if (inb->ptr > inb->limit) | |
820 | die("Premature end of data parsing flat device tree\n"); | |
821 | } | |
822 | ||
823 | static char *flat_read_string(struct inbuf *inb) | |
824 | { | |
825 | int len = 0; | |
826 | const char *p = inb->ptr; | |
827 | char *str; | |
828 | ||
829 | do { | |
830 | if (p >= inb->limit) | |
831 | die("Premature end of data parsing flat device tree\n"); | |
832 | len++; | |
833 | } while ((*p++) != '\0'); | |
834 | ||
658f29a5 | 835 | str = xstrdup(inb->ptr); |
a4da2e3e DG |
836 | |
837 | inb->ptr += len; | |
838 | ||
ed95d745 | 839 | flat_realign(inb, sizeof(uint32_t)); |
a4da2e3e DG |
840 | |
841 | return str; | |
842 | } | |
843 | ||
844 | static struct data flat_read_data(struct inbuf *inb, int len) | |
845 | { | |
846 | struct data d = empty_data; | |
847 | ||
848 | if (len == 0) | |
849 | return empty_data; | |
850 | ||
851 | d = data_grow_for(d, len); | |
852 | d.len = len; | |
853 | ||
854 | flat_read_chunk(inb, d.val, len); | |
855 | ||
ed95d745 | 856 | flat_realign(inb, sizeof(uint32_t)); |
a4da2e3e DG |
857 | |
858 | return d; | |
859 | } | |
860 | ||
861 | static char *flat_read_stringtable(struct inbuf *inb, int offset) | |
862 | { | |
863 | const char *p; | |
864 | ||
865 | p = inb->base + offset; | |
866 | while (1) { | |
867 | if (p >= inb->limit || p < inb->base) | |
868 | die("String offset %d overruns string table\n", | |
869 | offset); | |
870 | ||
871 | if (*p == '\0') | |
872 | break; | |
873 | ||
874 | p++; | |
875 | } | |
876 | ||
658f29a5 | 877 | return xstrdup(inb->base + offset); |
a4da2e3e DG |
878 | } |
879 | ||
880 | static struct property *flat_read_property(struct inbuf *dtbuf, | |
881 | struct inbuf *strbuf, int flags) | |
882 | { | |
ed95d745 | 883 | uint32_t proplen, stroff; |
a4da2e3e DG |
884 | char *name; |
885 | struct data val; | |
886 | ||
887 | proplen = flat_read_word(dtbuf); | |
888 | stroff = flat_read_word(dtbuf); | |
889 | ||
890 | name = flat_read_stringtable(strbuf, stroff); | |
891 | ||
892 | if ((flags & FTF_VARALIGN) && (proplen >= 8)) | |
893 | flat_realign(dtbuf, 8); | |
894 | ||
895 | val = flat_read_data(dtbuf, proplen); | |
896 | ||
658f29a5 | 897 | return build_property(name, val); |
a4da2e3e DG |
898 | } |
899 | ||
900 | ||
901 | static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb) | |
902 | { | |
903 | struct reserve_info *reservelist = NULL; | |
904 | struct reserve_info *new; | |
a4da2e3e DG |
905 | struct fdt_reserve_entry re; |
906 | ||
907 | /* | |
908 | * Each entry is a pair of u64 (addr, size) values for 4 cell_t's. | |
909 | * List terminates at an entry with size equal to zero. | |
910 | * | |
911 | * First pass, count entries. | |
912 | */ | |
a4da2e3e DG |
913 | while (1) { |
914 | flat_read_chunk(inb, &re, sizeof(re)); | |
ed95d745 DG |
915 | re.address = fdt64_to_cpu(re.address); |
916 | re.size = fdt64_to_cpu(re.size); | |
a4da2e3e DG |
917 | if (re.size == 0) |
918 | break; | |
919 | ||
658f29a5 | 920 | new = build_reserve_entry(re.address, re.size); |
a4da2e3e DG |
921 | reservelist = add_reserve_entry(reservelist, new); |
922 | } | |
923 | ||
924 | return reservelist; | |
925 | } | |
926 | ||
927 | ||
928 | static char *nodename_from_path(const char *ppath, const char *cpath) | |
929 | { | |
a4da2e3e DG |
930 | int plen; |
931 | ||
ed95d745 | 932 | plen = strlen(ppath); |
a4da2e3e | 933 | |
ed95d745 DG |
934 | if (!strneq(ppath, cpath, plen)) |
935 | die("Path \"%s\" is not valid as a child of \"%s\"\n", | |
936 | cpath, ppath); | |
a4da2e3e | 937 | |
ed95d745 DG |
938 | /* root node is a special case */ |
939 | if (!streq(ppath, "/")) | |
940 | plen++; | |
a4da2e3e | 941 | |
658f29a5 | 942 | return xstrdup(cpath + plen); |
a4da2e3e DG |
943 | } |
944 | ||
945 | static struct node *unflatten_tree(struct inbuf *dtbuf, | |
946 | struct inbuf *strbuf, | |
ed95d745 | 947 | const char *parent_flatname, int flags) |
a4da2e3e DG |
948 | { |
949 | struct node *node; | |
ed95d745 DG |
950 | char *flatname; |
951 | uint32_t val; | |
a4da2e3e DG |
952 | |
953 | node = build_node(NULL, NULL); | |
954 | ||
ed95d745 | 955 | flatname = flat_read_string(dtbuf); |
a4da2e3e | 956 | |
ed95d745 DG |
957 | if (flags & FTF_FULLPATH) |
958 | node->name = nodename_from_path(parent_flatname, flatname); | |
959 | else | |
960 | node->name = flatname; | |
a4da2e3e DG |
961 | |
962 | do { | |
963 | struct property *prop; | |
964 | struct node *child; | |
965 | ||
966 | val = flat_read_word(dtbuf); | |
967 | switch (val) { | |
968 | case FDT_PROP: | |
969 | if (node->children) | |
970 | fprintf(stderr, "Warning: Flat tree input has " | |
971 | "subnodes preceding a property.\n"); | |
972 | prop = flat_read_property(dtbuf, strbuf, flags); | |
973 | add_property(node, prop); | |
974 | break; | |
975 | ||
976 | case FDT_BEGIN_NODE: | |
ed95d745 | 977 | child = unflatten_tree(dtbuf,strbuf, flatname, flags); |
a4da2e3e DG |
978 | add_child(node, child); |
979 | break; | |
980 | ||
981 | case FDT_END_NODE: | |
982 | break; | |
983 | ||
984 | case FDT_END: | |
985 | die("Premature FDT_END in device tree blob\n"); | |
986 | break; | |
987 | ||
988 | case FDT_NOP: | |
989 | if (!(flags & FTF_NOPS)) | |
990 | fprintf(stderr, "Warning: NOP tag found in flat tree" | |
991 | " version <16\n"); | |
992 | ||
993 | /* Ignore */ | |
994 | break; | |
995 | ||
996 | default: | |
997 | die("Invalid opcode word %08x in device tree blob\n", | |
998 | val); | |
999 | } | |
1000 | } while (val != FDT_END_NODE); | |
1001 | ||
1002 | return node; | |
1003 | } | |
1004 | ||
1005 | ||
ed95d745 | 1006 | struct boot_info *dt_from_blob(const char *fname) |
a4da2e3e | 1007 | { |
658f29a5 | 1008 | FILE *f; |
ed95d745 DG |
1009 | uint32_t magic, totalsize, version, size_dt, boot_cpuid_phys; |
1010 | uint32_t off_dt, off_str, off_mem_rsvmap; | |
a4da2e3e DG |
1011 | int rc; |
1012 | char *blob; | |
1013 | struct fdt_header *fdt; | |
1014 | char *p; | |
1015 | struct inbuf dtbuf, strbuf; | |
1016 | struct inbuf memresvbuf; | |
1017 | int sizeleft; | |
1018 | struct reserve_info *reservelist; | |
1019 | struct node *tree; | |
ed95d745 | 1020 | uint32_t val; |
a4da2e3e DG |
1021 | int flags = 0; |
1022 | ||
658f29a5 | 1023 | f = srcfile_relative_open(fname, NULL); |
ed95d745 | 1024 | |
658f29a5 JB |
1025 | rc = fread(&magic, sizeof(magic), 1, f); |
1026 | if (ferror(f)) | |
a4da2e3e DG |
1027 | die("Error reading DT blob magic number: %s\n", |
1028 | strerror(errno)); | |
1029 | if (rc < 1) { | |
658f29a5 | 1030 | if (feof(f)) |
a4da2e3e DG |
1031 | die("EOF reading DT blob magic number\n"); |
1032 | else | |
1033 | die("Mysterious short read reading magic number\n"); | |
1034 | } | |
1035 | ||
ed95d745 | 1036 | magic = fdt32_to_cpu(magic); |
a4da2e3e DG |
1037 | if (magic != FDT_MAGIC) |
1038 | die("Blob has incorrect magic number\n"); | |
1039 | ||
658f29a5 JB |
1040 | rc = fread(&totalsize, sizeof(totalsize), 1, f); |
1041 | if (ferror(f)) | |
a4da2e3e DG |
1042 | die("Error reading DT blob size: %s\n", strerror(errno)); |
1043 | if (rc < 1) { | |
658f29a5 | 1044 | if (feof(f)) |
a4da2e3e DG |
1045 | die("EOF reading DT blob size\n"); |
1046 | else | |
1047 | die("Mysterious short read reading blob size\n"); | |
1048 | } | |
1049 | ||
ed95d745 | 1050 | totalsize = fdt32_to_cpu(totalsize); |
a4da2e3e DG |
1051 | if (totalsize < FDT_V1_SIZE) |
1052 | die("DT blob size (%d) is too small\n", totalsize); | |
1053 | ||
1054 | blob = xmalloc(totalsize); | |
1055 | ||
1056 | fdt = (struct fdt_header *)blob; | |
ed95d745 DG |
1057 | fdt->magic = cpu_to_fdt32(magic); |
1058 | fdt->totalsize = cpu_to_fdt32(totalsize); | |
a4da2e3e DG |
1059 | |
1060 | sizeleft = totalsize - sizeof(magic) - sizeof(totalsize); | |
1061 | p = blob + sizeof(magic) + sizeof(totalsize); | |
1062 | ||
1063 | while (sizeleft) { | |
658f29a5 | 1064 | if (feof(f)) |
a4da2e3e DG |
1065 | die("EOF before reading %d bytes of DT blob\n", |
1066 | totalsize); | |
1067 | ||
658f29a5 JB |
1068 | rc = fread(p, 1, sizeleft, f); |
1069 | if (ferror(f)) | |
a4da2e3e DG |
1070 | die("Error reading DT blob: %s\n", |
1071 | strerror(errno)); | |
1072 | ||
1073 | sizeleft -= rc; | |
1074 | p += rc; | |
1075 | } | |
1076 | ||
ed95d745 DG |
1077 | off_dt = fdt32_to_cpu(fdt->off_dt_struct); |
1078 | off_str = fdt32_to_cpu(fdt->off_dt_strings); | |
1079 | off_mem_rsvmap = fdt32_to_cpu(fdt->off_mem_rsvmap); | |
1080 | version = fdt32_to_cpu(fdt->version); | |
1081 | boot_cpuid_phys = fdt32_to_cpu(fdt->boot_cpuid_phys); | |
a4da2e3e DG |
1082 | |
1083 | if (off_mem_rsvmap >= totalsize) | |
1084 | die("Mem Reserve structure offset exceeds total size\n"); | |
1085 | ||
1086 | if (off_dt >= totalsize) | |
1087 | die("DT structure offset exceeds total size\n"); | |
1088 | ||
1089 | if (off_str > totalsize) | |
1090 | die("String table offset exceeds total size\n"); | |
1091 | ||
a4da2e3e | 1092 | if (version >= 3) { |
ed95d745 | 1093 | uint32_t size_str = fdt32_to_cpu(fdt->size_dt_strings); |
b9937347 | 1094 | if ((off_str+size_str < off_str) || (off_str+size_str > totalsize)) |
a4da2e3e | 1095 | die("String table extends past total size\n"); |
ed95d745 DG |
1096 | inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str); |
1097 | } else { | |
1098 | inbuf_init(&strbuf, blob + off_str, blob + totalsize); | |
a4da2e3e DG |
1099 | } |
1100 | ||
1101 | if (version >= 17) { | |
ed95d745 | 1102 | size_dt = fdt32_to_cpu(fdt->size_dt_struct); |
b9937347 | 1103 | if ((off_dt+size_dt < off_dt) || (off_dt+size_dt > totalsize)) |
a4da2e3e DG |
1104 | die("Structure block extends past total size\n"); |
1105 | } | |
1106 | ||
1107 | if (version < 16) { | |
1108 | flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN; | |
1109 | } else { | |
1110 | flags |= FTF_NOPS; | |
1111 | } | |
1112 | ||
1113 | inbuf_init(&memresvbuf, | |
1114 | blob + off_mem_rsvmap, blob + totalsize); | |
1115 | inbuf_init(&dtbuf, blob + off_dt, blob + totalsize); | |
a4da2e3e DG |
1116 | |
1117 | reservelist = flat_read_mem_reserve(&memresvbuf); | |
1118 | ||
1119 | val = flat_read_word(&dtbuf); | |
1120 | ||
1121 | if (val != FDT_BEGIN_NODE) | |
1122 | die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val); | |
1123 | ||
1124 | tree = unflatten_tree(&dtbuf, &strbuf, "", flags); | |
1125 | ||
1126 | val = flat_read_word(&dtbuf); | |
1127 | if (val != FDT_END) | |
1128 | die("Device tree blob doesn't end with FDT_END\n"); | |
1129 | ||
1130 | free(blob); | |
1131 | ||
658f29a5 | 1132 | fclose(f); |
ed95d745 DG |
1133 | |
1134 | return build_boot_info(reservelist, tree, boot_cpuid_phys); | |
a4da2e3e | 1135 | } |