1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2009
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 #include <common.h>
8 #include <bootm.h>
9 #include <bootstage.h>
10 #include <cpu_func.h>
11 #include <efi_loader.h>
12 #include <env.h>
13 #include <fdt_support.h>
14 #include <image.h>
15 #include <lmb.h>
16 #include <log.h>
17 #include <asm/global_data.h>
18 #include <linux/libfdt.h>
19 #include <malloc.h>
20 #include <mapmem.h>
21 #include <vxworks.h>
22 #include <tee/optee.h>
23 
24 DECLARE_GLOBAL_DATA_PTR;
25 
do_bootm_standalone(int flag,int argc,char * const argv[],bootm_headers_t * images)26 static int do_bootm_standalone(int flag, int argc, char *const argv[],
27 			       bootm_headers_t *images)
28 {
29 	char *s;
30 	int (*appl)(int, char *const[]);
31 
32 	/* Don't start if "autostart" is set to "no" */
33 	s = env_get("autostart");
34 	if ((s != NULL) && !strcmp(s, "no")) {
35 		env_set_hex("filesize", images->os.image_len);
36 		return 0;
37 	}
38 	appl = (int (*)(int, char * const []))images->ep;
39 	appl(argc, argv);
40 	return 0;
41 }
42 
43 /*******************************************************************/
44 /* OS booting routines */
45 /*******************************************************************/
46 
47 #if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
copy_args(char * dest,int argc,char * const argv[],char delim)48 static void copy_args(char *dest, int argc, char *const argv[], char delim)
49 {
50 	int i;
51 
52 	for (i = 0; i < argc; i++) {
53 		if (i > 0)
54 			*dest++ = delim;
55 		strcpy(dest, argv[i]);
56 		dest += strlen(argv[i]);
57 	}
58 }
59 #endif
60 
fit_unsupported_reset(const char * msg)61 static void __maybe_unused fit_unsupported_reset(const char *msg)
62 {
63 	if (CONFIG_IS_ENABLED(FIT_VERBOSE)) {
64 		printf("! FIT images not supported for '%s' - must reset board to recover!\n",
65 		       msg);
66 	}
67 }
68 
69 #ifdef CONFIG_BOOTM_NETBSD
do_bootm_netbsd(int flag,int argc,char * const argv[],bootm_headers_t * images)70 static int do_bootm_netbsd(int flag, int argc, char *const argv[],
71 			   bootm_headers_t *images)
72 {
73 	void (*loader)(struct bd_info *, image_header_t *, char *, char *);
74 	image_header_t *os_hdr, *hdr;
75 	ulong kernel_data, kernel_len;
76 	char *cmdline;
77 
78 	if (flag != BOOTM_STATE_OS_GO)
79 		return 0;
80 
81 #if defined(CONFIG_FIT)
82 	if (!images->legacy_hdr_valid) {
83 		fit_unsupported_reset("NetBSD");
84 		return 1;
85 	}
86 #endif
87 	hdr = images->legacy_hdr_os;
88 
89 	/*
90 	 * Booting a (NetBSD) kernel image
91 	 *
92 	 * This process is pretty similar to a standalone application:
93 	 * The (first part of an multi-) image must be a stage-2 loader,
94 	 * which in turn is responsible for loading & invoking the actual
95 	 * kernel.  The only differences are the parameters being passed:
96 	 * besides the board info strucure, the loader expects a command
97 	 * line, the name of the console device, and (optionally) the
98 	 * address of the original image header.
99 	 */
100 	os_hdr = NULL;
101 	if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
102 		image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
103 		if (kernel_len)
104 			os_hdr = hdr;
105 	}
106 
107 	if (argc > 0) {
108 		ulong len;
109 		int   i;
110 
111 		for (i = 0, len = 0; i < argc; i += 1)
112 			len += strlen(argv[i]) + 1;
113 		cmdline = malloc(len);
114 		copy_args(cmdline, argc, argv, ' ');
115 	} else {
116 		cmdline = env_get("bootargs");
117 		if (cmdline == NULL)
118 			cmdline = "";
119 	}
120 
121 	loader = (void (*)(struct bd_info *, image_header_t *, char *, char *))images->ep;
122 
123 	printf("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
124 	       (ulong)loader);
125 
126 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
127 
128 	/*
129 	 * NetBSD Stage-2 Loader Parameters:
130 	 *   arg[0]: pointer to board info data
131 	 *   arg[1]: image load address
132 	 *   arg[2]: char pointer to the console device to use
133 	 *   arg[3]: char pointer to the boot arguments
134 	 */
135 	(*loader)(gd->bd, os_hdr, "", cmdline);
136 
137 	return 1;
138 }
139 #endif /* CONFIG_BOOTM_NETBSD*/
140 
141 #ifdef CONFIG_LYNXKDI
do_bootm_lynxkdi(int flag,int argc,char * const argv[],bootm_headers_t * images)142 static int do_bootm_lynxkdi(int flag, int argc, char *const argv[],
143 			    bootm_headers_t *images)
144 {
145 	image_header_t *hdr = &images->legacy_hdr_os_copy;
146 
147 	if (flag != BOOTM_STATE_OS_GO)
148 		return 0;
149 
150 #if defined(CONFIG_FIT)
151 	if (!images->legacy_hdr_valid) {
152 		fit_unsupported_reset("Lynx");
153 		return 1;
154 	}
155 #endif
156 
157 	lynxkdi_boot((image_header_t *)hdr);
158 
159 	return 1;
160 }
161 #endif /* CONFIG_LYNXKDI */
162 
163 #ifdef CONFIG_BOOTM_RTEMS
do_bootm_rtems(int flag,int argc,char * const argv[],bootm_headers_t * images)164 static int do_bootm_rtems(int flag, int argc, char *const argv[],
165 			  bootm_headers_t *images)
166 {
167 	void (*entry_point)(struct bd_info *);
168 
169 	if (flag != BOOTM_STATE_OS_GO)
170 		return 0;
171 
172 #if defined(CONFIG_FIT)
173 	if (!images->legacy_hdr_valid) {
174 		fit_unsupported_reset("RTEMS");
175 		return 1;
176 	}
177 #endif
178 
179 	entry_point = (void (*)(struct bd_info *))images->ep;
180 
181 	printf("## Transferring control to RTEMS (at address %08lx) ...\n",
182 	       (ulong)entry_point);
183 
184 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
185 
186 	/*
187 	 * RTEMS Parameters:
188 	 *   r3: ptr to board info data
189 	 */
190 	(*entry_point)(gd->bd);
191 
192 	return 1;
193 }
194 #endif /* CONFIG_BOOTM_RTEMS */
195 
196 #if defined(CONFIG_BOOTM_OSE)
do_bootm_ose(int flag,int argc,char * const argv[],bootm_headers_t * images)197 static int do_bootm_ose(int flag, int argc, char *const argv[],
198 			bootm_headers_t *images)
199 {
200 	void (*entry_point)(void);
201 
202 	if (flag != BOOTM_STATE_OS_GO)
203 		return 0;
204 
205 #if defined(CONFIG_FIT)
206 	if (!images->legacy_hdr_valid) {
207 		fit_unsupported_reset("OSE");
208 		return 1;
209 	}
210 #endif
211 
212 	entry_point = (void (*)(void))images->ep;
213 
214 	printf("## Transferring control to OSE (at address %08lx) ...\n",
215 	       (ulong)entry_point);
216 
217 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
218 
219 	/*
220 	 * OSE Parameters:
221 	 *   None
222 	 */
223 	(*entry_point)();
224 
225 	return 1;
226 }
227 #endif /* CONFIG_BOOTM_OSE */
228 
229 #if defined(CONFIG_BOOTM_PLAN9)
do_bootm_plan9(int flag,int argc,char * const argv[],bootm_headers_t * images)230 static int do_bootm_plan9(int flag, int argc, char *const argv[],
231 			  bootm_headers_t *images)
232 {
233 	void (*entry_point)(void);
234 	char *s;
235 
236 	if (flag != BOOTM_STATE_OS_GO)
237 		return 0;
238 
239 #if defined(CONFIG_FIT)
240 	if (!images->legacy_hdr_valid) {
241 		fit_unsupported_reset("Plan 9");
242 		return 1;
243 	}
244 #endif
245 
246 	/* See README.plan9 */
247 	s = env_get("confaddr");
248 	if (s != NULL) {
249 		char *confaddr = (char *)hextoul(s, NULL);
250 
251 		if (argc > 0) {
252 			copy_args(confaddr, argc, argv, '\n');
253 		} else {
254 			s = env_get("bootargs");
255 			if (s != NULL)
256 				strcpy(confaddr, s);
257 		}
258 	}
259 
260 	entry_point = (void (*)(void))images->ep;
261 
262 	printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
263 	       (ulong)entry_point);
264 
265 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
266 
267 	/*
268 	 * Plan 9 Parameters:
269 	 *   None
270 	 */
271 	(*entry_point)();
272 
273 	return 1;
274 }
275 #endif /* CONFIG_BOOTM_PLAN9 */
276 
277 #if defined(CONFIG_BOOTM_VXWORKS) && \
278 	(defined(CONFIG_PPC) || defined(CONFIG_ARM))
279 
do_bootvx_fdt(bootm_headers_t * images)280 static void do_bootvx_fdt(bootm_headers_t *images)
281 {
282 #if defined(CONFIG_OF_LIBFDT)
283 	int ret;
284 	char *bootline;
285 	ulong of_size = images->ft_len;
286 	char **of_flat_tree = &images->ft_addr;
287 	struct lmb *lmb = &images->lmb;
288 
289 	if (*of_flat_tree) {
290 		boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
291 
292 		ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
293 		if (ret)
294 			return;
295 
296 		/* Update ethernet nodes */
297 		fdt_fixup_ethernet(*of_flat_tree);
298 
299 		ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
300 		if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
301 			bootline = env_get("bootargs");
302 			if (bootline) {
303 				ret = fdt_find_and_setprop(*of_flat_tree,
304 						"/chosen", "bootargs",
305 						bootline,
306 						strlen(bootline) + 1, 1);
307 				if (ret < 0) {
308 					printf("## ERROR: %s : %s\n", __func__,
309 					       fdt_strerror(ret));
310 					return;
311 				}
312 			}
313 		} else {
314 			printf("## ERROR: %s : %s\n", __func__,
315 			       fdt_strerror(ret));
316 			return;
317 		}
318 	}
319 #endif
320 
321 	boot_prep_vxworks(images);
322 
323 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
324 
325 #if defined(CONFIG_OF_LIBFDT)
326 	printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
327 	       (ulong)images->ep, (ulong)*of_flat_tree);
328 #else
329 	printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
330 #endif
331 
332 	boot_jump_vxworks(images);
333 
334 	puts("## vxWorks terminated\n");
335 }
336 
do_bootm_vxworks_legacy(int flag,int argc,char * const argv[],bootm_headers_t * images)337 static int do_bootm_vxworks_legacy(int flag, int argc, char *const argv[],
338 				   bootm_headers_t *images)
339 {
340 	if (flag != BOOTM_STATE_OS_GO)
341 		return 0;
342 
343 #if defined(CONFIG_FIT)
344 	if (!images->legacy_hdr_valid) {
345 		fit_unsupported_reset("VxWorks");
346 		return 1;
347 	}
348 #endif
349 
350 	do_bootvx_fdt(images);
351 
352 	return 1;
353 }
354 
do_bootm_vxworks(int flag,int argc,char * const argv[],bootm_headers_t * images)355 int do_bootm_vxworks(int flag, int argc, char *const argv[],
356 		     bootm_headers_t *images)
357 {
358 	char *bootargs;
359 	int pos;
360 	unsigned long vxflags;
361 	bool std_dtb = false;
362 
363 	/* get bootargs env */
364 	bootargs = env_get("bootargs");
365 
366 	if (bootargs != NULL) {
367 		for (pos = 0; pos < strlen(bootargs); pos++) {
368 			/* find f=0xnumber flag */
369 			if ((bootargs[pos] == '=') && (pos >= 1) &&
370 			    (bootargs[pos - 1] == 'f')) {
371 				vxflags = hextoul(&bootargs[pos + 1], NULL);
372 				if (vxflags & VXWORKS_SYSFLG_STD_DTB)
373 					std_dtb = true;
374 			}
375 		}
376 	}
377 
378 	if (std_dtb) {
379 		if (flag & BOOTM_STATE_OS_PREP)
380 			printf("   Using standard DTB\n");
381 		return do_bootm_linux(flag, argc, argv, images);
382 	} else {
383 		if (flag & BOOTM_STATE_OS_PREP)
384 			printf("   !!! WARNING !!! Using legacy DTB\n");
385 		return do_bootm_vxworks_legacy(flag, argc, argv, images);
386 	}
387 }
388 #endif
389 
390 #if defined(CONFIG_CMD_ELF)
do_bootm_qnxelf(int flag,int argc,char * const argv[],bootm_headers_t * images)391 static int do_bootm_qnxelf(int flag, int argc, char *const argv[],
392 			   bootm_headers_t *images)
393 {
394 	char *local_args[2];
395 	char str[16];
396 	int dcache;
397 
398 	if (flag != BOOTM_STATE_OS_GO)
399 		return 0;
400 
401 #if defined(CONFIG_FIT)
402 	if (!images->legacy_hdr_valid) {
403 		fit_unsupported_reset("QNX");
404 		return 1;
405 	}
406 #endif
407 
408 	sprintf(str, "%lx", images->ep); /* write entry-point into string */
409 	local_args[0] = argv[0];
410 	local_args[1] = str;	/* and provide it via the arguments */
411 
412 	/*
413 	 * QNX images require the data cache is disabled.
414 	 */
415 	dcache = dcache_status();
416 	if (dcache)
417 		dcache_disable();
418 
419 	do_bootelf(NULL, 0, 2, local_args);
420 
421 	if (dcache)
422 		dcache_enable();
423 
424 	return 1;
425 }
426 #endif
427 
428 #ifdef CONFIG_INTEGRITY
do_bootm_integrity(int flag,int argc,char * const argv[],bootm_headers_t * images)429 static int do_bootm_integrity(int flag, int argc, char *const argv[],
430 			      bootm_headers_t *images)
431 {
432 	void (*entry_point)(void);
433 
434 	if (flag != BOOTM_STATE_OS_GO)
435 		return 0;
436 
437 #if defined(CONFIG_FIT)
438 	if (!images->legacy_hdr_valid) {
439 		fit_unsupported_reset("INTEGRITY");
440 		return 1;
441 	}
442 #endif
443 
444 	entry_point = (void (*)(void))images->ep;
445 
446 	printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
447 	       (ulong)entry_point);
448 
449 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
450 
451 	/*
452 	 * INTEGRITY Parameters:
453 	 *   None
454 	 */
455 	(*entry_point)();
456 
457 	return 1;
458 }
459 #endif
460 
461 #ifdef CONFIG_BOOTM_OPENRTOS
do_bootm_openrtos(int flag,int argc,char * const argv[],bootm_headers_t * images)462 static int do_bootm_openrtos(int flag, int argc, char *const argv[],
463 			     bootm_headers_t *images)
464 {
465 	void (*entry_point)(void);
466 
467 	if (flag != BOOTM_STATE_OS_GO)
468 		return 0;
469 
470 	entry_point = (void (*)(void))images->ep;
471 
472 	printf("## Transferring control to OpenRTOS (at address %08lx) ...\n",
473 		(ulong)entry_point);
474 
475 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
476 
477 	/*
478 	 * OpenRTOS Parameters:
479 	 *   None
480 	 */
481 	(*entry_point)();
482 
483 	return 1;
484 }
485 #endif
486 
487 #ifdef CONFIG_BOOTM_OPTEE
do_bootm_tee(int flag,int argc,char * const argv[],bootm_headers_t * images)488 static int do_bootm_tee(int flag, int argc, char *const argv[],
489 			bootm_headers_t *images)
490 {
491 	int ret;
492 
493 	/* Verify OS type */
494 	if (images->os.os != IH_OS_TEE) {
495 		return 1;
496 	};
497 
498 	/* Validate OPTEE header */
499 	ret = optee_verify_bootm_image(images->os.image_start,
500 				       images->os.load,
501 				       images->os.image_len);
502 	if (ret)
503 		return ret;
504 
505 	/* Locate FDT etc */
506 	ret = bootm_find_images(flag, argc, argv, 0, 0);
507 	if (ret)
508 		return ret;
509 
510 	/* From here we can run the regular linux boot path */
511 	return do_bootm_linux(flag, argc, argv, images);
512 }
513 #endif
514 
515 #ifdef CONFIG_BOOTM_EFI
do_bootm_efi(int flag,int argc,char * const argv[],bootm_headers_t * images)516 static int do_bootm_efi(int flag, int argc, char *const argv[],
517 			bootm_headers_t *images)
518 {
519 	int ret;
520 	efi_status_t efi_ret;
521 	void *image_buf;
522 
523 	if (flag != BOOTM_STATE_OS_GO)
524 		return 0;
525 
526 	/* Locate FDT, if provided */
527 	ret = bootm_find_images(flag, argc, argv, 0, 0);
528 	if (ret)
529 		return ret;
530 
531 	/* Initialize EFI drivers */
532 	efi_ret = efi_init_obj_list();
533 	if (efi_ret != EFI_SUCCESS) {
534 		printf("## Failed to initialize UEFI sub-system: r = %lu\n",
535 		       efi_ret & ~EFI_ERROR_MASK);
536 		return 1;
537 	}
538 
539 	/* Install device tree */
540 	efi_ret = efi_install_fdt(images->ft_len
541 				  ? images->ft_addr : EFI_FDT_USE_INTERNAL);
542 	if (efi_ret != EFI_SUCCESS) {
543 		printf("## Failed to install device tree: r = %lu\n",
544 		       efi_ret & ~EFI_ERROR_MASK);
545 		return 1;
546 	}
547 
548 	/* Run EFI image */
549 	printf("## Transferring control to EFI (at address %08lx) ...\n",
550 	       images->ep);
551 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
552 
553 	/* We expect to return */
554 	images->os.type = IH_TYPE_STANDALONE;
555 
556 	image_buf = map_sysmem(images->ep, images->os.image_len);
557 
558 	efi_ret = efi_run_image(image_buf, images->os.image_len);
559 	if (efi_ret != EFI_SUCCESS)
560 		return 1;
561 	return 0;
562 }
563 #endif
564 
565 static boot_os_fn *boot_os[] = {
566 	[IH_OS_U_BOOT] = do_bootm_standalone,
567 #ifdef CONFIG_BOOTM_LINUX
568 	[IH_OS_LINUX] = do_bootm_linux,
569 #endif
570 #ifdef CONFIG_BOOTM_NETBSD
571 	[IH_OS_NETBSD] = do_bootm_netbsd,
572 #endif
573 #ifdef CONFIG_LYNXKDI
574 	[IH_OS_LYNXOS] = do_bootm_lynxkdi,
575 #endif
576 #ifdef CONFIG_BOOTM_RTEMS
577 	[IH_OS_RTEMS] = do_bootm_rtems,
578 #endif
579 #if defined(CONFIG_BOOTM_OSE)
580 	[IH_OS_OSE] = do_bootm_ose,
581 #endif
582 #if defined(CONFIG_BOOTM_PLAN9)
583 	[IH_OS_PLAN9] = do_bootm_plan9,
584 #endif
585 #if defined(CONFIG_BOOTM_VXWORKS) && \
586 	(defined(CONFIG_PPC) || defined(CONFIG_ARM) || defined(CONFIG_RISCV))
587 	[IH_OS_VXWORKS] = do_bootm_vxworks,
588 #endif
589 #if defined(CONFIG_CMD_ELF)
590 	[IH_OS_QNX] = do_bootm_qnxelf,
591 #endif
592 #ifdef CONFIG_INTEGRITY
593 	[IH_OS_INTEGRITY] = do_bootm_integrity,
594 #endif
595 #ifdef CONFIG_BOOTM_OPENRTOS
596 	[IH_OS_OPENRTOS] = do_bootm_openrtos,
597 #endif
598 #ifdef CONFIG_BOOTM_OPTEE
599 	[IH_OS_TEE] = do_bootm_tee,
600 #endif
601 #ifdef CONFIG_BOOTM_EFI
602 	[IH_OS_EFI] = do_bootm_efi,
603 #endif
604 };
605 
606 /* Allow for arch specific config before we boot */
arch_preboot_os(void)607 __weak void arch_preboot_os(void)
608 {
609 	/* please define platform specific arch_preboot_os() */
610 }
611 
612 /* Allow for board specific config before we boot */
board_preboot_os(void)613 __weak void board_preboot_os(void)
614 {
615 	/* please define board specific board_preboot_os() */
616 }
617 
boot_selected_os(int argc,char * const argv[],int state,bootm_headers_t * images,boot_os_fn * boot_fn)618 int boot_selected_os(int argc, char *const argv[], int state,
619 		     bootm_headers_t *images, boot_os_fn *boot_fn)
620 {
621 	arch_preboot_os();
622 	board_preboot_os();
623 	boot_fn(state, argc, argv, images);
624 
625 	/* Stand-alone may return when 'autostart' is 'no' */
626 	if (images->os.type == IH_TYPE_STANDALONE ||
627 	    IS_ENABLED(CONFIG_SANDBOX) ||
628 	    state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
629 		return 0;
630 	bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
631 	debug("\n## Control returned to monitor - resetting...\n");
632 
633 	return BOOTM_ERR_RESET;
634 }
635 
bootm_os_get_boot_func(int os)636 boot_os_fn *bootm_os_get_boot_func(int os)
637 {
638 #ifdef CONFIG_NEEDS_MANUAL_RELOC
639 	static bool relocated;
640 
641 	if (!relocated) {
642 		int i;
643 
644 		/* relocate boot function table */
645 		for (i = 0; i < ARRAY_SIZE(boot_os); i++)
646 			if (boot_os[i] != NULL)
647 				boot_os[i] += gd->reloc_off;
648 
649 		relocated = true;
650 	}
651 #endif
652 	return boot_os[os];
653 }
654