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. int seen_name_prop = 0;
  220. emit->beginnode(etarget, tree->labels);
  221. if (vi->flags & FTF_FULLPATH)
  222. emit->string(etarget, tree->fullpath, 0);
  223. else
  224. emit->string(etarget, tree->name, 0);
  225. emit->align(etarget, sizeof(cell_t));
  226. for_each_property(tree, prop) {
  227. int nameoff;
  228. if (streq(prop->name, "name"))
  229. seen_name_prop = 1;
  230. nameoff = stringtable_insert(strbuf, prop->name);
  231. emit->property(etarget, prop->labels);
  232. emit->cell(etarget, prop->val.len);
  233. emit->cell(etarget, nameoff);
  234. if ((vi->flags & FTF_VARALIGN) && (prop->val.len >= 8))
  235. emit->align(etarget, 8);
  236. emit->data(etarget, prop->val);
  237. emit->align(etarget, sizeof(cell_t));
  238. }
  239. if ((vi->flags & FTF_NAMEPROPS) && !seen_name_prop) {
  240. emit->property(etarget, NULL);
  241. emit->cell(etarget, tree->basenamelen+1);
  242. emit->cell(etarget, stringtable_insert(strbuf, "name"));
  243. if ((vi->flags & FTF_VARALIGN) && ((tree->basenamelen+1) >= 8))
  244. emit->align(etarget, 8);
  245. emit->string(etarget, tree->name, tree->basenamelen);
  246. emit->align(etarget, sizeof(cell_t));
  247. }
  248. for_each_child(tree, child) {
  249. flatten_tree(child, emit, etarget, strbuf, vi);
  250. }
  251. emit->endnode(etarget, tree->labels);
  252. }
  253. static struct data flatten_reserve_list(struct reserve_info *reservelist,
  254. struct version_info *vi)
  255. {
  256. struct reserve_info *re;
  257. struct data d = empty_data;
  258. static struct fdt_reserve_entry null_re = {0,0};
  259. int j;
  260. for (re = reservelist; re; re = re->next) {
  261. d = data_append_re(d, &re->re);
  262. }
  263. /*
  264. * Add additional reserved slots if the user asked for them.
  265. */
  266. for (j = 0; j < reservenum; j++) {
  267. d = data_append_re(d, &null_re);
  268. }
  269. return d;
  270. }
  271. static void make_fdt_header(struct fdt_header *fdt,
  272. struct version_info *vi,
  273. int reservesize, int dtsize, int strsize,
  274. int boot_cpuid_phys)
  275. {
  276. int reserve_off;
  277. reservesize += sizeof(struct fdt_reserve_entry);
  278. memset(fdt, 0xff, sizeof(*fdt));
  279. fdt->magic = cpu_to_fdt32(FDT_MAGIC);
  280. fdt->version = cpu_to_fdt32(vi->version);
  281. fdt->last_comp_version = cpu_to_fdt32(vi->last_comp_version);
  282. /* Reserve map should be doubleword aligned */
  283. reserve_off = ALIGN(vi->hdr_size, 8);
  284. fdt->off_mem_rsvmap = cpu_to_fdt32(reserve_off);
  285. fdt->off_dt_struct = cpu_to_fdt32(reserve_off + reservesize);
  286. fdt->off_dt_strings = cpu_to_fdt32(reserve_off + reservesize
  287. + dtsize);
  288. fdt->totalsize = cpu_to_fdt32(reserve_off + reservesize + dtsize + strsize);
  289. if (vi->flags & FTF_BOOTCPUID)
  290. fdt->boot_cpuid_phys = cpu_to_fdt32(boot_cpuid_phys);
  291. if (vi->flags & FTF_STRTABSIZE)
  292. fdt->size_dt_strings = cpu_to_fdt32(strsize);
  293. if (vi->flags & FTF_STRUCTSIZE)
  294. fdt->size_dt_struct = cpu_to_fdt32(dtsize);
  295. }
  296. void dt_to_blob(FILE *f, struct boot_info *bi, int version)
  297. {
  298. struct version_info *vi = NULL;
  299. int i;
  300. struct data blob = empty_data;
  301. struct data reservebuf = empty_data;
  302. struct data dtbuf = empty_data;
  303. struct data strbuf = empty_data;
  304. struct fdt_header fdt;
  305. int padlen = 0;
  306. for (i = 0; i < ARRAY_SIZE(version_table); i++) {
  307. if (version_table[i].version == version)
  308. vi = &version_table[i];
  309. }
  310. if (!vi)
  311. die("Unknown device tree blob version %d\n", version);
  312. flatten_tree(bi->dt, &bin_emitter, &dtbuf, &strbuf, vi);
  313. bin_emit_cell(&dtbuf, FDT_END);
  314. reservebuf = flatten_reserve_list(bi->reservelist, vi);
  315. /* Make header */
  316. make_fdt_header(&fdt, vi, reservebuf.len, dtbuf.len, strbuf.len,
  317. bi->boot_cpuid_phys);
  318. /*
  319. * If the user asked for more space than is used, adjust the totalsize.
  320. */
  321. if (minsize > 0) {
  322. padlen = minsize - fdt32_to_cpu(fdt.totalsize);
  323. if ((padlen < 0) && (quiet < 1))
  324. fprintf(stderr,
  325. "Warning: blob size %d >= minimum size %d\n",
  326. fdt32_to_cpu(fdt.totalsize), minsize);
  327. }
  328. if (padsize > 0)
  329. padlen = padsize;
  330. if (padlen > 0) {
  331. int tsize = fdt32_to_cpu(fdt.totalsize);
  332. tsize += padlen;
  333. fdt.totalsize = cpu_to_fdt32(tsize);
  334. }
  335. /*
  336. * Assemble the blob: start with the header, add with alignment
  337. * the reserve buffer, add the reserve map terminating zeroes,
  338. * the device tree itself, and finally the strings.
  339. */
  340. blob = data_append_data(blob, &fdt, vi->hdr_size);
  341. blob = data_append_align(blob, 8);
  342. blob = data_merge(blob, reservebuf);
  343. blob = data_append_zeroes(blob, sizeof(struct fdt_reserve_entry));
  344. blob = data_merge(blob, dtbuf);
  345. blob = data_merge(blob, strbuf);
  346. /*
  347. * If the user asked for more space than is used, pad out the blob.
  348. */
  349. if (padlen > 0)
  350. blob = data_append_zeroes(blob, padlen);
  351. if (fwrite(blob.val, blob.len, 1, f) != 1) {
  352. if (ferror(f))
  353. die("Error writing device tree blob: %s\n",
  354. strerror(errno));
  355. else
  356. die("Short write on device tree blob\n");
  357. }
  358. /*
  359. * data_merge() frees the right-hand element so only the blob
  360. * remains to be freed.
  361. */
  362. data_free(blob);
  363. }
  364. static void dump_stringtable_asm(FILE *f, struct data strbuf)
  365. {
  366. const char *p;
  367. int len;
  368. p = strbuf.val;
  369. while (p < (strbuf.val + strbuf.len)) {
  370. len = strlen(p);
  371. fprintf(f, "\t.string \"%s\"\n", p);
  372. p += len+1;
  373. }
  374. }
  375. void dt_to_asm(FILE *f, struct boot_info *bi, int version)
  376. {
  377. struct version_info *vi = NULL;
  378. int i;
  379. struct data strbuf = empty_data;
  380. struct reserve_info *re;
  381. const char *symprefix = "dt";
  382. for (i = 0; i < ARRAY_SIZE(version_table); i++) {
  383. if (version_table[i].version == version)
  384. vi = &version_table[i];
  385. }
  386. if (!vi)
  387. die("Unknown device tree blob version %d\n", version);
  388. fprintf(f, "/* autogenerated by dtc, do not edit */\n\n");
  389. emit_label(f, symprefix, "blob_start");
  390. emit_label(f, symprefix, "header");
  391. fprintf(f, "\t/* magic */\n");
  392. asm_emit_cell(f, FDT_MAGIC);
  393. fprintf(f, "\t/* totalsize */\n");
  394. ASM_EMIT_BELONG(f, "_%s_blob_abs_end - _%s_blob_start",
  395. symprefix, symprefix);
  396. fprintf(f, "\t/* off_dt_struct */\n");
  397. ASM_EMIT_BELONG(f, "_%s_struct_start - _%s_blob_start",
  398. symprefix, symprefix);
  399. fprintf(f, "\t/* off_dt_strings */\n");
  400. ASM_EMIT_BELONG(f, "_%s_strings_start - _%s_blob_start",
  401. symprefix, symprefix);
  402. fprintf(f, "\t/* off_mem_rsvmap */\n");
  403. ASM_EMIT_BELONG(f, "_%s_reserve_map - _%s_blob_start",
  404. symprefix, symprefix);
  405. fprintf(f, "\t/* version */\n");
  406. asm_emit_cell(f, vi->version);
  407. fprintf(f, "\t/* last_comp_version */\n");
  408. asm_emit_cell(f, vi->last_comp_version);
  409. if (vi->flags & FTF_BOOTCPUID) {
  410. fprintf(f, "\t/* boot_cpuid_phys */\n");
  411. asm_emit_cell(f, bi->boot_cpuid_phys);
  412. }
  413. if (vi->flags & FTF_STRTABSIZE) {
  414. fprintf(f, "\t/* size_dt_strings */\n");
  415. ASM_EMIT_BELONG(f, "_%s_strings_end - _%s_strings_start",
  416. symprefix, symprefix);
  417. }
  418. if (vi->flags & FTF_STRUCTSIZE) {
  419. fprintf(f, "\t/* size_dt_struct */\n");
  420. ASM_EMIT_BELONG(f, "_%s_struct_end - _%s_struct_start",
  421. symprefix, symprefix);
  422. }
  423. /*
  424. * Reserve map entries.
  425. * Align the reserve map to a doubleword boundary.
  426. * Each entry is an (address, size) pair of u64 values.
  427. * Always supply a zero-sized temination entry.
  428. */
  429. asm_emit_align(f, 8);
  430. emit_label(f, symprefix, "reserve_map");
  431. fprintf(f, "/* Memory reserve map from source file */\n");
  432. /*
  433. * Use .long on high and low halfs of u64s to avoid .quad
  434. * as it appears .quad isn't available in some assemblers.
  435. */
  436. for (re = bi->reservelist; re; re = re->next) {
  437. struct label *l;
  438. for_each_label(re->labels, l) {
  439. fprintf(f, "\t.globl\t%s\n", l->label);
  440. fprintf(f, "%s:\n", l->label);
  441. }
  442. ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.address >> 32));
  443. ASM_EMIT_BELONG(f, "0x%08x",
  444. (unsigned int)(re->re.address & 0xffffffff));
  445. ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.size >> 32));
  446. ASM_EMIT_BELONG(f, "0x%08x", (unsigned int)(re->re.size & 0xffffffff));
  447. }
  448. for (i = 0; i < reservenum; i++) {
  449. fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
  450. }
  451. fprintf(f, "\t.long\t0, 0\n\t.long\t0, 0\n");
  452. emit_label(f, symprefix, "struct_start");
  453. flatten_tree(bi->dt, &asm_emitter, f, &strbuf, vi);
  454. fprintf(f, "\t/* FDT_END */\n");
  455. asm_emit_cell(f, FDT_END);
  456. emit_label(f, symprefix, "struct_end");
  457. emit_label(f, symprefix, "strings_start");
  458. dump_stringtable_asm(f, strbuf);
  459. emit_label(f, symprefix, "strings_end");
  460. emit_label(f, symprefix, "blob_end");
  461. /*
  462. * If the user asked for more space than is used, pad it out.
  463. */
  464. if (minsize > 0) {
  465. fprintf(f, "\t.space\t%d - (_%s_blob_end - _%s_blob_start), 0\n",
  466. minsize, symprefix, symprefix);
  467. }
  468. if (padsize > 0) {
  469. fprintf(f, "\t.space\t%d, 0\n", padsize);
  470. }
  471. emit_label(f, symprefix, "blob_abs_end");
  472. data_free(strbuf);
  473. }
  474. struct inbuf {
  475. char *base, *limit, *ptr;
  476. };
  477. static void inbuf_init(struct inbuf *inb, void *base, void *limit)
  478. {
  479. inb->base = base;
  480. inb->limit = limit;
  481. inb->ptr = inb->base;
  482. }
  483. static void flat_read_chunk(struct inbuf *inb, void *p, int len)
  484. {
  485. if ((inb->ptr + len) > inb->limit)
  486. die("Premature end of data parsing flat device tree\n");
  487. memcpy(p, inb->ptr, len);
  488. inb->ptr += len;
  489. }
  490. static uint32_t flat_read_word(struct inbuf *inb)
  491. {
  492. uint32_t val;
  493. assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
  494. flat_read_chunk(inb, &val, sizeof(val));
  495. return fdt32_to_cpu(val);
  496. }
  497. static void flat_realign(struct inbuf *inb, int align)
  498. {
  499. int off = inb->ptr - inb->base;
  500. inb->ptr = inb->base + ALIGN(off, align);
  501. if (inb->ptr > inb->limit)
  502. die("Premature end of data parsing flat device tree\n");
  503. }
  504. static char *flat_read_string(struct inbuf *inb)
  505. {
  506. int len = 0;
  507. const char *p = inb->ptr;
  508. char *str;
  509. do {
  510. if (p >= inb->limit)
  511. die("Premature end of data parsing flat device tree\n");
  512. len++;
  513. } while ((*p++) != '\0');
  514. str = xstrdup(inb->ptr);
  515. inb->ptr += len;
  516. flat_realign(inb, sizeof(uint32_t));
  517. return str;
  518. }
  519. static struct data flat_read_data(struct inbuf *inb, int len)
  520. {
  521. struct data d = empty_data;
  522. if (len == 0)
  523. return empty_data;
  524. d = data_grow_for(d, len);
  525. d.len = len;
  526. flat_read_chunk(inb, d.val, len);
  527. flat_realign(inb, sizeof(uint32_t));
  528. return d;
  529. }
  530. static char *flat_read_stringtable(struct inbuf *inb, int offset)
  531. {
  532. const char *p;
  533. p = inb->base + offset;
  534. while (1) {
  535. if (p >= inb->limit || p < inb->base)
  536. die("String offset %d overruns string table\n",
  537. offset);
  538. if (*p == '\0')
  539. break;
  540. p++;
  541. }
  542. return xstrdup(inb->base + offset);
  543. }
  544. static struct property *flat_read_property(struct inbuf *dtbuf,
  545. struct inbuf *strbuf, int flags)
  546. {
  547. uint32_t proplen, stroff;
  548. char *name;
  549. struct data val;
  550. proplen = flat_read_word(dtbuf);
  551. stroff = flat_read_word(dtbuf);
  552. name = flat_read_stringtable(strbuf, stroff);
  553. if ((flags & FTF_VARALIGN) && (proplen >= 8))
  554. flat_realign(dtbuf, 8);
  555. val = flat_read_data(dtbuf, proplen);
  556. return build_property(name, val);
  557. }
  558. static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
  559. {
  560. struct reserve_info *reservelist = NULL;
  561. struct reserve_info *new;
  562. const char *p;
  563. struct fdt_reserve_entry re;
  564. /*
  565. * Each entry is a pair of u64 (addr, size) values for 4 cell_t's.
  566. * List terminates at an entry with size equal to zero.
  567. *
  568. * First pass, count entries.
  569. */
  570. p = inb->ptr;
  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. }