bootm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* Copyright (C) 2011
  2. * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
  3. * - Added prep subcommand support
  4. * - Reorganized source - modeled after powerpc version
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Marius Groeger <mgroeger@sysgo.de>
  9. *
  10. * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  11. *
  12. * SPDX-License-Identifier: GPL-2.0+
  13. */
  14. #include <common.h>
  15. #include <command.h>
  16. #include <image.h>
  17. #include <u-boot/zlib.h>
  18. #include <asm/byteorder.h>
  19. #include <libfdt.h>
  20. #include <fdt_support.h>
  21. #include <asm/bootm.h>
  22. #include <asm/secure.h>
  23. #include <linux/compiler.h>
  24. #include <bootm.h>
  25. #include <vxworks.h>
  26. #if defined(CONFIG_ARMV7_NONSEC) || defined(CONFIG_ARMV7_VIRT)
  27. #include <asm/armv7.h>
  28. #endif
  29. DECLARE_GLOBAL_DATA_PTR;
  30. static struct tag *params;
  31. static ulong get_sp(void)
  32. {
  33. ulong ret;
  34. asm("mov %0, sp" : "=r"(ret) : );
  35. return ret;
  36. }
  37. void arch_lmb_reserve(struct lmb *lmb)
  38. {
  39. ulong sp;
  40. /*
  41. * Booting a (Linux) kernel image
  42. *
  43. * Allocate space for command line and board info - the
  44. * address should be as high as possible within the reach of
  45. * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
  46. * memory, which means far enough below the current stack
  47. * pointer.
  48. */
  49. sp = get_sp();
  50. debug("## Current stack ends at 0x%08lx ", sp);
  51. /* adjust sp by 4K to be safe */
  52. sp -= 4096;
  53. lmb_reserve(lmb, sp,
  54. gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
  55. }
  56. /**
  57. * announce_and_cleanup() - Print message and prepare for kernel boot
  58. *
  59. * @fake: non-zero to do everything except actually boot
  60. */
  61. static void announce_and_cleanup(int fake)
  62. {
  63. printf("\nStarting kernel ...%s\n\n", fake ?
  64. "(fake run for tracing)" : "");
  65. bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
  66. #ifdef CONFIG_BOOTSTAGE_FDT
  67. bootstage_fdt_add_report();
  68. #endif
  69. #ifdef CONFIG_BOOTSTAGE_REPORT
  70. bootstage_report();
  71. #endif
  72. #ifdef CONFIG_USB_DEVICE
  73. udc_disconnect();
  74. #endif
  75. cleanup_before_linux();
  76. }
  77. static void setup_start_tag (bd_t *bd)
  78. {
  79. params = (struct tag *)bd->bi_boot_params;
  80. params->hdr.tag = ATAG_CORE;
  81. params->hdr.size = tag_size (tag_core);
  82. params->u.core.flags = 0;
  83. params->u.core.pagesize = 0;
  84. params->u.core.rootdev = 0;
  85. params = tag_next (params);
  86. }
  87. static void setup_memory_tags(bd_t *bd)
  88. {
  89. int i;
  90. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  91. params->hdr.tag = ATAG_MEM;
  92. params->hdr.size = tag_size (tag_mem32);
  93. params->u.mem.start = bd->bi_dram[i].start;
  94. params->u.mem.size = bd->bi_dram[i].size;
  95. params = tag_next (params);
  96. }
  97. }
  98. static void setup_commandline_tag(bd_t *bd, char *commandline)
  99. {
  100. char *p;
  101. if (!commandline)
  102. return;
  103. /* eat leading white space */
  104. for (p = commandline; *p == ' '; p++);
  105. /* skip non-existent command lines so the kernel will still
  106. * use its default command line.
  107. */
  108. if (*p == '\0')
  109. return;
  110. params->hdr.tag = ATAG_CMDLINE;
  111. params->hdr.size =
  112. (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
  113. strcpy (params->u.cmdline.cmdline, p);
  114. params = tag_next (params);
  115. }
  116. static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
  117. {
  118. /* an ATAG_INITRD node tells the kernel where the compressed
  119. * ramdisk can be found. ATAG_RDIMG is a better name, actually.
  120. */
  121. params->hdr.tag = ATAG_INITRD2;
  122. params->hdr.size = tag_size (tag_initrd);
  123. params->u.initrd.start = initrd_start;
  124. params->u.initrd.size = initrd_end - initrd_start;
  125. params = tag_next (params);
  126. }
  127. static void setup_serial_tag(struct tag **tmp)
  128. {
  129. struct tag *params = *tmp;
  130. struct tag_serialnr serialnr;
  131. get_board_serial(&serialnr);
  132. params->hdr.tag = ATAG_SERIAL;
  133. params->hdr.size = tag_size (tag_serialnr);
  134. params->u.serialnr.low = serialnr.low;
  135. params->u.serialnr.high= serialnr.high;
  136. params = tag_next (params);
  137. *tmp = params;
  138. }
  139. static void setup_revision_tag(struct tag **in_params)
  140. {
  141. u32 rev = 0;
  142. rev = get_board_rev();
  143. params->hdr.tag = ATAG_REVISION;
  144. params->hdr.size = tag_size (tag_revision);
  145. params->u.revision.rev = rev;
  146. params = tag_next (params);
  147. }
  148. static void setup_end_tag(bd_t *bd)
  149. {
  150. params->hdr.tag = ATAG_NONE;
  151. params->hdr.size = 0;
  152. }
  153. __weak void setup_board_tags(struct tag **in_params) {}
  154. #ifdef CONFIG_ARM64
  155. static void do_nonsec_virt_switch(void)
  156. {
  157. smp_kick_all_cpus();
  158. flush_dcache_all(); /* flush cache before swtiching to EL2 */
  159. armv8_switch_to_el2();
  160. #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
  161. armv8_switch_to_el1();
  162. #endif
  163. }
  164. #endif
  165. /* Subcommand: PREP */
  166. static void boot_prep_linux(bootm_headers_t *images)
  167. {
  168. char *commandline = getenv("bootargs");
  169. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
  170. #ifdef CONFIG_OF_LIBFDT
  171. debug("using: FDT\n");
  172. if (image_setup_linux(images)) {
  173. printf("FDT creation failed! hanging...");
  174. hang();
  175. }
  176. #endif
  177. } else if (BOOTM_ENABLE_TAGS) {
  178. debug("using: ATAGS\n");
  179. setup_start_tag(gd->bd);
  180. if (BOOTM_ENABLE_SERIAL_TAG)
  181. setup_serial_tag(&params);
  182. if (BOOTM_ENABLE_CMDLINE_TAG)
  183. setup_commandline_tag(gd->bd, commandline);
  184. if (BOOTM_ENABLE_REVISION_TAG)
  185. setup_revision_tag(&params);
  186. if (BOOTM_ENABLE_MEMORY_TAGS)
  187. setup_memory_tags(gd->bd);
  188. if (BOOTM_ENABLE_INITRD_TAG) {
  189. if (images->rd_start && images->rd_end) {
  190. setup_initrd_tag(gd->bd, images->rd_start,
  191. images->rd_end);
  192. }
  193. }
  194. setup_board_tags(&params);
  195. setup_end_tag(gd->bd);
  196. } else {
  197. printf("FDT and ATAGS support not compiled in - hanging\n");
  198. hang();
  199. }
  200. }
  201. /* Subcommand: GO */
  202. static void boot_jump_linux(bootm_headers_t *images, int flag)
  203. {
  204. #ifdef CONFIG_ARM64
  205. void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
  206. void *res2);
  207. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  208. kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
  209. void *res2))images->ep;
  210. debug("## Transferring control to Linux (at address %lx)...\n",
  211. (ulong) kernel_entry);
  212. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  213. announce_and_cleanup(fake);
  214. if (!fake) {
  215. do_nonsec_virt_switch();
  216. kernel_entry(images->ft_addr, NULL, NULL, NULL);
  217. }
  218. #else
  219. unsigned long machid = gd->bd->bi_arch_number;
  220. char *s;
  221. void (*kernel_entry)(int zero, int arch, uint params);
  222. unsigned long r2;
  223. int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
  224. kernel_entry = (void (*)(int, int, uint))images->ep;
  225. s = getenv("machid");
  226. if (s) {
  227. strict_strtoul(s, 16, &machid);
  228. printf("Using machid 0x%lx from environment\n", machid);
  229. }
  230. debug("## Transferring control to Linux (at address %08lx)" \
  231. "...\n", (ulong) kernel_entry);
  232. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  233. announce_and_cleanup(fake);
  234. if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
  235. r2 = (unsigned long)images->ft_addr;
  236. else
  237. r2 = gd->bd->bi_boot_params;
  238. if (!fake) {
  239. #if defined(CONFIG_ARMV7_NONSEC) || defined(CONFIG_ARMV7_VIRT)
  240. armv7_init_nonsec();
  241. secure_ram_addr(_do_nonsec_entry)(kernel_entry,
  242. 0, machid, r2);
  243. #else
  244. kernel_entry(0, machid, r2);
  245. #endif
  246. }
  247. #endif
  248. }
  249. /* Main Entry point for arm bootm implementation
  250. *
  251. * Modeled after the powerpc implementation
  252. * DIFFERENCE: Instead of calling prep and go at the end
  253. * they are called if subcommand is equal 0.
  254. */
  255. int do_bootm_linux(int flag, int argc, char * const argv[],
  256. bootm_headers_t *images)
  257. {
  258. /* No need for those on ARM */
  259. if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
  260. return -1;
  261. if (flag & BOOTM_STATE_OS_PREP) {
  262. boot_prep_linux(images);
  263. return 0;
  264. }
  265. if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
  266. boot_jump_linux(images, flag);
  267. return 0;
  268. }
  269. boot_prep_linux(images);
  270. boot_jump_linux(images, flag);
  271. return 0;
  272. }
  273. #ifdef CONFIG_CMD_BOOTZ
  274. struct zimage_header {
  275. uint32_t code[9];
  276. uint32_t zi_magic;
  277. uint32_t zi_start;
  278. uint32_t zi_end;
  279. };
  280. #define LINUX_ARM_ZIMAGE_MAGIC 0x016f2818
  281. int bootz_setup(ulong image, ulong *start, ulong *end)
  282. {
  283. struct zimage_header *zi;
  284. zi = (struct zimage_header *)map_sysmem(image, 0);
  285. if (zi->zi_magic != LINUX_ARM_ZIMAGE_MAGIC) {
  286. puts("Bad Linux ARM zImage magic!\n");
  287. return 1;
  288. }
  289. *start = zi->zi_start;
  290. *end = zi->zi_end;
  291. printf("Kernel image @ %#08lx [ %#08lx - %#08lx ]\n", image, *start,
  292. *end);
  293. return 0;
  294. }
  295. #endif /* CONFIG_CMD_BOOTZ */
  296. #if defined(CONFIG_BOOTM_VXWORKS)
  297. void boot_prep_vxworks(bootm_headers_t *images)
  298. {
  299. #if defined(CONFIG_OF_LIBFDT)
  300. int off;
  301. if (images->ft_addr) {
  302. off = fdt_path_offset(images->ft_addr, "/memory");
  303. if (off < 0) {
  304. if (arch_fixup_fdt(images->ft_addr))
  305. puts("## WARNING: fixup memory failed!\n");
  306. }
  307. }
  308. #endif
  309. cleanup_before_linux();
  310. }
  311. void boot_jump_vxworks(bootm_headers_t *images)
  312. {
  313. /* ARM VxWorks requires device tree physical address to be passed */
  314. ((void (*)(void *))images->ep)(images->ft_addr);
  315. }
  316. #endif