bootm.c 11 KB

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