bootm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* Copyright (C) 2011
  2. * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
  3. * - Added prep subcommand support
  4. * - Reorganized source - modeled after powerpc version
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Marius Groeger <mgroeger@sysgo.de>
  9. *
  10. * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  11. *
  12. * SPDX-License-Identifier: GPL-2.0+
  13. */
  14. #include <common.h>
  15. #include <command.h>
  16. #include <image.h>
  17. #include <u-boot/zlib.h>
  18. #include <asm/byteorder.h>
  19. #include <libfdt.h>
  20. #include <mapmem.h>
  21. #include <fdt_support.h>
  22. #include <asm/bootm.h>
  23. #include <asm/secure.h>
  24. #include <linux/compiler.h>
  25. #include <bootm.h>
  26. #include <vxworks.h>
  27. #ifdef CONFIG_ARMV7_NONSEC
  28. #include <asm/armv7.h>
  29. #endif
  30. DECLARE_GLOBAL_DATA_PTR;
  31. static struct tag *params;
  32. static ulong get_sp(void)
  33. {
  34. ulong ret;
  35. asm("mov %0, sp" : "=r"(ret) : );
  36. return ret;
  37. }
  38. void arch_lmb_reserve(struct lmb *lmb)
  39. {
  40. ulong sp;
  41. /*
  42. * Booting a (Linux) kernel image
  43. *
  44. * Allocate space for command line and board info - the
  45. * address should be as high as possible within the reach of
  46. * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
  47. * memory, which means far enough below the current stack
  48. * pointer.
  49. */
  50. sp = get_sp();
  51. debug("## Current stack ends at 0x%08lx ", sp);
  52. /* adjust sp by 4K to be safe */
  53. sp -= 4096;
  54. lmb_reserve(lmb, sp,
  55. gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
  56. }
  57. /**
  58. * announce_and_cleanup() - Print message and prepare for kernel boot
  59. *
  60. * @fake: non-zero to do everything except actually boot
  61. */
  62. static void announce_and_cleanup(int fake)
  63. {
  64. printf("\nStarting kernel ...%s\n\n", fake ?
  65. "(fake run for tracing)" : "");
  66. bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
  67. #ifdef CONFIG_BOOTSTAGE_FDT
  68. bootstage_fdt_add_report();
  69. #endif
  70. #ifdef CONFIG_BOOTSTAGE_REPORT
  71. bootstage_report();
  72. #endif
  73. #ifdef CONFIG_USB_DEVICE
  74. udc_disconnect();
  75. #endif
  76. cleanup_before_linux();
  77. }
  78. static void setup_start_tag (bd_t *bd)
  79. {
  80. params = (struct tag *)bd->bi_boot_params;
  81. params->hdr.tag = ATAG_CORE;
  82. params->hdr.size = tag_size (tag_core);
  83. params->u.core.flags = 0;
  84. params->u.core.pagesize = 0;
  85. params->u.core.rootdev = 0;
  86. params = tag_next (params);
  87. }
  88. static void setup_memory_tags(bd_t *bd)
  89. {
  90. int i;
  91. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  92. params->hdr.tag = ATAG_MEM;
  93. params->hdr.size = tag_size (tag_mem32);
  94. params->u.mem.start = bd->bi_dram[i].start;
  95. params->u.mem.size = bd->bi_dram[i].size;
  96. params = tag_next (params);
  97. }
  98. }
  99. static void setup_commandline_tag(bd_t *bd, char *commandline)
  100. {
  101. char *p;
  102. if (!commandline)
  103. return;
  104. /* eat leading white space */
  105. for (p = commandline; *p == ' '; p++);
  106. /* skip non-existent command lines so the kernel will still
  107. * use its default command line.
  108. */
  109. if (*p == '\0')
  110. return;
  111. params->hdr.tag = ATAG_CMDLINE;
  112. params->hdr.size =
  113. (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
  114. strcpy (params->u.cmdline.cmdline, p);
  115. params = tag_next (params);
  116. }
  117. static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
  118. {
  119. /* an ATAG_INITRD node tells the kernel where the compressed
  120. * ramdisk can be found. ATAG_RDIMG is a better name, actually.
  121. */
  122. params->hdr.tag = ATAG_INITRD2;
  123. params->hdr.size = tag_size (tag_initrd);
  124. params->u.initrd.start = initrd_start;
  125. params->u.initrd.size = initrd_end - initrd_start;
  126. params = tag_next (params);
  127. }
  128. static void setup_serial_tag(struct tag **tmp)
  129. {
  130. struct tag *params = *tmp;
  131. struct tag_serialnr serialnr;
  132. get_board_serial(&serialnr);
  133. params->hdr.tag = ATAG_SERIAL;
  134. params->hdr.size = tag_size (tag_serialnr);
  135. params->u.serialnr.low = serialnr.low;
  136. params->u.serialnr.high= serialnr.high;
  137. params = tag_next (params);
  138. *tmp = params;
  139. }
  140. static void setup_revision_tag(struct tag **in_params)
  141. {
  142. u32 rev = 0;
  143. rev = get_board_rev();
  144. params->hdr.tag = ATAG_REVISION;
  145. params->hdr.size = tag_size (tag_revision);
  146. params->u.revision.rev = rev;
  147. params = tag_next (params);
  148. }
  149. static void setup_end_tag(bd_t *bd)
  150. {
  151. params->hdr.tag = ATAG_NONE;
  152. params->hdr.size = 0;
  153. }
  154. __weak void setup_board_tags(struct tag **in_params) {}
  155. #ifdef CONFIG_ARM64
  156. static void do_nonsec_virt_switch(void)
  157. {
  158. smp_kick_all_cpus();
  159. dcache_disable(); /* flush cache before swtiching to EL2 */
  160. armv8_switch_to_el2();
  161. #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
  162. armv8_switch_to_el1();
  163. #endif
  164. }
  165. #endif
  166. /* Subcommand: PREP */
  167. static void boot_prep_linux(bootm_headers_t *images)
  168. {
  169. char *commandline = getenv("bootargs");
  170. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
  171. #ifdef CONFIG_OF_LIBFDT
  172. debug("using: FDT\n");
  173. if (image_setup_linux(images)) {
  174. printf("FDT creation failed! hanging...");
  175. hang();
  176. }
  177. #endif
  178. } else if (BOOTM_ENABLE_TAGS) {
  179. debug("using: ATAGS\n");
  180. setup_start_tag(gd->bd);
  181. if (BOOTM_ENABLE_SERIAL_TAG)
  182. setup_serial_tag(&params);
  183. if (BOOTM_ENABLE_CMDLINE_TAG)
  184. setup_commandline_tag(gd->bd, commandline);
  185. if (BOOTM_ENABLE_REVISION_TAG)
  186. setup_revision_tag(&params);
  187. if (BOOTM_ENABLE_MEMORY_TAGS)
  188. setup_memory_tags(gd->bd);
  189. if (BOOTM_ENABLE_INITRD_TAG) {
  190. if (images->rd_start && images->rd_end) {
  191. setup_initrd_tag(gd->bd, images->rd_start,
  192. images->rd_end);
  193. }
  194. }
  195. setup_board_tags(&params);
  196. setup_end_tag(gd->bd);
  197. } else {
  198. printf("FDT and ATAGS support not compiled in - hanging\n");
  199. hang();
  200. }
  201. }
  202. #ifdef CONFIG_ARMV7_NONSEC
  203. bool armv7_boot_nonsec(void)
  204. {
  205. char *s = getenv("bootm_boot_mode");
  206. #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
  207. bool nonsec = false;
  208. #else
  209. bool nonsec = true;
  210. #endif
  211. if (s && !strcmp(s, "sec"))
  212. nonsec = false;
  213. if (s && !strcmp(s, "nonsec"))
  214. nonsec = true;
  215. return nonsec;
  216. }
  217. #endif
  218. /* Subcommand: GO */
  219. static void boot_jump_linux(bootm_headers_t *images, int flag)
  220. {
  221. #ifdef CONFIG_ARM64
  222. void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
  223. void *res2);
  224. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  225. kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
  226. void *res2))images->ep;
  227. debug("## Transferring control to Linux (at address %lx)...\n",
  228. (ulong) kernel_entry);
  229. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  230. announce_and_cleanup(fake);
  231. if (!fake) {
  232. do_nonsec_virt_switch();
  233. kernel_entry(images->ft_addr, NULL, NULL, NULL);
  234. }
  235. #else
  236. unsigned long machid = gd->bd->bi_arch_number;
  237. char *s;
  238. void (*kernel_entry)(int zero, int arch, uint params);
  239. unsigned long r2;
  240. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  241. kernel_entry = (void (*)(int, int, uint))images->ep;
  242. s = getenv("machid");
  243. if (s) {
  244. if (strict_strtoul(s, 16, &machid) < 0) {
  245. debug("strict_strtoul failed!\n");
  246. return;
  247. }
  248. printf("Using machid 0x%lx from environment\n", machid);
  249. }
  250. debug("## Transferring control to Linux (at address %08lx)" \
  251. "...\n", (ulong) kernel_entry);
  252. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  253. announce_and_cleanup(fake);
  254. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
  255. r2 = (unsigned long)images->ft_addr;
  256. else
  257. r2 = gd->bd->bi_boot_params;
  258. if (!fake) {
  259. #ifdef CONFIG_ARMV7_NONSEC
  260. if (armv7_boot_nonsec()) {
  261. armv7_init_nonsec();
  262. secure_ram_addr(_do_nonsec_entry)(kernel_entry,
  263. 0, machid, r2);
  264. } else
  265. #endif
  266. kernel_entry(0, machid, r2);
  267. }
  268. #endif
  269. }
  270. /* Main Entry point for arm bootm implementation
  271. *
  272. * Modeled after the powerpc implementation
  273. * DIFFERENCE: Instead of calling prep and go at the end
  274. * they are called if subcommand is equal 0.
  275. */
  276. int do_bootm_linux(int flag, int argc, char * const argv[],
  277. bootm_headers_t *images)
  278. {
  279. /* No need for those on ARM */
  280. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  281. return -1;
  282. if (flag & BOOTM_STATE_OS_PREP) {
  283. boot_prep_linux(images);
  284. return 0;
  285. }
  286. if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
  287. boot_jump_linux(images, flag);
  288. return 0;
  289. }
  290. boot_prep_linux(images);
  291. boot_jump_linux(images, flag);
  292. return 0;
  293. }
  294. #ifdef CONFIG_CMD_BOOTZ
  295. struct zimage_header {
  296. uint32_t code[9];
  297. uint32_t zi_magic;
  298. uint32_t zi_start;
  299. uint32_t zi_end;
  300. };
  301. #define LINUX_ARM_ZIMAGE_MAGIC 0x016f2818
  302. int bootz_setup(ulong image, ulong *start, ulong *end)
  303. {
  304. struct zimage_header *zi;
  305. zi = (struct zimage_header *)map_sysmem(image, 0);
  306. if (zi->zi_magic != LINUX_ARM_ZIMAGE_MAGIC) {
  307. puts("Bad Linux ARM zImage magic!\n");
  308. return 1;
  309. }
  310. *start = zi->zi_start;
  311. *end = zi->zi_end;
  312. printf("Kernel image @ %#08lx [ %#08lx - %#08lx ]\n", image, *start,
  313. *end);
  314. return 0;
  315. }
  316. #endif /* CONFIG_CMD_BOOTZ */
  317. #if defined(CONFIG_BOOTM_VXWORKS)
  318. void boot_prep_vxworks(bootm_headers_t *images)
  319. {
  320. #if defined(CONFIG_OF_LIBFDT)
  321. int off;
  322. if (images->ft_addr) {
  323. off = fdt_path_offset(images->ft_addr, "/memory");
  324. if (off < 0) {
  325. if (arch_fixup_fdt(images->ft_addr))
  326. puts("## WARNING: fixup memory failed!\n");
  327. }
  328. }
  329. #endif
  330. cleanup_before_linux();
  331. }
  332. void boot_jump_vxworks(bootm_headers_t *images)
  333. {
  334. /* ARM VxWorks requires device tree physical address to be passed */
  335. ((void (*)(void *))images->ep)(images->ft_addr);
  336. }
  337. #endif