bootm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. /*
  191. * In boot_ramdisk_high(), it may relocate ramdisk to
  192. * a specified location. And set images->initrd_start &
  193. * images->initrd_end to relocated ramdisk's start/end
  194. * addresses. So use them instead of images->rd_start &
  195. * images->rd_end when possible.
  196. */
  197. if (images->initrd_start && images->initrd_end) {
  198. setup_initrd_tag(gd->bd, images->initrd_start,
  199. images->initrd_end);
  200. } else if (images->rd_start && images->rd_end) {
  201. setup_initrd_tag(gd->bd, images->rd_start,
  202. images->rd_end);
  203. }
  204. }
  205. setup_board_tags(&params);
  206. setup_end_tag(gd->bd);
  207. } else {
  208. printf("FDT and ATAGS support not compiled in - hanging\n");
  209. hang();
  210. }
  211. }
  212. __weak bool armv7_boot_nonsec_default(void)
  213. {
  214. #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
  215. return false;
  216. #else
  217. return true;
  218. #endif
  219. }
  220. #ifdef CONFIG_ARMV7_NONSEC
  221. bool armv7_boot_nonsec(void)
  222. {
  223. char *s = getenv("bootm_boot_mode");
  224. bool nonsec = armv7_boot_nonsec_default();
  225. if (s && !strcmp(s, "sec"))
  226. nonsec = false;
  227. if (s && !strcmp(s, "nonsec"))
  228. nonsec = true;
  229. return nonsec;
  230. }
  231. #endif
  232. /* Subcommand: GO */
  233. static void boot_jump_linux(bootm_headers_t *images, int flag)
  234. {
  235. #ifdef CONFIG_ARM64
  236. void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
  237. void *res2);
  238. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  239. kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
  240. void *res2))images->ep;
  241. debug("## Transferring control to Linux (at address %lx)...\n",
  242. (ulong) kernel_entry);
  243. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  244. announce_and_cleanup(fake);
  245. if (!fake) {
  246. do_nonsec_virt_switch();
  247. kernel_entry(images->ft_addr, NULL, NULL, NULL);
  248. }
  249. #else
  250. unsigned long machid = gd->bd->bi_arch_number;
  251. char *s;
  252. void (*kernel_entry)(int zero, int arch, uint params);
  253. unsigned long r2;
  254. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  255. kernel_entry = (void (*)(int, int, uint))images->ep;
  256. s = getenv("machid");
  257. if (s) {
  258. if (strict_strtoul(s, 16, &machid) < 0) {
  259. debug("strict_strtoul failed!\n");
  260. return;
  261. }
  262. printf("Using machid 0x%lx from environment\n", machid);
  263. }
  264. debug("## Transferring control to Linux (at address %08lx)" \
  265. "...\n", (ulong) kernel_entry);
  266. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  267. announce_and_cleanup(fake);
  268. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
  269. r2 = (unsigned long)images->ft_addr;
  270. else
  271. r2 = gd->bd->bi_boot_params;
  272. if (!fake) {
  273. #ifdef CONFIG_ARMV7_NONSEC
  274. if (armv7_boot_nonsec()) {
  275. armv7_init_nonsec();
  276. secure_ram_addr(_do_nonsec_entry)(kernel_entry,
  277. 0, machid, r2);
  278. } else
  279. #endif
  280. kernel_entry(0, machid, r2);
  281. }
  282. #endif
  283. }
  284. /* Main Entry point for arm bootm implementation
  285. *
  286. * Modeled after the powerpc implementation
  287. * DIFFERENCE: Instead of calling prep and go at the end
  288. * they are called if subcommand is equal 0.
  289. */
  290. int do_bootm_linux(int flag, int argc, char * const argv[],
  291. bootm_headers_t *images)
  292. {
  293. /* No need for those on ARM */
  294. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  295. return -1;
  296. if (flag & BOOTM_STATE_OS_PREP) {
  297. boot_prep_linux(images);
  298. return 0;
  299. }
  300. if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
  301. boot_jump_linux(images, flag);
  302. return 0;
  303. }
  304. boot_prep_linux(images);
  305. boot_jump_linux(images, flag);
  306. return 0;
  307. }
  308. #if defined(CONFIG_BOOTM_VXWORKS)
  309. void boot_prep_vxworks(bootm_headers_t *images)
  310. {
  311. #if defined(CONFIG_OF_LIBFDT)
  312. int off;
  313. if (images->ft_addr) {
  314. off = fdt_path_offset(images->ft_addr, "/memory");
  315. if (off < 0) {
  316. #ifdef CONFIG_ARCH_FIXUP_FDT
  317. if (arch_fixup_fdt(images->ft_addr))
  318. puts("## WARNING: fixup memory failed!\n");
  319. #endif
  320. }
  321. }
  322. #endif
  323. cleanup_before_linux();
  324. }
  325. void boot_jump_vxworks(bootm_headers_t *images)
  326. {
  327. /* ARM VxWorks requires device tree physical address to be passed */
  328. ((void (*)(void *))images->ep)(images->ft_addr);
  329. }
  330. #endif