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 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_LYNXKDI
142 static int do_bootm_lynxkdi(int flag
, int argc
, char *const argv
[],
143 bootm_headers_t
*images
)
145 image_header_t
*hdr
= &images
->legacy_hdr_os_copy
;
147 if (flag
!= BOOTM_STATE_OS_GO
)
150 #if defined(CONFIG_FIT)
151 if (!images
->legacy_hdr_valid
) {
152 fit_unsupported_reset("Lynx");
157 lynxkdi_boot((image_header_t
*)hdr
);
161 #endif /* CONFIG_LYNXKDI */
163 #ifdef CONFIG_BOOTM_RTEMS
164 static int do_bootm_rtems(int flag
, int argc
, char *const argv
[],
165 bootm_headers_t
*images
)
167 void (*entry_point
)(struct bd_info
*);
169 if (flag
!= BOOTM_STATE_OS_GO
)
172 #if defined(CONFIG_FIT)
173 if (!images
->legacy_hdr_valid
) {
174 fit_unsupported_reset("RTEMS");
179 entry_point
= (void (*)(struct bd_info
*))images
->ep
;
181 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
184 bootstage_mark(BOOTSTAGE_ID_RUN_OS
);
188 * r3: ptr to board info data
190 (*entry_point
)(gd
->bd
);
194 #endif /* CONFIG_BOOTM_RTEMS */
196 #if defined(CONFIG_BOOTM_OSE)
197 static int do_bootm_ose(int flag
, int argc
, char *const argv
[],
198 bootm_headers_t
*images
)
200 void (*entry_point
)(void);
202 if (flag
!= BOOTM_STATE_OS_GO
)
205 #if defined(CONFIG_FIT)
206 if (!images
->legacy_hdr_valid
) {
207 fit_unsupported_reset("OSE");
212 entry_point
= (void (*)(void))images
->ep
;
214 printf("## Transferring control to OSE (at address %08lx) ...\n",
217 bootstage_mark(BOOTSTAGE_ID_RUN_OS
);
227 #endif /* CONFIG_BOOTM_OSE */
229 #if defined(CONFIG_BOOTM_PLAN9)
230 static int do_bootm_plan9(int flag
, int argc
, char *const argv
[],
231 bootm_headers_t
*images
)
233 void (*entry_point
)(void);
236 if (flag
!= BOOTM_STATE_OS_GO
)
239 #if defined(CONFIG_FIT)
240 if (!images
->legacy_hdr_valid
) {
241 fit_unsupported_reset("Plan 9");
246 /* See README.plan9 */
247 s
= env_get("confaddr");
249 char *confaddr
= (char *)hextoul(s
, NULL
);
252 copy_args(confaddr
, argc
, argv
, '\n');
254 s
= env_get("bootargs");
260 entry_point
= (void (*)(void))images
->ep
;
262 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
265 bootstage_mark(BOOTSTAGE_ID_RUN_OS
);
275 #endif /* CONFIG_BOOTM_PLAN9 */
277 #if defined(CONFIG_BOOTM_VXWORKS) && \
278 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
280 static void do_bootvx_fdt(bootm_headers_t
*images
)
282 #if defined(CONFIG_OF_LIBFDT)
285 ulong of_size
= images
->ft_len
;
286 char **of_flat_tree
= &images
->ft_addr
;
287 struct lmb
*lmb
= &images
->lmb
;
290 boot_fdt_add_mem_rsv_regions(lmb
, *of_flat_tree
);
292 ret
= boot_relocate_fdt(lmb
, of_flat_tree
, &of_size
);
296 /* Update ethernet nodes */
297 fdt_fixup_ethernet(*of_flat_tree
);
299 ret
= fdt_add_subnode(*of_flat_tree
, 0, "chosen");
300 if ((ret
>= 0 || ret
== -FDT_ERR_EXISTS
)) {
301 bootline
= env_get("bootargs");
303 ret
= fdt_find_and_setprop(*of_flat_tree
,
304 "/chosen", "bootargs",
306 strlen(bootline
) + 1, 1);
308 printf("## ERROR: %s : %s\n", __func__
,
314 printf("## ERROR: %s : %s\n", __func__
,
321 boot_prep_vxworks(images
);
323 bootstage_mark(BOOTSTAGE_ID_RUN_OS
);
325 #if defined(CONFIG_OF_LIBFDT)
326 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
327 (ulong
)images
->ep
, (ulong
)*of_flat_tree
);
329 printf("## Starting vxWorks at 0x%08lx\n", (ulong
)images
->ep
);
332 boot_jump_vxworks(images
);
334 puts("## vxWorks terminated\n");
337 static int do_bootm_vxworks_legacy(int flag
, int argc
, char *const argv
[],
338 bootm_headers_t
*images
)
340 if (flag
!= BOOTM_STATE_OS_GO
)
343 #if defined(CONFIG_FIT)
344 if (!images
->legacy_hdr_valid
) {
345 fit_unsupported_reset("VxWorks");
350 do_bootvx_fdt(images
);
355 int do_bootm_vxworks(int flag
, int argc
, char *const argv
[],
356 bootm_headers_t
*images
)
360 unsigned long vxflags
;
361 bool std_dtb
= false;
363 /* get bootargs env */
364 bootargs
= env_get("bootargs");
366 if (bootargs
!= NULL
) {
367 for (pos
= 0; pos
< strlen(bootargs
); pos
++) {
368 /* find f=0xnumber flag */
369 if ((bootargs
[pos
] == '=') && (pos
>= 1) &&
370 (bootargs
[pos
- 1] == 'f')) {
371 vxflags
= hextoul(&bootargs
[pos
+ 1], NULL
);
372 if (vxflags
& VXWORKS_SYSFLG_STD_DTB
)
379 if (flag
& BOOTM_STATE_OS_PREP
)
380 printf(" Using standard DTB\n");
381 return do_bootm_linux(flag
, argc
, argv
, images
);
383 if (flag
& BOOTM_STATE_OS_PREP
)
384 printf(" !!! WARNING !!! Using legacy DTB\n");
385 return do_bootm_vxworks_legacy(flag
, argc
, argv
, images
);
390 #if defined(CONFIG_CMD_ELF)
391 static int do_bootm_qnxelf(int flag
, int argc
, char *const argv
[],
392 bootm_headers_t
*images
)
398 if (flag
!= BOOTM_STATE_OS_GO
)
401 #if defined(CONFIG_FIT)
402 if (!images
->legacy_hdr_valid
) {
403 fit_unsupported_reset("QNX");
408 sprintf(str
, "%lx", images
->ep
); /* write entry-point into string */
409 local_args
[0] = argv
[0];
410 local_args
[1] = str
; /* and provide it via the arguments */
413 * QNX images require the data cache is disabled.
415 dcache
= dcache_status();
419 do_bootelf(NULL
, 0, 2, local_args
);
428 #ifdef CONFIG_INTEGRITY
429 static int do_bootm_integrity(int flag
, int argc
, char *const argv
[],
430 bootm_headers_t
*images
)
432 void (*entry_point
)(void);
434 if (flag
!= BOOTM_STATE_OS_GO
)
437 #if defined(CONFIG_FIT)
438 if (!images
->legacy_hdr_valid
) {
439 fit_unsupported_reset("INTEGRITY");
444 entry_point
= (void (*)(void))images
->ep
;
446 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
449 bootstage_mark(BOOTSTAGE_ID_RUN_OS
);
452 * INTEGRITY Parameters:
461 #ifdef CONFIG_BOOTM_OPENRTOS
462 static int do_bootm_openrtos(int flag
, int argc
, char *const argv
[],
463 bootm_headers_t
*images
)
465 void (*entry_point
)(void);
467 if (flag
!= BOOTM_STATE_OS_GO
)
470 entry_point
= (void (*)(void))images
->ep
;
472 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
475 bootstage_mark(BOOTSTAGE_ID_RUN_OS
);
478 * OpenRTOS Parameters:
487 #ifdef CONFIG_BOOTM_OPTEE
488 static int do_bootm_tee(int flag
, int argc
, char *const argv
[],
489 bootm_headers_t
*images
)
494 if (images
->os
.os
!= IH_OS_TEE
) {
498 /* Validate OPTEE header */
499 ret
= optee_verify_bootm_image(images
->os
.image_start
,
501 images
->os
.image_len
);
506 ret
= bootm_find_images(flag
, argc
, argv
, 0, 0);
510 /* From here we can run the regular linux boot path */
511 return do_bootm_linux(flag
, argc
, argv
, images
);
515 #ifdef CONFIG_BOOTM_EFI
516 static int do_bootm_efi(int flag
, int argc
, char *const argv
[],
517 bootm_headers_t
*images
)
520 efi_status_t efi_ret
;
523 if (flag
!= BOOTM_STATE_OS_GO
)
526 /* Locate FDT, if provided */
527 ret
= bootm_find_images(flag
, argc
, argv
, 0, 0);
531 /* Initialize EFI drivers */
532 efi_ret
= efi_init_obj_list();
533 if (efi_ret
!= EFI_SUCCESS
) {
534 printf("## Failed to initialize UEFI sub-system: r = %lu\n",
535 efi_ret
& ~EFI_ERROR_MASK
);
539 /* Install device tree */
540 efi_ret
= efi_install_fdt(images
->ft_len
541 ? images
->ft_addr
: EFI_FDT_USE_INTERNAL
);
542 if (efi_ret
!= EFI_SUCCESS
) {
543 printf("## Failed to install device tree: r = %lu\n",
544 efi_ret
& ~EFI_ERROR_MASK
);
549 printf("## Transferring control to EFI (at address %08lx) ...\n",
551 bootstage_mark(BOOTSTAGE_ID_RUN_OS
);
553 /* We expect to return */
554 images
->os
.type
= IH_TYPE_STANDALONE
;
556 image_buf
= map_sysmem(images
->ep
, images
->os
.image_len
);
558 efi_ret
= efi_run_image(image_buf
, images
->os
.image_len
);
559 if (efi_ret
!= EFI_SUCCESS
)
565 static boot_os_fn
*boot_os
[] = {
566 [IH_OS_U_BOOT
] = do_bootm_standalone
,
567 #ifdef CONFIG_BOOTM_LINUX
568 [IH_OS_LINUX
] = do_bootm_linux
,
570 #ifdef CONFIG_BOOTM_NETBSD
571 [IH_OS_NETBSD
] = do_bootm_netbsd
,
573 #ifdef CONFIG_LYNXKDI
574 [IH_OS_LYNXOS
] = do_bootm_lynxkdi
,
576 #ifdef CONFIG_BOOTM_RTEMS
577 [IH_OS_RTEMS
] = do_bootm_rtems
,
579 #if defined(CONFIG_BOOTM_OSE)
580 [IH_OS_OSE
] = do_bootm_ose
,
582 #if defined(CONFIG_BOOTM_PLAN9)
583 [IH_OS_PLAN9
] = do_bootm_plan9
,
585 #if defined(CONFIG_BOOTM_VXWORKS) && \
586 (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
587 [IH_OS_VXWORKS
] = do_bootm_vxworks
,
589 #if defined(CONFIG_CMD_ELF)
590 [IH_OS_QNX
] = do_bootm_qnxelf
,
592 #ifdef CONFIG_INTEGRITY
593 [IH_OS_INTEGRITY
] = do_bootm_integrity
,
595 #ifdef CONFIG_BOOTM_OPENRTOS
596 [IH_OS_OPENRTOS
] = do_bootm_openrtos
,
598 #ifdef CONFIG_BOOTM_OPTEE
599 [IH_OS_TEE
] = do_bootm_tee
,
601 #ifdef CONFIG_BOOTM_EFI
602 [IH_OS_EFI
] = do_bootm_efi
,
606 /* Allow for arch specific config before we boot */
607 __weak
void arch_preboot_os(void)
609 /* please define platform specific arch_preboot_os() */
612 /* Allow for board specific config before we boot */
613 __weak
void board_preboot_os(void)
615 /* please define board specific board_preboot_os() */
618 int boot_selected_os(int argc
, char *const argv
[], int state
,
619 bootm_headers_t
*images
, boot_os_fn
*boot_fn
)
623 boot_fn(state
, argc
, argv
, images
);
625 /* Stand-alone may return when 'autostart' is 'no' */
626 if (images
->os
.type
== IH_TYPE_STANDALONE
||
627 IS_ENABLED(CONFIG_SANDBOX
) ||
628 state
== BOOTM_STATE_OS_FAKE_GO
) /* We expect to return */
630 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED
);
631 debug("\n## Control returned to monitor - resetting...\n");
633 return BOOTM_ERR_RESET
;
636 boot_os_fn
*bootm_os_get_boot_func(int os
)
638 #ifdef CONFIG_NEEDS_MANUAL_RELOC
639 static bool relocated
;
644 /* relocate boot function table */
645 for (i
= 0; i
< ARRAY_SIZE(boot_os
); i
++)
646 if (boot_os
[i
] != NULL
)
647 boot_os
[i
] += gd
->reloc_off
;