bootm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 <dm.h>
  17. #include <dm/root.h>
  18. #include <image.h>
  19. #include <u-boot/zlib.h>
  20. #include <asm/byteorder.h>
  21. #include <libfdt.h>
  22. #include <mapmem.h>
  23. #include <fdt_support.h>
  24. #include <asm/bootm.h>
  25. #include <asm/secure.h>
  26. #include <linux/compiler.h>
  27. #include <bootm.h>
  28. #include <vxworks.h>
  29. #ifdef CONFIG_ARMV7_NONSEC
  30. #include <asm/armv7.h>
  31. #endif
  32. #include <asm/setup.h>
  33. DECLARE_GLOBAL_DATA_PTR;
  34. static struct tag *params;
  35. static ulong get_sp(void)
  36. {
  37. ulong ret;
  38. asm("mov %0, sp" : "=r"(ret) : );
  39. return ret;
  40. }
  41. void arch_lmb_reserve(struct lmb *lmb)
  42. {
  43. ulong sp;
  44. /*
  45. * Booting a (Linux) kernel image
  46. *
  47. * Allocate space for command line and board info - the
  48. * address should be as high as possible within the reach of
  49. * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
  50. * memory, which means far enough below the current stack
  51. * pointer.
  52. */
  53. sp = get_sp();
  54. debug("## Current stack ends at 0x%08lx ", sp);
  55. /* adjust sp by 4K to be safe */
  56. sp -= 4096;
  57. lmb_reserve(lmb, sp,
  58. gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
  59. }
  60. __weak void board_quiesce_devices(void)
  61. {
  62. }
  63. /**
  64. * announce_and_cleanup() - Print message and prepare for kernel boot
  65. *
  66. * @fake: non-zero to do everything except actually boot
  67. */
  68. static void announce_and_cleanup(int fake)
  69. {
  70. printf("\nStarting kernel ...%s\n\n", fake ?
  71. "(fake run for tracing)" : "");
  72. bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
  73. #ifdef CONFIG_BOOTSTAGE_FDT
  74. bootstage_fdt_add_report();
  75. #endif
  76. #ifdef CONFIG_BOOTSTAGE_REPORT
  77. bootstage_report();
  78. #endif
  79. #ifdef CONFIG_USB_DEVICE
  80. udc_disconnect();
  81. #endif
  82. board_quiesce_devices();
  83. /*
  84. * Call remove function of all devices with a removal flag set.
  85. * This may be useful for last-stage operations, like cancelling
  86. * of DMA operation or releasing device internal buffers.
  87. */
  88. dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
  89. cleanup_before_linux();
  90. }
  91. static void setup_start_tag (bd_t *bd)
  92. {
  93. params = (struct tag *)bd->bi_boot_params;
  94. params->hdr.tag = ATAG_CORE;
  95. params->hdr.size = tag_size (tag_core);
  96. params->u.core.flags = 0;
  97. params->u.core.pagesize = 0;
  98. params->u.core.rootdev = 0;
  99. params = tag_next (params);
  100. }
  101. static void setup_memory_tags(bd_t *bd)
  102. {
  103. int i;
  104. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  105. params->hdr.tag = ATAG_MEM;
  106. params->hdr.size = tag_size (tag_mem32);
  107. params->u.mem.start = bd->bi_dram[i].start;
  108. params->u.mem.size = bd->bi_dram[i].size;
  109. params = tag_next (params);
  110. }
  111. }
  112. static void setup_commandline_tag(bd_t *bd, char *commandline)
  113. {
  114. char *p;
  115. if (!commandline)
  116. return;
  117. /* eat leading white space */
  118. for (p = commandline; *p == ' '; p++);
  119. /* skip non-existent command lines so the kernel will still
  120. * use its default command line.
  121. */
  122. if (*p == '\0')
  123. return;
  124. params->hdr.tag = ATAG_CMDLINE;
  125. params->hdr.size =
  126. (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
  127. strcpy (params->u.cmdline.cmdline, p);
  128. params = tag_next (params);
  129. }
  130. static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
  131. {
  132. /* an ATAG_INITRD node tells the kernel where the compressed
  133. * ramdisk can be found. ATAG_RDIMG is a better name, actually.
  134. */
  135. params->hdr.tag = ATAG_INITRD2;
  136. params->hdr.size = tag_size (tag_initrd);
  137. params->u.initrd.start = initrd_start;
  138. params->u.initrd.size = initrd_end - initrd_start;
  139. params = tag_next (params);
  140. }
  141. static void setup_serial_tag(struct tag **tmp)
  142. {
  143. struct tag *params = *tmp;
  144. struct tag_serialnr serialnr;
  145. get_board_serial(&serialnr);
  146. params->hdr.tag = ATAG_SERIAL;
  147. params->hdr.size = tag_size (tag_serialnr);
  148. params->u.serialnr.low = serialnr.low;
  149. params->u.serialnr.high= serialnr.high;
  150. params = tag_next (params);
  151. *tmp = params;
  152. }
  153. static void setup_revision_tag(struct tag **in_params)
  154. {
  155. u32 rev = 0;
  156. rev = get_board_rev();
  157. params->hdr.tag = ATAG_REVISION;
  158. params->hdr.size = tag_size (tag_revision);
  159. params->u.revision.rev = rev;
  160. params = tag_next (params);
  161. }
  162. static void setup_end_tag(bd_t *bd)
  163. {
  164. params->hdr.tag = ATAG_NONE;
  165. params->hdr.size = 0;
  166. }
  167. __weak void setup_board_tags(struct tag **in_params) {}
  168. #ifdef CONFIG_ARM64
  169. static void do_nonsec_virt_switch(void)
  170. {
  171. smp_kick_all_cpus();
  172. dcache_disable(); /* flush cache before swtiching to EL2 */
  173. }
  174. #endif
  175. /* Subcommand: PREP */
  176. static void boot_prep_linux(bootm_headers_t *images)
  177. {
  178. char *commandline = getenv("bootargs");
  179. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
  180. #ifdef CONFIG_OF_LIBFDT
  181. debug("using: FDT\n");
  182. if (image_setup_linux(images)) {
  183. printf("FDT creation failed! hanging...");
  184. hang();
  185. }
  186. #endif
  187. } else if (BOOTM_ENABLE_TAGS) {
  188. debug("using: ATAGS\n");
  189. setup_start_tag(gd->bd);
  190. if (BOOTM_ENABLE_SERIAL_TAG)
  191. setup_serial_tag(&params);
  192. if (BOOTM_ENABLE_CMDLINE_TAG)
  193. setup_commandline_tag(gd->bd, commandline);
  194. if (BOOTM_ENABLE_REVISION_TAG)
  195. setup_revision_tag(&params);
  196. if (BOOTM_ENABLE_MEMORY_TAGS)
  197. setup_memory_tags(gd->bd);
  198. if (BOOTM_ENABLE_INITRD_TAG) {
  199. /*
  200. * In boot_ramdisk_high(), it may relocate ramdisk to
  201. * a specified location. And set images->initrd_start &
  202. * images->initrd_end to relocated ramdisk's start/end
  203. * addresses. So use them instead of images->rd_start &
  204. * images->rd_end when possible.
  205. */
  206. if (images->initrd_start && images->initrd_end) {
  207. setup_initrd_tag(gd->bd, images->initrd_start,
  208. images->initrd_end);
  209. } else if (images->rd_start && images->rd_end) {
  210. setup_initrd_tag(gd->bd, images->rd_start,
  211. images->rd_end);
  212. }
  213. }
  214. setup_board_tags(&params);
  215. setup_end_tag(gd->bd);
  216. } else {
  217. printf("FDT and ATAGS support not compiled in - hanging\n");
  218. hang();
  219. }
  220. }
  221. __weak bool armv7_boot_nonsec_default(void)
  222. {
  223. #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
  224. return false;
  225. #else
  226. return true;
  227. #endif
  228. }
  229. #ifdef CONFIG_ARMV7_NONSEC
  230. bool armv7_boot_nonsec(void)
  231. {
  232. char *s = getenv("bootm_boot_mode");
  233. bool nonsec = armv7_boot_nonsec_default();
  234. if (s && !strcmp(s, "sec"))
  235. nonsec = false;
  236. if (s && !strcmp(s, "nonsec"))
  237. nonsec = true;
  238. return nonsec;
  239. }
  240. #endif
  241. #ifdef CONFIG_ARM64
  242. __weak void update_os_arch_secondary_cores(uint8_t os_arch)
  243. {
  244. }
  245. #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
  246. static void switch_to_el1(void)
  247. {
  248. if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
  249. (images.os.arch == IH_ARCH_ARM))
  250. armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
  251. (u64)images.ft_addr, 0,
  252. (u64)images.ep,
  253. ES_TO_AARCH32);
  254. else
  255. armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
  256. images.ep,
  257. ES_TO_AARCH64);
  258. }
  259. #endif
  260. #endif
  261. /* Subcommand: GO */
  262. static void boot_jump_linux(bootm_headers_t *images, int flag)
  263. {
  264. #ifdef CONFIG_ARM64
  265. void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
  266. void *res2);
  267. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  268. kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
  269. void *res2))images->ep;
  270. debug("## Transferring control to Linux (at address %lx)...\n",
  271. (ulong) kernel_entry);
  272. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  273. announce_and_cleanup(fake);
  274. if (!fake) {
  275. #ifdef CONFIG_ARMV8_PSCI
  276. armv8_setup_psci();
  277. #endif
  278. do_nonsec_virt_switch();
  279. update_os_arch_secondary_cores(images->os.arch);
  280. #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
  281. armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
  282. (u64)switch_to_el1, ES_TO_AARCH64);
  283. #else
  284. if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
  285. (images->os.arch == IH_ARCH_ARM))
  286. armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
  287. (u64)images->ft_addr, 0,
  288. (u64)images->ep,
  289. ES_TO_AARCH32);
  290. else
  291. armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
  292. images->ep,
  293. ES_TO_AARCH64);
  294. #endif
  295. }
  296. #else
  297. unsigned long machid = gd->bd->bi_arch_number;
  298. char *s;
  299. void (*kernel_entry)(int zero, int arch, uint params);
  300. unsigned long r2;
  301. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  302. kernel_entry = (void (*)(int, int, uint))images->ep;
  303. #ifdef CONFIG_CPU_V7M
  304. ulong addr = (ulong)kernel_entry | 1;
  305. kernel_entry = (void *)addr;
  306. #endif
  307. s = getenv("machid");
  308. if (s) {
  309. if (strict_strtoul(s, 16, &machid) < 0) {
  310. debug("strict_strtoul failed!\n");
  311. return;
  312. }
  313. printf("Using machid 0x%lx from environment\n", machid);
  314. }
  315. debug("## Transferring control to Linux (at address %08lx)" \
  316. "...\n", (ulong) kernel_entry);
  317. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  318. announce_and_cleanup(fake);
  319. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
  320. r2 = (unsigned long)images->ft_addr;
  321. else
  322. r2 = gd->bd->bi_boot_params;
  323. if (!fake) {
  324. #ifdef CONFIG_ARMV7_NONSEC
  325. if (armv7_boot_nonsec()) {
  326. armv7_init_nonsec();
  327. secure_ram_addr(_do_nonsec_entry)(kernel_entry,
  328. 0, machid, r2);
  329. } else
  330. #endif
  331. kernel_entry(0, machid, r2);
  332. }
  333. #endif
  334. }
  335. /* Main Entry point for arm bootm implementation
  336. *
  337. * Modeled after the powerpc implementation
  338. * DIFFERENCE: Instead of calling prep and go at the end
  339. * they are called if subcommand is equal 0.
  340. */
  341. int do_bootm_linux(int flag, int argc, char * const argv[],
  342. bootm_headers_t *images)
  343. {
  344. /* No need for those on ARM */
  345. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  346. return -1;
  347. if (flag & BOOTM_STATE_OS_PREP) {
  348. boot_prep_linux(images);
  349. return 0;
  350. }
  351. if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
  352. boot_jump_linux(images, flag);
  353. return 0;
  354. }
  355. boot_prep_linux(images);
  356. boot_jump_linux(images, flag);
  357. return 0;
  358. }
  359. #if defined(CONFIG_BOOTM_VXWORKS)
  360. void boot_prep_vxworks(bootm_headers_t *images)
  361. {
  362. #if defined(CONFIG_OF_LIBFDT)
  363. int off;
  364. if (images->ft_addr) {
  365. off = fdt_path_offset(images->ft_addr, "/memory");
  366. if (off < 0) {
  367. if (arch_fixup_fdt(images->ft_addr))
  368. puts("## WARNING: fixup memory failed!\n");
  369. }
  370. }
  371. #endif
  372. cleanup_before_linux();
  373. }
  374. void boot_jump_vxworks(bootm_headers_t *images)
  375. {
  376. /* ARM VxWorks requires device tree physical address to be passed */
  377. ((void (*)(void *))images->ep)(images->ft_addr);
  378. }
  379. #endif