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