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 int do_bootm_vxworks(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);
341 #if defined(CONFIG_CMD_ELF)
342 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
343 bootm_headers_t *images)
349 if (flag != BOOTM_STATE_OS_GO)
352 #if defined(CONFIG_FIT)
353 if (!images->legacy_hdr_valid) {
354 fit_unsupported_reset("QNX");
359 sprintf(str, "%lx", images->ep); /* write entry-point into string */
360 local_args[0] = argv[0];
361 local_args[1] = str; /* and provide it via the arguments */
364 * QNX images require the data cache is disabled.
366 dcache = dcache_status();
370 do_bootelf(NULL, 0, 2, local_args);
379 #ifdef CONFIG_INTEGRITY
380 static int do_bootm_integrity(int flag, int argc, char * const argv[],
381 bootm_headers_t *images)
383 void (*entry_point)(void);
385 if (flag != BOOTM_STATE_OS_GO)
388 #if defined(CONFIG_FIT)
389 if (!images->legacy_hdr_valid) {
390 fit_unsupported_reset("INTEGRITY");
395 entry_point = (void (*)(void))images->ep;
397 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
400 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
403 * INTEGRITY Parameters:
412 #ifdef CONFIG_BOOTM_OPENRTOS
413 static int do_bootm_openrtos(int flag, int argc, char * const argv[],
414 bootm_headers_t *images)
416 void (*entry_point)(void);
418 if (flag != BOOTM_STATE_OS_GO)
421 entry_point = (void (*)(void))images->ep;
423 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
426 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
429 * OpenRTOS Parameters:
438 #ifdef CONFIG_BOOTM_OPTEE
439 static int do_bootm_tee(int flag, int argc, char * const argv[],
440 bootm_headers_t *images)
445 if (images->os.os != IH_OS_TEE) {
449 /* Validate OPTEE header */
450 ret = optee_verify_bootm_image(images->os.image_start,
452 images->os.image_len);
457 ret = bootm_find_images(flag, argc, argv);
461 /* From here we can run the regular linux boot path */
462 return do_bootm_linux(flag, argc, argv, images);
466 static boot_os_fn *boot_os[] = {
467 [IH_OS_U_BOOT] = do_bootm_standalone,
468 #ifdef CONFIG_BOOTM_LINUX
469 [IH_OS_LINUX] = do_bootm_linux,
471 #ifdef CONFIG_BOOTM_NETBSD
472 [IH_OS_NETBSD] = do_bootm_netbsd,
474 #ifdef CONFIG_LYNXKDI
475 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
477 #ifdef CONFIG_BOOTM_RTEMS
478 [IH_OS_RTEMS] = do_bootm_rtems,
480 #if defined(CONFIG_BOOTM_OSE)
481 [IH_OS_OSE] = do_bootm_ose,
483 #if defined(CONFIG_BOOTM_PLAN9)
484 [IH_OS_PLAN9] = do_bootm_plan9,
486 #if defined(CONFIG_BOOTM_VXWORKS) && \
487 (defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
488 [IH_OS_VXWORKS] = do_bootm_vxworks,
490 #if defined(CONFIG_CMD_ELF)
491 [IH_OS_QNX] = do_bootm_qnxelf,
493 #ifdef CONFIG_INTEGRITY
494 [IH_OS_INTEGRITY] = do_bootm_integrity,
496 #ifdef CONFIG_BOOTM_OPENRTOS
497 [IH_OS_OPENRTOS] = do_bootm_openrtos,
499 #ifdef CONFIG_BOOTM_OPTEE
500 [IH_OS_TEE] = do_bootm_tee,
504 /* Allow for arch specific config before we boot */
505 __weak void arch_preboot_os(void)
507 /* please define platform specific arch_preboot_os() */
510 /* Allow for board specific config before we boot */
511 __weak void board_preboot_os(void)
513 /* please define board specific board_preboot_os() */
516 int boot_selected_os(int argc, char * const argv[], int state,
517 bootm_headers_t *images, boot_os_fn *boot_fn)
521 boot_fn(state, argc, argv, images);
523 /* Stand-alone may return when 'autostart' is 'no' */
524 if (images->os.type == IH_TYPE_STANDALONE ||
525 IS_ENABLED(CONFIG_SANDBOX) ||
526 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
528 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
529 debug("\n## Control returned to monitor - resetting...\n");
531 return BOOTM_ERR_RESET;
534 boot_os_fn *bootm_os_get_boot_func(int os)
536 #ifdef CONFIG_NEEDS_MANUAL_RELOC
537 static bool relocated;
542 /* relocate boot function table */
543 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
544 if (boot_os[i] != NULL)
545 boot_os[i] += gd->reloc_off;