flattree.c 21 KB

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