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>
16 #include <linux/libfdt.h>
20 #include <tee/optee.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 static int do_bootm_standalone(int flag, int argc, char * const argv[],
25 bootm_headers_t *images)
28 int (*appl)(int, char *const[]);
30 /* Don't start if "autostart" is set to "no" */
31 s = env_get("autostart");
32 if ((s != NULL) && !strcmp(s, "no")) {
33 env_set_hex("filesize", images->os.image_len);
36 appl = (int (*)(int, char * const []))images->ep;
41 /*******************************************************************/
42 /* OS booting routines */
43 /*******************************************************************/
45 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
46 static void copy_args(char *dest, int argc, char * const argv[], char delim)
50 for (i = 0; i < argc; i++) {
53 strcpy(dest, argv[i]);
54 dest += strlen(argv[i]);
59 #ifdef CONFIG_BOOTM_NETBSD
60 static int do_bootm_netbsd(int flag, int argc, char * const argv[],
61 bootm_headers_t *images)
63 void (*loader)(bd_t *, image_header_t *, char *, char *);
64 image_header_t *os_hdr, *hdr;
65 ulong kernel_data, kernel_len;
68 if (flag != BOOTM_STATE_OS_GO)
71 #if defined(CONFIG_FIT)
72 if (!images->legacy_hdr_valid) {
73 fit_unsupported_reset("NetBSD");
77 hdr = images->legacy_hdr_os;
80 * Booting a (NetBSD) kernel image
82 * This process is pretty similar to a standalone application:
83 * The (first part of an multi-) image must be a stage-2 loader,
84 * which in turn is responsible for loading & invoking the actual
85 * kernel. The only differences are the parameters being passed:
86 * besides the board info strucure, the loader expects a command
87 * line, the name of the console device, and (optionally) the
88 * address of the original image header.
91 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
92 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
101 for (i = 0, len = 0; i < argc; i += 1)
102 len += strlen(argv[i]) + 1;
103 cmdline = malloc(len);
104 copy_args(cmdline, argc, argv, ' ');
106 cmdline = env_get("bootargs");
111 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
113 printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
116 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
119 * NetBSD Stage-2 Loader Parameters:
120 * arg[0]: pointer to board info data
121 * arg[1]: image load address
122 * arg[2]: char pointer to the console device to use
123 * arg[3]: char pointer to the boot arguments
125 (*loader)(gd->bd, os_hdr, "", cmdline);
129 #endif /* CONFIG_BOOTM_NETBSD*/
131 #ifdef CONFIG_LYNXKDI
132 static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
133 bootm_headers_t *images)
135 image_header_t *hdr = &images->legacy_hdr_os_copy;
137 if (flag != BOOTM_STATE_OS_GO)
140 #if defined(CONFIG_FIT)
141 if (!images->legacy_hdr_valid) {
142 fit_unsupported_reset("Lynx");
147 lynxkdi_boot((image_header_t *)hdr);
151 #endif /* CONFIG_LYNXKDI */
153 #ifdef CONFIG_BOOTM_RTEMS
154 static int do_bootm_rtems(int flag, int argc, char * const argv[],
155 bootm_headers_t *images)
157 void (*entry_point)(bd_t *);
159 if (flag != BOOTM_STATE_OS_GO)
162 #if defined(CONFIG_FIT)
163 if (!images->legacy_hdr_valid) {
164 fit_unsupported_reset("RTEMS");
169 entry_point = (void (*)(bd_t *))images->ep;
171 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
174 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
178 * r3: ptr to board info data
180 (*entry_point)(gd->bd);
184 #endif /* CONFIG_BOOTM_RTEMS */
186 #if defined(CONFIG_BOOTM_OSE)
187 static int do_bootm_ose(int flag, int argc, char * const argv[],
188 bootm_headers_t *images)
190 void (*entry_point)(void);
192 if (flag != BOOTM_STATE_OS_GO)
195 #if defined(CONFIG_FIT)
196 if (!images->legacy_hdr_valid) {
197 fit_unsupported_reset("OSE");
202 entry_point = (void (*)(void))images->ep;
204 printf("## Transferring control to OSE (at address %08lx) ...\n",
207 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
217 #endif /* CONFIG_BOOTM_OSE */
219 #if defined(CONFIG_BOOTM_PLAN9)
220 static int do_bootm_plan9(int flag, int argc, char * const argv[],
221 bootm_headers_t *images)
223 void (*entry_point)(void);
226 if (flag != BOOTM_STATE_OS_GO)
229 #if defined(CONFIG_FIT)
230 if (!images->legacy_hdr_valid) {
231 fit_unsupported_reset("Plan 9");
236 /* See README.plan9 */
237 s = env_get("confaddr");
239 char *confaddr = (char *)simple_strtoul(s, NULL, 16);
242 copy_args(confaddr, argc, argv, '\n');
244 s = env_get("bootargs");
250 entry_point = (void (*)(void))images->ep;
252 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
255 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
265 #endif /* CONFIG_BOOTM_PLAN9 */
267 #if defined(CONFIG_BOOTM_VXWORKS) && \
268 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
270 static void do_bootvx_fdt(bootm_headers_t *images)
272 #if defined(CONFIG_OF_LIBFDT)
275 ulong of_size = images->ft_len;
276 char **of_flat_tree = &images->ft_addr;
277 struct lmb *lmb = &images->lmb;
280 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
282 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
286 /* Update ethernet nodes */
287 fdt_fixup_ethernet(*of_flat_tree);
289 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
290 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
291 bootline = env_get("bootargs");
293 ret = fdt_find_and_setprop(*of_flat_tree,
294 "/chosen", "bootargs",
296 strlen(bootline) + 1, 1);
298 printf("## ERROR: %s : %s\n", __func__,
304 printf("## ERROR: %s : %s\n", __func__,
311 boot_prep_vxworks(images);
313 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
315 #if defined(CONFIG_OF_LIBFDT)
316 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
317 (ulong)images->ep, (ulong)*of_flat_tree);
319 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
322 boot_jump_vxworks(images);
324 puts("## vxWorks terminated\n");
327 static int do_bootm_vxworks_legacy(int flag, int argc, char * const argv[],
328 bootm_headers_t *images)
330 if (flag != BOOTM_STATE_OS_GO)
333 #if defined(CONFIG_FIT)
334 if (!images->legacy_hdr_valid) {
335 fit_unsupported_reset("VxWorks");
340 do_bootvx_fdt(images);
345 int do_bootm_vxworks(int flag, int argc, char * const argv[],
346 bootm_headers_t *images)
350 unsigned long vxflags;
351 bool std_dtb = false;
353 /* get bootargs env */
354 bootargs = env_get("bootargs");
356 if (bootargs != NULL) {
357 for (pos = 0; pos < strlen(bootargs); pos++) {
358 /* find f=0xnumber flag */
359 if ((bootargs[pos] == '=') && (pos >= 1) &&
360 (bootargs[pos - 1] == 'f')) {
361 vxflags = simple_strtoul(&bootargs[pos + 1],
363 if (vxflags & VXWORKS_SYSFLG_STD_DTB)
370 if (flag & BOOTM_STATE_OS_PREP)
371 printf(" Using standard DTB\n");
372 return do_bootm_linux(flag, argc, argv, images);
374 if (flag & BOOTM_STATE_OS_PREP)
375 printf(" !!! WARNING !!! Using legacy DTB\n");
376 return do_bootm_vxworks_legacy(flag, argc, argv, images);
381 #if defined(CONFIG_CMD_ELF)
382 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
383 bootm_headers_t *images)
389 if (flag != BOOTM_STATE_OS_GO)
392 #if defined(CONFIG_FIT)
393 if (!images->legacy_hdr_valid) {
394 fit_unsupported_reset("QNX");
399 sprintf(str, "%lx", images->ep); /* write entry-point into string */
400 local_args[0] = argv[0];
401 local_args[1] = str; /* and provide it via the arguments */
404 * QNX images require the data cache is disabled.
406 dcache = dcache_status();
410 do_bootelf(NULL, 0, 2, local_args);
419 #ifdef CONFIG_INTEGRITY
420 static int do_bootm_integrity(int flag, int argc, char * const argv[],
421 bootm_headers_t *images)
423 void (*entry_point)(void);
425 if (flag != BOOTM_STATE_OS_GO)
428 #if defined(CONFIG_FIT)
429 if (!images->legacy_hdr_valid) {
430 fit_unsupported_reset("INTEGRITY");
435 entry_point = (void (*)(void))images->ep;
437 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
440 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
443 * INTEGRITY Parameters:
452 #ifdef CONFIG_BOOTM_OPENRTOS
453 static int do_bootm_openrtos(int flag, int argc, char * const argv[],
454 bootm_headers_t *images)
456 void (*entry_point)(void);
458 if (flag != BOOTM_STATE_OS_GO)
461 entry_point = (void (*)(void))images->ep;
463 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
466 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
469 * OpenRTOS Parameters:
478 #ifdef CONFIG_BOOTM_OPTEE
479 static int do_bootm_tee(int flag, int argc, char * const argv[],
480 bootm_headers_t *images)
485 if (images->os.os != IH_OS_TEE) {
489 /* Validate OPTEE header */
490 ret = optee_verify_bootm_image(images->os.image_start,
492 images->os.image_len);
497 ret = bootm_find_images(flag, argc, argv);
501 /* From here we can run the regular linux boot path */
502 return do_bootm_linux(flag, argc, argv, images);
506 #ifdef CONFIG_BOOTM_EFI
507 static int do_bootm_efi(int flag, int argc, char * const argv[],
508 bootm_headers_t *images)
511 efi_status_t efi_ret;
514 if (flag != BOOTM_STATE_OS_GO)
517 /* Locate FDT, if provided */
518 ret = bootm_find_images(flag, argc, argv);
522 /* Initialize EFI drivers */
523 efi_ret = efi_init_obj_list();
524 if (efi_ret != EFI_SUCCESS) {
525 printf("## Failed to initialize UEFI sub-system: r = %lu\n",
526 efi_ret & ~EFI_ERROR_MASK);
530 /* Install device tree */
531 efi_ret = efi_install_fdt(images->ft_len
532 ? images->ft_addr : EFI_FDT_USE_INTERNAL);
533 if (efi_ret != EFI_SUCCESS) {
534 printf("## Failed to install device tree: r = %lu\n",
535 efi_ret & ~EFI_ERROR_MASK);
540 printf("## Transferring control to EFI (at address %08lx) ...\n",
542 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
544 image_buf = map_sysmem(images->ep, images->os.image_len);
546 efi_ret = efi_run_image(image_buf, images->os.image_len);
547 if (efi_ret != EFI_SUCCESS) {
548 printf("## Failed to run EFI image: r = %lu\n",
549 efi_ret & ~EFI_ERROR_MASK);
557 static boot_os_fn *boot_os[] = {
558 [IH_OS_U_BOOT] = do_bootm_standalone,
559 #ifdef CONFIG_BOOTM_LINUX
560 [IH_OS_LINUX] = do_bootm_linux,
562 #ifdef CONFIG_BOOTM_NETBSD
563 [IH_OS_NETBSD] = do_bootm_netbsd,
565 #ifdef CONFIG_LYNXKDI
566 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
568 #ifdef CONFIG_BOOTM_RTEMS
569 [IH_OS_RTEMS] = do_bootm_rtems,
571 #if defined(CONFIG_BOOTM_OSE)
572 [IH_OS_OSE] = do_bootm_ose,
574 #if defined(CONFIG_BOOTM_PLAN9)
575 [IH_OS_PLAN9] = do_bootm_plan9,
577 #if defined(CONFIG_BOOTM_VXWORKS) && \
578 (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
579 [IH_OS_VXWORKS] = do_bootm_vxworks,
581 #if defined(CONFIG_CMD_ELF)
582 [IH_OS_QNX] = do_bootm_qnxelf,
584 #ifdef CONFIG_INTEGRITY
585 [IH_OS_INTEGRITY] = do_bootm_integrity,
587 #ifdef CONFIG_BOOTM_OPENRTOS
588 [IH_OS_OPENRTOS] = do_bootm_openrtos,
590 #ifdef CONFIG_BOOTM_OPTEE
591 [IH_OS_TEE] = do_bootm_tee,
593 #ifdef CONFIG_BOOTM_EFI
594 [IH_OS_EFI] = do_bootm_efi,
598 /* Allow for arch specific config before we boot */
599 __weak void arch_preboot_os(void)
601 /* please define platform specific arch_preboot_os() */
604 /* Allow for board specific config before we boot */
605 __weak void board_preboot_os(void)
607 /* please define board specific board_preboot_os() */
610 int boot_selected_os(int argc, char * const argv[], int state,
611 bootm_headers_t *images, boot_os_fn *boot_fn)
615 boot_fn(state, argc, argv, images);
617 /* Stand-alone may return when 'autostart' is 'no' */
618 if (images->os.type == IH_TYPE_STANDALONE ||
619 IS_ENABLED(CONFIG_SANDBOX) ||
620 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
622 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
623 debug("\n## Control returned to monitor - resetting...\n");
625 return BOOTM_ERR_RESET;
628 boot_os_fn *bootm_os_get_boot_func(int os)
630 #ifdef CONFIG_NEEDS_MANUAL_RELOC
631 static bool relocated;
636 /* relocate boot function table */
637 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
638 if (boot_os[i] != NULL)
639 boot_os[i] += gd->reloc_off;