bootm.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. __weak void board_quiesce_devices(void)
  58. {
  59. }
  60. /**
  61. * announce_and_cleanup() - Print message and prepare for kernel boot
  62. *
  63. * @fake: non-zero to do everything except actually boot
  64. */
  65. static void announce_and_cleanup(int fake)
  66. {
  67. printf("\nStarting kernel ...%s\n\n", fake ?
  68. "(fake run for tracing)" : "");
  69. bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
  70. #ifdef CONFIG_BOOTSTAGE_FDT
  71. bootstage_fdt_add_report();
  72. #endif
  73. #ifdef CONFIG_BOOTSTAGE_REPORT
  74. bootstage_report();
  75. #endif
  76. #ifdef CONFIG_USB_DEVICE
  77. udc_disconnect();
  78. #endif
  79. board_quiesce_devices();
  80. cleanup_before_linux();
  81. }
  82. static void setup_start_tag (bd_t *bd)
  83. {
  84. params = (struct tag *)bd->bi_boot_params;
  85. params->hdr.tag = ATAG_CORE;
  86. params->hdr.size = tag_size (tag_core);
  87. params->u.core.flags = 0;
  88. params->u.core.pagesize = 0;
  89. params->u.core.rootdev = 0;
  90. params = tag_next (params);
  91. }
  92. static void setup_memory_tags(bd_t *bd)
  93. {
  94. int i;
  95. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  96. params->hdr.tag = ATAG_MEM;
  97. params->hdr.size = tag_size (tag_mem32);
  98. params->u.mem.start = bd->bi_dram[i].start;
  99. params->u.mem.size = bd->bi_dram[i].size;
  100. params = tag_next (params);
  101. }
  102. }
  103. static void setup_commandline_tag(bd_t *bd, char *commandline)
  104. {
  105. char *p;
  106. if (!commandline)
  107. return;
  108. /* eat leading white space */
  109. for (p = commandline; *p == ' '; p++);
  110. /* skip non-existent command lines so the kernel will still
  111. * use its default command line.
  112. */
  113. if (*p == '\0')
  114. return;
  115. params->hdr.tag = ATAG_CMDLINE;
  116. params->hdr.size =
  117. (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
  118. strcpy (params->u.cmdline.cmdline, p);
  119. params = tag_next (params);
  120. }
  121. static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
  122. {
  123. /* an ATAG_INITRD node tells the kernel where the compressed
  124. * ramdisk can be found. ATAG_RDIMG is a better name, actually.
  125. */
  126. params->hdr.tag = ATAG_INITRD2;
  127. params->hdr.size = tag_size (tag_initrd);
  128. params->u.initrd.start = initrd_start;
  129. params->u.initrd.size = initrd_end - initrd_start;
  130. params = tag_next (params);
  131. }
  132. static void setup_serial_tag(struct tag **tmp)
  133. {
  134. struct tag *params = *tmp;
  135. struct tag_serialnr serialnr;
  136. get_board_serial(&serialnr);
  137. params->hdr.tag = ATAG_SERIAL;
  138. params->hdr.size = tag_size (tag_serialnr);
  139. params->u.serialnr.low = serialnr.low;
  140. params->u.serialnr.high= serialnr.high;
  141. params = tag_next (params);
  142. *tmp = params;
  143. }
  144. static void setup_revision_tag(struct tag **in_params)
  145. {
  146. u32 rev = 0;
  147. rev = get_board_rev();
  148. params->hdr.tag = ATAG_REVISION;
  149. params->hdr.size = tag_size (tag_revision);
  150. params->u.revision.rev = rev;
  151. params = tag_next (params);
  152. }
  153. static void setup_end_tag(bd_t *bd)
  154. {
  155. params->hdr.tag = ATAG_NONE;
  156. params->hdr.size = 0;
  157. }
  158. __weak void setup_board_tags(struct tag **in_params) {}
  159. #ifdef CONFIG_ARM64
  160. static void do_nonsec_virt_switch(void)
  161. {
  162. smp_kick_all_cpus();
  163. dcache_disable(); /* flush cache before swtiching to EL2 */
  164. armv8_switch_to_el2();
  165. #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
  166. armv8_switch_to_el1();
  167. #endif
  168. }
  169. #endif
  170. /* Subcommand: PREP */
  171. static void boot_prep_linux(bootm_headers_t *images)
  172. {
  173. char *commandline = getenv("bootargs");
  174. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
  175. #ifdef CONFIG_OF_LIBFDT
  176. debug("using: FDT\n");
  177. if (image_setup_linux(images)) {
  178. printf("FDT creation failed! hanging...");
  179. hang();
  180. }
  181. #endif
  182. } else if (BOOTM_ENABLE_TAGS) {
  183. debug("using: ATAGS\n");
  184. setup_start_tag(gd->bd);
  185. if (BOOTM_ENABLE_SERIAL_TAG)
  186. setup_serial_tag(&params);
  187. if (BOOTM_ENABLE_CMDLINE_TAG)
  188. setup_commandline_tag(gd->bd, commandline);
  189. if (BOOTM_ENABLE_REVISION_TAG)
  190. setup_revision_tag(&params);
  191. if (BOOTM_ENABLE_MEMORY_TAGS)
  192. setup_memory_tags(gd->bd);
  193. if (BOOTM_ENABLE_INITRD_TAG) {
  194. /*
  195. * In boot_ramdisk_high(), it may relocate ramdisk to
  196. * a specified location. And set images->initrd_start &
  197. * images->initrd_end to relocated ramdisk's start/end
  198. * addresses. So use them instead of images->rd_start &
  199. * images->rd_end when possible.
  200. */
  201. if (images->initrd_start && images->initrd_end) {
  202. setup_initrd_tag(gd->bd, images->initrd_start,
  203. images->initrd_end);
  204. } else if (images->rd_start && images->rd_end) {
  205. setup_initrd_tag(gd->bd, images->rd_start,
  206. images->rd_end);
  207. }
  208. }
  209. setup_board_tags(&params);
  210. setup_end_tag(gd->bd);
  211. } else {
  212. printf("FDT and ATAGS support not compiled in - hanging\n");
  213. hang();
  214. }
  215. }
  216. __weak bool armv7_boot_nonsec_default(void)
  217. {
  218. #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
  219. return false;
  220. #else
  221. return true;
  222. #endif
  223. }
  224. #ifdef CONFIG_ARMV7_NONSEC
  225. bool armv7_boot_nonsec(void)
  226. {
  227. char *s = getenv("bootm_boot_mode");
  228. bool nonsec = armv7_boot_nonsec_default();
  229. if (s && !strcmp(s, "sec"))
  230. nonsec = false;
  231. if (s && !strcmp(s, "nonsec"))
  232. nonsec = true;
  233. return nonsec;
  234. }
  235. #endif
  236. /* Subcommand: GO */
  237. static void boot_jump_linux(bootm_headers_t *images, int flag)
  238. {
  239. #ifdef CONFIG_ARM64
  240. void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
  241. void *res2);
  242. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  243. kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
  244. void *res2))images->ep;
  245. debug("## Transferring control to Linux (at address %lx)...\n",
  246. (ulong) kernel_entry);
  247. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  248. announce_and_cleanup(fake);
  249. if (!fake) {
  250. do_nonsec_virt_switch();
  251. kernel_entry(images->ft_addr, NULL, NULL, NULL);
  252. }
  253. #else
  254. unsigned long machid = gd->bd->bi_arch_number;
  255. char *s;
  256. void (*kernel_entry)(int zero, int arch, uint params);
  257. unsigned long r2;
  258. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  259. kernel_entry = (void (*)(int, int, uint))images->ep;
  260. s = getenv("machid");
  261. if (s) {
  262. if (strict_strtoul(s, 16, &machid) < 0) {
  263. debug("strict_strtoul failed!\n");
  264. return;
  265. }
  266. printf("Using machid 0x%lx from environment\n", machid);
  267. }
  268. debug("## Transferring control to Linux (at address %08lx)" \
  269. "...\n", (ulong) kernel_entry);
  270. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  271. announce_and_cleanup(fake);
  272. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
  273. r2 = (unsigned long)images->ft_addr;
  274. else
  275. r2 = gd->bd->bi_boot_params;
  276. if (!fake) {
  277. #ifdef CONFIG_ARMV7_NONSEC
  278. if (armv7_boot_nonsec()) {
  279. armv7_init_nonsec();
  280. secure_ram_addr(_do_nonsec_entry)(kernel_entry,
  281. 0, machid, r2);
  282. } else
  283. #endif
  284. kernel_entry(0, machid, r2);
  285. }
  286. #endif
  287. }
  288. /* Main Entry point for arm bootm implementation
  289. *
  290. * Modeled after the powerpc implementation
  291. * DIFFERENCE: Instead of calling prep and go at the end
  292. * they are called if subcommand is equal 0.
  293. */
  294. int do_bootm_linux(int flag, int argc, char * const argv[],
  295. bootm_headers_t *images)
  296. {
  297. /* No need for those on ARM */
  298. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  299. return -1;
  300. if (flag & BOOTM_STATE_OS_PREP) {
  301. boot_prep_linux(images);
  302. return 0;
  303. }
  304. if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
  305. boot_jump_linux(images, flag);
  306. return 0;
  307. }
  308. boot_prep_linux(images);
  309. boot_jump_linux(images, flag);
  310. return 0;
  311. }
  312. #if defined(CONFIG_BOOTM_VXWORKS)
  313. void boot_prep_vxworks(bootm_headers_t *images)
  314. {
  315. #if defined(CONFIG_OF_LIBFDT)
  316. int off;
  317. if (images->ft_addr) {
  318. off = fdt_path_offset(images->ft_addr, "/memory");
  319. if (off < 0) {
  320. #ifdef CONFIG_ARCH_FIXUP_FDT
  321. if (arch_fixup_fdt(images->ft_addr))
  322. puts("## WARNING: fixup memory failed!\n");
  323. #endif
  324. }
  325. }
  326. #endif
  327. cleanup_before_linux();
  328. }
  329. void boot_jump_vxworks(bootm_headers_t *images)
  330. {
  331. /* ARM VxWorks requires device tree physical address to be passed */
  332. ((void (*)(void *))images->ep)(images->ft_addr);
  333. }
  334. #endif