bootm.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. }
  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. #ifdef CONFIG_ARM64
  233. __weak void update_os_arch_secondary_cores(uint8_t os_arch)
  234. {
  235. }
  236. #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
  237. static void switch_to_el1(void)
  238. {
  239. if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
  240. (images.os.arch == IH_ARCH_ARM))
  241. armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
  242. (u64)images.ft_addr,
  243. (u64)images.ep,
  244. ES_TO_AARCH32);
  245. else
  246. armv8_switch_to_el1((u64)images.ft_addr, 0, 0,
  247. images.ep,
  248. ES_TO_AARCH64);
  249. }
  250. #endif
  251. #endif
  252. /* Subcommand: GO */
  253. static void boot_jump_linux(bootm_headers_t *images, int flag)
  254. {
  255. #ifdef CONFIG_ARM64
  256. void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
  257. void *res2);
  258. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  259. kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
  260. void *res2))images->ep;
  261. debug("## Transferring control to Linux (at address %lx)...\n",
  262. (ulong) kernel_entry);
  263. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  264. announce_and_cleanup(fake);
  265. if (!fake) {
  266. #ifdef CONFIG_ARMV8_PSCI
  267. armv8_setup_psci();
  268. #endif
  269. do_nonsec_virt_switch();
  270. update_os_arch_secondary_cores(images->os.arch);
  271. #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
  272. armv8_switch_to_el2((u64)images->ft_addr, 0, 0,
  273. (u64)switch_to_el1, ES_TO_AARCH64);
  274. #else
  275. if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
  276. (images->os.arch == IH_ARCH_ARM))
  277. armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
  278. (u64)images->ft_addr,
  279. (u64)images->ep,
  280. ES_TO_AARCH32);
  281. else
  282. armv8_switch_to_el2((u64)images->ft_addr, 0, 0,
  283. images->ep,
  284. ES_TO_AARCH64);
  285. #endif
  286. }
  287. #else
  288. unsigned long machid = gd->bd->bi_arch_number;
  289. char *s;
  290. void (*kernel_entry)(int zero, int arch, uint params);
  291. unsigned long r2;
  292. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  293. kernel_entry = (void (*)(int, int, uint))images->ep;
  294. s = getenv("machid");
  295. if (s) {
  296. if (strict_strtoul(s, 16, &machid) < 0) {
  297. debug("strict_strtoul failed!\n");
  298. return;
  299. }
  300. printf("Using machid 0x%lx from environment\n", machid);
  301. }
  302. debug("## Transferring control to Linux (at address %08lx)" \
  303. "...\n", (ulong) kernel_entry);
  304. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  305. announce_and_cleanup(fake);
  306. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
  307. r2 = (unsigned long)images->ft_addr;
  308. else
  309. r2 = gd->bd->bi_boot_params;
  310. if (!fake) {
  311. #ifdef CONFIG_ARMV7_NONSEC
  312. if (armv7_boot_nonsec()) {
  313. armv7_init_nonsec();
  314. secure_ram_addr(_do_nonsec_entry)(kernel_entry,
  315. 0, machid, r2);
  316. } else
  317. #endif
  318. kernel_entry(0, machid, r2);
  319. }
  320. #endif
  321. }
  322. /* Main Entry point for arm bootm implementation
  323. *
  324. * Modeled after the powerpc implementation
  325. * DIFFERENCE: Instead of calling prep and go at the end
  326. * they are called if subcommand is equal 0.
  327. */
  328. int do_bootm_linux(int flag, int argc, char * const argv[],
  329. bootm_headers_t *images)
  330. {
  331. /* No need for those on ARM */
  332. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  333. return -1;
  334. if (flag & BOOTM_STATE_OS_PREP) {
  335. boot_prep_linux(images);
  336. return 0;
  337. }
  338. if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
  339. boot_jump_linux(images, flag);
  340. return 0;
  341. }
  342. boot_prep_linux(images);
  343. boot_jump_linux(images, flag);
  344. return 0;
  345. }
  346. #if defined(CONFIG_BOOTM_VXWORKS)
  347. void boot_prep_vxworks(bootm_headers_t *images)
  348. {
  349. #if defined(CONFIG_OF_LIBFDT)
  350. int off;
  351. if (images->ft_addr) {
  352. off = fdt_path_offset(images->ft_addr, "/memory");
  353. if (off < 0) {
  354. if (arch_fixup_fdt(images->ft_addr))
  355. puts("## WARNING: fixup memory failed!\n");
  356. }
  357. }
  358. #endif
  359. cleanup_before_linux();
  360. }
  361. void boot_jump_vxworks(bootm_headers_t *images)
  362. {
  363. /* ARM VxWorks requires device tree physical address to be passed */
  364. ((void (*)(void *))images->ep)(images->ft_addr);
  365. }
  366. #endif