2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <fdt_support.h>
11 #include <linux/libfdt.h>
14 #include <tee/optee.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 static int do_bootm_standalone(int flag, int argc, char * const argv[],
19 bootm_headers_t *images)
22 int (*appl)(int, char *const[]);
24 /* Don't start if "autostart" is set to "no" */
25 s = env_get("autostart");
26 if ((s != NULL) && !strcmp(s, "no")) {
27 env_set_hex("filesize", images->os.image_len);
30 appl = (int (*)(int, char * const []))images->ep;
35 /*******************************************************************/
36 /* OS booting routines */
37 /*******************************************************************/
39 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
40 static void copy_args(char *dest, int argc, char * const argv[], char delim)
44 for (i = 0; i < argc; i++) {
47 strcpy(dest, argv[i]);
48 dest += strlen(argv[i]);
53 #ifdef CONFIG_BOOTM_NETBSD
54 static int do_bootm_netbsd(int flag, int argc, char * const argv[],
55 bootm_headers_t *images)
57 void (*loader)(bd_t *, image_header_t *, char *, char *);
58 image_header_t *os_hdr, *hdr;
59 ulong kernel_data, kernel_len;
62 if (flag != BOOTM_STATE_OS_GO)
65 #if defined(CONFIG_FIT)
66 if (!images->legacy_hdr_valid) {
67 fit_unsupported_reset("NetBSD");
71 hdr = images->legacy_hdr_os;
74 * Booting a (NetBSD) kernel image
76 * This process is pretty similar to a standalone application:
77 * The (first part of an multi-) image must be a stage-2 loader,
78 * which in turn is responsible for loading & invoking the actual
79 * kernel. The only differences are the parameters being passed:
80 * besides the board info strucure, the loader expects a command
81 * line, the name of the console device, and (optionally) the
82 * address of the original image header.
85 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
86 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
95 for (i = 0, len = 0; i < argc; i += 1)
96 len += strlen(argv[i]) + 1;
97 cmdline = malloc(len);
98 copy_args(cmdline, argc, argv, ' ');
100 cmdline = env_get("bootargs");
105 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
107 printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
110 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
113 * NetBSD Stage-2 Loader Parameters:
114 * arg[0]: pointer to board info data
115 * arg[1]: image load address
116 * arg[2]: char pointer to the console device to use
117 * arg[3]: char pointer to the boot arguments
119 (*loader)(gd->bd, os_hdr, "", cmdline);
123 #endif /* CONFIG_BOOTM_NETBSD*/
125 #ifdef CONFIG_LYNXKDI
126 static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
127 bootm_headers_t *images)
129 image_header_t *hdr = &images->legacy_hdr_os_copy;
131 if (flag != BOOTM_STATE_OS_GO)
134 #if defined(CONFIG_FIT)
135 if (!images->legacy_hdr_valid) {
136 fit_unsupported_reset("Lynx");
141 lynxkdi_boot((image_header_t *)hdr);
145 #endif /* CONFIG_LYNXKDI */
147 #ifdef CONFIG_BOOTM_RTEMS
148 static int do_bootm_rtems(int flag, int argc, char * const argv[],
149 bootm_headers_t *images)
151 void (*entry_point)(bd_t *);
153 if (flag != BOOTM_STATE_OS_GO)
156 #if defined(CONFIG_FIT)
157 if (!images->legacy_hdr_valid) {
158 fit_unsupported_reset("RTEMS");
163 entry_point = (void (*)(bd_t *))images->ep;
165 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
168 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
172 * r3: ptr to board info data
174 (*entry_point)(gd->bd);
178 #endif /* CONFIG_BOOTM_RTEMS */
180 #if defined(CONFIG_BOOTM_OSE)
181 static int do_bootm_ose(int flag, int argc, char * const argv[],
182 bootm_headers_t *images)
184 void (*entry_point)(void);
186 if (flag != BOOTM_STATE_OS_GO)
189 #if defined(CONFIG_FIT)
190 if (!images->legacy_hdr_valid) {
191 fit_unsupported_reset("OSE");
196 entry_point = (void (*)(void))images->ep;
198 printf("## Transferring control to OSE (at address %08lx) ...\n",
201 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
211 #endif /* CONFIG_BOOTM_OSE */
213 #if defined(CONFIG_BOOTM_PLAN9)
214 static int do_bootm_plan9(int flag, int argc, char * const argv[],
215 bootm_headers_t *images)
217 void (*entry_point)(void);
220 if (flag != BOOTM_STATE_OS_GO)
223 #if defined(CONFIG_FIT)
224 if (!images->legacy_hdr_valid) {
225 fit_unsupported_reset("Plan 9");
230 /* See README.plan9 */
231 s = env_get("confaddr");
233 char *confaddr = (char *)simple_strtoul(s, NULL, 16);
236 copy_args(confaddr, argc, argv, '\n');
238 s = env_get("bootargs");
244 entry_point = (void (*)(void))images->ep;
246 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
249 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
259 #endif /* CONFIG_BOOTM_PLAN9 */
261 #if defined(CONFIG_BOOTM_VXWORKS) && \
262 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
264 void do_bootvx_fdt(bootm_headers_t *images)
266 #if defined(CONFIG_OF_LIBFDT)
269 ulong of_size = images->ft_len;
270 char **of_flat_tree = &images->ft_addr;
271 struct lmb *lmb = &images->lmb;
274 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
276 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
280 /* Update ethernet nodes */
281 fdt_fixup_ethernet(*of_flat_tree);
283 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
284 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
285 bootline = env_get("bootargs");
287 ret = fdt_find_and_setprop(*of_flat_tree,
288 "/chosen", "bootargs",
290 strlen(bootline) + 1, 1);
292 printf("## ERROR: %s : %s\n", __func__,
298 printf("## ERROR: %s : %s\n", __func__,
305 boot_prep_vxworks(images);
307 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
309 #if defined(CONFIG_OF_LIBFDT)
310 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
311 (ulong)images->ep, (ulong)*of_flat_tree);
313 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
316 boot_jump_vxworks(images);
318 puts("## vxWorks terminated\n");
321 static int do_bootm_vxworks(int flag, int argc, char * const argv[],
322 bootm_headers_t *images)
324 if (flag != BOOTM_STATE_OS_GO)
327 #if defined(CONFIG_FIT)
328 if (!images->legacy_hdr_valid) {
329 fit_unsupported_reset("VxWorks");
334 do_bootvx_fdt(images);
340 #if defined(CONFIG_CMD_ELF)
341 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
342 bootm_headers_t *images)
348 if (flag != BOOTM_STATE_OS_GO)
351 #if defined(CONFIG_FIT)
352 if (!images->legacy_hdr_valid) {
353 fit_unsupported_reset("QNX");
358 sprintf(str, "%lx", images->ep); /* write entry-point into string */
359 local_args[0] = argv[0];
360 local_args[1] = str; /* and provide it via the arguments */
363 * QNX images require the data cache is disabled.
365 dcache = dcache_status();
369 do_bootelf(NULL, 0, 2, local_args);
378 #ifdef CONFIG_INTEGRITY
379 static int do_bootm_integrity(int flag, int argc, char * const argv[],
380 bootm_headers_t *images)
382 void (*entry_point)(void);
384 if (flag != BOOTM_STATE_OS_GO)
387 #if defined(CONFIG_FIT)
388 if (!images->legacy_hdr_valid) {
389 fit_unsupported_reset("INTEGRITY");
394 entry_point = (void (*)(void))images->ep;
396 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
399 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
402 * INTEGRITY Parameters:
411 #ifdef CONFIG_BOOTM_OPENRTOS
412 static int do_bootm_openrtos(int flag, int argc, char * const argv[],
413 bootm_headers_t *images)
415 void (*entry_point)(void);
417 if (flag != BOOTM_STATE_OS_GO)
420 entry_point = (void (*)(void))images->ep;
422 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
425 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
428 * OpenRTOS Parameters:
437 #ifdef CONFIG_BOOTM_OPTEE
438 static int do_bootm_tee(int flag, int argc, char * const argv[],
439 bootm_headers_t *images)
444 if (images->os.os != IH_OS_TEE) {
448 /* Validate OPTEE header */
449 ret = optee_verify_bootm_image(images->os.image_start,
451 images->os.image_len);
456 ret = bootm_find_images(flag, argc, argv);
460 /* From here we can run the regular linux boot path */
461 return do_bootm_linux(flag, argc, argv, images);
465 static boot_os_fn *boot_os[] = {
466 [IH_OS_U_BOOT] = do_bootm_standalone,
467 #ifdef CONFIG_BOOTM_LINUX
468 [IH_OS_LINUX] = do_bootm_linux,
470 #ifdef CONFIG_BOOTM_NETBSD
471 [IH_OS_NETBSD] = do_bootm_netbsd,
473 #ifdef CONFIG_LYNXKDI
474 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
476 #ifdef CONFIG_BOOTM_RTEMS
477 [IH_OS_RTEMS] = do_bootm_rtems,
479 #if defined(CONFIG_BOOTM_OSE)
480 [IH_OS_OSE] = do_bootm_ose,
482 #if defined(CONFIG_BOOTM_PLAN9)
483 [IH_OS_PLAN9] = do_bootm_plan9,
485 #if defined(CONFIG_BOOTM_VXWORKS) && \
486 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
487 [IH_OS_VXWORKS] = do_bootm_vxworks,
489 #if defined(CONFIG_CMD_ELF)
490 [IH_OS_QNX] = do_bootm_qnxelf,
492 #ifdef CONFIG_INTEGRITY
493 [IH_OS_INTEGRITY] = do_bootm_integrity,
495 #ifdef CONFIG_BOOTM_OPENRTOS
496 [IH_OS_OPENRTOS] = do_bootm_openrtos,
498 #ifdef CONFIG_BOOTM_OPTEE
499 [IH_OS_TEE] = do_bootm_tee,
503 /* Allow for arch specific config before we boot */
504 __weak void arch_preboot_os(void)
506 /* please define platform specific arch_preboot_os() */
509 int boot_selected_os(int argc, char * const argv[], int state,
510 bootm_headers_t *images, boot_os_fn *boot_fn)
513 boot_fn(state, argc, argv, images);
515 /* Stand-alone may return when 'autostart' is 'no' */
516 if (images->os.type == IH_TYPE_STANDALONE ||
517 IS_ENABLED(CONFIG_SANDBOX) ||
518 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
520 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
521 debug("\n## Control returned to monitor - resetting...\n");
523 return BOOTM_ERR_RESET;
526 boot_os_fn *bootm_os_get_boot_func(int os)
528 #ifdef CONFIG_NEEDS_MANUAL_RELOC
529 static bool relocated;
534 /* relocate boot function table */
535 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
536 if (boot_os[i] != NULL)
537 boot_os[i] += gd->reloc_off;