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