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