bootm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2006
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <watchdog.h>
  27. #include <command.h>
  28. #include <image.h>
  29. #include <malloc.h>
  30. #include <zlib.h>
  31. #include <bzlib.h>
  32. #include <environment.h>
  33. #include <asm/byteorder.h>
  34. #if defined(CONFIG_OF_LIBFDT)
  35. #include <fdt.h>
  36. #include <libfdt.h>
  37. #include <fdt_support.h>
  38. static void fdt_error (const char *msg);
  39. static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  40. bootm_headers_t *images, char **of_flat_tree, ulong *of_size);
  41. static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
  42. cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  43. char **of_flat_tree, ulong *of_size);
  44. #endif
  45. #ifdef CFG_INIT_RAM_LOCK
  46. #include <asm/cache.h>
  47. #endif
  48. DECLARE_GLOBAL_DATA_PTR;
  49. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  50. extern ulong get_effective_memsize(void);
  51. static ulong get_sp (void);
  52. static void set_clocks_in_mhz (bd_t *kbd);
  53. #ifndef CFG_LINUX_LOWMEM_MAX_SIZE
  54. #define CFG_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
  55. #endif
  56. void __attribute__((noinline))
  57. do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  58. bootm_headers_t *images)
  59. {
  60. ulong sp;
  61. ulong initrd_start, initrd_end;
  62. ulong rd_data_start, rd_data_end, rd_len;
  63. ulong size;
  64. ulong cmd_start, cmd_end, bootmap_base;
  65. bd_t *kbd;
  66. ulong ep = 0;
  67. void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
  68. int ret;
  69. ulong of_size = 0;
  70. struct lmb *lmb = images->lmb;
  71. #if defined(CONFIG_OF_LIBFDT)
  72. char *of_flat_tree = NULL;
  73. #endif
  74. bootmap_base = getenv_bootm_low();
  75. size = getenv_bootm_size();
  76. #ifdef DEBUG
  77. if (((u64)bootmap_base + size) > (CFG_SDRAM_BASE + (u64)gd->ram_size))
  78. puts("WARNING: bootm_low + bootm_size exceed total memory\n");
  79. if ((bootmap_base + size) > get_effective_memsize())
  80. puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
  81. #endif
  82. size = min(size, get_effective_memsize());
  83. size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
  84. if (size < getenv_bootm_size()) {
  85. ulong base = bootmap_base + size;
  86. printf("WARNING: adjusting available memory to %x\n", size);
  87. lmb_reserve(lmb, base, getenv_bootm_size() - size);
  88. }
  89. /*
  90. * Booting a (Linux) kernel image
  91. *
  92. * Allocate space for command line and board info - the
  93. * address should be as high as possible within the reach of
  94. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  95. * memory, which means far enough below the current stack
  96. * pointer.
  97. */
  98. sp = get_sp();
  99. debug ("## Current stack ends at 0x%08lx\n", sp);
  100. /* adjust sp by 1K to be safe */
  101. sp -= 1024;
  102. lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
  103. #if defined(CONFIG_OF_LIBFDT)
  104. /* find flattened device tree */
  105. ret = boot_get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
  106. if (ret)
  107. goto error;
  108. #endif
  109. if (!of_size) {
  110. /* allocate space and init command line */
  111. ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
  112. if (ret) {
  113. puts("ERROR with allocation of cmdline\n");
  114. goto error;
  115. }
  116. /* allocate space for kernel copy of board info */
  117. ret = boot_get_kbd (lmb, &kbd, bootmap_base);
  118. if (ret) {
  119. puts("ERROR with allocation of kernel bd\n");
  120. goto error;
  121. }
  122. set_clocks_in_mhz(kbd);
  123. }
  124. /* find kernel entry point */
  125. if (images->legacy_hdr_valid) {
  126. ep = image_get_ep (&images->legacy_hdr_os_copy);
  127. #if defined(CONFIG_FIT)
  128. } else if (images->fit_uname_os) {
  129. ret = fit_image_get_entry (images->fit_hdr_os,
  130. images->fit_noffset_os, &ep);
  131. if (ret) {
  132. puts ("Can't get entry point property!\n");
  133. goto error;
  134. }
  135. #endif
  136. } else {
  137. puts ("Could not find kernel entry point!\n");
  138. goto error;
  139. }
  140. kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
  141. /* find ramdisk */
  142. ret = boot_get_ramdisk (argc, argv, images, IH_ARCH_PPC,
  143. &rd_data_start, &rd_data_end);
  144. if (ret)
  145. goto error;
  146. rd_len = rd_data_end - rd_data_start;
  147. #if defined(CONFIG_OF_LIBFDT)
  148. ret = boot_relocate_fdt (lmb, bootmap_base,
  149. cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
  150. /*
  151. * Add the chosen node if it doesn't exist, add the env and bd_t
  152. * if the user wants it (the logic is in the subroutines).
  153. */
  154. if (of_size) {
  155. /* pass in dummy initrd info, we'll fix up later */
  156. if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
  157. fdt_error ("/chosen node create failed");
  158. goto error;
  159. }
  160. #ifdef CONFIG_OF_BOARD_SETUP
  161. /* Call the board-specific fixup routine */
  162. ft_board_setup(of_flat_tree, gd->bd);
  163. #endif
  164. }
  165. #endif /* CONFIG_OF_LIBFDT */
  166. ret = boot_ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
  167. if (ret)
  168. goto error;
  169. #if defined(CONFIG_OF_LIBFDT)
  170. /* fixup the initrd now that we know where it should be */
  171. if ((of_flat_tree) && (initrd_start && initrd_end)) {
  172. uint64_t addr, size;
  173. int total = fdt_num_mem_rsv(of_flat_tree);
  174. int j;
  175. /* Look for the dummy entry and delete it */
  176. for (j = 0; j < total; j++) {
  177. fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
  178. if (addr == rd_data_start) {
  179. fdt_del_mem_rsv(of_flat_tree, j);
  180. break;
  181. }
  182. }
  183. ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
  184. initrd_end - initrd_start + 1);
  185. if (ret < 0) {
  186. printf("fdt_chosen: %s\n", fdt_strerror(ret));
  187. goto error;
  188. }
  189. do_fixup_by_path_u32(of_flat_tree, "/chosen",
  190. "linux,initrd-start", initrd_start, 0);
  191. do_fixup_by_path_u32(of_flat_tree, "/chosen",
  192. "linux,initrd-end", initrd_end, 0);
  193. }
  194. #endif
  195. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  196. (ulong)kernel);
  197. show_boot_progress (15);
  198. #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  199. unlock_ram_in_cache();
  200. #endif
  201. if (!images->autostart)
  202. return ;
  203. #if defined(CONFIG_OF_LIBFDT)
  204. if (of_flat_tree) { /* device tree; boot new style */
  205. /*
  206. * Linux Kernel Parameters (passing device tree):
  207. * r3: pointer to the fdt, followed by the board info data
  208. * r4: physical pointer to the kernel itself
  209. * r5: NULL
  210. * r6: NULL
  211. * r7: NULL
  212. */
  213. debug (" Booting using OF flat tree...\n");
  214. (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
  215. /* does not return */
  216. } else
  217. #endif
  218. {
  219. /*
  220. * Linux Kernel Parameters (passing board info data):
  221. * r3: ptr to board info data
  222. * r4: initrd_start or 0 if no initrd
  223. * r5: initrd_end - unused if r4 is 0
  224. * r6: Start of command line string
  225. * r7: End of command line string
  226. */
  227. debug (" Booting using board info...\n");
  228. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  229. /* does not return */
  230. }
  231. return ;
  232. error:
  233. if (images->autostart)
  234. do_reset (cmdtp, flag, argc, argv);
  235. return ;
  236. }
  237. static ulong get_sp (void)
  238. {
  239. ulong sp;
  240. asm( "mr %0,1": "=r"(sp) : );
  241. return sp;
  242. }
  243. static void set_clocks_in_mhz (bd_t *kbd)
  244. {
  245. char *s;
  246. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  247. /* convert all clock information to MHz */
  248. kbd->bi_intfreq /= 1000000L;
  249. kbd->bi_busfreq /= 1000000L;
  250. #if defined(CONFIG_MPC8220)
  251. kbd->bi_inpfreq /= 1000000L;
  252. kbd->bi_pcifreq /= 1000000L;
  253. kbd->bi_pevfreq /= 1000000L;
  254. kbd->bi_flbfreq /= 1000000L;
  255. kbd->bi_vcofreq /= 1000000L;
  256. #endif
  257. #if defined(CONFIG_CPM2)
  258. kbd->bi_cpmfreq /= 1000000L;
  259. kbd->bi_brgfreq /= 1000000L;
  260. kbd->bi_sccfreq /= 1000000L;
  261. kbd->bi_vco /= 1000000L;
  262. #endif
  263. #if defined(CONFIG_MPC5xxx)
  264. kbd->bi_ipbfreq /= 1000000L;
  265. kbd->bi_pcifreq /= 1000000L;
  266. #endif /* CONFIG_MPC5xxx */
  267. }
  268. }
  269. #if defined(CONFIG_OF_LIBFDT)
  270. static void fdt_error (const char *msg)
  271. {
  272. puts ("ERROR: ");
  273. puts (msg);
  274. puts (" - must RESET the board to recover.\n");
  275. }
  276. static image_header_t *image_get_fdt (ulong fdt_addr)
  277. {
  278. image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
  279. image_print_contents (fdt_hdr);
  280. puts (" Verifying Checksum ... ");
  281. if (!image_check_hcrc (fdt_hdr)) {
  282. fdt_error ("fdt header checksum invalid");
  283. return NULL;
  284. }
  285. if (!image_check_dcrc (fdt_hdr)) {
  286. fdt_error ("fdt checksum invalid");
  287. return NULL;
  288. }
  289. puts ("OK\n");
  290. if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
  291. fdt_error ("uImage is not a fdt");
  292. return NULL;
  293. }
  294. if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
  295. fdt_error ("uImage is compressed");
  296. return NULL;
  297. }
  298. if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
  299. fdt_error ("uImage data is not a fdt");
  300. return NULL;
  301. }
  302. return fdt_hdr;
  303. }
  304. /**
  305. * fit_check_fdt - verify FIT format FDT subimage
  306. * @fit_hdr: pointer to the FIT header
  307. * fdt_noffset: FDT subimage node offset within FIT image
  308. * @verify: data CRC verification flag
  309. *
  310. * fit_check_fdt() verifies integrity of the FDT subimage and from
  311. * specified FIT image.
  312. *
  313. * returns:
  314. * 1, on success
  315. * 0, on failure
  316. */
  317. #if defined(CONFIG_FIT)
  318. static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
  319. {
  320. fit_image_print (fit, fdt_noffset, " ");
  321. if (verify) {
  322. puts (" Verifying Hash Integrity ... ");
  323. if (!fit_image_check_hashes (fit, fdt_noffset)) {
  324. fdt_error ("Bad Data Hash");
  325. return 0;
  326. }
  327. puts ("OK\n");
  328. }
  329. if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
  330. fdt_error ("Not a FDT image");
  331. return 0;
  332. }
  333. if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
  334. fdt_error ("FDT image is compressed");
  335. return 0;
  336. }
  337. return 1;
  338. }
  339. #endif /* CONFIG_FIT */
  340. static int boot_get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  341. bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
  342. {
  343. ulong fdt_addr;
  344. image_header_t *fdt_hdr;
  345. char *fdt_blob = NULL;
  346. ulong image_start, image_end;
  347. ulong load_start, load_end;
  348. #if defined(CONFIG_FIT)
  349. void *fit_hdr;
  350. const char *fit_uname_config = NULL;
  351. const char *fit_uname_fdt = NULL;
  352. ulong default_addr;
  353. int cfg_noffset;
  354. int fdt_noffset;
  355. const void *data;
  356. size_t size;
  357. #endif
  358. *of_flat_tree = NULL;
  359. *of_size = 0;
  360. if (argc > 3 || genimg_has_config (images)) {
  361. #if defined(CONFIG_FIT)
  362. if (argc > 3) {
  363. /*
  364. * If the FDT blob comes from the FIT image and the
  365. * FIT image address is omitted in the command line
  366. * argument, try to use ramdisk or os FIT image
  367. * address or default load address.
  368. */
  369. if (images->fit_uname_rd)
  370. default_addr = (ulong)images->fit_hdr_rd;
  371. else if (images->fit_uname_os)
  372. default_addr = (ulong)images->fit_hdr_os;
  373. else
  374. default_addr = load_addr;
  375. if (fit_parse_conf (argv[3], default_addr,
  376. &fdt_addr, &fit_uname_config)) {
  377. debug ("* fdt: config '%s' from image at 0x%08lx\n",
  378. fit_uname_config, fdt_addr);
  379. } else if (fit_parse_subimage (argv[3], default_addr,
  380. &fdt_addr, &fit_uname_fdt)) {
  381. debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
  382. fit_uname_fdt, fdt_addr);
  383. } else
  384. #endif
  385. {
  386. fdt_addr = simple_strtoul(argv[3], NULL, 16);
  387. debug ("* fdt: cmdline image address = 0x%08lx\n",
  388. fdt_addr);
  389. }
  390. #if defined(CONFIG_FIT)
  391. } else {
  392. /* use FIT configuration provided in first bootm
  393. * command argument
  394. */
  395. fdt_addr = (ulong)images->fit_hdr_os;
  396. fit_uname_config = images->fit_uname_cfg;
  397. debug ("* fdt: using config '%s' from image at 0x%08lx\n",
  398. fit_uname_config, fdt_addr);
  399. /*
  400. * Check whether configuration has FDT blob defined,
  401. * if not quit silently.
  402. */
  403. fit_hdr = (void *)fdt_addr;
  404. cfg_noffset = fit_conf_get_node (fit_hdr,
  405. fit_uname_config);
  406. if (cfg_noffset < 0) {
  407. debug ("* fdt: no such config\n");
  408. return 0;
  409. }
  410. fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
  411. cfg_noffset);
  412. if (fdt_noffset < 0) {
  413. debug ("* fdt: no fdt in config\n");
  414. return 0;
  415. }
  416. }
  417. #endif
  418. debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
  419. fdt_addr);
  420. /* copy from dataflash if needed */
  421. fdt_addr = genimg_get_image (fdt_addr);
  422. /*
  423. * Check if there is an FDT image at the
  424. * address provided in the second bootm argument
  425. * check image type, for FIT images get a FIT node.
  426. */
  427. switch (genimg_get_format ((void *)fdt_addr)) {
  428. case IMAGE_FORMAT_LEGACY:
  429. /* verify fdt_addr points to a valid image header */
  430. printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
  431. fdt_addr);
  432. fdt_hdr = image_get_fdt (fdt_addr);
  433. if (!fdt_hdr)
  434. goto error;
  435. /*
  436. * move image data to the load address,
  437. * make sure we don't overwrite initial image
  438. */
  439. image_start = (ulong)fdt_hdr;
  440. image_end = image_get_image_end (fdt_hdr);
  441. load_start = image_get_load (fdt_hdr);
  442. load_end = load_start + image_get_data_size (fdt_hdr);
  443. if ((load_start < image_end) && (load_end > image_start)) {
  444. fdt_error ("fdt overwritten");
  445. goto error;
  446. }
  447. debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
  448. image_get_data (fdt_hdr), load_start);
  449. memmove ((void *)load_start,
  450. (void *)image_get_data (fdt_hdr),
  451. image_get_data_size (fdt_hdr));
  452. fdt_blob = (char *)load_start;
  453. break;
  454. case IMAGE_FORMAT_FIT:
  455. /*
  456. * This case will catch both: new uImage format
  457. * (libfdt based) and raw FDT blob (also libfdt
  458. * based).
  459. */
  460. #if defined(CONFIG_FIT)
  461. /* check FDT blob vs FIT blob */
  462. if (fit_check_format ((const void *)fdt_addr)) {
  463. /*
  464. * FIT image
  465. */
  466. fit_hdr = (void *)fdt_addr;
  467. printf ("## Flattened Device Tree from FIT Image at %08lx\n",
  468. fdt_addr);
  469. if (!fit_uname_fdt) {
  470. /*
  471. * no FDT blob image node unit name,
  472. * try to get config node first. If
  473. * config unit node name is NULL
  474. * fit_conf_get_node() will try to
  475. * find default config node
  476. */
  477. cfg_noffset = fit_conf_get_node (fit_hdr,
  478. fit_uname_config);
  479. if (cfg_noffset < 0) {
  480. fdt_error ("Could not find configuration node\n");
  481. goto error;
  482. }
  483. fit_uname_config = fdt_get_name (fit_hdr,
  484. cfg_noffset, NULL);
  485. printf (" Using '%s' configuration\n",
  486. fit_uname_config);
  487. fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
  488. cfg_noffset);
  489. fit_uname_fdt = fit_get_name (fit_hdr,
  490. fdt_noffset, NULL);
  491. } else {
  492. /* get FDT component image node offset */
  493. fdt_noffset = fit_image_get_node (fit_hdr,
  494. fit_uname_fdt);
  495. }
  496. if (fdt_noffset < 0) {
  497. fdt_error ("Could not find subimage node\n");
  498. goto error;
  499. }
  500. printf (" Trying '%s' FDT blob subimage\n",
  501. fit_uname_fdt);
  502. if (!fit_check_fdt (fit_hdr, fdt_noffset,
  503. images->verify))
  504. goto error;
  505. /* get ramdisk image data address and length */
  506. if (fit_image_get_data (fit_hdr, fdt_noffset,
  507. &data, &size)) {
  508. fdt_error ("Could not find FDT subimage data");
  509. goto error;
  510. }
  511. /* verift that image data is a proper FDT blob */
  512. if (fdt_check_header ((char *)data) != 0) {
  513. fdt_error ("Subimage data is not a FTD");
  514. goto error;
  515. }
  516. /*
  517. * move image data to the load address,
  518. * make sure we don't overwrite initial image
  519. */
  520. image_start = (ulong)fit_hdr;
  521. image_end = fit_get_end (fit_hdr);
  522. if (fit_image_get_load (fit_hdr, fdt_noffset,
  523. &load_start) == 0) {
  524. load_end = load_start + size;
  525. if ((load_start < image_end) &&
  526. (load_end > image_start)) {
  527. fdt_error ("FDT overwritten");
  528. goto error;
  529. }
  530. printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
  531. (ulong)data, load_start);
  532. memmove ((void *)load_start,
  533. (void *)data, size);
  534. fdt_blob = (char *)load_start;
  535. } else {
  536. fdt_blob = (char *)data;
  537. }
  538. images->fit_hdr_fdt = fit_hdr;
  539. images->fit_uname_fdt = fit_uname_fdt;
  540. images->fit_noffset_fdt = fdt_noffset;
  541. break;
  542. } else
  543. #endif
  544. {
  545. /*
  546. * FDT blob
  547. */
  548. fdt_blob = (char *)fdt_addr;
  549. debug ("* fdt: raw FDT blob\n");
  550. printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob);
  551. }
  552. break;
  553. default:
  554. fdt_error ("Did not find a cmdline Flattened Device Tree");
  555. goto error;
  556. }
  557. printf (" Booting using the fdt blob at 0x%x\n", fdt_blob);
  558. } else if (images->legacy_hdr_valid &&
  559. image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
  560. ulong fdt_data, fdt_len;
  561. /*
  562. * Now check if we have a legacy multi-component image,
  563. * get second entry data start address and len.
  564. */
  565. printf ("## Flattened Device Tree from multi "
  566. "component Image at %08lX\n",
  567. (ulong)images->legacy_hdr_os);
  568. image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
  569. if (fdt_len) {
  570. fdt_blob = (char *)fdt_data;
  571. printf (" Booting using the fdt at 0x%x\n", fdt_blob);
  572. if (fdt_check_header (fdt_blob) != 0) {
  573. fdt_error ("image is not a fdt");
  574. goto error;
  575. }
  576. if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
  577. fdt_error ("fdt size != image size");
  578. goto error;
  579. }
  580. } else {
  581. debug ("## No Flattened Device Tree\n");
  582. return 0;
  583. }
  584. } else {
  585. debug ("## No Flattened Device Tree\n");
  586. return 0;
  587. }
  588. *of_flat_tree = fdt_blob;
  589. *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
  590. debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
  591. *of_flat_tree, *of_size);
  592. return 0;
  593. error:
  594. do_reset (cmdtp, flag, argc, argv);
  595. return 1;
  596. }
  597. static int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
  598. cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  599. char **of_flat_tree, ulong *of_size)
  600. {
  601. char *fdt_blob = *of_flat_tree;
  602. ulong relocate = 0;
  603. ulong of_len = 0;
  604. /* nothing to do */
  605. if (*of_size == 0)
  606. return 0;
  607. if (fdt_check_header (fdt_blob) != 0) {
  608. fdt_error ("image is not a fdt");
  609. goto error;
  610. }
  611. #ifndef CFG_NO_FLASH
  612. /* move the blob if it is in flash (set relocate) */
  613. if (addr2info ((ulong)fdt_blob) != NULL)
  614. relocate = 1;
  615. #endif
  616. /*
  617. * The blob must be within CFG_BOOTMAPSZ,
  618. * so we flag it to be copied if it is not.
  619. */
  620. if (fdt_blob >= (char *)CFG_BOOTMAPSZ)
  621. relocate = 1;
  622. of_len = be32_to_cpu (fdt_totalsize (fdt_blob));
  623. /* move flattend device tree if needed */
  624. if (relocate) {
  625. int err;
  626. ulong of_start;
  627. /* position on a 4K boundary before the alloc_current */
  628. of_start = lmb_alloc_base(lmb, of_len, 0x1000,
  629. (CFG_BOOTMAPSZ + bootmap_base));
  630. if (of_start == 0) {
  631. puts("device tree - allocation error\n");
  632. goto error;
  633. }
  634. debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  635. (ulong)fdt_blob, (ulong)fdt_blob + of_len - 1,
  636. of_len, of_len);
  637. printf (" Loading Device Tree to %08lx, end %08lx ... ",
  638. of_start, of_start + of_len - 1);
  639. err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
  640. if (err != 0) {
  641. fdt_error ("fdt move failed");
  642. goto error;
  643. }
  644. puts ("OK\n");
  645. *of_flat_tree = (char *)of_start;
  646. } else {
  647. *of_flat_tree = fdt_blob;
  648. lmb_reserve(lmb, (ulong)fdt, of_len);
  649. }
  650. return 0;
  651. error:
  652. return 1;
  653. }
  654. #endif