1 // SPDX-License-Identifier: GPL-2.0+
3 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
4 * - Added prep subcommand support
5 * - Reorganized source - modeled after powerpc version
8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Marius Groeger <mgroeger@sysgo.de>
11 * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
15 #include <bootstage.h>
24 #include <u-boot/zlib.h>
25 #include <asm/byteorder.h>
26 #include <linux/libfdt.h>
28 #include <fdt_support.h>
29 #include <asm/bootm.h>
30 #include <asm/secure.h>
31 #include <linux/compiler.h>
34 #include <asm/cache.h>
36 #ifdef CONFIG_ARMV7_NONSEC
37 #include <asm/armv7.h>
39 #include <asm/setup.h>
41 DECLARE_GLOBAL_DATA_PTR;
43 static struct tag *params;
45 static ulong get_sp(void)
49 asm("mov %0, sp" : "=r"(ret) : );
53 void arch_lmb_reserve(struct lmb *lmb)
59 * Booting a (Linux) kernel image
61 * Allocate space for command line and board info - the
62 * address should be as high as possible within the reach of
63 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
64 * memory, which means far enough below the current stack
68 debug("## Current stack ends at 0x%08lx ", sp);
70 /* adjust sp by 4K to be safe */
72 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
73 if (!gd->bd->bi_dram[bank].size ||
74 sp < gd->bd->bi_dram[bank].start)
76 /* Watch out for RAM at end of address space! */
77 bank_end = gd->bd->bi_dram[bank].start +
78 gd->bd->bi_dram[bank].size - 1;
81 if (bank_end > gd->ram_top)
82 bank_end = gd->ram_top - 1;
84 lmb_reserve(lmb, sp, bank_end - sp + 1);
89 __weak void board_quiesce_devices(void)
94 * announce_and_cleanup() - Print message and prepare for kernel boot
96 * @fake: non-zero to do everything except actually boot
98 static void announce_and_cleanup(int fake)
100 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
101 #ifdef CONFIG_BOOTSTAGE_FDT
102 bootstage_fdt_add_report();
104 #ifdef CONFIG_BOOTSTAGE_REPORT
108 #ifdef CONFIG_USB_DEVICE
112 board_quiesce_devices();
114 printf("\nStarting kernel ...%s\n\n", fake ?
115 "(fake run for tracing)" : "");
117 * Call remove function of all devices with a removal flag set.
118 * This may be useful for last-stage operations, like cancelling
119 * of DMA operation or releasing device internal buffers.
121 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
123 cleanup_before_linux();
126 static void setup_start_tag (bd_t *bd)
128 params = (struct tag *)bd->bi_boot_params;
130 params->hdr.tag = ATAG_CORE;
131 params->hdr.size = tag_size (tag_core);
133 params->u.core.flags = 0;
134 params->u.core.pagesize = 0;
135 params->u.core.rootdev = 0;
137 params = tag_next (params);
140 static void setup_memory_tags(bd_t *bd)
144 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
145 params->hdr.tag = ATAG_MEM;
146 params->hdr.size = tag_size (tag_mem32);
148 params->u.mem.start = bd->bi_dram[i].start;
149 params->u.mem.size = bd->bi_dram[i].size;
151 params = tag_next (params);
155 static void setup_commandline_tag(bd_t *bd, char *commandline)
162 /* eat leading white space */
163 for (p = commandline; *p == ' '; p++);
165 /* skip non-existent command lines so the kernel will still
166 * use its default command line.
171 params->hdr.tag = ATAG_CMDLINE;
173 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
175 strcpy (params->u.cmdline.cmdline, p);
177 params = tag_next (params);
180 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
182 /* an ATAG_INITRD node tells the kernel where the compressed
183 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
185 params->hdr.tag = ATAG_INITRD2;
186 params->hdr.size = tag_size (tag_initrd);
188 params->u.initrd.start = initrd_start;
189 params->u.initrd.size = initrd_end - initrd_start;
191 params = tag_next (params);
194 static void setup_serial_tag(struct tag **tmp)
196 struct tag *params = *tmp;
197 struct tag_serialnr serialnr;
199 get_board_serial(&serialnr);
200 params->hdr.tag = ATAG_SERIAL;
201 params->hdr.size = tag_size (tag_serialnr);
202 params->u.serialnr.low = serialnr.low;
203 params->u.serialnr.high= serialnr.high;
204 params = tag_next (params);
208 static void setup_revision_tag(struct tag **in_params)
212 rev = get_board_rev();
213 params->hdr.tag = ATAG_REVISION;
214 params->hdr.size = tag_size (tag_revision);
215 params->u.revision.rev = rev;
216 params = tag_next (params);
219 static void setup_end_tag(bd_t *bd)
221 params->hdr.tag = ATAG_NONE;
222 params->hdr.size = 0;
225 __weak void setup_board_tags(struct tag **in_params) {}
228 static void do_nonsec_virt_switch(void)
231 dcache_disable(); /* flush cache before swtiching to EL2 */
235 __weak void board_prep_linux(bootm_headers_t *images) { }
237 /* Subcommand: PREP */
238 static void boot_prep_linux(bootm_headers_t *images)
240 char *commandline = env_get("bootargs");
242 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
243 #ifdef CONFIG_OF_LIBFDT
244 debug("using: FDT\n");
245 if (image_setup_linux(images)) {
246 printf("FDT creation failed! hanging...");
250 } else if (BOOTM_ENABLE_TAGS) {
251 debug("using: ATAGS\n");
252 setup_start_tag(gd->bd);
253 if (BOOTM_ENABLE_SERIAL_TAG)
254 setup_serial_tag(¶ms);
255 if (BOOTM_ENABLE_CMDLINE_TAG)
256 setup_commandline_tag(gd->bd, commandline);
257 if (BOOTM_ENABLE_REVISION_TAG)
258 setup_revision_tag(¶ms);
259 if (BOOTM_ENABLE_MEMORY_TAGS)
260 setup_memory_tags(gd->bd);
261 if (BOOTM_ENABLE_INITRD_TAG) {
263 * In boot_ramdisk_high(), it may relocate ramdisk to
264 * a specified location. And set images->initrd_start &
265 * images->initrd_end to relocated ramdisk's start/end
266 * addresses. So use them instead of images->rd_start &
267 * images->rd_end when possible.
269 if (images->initrd_start && images->initrd_end) {
270 setup_initrd_tag(gd->bd, images->initrd_start,
272 } else if (images->rd_start && images->rd_end) {
273 setup_initrd_tag(gd->bd, images->rd_start,
277 setup_board_tags(¶ms);
278 setup_end_tag(gd->bd);
280 printf("FDT and ATAGS support not compiled in - hanging\n");
284 board_prep_linux(images);
287 __weak bool armv7_boot_nonsec_default(void)
289 #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
296 #ifdef CONFIG_ARMV7_NONSEC
297 bool armv7_boot_nonsec(void)
299 char *s = env_get("bootm_boot_mode");
300 bool nonsec = armv7_boot_nonsec_default();
302 if (s && !strcmp(s, "sec"))
305 if (s && !strcmp(s, "nonsec"))
313 __weak void update_os_arch_secondary_cores(uint8_t os_arch)
317 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
318 static void switch_to_el1(void)
320 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
321 (images.os.arch == IH_ARCH_ARM))
322 armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
323 (u64)images.ft_addr, 0,
327 armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
335 static void boot_jump_linux(bootm_headers_t *images, int flag)
338 void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
340 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
342 kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
343 void *res2))images->ep;
345 debug("## Transferring control to Linux (at address %lx)...\n",
346 (ulong) kernel_entry);
347 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
349 announce_and_cleanup(fake);
352 #ifdef CONFIG_ARMV8_PSCI
355 do_nonsec_virt_switch();
357 update_os_arch_secondary_cores(images->os.arch);
359 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
360 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
361 (u64)switch_to_el1, ES_TO_AARCH64);
363 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
364 (images->os.arch == IH_ARCH_ARM))
365 armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
366 (u64)images->ft_addr, 0,
370 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
376 unsigned long machid = gd->bd->bi_arch_number;
378 void (*kernel_entry)(int zero, int arch, uint params);
380 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
382 kernel_entry = (void (*)(int, int, uint))images->ep;
383 #ifdef CONFIG_CPU_V7M
384 ulong addr = (ulong)kernel_entry | 1;
385 kernel_entry = (void *)addr;
387 s = env_get("machid");
389 if (strict_strtoul(s, 16, &machid) < 0) {
390 debug("strict_strtoul failed!\n");
393 printf("Using machid 0x%lx from environment\n", machid);
396 debug("## Transferring control to Linux (at address %08lx)" \
397 "...\n", (ulong) kernel_entry);
398 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
399 announce_and_cleanup(fake);
401 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
402 r2 = (unsigned long)images->ft_addr;
404 r2 = gd->bd->bi_boot_params;
407 #ifdef CONFIG_ARMV7_NONSEC
408 if (armv7_boot_nonsec()) {
410 secure_ram_addr(_do_nonsec_entry)(kernel_entry,
414 kernel_entry(0, machid, r2);
419 /* Main Entry point for arm bootm implementation
421 * Modeled after the powerpc implementation
422 * DIFFERENCE: Instead of calling prep and go at the end
423 * they are called if subcommand is equal 0.
425 int do_bootm_linux(int flag, int argc, char * const argv[],
426 bootm_headers_t *images)
428 /* No need for those on ARM */
429 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
432 if (flag & BOOTM_STATE_OS_PREP) {
433 boot_prep_linux(images);
437 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
438 boot_jump_linux(images, flag);
442 boot_prep_linux(images);
443 boot_jump_linux(images, flag);
447 #if defined(CONFIG_BOOTM_VXWORKS)
448 void boot_prep_vxworks(bootm_headers_t *images)
450 #if defined(CONFIG_OF_LIBFDT)
453 if (images->ft_addr) {
454 off = fdt_path_offset(images->ft_addr, "/memory");
456 if (arch_fixup_fdt(images->ft_addr))
457 puts("## WARNING: fixup memory failed!\n");
461 cleanup_before_linux();
463 void boot_jump_vxworks(bootm_headers_t *images)
465 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
470 /* ARM VxWorks requires device tree physical address to be passed */
471 ((void (*)(void *))images->ep)(images->ft_addr);