bootm.c 11 KB

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