1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 #include <fdt_support.h>
10 #include <linux/libfdt.h>
13 #include <tee/optee.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 static int do_bootm_standalone(int flag, int argc, char * const argv[],
18 bootm_headers_t *images)
21 int (*appl)(int, char *const[]);
23 /* Don't start if "autostart" is set to "no" */
24 s = env_get("autostart");
25 if ((s != NULL) && !strcmp(s, "no")) {
26 env_set_hex("filesize", images->os.image_len);
29 appl = (int (*)(int, char * const []))images->ep;
34 /*******************************************************************/
35 /* OS booting routines */
36 /*******************************************************************/
38 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
39 static void copy_args(char *dest, int argc, char * const argv[], char delim)
43 for (i = 0; i < argc; i++) {
46 strcpy(dest, argv[i]);
47 dest += strlen(argv[i]);
52 #ifdef CONFIG_BOOTM_NETBSD
53 static int do_bootm_netbsd(int flag, int argc, char * const argv[],
54 bootm_headers_t *images)
56 void (*loader)(bd_t *, image_header_t *, char *, char *);
57 image_header_t *os_hdr, *hdr;
58 ulong kernel_data, kernel_len;
61 if (flag != BOOTM_STATE_OS_GO)
64 #if defined(CONFIG_FIT)
65 if (!images->legacy_hdr_valid) {
66 fit_unsupported_reset("NetBSD");
70 hdr = images->legacy_hdr_os;
73 * Booting a (NetBSD) kernel image
75 * This process is pretty similar to a standalone application:
76 * The (first part of an multi-) image must be a stage-2 loader,
77 * which in turn is responsible for loading & invoking the actual
78 * kernel. The only differences are the parameters being passed:
79 * besides the board info strucure, the loader expects a command
80 * line, the name of the console device, and (optionally) the
81 * address of the original image header.
84 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
85 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
94 for (i = 0, len = 0; i < argc; i += 1)
95 len += strlen(argv[i]) + 1;
96 cmdline = malloc(len);
97 copy_args(cmdline, argc, argv, ' ');
99 cmdline = env_get("bootargs");
104 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
106 printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
109 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
112 * NetBSD Stage-2 Loader Parameters:
113 * arg[0]: pointer to board info data
114 * arg[1]: image load address
115 * arg[2]: char pointer to the console device to use
116 * arg[3]: char pointer to the boot arguments
118 (*loader)(gd->bd, os_hdr, "", cmdline);
122 #endif /* CONFIG_BOOTM_NETBSD*/
124 #ifdef CONFIG_LYNXKDI
125 static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
126 bootm_headers_t *images)
128 image_header_t *hdr = &images->legacy_hdr_os_copy;
130 if (flag != BOOTM_STATE_OS_GO)
133 #if defined(CONFIG_FIT)
134 if (!images->legacy_hdr_valid) {
135 fit_unsupported_reset("Lynx");
140 lynxkdi_boot((image_header_t *)hdr);
144 #endif /* CONFIG_LYNXKDI */
146 #ifdef CONFIG_BOOTM_RTEMS
147 static int do_bootm_rtems(int flag, int argc, char * const argv[],
148 bootm_headers_t *images)
150 void (*entry_point)(bd_t *);
152 if (flag != BOOTM_STATE_OS_GO)
155 #if defined(CONFIG_FIT)
156 if (!images->legacy_hdr_valid) {
157 fit_unsupported_reset("RTEMS");
162 entry_point = (void (*)(bd_t *))images->ep;
164 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
167 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
171 * r3: ptr to board info data
173 (*entry_point)(gd->bd);
177 #endif /* CONFIG_BOOTM_RTEMS */
179 #if defined(CONFIG_BOOTM_OSE)
180 static int do_bootm_ose(int flag, int argc, char * const argv[],
181 bootm_headers_t *images)
183 void (*entry_point)(void);
185 if (flag != BOOTM_STATE_OS_GO)
188 #if defined(CONFIG_FIT)
189 if (!images->legacy_hdr_valid) {
190 fit_unsupported_reset("OSE");
195 entry_point = (void (*)(void))images->ep;
197 printf("## Transferring control to OSE (at address %08lx) ...\n",
200 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
210 #endif /* CONFIG_BOOTM_OSE */
212 #if defined(CONFIG_BOOTM_PLAN9)
213 static int do_bootm_plan9(int flag, int argc, char * const argv[],
214 bootm_headers_t *images)
216 void (*entry_point)(void);
219 if (flag != BOOTM_STATE_OS_GO)
222 #if defined(CONFIG_FIT)
223 if (!images->legacy_hdr_valid) {
224 fit_unsupported_reset("Plan 9");
229 /* See README.plan9 */
230 s = env_get("confaddr");
232 char *confaddr = (char *)simple_strtoul(s, NULL, 16);
235 copy_args(confaddr, argc, argv, '\n');
237 s = env_get("bootargs");
243 entry_point = (void (*)(void))images->ep;
245 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
248 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
258 #endif /* CONFIG_BOOTM_PLAN9 */
260 #if defined(CONFIG_BOOTM_VXWORKS) && \
261 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
263 void do_bootvx_fdt(bootm_headers_t *images)
265 #if defined(CONFIG_OF_LIBFDT)
268 ulong of_size = images->ft_len;
269 char **of_flat_tree = &images->ft_addr;
270 struct lmb *lmb = &images->lmb;
273 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
275 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
279 /* Update ethernet nodes */
280 fdt_fixup_ethernet(*of_flat_tree);
282 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
283 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
284 bootline = env_get("bootargs");
286 ret = fdt_find_and_setprop(*of_flat_tree,
287 "/chosen", "bootargs",
289 strlen(bootline) + 1, 1);
291 printf("## ERROR: %s : %s\n", __func__,
297 printf("## ERROR: %s : %s\n", __func__,
304 boot_prep_vxworks(images);
306 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
308 #if defined(CONFIG_OF_LIBFDT)
309 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
310 (ulong)images->ep, (ulong)*of_flat_tree);
312 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
315 boot_jump_vxworks(images);
317 puts("## vxWorks terminated\n");
320 static int do_bootm_vxworks(int flag, int argc, char * const argv[],
321 bootm_headers_t *images)
323 if (flag != BOOTM_STATE_OS_GO)
326 #if defined(CONFIG_FIT)
327 if (!images->legacy_hdr_valid) {
328 fit_unsupported_reset("VxWorks");
333 do_bootvx_fdt(images);
339 #if defined(CONFIG_CMD_ELF)
340 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
341 bootm_headers_t *images)
347 if (flag != BOOTM_STATE_OS_GO)
350 #if defined(CONFIG_FIT)
351 if (!images->legacy_hdr_valid) {
352 fit_unsupported_reset("QNX");
357 sprintf(str, "%lx", images->ep); /* write entry-point into string */
358 local_args[0] = argv[0];
359 local_args[1] = str; /* and provide it via the arguments */
362 * QNX images require the data cache is disabled.
364 dcache = dcache_status();
368 do_bootelf(NULL, 0, 2, local_args);
377 #ifdef CONFIG_INTEGRITY
378 static int do_bootm_integrity(int flag, int argc, char * const argv[],
379 bootm_headers_t *images)
381 void (*entry_point)(void);
383 if (flag != BOOTM_STATE_OS_GO)
386 #if defined(CONFIG_FIT)
387 if (!images->legacy_hdr_valid) {
388 fit_unsupported_reset("INTEGRITY");
393 entry_point = (void (*)(void))images->ep;
395 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
398 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
401 * INTEGRITY Parameters:
410 #ifdef CONFIG_BOOTM_OPENRTOS
411 static int do_bootm_openrtos(int flag, int argc, char * const argv[],
412 bootm_headers_t *images)
414 void (*entry_point)(void);
416 if (flag != BOOTM_STATE_OS_GO)
419 entry_point = (void (*)(void))images->ep;
421 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
424 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
427 * OpenRTOS Parameters:
436 #ifdef CONFIG_BOOTM_OPTEE
437 static int do_bootm_tee(int flag, int argc, char * const argv[],
438 bootm_headers_t *images)
443 if (images->os.os != IH_OS_TEE) {
447 /* Validate OPTEE header */
448 ret = optee_verify_bootm_image(images->os.image_start,
450 images->os.image_len);
455 ret = bootm_find_images(flag, argc, argv);
459 /* From here we can run the regular linux boot path */
460 return do_bootm_linux(flag, argc, argv, images);
464 static boot_os_fn *boot_os[] = {
465 [IH_OS_U_BOOT] = do_bootm_standalone,
466 #ifdef CONFIG_BOOTM_LINUX
467 [IH_OS_LINUX] = do_bootm_linux,
469 #ifdef CONFIG_BOOTM_NETBSD
470 [IH_OS_NETBSD] = do_bootm_netbsd,
472 #ifdef CONFIG_LYNXKDI
473 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
475 #ifdef CONFIG_BOOTM_RTEMS
476 [IH_OS_RTEMS] = do_bootm_rtems,
478 #if defined(CONFIG_BOOTM_OSE)
479 [IH_OS_OSE] = do_bootm_ose,
481 #if defined(CONFIG_BOOTM_PLAN9)
482 [IH_OS_PLAN9] = do_bootm_plan9,
484 #if defined(CONFIG_BOOTM_VXWORKS) && \
485 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
486 [IH_OS_VXWORKS] = do_bootm_vxworks,
488 #if defined(CONFIG_CMD_ELF)
489 [IH_OS_QNX] = do_bootm_qnxelf,
491 #ifdef CONFIG_INTEGRITY
492 [IH_OS_INTEGRITY] = do_bootm_integrity,
494 #ifdef CONFIG_BOOTM_OPENRTOS
495 [IH_OS_OPENRTOS] = do_bootm_openrtos,
497 #ifdef CONFIG_BOOTM_OPTEE
498 [IH_OS_TEE] = do_bootm_tee,
502 /* Allow for arch specific config before we boot */
503 __weak void arch_preboot_os(void)
505 /* please define platform specific arch_preboot_os() */
508 /* Allow for board specific config before we boot */
509 __weak void board_preboot_os(void)
511 /* please define board specific board_preboot_os() */
514 int boot_selected_os(int argc, char * const argv[], int state,
515 bootm_headers_t *images, boot_os_fn *boot_fn)
519 boot_fn(state, argc, argv, images);
521 /* Stand-alone may return when 'autostart' is 'no' */
522 if (images->os.type == IH_TYPE_STANDALONE ||
523 IS_ENABLED(CONFIG_SANDBOX) ||
524 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
526 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
527 debug("\n## Control returned to monitor - resetting...\n");
529 return BOOTM_ERR_RESET;
532 boot_os_fn *bootm_os_get_boot_func(int os)
534 #ifdef CONFIG_NEEDS_MANUAL_RELOC
535 static bool relocated;
540 /* relocate boot function table */
541 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
542 if (boot_os[i] != NULL)
543 boot_os[i] += gd->reloc_off;