1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
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)
30 int (*appl)(int, char *const[]);
32 /* Don't start if "autostart" is set to "no" */
33 s = env_get("autostart");
34 if ((s != NULL) && !strcmp(s, "no")) {
35 env_set_hex("filesize", images->os.image_len);
38 appl = (int (*)(int, char * const []))images->ep;
43 /*******************************************************************/
44 /* OS booting routines */
45 /*******************************************************************/
47 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
48 static void copy_args(char *dest, int argc, char *const argv[], char delim)
52 for (i = 0; i < argc; i++) {
55 strcpy(dest, argv[i]);
56 dest += strlen(argv[i]);
61 static void __maybe_unused fit_unsupported_reset(const char *msg)
63 if (CONFIG_IS_ENABLED(FIT_VERBOSE)) {
64 printf("! FIT images not supported for '%s' - must reset board to recover!\n",
69 #ifdef CONFIG_BOOTM_NETBSD
70 static int do_bootm_netbsd(int flag, int argc, char *const argv[],
71 bootm_headers_t *images)
73 void (*loader)(struct bd_info *, image_header_t *, char *, char *);
74 image_header_t *os_hdr, *hdr;
75 ulong kernel_data, kernel_len;
78 if (flag != BOOTM_STATE_OS_GO)
81 #if defined(CONFIG_FIT)
82 if (!images->legacy_hdr_valid) {
83 fit_unsupported_reset("NetBSD");
87 hdr = images->legacy_hdr_os;
90 * Booting a (NetBSD) kernel image
92 * This process is pretty similar to a standalone application:
93 * The (first part of an multi-) image must be a stage-2 loader,
94 * which in turn is responsible for loading & invoking the actual
95 * kernel. The only differences are the parameters being passed:
96 * besides the board info strucure, the loader expects a command
97 * line, the name of the console device, and (optionally) the
98 * address of the original image header.
101 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
102 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
111 for (i = 0, len = 0; i < argc; i += 1)
112 len += strlen(argv[i]) + 1;
113 cmdline = malloc(len);
114 copy_args(cmdline, argc, argv, ' ');
116 cmdline = env_get("bootargs");
121 loader = (void (*)(struct bd_info *, image_header_t *, char *, char *))images->ep;
123 printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
126 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
129 * NetBSD Stage-2 Loader Parameters:
130 * arg[0]: pointer to board info data
131 * arg[1]: image load address
132 * arg[2]: char pointer to the console device to use
133 * arg[3]: char pointer to the boot arguments
135 (*loader)(gd->bd, os_hdr, "", cmdline);
139 #endif /* CONFIG_BOOTM_NETBSD*/
141 #ifdef CONFIG_BOOTM_RTEMS
142 static int do_bootm_rtems(int flag, int argc, char *const argv[],
143 bootm_headers_t *images)
145 void (*entry_point)(struct bd_info *);
147 if (flag != BOOTM_STATE_OS_GO)
150 #if defined(CONFIG_FIT)
151 if (!images->legacy_hdr_valid) {
152 fit_unsupported_reset("RTEMS");
157 entry_point = (void (*)(struct bd_info *))images->ep;
159 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
162 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
166 * r3: ptr to board info data
168 (*entry_point)(gd->bd);
172 #endif /* CONFIG_BOOTM_RTEMS */
174 #if defined(CONFIG_BOOTM_OSE)
175 static int do_bootm_ose(int flag, int argc, char *const argv[],
176 bootm_headers_t *images)
178 void (*entry_point)(void);
180 if (flag != BOOTM_STATE_OS_GO)
183 #if defined(CONFIG_FIT)
184 if (!images->legacy_hdr_valid) {
185 fit_unsupported_reset("OSE");
190 entry_point = (void (*)(void))images->ep;
192 printf("## Transferring control to OSE (at address %08lx) ...\n",
195 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
205 #endif /* CONFIG_BOOTM_OSE */
207 #if defined(CONFIG_BOOTM_PLAN9)
208 static int do_bootm_plan9(int flag, int argc, char *const argv[],
209 bootm_headers_t *images)
211 void (*entry_point)(void);
214 if (flag != BOOTM_STATE_OS_GO)
217 #if defined(CONFIG_FIT)
218 if (!images->legacy_hdr_valid) {
219 fit_unsupported_reset("Plan 9");
224 /* See README.plan9 */
225 s = env_get("confaddr");
227 char *confaddr = (char *)hextoul(s, NULL);
230 copy_args(confaddr, argc, argv, '\n');
232 s = env_get("bootargs");
238 entry_point = (void (*)(void))images->ep;
240 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
243 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
253 #endif /* CONFIG_BOOTM_PLAN9 */
255 #if defined(CONFIG_BOOTM_VXWORKS) && \
256 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
258 static void do_bootvx_fdt(bootm_headers_t *images)
260 #if defined(CONFIG_OF_LIBFDT)
263 ulong of_size = images->ft_len;
264 char **of_flat_tree = &images->ft_addr;
265 struct lmb *lmb = &images->lmb;
268 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
270 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
274 /* Update ethernet nodes */
275 fdt_fixup_ethernet(*of_flat_tree);
277 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
278 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
279 bootline = env_get("bootargs");
281 ret = fdt_find_and_setprop(*of_flat_tree,
282 "/chosen", "bootargs",
284 strlen(bootline) + 1, 1);
286 printf("## ERROR: %s : %s\n", __func__,
292 printf("## ERROR: %s : %s\n", __func__,
299 boot_prep_vxworks(images);
301 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
303 #if defined(CONFIG_OF_LIBFDT)
304 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
305 (ulong)images->ep, (ulong)*of_flat_tree);
307 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
310 boot_jump_vxworks(images);
312 puts("## vxWorks terminated\n");
315 static int do_bootm_vxworks_legacy(int flag, int argc, char *const argv[],
316 bootm_headers_t *images)
318 if (flag != BOOTM_STATE_OS_GO)
321 #if defined(CONFIG_FIT)
322 if (!images->legacy_hdr_valid) {
323 fit_unsupported_reset("VxWorks");
328 do_bootvx_fdt(images);
333 int do_bootm_vxworks(int flag, int argc, char *const argv[],
334 bootm_headers_t *images)
338 unsigned long vxflags;
339 bool std_dtb = false;
341 /* get bootargs env */
342 bootargs = env_get("bootargs");
344 if (bootargs != NULL) {
345 for (pos = 0; pos < strlen(bootargs); pos++) {
346 /* find f=0xnumber flag */
347 if ((bootargs[pos] == '=') && (pos >= 1) &&
348 (bootargs[pos - 1] == 'f')) {
349 vxflags = hextoul(&bootargs[pos + 1], NULL);
350 if (vxflags & VXWORKS_SYSFLG_STD_DTB)
357 if (flag & BOOTM_STATE_OS_PREP)
358 printf(" Using standard DTB\n");
359 return do_bootm_linux(flag, argc, argv, images);
361 if (flag & BOOTM_STATE_OS_PREP)
362 printf(" !!! WARNING !!! Using legacy DTB\n");
363 return do_bootm_vxworks_legacy(flag, argc, argv, images);
368 #if defined(CONFIG_CMD_ELF)
369 static int do_bootm_qnxelf(int flag, int argc, char *const argv[],
370 bootm_headers_t *images)
376 if (flag != BOOTM_STATE_OS_GO)
379 #if defined(CONFIG_FIT)
380 if (!images->legacy_hdr_valid) {
381 fit_unsupported_reset("QNX");
386 sprintf(str, "%lx", images->ep); /* write entry-point into string */
387 local_args[0] = argv[0];
388 local_args[1] = str; /* and provide it via the arguments */
391 * QNX images require the data cache is disabled.
393 dcache = dcache_status();
397 do_bootelf(NULL, 0, 2, local_args);
406 #ifdef CONFIG_INTEGRITY
407 static int do_bootm_integrity(int flag, int argc, char *const argv[],
408 bootm_headers_t *images)
410 void (*entry_point)(void);
412 if (flag != BOOTM_STATE_OS_GO)
415 #if defined(CONFIG_FIT)
416 if (!images->legacy_hdr_valid) {
417 fit_unsupported_reset("INTEGRITY");
422 entry_point = (void (*)(void))images->ep;
424 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
427 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
430 * INTEGRITY Parameters:
439 #ifdef CONFIG_BOOTM_OPENRTOS
440 static int do_bootm_openrtos(int flag, int argc, char *const argv[],
441 bootm_headers_t *images)
443 void (*entry_point)(void);
445 if (flag != BOOTM_STATE_OS_GO)
448 entry_point = (void (*)(void))images->ep;
450 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
453 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
456 * OpenRTOS Parameters:
465 #ifdef CONFIG_BOOTM_OPTEE
466 static int do_bootm_tee(int flag, int argc, char *const argv[],
467 bootm_headers_t *images)
472 if (images->os.os != IH_OS_TEE) {
476 /* Validate OPTEE header */
477 ret = optee_verify_bootm_image(images->os.image_start,
479 images->os.image_len);
484 ret = bootm_find_images(flag, argc, argv, 0, 0);
488 /* From here we can run the regular linux boot path */
489 return do_bootm_linux(flag, argc, argv, images);
493 #ifdef CONFIG_BOOTM_EFI
494 static int do_bootm_efi(int flag, int argc, char *const argv[],
495 bootm_headers_t *images)
498 efi_status_t efi_ret;
501 if (flag != BOOTM_STATE_OS_GO)
504 /* Locate FDT, if provided */
505 ret = bootm_find_images(flag, argc, argv, 0, 0);
509 /* Initialize EFI drivers */
510 efi_ret = efi_init_obj_list();
511 if (efi_ret != EFI_SUCCESS) {
512 printf("## Failed to initialize UEFI sub-system: r = %lu\n",
513 efi_ret & ~EFI_ERROR_MASK);
517 /* Install device tree */
518 efi_ret = efi_install_fdt(images->ft_len
519 ? images->ft_addr : EFI_FDT_USE_INTERNAL);
520 if (efi_ret != EFI_SUCCESS) {
521 printf("## Failed to install device tree: r = %lu\n",
522 efi_ret & ~EFI_ERROR_MASK);
527 printf("## Transferring control to EFI (at address %08lx) ...\n",
529 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
531 /* We expect to return */
532 images->os.type = IH_TYPE_STANDALONE;
534 image_buf = map_sysmem(images->ep, images->os.image_len);
536 efi_ret = efi_run_image(image_buf, images->os.image_len);
537 if (efi_ret != EFI_SUCCESS)
543 static boot_os_fn *boot_os[] = {
544 [IH_OS_U_BOOT] = do_bootm_standalone,
545 #ifdef CONFIG_BOOTM_LINUX
546 [IH_OS_LINUX] = do_bootm_linux,
548 #ifdef CONFIG_BOOTM_NETBSD
549 [IH_OS_NETBSD] = do_bootm_netbsd,
551 #ifdef CONFIG_BOOTM_RTEMS
552 [IH_OS_RTEMS] = do_bootm_rtems,
554 #if defined(CONFIG_BOOTM_OSE)
555 [IH_OS_OSE] = do_bootm_ose,
557 #if defined(CONFIG_BOOTM_PLAN9)
558 [IH_OS_PLAN9] = do_bootm_plan9,
560 #if defined(CONFIG_BOOTM_VXWORKS) && \
561 (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
562 [IH_OS_VXWORKS] = do_bootm_vxworks,
564 #if defined(CONFIG_CMD_ELF)
565 [IH_OS_QNX] = do_bootm_qnxelf,
567 #ifdef CONFIG_INTEGRITY
568 [IH_OS_INTEGRITY] = do_bootm_integrity,
570 #ifdef CONFIG_BOOTM_OPENRTOS
571 [IH_OS_OPENRTOS] = do_bootm_openrtos,
573 #ifdef CONFIG_BOOTM_OPTEE
574 [IH_OS_TEE] = do_bootm_tee,
576 #ifdef CONFIG_BOOTM_EFI
577 [IH_OS_EFI] = do_bootm_efi,
581 /* Allow for arch specific config before we boot */
582 __weak void arch_preboot_os(void)
584 /* please define platform specific arch_preboot_os() */
587 /* Allow for board specific config before we boot */
588 __weak void board_preboot_os(void)
590 /* please define board specific board_preboot_os() */
593 int boot_selected_os(int argc, char *const argv[], int state,
594 bootm_headers_t *images, boot_os_fn *boot_fn)
598 boot_fn(state, argc, argv, images);
600 /* Stand-alone may return when 'autostart' is 'no' */
601 if (images->os.type == IH_TYPE_STANDALONE ||
602 IS_ENABLED(CONFIG_SANDBOX) ||
603 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
605 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
606 debug("\n## Control returned to monitor - resetting...\n");
608 return BOOTM_ERR_RESET;
611 boot_os_fn *bootm_os_get_boot_func(int os)
613 #ifdef CONFIG_NEEDS_MANUAL_RELOC
614 static bool relocated;
619 /* relocate boot function table */
620 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
621 if (boot_os[i] != NULL)
622 boot_os[i] += gd->reloc_off;