1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
10 #include <efi_loader.h>
12 #include <fdt_support.h>
16 #include <asm/global_data.h>
17 #include <linux/libfdt.h>
21 #include <tee/optee.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 static int do_bootm_standalone(int flag, struct bootm_info *bmi)
27 struct bootm_headers *images = bmi->images;
28 int (*appl)(int, char *const[]);
30 if (!env_get_autostart()) {
31 env_set_hex("filesize", images->os.image_len);
34 appl = (int (*)(int, char * const []))images->ep;
35 appl(bmi->argc, bmi->argv);
39 /*******************************************************************/
40 /* OS booting routines */
41 /*******************************************************************/
43 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
44 static void copy_args(char *dest, int argc, char *const argv[], char delim)
48 for (i = 0; i < argc; i++) {
51 strcpy(dest, argv[i]);
52 dest += strlen(argv[i]);
57 static void __maybe_unused fit_unsupported_reset(const char *msg)
59 if (CONFIG_IS_ENABLED(FIT_VERBOSE)) {
60 printf("! FIT images not supported for '%s' - must reset board to recover!\n",
65 #ifdef CONFIG_BOOTM_NETBSD
66 static int do_bootm_netbsd(int flag, struct bootm_info *bmi)
68 struct bootm_headers *images = bmi->images;
69 void (*loader)(struct bd_info *bd, struct legacy_img_hdr *hdr,
70 char *console, char *cmdline);
71 struct legacy_img_hdr *os_hdr, *hdr;
72 ulong kernel_data, kernel_len;
75 if (flag != BOOTM_STATE_OS_GO)
78 #if defined(CONFIG_FIT)
79 if (!images->legacy_hdr_valid) {
80 fit_unsupported_reset("NetBSD");
84 hdr = images->legacy_hdr_os;
87 * Booting a (NetBSD) kernel image
89 * This process is pretty similar to a standalone application:
90 * The (first part of an multi-) image must be a stage-2 loader,
91 * which in turn is responsible for loading & invoking the actual
92 * kernel. The only differences are the parameters being passed:
93 * besides the board info strucure, the loader expects a command
94 * line, the name of the console device, and (optionally) the
95 * address of the original image header.
98 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
99 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
108 for (i = 0, len = 0; i < bmi->argc; i += 1)
109 len += strlen(bmi->argv[i]) + 1;
110 cmdline = malloc(len);
111 copy_args(cmdline, bmi->argc, bmi->argv, ' ');
113 cmdline = env_get("bootargs");
118 loader = (void (*)(struct bd_info *, struct legacy_img_hdr *, char *, char *))images->ep;
120 printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
123 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
126 * NetBSD Stage-2 Loader Parameters:
127 * arg[0]: pointer to board info data
128 * arg[1]: image load address
129 * arg[2]: char pointer to the console device to use
130 * arg[3]: char pointer to the boot arguments
132 (*loader)(gd->bd, os_hdr, "", cmdline);
136 #endif /* CONFIG_BOOTM_NETBSD*/
138 #ifdef CONFIG_BOOTM_RTEMS
139 static int do_bootm_rtems(int flag, struct bootm_info *bmi)
141 struct bootm_headers *images = bmi->images;
142 void (*entry_point)(struct bd_info *);
144 if (flag != BOOTM_STATE_OS_GO)
147 #if defined(CONFIG_FIT)
148 if (!images->legacy_hdr_valid) {
149 fit_unsupported_reset("RTEMS");
154 entry_point = (void (*)(struct bd_info *))images->ep;
156 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
159 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
163 * r3: ptr to board info data
165 (*entry_point)(gd->bd);
169 #endif /* CONFIG_BOOTM_RTEMS */
171 #if defined(CONFIG_BOOTM_OSE)
172 static int do_bootm_ose(int flag, struct bootm_info *bmi)
174 struct bootm_headers *images = bmi->images;
175 void (*entry_point)(void);
177 if (flag != BOOTM_STATE_OS_GO)
180 #if defined(CONFIG_FIT)
181 if (!images->legacy_hdr_valid) {
182 fit_unsupported_reset("OSE");
187 entry_point = (void (*)(void))images->ep;
189 printf("## Transferring control to OSE (at address %08lx) ...\n",
192 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
202 #endif /* CONFIG_BOOTM_OSE */
204 #if defined(CONFIG_BOOTM_PLAN9)
205 static int do_bootm_plan9(int flag, struct bootm_info *bmi)
207 struct bootm_headers *images = bmi->images;
208 void (*entry_point)(void);
211 if (flag != BOOTM_STATE_OS_GO)
214 #if defined(CONFIG_FIT)
215 if (!images->legacy_hdr_valid) {
216 fit_unsupported_reset("Plan 9");
221 /* See README.plan9 */
222 s = env_get("confaddr");
224 char *confaddr = (char *)hextoul(s, NULL);
227 copy_args(confaddr, bmi->argc, bmi->argv, '\n');
229 s = env_get("bootargs");
235 entry_point = (void (*)(void))images->ep;
237 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
240 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
250 #endif /* CONFIG_BOOTM_PLAN9 */
252 #if defined(CONFIG_BOOTM_VXWORKS) && \
253 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
255 static void do_bootvx_fdt(struct bootm_headers *images)
257 #if defined(CONFIG_OF_LIBFDT)
260 ulong of_size = images->ft_len;
261 char **of_flat_tree = &images->ft_addr;
262 struct lmb *lmb = &images->lmb;
265 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
267 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
271 /* Update ethernet nodes */
272 fdt_fixup_ethernet(*of_flat_tree);
274 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
275 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
276 bootline = env_get("bootargs");
278 ret = fdt_find_and_setprop(*of_flat_tree,
279 "/chosen", "bootargs",
281 strlen(bootline) + 1, 1);
283 printf("## ERROR: %s : %s\n", __func__,
289 printf("## ERROR: %s : %s\n", __func__,
296 boot_prep_vxworks(images);
298 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
300 #if defined(CONFIG_OF_LIBFDT)
301 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
302 (ulong)images->ep, (ulong)*of_flat_tree);
304 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
308 boot_jump_vxworks(images);
310 puts("## vxWorks terminated\n");
313 static int do_bootm_vxworks_legacy(int flag, struct bootm_info *bmi)
315 struct bootm_headers *images = bmi->images;
317 if (flag != BOOTM_STATE_OS_GO)
320 do_bootvx_fdt(images);
325 int do_bootm_vxworks(int flag, struct bootm_info *bmi)
329 unsigned long vxflags;
330 bool std_dtb = false;
332 /* get bootargs env */
333 bootargs = env_get("bootargs");
335 if (bootargs != NULL) {
336 for (pos = 0; pos < strlen(bootargs); pos++) {
337 /* find f=0xnumber flag */
338 if ((bootargs[pos] == '=') && (pos >= 1) &&
339 (bootargs[pos - 1] == 'f')) {
340 vxflags = hextoul(&bootargs[pos + 1], NULL);
341 if (vxflags & VXWORKS_SYSFLG_STD_DTB)
348 if (flag & BOOTM_STATE_OS_PREP)
349 printf(" Using standard DTB\n");
350 return do_bootm_linux(flag, bmi);
352 if (flag & BOOTM_STATE_OS_PREP)
353 printf(" !!! WARNING !!! Using legacy DTB\n");
354 return do_bootm_vxworks_legacy(flag, bmi);
359 #if defined(CONFIG_CMD_ELF)
360 static int do_bootm_qnxelf(int flag, struct bootm_info *bmi)
362 struct bootm_headers *images = bmi->images;
367 if (flag != BOOTM_STATE_OS_GO)
370 #if defined(CONFIG_FIT)
371 if (!images->legacy_hdr_valid) {
372 fit_unsupported_reset("QNX");
377 sprintf(str, "%lx", images->ep); /* write entry-point into string */
378 local_args[0] = bmi->argv[0];
379 local_args[1] = str; /* and provide it via the arguments */
382 * QNX images require the data cache is disabled.
384 dcache = dcache_status();
388 do_bootelf(NULL, 0, 2, local_args);
397 #ifdef CONFIG_INTEGRITY
398 static int do_bootm_integrity(int flag, struct bootm_info *bmi)
400 struct bootm_headers *images = bmi->images;
401 void (*entry_point)(void);
403 if (flag != BOOTM_STATE_OS_GO)
406 #if defined(CONFIG_FIT)
407 if (!images->legacy_hdr_valid) {
408 fit_unsupported_reset("INTEGRITY");
413 entry_point = (void (*)(void))images->ep;
415 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
418 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
421 * INTEGRITY Parameters:
430 #ifdef CONFIG_BOOTM_OPENRTOS
431 static int do_bootm_openrtos(int flag, struct bootm_info *bmi)
433 struct bootm_headers *images = bmi->images;
434 void (*entry_point)(void);
436 if (flag != BOOTM_STATE_OS_GO)
439 entry_point = (void (*)(void))images->ep;
441 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
444 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
447 * OpenRTOS Parameters:
456 #ifdef CONFIG_BOOTM_OPTEE
457 static int do_bootm_tee(int flag, struct bootm_info *bmi)
459 struct bootm_headers *images = bmi->images;
462 /* Validate OPTEE header */
463 ret = optee_verify_bootm_image(images->os.image_start,
465 images->os.image_len);
469 /* From here we can run the regular linux boot path */
470 return do_bootm_linux(flag, bmi);
474 #ifdef CONFIG_BOOTM_EFI
475 static int do_bootm_efi(int flag, struct bootm_info *bmi)
477 struct bootm_headers *images = bmi->images;
481 if (flag != BOOTM_STATE_OS_GO)
484 /* We expect to return */
485 images->os.type = IH_TYPE_STANDALONE;
487 image_buf = map_sysmem(images->ep, images->os.image_len);
490 printf("## Transferring control to EFI (at address %08lx) ...\n",
492 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
494 ret = efi_binary_run(image_buf, images->os.image_len,
496 ? images->ft_addr : EFI_FDT_USE_INTERNAL);
502 static boot_os_fn *boot_os[] = {
503 [IH_OS_U_BOOT] = do_bootm_standalone,
504 #ifdef CONFIG_BOOTM_LINUX
505 [IH_OS_LINUX] = do_bootm_linux,
507 #ifdef CONFIG_BOOTM_NETBSD
508 [IH_OS_NETBSD] = do_bootm_netbsd,
510 #ifdef CONFIG_BOOTM_RTEMS
511 [IH_OS_RTEMS] = do_bootm_rtems,
513 #if defined(CONFIG_BOOTM_OSE)
514 [IH_OS_OSE] = do_bootm_ose,
516 #if defined(CONFIG_BOOTM_PLAN9)
517 [IH_OS_PLAN9] = do_bootm_plan9,
519 #if defined(CONFIG_BOOTM_VXWORKS) && \
520 (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
521 [IH_OS_VXWORKS] = do_bootm_vxworks,
523 #if defined(CONFIG_CMD_ELF)
524 [IH_OS_QNX] = do_bootm_qnxelf,
526 #ifdef CONFIG_INTEGRITY
527 [IH_OS_INTEGRITY] = do_bootm_integrity,
529 #ifdef CONFIG_BOOTM_OPENRTOS
530 [IH_OS_OPENRTOS] = do_bootm_openrtos,
532 #ifdef CONFIG_BOOTM_OPTEE
533 [IH_OS_TEE] = do_bootm_tee,
535 #ifdef CONFIG_BOOTM_EFI
536 [IH_OS_EFI] = do_bootm_efi,
540 /* Allow for arch specific config before we boot */
541 __weak void arch_preboot_os(void)
543 /* please define platform specific arch_preboot_os() */
546 /* Allow for board specific config before we boot */
547 __weak void board_preboot_os(void)
549 /* please define board specific board_preboot_os() */
552 int boot_selected_os(int state, struct bootm_info *bmi, boot_os_fn *boot_fn)
559 /* Stand-alone may return when 'autostart' is 'no' */
560 if (bmi->images->os.type == IH_TYPE_STANDALONE ||
561 IS_ENABLED(CONFIG_SANDBOX) ||
562 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
564 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
565 debug("\n## Control returned to monitor - resetting...\n");
567 return BOOTM_ERR_RESET;
570 boot_os_fn *bootm_os_get_boot_func(int os)