bootm.c 11 KB

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