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