bootm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 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 fdt_relocate (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. void __attribute__((noinline))
  55. do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  56. bootm_headers_t *images)
  57. {
  58. ulong sp;
  59. ulong initrd_start, initrd_end;
  60. ulong rd_data_start, rd_data_end, rd_len;
  61. ulong cmd_start, cmd_end, bootmap_base;
  62. bd_t *kbd;
  63. ulong ep = 0;
  64. void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
  65. int ret;
  66. ulong of_size = 0;
  67. struct lmb *lmb = images->lmb;
  68. #if defined(CONFIG_OF_LIBFDT)
  69. char *of_flat_tree = NULL;
  70. #endif
  71. bootmap_base = 0;
  72. /*
  73. * Booting a (Linux) kernel image
  74. *
  75. * Allocate space for command line and board info - the
  76. * address should be as high as possible within the reach of
  77. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  78. * memory, which means far enough below the current stack
  79. * pointer.
  80. */
  81. sp = get_sp();
  82. debug ("## Current stack ends at 0x%08lx ", sp);
  83. /* adjust sp by 1K to be safe */
  84. sp -= 1024;
  85. lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
  86. #if defined(CONFIG_OF_LIBFDT)
  87. /* find flattened device tree */
  88. ret = get_fdt (cmdtp, flag, argc, argv, images, &of_flat_tree, &of_size);
  89. if (ret)
  90. goto error;
  91. #endif
  92. if (!of_size) {
  93. /* allocate space and init command line */
  94. ret = get_boot_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
  95. if (ret) {
  96. puts("ERROR with allocation of cmdline\n");
  97. goto error;
  98. }
  99. /* allocate space for kernel copy of board info */
  100. ret = get_boot_kbd (lmb, &kbd, bootmap_base);
  101. if (ret) {
  102. puts("ERROR with allocation of kernel bd\n");
  103. goto error;
  104. }
  105. set_clocks_in_mhz(kbd);
  106. }
  107. /* find kernel entry point */
  108. if (images->legacy_hdr_valid) {
  109. ep = image_get_ep (images->legacy_hdr_os);
  110. #if defined(CONFIG_FIT)
  111. } else if (images->fit_uname_os) {
  112. fit_unsupported_reset ("PPC linux bootm");
  113. goto error;
  114. #endif
  115. } else {
  116. puts ("Could not find kernel entry point!\n");
  117. goto error;
  118. }
  119. kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))ep;
  120. /* find ramdisk */
  121. ret = get_ramdisk (cmdtp, flag, argc, argv, images,
  122. IH_ARCH_PPC, &rd_data_start, &rd_data_end);
  123. if (ret)
  124. goto error;
  125. rd_len = rd_data_end - rd_data_start;
  126. #if defined(CONFIG_OF_LIBFDT)
  127. ret = fdt_relocate (lmb, bootmap_base,
  128. cmdtp, flag, argc, argv, &of_flat_tree, &of_size);
  129. /*
  130. * Add the chosen node if it doesn't exist, add the env and bd_t
  131. * if the user wants it (the logic is in the subroutines).
  132. */
  133. if (of_size) {
  134. /* pass in dummy initrd info, we'll fix up later */
  135. if (fdt_chosen(of_flat_tree, rd_data_start, rd_data_end, 0) < 0) {
  136. fdt_error ("/chosen node create failed");
  137. goto error;
  138. }
  139. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  140. if (fdt_env(of_flat_tree) < 0) {
  141. fdt_error ("/u-boot-env node create failed");
  142. goto error;
  143. }
  144. #endif
  145. #ifdef CONFIG_OF_HAS_BD_T
  146. if (fdt_bd_t(of_flat_tree) < 0) {
  147. fdt_error ("/bd_t node create failed");
  148. goto error;
  149. }
  150. #endif
  151. #ifdef CONFIG_OF_BOARD_SETUP
  152. /* Call the board-specific fixup routine */
  153. ft_board_setup(of_flat_tree, gd->bd);
  154. #endif
  155. }
  156. #endif /* CONFIG_OF_LIBFDT */
  157. ret = ramdisk_high (lmb, rd_data_start, rd_len, &initrd_start, &initrd_end);
  158. if (ret)
  159. goto error;
  160. #if defined(CONFIG_OF_LIBFDT)
  161. /* fixup the initrd now that we know where it should be */
  162. if ((of_flat_tree) && (initrd_start && initrd_end)) {
  163. uint64_t addr, size;
  164. int total = fdt_num_mem_rsv(of_flat_tree);
  165. int j;
  166. /* Look for the dummy entry and delete it */
  167. for (j = 0; j < total; j++) {
  168. fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
  169. if (addr == rd_data_start) {
  170. fdt_del_mem_rsv(of_flat_tree, j);
  171. break;
  172. }
  173. }
  174. ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
  175. initrd_end - initrd_start + 1);
  176. if (ret < 0) {
  177. printf("fdt_chosen: %s\n", fdt_strerror(ret));
  178. goto error;
  179. }
  180. do_fixup_by_path_u32(of_flat_tree, "/chosen",
  181. "linux,initrd-start", initrd_start, 0);
  182. do_fixup_by_path_u32(of_flat_tree, "/chosen",
  183. "linux,initrd-end", initrd_end, 0);
  184. }
  185. #endif
  186. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  187. (ulong)kernel);
  188. show_boot_progress (15);
  189. #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  190. unlock_ram_in_cache();
  191. #endif
  192. #if defined(CONFIG_OF_LIBFDT)
  193. if (of_flat_tree) { /* device tree; boot new style */
  194. /*
  195. * Linux Kernel Parameters (passing device tree):
  196. * r3: pointer to the fdt, followed by the board info data
  197. * r4: physical pointer to the kernel itself
  198. * r5: NULL
  199. * r6: NULL
  200. * r7: NULL
  201. */
  202. (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
  203. /* does not return */
  204. }
  205. #endif
  206. /*
  207. * Linux Kernel Parameters (passing board info data):
  208. * r3: ptr to board info data
  209. * r4: initrd_start or 0 if no initrd
  210. * r5: initrd_end - unused if r4 is 0
  211. * r6: Start of command line string
  212. * r7: End of command line string
  213. */
  214. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  215. /* does not return */
  216. return ;
  217. error:
  218. do_reset (cmdtp, flag, argc, argv);
  219. return ;
  220. }
  221. static ulong get_sp (void)
  222. {
  223. ulong sp;
  224. asm( "mr %0,1": "=r"(sp) : );
  225. return sp;
  226. }
  227. static void set_clocks_in_mhz (bd_t *kbd)
  228. {
  229. char *s;
  230. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  231. /* convert all clock information to MHz */
  232. kbd->bi_intfreq /= 1000000L;
  233. kbd->bi_busfreq /= 1000000L;
  234. #if defined(CONFIG_MPC8220)
  235. kbd->bi_inpfreq /= 1000000L;
  236. kbd->bi_pcifreq /= 1000000L;
  237. kbd->bi_pevfreq /= 1000000L;
  238. kbd->bi_flbfreq /= 1000000L;
  239. kbd->bi_vcofreq /= 1000000L;
  240. #endif
  241. #if defined(CONFIG_CPM2)
  242. kbd->bi_cpmfreq /= 1000000L;
  243. kbd->bi_brgfreq /= 1000000L;
  244. kbd->bi_sccfreq /= 1000000L;
  245. kbd->bi_vco /= 1000000L;
  246. #endif
  247. #if defined(CONFIG_MPC5xxx)
  248. kbd->bi_ipbfreq /= 1000000L;
  249. kbd->bi_pcifreq /= 1000000L;
  250. #endif /* CONFIG_MPC5xxx */
  251. }
  252. }
  253. #if defined(CONFIG_OF_LIBFDT)
  254. static void fdt_error (const char *msg)
  255. {
  256. puts ("ERROR: ");
  257. puts (msg);
  258. puts (" - must RESET the board to recover.\n");
  259. }
  260. static image_header_t *image_get_fdt (ulong fdt_addr)
  261. {
  262. image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
  263. image_print_contents (fdt_hdr);
  264. puts (" Verifying Checksum ... ");
  265. if (!image_check_hcrc (fdt_hdr)) {
  266. fdt_error ("fdt header checksum invalid");
  267. return NULL;
  268. }
  269. if (!image_check_dcrc (fdt_hdr)) {
  270. fdt_error ("fdt checksum invalid");
  271. return NULL;
  272. }
  273. puts ("OK\n");
  274. if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
  275. fdt_error ("uImage is not a fdt");
  276. return NULL;
  277. }
  278. if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
  279. fdt_error ("uImage is compressed");
  280. return NULL;
  281. }
  282. if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
  283. fdt_error ("uImage data is not a fdt");
  284. return NULL;
  285. }
  286. return fdt_hdr;
  287. }
  288. static int get_fdt (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  289. bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
  290. {
  291. ulong fdt_addr;
  292. image_header_t *fdt_hdr;
  293. char *fdt_blob = NULL;
  294. ulong image_start, image_end;
  295. ulong load_start, load_end;
  296. #if defined(CONFIG_FIT)
  297. void *fit_hdr;
  298. const char *fit_uname_config = NULL;
  299. const char *fit_uname_fdt = NULL;
  300. ulong default_addr;
  301. #endif
  302. if (argc > 3) {
  303. #if defined(CONFIG_FIT)
  304. /*
  305. * If the FDT blob comes from the FIT image and the FIT image
  306. * address is omitted in the command line argument, try to use
  307. * ramdisk or os FIT image address or default load address.
  308. */
  309. if (images->fit_uname_rd)
  310. default_addr = (ulong)images->fit_hdr_rd;
  311. else if (images->fit_uname_os)
  312. default_addr = (ulong)images->fit_hdr_os;
  313. else
  314. default_addr = load_addr;
  315. if (fit_parse_conf (argv[3], default_addr,
  316. &fdt_addr, &fit_uname_config)) {
  317. debug ("* fdt: config '%s' from image at 0x%08lx\n",
  318. fit_uname_config, fdt_addr);
  319. } else if (fit_parse_subimage (argv[3], default_addr,
  320. &fdt_addr, &fit_uname_fdt)) {
  321. debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
  322. fit_uname_fdt, fdt_addr);
  323. } else
  324. #endif
  325. {
  326. fdt_addr = simple_strtoul(argv[3], NULL, 16);
  327. debug ("* fdt: cmdline image address = 0x%08lx\n",
  328. fdt_addr);
  329. }
  330. debug ("## Checking for 'FDT'/'FDT image' at %08lx\n",
  331. fdt_addr);
  332. /* copy from dataflash if needed */
  333. fdt_addr = gen_get_image (fdt_addr);
  334. /*
  335. * Check if there is an FDT image at the
  336. * address provided in the second bootm argument
  337. * check image type, for FIT images get a FIT node.
  338. */
  339. switch (gen_image_get_format ((void *)fdt_addr)) {
  340. case IMAGE_FORMAT_LEGACY:
  341. debug ("* fdt: legacy format image\n");
  342. /* verify fdt_addr points to a valid image header */
  343. printf ("## Flattened Device Tree Legacy Image at %08lx\n",
  344. fdt_addr);
  345. fdt_hdr = image_get_fdt (fdt_addr);
  346. if (!fdt_hdr)
  347. goto error;
  348. /*
  349. * move image data to the load address,
  350. * make sure we don't overwrite initial image
  351. */
  352. image_start = (ulong)fdt_hdr;
  353. image_end = image_get_image_end (fdt_hdr);
  354. load_start = image_get_load (fdt_hdr);
  355. load_end = load_start + image_get_data_size (fdt_hdr);
  356. if ((load_start < image_end) && (load_end > image_start)) {
  357. fdt_error ("fdt overwritten");
  358. goto error;
  359. }
  360. memmove ((void *)image_get_load (fdt_hdr),
  361. (void *)image_get_data (fdt_hdr),
  362. image_get_data_size (fdt_hdr));
  363. fdt_blob = (char *)image_get_load (fdt_hdr);
  364. break;
  365. case IMAGE_FORMAT_FIT:
  366. /*
  367. * This case will catch both: new uImage format
  368. * (libfdt based) and raw FDT blob (also libfdt
  369. * based).
  370. */
  371. #if defined(CONFIG_FIT)
  372. /* check FDT blob vs FIT blob */
  373. if (0) { /* FIXME: call FIT format verification */
  374. /*
  375. * FIT image
  376. */
  377. fit_hdr = (void *)fdt_addr;
  378. debug ("* fdt: FIT format image\n");
  379. fit_unsupported_reset ("PPC fdt");
  380. goto error;
  381. } else
  382. #endif
  383. {
  384. /*
  385. * FDT blob
  386. */
  387. debug ("* fdt: raw FDT blob\n");
  388. printf ("## Flattened Device Tree blob at %08lx\n", fdt_blob);
  389. fdt_blob = (char *)fdt_addr;
  390. }
  391. break;
  392. default:
  393. fdt_error ("Did not find a cmdline Flattened Device Tree");
  394. goto error;
  395. }
  396. printf (" Booting using the fdt blob at 0x%x\n", fdt_blob);
  397. } else if (images->legacy_hdr_valid &&
  398. image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
  399. ulong fdt_data, fdt_len;
  400. /*
  401. * Now check if we have a legacy multi-component image,
  402. * get second entry data start address and len.
  403. */
  404. printf ("## Flattened Device Tree from multi "
  405. "component Image at %08lX\n",
  406. (ulong)images->legacy_hdr_os);
  407. image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
  408. if (fdt_len) {
  409. fdt_blob = (char *)fdt_data;
  410. printf (" Booting using the fdt at 0x%x\n", fdt_blob);
  411. if (fdt_check_header (fdt_blob) != 0) {
  412. fdt_error ("image is not a fdt");
  413. goto error;
  414. }
  415. if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
  416. fdt_error ("fdt size != image size");
  417. goto error;
  418. }
  419. } else {
  420. fdt_error ("Did not find a Flattened Device Tree "
  421. "in a legacy multi-component image");
  422. goto error;
  423. }
  424. } else {
  425. debug ("## No Flattened Device Tree\n");
  426. *of_flat_tree = NULL;
  427. *of_size = 0;
  428. return 0;
  429. }
  430. *of_flat_tree = fdt_blob;
  431. *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
  432. debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
  433. *of_flat_tree, *of_size);
  434. return 0;
  435. error:
  436. do_reset (cmdtp, flag, argc, argv);
  437. return 1;
  438. }
  439. static int fdt_relocate (struct lmb *lmb, ulong bootmap_base,
  440. cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  441. char **of_flat_tree, ulong *of_size)
  442. {
  443. char *fdt_blob = *of_flat_tree;
  444. ulong relocate = 0;
  445. ulong of_len = 0;
  446. /* nothing to do */
  447. if (*of_size == 0)
  448. return 0;
  449. if (fdt_check_header (fdt_blob) != 0) {
  450. fdt_error ("image is not a fdt");
  451. goto error;
  452. }
  453. #ifndef CFG_NO_FLASH
  454. /* move the blob if it is in flash (set relocate) */
  455. if (addr2info ((ulong)fdt_blob) != NULL)
  456. relocate = 1;
  457. #endif
  458. /*
  459. * The blob must be within CFG_BOOTMAPSZ,
  460. * so we flag it to be copied if it is not.
  461. */
  462. if (fdt_blob >= (char *)CFG_BOOTMAPSZ)
  463. relocate = 1;
  464. of_len = be32_to_cpu (fdt_totalsize (fdt));
  465. /* move flattend device tree if needed */
  466. if (relocate) {
  467. int err;
  468. ulong of_start;
  469. /* position on a 4K boundary before the alloc_current */
  470. of_start = lmb_alloc_base(lmb, of_len, 0x1000,
  471. (CFG_BOOTMAPSZ + bootmap_base));
  472. if (of_start == 0) {
  473. puts("device tree - allocation error\n");
  474. goto error;
  475. }
  476. debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  477. (ulong)fdt_blob, (ulong)fdt_blob + of_len - 1,
  478. of_len, of_len);
  479. printf (" Loading Device Tree to %08lx, end %08lx ... ",
  480. of_start, of_start + of_len - 1);
  481. err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
  482. if (err != 0) {
  483. fdt_error ("fdt move failed");
  484. goto error;
  485. }
  486. puts ("OK\n");
  487. *of_flat_tree = (char *)of_start;
  488. } else {
  489. *of_flat_tree = fdt_blob;
  490. lmb_reserve(lmb, (ulong)fdt, of_len);
  491. }
  492. return 0;
  493. error:
  494. return 1;
  495. }
  496. #endif