bootm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 (bd_t *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(bd_t *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(bd_t *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(bd_t *bd, ulong initrd_start, ulong initrd_end)
  150. {
  151. /* an ATAG_INITRD node tells the kernel where the compressed
  152. * ramdisk can be found. ATAG_RDIMG is a better name, actually.
  153. */
  154. params->hdr.tag = ATAG_INITRD2;
  155. params->hdr.size = tag_size (tag_initrd);
  156. params->u.initrd.start = initrd_start;
  157. params->u.initrd.size = initrd_end - initrd_start;
  158. params = tag_next (params);
  159. }
  160. static void setup_serial_tag(struct tag **tmp)
  161. {
  162. struct tag *params = *tmp;
  163. struct tag_serialnr serialnr;
  164. get_board_serial(&serialnr);
  165. params->hdr.tag = ATAG_SERIAL;
  166. params->hdr.size = tag_size (tag_serialnr);
  167. params->u.serialnr.low = serialnr.low;
  168. params->u.serialnr.high= serialnr.high;
  169. params = tag_next (params);
  170. *tmp = params;
  171. }
  172. static void setup_revision_tag(struct tag **in_params)
  173. {
  174. u32 rev = 0;
  175. rev = get_board_rev();
  176. params->hdr.tag = ATAG_REVISION;
  177. params->hdr.size = tag_size (tag_revision);
  178. params->u.revision.rev = rev;
  179. params = tag_next (params);
  180. }
  181. static void setup_end_tag(bd_t *bd)
  182. {
  183. params->hdr.tag = ATAG_NONE;
  184. params->hdr.size = 0;
  185. }
  186. __weak void setup_board_tags(struct tag **in_params) {}
  187. #ifdef CONFIG_ARM64
  188. static void do_nonsec_virt_switch(void)
  189. {
  190. smp_kick_all_cpus();
  191. dcache_disable(); /* flush cache before swtiching to EL2 */
  192. }
  193. #endif
  194. __weak void board_prep_linux(bootm_headers_t *images) { }
  195. /* Subcommand: PREP */
  196. static void boot_prep_linux(bootm_headers_t *images)
  197. {
  198. char *commandline = env_get("bootargs");
  199. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
  200. #ifdef CONFIG_OF_LIBFDT
  201. debug("using: FDT\n");
  202. if (image_setup_linux(images)) {
  203. printf("FDT creation failed! hanging...");
  204. hang();
  205. }
  206. #endif
  207. } else if (BOOTM_ENABLE_TAGS) {
  208. debug("using: ATAGS\n");
  209. setup_start_tag(gd->bd);
  210. if (BOOTM_ENABLE_SERIAL_TAG)
  211. setup_serial_tag(&params);
  212. if (BOOTM_ENABLE_CMDLINE_TAG)
  213. setup_commandline_tag(gd->bd, commandline);
  214. if (BOOTM_ENABLE_REVISION_TAG)
  215. setup_revision_tag(&params);
  216. if (BOOTM_ENABLE_MEMORY_TAGS)
  217. setup_memory_tags(gd->bd);
  218. if (BOOTM_ENABLE_INITRD_TAG) {
  219. /*
  220. * In boot_ramdisk_high(), it may relocate ramdisk to
  221. * a specified location. And set images->initrd_start &
  222. * images->initrd_end to relocated ramdisk's start/end
  223. * addresses. So use them instead of images->rd_start &
  224. * images->rd_end when possible.
  225. */
  226. if (images->initrd_start && images->initrd_end) {
  227. setup_initrd_tag(gd->bd, images->initrd_start,
  228. images->initrd_end);
  229. } else if (images->rd_start && images->rd_end) {
  230. setup_initrd_tag(gd->bd, images->rd_start,
  231. images->rd_end);
  232. }
  233. }
  234. setup_board_tags(&params);
  235. setup_end_tag(gd->bd);
  236. } else {
  237. printf("FDT and ATAGS support not compiled in - hanging\n");
  238. hang();
  239. }
  240. board_prep_linux(images);
  241. }
  242. __weak bool armv7_boot_nonsec_default(void)
  243. {
  244. #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
  245. return false;
  246. #else
  247. return true;
  248. #endif
  249. }
  250. #ifdef CONFIG_ARMV7_NONSEC
  251. bool armv7_boot_nonsec(void)
  252. {
  253. char *s = env_get("bootm_boot_mode");
  254. bool nonsec = armv7_boot_nonsec_default();
  255. if (s && !strcmp(s, "sec"))
  256. nonsec = false;
  257. if (s && !strcmp(s, "nonsec"))
  258. nonsec = true;
  259. return nonsec;
  260. }
  261. #endif
  262. #ifdef CONFIG_ARM64
  263. __weak void update_os_arch_secondary_cores(uint8_t os_arch)
  264. {
  265. }
  266. #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
  267. static void switch_to_el1(void)
  268. {
  269. if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
  270. (images.os.arch == IH_ARCH_ARM))
  271. armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
  272. (u64)images.ft_addr, 0,
  273. (u64)images.ep,
  274. ES_TO_AARCH32);
  275. else
  276. armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
  277. images.ep,
  278. ES_TO_AARCH64);
  279. }
  280. #endif
  281. #endif
  282. /* Subcommand: GO */
  283. static void boot_jump_linux(bootm_headers_t *images, int flag)
  284. {
  285. #ifdef CONFIG_ARM64
  286. void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
  287. void *res2);
  288. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  289. kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
  290. void *res2))images->ep;
  291. debug("## Transferring control to Linux (at address %lx)...\n",
  292. (ulong) kernel_entry);
  293. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  294. announce_and_cleanup(fake);
  295. if (!fake) {
  296. #ifdef CONFIG_ARMV8_PSCI
  297. armv8_setup_psci();
  298. #endif
  299. do_nonsec_virt_switch();
  300. update_os_arch_secondary_cores(images->os.arch);
  301. #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
  302. armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
  303. (u64)switch_to_el1, ES_TO_AARCH64);
  304. #else
  305. if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
  306. (images->os.arch == IH_ARCH_ARM))
  307. armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
  308. (u64)images->ft_addr, 0,
  309. (u64)images->ep,
  310. ES_TO_AARCH32);
  311. else
  312. armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
  313. images->ep,
  314. ES_TO_AARCH64);
  315. #endif
  316. }
  317. #else
  318. unsigned long machid = gd->bd->bi_arch_number;
  319. char *s;
  320. void (*kernel_entry)(int zero, int arch, uint params);
  321. unsigned long r2;
  322. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  323. kernel_entry = (void (*)(int, int, uint))images->ep;
  324. #ifdef CONFIG_CPU_V7M
  325. ulong addr = (ulong)kernel_entry | 1;
  326. kernel_entry = (void *)addr;
  327. #endif
  328. s = env_get("machid");
  329. if (s) {
  330. if (strict_strtoul(s, 16, &machid) < 0) {
  331. debug("strict_strtoul failed!\n");
  332. return;
  333. }
  334. printf("Using machid 0x%lx from environment\n", machid);
  335. }
  336. debug("## Transferring control to Linux (at address %08lx)" \
  337. "...\n", (ulong) kernel_entry);
  338. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  339. announce_and_cleanup(fake);
  340. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
  341. r2 = (unsigned long)images->ft_addr;
  342. else
  343. r2 = gd->bd->bi_boot_params;
  344. if (!fake) {
  345. #ifdef CONFIG_ARMV7_NONSEC
  346. if (armv7_boot_nonsec()) {
  347. armv7_init_nonsec();
  348. secure_ram_addr(_do_nonsec_entry)(kernel_entry,
  349. 0, machid, r2);
  350. } else
  351. #endif
  352. kernel_entry(0, machid, r2);
  353. }
  354. #endif
  355. }
  356. /* Main Entry point for arm bootm implementation
  357. *
  358. * Modeled after the powerpc implementation
  359. * DIFFERENCE: Instead of calling prep and go at the end
  360. * they are called if subcommand is equal 0.
  361. */
  362. int do_bootm_linux(int flag, int argc, char *const argv[],
  363. bootm_headers_t *images)
  364. {
  365. /* No need for those on ARM */
  366. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  367. return -1;
  368. if (flag & BOOTM_STATE_OS_PREP) {
  369. boot_prep_linux(images);
  370. return 0;
  371. }
  372. if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
  373. boot_jump_linux(images, flag);
  374. return 0;
  375. }
  376. boot_prep_linux(images);
  377. boot_jump_linux(images, flag);
  378. return 0;
  379. }
  380. #if defined(CONFIG_BOOTM_VXWORKS)
  381. void boot_prep_vxworks(bootm_headers_t *images)
  382. {
  383. #if defined(CONFIG_OF_LIBFDT)
  384. int off;
  385. if (images->ft_addr) {
  386. off = fdt_path_offset(images->ft_addr, "/memory");
  387. if (off > 0) {
  388. if (arch_fixup_fdt(images->ft_addr))
  389. puts("## WARNING: fixup memory failed!\n");
  390. }
  391. }
  392. #endif
  393. cleanup_before_linux();
  394. }
  395. void boot_jump_vxworks(bootm_headers_t *images)
  396. {
  397. #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
  398. armv8_setup_psci();
  399. smp_kick_all_cpus();
  400. #endif
  401. /* ARM VxWorks requires device tree physical address to be passed */
  402. ((void (*)(void *))images->ep)(images->ft_addr);
  403. }
  404. #endif