bootm.c 19 KB

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