bootm.c 19 KB

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