xref: /openbmc/u-boot/common/bootm_os.c (revision 679590eb)
1 /*
2  * (C) Copyright 2000-2009
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <bootm.h>
10 #include <fdt_support.h>
11 #include <libfdt.h>
12 #include <malloc.h>
13 #include <vxworks.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 static int do_bootm_standalone(int flag, int argc, char * const argv[],
18 			       bootm_headers_t *images)
19 {
20 	char *s;
21 	int (*appl)(int, char *const[]);
22 
23 	/* Don't start if "autostart" is set to "no" */
24 	s = getenv("autostart");
25 	if ((s != NULL) && !strcmp(s, "no")) {
26 		setenv_hex("filesize", images->os.image_len);
27 		return 0;
28 	}
29 	appl = (int (*)(int, char * const []))images->ep;
30 	appl(argc, argv);
31 	return 0;
32 }
33 
34 /*******************************************************************/
35 /* OS booting routines */
36 /*******************************************************************/
37 
38 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
39 static void copy_args(char *dest, int argc, char * const argv[], char delim)
40 {
41 	int i;
42 
43 	for (i = 0; i < argc; i++) {
44 		if (i > 0)
45 			*dest++ = delim;
46 		strcpy(dest, argv[i]);
47 		dest += strlen(argv[i]);
48 	}
49 }
50 #endif
51 
52 #ifdef CONFIG_BOOTM_NETBSD
53 static int do_bootm_netbsd(int flag, int argc, char * const argv[],
54 			    bootm_headers_t *images)
55 {
56 	void (*loader)(bd_t *, image_header_t *, char *, char *);
57 	image_header_t *os_hdr, *hdr;
58 	ulong kernel_data, kernel_len;
59 	char *cmdline;
60 
61 	if (flag != BOOTM_STATE_OS_GO)
62 		return 0;
63 
64 #if defined(CONFIG_FIT)
65 	if (!images->legacy_hdr_valid) {
66 		fit_unsupported_reset("NetBSD");
67 		return 1;
68 	}
69 #endif
70 	hdr = images->legacy_hdr_os;
71 
72 	/*
73 	 * Booting a (NetBSD) kernel image
74 	 *
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.
82 	 */
83 	os_hdr = NULL;
84 	if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
85 		image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
86 		if (kernel_len)
87 			os_hdr = hdr;
88 	}
89 
90 	if (argc > 0) {
91 		ulong len;
92 		int   i;
93 
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, ' ');
98 	} else {
99 		cmdline = getenv("bootargs");
100 		if (cmdline == NULL)
101 			cmdline = "";
102 	}
103 
104 	loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
105 
106 	printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
107 	       (ulong)loader);
108 
109 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
110 
111 	/*
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
117 	 */
118 	(*loader)(gd->bd, os_hdr, "", cmdline);
119 
120 	return 1;
121 }
122 #endif /* CONFIG_BOOTM_NETBSD*/
123 
124 #ifdef CONFIG_LYNXKDI
125 static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
126 			     bootm_headers_t *images)
127 {
128 	image_header_t *hdr = &images->legacy_hdr_os_copy;
129 
130 	if (flag != BOOTM_STATE_OS_GO)
131 		return 0;
132 
133 #if defined(CONFIG_FIT)
134 	if (!images->legacy_hdr_valid) {
135 		fit_unsupported_reset("Lynx");
136 		return 1;
137 	}
138 #endif
139 
140 	lynxkdi_boot((image_header_t *)hdr);
141 
142 	return 1;
143 }
144 #endif /* CONFIG_LYNXKDI */
145 
146 #ifdef CONFIG_BOOTM_RTEMS
147 static int do_bootm_rtems(int flag, int argc, char * const argv[],
148 			   bootm_headers_t *images)
149 {
150 	void (*entry_point)(bd_t *);
151 
152 	if (flag != BOOTM_STATE_OS_GO)
153 		return 0;
154 
155 #if defined(CONFIG_FIT)
156 	if (!images->legacy_hdr_valid) {
157 		fit_unsupported_reset("RTEMS");
158 		return 1;
159 	}
160 #endif
161 
162 	entry_point = (void (*)(bd_t *))images->ep;
163 
164 	printf("## Transferring control to RTEMS (at address %08lx) ...\n",
165 	       (ulong)entry_point);
166 
167 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
168 
169 	/*
170 	 * RTEMS Parameters:
171 	 *   r3: ptr to board info data
172 	 */
173 	(*entry_point)(gd->bd);
174 
175 	return 1;
176 }
177 #endif /* CONFIG_BOOTM_RTEMS */
178 
179 #if defined(CONFIG_BOOTM_OSE)
180 static int do_bootm_ose(int flag, int argc, char * const argv[],
181 			   bootm_headers_t *images)
182 {
183 	void (*entry_point)(void);
184 
185 	if (flag != BOOTM_STATE_OS_GO)
186 		return 0;
187 
188 #if defined(CONFIG_FIT)
189 	if (!images->legacy_hdr_valid) {
190 		fit_unsupported_reset("OSE");
191 		return 1;
192 	}
193 #endif
194 
195 	entry_point = (void (*)(void))images->ep;
196 
197 	printf("## Transferring control to OSE (at address %08lx) ...\n",
198 	       (ulong)entry_point);
199 
200 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
201 
202 	/*
203 	 * OSE Parameters:
204 	 *   None
205 	 */
206 	(*entry_point)();
207 
208 	return 1;
209 }
210 #endif /* CONFIG_BOOTM_OSE */
211 
212 #if defined(CONFIG_BOOTM_PLAN9)
213 static int do_bootm_plan9(int flag, int argc, char * const argv[],
214 			   bootm_headers_t *images)
215 {
216 	void (*entry_point)(void);
217 	char *s;
218 
219 	if (flag != BOOTM_STATE_OS_GO)
220 		return 0;
221 
222 #if defined(CONFIG_FIT)
223 	if (!images->legacy_hdr_valid) {
224 		fit_unsupported_reset("Plan 9");
225 		return 1;
226 	}
227 #endif
228 
229 	/* See README.plan9 */
230 	s = getenv("confaddr");
231 	if (s != NULL) {
232 		char *confaddr = (char *)simple_strtoul(s, NULL, 16);
233 
234 		if (argc > 0) {
235 			copy_args(confaddr, argc, argv, '\n');
236 		} else {
237 			s = getenv("bootargs");
238 			if (s != NULL)
239 				strcpy(confaddr, s);
240 		}
241 	}
242 
243 	entry_point = (void (*)(void))images->ep;
244 
245 	printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
246 	       (ulong)entry_point);
247 
248 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
249 
250 	/*
251 	 * Plan 9 Parameters:
252 	 *   None
253 	 */
254 	(*entry_point)();
255 
256 	return 1;
257 }
258 #endif /* CONFIG_BOOTM_PLAN9 */
259 
260 #if defined(CONFIG_BOOTM_VXWORKS) && \
261 	(defined(CONFIG_PPC) || defined(CONFIG_ARM))
262 
263 void do_bootvx_fdt(bootm_headers_t *images)
264 {
265 #if defined(CONFIG_OF_LIBFDT)
266 	int ret;
267 	char *bootline;
268 	ulong of_size = images->ft_len;
269 	char **of_flat_tree = &images->ft_addr;
270 	struct lmb *lmb = &images->lmb;
271 
272 	if (*of_flat_tree) {
273 		boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
274 
275 		ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
276 		if (ret)
277 			return;
278 
279 		ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
280 		if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
281 			bootline = getenv("bootargs");
282 			if (bootline) {
283 				ret = fdt_find_and_setprop(*of_flat_tree,
284 						"/chosen", "bootargs",
285 						bootline,
286 						strlen(bootline) + 1, 1);
287 				if (ret < 0) {
288 					printf("## ERROR: %s : %s\n", __func__,
289 					       fdt_strerror(ret));
290 					return;
291 				}
292 			}
293 		} else {
294 			printf("## ERROR: %s : %s\n", __func__,
295 			       fdt_strerror(ret));
296 			return;
297 		}
298 	}
299 #endif
300 
301 	boot_prep_vxworks(images);
302 
303 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
304 
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);
308 #else
309 	printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
310 #endif
311 
312 	boot_jump_vxworks(images);
313 
314 	puts("## vxWorks terminated\n");
315 }
316 
317 static int do_bootm_vxworks(int flag, int argc, char * const argv[],
318 			     bootm_headers_t *images)
319 {
320 	if (flag != BOOTM_STATE_OS_GO)
321 		return 0;
322 
323 #if defined(CONFIG_FIT)
324 	if (!images->legacy_hdr_valid) {
325 		fit_unsupported_reset("VxWorks");
326 		return 1;
327 	}
328 #endif
329 
330 	do_bootvx_fdt(images);
331 
332 	return 1;
333 }
334 #endif
335 
336 #if defined(CONFIG_CMD_ELF)
337 static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
338 			    bootm_headers_t *images)
339 {
340 	char *local_args[2];
341 	char str[16];
342 	int dcache;
343 
344 	if (flag != BOOTM_STATE_OS_GO)
345 		return 0;
346 
347 #if defined(CONFIG_FIT)
348 	if (!images->legacy_hdr_valid) {
349 		fit_unsupported_reset("QNX");
350 		return 1;
351 	}
352 #endif
353 
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 */
357 
358 	/*
359 	 * QNX images require the data cache is disabled.
360 	 */
361 	dcache = dcache_status();
362 	if (dcache)
363 		dcache_disable();
364 
365 	do_bootelf(NULL, 0, 2, local_args);
366 
367 	if (dcache)
368 		dcache_enable();
369 
370 	return 1;
371 }
372 #endif
373 
374 #ifdef CONFIG_INTEGRITY
375 static int do_bootm_integrity(int flag, int argc, char * const argv[],
376 			   bootm_headers_t *images)
377 {
378 	void (*entry_point)(void);
379 
380 	if (flag != BOOTM_STATE_OS_GO)
381 		return 0;
382 
383 #if defined(CONFIG_FIT)
384 	if (!images->legacy_hdr_valid) {
385 		fit_unsupported_reset("INTEGRITY");
386 		return 1;
387 	}
388 #endif
389 
390 	entry_point = (void (*)(void))images->ep;
391 
392 	printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
393 	       (ulong)entry_point);
394 
395 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
396 
397 	/*
398 	 * INTEGRITY Parameters:
399 	 *   None
400 	 */
401 	(*entry_point)();
402 
403 	return 1;
404 }
405 #endif
406 
407 #ifdef CONFIG_BOOTM_OPENRTOS
408 static int do_bootm_openrtos(int flag, int argc, char * const argv[],
409 			   bootm_headers_t *images)
410 {
411 	void (*entry_point)(void);
412 
413 	if (flag != BOOTM_STATE_OS_GO)
414 		return 0;
415 
416 	entry_point = (void (*)(void))images->ep;
417 
418 	printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
419 		(ulong)entry_point);
420 
421 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
422 
423 	/*
424 	 * OpenRTOS Parameters:
425 	 *   None
426 	 */
427 	(*entry_point)();
428 
429 	return 1;
430 }
431 #endif
432 
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,
437 #endif
438 #ifdef CONFIG_BOOTM_NETBSD
439 	[IH_OS_NETBSD] = do_bootm_netbsd,
440 #endif
441 #ifdef CONFIG_LYNXKDI
442 	[IH_OS_LYNXOS] = do_bootm_lynxkdi,
443 #endif
444 #ifdef CONFIG_BOOTM_RTEMS
445 	[IH_OS_RTEMS] = do_bootm_rtems,
446 #endif
447 #if defined(CONFIG_BOOTM_OSE)
448 	[IH_OS_OSE] = do_bootm_ose,
449 #endif
450 #if defined(CONFIG_BOOTM_PLAN9)
451 	[IH_OS_PLAN9] = do_bootm_plan9,
452 #endif
453 #if defined(CONFIG_BOOTM_VXWORKS) && \
454 	(defined(CONFIG_PPC) || defined(CONFIG_ARM))
455 	[IH_OS_VXWORKS] = do_bootm_vxworks,
456 #endif
457 #if defined(CONFIG_CMD_ELF)
458 	[IH_OS_QNX] = do_bootm_qnxelf,
459 #endif
460 #ifdef CONFIG_INTEGRITY
461 	[IH_OS_INTEGRITY] = do_bootm_integrity,
462 #endif
463 #ifdef CONFIG_BOOTM_OPENRTOS
464 	[IH_OS_OPENRTOS] = do_bootm_openrtos,
465 #endif
466 };
467 
468 /* Allow for arch specific config before we boot */
469 __weak void arch_preboot_os(void)
470 {
471 	/* please define platform specific arch_preboot_os() */
472 }
473 
474 int boot_selected_os(int argc, char * const argv[], int state,
475 		     bootm_headers_t *images, boot_os_fn *boot_fn)
476 {
477 	arch_preboot_os();
478 	boot_fn(state, argc, argv, images);
479 
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 */
484 		return 0;
485 	bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
486 	debug("\n## Control returned to monitor - resetting...\n");
487 
488 	return BOOTM_ERR_RESET;
489 }
490 
491 boot_os_fn *bootm_os_get_boot_func(int os)
492 {
493 #ifdef CONFIG_NEEDS_MANUAL_RELOC
494 	static bool relocated;
495 
496 	if (!relocated) {
497 		int i;
498 
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;
503 
504 		relocated = true;
505 	}
506 #endif
507 	return boot_os[os];
508 }
509