1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 #include <efi_loader.h>
12 #include <fdt_support.h>
13 #include <linux/libfdt.h>
17 #include <tee/optee.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 static int do_bootm_standalone(int flag, int argc, char * const argv[],
22 bootm_headers_t *images)
25 int (*appl)(int, char *const[]);
27 /* Don't start if "autostart" is set to "no" */
28 s = env_get("autostart");
29 if ((s != NULL) && !strcmp(s, "no")) {
30 env_set_hex("filesize", images->os.image_len);
33 appl = (int (*)(int, char * const []))images->ep;
38 /*******************************************************************/
39 /* OS booting routines */
40 /*******************************************************************/
42 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
43 static void copy_args(char *dest, int argc, char * const argv[], char delim)
47 for (i = 0; i < argc; i++) {
50 strcpy(dest, argv[i]);
51 dest += strlen(argv[i]);
56 #ifdef CONFIG_BOOTM_NETBSD
57 static int do_bootm_netbsd(int flag, int argc, char * const argv[],
58 bootm_headers_t *images)
60 void (*loader)(bd_t *, image_header_t *, char *, char *);
61 image_header_t *os_hdr, *hdr;
62 ulong kernel_data, kernel_len;
65 if (flag != BOOTM_STATE_OS_GO)
68 #if defined(CONFIG_FIT)
69 if (!images->legacy_hdr_valid) {
70 fit_unsupported_reset("NetBSD");
74 hdr = images->legacy_hdr_os;
77 * Booting a (NetBSD) kernel image
79 * This process is pretty similar to a standalone application:
80 * The (first part of an multi-) image must be a stage-2 loader,
81 * which in turn is responsible for loading & invoking the actual
82 * kernel. The only differences are the parameters being passed:
83 * besides the board info strucure, the loader expects a command
84 * line, the name of the console device, and (optionally) the
85 * address of the original image header.
88 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
89 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
98 for (i = 0, len = 0; i < argc; i += 1)
99 len += strlen(argv[i]) + 1;
100 cmdline = malloc(len);
101 copy_args(cmdline, argc, argv, ' ');
103 cmdline = env_get("bootargs");
108 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
110 printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
113 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
116 * NetBSD Stage-2 Loader Parameters:
117 * arg[0]: pointer to board info data
118 * arg[1]: image load address
119 * arg[2]: char pointer to the console device to use
120 * arg[3]: char pointer to the boot arguments
122 (*loader)(gd->bd, os_hdr, "", cmdline);
126 #endif /* CONFIG_BOOTM_NETBSD*/
128 #ifdef CONFIG_LYNXKDI
129 static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
130 bootm_headers_t *images)
132 image_header_t *hdr = &images->legacy_hdr_os_copy;
134 if (flag != BOOTM_STATE_OS_GO)
137 #if defined(CONFIG_FIT)
138 if (!images->legacy_hdr_valid) {
139 fit_unsupported_reset("Lynx");
144 lynxkdi_boot((image_header_t *)hdr);
148 #endif /* CONFIG_LYNXKDI */
150 #ifdef CONFIG_BOOTM_RTEMS
151 static int do_bootm_rtems(int flag, int argc, char * const argv[],
152 bootm_headers_t *images)
154 void (*entry_point)(bd_t *);
156 if (flag != BOOTM_STATE_OS_GO)
159 #if defined(CONFIG_FIT)
160 if (!images->legacy_hdr_valid) {
161 fit_unsupported_reset("RTEMS");
166 entry_point = (void (*)(bd_t *))images->ep;
168 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
171 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
175 * r3: ptr to board info data
177 (*entry_point)(gd->bd);
181 #endif /* CONFIG_BOOTM_RTEMS */
183 #if defined(CONFIG_BOOTM_OSE)
184 static int do_bootm_ose(int flag, int argc, char * const argv[],
185 bootm_headers_t *images)
187 void (*entry_point)(void);
189 if (flag != BOOTM_STATE_OS_GO)
192 #if defined(CONFIG_FIT)
193 if (!images->legacy_hdr_valid) {
194 fit_unsupported_reset("OSE");
199 entry_point = (void (*)(void))images->ep;
201 printf("## Transferring control to OSE (at address %08lx) ...\n",
204 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
214 #endif /* CONFIG_BOOTM_OSE */
216 #if defined(CONFIG_BOOTM_PLAN9)
217 static int do_bootm_plan9(int flag, int argc, char * const argv[],
218 bootm_headers_t *images)
220 void (*entry_point)(void);
223 if (flag != BOOTM_STATE_OS_GO)
226 #if defined(CONFIG_FIT)
227 if (!images->legacy_hdr_valid) {
228 fit_unsupported_reset("Plan 9");
233 /* See README.plan9 */
234 s = env_get("confaddr");
236 char *confaddr = (char *)simple_strtoul(s, NULL, 16);
239 copy_args(confaddr, argc, argv, '\n');
241 s = env_get("bootargs");
247 entry_point = (void (*)(void))images->ep;
249 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
252 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
262 #endif /* CONFIG_BOOTM_PLAN9 */
264 #if defined(CONFIG_BOOTM_VXWORKS) && \
265 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
267 static void do_bootvx_fdt(bootm_headers_t *images)
269 #if defined(CONFIG_OF_LIBFDT)
272 ulong of_size = images->ft_len;
273 char **of_flat_tree = &images->ft_addr;
274 struct lmb *lmb = &images->lmb;
277 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
279 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
283 /* Update ethernet nodes */
284 fdt_fixup_ethernet(*of_flat_tree);
286 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
287 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
288 bootline = env_get("bootargs");
290 ret = fdt_find_and_setprop(*of_flat_tree,
291 "/chosen", "bootargs",
293 strlen(bootline) + 1, 1);
295 printf("## ERROR: %s : %s\n", __func__,
301 printf("## ERROR: %s : %s\n", __func__,
308 boot_prep_vxworks(images);
310 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
312 #if defined(CONFIG_OF_LIBFDT)
313 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
314 (ulong)images->ep, (ulong)*of_flat_tree);
316 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
319 boot_jump_vxworks(images);
321 puts("## vxWorks terminated\n");
324 static int do_bootm_vxworks_legacy(int flag, int argc, char * const argv[],
325 bootm_headers_t *images)
327 if (flag != BOOTM_STATE_OS_GO)
330 #if defined(CONFIG_FIT)
331 if (!images->legacy_hdr_valid) {
332 fit_unsupported_reset("VxWorks");
337 do_bootvx_fdt(images);
342 int do_bootm_vxworks(int flag, int argc, char * const argv[],
343 bootm_headers_t *images)
347 unsigned long vxflags;
348 bool std_dtb = false;
350 /* get bootargs env */
351 bootargs = env_get("bootargs");
353 if (bootargs != NULL) {
354 for (pos = 0; pos < strlen(bootargs); pos++) {
355 /* find f=0xnumber flag */
356 if ((bootargs[pos] == '=') && (pos >= 1) &&
357 (bootargs[pos - 1] == 'f')) {
358 vxflags = simple_strtoul(&bootargs[pos + 1],
360 if (vxflags & VXWORKS_SYSFLG_STD_DTB)
367 if (flag & BOOTM_STATE_OS_PREP)
368 printf(" Using standard DTB\n");
369 return do_bootm_linux(flag, argc, argv, images);
371 if (flag & BOOTM_STATE_OS_PREP)
372 printf(" !!! WARNING !!! Using legacy DTB\n");
373 return do_bootm_vxworks_legacy(flag, argc, argv, images);
378 #if defined(CONFIG_CMD_ELF)
379 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
380 bootm_headers_t *images)
386 if (flag != BOOTM_STATE_OS_GO)
389 #if defined(CONFIG_FIT)
390 if (!images->legacy_hdr_valid) {
391 fit_unsupported_reset("QNX");
396 sprintf(str, "%lx", images->ep); /* write entry-point into string */
397 local_args[0] = argv[0];
398 local_args[1] = str; /* and provide it via the arguments */
401 * QNX images require the data cache is disabled.
403 dcache = dcache_status();
407 do_bootelf(NULL, 0, 2, local_args);
416 #ifdef CONFIG_INTEGRITY
417 static int do_bootm_integrity(int flag, int argc, char * const argv[],
418 bootm_headers_t *images)
420 void (*entry_point)(void);
422 if (flag != BOOTM_STATE_OS_GO)
425 #if defined(CONFIG_FIT)
426 if (!images->legacy_hdr_valid) {
427 fit_unsupported_reset("INTEGRITY");
432 entry_point = (void (*)(void))images->ep;
434 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
437 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
440 * INTEGRITY Parameters:
449 #ifdef CONFIG_BOOTM_OPENRTOS
450 static int do_bootm_openrtos(int flag, int argc, char * const argv[],
451 bootm_headers_t *images)
453 void (*entry_point)(void);
455 if (flag != BOOTM_STATE_OS_GO)
458 entry_point = (void (*)(void))images->ep;
460 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
463 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
466 * OpenRTOS Parameters:
475 #ifdef CONFIG_BOOTM_OPTEE
476 static int do_bootm_tee(int flag, int argc, char * const argv[],
477 bootm_headers_t *images)
482 if (images->os.os != IH_OS_TEE) {
486 /* Validate OPTEE header */
487 ret = optee_verify_bootm_image(images->os.image_start,
489 images->os.image_len);
494 ret = bootm_find_images(flag, argc, argv);
498 /* From here we can run the regular linux boot path */
499 return do_bootm_linux(flag, argc, argv, images);
503 #ifdef CONFIG_BOOTM_EFI
504 static int do_bootm_efi(int flag, int argc, char * const argv[],
505 bootm_headers_t *images)
508 efi_status_t efi_ret;
511 if (flag != BOOTM_STATE_OS_GO)
514 /* Locate FDT, if provided */
515 ret = bootm_find_images(flag, argc, argv);
519 /* Initialize EFI drivers */
520 efi_ret = efi_init_obj_list();
521 if (efi_ret != EFI_SUCCESS) {
522 printf("## Failed to initialize UEFI sub-system: r = %lu\n",
523 efi_ret & ~EFI_ERROR_MASK);
527 /* Install device tree */
528 efi_ret = efi_install_fdt(images->ft_len
529 ? images->ft_addr : EFI_FDT_USE_INTERNAL);
530 if (efi_ret != EFI_SUCCESS) {
531 printf("## Failed to install device tree: r = %lu\n",
532 efi_ret & ~EFI_ERROR_MASK);
537 printf("## Transferring control to EFI (at address %08lx) ...\n",
539 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
541 image_buf = map_sysmem(images->ep, images->os.image_len);
543 efi_ret = efi_run_image(image_buf, images->os.image_len);
544 if (efi_ret != EFI_SUCCESS) {
545 printf("## Failed to run EFI image: r = %lu\n",
546 efi_ret & ~EFI_ERROR_MASK);
554 static boot_os_fn *boot_os[] = {
555 [IH_OS_U_BOOT] = do_bootm_standalone,
556 #ifdef CONFIG_BOOTM_LINUX
557 [IH_OS_LINUX] = do_bootm_linux,
559 #ifdef CONFIG_BOOTM_NETBSD
560 [IH_OS_NETBSD] = do_bootm_netbsd,
562 #ifdef CONFIG_LYNXKDI
563 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
565 #ifdef CONFIG_BOOTM_RTEMS
566 [IH_OS_RTEMS] = do_bootm_rtems,
568 #if defined(CONFIG_BOOTM_OSE)
569 [IH_OS_OSE] = do_bootm_ose,
571 #if defined(CONFIG_BOOTM_PLAN9)
572 [IH_OS_PLAN9] = do_bootm_plan9,
574 #if defined(CONFIG_BOOTM_VXWORKS) && \
575 (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
576 [IH_OS_VXWORKS] = do_bootm_vxworks,
578 #if defined(CONFIG_CMD_ELF)
579 [IH_OS_QNX] = do_bootm_qnxelf,
581 #ifdef CONFIG_INTEGRITY
582 [IH_OS_INTEGRITY] = do_bootm_integrity,
584 #ifdef CONFIG_BOOTM_OPENRTOS
585 [IH_OS_OPENRTOS] = do_bootm_openrtos,
587 #ifdef CONFIG_BOOTM_OPTEE
588 [IH_OS_TEE] = do_bootm_tee,
590 #ifdef CONFIG_BOOTM_EFI
591 [IH_OS_EFI] = do_bootm_efi,
595 /* Allow for arch specific config before we boot */
596 __weak void arch_preboot_os(void)
598 /* please define platform specific arch_preboot_os() */
601 /* Allow for board specific config before we boot */
602 __weak void board_preboot_os(void)
604 /* please define board specific board_preboot_os() */
607 int boot_selected_os(int argc, char * const argv[], int state,
608 bootm_headers_t *images, boot_os_fn *boot_fn)
612 boot_fn(state, argc, argv, images);
614 /* Stand-alone may return when 'autostart' is 'no' */
615 if (images->os.type == IH_TYPE_STANDALONE ||
616 IS_ENABLED(CONFIG_SANDBOX) ||
617 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
619 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
620 debug("\n## Control returned to monitor - resetting...\n");
622 return BOOTM_ERR_RESET;
625 boot_os_fn *bootm_os_get_boot_func(int os)
627 #ifdef CONFIG_NEEDS_MANUAL_RELOC
628 static bool relocated;
633 /* relocate boot function table */
634 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
635 if (boot_os[i] != NULL)
636 boot_os[i] += gd->reloc_off;