bootm.c 11 KB

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