bootm.c 10 KB

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