bootm.c 10 KB

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