bootm.c 14 KB

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