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