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, struct bootm_info *bmi)
28 struct bootm_headers *images = bmi->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;
36 appl(bmi->argc, bmi->argv);
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, struct bootm_info *bmi)
69 struct bootm_headers *images = bmi->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 < bmi->argc; i += 1)
110 len += strlen(bmi->argv[i]) + 1;
111 cmdline = malloc(len);
112 copy_args(cmdline, bmi->argc, bmi->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, struct bootm_info *bmi)
142 struct bootm_headers *images = bmi->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, struct bootm_info *bmi)
175 struct bootm_headers *images = bmi->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, struct bootm_info *bmi)
208 struct bootm_headers *images = bmi->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, bmi->argc, bmi->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, struct bootm_info *bmi)
316 struct bootm_headers *images = bmi->images;
318 if (flag != BOOTM_STATE_OS_GO)
321 do_bootvx_fdt(images);
326 int do_bootm_vxworks(int flag, struct bootm_info *bmi)
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, bmi);
353 if (flag & BOOTM_STATE_OS_PREP)
354 printf(" !!! WARNING !!! Using legacy DTB\n");
355 return do_bootm_vxworks_legacy(flag, bmi);
360 #if defined(CONFIG_CMD_ELF)
361 static int do_bootm_qnxelf(int flag, struct bootm_info *bmi)
363 struct bootm_headers *images = bmi->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] = bmi->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, struct bootm_info *bmi)
401 struct bootm_headers *images = bmi->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, struct bootm_info *bmi)
434 struct bootm_headers *images = bmi->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, struct bootm_info *bmi)
460 struct bootm_headers *images = bmi->images;
463 /* Validate OPTEE header */
464 ret = optee_verify_bootm_image(images->os.image_start,
466 images->os.image_len);
470 /* From here we can run the regular linux boot path */
471 return do_bootm_linux(flag, bmi);
475 #ifdef CONFIG_BOOTM_EFI
476 static int do_bootm_efi(int flag, struct bootm_info *bmi)
478 struct bootm_headers *images = bmi->images;
482 if (flag != BOOTM_STATE_OS_GO)
485 /* We expect to return */
486 images->os.type = IH_TYPE_STANDALONE;
488 image_buf = map_sysmem(images->ep, images->os.image_len);
491 printf("## Transferring control to EFI (at address %08lx) ...\n",
493 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
495 ret = efi_binary_run(image_buf, images->os.image_len,
497 ? images->ft_addr : EFI_FDT_USE_INTERNAL);
503 static boot_os_fn *boot_os[] = {
504 [IH_OS_U_BOOT] = do_bootm_standalone,
505 #ifdef CONFIG_BOOTM_LINUX
506 [IH_OS_LINUX] = do_bootm_linux,
508 #ifdef CONFIG_BOOTM_NETBSD
509 [IH_OS_NETBSD] = do_bootm_netbsd,
511 #ifdef CONFIG_BOOTM_RTEMS
512 [IH_OS_RTEMS] = do_bootm_rtems,
514 #if defined(CONFIG_BOOTM_OSE)
515 [IH_OS_OSE] = do_bootm_ose,
517 #if defined(CONFIG_BOOTM_PLAN9)
518 [IH_OS_PLAN9] = do_bootm_plan9,
520 #if defined(CONFIG_BOOTM_VXWORKS) && \
521 (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
522 [IH_OS_VXWORKS] = do_bootm_vxworks,
524 #if defined(CONFIG_CMD_ELF)
525 [IH_OS_QNX] = do_bootm_qnxelf,
527 #ifdef CONFIG_INTEGRITY
528 [IH_OS_INTEGRITY] = do_bootm_integrity,
530 #ifdef CONFIG_BOOTM_OPENRTOS
531 [IH_OS_OPENRTOS] = do_bootm_openrtos,
533 #ifdef CONFIG_BOOTM_OPTEE
534 [IH_OS_TEE] = do_bootm_tee,
536 #ifdef CONFIG_BOOTM_EFI
537 [IH_OS_EFI] = do_bootm_efi,
541 /* Allow for arch specific config before we boot */
542 __weak void arch_preboot_os(void)
544 /* please define platform specific arch_preboot_os() */
547 /* Allow for board specific config before we boot */
548 __weak void board_preboot_os(void)
550 /* please define board specific board_preboot_os() */
553 int boot_selected_os(int state, struct bootm_info *bmi, boot_os_fn *boot_fn)
560 /* Stand-alone may return when 'autostart' is 'no' */
561 if (bmi->images->os.type == IH_TYPE_STANDALONE ||
562 IS_ENABLED(CONFIG_SANDBOX) ||
563 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
565 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
566 debug("\n## Control returned to monitor - resetting...\n");
568 return BOOTM_ERR_RESET;
571 boot_os_fn *bootm_os_get_boot_func(int os)