bootm.c 14 KB

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