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