flattree.c 22 KB

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