flattree.c 23 KB

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