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