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