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