ft_build.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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((char *)p, name) == 0)
  73. return p - cxt->p;
  74. p += strlen((char *)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 = (u8 *)bph + bph->off_dt_struct;
  117. cxt->p_end = (u8 *)bph + bph->totalsize;
  118. cxt->p = (u8 *)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. puts(" = \"");
  177. puts(data);
  178. puts("\"");
  179. return;
  180. }
  181. switch (len) {
  182. case 1: /* byte */
  183. printf(" = <%02x>", (*(u8 *) data) & 0xff);
  184. break;
  185. case 2: /* half-word */
  186. printf(" = <%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
  187. break;
  188. case 4: /* word */
  189. printf(" = <%x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
  190. break;
  191. case 8: /* double-word */
  192. printf(" = <%qx>", be64_to_cpu(*(uint64_t *) data));
  193. break;
  194. default: /* anything else... hexdump */
  195. printf(" = [");
  196. for (i = 0, s = data; i < len; i++)
  197. printf("%02x%s", s[i], i < len - 1 ? " " : "");
  198. printf("]");
  199. break;
  200. }
  201. }
  202. void ft_dump_blob(const void *bphp)
  203. {
  204. const struct boot_param_header *bph = bphp;
  205. const uint64_t *p_rsvmap = (const uint64_t *)
  206. ((const char *)bph + be32_to_cpu(bph->off_mem_rsvmap));
  207. const u32 *p_struct = (const u32 *)
  208. ((const char *)bph + be32_to_cpu(bph->off_dt_struct));
  209. const u32 *p_strings = (const u32 *)
  210. ((const char *)bph + be32_to_cpu(bph->off_dt_strings));
  211. u32 tag;
  212. const u32 *p;
  213. const char *s, *t;
  214. int depth, sz, shift;
  215. int i;
  216. uint64_t addr, size;
  217. if (be32_to_cpu(bph->magic) != OF_DT_HEADER) {
  218. /* not valid tree */
  219. return;
  220. }
  221. depth = 0;
  222. shift = 4;
  223. for (i = 0;; i++) {
  224. addr = be64_to_cpu(p_rsvmap[i * 2]);
  225. size = be64_to_cpu(p_rsvmap[i * 2 + 1]);
  226. if (addr == 0 && size == 0)
  227. break;
  228. printf("/memreserve/ %qx %qx;\n", addr, size);
  229. }
  230. p = p_struct;
  231. while ((tag = be32_to_cpu(*p++)) != OF_DT_END) {
  232. /* printf("tag: 0x%08x (%d)\n", tag, p - p_struct); */
  233. if (tag == OF_DT_BEGIN_NODE) {
  234. s = (const char *)p;
  235. p = (u32 *) _ALIGN((unsigned long)p + strlen(s) + 1, 4);
  236. printf("%*s%s {\n", depth * shift, "", s);
  237. depth++;
  238. continue;
  239. }
  240. if (tag == OF_DT_END_NODE) {
  241. depth--;
  242. printf("%*s};\n", depth * shift, "");
  243. continue;
  244. }
  245. if (tag == OF_DT_NOP) {
  246. printf("%*s[NOP]\n", depth * shift, "");
  247. continue;
  248. }
  249. if (tag != OF_DT_PROP) {
  250. fprintf(stderr, "%*s ** Unknown tag 0x%08x at 0x%x\n",
  251. depth * shift, "", tag, --p);
  252. break;
  253. }
  254. sz = be32_to_cpu(*p++);
  255. s = (const char *)p_strings + be32_to_cpu(*p++);
  256. t = (const char *)p;
  257. p = (const u32 *)_ALIGN((unsigned long)p + sz, 4);
  258. printf("%*s%s", depth * shift, "", s);
  259. print_data(t, sz);
  260. printf(";\n");
  261. }
  262. }
  263. void ft_backtrack_node(struct ft_cxt *cxt)
  264. {
  265. int i = 4;
  266. while (be32_to_cpu(*(u32 *) (cxt->p - i)) != OF_DT_END_NODE)
  267. i += 4;
  268. memmove (cxt->p - i, cxt->p, cxt->p_end - cxt->p);
  269. cxt->p_end -= i;
  270. cxt->p -= i;
  271. }
  272. void *ft_get_prop(void *bphp, const char *propname, int *szp)
  273. {
  274. struct boot_param_header *bph = bphp;
  275. uint32_t *p_struct =
  276. (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_struct));
  277. uint32_t *p_strings =
  278. (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_strings));
  279. uint32_t version = be32_to_cpu(bph->version);
  280. uint32_t tag;
  281. uint32_t *p;
  282. char *s, *t;
  283. char *ss;
  284. int sz;
  285. static char path[256], prop[256];
  286. path[0] = '\0';
  287. p = p_struct;
  288. while ((tag = be32_to_cpu(*p++)) != OF_DT_END) {
  289. if (tag == OF_DT_BEGIN_NODE) {
  290. s = (char *)p;
  291. p = (uint32_t *) _ALIGN((unsigned long)p + strlen(s) +
  292. 1, 4);
  293. strcat(path, s);
  294. strcat(path, "/");
  295. continue;
  296. }
  297. if (tag == OF_DT_END_NODE) {
  298. path[strlen(path) - 1] = '\0';
  299. ss = strrchr(path, '/');
  300. if (ss != NULL)
  301. ss[1] = '\0';
  302. continue;
  303. }
  304. if (tag == OF_DT_NOP)
  305. continue;
  306. if (tag != OF_DT_PROP)
  307. break;
  308. sz = be32_to_cpu(*p++);
  309. s = (char *)p_strings + be32_to_cpu(*p++);
  310. if (version < 0x10 && sz >= 8)
  311. p = (uint32_t *) _ALIGN((unsigned long)p, 8);
  312. t = (char *)p;
  313. p = (uint32_t *) _ALIGN((unsigned long)p + sz, 4);
  314. strcpy(prop, path);
  315. strcat(prop, s);
  316. if (strcmp(prop, propname) == 0) {
  317. *szp = sz;
  318. return t;
  319. }
  320. }
  321. return NULL;
  322. }
  323. /********************************************************************/
  324. /* Function that returns a character from the environment */
  325. extern uchar(*env_get_char) (int);
  326. #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
  327. #ifdef CONFIG_OF_HAS_BD_T
  328. static const struct {
  329. const char *name;
  330. int offset;
  331. } bd_map[] = {
  332. BDM(memstart),
  333. BDM(memsize),
  334. BDM(flashstart),
  335. BDM(flashsize),
  336. BDM(flashoffset),
  337. BDM(sramstart),
  338. BDM(sramsize),
  339. #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
  340. || defined(CONFIG_E500)
  341. BDM(immr_base),
  342. #endif
  343. #if defined(CONFIG_MPC5xxx)
  344. BDM(mbar_base),
  345. #endif
  346. #if defined(CONFIG_MPC83XX)
  347. BDM(immrbar),
  348. #endif
  349. #if defined(CONFIG_MPC8220)
  350. BDM(mbar_base),
  351. BDM(inpfreq),
  352. BDM(pcifreq),
  353. BDM(pevfreq),
  354. BDM(flbfreq),
  355. BDM(vcofreq),
  356. #endif
  357. BDM(bootflags),
  358. BDM(ip_addr),
  359. BDM(intfreq),
  360. BDM(busfreq),
  361. #ifdef CONFIG_CPM2
  362. BDM(cpmfreq),
  363. BDM(brgfreq),
  364. BDM(sccfreq),
  365. BDM(vco),
  366. #endif
  367. #if defined(CONFIG_MPC5xxx)
  368. BDM(ipbfreq),
  369. BDM(pcifreq),
  370. #endif
  371. BDM(baudrate),
  372. };
  373. #endif
  374. void ft_setup(void *blob, bd_t * bd, ulong initrd_start, ulong initrd_end)
  375. {
  376. u32 *p;
  377. int len;
  378. struct ft_cxt cxt;
  379. ulong clock;
  380. #if defined(CONFIG_OF_HAS_UBOOT_ENV)
  381. int k, nxt;
  382. #endif
  383. #if defined(CONFIG_OF_HAS_BD_T)
  384. u8 *end;
  385. #endif
  386. #if defined(CONFIG_OF_HAS_UBOOT_ENV) || defined(CONFIG_OF_HAS_BD_T)
  387. int i;
  388. static char tmpenv[256];
  389. #endif
  390. /* disable OF tree; booting old kernel */
  391. if (getenv("disable_of") != NULL) {
  392. memcpy(blob, bd, sizeof(*bd));
  393. return;
  394. }
  395. #ifdef DEBUG
  396. printf ("recieved oftree\n");
  397. ft_dump_blob(blob);
  398. #endif
  399. ft_init_cxt(&cxt, blob);
  400. if (initrd_start && initrd_end)
  401. ft_add_rsvmap(&cxt, initrd_start, initrd_end - initrd_start + 1);
  402. /* back into root */
  403. ft_backtrack_node(&cxt);
  404. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  405. ft_begin_node(&cxt, "u-boot-env");
  406. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  407. char *s, *lval, *rval;
  408. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) ;
  409. s = tmpenv;
  410. for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
  411. *s++ = env_get_char(k);
  412. *s++ = '\0';
  413. lval = tmpenv;
  414. s = strchr(tmpenv, '=');
  415. if (s != NULL) {
  416. *s++ = '\0';
  417. rval = s;
  418. } else
  419. continue;
  420. ft_prop_str(&cxt, lval, rval);
  421. }
  422. ft_end_node(&cxt);
  423. #endif
  424. ft_begin_node(&cxt, "chosen");
  425. ft_prop_str(&cxt, "name", "chosen");
  426. ft_prop_str(&cxt, "bootargs", getenv("bootargs"));
  427. ft_prop_int(&cxt, "linux,platform", 0x600); /* what is this? */
  428. if (initrd_start && initrd_end) {
  429. ft_prop_int(&cxt, "linux,initrd-start", initrd_start);
  430. ft_prop_int(&cxt, "linux,initrd-end", initrd_end);
  431. }
  432. #ifdef OF_STDOUT_PATH
  433. ft_prop_str(&cxt, "linux,stdout-path", OF_STDOUT_PATH);
  434. #endif
  435. ft_end_node(&cxt);
  436. ft_end_node(&cxt); /* end root */
  437. ft_end_tree(&cxt);
  438. ft_finalize_tree(&cxt);
  439. #ifdef CONFIG_OF_HAS_BD_T
  440. /* paste the bd_t at the end of the flat tree */
  441. end = (char *)blob +
  442. be32_to_cpu(((struct boot_param_header *)blob)->totalsize);
  443. memcpy(end, bd, sizeof(*bd));
  444. #endif
  445. #ifdef CONFIG_PPC
  446. #ifdef CONFIG_OF_HAS_BD_T
  447. for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
  448. uint32_t v;
  449. sprintf(tmpenv, "/bd_t/%s", bd_map[i].name);
  450. v = *(uint32_t *)((char *)bd + bd_map[i].offset);
  451. p = ft_get_prop(blob, tmpenv, &len);
  452. if (p != NULL)
  453. *p = cpu_to_be32(v);
  454. }
  455. p = ft_get_prop(blob, "/bd_t/enetaddr", &len);
  456. if (p != NULL)
  457. memcpy(p, bd->bi_enetaddr, 6);
  458. p = ft_get_prop(blob, "/bd_t/ethspeed", &len);
  459. if (p != NULL)
  460. *p = cpu_to_be32((uint32_t) bd->bi_ethspeed);
  461. #endif
  462. clock = bd->bi_intfreq;
  463. p = ft_get_prop(blob, "/cpus/" OF_CPU "/clock-frequency", &len);
  464. if (p != NULL)
  465. *p = cpu_to_be32(clock);
  466. #ifdef OF_TBCLK
  467. clock = OF_TBCLK;
  468. p = ft_get_prop(blob, "/cpus/" OF_CPU "/timebase-frequency", &len);
  469. if (p != NULL)
  470. *p = cpu_to_be32(clock);
  471. #endif
  472. #endif /* __powerpc__ */
  473. #ifdef CONFIG_OF_BOARD_SETUP
  474. ft_board_setup(blob, bd);
  475. #endif
  476. /* in case the size changed in the platform code */
  477. ft_finalize_tree(&cxt);
  478. #ifdef DEBUG
  479. printf("final OF-tree\n");
  480. ft_dump_blob(blob);
  481. #endif
  482. }
  483. #endif