ft_build.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #include <linux/ctype.h>
  29. #undef DEBUG
  30. /* align addr on a size boundary - adjust address up if needed -- Cort */
  31. #define _ALIGN(addr,size) (((addr)+(size)-1)&(~((size)-1)))
  32. #ifndef CONFIG_OF_BOOT_CPU
  33. #define CONFIG_OF_BOOT_CPU 0
  34. #endif
  35. #define SIZE_OF_RSVMAP_ENTRY (2*sizeof(u64))
  36. static void ft_put_word(struct ft_cxt *cxt, u32 v)
  37. {
  38. memmove(cxt->p + sizeof(u32), cxt->p, cxt->p_end - cxt->p);
  39. *(u32 *) cxt->p = cpu_to_be32(v);
  40. cxt->p += sizeof(u32);
  41. cxt->p_end += sizeof(u32);
  42. }
  43. static inline void ft_put_bin(struct ft_cxt *cxt, const void *data, int sz)
  44. {
  45. int aligned_size = ((u8 *)_ALIGN((unsigned long)cxt->p + sz,
  46. sizeof(u32))) - cxt->p;
  47. memmove(cxt->p + aligned_size, cxt->p, cxt->p_end - cxt->p);
  48. /* make sure the last bytes are zeroed */
  49. memset(cxt->p + aligned_size - (aligned_size % sizeof(u32)), 0,
  50. (aligned_size % sizeof(u32)));
  51. memcpy(cxt->p, data, sz);
  52. cxt->p += aligned_size;
  53. cxt->p_end += aligned_size;
  54. }
  55. void ft_begin_node(struct ft_cxt *cxt, const char *name)
  56. {
  57. ft_put_word(cxt, OF_DT_BEGIN_NODE);
  58. ft_put_bin(cxt, name, strlen(name) + 1);
  59. }
  60. void ft_end_node(struct ft_cxt *cxt)
  61. {
  62. ft_put_word(cxt, OF_DT_END_NODE);
  63. }
  64. void ft_nop(struct ft_cxt *cxt)
  65. {
  66. ft_put_word(cxt, OF_DT_NOP);
  67. }
  68. static int lookup_string(struct ft_cxt *cxt, const char *name)
  69. {
  70. u8 *p;
  71. p = cxt->p;
  72. while (p < cxt->p_end) {
  73. if (strcmp((char *)p, name) == 0)
  74. return p - cxt->p;
  75. p += strlen((char *)p) + 1;
  76. }
  77. return -1;
  78. }
  79. void ft_prop(struct ft_cxt *cxt, const char *name, const void *data, int sz)
  80. {
  81. int off = 0;
  82. off = lookup_string(cxt, name);
  83. if (off == -1) {
  84. memcpy(cxt->p_end, name, strlen(name) + 1);
  85. off = cxt->p_end - cxt->p;
  86. cxt->p_end += strlen(name) + 1;
  87. }
  88. /* now put offset from beginning of *STRUCTURE* */
  89. /* will be fixed up at the end */
  90. ft_put_word(cxt, OF_DT_PROP);
  91. ft_put_word(cxt, sz);
  92. ft_put_word(cxt, off);
  93. ft_put_bin(cxt, data, sz);
  94. }
  95. void ft_prop_str(struct ft_cxt *cxt, const char *name, const char *str)
  96. {
  97. ft_prop(cxt, name, str, strlen(str) + 1);
  98. }
  99. void ft_prop_int(struct ft_cxt *cxt, const char *name, int val)
  100. {
  101. u32 v = cpu_to_be32((u32) val);
  102. ft_prop(cxt, name, &v, sizeof(u32));
  103. }
  104. /* pick up and start working on a tree in place */
  105. void ft_init_cxt(struct ft_cxt *cxt, void *blob)
  106. {
  107. struct boot_param_header *bph = blob;
  108. memset(cxt, 0, sizeof(*cxt));
  109. cxt->bph = bph;
  110. bph->boot_cpuid_phys = CONFIG_OF_BOOT_CPU;
  111. /* find beginning and end of reserve map table (zeros in last entry) */
  112. cxt->p_rsvmap = (u8 *)bph + bph->off_mem_rsvmap;
  113. while ( ((uint64_t *)cxt->p_rsvmap)[0] != 0 &&
  114. ((uint64_t *)cxt->p_rsvmap)[1] != 0 ) {
  115. cxt->p_rsvmap += SIZE_OF_RSVMAP_ENTRY;
  116. }
  117. cxt->p_start = (u8 *)bph + bph->off_dt_struct;
  118. cxt->p_end = (u8 *)bph + bph->totalsize;
  119. cxt->p = (u8 *)bph + bph->off_dt_strings;
  120. }
  121. /* add a reserver physical area to the rsvmap */
  122. void ft_add_rsvmap(struct ft_cxt *cxt, u64 physstart, u64 physend)
  123. {
  124. memmove(cxt->p_rsvmap + SIZE_OF_RSVMAP_ENTRY, cxt->p_rsvmap,
  125. cxt->p_end - cxt->p_rsvmap);
  126. ((u64 *)cxt->p_rsvmap)[0] = cpu_to_be64(physstart);
  127. ((u64 *)cxt->p_rsvmap)[1] = cpu_to_be64(physend);
  128. ((u64 *)cxt->p_rsvmap)[2] = 0;
  129. ((u64 *)cxt->p_rsvmap)[3] = 0;
  130. cxt->p_rsvmap += SIZE_OF_RSVMAP_ENTRY;
  131. cxt->p_start += SIZE_OF_RSVMAP_ENTRY;
  132. cxt->p += SIZE_OF_RSVMAP_ENTRY;
  133. cxt->p_end += SIZE_OF_RSVMAP_ENTRY;
  134. }
  135. void ft_end_tree(struct ft_cxt *cxt)
  136. {
  137. ft_put_word(cxt, OF_DT_END);
  138. }
  139. /* update the boot param header with correct values */
  140. void ft_finalize_tree(struct ft_cxt *cxt) {
  141. struct boot_param_header *bph = cxt->bph;
  142. bph->totalsize = cxt->p_end - (u8 *)bph;
  143. bph->off_dt_struct = cxt->p_start - (u8 *)bph;
  144. bph->off_dt_strings = cxt->p - (u8 *)bph;
  145. bph->dt_strings_size = cxt->p_end - cxt->p;
  146. }
  147. static int is_printable_string(const void *data, int len)
  148. {
  149. const char *s = data;
  150. const char *ss;
  151. /* zero length is not */
  152. if (len == 0)
  153. return 0;
  154. /* must terminate with zero */
  155. if (s[len - 1] != '\0')
  156. return 0;
  157. ss = s;
  158. while (*s && isprint(*s))
  159. s++;
  160. /* not zero, or not done yet */
  161. if (*s != '\0' || (s + 1 - ss) < len)
  162. return 0;
  163. return 1;
  164. }
  165. static void print_data(const void *data, int len)
  166. {
  167. int i;
  168. const u8 *s;
  169. /* no data, don't print */
  170. if (len == 0)
  171. return;
  172. if (is_printable_string(data, len)) {
  173. puts(" = \"");
  174. puts(data);
  175. puts("\"");
  176. return;
  177. }
  178. switch (len) {
  179. case 1: /* byte */
  180. printf(" = <%02x>", (*(u8 *) data) & 0xff);
  181. break;
  182. case 2: /* half-word */
  183. printf(" = <%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
  184. break;
  185. case 4: /* word */
  186. printf(" = <%x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
  187. break;
  188. case 8: /* double-word */
  189. printf(" = <%qx>", be64_to_cpu(*(uint64_t *) data));
  190. break;
  191. default: /* anything else... hexdump */
  192. printf(" = [");
  193. for (i = 0, s = data; i < len; i++)
  194. printf("%02x%s", s[i], i < len - 1 ? " " : "");
  195. printf("]");
  196. break;
  197. }
  198. }
  199. void ft_dump_blob(const void *bphp)
  200. {
  201. const struct boot_param_header *bph = bphp;
  202. const uint64_t *p_rsvmap = (const uint64_t *)
  203. ((const char *)bph + be32_to_cpu(bph->off_mem_rsvmap));
  204. const u32 *p_struct = (const u32 *)
  205. ((const char *)bph + be32_to_cpu(bph->off_dt_struct));
  206. const u32 *p_strings = (const u32 *)
  207. ((const char *)bph + be32_to_cpu(bph->off_dt_strings));
  208. u32 tag;
  209. const u32 *p;
  210. const char *s, *t;
  211. int depth, sz, shift;
  212. int i;
  213. uint64_t addr, size;
  214. if (be32_to_cpu(bph->magic) != OF_DT_HEADER) {
  215. /* not valid tree */
  216. return;
  217. }
  218. depth = 0;
  219. shift = 4;
  220. for (i = 0;; i++) {
  221. addr = be64_to_cpu(p_rsvmap[i * 2]);
  222. size = be64_to_cpu(p_rsvmap[i * 2 + 1]);
  223. if (addr == 0 && size == 0)
  224. break;
  225. printf("/memreserve/ %qx %qx;\n", addr, size);
  226. }
  227. p = p_struct;
  228. while ((tag = be32_to_cpu(*p++)) != OF_DT_END) {
  229. /* printf("tag: 0x%08x (%d)\n", tag, p - p_struct); */
  230. if (tag == OF_DT_BEGIN_NODE) {
  231. s = (const char *)p;
  232. p = (u32 *) _ALIGN((unsigned long)p + strlen(s) + 1, 4);
  233. printf("%*s%s {\n", depth * shift, "", s);
  234. depth++;
  235. continue;
  236. }
  237. if (tag == OF_DT_END_NODE) {
  238. depth--;
  239. printf("%*s};\n", depth * shift, "");
  240. continue;
  241. }
  242. if (tag == OF_DT_NOP) {
  243. printf("%*s[NOP]\n", depth * shift, "");
  244. continue;
  245. }
  246. if (tag != OF_DT_PROP) {
  247. fprintf(stderr, "%*s ** Unknown tag 0x%08x at 0x%x\n",
  248. depth * shift, "", tag, --p);
  249. break;
  250. }
  251. sz = be32_to_cpu(*p++);
  252. s = (const char *)p_strings + be32_to_cpu(*p++);
  253. t = (const char *)p;
  254. p = (const u32 *)_ALIGN((unsigned long)p + sz, 4);
  255. printf("%*s%s", depth * shift, "", s);
  256. print_data(t, sz);
  257. printf(";\n");
  258. }
  259. }
  260. void ft_backtrack_node(struct ft_cxt *cxt)
  261. {
  262. int i = 4;
  263. while (be32_to_cpu(*(u32 *) (cxt->p - i)) != OF_DT_END_NODE)
  264. i += 4;
  265. memmove (cxt->p - i, cxt->p, cxt->p_end - cxt->p);
  266. cxt->p_end -= i;
  267. cxt->p -= i;
  268. }
  269. void *ft_get_prop(void *bphp, const char *propname, int *szp)
  270. {
  271. struct boot_param_header *bph = bphp;
  272. uint32_t *p_struct =
  273. (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_struct));
  274. uint32_t *p_strings =
  275. (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_strings));
  276. uint32_t version = be32_to_cpu(bph->version);
  277. uint32_t tag;
  278. uint32_t *p;
  279. char *s, *t;
  280. char *ss;
  281. int sz;
  282. static char path[256], prop[256];
  283. path[0] = '\0';
  284. p = p_struct;
  285. while ((tag = be32_to_cpu(*p++)) != OF_DT_END) {
  286. if (tag == OF_DT_BEGIN_NODE) {
  287. s = (char *)p;
  288. p = (uint32_t *) _ALIGN((unsigned long)p + strlen(s) +
  289. 1, 4);
  290. strcat(path, s);
  291. strcat(path, "/");
  292. continue;
  293. }
  294. if (tag == OF_DT_END_NODE) {
  295. path[strlen(path) - 1] = '\0';
  296. ss = strrchr(path, '/');
  297. if (ss != NULL)
  298. ss[1] = '\0';
  299. continue;
  300. }
  301. if (tag == OF_DT_NOP)
  302. continue;
  303. if (tag != OF_DT_PROP)
  304. break;
  305. sz = be32_to_cpu(*p++);
  306. s = (char *)p_strings + be32_to_cpu(*p++);
  307. if (version < 0x10 && sz >= 8)
  308. p = (uint32_t *) _ALIGN((unsigned long)p, 8);
  309. t = (char *)p;
  310. p = (uint32_t *) _ALIGN((unsigned long)p + sz, 4);
  311. strcpy(prop, path);
  312. strcat(prop, s);
  313. if (strcmp(prop, propname) == 0) {
  314. *szp = sz;
  315. return t;
  316. }
  317. }
  318. return NULL;
  319. }
  320. /********************************************************************/
  321. /* Function that returns a character from the environment */
  322. extern uchar(*env_get_char) (int);
  323. void ft_setup(void *blob, bd_t * bd, ulong initrd_start, ulong initrd_end)
  324. {
  325. u32 *p;
  326. int len;
  327. struct ft_cxt cxt;
  328. ulong clock;
  329. /* disable OF tree; booting old kernel */
  330. if (getenv("disable_of") != NULL) {
  331. memcpy(blob, bd, sizeof(*bd));
  332. return;
  333. }
  334. #ifdef DEBUG
  335. printf ("recieved oftree\n");
  336. ft_dump_blob(blob);
  337. #endif
  338. ft_init_cxt(&cxt, blob);
  339. if (initrd_start && initrd_end)
  340. ft_add_rsvmap(&cxt, initrd_start, initrd_end - initrd_start + 1);
  341. /* back into root */
  342. ft_backtrack_node(&cxt);
  343. ft_begin_node(&cxt, "chosen");
  344. ft_prop_str(&cxt, "name", "chosen");
  345. ft_prop_str(&cxt, "bootargs", getenv("bootargs"));
  346. ft_prop_int(&cxt, "linux,platform", 0x600); /* what is this? */
  347. if (initrd_start && initrd_end) {
  348. ft_prop_int(&cxt, "linux,initrd-start", initrd_start);
  349. ft_prop_int(&cxt, "linux,initrd-end", initrd_end);
  350. }
  351. #ifdef OF_STDOUT_PATH
  352. ft_prop_str(&cxt, "linux,stdout-path", OF_STDOUT_PATH);
  353. #endif
  354. ft_end_node(&cxt);
  355. ft_end_node(&cxt); /* end root */
  356. ft_end_tree(&cxt);
  357. ft_finalize_tree(&cxt);
  358. #ifdef CONFIG_PPC
  359. clock = bd->bi_intfreq;
  360. p = ft_get_prop(blob, "/cpus/" OF_CPU "/clock-frequency", &len);
  361. if (p != NULL)
  362. *p = cpu_to_be32(clock);
  363. #ifdef OF_TBCLK
  364. clock = OF_TBCLK;
  365. p = ft_get_prop(blob, "/cpus/" OF_CPU "/timebase-frequency", &len);
  366. if (p != NULL)
  367. *p = cpu_to_be32(clock);
  368. #endif
  369. #endif /* __powerpc__ */
  370. #ifdef CONFIG_OF_BOARD_SETUP
  371. ft_board_setup(blob, bd);
  372. #endif
  373. /* in case the size changed in the platform code */
  374. ft_finalize_tree(&cxt);
  375. #ifdef DEBUG
  376. printf("final OF-tree\n");
  377. ft_dump_blob(blob);
  378. #endif
  379. }
  380. #endif