xref: /rk3399_rockchip-uboot/common/image.c (revision e1aab64013804e120d3fc5d6940ed7245d8f9c2a)
1 /*
2  * (C) Copyright 2008 Semihalf
3  *
4  * (C) Copyright 2000-2006
5  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #ifndef USE_HOSTCC
11 #include <common.h>
12 #include <watchdog.h>
13 
14 #ifdef CONFIG_SHOW_BOOT_PROGRESS
15 #include <status_led.h>
16 #endif
17 
18 #include <rtc.h>
19 
20 #include <environment.h>
21 #include <image.h>
22 #include <mapmem.h>
23 
24 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
25 #include <linux/libfdt.h>
26 #include <fdt_support.h>
27 #include <fpga.h>
28 #include <xilinx.h>
29 #endif
30 
31 #include <u-boot/md5.h>
32 #include <u-boot/sha1.h>
33 #include <linux/errno.h>
34 #include <asm/io.h>
35 
36 #ifdef CONFIG_CMD_BDI
37 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
38 #endif
39 
40 DECLARE_GLOBAL_DATA_PTR;
41 
42 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
43 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
44 						int verify);
45 #endif
46 #else
47 #include "mkimage.h"
48 #include <u-boot/md5.h>
49 #include <time.h>
50 #include <image.h>
51 
52 #ifndef __maybe_unused
53 # define __maybe_unused		/* unimplemented */
54 #endif
55 #endif /* !USE_HOSTCC*/
56 
57 #include <u-boot/crc.h>
58 
59 #ifndef CONFIG_SYS_BARGSIZE
60 #define CONFIG_SYS_BARGSIZE 512
61 #endif
62 
63 static const table_entry_t uimage_arch[] = {
64 	{	IH_ARCH_INVALID,	"invalid",	"Invalid ARCH",	},
65 	{	IH_ARCH_ALPHA,		"alpha",	"Alpha",	},
66 	{	IH_ARCH_ARM,		"arm",		"ARM",		},
67 	{	IH_ARCH_I386,		"x86",		"Intel x86",	},
68 	{	IH_ARCH_IA64,		"ia64",		"IA64",		},
69 	{	IH_ARCH_M68K,		"m68k",		"M68K",		},
70 	{	IH_ARCH_MICROBLAZE,	"microblaze",	"MicroBlaze",	},
71 	{	IH_ARCH_MIPS,		"mips",		"MIPS",		},
72 	{	IH_ARCH_MIPS64,		"mips64",	"MIPS 64 Bit",	},
73 	{	IH_ARCH_NIOS2,		"nios2",	"NIOS II",	},
74 	{	IH_ARCH_PPC,		"powerpc",	"PowerPC",	},
75 	{	IH_ARCH_PPC,		"ppc",		"PowerPC",	},
76 	{	IH_ARCH_S390,		"s390",		"IBM S390",	},
77 	{	IH_ARCH_SH,		"sh",		"SuperH",	},
78 	{	IH_ARCH_SPARC,		"sparc",	"SPARC",	},
79 	{	IH_ARCH_SPARC64,	"sparc64",	"SPARC 64 Bit",	},
80 	{	IH_ARCH_BLACKFIN,	"blackfin",	"Blackfin",	},
81 	{	IH_ARCH_AVR32,		"avr32",	"AVR32",	},
82 	{	IH_ARCH_NDS32,		"nds32",	"NDS32",	},
83 	{	IH_ARCH_OPENRISC,	"or1k",		"OpenRISC 1000",},
84 	{	IH_ARCH_SANDBOX,	"sandbox",	"Sandbox",	},
85 	{	IH_ARCH_ARM64,		"arm64",	"AArch64",	},
86 	{	IH_ARCH_ARC,		"arc",		"ARC",		},
87 	{	IH_ARCH_X86_64,		"x86_64",	"AMD x86_64",	},
88 	{	IH_ARCH_XTENSA,		"xtensa",	"Xtensa",	},
89 	{	-1,			"",		"",		},
90 };
91 
92 static const table_entry_t uimage_os[] = {
93 	{	IH_OS_INVALID,	"invalid",	"Invalid OS",		},
94 	{       IH_OS_OP_TEE, "op-tee", "OP-TEE"  },
95 	{       IH_OS_ARM_TRUSTED_FIRMWARE, "arm-trusted-firmware", "ARM Trusted Firmware"  },
96 	{	IH_OS_LINUX,	"linux",	"Linux",		},
97 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
98 	{	IH_OS_LYNXOS,	"lynxos",	"LynxOS",		},
99 #endif
100 	{	IH_OS_NETBSD,	"netbsd",	"NetBSD",		},
101 	{	IH_OS_OSE,	"ose",		"Enea OSE",		},
102 	{	IH_OS_PLAN9,	"plan9",	"Plan 9",		},
103 	{	IH_OS_RTEMS,	"rtems",	"RTEMS",		},
104 	{	IH_OS_U_BOOT,	"u-boot",	"U-Boot",		},
105 	{	IH_OS_VXWORKS,	"vxworks",	"VxWorks",		},
106 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
107 	{	IH_OS_QNX,	"qnx",		"QNX",			},
108 #endif
109 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
110 	{	IH_OS_INTEGRITY,"integrity",	"INTEGRITY",		},
111 #endif
112 #ifdef USE_HOSTCC
113 	{	IH_OS_4_4BSD,	"4_4bsd",	"4_4BSD",		},
114 	{	IH_OS_DELL,	"dell",		"Dell",			},
115 	{	IH_OS_ESIX,	"esix",		"Esix",			},
116 	{	IH_OS_FREEBSD,	"freebsd",	"FreeBSD",		},
117 	{	IH_OS_IRIX,	"irix",		"Irix",			},
118 	{	IH_OS_NCR,	"ncr",		"NCR",			},
119 	{	IH_OS_OPENBSD,	"openbsd",	"OpenBSD",		},
120 	{	IH_OS_PSOS,	"psos",		"pSOS",			},
121 	{	IH_OS_SCO,	"sco",		"SCO",			},
122 	{	IH_OS_SOLARIS,	"solaris",	"Solaris",		},
123 	{	IH_OS_SVR4,	"svr4",		"SVR4",			},
124 #endif
125 #if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
126 	{	IH_OS_OPENRTOS,	"openrtos",	"OpenRTOS",		},
127 #endif
128 
129 	{	-1,		"",		"",			},
130 };
131 
132 static const table_entry_t uimage_type[] = {
133 	{	IH_TYPE_AISIMAGE,   "aisimage",   "Davinci AIS image",},
134 	{	IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image",	},
135 	{	IH_TYPE_FIRMWARE,   "firmware",	  "Firmware",		},
136 	{	IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",	},
137 	{	IH_TYPE_GPIMAGE,    "gpimage",    "TI Keystone SPL Image",},
138 	{	IH_TYPE_KERNEL,	    "kernel",	  "Kernel Image",	},
139 	{	IH_TYPE_KERNEL_NOLOAD, "kernel_noload",  "Kernel Image (no loading done)", },
140 	{	IH_TYPE_KWBIMAGE,   "kwbimage",   "Kirkwood Boot Image",},
141 	{	IH_TYPE_IMXIMAGE,   "imximage",   "Freescale i.MX Boot Image",},
142 	{	IH_TYPE_INVALID,    "invalid",	  "Invalid Image",	},
143 	{	IH_TYPE_MULTI,	    "multi",	  "Multi-File Image",	},
144 	{	IH_TYPE_OMAPIMAGE,  "omapimage",  "TI OMAP SPL With GP CH",},
145 	{	IH_TYPE_PBLIMAGE,   "pblimage",   "Freescale PBL Boot Image",},
146 	{	IH_TYPE_RAMDISK,    "ramdisk",	  "RAMDisk Image",	},
147 	{	IH_TYPE_SCRIPT,     "script",	  "Script",		},
148 	{	IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SOCFPGA preloader",},
149 	{	IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
150 	{	IH_TYPE_UBLIMAGE,   "ublimage",   "Davinci UBL image",},
151 	{	IH_TYPE_MXSIMAGE,   "mxsimage",   "Freescale MXS Boot Image",},
152 	{	IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
153 	{	IH_TYPE_X86_SETUP,  "x86_setup",  "x86 setup.bin",    },
154 	{	IH_TYPE_LPC32XXIMAGE, "lpc32xximage",  "LPC32XX Boot Image", },
155 	{	IH_TYPE_RKIMAGE,    "rkimage",    "Rockchip Boot Image" },
156 	{	IH_TYPE_RKSD,       "rksd",       "Rockchip SD Boot Image" },
157 	{	IH_TYPE_RKSPI,      "rkspi",      "Rockchip SPI Boot Image" },
158 	{	IH_TYPE_VYBRIDIMAGE, "vybridimage",  "Vybrid Boot Image", },
159 	{	IH_TYPE_ZYNQIMAGE,  "zynqimage",  "Xilinx Zynq Boot Image" },
160 	{	IH_TYPE_ZYNQMPIMAGE, "zynqmpimage", "Xilinx ZynqMP Boot Image" },
161 	{	IH_TYPE_FPGA,       "fpga",       "FPGA Image" },
162 	{       IH_TYPE_TEE,        "tee",        "Trusted Execution Environment Image",},
163 	{	IH_TYPE_FIRMWARE_IVT, "firmware_ivt", "Firmware with HABv4 IVT" },
164 	{       IH_TYPE_PMMC,        "pmmc",        "TI Power Management Micro-Controller Firmware",},
165 	{	IH_TYPE_RKNAND,     "rknand",     "Rockchip NAND Boot Image" },
166 	{	-1,		    "",		  "",			},
167 };
168 
169 static const table_entry_t uimage_comp[] = {
170 	{	IH_COMP_NONE,	"none",		"uncompressed",		},
171 	{	IH_COMP_BZIP2,	"bzip2",	"bzip2 compressed",	},
172 	{	IH_COMP_GZIP,	"gzip",		"gzip compressed",	},
173 	{	IH_COMP_LZMA,	"lzma",		"lzma compressed",	},
174 	{	IH_COMP_LZO,	"lzo",		"lzo compressed",	},
175 	{	IH_COMP_LZ4,	"lz4",		"lz4 compressed",	},
176 	{	-1,		"",		"",			},
177 };
178 
179 struct table_info {
180 	const char *desc;
181 	int count;
182 	const table_entry_t *table;
183 };
184 
185 static const struct table_info table_info[IH_COUNT] = {
186 	{ "architecture", IH_ARCH_COUNT, uimage_arch },
187 	{ "compression", IH_COMP_COUNT, uimage_comp },
188 	{ "operating system", IH_OS_COUNT, uimage_os },
189 	{ "image type", IH_TYPE_COUNT, uimage_type },
190 };
191 
192 /*****************************************************************************/
193 /* Legacy format routines */
194 /*****************************************************************************/
195 #ifndef USE_HOSTCC
196 #ifndef CONFIG_SPL_BUILD
197 uint32_t image_get_load(const image_header_t *hdr)
198 {
199 	uint32_t load = uimage_to_cpu(hdr->ih_load);
200 
201 	return (load == IMAGE_PARAM_INVAL) ?
202 		env_get_ulong("kernel_addr_r", 16, 0) : load;
203 }
204 
205 uint32_t image_get_ep(const image_header_t *hdr)
206 {
207 	uint32_t ep = uimage_to_cpu(hdr->ih_ep);
208 
209 	return (ep == IMAGE_PARAM_INVAL) ?
210 		env_get_ulong("kernel_addr_r", 16, 0) : ep;
211 }
212 #endif
213 #endif
214 
215 int image_check_hcrc(const image_header_t *hdr)
216 {
217 	ulong hcrc;
218 	ulong len = image_get_header_size();
219 	image_header_t header;
220 
221 	/* Copy header so we can blank CRC field for re-calculation */
222 	memmove(&header, (char *)hdr, image_get_header_size());
223 	image_set_hcrc(&header, 0);
224 
225 	hcrc = crc32(0, (unsigned char *)&header, len);
226 
227 	return (hcrc == image_get_hcrc(hdr));
228 }
229 
230 int image_check_dcrc(const image_header_t *hdr)
231 {
232 	ulong data = image_get_data(hdr);
233 	ulong len = image_get_data_size(hdr);
234 	ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
235 
236 	return (dcrc == image_get_dcrc(hdr));
237 }
238 
239 /**
240  * image_multi_count - get component (sub-image) count
241  * @hdr: pointer to the header of the multi component image
242  *
243  * image_multi_count() returns number of components in a multi
244  * component image.
245  *
246  * Note: no checking of the image type is done, caller must pass
247  * a valid multi component image.
248  *
249  * returns:
250  *     number of components
251  */
252 ulong image_multi_count(const image_header_t *hdr)
253 {
254 	ulong i, count = 0;
255 	uint32_t *size;
256 
257 	/* get start of the image payload, which in case of multi
258 	 * component images that points to a table of component sizes */
259 	size = (uint32_t *)image_get_data(hdr);
260 
261 	/* count non empty slots */
262 	for (i = 0; size[i] != IMAGE_PARAM_INVAL; ++i)
263 		count++;
264 
265 	return count;
266 }
267 
268 /**
269  * image_multi_getimg - get component data address and size
270  * @hdr: pointer to the header of the multi component image
271  * @idx: index of the requested component
272  * @data: pointer to a ulong variable, will hold component data address
273  * @len: pointer to a ulong variable, will hold component size
274  *
275  * image_multi_getimg() returns size and data address for the requested
276  * component in a multi component image.
277  *
278  * Note: no checking of the image type is done, caller must pass
279  * a valid multi component image.
280  *
281  * returns:
282  *     data address and size of the component, if idx is valid
283  *     0 in data and len, if idx is out of range
284  */
285 void image_multi_getimg(const image_header_t *hdr, ulong idx,
286 			ulong *data, ulong *len)
287 {
288 	int i;
289 	uint32_t *size;
290 	ulong offset, count, img_data;
291 
292 	/* get number of component */
293 	count = image_multi_count(hdr);
294 
295 	/* get start of the image payload, which in case of multi
296 	 * component images that points to a table of component sizes */
297 	size = (uint32_t *)image_get_data(hdr);
298 
299 	/* get address of the proper component data start, which means
300 	 * skipping sizes table (add 1 for last, null entry) */
301 	img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
302 
303 	if (idx < count) {
304 		*len = uimage_to_cpu(size[idx]);
305 		offset = 0;
306 
307 		/* go over all indices preceding requested component idx */
308 		for (i = 0; i < idx; i++) {
309 			/* add up i-th component size, rounding up to 4 bytes */
310 			offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
311 		}
312 
313 		/* calculate idx-th component data address */
314 		*data = img_data + offset;
315 	} else {
316 		*len = 0;
317 		*data = 0;
318 	}
319 }
320 
321 static void image_print_type(const image_header_t *hdr)
322 {
323 	const char __maybe_unused *os, *arch, *type, *comp;
324 
325 	os = genimg_get_os_name(image_get_os(hdr));
326 	arch = genimg_get_arch_name(image_get_arch(hdr));
327 	type = genimg_get_type_name(image_get_type(hdr));
328 	comp = genimg_get_comp_name(image_get_comp(hdr));
329 
330 	printf("%s %s %s (%s)\n", arch, os, type, comp);
331 }
332 
333 /**
334  * image_print_contents - prints out the contents of the legacy format image
335  * @ptr: pointer to the legacy format image header
336  * @p: pointer to prefix string
337  *
338  * image_print_contents() formats a multi line legacy image contents description.
339  * The routine prints out all header fields followed by the size/offset data
340  * for MULTI/SCRIPT images.
341  *
342  * returns:
343  *     no returned results
344  */
345 void image_print_contents(const void *ptr)
346 {
347 	const image_header_t *hdr = (const image_header_t *)ptr;
348 	const char __maybe_unused *p;
349 
350 	p = IMAGE_INDENT_STRING;
351 	printf("%sImage Name:   %.*s\n", p, IH_NMLEN, image_get_name(hdr));
352 	if (IMAGE_ENABLE_TIMESTAMP) {
353 		printf("%sCreated:      ", p);
354 		genimg_print_time((time_t)image_get_time(hdr));
355 	}
356 	printf("%sImage Type:   ", p);
357 	image_print_type(hdr);
358 	printf("%sData Size:    ", p);
359 	genimg_print_size(image_get_data_size(hdr));
360 	printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
361 	printf("%sEntry Point:  %08x\n", p, image_get_ep(hdr));
362 
363 	if (image_check_type(hdr, IH_TYPE_MULTI) ||
364 			image_check_type(hdr, IH_TYPE_SCRIPT)) {
365 		int i;
366 		ulong data, len;
367 		ulong count = image_multi_count(hdr);
368 
369 		printf("%sContents:\n", p);
370 		for (i = 0; i < count; i++) {
371 			image_multi_getimg(hdr, i, &data, &len);
372 
373 			printf("%s   Image %d: ", p, i);
374 			genimg_print_size(len);
375 
376 			if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
377 				/*
378 				 * the user may need to know offsets
379 				 * if planning to do something with
380 				 * multiple files
381 				 */
382 				printf("%s    Offset = 0x%08lx\n", p, data);
383 			}
384 		}
385 	} else if (image_check_type(hdr, IH_TYPE_FIRMWARE_IVT)) {
386 		printf("HAB Blocks:   0x%08x   0x0000   0x%08x\n",
387 				image_get_load(hdr) - image_get_header_size(),
388 				image_get_size(hdr) + image_get_header_size()
389 						- 0x1FE0);
390 	}
391 }
392 
393 
394 #ifndef USE_HOSTCC
395 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
396 /**
397  * image_get_ramdisk - get and verify ramdisk image
398  * @rd_addr: ramdisk image start address
399  * @arch: expected ramdisk architecture
400  * @verify: checksum verification flag
401  *
402  * image_get_ramdisk() returns a pointer to the verified ramdisk image
403  * header. Routine receives image start address and expected architecture
404  * flag. Verification done covers data and header integrity and os/type/arch
405  * fields checking.
406  *
407  * returns:
408  *     pointer to a ramdisk image header, if image was found and valid
409  *     otherwise, return NULL
410  */
411 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
412 						int verify)
413 {
414 	const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
415 
416 	if (!image_check_magic(rd_hdr)) {
417 		puts("Bad Magic Number\n");
418 		bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
419 		return NULL;
420 	}
421 
422 	if (!image_check_hcrc(rd_hdr)) {
423 		puts("Bad Header Checksum\n");
424 		bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
425 		return NULL;
426 	}
427 
428 	bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
429 	image_print_contents(rd_hdr);
430 
431 	if (verify) {
432 		puts("   Verifying Checksum ... ");
433 		if (!image_check_dcrc(rd_hdr)) {
434 			puts("Bad Data CRC\n");
435 			bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
436 			return NULL;
437 		}
438 		puts("OK\n");
439 	}
440 
441 	bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
442 
443 	if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
444 	    !image_check_arch(rd_hdr, arch) ||
445 	    !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
446 		printf("No Linux %s Ramdisk Image\n",
447 				genimg_get_arch_name(arch));
448 		bootstage_error(BOOTSTAGE_ID_RAMDISK);
449 		return NULL;
450 	}
451 
452 	return rd_hdr;
453 }
454 #endif
455 #endif /* !USE_HOSTCC */
456 
457 /*****************************************************************************/
458 /* Shared dual-format routines */
459 /*****************************************************************************/
460 #ifndef USE_HOSTCC
461 ulong load_addr = CONFIG_SYS_LOAD_ADDR;	/* Default Load Address */
462 ulong save_addr;			/* Default Save Address */
463 ulong save_size;			/* Default Save Size (in bytes) */
464 
465 static int on_loadaddr(const char *name, const char *value, enum env_op op,
466 	int flags)
467 {
468 	switch (op) {
469 	case env_op_create:
470 	case env_op_overwrite:
471 		load_addr = simple_strtoul(value, NULL, 16);
472 		break;
473 	default:
474 		break;
475 	}
476 
477 	return 0;
478 }
479 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
480 
481 ulong env_get_bootm_low(void)
482 {
483 	char *s = env_get("bootm_low");
484 	if (s) {
485 		ulong tmp = simple_strtoul(s, NULL, 16);
486 		return tmp;
487 	}
488 
489 #if defined(CONFIG_SYS_SDRAM_BASE)
490 	return CONFIG_SYS_SDRAM_BASE;
491 #elif defined(CONFIG_ARM)
492 	return gd->bd->bi_dram[0].start;
493 #else
494 	return 0;
495 #endif
496 }
497 
498 phys_size_t env_get_bootm_size(void)
499 {
500 	phys_size_t tmp, size;
501 	phys_addr_t start;
502 	char *s = env_get("bootm_size");
503 	if (s) {
504 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
505 		return tmp;
506 	}
507 
508 #if defined(CONFIG_ARM) && defined(CONFIG_NR_DRAM_BANKS)
509 	start = gd->bd->bi_dram[0].start;
510 	size = gd->bd->bi_dram[0].size;
511 #else
512 	start = gd->bd->bi_memstart;
513 	size = gd->bd->bi_memsize;
514 #endif
515 
516 	s = env_get("bootm_low");
517 	if (s)
518 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
519 	else
520 		tmp = start;
521 
522 	return size - (tmp - start);
523 }
524 
525 phys_size_t env_get_bootm_mapsize(void)
526 {
527 	phys_size_t tmp;
528 	char *s = env_get("bootm_mapsize");
529 	if (s) {
530 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
531 		return tmp;
532 	}
533 
534 #if defined(CONFIG_SYS_BOOTMAPSZ)
535 	return CONFIG_SYS_BOOTMAPSZ;
536 #else
537 	return env_get_bootm_size();
538 #endif
539 }
540 
541 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
542 {
543 	if (to == from)
544 		return;
545 
546 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
547 	if (to > from) {
548 		from += len;
549 		to += len;
550 	}
551 	while (len > 0) {
552 		size_t tail = (len > chunksz) ? chunksz : len;
553 		WATCHDOG_RESET();
554 		if (to > from) {
555 			to -= tail;
556 			from -= tail;
557 		}
558 		memmove(to, from, tail);
559 		if (to < from) {
560 			to += tail;
561 			from += tail;
562 		}
563 		len -= tail;
564 	}
565 #else	/* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
566 	memmove(to, from, len);
567 #endif	/* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
568 }
569 #endif /* !USE_HOSTCC */
570 
571 void genimg_print_size(uint32_t size)
572 {
573 #ifndef USE_HOSTCC
574 	printf("%d Bytes = ", size);
575 	print_size(size, "\n");
576 #else
577 	printf("%d Bytes = %.2f KiB = %.2f MiB\n",
578 			size, (double)size / 1.024e3,
579 			(double)size / 1.048576e6);
580 #endif
581 }
582 
583 #if IMAGE_ENABLE_TIMESTAMP
584 void genimg_print_time(time_t timestamp)
585 {
586 #ifndef USE_HOSTCC
587 	struct rtc_time tm;
588 
589 	rtc_to_tm(timestamp, &tm);
590 	printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
591 			tm.tm_year, tm.tm_mon, tm.tm_mday,
592 			tm.tm_hour, tm.tm_min, tm.tm_sec);
593 #else
594 	printf("%s", ctime(&timestamp));
595 #endif
596 }
597 #endif
598 
599 const table_entry_t *get_table_entry(const table_entry_t *table, int id)
600 {
601 	for (; table->id >= 0; ++table) {
602 		if (table->id == id)
603 			return table;
604 	}
605 	return NULL;
606 }
607 
608 static const char *unknown_msg(enum ih_category category)
609 {
610 	static const char unknown_str[] = "Unknown ";
611 	static char msg[30];
612 
613 	strcpy(msg, unknown_str);
614 	strncat(msg, table_info[category].desc,
615 		sizeof(msg) - sizeof(unknown_str));
616 
617 	return msg;
618 }
619 
620 /**
621  * get_cat_table_entry_name - translate entry id to long name
622  * @category: category to look up (enum ih_category)
623  * @id: entry id to be translated
624  *
625  * This will scan the translation table trying to find the entry that matches
626  * the given id.
627  *
628  * @retur long entry name if translation succeeds; error string on failure
629  */
630 const char *genimg_get_cat_name(enum ih_category category, uint id)
631 {
632 	const table_entry_t *entry;
633 
634 	entry = get_table_entry(table_info[category].table, id);
635 	if (!entry)
636 		return unknown_msg(category);
637 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
638 	return entry->lname;
639 #else
640 	return entry->lname + gd->reloc_off;
641 #endif
642 }
643 
644 /**
645  * get_cat_table_entry_short_name - translate entry id to short name
646  * @category: category to look up (enum ih_category)
647  * @id: entry id to be translated
648  *
649  * This will scan the translation table trying to find the entry that matches
650  * the given id.
651  *
652  * @retur short entry name if translation succeeds; error string on failure
653  */
654 const char *genimg_get_cat_short_name(enum ih_category category, uint id)
655 {
656 	const table_entry_t *entry;
657 
658 	entry = get_table_entry(table_info[category].table, id);
659 	if (!entry)
660 		return unknown_msg(category);
661 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
662 	return entry->sname;
663 #else
664 	return entry->sname + gd->reloc_off;
665 #endif
666 }
667 
668 int genimg_get_cat_count(enum ih_category category)
669 {
670 	return table_info[category].count;
671 }
672 
673 const char *genimg_get_cat_desc(enum ih_category category)
674 {
675 	return table_info[category].desc;
676 }
677 
678 /**
679  * get_table_entry_name - translate entry id to long name
680  * @table: pointer to a translation table for entries of a specific type
681  * @msg: message to be returned when translation fails
682  * @id: entry id to be translated
683  *
684  * get_table_entry_name() will go over translation table trying to find
685  * entry that matches given id. If matching entry is found, its long
686  * name is returned to the caller.
687  *
688  * returns:
689  *     long entry name if translation succeeds
690  *     msg otherwise
691  */
692 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
693 {
694 	table = get_table_entry(table, id);
695 	if (!table)
696 		return msg;
697 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
698 	return table->lname;
699 #else
700 	return table->lname + gd->reloc_off;
701 #endif
702 }
703 
704 const char *genimg_get_os_name(uint8_t os)
705 {
706 	return (get_table_entry_name(uimage_os, "Unknown OS", os));
707 }
708 
709 const char *genimg_get_arch_name(uint8_t arch)
710 {
711 	return (get_table_entry_name(uimage_arch, "Unknown Architecture",
712 					arch));
713 }
714 
715 const char *genimg_get_type_name(uint8_t type)
716 {
717 	return (get_table_entry_name(uimage_type, "Unknown Image", type));
718 }
719 
720 static const char *genimg_get_short_name(const table_entry_t *table, int val)
721 {
722 	table = get_table_entry(table, val);
723 	if (!table)
724 		return "unknown";
725 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
726 	return table->sname;
727 #else
728 	return table->sname + gd->reloc_off;
729 #endif
730 }
731 
732 const char *genimg_get_type_short_name(uint8_t type)
733 {
734 	return genimg_get_short_name(uimage_type, type);
735 }
736 
737 const char *genimg_get_comp_name(uint8_t comp)
738 {
739 	return (get_table_entry_name(uimage_comp, "Unknown Compression",
740 					comp));
741 }
742 
743 const char *genimg_get_comp_short_name(uint8_t comp)
744 {
745 	return genimg_get_short_name(uimage_comp, comp);
746 }
747 
748 const char *genimg_get_os_short_name(uint8_t os)
749 {
750 	return genimg_get_short_name(uimage_os, os);
751 }
752 
753 const char *genimg_get_arch_short_name(uint8_t arch)
754 {
755 	return genimg_get_short_name(uimage_arch, arch);
756 }
757 
758 /**
759  * get_table_entry_id - translate short entry name to id
760  * @table: pointer to a translation table for entries of a specific type
761  * @table_name: to be used in case of error
762  * @name: entry short name to be translated
763  *
764  * get_table_entry_id() will go over translation table trying to find
765  * entry that matches given short name. If matching entry is found,
766  * its id returned to the caller.
767  *
768  * returns:
769  *     entry id if translation succeeds
770  *     -1 otherwise
771  */
772 int get_table_entry_id(const table_entry_t *table,
773 		const char *table_name, const char *name)
774 {
775 	const table_entry_t *t;
776 
777 	for (t = table; t->id >= 0; ++t) {
778 #ifdef CONFIG_NEEDS_MANUAL_RELOC
779 		if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
780 #else
781 		if (t->sname && strcasecmp(t->sname, name) == 0)
782 #endif
783 			return (t->id);
784 	}
785 	debug("Invalid %s Type: %s\n", table_name, name);
786 
787 	return -1;
788 }
789 
790 int genimg_get_os_id(const char *name)
791 {
792 	return (get_table_entry_id(uimage_os, "OS", name));
793 }
794 
795 int genimg_get_arch_id(const char *name)
796 {
797 	return (get_table_entry_id(uimage_arch, "CPU", name));
798 }
799 
800 int genimg_get_type_id(const char *name)
801 {
802 	return (get_table_entry_id(uimage_type, "Image", name));
803 }
804 
805 int genimg_get_comp_id(const char *name)
806 {
807 	return (get_table_entry_id(uimage_comp, "Compression", name));
808 }
809 
810 #ifndef USE_HOSTCC
811 /**
812  * genimg_get_kernel_addr_fit - get the real kernel address and return 2
813  *                              FIT strings
814  * @img_addr: a string might contain real image address
815  * @fit_uname_config: double pointer to a char, will hold pointer to a
816  *                    configuration unit name
817  * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
818  *                    name
819  *
820  * genimg_get_kernel_addr_fit get the real kernel start address from a string
821  * which is normally the first argv of bootm/bootz
822  *
823  * returns:
824  *     kernel start address
825  */
826 ulong genimg_get_kernel_addr_fit(char * const img_addr,
827 			     const char **fit_uname_config,
828 			     const char **fit_uname_kernel)
829 {
830 	ulong kernel_addr;
831 
832 	/* find out kernel image address */
833 	if (!img_addr) {
834 		kernel_addr = load_addr;
835 		debug("*  kernel: default image load address = 0x%08lx\n",
836 		      load_addr);
837 #if CONFIG_IS_ENABLED(FIT)
838 	} else if (fit_parse_conf(img_addr, load_addr, &kernel_addr,
839 				  fit_uname_config)) {
840 		debug("*  kernel: config '%s' from image at 0x%08lx\n",
841 		      *fit_uname_config, kernel_addr);
842 	} else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
843 				     fit_uname_kernel)) {
844 		debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
845 		      *fit_uname_kernel, kernel_addr);
846 #endif
847 	} else {
848 		kernel_addr = simple_strtoul(img_addr, NULL, 16);
849 		debug("*  kernel: cmdline image address = 0x%08lx\n",
850 		      kernel_addr);
851 	}
852 
853 	return kernel_addr;
854 }
855 
856 /**
857  * genimg_get_kernel_addr() is the simple version of
858  * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
859  */
860 ulong genimg_get_kernel_addr(char * const img_addr)
861 {
862 	const char *fit_uname_config = NULL;
863 	const char *fit_uname_kernel = NULL;
864 
865 	return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
866 					  &fit_uname_kernel);
867 }
868 
869 /**
870  * genimg_get_format - get image format type
871  * @img_addr: image start address
872  *
873  * genimg_get_format() checks whether provided address points to a valid
874  * legacy or FIT image.
875  *
876  * New uImage format and FDT blob are based on a libfdt. FDT blob
877  * may be passed directly or embedded in a FIT image. In both situations
878  * genimg_get_format() must be able to dectect libfdt header.
879  *
880  * returns:
881  *     image format type or IMAGE_FORMAT_INVALID if no image is present
882  */
883 int genimg_get_format(const void *img_addr)
884 {
885 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
886 	const image_header_t *hdr;
887 
888 	hdr = (const image_header_t *)img_addr;
889 	if (image_check_magic(hdr))
890 		return IMAGE_FORMAT_LEGACY;
891 #endif
892 #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
893 	if (fdt_check_header(img_addr) == 0)
894 		return IMAGE_FORMAT_FIT;
895 #endif
896 #ifdef CONFIG_ANDROID_BOOT_IMAGE
897 	if (android_image_check_header(img_addr) == 0)
898 		return IMAGE_FORMAT_ANDROID;
899 #endif
900 
901 	return IMAGE_FORMAT_INVALID;
902 }
903 
904 /**
905  * fit_has_config - check if there is a valid FIT configuration
906  * @images: pointer to the bootm command headers structure
907  *
908  * fit_has_config() checks if there is a FIT configuration in use
909  * (if FTI support is present).
910  *
911  * returns:
912  *     0, no FIT support or no configuration found
913  *     1, configuration found
914  */
915 int genimg_has_config(bootm_headers_t *images)
916 {
917 #if IMAGE_ENABLE_FIT
918 	if (images->fit_uname_cfg)
919 		return 1;
920 #endif
921 	return 0;
922 }
923 
924 /**
925  * boot_get_ramdisk - main ramdisk handling routine
926  * @argc: command argument count
927  * @argv: command argument list
928  * @images: pointer to the bootm images structure
929  * @arch: expected ramdisk architecture
930  * @rd_start: pointer to a ulong variable, will hold ramdisk start address
931  * @rd_end: pointer to a ulong variable, will hold ramdisk end
932  *
933  * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
934  * Curently supported are the following ramdisk sources:
935  *      - multicomponent kernel/ramdisk image,
936  *      - commandline provided address of decicated ramdisk image.
937  *
938  * returns:
939  *     0, if ramdisk image was found and valid, or skiped
940  *     rd_start and rd_end are set to ramdisk start/end addresses if
941  *     ramdisk image is found and valid
942  *
943  *     1, if ramdisk image is found but corrupted, or invalid
944  *     rd_start and rd_end are set to 0 if no ramdisk exists
945  */
946 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
947 		uint8_t arch, ulong *rd_start, ulong *rd_end)
948 {
949 	ulong rd_addr, rd_load;
950 	ulong rd_data, rd_len;
951 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
952 	const image_header_t *rd_hdr;
953 #endif
954 	void *buf;
955 #ifdef CONFIG_SUPPORT_RAW_INITRD
956 	char *end;
957 #endif
958 #if IMAGE_ENABLE_FIT
959 	const char	*fit_uname_config = images->fit_uname_cfg;
960 	const char	*fit_uname_ramdisk = NULL;
961 	ulong		default_addr;
962 	int		rd_noffset;
963 #endif
964 	const char *select = NULL;
965 
966 	*rd_start = 0;
967 	*rd_end = 0;
968 
969 #ifdef CONFIG_ANDROID_BOOT_IMAGE
970 	/*
971 	 * Look for an Android boot image.
972 	 */
973 	buf = map_sysmem(images->os.start, 0);
974 	if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
975 		select = argv[0];
976 #endif
977 
978 	if (argc >= 2)
979 		select = argv[1];
980 
981 	/*
982 	 * Look for a '-' which indicates to ignore the
983 	 * ramdisk argument
984 	 */
985 	if (select && strcmp(select, "-") ==  0) {
986 		debug("## Skipping init Ramdisk\n");
987 		rd_len = rd_data = 0;
988 	} else if (select || genimg_has_config(images)) {
989 #if IMAGE_ENABLE_FIT
990 		if (select) {
991 			/*
992 			 * If the init ramdisk comes from the FIT image and
993 			 * the FIT image address is omitted in the command
994 			 * line argument, try to use os FIT image address or
995 			 * default load address.
996 			 */
997 			if (images->fit_uname_os)
998 				default_addr = (ulong)images->fit_hdr_os;
999 			else
1000 				default_addr = load_addr;
1001 
1002 			if (fit_parse_conf(select, default_addr,
1003 					   &rd_addr, &fit_uname_config)) {
1004 				debug("*  ramdisk: config '%s' from image at "
1005 						"0x%08lx\n",
1006 						fit_uname_config, rd_addr);
1007 			} else if (fit_parse_subimage(select, default_addr,
1008 						&rd_addr, &fit_uname_ramdisk)) {
1009 				debug("*  ramdisk: subimage '%s' from image at "
1010 						"0x%08lx\n",
1011 						fit_uname_ramdisk, rd_addr);
1012 			} else
1013 #endif
1014 			{
1015 				rd_addr = simple_strtoul(select, NULL, 16);
1016 				debug("*  ramdisk: cmdline image address = "
1017 						"0x%08lx\n",
1018 						rd_addr);
1019 			}
1020 #if IMAGE_ENABLE_FIT
1021 		} else {
1022 			/* use FIT configuration provided in first bootm
1023 			 * command argument. If the property is not defined,
1024 			 * quit silently.
1025 			 */
1026 			rd_addr = map_to_sysmem(images->fit_hdr_os);
1027 			rd_noffset = fit_get_node_from_config(images,
1028 					FIT_RAMDISK_PROP, rd_addr);
1029 			if (rd_noffset == -ENOENT)
1030 				return 0;
1031 			else if (rd_noffset < 0)
1032 				return 1;
1033 		}
1034 #endif
1035 
1036 		/*
1037 		 * Check if there is an initrd image at the
1038 		 * address provided in the second bootm argument
1039 		 * check image type, for FIT images get FIT node.
1040 		 */
1041 		buf = map_sysmem(rd_addr, 0);
1042 		switch (genimg_get_format(buf)) {
1043 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
1044 		case IMAGE_FORMAT_LEGACY:
1045 			printf("## Loading init Ramdisk from Legacy "
1046 					"Image at %08lx ...\n", rd_addr);
1047 
1048 			bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
1049 			rd_hdr = image_get_ramdisk(rd_addr, arch,
1050 							images->verify);
1051 
1052 			if (rd_hdr == NULL)
1053 				return 1;
1054 
1055 			rd_data = image_get_data(rd_hdr);
1056 			rd_len = image_get_data_size(rd_hdr);
1057 			rd_load = image_get_load(rd_hdr);
1058 			break;
1059 #endif
1060 #if IMAGE_ENABLE_FIT
1061 		case IMAGE_FORMAT_FIT:
1062 			rd_noffset = fit_image_load(images,
1063 					rd_addr, &fit_uname_ramdisk,
1064 					&fit_uname_config, arch,
1065 					IH_TYPE_RAMDISK,
1066 					BOOTSTAGE_ID_FIT_RD_START,
1067 					FIT_LOAD_OPTIONAL_NON_ZERO,
1068 					&rd_data, &rd_len);
1069 			if (rd_noffset < 0)
1070 				return 1;
1071 
1072 			images->fit_hdr_rd = map_sysmem(rd_addr, 0);
1073 			images->fit_uname_rd = fit_uname_ramdisk;
1074 			images->fit_noffset_rd = rd_noffset;
1075 			break;
1076 #endif
1077 #ifdef CONFIG_ANDROID_BOOT_IMAGE
1078 		case IMAGE_FORMAT_ANDROID:
1079 			android_image_get_ramdisk((void *)images->os.start,
1080 				&rd_data, &rd_len);
1081 			break;
1082 #endif
1083 		default:
1084 #ifdef CONFIG_SUPPORT_RAW_INITRD
1085 			end = NULL;
1086 			if (select)
1087 				end = strchr(select, ':');
1088 			if (end) {
1089 				rd_len = simple_strtoul(++end, NULL, 16);
1090 				rd_data = rd_addr;
1091 			} else
1092 #endif
1093 			{
1094 				puts("Wrong Ramdisk Image Format\n");
1095 				rd_data = rd_len = rd_load = 0;
1096 				return 1;
1097 			}
1098 		}
1099 	} else if (images->legacy_hdr_valid &&
1100 			image_check_type(&images->legacy_hdr_os_copy,
1101 						IH_TYPE_MULTI)) {
1102 
1103 		/*
1104 		 * Now check if we have a legacy mult-component image,
1105 		 * get second entry data start address and len.
1106 		 */
1107 		bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1108 		printf("## Loading init Ramdisk from multi component "
1109 				"Legacy Image at %08lx ...\n",
1110 				(ulong)images->legacy_hdr_os);
1111 
1112 		image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1113 	} else {
1114 		/*
1115 		 * no initrd image
1116 		 */
1117 		bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1118 		rd_len = rd_data = 0;
1119 	}
1120 
1121 	if (!rd_data) {
1122 		debug("## No init Ramdisk\n");
1123 	} else {
1124 		*rd_start = rd_data;
1125 		*rd_end = rd_data + rd_len;
1126 	}
1127 	debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1128 			*rd_start, *rd_end);
1129 
1130 	return 0;
1131 }
1132 
1133 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1134 /**
1135  * boot_ramdisk_high - relocate init ramdisk
1136  * @lmb: pointer to lmb handle, will be used for memory mgmt
1137  * @rd_data: ramdisk data start address
1138  * @rd_len: ramdisk data length
1139  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1140  *      start address (after possible relocation)
1141  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1142  *      end address (after possible relocation)
1143  *
1144  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
1145  * variable and if requested ramdisk data is moved to a specified location.
1146  *
1147  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1148  * start/end addresses if ramdisk image start and len were provided,
1149  * otherwise set initrd_start and initrd_end set to zeros.
1150  *
1151  * returns:
1152  *      0 - success
1153  *     -1 - failure
1154  */
1155 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1156 		  ulong *initrd_start, ulong *initrd_end)
1157 {
1158 	char	*s;
1159 	ulong	initrd_high;
1160 	int	initrd_copy_to_ram = 1;
1161 
1162 	s = env_get("initrd_high");
1163 	if (s) {
1164 		/* a value of "no" or a similar string will act like 0,
1165 		 * turning the "load high" feature off. This is intentional.
1166 		 */
1167 		initrd_high = simple_strtoul(s, NULL, 16);
1168 		if (initrd_high == ~0)
1169 			initrd_copy_to_ram = 0;
1170 	} else {
1171 		initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
1172 	}
1173 
1174 
1175 	debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1176 			initrd_high, initrd_copy_to_ram);
1177 
1178 	if (rd_data) {
1179 		if (!initrd_copy_to_ram) {	/* zero-copy ramdisk support */
1180 			debug("   in-place initrd\n");
1181 			*initrd_start = rd_data;
1182 			*initrd_end = rd_data + rd_len;
1183 			lmb_reserve(lmb, rd_data, rd_len);
1184 		} else {
1185 			if (initrd_high && env_get_yesno("bootm-reloc-at"))
1186 				initrd_high += rd_len;
1187 
1188 			if (initrd_high)
1189 				*initrd_start = (ulong)lmb_alloc_base(lmb,
1190 						rd_len, 0x1000, initrd_high);
1191 			else
1192 				*initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1193 								 0x1000);
1194 
1195 			if (*initrd_start == 0) {
1196 				puts("ramdisk - allocation error\n");
1197 				goto error;
1198 			}
1199 			bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1200 
1201 			*initrd_end = *initrd_start + rd_len;
1202 			printf("   Loading Ramdisk to %08lx, end %08lx ... ",
1203 					*initrd_start, *initrd_end);
1204 
1205 			memmove_wd((void *)*initrd_start,
1206 					(void *)rd_data, rd_len, CHUNKSZ);
1207 
1208 #ifdef CONFIG_MP
1209 			/*
1210 			 * Ensure the image is flushed to memory to handle
1211 			 * AMP boot scenarios in which we might not be
1212 			 * HW cache coherent
1213 			 */
1214 			flush_cache((unsigned long)*initrd_start, rd_len);
1215 #endif
1216 			puts("OK\n");
1217 		}
1218 	} else {
1219 		*initrd_start = 0;
1220 		*initrd_end = 0;
1221 	}
1222 	debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1223 			*initrd_start, *initrd_end);
1224 
1225 	return 0;
1226 
1227 error:
1228 	return -1;
1229 }
1230 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1231 
1232 int boot_get_setup(bootm_headers_t *images, uint8_t arch,
1233 		   ulong *setup_start, ulong *setup_len)
1234 {
1235 #if IMAGE_ENABLE_FIT
1236 	return boot_get_setup_fit(images, arch, setup_start, setup_len);
1237 #else
1238 	return -ENOENT;
1239 #endif
1240 }
1241 
1242 #if IMAGE_ENABLE_FIT
1243 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_XILINX)
1244 int boot_get_fpga(int argc, char * const argv[], bootm_headers_t *images,
1245 		  uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1246 {
1247 	ulong tmp_img_addr, img_data, img_len;
1248 	void *buf;
1249 	int conf_noffset;
1250 	int fit_img_result;
1251 	const char *uname, *name;
1252 	int err;
1253 	int devnum = 0; /* TODO support multi fpga platforms */
1254 	const fpga_desc * const desc = fpga_get_desc(devnum);
1255 	xilinx_desc *desc_xilinx = desc->devdesc;
1256 
1257 	/* Check to see if the images struct has a FIT configuration */
1258 	if (!genimg_has_config(images)) {
1259 		debug("## FIT configuration was not specified\n");
1260 		return 0;
1261 	}
1262 
1263 	/*
1264 	 * Obtain the os FIT header from the images struct
1265 	 */
1266 	tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
1267 	buf = map_sysmem(tmp_img_addr, 0);
1268 	/*
1269 	 * Check image type. For FIT images get FIT node
1270 	 * and attempt to locate a generic binary.
1271 	 */
1272 	switch (genimg_get_format(buf)) {
1273 	case IMAGE_FORMAT_FIT:
1274 		conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1275 
1276 		uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
1277 					   NULL);
1278 		if (!uname) {
1279 			debug("## FPGA image is not specified\n");
1280 			return 0;
1281 		}
1282 		fit_img_result = fit_image_load(images,
1283 						tmp_img_addr,
1284 						(const char **)&uname,
1285 						&(images->fit_uname_cfg),
1286 						arch,
1287 						IH_TYPE_FPGA,
1288 						BOOTSTAGE_ID_FPGA_INIT,
1289 						FIT_LOAD_OPTIONAL_NON_ZERO,
1290 						&img_data, &img_len);
1291 
1292 		debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
1293 		      uname, img_data, img_len);
1294 
1295 		if (fit_img_result < 0) {
1296 			/* Something went wrong! */
1297 			return fit_img_result;
1298 		}
1299 
1300 		if (img_len >= desc_xilinx->size) {
1301 			name = "full";
1302 			err = fpga_loadbitstream(devnum, (char *)img_data,
1303 						 img_len, BIT_FULL);
1304 			if (err)
1305 				err = fpga_load(devnum, (const void *)img_data,
1306 						img_len, BIT_FULL);
1307 		} else {
1308 			name = "partial";
1309 			err = fpga_loadbitstream(devnum, (char *)img_data,
1310 						 img_len, BIT_PARTIAL);
1311 			if (err)
1312 				err = fpga_load(devnum, (const void *)img_data,
1313 						img_len, BIT_PARTIAL);
1314 		}
1315 
1316 		if (err)
1317 			return err;
1318 
1319 		printf("   Programming %s bitstream... OK\n", name);
1320 		break;
1321 	default:
1322 		printf("The given image format is not supported (corrupt?)\n");
1323 		return 1;
1324 	}
1325 
1326 	return 0;
1327 }
1328 #endif
1329 
1330 static void fit_loadable_process(uint8_t img_type,
1331 				 ulong img_data,
1332 				 ulong img_len)
1333 {
1334 	int i;
1335 	const unsigned int count =
1336 			ll_entry_count(struct fit_loadable_tbl, fit_loadable);
1337 	struct fit_loadable_tbl *fit_loadable_handler =
1338 			ll_entry_start(struct fit_loadable_tbl, fit_loadable);
1339 	/* For each loadable handler */
1340 	for (i = 0; i < count; i++, fit_loadable_handler++)
1341 		/* matching this type */
1342 		if (fit_loadable_handler->type == img_type)
1343 			/* call that handler with this image data */
1344 			fit_loadable_handler->handler(img_data, img_len);
1345 }
1346 
1347 int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
1348 		uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1349 {
1350 	/*
1351 	 * These variables are used to hold the current image location
1352 	 * in system memory.
1353 	 */
1354 	ulong tmp_img_addr;
1355 	/*
1356 	 * These two variables are requirements for fit_image_load, but
1357 	 * their values are not used
1358 	 */
1359 	ulong img_data, img_len;
1360 	void *buf;
1361 	int loadables_index;
1362 	int conf_noffset;
1363 	int fit_img_result;
1364 	const char *uname;
1365 	uint8_t img_type;
1366 
1367 	/* Check to see if the images struct has a FIT configuration */
1368 	if (!genimg_has_config(images)) {
1369 		debug("## FIT configuration was not specified\n");
1370 		return 0;
1371 	}
1372 
1373 	/*
1374 	 * Obtain the os FIT header from the images struct
1375 	 */
1376 	tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
1377 	buf = map_sysmem(tmp_img_addr, 0);
1378 	/*
1379 	 * Check image type. For FIT images get FIT node
1380 	 * and attempt to locate a generic binary.
1381 	 */
1382 	switch (genimg_get_format(buf)) {
1383 	case IMAGE_FORMAT_FIT:
1384 		conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1385 
1386 		for (loadables_index = 0;
1387 		     uname = fdt_stringlist_get(buf, conf_noffset,
1388 					FIT_LOADABLE_PROP, loadables_index,
1389 					NULL), uname;
1390 		     loadables_index++)
1391 		{
1392 			fit_img_result = fit_image_load(images,
1393 				tmp_img_addr,
1394 				&uname,
1395 				&(images->fit_uname_cfg), arch,
1396 				IH_TYPE_LOADABLE,
1397 				BOOTSTAGE_ID_FIT_LOADABLE_START,
1398 				FIT_LOAD_OPTIONAL_NON_ZERO,
1399 				&img_data, &img_len);
1400 			if (fit_img_result < 0) {
1401 				/* Something went wrong! */
1402 				return fit_img_result;
1403 			}
1404 
1405 			fit_img_result = fit_image_get_node(buf, uname);
1406 			if (fit_img_result < 0) {
1407 				/* Something went wrong! */
1408 				return fit_img_result;
1409 			}
1410 			fit_img_result = fit_image_get_type(buf,
1411 							    fit_img_result,
1412 							    &img_type);
1413 			if (fit_img_result < 0) {
1414 				/* Something went wrong! */
1415 				return fit_img_result;
1416 			}
1417 
1418 			fit_loadable_process(img_type, img_data, img_len);
1419 		}
1420 		break;
1421 	default:
1422 		printf("The given image format is not supported (corrupt?)\n");
1423 		return 1;
1424 	}
1425 
1426 	return 0;
1427 }
1428 #endif
1429 
1430 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1431 /**
1432  * boot_get_cmdline - allocate and initialize kernel cmdline
1433  * @lmb: pointer to lmb handle, will be used for memory mgmt
1434  * @cmd_start: pointer to a ulong variable, will hold cmdline start
1435  * @cmd_end: pointer to a ulong variable, will hold cmdline end
1436  *
1437  * boot_get_cmdline() allocates space for kernel command line below
1438  * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environemnt
1439  * variable is present its contents is copied to allocated kernel
1440  * command line.
1441  *
1442  * returns:
1443  *      0 - success
1444  *     -1 - failure
1445  */
1446 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1447 {
1448 	char *cmdline;
1449 	char *s;
1450 
1451 	cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1452 				env_get_bootm_mapsize() + env_get_bootm_low());
1453 
1454 	if (cmdline == NULL)
1455 		return -1;
1456 
1457 	s = env_get("bootargs");
1458 	if (!s)
1459 		s = "";
1460 
1461 	strcpy(cmdline, s);
1462 
1463 	*cmd_start = (ulong) & cmdline[0];
1464 	*cmd_end = *cmd_start + strlen(cmdline);
1465 
1466 	debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1467 
1468 	return 0;
1469 }
1470 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1471 
1472 #ifdef CONFIG_SYS_BOOT_GET_KBD
1473 /**
1474  * boot_get_kbd - allocate and initialize kernel copy of board info
1475  * @lmb: pointer to lmb handle, will be used for memory mgmt
1476  * @kbd: double pointer to board info data
1477  *
1478  * boot_get_kbd() allocates space for kernel copy of board info data below
1479  * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
1480  * with the current u-boot board info data.
1481  *
1482  * returns:
1483  *      0 - success
1484  *     -1 - failure
1485  */
1486 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1487 {
1488 	*kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1489 				env_get_bootm_mapsize() + env_get_bootm_low());
1490 	if (*kbd == NULL)
1491 		return -1;
1492 
1493 	**kbd = *(gd->bd);
1494 
1495 	debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1496 
1497 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1498 	do_bdinfo(NULL, 0, 0, NULL);
1499 #endif
1500 
1501 	return 0;
1502 }
1503 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1504 
1505 #ifdef CONFIG_LMB
1506 int image_setup_linux(bootm_headers_t *images)
1507 {
1508 	ulong of_size = images->ft_len;
1509 	char **of_flat_tree = &images->ft_addr;
1510 	struct lmb *lmb = &images->lmb;
1511 	int ret;
1512 
1513 	if (IMAGE_ENABLE_OF_LIBFDT)
1514 		boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1515 
1516 	if (IMAGE_BOOT_GET_CMDLINE) {
1517 		ret = boot_get_cmdline(lmb, &images->cmdline_start,
1518 				&images->cmdline_end);
1519 		if (ret) {
1520 			puts("ERROR with allocation of cmdline\n");
1521 			return ret;
1522 		}
1523 	}
1524 
1525 	if (IMAGE_ENABLE_OF_LIBFDT) {
1526 		ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1527 		if (ret)
1528 			return ret;
1529 	}
1530 
1531 	if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
1532 		ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
1533 		if (ret)
1534 			return ret;
1535 	}
1536 
1537 	return 0;
1538 }
1539 #endif /* CONFIG_LMB */
1540 #endif /* !USE_HOSTCC */
1541