bootm.c 20 KB

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