xref: /rk3399_rockchip-uboot/common/image.c (revision d985c8498c4e47095820da97aa722381d39172c5)
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 #define DEBUG
27 
28 #ifndef USE_HOSTCC
29 #include <common.h>
30 #include <watchdog.h>
31 
32 #ifdef CONFIG_SHOW_BOOT_PROGRESS
33 #include <status_led.h>
34 #endif
35 
36 #ifdef CONFIG_HAS_DATAFLASH
37 #include <dataflash.h>
38 #endif
39 
40 #ifdef CONFIG_LOGBUFFER
41 #include <logbuff.h>
42 #endif
43 
44 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
45 #include <rtc.h>
46 #endif
47 
48 #include <image.h>
49 
50 #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
51 #include <fdt.h>
52 #include <libfdt.h>
53 #include <fdt_support.h>
54 #endif
55 
56 #if defined(CONFIG_FIT)
57 #include <sha1.h>
58 
59 static int fit_check_ramdisk (const void *fit, int os_noffset,
60 		uint8_t arch, int verify);
61 #endif
62 
63 #ifdef CONFIG_CMD_BDI
64 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
65 #endif
66 
67 DECLARE_GLOBAL_DATA_PTR;
68 
69 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
70 						int verify);
71 #else
72 #include "mkimage.h"
73 #include <time.h>
74 #include <image.h>
75 #endif /* !USE_HOSTCC*/
76 
77 typedef struct table_entry {
78 	int	id;		/* as defined in image.h	*/
79 	char	*sname;		/* short (input) name		*/
80 	char	*lname;		/* long (output) name		*/
81 } table_entry_t;
82 
83 static table_entry_t uimage_arch[] = {
84 	{	IH_ARCH_INVALID,	NULL,		"Invalid ARCH",	},
85 	{	IH_ARCH_ALPHA,		"alpha",	"Alpha",	},
86 	{	IH_ARCH_ARM,		"arm",		"ARM",		},
87 	{	IH_ARCH_I386,		"x86",		"Intel x86",	},
88 	{	IH_ARCH_IA64,		"ia64",		"IA64",		},
89 	{	IH_ARCH_M68K,		"m68k",		"M68K",		},
90 	{	IH_ARCH_MICROBLAZE,	"microblaze",	"MicroBlaze",	},
91 	{	IH_ARCH_MIPS,		"mips",		"MIPS",		},
92 	{	IH_ARCH_MIPS64,		"mips64",	"MIPS 64 Bit",	},
93 	{	IH_ARCH_NIOS,		"nios",		"NIOS",		},
94 	{	IH_ARCH_NIOS2,		"nios2",	"NIOS II",	},
95 	{	IH_ARCH_PPC,		"ppc",		"PowerPC",	},
96 	{	IH_ARCH_S390,		"s390",		"IBM S390",	},
97 	{	IH_ARCH_SH,		"sh",		"SuperH",	},
98 	{	IH_ARCH_SPARC,		"sparc",	"SPARC",	},
99 	{	IH_ARCH_SPARC64,	"sparc64",	"SPARC 64 Bit",	},
100 	{	IH_ARCH_BLACKFIN,	"blackfin",	"Blackfin",	},
101 	{	IH_ARCH_AVR32,		"avr32",	"AVR32",	},
102 	{	-1,			"",		"",		},
103 };
104 
105 static table_entry_t uimage_os[] = {
106 	{	IH_OS_INVALID,	NULL,		"Invalid OS",		},
107 #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
108 	{	IH_OS_ARTOS,	"artos",	"ARTOS",		},
109 #endif
110 	{	IH_OS_LINUX,	"linux",	"Linux",		},
111 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
112 	{	IH_OS_LYNXOS,	"lynxos",	"LynxOS",		},
113 #endif
114 	{	IH_OS_NETBSD,	"netbsd",	"NetBSD",		},
115 	{	IH_OS_RTEMS,	"rtems",	"RTEMS",		},
116 	{	IH_OS_U_BOOT,	"u-boot",	"U-Boot",		},
117 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
118 	{	IH_OS_QNX,	"qnx",		"QNX",			},
119 	{	IH_OS_VXWORKS,	"vxworks",	"VxWorks",		},
120 #endif
121 #ifdef USE_HOSTCC
122 	{	IH_OS_4_4BSD,	"4_4bsd",	"4_4BSD",		},
123 	{	IH_OS_DELL,	"dell",		"Dell",			},
124 	{	IH_OS_ESIX,	"esix",		"Esix",			},
125 	{	IH_OS_FREEBSD,	"freebsd",	"FreeBSD",		},
126 	{	IH_OS_IRIX,	"irix",		"Irix",			},
127 	{	IH_OS_NCR,	"ncr",		"NCR",			},
128 	{	IH_OS_OPENBSD,	"openbsd",	"OpenBSD",		},
129 	{	IH_OS_PSOS,	"psos",		"pSOS",			},
130 	{	IH_OS_SCO,	"sco",		"SCO",			},
131 	{	IH_OS_SOLARIS,	"solaris",	"Solaris",		},
132 	{	IH_OS_SVR4,	"svr4",		"SVR4",			},
133 #endif
134 	{	-1,		"",		"",			},
135 };
136 
137 static table_entry_t uimage_type[] = {
138 	{	IH_TYPE_INVALID,    NULL,	  "Invalid Image",	},
139 	{	IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image",	},
140 	{	IH_TYPE_FIRMWARE,   "firmware",	  "Firmware",		},
141 	{	IH_TYPE_KERNEL,	    "kernel",	  "Kernel Image",	},
142 	{	IH_TYPE_MULTI,	    "multi",	  "Multi-File Image",	},
143 	{	IH_TYPE_RAMDISK,    "ramdisk",	  "RAMDisk Image",	},
144 	{	IH_TYPE_SCRIPT,     "script",	  "Script",		},
145 	{	IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
146 	{	IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",	},
147 	{	-1,		    "",		  "",			},
148 };
149 
150 static 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 	{	-1,		"",		"",			},
155 };
156 
157 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
158 static void genimg_print_size (uint32_t size);
159 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
160 static void genimg_print_time (time_t timestamp);
161 #endif
162 
163 /*****************************************************************************/
164 /* Legacy format routines */
165 /*****************************************************************************/
166 int image_check_hcrc (image_header_t *hdr)
167 {
168 	ulong hcrc;
169 	ulong len = image_get_header_size ();
170 	image_header_t header;
171 
172 	/* Copy header so we can blank CRC field for re-calculation */
173 	memmove (&header, (char *)hdr, image_get_header_size ());
174 	image_set_hcrc (&header, 0);
175 
176 	hcrc = crc32 (0, (unsigned char *)&header, len);
177 
178 	return (hcrc == image_get_hcrc (hdr));
179 }
180 
181 int image_check_dcrc (image_header_t *hdr)
182 {
183 	ulong data = image_get_data (hdr);
184 	ulong len = image_get_data_size (hdr);
185 	ulong dcrc = crc32 (0, (unsigned char *)data, len);
186 
187 	return (dcrc == image_get_dcrc (hdr));
188 }
189 
190 #ifndef USE_HOSTCC
191 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
192 {
193 	ulong dcrc = 0;
194 	ulong len = image_get_data_size (hdr);
195 	ulong data = image_get_data (hdr);
196 
197 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
198 	ulong cdata = data;
199 	ulong edata = cdata + len;
200 
201 	while (cdata < edata) {
202 		ulong chunk = edata - cdata;
203 
204 		if (chunk > chunksz)
205 			chunk = chunksz;
206 		dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
207 		cdata += chunk;
208 
209 		WATCHDOG_RESET ();
210 	}
211 #else
212 	dcrc = crc32 (0, (unsigned char *)data, len);
213 #endif
214 
215 	return (dcrc == image_get_dcrc (hdr));
216 }
217 #endif /* !USE_HOSTCC */
218 
219 /**
220  * image_multi_count - get component (sub-image) count
221  * @hdr: pointer to the header of the multi component image
222  *
223  * image_multi_count() returns number of components in a multi
224  * component image.
225  *
226  * Note: no checking of the image type is done, caller must pass
227  * a valid multi component image.
228  *
229  * returns:
230  *     number of components
231  */
232 ulong image_multi_count (image_header_t *hdr)
233 {
234 	ulong i, count = 0;
235 	uint32_t *size;
236 
237 	/* get start of the image payload, which in case of multi
238 	 * component images that points to a table of component sizes */
239 	size = (uint32_t *)image_get_data (hdr);
240 
241 	/* count non empty slots */
242 	for (i = 0; size[i]; ++i)
243 		count++;
244 
245 	return count;
246 }
247 
248 /**
249  * image_multi_getimg - get component data address and size
250  * @hdr: pointer to the header of the multi component image
251  * @idx: index of the requested component
252  * @data: pointer to a ulong variable, will hold component data address
253  * @len: pointer to a ulong variable, will hold component size
254  *
255  * image_multi_getimg() returns size and data address for the requested
256  * component in a multi component image.
257  *
258  * Note: no checking of the image type is done, caller must pass
259  * a valid multi component image.
260  *
261  * returns:
262  *     data address and size of the component, if idx is valid
263  *     0 in data and len, if idx is out of range
264  */
265 void image_multi_getimg (image_header_t *hdr, ulong idx,
266 			ulong *data, ulong *len)
267 {
268 	int i;
269 	uint32_t *size;
270 	ulong offset, tail, count, img_data;
271 
272 	/* get number of component */
273 	count = image_multi_count (hdr);
274 
275 	/* get start of the image payload, which in case of multi
276 	 * component images that points to a table of component sizes */
277 	size = (uint32_t *)image_get_data (hdr);
278 
279 	/* get address of the proper component data start, which means
280 	 * skipping sizes table (add 1 for last, null entry) */
281 	img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
282 
283 	if (idx < count) {
284 		*len = uimage_to_cpu (size[idx]);
285 		offset = 0;
286 		tail = 0;
287 
288 		/* go over all indices preceding requested component idx */
289 		for (i = 0; i < idx; i++) {
290 			/* add up i-th component size */
291 			offset += uimage_to_cpu (size[i]);
292 
293 			/* add up alignment for i-th component */
294 			tail += (4 - uimage_to_cpu (size[i]) % 4);
295 		}
296 
297 		/* calculate idx-th component data address */
298 		*data = img_data + offset + tail;
299 	} else {
300 		*len = 0;
301 		*data = 0;
302 	}
303 }
304 
305 static void image_print_type (image_header_t *hdr)
306 {
307 	const char *os, *arch, *type, *comp;
308 
309 	os = genimg_get_os_name (image_get_os (hdr));
310 	arch = genimg_get_arch_name (image_get_arch (hdr));
311 	type = genimg_get_type_name (image_get_type (hdr));
312 	comp = genimg_get_comp_name (image_get_comp (hdr));
313 
314 	printf ("%s %s %s (%s)\n", arch, os, type, comp);
315 }
316 
317 /**
318  * __image_print_contents - prints out the contents of the legacy format image
319  * @hdr: pointer to the legacy format image header
320  * @p: pointer to prefix string
321  *
322  * __image_print_contents() formats a multi line legacy image contents description.
323  * The routine prints out all header fields followed by the size/offset data
324  * for MULTI/SCRIPT images.
325  *
326  * returns:
327  *     no returned results
328  */
329 static void __image_print_contents (image_header_t *hdr, const char *p)
330 {
331 	printf ("%sImage Name:   %.*s\n", p, IH_NMLEN, image_get_name (hdr));
332 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
333 	printf ("%sCreated:      ", p);
334 	genimg_print_time ((time_t)image_get_time (hdr));
335 #endif
336 	printf ("%sImage Type:   ", p);
337 	image_print_type (hdr);
338 	printf ("%sData Size:    ", p);
339 	genimg_print_size (image_get_data_size (hdr));
340 	printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
341 	printf ("%sEntry Point:  %08x\n", p, image_get_ep (hdr));
342 
343 	if (image_check_type (hdr, IH_TYPE_MULTI) ||
344 			image_check_type (hdr, IH_TYPE_SCRIPT)) {
345 		int i;
346 		ulong data, len;
347 		ulong count = image_multi_count (hdr);
348 
349 		printf ("%sContents:\n", p);
350 		for (i = 0; i < count; i++) {
351 			image_multi_getimg (hdr, i, &data, &len);
352 
353 			printf ("%s   Image %d: ", p, i);
354 			genimg_print_size (len);
355 
356 			if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
357 				/*
358 				 * the user may need to know offsets
359 				 * if planning to do something with
360 				 * multiple files
361 				 */
362 				printf ("%s    Offset = 0x%08lx\n", p, data);
363 			}
364 		}
365 	}
366 }
367 
368 inline void image_print_contents (image_header_t *hdr)
369 {
370 	__image_print_contents (hdr, "   ");
371 }
372 
373 inline void image_print_contents_noindent (image_header_t *hdr)
374 {
375 	__image_print_contents (hdr, "");
376 }
377 
378 #ifndef USE_HOSTCC
379 /**
380  * image_get_ramdisk - get and verify ramdisk image
381  * @rd_addr: ramdisk image start address
382  * @arch: expected ramdisk architecture
383  * @verify: checksum verification flag
384  *
385  * image_get_ramdisk() returns a pointer to the verified ramdisk image
386  * header. Routine receives image start address and expected architecture
387  * flag. Verification done covers data and header integrity and os/type/arch
388  * fields checking.
389  *
390  * If dataflash support is enabled routine checks for dataflash addresses
391  * and handles required dataflash reads.
392  *
393  * returns:
394  *     pointer to a ramdisk image header, if image was found and valid
395  *     otherwise, return NULL
396  */
397 static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
398 						int verify)
399 {
400 	image_header_t *rd_hdr;
401 
402 	show_boot_progress (9);
403 	rd_hdr = (image_header_t *)rd_addr;
404 
405 	if (!image_check_magic (rd_hdr)) {
406 		puts ("Bad Magic Number\n");
407 		show_boot_progress (-10);
408 		return NULL;
409 	}
410 
411 	if (!image_check_hcrc (rd_hdr)) {
412 		puts ("Bad Header Checksum\n");
413 		show_boot_progress (-11);
414 		return NULL;
415 	}
416 
417 	show_boot_progress (10);
418 	image_print_contents (rd_hdr);
419 
420 	if (verify) {
421 		puts("   Verifying Checksum ... ");
422 		if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
423 			puts ("Bad Data CRC\n");
424 			show_boot_progress (-12);
425 			return NULL;
426 		}
427 		puts("OK\n");
428 	}
429 
430 	show_boot_progress (11);
431 
432 	if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
433 	    !image_check_arch (rd_hdr, arch) ||
434 	    !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
435 		printf ("No Linux %s Ramdisk Image\n",
436 				genimg_get_arch_name(arch));
437 		show_boot_progress (-13);
438 		return NULL;
439 	}
440 
441 	return rd_hdr;
442 }
443 #endif /* !USE_HOSTCC */
444 
445 /*****************************************************************************/
446 /* Shared dual-format routines */
447 /*****************************************************************************/
448 #ifndef USE_HOSTCC
449 int getenv_verify (void)
450 {
451 	char *s = getenv ("verify");
452 	return (s && (*s == 'n')) ? 0 : 1;
453 }
454 
455 int getenv_autostart (void)
456 {
457 	char *s = getenv ("autostart");
458 	return (s && (*s == 'n')) ? 0 : 1;
459 }
460 
461 ulong getenv_bootm_low(void)
462 {
463 	char *s = getenv ("bootm_low");
464 	if (s) {
465 		ulong tmp = simple_strtoul (s, NULL, 16);
466 		return tmp;
467 	}
468 
469 #ifdef CFG_SDRAM_BASE
470 	return CFG_SDRAM_BASE;
471 #else
472 	return 0;
473 #endif
474 }
475 
476 ulong getenv_bootm_size(void)
477 {
478 	char *s = getenv ("bootm_size");
479 	if (s) {
480 		ulong tmp = simple_strtoul (s, NULL, 16);
481 		return tmp;
482 	}
483 
484 	return gd->bd->bi_memsize;
485 }
486 
487 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
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 static 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 defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
517 static 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 /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
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 static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
547 {
548 	for (; table->id >= 0; ++table) {
549 		if (table->id == id)
550 			return (table->lname);
551 	}
552 	return (msg);
553 }
554 
555 const char *genimg_get_os_name (uint8_t os)
556 {
557 	return (get_table_entry_name (uimage_os, "Unknown OS", os));
558 }
559 
560 const char *genimg_get_arch_name (uint8_t arch)
561 {
562 	return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
563 }
564 
565 const char *genimg_get_type_name (uint8_t type)
566 {
567 	return (get_table_entry_name (uimage_type, "Unknown Image", type));
568 }
569 
570 const char *genimg_get_comp_name (uint8_t comp)
571 {
572 	return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
573 }
574 
575 /**
576  * get_table_entry_id - translate short entry name to id
577  * @table: pointer to a translation table for entries of a specific type
578  * @table_name: to be used in case of error
579  * @name: entry short name to be translated
580  *
581  * get_table_entry_id() will go over translation table trying to find
582  * entry that matches given short name. If matching entry is found,
583  * its id returned to the caller.
584  *
585  * returns:
586  *     entry id if translation succeeds
587  *     -1 otherwise
588  */
589 static int get_table_entry_id (table_entry_t *table,
590 		const char *table_name, const char *name)
591 {
592 	table_entry_t *t;
593 #ifdef USE_HOSTCC
594 	int first = 1;
595 
596 	for (t = table; t->id >= 0; ++t) {
597 		if (t->sname && strcasecmp(t->sname, name) == 0)
598 			return (t->id);
599 	}
600 
601 	fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
602 	for (t = table; t->id >= 0; ++t) {
603 		if (t->sname == NULL)
604 			continue;
605 		fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
606 		first = 0;
607 	}
608 	fprintf (stderr, "\n");
609 #else
610 	for (t = table; t->id >= 0; ++t) {
611 		if (t->sname && strcmp(t->sname, name) == 0)
612 			return (t->id);
613 	}
614 	debug ("Invalid %s Type: %s\n", table_name, name);
615 #endif /* USE_HOSTCC */
616 	return (-1);
617 }
618 
619 int genimg_get_os_id (const char *name)
620 {
621 	return (get_table_entry_id (uimage_os, "OS", name));
622 }
623 
624 int genimg_get_arch_id (const char *name)
625 {
626 	return (get_table_entry_id (uimage_arch, "CPU", name));
627 }
628 
629 int genimg_get_type_id (const char *name)
630 {
631 	return (get_table_entry_id (uimage_type, "Image", name));
632 }
633 
634 int genimg_get_comp_id (const char *name)
635 {
636 	return (get_table_entry_id (uimage_comp, "Compression", name));
637 }
638 
639 #ifndef USE_HOSTCC
640 /**
641  * genimg_get_format - get image format type
642  * @img_addr: image start address
643  *
644  * genimg_get_format() checks whether provided address points to a valid
645  * legacy or FIT image.
646  *
647  * New uImage format and FDT blob are based on a libfdt. FDT blob
648  * may be passed directly or embedded in a FIT image. In both situations
649  * genimg_get_format() must be able to dectect libfdt header.
650  *
651  * returns:
652  *     image format type or IMAGE_FORMAT_INVALID if no image is present
653  */
654 int genimg_get_format (void *img_addr)
655 {
656 	ulong		format = IMAGE_FORMAT_INVALID;
657 	image_header_t	*hdr;
658 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
659 	char		*fit_hdr;
660 #endif
661 
662 	hdr = (image_header_t *)img_addr;
663 	if (image_check_magic(hdr))
664 		format = IMAGE_FORMAT_LEGACY;
665 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
666 	else {
667 		fit_hdr = (char *)img_addr;
668 		if (fdt_check_header (fit_hdr) == 0)
669 			format = IMAGE_FORMAT_FIT;
670 	}
671 #endif
672 
673 	return format;
674 }
675 
676 /**
677  * genimg_get_image - get image from special storage (if necessary)
678  * @img_addr: image start address
679  *
680  * genimg_get_image() checks if provided image start adddress is located
681  * in a dataflash storage. If so, image is moved to a system RAM memory.
682  *
683  * returns:
684  *     image start address after possible relocation from special storage
685  */
686 ulong genimg_get_image (ulong img_addr)
687 {
688 	ulong ram_addr = img_addr;
689 
690 #ifdef CONFIG_HAS_DATAFLASH
691 	ulong h_size, d_size;
692 
693 	if (addr_dataflash (img_addr)){
694 		/* ger RAM address */
695 		ram_addr = CFG_LOAD_ADDR;
696 
697 		/* get header size */
698 		h_size = image_get_header_size ();
699 #if defined(CONFIG_FIT)
700 		if (sizeof(struct fdt_header) > h_size)
701 			h_size = sizeof(struct fdt_header);
702 #endif
703 
704 		/* read in header */
705 		debug ("   Reading image header from dataflash address "
706 			"%08lx to RAM address %08lx\n", img_addr, ram_addr);
707 
708 		read_dataflash (img_addr, h_size, (char *)ram_addr);
709 
710 		/* get data size */
711 		switch (genimg_get_format ((void *)ram_addr)) {
712 		case IMAGE_FORMAT_LEGACY:
713 			d_size = image_get_data_size ((image_header_t *)ram_addr);
714 			debug ("   Legacy format image found at 0x%08lx, size 0x%08lx\n",
715 					ram_addr, d_size);
716 			break;
717 #if defined(CONFIG_FIT)
718 		case IMAGE_FORMAT_FIT:
719 			d_size = fit_get_size ((const void *)ram_addr) - h_size;
720 			debug ("   FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
721 					ram_addr, d_size);
722 			break;
723 #endif
724 		default:
725 			printf ("   No valid image found at 0x%08lx\n", img_addr);
726 			return ram_addr;
727 		}
728 
729 		/* read in image data */
730 		debug ("   Reading image remaining data from dataflash address "
731 			"%08lx to RAM address %08lx\n", img_addr + h_size,
732 			ram_addr + h_size);
733 
734 		read_dataflash (img_addr + h_size, d_size,
735 				(char *)(ram_addr + h_size));
736 
737 	}
738 #endif /* CONFIG_HAS_DATAFLASH */
739 
740 	return ram_addr;
741 }
742 
743 /**
744  * boot_get_ramdisk - main ramdisk handling routine
745  * @argc: command argument count
746  * @argv: command argument list
747  * @images: pointer to the bootm images structure
748  * @arch: expected ramdisk architecture
749  * @rd_start: pointer to a ulong variable, will hold ramdisk start address
750  * @rd_end: pointer to a ulong variable, will hold ramdisk end
751  *
752  * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
753  * Curently supported are the following ramdisk sources:
754  *      - multicomponent kernel/ramdisk image,
755  *      - commandline provided address of decicated ramdisk image.
756  *
757  * returns:
758  *     0, if ramdisk image was found and valid, or skiped
759  *     rd_start and rd_end are set to ramdisk start/end addresses if
760  *     ramdisk image is found and valid
761  *
762  *     1, if ramdisk image is found but corrupted
763  *     rd_start and rd_end are set to 0 if no ramdisk exists
764  */
765 int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
766 		uint8_t arch, ulong *rd_start, ulong *rd_end)
767 {
768 	ulong rd_addr, rd_load;
769 	ulong rd_data, rd_len;
770 	image_header_t *rd_hdr;
771 #if defined(CONFIG_FIT)
772 	void		*fit_hdr;
773 	const char	*fit_uname_config = NULL;
774 	const char	*fit_uname_ramdisk = NULL;
775 	ulong		default_addr;
776 	int		rd_noffset;
777 	int		conf_noffset;
778 	const void	*data;
779 	size_t		size;
780 #endif
781 
782 	*rd_start = 0;
783 	*rd_end = 0;
784 
785 	/*
786 	 * Look for a '-' which indicates to ignore the
787 	 * ramdisk argument
788 	 */
789 	if ((argc >= 3) && (strcmp(argv[2], "-") ==  0)) {
790 		debug ("## Skipping init Ramdisk\n");
791 		rd_len = rd_data = 0;
792 	} else if (argc >= 3) {
793 #if defined(CONFIG_FIT)
794 		/*
795 		 * If the init ramdisk comes from the FIT image and the FIT image
796 		 * address is omitted in the command line argument, try to use
797 		 * os FIT image address or default load address.
798 		 */
799 		if (images->fit_uname_os)
800 			default_addr = (ulong)images->fit_hdr_os;
801 		else
802 			default_addr = load_addr;
803 
804 		if (fit_parse_conf (argv[2], default_addr,
805 					&rd_addr, &fit_uname_config)) {
806 			debug ("*  ramdisk: config '%s' from image at 0x%08lx\n",
807 					fit_uname_config, rd_addr);
808 		} else if (fit_parse_subimage (argv[2], default_addr,
809 					&rd_addr, &fit_uname_ramdisk)) {
810 			debug ("*  ramdisk: subimage '%s' from image at 0x%08lx\n",
811 					fit_uname_ramdisk, rd_addr);
812 		} else
813 #endif
814 		{
815 			rd_addr = simple_strtoul(argv[2], NULL, 16);
816 			debug ("*  ramdisk: cmdline image address = 0x%08lx\n",
817 					rd_addr);
818 		}
819 
820 		/* copy from dataflash if needed */
821 		rd_addr = genimg_get_image (rd_addr);
822 
823 		/*
824 		 * Check if there is an initrd image at the
825 		 * address provided in the second bootm argument
826 		 * check image type, for FIT images get FIT node.
827 		 */
828 		switch (genimg_get_format ((void *)rd_addr)) {
829 		case IMAGE_FORMAT_LEGACY:
830 			printf ("## Loading init Ramdisk from Legacy "
831 					"Image at %08lx ...\n", rd_addr);
832 
833 			rd_hdr = image_get_ramdisk (rd_addr, arch,
834 							images->verify);
835 
836 			if (rd_hdr == NULL)
837 				return 1;
838 
839 			rd_data = image_get_data (rd_hdr);
840 			rd_len = image_get_data_size (rd_hdr);
841 			rd_load = image_get_load (rd_hdr);
842 			break;
843 #if defined(CONFIG_FIT)
844 		case IMAGE_FORMAT_FIT:
845 			fit_hdr = (void *)rd_addr;
846 			printf ("## Loading init Ramdisk from FIT "
847 					"Image at %08lx ...\n", rd_addr);
848 
849 			if (!fit_check_format (fit_hdr)) {
850 				puts ("Bad FIT ramdisk image format!\n");
851 				return 0;
852 			}
853 
854 			if (!fit_uname_ramdisk) {
855 				/*
856 				 * no ramdisk image node unit name, try to get config
857 				 * node first. If config unit node name is NULL
858 				 * fit_conf_get_node() will try to find default config node
859 				 */
860 				conf_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
861 				if (conf_noffset < 0)
862 					return 0;
863 
864 				rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, conf_noffset);
865 				fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
866 			} else {
867 				/* get ramdisk component image node offset */
868 				rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
869 			}
870 			if (rd_noffset < 0)
871 				return 0;
872 
873 			printf ("   Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
874 
875 			if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
876 				return 0;
877 
878 			/* get ramdisk image data address and length */
879 			if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
880 				puts ("Could not find ramdisk subimage data!\n");
881 				return 0;
882 			}
883 
884 			rd_data = (ulong)data;
885 			rd_len = size;
886 
887 			if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
888 				puts ("Can't get ramdisk subimage load address!\n");
889 				return 0;
890 			}
891 
892 			images->fit_hdr_rd = fit_hdr;
893 			images->fit_uname_rd = fit_uname_ramdisk;
894 			break;
895 #endif
896 		default:
897 			puts ("Wrong Ramdisk Image Format\n");
898 			rd_data = rd_len = rd_load = 0;
899 		}
900 
901 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
902 		/*
903 		 * We need to copy the ramdisk to SRAM to let Linux boot
904 		 */
905 		if (rd_data) {
906 			memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
907 			rd_data = rd_load;
908 		}
909 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
910 
911 	} else if (images->legacy_hdr_valid &&
912 			image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
913 		/*
914 		 * Now check if we have a legacy mult-component image,
915 		 * get second entry data start address and len.
916 		 */
917 		show_boot_progress (13);
918 		printf ("## Loading init Ramdisk from multi component "
919 				"Legacy Image at %08lx ...\n",
920 				(ulong)images->legacy_hdr_os);
921 
922 		image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
923 	} else {
924 		/*
925 		 * no initrd image
926 		 */
927 		show_boot_progress (14);
928 		rd_len = rd_data = 0;
929 	}
930 
931 	if (!rd_data) {
932 		debug ("## No init Ramdisk\n");
933 	} else {
934 		*rd_start = rd_data;
935 		*rd_end = rd_data + rd_len;
936 	}
937 	debug ("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
938 			*rd_start, *rd_end);
939 
940 	return 0;
941 }
942 
943 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
944 /**
945  * boot_ramdisk_high - relocate init ramdisk
946  * @lmb: pointer to lmb handle, will be used for memory mgmt
947  * @rd_data: ramdisk data start address
948  * @rd_len: ramdisk data length
949  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
950  *      start address (after possible relocation)
951  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
952  *      end address (after possible relocation)
953  *
954  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
955  * variable and if requested ramdisk data is moved to a specified location.
956  *
957  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
958  * start/end addresses if ramdisk image start and len were provided,
959  * otherwise set initrd_start and initrd_end set to zeros.
960  *
961  * returns:
962  *      0 - success
963  *     -1 - failure
964  */
965 int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
966 		  ulong *initrd_start, ulong *initrd_end)
967 {
968 	char	*s;
969 	ulong	initrd_high;
970 	int	initrd_copy_to_ram = 1;
971 
972 	if ((s = getenv ("initrd_high")) != NULL) {
973 		/* a value of "no" or a similar string will act like 0,
974 		 * turning the "load high" feature off. This is intentional.
975 		 */
976 		initrd_high = simple_strtoul (s, NULL, 16);
977 		if (initrd_high == ~0)
978 			initrd_copy_to_ram = 0;
979 	} else {
980 		/* not set, no restrictions to load high */
981 		initrd_high = ~0;
982 	}
983 
984 	debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
985 			initrd_high, initrd_copy_to_ram);
986 
987 	if (rd_data) {
988 		if (!initrd_copy_to_ram) {	/* zero-copy ramdisk support */
989 			debug ("   in-place initrd\n");
990 			*initrd_start = rd_data;
991 			*initrd_end = rd_data + rd_len;
992 			lmb_reserve(lmb, rd_data, rd_len);
993 		} else {
994 			if (initrd_high)
995 				*initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
996 			else
997 				*initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
998 
999 			if (*initrd_start == 0) {
1000 				puts ("ramdisk - allocation error\n");
1001 				goto error;
1002 			}
1003 			show_boot_progress (12);
1004 
1005 			*initrd_end = *initrd_start + rd_len;
1006 			printf ("   Loading Ramdisk to %08lx, end %08lx ... ",
1007 					*initrd_start, *initrd_end);
1008 
1009 			memmove_wd ((void *)*initrd_start,
1010 					(void *)rd_data, rd_len, CHUNKSZ);
1011 
1012 			puts ("OK\n");
1013 		}
1014 	} else {
1015 		*initrd_start = 0;
1016 		*initrd_end = 0;
1017 	}
1018 	debug ("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1019 			*initrd_start, *initrd_end);
1020 
1021 	return 0;
1022 
1023 error:
1024 	return -1;
1025 }
1026 
1027 /**
1028  * boot_get_cmdline - allocate and initialize kernel cmdline
1029  * @lmb: pointer to lmb handle, will be used for memory mgmt
1030  * @cmd_start: pointer to a ulong variable, will hold cmdline start
1031  * @cmd_end: pointer to a ulong variable, will hold cmdline end
1032  * @bootmap_base: ulong variable, holds offset in physical memory to
1033  * base of bootmap
1034  *
1035  * boot_get_cmdline() allocates space for kernel command line below
1036  * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
1037  * variable is present its contents is copied to allocated kernel
1038  * command line.
1039  *
1040  * returns:
1041  *      0 - success
1042  *     -1 - failure
1043  */
1044 int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
1045 			ulong bootmap_base)
1046 {
1047 	char *cmdline;
1048 	char *s;
1049 
1050 	cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1051 					 CFG_BOOTMAPSZ + bootmap_base);
1052 
1053 	if (cmdline == NULL)
1054 		return -1;
1055 
1056 	if ((s = getenv("bootargs")) == NULL)
1057 		s = "";
1058 
1059 	strcpy(cmdline, s);
1060 
1061 	*cmd_start = (ulong) & cmdline[0];
1062 	*cmd_end = *cmd_start + strlen(cmdline);
1063 
1064 	debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1065 
1066 	return 0;
1067 }
1068 
1069 /**
1070  * boot_get_kbd - allocate and initialize kernel copy of board info
1071  * @lmb: pointer to lmb handle, will be used for memory mgmt
1072  * @kbd: double pointer to board info data
1073  * @bootmap_base: ulong variable, holds offset in physical memory to
1074  * base of bootmap
1075  *
1076  * boot_get_kbd() allocates space for kernel copy of board info data below
1077  * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1078  * the current u-boot board info data.
1079  *
1080  * returns:
1081  *      0 - success
1082  *     -1 - failure
1083  */
1084 int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
1085 {
1086 	*kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1087 				      CFG_BOOTMAPSZ + bootmap_base);
1088 	if (*kbd == NULL)
1089 		return -1;
1090 
1091 	**kbd = *(gd->bd);
1092 
1093 	debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1094 
1095 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1096 	do_bdinfo(NULL, 0, 0, NULL);
1097 #endif
1098 
1099 	return 0;
1100 }
1101 #endif /* CONFIG_PPC || CONFIG_M68K */
1102 #endif /* !USE_HOSTCC */
1103 
1104 #if defined(CONFIG_FIT)
1105 /*****************************************************************************/
1106 /* New uImage format routines */
1107 /*****************************************************************************/
1108 #ifndef USE_HOSTCC
1109 static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1110 		ulong *addr, const char **name)
1111 {
1112 	const char *sep;
1113 
1114 	*addr = addr_curr;
1115 	*name = NULL;
1116 
1117 	sep = strchr (spec, sepc);
1118 	if (sep) {
1119 		if (sep - spec > 0)
1120 			*addr = simple_strtoul (spec, NULL, 16);
1121 
1122 		*name = sep + 1;
1123 		return 1;
1124 	}
1125 
1126 	return 0;
1127 }
1128 
1129 /**
1130  * fit_parse_conf - parse FIT configuration spec
1131  * @spec: input string, containing configuration spec
1132  * @add_curr: current image address (to be used as a possible default)
1133  * @addr: pointer to a ulong variable, will hold FIT image address of a given
1134  * configuration
1135  * @conf_name double pointer to a char, will hold pointer to a configuration
1136  * unit name
1137  *
1138  * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1139  * where <addr> is a FIT image address that contains configuration
1140  * with a <conf> unit name.
1141  *
1142  * Address part is optional, and if omitted default add_curr will
1143  * be used instead.
1144  *
1145  * returns:
1146  *     1 if spec is a valid configuration string,
1147  *     addr and conf_name are set accordingly
1148  *     0 otherwise
1149  */
1150 inline int fit_parse_conf (const char *spec, ulong addr_curr,
1151 		ulong *addr, const char **conf_name)
1152 {
1153 	return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1154 }
1155 
1156 /**
1157  * fit_parse_subimage - parse FIT subimage spec
1158  * @spec: input string, containing subimage spec
1159  * @add_curr: current image address (to be used as a possible default)
1160  * @addr: pointer to a ulong variable, will hold FIT image address of a given
1161  * subimage
1162  * @image_name: double pointer to a char, will hold pointer to a subimage name
1163  *
1164  * fit_parse_subimage() expects subimage spec in the for of
1165  * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1166  * subimage with a <subimg> unit name.
1167  *
1168  * Address part is optional, and if omitted default add_curr will
1169  * be used instead.
1170  *
1171  * returns:
1172  *     1 if spec is a valid subimage string,
1173  *     addr and image_name are set accordingly
1174  *     0 otherwise
1175  */
1176 inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1177 		ulong *addr, const char **image_name)
1178 {
1179 	return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1180 }
1181 #endif /* !USE_HOSTCC */
1182 
1183 static void fit_get_debug (const void *fit, int noffset,
1184 		char *prop_name, int err)
1185 {
1186 	debug ("Can't get '%s' property from FIT 0x%08lx, "
1187 		"node: offset %d, name %s (%s)\n",
1188 		prop_name, (ulong)fit, noffset,
1189 		fit_get_name (fit, noffset, NULL),
1190 		fdt_strerror (err));
1191 }
1192 
1193 /**
1194  * __fit_print_contents - prints out the contents of the FIT format image
1195  * @fit: pointer to the FIT format image header
1196  * @p: pointer to prefix string
1197  *
1198  * __fit_print_contents() formats a multi line FIT image contents description.
1199  * The routine prints out FIT image properties (root node level) follwed by
1200  * the details of each component image.
1201  *
1202  * returns:
1203  *     no returned results
1204  */
1205 static void __fit_print_contents (const void *fit, const char *p)
1206 {
1207 	char *desc;
1208 	char *uname;
1209 	int images_noffset;
1210 	int confs_noffset;
1211 	int noffset;
1212 	int ndepth;
1213 	int count = 0;
1214 	int ret;
1215 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1216 	time_t timestamp;
1217 #endif
1218 
1219 	/* Root node properties */
1220 	ret = fit_get_desc (fit, 0, &desc);
1221 	printf ("%sFIT description: ", p);
1222 	if (ret)
1223 		printf ("unavailable\n");
1224 	else
1225 		printf ("%s\n", desc);
1226 
1227 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1228 	ret = fit_get_timestamp (fit, 0, &timestamp);
1229 	printf ("%sCreated:         ", p);
1230 	if (ret)
1231 		printf ("unavailable\n");
1232 	else
1233 		genimg_print_time (timestamp);
1234 #endif
1235 
1236 	/* Find images parent node offset */
1237 	images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1238 	if (images_noffset < 0) {
1239 		printf ("Can't find images parent node '%s' (%s)\n",
1240 			FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1241 		return;
1242 	}
1243 
1244 	/* Process its subnodes, print out component images details */
1245 	for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1246 	     (noffset >= 0) && (ndepth > 0);
1247 	     noffset = fdt_next_node (fit, noffset, &ndepth)) {
1248 		if (ndepth == 1) {
1249 			/*
1250 			 * Direct child node of the images parent node,
1251 			 * i.e. component image node.
1252 			 */
1253 			printf ("%s Image %u (%s)\n", p, count++,
1254 					fit_get_name(fit, noffset, NULL));
1255 
1256 			fit_image_print (fit, noffset, p);
1257 		}
1258 	}
1259 
1260 	/* Find configurations parent node offset */
1261 	confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1262 	if (confs_noffset < 0) {
1263 		debug ("Can't get configurations parent node '%s' (%s)\n",
1264 			FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1265 		return;
1266 	}
1267 
1268 	/* get default configuration unit name from default property */
1269 	uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1270 	if (uname)
1271 		printf ("%s Default Configuration: '%s'\n", p, uname);
1272 
1273 	/* Process its subnodes, print out configurations details */
1274 	for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1275 	     (noffset >= 0) && (ndepth > 0);
1276 	     noffset = fdt_next_node (fit, noffset, &ndepth)) {
1277 		if (ndepth == 1) {
1278 			/*
1279 			 * Direct child node of the configurations parent node,
1280 			 * i.e. configuration node.
1281 			 */
1282 			printf ("%s Configuration %u (%s)\n", p, count++,
1283 					fit_get_name(fit, noffset, NULL));
1284 
1285 			fit_conf_print (fit, noffset, p);
1286 		}
1287 	}
1288 }
1289 
1290 inline void fit_print_contents (const void *fit)
1291 {
1292 	__fit_print_contents (fit, "   ");
1293 }
1294 
1295 inline void fit_print_contents_noindent (const void *fit)
1296 {
1297 	__fit_print_contents (fit, "");
1298 }
1299 
1300 /**
1301  * fit_image_print - prints out the FIT component image details
1302  * @fit: pointer to the FIT format image header
1303  * @image_noffset: offset of the component image node
1304  * @p: pointer to prefix string
1305  *
1306  * fit_image_print() lists all mandatory properies for the processed component
1307  * image. If present, hash nodes are printed out as well.
1308  *
1309  * returns:
1310  *     no returned results
1311  */
1312 void fit_image_print (const void *fit, int image_noffset, const char *p)
1313 {
1314 	char *desc;
1315 	uint8_t type, arch, os, comp;
1316 	size_t size;
1317 	ulong load, entry;
1318 	const void *data;
1319 	int noffset;
1320 	int ndepth;
1321 	int ret;
1322 
1323 	/* Mandatory properties */
1324 	ret = fit_get_desc (fit, image_noffset, &desc);
1325 	printf ("%s  Description:  ", p);
1326 	if (ret)
1327 		printf ("unavailable\n");
1328 	else
1329 		printf ("%s\n", desc);
1330 
1331 	fit_image_get_type (fit, image_noffset, &type);
1332 	printf ("%s  Type:         %s\n", p, genimg_get_type_name (type));
1333 
1334 	fit_image_get_comp (fit, image_noffset, &comp);
1335 	printf ("%s  Compression:  %s\n", p, genimg_get_comp_name (comp));
1336 
1337 	ret = fit_image_get_data (fit, image_noffset, &data, &size);
1338 
1339 #ifndef USE_HOSTCC
1340 	printf ("%s  Data Start:   ", p);
1341 	if (ret)
1342 		printf ("unavailable\n");
1343 	else
1344 		printf ("0x%08lx\n", (ulong)data);
1345 #endif
1346 
1347 	printf ("%s  Data Size:    ", p);
1348 	if (ret)
1349 		printf ("unavailable\n");
1350 	else
1351 		genimg_print_size (size);
1352 
1353 	/* Remaining, type dependent properties */
1354 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1355 	    (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1356 	    (type == IH_TYPE_FLATDT)) {
1357 		fit_image_get_arch (fit, image_noffset, &arch);
1358 		printf ("%s  Architecture: %s\n", p, genimg_get_arch_name (arch));
1359 	}
1360 
1361 	if (type == IH_TYPE_KERNEL) {
1362 		fit_image_get_os (fit, image_noffset, &os);
1363 		printf ("%s  OS:           %s\n", p, genimg_get_os_name (os));
1364 	}
1365 
1366 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1367 		ret = fit_image_get_load (fit, image_noffset, &load);
1368 		printf ("%s  Load Address: ", p);
1369 		if (ret)
1370 			printf ("unavailable\n");
1371 		else
1372 			printf ("0x%08lx\n", load);
1373 
1374 		fit_image_get_entry (fit, image_noffset, &entry);
1375 		printf ("%s  Entry Point:  ", p);
1376 		if (ret)
1377 			printf ("unavailable\n");
1378 		else
1379 			printf ("0x%08lx\n", entry);
1380 	}
1381 
1382 	/* Process all hash subnodes of the component image node */
1383 	for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1384 	     (noffset >= 0) && (ndepth > 0);
1385 	     noffset = fdt_next_node (fit, noffset, &ndepth)) {
1386 		if (ndepth == 1) {
1387 			/* Direct child node of the component image node */
1388 			fit_image_print_hash (fit, noffset, p);
1389 		}
1390 	}
1391 }
1392 
1393 /**
1394  * fit_image_print_hash - prints out the hash node details
1395  * @fit: pointer to the FIT format image header
1396  * @noffset: offset of the hash node
1397  * @p: pointer to prefix string
1398  *
1399  * fit_image_print_hash() lists properies for the processed hash node
1400  *
1401  * returns:
1402  *     no returned results
1403  */
1404 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1405 {
1406 	char *algo;
1407 	uint8_t *value;
1408 	int value_len;
1409 	int i, ret;
1410 
1411 	/*
1412 	 * Check subnode name, must be equal to "hash".
1413 	 * Multiple hash nodes require unique unit node
1414 	 * names, e.g. hash@1, hash@2, etc.
1415 	 */
1416 	if (strncmp (fit_get_name(fit, noffset, NULL),
1417 			FIT_HASH_NODENAME,
1418 			strlen(FIT_HASH_NODENAME)) != 0)
1419 		return;
1420 
1421 	debug ("%s  Hash node:    '%s'\n", p,
1422 			fit_get_name (fit, noffset, NULL));
1423 
1424 	printf ("%s  Hash algo:    ", p);
1425 	if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1426 		printf ("invalid/unsupported\n");
1427 		return;
1428 	}
1429 	printf ("%s\n", algo);
1430 
1431 	ret = fit_image_hash_get_value (fit, noffset, &value,
1432 					&value_len);
1433 	printf ("%s  Hash value:   ", p);
1434 	if (ret) {
1435 		printf ("unavailable\n");
1436 	} else {
1437 		for (i = 0; i < value_len; i++)
1438 			printf ("%02x", value[i]);
1439 		printf ("\n");
1440 	}
1441 
1442 	debug  ("%s  Hash len:     %d\n", p, value_len);
1443 }
1444 
1445 /**
1446  * fit_get_desc - get node description property
1447  * @fit: pointer to the FIT format image header
1448  * @noffset: node offset
1449  * @desc: double pointer to the char, will hold pointer to the descrption
1450  *
1451  * fit_get_desc() reads description property from a given node, if
1452  * description is found pointer to it is returened in third call argument.
1453  *
1454  * returns:
1455  *     0, on success
1456  *     -1, on failure
1457  */
1458 int fit_get_desc (const void *fit, int noffset, char **desc)
1459 {
1460 	int len;
1461 
1462 	*desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1463 	if (*desc == NULL) {
1464 		fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1465 		return -1;
1466 	}
1467 
1468 	return 0;
1469 }
1470 
1471 /**
1472  * fit_get_timestamp - get node timestamp property
1473  * @fit: pointer to the FIT format image header
1474  * @noffset: node offset
1475  * @timestamp: pointer to the time_t, will hold read timestamp
1476  *
1477  * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1478  * is found and has a correct size its value is retured in third call
1479  * argument.
1480  *
1481  * returns:
1482  *     0, on success
1483  *     -1, on property read failure
1484  *     -2, on wrong timestamp size
1485  */
1486 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1487 {
1488 	int len;
1489 	const void *data;
1490 
1491 	data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1492 	if (data == NULL) {
1493 		fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1494 		return -1;
1495 	}
1496 	if (len != sizeof (uint32_t)) {
1497 		debug ("FIT timestamp with incorrect size of (%u)\n", len);
1498 		return -2;
1499 	}
1500 
1501 	*timestamp = uimage_to_cpu (*((uint32_t *)data));
1502 	return 0;
1503 }
1504 
1505 /**
1506  * fit_image_get_node - get node offset for component image of a given unit name
1507  * @fit: pointer to the FIT format image header
1508  * @image_uname: component image node unit name
1509  *
1510  * fit_image_get_node() finds a component image (withing the '/images'
1511  * node) of a provided unit name. If image is found its node offset is
1512  * returned to the caller.
1513  *
1514  * returns:
1515  *     image node offset when found (>=0)
1516  *     negative number on failure (FDT_ERR_* code)
1517  */
1518 int fit_image_get_node (const void *fit, const char *image_uname)
1519 {
1520 	int noffset, images_noffset;
1521 
1522 	images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1523 	if (images_noffset < 0) {
1524 		debug ("Can't find images parent node '%s' (%s)\n",
1525 			FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1526 		return images_noffset;
1527 	}
1528 
1529 	noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1530 	if (noffset < 0) {
1531 		debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1532 			image_uname, fdt_strerror (noffset));
1533 	}
1534 
1535 	return noffset;
1536 }
1537 
1538 /**
1539  * fit_image_get_os - get os id for a given component image node
1540  * @fit: pointer to the FIT format image header
1541  * @noffset: component image node offset
1542  * @os: pointer to the uint8_t, will hold os numeric id
1543  *
1544  * fit_image_get_os() finds os property in a given component image node.
1545  * If the property is found, its (string) value is translated to the numeric
1546  * id which is returned to the caller.
1547  *
1548  * returns:
1549  *     0, on success
1550  *     -1, on failure
1551  */
1552 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1553 {
1554 	int len;
1555 	const void *data;
1556 
1557 	/* Get OS name from property data */
1558 	data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1559 	if (data == NULL) {
1560 		fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1561 		*os = -1;
1562 		return -1;
1563 	}
1564 
1565 	/* Translate OS name to id */
1566 	*os = genimg_get_os_id (data);
1567 	return 0;
1568 }
1569 
1570 /**
1571  * fit_image_get_arch - get arch id for a given component image node
1572  * @fit: pointer to the FIT format image header
1573  * @noffset: component image node offset
1574  * @arch: pointer to the uint8_t, will hold arch numeric id
1575  *
1576  * fit_image_get_arch() finds arch property in a given component image node.
1577  * If the property is found, its (string) value is translated to the numeric
1578  * id which is returned to the caller.
1579  *
1580  * returns:
1581  *     0, on success
1582  *     -1, on failure
1583  */
1584 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1585 {
1586 	int len;
1587 	const void *data;
1588 
1589 	/* Get architecture name from property data */
1590 	data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1591 	if (data == NULL) {
1592 		fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1593 		*arch = -1;
1594 		return -1;
1595 	}
1596 
1597 	/* Translate architecture name to id */
1598 	*arch = genimg_get_arch_id (data);
1599 	return 0;
1600 }
1601 
1602 /**
1603  * fit_image_get_type - get type id for a given component image node
1604  * @fit: pointer to the FIT format image header
1605  * @noffset: component image node offset
1606  * @type: pointer to the uint8_t, will hold type numeric id
1607  *
1608  * fit_image_get_type() finds type property in a given component image node.
1609  * If the property is found, its (string) value is translated to the numeric
1610  * id which is returned to the caller.
1611  *
1612  * returns:
1613  *     0, on success
1614  *     -1, on failure
1615  */
1616 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1617 {
1618 	int len;
1619 	const void *data;
1620 
1621 	/* Get image type name from property data */
1622 	data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1623 	if (data == NULL) {
1624 		fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1625 		*type = -1;
1626 		return -1;
1627 	}
1628 
1629 	/* Translate image type name to id */
1630 	*type = genimg_get_type_id (data);
1631 	return 0;
1632 }
1633 
1634 /**
1635  * fit_image_get_comp - get comp id for a given component image node
1636  * @fit: pointer to the FIT format image header
1637  * @noffset: component image node offset
1638  * @comp: pointer to the uint8_t, will hold comp numeric id
1639  *
1640  * fit_image_get_comp() finds comp property in a given component image node.
1641  * If the property is found, its (string) value is translated to the numeric
1642  * id which is returned to the caller.
1643  *
1644  * returns:
1645  *     0, on success
1646  *     -1, on failure
1647  */
1648 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1649 {
1650 	int len;
1651 	const void *data;
1652 
1653 	/* Get compression name from property data */
1654 	data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1655 	if (data == NULL) {
1656 		fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1657 		*comp = -1;
1658 		return -1;
1659 	}
1660 
1661 	/* Translate compression name to id */
1662 	*comp = genimg_get_comp_id (data);
1663 	return 0;
1664 }
1665 
1666 /**
1667  * fit_image_get_load - get load address property for a given component image node
1668  * @fit: pointer to the FIT format image header
1669  * @noffset: component image node offset
1670  * @load: pointer to the uint32_t, will hold load address
1671  *
1672  * fit_image_get_load() finds load address property in a given component image node.
1673  * If the property is found, its value is returned to the caller.
1674  *
1675  * returns:
1676  *     0, on success
1677  *     -1, on failure
1678  */
1679 int fit_image_get_load (const void *fit, int noffset, ulong *load)
1680 {
1681 	int len;
1682 	const uint32_t *data;
1683 
1684 	data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1685 	if (data == NULL) {
1686 		fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1687 		return -1;
1688 	}
1689 
1690 	*load = uimage_to_cpu (*data);
1691 	return 0;
1692 }
1693 
1694 /**
1695  * fit_image_get_entry - get entry point address property for a given component image node
1696  * @fit: pointer to the FIT format image header
1697  * @noffset: component image node offset
1698  * @entry: pointer to the uint32_t, will hold entry point address
1699  *
1700  * fit_image_get_entry() finds entry point address property in a given component image node.
1701  * If the property is found, its value is returned to the caller.
1702  *
1703  * returns:
1704  *     0, on success
1705  *     -1, on failure
1706  */
1707 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1708 {
1709 	int len;
1710 	const uint32_t *data;
1711 
1712 	data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1713 	if (data == NULL) {
1714 		fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1715 		return -1;
1716 	}
1717 
1718 	*entry = uimage_to_cpu (*data);
1719 	return 0;
1720 }
1721 
1722 /**
1723  * fit_image_get_data - get data property and its size for a given component image node
1724  * @fit: pointer to the FIT format image header
1725  * @noffset: component image node offset
1726  * @data: double pointer to void, will hold data property's data address
1727  * @size: pointer to size_t, will hold data property's data size
1728  *
1729  * fit_image_get_data() finds data property in a given component image node.
1730  * If the property is found its data start address and size are returned to
1731  * the caller.
1732  *
1733  * returns:
1734  *     0, on success
1735  *     -1, on failure
1736  */
1737 int fit_image_get_data (const void *fit, int noffset,
1738 		const void **data, size_t *size)
1739 {
1740 	int len;
1741 
1742 	*data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1743 	if (*data == NULL) {
1744 		fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1745 		*size = 0;
1746 		return -1;
1747 	}
1748 
1749 	*size = len;
1750 	return 0;
1751 }
1752 
1753 /**
1754  * fit_image_hash_get_algo - get hash algorithm name
1755  * @fit: pointer to the FIT format image header
1756  * @noffset: hash node offset
1757  * @algo: double pointer to char, will hold pointer to the algorithm name
1758  *
1759  * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1760  * If the property is found its data start address is returned to the caller.
1761  *
1762  * returns:
1763  *     0, on success
1764  *     -1, on failure
1765  */
1766 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1767 {
1768 	int len;
1769 
1770 	*algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1771 	if (*algo == NULL) {
1772 		fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1773 		return -1;
1774 	}
1775 
1776 	return 0;
1777 }
1778 
1779 /**
1780  * fit_image_hash_get_value - get hash value and length
1781  * @fit: pointer to the FIT format image header
1782  * @noffset: hash node offset
1783  * @value: double pointer to uint8_t, will hold address of a hash value data
1784  * @value_len: pointer to an int, will hold hash data length
1785  *
1786  * fit_image_hash_get_value() finds hash value property in a given hash node.
1787  * If the property is found its data start address and size are returned to
1788  * the caller.
1789  *
1790  * returns:
1791  *     0, on success
1792  *     -1, on failure
1793  */
1794 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1795 				int *value_len)
1796 {
1797 	int len;
1798 
1799 	*value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1800 	if (*value == NULL) {
1801 		fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1802 		*value_len = 0;
1803 		return -1;
1804 	}
1805 
1806 	*value_len = len;
1807 	return 0;
1808 }
1809 
1810 /**
1811  * fit_set_timestamp - set node timestamp property
1812  * @fit: pointer to the FIT format image header
1813  * @noffset: node offset
1814  * @timestamp: timestamp value to be set
1815  *
1816  * fit_set_timestamp() attempts to set timestamp property in the requested
1817  * node and returns operation status to the caller.
1818  *
1819  * returns:
1820  *     0, on success
1821  *     -1, on property read failure
1822  */
1823 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1824 {
1825 	uint32_t t;
1826 	int ret;
1827 
1828 	t = cpu_to_uimage (timestamp);
1829 	ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1830 				sizeof (uint32_t));
1831 	if (ret) {
1832 		printf ("Can't set '%s' property for '%s' node (%s)\n",
1833 			FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1834 			fdt_strerror (ret));
1835 		return -1;
1836 	}
1837 
1838 	return 0;
1839 }
1840 
1841 /**
1842  * calculate_hash - calculate and return hash for provided input data
1843  * @data: pointer to the input data
1844  * @data_len: data length
1845  * @algo: requested hash algorithm
1846  * @value: pointer to the char, will hold hash value data (caller must
1847  * allocate enough free space)
1848  * value_len: length of the calculated hash
1849  *
1850  * calculate_hash() computes input data hash according to the requested algorithm.
1851  * Resulting hash value is placed in caller provided 'value' buffer, length
1852  * of the calculated hash is returned via value_len pointer argument.
1853  *
1854  * returns:
1855  *     0, on success
1856  *    -1, when algo is unsupported
1857  */
1858 static int calculate_hash (const void *data, int data_len, const char *algo,
1859 			uint8_t *value, int *value_len)
1860 {
1861 	if (strcmp (algo, "crc32") == 0 ) {
1862 		*((uint32_t *)value) = crc32 (0, data, data_len);
1863 		*((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1864 		*value_len = 4;
1865 	} else if (strcmp (algo, "sha1") == 0 ) {
1866 		sha1_csum ((unsigned char *) data, data_len,
1867 				(unsigned char *) value);
1868 		*value_len = 20;
1869 	} else if (strcmp (algo, "md5") == 0 ) {
1870 		printf ("MD5 not supported\n");
1871 		*value_len = 0;
1872 	} else {
1873 		debug ("Unsupported hash alogrithm\n");
1874 		return -1;
1875 	}
1876 	return 0;
1877 }
1878 
1879 #ifdef USE_HOSTCC
1880 /**
1881  * fit_set_hashes - process FIT component image nodes and calculate hashes
1882  * @fit: pointer to the FIT format image header
1883  *
1884  * fit_set_hashes() adds hash values for all component images in the FIT blob.
1885  * Hashes are calculated for all component images which have hash subnodes
1886  * with algorithm property set to one of the supported hash algorithms.
1887  *
1888  * returns
1889  *     0, on success
1890  *     libfdt error code, on failure
1891  */
1892 int fit_set_hashes (void *fit)
1893 {
1894 	int images_noffset;
1895 	int noffset;
1896 	int ndepth;
1897 	int ret;
1898 
1899 	/* Find images parent node offset */
1900 	images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1901 	if (images_noffset < 0) {
1902 		printf ("Can't find images parent node '%s' (%s)\n",
1903 			FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1904 		return images_noffset;
1905 	}
1906 
1907 	/* Process its subnodes, print out component images details */
1908 	for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1909 	     (noffset >= 0) && (ndepth > 0);
1910 	     noffset = fdt_next_node (fit, noffset, &ndepth)) {
1911 		if (ndepth == 1) {
1912 			/*
1913 			 * Direct child node of the images parent node,
1914 			 * i.e. component image node.
1915 			 */
1916 			ret = fit_image_set_hashes (fit, noffset);
1917 			if (ret)
1918 				return ret;
1919 		}
1920 	}
1921 
1922 	return 0;
1923 }
1924 
1925 /**
1926  * fit_image_set_hashes - calculate/set hashes for given component image node
1927  * @fit: pointer to the FIT format image header
1928  * @image_noffset: requested component image node
1929  *
1930  * fit_image_set_hashes() adds hash values for an component image node. All
1931  * existing hash subnodes are checked, if algorithm property is set to one of
1932  * the supported hash algorithms, hash value is computed and corresponding
1933  * hash node property is set, for example:
1934  *
1935  * Input component image node structure:
1936  *
1937  * o image@1 (at image_noffset)
1938  *   | - data = [binary data]
1939  *   o hash@1
1940  *     |- algo = "sha1"
1941  *
1942  * Output component image node structure:
1943  *
1944  * o image@1 (at image_noffset)
1945  *   | - data = [binary data]
1946  *   o hash@1
1947  *     |- algo = "sha1"
1948  *     |- value = sha1(data)
1949  *
1950  * returns:
1951  *     0 on sucess
1952  *    <0 on failure
1953  */
1954 int fit_image_set_hashes (void *fit, int image_noffset)
1955 {
1956 	const void *data;
1957 	size_t size;
1958 	char *algo;
1959 	uint8_t value[FIT_MAX_HASH_LEN];
1960 	int value_len;
1961 	int noffset;
1962 	int ndepth;
1963 
1964 	/* Get image data and data length */
1965 	if (fit_image_get_data (fit, image_noffset, &data, &size)) {
1966 		printf ("Can't get image data/size\n");
1967 		return -1;
1968 	}
1969 
1970 	/* Process all hash subnodes of the component image node */
1971 	for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1972 	     (noffset >= 0) && (ndepth > 0);
1973 	     noffset = fdt_next_node (fit, noffset, &ndepth)) {
1974 		if (ndepth == 1) {
1975 			/* Direct child node of the component image node */
1976 
1977 			/*
1978 			 * Check subnode name, must be equal to "hash".
1979 			 * Multiple hash nodes require unique unit node
1980 			 * names, e.g. hash@1, hash@2, etc.
1981 			 */
1982 			if (strncmp (fit_get_name(fit, noffset, NULL),
1983 						FIT_HASH_NODENAME,
1984 						strlen(FIT_HASH_NODENAME)) != 0) {
1985 				/* Not a hash subnode, skip it */
1986 				continue;
1987 			}
1988 
1989 			if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1990 				printf ("Can't get hash algo property for "
1991 					"'%s' hash node in '%s' image node\n",
1992 					fit_get_name (fit, noffset, NULL),
1993 					fit_get_name (fit, image_noffset, NULL));
1994 				return -1;
1995 			}
1996 
1997 			if (calculate_hash (data, size, algo, value, &value_len)) {
1998 				printf ("Unsupported hash algorithm (%s) for "
1999 					"'%s' hash node in '%s' image node\n",
2000 					algo, fit_get_name (fit, noffset, NULL),
2001 					fit_get_name (fit, image_noffset, NULL));
2002 				return -1;
2003 			}
2004 
2005 			if (fit_image_hash_set_value (fit, noffset, value,
2006 							value_len)) {
2007 				printf ("Can't set hash value for "
2008 					"'%s' hash node in '%s' image node\n",
2009 					fit_get_name (fit, noffset, NULL),
2010 					fit_get_name (fit, image_noffset, NULL));
2011 				return -1;
2012 			}
2013 		}
2014 	}
2015 
2016 	return 0;
2017 }
2018 
2019 /**
2020  * fit_image_hash_set_value - set hash value in requested has node
2021  * @fit: pointer to the FIT format image header
2022  * @noffset: hash node offset
2023  * @value: hash value to be set
2024  * @value_len: hash value length
2025  *
2026  * fit_image_hash_set_value() attempts to set hash value in a node at offset
2027  * given and returns operation status to the caller.
2028  *
2029  * returns
2030  *     0, on success
2031  *     -1, on failure
2032  */
2033 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2034 				int value_len)
2035 {
2036 	int ret;
2037 
2038 	ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2039 	if (ret) {
2040 		printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2041 			FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2042 			fdt_strerror (ret));
2043 		return -1;
2044 	}
2045 
2046 	return 0;
2047 }
2048 #endif /* USE_HOSTCC */
2049 
2050 /**
2051  * fit_image_check_hashes - verify data intergity
2052  * @fit: pointer to the FIT format image header
2053  * @image_noffset: component image node offset
2054  *
2055  * fit_image_check_hashes() goes over component image hash nodes,
2056  * re-calculates each data hash and compares with the value stored in hash
2057  * node.
2058  *
2059  * returns:
2060  *     1, if all hashes are valid
2061  *     0, otherwise (or on error)
2062  */
2063 int fit_image_check_hashes (const void *fit, int image_noffset)
2064 {
2065 	const void	*data;
2066 	size_t		size;
2067 	char		*algo;
2068 	uint8_t		*fit_value;
2069 	int		fit_value_len;
2070 	uint8_t		value[FIT_MAX_HASH_LEN];
2071 	int		value_len;
2072 	int		noffset;
2073 	int		ndepth;
2074 	char		*err_msg = "";
2075 
2076 	/* Get image data and data length */
2077 	if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2078 		printf ("Can't get image data/size\n");
2079 		return 0;
2080 	}
2081 
2082 	/* Process all hash subnodes of the component image node */
2083 	for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2084 	     (noffset >= 0) && (ndepth > 0);
2085 	     noffset = fdt_next_node (fit, noffset, &ndepth)) {
2086 		if (ndepth == 1) {
2087 			/* Direct child node of the component image node */
2088 
2089 			/*
2090 			 * Check subnode name, must be equal to "hash".
2091 			 * Multiple hash nodes require unique unit node
2092 			 * names, e.g. hash@1, hash@2, etc.
2093 			 */
2094 			if (strncmp (fit_get_name(fit, noffset, NULL),
2095 					FIT_HASH_NODENAME,
2096 					strlen(FIT_HASH_NODENAME)) != 0)
2097 				continue;
2098 
2099 			if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2100 				err_msg = "Can't get hash algo property";
2101 				goto error;
2102 			}
2103 			printf ("%s", algo);
2104 
2105 			if (fit_image_hash_get_value (fit, noffset, &fit_value,
2106 							&fit_value_len)) {
2107 				err_msg = "Can't get hash value property";
2108 				goto error;
2109 			}
2110 
2111 			if (calculate_hash (data, size, algo, value, &value_len)) {
2112 				err_msg = "Unsupported hash algorithm";
2113 				goto error;
2114 			}
2115 
2116 			if (value_len != fit_value_len) {
2117 				err_msg = "Bad hash value len";
2118 				goto error;
2119 			} else if (memcmp (value, fit_value, value_len) != 0) {
2120 				err_msg = "Bad hash value";
2121 				goto error;
2122 			}
2123 			printf ("+ ");
2124 		}
2125 	}
2126 
2127 	return 1;
2128 
2129 error:
2130 	printf ("%s for '%s' hash node in '%s' image node\n",
2131 			err_msg, fit_get_name (fit, noffset, NULL),
2132 			fit_get_name (fit, image_noffset, NULL));
2133 	return 0;
2134 }
2135 
2136 /**
2137  * fit_image_check_os - check whether image node is of a given os type
2138  * @fit: pointer to the FIT format image header
2139  * @noffset: component image node offset
2140  * @os: requested image os
2141  *
2142  * fit_image_check_os() reads image os property and compares its numeric
2143  * id with the requested os. Comparison result is returned to the caller.
2144  *
2145  * returns:
2146  *     1 if image is of given os type
2147  *     0 otherwise (or on error)
2148  */
2149 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2150 {
2151 	uint8_t image_os;
2152 
2153 	if (fit_image_get_os (fit, noffset, &image_os))
2154 		return 0;
2155 	return (os == image_os);
2156 }
2157 
2158 /**
2159  * fit_image_check_arch - check whether image node is of a given arch
2160  * @fit: pointer to the FIT format image header
2161  * @noffset: component image node offset
2162  * @arch: requested imagearch
2163  *
2164  * fit_image_check_arch() reads image arch property and compares its numeric
2165  * id with the requested arch. Comparison result is returned to the caller.
2166  *
2167  * returns:
2168  *     1 if image is of given arch
2169  *     0 otherwise (or on error)
2170  */
2171 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2172 {
2173 	uint8_t image_arch;
2174 
2175 	if (fit_image_get_arch (fit, noffset, &image_arch))
2176 		return 0;
2177 	return (arch == image_arch);
2178 }
2179 
2180 /**
2181  * fit_image_check_type - check whether image node is of a given type
2182  * @fit: pointer to the FIT format image header
2183  * @noffset: component image node offset
2184  * @type: requested image type
2185  *
2186  * fit_image_check_type() reads image type property and compares its numeric
2187  * id with the requested type. Comparison result is returned to the caller.
2188  *
2189  * returns:
2190  *     1 if image is of given type
2191  *     0 otherwise (or on error)
2192  */
2193 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2194 {
2195 	uint8_t image_type;
2196 
2197 	if (fit_image_get_type (fit, noffset, &image_type))
2198 		return 0;
2199 	return (type == image_type);
2200 }
2201 
2202 /**
2203  * fit_image_check_comp - check whether image node uses given compression
2204  * @fit: pointer to the FIT format image header
2205  * @noffset: component image node offset
2206  * @comp: requested image compression type
2207  *
2208  * fit_image_check_comp() reads image compression property and compares its
2209  * numeric id with the requested compression type. Comparison result is
2210  * returned to the caller.
2211  *
2212  * returns:
2213  *     1 if image uses requested compression
2214  *     0 otherwise (or on error)
2215  */
2216 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2217 {
2218 	uint8_t image_comp;
2219 
2220 	if (fit_image_get_comp (fit, noffset, &image_comp))
2221 		return 0;
2222 	return (comp == image_comp);
2223 }
2224 
2225 /**
2226  * fit_check_format - sanity check FIT image format
2227  * @fit: pointer to the FIT format image header
2228  *
2229  * fit_check_format() runs a basic sanity FIT image verification.
2230  * Routine checks for mandatory properties, nodes, etc.
2231  *
2232  * returns:
2233  *     1, on success
2234  *     0, on failure
2235  */
2236 int fit_check_format (const void *fit)
2237 {
2238 	/* mandatory / node 'description' property */
2239 	if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2240 		debug ("Wrong FIT format: no description\n");
2241 		return 0;
2242 	}
2243 
2244 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2245 	/* mandatory / node 'timestamp' property */
2246 	if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2247 		debug ("Wrong FIT format: no description\n");
2248 		return 0;
2249 	}
2250 #endif
2251 
2252 	/* mandatory subimages parent '/images' node */
2253 	if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2254 		debug ("Wrong FIT format: no images parent node\n");
2255 		return 0;
2256 	}
2257 
2258 	return 1;
2259 }
2260 
2261 /**
2262  * fit_conf_get_node - get node offset for configuration of a given unit name
2263  * @fit: pointer to the FIT format image header
2264  * @conf_uname: configuration node unit name
2265  *
2266  * fit_conf_get_node() finds a configuration (withing the '/configurations'
2267  * parant node) of a provided unit name. If configuration is found its node offset
2268  * is returned to the caller.
2269  *
2270  * When NULL is provided in second argument fit_conf_get_node() will search
2271  * for a default configuration node instead. Default configuration node unit name
2272  * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2273  *
2274  * returns:
2275  *     configuration node offset when found (>=0)
2276  *     negative number on failure (FDT_ERR_* code)
2277  */
2278 int fit_conf_get_node (const void *fit, const char *conf_uname)
2279 {
2280 	int noffset, confs_noffset;
2281 	int len;
2282 
2283 	confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2284 	if (confs_noffset < 0) {
2285 		debug ("Can't find configurations parent node '%s' (%s)\n",
2286 			FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2287 		return confs_noffset;
2288 	}
2289 
2290 	if (conf_uname == NULL) {
2291 		/* get configuration unit name from the default property */
2292 		debug ("No configuration specified, trying default...\n");
2293 		conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2294 		if (conf_uname == NULL) {
2295 			fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2296 			return len;
2297 		}
2298 		debug ("Found default configuration: '%s'\n", conf_uname);
2299 	}
2300 
2301 	noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2302 	if (noffset < 0) {
2303 		debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2304 			conf_uname, fdt_strerror (noffset));
2305 	}
2306 
2307 	return noffset;
2308 }
2309 
2310 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2311 		const char *prop_name)
2312 {
2313 	char *uname;
2314 	int len;
2315 
2316 	/* get kernel image unit name from configuration kernel property */
2317 	uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2318 	if (uname == NULL)
2319 		return len;
2320 
2321 	return fit_image_get_node (fit, uname);
2322 }
2323 
2324 /**
2325  * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2326  * a given configuration
2327  * @fit: pointer to the FIT format image header
2328  * @noffset: configuration node offset
2329  *
2330  * fit_conf_get_kernel_node() retrives kernel image node unit name from
2331  * configuration FIT_KERNEL_PROP property and translates it to the node
2332  * offset.
2333  *
2334  * returns:
2335  *     image node offset when found (>=0)
2336  *     negative number on failure (FDT_ERR_* code)
2337  */
2338 int fit_conf_get_kernel_node (const void *fit, int noffset)
2339 {
2340 	return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2341 }
2342 
2343 /**
2344  * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2345  * a given configuration
2346  * @fit: pointer to the FIT format image header
2347  * @noffset: configuration node offset
2348  *
2349  * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2350  * configuration FIT_KERNEL_PROP property and translates it to the node
2351  * offset.
2352  *
2353  * returns:
2354  *     image node offset when found (>=0)
2355  *     negative number on failure (FDT_ERR_* code)
2356  */
2357 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2358 {
2359 	return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2360 }
2361 
2362 /**
2363  * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2364  * a given configuration
2365  * @fit: pointer to the FIT format image header
2366  * @noffset: configuration node offset
2367  *
2368  * fit_conf_get_fdt_node() retrives fdt image node unit name from
2369  * configuration FIT_KERNEL_PROP property and translates it to the node
2370  * offset.
2371  *
2372  * returns:
2373  *     image node offset when found (>=0)
2374  *     negative number on failure (FDT_ERR_* code)
2375  */
2376 int fit_conf_get_fdt_node (const void *fit, int noffset)
2377 {
2378 	return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2379 }
2380 
2381 /**
2382  * fit_conf_print - prints out the FIT configuration details
2383  * @fit: pointer to the FIT format image header
2384  * @conf_noffset: offset of the configuration node
2385  * @p: pointer to prefix string
2386  *
2387  * fit_conf_print() lists all mandatory properies for the processed
2388  * configuration node.
2389  *
2390  * returns:
2391  *     no returned results
2392  */
2393 void fit_conf_print (const void *fit, int noffset, const char *p)
2394 {
2395 	char *desc;
2396 	char *uname;
2397 	int ret;
2398 
2399 	/* Mandatory properties */
2400 	ret = fit_get_desc (fit, noffset, &desc);
2401 	printf ("%s  Description:  ", p);
2402 	if (ret)
2403 		printf ("unavailable\n");
2404 	else
2405 		printf ("%s\n", desc);
2406 
2407 	uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2408 	printf ("%s  Kernel:       ", p);
2409 	if (uname == NULL)
2410 		printf ("unavailable\n");
2411 	else
2412 		printf ("%s\n", uname);
2413 
2414 	/* Optional properties */
2415 	uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2416 	if (uname)
2417 		printf ("%s  Init Ramdisk: %s\n", p, uname);
2418 
2419 	uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2420 	if (uname)
2421 		printf ("%s  FDT:          %s\n", p, uname);
2422 }
2423 
2424 /**
2425  * fit_check_ramdisk - verify FIT format ramdisk subimage
2426  * @fit_hdr: pointer to the FIT ramdisk header
2427  * @rd_noffset: ramdisk subimage node offset within FIT image
2428  * @arch: requested ramdisk image architecture type
2429  * @verify: data CRC verification flag
2430  *
2431  * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2432  * specified FIT image.
2433  *
2434  * returns:
2435  *     1, on success
2436  *     0, on failure
2437  */
2438 #ifndef USE_HOSTCC
2439 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2440 {
2441 	fit_image_print (fit, rd_noffset, "   ");
2442 
2443 	if (verify) {
2444 		puts ("   Verifying Hash Integrity ... ");
2445 		if (!fit_image_check_hashes (fit, rd_noffset)) {
2446 			puts ("Bad Data Hash\n");
2447 			return 0;
2448 		}
2449 		puts ("OK\n");
2450 	}
2451 
2452 	if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2453 	    !fit_image_check_arch (fit, rd_noffset, arch) ||
2454 	    !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2455 		printf ("No Linux %s Ramdisk Image\n",
2456 				genimg_get_arch_name(arch));
2457 		return 0;
2458 	}
2459 
2460 	return 1;
2461 }
2462 #endif /* USE_HOSTCC */
2463 #endif /* CONFIG_FIT */
2464