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