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