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)
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 #ifdef CONFIG_BOOTM_NETBSD
62 static int do_bootm_netbsd(int flag, int argc, char *const argv[],
63 bootm_headers_t *images)
65 void (*loader)(struct bd_info *, image_header_t *, char *, char *);
66 image_header_t *os_hdr, *hdr;
67 ulong kernel_data, kernel_len;
70 if (flag != BOOTM_STATE_OS_GO)
73 #if defined(CONFIG_FIT)
74 if (!images->legacy_hdr_valid) {
75 fit_unsupported_reset("NetBSD");
79 hdr = images->legacy_hdr_os;
82 * Booting a (NetBSD) kernel image
84 * This process is pretty similar to a standalone application:
85 * The (first part of an multi-) image must be a stage-2 loader,
86 * which in turn is responsible for loading & invoking the actual
87 * kernel. The only differences are the parameters being passed:
88 * besides the board info strucure, the loader expects a command
89 * line, the name of the console device, and (optionally) the
90 * address of the original image header.
93 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
94 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
103 for (i = 0, len = 0; i < argc; i += 1)
104 len += strlen(argv[i]) + 1;
105 cmdline = malloc(len);
106 copy_args(cmdline, argc, argv, ' ');
108 cmdline = env_get("bootargs");
113 loader = (void (*)(struct bd_info *, image_header_t *, char *, char *))images->ep;
115 printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
118 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
121 * NetBSD Stage-2 Loader Parameters:
122 * arg[0]: pointer to board info data
123 * arg[1]: image load address
124 * arg[2]: char pointer to the console device to use
125 * arg[3]: char pointer to the boot arguments
127 (*loader)(gd->bd, os_hdr, "", cmdline);
131 #endif /* CONFIG_BOOTM_NETBSD*/
133 #ifdef CONFIG_LYNXKDI
134 static int do_bootm_lynxkdi(int flag, int argc, char *const argv[],
135 bootm_headers_t *images)
137 image_header_t *hdr = &images->legacy_hdr_os_copy;
139 if (flag != BOOTM_STATE_OS_GO)
142 #if defined(CONFIG_FIT)
143 if (!images->legacy_hdr_valid) {
144 fit_unsupported_reset("Lynx");
149 lynxkdi_boot((image_header_t *)hdr);
153 #endif /* CONFIG_LYNXKDI */
155 #ifdef CONFIG_BOOTM_RTEMS
156 static int do_bootm_rtems(int flag, int argc, char *const argv[],
157 bootm_headers_t *images)
159 void (*entry_point)(struct bd_info *);
161 if (flag != BOOTM_STATE_OS_GO)
164 #if defined(CONFIG_FIT)
165 if (!images->legacy_hdr_valid) {
166 fit_unsupported_reset("RTEMS");
171 entry_point = (void (*)(struct bd_info *))images->ep;
173 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
176 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
180 * r3: ptr to board info data
182 (*entry_point)(gd->bd);
186 #endif /* CONFIG_BOOTM_RTEMS */
188 #if defined(CONFIG_BOOTM_OSE)
189 static int do_bootm_ose(int flag, int argc, char *const argv[],
190 bootm_headers_t *images)
192 void (*entry_point)(void);
194 if (flag != BOOTM_STATE_OS_GO)
197 #if defined(CONFIG_FIT)
198 if (!images->legacy_hdr_valid) {
199 fit_unsupported_reset("OSE");
204 entry_point = (void (*)(void))images->ep;
206 printf("## Transferring control to OSE (at address %08lx) ...\n",
209 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
219 #endif /* CONFIG_BOOTM_OSE */
221 #if defined(CONFIG_BOOTM_PLAN9)
222 static int do_bootm_plan9(int flag, int argc, char *const argv[],
223 bootm_headers_t *images)
225 void (*entry_point)(void);
228 if (flag != BOOTM_STATE_OS_GO)
231 #if defined(CONFIG_FIT)
232 if (!images->legacy_hdr_valid) {
233 fit_unsupported_reset("Plan 9");
238 /* See README.plan9 */
239 s = env_get("confaddr");
241 char *confaddr = (char *)hextoul(s, NULL);
244 copy_args(confaddr, argc, argv, '\n');
246 s = env_get("bootargs");
252 entry_point = (void (*)(void))images->ep;
254 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
257 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
267 #endif /* CONFIG_BOOTM_PLAN9 */
269 #if defined(CONFIG_BOOTM_VXWORKS) && \
270 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
272 static void do_bootvx_fdt(bootm_headers_t *images)
274 #if defined(CONFIG_OF_LIBFDT)
277 ulong of_size = images->ft_len;
278 char **of_flat_tree = &images->ft_addr;
279 struct lmb *lmb = &images->lmb;
282 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
284 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
288 /* Update ethernet nodes */
289 fdt_fixup_ethernet(*of_flat_tree);
291 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
292 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
293 bootline = env_get("bootargs");
295 ret = fdt_find_and_setprop(*of_flat_tree,
296 "/chosen", "bootargs",
298 strlen(bootline) + 1, 1);
300 printf("## ERROR: %s : %s\n", __func__,
306 printf("## ERROR: %s : %s\n", __func__,
313 boot_prep_vxworks(images);
315 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
317 #if defined(CONFIG_OF_LIBFDT)
318 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
319 (ulong)images->ep, (ulong)*of_flat_tree);
321 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
324 boot_jump_vxworks(images);
326 puts("## vxWorks terminated\n");
329 static int do_bootm_vxworks_legacy(int flag, int argc, char *const argv[],
330 bootm_headers_t *images)
332 if (flag != BOOTM_STATE_OS_GO)
335 #if defined(CONFIG_FIT)
336 if (!images->legacy_hdr_valid) {
337 fit_unsupported_reset("VxWorks");
342 do_bootvx_fdt(images);
347 int do_bootm_vxworks(int flag, int argc, char *const argv[],
348 bootm_headers_t *images)
352 unsigned long vxflags;
353 bool std_dtb = false;
355 /* get bootargs env */
356 bootargs = env_get("bootargs");
358 if (bootargs != NULL) {
359 for (pos = 0; pos < strlen(bootargs); pos++) {
360 /* find f=0xnumber flag */
361 if ((bootargs[pos] == '=') && (pos >= 1) &&
362 (bootargs[pos - 1] == 'f')) {
363 vxflags = hextoul(&bootargs[pos + 1], NULL);
364 if (vxflags & VXWORKS_SYSFLG_STD_DTB)
371 if (flag & BOOTM_STATE_OS_PREP)
372 printf(" Using standard DTB\n");
373 return do_bootm_linux(flag, argc, argv, images);
375 if (flag & BOOTM_STATE_OS_PREP)
376 printf(" !!! WARNING !!! Using legacy DTB\n");
377 return do_bootm_vxworks_legacy(flag, argc, argv, images);
382 #if defined(CONFIG_CMD_ELF)
383 static int do_bootm_qnxelf(int flag, int argc, char *const argv[],
384 bootm_headers_t *images)
390 if (flag != BOOTM_STATE_OS_GO)
393 #if defined(CONFIG_FIT)
394 if (!images->legacy_hdr_valid) {
395 fit_unsupported_reset("QNX");
400 sprintf(str, "%lx", images->ep); /* write entry-point into string */
401 local_args[0] = argv[0];
402 local_args[1] = str; /* and provide it via the arguments */
405 * QNX images require the data cache is disabled.
407 dcache = dcache_status();
411 do_bootelf(NULL, 0, 2, local_args);
420 #ifdef CONFIG_INTEGRITY
421 static int do_bootm_integrity(int flag, int argc, char *const argv[],
422 bootm_headers_t *images)
424 void (*entry_point)(void);
426 if (flag != BOOTM_STATE_OS_GO)
429 #if defined(CONFIG_FIT)
430 if (!images->legacy_hdr_valid) {
431 fit_unsupported_reset("INTEGRITY");
436 entry_point = (void (*)(void))images->ep;
438 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
441 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
444 * INTEGRITY Parameters:
453 #ifdef CONFIG_BOOTM_OPENRTOS
454 static int do_bootm_openrtos(int flag, int argc, char *const argv[],
455 bootm_headers_t *images)
457 void (*entry_point)(void);
459 if (flag != BOOTM_STATE_OS_GO)
462 entry_point = (void (*)(void))images->ep;
464 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
467 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
470 * OpenRTOS Parameters:
479 #ifdef CONFIG_BOOTM_OPTEE
480 static int do_bootm_tee(int flag, int argc, char *const argv[],
481 bootm_headers_t *images)
486 if (images->os.os != IH_OS_TEE) {
490 /* Validate OPTEE header */
491 ret = optee_verify_bootm_image(images->os.image_start,
493 images->os.image_len);
498 ret = bootm_find_images(flag, argc, argv, 0, 0);
502 /* From here we can run the regular linux boot path */
503 return do_bootm_linux(flag, argc, argv, images);
507 #ifdef CONFIG_BOOTM_EFI
508 static int do_bootm_efi(int flag, int argc, char *const argv[],
509 bootm_headers_t *images)
512 efi_status_t efi_ret;
515 if (flag != BOOTM_STATE_OS_GO)
518 /* Locate FDT, if provided */
519 ret = bootm_find_images(flag, argc, argv, 0, 0);
523 /* Initialize EFI drivers */
524 efi_ret = efi_init_obj_list();
525 if (efi_ret != EFI_SUCCESS) {
526 printf("## Failed to initialize UEFI sub-system: r = %lu\n",
527 efi_ret & ~EFI_ERROR_MASK);
531 /* Install device tree */
532 efi_ret = efi_install_fdt(images->ft_len
533 ? images->ft_addr : EFI_FDT_USE_INTERNAL);
534 if (efi_ret != EFI_SUCCESS) {
535 printf("## Failed to install device tree: r = %lu\n",
536 efi_ret & ~EFI_ERROR_MASK);
541 printf("## Transferring control to EFI (at address %08lx) ...\n",
543 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
545 /* We expect to return */
546 images->os.type = IH_TYPE_STANDALONE;
548 image_buf = map_sysmem(images->ep, images->os.image_len);
550 efi_ret = efi_run_image(image_buf, images->os.image_len);
551 if (efi_ret != EFI_SUCCESS)
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;