bootm.c 19 KB

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