xref: /openbmc/u-boot/common/bootm.c (revision 2502bfc4)
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 #ifndef USE_HOSTCC
8 #include <common.h>
9 #include <bootstage.h>
10 #include <bzlib.h>
11 #include <errno.h>
12 #include <fdt_support.h>
13 #include <lmb.h>
14 #include <malloc.h>
15 #include <mapmem.h>
16 #include <asm/io.h>
17 #include <linux/lzo.h>
18 #include <lzma/LzmaTypes.h>
19 #include <lzma/LzmaDec.h>
20 #include <lzma/LzmaTools.h>
21 #include <tpm-v2.h>
22 #if defined(CONFIG_CMD_USB)
23 #include <usb.h>
24 #endif
25 #else
26 #include "mkimage.h"
27 #endif
28 
29 #include <command.h>
30 #include <bootm.h>
31 #include <image.h>
32 
33 #ifndef CONFIG_SYS_BOOTM_LEN
34 /* use 8MByte as default max gunzip size */
35 #define CONFIG_SYS_BOOTM_LEN	0x800000
36 #endif
37 
38 #define IH_INITRD_ARCH IH_ARCH_DEFAULT
39 
40 #ifndef USE_HOSTCC
41 
42 DECLARE_GLOBAL_DATA_PTR;
43 
44 bootm_headers_t images;		/* pointers to os/initrd/fdt images */
45 
46 static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
47 				   char * const argv[], bootm_headers_t *images,
48 				   ulong *os_data, ulong *os_len);
49 
board_quiesce_devices(void)50 __weak void board_quiesce_devices(void)
51 {
52 }
53 
54 #ifdef CONFIG_LMB
boot_start_lmb(bootm_headers_t * images)55 static void boot_start_lmb(bootm_headers_t *images)
56 {
57 	ulong		mem_start;
58 	phys_size_t	mem_size;
59 
60 	mem_start = env_get_bootm_low();
61 	mem_size = env_get_bootm_size();
62 
63 	lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
64 				   mem_size, NULL);
65 }
66 #else
67 #define lmb_reserve(lmb, base, size)
boot_start_lmb(bootm_headers_t * images)68 static inline void boot_start_lmb(bootm_headers_t *images) { }
69 #endif
70 
bootm_start(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])71 static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc,
72 		       char * const argv[])
73 {
74 	memset((void *)&images, 0, sizeof(images));
75 	images.verify = env_get_yesno("verify");
76 
77 	boot_start_lmb(&images);
78 
79 	bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
80 	images.state = BOOTM_STATE_START;
81 
82 	return 0;
83 }
84 
bootm_find_os(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])85 static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc,
86 			 char * const argv[])
87 {
88 	const void *os_hdr;
89 	bool ep_found = false;
90 	int ret;
91 
92 	/* get kernel image header, start address and length */
93 	os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
94 			&images, &images.os.image_start, &images.os.image_len);
95 	if (images.os.image_len == 0) {
96 		puts("ERROR: can't get kernel image!\n");
97 		return 1;
98 	}
99 
100 	/* get image parameters */
101 	switch (genimg_get_format(os_hdr)) {
102 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
103 	case IMAGE_FORMAT_LEGACY:
104 		images.os.type = image_get_type(os_hdr);
105 		images.os.comp = image_get_comp(os_hdr);
106 		images.os.os = image_get_os(os_hdr);
107 
108 		images.os.end = image_get_image_end(os_hdr);
109 		images.os.load = image_get_load(os_hdr);
110 		images.os.arch = image_get_arch(os_hdr);
111 		break;
112 #endif
113 #if IMAGE_ENABLE_FIT
114 	case IMAGE_FORMAT_FIT:
115 		if (fit_image_get_type(images.fit_hdr_os,
116 				       images.fit_noffset_os,
117 				       &images.os.type)) {
118 			puts("Can't get image type!\n");
119 			bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
120 			return 1;
121 		}
122 
123 		if (fit_image_get_comp(images.fit_hdr_os,
124 				       images.fit_noffset_os,
125 				       &images.os.comp)) {
126 			puts("Can't get image compression!\n");
127 			bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
128 			return 1;
129 		}
130 
131 		if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
132 				     &images.os.os)) {
133 			puts("Can't get image OS!\n");
134 			bootstage_error(BOOTSTAGE_ID_FIT_OS);
135 			return 1;
136 		}
137 
138 		if (fit_image_get_arch(images.fit_hdr_os,
139 				       images.fit_noffset_os,
140 				       &images.os.arch)) {
141 			puts("Can't get image ARCH!\n");
142 			return 1;
143 		}
144 
145 		images.os.end = fit_get_end(images.fit_hdr_os);
146 
147 		if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
148 				       &images.os.load)) {
149 			puts("Can't get image load address!\n");
150 			bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
151 			return 1;
152 		}
153 		break;
154 #endif
155 #ifdef CONFIG_ANDROID_BOOT_IMAGE
156 	case IMAGE_FORMAT_ANDROID:
157 		images.os.type = IH_TYPE_KERNEL;
158 		images.os.comp = IH_COMP_NONE;
159 		images.os.os = IH_OS_LINUX;
160 
161 		images.os.end = android_image_get_end(os_hdr);
162 		images.os.load = android_image_get_kload(os_hdr);
163 		images.ep = images.os.load;
164 		ep_found = true;
165 		break;
166 #endif
167 	default:
168 		puts("ERROR: unknown image format type!\n");
169 		return 1;
170 	}
171 
172 	/* If we have a valid setup.bin, we will use that for entry (x86) */
173 	if (images.os.arch == IH_ARCH_I386 ||
174 	    images.os.arch == IH_ARCH_X86_64) {
175 		ulong len;
176 
177 		ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
178 		if (ret < 0 && ret != -ENOENT) {
179 			puts("Could not find a valid setup.bin for x86\n");
180 			return 1;
181 		}
182 		/* Kernel entry point is the setup.bin */
183 	} else if (images.legacy_hdr_valid) {
184 		images.ep = image_get_ep(&images.legacy_hdr_os_copy);
185 #if IMAGE_ENABLE_FIT
186 	} else if (images.fit_uname_os) {
187 		int ret;
188 
189 		ret = fit_image_get_entry(images.fit_hdr_os,
190 					  images.fit_noffset_os, &images.ep);
191 		if (ret) {
192 			puts("Can't get entry point property!\n");
193 			return 1;
194 		}
195 #endif
196 	} else if (!ep_found) {
197 		puts("Could not find kernel entry point!\n");
198 		return 1;
199 	}
200 
201 	if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
202 		if (CONFIG_IS_ENABLED(CMD_BOOTI) &&
203 		    images.os.arch == IH_ARCH_ARM64) {
204 			ulong image_addr;
205 			ulong image_size;
206 
207 			ret = booti_setup(images.os.image_start, &image_addr,
208 					  &image_size, true);
209 			if (ret != 0)
210 				return 1;
211 
212 			images.os.type = IH_TYPE_KERNEL;
213 			images.os.load = image_addr;
214 			images.ep = image_addr;
215 		} else {
216 			images.os.load = images.os.image_start;
217 			images.ep += images.os.image_start;
218 		}
219 	}
220 
221 	images.os.start = map_to_sysmem(os_hdr);
222 
223 	return 0;
224 }
225 
226 /**
227  * bootm_find_images - wrapper to find and locate various images
228  * @flag: Ignored Argument
229  * @argc: command argument count
230  * @argv: command argument list
231  *
232  * boot_find_images() will attempt to load an available ramdisk,
233  * flattened device tree, as well as specifically marked
234  * "loadable" images (loadables are FIT only)
235  *
236  * Note: bootm_find_images will skip an image if it is not found
237  *
238  * @return:
239  *     0, if all existing images were loaded correctly
240  *     1, if an image is found but corrupted, or invalid
241  */
bootm_find_images(int flag,int argc,char * const argv[])242 int bootm_find_images(int flag, int argc, char * const argv[])
243 {
244 	int ret;
245 
246 	/* find ramdisk */
247 	ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
248 			       &images.rd_start, &images.rd_end);
249 	if (ret) {
250 		puts("Ramdisk image is corrupt or invalid\n");
251 		return 1;
252 	}
253 
254 #if IMAGE_ENABLE_OF_LIBFDT
255 	/* find flattened device tree */
256 	ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
257 			   &images.ft_addr, &images.ft_len);
258 	if (ret) {
259 		puts("Could not find a valid device tree\n");
260 		return 1;
261 	}
262 	if (CONFIG_IS_ENABLED(CMD_FDT))
263 		set_working_fdt_addr(map_to_sysmem(images.ft_addr));
264 #endif
265 
266 #if IMAGE_ENABLE_FIT
267 #if defined(CONFIG_FPGA)
268 	/* find bitstreams */
269 	ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
270 			    NULL, NULL);
271 	if (ret) {
272 		printf("FPGA image is corrupted or invalid\n");
273 		return 1;
274 	}
275 #endif
276 
277 	/* find all of the loadables */
278 	ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
279 			       NULL, NULL);
280 	if (ret) {
281 		printf("Loadable(s) is corrupt or invalid\n");
282 		return 1;
283 	}
284 #endif
285 
286 	return 0;
287 }
288 
bootm_find_other(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])289 static int bootm_find_other(cmd_tbl_t *cmdtp, int flag, int argc,
290 			    char * const argv[])
291 {
292 	if (((images.os.type == IH_TYPE_KERNEL) ||
293 	     (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
294 	     (images.os.type == IH_TYPE_MULTI)) &&
295 	    (images.os.os == IH_OS_LINUX ||
296 		 images.os.os == IH_OS_VXWORKS))
297 		return bootm_find_images(flag, argc, argv);
298 
299 	return 0;
300 }
301 #endif /* USE_HOSTC */
302 
303 /**
304  * print_decomp_msg() - Print a suitable decompression/loading message
305  *
306  * @type:	OS type (IH_OS_...)
307  * @comp_type:	Compression type being used (IH_COMP_...)
308  * @is_xip:	true if the load address matches the image start
309  */
print_decomp_msg(int comp_type,int type,bool is_xip)310 static void print_decomp_msg(int comp_type, int type, bool is_xip)
311 {
312 	const char *name = genimg_get_type_name(type);
313 
314 	if (comp_type == IH_COMP_NONE)
315 		printf("   %s %s ... ", is_xip ? "XIP" : "Loading", name);
316 	else
317 		printf("   Uncompressing %s ... ", name);
318 }
319 
320 /**
321  * handle_decomp_error() - display a decompression error
322  *
323  * This function tries to produce a useful message. In the case where the
324  * uncompressed size is the same as the available space, we can assume that
325  * the image is too large for the buffer.
326  *
327  * @comp_type:		Compression type being used (IH_COMP_...)
328  * @uncomp_size:	Number of bytes uncompressed
329  * @unc_len:		Amount of space available for decompression
330  * @ret:		Error code to report
331  * @return BOOTM_ERR_RESET, indicating that the board must be reset
332  */
handle_decomp_error(int comp_type,size_t uncomp_size,size_t unc_len,int ret)333 static int handle_decomp_error(int comp_type, size_t uncomp_size,
334 			       size_t unc_len, int ret)
335 {
336 	const char *name = genimg_get_comp_name(comp_type);
337 
338 	if (uncomp_size >= unc_len)
339 		printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
340 	else
341 		printf("%s: uncompress error %d\n", name, ret);
342 
343 	/*
344 	 * The decompression routines are now safe, so will not write beyond
345 	 * their bounds. Probably it is not necessary to reset, but maintain
346 	 * the current behaviour for now.
347 	 */
348 	printf("Must RESET board to recover\n");
349 #ifndef USE_HOSTCC
350 	bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
351 #endif
352 
353 	return BOOTM_ERR_RESET;
354 }
355 
bootm_decomp_image(int comp,ulong load,ulong image_start,int type,void * load_buf,void * image_buf,ulong image_len,uint unc_len,ulong * load_end)356 int bootm_decomp_image(int comp, ulong load, ulong image_start, int type,
357 		       void *load_buf, void *image_buf, ulong image_len,
358 		       uint unc_len, ulong *load_end)
359 {
360 	int ret = 0;
361 
362 	*load_end = load;
363 	print_decomp_msg(comp, type, load == image_start);
364 
365 	/*
366 	 * Load the image to the right place, decompressing if needed. After
367 	 * this, image_len will be set to the number of uncompressed bytes
368 	 * loaded, ret will be non-zero on error.
369 	 */
370 	switch (comp) {
371 	case IH_COMP_NONE:
372 		if (load == image_start)
373 			break;
374 		if (image_len <= unc_len)
375 			memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
376 		else
377 			ret = 1;
378 		break;
379 #ifdef CONFIG_GZIP
380 	case IH_COMP_GZIP: {
381 		ret = gunzip(load_buf, unc_len, image_buf, &image_len);
382 		break;
383 	}
384 #endif /* CONFIG_GZIP */
385 #ifdef CONFIG_BZIP2
386 	case IH_COMP_BZIP2: {
387 		uint size = unc_len;
388 
389 		/*
390 		 * If we've got less than 4 MB of malloc() space,
391 		 * use slower decompression algorithm which requires
392 		 * at most 2300 KB of memory.
393 		 */
394 		ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
395 			image_buf, image_len,
396 			CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
397 		image_len = size;
398 		break;
399 	}
400 #endif /* CONFIG_BZIP2 */
401 #ifdef CONFIG_LZMA
402 	case IH_COMP_LZMA: {
403 		SizeT lzma_len = unc_len;
404 
405 		ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
406 					       image_buf, image_len);
407 		image_len = lzma_len;
408 		break;
409 	}
410 #endif /* CONFIG_LZMA */
411 #ifdef CONFIG_LZO
412 	case IH_COMP_LZO: {
413 		size_t size = unc_len;
414 
415 		ret = lzop_decompress(image_buf, image_len, load_buf, &size);
416 		image_len = size;
417 		break;
418 	}
419 #endif /* CONFIG_LZO */
420 #ifdef CONFIG_LZ4
421 	case IH_COMP_LZ4: {
422 		size_t size = unc_len;
423 
424 		ret = ulz4fn(image_buf, image_len, load_buf, &size);
425 		image_len = size;
426 		break;
427 	}
428 #endif /* CONFIG_LZ4 */
429 	default:
430 		printf("Unimplemented compression type %d\n", comp);
431 		return BOOTM_ERR_UNIMPLEMENTED;
432 	}
433 
434 	if (ret)
435 		return handle_decomp_error(comp, image_len, unc_len, ret);
436 	*load_end = load + image_len;
437 
438 	puts("OK\n");
439 
440 	return 0;
441 }
442 
443 #ifndef USE_HOSTCC
bootm_load_os(bootm_headers_t * images,int boot_progress)444 static int bootm_load_os(bootm_headers_t *images, int boot_progress)
445 {
446 	image_info_t os = images->os;
447 	ulong load = os.load;
448 	ulong load_end;
449 	ulong blob_start = os.start;
450 	ulong blob_end = os.end;
451 	ulong image_start = os.image_start;
452 	ulong image_len = os.image_len;
453 	ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
454 	ulong flush_len;
455 	bool no_overlap;
456 	void *load_buf, *image_buf;
457 	int err;
458 
459 	load_buf = map_sysmem(load, 0);
460 	image_buf = map_sysmem(os.image_start, image_len);
461 	err = bootm_decomp_image(os.comp, load, os.image_start, os.type,
462 				 load_buf, image_buf, image_len,
463 				 CONFIG_SYS_BOOTM_LEN, &load_end);
464 	if (err) {
465 		bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
466 		return err;
467 	}
468 
469 	flush_len = load_end - load;
470 	if (flush_start < load)
471 		flush_len += load - flush_start;
472 
473 	flush_cache(flush_start, ALIGN(flush_len, ARCH_DMA_MINALIGN));
474 
475 	debug("   kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
476 	bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
477 
478 	no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
479 
480 	if (!no_overlap && load < blob_end && load_end > blob_start) {
481 		debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
482 		      blob_start, blob_end);
483 		debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
484 		      load_end);
485 
486 		/* Check what type of image this is. */
487 		if (images->legacy_hdr_valid) {
488 			if (image_get_type(&images->legacy_hdr_os_copy)
489 					== IH_TYPE_MULTI)
490 				puts("WARNING: legacy format multi component image overwritten\n");
491 			return BOOTM_ERR_OVERLAP;
492 		} else {
493 			puts("ERROR: new format image overwritten - must RESET the board to recover\n");
494 			bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
495 			return BOOTM_ERR_RESET;
496 		}
497 	}
498 
499 	lmb_reserve(&images->lmb, images->os.load, (load_end -
500 						    images->os.load));
501 	return 0;
502 }
503 
504 /**
505  * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
506  *
507  * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
508  *	enabled)
509  */
bootm_disable_interrupts(void)510 ulong bootm_disable_interrupts(void)
511 {
512 	ulong iflag;
513 
514 	/*
515 	 * We have reached the point of no return: we are going to
516 	 * overwrite all exception vector code, so we cannot easily
517 	 * recover from any failures any more...
518 	 */
519 	iflag = disable_interrupts();
520 #ifdef CONFIG_NETCONSOLE
521 	/* Stop the ethernet stack if NetConsole could have left it up */
522 	eth_halt();
523 # ifndef CONFIG_DM_ETH
524 	eth_unregister(eth_get_dev());
525 # endif
526 #endif
527 
528 #if defined(CONFIG_CMD_USB)
529 	/*
530 	 * turn off USB to prevent the host controller from writing to the
531 	 * SDRAM while Linux is booting. This could happen (at least for OHCI
532 	 * controller), because the HCCA (Host Controller Communication Area)
533 	 * lies within the SDRAM and the host controller writes continously to
534 	 * this area (as busmaster!). The HccaFrameNumber is for example
535 	 * updated every 1 ms within the HCCA structure in SDRAM! For more
536 	 * details see the OpenHCI specification.
537 	 */
538 	usb_stop();
539 #endif
540 	return iflag;
541 }
542 
543 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
544 
545 #define CONSOLE_ARG     "console="
546 #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
547 
fixup_silent_linux(void)548 static void fixup_silent_linux(void)
549 {
550 	char *buf;
551 	const char *env_val;
552 	char *cmdline = env_get("bootargs");
553 	int want_silent;
554 
555 	/*
556 	 * Only fix cmdline when requested. The environment variable can be:
557 	 *
558 	 *	no - we never fixup
559 	 *	yes - we always fixup
560 	 *	unset - we rely on the console silent flag
561 	 */
562 	want_silent = env_get_yesno("silent_linux");
563 	if (want_silent == 0)
564 		return;
565 	else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
566 		return;
567 
568 	debug("before silent fix-up: %s\n", cmdline);
569 	if (cmdline && (cmdline[0] != '\0')) {
570 		char *start = strstr(cmdline, CONSOLE_ARG);
571 
572 		/* Allocate space for maximum possible new command line */
573 		buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
574 		if (!buf) {
575 			debug("%s: out of memory\n", __func__);
576 			return;
577 		}
578 
579 		if (start) {
580 			char *end = strchr(start, ' ');
581 			int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
582 
583 			strncpy(buf, cmdline, num_start_bytes);
584 			if (end)
585 				strcpy(buf + num_start_bytes, end);
586 			else
587 				buf[num_start_bytes] = '\0';
588 		} else {
589 			sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
590 		}
591 		env_val = buf;
592 	} else {
593 		buf = NULL;
594 		env_val = CONSOLE_ARG;
595 	}
596 
597 	env_set("bootargs", env_val);
598 	debug("after silent fix-up: %s\n", env_val);
599 	free(buf);
600 }
601 #endif /* CONFIG_SILENT_CONSOLE */
602 
bootm_measure(struct bootm_headers * images)603 int bootm_measure(struct bootm_headers *images)
604 {
605 	int ret = 0;
606 
607 	if (IS_ENABLED(CONFIG_MEASURED_BOOT)) {
608 		struct tcg2_event_log elog;
609 		struct udevice *dev;
610 		void *initrd_buf;
611 		void *image_buf;
612 		const char *s;
613 		u32 rd_len;
614 		bool ign;
615 
616 		elog.log_size = 0;
617 		ign = IS_ENABLED(CONFIG_MEASURE_IGNORE_LOG);
618 		ret = tcg2_measurement_init(&dev, &elog, ign);
619 		if (ret)
620 			return ret;
621 
622 		image_buf = map_sysmem(images->os.image_start,
623 				       images->os.image_len);
624 		ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len,
625 					image_buf, EV_COMPACT_HASH,
626 					strlen("linux") + 1, (u8 *)"linux");
627 		if (ret)
628 			goto unmap_image;
629 
630 		rd_len = images->rd_end - images->rd_start;
631 		initrd_buf = map_sysmem(images->rd_start, rd_len);
632 		ret = tcg2_measure_data(dev, &elog, 9, rd_len, initrd_buf,
633 					EV_COMPACT_HASH, strlen("initrd") + 1,
634 					(u8 *)"initrd");
635 		if (ret)
636 			goto unmap_initrd;
637 
638 		if (IS_ENABLED(CONFIG_MEASURE_DEVICETREE)) {
639 			ret = tcg2_measure_data(dev, &elog, 0, images->ft_len,
640 						(u8 *)images->ft_addr,
641 						EV_TABLE_OF_DEVICES,
642 						strlen("dts") + 1,
643 						(u8 *)"dts");
644 			if (ret)
645 				goto unmap_initrd;
646 		}
647 
648 		s = env_get("bootargs");
649 		if (!s)
650 			s = "";
651 		ret = tcg2_measure_data(dev, &elog, 1, strlen(s) + 1, (u8 *)s,
652 					EV_PLATFORM_CONFIG_FLAGS,
653 					strlen(s) + 1, (u8 *)s);
654 
655 unmap_initrd:
656 		unmap_sysmem(initrd_buf);
657 
658 unmap_image:
659 		unmap_sysmem(image_buf);
660 		tcg2_measurement_term(dev, &elog, ret != 0);
661 	}
662 
663 	return ret;
664 }
665 
666 /**
667  * Execute selected states of the bootm command.
668  *
669  * Note the arguments to this state must be the first argument, Any 'bootm'
670  * or sub-command arguments must have already been taken.
671  *
672  * Note that if states contains more than one flag it MUST contain
673  * BOOTM_STATE_START, since this handles and consumes the command line args.
674  *
675  * Also note that aside from boot_os_fn functions and bootm_load_os no other
676  * functions we store the return value of in 'ret' may use a negative return
677  * value, without special handling.
678  *
679  * @param cmdtp		Pointer to bootm command table entry
680  * @param flag		Command flags (CMD_FLAG_...)
681  * @param argc		Number of subcommand arguments (0 = no arguments)
682  * @param argv		Arguments
683  * @param states	Mask containing states to run (BOOTM_STATE_...)
684  * @param images	Image header information
685  * @param boot_progress 1 to show boot progress, 0 to not do this
686  * @return 0 if ok, something else on error. Some errors will cause this
687  *	function to perform a reboot! If states contains BOOTM_STATE_OS_GO
688  *	then the intent is to boot an OS, so this function will not return
689  *	unless the image type is standalone.
690  */
do_bootm_states(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[],int states,bootm_headers_t * images,int boot_progress)691 int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
692 		    int states, bootm_headers_t *images, int boot_progress)
693 {
694 	boot_os_fn *boot_fn;
695 	ulong iflag = 0;
696 	int ret = 0, need_boot_fn;
697 
698 	images->state |= states;
699 
700 	/*
701 	 * Work through the states and see how far we get. We stop on
702 	 * any error.
703 	 */
704 	if (states & BOOTM_STATE_START)
705 		ret = bootm_start(cmdtp, flag, argc, argv);
706 
707 	if (!ret && (states & BOOTM_STATE_FINDOS))
708 		ret = bootm_find_os(cmdtp, flag, argc, argv);
709 
710 	if (!ret && (states & BOOTM_STATE_FINDOTHER))
711 		ret = bootm_find_other(cmdtp, flag, argc, argv);
712 
713 	if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret &&
714 	    (states & BOOTM_STATE_MEASURE))
715 		bootm_measure(images);
716 
717 	/* Load the OS */
718 	if (!ret && (states & BOOTM_STATE_LOADOS)) {
719 		iflag = bootm_disable_interrupts();
720 		ret = bootm_load_os(images, 0);
721 		if (ret && ret != BOOTM_ERR_OVERLAP)
722 			goto err;
723 		else if (ret == BOOTM_ERR_OVERLAP)
724 			ret = 0;
725 	}
726 
727 	/* Relocate the ramdisk */
728 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
729 	if (!ret && (states & BOOTM_STATE_RAMDISK)) {
730 		ulong rd_len = images->rd_end - images->rd_start;
731 
732 		ret = boot_ramdisk_high(&images->lmb, images->rd_start,
733 			rd_len, &images->initrd_start, &images->initrd_end);
734 		if (!ret) {
735 			env_set_hex("initrd_start", images->initrd_start);
736 			env_set_hex("initrd_end", images->initrd_end);
737 		}
738 	}
739 #endif
740 #if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB)
741 	if (!ret && (states & BOOTM_STATE_FDT)) {
742 		boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
743 		ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
744 					&images->ft_len);
745 	}
746 #endif
747 
748 	/* From now on, we need the OS boot function */
749 	if (ret)
750 		return ret;
751 	boot_fn = bootm_os_get_boot_func(images->os.os);
752 	need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
753 			BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
754 			BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
755 	if (boot_fn == NULL && need_boot_fn) {
756 		if (iflag)
757 			enable_interrupts();
758 		printf("ERROR: booting os '%s' (%d) is not supported\n",
759 		       genimg_get_os_name(images->os.os), images->os.os);
760 		bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
761 		return 1;
762 	}
763 
764 
765 	/* Call various other states that are not generally used */
766 	if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
767 		ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
768 	if (!ret && (states & BOOTM_STATE_OS_BD_T))
769 		ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
770 	if (!ret && (states & BOOTM_STATE_OS_PREP)) {
771 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
772 		if (images->os.os == IH_OS_LINUX)
773 			fixup_silent_linux();
774 #endif
775 		ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
776 	}
777 
778 #ifdef CONFIG_TRACE
779 	/* Pretend to run the OS, then run a user command */
780 	if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
781 		char *cmd_list = env_get("fakegocmd");
782 
783 		ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
784 				images, boot_fn);
785 		if (!ret && cmd_list)
786 			ret = run_command_list(cmd_list, -1, flag);
787 	}
788 #endif
789 
790 	/* Check for unsupported subcommand. */
791 	if (ret) {
792 		puts("subcommand not supported\n");
793 		return ret;
794 	}
795 
796 	/* Now run the OS! We hope this doesn't return */
797 	if (!ret && (states & BOOTM_STATE_OS_GO))
798 		ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
799 				images, boot_fn);
800 
801 	/* Deal with any fallout */
802 err:
803 	if (iflag)
804 		enable_interrupts();
805 
806 	if (ret == BOOTM_ERR_UNIMPLEMENTED)
807 		bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
808 	else if (ret == BOOTM_ERR_RESET)
809 		do_reset(cmdtp, flag, argc, argv);
810 
811 	return ret;
812 }
813 
814 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
815 /**
816  * image_get_kernel - verify legacy format kernel image
817  * @img_addr: in RAM address of the legacy format image to be verified
818  * @verify: data CRC verification flag
819  *
820  * image_get_kernel() verifies legacy image integrity and returns pointer to
821  * legacy image header if image verification was completed successfully.
822  *
823  * returns:
824  *     pointer to a legacy image header if valid image was found
825  *     otherwise return NULL
826  */
image_get_kernel(ulong img_addr,int verify)827 static image_header_t *image_get_kernel(ulong img_addr, int verify)
828 {
829 	image_header_t *hdr = (image_header_t *)img_addr;
830 
831 	if (!image_check_magic(hdr)) {
832 		puts("Bad Magic Number\n");
833 		bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
834 		return NULL;
835 	}
836 	bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
837 
838 	if (!image_check_hcrc(hdr)) {
839 		puts("Bad Header Checksum\n");
840 		bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
841 		return NULL;
842 	}
843 
844 	bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
845 	image_print_contents(hdr);
846 
847 	if (verify) {
848 		puts("   Verifying Checksum ... ");
849 		if (!image_check_dcrc(hdr)) {
850 			printf("Bad Data CRC\n");
851 			bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
852 			return NULL;
853 		}
854 		puts("OK\n");
855 	}
856 	bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
857 
858 	if (!image_check_target_arch(hdr)) {
859 		printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
860 		bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
861 		return NULL;
862 	}
863 	return hdr;
864 }
865 #endif
866 
867 /**
868  * boot_get_kernel - find kernel image
869  * @os_data: pointer to a ulong variable, will hold os data start address
870  * @os_len: pointer to a ulong variable, will hold os data length
871  *
872  * boot_get_kernel() tries to find a kernel image, verifies its integrity
873  * and locates kernel data.
874  *
875  * returns:
876  *     pointer to image header if valid image was found, plus kernel start
877  *     address and length, otherwise NULL
878  */
boot_get_kernel(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[],bootm_headers_t * images,ulong * os_data,ulong * os_len)879 static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
880 				   char * const argv[], bootm_headers_t *images,
881 				   ulong *os_data, ulong *os_len)
882 {
883 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
884 	image_header_t	*hdr;
885 #endif
886 	ulong		img_addr;
887 	const void *buf;
888 	const char	*fit_uname_config = NULL;
889 	const char	*fit_uname_kernel = NULL;
890 #if IMAGE_ENABLE_FIT
891 	int		os_noffset;
892 #endif
893 
894 	img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
895 					      &fit_uname_config,
896 					      &fit_uname_kernel);
897 
898 	bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
899 
900 	/* check image type, for FIT images get FIT kernel node */
901 	*os_data = *os_len = 0;
902 	buf = map_sysmem(img_addr, 0);
903 	switch (genimg_get_format(buf)) {
904 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
905 	case IMAGE_FORMAT_LEGACY:
906 		printf("## Booting kernel from Legacy Image at %08lx ...\n",
907 		       img_addr);
908 		hdr = image_get_kernel(img_addr, images->verify);
909 		if (!hdr)
910 			return NULL;
911 		bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
912 
913 		/* get os_data and os_len */
914 		switch (image_get_type(hdr)) {
915 		case IH_TYPE_KERNEL:
916 		case IH_TYPE_KERNEL_NOLOAD:
917 			*os_data = image_get_data(hdr);
918 			*os_len = image_get_data_size(hdr);
919 			break;
920 		case IH_TYPE_MULTI:
921 			image_multi_getimg(hdr, 0, os_data, os_len);
922 			break;
923 		case IH_TYPE_STANDALONE:
924 			*os_data = image_get_data(hdr);
925 			*os_len = image_get_data_size(hdr);
926 			break;
927 		default:
928 			printf("Wrong Image Type for %s command\n",
929 			       cmdtp->name);
930 			bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
931 			return NULL;
932 		}
933 
934 		/*
935 		 * copy image header to allow for image overwrites during
936 		 * kernel decompression.
937 		 */
938 		memmove(&images->legacy_hdr_os_copy, hdr,
939 			sizeof(image_header_t));
940 
941 		/* save pointer to image header */
942 		images->legacy_hdr_os = hdr;
943 
944 		images->legacy_hdr_valid = 1;
945 		bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
946 		break;
947 #endif
948 #if IMAGE_ENABLE_FIT
949 	case IMAGE_FORMAT_FIT:
950 		os_noffset = fit_image_load(images, img_addr,
951 				&fit_uname_kernel, &fit_uname_config,
952 				IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
953 				BOOTSTAGE_ID_FIT_KERNEL_START,
954 				FIT_LOAD_IGNORED, os_data, os_len);
955 		if (os_noffset < 0)
956 			return NULL;
957 
958 		images->fit_hdr_os = map_sysmem(img_addr, 0);
959 		images->fit_uname_os = fit_uname_kernel;
960 		images->fit_uname_cfg = fit_uname_config;
961 		images->fit_noffset_os = os_noffset;
962 		break;
963 #endif
964 #ifdef CONFIG_ANDROID_BOOT_IMAGE
965 	case IMAGE_FORMAT_ANDROID:
966 		printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
967 		if (android_image_get_kernel(buf, images->verify,
968 					     os_data, os_len))
969 			return NULL;
970 		break;
971 #endif
972 	default:
973 		printf("Wrong Image Format for %s command\n", cmdtp->name);
974 		bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
975 		return NULL;
976 	}
977 
978 	debug("   kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
979 	      *os_data, *os_len, *os_len);
980 
981 	return buf;
982 }
983 
984 /**
985  * switch_to_non_secure_mode() - switch to non-secure mode
986  *
987  * This routine is overridden by architectures requiring this feature.
988  */
switch_to_non_secure_mode(void)989 void __weak switch_to_non_secure_mode(void)
990 {
991 }
992 
993 #else /* USE_HOSTCC */
994 
memmove_wd(void * to,void * from,size_t len,ulong chunksz)995 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
996 {
997 	memmove(to, from, len);
998 }
999 
bootm_host_load_image(const void * fit,int req_image_type)1000 static int bootm_host_load_image(const void *fit, int req_image_type)
1001 {
1002 	const char *fit_uname_config = NULL;
1003 	ulong data, len;
1004 	bootm_headers_t images;
1005 	int noffset;
1006 	ulong load_end;
1007 	uint8_t image_type;
1008 	uint8_t imape_comp;
1009 	void *load_buf;
1010 	int ret;
1011 
1012 	memset(&images, '\0', sizeof(images));
1013 	images.verify = 1;
1014 	noffset = fit_image_load(&images, (ulong)fit,
1015 		NULL, &fit_uname_config,
1016 		IH_ARCH_DEFAULT, req_image_type, -1,
1017 		FIT_LOAD_IGNORED, &data, &len);
1018 	if (noffset < 0)
1019 		return noffset;
1020 	if (fit_image_get_type(fit, noffset, &image_type)) {
1021 		puts("Can't get image type!\n");
1022 		return -EINVAL;
1023 	}
1024 
1025 	if (fit_image_get_comp(fit, noffset, &imape_comp)) {
1026 		puts("Can't get image compression!\n");
1027 		return -EINVAL;
1028 	}
1029 
1030 	/* Allow the image to expand by a factor of 4, should be safe */
1031 	load_buf = malloc((1 << 20) + len * 4);
1032 	ret = bootm_decomp_image(imape_comp, 0, data, image_type, load_buf,
1033 				 (void *)data, len, CONFIG_SYS_BOOTM_LEN,
1034 				 &load_end);
1035 	free(load_buf);
1036 
1037 	if (ret && ret != BOOTM_ERR_UNIMPLEMENTED)
1038 		return ret;
1039 
1040 	return 0;
1041 }
1042 
bootm_host_load_images(const void * fit,int cfg_noffset)1043 int bootm_host_load_images(const void *fit, int cfg_noffset)
1044 {
1045 	static uint8_t image_types[] = {
1046 		IH_TYPE_KERNEL,
1047 		IH_TYPE_FLATDT,
1048 		IH_TYPE_RAMDISK,
1049 	};
1050 	int err = 0;
1051 	int i;
1052 
1053 	for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1054 		int ret;
1055 
1056 		ret = bootm_host_load_image(fit, image_types[i]);
1057 		if (!err && ret && ret != -ENOENT)
1058 			err = ret;
1059 	}
1060 
1061 	/* Return the first error we found */
1062 	return err;
1063 }
1064 
1065 #endif /* ndef USE_HOSTCC */
1066