bootm.c 11 KB

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