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