flattree.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 dt_info *dti, 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(dti->dt, &bin_emitter, &dtbuf, &strbuf, vi);
  315. bin_emit_cell(&dtbuf, FDT_END);
  316. reservebuf = flatten_reserve_list(dti->reservelist, vi);
  317. /* Make header */
  318. make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len,
  319. dti->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) {
  326. padlen = 0;
  327. if (quiet < 1)
  328. fprintf(stderr,
  329. "Warning: blob size %d >= minimum size %d\n",
  330. fdt32_to_cpu(fdt.totalsize), minsize);
  331. }
  332. }
  333. if (padsize > 0)
  334. padlen = padsize;
  335. if (alignsize > 0)
  336. padlen = ALIGN(fdt32_to_cpu(fdt.totalsize) + padlen, alignsize)
  337. - fdt32_to_cpu(fdt.totalsize);
  338. if (padlen > 0) {
  339. int tsize = fdt32_to_cpu(fdt.totalsize);
  340. tsize += padlen;
  341. fdt.totalsize = cpu_to_fdt32(tsize);
  342. }
  343. /*
  344. * Assemble the blob: start with the header, add with alignment
  345. * the reserve buffer, add the reserve map terminating zeroes,
  346. * the device tree itself, and finally the strings.
  347. */
  348. blob = data_append_data(blob, &fdt, vi->hdr_size);
  349. blob = data_append_align(blob, 8);
  350. blob = data_merge(blob, reservebuf);
  351. blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry));
  352. blob = data_merge(blob, dtbuf);
  353. blob = data_merge(blob, strbuf);
  354. /*
  355. * If the user asked for more space than is used, pad out the blob.
  356. */
  357. if (padlen > 0)
  358. blob = data_append_zeroes(blob, padlen);
  359. if (fwrite(blob.val, blob.len, 1, f) != 1) {
  360. if (ferror(f))
  361. die("Error writing device tree blob: %s\n",
  362. strerror(errno));
  363. else
  364. die("Short write on device tree blob\n");
  365. }
  366. /*
  367. * data_merge() frees the right-hand element so only the blob
  368. * remains to be freed.
  369. */
  370. data_free(blob);
  371. }
  372. static void dump_stringtable_asm(FILE *f, struct data strbuf)
  373. {
  374. const char *p;
  375. int len;
  376. p = strbuf.val;
  377. while (p < (strbuf.val + strbuf.len)) {
  378. len = strlen(p);
  379. fprintf(f, "\t.string \"%s\"\n", p);
  380. p += len+1;
  381. }
  382. }
  383. void dt_to_asm(FILE *f, struct dt_info *dti, int version)
  384. {
  385. struct version_info *vi = NULL;
  386. int i;
  387. struct data strbuf = empty_data;
  388. struct reserve_info *re;
  389. const char *symprefix = "dt";
  390. for (i = 0; i < ARRAY_SIZE(version_table); i++) {
  391. if (version_table[i].version == version)
  392. vi = &version_table[i];
  393. }
  394. if (!vi)
  395. die("Unknown device tree blob version %d\n", version);
  396. fprintf(f, "/* autogenerated by dtc, do not edit */\n\n");
  397. emit_label(f, symprefix, "blob_start");
  398. emit_label(f, symprefix, "header");
  399. fprintf(f, "\t/* magic */\n");
  400. asm_emit_cell(f, FDT_MAGIC);
  401. fprintf(f, "\t/* totalsize */\n");
  402. ASM_EMIT_BELONG(f, "_%s_blob_abs_end - _%s_blob_start",
  403. symprefix, symprefix);
  404. fprintf(f, "\t/* off_dt_struct */\n");
  405. ASM_EMIT_BELONG(f, "_%s_struct_start - _%s_blob_start",
  406. symprefix, symprefix);
  407. fprintf(f, "\t/* off_dt_strings */\n");
  408. ASM_EMIT_BELONG(f, "_%s_strings_start - _%s_blob_start",
  409. symprefix, symprefix);
  410. fprintf(f, "\t/* off_mem_rsvmap */\n");
  411. ASM_EMIT_BELONG(f, "_%s_reserve_map - _%s_blob_start",
  412. symprefix, symprefix);
  413. fprintf(f, "\t/* version */\n");
  414. asm_emit_cell(f, vi->version);
  415. fprintf(f, "\t/* last_comp_version */\n");
  416. asm_emit_cell(f, vi->last_comp_version);
  417. if (vi->flags & FTF_BOOTCPUID) {
  418. fprintf(f, "\t/* boot_cpuid_phys */\n");
  419. asm_emit_cell(f, dti->boot_cpuid_phys);
  420. }
  421. if (vi->flags & FTF_STRTABSIZE) {
  422. fprintf(f, "\t/* size_dt_strings */\n");
  423. ASM_EMIT_BELONG(f, "_%s_strings_end - _%s_strings_start",
  424. symprefix, symprefix);
  425. }
  426. if (vi->flags & FTF_STRUCTSIZE) {
  427. fprintf(f, "\t/* size_dt_struct */\n");
  428. ASM_EMIT_BELONG(f, "_%s_struct_end - _%s_struct_start",
  429. symprefix, symprefix);
  430. }
  431. /*
  432. * Reserve map entries.
  433. * Align the reserve map to a doubleword boundary.
  434. * Each entry is an (address, size) pair of u64 values.
  435. * Always supply a zero-sized temination entry.
  436. */
  437. asm_emit_align(f, 8);
  438. emit_label(f, symprefix, "reserve_map");
  439. fprintf(f, "/* Memory reserve map from source file */\n");
  440. /*
  441. * Use .long on high and low halfs of u64s to avoid .quad
  442. * as it appears .quad isn't available in some assemblers.
  443. */
  444. for (re = dti->reservelist; re; re = re->next) {
  445. struct label *l;
  446. for_each_label(re->labels, l) {
  447. fprintf(f, "\t.globl\t%s\n", l->label);
  448. fprintf(f, "%s:\n", l->label);
  449. }
  450. ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.address >> 32));
  451. ASM_EMIT_BELONG(f, "0x%08x",
  452. (unsigned int)(re->re.address & 0xffffffff));
  453. ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.size >> 32));
  454. ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.size & 0xffffffff));
  455. }
  456. for (i = 0; i < reservenum; i++) {
  457. fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
  458. }
  459. fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
  460. emit_label(f, symprefix, "struct_start");
  461. flatten_tree(dti->dt, &asm_emitter, f, &strbuf, vi);
  462. fprintf(f, "\t/* FDT_END */\n");
  463. asm_emit_cell(f, FDT_END);
  464. emit_label(f, symprefix, "struct_end");
  465. emit_label(f, symprefix, "strings_start");
  466. dump_stringtable_asm(f, strbuf);
  467. emit_label(f, symprefix, "strings_end");
  468. emit_label(f, symprefix, "blob_end");
  469. /*
  470. * If the user asked for more space than is used, pad it out.
  471. */
  472. if (minsize > 0) {
  473. fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n",
  474. minsize, symprefix, symprefix);
  475. }
  476. if (padsize > 0) {
  477. fprintf(f, "\t.space\t%d, 0\n", padsize);
  478. }
  479. if (alignsize > 0)
  480. asm_emit_align(f, alignsize);
  481. emit_label(f, symprefix, "blob_abs_end");
  482. data_free(strbuf);
  483. }
  484. struct inbuf {
  485. char *base, *limit, *ptr;
  486. };
  487. static void inbuf_init(struct inbuf *inb, void *base, void *limit)
  488. {
  489. inb->base = base;
  490. inb->limit = limit;
  491. inb->ptr = inb->base;
  492. }
  493. static void flat_read_chunk(struct inbuf *inb, void *p, int len)
  494. {
  495. if ((inb->ptr + len) > inb->limit)
  496. die("Premature end of data parsing flat device tree\n");
  497. memcpy(p, inb->ptr, len);
  498. inb->ptr += len;
  499. }
  500. static uint32_t flat_read_word(struct inbuf *inb)
  501. {
  502. uint32_t val;
  503. assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
  504. flat_read_chunk(inb, &val, sizeof(val));
  505. return fdt32_to_cpu(val);
  506. }
  507. static void flat_realign(struct inbuf *inb, int align)
  508. {
  509. int off = inb->ptr - inb->base;
  510. inb->ptr = inb->base + ALIGN(off, align);
  511. if (inb->ptr > inb->limit)
  512. die("Premature end of data parsing flat device tree\n");
  513. }
  514. static char *flat_read_string(struct inbuf *inb)
  515. {
  516. int len = 0;
  517. const char *p = inb->ptr;
  518. char *str;
  519. do {
  520. if (p >= inb->limit)
  521. die("Premature end of data parsing flat device tree\n");
  522. len++;
  523. } while ((*p++) != '\0');
  524. str = xstrdup(inb->ptr);
  525. inb->ptr += len;
  526. flat_realign(inb, sizeof(uint32_t));
  527. return str;
  528. }
  529. static struct data flat_read_data(struct inbuf *inb, int len)
  530. {
  531. struct data d = empty_data;
  532. if (len == 0)
  533. return empty_data;
  534. d = data_grow_for(d, len);
  535. d.len = len;
  536. flat_read_chunk(inb, d.val, len);
  537. flat_realign(inb, sizeof(uint32_t));
  538. return d;
  539. }
  540. static char *flat_read_stringtable(struct inbuf *inb, int offset)
  541. {
  542. const char *p;
  543. p = inb->base + offset;
  544. while (1) {
  545. if (p >= inb->limit || p < inb->base)
  546. die("String offset %d overruns string table\n",
  547. offset);
  548. if (*p == '\0')
  549. break;
  550. p++;
  551. }
  552. return xstrdup(inb->base + offset);
  553. }
  554. static struct property *flat_read_property(struct inbuf *dtbuf,
  555. struct inbuf *strbuf, int flags)
  556. {
  557. uint32_t proplen, stroff;
  558. char *name;
  559. struct data val;
  560. proplen = flat_read_word(dtbuf);
  561. stroff = flat_read_word(dtbuf);
  562. name = flat_read_stringtable(strbuf, stroff);
  563. if ((flags & FTF_VARALIGN) && (proplen >= 8))
  564. flat_realign(dtbuf, 8);
  565. val = flat_read_data(dtbuf, proplen);
  566. return build_property(name, val);
  567. }
  568. static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
  569. {
  570. struct reserve_info *reservelist = NULL;
  571. struct reserve_info *new;
  572. struct fdt_reserve_entry re;
  573. /*
  574. * Each entry is a pair of u64 (addr, size) values for 4 cell_t's.
  575. * List terminates at an entry with size equal to zero.
  576. *
  577. * First pass, count entries.
  578. */
  579. while (1) {
  580. flat_read_chunk(inb, &re, sizeof(re));
  581. re.address = fdt64_to_cpu(re.address);
  582. re.size = fdt64_to_cpu(re.size);
  583. if (re.size == 0)
  584. break;
  585. new = build_reserve_entry(re.address, re.size);
  586. reservelist = add_reserve_entry(reservelist, new);
  587. }
  588. return reservelist;
  589. }
  590. static char *nodename_from_path(const char *ppath, const char *cpath)
  591. {
  592. int plen;
  593. plen = strlen(ppath);
  594. if (!strneq(ppath, cpath, plen))
  595. die("Path \"%s\" is not valid as a child of \"%s\"\n",
  596. cpath, ppath);
  597. /* root node is a special case */
  598. if (!streq(ppath, "/"))
  599. plen++;
  600. return xstrdup(cpath + plen);
  601. }
  602. static struct node *unflatten_tree(struct inbuf *dtbuf,
  603. struct inbuf *strbuf,
  604. const char *parent_flatname, int flags)
  605. {
  606. struct node *node;
  607. char *flatname;
  608. uint32_t val;
  609. node = build_node(NULL, NULL);
  610. flatname = flat_read_string(dtbuf);
  611. if (flags & FTF_FULLPATH)
  612. node->name = nodename_from_path(parent_flatname, flatname);
  613. else
  614. node->name = flatname;
  615. do {
  616. struct property *prop;
  617. struct node *child;
  618. val = flat_read_word(dtbuf);
  619. switch (val) {
  620. case FDT_PROP:
  621. if (node->children)
  622. fprintf(stderr, "Warning: Flat tree input has "
  623. "subnodes preceding a property.\n");
  624. prop = flat_read_property(dtbuf, strbuf, flags);
  625. add_property(node, prop);
  626. break;
  627. case FDT_BEGIN_NODE:
  628. child = unflatten_tree(dtbuf,strbuf, flatname, flags);
  629. add_child(node, child);
  630. break;
  631. case FDT_END_NODE:
  632. break;
  633. case FDT_END:
  634. die("Premature FDT_END in device tree blob\n");
  635. break;
  636. case FDT_NOP:
  637. if (!(flags & FTF_NOPS))
  638. fprintf(stderr, "Warning: NOP tag found in flat tree"
  639. " version <16\n");
  640. /* Ignore */
  641. break;
  642. default:
  643. die("Invalid opcode word %08x in device tree blob\n",
  644. val);
  645. }
  646. } while (val != FDT_END_NODE);
  647. if (node->name != flatname) {
  648. free(flatname);
  649. }
  650. return node;
  651. }
  652. struct dt_info *dt_from_blob(const char *fname)
  653. {
  654. FILE *f;
  655. uint32_t magic, totalsize, version, size_dt, boot_cpuid_phys;
  656. uint32_t off_dt, off_str, off_mem_rsvmap;
  657. int rc;
  658. char *blob;
  659. struct fdt_header *fdt;
  660. char *p;
  661. struct inbuf dtbuf, strbuf;
  662. struct inbuf memresvbuf;
  663. int sizeleft;
  664. struct reserve_info *reservelist;
  665. struct node *tree;
  666. uint32_t val;
  667. int flags = 0;
  668. f = srcfile_relative_open(fname, NULL);
  669. rc = fread(&magic, sizeof(magic), 1, f);
  670. if (ferror(f))
  671. die("Error reading DT blob magic number: %s\n",
  672. strerror(errno));
  673. if (rc < 1) {
  674. if (feof(f))
  675. die("EOF reading DT blob magic number\n");
  676. else
  677. die("Mysterious short read reading magic number\n");
  678. }
  679. magic = fdt32_to_cpu(magic);
  680. if (magic != FDT_MAGIC)
  681. die("Blob has incorrect magic number\n");
  682. rc = fread(&totalsize, sizeof(totalsize), 1, f);
  683. if (ferror(f))
  684. die("Error reading DT blob size: %s\n", strerror(errno));
  685. if (rc < 1) {
  686. if (feof(f))
  687. die("EOF reading DT blob size\n");
  688. else
  689. die("Mysterious short read reading blob size\n");
  690. }
  691. totalsize = fdt32_to_cpu(totalsize);
  692. if (totalsize < FDT_V1_SIZE)
  693. die("DT blob size (%d) is too small\n", totalsize);
  694. blob = xmalloc(totalsize);
  695. fdt = (struct fdt_header *)blob;
  696. fdt->magic = cpu_to_fdt32(magic);
  697. fdt->totalsize = cpu_to_fdt32(totalsize);
  698. sizeleft = totalsize - sizeof(magic) - sizeof(totalsize);
  699. p = blob + sizeof(magic) + sizeof(totalsize);
  700. while (sizeleft) {
  701. if (feof(f))
  702. die("EOF before reading %d bytes of DT blob\n",
  703. totalsize);
  704. rc = fread(p, 1, sizeleft, f);
  705. if (ferror(f))
  706. die("Error reading DT blob: %s\n",
  707. strerror(errno));
  708. sizeleft -= rc;
  709. p += rc;
  710. }
  711. off_dt = fdt32_to_cpu(fdt->off_dt_struct);
  712. off_str = fdt32_to_cpu(fdt->off_dt_strings);
  713. off_mem_rsvmap = fdt32_to_cpu(fdt->off_mem_rsvmap);
  714. version = fdt32_to_cpu(fdt->version);
  715. boot_cpuid_phys = fdt32_to_cpu(fdt->boot_cpuid_phys);
  716. if (off_mem_rsvmap >= totalsize)
  717. die("Mem Reserve structure offset exceeds total size\n");
  718. if (off_dt >= totalsize)
  719. die("DT structure offset exceeds total size\n");
  720. if (off_str > totalsize)
  721. die("String table offset exceeds total size\n");
  722. if (version >= 3) {
  723. uint32_t size_str = fdt32_to_cpu(fdt->size_dt_strings);
  724. if ((off_str+size_str < off_str) || (off_str+size_str > totalsize))
  725. die("String table extends past total size\n");
  726. inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str);
  727. } else {
  728. inbuf_init(&strbuf, blob + off_str, blob + totalsize);
  729. }
  730. if (version >= 17) {
  731. size_dt = fdt32_to_cpu(fdt->size_dt_struct);
  732. if ((off_dt+size_dt < off_dt) || (off_dt+size_dt > totalsize))
  733. die("Structure block extends past total size\n");
  734. }
  735. if (version < 16) {
  736. flags |= FTF_FULLPATH | FTF_NAMEPROPS | FTF_VARALIGN;
  737. } else {
  738. flags |= FTF_NOPS;
  739. }
  740. inbuf_init(&memresvbuf,
  741. blob + off_mem_rsvmap, blob + totalsize);
  742. inbuf_init(&dtbuf, blob + off_dt, blob + totalsize);
  743. reservelist = flat_read_mem_reserve(&memresvbuf);
  744. val = flat_read_word(&dtbuf);
  745. if (val != FDT_BEGIN_NODE)
  746. die("Device tree blob doesn't begin with FDT_BEGIN_NODE (begins with 0x%08x)\n", val);
  747. tree = unflatten_tree(&dtbuf, &strbuf, "", flags);
  748. val = flat_read_word(&dtbuf);
  749. if (val != FDT_END)
  750. die("Device tree blob doesn't end with FDT_END\n");
  751. free(blob);
  752. fclose(f);
  753. return build_dt_info(DTSF_V1, reservelist, tree, boot_cpuid_phys);
  754. }