bootm.c 8.5 KB

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