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 struct bootm_headers *images)
29 int (*appl)(int, char *const[]);
31 if (!env_get_autostart()) {
32 env_set_hex("filesize", images->os.image_len);
35 appl = (int (*)(int, char * const []))images->ep;
40 /*******************************************************************/
41 /* OS booting routines */
42 /*******************************************************************/
44 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
45 static void copy_args(char *dest, int argc, char *const argv[], char delim)
49 for (i = 0; i < argc; i++) {
52 strcpy(dest, argv[i]);
53 dest += strlen(argv[i]);
58 static void __maybe_unused fit_unsupported_reset(const char *msg)
60 if (CONFIG_IS_ENABLED(FIT_VERBOSE)) {
61 printf("! FIT images not supported for '%s' - must reset board to recover!\n",
66 #ifdef CONFIG_BOOTM_NETBSD
67 static int do_bootm_netbsd(int flag, int argc, char *const argv[],
68 struct bootm_headers *images)
70 void (*loader)(struct bd_info *bd, struct legacy_img_hdr *hdr,
71 char *console, char *cmdline);
72 struct legacy_img_hdr *os_hdr, *hdr;
73 ulong kernel_data, kernel_len;
76 if (flag != BOOTM_STATE_OS_GO)
79 #if defined(CONFIG_FIT)
80 if (!images->legacy_hdr_valid) {
81 fit_unsupported_reset("NetBSD");
85 hdr = images->legacy_hdr_os;
88 * Booting a (NetBSD) kernel image
90 * This process is pretty similar to a standalone application:
91 * The (first part of an multi-) image must be a stage-2 loader,
92 * which in turn is responsible for loading & invoking the actual
93 * kernel. The only differences are the parameters being passed:
94 * besides the board info strucure, the loader expects a command
95 * line, the name of the console device, and (optionally) the
96 * address of the original image header.
99 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
100 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
109 for (i = 0, len = 0; i < argc; i += 1)
110 len += strlen(argv[i]) + 1;
111 cmdline = malloc(len);
112 copy_args(cmdline, argc, argv, ' ');
114 cmdline = env_get("bootargs");
119 loader = (void (*)(struct bd_info *, struct legacy_img_hdr *, char *, char *))images->ep;
121 printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
124 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
127 * NetBSD Stage-2 Loader Parameters:
128 * arg[0]: pointer to board info data
129 * arg[1]: image load address
130 * arg[2]: char pointer to the console device to use
131 * arg[3]: char pointer to the boot arguments
133 (*loader)(gd->bd, os_hdr, "", cmdline);
137 #endif /* CONFIG_BOOTM_NETBSD*/
139 #ifdef CONFIG_BOOTM_RTEMS
140 static int do_bootm_rtems(int flag, int argc, char *const argv[],
141 struct bootm_headers *images)
143 void (*entry_point)(struct bd_info *);
145 if (flag != BOOTM_STATE_OS_GO)
148 #if defined(CONFIG_FIT)
149 if (!images->legacy_hdr_valid) {
150 fit_unsupported_reset("RTEMS");
155 entry_point = (void (*)(struct bd_info *))images->ep;
157 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
160 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
164 * r3: ptr to board info data
166 (*entry_point)(gd->bd);
170 #endif /* CONFIG_BOOTM_RTEMS */
172 #if defined(CONFIG_BOOTM_OSE)
173 static int do_bootm_ose(int flag, int argc, char *const argv[],
174 struct bootm_headers *images)
176 void (*entry_point)(void);
178 if (flag != BOOTM_STATE_OS_GO)
181 #if defined(CONFIG_FIT)
182 if (!images->legacy_hdr_valid) {
183 fit_unsupported_reset("OSE");
188 entry_point = (void (*)(void))images->ep;
190 printf("## Transferring control to OSE (at address %08lx) ...\n",
193 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
203 #endif /* CONFIG_BOOTM_OSE */
205 #if defined(CONFIG_BOOTM_PLAN9)
206 static int do_bootm_plan9(int flag, int argc, char *const argv[],
207 struct bootm_headers *images)
209 void (*entry_point)(void);
212 if (flag != BOOTM_STATE_OS_GO)
215 #if defined(CONFIG_FIT)
216 if (!images->legacy_hdr_valid) {
217 fit_unsupported_reset("Plan 9");
222 /* See README.plan9 */
223 s = env_get("confaddr");
225 char *confaddr = (char *)hextoul(s, NULL);
228 copy_args(confaddr, argc, argv, '\n');
230 s = env_get("bootargs");
236 entry_point = (void (*)(void))images->ep;
238 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
241 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
251 #endif /* CONFIG_BOOTM_PLAN9 */
253 #if defined(CONFIG_BOOTM_VXWORKS) && \
254 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
256 static void do_bootvx_fdt(struct bootm_headers *images)
258 #if defined(CONFIG_OF_LIBFDT)
261 ulong of_size = images->ft_len;
262 char **of_flat_tree = &images->ft_addr;
263 struct lmb *lmb = &images->lmb;
266 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
268 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
272 /* Update ethernet nodes */
273 fdt_fixup_ethernet(*of_flat_tree);
275 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
276 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
277 bootline = env_get("bootargs");
279 ret = fdt_find_and_setprop(*of_flat_tree,
280 "/chosen", "bootargs",
282 strlen(bootline) + 1, 1);
284 printf("## ERROR: %s : %s\n", __func__,
290 printf("## ERROR: %s : %s\n", __func__,
297 boot_prep_vxworks(images);
299 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
301 #if defined(CONFIG_OF_LIBFDT)
302 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
303 (ulong)images->ep, (ulong)*of_flat_tree);
305 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
309 boot_jump_vxworks(images);
311 puts("## vxWorks terminated\n");
314 static int do_bootm_vxworks_legacy(int flag, int argc, char *const argv[],
315 struct bootm_headers *images)
317 if (flag != BOOTM_STATE_OS_GO)
320 do_bootvx_fdt(images);
325 int do_bootm_vxworks(int flag, int argc, char *const argv[],
326 struct bootm_headers *images)
330 unsigned long vxflags;
331 bool std_dtb = false;
333 /* get bootargs env */
334 bootargs = env_get("bootargs");
336 if (bootargs != NULL) {
337 for (pos = 0; pos < strlen(bootargs); pos++) {
338 /* find f=0xnumber flag */
339 if ((bootargs[pos] == '=') && (pos >= 1) &&
340 (bootargs[pos - 1] == 'f')) {
341 vxflags = hextoul(&bootargs[pos + 1], NULL);
342 if (vxflags & VXWORKS_SYSFLG_STD_DTB)
349 if (flag & BOOTM_STATE_OS_PREP)
350 printf(" Using standard DTB\n");
351 return do_bootm_linux(flag, argc, argv, images);
353 if (flag & BOOTM_STATE_OS_PREP)
354 printf(" !!! WARNING !!! Using legacy DTB\n");
355 return do_bootm_vxworks_legacy(flag, argc, argv, images);
360 #if defined(CONFIG_CMD_ELF)
361 static int do_bootm_qnxelf(int flag, int argc, char *const argv[],
362 struct bootm_headers *images)
368 if (flag != BOOTM_STATE_OS_GO)
371 #if defined(CONFIG_FIT)
372 if (!images->legacy_hdr_valid) {
373 fit_unsupported_reset("QNX");
378 sprintf(str, "%lx", images->ep); /* write entry-point into string */
379 local_args[0] = argv[0];
380 local_args[1] = str; /* and provide it via the arguments */
383 * QNX images require the data cache is disabled.
385 dcache = dcache_status();
389 do_bootelf(NULL, 0, 2, local_args);
398 #ifdef CONFIG_INTEGRITY
399 static int do_bootm_integrity(int flag, int argc, char *const argv[],
400 struct bootm_headers *images)
402 void (*entry_point)(void);
404 if (flag != BOOTM_STATE_OS_GO)
407 #if defined(CONFIG_FIT)
408 if (!images->legacy_hdr_valid) {
409 fit_unsupported_reset("INTEGRITY");
414 entry_point = (void (*)(void))images->ep;
416 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
419 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
422 * INTEGRITY Parameters:
431 #ifdef CONFIG_BOOTM_OPENRTOS
432 static int do_bootm_openrtos(int flag, int argc, char *const argv[],
433 struct bootm_headers *images)
435 void (*entry_point)(void);
437 if (flag != BOOTM_STATE_OS_GO)
440 entry_point = (void (*)(void))images->ep;
442 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
445 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
448 * OpenRTOS Parameters:
457 #ifdef CONFIG_BOOTM_OPTEE
458 static int do_bootm_tee(int flag, int argc, char *const argv[],
459 struct bootm_headers *images)
464 if (images->os.os != IH_OS_TEE) {
468 /* Validate OPTEE header */
469 ret = optee_verify_bootm_image(images->os.image_start,
471 images->os.image_len);
476 ret = bootm_find_images(flag, argc, argv, 0, 0);
480 /* From here we can run the regular linux boot path */
481 return do_bootm_linux(flag, argc, argv, images);
485 #ifdef CONFIG_BOOTM_EFI
486 static int do_bootm_efi(int flag, int argc, char *const argv[],
487 struct bootm_headers *images)
490 efi_status_t efi_ret;
493 if (flag != BOOTM_STATE_OS_GO)
496 /* Locate FDT, if provided */
497 ret = bootm_find_images(flag, argc, argv, 0, 0);
501 /* Initialize EFI drivers */
502 efi_ret = efi_init_obj_list();
503 if (efi_ret != EFI_SUCCESS) {
504 printf("## Failed to initialize UEFI sub-system: r = %lu\n",
505 efi_ret & ~EFI_ERROR_MASK);
509 /* Install device tree */
510 efi_ret = efi_install_fdt(images->ft_len
511 ? images->ft_addr : EFI_FDT_USE_INTERNAL);
512 if (efi_ret != EFI_SUCCESS) {
513 printf("## Failed to install device tree: r = %lu\n",
514 efi_ret & ~EFI_ERROR_MASK);
519 printf("## Transferring control to EFI (at address %08lx) ...\n",
521 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
523 /* We expect to return */
524 images->os.type = IH_TYPE_STANDALONE;
526 image_buf = map_sysmem(images->ep, images->os.image_len);
528 efi_ret = efi_run_image(image_buf, images->os.image_len);
529 if (efi_ret != EFI_SUCCESS)
535 static boot_os_fn *boot_os[] = {
536 [IH_OS_U_BOOT] = do_bootm_standalone,
537 #ifdef CONFIG_BOOTM_LINUX
538 [IH_OS_LINUX] = do_bootm_linux,
540 #ifdef CONFIG_BOOTM_NETBSD
541 [IH_OS_NETBSD] = do_bootm_netbsd,
543 #ifdef CONFIG_BOOTM_RTEMS
544 [IH_OS_RTEMS] = do_bootm_rtems,
546 #if defined(CONFIG_BOOTM_OSE)
547 [IH_OS_OSE] = do_bootm_ose,
549 #if defined(CONFIG_BOOTM_PLAN9)
550 [IH_OS_PLAN9] = do_bootm_plan9,
552 #if defined(CONFIG_BOOTM_VXWORKS) && \
553 (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
554 [IH_OS_VXWORKS] = do_bootm_vxworks,
556 #if defined(CONFIG_CMD_ELF)
557 [IH_OS_QNX] = do_bootm_qnxelf,
559 #ifdef CONFIG_INTEGRITY
560 [IH_OS_INTEGRITY] = do_bootm_integrity,
562 #ifdef CONFIG_BOOTM_OPENRTOS
563 [IH_OS_OPENRTOS] = do_bootm_openrtos,
565 #ifdef CONFIG_BOOTM_OPTEE
566 [IH_OS_TEE] = do_bootm_tee,
568 #ifdef CONFIG_BOOTM_EFI
569 [IH_OS_EFI] = do_bootm_efi,
573 /* Allow for arch specific config before we boot */
574 __weak void arch_preboot_os(void)
576 /* please define platform specific arch_preboot_os() */
579 /* Allow for board specific config before we boot */
580 __weak void board_preboot_os(void)
582 /* please define board specific board_preboot_os() */
585 int boot_selected_os(int argc, char *const argv[], int state,
586 struct bootm_headers *images, boot_os_fn *boot_fn)
590 boot_fn(state, argc, argv, images);
592 /* Stand-alone may return when 'autostart' is 'no' */
593 if (images->os.type == IH_TYPE_STANDALONE ||
594 IS_ENABLED(CONFIG_SANDBOX) ||
595 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
597 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
598 debug("\n## Control returned to monitor - resetting...\n");
600 return BOOTM_ERR_RESET;
603 boot_os_fn *bootm_os_get_boot_func(int os)