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