ft_build.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * OF flat tree builder
  3. * Written by: Pantelis Antoniou <pantelis.antoniou@gmail.com>
  4. * Updated by: Matthew McClintock <msm@freescale.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <malloc.h>
  23. #include <environment.h>
  24. #ifdef CONFIG_OF_FLAT_TREE
  25. #include <asm/errno.h>
  26. #include <stddef.h>
  27. #include <ft_build.h>
  28. #undef DEBUG
  29. /* align addr on a size boundary - adjust address up if needed -- Cort */
  30. #define _ALIGN(addr,size) (((addr)+(size)-1)&(~((size)-1)))
  31. #ifndef CONFIG_OF_BOOT_CPU
  32. #define CONFIG_OF_BOOT_CPU 0
  33. #endif
  34. #define SIZE_OF_RSVMAP_ENTRY (2*sizeof(u64))
  35. static void ft_put_word(struct ft_cxt *cxt, u32 v)
  36. {
  37. memmove(cxt->p + sizeof(u32), cxt->p, cxt->p_end - cxt->p);
  38. *(u32 *) cxt->p = cpu_to_be32(v);
  39. cxt->p += sizeof(u32);
  40. cxt->p_end += sizeof(u32);
  41. }
  42. static inline void ft_put_bin(struct ft_cxt *cxt, const void *data, int sz)
  43. {
  44. int aligned_size = ((u8 *)_ALIGN((unsigned long)cxt->p + sz,
  45. sizeof(u32))) - cxt->p;
  46. memmove(cxt->p + aligned_size, cxt->p, cxt->p_end - cxt->p);
  47. /* make sure the last bytes are zeroed */
  48. memset(cxt->p + aligned_size - (aligned_size % sizeof(u32)), 0,
  49. (aligned_size % sizeof(u32)));
  50. memcpy(cxt->p, data, sz);
  51. cxt->p += aligned_size;
  52. cxt->p_end += aligned_size;
  53. }
  54. void ft_begin_node(struct ft_cxt *cxt, const char *name)
  55. {
  56. ft_put_word(cxt, OF_DT_BEGIN_NODE);
  57. ft_put_bin(cxt, name, strlen(name) + 1);
  58. }
  59. void ft_end_node(struct ft_cxt *cxt)
  60. {
  61. ft_put_word(cxt, OF_DT_END_NODE);
  62. }
  63. void ft_nop(struct ft_cxt *cxt)
  64. {
  65. ft_put_word(cxt, OF_DT_NOP);
  66. }
  67. static int lookup_string(struct ft_cxt *cxt, const char *name)
  68. {
  69. u8 *p;
  70. p = cxt->p;
  71. while (p < cxt->p_end) {
  72. if (strcmp(p, name) == 0)
  73. return p - cxt->p;
  74. p += strlen(p) + 1;
  75. }
  76. return -1;
  77. }
  78. void ft_prop(struct ft_cxt *cxt, const char *name, const void *data, int sz)
  79. {
  80. int off = 0;
  81. off = lookup_string(cxt, name);
  82. if (off == -1) {
  83. memcpy(cxt->p_end, name, strlen(name) + 1);
  84. off = cxt->p_end - cxt->p;
  85. cxt->p_end += strlen(name) + 1;
  86. }
  87. /* now put offset from beginning of *STRUCTURE* */
  88. /* will be fixed up at the end */
  89. ft_put_word(cxt, OF_DT_PROP);
  90. ft_put_word(cxt, sz);
  91. ft_put_word(cxt, off);
  92. ft_put_bin(cxt, data, sz);
  93. }
  94. void ft_prop_str(struct ft_cxt *cxt, const char *name, const char *str)
  95. {
  96. ft_prop(cxt, name, str, strlen(str) + 1);
  97. }
  98. void ft_prop_int(struct ft_cxt *cxt, const char *name, int val)
  99. {
  100. u32 v = cpu_to_be32((u32) val);
  101. ft_prop(cxt, name, &v, sizeof(u32));
  102. }
  103. /* pick up and start working on a tree in place */
  104. void ft_init_cxt(struct ft_cxt *cxt, void *blob)
  105. {
  106. struct boot_param_header *bph = blob;
  107. memset(cxt, 0, sizeof(*cxt));
  108. cxt->bph = bph;
  109. bph->boot_cpuid_phys = CONFIG_OF_BOOT_CPU;
  110. /* find beginning and end of reserve map table (zeros in last entry) */
  111. cxt->p_rsvmap = (u8 *)bph + bph->off_mem_rsvmap;
  112. while ( ((uint64_t *)cxt->p_rsvmap)[0] != 0 &&
  113. ((uint64_t *)cxt->p_rsvmap)[1] != 0 ) {
  114. cxt->p_rsvmap += SIZE_OF_RSVMAP_ENTRY;
  115. }
  116. cxt->p_start = (char*)bph + bph->off_dt_struct;
  117. cxt->p_end = (char *)bph + bph->totalsize;
  118. cxt->p = (char *)bph + bph->off_dt_strings;
  119. }
  120. /* add a reserver physical area to the rsvmap */
  121. void ft_add_rsvmap(struct ft_cxt *cxt, u64 physstart, u64 physend)
  122. {
  123. memmove(cxt->p_rsvmap + SIZE_OF_RSVMAP_ENTRY, cxt->p_rsvmap,
  124. cxt->p_end - cxt->p_rsvmap);
  125. ((u64 *)cxt->p_rsvmap)[0] = cpu_to_be64(physstart);
  126. ((u64 *)cxt->p_rsvmap)[1] = cpu_to_be64(physend);
  127. ((u64 *)cxt->p_rsvmap)[2] = 0;
  128. ((u64 *)cxt->p_rsvmap)[3] = 0;
  129. cxt->p_rsvmap += SIZE_OF_RSVMAP_ENTRY;
  130. cxt->p_start += SIZE_OF_RSVMAP_ENTRY;
  131. cxt->p += SIZE_OF_RSVMAP_ENTRY;
  132. cxt->p_end += SIZE_OF_RSVMAP_ENTRY;
  133. }
  134. void ft_end_tree(struct ft_cxt *cxt)
  135. {
  136. ft_put_word(cxt, OF_DT_END);
  137. }
  138. /* update the boot param header with correct values */
  139. void ft_finalize_tree(struct ft_cxt *cxt) {
  140. struct boot_param_header *bph = cxt->bph;
  141. bph->totalsize = cxt->p_end - (u8 *)bph;
  142. bph->off_dt_struct = cxt->p_start - (u8 *)bph;
  143. bph->off_dt_strings = cxt->p - (u8 *)bph;
  144. bph->dt_strings_size = cxt->p_end - cxt->p;
  145. }
  146. static inline int isprint(int c)
  147. {
  148. return c >= 0x20 && c <= 0x7e;
  149. }
  150. static int is_printable_string(const void *data, int len)
  151. {
  152. const char *s = data;
  153. const char *ss;
  154. /* zero length is not */
  155. if (len == 0)
  156. return 0;
  157. /* must terminate with zero */
  158. if (s[len - 1] != '\0')
  159. return 0;
  160. ss = s;
  161. while (*s && isprint(*s))
  162. s++;
  163. /* not zero, or not done yet */
  164. if (*s != '\0' || (s + 1 - ss) < len)
  165. return 0;
  166. return 1;
  167. }
  168. static void print_data(const void *data, int len)
  169. {
  170. int i;
  171. const u8 *s;
  172. /* no data, don't print */
  173. if (len == 0)
  174. return;
  175. if (is_printable_string(data, len)) {
  176. printf(" = \"%s\"", (char *)data);
  177. return;
  178. }
  179. switch (len) {
  180. case 1: /* byte */
  181. printf(" = <%02x>", (*(u8 *) data) & 0xff);
  182. break;
  183. case 2: /* half-word */
  184. printf(" = <%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
  185. break;
  186. case 4: /* word */
  187. printf(" = <%x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
  188. break;
  189. case 8: /* double-word */
  190. printf(" = <%qx>", be64_to_cpu(*(uint64_t *) data));
  191. break;
  192. default: /* anything else... hexdump */
  193. printf(" = [");
  194. for (i = 0, s = data; i < len; i++)
  195. printf("%02x%s", s[i], i < len - 1 ? " " : "");
  196. printf("]");
  197. break;
  198. }
  199. }
  200. void ft_dump_blob(const void *bphp)
  201. {
  202. const struct boot_param_header *bph = bphp;
  203. const uint64_t *p_rsvmap = (const uint64_t *)
  204. ((const char *)bph + be32_to_cpu(bph->off_mem_rsvmap));
  205. const u32 *p_struct = (const u32 *)
  206. ((const char *)bph + be32_to_cpu(bph->off_dt_struct));
  207. const u32 *p_strings = (const u32 *)
  208. ((const char *)bph + be32_to_cpu(bph->off_dt_strings));
  209. u32 tag;
  210. const u32 *p;
  211. const char *s, *t;
  212. int depth, sz, shift;
  213. int i;
  214. uint64_t addr, size;
  215. if (be32_to_cpu(bph->magic) != OF_DT_HEADER) {
  216. /* not valid tree */
  217. return;
  218. }
  219. depth = 0;
  220. shift = 4;
  221. for (i = 0;; i++) {
  222. addr = be64_to_cpu(p_rsvmap[i * 2]);
  223. size = be64_to_cpu(p_rsvmap[i * 2 + 1]);
  224. if (addr == 0 && size == 0)
  225. break;
  226. printf("/memreserve/ %qx %qx;\n", addr, size);
  227. }
  228. p = p_struct;
  229. while ((tag = be32_to_cpu(*p++)) != OF_DT_END) {
  230. /* printf("tag: 0x%08x (%d)\n", tag, p - p_struct); */
  231. if (tag == OF_DT_BEGIN_NODE) {
  232. s = (const char *)p;
  233. p = (u32 *) _ALIGN((unsigned long)p + strlen(s) + 1, 4);
  234. printf("%*s%s {\n", depth * shift, "", s);
  235. depth++;
  236. continue;
  237. }
  238. if (tag == OF_DT_END_NODE) {
  239. depth--;
  240. printf("%*s};\n", depth * shift, "");
  241. continue;
  242. }
  243. if (tag == OF_DT_NOP) {
  244. printf("%*s[NOP]\n", depth * shift, "");
  245. continue;
  246. }
  247. if (tag != OF_DT_PROP) {
  248. fprintf(stderr, "%*s ** Unknown tag 0x%08x at 0x%x\n",
  249. depth * shift, "", tag, --p);
  250. break;
  251. }
  252. sz = be32_to_cpu(*p++);
  253. s = (const char *)p_strings + be32_to_cpu(*p++);
  254. t = (const char *)p;
  255. p = (const u32 *)_ALIGN((unsigned long)p + sz, 4);
  256. printf("%*s%s", depth * shift, "", s);
  257. print_data(t, sz);
  258. printf(";\n");
  259. }
  260. }
  261. void ft_backtrack_node(struct ft_cxt *cxt)
  262. {
  263. int i = 4;
  264. while (be32_to_cpu(*(u32 *) (cxt->p - i)) != OF_DT_END_NODE)
  265. i += 4;
  266. memmove (cxt->p - i, cxt->p, cxt->p_end - cxt->p);
  267. cxt->p_end -= i;
  268. cxt->p -= i;
  269. }
  270. void *ft_get_prop(void *bphp, const char *propname, int *szp)
  271. {
  272. struct boot_param_header *bph = bphp;
  273. uint32_t *p_struct =
  274. (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_struct));
  275. uint32_t *p_strings =
  276. (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_strings));
  277. uint32_t version = be32_to_cpu(bph->version);
  278. uint32_t tag;
  279. uint32_t *p;
  280. char *s, *t;
  281. char *ss;
  282. int sz;
  283. static char path[256], prop[256];
  284. path[0] = '\0';
  285. p = p_struct;
  286. while ((tag = be32_to_cpu(*p++)) != OF_DT_END) {
  287. if (tag == OF_DT_BEGIN_NODE) {
  288. s = (char *)p;
  289. p = (uint32_t *) _ALIGN((unsigned long)p + strlen(s) +
  290. 1, 4);
  291. strcat(path, s);
  292. strcat(path, "/");
  293. continue;
  294. }
  295. if (tag == OF_DT_END_NODE) {
  296. path[strlen(path) - 1] = '\0';
  297. ss = strrchr(path, '/');
  298. if (ss != NULL)
  299. ss[1] = '\0';
  300. continue;
  301. }
  302. if (tag == OF_DT_NOP)
  303. continue;
  304. if (tag != OF_DT_PROP)
  305. break;
  306. sz = be32_to_cpu(*p++);
  307. s = (char *)p_strings + be32_to_cpu(*p++);
  308. if (version < 0x10 && sz >= 8)
  309. p = (uint32_t *) _ALIGN((unsigned long)p, 8);
  310. t = (char *)p;
  311. p = (uint32_t *) _ALIGN((unsigned long)p + sz, 4);
  312. strcpy(prop, path);
  313. strcat(prop, s);
  314. if (strcmp(prop, propname) == 0) {
  315. *szp = sz;
  316. return t;
  317. }
  318. }
  319. return NULL;
  320. }
  321. /********************************************************************/
  322. /* Function that returns a character from the environment */
  323. extern uchar(*env_get_char) (int);
  324. #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
  325. #ifdef CONFIG_OF_HAS_BD_T
  326. static const struct {
  327. const char *name;
  328. int offset;
  329. } bd_map[] = {
  330. BDM(memstart),
  331. BDM(memsize),
  332. BDM(flashstart),
  333. BDM(flashsize),
  334. BDM(flashoffset),
  335. BDM(sramstart),
  336. BDM(sramsize),
  337. #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
  338. || defined(CONFIG_E500)
  339. BDM(immr_base),
  340. #endif
  341. #if defined(CONFIG_MPC5xxx)
  342. BDM(mbar_base),
  343. #endif
  344. #if defined(CONFIG_MPC83XX)
  345. BDM(immrbar),
  346. #endif
  347. #if defined(CONFIG_MPC8220)
  348. BDM(mbar_base),
  349. BDM(inpfreq),
  350. BDM(pcifreq),
  351. BDM(pevfreq),
  352. BDM(flbfreq),
  353. BDM(vcofreq),
  354. #endif
  355. BDM(bootflags),
  356. BDM(ip_addr),
  357. BDM(intfreq),
  358. BDM(busfreq),
  359. #ifdef CONFIG_CPM2
  360. BDM(cpmfreq),
  361. BDM(brgfreq),
  362. BDM(sccfreq),
  363. BDM(vco),
  364. #endif
  365. #if defined(CONFIG_MPC5xxx)
  366. BDM(ipbfreq),
  367. BDM(pcifreq),
  368. #endif
  369. BDM(baudrate),
  370. };
  371. #endif
  372. void ft_setup(void *blob, bd_t * bd, ulong initrd_start, ulong initrd_end)
  373. {
  374. u32 *p;
  375. int len;
  376. struct ft_cxt cxt;
  377. ulong clock;
  378. #if defined(CONFIG_OF_HAS_UBOOT_ENV)
  379. int k, nxt;
  380. #endif
  381. #if defined(CONFIG_OF_HAS_BD_T)
  382. u8 *end;
  383. #endif
  384. #if defined(CONFIG_OF_HAS_UBOOT_ENV) || defined(CONFIG_OF_HAS_BD_T)
  385. int i;
  386. static char tmpenv[256];
  387. #endif
  388. /* disable OF tree; booting old kernel */
  389. if (getenv("disable_of") != NULL) {
  390. memcpy(blob, bd, sizeof(*bd));
  391. return;
  392. }
  393. #ifdef DEBUG
  394. printf ("recieved oftree\n");
  395. ft_dump_blob(blob);
  396. #endif
  397. ft_init_cxt(&cxt, blob);
  398. if (initrd_start && initrd_end)
  399. ft_add_rsvmap(&cxt, initrd_start, initrd_end - initrd_start + 1);
  400. /* back into root */
  401. ft_backtrack_node(&cxt);
  402. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  403. ft_begin_node(&cxt, "u-boot-env");
  404. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  405. char *s, *lval, *rval;
  406. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) ;
  407. s = tmpenv;
  408. for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
  409. *s++ = env_get_char(k);
  410. *s++ = '\0';
  411. lval = tmpenv;
  412. s = strchr(tmpenv, '=');
  413. if (s != NULL) {
  414. *s++ = '\0';
  415. rval = s;
  416. } else
  417. continue;
  418. ft_prop_str(&cxt, lval, rval);
  419. }
  420. ft_end_node(&cxt);
  421. #endif
  422. ft_begin_node(&cxt, "chosen");
  423. ft_prop_str(&cxt, "name", "chosen");
  424. ft_prop_str(&cxt, "bootargs", getenv("bootargs"));
  425. ft_prop_int(&cxt, "linux,platform", 0x600); /* what is this? */
  426. if (initrd_start && initrd_end) {
  427. ft_prop_int(&cxt, "linux,initrd-start", initrd_start);
  428. ft_prop_int(&cxt, "linux,initrd-end", initrd_end);
  429. }
  430. #ifdef OF_STDOUT_PATH
  431. ft_prop_str(&cxt, "linux,stdout-path", OF_STDOUT_PATH);
  432. #endif
  433. ft_end_node(&cxt);
  434. ft_end_node(&cxt); /* end root */
  435. ft_end_tree(&cxt);
  436. ft_finalize_tree(&cxt);
  437. #ifdef CONFIG_OF_HAS_BD_T
  438. /* paste the bd_t at the end of the flat tree */
  439. end = (char *)blob +
  440. be32_to_cpu(((struct boot_param_header *)blob)->totalsize);
  441. memcpy(end, bd, sizeof(*bd));
  442. #endif
  443. #ifdef CONFIG_PPC
  444. #ifdef CONFIG_OF_HAS_BD_T
  445. for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
  446. uint32_t v;
  447. sprintf(tmpenv, "/bd_t/%s", bd_map[i].name);
  448. v = *(uint32_t *)((char *)bd + bd_map[i].offset);
  449. p = ft_get_prop(blob, tmpenv, &len);
  450. if (p != NULL)
  451. *p = cpu_to_be32(v);
  452. }
  453. p = ft_get_prop(blob, "/bd_t/enetaddr", &len);
  454. if (p != NULL)
  455. memcpy(p, bd->bi_enetaddr, 6);
  456. p = ft_get_prop(blob, "/bd_t/ethspeed", &len);
  457. if (p != NULL)
  458. *p = cpu_to_be32((uint32_t) bd->bi_ethspeed);
  459. #endif
  460. clock = bd->bi_intfreq;
  461. p = ft_get_prop(blob, "/cpus/" OF_CPU "/clock-frequency", &len);
  462. if (p != NULL)
  463. *p = cpu_to_be32(clock);
  464. #ifdef OF_TBCLK
  465. clock = OF_TBCLK;
  466. p = ft_get_prop(blob, "/cpus/" OF_CPU "/timebase-frequency", &len);
  467. if (p != NULL)
  468. *p = cpu_to_be32(clock);
  469. #endif
  470. #endif /* __powerpc__ */
  471. #ifdef CONFIG_OF_BOARD_SETUP
  472. ft_board_setup(blob, bd);
  473. #endif
  474. /* in case the size changed in the platform code */
  475. ft_finalize_tree(&cxt);
  476. #ifdef DEBUG
  477. printf("final OF-tree\n");
  478. ft_dump_blob(blob);
  479. #endif
  480. }
  481. #endif