bootm.c 11 KB

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