1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 #include <efi_loader.h>
13 #include <fdt_support.h>
17 #include <asm/global_data.h>
18 #include <linux/libfdt.h>
22 #include <tee/optee.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 static int do_bootm_standalone(int flag, int argc, char *const argv[],
27 bootm_headers_t *images)
29 int (*appl)(int, char *const[]);
31 if (!env_get_autostart()) {
32 env_set_hex("filesize", images->os.image_len);
35 appl = (int (*)(int, char * const []))images->ep;
40 /*******************************************************************/
41 /* OS booting routines */
42 /*******************************************************************/
44 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
45 static void copy_args(char *dest, int argc, char *const argv[], char delim)
49 for (i = 0; i < argc; i++) {
52 strcpy(dest, argv[i]);
53 dest += strlen(argv[i]);
58 static void __maybe_unused fit_unsupported_reset(const char *msg)
60 if (CONFIG_IS_ENABLED(FIT_VERBOSE)) {
61 printf("! FIT images not supported for '%s' - must reset board to recover!\n",
66 #ifdef CONFIG_BOOTM_NETBSD
67 static int do_bootm_netbsd(int flag, int argc, char *const argv[],
68 bootm_headers_t *images)
70 void (*loader)(struct bd_info *, image_header_t *, char *, char *);
71 image_header_t *os_hdr, *hdr;
72 ulong kernel_data, kernel_len;
75 if (flag != BOOTM_STATE_OS_GO)
78 #if defined(CONFIG_FIT)
79 if (!images->legacy_hdr_valid) {
80 fit_unsupported_reset("NetBSD");
84 hdr = images->legacy_hdr_os;
87 * Booting a (NetBSD) kernel image
89 * This process is pretty similar to a standalone application:
90 * The (first part of an multi-) image must be a stage-2 loader,
91 * which in turn is responsible for loading & invoking the actual
92 * kernel. The only differences are the parameters being passed:
93 * besides the board info strucure, the loader expects a command
94 * line, the name of the console device, and (optionally) the
95 * address of the original image header.
98 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
99 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
108 for (i = 0, len = 0; i < argc; i += 1)
109 len += strlen(argv[i]) + 1;
110 cmdline = malloc(len);
111 copy_args(cmdline, argc, argv, ' ');
113 cmdline = env_get("bootargs");
118 loader = (void (*)(struct bd_info *, image_header_t *, char *, char *))images->ep;
120 printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
123 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
126 * NetBSD Stage-2 Loader Parameters:
127 * arg[0]: pointer to board info data
128 * arg[1]: image load address
129 * arg[2]: char pointer to the console device to use
130 * arg[3]: char pointer to the boot arguments
132 (*loader)(gd->bd, os_hdr, "", cmdline);
136 #endif /* CONFIG_BOOTM_NETBSD*/
138 #ifdef CONFIG_BOOTM_RTEMS
139 static int do_bootm_rtems(int flag, int argc, char *const argv[],
140 bootm_headers_t *images)
142 void (*entry_point)(struct bd_info *);
144 if (flag != BOOTM_STATE_OS_GO)
147 #if defined(CONFIG_FIT)
148 if (!images->legacy_hdr_valid) {
149 fit_unsupported_reset("RTEMS");
154 entry_point = (void (*)(struct bd_info *))images->ep;
156 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
159 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
163 * r3: ptr to board info data
165 (*entry_point)(gd->bd);
169 #endif /* CONFIG_BOOTM_RTEMS */
171 #if defined(CONFIG_BOOTM_OSE)
172 static int do_bootm_ose(int flag, int argc, char *const argv[],
173 bootm_headers_t *images)
175 void (*entry_point)(void);
177 if (flag != BOOTM_STATE_OS_GO)
180 #if defined(CONFIG_FIT)
181 if (!images->legacy_hdr_valid) {
182 fit_unsupported_reset("OSE");
187 entry_point = (void (*)(void))images->ep;
189 printf("## Transferring control to OSE (at address %08lx) ...\n",
192 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
202 #endif /* CONFIG_BOOTM_OSE */
204 #if defined(CONFIG_BOOTM_PLAN9)
205 static int do_bootm_plan9(int flag, int argc, char *const argv[],
206 bootm_headers_t *images)
208 void (*entry_point)(void);
211 if (flag != BOOTM_STATE_OS_GO)
214 #if defined(CONFIG_FIT)
215 if (!images->legacy_hdr_valid) {
216 fit_unsupported_reset("Plan 9");
221 /* See README.plan9 */
222 s = env_get("confaddr");
224 char *confaddr = (char *)hextoul(s, NULL);
227 copy_args(confaddr, argc, argv, '\n');
229 s = env_get("bootargs");
235 entry_point = (void (*)(void))images->ep;
237 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
240 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
250 #endif /* CONFIG_BOOTM_PLAN9 */
252 #if defined(CONFIG_BOOTM_VXWORKS) && \
253 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
255 static void do_bootvx_fdt(bootm_headers_t *images)
257 #if defined(CONFIG_OF_LIBFDT)
260 ulong of_size = images->ft_len;
261 char **of_flat_tree = &images->ft_addr;
262 struct lmb *lmb = &images->lmb;
265 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
267 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
271 /* Update ethernet nodes */
272 fdt_fixup_ethernet(*of_flat_tree);
274 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
275 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
276 bootline = env_get("bootargs");
278 ret = fdt_find_and_setprop(*of_flat_tree,
279 "/chosen", "bootargs",
281 strlen(bootline) + 1, 1);
283 printf("## ERROR: %s : %s\n", __func__,
289 printf("## ERROR: %s : %s\n", __func__,
296 boot_prep_vxworks(images);
298 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
300 #if defined(CONFIG_OF_LIBFDT)
301 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
302 (ulong)images->ep, (ulong)*of_flat_tree);
304 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
307 boot_jump_vxworks(images);
309 puts("## vxWorks terminated\n");
312 static int do_bootm_vxworks_legacy(int flag, int argc, char *const argv[],
313 bootm_headers_t *images)
315 if (flag != BOOTM_STATE_OS_GO)
318 #if defined(CONFIG_FIT)
319 if (!images->legacy_hdr_valid) {
320 fit_unsupported_reset("VxWorks");
325 do_bootvx_fdt(images);
330 int do_bootm_vxworks(int flag, int argc, char *const argv[],
331 bootm_headers_t *images)
335 unsigned long vxflags;
336 bool std_dtb = false;
338 /* get bootargs env */
339 bootargs = env_get("bootargs");
341 if (bootargs != NULL) {
342 for (pos = 0; pos < strlen(bootargs); pos++) {
343 /* find f=0xnumber flag */
344 if ((bootargs[pos] == '=') && (pos >= 1) &&
345 (bootargs[pos - 1] == 'f')) {
346 vxflags = hextoul(&bootargs[pos + 1], NULL);
347 if (vxflags & VXWORKS_SYSFLG_STD_DTB)
354 if (flag & BOOTM_STATE_OS_PREP)
355 printf(" Using standard DTB\n");
356 return do_bootm_linux(flag, argc, argv, images);
358 if (flag & BOOTM_STATE_OS_PREP)
359 printf(" !!! WARNING !!! Using legacy DTB\n");
360 return do_bootm_vxworks_legacy(flag, argc, argv, images);
365 #if defined(CONFIG_CMD_ELF)
366 static int do_bootm_qnxelf(int flag, int argc, char *const argv[],
367 bootm_headers_t *images)
373 if (flag != BOOTM_STATE_OS_GO)
376 #if defined(CONFIG_FIT)
377 if (!images->legacy_hdr_valid) {
378 fit_unsupported_reset("QNX");
383 sprintf(str, "%lx", images->ep); /* write entry-point into string */
384 local_args[0] = argv[0];
385 local_args[1] = str; /* and provide it via the arguments */
388 * QNX images require the data cache is disabled.
390 dcache = dcache_status();
394 do_bootelf(NULL, 0, 2, local_args);
403 #ifdef CONFIG_INTEGRITY
404 static int do_bootm_integrity(int flag, int argc, char *const argv[],
405 bootm_headers_t *images)
407 void (*entry_point)(void);
409 if (flag != BOOTM_STATE_OS_GO)
412 #if defined(CONFIG_FIT)
413 if (!images->legacy_hdr_valid) {
414 fit_unsupported_reset("INTEGRITY");
419 entry_point = (void (*)(void))images->ep;
421 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
424 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
427 * INTEGRITY Parameters:
436 #ifdef CONFIG_BOOTM_OPENRTOS
437 static int do_bootm_openrtos(int flag, int argc, char *const argv[],
438 bootm_headers_t *images)
440 void (*entry_point)(void);
442 if (flag != BOOTM_STATE_OS_GO)
445 entry_point = (void (*)(void))images->ep;
447 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
450 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
453 * OpenRTOS Parameters:
462 #ifdef CONFIG_BOOTM_OPTEE
463 static int do_bootm_tee(int flag, int argc, char *const argv[],
464 bootm_headers_t *images)
469 if (images->os.os != IH_OS_TEE) {
473 /* Validate OPTEE header */
474 ret = optee_verify_bootm_image(images->os.image_start,
476 images->os.image_len);
481 ret = bootm_find_images(flag, argc, argv, 0, 0);
485 /* From here we can run the regular linux boot path */
486 return do_bootm_linux(flag, argc, argv, images);
490 #ifdef CONFIG_BOOTM_EFI
491 static int do_bootm_efi(int flag, int argc, char *const argv[],
492 bootm_headers_t *images)
495 efi_status_t efi_ret;
498 if (flag != BOOTM_STATE_OS_GO)
501 /* Locate FDT, if provided */
502 ret = bootm_find_images(flag, argc, argv, 0, 0);
506 /* Initialize EFI drivers */
507 efi_ret = efi_init_obj_list();
508 if (efi_ret != EFI_SUCCESS) {
509 printf("## Failed to initialize UEFI sub-system: r = %lu\n",
510 efi_ret & ~EFI_ERROR_MASK);
514 /* Install device tree */
515 efi_ret = efi_install_fdt(images->ft_len
516 ? images->ft_addr : EFI_FDT_USE_INTERNAL);
517 if (efi_ret != EFI_SUCCESS) {
518 printf("## Failed to install device tree: r = %lu\n",
519 efi_ret & ~EFI_ERROR_MASK);
524 printf("## Transferring control to EFI (at address %08lx) ...\n",
526 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
528 /* We expect to return */
529 images->os.type = IH_TYPE_STANDALONE;
531 image_buf = map_sysmem(images->ep, images->os.image_len);
533 efi_ret = efi_run_image(image_buf, images->os.image_len);
534 if (efi_ret != EFI_SUCCESS)
540 static boot_os_fn *boot_os[] = {
541 [IH_OS_U_BOOT] = do_bootm_standalone,
542 #ifdef CONFIG_BOOTM_LINUX
543 [IH_OS_LINUX] = do_bootm_linux,
545 #ifdef CONFIG_BOOTM_NETBSD
546 [IH_OS_NETBSD] = do_bootm_netbsd,
548 #ifdef CONFIG_BOOTM_RTEMS
549 [IH_OS_RTEMS] = do_bootm_rtems,
551 #if defined(CONFIG_BOOTM_OSE)
552 [IH_OS_OSE] = do_bootm_ose,
554 #if defined(CONFIG_BOOTM_PLAN9)
555 [IH_OS_PLAN9] = do_bootm_plan9,
557 #if defined(CONFIG_BOOTM_VXWORKS) && \
558 (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
559 [IH_OS_VXWORKS] = do_bootm_vxworks,
561 #if defined(CONFIG_CMD_ELF)
562 [IH_OS_QNX] = do_bootm_qnxelf,
564 #ifdef CONFIG_INTEGRITY
565 [IH_OS_INTEGRITY] = do_bootm_integrity,
567 #ifdef CONFIG_BOOTM_OPENRTOS
568 [IH_OS_OPENRTOS] = do_bootm_openrtos,
570 #ifdef CONFIG_BOOTM_OPTEE
571 [IH_OS_TEE] = do_bootm_tee,
573 #ifdef CONFIG_BOOTM_EFI
574 [IH_OS_EFI] = do_bootm_efi,
578 /* Allow for arch specific config before we boot */
579 __weak void arch_preboot_os(void)
581 /* please define platform specific arch_preboot_os() */
584 /* Allow for board specific config before we boot */
585 __weak void board_preboot_os(void)
587 /* please define board specific board_preboot_os() */
590 int boot_selected_os(int argc, char *const argv[], int state,
591 bootm_headers_t *images, boot_os_fn *boot_fn)
595 boot_fn(state, argc, argv, images);
597 /* Stand-alone may return when 'autostart' is 'no' */
598 if (images->os.type == IH_TYPE_STANDALONE ||
599 IS_ENABLED(CONFIG_SANDBOX) ||
600 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
602 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
603 debug("\n## Control returned to monitor - resetting...\n");
605 return BOOTM_ERR_RESET;
608 boot_os_fn *bootm_os_get_boot_func(int os)
610 #ifdef CONFIG_NEEDS_MANUAL_RELOC
611 static bool relocated;
616 /* relocate boot function table */
617 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
618 if (boot_os[i] != NULL)
619 boot_os[i] += gd->reloc_off;