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)
22 #include <u-boot/zlib.h>
23 #include <asm/byteorder.h>
24 #include <linux/libfdt.h>
26 #include <fdt_support.h>
27 #include <asm/bootm.h>
28 #include <asm/secure.h>
29 #include <linux/compiler.h>
33 #ifdef CONFIG_ARMV7_NONSEC
34 #include <asm/armv7.h>
36 #include <asm/setup.h>
38 DECLARE_GLOBAL_DATA_PTR;
40 static struct tag *params;
42 static ulong get_sp(void)
46 asm("mov %0, sp" : "=r"(ret) : );
50 void arch_lmb_reserve(struct lmb *lmb)
56 * Booting a (Linux) kernel image
58 * Allocate space for command line and board info - the
59 * address should be as high as possible within the reach of
60 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
61 * memory, which means far enough below the current stack
65 debug("## Current stack ends at 0x%08lx ", sp);
67 /* adjust sp by 4K to be safe */
69 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
70 if (!gd->bd->bi_dram[bank].size ||
71 sp < gd->bd->bi_dram[bank].start)
73 /* Watch out for RAM at end of address space! */
74 bank_end = gd->bd->bi_dram[bank].start +
75 gd->bd->bi_dram[bank].size - 1;
78 if (bank_end > gd->ram_top)
79 bank_end = gd->ram_top - 1;
81 lmb_reserve(lmb, sp, bank_end - sp + 1);
86 __weak void board_quiesce_devices(void)
91 * announce_and_cleanup() - Print message and prepare for kernel boot
93 * @fake: non-zero to do everything except actually boot
95 static void announce_and_cleanup(int fake)
97 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
98 #ifdef CONFIG_BOOTSTAGE_FDT
99 bootstage_fdt_add_report();
101 #ifdef CONFIG_BOOTSTAGE_REPORT
105 #ifdef CONFIG_USB_DEVICE
109 board_quiesce_devices();
111 printf("\nStarting kernel ...%s\n\n", fake ?
112 "(fake run for tracing)" : "");
114 * Call remove function of all devices with a removal flag set.
115 * This may be useful for last-stage operations, like cancelling
116 * of DMA operation or releasing device internal buffers.
118 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
120 cleanup_before_linux();
123 static void setup_start_tag (bd_t *bd)
125 params = (struct tag *)bd->bi_boot_params;
127 params->hdr.tag = ATAG_CORE;
128 params->hdr.size = tag_size (tag_core);
130 params->u.core.flags = 0;
131 params->u.core.pagesize = 0;
132 params->u.core.rootdev = 0;
134 params = tag_next (params);
137 static void setup_memory_tags(bd_t *bd)
141 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
142 params->hdr.tag = ATAG_MEM;
143 params->hdr.size = tag_size (tag_mem32);
145 params->u.mem.start = bd->bi_dram[i].start;
146 params->u.mem.size = bd->bi_dram[i].size;
148 params = tag_next (params);
152 static void setup_commandline_tag(bd_t *bd, char *commandline)
159 /* eat leading white space */
160 for (p = commandline; *p == ' '; p++);
162 /* skip non-existent command lines so the kernel will still
163 * use its default command line.
168 params->hdr.tag = ATAG_CMDLINE;
170 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
172 strcpy (params->u.cmdline.cmdline, p);
174 params = tag_next (params);
177 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
179 /* an ATAG_INITRD node tells the kernel where the compressed
180 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
182 params->hdr.tag = ATAG_INITRD2;
183 params->hdr.size = tag_size (tag_initrd);
185 params->u.initrd.start = initrd_start;
186 params->u.initrd.size = initrd_end - initrd_start;
188 params = tag_next (params);
191 static void setup_serial_tag(struct tag **tmp)
193 struct tag *params = *tmp;
194 struct tag_serialnr serialnr;
196 get_board_serial(&serialnr);
197 params->hdr.tag = ATAG_SERIAL;
198 params->hdr.size = tag_size (tag_serialnr);
199 params->u.serialnr.low = serialnr.low;
200 params->u.serialnr.high= serialnr.high;
201 params = tag_next (params);
205 static void setup_revision_tag(struct tag **in_params)
209 rev = get_board_rev();
210 params->hdr.tag = ATAG_REVISION;
211 params->hdr.size = tag_size (tag_revision);
212 params->u.revision.rev = rev;
213 params = tag_next (params);
216 static void setup_end_tag(bd_t *bd)
218 params->hdr.tag = ATAG_NONE;
219 params->hdr.size = 0;
222 __weak void setup_board_tags(struct tag **in_params) {}
225 static void do_nonsec_virt_switch(void)
228 dcache_disable(); /* flush cache before swtiching to EL2 */
232 __weak void board_prep_linux(bootm_headers_t *images) { }
234 /* Subcommand: PREP */
235 static void boot_prep_linux(bootm_headers_t *images)
237 char *commandline = env_get("bootargs");
239 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
240 #ifdef CONFIG_OF_LIBFDT
241 debug("using: FDT\n");
242 if (image_setup_linux(images)) {
243 printf("FDT creation failed! hanging...");
247 } else if (BOOTM_ENABLE_TAGS) {
248 debug("using: ATAGS\n");
249 setup_start_tag(gd->bd);
250 if (BOOTM_ENABLE_SERIAL_TAG)
251 setup_serial_tag(¶ms);
252 if (BOOTM_ENABLE_CMDLINE_TAG)
253 setup_commandline_tag(gd->bd, commandline);
254 if (BOOTM_ENABLE_REVISION_TAG)
255 setup_revision_tag(¶ms);
256 if (BOOTM_ENABLE_MEMORY_TAGS)
257 setup_memory_tags(gd->bd);
258 if (BOOTM_ENABLE_INITRD_TAG) {
260 * In boot_ramdisk_high(), it may relocate ramdisk to
261 * a specified location. And set images->initrd_start &
262 * images->initrd_end to relocated ramdisk's start/end
263 * addresses. So use them instead of images->rd_start &
264 * images->rd_end when possible.
266 if (images->initrd_start && images->initrd_end) {
267 setup_initrd_tag(gd->bd, images->initrd_start,
269 } else if (images->rd_start && images->rd_end) {
270 setup_initrd_tag(gd->bd, images->rd_start,
274 setup_board_tags(¶ms);
275 setup_end_tag(gd->bd);
277 printf("FDT and ATAGS support not compiled in - hanging\n");
281 board_prep_linux(images);
284 __weak bool armv7_boot_nonsec_default(void)
286 #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
293 #ifdef CONFIG_ARMV7_NONSEC
294 bool armv7_boot_nonsec(void)
296 char *s = env_get("bootm_boot_mode");
297 bool nonsec = armv7_boot_nonsec_default();
299 if (s && !strcmp(s, "sec"))
302 if (s && !strcmp(s, "nonsec"))
310 __weak void update_os_arch_secondary_cores(uint8_t os_arch)
314 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
315 static void switch_to_el1(void)
317 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
318 (images.os.arch == IH_ARCH_ARM))
319 armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
320 (u64)images.ft_addr, 0,
324 armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
332 static void boot_jump_linux(bootm_headers_t *images, int flag)
335 void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
337 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
339 kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
340 void *res2))images->ep;
342 debug("## Transferring control to Linux (at address %lx)...\n",
343 (ulong) kernel_entry);
344 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
346 announce_and_cleanup(fake);
349 #ifdef CONFIG_ARMV8_PSCI
352 do_nonsec_virt_switch();
354 update_os_arch_secondary_cores(images->os.arch);
356 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
357 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
358 (u64)switch_to_el1, ES_TO_AARCH64);
360 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
361 (images->os.arch == IH_ARCH_ARM))
362 armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
363 (u64)images->ft_addr, 0,
367 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
373 unsigned long machid = gd->bd->bi_arch_number;
375 void (*kernel_entry)(int zero, int arch, uint params);
377 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
379 kernel_entry = (void (*)(int, int, uint))images->ep;
380 #ifdef CONFIG_CPU_V7M
381 ulong addr = (ulong)kernel_entry | 1;
382 kernel_entry = (void *)addr;
384 s = env_get("machid");
386 if (strict_strtoul(s, 16, &machid) < 0) {
387 debug("strict_strtoul failed!\n");
390 printf("Using machid 0x%lx from environment\n", machid);
393 debug("## Transferring control to Linux (at address %08lx)" \
394 "...\n", (ulong) kernel_entry);
395 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
396 announce_and_cleanup(fake);
398 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
399 r2 = (unsigned long)images->ft_addr;
401 r2 = gd->bd->bi_boot_params;
404 #ifdef CONFIG_ARMV7_NONSEC
405 if (armv7_boot_nonsec()) {
407 secure_ram_addr(_do_nonsec_entry)(kernel_entry,
411 kernel_entry(0, machid, r2);
416 /* Main Entry point for arm bootm implementation
418 * Modeled after the powerpc implementation
419 * DIFFERENCE: Instead of calling prep and go at the end
420 * they are called if subcommand is equal 0.
422 int do_bootm_linux(int flag, int argc, char * const argv[],
423 bootm_headers_t *images)
425 /* No need for those on ARM */
426 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
429 if (flag & BOOTM_STATE_OS_PREP) {
430 boot_prep_linux(images);
434 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
435 boot_jump_linux(images, flag);
439 boot_prep_linux(images);
440 boot_jump_linux(images, flag);
444 #if defined(CONFIG_BOOTM_VXWORKS)
445 void boot_prep_vxworks(bootm_headers_t *images)
447 #if defined(CONFIG_OF_LIBFDT)
450 if (images->ft_addr) {
451 off = fdt_path_offset(images->ft_addr, "/memory");
453 if (arch_fixup_fdt(images->ft_addr))
454 puts("## WARNING: fixup memory failed!\n");
458 cleanup_before_linux();
460 void boot_jump_vxworks(bootm_headers_t *images)
462 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
467 /* ARM VxWorks requires device tree physical address to be passed */
468 ((void (*)(void *))images->ep)(images->ft_addr);