bootm.c 8.4 KB

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