bootm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. #elif defined(CONFIG_OF_FLAT_TREE)
  40. #include <ft_build.h>
  41. #endif
  42. #ifdef CONFIG_LOGBUFFER
  43. #include <logbuff.h>
  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. #if defined(CONFIG_CMD_BDI)
  51. extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  52. #endif
  53. void __attribute__((noinline))
  54. do_bootm_linux(cmd_tbl_t *cmdtp, int flag,
  55. int argc, char *argv[],
  56. image_header_t *hdr,
  57. int verify)
  58. {
  59. ulong initrd_high;
  60. int initrd_copy_to_ram = 1;
  61. ulong initrd_start, initrd_end;
  62. ulong rd_data, rd_len;
  63. image_header_t *rd_hdr;
  64. ulong cmd_start, cmd_end;
  65. char *cmdline;
  66. ulong sp;
  67. char *s;
  68. bd_t *kbd;
  69. void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
  70. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  71. image_header_t *fdt_hdr;
  72. char *of_flat_tree = NULL;
  73. ulong of_data = 0;
  74. #endif
  75. if ((s = getenv ("initrd_high")) != NULL) {
  76. /* a value of "no" or a similar string will act like 0,
  77. * turning the "load high" feature off. This is intentional.
  78. */
  79. initrd_high = simple_strtoul(s, NULL, 16);
  80. if (initrd_high == ~0)
  81. initrd_copy_to_ram = 0;
  82. } else { /* not set, no restrictions to load high */
  83. initrd_high = ~0;
  84. }
  85. #ifdef CONFIG_LOGBUFFER
  86. kbd=gd->bd;
  87. /* Prevent initrd from overwriting logbuffer */
  88. if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
  89. initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
  90. debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
  91. #endif
  92. /*
  93. * Booting a (Linux) kernel image
  94. *
  95. * Allocate space for command line and board info - the
  96. * address should be as high as possible within the reach of
  97. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  98. * memory, which means far enough below the current stack
  99. * pointer.
  100. */
  101. asm( "mr %0,1": "=r"(sp) : );
  102. debug ("## Current stack ends at 0x%08lX ", sp);
  103. sp -= 2048; /* just to be sure */
  104. if (sp > CFG_BOOTMAPSZ)
  105. sp = CFG_BOOTMAPSZ;
  106. sp &= ~0xF;
  107. debug ("=> set upper limit to 0x%08lX\n", sp);
  108. cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
  109. kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
  110. if ((s = getenv("bootargs")) == NULL)
  111. s = "";
  112. strcpy (cmdline, s);
  113. cmd_start = (ulong)&cmdline[0];
  114. cmd_end = cmd_start + strlen(cmdline);
  115. *kbd = *(gd->bd);
  116. #ifdef DEBUG
  117. printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
  118. #if defined(CONFIG_CMD_BDI)
  119. do_bdinfo (NULL, 0, 0, NULL);
  120. #endif
  121. #endif
  122. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  123. /* convert all clock information to MHz */
  124. kbd->bi_intfreq /= 1000000L;
  125. kbd->bi_busfreq /= 1000000L;
  126. #if defined(CONFIG_MPC8220)
  127. kbd->bi_inpfreq /= 1000000L;
  128. kbd->bi_pcifreq /= 1000000L;
  129. kbd->bi_pevfreq /= 1000000L;
  130. kbd->bi_flbfreq /= 1000000L;
  131. kbd->bi_vcofreq /= 1000000L;
  132. #endif
  133. #if defined(CONFIG_CPM2)
  134. kbd->bi_cpmfreq /= 1000000L;
  135. kbd->bi_brgfreq /= 1000000L;
  136. kbd->bi_sccfreq /= 1000000L;
  137. kbd->bi_vco /= 1000000L;
  138. #endif
  139. #if defined(CONFIG_MPC5xxx)
  140. kbd->bi_ipbfreq /= 1000000L;
  141. kbd->bi_pcifreq /= 1000000L;
  142. #endif /* CONFIG_MPC5xxx */
  143. }
  144. kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
  145. /*
  146. * Check if there is an initrd image
  147. */
  148. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  149. /* Look for a '-' which indicates to ignore the ramdisk argument */
  150. if (argc >= 3 && strcmp(argv[2], "-") == 0) {
  151. debug ("Skipping initrd\n");
  152. rd_len = rd_data = 0;
  153. }
  154. else
  155. #endif
  156. if (argc >= 3) {
  157. debug ("Not skipping initrd\n");
  158. show_boot_progress (9);
  159. rd_hdr = (image_header_t *)simple_strtoul (argv[2], NULL, 16);
  160. printf ("## Loading RAMDisk Image at %08lx ...\n", (ulong)rd_hdr);
  161. if (!image_check_magic (rd_hdr)) {
  162. puts ("Bad Magic Number\n");
  163. show_boot_progress (-10);
  164. do_reset (cmdtp, flag, argc, argv);
  165. }
  166. if (!image_check_hcrc (rd_hdr)) {
  167. puts ("Bad Header Checksum\n");
  168. show_boot_progress (-11);
  169. do_reset (cmdtp, flag, argc, argv);
  170. }
  171. show_boot_progress (10);
  172. print_image_hdr (rd_hdr);
  173. if (verify) {
  174. puts (" Verifying Checksum ... ");
  175. if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
  176. puts ("Bad Data CRC\n");
  177. show_boot_progress (-12);
  178. do_reset (cmdtp, flag, argc, argv);
  179. }
  180. puts ("OK\n");
  181. }
  182. show_boot_progress (11);
  183. if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
  184. !image_check_arch (rd_hdr, IH_ARCH_PPC) ||
  185. !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
  186. puts ("No Linux PPC Ramdisk Image\n");
  187. show_boot_progress (-13);
  188. do_reset (cmdtp, flag, argc, argv);
  189. }
  190. rd_data = image_get_data (rd_hdr);
  191. rd_len = image_get_data_size (rd_hdr);
  192. /*
  193. * Now check if we have a multifile image
  194. */
  195. } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
  196. /*
  197. * Get second entry data start address and len
  198. */
  199. image_multi_getimg (hdr, 1, &rd_data, &rd_len);
  200. show_boot_progress (13);
  201. } else {
  202. /*
  203. * No initrd image
  204. */
  205. show_boot_progress (14);
  206. rd_len = rd_data = 0;
  207. }
  208. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  209. if(argc > 3) {
  210. of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
  211. fdt_hdr = (image_header_t *)of_flat_tree;
  212. #if defined(CONFIG_OF_FLAT_TREE)
  213. if (*((ulong *)(of_flat_tree)) == OF_DT_HEADER) {
  214. #elif defined(CONFIG_OF_LIBFDT)
  215. if (fdt_check_header (of_flat_tree) == 0) {
  216. #endif
  217. #ifndef CFG_NO_FLASH
  218. if (addr2info((ulong)of_flat_tree) != NULL)
  219. of_data = (ulong)of_flat_tree;
  220. #endif
  221. } else if (image_check_magic (fdt_hdr)) {
  222. ulong image_start, image_end;
  223. ulong load_start, load_end;
  224. printf ("## Flat Device Tree at %08lX\n", fdt_hdr);
  225. print_image_hdr (fdt_hdr);
  226. image_start = (ulong)fdt_hdr;
  227. image_end = image_get_image_end (fdt_hdr);
  228. load_start = image_get_load (fdt_hdr);
  229. load_end = load_start + image_get_data_size (fdt_hdr);
  230. if ((load_start < image_end) && (load_end > image_start)) {
  231. puts ("ERROR: fdt overwritten - "
  232. "must RESET the board to recover.\n");
  233. do_reset (cmdtp, flag, argc, argv);
  234. }
  235. puts (" Verifying Checksum ... ");
  236. if (!image_check_hcrc (fdt_hdr)) {
  237. puts ("ERROR: fdt header checksum invalid - "
  238. "must RESET the board to recover.\n");
  239. do_reset (cmdtp, flag, argc, argv);
  240. }
  241. if (!image_check_dcrc (fdt_hdr)) {
  242. puts ("ERROR: fdt checksum invalid - "
  243. "must RESET the board to recover.\n");
  244. do_reset (cmdtp, flag, argc, argv);
  245. }
  246. puts ("OK\n");
  247. if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
  248. puts ("ERROR: uImage is not a fdt - "
  249. "must RESET the board to recover.\n");
  250. do_reset (cmdtp, flag, argc, argv);
  251. }
  252. if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
  253. puts ("ERROR: uImage is compressed - "
  254. "must RESET the board to recover.\n");
  255. do_reset (cmdtp, flag, argc, argv);
  256. }
  257. #if defined(CONFIG_OF_FLAT_TREE)
  258. if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
  259. #elif defined(CONFIG_OF_LIBFDT)
  260. if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
  261. #endif
  262. puts ("ERROR: uImage data is not a fdt - "
  263. "must RESET the board to recover.\n");
  264. do_reset (cmdtp, flag, argc, argv);
  265. }
  266. memmove ((void *)image_get_load (fdt_hdr),
  267. (void *)(of_flat_tree + image_get_header_size ()),
  268. image_get_data_size (fdt_hdr));
  269. of_flat_tree = (char *)image_get_load (fdt_hdr);
  270. } else {
  271. puts ("Did not find a flat Flat Device Tree.\n"
  272. "Must RESET the board to recover.\n");
  273. do_reset (cmdtp, flag, argc, argv);
  274. }
  275. printf (" Booting using the fdt at 0x%x\n",
  276. of_flat_tree);
  277. } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
  278. ulong fdt_data, fdt_len;
  279. image_multi_getimg (hdr, 2, &fdt_data, &fdt_len);
  280. if (fdt_len) {
  281. of_flat_tree = (char *)fdt_data;
  282. #ifndef CFG_NO_FLASH
  283. /* move the blob if it is in flash (set of_data to !null) */
  284. if (addr2info ((ulong)of_flat_tree) != NULL)
  285. of_data = (ulong)of_flat_tree;
  286. #endif
  287. #if defined(CONFIG_OF_FLAT_TREE)
  288. if (*((ulong *)(of_flat_tree)) != OF_DT_HEADER) {
  289. #elif defined(CONFIG_OF_LIBFDT)
  290. if (fdt_check_header (of_flat_tree) != 0) {
  291. #endif
  292. puts ("ERROR: image is not a fdt - "
  293. "must RESET the board to recover.\n");
  294. do_reset (cmdtp, flag, argc, argv);
  295. }
  296. #if defined(CONFIG_OF_FLAT_TREE)
  297. if (((struct boot_param_header *)of_flat_tree)->totalsize != fdt_len) {
  298. #elif defined(CONFIG_OF_LIBFDT)
  299. if (be32_to_cpu (fdt_totalsize (of_flat_tree)) != fdt_len) {
  300. #endif
  301. puts ("ERROR: fdt size != image size - "
  302. "must RESET the board to recover.\n");
  303. do_reset (cmdtp, flag, argc, argv);
  304. }
  305. }
  306. }
  307. #endif
  308. if (!rd_data) {
  309. debug ("No initrd\n");
  310. }
  311. if (rd_data) {
  312. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  313. initrd_start = rd_data;
  314. initrd_end = initrd_start + rd_len;
  315. } else {
  316. initrd_start = (ulong)kbd - rd_len;
  317. initrd_start &= ~(4096 - 1); /* align on page */
  318. if (initrd_high) {
  319. ulong nsp;
  320. /*
  321. * the inital ramdisk does not need to be within
  322. * CFG_BOOTMAPSZ as it is not accessed until after
  323. * the mm system is initialised.
  324. *
  325. * do the stack bottom calculation again and see if
  326. * the initrd will fit just below the monitor stack
  327. * bottom without overwriting the area allocated
  328. * above for command line args and board info.
  329. */
  330. asm( "mr %0,1": "=r"(nsp) : );
  331. nsp -= 2048; /* just to be sure */
  332. nsp &= ~0xF;
  333. if (nsp > initrd_high) /* limit as specified */
  334. nsp = initrd_high;
  335. nsp -= rd_len;
  336. nsp &= ~(4096 - 1); /* align on page */
  337. if (nsp >= sp)
  338. initrd_start = nsp;
  339. }
  340. show_boot_progress (12);
  341. debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  342. rd_data, rd_data + rd_len - 1, rd_len, rd_len);
  343. initrd_end = initrd_start + rd_len;
  344. printf (" Loading Ramdisk to %08lx, end %08lx ... ",
  345. initrd_start, initrd_end);
  346. memmove_wd((void *)initrd_start,
  347. (void *)rd_data, rd_len, CHUNKSZ);
  348. puts ("OK\n");
  349. }
  350. } else {
  351. initrd_start = 0;
  352. initrd_end = 0;
  353. }
  354. #if defined(CONFIG_OF_LIBFDT)
  355. #ifdef CFG_BOOTMAPSZ
  356. /*
  357. * The blob must be within CFG_BOOTMAPSZ,
  358. * so we flag it to be copied if it is not.
  359. */
  360. if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
  361. of_data = (ulong)of_flat_tree;
  362. #endif
  363. /* move of_flat_tree if needed */
  364. if (of_data) {
  365. int err;
  366. ulong of_start, of_len;
  367. of_len = be32_to_cpu(fdt_totalsize(of_data));
  368. /* position on a 4K boundary before the kbd */
  369. of_start = (ulong)kbd - of_len;
  370. of_start &= ~(4096 - 1); /* align on page */
  371. debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  372. of_data, of_data + of_len - 1, of_len, of_len);
  373. of_flat_tree = (char *)of_start;
  374. printf (" Loading Device Tree to %08lx, end %08lx ... ",
  375. of_start, of_start + of_len - 1);
  376. err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
  377. if (err != 0) {
  378. puts ("ERROR: fdt move failed - "
  379. "must RESET the board to recover.\n");
  380. do_reset (cmdtp, flag, argc, argv);
  381. }
  382. puts ("OK\n");
  383. }
  384. /*
  385. * Add the chosen node if it doesn't exist, add the env and bd_t
  386. * if the user wants it (the logic is in the subroutines).
  387. */
  388. if (of_flat_tree) {
  389. if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
  390. puts ("ERROR: /chosen node create failed - "
  391. "must RESET the board to recover.\n");
  392. do_reset (cmdtp, flag, argc, argv);
  393. }
  394. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  395. if (fdt_env(of_flat_tree) < 0) {
  396. puts ("ERROR: /u-boot-env node create failed - "
  397. "must RESET the board to recover.\n");
  398. do_reset (cmdtp, flag, argc, argv);
  399. }
  400. #endif
  401. #ifdef CONFIG_OF_HAS_BD_T
  402. if (fdt_bd_t(of_flat_tree) < 0) {
  403. puts ("ERROR: /bd_t node create failed - "
  404. "must RESET the board to recover.\n");
  405. do_reset (cmdtp, flag, argc, argv);
  406. }
  407. #endif
  408. #ifdef CONFIG_OF_BOARD_SETUP
  409. /* Call the board-specific fixup routine */
  410. ft_board_setup(of_flat_tree, gd->bd);
  411. #endif
  412. }
  413. #elif defined(CONFIG_OF_FLAT_TREE)
  414. #ifdef CFG_BOOTMAPSZ
  415. /*
  416. * The blob must be within CFG_BOOTMAPSZ,
  417. * so we flag it to be copied if it is not.
  418. */
  419. if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
  420. of_data = (ulong)of_flat_tree;
  421. #endif
  422. /* move of_flat_tree if needed */
  423. if (of_data) {
  424. ulong of_start, of_len;
  425. of_len = ((struct boot_param_header *)of_data)->totalsize;
  426. /* provide extra 8k pad */
  427. of_start = (ulong)kbd - of_len - 8192;
  428. of_start &= ~(4096 - 1); /* align on page */
  429. debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  430. of_data, of_data + of_len - 1, of_len, of_len);
  431. of_flat_tree = (char *)of_start;
  432. printf (" Loading Device Tree to %08lx, end %08lx ... ",
  433. of_start, of_start + of_len - 1);
  434. memmove ((void *)of_start, (void *)of_data, of_len);
  435. puts ("OK\n");
  436. }
  437. /*
  438. * Create the /chosen node and modify the blob with board specific
  439. * values as needed.
  440. */
  441. ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
  442. /* ft_dump_blob(of_flat_tree); */
  443. #endif /* #if defined(CONFIG_OF_LIBFDT) #elif defined(CONFIG_OF_FLAT_TREE) */
  444. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  445. (ulong)kernel);
  446. show_boot_progress (15);
  447. #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  448. unlock_ram_in_cache();
  449. #endif
  450. #if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
  451. if (of_flat_tree) { /* device tree; boot new style */
  452. /*
  453. * Linux Kernel Parameters (passing device tree):
  454. * r3: pointer to the fdt, followed by the board info data
  455. * r4: physical pointer to the kernel itself
  456. * r5: NULL
  457. * r6: NULL
  458. * r7: NULL
  459. */
  460. (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
  461. /* does not return */
  462. }
  463. #endif
  464. /*
  465. * Linux Kernel Parameters (passing board info data):
  466. * r3: ptr to board info data
  467. * r4: initrd_start or 0 if no initrd
  468. * r5: initrd_end - unused if r4 is 0
  469. * r6: Start of command line string
  470. * r7: End of command line string
  471. */
  472. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  473. /* does not return */
  474. }