bootm.c 9.3 KB

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