1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 #include <fdt_support.h>
12 #include <linux/libfdt.h>
15 #include <tee/optee.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 static int do_bootm_standalone(int flag, int argc, char * const argv[],
20 bootm_headers_t *images)
23 int (*appl)(int, char *const[]);
25 /* Don't start if "autostart" is set to "no" */
26 s = env_get("autostart");
27 if ((s != NULL) && !strcmp(s, "no")) {
28 env_set_hex("filesize", images->os.image_len);
31 appl = (int (*)(int, char * const []))images->ep;
36 /*******************************************************************/
37 /* OS booting routines */
38 /*******************************************************************/
40 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
41 static void copy_args(char *dest, int argc, char * const argv[], char delim)
45 for (i = 0; i < argc; i++) {
48 strcpy(dest, argv[i]);
49 dest += strlen(argv[i]);
54 #ifdef CONFIG_BOOTM_NETBSD
55 static int do_bootm_netbsd(int flag, int argc, char * const argv[],
56 bootm_headers_t *images)
58 void (*loader)(bd_t *, image_header_t *, char *, char *);
59 image_header_t *os_hdr, *hdr;
60 ulong kernel_data, kernel_len;
63 if (flag != BOOTM_STATE_OS_GO)
66 #if defined(CONFIG_FIT)
67 if (!images->legacy_hdr_valid) {
68 fit_unsupported_reset("NetBSD");
72 hdr = images->legacy_hdr_os;
75 * Booting a (NetBSD) kernel image
77 * This process is pretty similar to a standalone application:
78 * The (first part of an multi-) image must be a stage-2 loader,
79 * which in turn is responsible for loading & invoking the actual
80 * kernel. The only differences are the parameters being passed:
81 * besides the board info strucure, the loader expects a command
82 * line, the name of the console device, and (optionally) the
83 * address of the original image header.
86 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
87 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
96 for (i = 0, len = 0; i < argc; i += 1)
97 len += strlen(argv[i]) + 1;
98 cmdline = malloc(len);
99 copy_args(cmdline, argc, argv, ' ');
101 cmdline = env_get("bootargs");
106 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
108 printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
111 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
114 * NetBSD Stage-2 Loader Parameters:
115 * arg[0]: pointer to board info data
116 * arg[1]: image load address
117 * arg[2]: char pointer to the console device to use
118 * arg[3]: char pointer to the boot arguments
120 (*loader)(gd->bd, os_hdr, "", cmdline);
124 #endif /* CONFIG_BOOTM_NETBSD*/
126 #ifdef CONFIG_LYNXKDI
127 static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
128 bootm_headers_t *images)
130 image_header_t *hdr = &images->legacy_hdr_os_copy;
132 if (flag != BOOTM_STATE_OS_GO)
135 #if defined(CONFIG_FIT)
136 if (!images->legacy_hdr_valid) {
137 fit_unsupported_reset("Lynx");
142 lynxkdi_boot((image_header_t *)hdr);
146 #endif /* CONFIG_LYNXKDI */
148 #ifdef CONFIG_BOOTM_RTEMS
149 static int do_bootm_rtems(int flag, int argc, char * const argv[],
150 bootm_headers_t *images)
152 void (*entry_point)(bd_t *);
154 if (flag != BOOTM_STATE_OS_GO)
157 #if defined(CONFIG_FIT)
158 if (!images->legacy_hdr_valid) {
159 fit_unsupported_reset("RTEMS");
164 entry_point = (void (*)(bd_t *))images->ep;
166 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
169 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
173 * r3: ptr to board info data
175 (*entry_point)(gd->bd);
179 #endif /* CONFIG_BOOTM_RTEMS */
181 #if defined(CONFIG_BOOTM_OSE)
182 static int do_bootm_ose(int flag, int argc, char * const argv[],
183 bootm_headers_t *images)
185 void (*entry_point)(void);
187 if (flag != BOOTM_STATE_OS_GO)
190 #if defined(CONFIG_FIT)
191 if (!images->legacy_hdr_valid) {
192 fit_unsupported_reset("OSE");
197 entry_point = (void (*)(void))images->ep;
199 printf("## Transferring control to OSE (at address %08lx) ...\n",
202 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
212 #endif /* CONFIG_BOOTM_OSE */
214 #if defined(CONFIG_BOOTM_PLAN9)
215 static int do_bootm_plan9(int flag, int argc, char * const argv[],
216 bootm_headers_t *images)
218 void (*entry_point)(void);
221 if (flag != BOOTM_STATE_OS_GO)
224 #if defined(CONFIG_FIT)
225 if (!images->legacy_hdr_valid) {
226 fit_unsupported_reset("Plan 9");
231 /* See README.plan9 */
232 s = env_get("confaddr");
234 char *confaddr = (char *)simple_strtoul(s, NULL, 16);
237 copy_args(confaddr, argc, argv, '\n');
239 s = env_get("bootargs");
245 entry_point = (void (*)(void))images->ep;
247 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
250 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
260 #endif /* CONFIG_BOOTM_PLAN9 */
262 #if defined(CONFIG_BOOTM_VXWORKS) && \
263 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
265 static void do_bootvx_fdt(bootm_headers_t *images)
267 #if defined(CONFIG_OF_LIBFDT)
270 ulong of_size = images->ft_len;
271 char **of_flat_tree = &images->ft_addr;
272 struct lmb *lmb = &images->lmb;
275 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
277 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
281 /* Update ethernet nodes */
282 fdt_fixup_ethernet(*of_flat_tree);
284 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
285 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
286 bootline = env_get("bootargs");
288 ret = fdt_find_and_setprop(*of_flat_tree,
289 "/chosen", "bootargs",
291 strlen(bootline) + 1, 1);
293 printf("## ERROR: %s : %s\n", __func__,
299 printf("## ERROR: %s : %s\n", __func__,
306 boot_prep_vxworks(images);
308 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
310 #if defined(CONFIG_OF_LIBFDT)
311 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
312 (ulong)images->ep, (ulong)*of_flat_tree);
314 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
317 boot_jump_vxworks(images);
319 puts("## vxWorks terminated\n");
322 static int do_bootm_vxworks_legacy(int flag, int argc, char * const argv[],
323 bootm_headers_t *images)
325 if (flag != BOOTM_STATE_OS_GO)
328 #if defined(CONFIG_FIT)
329 if (!images->legacy_hdr_valid) {
330 fit_unsupported_reset("VxWorks");
335 do_bootvx_fdt(images);
340 int do_bootm_vxworks(int flag, int argc, char * const argv[],
341 bootm_headers_t *images)
345 unsigned long vxflags;
346 bool std_dtb = false;
348 /* get bootargs env */
349 bootargs = env_get("bootargs");
351 if (bootargs != NULL) {
352 for (pos = 0; pos < strlen(bootargs); pos++) {
353 /* find f=0xnumber flag */
354 if ((bootargs[pos] == '=') && (pos >= 1) &&
355 (bootargs[pos - 1] == 'f')) {
356 vxflags = simple_strtoul(&bootargs[pos + 1],
358 if (vxflags & VXWORKS_SYSFLG_STD_DTB)
365 if (flag & BOOTM_STATE_OS_PREP)
366 printf(" Using standard DTB\n");
367 return do_bootm_linux(flag, argc, argv, images);
369 if (flag & BOOTM_STATE_OS_PREP)
370 printf(" !!! WARNING !!! Using legacy DTB\n");
371 return do_bootm_vxworks_legacy(flag, argc, argv, images);
376 #if defined(CONFIG_CMD_ELF)
377 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
378 bootm_headers_t *images)
384 if (flag != BOOTM_STATE_OS_GO)
387 #if defined(CONFIG_FIT)
388 if (!images->legacy_hdr_valid) {
389 fit_unsupported_reset("QNX");
394 sprintf(str, "%lx", images->ep); /* write entry-point into string */
395 local_args[0] = argv[0];
396 local_args[1] = str; /* and provide it via the arguments */
399 * QNX images require the data cache is disabled.
401 dcache = dcache_status();
405 do_bootelf(NULL, 0, 2, local_args);
414 #ifdef CONFIG_INTEGRITY
415 static int do_bootm_integrity(int flag, int argc, char * const argv[],
416 bootm_headers_t *images)
418 void (*entry_point)(void);
420 if (flag != BOOTM_STATE_OS_GO)
423 #if defined(CONFIG_FIT)
424 if (!images->legacy_hdr_valid) {
425 fit_unsupported_reset("INTEGRITY");
430 entry_point = (void (*)(void))images->ep;
432 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
435 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
438 * INTEGRITY Parameters:
447 #ifdef CONFIG_BOOTM_OPENRTOS
448 static int do_bootm_openrtos(int flag, int argc, char * const argv[],
449 bootm_headers_t *images)
451 void (*entry_point)(void);
453 if (flag != BOOTM_STATE_OS_GO)
456 entry_point = (void (*)(void))images->ep;
458 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
461 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
464 * OpenRTOS Parameters:
473 #ifdef CONFIG_BOOTM_OPTEE
474 static int do_bootm_tee(int flag, int argc, char * const argv[],
475 bootm_headers_t *images)
480 if (images->os.os != IH_OS_TEE) {
484 /* Validate OPTEE header */
485 ret = optee_verify_bootm_image(images->os.image_start,
487 images->os.image_len);
492 ret = bootm_find_images(flag, argc, argv);
496 /* From here we can run the regular linux boot path */
497 return do_bootm_linux(flag, argc, argv, images);
501 static boot_os_fn *boot_os[] = {
502 [IH_OS_U_BOOT] = do_bootm_standalone,
503 #ifdef CONFIG_BOOTM_LINUX
504 [IH_OS_LINUX] = do_bootm_linux,
506 #ifdef CONFIG_BOOTM_NETBSD
507 [IH_OS_NETBSD] = do_bootm_netbsd,
509 #ifdef CONFIG_LYNXKDI
510 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
512 #ifdef CONFIG_BOOTM_RTEMS
513 [IH_OS_RTEMS] = do_bootm_rtems,
515 #if defined(CONFIG_BOOTM_OSE)
516 [IH_OS_OSE] = do_bootm_ose,
518 #if defined(CONFIG_BOOTM_PLAN9)
519 [IH_OS_PLAN9] = do_bootm_plan9,
521 #if defined(CONFIG_BOOTM_VXWORKS) && \
522 (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
523 [IH_OS_VXWORKS] = do_bootm_vxworks,
525 #if defined(CONFIG_CMD_ELF)
526 [IH_OS_QNX] = do_bootm_qnxelf,
528 #ifdef CONFIG_INTEGRITY
529 [IH_OS_INTEGRITY] = do_bootm_integrity,
531 #ifdef CONFIG_BOOTM_OPENRTOS
532 [IH_OS_OPENRTOS] = do_bootm_openrtos,
534 #ifdef CONFIG_BOOTM_OPTEE
535 [IH_OS_TEE] = do_bootm_tee,
539 /* Allow for arch specific config before we boot */
540 __weak void arch_preboot_os(void)
542 /* please define platform specific arch_preboot_os() */
545 /* Allow for board specific config before we boot */
546 __weak void board_preboot_os(void)
548 /* please define board specific board_preboot_os() */
551 int boot_selected_os(int argc, char * const argv[], int state,
552 bootm_headers_t *images, boot_os_fn *boot_fn)
556 boot_fn(state, argc, argv, images);
558 /* Stand-alone may return when 'autostart' is 'no' */
559 if (images->os.type == IH_TYPE_STANDALONE ||
560 IS_ENABLED(CONFIG_SANDBOX) ||
561 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
563 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
564 debug("\n## Control returned to monitor - resetting...\n");
566 return BOOTM_ERR_RESET;
569 boot_os_fn *bootm_os_get_boot_func(int os)
571 #ifdef CONFIG_NEEDS_MANUAL_RELOC
572 static bool relocated;
577 /* relocate boot function table */
578 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
579 if (boot_os[i] != NULL)
580 boot_os[i] += gd->reloc_off;