flattree.c 22 KB

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