2 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
3 * - Added prep subcommand support
4 * - Reorganized source - modeled after powerpc version
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
10 * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
12 * SPDX-License-Identifier: GPL-2.0+
20 #include <u-boot/zlib.h>
21 #include <asm/byteorder.h>
24 #include <fdt_support.h>
25 #include <asm/bootm.h>
26 #include <asm/secure.h>
27 #include <linux/compiler.h>
31 #ifdef CONFIG_ARMV7_NONSEC
32 #include <asm/armv7.h>
35 DECLARE_GLOBAL_DATA_PTR;
37 static struct tag *params;
39 static ulong get_sp(void)
43 asm("mov %0, sp" : "=r"(ret) : );
47 void arch_lmb_reserve(struct lmb *lmb)
52 * Booting a (Linux) kernel image
54 * Allocate space for command line and board info - the
55 * address should be as high as possible within the reach of
56 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
57 * memory, which means far enough below the current stack
61 debug("## Current stack ends at 0x%08lx ", sp);
63 /* adjust sp by 4K to be safe */
66 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
69 __weak void board_quiesce_devices(void)
74 * announce_and_cleanup() - Print message and prepare for kernel boot
76 * @fake: non-zero to do everything except actually boot
78 static void announce_and_cleanup(int fake)
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();
86 #ifdef CONFIG_BOOTSTAGE_REPORT
90 #ifdef CONFIG_USB_DEVICE
94 board_quiesce_devices();
97 * Call remove function of all devices with a removal flag set.
98 * This may be useful for last-stage operations, like cancelling
99 * of DMA operation or releasing device internal buffers.
101 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
103 cleanup_before_linux();
106 static void setup_start_tag (bd_t *bd)
108 params = (struct tag *)bd->bi_boot_params;
110 params->hdr.tag = ATAG_CORE;
111 params->hdr.size = tag_size (tag_core);
113 params->u.core.flags = 0;
114 params->u.core.pagesize = 0;
115 params->u.core.rootdev = 0;
117 params = tag_next (params);
120 static void setup_memory_tags(bd_t *bd)
124 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
125 params->hdr.tag = ATAG_MEM;
126 params->hdr.size = tag_size (tag_mem32);
128 params->u.mem.start = bd->bi_dram[i].start;
129 params->u.mem.size = bd->bi_dram[i].size;
131 params = tag_next (params);
135 static void setup_commandline_tag(bd_t *bd, char *commandline)
142 /* eat leading white space */
143 for (p = commandline; *p == ' '; p++);
145 /* skip non-existent command lines so the kernel will still
146 * use its default command line.
151 params->hdr.tag = ATAG_CMDLINE;
153 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
155 strcpy (params->u.cmdline.cmdline, p);
157 params = tag_next (params);
160 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
162 /* an ATAG_INITRD node tells the kernel where the compressed
163 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
165 params->hdr.tag = ATAG_INITRD2;
166 params->hdr.size = tag_size (tag_initrd);
168 params->u.initrd.start = initrd_start;
169 params->u.initrd.size = initrd_end - initrd_start;
171 params = tag_next (params);
174 static void setup_serial_tag(struct tag **tmp)
176 struct tag *params = *tmp;
177 struct tag_serialnr serialnr;
179 get_board_serial(&serialnr);
180 params->hdr.tag = ATAG_SERIAL;
181 params->hdr.size = tag_size (tag_serialnr);
182 params->u.serialnr.low = serialnr.low;
183 params->u.serialnr.high= serialnr.high;
184 params = tag_next (params);
188 static void setup_revision_tag(struct tag **in_params)
192 rev = get_board_rev();
193 params->hdr.tag = ATAG_REVISION;
194 params->hdr.size = tag_size (tag_revision);
195 params->u.revision.rev = rev;
196 params = tag_next (params);
199 static void setup_end_tag(bd_t *bd)
201 params->hdr.tag = ATAG_NONE;
202 params->hdr.size = 0;
205 __weak void setup_board_tags(struct tag **in_params) {}
208 static void do_nonsec_virt_switch(void)
211 dcache_disable(); /* flush cache before swtiching to EL2 */
215 /* Subcommand: PREP */
216 static void boot_prep_linux(bootm_headers_t *images)
218 char *commandline = getenv("bootargs");
220 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
221 #ifdef CONFIG_OF_LIBFDT
222 debug("using: FDT\n");
223 if (image_setup_linux(images)) {
224 printf("FDT creation failed! hanging...");
228 } else if (BOOTM_ENABLE_TAGS) {
229 debug("using: ATAGS\n");
230 setup_start_tag(gd->bd);
231 if (BOOTM_ENABLE_SERIAL_TAG)
232 setup_serial_tag(¶ms);
233 if (BOOTM_ENABLE_CMDLINE_TAG)
234 setup_commandline_tag(gd->bd, commandline);
235 if (BOOTM_ENABLE_REVISION_TAG)
236 setup_revision_tag(¶ms);
237 if (BOOTM_ENABLE_MEMORY_TAGS)
238 setup_memory_tags(gd->bd);
239 if (BOOTM_ENABLE_INITRD_TAG) {
241 * In boot_ramdisk_high(), it may relocate ramdisk to
242 * a specified location. And set images->initrd_start &
243 * images->initrd_end to relocated ramdisk's start/end
244 * addresses. So use them instead of images->rd_start &
245 * images->rd_end when possible.
247 if (images->initrd_start && images->initrd_end) {
248 setup_initrd_tag(gd->bd, images->initrd_start,
250 } else if (images->rd_start && images->rd_end) {
251 setup_initrd_tag(gd->bd, images->rd_start,
255 setup_board_tags(¶ms);
256 setup_end_tag(gd->bd);
258 printf("FDT and ATAGS support not compiled in - hanging\n");
263 __weak bool armv7_boot_nonsec_default(void)
265 #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
272 #ifdef CONFIG_ARMV7_NONSEC
273 bool armv7_boot_nonsec(void)
275 char *s = getenv("bootm_boot_mode");
276 bool nonsec = armv7_boot_nonsec_default();
278 if (s && !strcmp(s, "sec"))
281 if (s && !strcmp(s, "nonsec"))
289 __weak void update_os_arch_secondary_cores(uint8_t os_arch)
293 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
294 static void switch_to_el1(void)
296 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
297 (images.os.arch == IH_ARCH_ARM))
298 armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
299 (u64)images.ft_addr, 0,
303 armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
311 static void boot_jump_linux(bootm_headers_t *images, int flag)
314 void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
316 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
318 kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
319 void *res2))images->ep;
321 debug("## Transferring control to Linux (at address %lx)...\n",
322 (ulong) kernel_entry);
323 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
325 announce_and_cleanup(fake);
328 #ifdef CONFIG_ARMV8_PSCI
331 do_nonsec_virt_switch();
333 update_os_arch_secondary_cores(images->os.arch);
335 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
336 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
337 (u64)switch_to_el1, ES_TO_AARCH64);
339 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
340 (images->os.arch == IH_ARCH_ARM))
341 armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
342 (u64)images->ft_addr, 0,
346 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
352 unsigned long machid = gd->bd->bi_arch_number;
354 void (*kernel_entry)(int zero, int arch, uint params);
356 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
358 kernel_entry = (void (*)(int, int, uint))images->ep;
359 #ifdef CONFIG_CPU_V7M
360 ulong addr = (ulong)kernel_entry | 1;
361 kernel_entry = (void *)addr;
363 s = getenv("machid");
365 if (strict_strtoul(s, 16, &machid) < 0) {
366 debug("strict_strtoul failed!\n");
369 printf("Using machid 0x%lx from environment\n", machid);
372 debug("## Transferring control to Linux (at address %08lx)" \
373 "...\n", (ulong) kernel_entry);
374 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
375 announce_and_cleanup(fake);
377 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
378 r2 = (unsigned long)images->ft_addr;
380 r2 = gd->bd->bi_boot_params;
383 #ifdef CONFIG_ARMV7_NONSEC
384 if (armv7_boot_nonsec()) {
386 secure_ram_addr(_do_nonsec_entry)(kernel_entry,
390 kernel_entry(0, machid, r2);
395 /* Main Entry point for arm bootm implementation
397 * Modeled after the powerpc implementation
398 * DIFFERENCE: Instead of calling prep and go at the end
399 * they are called if subcommand is equal 0.
401 int do_bootm_linux(int flag, int argc, char * const argv[],
402 bootm_headers_t *images)
404 /* No need for those on ARM */
405 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
408 if (flag & BOOTM_STATE_OS_PREP) {
409 boot_prep_linux(images);
413 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
414 boot_jump_linux(images, flag);
418 boot_prep_linux(images);
419 boot_jump_linux(images, flag);
423 #if defined(CONFIG_BOOTM_VXWORKS)
424 void boot_prep_vxworks(bootm_headers_t *images)
426 #if defined(CONFIG_OF_LIBFDT)
429 if (images->ft_addr) {
430 off = fdt_path_offset(images->ft_addr, "/memory");
432 if (arch_fixup_fdt(images->ft_addr))
433 puts("## WARNING: fixup memory failed!\n");
437 cleanup_before_linux();
439 void boot_jump_vxworks(bootm_headers_t *images)
441 /* ARM VxWorks requires device tree physical address to be passed */
442 ((void (*)(void *))images->ep)(images->ft_addr);