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>
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 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
280 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
281 bootline = env_get("bootargs");
283 ret = fdt_find_and_setprop(*of_flat_tree,
284 "/chosen", "bootargs",
286 strlen(bootline) + 1, 1);
288 printf("## ERROR: %s : %s\n", __func__,
294 printf("## ERROR: %s : %s\n", __func__,
301 boot_prep_vxworks(images);
303 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
305 #if defined(CONFIG_OF_LIBFDT)
306 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
307 (ulong)images->ep, (ulong)*of_flat_tree);
309 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
312 boot_jump_vxworks(images);
314 puts("## vxWorks terminated\n");
317 static int do_bootm_vxworks(int flag, int argc, char * const argv[],
318 bootm_headers_t *images)
320 if (flag != BOOTM_STATE_OS_GO)
323 #if defined(CONFIG_FIT)
324 if (!images->legacy_hdr_valid) {
325 fit_unsupported_reset("VxWorks");
330 do_bootvx_fdt(images);
336 #if defined(CONFIG_CMD_ELF)
337 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
338 bootm_headers_t *images)
344 if (flag != BOOTM_STATE_OS_GO)
347 #if defined(CONFIG_FIT)
348 if (!images->legacy_hdr_valid) {
349 fit_unsupported_reset("QNX");
354 sprintf(str, "%lx", images->ep); /* write entry-point into string */
355 local_args[0] = argv[0];
356 local_args[1] = str; /* and provide it via the arguments */
359 * QNX images require the data cache is disabled.
361 dcache = dcache_status();
365 do_bootelf(NULL, 0, 2, local_args);
374 #ifdef CONFIG_INTEGRITY
375 static int do_bootm_integrity(int flag, int argc, char * const argv[],
376 bootm_headers_t *images)
378 void (*entry_point)(void);
380 if (flag != BOOTM_STATE_OS_GO)
383 #if defined(CONFIG_FIT)
384 if (!images->legacy_hdr_valid) {
385 fit_unsupported_reset("INTEGRITY");
390 entry_point = (void (*)(void))images->ep;
392 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
395 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
398 * INTEGRITY Parameters:
407 #ifdef CONFIG_BOOTM_OPENRTOS
408 static int do_bootm_openrtos(int flag, int argc, char * const argv[],
409 bootm_headers_t *images)
411 void (*entry_point)(void);
413 if (flag != BOOTM_STATE_OS_GO)
416 entry_point = (void (*)(void))images->ep;
418 printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
421 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
424 * OpenRTOS Parameters:
433 static boot_os_fn *boot_os[] = {
434 [IH_OS_U_BOOT] = do_bootm_standalone,
435 #ifdef CONFIG_BOOTM_LINUX
436 [IH_OS_LINUX] = do_bootm_linux,
438 #ifdef CONFIG_BOOTM_NETBSD
439 [IH_OS_NETBSD] = do_bootm_netbsd,
441 #ifdef CONFIG_LYNXKDI
442 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
444 #ifdef CONFIG_BOOTM_RTEMS
445 [IH_OS_RTEMS] = do_bootm_rtems,
447 #if defined(CONFIG_BOOTM_OSE)
448 [IH_OS_OSE] = do_bootm_ose,
450 #if defined(CONFIG_BOOTM_PLAN9)
451 [IH_OS_PLAN9] = do_bootm_plan9,
453 #if defined(CONFIG_BOOTM_VXWORKS) && \
454 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
455 [IH_OS_VXWORKS] = do_bootm_vxworks,
457 #if defined(CONFIG_CMD_ELF)
458 [IH_OS_QNX] = do_bootm_qnxelf,
460 #ifdef CONFIG_INTEGRITY
461 [IH_OS_INTEGRITY] = do_bootm_integrity,
463 #ifdef CONFIG_BOOTM_OPENRTOS
464 [IH_OS_OPENRTOS] = do_bootm_openrtos,
468 /* Allow for arch specific config before we boot */
469 __weak void arch_preboot_os(void)
471 /* please define platform specific arch_preboot_os() */
474 int boot_selected_os(int argc, char * const argv[], int state,
475 bootm_headers_t *images, boot_os_fn *boot_fn)
478 boot_fn(state, argc, argv, images);
480 /* Stand-alone may return when 'autostart' is 'no' */
481 if (images->os.type == IH_TYPE_STANDALONE ||
482 IS_ENABLED(CONFIG_SANDBOX) ||
483 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
485 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
486 debug("\n## Control returned to monitor - resetting...\n");
488 return BOOTM_ERR_RESET;
491 boot_os_fn *bootm_os_get_boot_func(int os)
493 #ifdef CONFIG_NEEDS_MANUAL_RELOC
494 static bool relocated;
499 /* relocate boot function table */
500 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
501 if (boot_os[i] != NULL)
502 boot_os[i] += gd->reloc_off;