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 /* 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 static boot_os_fn *boot_os[] = {
437 [IH_OS_U_BOOT] = do_bootm_standalone,
438 #ifdef CONFIG_BOOTM_LINUX
439 [IH_OS_LINUX] = do_bootm_linux,
441 #ifdef CONFIG_BOOTM_NETBSD
442 [IH_OS_NETBSD] = do_bootm_netbsd,
444 #ifdef CONFIG_LYNXKDI
445 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
447 #ifdef CONFIG_BOOTM_RTEMS
448 [IH_OS_RTEMS] = do_bootm_rtems,
450 #if defined(CONFIG_BOOTM_OSE)
451 [IH_OS_OSE] = do_bootm_ose,
453 #if defined(CONFIG_BOOTM_PLAN9)
454 [IH_OS_PLAN9] = do_bootm_plan9,
456 #if defined(CONFIG_BOOTM_VXWORKS) && \
457 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
458 [IH_OS_VXWORKS] = do_bootm_vxworks,
460 #if defined(CONFIG_CMD_ELF)
461 [IH_OS_QNX] = do_bootm_qnxelf,
463 #ifdef CONFIG_INTEGRITY
464 [IH_OS_INTEGRITY] = do_bootm_integrity,
466 #ifdef CONFIG_BOOTM_OPENRTOS
467 [IH_OS_OPENRTOS] = do_bootm_openrtos,
471 /* Allow for arch specific config before we boot */
472 __weak void arch_preboot_os(void)
474 /* please define platform specific arch_preboot_os() */
477 int boot_selected_os(int argc, char * const argv[], int state,
478 bootm_headers_t *images, boot_os_fn *boot_fn)
481 boot_fn(state, argc, argv, images);
483 /* Stand-alone may return when 'autostart' is 'no' */
484 if (images->os.type == IH_TYPE_STANDALONE ||
485 IS_ENABLED(CONFIG_SANDBOX) ||
486 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
488 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
489 debug("\n## Control returned to monitor - resetting...\n");
491 return BOOTM_ERR_RESET;
494 boot_os_fn *bootm_os_get_boot_func(int os)
496 #ifdef CONFIG_NEEDS_MANUAL_RELOC
497 static bool relocated;
502 /* relocate boot function table */
503 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
504 if (boot_os[i] != NULL)
505 boot_os[i] += gd->reloc_off;