xref: /openbmc/u-boot/common/image.c (revision 36c7c925)
1 /*
2  * (C) Copyright 2008 Semihalf
3  *
4  * (C) Copyright 2000-2006
5  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25 
26 #ifndef USE_HOSTCC
27 #include <common.h>
28 #include <watchdog.h>
29 
30 #ifdef CONFIG_SHOW_BOOT_PROGRESS
31 #include <status_led.h>
32 #endif
33 
34 #ifdef CONFIG_HAS_DATAFLASH
35 #include <dataflash.h>
36 #endif
37 
38 #ifdef CONFIG_LOGBUFFER
39 #include <logbuff.h>
40 #endif
41 
42 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43 #include <rtc.h>
44 #endif
45 
46 #include <environment.h>
47 #include <image.h>
48 
49 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
50 #include <libfdt.h>
51 #include <fdt_support.h>
52 #endif
53 
54 #if defined(CONFIG_FIT)
55 #include <u-boot/md5.h>
56 #include <sha1.h>
57 
58 static int fit_check_ramdisk(const void *fit, int os_noffset,
59 		uint8_t arch, int verify);
60 #endif
61 
62 #ifdef CONFIG_CMD_BDI
63 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
64 #endif
65 
66 DECLARE_GLOBAL_DATA_PTR;
67 
68 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
69 						int verify);
70 #else
71 #include "mkimage.h"
72 #include <u-boot/md5.h>
73 #include <time.h>
74 #include <image.h>
75 #endif /* !USE_HOSTCC*/
76 
77 static const table_entry_t uimage_arch[] = {
78 	{	IH_ARCH_INVALID,	NULL,		"Invalid ARCH",	},
79 	{	IH_ARCH_ALPHA,		"alpha",	"Alpha",	},
80 	{	IH_ARCH_ARM,		"arm",		"ARM",		},
81 	{	IH_ARCH_I386,		"x86",		"Intel x86",	},
82 	{	IH_ARCH_IA64,		"ia64",		"IA64",		},
83 	{	IH_ARCH_M68K,		"m68k",		"M68K",		},
84 	{	IH_ARCH_MICROBLAZE,	"microblaze",	"MicroBlaze",	},
85 	{	IH_ARCH_MIPS,		"mips",		"MIPS",		},
86 	{	IH_ARCH_MIPS64,		"mips64",	"MIPS 64 Bit",	},
87 	{	IH_ARCH_NIOS2,		"nios2",	"NIOS II",	},
88 	{	IH_ARCH_PPC,		"powerpc",	"PowerPC",	},
89 	{	IH_ARCH_PPC,		"ppc",		"PowerPC",	},
90 	{	IH_ARCH_S390,		"s390",		"IBM S390",	},
91 	{	IH_ARCH_SH,		"sh",		"SuperH",	},
92 	{	IH_ARCH_SPARC,		"sparc",	"SPARC",	},
93 	{	IH_ARCH_SPARC64,	"sparc64",	"SPARC 64 Bit",	},
94 	{	IH_ARCH_BLACKFIN,	"blackfin",	"Blackfin",	},
95 	{	IH_ARCH_AVR32,		"avr32",	"AVR32",	},
96 	{	IH_ARCH_NDS32,		"nds32",	"NDS32",	},
97 	{	IH_ARCH_OPENRISC,	"or1k",		"OpenRISC 1000",},
98 	{	-1,			"",		"",		},
99 };
100 
101 static const table_entry_t uimage_os[] = {
102 	{	IH_OS_INVALID,	NULL,		"Invalid OS",		},
103 	{	IH_OS_LINUX,	"linux",	"Linux",		},
104 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
105 	{	IH_OS_LYNXOS,	"lynxos",	"LynxOS",		},
106 #endif
107 	{	IH_OS_NETBSD,	"netbsd",	"NetBSD",		},
108 	{	IH_OS_OSE,	"ose",		"Enea OSE",		},
109 	{	IH_OS_RTEMS,	"rtems",	"RTEMS",		},
110 	{	IH_OS_U_BOOT,	"u-boot",	"U-Boot",		},
111 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
112 	{	IH_OS_QNX,	"qnx",		"QNX",			},
113 	{	IH_OS_VXWORKS,	"vxworks",	"VxWorks",		},
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 	{	-1,		"",		"",			},
132 };
133 
134 static const table_entry_t uimage_type[] = {
135 	{	IH_TYPE_AISIMAGE,   "aisimage",   "Davinci AIS image",},
136 	{	IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image",	},
137 	{	IH_TYPE_FIRMWARE,   "firmware",	  "Firmware",		},
138 	{	IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",	},
139 	{	IH_TYPE_KERNEL,	    "kernel",	  "Kernel Image",	},
140 	{	IH_TYPE_KERNEL_NOLOAD, "kernel_noload",  "Kernel Image (no loading done)", },
141 	{	IH_TYPE_KWBIMAGE,   "kwbimage",   "Kirkwood Boot Image",},
142 	{	IH_TYPE_IMXIMAGE,   "imximage",   "Freescale i.MX Boot Image",},
143 	{	IH_TYPE_INVALID,    NULL,	  "Invalid Image",	},
144 	{	IH_TYPE_MULTI,	    "multi",	  "Multi-File Image",	},
145 	{	IH_TYPE_OMAPIMAGE,  "omapimage",  "TI OMAP SPL With GP CH",},
146 	{	IH_TYPE_PBLIMAGE,   "pblimage",   "Freescale PBL Boot Image",},
147 	{	IH_TYPE_RAMDISK,    "ramdisk",	  "RAMDisk Image",	},
148 	{	IH_TYPE_SCRIPT,     "script",	  "Script",		},
149 	{	IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
150 	{	IH_TYPE_UBLIMAGE,   "ublimage",   "Davinci UBL image",},
151 	{	-1,		    "",		  "",			},
152 };
153 
154 static const table_entry_t uimage_comp[] = {
155 	{	IH_COMP_NONE,	"none",		"uncompressed",		},
156 	{	IH_COMP_BZIP2,	"bzip2",	"bzip2 compressed",	},
157 	{	IH_COMP_GZIP,	"gzip",		"gzip compressed",	},
158 	{	IH_COMP_LZMA,	"lzma",		"lzma compressed",	},
159 	{	IH_COMP_LZO,	"lzo",		"lzo compressed",	},
160 	{	-1,		"",		"",			},
161 };
162 
163 uint32_t crc32(uint32_t, const unsigned char *, uint);
164 uint32_t crc32_wd(uint32_t, const unsigned char *, uint, uint);
165 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
166 static void genimg_print_time(time_t timestamp);
167 #endif
168 
169 /*****************************************************************************/
170 /* Legacy format routines */
171 /*****************************************************************************/
172 int image_check_hcrc(const image_header_t *hdr)
173 {
174 	ulong hcrc;
175 	ulong len = image_get_header_size();
176 	image_header_t header;
177 
178 	/* Copy header so we can blank CRC field for re-calculation */
179 	memmove(&header, (char *)hdr, image_get_header_size());
180 	image_set_hcrc(&header, 0);
181 
182 	hcrc = crc32(0, (unsigned char *)&header, len);
183 
184 	return (hcrc == image_get_hcrc(hdr));
185 }
186 
187 int image_check_dcrc(const image_header_t *hdr)
188 {
189 	ulong data = image_get_data(hdr);
190 	ulong len = image_get_data_size(hdr);
191 	ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
192 
193 	return (dcrc == image_get_dcrc(hdr));
194 }
195 
196 /**
197  * image_multi_count - get component (sub-image) count
198  * @hdr: pointer to the header of the multi component image
199  *
200  * image_multi_count() returns number of components in a multi
201  * component image.
202  *
203  * Note: no checking of the image type is done, caller must pass
204  * a valid multi component image.
205  *
206  * returns:
207  *     number of components
208  */
209 ulong image_multi_count(const image_header_t *hdr)
210 {
211 	ulong i, count = 0;
212 	uint32_t *size;
213 
214 	/* get start of the image payload, which in case of multi
215 	 * component images that points to a table of component sizes */
216 	size = (uint32_t *)image_get_data(hdr);
217 
218 	/* count non empty slots */
219 	for (i = 0; size[i]; ++i)
220 		count++;
221 
222 	return count;
223 }
224 
225 /**
226  * image_multi_getimg - get component data address and size
227  * @hdr: pointer to the header of the multi component image
228  * @idx: index of the requested component
229  * @data: pointer to a ulong variable, will hold component data address
230  * @len: pointer to a ulong variable, will hold component size
231  *
232  * image_multi_getimg() returns size and data address for the requested
233  * component in a multi component image.
234  *
235  * Note: no checking of the image type is done, caller must pass
236  * a valid multi component image.
237  *
238  * returns:
239  *     data address and size of the component, if idx is valid
240  *     0 in data and len, if idx is out of range
241  */
242 void image_multi_getimg(const image_header_t *hdr, ulong idx,
243 			ulong *data, ulong *len)
244 {
245 	int i;
246 	uint32_t *size;
247 	ulong offset, count, img_data;
248 
249 	/* get number of component */
250 	count = image_multi_count(hdr);
251 
252 	/* get start of the image payload, which in case of multi
253 	 * component images that points to a table of component sizes */
254 	size = (uint32_t *)image_get_data(hdr);
255 
256 	/* get address of the proper component data start, which means
257 	 * skipping sizes table (add 1 for last, null entry) */
258 	img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
259 
260 	if (idx < count) {
261 		*len = uimage_to_cpu(size[idx]);
262 		offset = 0;
263 
264 		/* go over all indices preceding requested component idx */
265 		for (i = 0; i < idx; i++) {
266 			/* add up i-th component size, rounding up to 4 bytes */
267 			offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
268 		}
269 
270 		/* calculate idx-th component data address */
271 		*data = img_data + offset;
272 	} else {
273 		*len = 0;
274 		*data = 0;
275 	}
276 }
277 
278 static void image_print_type(const image_header_t *hdr)
279 {
280 	const char *os, *arch, *type, *comp;
281 
282 	os = genimg_get_os_name(image_get_os(hdr));
283 	arch = genimg_get_arch_name(image_get_arch(hdr));
284 	type = genimg_get_type_name(image_get_type(hdr));
285 	comp = genimg_get_comp_name(image_get_comp(hdr));
286 
287 	printf("%s %s %s (%s)\n", arch, os, type, comp);
288 }
289 
290 /**
291  * image_print_contents - prints out the contents of the legacy format image
292  * @ptr: pointer to the legacy format image header
293  * @p: pointer to prefix string
294  *
295  * image_print_contents() formats a multi line legacy image contents description.
296  * The routine prints out all header fields followed by the size/offset data
297  * for MULTI/SCRIPT images.
298  *
299  * returns:
300  *     no returned results
301  */
302 void image_print_contents(const void *ptr)
303 {
304 	const image_header_t *hdr = (const image_header_t *)ptr;
305 	const char *p;
306 
307 #ifdef USE_HOSTCC
308 	p = "";
309 #else
310 	p = "   ";
311 #endif
312 
313 	printf("%sImage Name:   %.*s\n", p, IH_NMLEN, image_get_name(hdr));
314 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
315 	printf("%sCreated:      ", p);
316 	genimg_print_time((time_t)image_get_time(hdr));
317 #endif
318 	printf("%sImage Type:   ", p);
319 	image_print_type(hdr);
320 	printf("%sData Size:    ", p);
321 	genimg_print_size(image_get_data_size(hdr));
322 	printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
323 	printf("%sEntry Point:  %08x\n", p, image_get_ep(hdr));
324 
325 	if (image_check_type(hdr, IH_TYPE_MULTI) ||
326 			image_check_type(hdr, IH_TYPE_SCRIPT)) {
327 		int i;
328 		ulong data, len;
329 		ulong count = image_multi_count(hdr);
330 
331 		printf("%sContents:\n", p);
332 		for (i = 0; i < count; i++) {
333 			image_multi_getimg(hdr, i, &data, &len);
334 
335 			printf("%s   Image %d: ", p, i);
336 			genimg_print_size(len);
337 
338 			if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
339 				/*
340 				 * the user may need to know offsets
341 				 * if planning to do something with
342 				 * multiple files
343 				 */
344 				printf("%s    Offset = 0x%08lx\n", p, data);
345 			}
346 		}
347 	}
348 }
349 
350 
351 #ifndef USE_HOSTCC
352 /**
353  * image_get_ramdisk - get and verify ramdisk image
354  * @rd_addr: ramdisk image start address
355  * @arch: expected ramdisk architecture
356  * @verify: checksum verification flag
357  *
358  * image_get_ramdisk() returns a pointer to the verified ramdisk image
359  * header. Routine receives image start address and expected architecture
360  * flag. Verification done covers data and header integrity and os/type/arch
361  * fields checking.
362  *
363  * If dataflash support is enabled routine checks for dataflash addresses
364  * and handles required dataflash reads.
365  *
366  * returns:
367  *     pointer to a ramdisk image header, if image was found and valid
368  *     otherwise, return NULL
369  */
370 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
371 						int verify)
372 {
373 	const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
374 
375 	if (!image_check_magic(rd_hdr)) {
376 		puts("Bad Magic Number\n");
377 		bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
378 		return NULL;
379 	}
380 
381 	if (!image_check_hcrc(rd_hdr)) {
382 		puts("Bad Header Checksum\n");
383 		bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
384 		return NULL;
385 	}
386 
387 	bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
388 	image_print_contents(rd_hdr);
389 
390 	if (verify) {
391 		puts("   Verifying Checksum ... ");
392 		if (!image_check_dcrc(rd_hdr)) {
393 			puts("Bad Data CRC\n");
394 			bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
395 			return NULL;
396 		}
397 		puts("OK\n");
398 	}
399 
400 	bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
401 
402 	if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
403 	    !image_check_arch(rd_hdr, arch) ||
404 	    !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
405 		printf("No Linux %s Ramdisk Image\n",
406 				genimg_get_arch_name(arch));
407 		bootstage_error(BOOTSTAGE_ID_RAMDISK);
408 		return NULL;
409 	}
410 
411 	return rd_hdr;
412 }
413 #endif /* !USE_HOSTCC */
414 
415 /*****************************************************************************/
416 /* Shared dual-format routines */
417 /*****************************************************************************/
418 #ifndef USE_HOSTCC
419 ulong load_addr = CONFIG_SYS_LOAD_ADDR;	/* Default Load Address */
420 ulong save_addr;			/* Default Save Address */
421 ulong save_size;			/* Default Save Size (in bytes) */
422 
423 static int on_loadaddr(const char *name, const char *value, enum env_op op,
424 	int flags)
425 {
426 	switch (op) {
427 	case env_op_create:
428 	case env_op_overwrite:
429 		load_addr = simple_strtoul(value, NULL, 16);
430 		break;
431 	default:
432 		break;
433 	}
434 
435 	return 0;
436 }
437 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
438 
439 ulong getenv_bootm_low(void)
440 {
441 	char *s = getenv("bootm_low");
442 	if (s) {
443 		ulong tmp = simple_strtoul(s, NULL, 16);
444 		return tmp;
445 	}
446 
447 #if defined(CONFIG_SYS_SDRAM_BASE)
448 	return CONFIG_SYS_SDRAM_BASE;
449 #elif defined(CONFIG_ARM)
450 	return gd->bd->bi_dram[0].start;
451 #else
452 	return 0;
453 #endif
454 }
455 
456 phys_size_t getenv_bootm_size(void)
457 {
458 	phys_size_t tmp;
459 	char *s = getenv("bootm_size");
460 	if (s) {
461 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
462 		return tmp;
463 	}
464 	s = getenv("bootm_low");
465 	if (s)
466 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
467 	else
468 		tmp = 0;
469 
470 
471 #if defined(CONFIG_ARM)
472 	return gd->bd->bi_dram[0].size - tmp;
473 #else
474 	return gd->bd->bi_memsize - tmp;
475 #endif
476 }
477 
478 phys_size_t getenv_bootm_mapsize(void)
479 {
480 	phys_size_t tmp;
481 	char *s = getenv("bootm_mapsize");
482 	if (s) {
483 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
484 		return tmp;
485 	}
486 
487 #if defined(CONFIG_SYS_BOOTMAPSZ)
488 	return CONFIG_SYS_BOOTMAPSZ;
489 #else
490 	return getenv_bootm_size();
491 #endif
492 }
493 
494 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
495 {
496 	if (to == from)
497 		return;
498 
499 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
500 	while (len > 0) {
501 		size_t tail = (len > chunksz) ? chunksz : len;
502 		WATCHDOG_RESET();
503 		memmove(to, from, tail);
504 		to += tail;
505 		from += tail;
506 		len -= tail;
507 	}
508 #else	/* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
509 	memmove(to, from, len);
510 #endif	/* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
511 }
512 #endif /* !USE_HOSTCC */
513 
514 void genimg_print_size(uint32_t size)
515 {
516 #ifndef USE_HOSTCC
517 	printf("%d Bytes = ", size);
518 	print_size(size, "\n");
519 #else
520 	printf("%d Bytes = %.2f kB = %.2f MB\n",
521 			size, (double)size / 1.024e3,
522 			(double)size / 1.048576e6);
523 #endif
524 }
525 
526 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
527 static void genimg_print_time(time_t timestamp)
528 {
529 #ifndef USE_HOSTCC
530 	struct rtc_time tm;
531 
532 	to_tm(timestamp, &tm);
533 	printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
534 			tm.tm_year, tm.tm_mon, tm.tm_mday,
535 			tm.tm_hour, tm.tm_min, tm.tm_sec);
536 #else
537 	printf("%s", ctime(&timestamp));
538 #endif
539 }
540 #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
541 
542 /**
543  * get_table_entry_name - translate entry id to long name
544  * @table: pointer to a translation table for entries of a specific type
545  * @msg: message to be returned when translation fails
546  * @id: entry id to be translated
547  *
548  * get_table_entry_name() will go over translation table trying to find
549  * entry that matches given id. If matching entry is found, its long
550  * name is returned to the caller.
551  *
552  * returns:
553  *     long entry name if translation succeeds
554  *     msg otherwise
555  */
556 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
557 {
558 	for (; table->id >= 0; ++table) {
559 		if (table->id == id)
560 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
561 			return table->lname;
562 #else
563 			return table->lname + gd->reloc_off;
564 #endif
565 	}
566 	return (msg);
567 }
568 
569 const char *genimg_get_os_name(uint8_t os)
570 {
571 	return (get_table_entry_name(uimage_os, "Unknown OS", os));
572 }
573 
574 const char *genimg_get_arch_name(uint8_t arch)
575 {
576 	return (get_table_entry_name(uimage_arch, "Unknown Architecture",
577 					arch));
578 }
579 
580 const char *genimg_get_type_name(uint8_t type)
581 {
582 	return (get_table_entry_name(uimage_type, "Unknown Image", type));
583 }
584 
585 const char *genimg_get_comp_name(uint8_t comp)
586 {
587 	return (get_table_entry_name(uimage_comp, "Unknown Compression",
588 					comp));
589 }
590 
591 /**
592  * get_table_entry_id - translate short entry name to id
593  * @table: pointer to a translation table for entries of a specific type
594  * @table_name: to be used in case of error
595  * @name: entry short name to be translated
596  *
597  * get_table_entry_id() will go over translation table trying to find
598  * entry that matches given short name. If matching entry is found,
599  * its id returned to the caller.
600  *
601  * returns:
602  *     entry id if translation succeeds
603  *     -1 otherwise
604  */
605 int get_table_entry_id(const table_entry_t *table,
606 		const char *table_name, const char *name)
607 {
608 	const table_entry_t *t;
609 #ifdef USE_HOSTCC
610 	int first = 1;
611 
612 	for (t = table; t->id >= 0; ++t) {
613 		if (t->sname && strcasecmp(t->sname, name) == 0)
614 			return(t->id);
615 	}
616 
617 	fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
618 	for (t = table; t->id >= 0; ++t) {
619 		if (t->sname == NULL)
620 			continue;
621 		fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
622 		first = 0;
623 	}
624 	fprintf(stderr, "\n");
625 #else
626 	for (t = table; t->id >= 0; ++t) {
627 #ifdef CONFIG_NEEDS_MANUAL_RELOC
628 		if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
629 #else
630 		if (t->sname && strcmp(t->sname, name) == 0)
631 #endif
632 			return (t->id);
633 	}
634 	debug("Invalid %s Type: %s\n", table_name, name);
635 #endif /* USE_HOSTCC */
636 	return (-1);
637 }
638 
639 int genimg_get_os_id(const char *name)
640 {
641 	return (get_table_entry_id(uimage_os, "OS", name));
642 }
643 
644 int genimg_get_arch_id(const char *name)
645 {
646 	return (get_table_entry_id(uimage_arch, "CPU", name));
647 }
648 
649 int genimg_get_type_id(const char *name)
650 {
651 	return (get_table_entry_id(uimage_type, "Image", name));
652 }
653 
654 int genimg_get_comp_id(const char *name)
655 {
656 	return (get_table_entry_id(uimage_comp, "Compression", name));
657 }
658 
659 #ifndef USE_HOSTCC
660 /**
661  * genimg_get_format - get image format type
662  * @img_addr: image start address
663  *
664  * genimg_get_format() checks whether provided address points to a valid
665  * legacy or FIT image.
666  *
667  * New uImage format and FDT blob are based on a libfdt. FDT blob
668  * may be passed directly or embedded in a FIT image. In both situations
669  * genimg_get_format() must be able to dectect libfdt header.
670  *
671  * returns:
672  *     image format type or IMAGE_FORMAT_INVALID if no image is present
673  */
674 int genimg_get_format(void *img_addr)
675 {
676 	ulong format = IMAGE_FORMAT_INVALID;
677 	const image_header_t *hdr;
678 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
679 	char *fit_hdr;
680 #endif
681 
682 	hdr = (const image_header_t *)img_addr;
683 	if (image_check_magic(hdr))
684 		format = IMAGE_FORMAT_LEGACY;
685 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
686 	else {
687 		fit_hdr = (char *)img_addr;
688 		if (fdt_check_header(fit_hdr) == 0)
689 			format = IMAGE_FORMAT_FIT;
690 	}
691 #endif
692 
693 	return format;
694 }
695 
696 /**
697  * genimg_get_image - get image from special storage (if necessary)
698  * @img_addr: image start address
699  *
700  * genimg_get_image() checks if provided image start adddress is located
701  * in a dataflash storage. If so, image is moved to a system RAM memory.
702  *
703  * returns:
704  *     image start address after possible relocation from special storage
705  */
706 ulong genimg_get_image(ulong img_addr)
707 {
708 	ulong ram_addr = img_addr;
709 
710 #ifdef CONFIG_HAS_DATAFLASH
711 	ulong h_size, d_size;
712 
713 	if (addr_dataflash(img_addr)) {
714 		/* ger RAM address */
715 		ram_addr = CONFIG_SYS_LOAD_ADDR;
716 
717 		/* get header size */
718 		h_size = image_get_header_size();
719 #if defined(CONFIG_FIT)
720 		if (sizeof(struct fdt_header) > h_size)
721 			h_size = sizeof(struct fdt_header);
722 #endif
723 
724 		/* read in header */
725 		debug("   Reading image header from dataflash address "
726 			"%08lx to RAM address %08lx\n", img_addr, ram_addr);
727 
728 		read_dataflash(img_addr, h_size, (char *)ram_addr);
729 
730 		/* get data size */
731 		switch (genimg_get_format((void *)ram_addr)) {
732 		case IMAGE_FORMAT_LEGACY:
733 			d_size = image_get_data_size(
734 					(const image_header_t *)ram_addr);
735 			debug("   Legacy format image found at 0x%08lx, "
736 					"size 0x%08lx\n",
737 					ram_addr, d_size);
738 			break;
739 #if defined(CONFIG_FIT)
740 		case IMAGE_FORMAT_FIT:
741 			d_size = fit_get_size((const void *)ram_addr) - h_size;
742 			debug("   FIT/FDT format image found at 0x%08lx, "
743 					"size 0x%08lx\n",
744 					ram_addr, d_size);
745 			break;
746 #endif
747 		default:
748 			printf("   No valid image found at 0x%08lx\n",
749 				img_addr);
750 			return ram_addr;
751 		}
752 
753 		/* read in image data */
754 		debug("   Reading image remaining data from dataflash address "
755 			"%08lx to RAM address %08lx\n", img_addr + h_size,
756 			ram_addr + h_size);
757 
758 		read_dataflash(img_addr + h_size, d_size,
759 				(char *)(ram_addr + h_size));
760 
761 	}
762 #endif /* CONFIG_HAS_DATAFLASH */
763 
764 	return ram_addr;
765 }
766 
767 /**
768  * fit_has_config - check if there is a valid FIT configuration
769  * @images: pointer to the bootm command headers structure
770  *
771  * fit_has_config() checks if there is a FIT configuration in use
772  * (if FTI support is present).
773  *
774  * returns:
775  *     0, no FIT support or no configuration found
776  *     1, configuration found
777  */
778 int genimg_has_config(bootm_headers_t *images)
779 {
780 #if defined(CONFIG_FIT)
781 	if (images->fit_uname_cfg)
782 		return 1;
783 #endif
784 	return 0;
785 }
786 
787 /**
788  * boot_get_ramdisk - main ramdisk handling routine
789  * @argc: command argument count
790  * @argv: command argument list
791  * @images: pointer to the bootm images structure
792  * @arch: expected ramdisk architecture
793  * @rd_start: pointer to a ulong variable, will hold ramdisk start address
794  * @rd_end: pointer to a ulong variable, will hold ramdisk end
795  *
796  * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
797  * Curently supported are the following ramdisk sources:
798  *      - multicomponent kernel/ramdisk image,
799  *      - commandline provided address of decicated ramdisk image.
800  *
801  * returns:
802  *     0, if ramdisk image was found and valid, or skiped
803  *     rd_start and rd_end are set to ramdisk start/end addresses if
804  *     ramdisk image is found and valid
805  *
806  *     1, if ramdisk image is found but corrupted, or invalid
807  *     rd_start and rd_end are set to 0 if no ramdisk exists
808  */
809 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
810 		uint8_t arch, ulong *rd_start, ulong *rd_end)
811 {
812 	ulong rd_addr, rd_load;
813 	ulong rd_data, rd_len;
814 	const image_header_t *rd_hdr;
815 #ifdef CONFIG_SUPPORT_RAW_INITRD
816 	char *end;
817 #endif
818 #if defined(CONFIG_FIT)
819 	void		*fit_hdr;
820 	const char	*fit_uname_config = NULL;
821 	const char	*fit_uname_ramdisk = NULL;
822 	ulong		default_addr;
823 	int		rd_noffset;
824 	int		cfg_noffset;
825 	const void	*data;
826 	size_t		size;
827 #endif
828 
829 	*rd_start = 0;
830 	*rd_end = 0;
831 
832 	/*
833 	 * Look for a '-' which indicates to ignore the
834 	 * ramdisk argument
835 	 */
836 	if ((argc >= 3) && (strcmp(argv[2], "-") ==  0)) {
837 		debug("## Skipping init Ramdisk\n");
838 		rd_len = rd_data = 0;
839 	} else if (argc >= 3 || genimg_has_config(images)) {
840 #if defined(CONFIG_FIT)
841 		if (argc >= 3) {
842 			/*
843 			 * If the init ramdisk comes from the FIT image and
844 			 * the FIT image address is omitted in the command
845 			 * line argument, try to use os FIT image address or
846 			 * default load address.
847 			 */
848 			if (images->fit_uname_os)
849 				default_addr = (ulong)images->fit_hdr_os;
850 			else
851 				default_addr = load_addr;
852 
853 			if (fit_parse_conf(argv[2], default_addr,
854 						&rd_addr, &fit_uname_config)) {
855 				debug("*  ramdisk: config '%s' from image at "
856 						"0x%08lx\n",
857 						fit_uname_config, rd_addr);
858 			} else if (fit_parse_subimage(argv[2], default_addr,
859 						&rd_addr, &fit_uname_ramdisk)) {
860 				debug("*  ramdisk: subimage '%s' from image at "
861 						"0x%08lx\n",
862 						fit_uname_ramdisk, rd_addr);
863 			} else
864 #endif
865 			{
866 				rd_addr = simple_strtoul(argv[2], NULL, 16);
867 				debug("*  ramdisk: cmdline image address = "
868 						"0x%08lx\n",
869 						rd_addr);
870 			}
871 #if defined(CONFIG_FIT)
872 		} else {
873 			/* use FIT configuration provided in first bootm
874 			 * command argument
875 			 */
876 			rd_addr = (ulong)images->fit_hdr_os;
877 			fit_uname_config = images->fit_uname_cfg;
878 			debug("*  ramdisk: using config '%s' from image "
879 					"at 0x%08lx\n",
880 					fit_uname_config, rd_addr);
881 
882 			/*
883 			 * Check whether configuration has ramdisk defined,
884 			 * if not, don't try to use it, quit silently.
885 			 */
886 			fit_hdr = (void *)rd_addr;
887 			cfg_noffset = fit_conf_get_node(fit_hdr,
888 							fit_uname_config);
889 			if (cfg_noffset < 0) {
890 				debug("*  ramdisk: no such config\n");
891 				return 1;
892 			}
893 
894 			rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
895 								cfg_noffset);
896 			if (rd_noffset < 0) {
897 				debug("*  ramdisk: no ramdisk in config\n");
898 				return 0;
899 			}
900 		}
901 #endif
902 
903 		/* copy from dataflash if needed */
904 		rd_addr = genimg_get_image(rd_addr);
905 
906 		/*
907 		 * Check if there is an initrd image at the
908 		 * address provided in the second bootm argument
909 		 * check image type, for FIT images get FIT node.
910 		 */
911 		switch (genimg_get_format((void *)rd_addr)) {
912 		case IMAGE_FORMAT_LEGACY:
913 			printf("## Loading init Ramdisk from Legacy "
914 					"Image at %08lx ...\n", rd_addr);
915 
916 			bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
917 			rd_hdr = image_get_ramdisk(rd_addr, arch,
918 							images->verify);
919 
920 			if (rd_hdr == NULL)
921 				return 1;
922 
923 			rd_data = image_get_data(rd_hdr);
924 			rd_len = image_get_data_size(rd_hdr);
925 			rd_load = image_get_load(rd_hdr);
926 			break;
927 #if defined(CONFIG_FIT)
928 		case IMAGE_FORMAT_FIT:
929 			fit_hdr = (void *)rd_addr;
930 			printf("## Loading init Ramdisk from FIT "
931 					"Image at %08lx ...\n", rd_addr);
932 
933 			bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
934 			if (!fit_check_format(fit_hdr)) {
935 				puts("Bad FIT ramdisk image format!\n");
936 				bootstage_error(
937 					BOOTSTAGE_ID_FIT_RD_FORMAT);
938 				return 1;
939 			}
940 			bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
941 
942 			if (!fit_uname_ramdisk) {
943 				/*
944 				 * no ramdisk image node unit name, try to get config
945 				 * node first. If config unit node name is NULL
946 				 * fit_conf_get_node() will try to find default config node
947 				 */
948 				bootstage_mark(
949 					BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
950 				cfg_noffset = fit_conf_get_node(fit_hdr,
951 							fit_uname_config);
952 				if (cfg_noffset < 0) {
953 					puts("Could not find configuration "
954 						"node\n");
955 					bootstage_error(
956 					BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
957 					return 1;
958 				}
959 				fit_uname_config = fdt_get_name(fit_hdr,
960 							cfg_noffset, NULL);
961 				printf("   Using '%s' configuration\n",
962 					fit_uname_config);
963 
964 				rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
965 							cfg_noffset);
966 				fit_uname_ramdisk = fit_get_name(fit_hdr,
967 							rd_noffset, NULL);
968 			} else {
969 				/* get ramdisk component image node offset */
970 				bootstage_mark(
971 					BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
972 				rd_noffset = fit_image_get_node(fit_hdr,
973 						fit_uname_ramdisk);
974 			}
975 			if (rd_noffset < 0) {
976 				puts("Could not find subimage node\n");
977 				bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
978 				return 1;
979 			}
980 
981 			printf("   Trying '%s' ramdisk subimage\n",
982 				fit_uname_ramdisk);
983 
984 			bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
985 			if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
986 						images->verify))
987 				return 1;
988 
989 			/* get ramdisk image data address and length */
990 			if (fit_image_get_data(fit_hdr, rd_noffset, &data,
991 						&size)) {
992 				puts("Could not find ramdisk subimage data!\n");
993 				bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
994 				return 1;
995 			}
996 			bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
997 
998 			rd_data = (ulong)data;
999 			rd_len = size;
1000 
1001 			if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
1002 				puts("Can't get ramdisk subimage load "
1003 					"address!\n");
1004 				bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
1005 				return 1;
1006 			}
1007 			bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
1008 
1009 			images->fit_hdr_rd = fit_hdr;
1010 			images->fit_uname_rd = fit_uname_ramdisk;
1011 			images->fit_noffset_rd = rd_noffset;
1012 			break;
1013 #endif
1014 		default:
1015 #ifdef CONFIG_SUPPORT_RAW_INITRD
1016 			if (argc >= 3 && (end = strchr(argv[2], ':'))) {
1017 				rd_len = simple_strtoul(++end, NULL, 16);
1018 				rd_data = rd_addr;
1019 			} else
1020 #endif
1021 			{
1022 				puts("Wrong Ramdisk Image Format\n");
1023 				rd_data = rd_len = rd_load = 0;
1024 				return 1;
1025 			}
1026 		}
1027 	} else if (images->legacy_hdr_valid &&
1028 			image_check_type(&images->legacy_hdr_os_copy,
1029 						IH_TYPE_MULTI)) {
1030 
1031 		/*
1032 		 * Now check if we have a legacy mult-component image,
1033 		 * get second entry data start address and len.
1034 		 */
1035 		bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1036 		printf("## Loading init Ramdisk from multi component "
1037 				"Legacy Image at %08lx ...\n",
1038 				(ulong)images->legacy_hdr_os);
1039 
1040 		image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1041 	} else {
1042 		/*
1043 		 * no initrd image
1044 		 */
1045 		bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1046 		rd_len = rd_data = 0;
1047 	}
1048 
1049 	if (!rd_data) {
1050 		debug("## No init Ramdisk\n");
1051 	} else {
1052 		*rd_start = rd_data;
1053 		*rd_end = rd_data + rd_len;
1054 	}
1055 	debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1056 			*rd_start, *rd_end);
1057 
1058 	return 0;
1059 }
1060 
1061 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1062 /**
1063  * boot_ramdisk_high - relocate init ramdisk
1064  * @lmb: pointer to lmb handle, will be used for memory mgmt
1065  * @rd_data: ramdisk data start address
1066  * @rd_len: ramdisk data length
1067  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1068  *      start address (after possible relocation)
1069  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1070  *      end address (after possible relocation)
1071  *
1072  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1073  * variable and if requested ramdisk data is moved to a specified location.
1074  *
1075  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1076  * start/end addresses if ramdisk image start and len were provided,
1077  * otherwise set initrd_start and initrd_end set to zeros.
1078  *
1079  * returns:
1080  *      0 - success
1081  *     -1 - failure
1082  */
1083 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1084 		  ulong *initrd_start, ulong *initrd_end)
1085 {
1086 	char	*s;
1087 	ulong	initrd_high;
1088 	int	initrd_copy_to_ram = 1;
1089 
1090 	if ((s = getenv("initrd_high")) != NULL) {
1091 		/* a value of "no" or a similar string will act like 0,
1092 		 * turning the "load high" feature off. This is intentional.
1093 		 */
1094 		initrd_high = simple_strtoul(s, NULL, 16);
1095 		if (initrd_high == ~0)
1096 			initrd_copy_to_ram = 0;
1097 	} else {
1098 		/* not set, no restrictions to load high */
1099 		initrd_high = ~0;
1100 	}
1101 
1102 
1103 #ifdef CONFIG_LOGBUFFER
1104 	/* Prevent initrd from overwriting logbuffer */
1105 	lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1106 #endif
1107 
1108 	debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1109 			initrd_high, initrd_copy_to_ram);
1110 
1111 	if (rd_data) {
1112 		if (!initrd_copy_to_ram) {	/* zero-copy ramdisk support */
1113 			debug("   in-place initrd\n");
1114 			*initrd_start = rd_data;
1115 			*initrd_end = rd_data + rd_len;
1116 			lmb_reserve(lmb, rd_data, rd_len);
1117 		} else {
1118 			if (initrd_high)
1119 				*initrd_start = (ulong)lmb_alloc_base(lmb,
1120 						rd_len, 0x1000, initrd_high);
1121 			else
1122 				*initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1123 								 0x1000);
1124 
1125 			if (*initrd_start == 0) {
1126 				puts("ramdisk - allocation error\n");
1127 				goto error;
1128 			}
1129 			bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1130 
1131 			*initrd_end = *initrd_start + rd_len;
1132 			printf("   Loading Ramdisk to %08lx, end %08lx ... ",
1133 					*initrd_start, *initrd_end);
1134 
1135 			memmove_wd((void *)*initrd_start,
1136 					(void *)rd_data, rd_len, CHUNKSZ);
1137 
1138 #ifdef CONFIG_MP
1139 			/*
1140 			 * Ensure the image is flushed to memory to handle
1141 			 * AMP boot scenarios in which we might not be
1142 			 * HW cache coherent
1143 			 */
1144 			flush_cache((unsigned long)*initrd_start, rd_len);
1145 #endif
1146 			puts("OK\n");
1147 		}
1148 	} else {
1149 		*initrd_start = 0;
1150 		*initrd_end = 0;
1151 	}
1152 	debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1153 			*initrd_start, *initrd_end);
1154 
1155 	return 0;
1156 
1157 error:
1158 	return -1;
1159 }
1160 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1161 
1162 #ifdef CONFIG_OF_LIBFDT
1163 static void fdt_error(const char *msg)
1164 {
1165 	puts("ERROR: ");
1166 	puts(msg);
1167 	puts(" - must RESET the board to recover.\n");
1168 }
1169 
1170 static const image_header_t *image_get_fdt(ulong fdt_addr)
1171 {
1172 	const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1173 
1174 	image_print_contents(fdt_hdr);
1175 
1176 	puts("   Verifying Checksum ... ");
1177 	if (!image_check_hcrc(fdt_hdr)) {
1178 		fdt_error("fdt header checksum invalid");
1179 		return NULL;
1180 	}
1181 
1182 	if (!image_check_dcrc(fdt_hdr)) {
1183 		fdt_error("fdt checksum invalid");
1184 		return NULL;
1185 	}
1186 	puts("OK\n");
1187 
1188 	if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1189 		fdt_error("uImage is not a fdt");
1190 		return NULL;
1191 	}
1192 	if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1193 		fdt_error("uImage is compressed");
1194 		return NULL;
1195 	}
1196 	if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1197 		fdt_error("uImage data is not a fdt");
1198 		return NULL;
1199 	}
1200 	return fdt_hdr;
1201 }
1202 
1203 /**
1204  * fit_check_fdt - verify FIT format FDT subimage
1205  * @fit_hdr: pointer to the FIT  header
1206  * fdt_noffset: FDT subimage node offset within FIT image
1207  * @verify: data CRC verification flag
1208  *
1209  * fit_check_fdt() verifies integrity of the FDT subimage and from
1210  * specified FIT image.
1211  *
1212  * returns:
1213  *     1, on success
1214  *     0, on failure
1215  */
1216 #if defined(CONFIG_FIT)
1217 static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
1218 {
1219 	fit_image_print(fit, fdt_noffset, "   ");
1220 
1221 	if (verify) {
1222 		puts("   Verifying Hash Integrity ... ");
1223 		if (!fit_image_check_hashes(fit, fdt_noffset)) {
1224 			fdt_error("Bad Data Hash");
1225 			return 0;
1226 		}
1227 		puts("OK\n");
1228 	}
1229 
1230 	if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1231 		fdt_error("Not a FDT image");
1232 		return 0;
1233 	}
1234 
1235 	if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1236 		fdt_error("FDT image is compressed");
1237 		return 0;
1238 	}
1239 
1240 	return 1;
1241 }
1242 #endif /* CONFIG_FIT */
1243 
1244 #ifndef CONFIG_SYS_FDT_PAD
1245 #define CONFIG_SYS_FDT_PAD 0x3000
1246 #endif
1247 
1248 #if defined(CONFIG_OF_LIBFDT)
1249 /**
1250  * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1251  * @lmb: pointer to lmb handle, will be used for memory mgmt
1252  * @fdt_blob: pointer to fdt blob base address
1253  *
1254  * Adds the memreserve regions in the dtb to the lmb block.  Adding the
1255  * memreserve regions prevents u-boot from using them to store the initrd
1256  * or the fdt blob.
1257  */
1258 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1259 {
1260 	uint64_t addr, size;
1261 	int i, total;
1262 
1263 	if (fdt_check_header(fdt_blob) != 0)
1264 		return;
1265 
1266 	total = fdt_num_mem_rsv(fdt_blob);
1267 	for (i = 0; i < total; i++) {
1268 		if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1269 			continue;
1270 		printf("   reserving fdt memory region: addr=%llx size=%llx\n",
1271 			(unsigned long long)addr, (unsigned long long)size);
1272 		lmb_reserve(lmb, addr, size);
1273 	}
1274 }
1275 
1276 /**
1277  * boot_relocate_fdt - relocate flat device tree
1278  * @lmb: pointer to lmb handle, will be used for memory mgmt
1279  * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1280  * @of_size: pointer to a ulong variable, will hold fdt length
1281  *
1282  * boot_relocate_fdt() allocates a region of memory within the bootmap and
1283  * relocates the of_flat_tree into that region, even if the fdt is already in
1284  * the bootmap.  It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1285  * bytes.
1286  *
1287  * of_flat_tree and of_size are set to final (after relocation) values
1288  *
1289  * returns:
1290  *      0 - success
1291  *      1 - failure
1292  */
1293 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1294 {
1295 	void	*fdt_blob = *of_flat_tree;
1296 	void	*of_start = NULL;
1297 	char	*fdt_high;
1298 	ulong	of_len = 0;
1299 	int	err;
1300 	int	disable_relocation = 0;
1301 
1302 	/* nothing to do */
1303 	if (*of_size == 0)
1304 		return 0;
1305 
1306 	if (fdt_check_header(fdt_blob) != 0) {
1307 		fdt_error("image is not a fdt");
1308 		goto error;
1309 	}
1310 
1311 	/* position on a 4K boundary before the alloc_current */
1312 	/* Pad the FDT by a specified amount */
1313 	of_len = *of_size + CONFIG_SYS_FDT_PAD;
1314 
1315 	/* If fdt_high is set use it to select the relocation address */
1316 	fdt_high = getenv("fdt_high");
1317 	if (fdt_high) {
1318 		void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1319 
1320 		if (((ulong) desired_addr) == ~0UL) {
1321 			/* All ones means use fdt in place */
1322 			of_start = fdt_blob;
1323 			lmb_reserve(lmb, (ulong)of_start, of_len);
1324 			disable_relocation = 1;
1325 		} else if (desired_addr) {
1326 			of_start =
1327 			    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1328 							   (ulong)desired_addr);
1329 			if (of_start == NULL) {
1330 				puts("Failed using fdt_high value for Device Tree");
1331 				goto error;
1332 			}
1333 		} else {
1334 			of_start =
1335 			    (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
1336 		}
1337 	} else {
1338 		of_start =
1339 		    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1340 						   getenv_bootm_mapsize()
1341 						   + getenv_bootm_low());
1342 	}
1343 
1344 	if (of_start == NULL) {
1345 		puts("device tree - allocation error\n");
1346 		goto error;
1347 	}
1348 
1349 	if (disable_relocation) {
1350 		/* We assume there is space after the existing fdt to use for padding */
1351 		fdt_set_totalsize(of_start, of_len);
1352 		printf("   Using Device Tree in place at %p, end %p\n",
1353 		       of_start, of_start + of_len - 1);
1354 	} else {
1355 		debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1356 			fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1357 
1358 		printf("   Loading Device Tree to %p, end %p ... ",
1359 			of_start, of_start + of_len - 1);
1360 
1361 		err = fdt_open_into(fdt_blob, of_start, of_len);
1362 		if (err != 0) {
1363 			fdt_error("fdt move failed");
1364 			goto error;
1365 		}
1366 		puts("OK\n");
1367 	}
1368 
1369 	*of_flat_tree = of_start;
1370 	*of_size = of_len;
1371 
1372 	set_working_fdt_addr(*of_flat_tree);
1373 	return 0;
1374 
1375 error:
1376 	return 1;
1377 }
1378 #endif /* CONFIG_OF_LIBFDT */
1379 
1380 /**
1381  * boot_get_fdt - main fdt handling routine
1382  * @argc: command argument count
1383  * @argv: command argument list
1384  * @images: pointer to the bootm images structure
1385  * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1386  * @of_size: pointer to a ulong variable, will hold fdt length
1387  *
1388  * boot_get_fdt() is responsible for finding a valid flat device tree image.
1389  * Curently supported are the following ramdisk sources:
1390  *      - multicomponent kernel/ramdisk image,
1391  *      - commandline provided address of decicated ramdisk image.
1392  *
1393  * returns:
1394  *     0, if fdt image was found and valid, or skipped
1395  *     of_flat_tree and of_size are set to fdt start address and length if
1396  *     fdt image is found and valid
1397  *
1398  *     1, if fdt image is found but corrupted
1399  *     of_flat_tree and of_size are set to 0 if no fdt exists
1400  */
1401 int boot_get_fdt(int flag, int argc, char * const argv[],
1402 		bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
1403 {
1404 	const image_header_t *fdt_hdr;
1405 	ulong		fdt_addr;
1406 	char		*fdt_blob = NULL;
1407 	ulong		image_start, image_data, image_end;
1408 	ulong		load_start, load_end;
1409 #if defined(CONFIG_FIT)
1410 	void		*fit_hdr;
1411 	const char	*fit_uname_config = NULL;
1412 	const char	*fit_uname_fdt = NULL;
1413 	ulong		default_addr;
1414 	int		cfg_noffset;
1415 	int		fdt_noffset;
1416 	const void	*data;
1417 	size_t		size;
1418 #endif
1419 
1420 	*of_flat_tree = NULL;
1421 	*of_size = 0;
1422 
1423 	if (argc > 3 || genimg_has_config(images)) {
1424 #if defined(CONFIG_FIT)
1425 		if (argc > 3) {
1426 			/*
1427 			 * If the FDT blob comes from the FIT image and the
1428 			 * FIT image address is omitted in the command line
1429 			 * argument, try to use ramdisk or os FIT image
1430 			 * address or default load address.
1431 			 */
1432 			if (images->fit_uname_rd)
1433 				default_addr = (ulong)images->fit_hdr_rd;
1434 			else if (images->fit_uname_os)
1435 				default_addr = (ulong)images->fit_hdr_os;
1436 			else
1437 				default_addr = load_addr;
1438 
1439 			if (fit_parse_conf(argv[3], default_addr,
1440 						&fdt_addr, &fit_uname_config)) {
1441 				debug("*  fdt: config '%s' from image at "
1442 						"0x%08lx\n",
1443 						fit_uname_config, fdt_addr);
1444 			} else if (fit_parse_subimage(argv[3], default_addr,
1445 						&fdt_addr, &fit_uname_fdt)) {
1446 				debug("*  fdt: subimage '%s' from image at "
1447 						"0x%08lx\n",
1448 						fit_uname_fdt, fdt_addr);
1449 			} else
1450 #endif
1451 			{
1452 				fdt_addr = simple_strtoul(argv[3], NULL, 16);
1453 				debug("*  fdt: cmdline image address = "
1454 						"0x%08lx\n",
1455 						fdt_addr);
1456 			}
1457 #if defined(CONFIG_FIT)
1458 		} else {
1459 			/* use FIT configuration provided in first bootm
1460 			 * command argument
1461 			 */
1462 			fdt_addr = (ulong)images->fit_hdr_os;
1463 			fit_uname_config = images->fit_uname_cfg;
1464 			debug("*  fdt: using config '%s' from image "
1465 					"at 0x%08lx\n",
1466 					fit_uname_config, fdt_addr);
1467 
1468 			/*
1469 			 * Check whether configuration has FDT blob defined,
1470 			 * if not quit silently.
1471 			 */
1472 			fit_hdr = (void *)fdt_addr;
1473 			cfg_noffset = fit_conf_get_node(fit_hdr,
1474 					fit_uname_config);
1475 			if (cfg_noffset < 0) {
1476 				debug("*  fdt: no such config\n");
1477 				return 0;
1478 			}
1479 
1480 			fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
1481 					cfg_noffset);
1482 			if (fdt_noffset < 0) {
1483 				debug("*  fdt: no fdt in config\n");
1484 				return 0;
1485 			}
1486 		}
1487 #endif
1488 
1489 		debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1490 				fdt_addr);
1491 
1492 		/* copy from dataflash if needed */
1493 		fdt_addr = genimg_get_image(fdt_addr);
1494 
1495 		/*
1496 		 * Check if there is an FDT image at the
1497 		 * address provided in the second bootm argument
1498 		 * check image type, for FIT images get a FIT node.
1499 		 */
1500 		switch (genimg_get_format((void *)fdt_addr)) {
1501 		case IMAGE_FORMAT_LEGACY:
1502 			/* verify fdt_addr points to a valid image header */
1503 			printf("## Flattened Device Tree from Legacy Image "
1504 					"at %08lx\n",
1505 					fdt_addr);
1506 			fdt_hdr = image_get_fdt(fdt_addr);
1507 			if (!fdt_hdr)
1508 				goto error;
1509 
1510 			/*
1511 			 * move image data to the load address,
1512 			 * make sure we don't overwrite initial image
1513 			 */
1514 			image_start = (ulong)fdt_hdr;
1515 			image_data = (ulong)image_get_data(fdt_hdr);
1516 			image_end = image_get_image_end(fdt_hdr);
1517 
1518 			load_start = image_get_load(fdt_hdr);
1519 			load_end = load_start + image_get_data_size(fdt_hdr);
1520 
1521 			if (load_start == image_start ||
1522 			    load_start == image_data) {
1523 				fdt_blob = (char *)image_data;
1524 				break;
1525 			}
1526 
1527 			if ((load_start < image_end) && (load_end > image_start)) {
1528 				fdt_error("fdt overwritten");
1529 				goto error;
1530 			}
1531 
1532 			debug("   Loading FDT from 0x%08lx to 0x%08lx\n",
1533 					image_data, load_start);
1534 
1535 			memmove((void *)load_start,
1536 					(void *)image_data,
1537 					image_get_data_size(fdt_hdr));
1538 
1539 			fdt_blob = (char *)load_start;
1540 			break;
1541 		case IMAGE_FORMAT_FIT:
1542 			/*
1543 			 * This case will catch both: new uImage format
1544 			 * (libfdt based) and raw FDT blob (also libfdt
1545 			 * based).
1546 			 */
1547 #if defined(CONFIG_FIT)
1548 			/* check FDT blob vs FIT blob */
1549 			if (fit_check_format((const void *)fdt_addr)) {
1550 				/*
1551 				 * FIT image
1552 				 */
1553 				fit_hdr = (void *)fdt_addr;
1554 				printf("## Flattened Device Tree from FIT "
1555 						"Image at %08lx\n",
1556 						fdt_addr);
1557 
1558 				if (!fit_uname_fdt) {
1559 					/*
1560 					 * no FDT blob image node unit name,
1561 					 * try to get config node first. If
1562 					 * config unit node name is NULL
1563 					 * fit_conf_get_node() will try to
1564 					 * find default config node
1565 					 */
1566 					cfg_noffset = fit_conf_get_node(fit_hdr,
1567 							fit_uname_config);
1568 
1569 					if (cfg_noffset < 0) {
1570 						fdt_error("Could not find "
1571 							    "configuration "
1572 							    "node\n");
1573 						goto error;
1574 					}
1575 
1576 					fit_uname_config = fdt_get_name(fit_hdr,
1577 							cfg_noffset, NULL);
1578 					printf("   Using '%s' configuration\n",
1579 							fit_uname_config);
1580 
1581 					fdt_noffset = fit_conf_get_fdt_node(
1582 							fit_hdr,
1583 							cfg_noffset);
1584 					fit_uname_fdt = fit_get_name(fit_hdr,
1585 							fdt_noffset, NULL);
1586 				} else {
1587 					/* get FDT component image node offset */
1588 					fdt_noffset = fit_image_get_node(
1589 								fit_hdr,
1590 								fit_uname_fdt);
1591 				}
1592 				if (fdt_noffset < 0) {
1593 					fdt_error("Could not find subimage "
1594 							"node\n");
1595 					goto error;
1596 				}
1597 
1598 				printf("   Trying '%s' FDT blob subimage\n",
1599 						fit_uname_fdt);
1600 
1601 				if (!fit_check_fdt(fit_hdr, fdt_noffset,
1602 							images->verify))
1603 					goto error;
1604 
1605 				/* get ramdisk image data address and length */
1606 				if (fit_image_get_data(fit_hdr, fdt_noffset,
1607 							&data, &size)) {
1608 					fdt_error("Could not find FDT "
1609 							"subimage data");
1610 					goto error;
1611 				}
1612 
1613 				/* verift that image data is a proper FDT blob */
1614 				if (fdt_check_header((char *)data) != 0) {
1615 					fdt_error("Subimage data is not a FTD");
1616 					goto error;
1617 				}
1618 
1619 				/*
1620 				 * move image data to the load address,
1621 				 * make sure we don't overwrite initial image
1622 				 */
1623 				image_start = (ulong)fit_hdr;
1624 				image_end = fit_get_end(fit_hdr);
1625 
1626 				if (fit_image_get_load(fit_hdr, fdt_noffset,
1627 							&load_start) == 0) {
1628 					load_end = load_start + size;
1629 
1630 					if ((load_start < image_end) &&
1631 							(load_end > image_start)) {
1632 						fdt_error("FDT overwritten");
1633 						goto error;
1634 					}
1635 
1636 					printf("   Loading FDT from 0x%08lx "
1637 							"to 0x%08lx\n",
1638 							(ulong)data,
1639 							load_start);
1640 
1641 					memmove((void *)load_start,
1642 							(void *)data, size);
1643 
1644 					fdt_blob = (char *)load_start;
1645 				} else {
1646 					fdt_blob = (char *)data;
1647 				}
1648 
1649 				images->fit_hdr_fdt = fit_hdr;
1650 				images->fit_uname_fdt = fit_uname_fdt;
1651 				images->fit_noffset_fdt = fdt_noffset;
1652 				break;
1653 			} else
1654 #endif
1655 			{
1656 				/*
1657 				 * FDT blob
1658 				 */
1659 				fdt_blob = (char *)fdt_addr;
1660 				debug("*  fdt: raw FDT blob\n");
1661 				printf("## Flattened Device Tree blob at "
1662 					"%08lx\n", (long)fdt_blob);
1663 			}
1664 			break;
1665 		default:
1666 			puts("ERROR: Did not find a cmdline Flattened Device "
1667 				"Tree\n");
1668 			goto error;
1669 		}
1670 
1671 		printf("   Booting using the fdt blob at 0x%p\n", fdt_blob);
1672 
1673 	} else if (images->legacy_hdr_valid &&
1674 			image_check_type(&images->legacy_hdr_os_copy,
1675 						IH_TYPE_MULTI)) {
1676 
1677 		ulong fdt_data, fdt_len;
1678 
1679 		/*
1680 		 * Now check if we have a legacy multi-component image,
1681 		 * get second entry data start address and len.
1682 		 */
1683 		printf("## Flattened Device Tree from multi "
1684 			"component Image at %08lX\n",
1685 			(ulong)images->legacy_hdr_os);
1686 
1687 		image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1688 					&fdt_len);
1689 		if (fdt_len) {
1690 
1691 			fdt_blob = (char *)fdt_data;
1692 			printf("   Booting using the fdt at 0x%p\n", fdt_blob);
1693 
1694 			if (fdt_check_header(fdt_blob) != 0) {
1695 				fdt_error("image is not a fdt");
1696 				goto error;
1697 			}
1698 
1699 			if (fdt_totalsize(fdt_blob) != fdt_len) {
1700 				fdt_error("fdt size != image size");
1701 				goto error;
1702 			}
1703 		} else {
1704 			debug("## No Flattened Device Tree\n");
1705 			return 0;
1706 		}
1707 	} else {
1708 		debug("## No Flattened Device Tree\n");
1709 		return 0;
1710 	}
1711 
1712 	*of_flat_tree = fdt_blob;
1713 	*of_size = fdt_totalsize(fdt_blob);
1714 	debug("   of_flat_tree at 0x%08lx size 0x%08lx\n",
1715 			(ulong)*of_flat_tree, *of_size);
1716 
1717 	return 0;
1718 
1719 error:
1720 	*of_flat_tree = NULL;
1721 	*of_size = 0;
1722 	return 1;
1723 }
1724 #endif /* CONFIG_OF_LIBFDT */
1725 
1726 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1727 /**
1728  * boot_get_cmdline - allocate and initialize kernel cmdline
1729  * @lmb: pointer to lmb handle, will be used for memory mgmt
1730  * @cmd_start: pointer to a ulong variable, will hold cmdline start
1731  * @cmd_end: pointer to a ulong variable, will hold cmdline end
1732  *
1733  * boot_get_cmdline() allocates space for kernel command line below
1734  * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1735  * variable is present its contents is copied to allocated kernel
1736  * command line.
1737  *
1738  * returns:
1739  *      0 - success
1740  *     -1 - failure
1741  */
1742 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1743 {
1744 	char *cmdline;
1745 	char *s;
1746 
1747 	cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1748 				getenv_bootm_mapsize() + getenv_bootm_low());
1749 
1750 	if (cmdline == NULL)
1751 		return -1;
1752 
1753 	if ((s = getenv("bootargs")) == NULL)
1754 		s = "";
1755 
1756 	strcpy(cmdline, s);
1757 
1758 	*cmd_start = (ulong) & cmdline[0];
1759 	*cmd_end = *cmd_start + strlen(cmdline);
1760 
1761 	debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1762 
1763 	return 0;
1764 }
1765 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1766 
1767 #ifdef CONFIG_SYS_BOOT_GET_KBD
1768 /**
1769  * boot_get_kbd - allocate and initialize kernel copy of board info
1770  * @lmb: pointer to lmb handle, will be used for memory mgmt
1771  * @kbd: double pointer to board info data
1772  *
1773  * boot_get_kbd() allocates space for kernel copy of board info data below
1774  * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1775  * with the current u-boot board info data.
1776  *
1777  * returns:
1778  *      0 - success
1779  *     -1 - failure
1780  */
1781 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1782 {
1783 	*kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1784 				getenv_bootm_mapsize() + getenv_bootm_low());
1785 	if (*kbd == NULL)
1786 		return -1;
1787 
1788 	**kbd = *(gd->bd);
1789 
1790 	debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1791 
1792 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1793 	do_bdinfo(NULL, 0, 0, NULL);
1794 #endif
1795 
1796 	return 0;
1797 }
1798 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1799 #endif /* !USE_HOSTCC */
1800 
1801 #if defined(CONFIG_FIT)
1802 /*****************************************************************************/
1803 /* New uImage format routines */
1804 /*****************************************************************************/
1805 #ifndef USE_HOSTCC
1806 static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
1807 		ulong *addr, const char **name)
1808 {
1809 	const char *sep;
1810 
1811 	*addr = addr_curr;
1812 	*name = NULL;
1813 
1814 	sep = strchr(spec, sepc);
1815 	if (sep) {
1816 		if (sep - spec > 0)
1817 			*addr = simple_strtoul(spec, NULL, 16);
1818 
1819 		*name = sep + 1;
1820 		return 1;
1821 	}
1822 
1823 	return 0;
1824 }
1825 
1826 /**
1827  * fit_parse_conf - parse FIT configuration spec
1828  * @spec: input string, containing configuration spec
1829  * @add_curr: current image address (to be used as a possible default)
1830  * @addr: pointer to a ulong variable, will hold FIT image address of a given
1831  * configuration
1832  * @conf_name double pointer to a char, will hold pointer to a configuration
1833  * unit name
1834  *
1835  * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1836  * where <addr> is a FIT image address that contains configuration
1837  * with a <conf> unit name.
1838  *
1839  * Address part is optional, and if omitted default add_curr will
1840  * be used instead.
1841  *
1842  * returns:
1843  *     1 if spec is a valid configuration string,
1844  *     addr and conf_name are set accordingly
1845  *     0 otherwise
1846  */
1847 int fit_parse_conf(const char *spec, ulong addr_curr,
1848 		ulong *addr, const char **conf_name)
1849 {
1850 	return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
1851 }
1852 
1853 /**
1854  * fit_parse_subimage - parse FIT subimage spec
1855  * @spec: input string, containing subimage spec
1856  * @add_curr: current image address (to be used as a possible default)
1857  * @addr: pointer to a ulong variable, will hold FIT image address of a given
1858  * subimage
1859  * @image_name: double pointer to a char, will hold pointer to a subimage name
1860  *
1861  * fit_parse_subimage() expects subimage spec in the for of
1862  * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1863  * subimage with a <subimg> unit name.
1864  *
1865  * Address part is optional, and if omitted default add_curr will
1866  * be used instead.
1867  *
1868  * returns:
1869  *     1 if spec is a valid subimage string,
1870  *     addr and image_name are set accordingly
1871  *     0 otherwise
1872  */
1873 int fit_parse_subimage(const char *spec, ulong addr_curr,
1874 		ulong *addr, const char **image_name)
1875 {
1876 	return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
1877 }
1878 #endif /* !USE_HOSTCC */
1879 
1880 static void fit_get_debug(const void *fit, int noffset,
1881 		char *prop_name, int err)
1882 {
1883 	debug("Can't get '%s' property from FIT 0x%08lx, "
1884 		"node: offset %d, name %s (%s)\n",
1885 		prop_name, (ulong)fit, noffset,
1886 		fit_get_name(fit, noffset, NULL),
1887 		fdt_strerror(err));
1888 }
1889 
1890 /**
1891  * fit_print_contents - prints out the contents of the FIT format image
1892  * @fit: pointer to the FIT format image header
1893  * @p: pointer to prefix string
1894  *
1895  * fit_print_contents() formats a multi line FIT image contents description.
1896  * The routine prints out FIT image properties (root node level) follwed by
1897  * the details of each component image.
1898  *
1899  * returns:
1900  *     no returned results
1901  */
1902 void fit_print_contents(const void *fit)
1903 {
1904 	char *desc;
1905 	char *uname;
1906 	int images_noffset;
1907 	int confs_noffset;
1908 	int noffset;
1909 	int ndepth;
1910 	int count = 0;
1911 	int ret;
1912 	const char *p;
1913 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1914 	time_t timestamp;
1915 #endif
1916 
1917 #ifdef USE_HOSTCC
1918 	p = "";
1919 #else
1920 	p = "   ";
1921 #endif
1922 
1923 	/* Root node properties */
1924 	ret = fit_get_desc(fit, 0, &desc);
1925 	printf("%sFIT description: ", p);
1926 	if (ret)
1927 		printf("unavailable\n");
1928 	else
1929 		printf("%s\n", desc);
1930 
1931 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1932 	ret = fit_get_timestamp(fit, 0, &timestamp);
1933 	printf("%sCreated:         ", p);
1934 	if (ret)
1935 		printf("unavailable\n");
1936 	else
1937 		genimg_print_time(timestamp);
1938 #endif
1939 
1940 	/* Find images parent node offset */
1941 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1942 	if (images_noffset < 0) {
1943 		printf("Can't find images parent node '%s' (%s)\n",
1944 			FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1945 		return;
1946 	}
1947 
1948 	/* Process its subnodes, print out component images details */
1949 	for (ndepth = 0, count = 0,
1950 		noffset = fdt_next_node(fit, images_noffset, &ndepth);
1951 	     (noffset >= 0) && (ndepth > 0);
1952 	     noffset = fdt_next_node(fit, noffset, &ndepth)) {
1953 		if (ndepth == 1) {
1954 			/*
1955 			 * Direct child node of the images parent node,
1956 			 * i.e. component image node.
1957 			 */
1958 			printf("%s Image %u (%s)\n", p, count++,
1959 					fit_get_name(fit, noffset, NULL));
1960 
1961 			fit_image_print(fit, noffset, p);
1962 		}
1963 	}
1964 
1965 	/* Find configurations parent node offset */
1966 	confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
1967 	if (confs_noffset < 0) {
1968 		debug("Can't get configurations parent node '%s' (%s)\n",
1969 			FIT_CONFS_PATH, fdt_strerror(confs_noffset));
1970 		return;
1971 	}
1972 
1973 	/* get default configuration unit name from default property */
1974 	uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
1975 	if (uname)
1976 		printf("%s Default Configuration: '%s'\n", p, uname);
1977 
1978 	/* Process its subnodes, print out configurations details */
1979 	for (ndepth = 0, count = 0,
1980 		noffset = fdt_next_node(fit, confs_noffset, &ndepth);
1981 	     (noffset >= 0) && (ndepth > 0);
1982 	     noffset = fdt_next_node(fit, noffset, &ndepth)) {
1983 		if (ndepth == 1) {
1984 			/*
1985 			 * Direct child node of the configurations parent node,
1986 			 * i.e. configuration node.
1987 			 */
1988 			printf("%s Configuration %u (%s)\n", p, count++,
1989 					fit_get_name(fit, noffset, NULL));
1990 
1991 			fit_conf_print(fit, noffset, p);
1992 		}
1993 	}
1994 }
1995 
1996 /**
1997  * fit_image_print - prints out the FIT component image details
1998  * @fit: pointer to the FIT format image header
1999  * @image_noffset: offset of the component image node
2000  * @p: pointer to prefix string
2001  *
2002  * fit_image_print() lists all mandatory properies for the processed component
2003  * image. If present, hash nodes are printed out as well. Load
2004  * address for images of type firmware is also printed out. Since the load
2005  * address is not mandatory for firmware images, it will be output as
2006  * "unavailable" when not present.
2007  *
2008  * returns:
2009  *     no returned results
2010  */
2011 void fit_image_print(const void *fit, int image_noffset, const char *p)
2012 {
2013 	char *desc;
2014 	uint8_t type, arch, os, comp;
2015 	size_t size;
2016 	ulong load, entry;
2017 	const void *data;
2018 	int noffset;
2019 	int ndepth;
2020 	int ret;
2021 
2022 	/* Mandatory properties */
2023 	ret = fit_get_desc(fit, image_noffset, &desc);
2024 	printf("%s  Description:  ", p);
2025 	if (ret)
2026 		printf("unavailable\n");
2027 	else
2028 		printf("%s\n", desc);
2029 
2030 	fit_image_get_type(fit, image_noffset, &type);
2031 	printf("%s  Type:         %s\n", p, genimg_get_type_name(type));
2032 
2033 	fit_image_get_comp(fit, image_noffset, &comp);
2034 	printf("%s  Compression:  %s\n", p, genimg_get_comp_name(comp));
2035 
2036 	ret = fit_image_get_data(fit, image_noffset, &data, &size);
2037 
2038 #ifndef USE_HOSTCC
2039 	printf("%s  Data Start:   ", p);
2040 	if (ret)
2041 		printf("unavailable\n");
2042 	else
2043 		printf("0x%08lx\n", (ulong)data);
2044 #endif
2045 
2046 	printf("%s  Data Size:    ", p);
2047 	if (ret)
2048 		printf("unavailable\n");
2049 	else
2050 		genimg_print_size(size);
2051 
2052 	/* Remaining, type dependent properties */
2053 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2054 	    (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2055 	    (type == IH_TYPE_FLATDT)) {
2056 		fit_image_get_arch(fit, image_noffset, &arch);
2057 		printf("%s  Architecture: %s\n", p, genimg_get_arch_name(arch));
2058 	}
2059 
2060 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
2061 		fit_image_get_os(fit, image_noffset, &os);
2062 		printf("%s  OS:           %s\n", p, genimg_get_os_name(os));
2063 	}
2064 
2065 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2066 		(type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
2067 		ret = fit_image_get_load(fit, image_noffset, &load);
2068 		printf("%s  Load Address: ", p);
2069 		if (ret)
2070 			printf("unavailable\n");
2071 		else
2072 			printf("0x%08lx\n", load);
2073 	}
2074 
2075 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2076 		(type == IH_TYPE_RAMDISK)) {
2077 		fit_image_get_entry(fit, image_noffset, &entry);
2078 		printf("%s  Entry Point:  ", p);
2079 		if (ret)
2080 			printf("unavailable\n");
2081 		else
2082 			printf("0x%08lx\n", entry);
2083 	}
2084 
2085 	/* Process all hash subnodes of the component image node */
2086 	for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2087 	     (noffset >= 0) && (ndepth > 0);
2088 	     noffset = fdt_next_node(fit, noffset, &ndepth)) {
2089 		if (ndepth == 1) {
2090 			/* Direct child node of the component image node */
2091 			fit_image_print_hash(fit, noffset, p);
2092 		}
2093 	}
2094 }
2095 
2096 /**
2097  * fit_image_print_hash - prints out the hash node details
2098  * @fit: pointer to the FIT format image header
2099  * @noffset: offset of the hash node
2100  * @p: pointer to prefix string
2101  *
2102  * fit_image_print_hash() lists properies for the processed hash node
2103  *
2104  * returns:
2105  *     no returned results
2106  */
2107 void fit_image_print_hash(const void *fit, int noffset, const char *p)
2108 {
2109 	char *algo;
2110 	uint8_t *value;
2111 	int value_len;
2112 	int i, ret;
2113 
2114 	/*
2115 	 * Check subnode name, must be equal to "hash".
2116 	 * Multiple hash nodes require unique unit node
2117 	 * names, e.g. hash@1, hash@2, etc.
2118 	 */
2119 	if (strncmp(fit_get_name(fit, noffset, NULL),
2120 			FIT_HASH_NODENAME,
2121 			strlen(FIT_HASH_NODENAME)) != 0)
2122 		return;
2123 
2124 	debug("%s  Hash node:    '%s'\n", p,
2125 			fit_get_name(fit, noffset, NULL));
2126 
2127 	printf("%s  Hash algo:    ", p);
2128 	if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2129 		printf("invalid/unsupported\n");
2130 		return;
2131 	}
2132 	printf("%s\n", algo);
2133 
2134 	ret = fit_image_hash_get_value(fit, noffset, &value,
2135 					&value_len);
2136 	printf("%s  Hash value:   ", p);
2137 	if (ret) {
2138 		printf("unavailable\n");
2139 	} else {
2140 		for (i = 0; i < value_len; i++)
2141 			printf("%02x", value[i]);
2142 		printf("\n");
2143 	}
2144 
2145 	debug("%s  Hash len:     %d\n", p, value_len);
2146 }
2147 
2148 /**
2149  * fit_get_desc - get node description property
2150  * @fit: pointer to the FIT format image header
2151  * @noffset: node offset
2152  * @desc: double pointer to the char, will hold pointer to the descrption
2153  *
2154  * fit_get_desc() reads description property from a given node, if
2155  * description is found pointer to it is returened in third call argument.
2156  *
2157  * returns:
2158  *     0, on success
2159  *     -1, on failure
2160  */
2161 int fit_get_desc(const void *fit, int noffset, char **desc)
2162 {
2163 	int len;
2164 
2165 	*desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
2166 	if (*desc == NULL) {
2167 		fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
2168 		return -1;
2169 	}
2170 
2171 	return 0;
2172 }
2173 
2174 /**
2175  * fit_get_timestamp - get node timestamp property
2176  * @fit: pointer to the FIT format image header
2177  * @noffset: node offset
2178  * @timestamp: pointer to the time_t, will hold read timestamp
2179  *
2180  * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2181  * is found and has a correct size its value is retured in third call
2182  * argument.
2183  *
2184  * returns:
2185  *     0, on success
2186  *     -1, on property read failure
2187  *     -2, on wrong timestamp size
2188  */
2189 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
2190 {
2191 	int len;
2192 	const void *data;
2193 
2194 	data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
2195 	if (data == NULL) {
2196 		fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
2197 		return -1;
2198 	}
2199 	if (len != sizeof(uint32_t)) {
2200 		debug("FIT timestamp with incorrect size of (%u)\n", len);
2201 		return -2;
2202 	}
2203 
2204 	*timestamp = uimage_to_cpu(*((uint32_t *)data));
2205 	return 0;
2206 }
2207 
2208 /**
2209  * fit_image_get_node - get node offset for component image of a given unit name
2210  * @fit: pointer to the FIT format image header
2211  * @image_uname: component image node unit name
2212  *
2213  * fit_image_get_node() finds a component image (withing the '/images'
2214  * node) of a provided unit name. If image is found its node offset is
2215  * returned to the caller.
2216  *
2217  * returns:
2218  *     image node offset when found (>=0)
2219  *     negative number on failure (FDT_ERR_* code)
2220  */
2221 int fit_image_get_node(const void *fit, const char *image_uname)
2222 {
2223 	int noffset, images_noffset;
2224 
2225 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2226 	if (images_noffset < 0) {
2227 		debug("Can't find images parent node '%s' (%s)\n",
2228 			FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2229 		return images_noffset;
2230 	}
2231 
2232 	noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
2233 	if (noffset < 0) {
2234 		debug("Can't get node offset for image unit name: '%s' (%s)\n",
2235 			image_uname, fdt_strerror(noffset));
2236 	}
2237 
2238 	return noffset;
2239 }
2240 
2241 /**
2242  * fit_image_get_os - get os id for a given component image node
2243  * @fit: pointer to the FIT format image header
2244  * @noffset: component image node offset
2245  * @os: pointer to the uint8_t, will hold os numeric id
2246  *
2247  * fit_image_get_os() finds os property in a given component image node.
2248  * If the property is found, its (string) value is translated to the numeric
2249  * id which is returned to the caller.
2250  *
2251  * returns:
2252  *     0, on success
2253  *     -1, on failure
2254  */
2255 int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
2256 {
2257 	int len;
2258 	const void *data;
2259 
2260 	/* Get OS name from property data */
2261 	data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
2262 	if (data == NULL) {
2263 		fit_get_debug(fit, noffset, FIT_OS_PROP, len);
2264 		*os = -1;
2265 		return -1;
2266 	}
2267 
2268 	/* Translate OS name to id */
2269 	*os = genimg_get_os_id(data);
2270 	return 0;
2271 }
2272 
2273 /**
2274  * fit_image_get_arch - get arch id for a given component image node
2275  * @fit: pointer to the FIT format image header
2276  * @noffset: component image node offset
2277  * @arch: pointer to the uint8_t, will hold arch numeric id
2278  *
2279  * fit_image_get_arch() finds arch property in a given component image node.
2280  * If the property is found, its (string) value is translated to the numeric
2281  * id which is returned to the caller.
2282  *
2283  * returns:
2284  *     0, on success
2285  *     -1, on failure
2286  */
2287 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
2288 {
2289 	int len;
2290 	const void *data;
2291 
2292 	/* Get architecture name from property data */
2293 	data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
2294 	if (data == NULL) {
2295 		fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
2296 		*arch = -1;
2297 		return -1;
2298 	}
2299 
2300 	/* Translate architecture name to id */
2301 	*arch = genimg_get_arch_id(data);
2302 	return 0;
2303 }
2304 
2305 /**
2306  * fit_image_get_type - get type id for a given component image node
2307  * @fit: pointer to the FIT format image header
2308  * @noffset: component image node offset
2309  * @type: pointer to the uint8_t, will hold type numeric id
2310  *
2311  * fit_image_get_type() finds type property in a given component image node.
2312  * If the property is found, its (string) value is translated to the numeric
2313  * id which is returned to the caller.
2314  *
2315  * returns:
2316  *     0, on success
2317  *     -1, on failure
2318  */
2319 int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
2320 {
2321 	int len;
2322 	const void *data;
2323 
2324 	/* Get image type name from property data */
2325 	data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
2326 	if (data == NULL) {
2327 		fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
2328 		*type = -1;
2329 		return -1;
2330 	}
2331 
2332 	/* Translate image type name to id */
2333 	*type = genimg_get_type_id(data);
2334 	return 0;
2335 }
2336 
2337 /**
2338  * fit_image_get_comp - get comp id for a given component image node
2339  * @fit: pointer to the FIT format image header
2340  * @noffset: component image node offset
2341  * @comp: pointer to the uint8_t, will hold comp numeric id
2342  *
2343  * fit_image_get_comp() finds comp property in a given component image node.
2344  * If the property is found, its (string) value is translated to the numeric
2345  * id which is returned to the caller.
2346  *
2347  * returns:
2348  *     0, on success
2349  *     -1, on failure
2350  */
2351 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
2352 {
2353 	int len;
2354 	const void *data;
2355 
2356 	/* Get compression name from property data */
2357 	data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
2358 	if (data == NULL) {
2359 		fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
2360 		*comp = -1;
2361 		return -1;
2362 	}
2363 
2364 	/* Translate compression name to id */
2365 	*comp = genimg_get_comp_id(data);
2366 	return 0;
2367 }
2368 
2369 /**
2370  * fit_image_get_load - get load address property for a given component image node
2371  * @fit: pointer to the FIT format image header
2372  * @noffset: component image node offset
2373  * @load: pointer to the uint32_t, will hold load address
2374  *
2375  * fit_image_get_load() finds load address property in a given component image node.
2376  * If the property is found, its value is returned to the caller.
2377  *
2378  * returns:
2379  *     0, on success
2380  *     -1, on failure
2381  */
2382 int fit_image_get_load(const void *fit, int noffset, ulong *load)
2383 {
2384 	int len;
2385 	const uint32_t *data;
2386 
2387 	data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
2388 	if (data == NULL) {
2389 		fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
2390 		return -1;
2391 	}
2392 
2393 	*load = uimage_to_cpu(*data);
2394 	return 0;
2395 }
2396 
2397 /**
2398  * fit_image_get_entry - get entry point address property for a given component image node
2399  * @fit: pointer to the FIT format image header
2400  * @noffset: component image node offset
2401  * @entry: pointer to the uint32_t, will hold entry point address
2402  *
2403  * fit_image_get_entry() finds entry point address property in a given component image node.
2404  * If the property is found, its value is returned to the caller.
2405  *
2406  * returns:
2407  *     0, on success
2408  *     -1, on failure
2409  */
2410 int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
2411 {
2412 	int len;
2413 	const uint32_t *data;
2414 
2415 	data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
2416 	if (data == NULL) {
2417 		fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
2418 		return -1;
2419 	}
2420 
2421 	*entry = uimage_to_cpu(*data);
2422 	return 0;
2423 }
2424 
2425 /**
2426  * fit_image_get_data - get data property and its size for a given component image node
2427  * @fit: pointer to the FIT format image header
2428  * @noffset: component image node offset
2429  * @data: double pointer to void, will hold data property's data address
2430  * @size: pointer to size_t, will hold data property's data size
2431  *
2432  * fit_image_get_data() finds data property in a given component image node.
2433  * If the property is found its data start address and size are returned to
2434  * the caller.
2435  *
2436  * returns:
2437  *     0, on success
2438  *     -1, on failure
2439  */
2440 int fit_image_get_data(const void *fit, int noffset,
2441 		const void **data, size_t *size)
2442 {
2443 	int len;
2444 
2445 	*data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
2446 	if (*data == NULL) {
2447 		fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
2448 		*size = 0;
2449 		return -1;
2450 	}
2451 
2452 	*size = len;
2453 	return 0;
2454 }
2455 
2456 /**
2457  * fit_image_hash_get_algo - get hash algorithm name
2458  * @fit: pointer to the FIT format image header
2459  * @noffset: hash node offset
2460  * @algo: double pointer to char, will hold pointer to the algorithm name
2461  *
2462  * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2463  * If the property is found its data start address is returned to the caller.
2464  *
2465  * returns:
2466  *     0, on success
2467  *     -1, on failure
2468  */
2469 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
2470 {
2471 	int len;
2472 
2473 	*algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
2474 	if (*algo == NULL) {
2475 		fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
2476 		return -1;
2477 	}
2478 
2479 	return 0;
2480 }
2481 
2482 /**
2483  * fit_image_hash_get_value - get hash value and length
2484  * @fit: pointer to the FIT format image header
2485  * @noffset: hash node offset
2486  * @value: double pointer to uint8_t, will hold address of a hash value data
2487  * @value_len: pointer to an int, will hold hash data length
2488  *
2489  * fit_image_hash_get_value() finds hash value property in a given hash node.
2490  * If the property is found its data start address and size are returned to
2491  * the caller.
2492  *
2493  * returns:
2494  *     0, on success
2495  *     -1, on failure
2496  */
2497 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
2498 				int *value_len)
2499 {
2500 	int len;
2501 
2502 	*value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
2503 	if (*value == NULL) {
2504 		fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
2505 		*value_len = 0;
2506 		return -1;
2507 	}
2508 
2509 	*value_len = len;
2510 	return 0;
2511 }
2512 
2513 #ifndef USE_HOSTCC
2514 /**
2515  * fit_image_hash_get_ignore - get hash ignore flag
2516  * @fit: pointer to the FIT format image header
2517  * @noffset: hash node offset
2518  * @ignore: pointer to an int, will hold hash ignore flag
2519  *
2520  * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
2521  * If the property is found and non-zero, the hash algorithm is not verified by
2522  * u-boot automatically.
2523  *
2524  * returns:
2525  *     0, on ignore not found
2526  *     value, on ignore found
2527  */
2528 int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
2529 {
2530 	int len;
2531 	int *value;
2532 
2533 	value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
2534 	if (value == NULL || len != sizeof(int))
2535 		*ignore = 0;
2536 	else
2537 		*ignore = *value;
2538 
2539 	return 0;
2540 }
2541 #endif
2542 
2543 /**
2544  * fit_set_timestamp - set node timestamp property
2545  * @fit: pointer to the FIT format image header
2546  * @noffset: node offset
2547  * @timestamp: timestamp value to be set
2548  *
2549  * fit_set_timestamp() attempts to set timestamp property in the requested
2550  * node and returns operation status to the caller.
2551  *
2552  * returns:
2553  *     0, on success
2554  *     -1, on property read failure
2555  */
2556 int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
2557 {
2558 	uint32_t t;
2559 	int ret;
2560 
2561 	t = cpu_to_uimage(timestamp);
2562 	ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2563 				sizeof(uint32_t));
2564 	if (ret) {
2565 		printf("Can't set '%s' property for '%s' node (%s)\n",
2566 			FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2567 			fdt_strerror(ret));
2568 		return -1;
2569 	}
2570 
2571 	return 0;
2572 }
2573 
2574 /**
2575  * calculate_hash - calculate and return hash for provided input data
2576  * @data: pointer to the input data
2577  * @data_len: data length
2578  * @algo: requested hash algorithm
2579  * @value: pointer to the char, will hold hash value data (caller must
2580  * allocate enough free space)
2581  * value_len: length of the calculated hash
2582  *
2583  * calculate_hash() computes input data hash according to the requested algorithm.
2584  * Resulting hash value is placed in caller provided 'value' buffer, length
2585  * of the calculated hash is returned via value_len pointer argument.
2586  *
2587  * returns:
2588  *     0, on success
2589  *    -1, when algo is unsupported
2590  */
2591 static int calculate_hash(const void *data, int data_len, const char *algo,
2592 			uint8_t *value, int *value_len)
2593 {
2594 	if (strcmp(algo, "crc32") == 0) {
2595 		*((uint32_t *)value) = crc32_wd(0, data, data_len,
2596 							CHUNKSZ_CRC32);
2597 		*((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
2598 		*value_len = 4;
2599 	} else if (strcmp(algo, "sha1") == 0) {
2600 		sha1_csum_wd((unsigned char *) data, data_len,
2601 				(unsigned char *) value, CHUNKSZ_SHA1);
2602 		*value_len = 20;
2603 	} else if (strcmp(algo, "md5") == 0) {
2604 		md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2605 		*value_len = 16;
2606 	} else {
2607 		debug("Unsupported hash alogrithm\n");
2608 		return -1;
2609 	}
2610 	return 0;
2611 }
2612 
2613 #ifdef USE_HOSTCC
2614 /**
2615  * fit_set_hashes - process FIT component image nodes and calculate hashes
2616  * @fit: pointer to the FIT format image header
2617  *
2618  * fit_set_hashes() adds hash values for all component images in the FIT blob.
2619  * Hashes are calculated for all component images which have hash subnodes
2620  * with algorithm property set to one of the supported hash algorithms.
2621  *
2622  * returns
2623  *     0, on success
2624  *     libfdt error code, on failure
2625  */
2626 int fit_set_hashes(void *fit)
2627 {
2628 	int images_noffset;
2629 	int noffset;
2630 	int ndepth;
2631 	int ret;
2632 
2633 	/* Find images parent node offset */
2634 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2635 	if (images_noffset < 0) {
2636 		printf("Can't find images parent node '%s' (%s)\n",
2637 			FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2638 		return images_noffset;
2639 	}
2640 
2641 	/* Process its subnodes, print out component images details */
2642 	for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
2643 	     (noffset >= 0) && (ndepth > 0);
2644 	     noffset = fdt_next_node(fit, noffset, &ndepth)) {
2645 		if (ndepth == 1) {
2646 			/*
2647 			 * Direct child node of the images parent node,
2648 			 * i.e. component image node.
2649 			 */
2650 			ret = fit_image_set_hashes(fit, noffset);
2651 			if (ret)
2652 				return ret;
2653 		}
2654 	}
2655 
2656 	return 0;
2657 }
2658 
2659 /**
2660  * fit_image_set_hashes - calculate/set hashes for given component image node
2661  * @fit: pointer to the FIT format image header
2662  * @image_noffset: requested component image node
2663  *
2664  * fit_image_set_hashes() adds hash values for an component image node. All
2665  * existing hash subnodes are checked, if algorithm property is set to one of
2666  * the supported hash algorithms, hash value is computed and corresponding
2667  * hash node property is set, for example:
2668  *
2669  * Input component image node structure:
2670  *
2671  * o image@1 (at image_noffset)
2672  *   | - data = [binary data]
2673  *   o hash@1
2674  *     |- algo = "sha1"
2675  *
2676  * Output component image node structure:
2677  *
2678  * o image@1 (at image_noffset)
2679  *   | - data = [binary data]
2680  *   o hash@1
2681  *     |- algo = "sha1"
2682  *     |- value = sha1(data)
2683  *
2684  * returns:
2685  *     0 on sucess
2686  *    <0 on failure
2687  */
2688 int fit_image_set_hashes(void *fit, int image_noffset)
2689 {
2690 	const void *data;
2691 	size_t size;
2692 	char *algo;
2693 	uint8_t value[FIT_MAX_HASH_LEN];
2694 	int value_len;
2695 	int noffset;
2696 	int ndepth;
2697 
2698 	/* Get image data and data length */
2699 	if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2700 		printf("Can't get image data/size\n");
2701 		return -1;
2702 	}
2703 
2704 	/* Process all hash subnodes of the component image node */
2705 	for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2706 	     (noffset >= 0) && (ndepth > 0);
2707 	     noffset = fdt_next_node(fit, noffset, &ndepth)) {
2708 		if (ndepth == 1) {
2709 			/* Direct child node of the component image node */
2710 
2711 			/*
2712 			 * Check subnode name, must be equal to "hash".
2713 			 * Multiple hash nodes require unique unit node
2714 			 * names, e.g. hash@1, hash@2, etc.
2715 			 */
2716 			if (strncmp(fit_get_name(fit, noffset, NULL),
2717 						FIT_HASH_NODENAME,
2718 						strlen(FIT_HASH_NODENAME)) != 0) {
2719 				/* Not a hash subnode, skip it */
2720 				continue;
2721 			}
2722 
2723 			if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2724 				printf("Can't get hash algo property for "
2725 					"'%s' hash node in '%s' image node\n",
2726 					fit_get_name(fit, noffset, NULL),
2727 					fit_get_name(fit, image_noffset, NULL));
2728 				return -1;
2729 			}
2730 
2731 			if (calculate_hash(data, size, algo, value,
2732 						&value_len)) {
2733 				printf("Unsupported hash algorithm (%s) for "
2734 					"'%s' hash node in '%s' image node\n",
2735 					algo, fit_get_name(fit, noffset, NULL),
2736 					fit_get_name(fit, image_noffset,
2737 							NULL));
2738 				return -1;
2739 			}
2740 
2741 			if (fit_image_hash_set_value(fit, noffset, value,
2742 							value_len)) {
2743 				printf("Can't set hash value for "
2744 					"'%s' hash node in '%s' image node\n",
2745 					fit_get_name(fit, noffset, NULL),
2746 					fit_get_name(fit, image_noffset, NULL));
2747 				return -1;
2748 			}
2749 		}
2750 	}
2751 
2752 	return 0;
2753 }
2754 
2755 /**
2756  * fit_image_hash_set_value - set hash value in requested has node
2757  * @fit: pointer to the FIT format image header
2758  * @noffset: hash node offset
2759  * @value: hash value to be set
2760  * @value_len: hash value length
2761  *
2762  * fit_image_hash_set_value() attempts to set hash value in a node at offset
2763  * given and returns operation status to the caller.
2764  *
2765  * returns
2766  *     0, on success
2767  *     -1, on failure
2768  */
2769 int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
2770 				int value_len)
2771 {
2772 	int ret;
2773 
2774 	ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
2775 	if (ret) {
2776 		printf("Can't set hash '%s' property for '%s' node(%s)\n",
2777 			FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2778 			fdt_strerror(ret));
2779 		return -1;
2780 	}
2781 
2782 	return 0;
2783 }
2784 #endif /* USE_HOSTCC */
2785 
2786 /**
2787  * fit_image_check_hashes - verify data intergity
2788  * @fit: pointer to the FIT format image header
2789  * @image_noffset: component image node offset
2790  *
2791  * fit_image_check_hashes() goes over component image hash nodes,
2792  * re-calculates each data hash and compares with the value stored in hash
2793  * node.
2794  *
2795  * returns:
2796  *     1, if all hashes are valid
2797  *     0, otherwise (or on error)
2798  */
2799 int fit_image_check_hashes(const void *fit, int image_noffset)
2800 {
2801 	const void	*data;
2802 	size_t		size;
2803 	char		*algo;
2804 	uint8_t		*fit_value;
2805 	int		fit_value_len;
2806 #ifndef USE_HOSTCC
2807 	int		ignore;
2808 #endif
2809 	uint8_t		value[FIT_MAX_HASH_LEN];
2810 	int		value_len;
2811 	int		noffset;
2812 	int		ndepth;
2813 	char		*err_msg = "";
2814 
2815 	/* Get image data and data length */
2816 	if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2817 		printf("Can't get image data/size\n");
2818 		return 0;
2819 	}
2820 
2821 	/* Process all hash subnodes of the component image node */
2822 	for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
2823 	     (noffset >= 0) && (ndepth > 0);
2824 	     noffset = fdt_next_node(fit, noffset, &ndepth)) {
2825 		if (ndepth == 1) {
2826 			/* Direct child node of the component image node */
2827 
2828 			/*
2829 			 * Check subnode name, must be equal to "hash".
2830 			 * Multiple hash nodes require unique unit node
2831 			 * names, e.g. hash@1, hash@2, etc.
2832 			 */
2833 			if (strncmp(fit_get_name(fit, noffset, NULL),
2834 					FIT_HASH_NODENAME,
2835 					strlen(FIT_HASH_NODENAME)) != 0)
2836 				continue;
2837 
2838 			if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2839 				err_msg = " error!\nCan't get hash algo "
2840 						"property";
2841 				goto error;
2842 			}
2843 			printf("%s", algo);
2844 
2845 #ifndef USE_HOSTCC
2846 			fit_image_hash_get_ignore(fit, noffset, &ignore);
2847 			if (ignore) {
2848 				printf("-skipped ");
2849 				continue;
2850 			}
2851 #endif
2852 
2853 			if (fit_image_hash_get_value(fit, noffset, &fit_value,
2854 							&fit_value_len)) {
2855 				err_msg = " error!\nCan't get hash value "
2856 						"property";
2857 				goto error;
2858 			}
2859 
2860 			if (calculate_hash(data, size, algo, value,
2861 						&value_len)) {
2862 				err_msg = " error!\n"
2863 						"Unsupported hash algorithm";
2864 				goto error;
2865 			}
2866 
2867 			if (value_len != fit_value_len) {
2868 				err_msg = " error !\nBad hash value len";
2869 				goto error;
2870 			} else if (memcmp(value, fit_value, value_len) != 0) {
2871 				err_msg = " error!\nBad hash value";
2872 				goto error;
2873 			}
2874 			printf("+ ");
2875 		}
2876 	}
2877 
2878 	if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
2879 		err_msg = " error!\nCorrupted or truncated tree";
2880 		goto error;
2881 	}
2882 
2883 	return 1;
2884 
2885 error:
2886 	printf("%s for '%s' hash node in '%s' image node\n",
2887 			err_msg, fit_get_name(fit, noffset, NULL),
2888 			fit_get_name(fit, image_noffset, NULL));
2889 	return 0;
2890 }
2891 
2892 /**
2893  * fit_all_image_check_hashes - verify data intergity for all images
2894  * @fit: pointer to the FIT format image header
2895  *
2896  * fit_all_image_check_hashes() goes over all images in the FIT and
2897  * for every images checks if all it's hashes are valid.
2898  *
2899  * returns:
2900  *     1, if all hashes of all images are valid
2901  *     0, otherwise (or on error)
2902  */
2903 int fit_all_image_check_hashes(const void *fit)
2904 {
2905 	int images_noffset;
2906 	int noffset;
2907 	int ndepth;
2908 	int count;
2909 
2910 	/* Find images parent node offset */
2911 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
2912 	if (images_noffset < 0) {
2913 		printf("Can't find images parent node '%s' (%s)\n",
2914 			FIT_IMAGES_PATH, fdt_strerror(images_noffset));
2915 		return 0;
2916 	}
2917 
2918 	/* Process all image subnodes, check hashes for each */
2919 	printf("## Checking hash(es) for FIT Image at %08lx ...\n",
2920 		(ulong)fit);
2921 	for (ndepth = 0, count = 0,
2922 		noffset = fdt_next_node(fit, images_noffset, &ndepth);
2923 		(noffset >= 0) && (ndepth > 0);
2924 		noffset = fdt_next_node(fit, noffset, &ndepth)) {
2925 		if (ndepth == 1) {
2926 			/*
2927 			 * Direct child node of the images parent node,
2928 			 * i.e. component image node.
2929 			 */
2930 			printf("   Hash(es) for Image %u (%s): ", count++,
2931 					fit_get_name(fit, noffset, NULL));
2932 
2933 			if (!fit_image_check_hashes(fit, noffset))
2934 				return 0;
2935 			printf("\n");
2936 		}
2937 	}
2938 	return 1;
2939 }
2940 
2941 /**
2942  * fit_image_check_os - check whether image node is of a given os type
2943  * @fit: pointer to the FIT format image header
2944  * @noffset: component image node offset
2945  * @os: requested image os
2946  *
2947  * fit_image_check_os() reads image os property and compares its numeric
2948  * id with the requested os. Comparison result is returned to the caller.
2949  *
2950  * returns:
2951  *     1 if image is of given os type
2952  *     0 otherwise (or on error)
2953  */
2954 int fit_image_check_os(const void *fit, int noffset, uint8_t os)
2955 {
2956 	uint8_t image_os;
2957 
2958 	if (fit_image_get_os(fit, noffset, &image_os))
2959 		return 0;
2960 	return (os == image_os);
2961 }
2962 
2963 /**
2964  * fit_image_check_arch - check whether image node is of a given arch
2965  * @fit: pointer to the FIT format image header
2966  * @noffset: component image node offset
2967  * @arch: requested imagearch
2968  *
2969  * fit_image_check_arch() reads image arch property and compares its numeric
2970  * id with the requested arch. Comparison result is returned to the caller.
2971  *
2972  * returns:
2973  *     1 if image is of given arch
2974  *     0 otherwise (or on error)
2975  */
2976 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
2977 {
2978 	uint8_t image_arch;
2979 
2980 	if (fit_image_get_arch(fit, noffset, &image_arch))
2981 		return 0;
2982 	return (arch == image_arch);
2983 }
2984 
2985 /**
2986  * fit_image_check_type - check whether image node is of a given type
2987  * @fit: pointer to the FIT format image header
2988  * @noffset: component image node offset
2989  * @type: requested image type
2990  *
2991  * fit_image_check_type() reads image type property and compares its numeric
2992  * id with the requested type. Comparison result is returned to the caller.
2993  *
2994  * returns:
2995  *     1 if image is of given type
2996  *     0 otherwise (or on error)
2997  */
2998 int fit_image_check_type(const void *fit, int noffset, uint8_t type)
2999 {
3000 	uint8_t image_type;
3001 
3002 	if (fit_image_get_type(fit, noffset, &image_type))
3003 		return 0;
3004 	return (type == image_type);
3005 }
3006 
3007 /**
3008  * fit_image_check_comp - check whether image node uses given compression
3009  * @fit: pointer to the FIT format image header
3010  * @noffset: component image node offset
3011  * @comp: requested image compression type
3012  *
3013  * fit_image_check_comp() reads image compression property and compares its
3014  * numeric id with the requested compression type. Comparison result is
3015  * returned to the caller.
3016  *
3017  * returns:
3018  *     1 if image uses requested compression
3019  *     0 otherwise (or on error)
3020  */
3021 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
3022 {
3023 	uint8_t image_comp;
3024 
3025 	if (fit_image_get_comp(fit, noffset, &image_comp))
3026 		return 0;
3027 	return (comp == image_comp);
3028 }
3029 
3030 /**
3031  * fit_check_format - sanity check FIT image format
3032  * @fit: pointer to the FIT format image header
3033  *
3034  * fit_check_format() runs a basic sanity FIT image verification.
3035  * Routine checks for mandatory properties, nodes, etc.
3036  *
3037  * returns:
3038  *     1, on success
3039  *     0, on failure
3040  */
3041 int fit_check_format(const void *fit)
3042 {
3043 	/* mandatory / node 'description' property */
3044 	if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
3045 		debug("Wrong FIT format: no description\n");
3046 		return 0;
3047 	}
3048 
3049 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
3050 	/* mandatory / node 'timestamp' property */
3051 	if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
3052 		debug("Wrong FIT format: no timestamp\n");
3053 		return 0;
3054 	}
3055 #endif
3056 
3057 	/* mandatory subimages parent '/images' node */
3058 	if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
3059 		debug("Wrong FIT format: no images parent node\n");
3060 		return 0;
3061 	}
3062 
3063 	return 1;
3064 }
3065 
3066 
3067 /**
3068  * fit_conf_find_compat
3069  * @fit: pointer to the FIT format image header
3070  * @fdt: pointer to the device tree to compare against
3071  *
3072  * fit_conf_find_compat() attempts to find the configuration whose fdt is the
3073  * most compatible with the passed in device tree.
3074  *
3075  * Example:
3076  *
3077  * / o image-tree
3078  *   |-o images
3079  *   | |-o fdt@1
3080  *   | |-o fdt@2
3081  *   |
3082  *   |-o configurations
3083  *     |-o config@1
3084  *     | |-fdt = fdt@1
3085  *     |
3086  *     |-o config@2
3087  *       |-fdt = fdt@2
3088  *
3089  * / o U-Boot fdt
3090  *   |-compatible = "foo,bar", "bim,bam"
3091  *
3092  * / o kernel fdt1
3093  *   |-compatible = "foo,bar",
3094  *
3095  * / o kernel fdt2
3096  *   |-compatible = "bim,bam", "baz,biz"
3097  *
3098  * Configuration 1 would be picked because the first string in U-Boot's
3099  * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
3100  * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
3101  *
3102  * returns:
3103  *     offset to the configuration to use if one was found
3104  *     -1 otherwise
3105  */
3106 int fit_conf_find_compat(const void *fit, const void *fdt)
3107 {
3108 	int ndepth = 0;
3109 	int noffset, confs_noffset, images_noffset;
3110 	const void *fdt_compat;
3111 	int fdt_compat_len;
3112 	int best_match_offset = 0;
3113 	int best_match_pos = 0;
3114 
3115 	confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3116 	images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
3117 	if (confs_noffset < 0 || images_noffset < 0) {
3118 		debug("Can't find configurations or images nodes.\n");
3119 		return -1;
3120 	}
3121 
3122 	fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
3123 	if (!fdt_compat) {
3124 		debug("Fdt for comparison has no \"compatible\" property.\n");
3125 		return -1;
3126 	}
3127 
3128 	/*
3129 	 * Loop over the configurations in the FIT image.
3130 	 */
3131 	for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
3132 			(noffset >= 0) && (ndepth > 0);
3133 			noffset = fdt_next_node(fit, noffset, &ndepth)) {
3134 		const void *kfdt;
3135 		const char *kfdt_name;
3136 		int kfdt_noffset;
3137 		const char *cur_fdt_compat;
3138 		int len;
3139 		size_t size;
3140 		int i;
3141 
3142 		if (ndepth > 1)
3143 			continue;
3144 
3145 		kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
3146 		if (!kfdt_name) {
3147 			debug("No fdt property found.\n");
3148 			continue;
3149 		}
3150 		kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
3151 						  kfdt_name);
3152 		if (kfdt_noffset < 0) {
3153 			debug("No image node named \"%s\" found.\n",
3154 			      kfdt_name);
3155 			continue;
3156 		}
3157 		/*
3158 		 * Get a pointer to this configuration's fdt.
3159 		 */
3160 		if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
3161 			debug("Failed to get fdt \"%s\".\n", kfdt_name);
3162 			continue;
3163 		}
3164 
3165 		len = fdt_compat_len;
3166 		cur_fdt_compat = fdt_compat;
3167 		/*
3168 		 * Look for a match for each U-Boot compatibility string in
3169 		 * turn in this configuration's fdt.
3170 		 */
3171 		for (i = 0; len > 0 &&
3172 		     (!best_match_offset || best_match_pos > i); i++) {
3173 			int cur_len = strlen(cur_fdt_compat) + 1;
3174 
3175 			if (!fdt_node_check_compatible(kfdt, 0,
3176 						       cur_fdt_compat)) {
3177 				best_match_offset = noffset;
3178 				best_match_pos = i;
3179 				break;
3180 			}
3181 			len -= cur_len;
3182 			cur_fdt_compat += cur_len;
3183 		}
3184 	}
3185 	if (!best_match_offset) {
3186 		debug("No match found.\n");
3187 		return -1;
3188 	}
3189 
3190 	return best_match_offset;
3191 }
3192 
3193 /**
3194  * fit_conf_get_node - get node offset for configuration of a given unit name
3195  * @fit: pointer to the FIT format image header
3196  * @conf_uname: configuration node unit name
3197  *
3198  * fit_conf_get_node() finds a configuration (withing the '/configurations'
3199  * parant node) of a provided unit name. If configuration is found its node offset
3200  * is returned to the caller.
3201  *
3202  * When NULL is provided in second argument fit_conf_get_node() will search
3203  * for a default configuration node instead. Default configuration node unit name
3204  * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3205  *
3206  * returns:
3207  *     configuration node offset when found (>=0)
3208  *     negative number on failure (FDT_ERR_* code)
3209  */
3210 int fit_conf_get_node(const void *fit, const char *conf_uname)
3211 {
3212 	int noffset, confs_noffset;
3213 	int len;
3214 
3215 	confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
3216 	if (confs_noffset < 0) {
3217 		debug("Can't find configurations parent node '%s' (%s)\n",
3218 			FIT_CONFS_PATH, fdt_strerror(confs_noffset));
3219 		return confs_noffset;
3220 	}
3221 
3222 	if (conf_uname == NULL) {
3223 		/* get configuration unit name from the default property */
3224 		debug("No configuration specified, trying default...\n");
3225 		conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3226 						 FIT_DEFAULT_PROP, &len);
3227 		if (conf_uname == NULL) {
3228 			fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3229 					len);
3230 			return len;
3231 		}
3232 		debug("Found default configuration: '%s'\n", conf_uname);
3233 	}
3234 
3235 	noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
3236 	if (noffset < 0) {
3237 		debug("Can't get node offset for configuration unit name: "
3238 			"'%s' (%s)\n",
3239 			conf_uname, fdt_strerror(noffset));
3240 	}
3241 
3242 	return noffset;
3243 }
3244 
3245 static int __fit_conf_get_prop_node(const void *fit, int noffset,
3246 		const char *prop_name)
3247 {
3248 	char *uname;
3249 	int len;
3250 
3251 	/* get kernel image unit name from configuration kernel property */
3252 	uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
3253 	if (uname == NULL)
3254 		return len;
3255 
3256 	return fit_image_get_node(fit, uname);
3257 }
3258 
3259 /**
3260  * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3261  * a given configuration
3262  * @fit: pointer to the FIT format image header
3263  * @noffset: configuration node offset
3264  *
3265  * fit_conf_get_kernel_node() retrives kernel image node unit name from
3266  * configuration FIT_KERNEL_PROP property and translates it to the node
3267  * offset.
3268  *
3269  * returns:
3270  *     image node offset when found (>=0)
3271  *     negative number on failure (FDT_ERR_* code)
3272  */
3273 int fit_conf_get_kernel_node(const void *fit, int noffset)
3274 {
3275 	return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
3276 }
3277 
3278 /**
3279  * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3280  * a given configuration
3281  * @fit: pointer to the FIT format image header
3282  * @noffset: configuration node offset
3283  *
3284  * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3285  * configuration FIT_KERNEL_PROP property and translates it to the node
3286  * offset.
3287  *
3288  * returns:
3289  *     image node offset when found (>=0)
3290  *     negative number on failure (FDT_ERR_* code)
3291  */
3292 int fit_conf_get_ramdisk_node(const void *fit, int noffset)
3293 {
3294 	return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
3295 }
3296 
3297 /**
3298  * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3299  * a given configuration
3300  * @fit: pointer to the FIT format image header
3301  * @noffset: configuration node offset
3302  *
3303  * fit_conf_get_fdt_node() retrives fdt image node unit name from
3304  * configuration FIT_KERNEL_PROP property and translates it to the node
3305  * offset.
3306  *
3307  * returns:
3308  *     image node offset when found (>=0)
3309  *     negative number on failure (FDT_ERR_* code)
3310  */
3311 int fit_conf_get_fdt_node(const void *fit, int noffset)
3312 {
3313 	return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
3314 }
3315 
3316 /**
3317  * fit_conf_print - prints out the FIT configuration details
3318  * @fit: pointer to the FIT format image header
3319  * @noffset: offset of the configuration node
3320  * @p: pointer to prefix string
3321  *
3322  * fit_conf_print() lists all mandatory properies for the processed
3323  * configuration node.
3324  *
3325  * returns:
3326  *     no returned results
3327  */
3328 void fit_conf_print(const void *fit, int noffset, const char *p)
3329 {
3330 	char *desc;
3331 	char *uname;
3332 	int ret;
3333 
3334 	/* Mandatory properties */
3335 	ret = fit_get_desc(fit, noffset, &desc);
3336 	printf("%s  Description:  ", p);
3337 	if (ret)
3338 		printf("unavailable\n");
3339 	else
3340 		printf("%s\n", desc);
3341 
3342 	uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3343 	printf("%s  Kernel:       ", p);
3344 	if (uname == NULL)
3345 		printf("unavailable\n");
3346 	else
3347 		printf("%s\n", uname);
3348 
3349 	/* Optional properties */
3350 	uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
3351 	if (uname)
3352 		printf("%s  Init Ramdisk: %s\n", p, uname);
3353 
3354 	uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
3355 	if (uname)
3356 		printf("%s  FDT:          %s\n", p, uname);
3357 }
3358 
3359 /**
3360  * fit_check_ramdisk - verify FIT format ramdisk subimage
3361  * @fit_hdr: pointer to the FIT ramdisk header
3362  * @rd_noffset: ramdisk subimage node offset within FIT image
3363  * @arch: requested ramdisk image architecture type
3364  * @verify: data CRC verification flag
3365  *
3366  * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3367  * specified FIT image.
3368  *
3369  * returns:
3370  *     1, on success
3371  *     0, on failure
3372  */
3373 #ifndef USE_HOSTCC
3374 static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3375 				int verify)
3376 {
3377 	fit_image_print(fit, rd_noffset, "   ");
3378 
3379 	if (verify) {
3380 		puts("   Verifying Hash Integrity ... ");
3381 		if (!fit_image_check_hashes(fit, rd_noffset)) {
3382 			puts("Bad Data Hash\n");
3383 			bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
3384 			return 0;
3385 		}
3386 		puts("OK\n");
3387 	}
3388 
3389 	bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3390 	if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3391 	    !fit_image_check_arch(fit, rd_noffset, arch) ||
3392 	    !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3393 		printf("No Linux %s Ramdisk Image\n",
3394 				genimg_get_arch_name(arch));
3395 		bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
3396 		return 0;
3397 	}
3398 
3399 	bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
3400 	return 1;
3401 }
3402 #endif /* USE_HOSTCC */
3403 #endif /* CONFIG_FIT */
3404