bootm.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 ulong get_fdt (ulong alloc_current, cmd_tbl_t *cmdtp, int flag,
  41. int argc, char *argv[],
  42. image_header_t *hdr, char **of_flat_tree);
  43. #endif
  44. #ifdef CFG_INIT_RAM_LOCK
  45. #include <asm/cache.h>
  46. #endif
  47. DECLARE_GLOBAL_DATA_PTR;
  48. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  49. static ulong get_sp (void);
  50. static void set_clocks_in_mhz (bd_t *kbd);
  51. void __attribute__((noinline))
  52. do_bootm_linux(cmd_tbl_t *cmdtp, int flag,
  53. int argc, char *argv[],
  54. image_header_t *hdr,
  55. int verify)
  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. void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
  63. #if defined(CONFIG_OF_LIBFDT)
  64. char *of_flat_tree;
  65. #endif
  66. /*
  67. * Booting a (Linux) kernel image
  68. *
  69. * Allocate space for command line and board info - the
  70. * address should be as high as possible within the reach of
  71. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  72. * memory, which means far enough below the current stack
  73. * pointer.
  74. */
  75. sp = get_sp();
  76. debug ("## Current stack ends at 0x%08lx ", sp);
  77. alloc_current = sp_limit = get_boot_sp_limit(sp);
  78. debug ("=> set upper limit to 0x%08lx\n", sp_limit);
  79. /* allocate space and init command line */
  80. alloc_current = get_boot_cmdline (alloc_current, &cmd_start, &cmd_end);
  81. /* allocate space for kernel copy of board info */
  82. alloc_current = get_boot_kbd (alloc_current, &kbd);
  83. set_clocks_in_mhz(kbd);
  84. /* find kernel */
  85. kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
  86. /* find ramdisk */
  87. get_ramdisk (cmdtp, flag, argc, argv, hdr, verify,
  88. IH_ARCH_PPC, &rd_data_start, &rd_data_end);
  89. rd_len = rd_data_end - rd_data_start;
  90. alloc_current = ramdisk_high (alloc_current, rd_data_start, rd_len,
  91. kbd, sp_limit, get_sp (),
  92. &initrd_start, &initrd_end);
  93. #if defined(CONFIG_OF_LIBFDT)
  94. /* find flattened device tree */
  95. alloc_current = get_fdt (alloc_current,
  96. cmdtp, flag, argc, argv, hdr, &of_flat_tree);
  97. /*
  98. * Add the chosen node if it doesn't exist, add the env and bd_t
  99. * if the user wants it (the logic is in the subroutines).
  100. */
  101. if (of_flat_tree) {
  102. if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
  103. fdt_error ("/chosen node create failed");
  104. do_reset (cmdtp, flag, argc, argv);
  105. }
  106. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  107. if (fdt_env(of_flat_tree) < 0) {
  108. fdt_error ("/u-boot-env node create failed");
  109. do_reset (cmdtp, flag, argc, argv);
  110. }
  111. #endif
  112. #ifdef CONFIG_OF_HAS_BD_T
  113. if (fdt_bd_t(of_flat_tree) < 0) {
  114. fdt_error ("/bd_t node create failed");
  115. do_reset (cmdtp, flag, argc, argv);
  116. }
  117. #endif
  118. #ifdef CONFIG_OF_BOARD_SETUP
  119. /* Call the board-specific fixup routine */
  120. ft_board_setup(of_flat_tree, gd->bd);
  121. #endif
  122. }
  123. #endif /* CONFIG_OF_LIBFDT */
  124. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  125. (ulong)kernel);
  126. show_boot_progress (15);
  127. #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  128. unlock_ram_in_cache();
  129. #endif
  130. #if defined(CONFIG_OF_LIBFDT)
  131. if (of_flat_tree) { /* device tree; boot new style */
  132. /*
  133. * Linux Kernel Parameters (passing device tree):
  134. * r3: pointer to the fdt, followed by the board info data
  135. * r4: physical pointer to the kernel itself
  136. * r5: NULL
  137. * r6: NULL
  138. * r7: NULL
  139. */
  140. (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
  141. /* does not return */
  142. }
  143. #endif
  144. /*
  145. * Linux Kernel Parameters (passing board info data):
  146. * r3: ptr to board info data
  147. * r4: initrd_start or 0 if no initrd
  148. * r5: initrd_end - unused if r4 is 0
  149. * r6: Start of command line string
  150. * r7: End of command line string
  151. */
  152. (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
  153. /* does not return */
  154. }
  155. static ulong get_sp (void)
  156. {
  157. ulong sp;
  158. asm( "mr %0,1": "=r"(sp) : );
  159. return sp;
  160. }
  161. static void set_clocks_in_mhz (bd_t *kbd)
  162. {
  163. char *s;
  164. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  165. /* convert all clock information to MHz */
  166. kbd->bi_intfreq /= 1000000L;
  167. kbd->bi_busfreq /= 1000000L;
  168. #if defined(CONFIG_MPC8220)
  169. kbd->bi_inpfreq /= 1000000L;
  170. kbd->bi_pcifreq /= 1000000L;
  171. kbd->bi_pevfreq /= 1000000L;
  172. kbd->bi_flbfreq /= 1000000L;
  173. kbd->bi_vcofreq /= 1000000L;
  174. #endif
  175. #if defined(CONFIG_CPM2)
  176. kbd->bi_cpmfreq /= 1000000L;
  177. kbd->bi_brgfreq /= 1000000L;
  178. kbd->bi_sccfreq /= 1000000L;
  179. kbd->bi_vco /= 1000000L;
  180. #endif
  181. #if defined(CONFIG_MPC5xxx)
  182. kbd->bi_ipbfreq /= 1000000L;
  183. kbd->bi_pcifreq /= 1000000L;
  184. #endif /* CONFIG_MPC5xxx */
  185. }
  186. }
  187. #if defined(CONFIG_OF_LIBFDT)
  188. static void fdt_error (const char *msg)
  189. {
  190. puts ("ERROR: ");
  191. puts (msg);
  192. puts (" - must RESET the board to recover.\n");
  193. }
  194. static ulong get_fdt (ulong alloc_current,
  195. cmd_tbl_t *cmdtp, int flag,
  196. int argc, char *argv[],
  197. image_header_t *hdr, char **of_flat_tree)
  198. {
  199. image_header_t *fdt_hdr;
  200. ulong fdt_relocate = 0;
  201. char *fdt = NULL;
  202. ulong new_alloc_current;
  203. if(argc > 3) {
  204. fdt = (char *)simple_strtoul (argv[3], NULL, 16);
  205. debug ("## Checking for 'FDT'/'FDT image' at %08lx\n", fdt);
  206. /* copy from dataflash if needed */
  207. fdt = (char *)gen_get_image ((ulong)fdt);
  208. fdt_hdr = (image_header_t *)fdt;
  209. if (fdt_check_header (fdt) == 0) {
  210. printf ("## Flattened Device Tree blob at %08lx\n", fdt);
  211. #ifndef CFG_NO_FLASH
  212. if (addr2info ((ulong)fdt) != NULL)
  213. fdt_relocate = 1;
  214. #endif
  215. } else if (image_check_magic (fdt_hdr)) {
  216. ulong image_start, image_end;
  217. ulong load_start, load_end;
  218. printf ("## Flattened Device Tree Image at %08lx\n",
  219. fdt_hdr);
  220. print_image_hdr (fdt_hdr);
  221. image_start = (ulong)fdt_hdr;
  222. image_end = image_get_image_end (fdt_hdr);
  223. load_start = image_get_load (fdt_hdr);
  224. load_end = load_start + image_get_data_size (fdt_hdr);
  225. if ((load_start < image_end) && (load_end > image_start)) {
  226. fdt_error ("fdt overwritten");
  227. do_reset (cmdtp, flag, argc, argv);
  228. }
  229. puts (" Verifying Checksum ... ");
  230. if (!image_check_hcrc (fdt_hdr)) {
  231. fdt_error ("fdt header checksum invalid");
  232. do_reset (cmdtp, flag, argc, argv);
  233. }
  234. if (!image_check_dcrc (fdt_hdr)) {
  235. fdt_error ("fdt checksum invalid");
  236. do_reset (cmdtp, flag, argc, argv);
  237. }
  238. puts ("OK\n");
  239. if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
  240. fdt_error ("uImage is not a fdt");
  241. do_reset (cmdtp, flag, argc, argv);
  242. }
  243. if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
  244. fdt_error ("uImage is compressed");
  245. do_reset (cmdtp, flag, argc, argv);
  246. }
  247. if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
  248. fdt_error ("uImage data is not a fdt");
  249. do_reset (cmdtp, flag, argc, argv);
  250. }
  251. memmove ((void *)image_get_load (fdt_hdr),
  252. (void *)image_get_data (fdt_hdr),
  253. image_get_data_size (fdt_hdr));
  254. fdt = (char *)image_get_load (fdt_hdr);
  255. } else {
  256. fdt_error ("Did not find a Flattened Device Tree");
  257. do_reset (cmdtp, flag, argc, argv);
  258. }
  259. printf (" Booting using the fdt at 0x%x\n",
  260. fdt);
  261. } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
  262. ulong fdt_data, fdt_len;
  263. printf ("## Flattened Device Tree from multi "
  264. "component Image at %08lX\n", (ulong)hdr);
  265. image_multi_getimg (hdr, 2, &fdt_data, &fdt_len);
  266. if (fdt_len) {
  267. fdt = (char *)fdt_data;
  268. printf (" Booting using the fdt at 0x%x\n", fdt);
  269. #ifndef CFG_NO_FLASH
  270. /* move the blob if it is in flash (set of_relocte) */
  271. if (addr2info ((ulong)fdt) != NULL)
  272. fdt_relocate = 1;
  273. #endif
  274. if (fdt_check_header (fdt) != 0) {
  275. fdt_error ("image is not a fdt");
  276. do_reset (cmdtp, flag, argc, argv);
  277. }
  278. if (be32_to_cpu (fdt_totalsize (fdt)) != fdt_len) {
  279. fdt_error ("fdt size != image size");
  280. do_reset (cmdtp, flag, argc, argv);
  281. }
  282. } else {
  283. debug (" Did not find a Flattened Device Tree");
  284. }
  285. }
  286. #ifdef CFG_BOOTMAPSZ
  287. /*
  288. * The blob must be within CFG_BOOTMAPSZ,
  289. * so we flag it to be copied if it is not.
  290. */
  291. if (fdt >= (char *)CFG_BOOTMAPSZ)
  292. fdt_relocate = 1;
  293. #endif
  294. /* move flattend device tree if needed */
  295. if (fdt_relocate) {
  296. int err;
  297. ulong of_start, of_len;
  298. of_len = be32_to_cpu (fdt_totalsize (fdt));
  299. /* position on a 4K boundary before the alloc_current */
  300. of_start = alloc_current - of_len;
  301. of_start &= ~(4096 - 1); /* align on page */
  302. debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
  303. (ulong)fdt, (ulong)fdt + of_len - 1,
  304. of_len, of_len);
  305. printf (" Loading Device Tree to %08lx, end %08lx ... ",
  306. of_start, of_start + of_len - 1);
  307. err = fdt_open_into (fdt, (void *)of_start, of_len);
  308. if (err != 0) {
  309. fdt_error ("fdt move failed");
  310. do_reset (cmdtp, flag, argc, argv);
  311. }
  312. puts ("OK\n");
  313. *of_flat_tree = (char *)of_start;
  314. new_alloc_current = of_start;
  315. } else {
  316. *of_flat_tree = fdt;
  317. new_alloc_current = alloc_current;
  318. }
  319. return new_alloc_current;
  320. }
  321. #endif