xref: /rk3399_rockchip-uboot/common/bootm.c (revision f36ea2f6e17621c4d9dd97c4dbfab62d03d061df)
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 	set_working_fdt_addr((ulong)images.ft_addr);
269 	lmb_reserve(&images.lmb, (ulong)images.ft_addr, (ulong)images.ft_len);
270 #endif
271 
272 #if IMAGE_ENABLE_FIT
273 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_XILINX)
274 	/* find bitstreams */
275 	ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
276 			    NULL, NULL);
277 	if (ret) {
278 		printf("FPGA image is corrupted or invalid\n");
279 		return 1;
280 	}
281 #endif
282 
283 	/* find all of the loadables */
284 	ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
285 			       NULL, NULL);
286 	if (ret) {
287 		printf("Loadable(s) is corrupt or invalid\n");
288 		return 1;
289 	}
290 #endif
291 
292 	return 0;
293 }
294 
295 static int bootm_find_other(cmd_tbl_t *cmdtp, int flag, int argc,
296 			    char * const argv[])
297 {
298 	if (((images.os.type == IH_TYPE_KERNEL) ||
299 	     (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
300 	     (images.os.type == IH_TYPE_MULTI)) &&
301 	    (images.os.os == IH_OS_LINUX ||
302 		 images.os.os == IH_OS_VXWORKS))
303 		return bootm_find_images(flag, argc, argv);
304 
305 	return 0;
306 }
307 #endif /* USE_HOSTC */
308 
309 /**
310  * print_decomp_msg() - Print a suitable decompression/loading message
311  *
312  * @type:	OS type (IH_OS_...)
313  * @comp_type:	Compression type being used (IH_COMP_...)
314  * @is_xip:	true if the load address matches the image start
315  */
316 static void print_decomp_msg(int comp_type, int type, bool is_xip,
317 			     ulong src, ulong dst)
318 {
319 	const char *name = genimg_get_type_name(type);
320 	const char *comp_name[] = {
321 		[IH_COMP_NONE]  = "",
322 		[IH_COMP_GZIP]  = "GZIP",
323 		[IH_COMP_BZIP2] = "BZIP2",
324 		[IH_COMP_LZMA]  = "LZMA",
325 		[IH_COMP_LZO]   = "LZO",
326 		[IH_COMP_LZ4]   = "LZ4",
327 		[IH_COMP_ZIMAGE]= "ZIMAGE",
328 	};
329 
330 	if (comp_type == IH_COMP_NONE)
331 		printf("   %s %s from 0x%08lx to 0x%08lx ... ",
332 		       is_xip ? "XIP" : "Loading", name, src, dst);
333 	else
334 		printf("   Uncompressing %s %s from 0x%08lx to 0x%08lx ... ",
335 		       comp_name[comp_type], name, src, dst);
336 }
337 
338 /**
339  * handle_decomp_error() - display a decompression error
340  *
341  * This function tries to produce a useful message. In the case where the
342  * uncompressed size is the same as the available space, we can assume that
343  * the image is too large for the buffer.
344  *
345  * @comp_type:		Compression type being used (IH_COMP_...)
346  * @uncomp_size:	Number of bytes uncompressed
347  * @unc_len:		Amount of space available for decompression
348  * @ret:		Error code to report
349  * @return BOOTM_ERR_RESET, indicating that the board must be reset
350  */
351 static int handle_decomp_error(int comp_type, size_t uncomp_size,
352 			       size_t unc_len, int ret)
353 {
354 	const char *name = genimg_get_comp_name(comp_type);
355 
356 	if (uncomp_size >= unc_len)
357 		printf("Image too large(0x%lx >= 0x%lx): increase CONFIG_SYS_BOOTM_LEN\n",
358 		       (ulong)uncomp_size, (ulong)unc_len);
359 	else
360 		printf("%s: uncompress error %d\n", name, ret);
361 
362 	/*
363 	 * The decompression routines are now safe, so will not write beyond
364 	 * their bounds. Probably it is not necessary to reset, but maintain
365 	 * the current behaviour for now.
366 	 */
367 	printf("Must RESET board to recover\n");
368 #ifndef USE_HOSTCC
369 	bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
370 #endif
371 
372 	return BOOTM_ERR_RESET;
373 }
374 
375 int bootm_parse_comp(const unsigned char *hdr)
376 {
377 #if defined(CONFIG_CMD_BOOTZ)
378 	ulong start, end;
379 
380 	if (!bootz_setup((ulong)hdr, &start, &end))
381 		return IH_COMP_ZIMAGE;
382 #endif
383 #if defined(CONFIG_LZ4)
384 	if (lz4_is_valid_header(hdr))
385 		return IH_COMP_LZ4;
386 #endif
387 #if defined(CONFIG_LZO)
388 	if (lzop_is_valid_header(hdr))
389 		return IH_COMP_LZO;
390 #endif
391 #if defined(CONFIG_GZIP)
392 	if (gzip_parse_header(hdr, 0xffff) > 0)
393 		return IH_COMP_GZIP;
394 #endif
395 #if defined(CONFIG_BZIP2)
396 	if ((hdr[0] == 'B') && (hdr[1] == 'Z') && (hdr[2] == 'h'))
397 		return IH_COMP_BZIP2;
398 #endif
399 #if defined(CONFIG_LZMA)
400 	if (lzma_is_valid(hdr))
401 		return IH_COMP_LZMA;
402 #endif
403 
404 	return IH_COMP_NONE;
405 }
406 
407 int bootm_decomp_image(int comp, ulong load, ulong image_start, int type,
408 		       void *load_buf, void *image_buf, ulong image_len,
409 		       uint unc_len, ulong *load_end)
410 {
411 	int ret = 0;
412 
413 	*load_end = load;
414 	print_decomp_msg(comp, type, load == image_start,
415 		(ulong)image_buf, (ulong)load_buf);
416 
417 	/*
418 	 * Load the image to the right place, decompressing if needed. After
419 	 * this, image_len will be set to the number of uncompressed bytes
420 	 * loaded, ret will be non-zero on error.
421 	 */
422 	switch (comp) {
423 	case IH_COMP_NONE:
424 		if (load == image_start)
425 			break;
426 		if (image_len <= unc_len)
427 			memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
428 		else
429 			ret = 1;
430 		break;
431 #ifdef CONFIG_GZIP
432 	case IH_COMP_GZIP: {
433 		ret = gunzip(load_buf, unc_len, image_buf, &image_len);
434 		break;
435 	}
436 #endif /* CONFIG_GZIP */
437 #ifdef CONFIG_BZIP2
438 	case IH_COMP_BZIP2: {
439 		uint size = unc_len;
440 
441 		/*
442 		 * If we've got less than 4 MB of malloc() space,
443 		 * use slower decompression algorithm which requires
444 		 * at most 2300 KB of memory.
445 		 */
446 		ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
447 			image_buf, image_len,
448 			CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
449 		image_len = size;
450 		break;
451 	}
452 #endif /* CONFIG_BZIP2 */
453 #ifdef CONFIG_LZMA
454 	case IH_COMP_LZMA: {
455 		SizeT lzma_len = unc_len;
456 
457 		ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
458 					       image_buf, image_len);
459 		image_len = lzma_len;
460 		break;
461 	}
462 #endif /* CONFIG_LZMA */
463 #ifdef CONFIG_LZO
464 	case IH_COMP_LZO: {
465 		size_t size = unc_len;
466 
467 		ret = lzop_decompress(image_buf, image_len, load_buf, &size);
468 		image_len = size;
469 		break;
470 	}
471 #endif /* CONFIG_LZO */
472 #ifdef CONFIG_LZ4
473 	case IH_COMP_LZ4: {
474 		size_t size = unc_len;
475 
476 		ret = ulz4fn(image_buf, image_len, load_buf, &size);
477 		image_len = size;
478 		break;
479 	}
480 #endif /* CONFIG_LZ4 */
481 	default:
482 		printf("Unimplemented compression type %d\n", comp);
483 		return BOOTM_ERR_UNIMPLEMENTED;
484 	}
485 
486 	if (ret)
487 		return handle_decomp_error(comp, image_len, unc_len, ret);
488 	*load_end = load + image_len;
489 
490 	if (comp == IH_COMP_NONE || comp == IH_COMP_ZIMAGE)
491 		puts("OK\n");
492 	else
493 		printf("with %08lx bytes OK\n", image_len);
494 
495 	return 0;
496 }
497 
498 #ifndef USE_HOSTCC
499 static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end,
500 			 int boot_progress)
501 {
502 	image_info_t os = images->os;
503 	ulong load = os.load;
504 	ulong blob_start = os.start;
505 	ulong blob_end = os.end;
506 	ulong image_start = os.image_start;
507 	ulong image_len = os.image_len;
508 	bool no_overlap;
509 	void *load_buf, *image_buf;
510 	int err;
511 
512 	load_buf = map_sysmem(load, 0);
513 	image_buf = map_sysmem(os.image_start, image_len);
514 	err = bootm_decomp_image(os.comp, load, os.image_start, os.type,
515 				 load_buf, image_buf, image_len,
516 				 CONFIG_SYS_BOOTM_LEN, load_end);
517 	if (err) {
518 		bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
519 		return err;
520 	}
521 	flush_cache(load, ALIGN(*load_end - load, ARCH_DMA_MINALIGN));
522 
523 	debug("   kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
524 	bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
525 
526 	no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
527 
528 	if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
529 		printf("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
530 		       blob_start, blob_end);
531 		printf("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
532 		       *load_end);
533 
534 		/* Check what type of image this is. */
535 		if (images->legacy_hdr_valid) {
536 			if (image_get_type(&images->legacy_hdr_os_copy)
537 					== IH_TYPE_MULTI)
538 				puts("WARNING: legacy format multi component image overwritten\n");
539 			return BOOTM_ERR_OVERLAP;
540 		} else {
541 			puts("ERROR: new format image overwritten - must RESET the board to recover\n");
542 			bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
543 			return BOOTM_ERR_RESET;
544 		}
545 	}
546 
547 	return 0;
548 }
549 
550 /**
551  * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
552  *
553  * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
554  *	enabled)
555  */
556 ulong bootm_disable_interrupts(void)
557 {
558 	ulong iflag;
559 
560 	/*
561 	 * Do not go further if usb is boot device,
562 	 * We may access usb at late sequence.
563 	 */
564 	if (!strcmp(env_get("devtype"), "usb"))
565 		return 0;
566 
567 	/*
568 	 * We have reached the point of no return: we are going to
569 	 * overwrite all exception vector code, so we cannot easily
570 	 * recover from any failures any more...
571 	 */
572 	iflag = disable_interrupts();
573 #ifdef CONFIG_NETCONSOLE
574 	/* Stop the ethernet stack if NetConsole could have left it up */
575 	eth_halt();
576 # ifndef CONFIG_DM_ETH
577 	eth_unregister(eth_get_dev());
578 # endif
579 #endif
580 
581 #if defined(CONFIG_CMD_USB)
582 	/*
583 	 * turn off USB to prevent the host controller from writing to the
584 	 * SDRAM while Linux is booting. This could happen (at least for OHCI
585 	 * controller), because the HCCA (Host Controller Communication Area)
586 	 * lies within the SDRAM and the host controller writes continously to
587 	 * this area (as busmaster!). The HccaFrameNumber is for example
588 	 * updated every 1 ms within the HCCA structure in SDRAM! For more
589 	 * details see the OpenHCI specification.
590 	 */
591 	usb_stop();
592 #endif
593 	return iflag;
594 }
595 
596 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
597 
598 #define CONSOLE_ARG     "console="
599 #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
600 
601 static void fixup_silent_linux(void)
602 {
603 	char *buf;
604 	const char *env_val;
605 	char *cmdline = env_get("bootargs");
606 	int want_silent;
607 
608 	/*
609 	 * Only fix cmdline when requested. The environment variable can be:
610 	 *
611 	 *	no - we never fixup
612 	 *	yes - we always fixup
613 	 *	unset - we rely on the console silent flag
614 	 */
615 	want_silent = env_get_yesno("silent_linux");
616 	if (want_silent == 0)
617 		return;
618 	else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
619 		return;
620 
621 	debug("before silent fix-up: %s\n", cmdline);
622 	if (cmdline && (cmdline[0] != '\0')) {
623 		char *start = strstr(cmdline, CONSOLE_ARG);
624 
625 		/* Allocate space for maximum possible new command line */
626 		buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
627 		if (!buf) {
628 			debug("%s: out of memory\n", __func__);
629 			return;
630 		}
631 
632 		if (start) {
633 			char *end = strchr(start, ' ');
634 			int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
635 
636 			strncpy(buf, cmdline, num_start_bytes);
637 			if (end)
638 				strcpy(buf + num_start_bytes, end);
639 			else
640 				buf[num_start_bytes] = '\0';
641 		} else {
642 			sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
643 		}
644 		env_val = buf;
645 	} else {
646 		buf = NULL;
647 		env_val = CONSOLE_ARG;
648 	}
649 
650 	env_set("bootargs", env_val);
651 	debug("after silent fix-up: %s\n", env_val);
652 	free(buf);
653 }
654 #endif /* CONFIG_SILENT_CONSOLE */
655 
656 /**
657  * Execute selected states of the bootm command.
658  *
659  * Note the arguments to this state must be the first argument, Any 'bootm'
660  * or sub-command arguments must have already been taken.
661  *
662  * Note that if states contains more than one flag it MUST contain
663  * BOOTM_STATE_START, since this handles and consumes the command line args.
664  *
665  * Also note that aside from boot_os_fn functions and bootm_load_os no other
666  * functions we store the return value of in 'ret' may use a negative return
667  * value, without special handling.
668  *
669  * @param cmdtp		Pointer to bootm command table entry
670  * @param flag		Command flags (CMD_FLAG_...)
671  * @param argc		Number of subcommand arguments (0 = no arguments)
672  * @param argv		Arguments
673  * @param states	Mask containing states to run (BOOTM_STATE_...)
674  * @param images	Image header information
675  * @param boot_progress 1 to show boot progress, 0 to not do this
676  * @return 0 if ok, something else on error. Some errors will cause this
677  *	function to perform a reboot! If states contains BOOTM_STATE_OS_GO
678  *	then the intent is to boot an OS, so this function will not return
679  *	unless the image type is standalone.
680  */
681 int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
682 		    int states, bootm_headers_t *images, int boot_progress)
683 {
684 	boot_os_fn *boot_fn;
685 	ulong iflag = 0;
686 	int ret = 0, need_boot_fn;
687 
688 	images->state |= states;
689 
690 	/*
691 	 * Work through the states and see how far we get. We stop on
692 	 * any error.
693 	 */
694 	if (states & BOOTM_STATE_START)
695 		ret = bootm_start(cmdtp, flag, argc, argv);
696 
697 	if (!ret && (states & BOOTM_STATE_FINDOS))
698 		ret = bootm_find_os(cmdtp, flag, argc, argv);
699 
700 	if (!ret && (states & BOOTM_STATE_FINDOTHER))
701 		ret = bootm_find_other(cmdtp, flag, argc, argv);
702 
703 	/* Load the OS */
704 	if (!ret && (states & BOOTM_STATE_LOADOS)) {
705 		ulong load_end;
706 
707 		iflag = bootm_disable_interrupts();
708 		ret = bootm_load_os(images, &load_end, 0);
709 		if (ret == 0)
710 			lmb_reserve(&images->lmb, images->os.load,
711 				    (load_end - images->os.load));
712 		else if (ret && ret != BOOTM_ERR_OVERLAP)
713 			goto err;
714 		else if (ret == BOOTM_ERR_OVERLAP)
715 			ret = 0;
716 	}
717 
718 	/* Resever memory before any lmb_alloc, as early as possible */
719 #if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB)
720 	if (!ret && ((states & BOOTM_STATE_RAMDISK) ||
721 	    (states & BOOTM_STATE_FDT)))
722 		boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
723 #endif
724 	/* Relocate the ramdisk */
725 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
726 	if (!ret && (states & BOOTM_STATE_RAMDISK)) {
727 		ulong rd_len = images->rd_end - images->rd_start;
728 
729 		ret = boot_ramdisk_high(&images->lmb, images->rd_start,
730 			rd_len, &images->initrd_start, &images->initrd_end);
731 		if (!ret) {
732 			env_set_hex("initrd_start", images->initrd_start);
733 			env_set_hex("initrd_end", images->initrd_end);
734 		}
735 	}
736 #endif
737 #if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB)
738 	if (!ret && (states & BOOTM_STATE_FDT)) {
739 		ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
740 					&images->ft_len);
741 	}
742 #endif
743 
744 	/* From now on, we need the OS boot function */
745 	if (ret)
746 		return ret;
747 	boot_fn = bootm_os_get_boot_func(images->os.os);
748 	need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
749 			BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
750 			BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
751 	if (boot_fn == NULL && need_boot_fn) {
752 		if (iflag)
753 			enable_interrupts();
754 		printf("ERROR: booting os '%s' (%d) is not supported\n",
755 		       genimg_get_os_name(images->os.os), images->os.os);
756 		bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
757 		return 1;
758 	}
759 
760 
761 	/* Call various other states that are not generally used */
762 	if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
763 		ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
764 	if (!ret && (states & BOOTM_STATE_OS_BD_T))
765 		ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
766 	if (!ret && (states & BOOTM_STATE_OS_PREP)) {
767 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
768 		if (images->os.os == IH_OS_LINUX)
769 			fixup_silent_linux();
770 #endif
771 		arch_preboot_os(BOOTM_STATE_OS_PREP);
772 
773 		ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
774 	}
775 
776 #ifdef CONFIG_TRACE
777 	/* Pretend to run the OS, then run a user command */
778 	if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
779 		char *cmd_list = env_get("fakegocmd");
780 
781 		ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
782 				images, boot_fn);
783 		if (!ret && cmd_list)
784 			ret = run_command_list(cmd_list, -1, flag);
785 	}
786 #endif
787 
788 	/* Check for unsupported subcommand. */
789 	if (ret) {
790 		puts("subcommand not supported\n");
791 		return ret;
792 	}
793 
794 	/* Now run the OS! We hope this doesn't return */
795 	if (!ret && (states & BOOTM_STATE_OS_GO))
796 		ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
797 				images, boot_fn);
798 
799 	/* Deal with any fallout */
800 err:
801 	if (iflag)
802 		enable_interrupts();
803 
804 	if (ret == BOOTM_ERR_UNIMPLEMENTED)
805 		bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
806 	else if (ret == BOOTM_ERR_RESET)
807 		do_reset(cmdtp, flag, argc, argv);
808 
809 	return ret;
810 }
811 
812 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
813 /**
814  * image_get_kernel - verify legacy format kernel image
815  * @img_addr: in RAM address of the legacy format image to be verified
816  * @verify: data CRC verification flag
817  *
818  * image_get_kernel() verifies legacy image integrity and returns pointer to
819  * legacy image header if image verification was completed successfully.
820  *
821  * returns:
822  *     pointer to a legacy image header if valid image was found
823  *     otherwise return NULL
824  */
825 static image_header_t *image_get_kernel(ulong img_addr, int verify)
826 {
827 	image_header_t *hdr = (image_header_t *)img_addr;
828 
829 	if (!image_check_magic(hdr)) {
830 		puts("Bad Magic Number\n");
831 		bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
832 		return NULL;
833 	}
834 	bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
835 
836 	if (!image_check_hcrc(hdr)) {
837 		puts("Bad Header Checksum\n");
838 		bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
839 		return NULL;
840 	}
841 
842 	bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
843 	image_print_contents(hdr);
844 
845 	if (verify) {
846 		puts("   Verifying Checksum ... ");
847 		if (!image_check_dcrc(hdr)) {
848 			printf("Bad Data CRC\n");
849 			bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
850 			return NULL;
851 		}
852 		puts("OK\n");
853 	}
854 	bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
855 
856 	if (!image_check_target_arch(hdr)) {
857 		printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
858 		bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
859 		return NULL;
860 	}
861 	return hdr;
862 }
863 #endif
864 
865 /**
866  * boot_get_kernel - find kernel image
867  * @os_data: pointer to a ulong variable, will hold os data start address
868  * @os_len: pointer to a ulong variable, will hold os data length
869  *
870  * boot_get_kernel() tries to find a kernel image, verifies its integrity
871  * and locates kernel data.
872  *
873  * returns:
874  *     pointer to image header if valid image was found, plus kernel start
875  *     address and length, otherwise NULL
876  */
877 static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
878 				   char * const argv[], bootm_headers_t *images,
879 				   ulong *os_data, ulong *os_len)
880 {
881 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
882 	image_header_t	*hdr;
883 #endif
884 	ulong		img_addr;
885 	const void *buf;
886 	const char	*fit_uname_config = NULL;
887 	const char	*fit_uname_kernel = NULL;
888 #if IMAGE_ENABLE_FIT
889 	int		os_noffset;
890 #endif
891 
892 	img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
893 					      &fit_uname_config,
894 					      &fit_uname_kernel);
895 
896 	bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
897 
898 	/* check image type, for FIT images get FIT kernel node */
899 	*os_data = *os_len = 0;
900 	buf = map_sysmem(img_addr, 0);
901 	switch (genimg_get_format(buf)) {
902 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
903 	case IMAGE_FORMAT_LEGACY:
904 		printf("## Booting kernel from Legacy Image at %08lx ...\n",
905 		       img_addr);
906 		hdr = image_get_kernel(img_addr, images->verify);
907 		if (!hdr)
908 			return NULL;
909 		bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
910 
911 		/* get os_data and os_len */
912 		switch (image_get_type(hdr)) {
913 		case IH_TYPE_KERNEL:
914 		case IH_TYPE_KERNEL_NOLOAD:
915 			*os_data = image_get_data(hdr);
916 			*os_len = image_get_data_size(hdr);
917 			break;
918 		case IH_TYPE_MULTI:
919 			image_multi_getimg(hdr, 0, os_data, os_len);
920 			break;
921 		case IH_TYPE_STANDALONE:
922 			*os_data = image_get_data(hdr);
923 			*os_len = image_get_data_size(hdr);
924 			break;
925 		default:
926 			if (cmdtp)
927 				printf("Wrong Image Type for %s command\n",
928 				       cmdtp->name);
929 			bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
930 			return NULL;
931 		}
932 
933 		/*
934 		 * copy image header to allow for image overwrites during
935 		 * kernel decompression.
936 		 */
937 		memmove(&images->legacy_hdr_os_copy, hdr,
938 			sizeof(image_header_t));
939 
940 		/* save pointer to image header */
941 		images->legacy_hdr_os = hdr;
942 
943 		images->legacy_hdr_valid = 1;
944 		bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
945 		break;
946 #endif
947 #if IMAGE_ENABLE_FIT
948 	case IMAGE_FORMAT_FIT:
949 		os_noffset = fit_image_load(images, img_addr,
950 				&fit_uname_kernel, &fit_uname_config,
951 				IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
952 				BOOTSTAGE_ID_FIT_KERNEL_START,
953 				FIT_LOAD_IGNORED, os_data, os_len);
954 		if (os_noffset < 0)
955 			return NULL;
956 
957 		images->fit_hdr_os = map_sysmem(img_addr, 0);
958 		images->fit_uname_os = fit_uname_kernel;
959 		images->fit_uname_cfg = fit_uname_config;
960 		images->fit_noffset_os = os_noffset;
961 		break;
962 #endif
963 #ifdef CONFIG_ANDROID_BOOT_IMAGE
964 	case IMAGE_FORMAT_ANDROID:
965 		printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
966 		if (android_image_get_kernel(buf, images->verify,
967 					     os_data, os_len))
968 			return NULL;
969 		break;
970 #endif
971 	default:
972 		if (cmdtp)
973 			printf("Wrong Image Format for %s command\n",
974 			       cmdtp->name);
975 		bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
976 		return NULL;
977 	}
978 
979 	debug("   kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
980 	      *os_data, *os_len, *os_len);
981 
982 	return buf;
983 }
984 #else /* USE_HOSTCC */
985 
986 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
987 {
988 	memmove(to, from, len);
989 }
990 
991 static int bootm_host_load_image(const void *fit, int req_image_type, int index)
992 {
993 	const char *fit_uname_config = NULL;
994 	ulong data, len;
995 	bootm_headers_t images;
996 	int noffset;
997 	ulong load_end;
998 	uint8_t image_type;
999 	uint8_t imape_comp;
1000 	void *load_buf;
1001 	int ret;
1002 
1003 	memset(&images, '\0', sizeof(images));
1004 	images.verify = 1;
1005 	noffset = fit_image_load_index(&images, (ulong)fit,
1006 		NULL, &fit_uname_config,
1007 		IH_ARCH_DEFAULT, req_image_type, index, -1,
1008 		FIT_LOAD_IGNORED, &data, &len);
1009 	if (noffset < 0)
1010 		return noffset;
1011 	if (fit_image_get_type(fit, noffset, &image_type)) {
1012 		puts("Can't get image type!\n");
1013 		return -EINVAL;
1014 	}
1015 
1016 	if (fit_image_get_comp(fit, noffset, &imape_comp)) {
1017 		puts("Can't get image compression!\n");
1018 		return -EINVAL;
1019 	}
1020 
1021 	/* Allow the image to expand by a factor of 4, should be safe */
1022 	load_buf = malloc((1 << 20) + len * 4);
1023 	ret = bootm_decomp_image(imape_comp, 0, data, image_type, load_buf,
1024 				 (void *)data, len, CONFIG_SYS_BOOTM_LEN,
1025 				 &load_end);
1026 	free(load_buf);
1027 
1028 	if (ret && ret != BOOTM_ERR_UNIMPLEMENTED)
1029 		return ret;
1030 
1031 	return 0;
1032 }
1033 
1034 int bootm_host_load_images(const void *fit, int cfg_noffset, int is_spl)
1035 {
1036 	static uint8_t image_types[] = {
1037 		IH_TYPE_KERNEL,
1038 		IH_TYPE_FLATDT,
1039 		IH_TYPE_RAMDISK,
1040 	};
1041 #ifdef CONFIG_SPL_ATF
1042 	static uint8_t image_types_spl[] = {
1043 		IH_TYPE_FLATDT,
1044 		IH_TYPE_FIRMWARE,
1045 		IH_TYPE_LOADABLE,
1046 		IH_TYPE_LOADABLE,
1047 		IH_TYPE_LOADABLE,
1048 	};
1049 #else
1050 	static uint8_t image_types_spl[] = {
1051 		IH_TYPE_FLATDT,
1052 		IH_TYPE_FIRMWARE,
1053 		IH_TYPE_LOADABLE,
1054 	};
1055 #endif
1056 	int loadable_index = 0;
1057 	int err = 0;
1058 	int index;
1059 	int i;
1060 
1061 	for (i = 0; !is_spl && i < ARRAY_SIZE(image_types); i++) {
1062 		int ret;
1063 
1064 		ret = bootm_host_load_image(fit, image_types[i], 0);
1065 		if (!err && ret && ret != -ENOENT)
1066 			err = ret;
1067 	}
1068 
1069 	for (i = 0; is_spl && i < ARRAY_SIZE(image_types_spl); i++) {
1070 		int ret;
1071 
1072 		if (image_types_spl[i] == IH_TYPE_LOADABLE)
1073 			index = loadable_index++;
1074 		else
1075 			index = 0;
1076 
1077 		ret = bootm_host_load_image(fit, image_types_spl[i], index);
1078 		if (!err && ret && ret != -ENOENT)
1079 			err = ret;
1080 	}
1081 
1082 	/* Return the first error we found */
1083 	return err;
1084 }
1085 
1086 #endif /* ndef USE_HOSTCC */
1087