xref: /openbmc/u-boot/common/image.c (revision 3d0ea311)
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(CFG_SDRAM_BASE)
430 	return CFG_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 CFG_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 = CFG_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 CFG_FDT_PAD
1158 #define CFG_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 CFG_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 + CFG_FDT_PAD) >=
1206 			((char *)CFG_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 + CFG_FDT_PAD;
1217 		of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
1218 				(CFG_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 = (CFG_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, CFG_BARGSIZE, 0xf,
1602 					 CFG_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 				      CFG_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.
1856  *
1857  * returns:
1858  *     no returned results
1859  */
1860 void fit_image_print (const void *fit, int image_noffset, const char *p)
1861 {
1862 	char *desc;
1863 	uint8_t type, arch, os, comp;
1864 	size_t size;
1865 	ulong load, entry;
1866 	const void *data;
1867 	int noffset;
1868 	int ndepth;
1869 	int ret;
1870 
1871 	/* Mandatory properties */
1872 	ret = fit_get_desc (fit, image_noffset, &desc);
1873 	printf ("%s  Description:  ", p);
1874 	if (ret)
1875 		printf ("unavailable\n");
1876 	else
1877 		printf ("%s\n", desc);
1878 
1879 	fit_image_get_type (fit, image_noffset, &type);
1880 	printf ("%s  Type:         %s\n", p, genimg_get_type_name (type));
1881 
1882 	fit_image_get_comp (fit, image_noffset, &comp);
1883 	printf ("%s  Compression:  %s\n", p, genimg_get_comp_name (comp));
1884 
1885 	ret = fit_image_get_data (fit, image_noffset, &data, &size);
1886 
1887 #ifndef USE_HOSTCC
1888 	printf ("%s  Data Start:   ", p);
1889 	if (ret)
1890 		printf ("unavailable\n");
1891 	else
1892 		printf ("0x%08lx\n", (ulong)data);
1893 #endif
1894 
1895 	printf ("%s  Data Size:    ", p);
1896 	if (ret)
1897 		printf ("unavailable\n");
1898 	else
1899 		genimg_print_size (size);
1900 
1901 	/* Remaining, type dependent properties */
1902 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1903 	    (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1904 	    (type == IH_TYPE_FLATDT)) {
1905 		fit_image_get_arch (fit, image_noffset, &arch);
1906 		printf ("%s  Architecture: %s\n", p, genimg_get_arch_name (arch));
1907 	}
1908 
1909 	if (type == IH_TYPE_KERNEL) {
1910 		fit_image_get_os (fit, image_noffset, &os);
1911 		printf ("%s  OS:           %s\n", p, genimg_get_os_name (os));
1912 	}
1913 
1914 	if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1915 		ret = fit_image_get_load (fit, image_noffset, &load);
1916 		printf ("%s  Load Address: ", p);
1917 		if (ret)
1918 			printf ("unavailable\n");
1919 		else
1920 			printf ("0x%08lx\n", load);
1921 
1922 		fit_image_get_entry (fit, image_noffset, &entry);
1923 		printf ("%s  Entry Point:  ", p);
1924 		if (ret)
1925 			printf ("unavailable\n");
1926 		else
1927 			printf ("0x%08lx\n", entry);
1928 	}
1929 
1930 	/* Process all hash subnodes of the component image node */
1931 	for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1932 	     (noffset >= 0) && (ndepth > 0);
1933 	     noffset = fdt_next_node (fit, noffset, &ndepth)) {
1934 		if (ndepth == 1) {
1935 			/* Direct child node of the component image node */
1936 			fit_image_print_hash (fit, noffset, p);
1937 		}
1938 	}
1939 }
1940 
1941 /**
1942  * fit_image_print_hash - prints out the hash node details
1943  * @fit: pointer to the FIT format image header
1944  * @noffset: offset of the hash node
1945  * @p: pointer to prefix string
1946  *
1947  * fit_image_print_hash() lists properies for the processed hash node
1948  *
1949  * returns:
1950  *     no returned results
1951  */
1952 void fit_image_print_hash (const void *fit, int noffset, const char *p)
1953 {
1954 	char *algo;
1955 	uint8_t *value;
1956 	int value_len;
1957 	int i, ret;
1958 
1959 	/*
1960 	 * Check subnode name, must be equal to "hash".
1961 	 * Multiple hash nodes require unique unit node
1962 	 * names, e.g. hash@1, hash@2, etc.
1963 	 */
1964 	if (strncmp (fit_get_name(fit, noffset, NULL),
1965 			FIT_HASH_NODENAME,
1966 			strlen(FIT_HASH_NODENAME)) != 0)
1967 		return;
1968 
1969 	debug ("%s  Hash node:    '%s'\n", p,
1970 			fit_get_name (fit, noffset, NULL));
1971 
1972 	printf ("%s  Hash algo:    ", p);
1973 	if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1974 		printf ("invalid/unsupported\n");
1975 		return;
1976 	}
1977 	printf ("%s\n", algo);
1978 
1979 	ret = fit_image_hash_get_value (fit, noffset, &value,
1980 					&value_len);
1981 	printf ("%s  Hash value:   ", p);
1982 	if (ret) {
1983 		printf ("unavailable\n");
1984 	} else {
1985 		for (i = 0; i < value_len; i++)
1986 			printf ("%02x", value[i]);
1987 		printf ("\n");
1988 	}
1989 
1990 	debug  ("%s  Hash len:     %d\n", p, value_len);
1991 }
1992 
1993 /**
1994  * fit_get_desc - get node description property
1995  * @fit: pointer to the FIT format image header
1996  * @noffset: node offset
1997  * @desc: double pointer to the char, will hold pointer to the descrption
1998  *
1999  * fit_get_desc() reads description property from a given node, if
2000  * description is found pointer to it is returened in third call argument.
2001  *
2002  * returns:
2003  *     0, on success
2004  *     -1, on failure
2005  */
2006 int fit_get_desc (const void *fit, int noffset, char **desc)
2007 {
2008 	int len;
2009 
2010 	*desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2011 	if (*desc == NULL) {
2012 		fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2013 		return -1;
2014 	}
2015 
2016 	return 0;
2017 }
2018 
2019 /**
2020  * fit_get_timestamp - get node timestamp property
2021  * @fit: pointer to the FIT format image header
2022  * @noffset: node offset
2023  * @timestamp: pointer to the time_t, will hold read timestamp
2024  *
2025  * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2026  * is found and has a correct size its value is retured in third call
2027  * argument.
2028  *
2029  * returns:
2030  *     0, on success
2031  *     -1, on property read failure
2032  *     -2, on wrong timestamp size
2033  */
2034 int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2035 {
2036 	int len;
2037 	const void *data;
2038 
2039 	data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2040 	if (data == NULL) {
2041 		fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2042 		return -1;
2043 	}
2044 	if (len != sizeof (uint32_t)) {
2045 		debug ("FIT timestamp with incorrect size of (%u)\n", len);
2046 		return -2;
2047 	}
2048 
2049 	*timestamp = uimage_to_cpu (*((uint32_t *)data));
2050 	return 0;
2051 }
2052 
2053 /**
2054  * fit_image_get_node - get node offset for component image of a given unit name
2055  * @fit: pointer to the FIT format image header
2056  * @image_uname: component image node unit name
2057  *
2058  * fit_image_get_node() finds a component image (withing the '/images'
2059  * node) of a provided unit name. If image is found its node offset is
2060  * returned to the caller.
2061  *
2062  * returns:
2063  *     image node offset when found (>=0)
2064  *     negative number on failure (FDT_ERR_* code)
2065  */
2066 int fit_image_get_node (const void *fit, const char *image_uname)
2067 {
2068 	int noffset, images_noffset;
2069 
2070 	images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2071 	if (images_noffset < 0) {
2072 		debug ("Can't find images parent node '%s' (%s)\n",
2073 			FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2074 		return images_noffset;
2075 	}
2076 
2077 	noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2078 	if (noffset < 0) {
2079 		debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2080 			image_uname, fdt_strerror (noffset));
2081 	}
2082 
2083 	return noffset;
2084 }
2085 
2086 /**
2087  * fit_image_get_os - get os id for a given component image node
2088  * @fit: pointer to the FIT format image header
2089  * @noffset: component image node offset
2090  * @os: pointer to the uint8_t, will hold os numeric id
2091  *
2092  * fit_image_get_os() finds os property in a given component image node.
2093  * If the property is found, its (string) value is translated to the numeric
2094  * id which is returned to the caller.
2095  *
2096  * returns:
2097  *     0, on success
2098  *     -1, on failure
2099  */
2100 int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2101 {
2102 	int len;
2103 	const void *data;
2104 
2105 	/* Get OS name from property data */
2106 	data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2107 	if (data == NULL) {
2108 		fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2109 		*os = -1;
2110 		return -1;
2111 	}
2112 
2113 	/* Translate OS name to id */
2114 	*os = genimg_get_os_id (data);
2115 	return 0;
2116 }
2117 
2118 /**
2119  * fit_image_get_arch - get arch id for a given component image node
2120  * @fit: pointer to the FIT format image header
2121  * @noffset: component image node offset
2122  * @arch: pointer to the uint8_t, will hold arch numeric id
2123  *
2124  * fit_image_get_arch() finds arch property in a given component image node.
2125  * If the property is found, its (string) value is translated to the numeric
2126  * id which is returned to the caller.
2127  *
2128  * returns:
2129  *     0, on success
2130  *     -1, on failure
2131  */
2132 int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2133 {
2134 	int len;
2135 	const void *data;
2136 
2137 	/* Get architecture name from property data */
2138 	data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2139 	if (data == NULL) {
2140 		fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2141 		*arch = -1;
2142 		return -1;
2143 	}
2144 
2145 	/* Translate architecture name to id */
2146 	*arch = genimg_get_arch_id (data);
2147 	return 0;
2148 }
2149 
2150 /**
2151  * fit_image_get_type - get type id for a given component image node
2152  * @fit: pointer to the FIT format image header
2153  * @noffset: component image node offset
2154  * @type: pointer to the uint8_t, will hold type numeric id
2155  *
2156  * fit_image_get_type() finds type property in a given component image node.
2157  * If the property is found, its (string) value is translated to the numeric
2158  * id which is returned to the caller.
2159  *
2160  * returns:
2161  *     0, on success
2162  *     -1, on failure
2163  */
2164 int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2165 {
2166 	int len;
2167 	const void *data;
2168 
2169 	/* Get image type name from property data */
2170 	data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2171 	if (data == NULL) {
2172 		fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2173 		*type = -1;
2174 		return -1;
2175 	}
2176 
2177 	/* Translate image type name to id */
2178 	*type = genimg_get_type_id (data);
2179 	return 0;
2180 }
2181 
2182 /**
2183  * fit_image_get_comp - get comp id for a given component image node
2184  * @fit: pointer to the FIT format image header
2185  * @noffset: component image node offset
2186  * @comp: pointer to the uint8_t, will hold comp numeric id
2187  *
2188  * fit_image_get_comp() finds comp property in a given component image node.
2189  * If the property is found, its (string) value is translated to the numeric
2190  * id which is returned to the caller.
2191  *
2192  * returns:
2193  *     0, on success
2194  *     -1, on failure
2195  */
2196 int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2197 {
2198 	int len;
2199 	const void *data;
2200 
2201 	/* Get compression name from property data */
2202 	data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2203 	if (data == NULL) {
2204 		fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2205 		*comp = -1;
2206 		return -1;
2207 	}
2208 
2209 	/* Translate compression name to id */
2210 	*comp = genimg_get_comp_id (data);
2211 	return 0;
2212 }
2213 
2214 /**
2215  * fit_image_get_load - get load address property for a given component image node
2216  * @fit: pointer to the FIT format image header
2217  * @noffset: component image node offset
2218  * @load: pointer to the uint32_t, will hold load address
2219  *
2220  * fit_image_get_load() finds load address property in a given component image node.
2221  * If the property is found, its value is returned to the caller.
2222  *
2223  * returns:
2224  *     0, on success
2225  *     -1, on failure
2226  */
2227 int fit_image_get_load (const void *fit, int noffset, ulong *load)
2228 {
2229 	int len;
2230 	const uint32_t *data;
2231 
2232 	data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2233 	if (data == NULL) {
2234 		fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2235 		return -1;
2236 	}
2237 
2238 	*load = uimage_to_cpu (*data);
2239 	return 0;
2240 }
2241 
2242 /**
2243  * fit_image_get_entry - get entry point address property for a given component image node
2244  * @fit: pointer to the FIT format image header
2245  * @noffset: component image node offset
2246  * @entry: pointer to the uint32_t, will hold entry point address
2247  *
2248  * fit_image_get_entry() finds entry point address property in a given component image node.
2249  * If the property is found, its value is returned to the caller.
2250  *
2251  * returns:
2252  *     0, on success
2253  *     -1, on failure
2254  */
2255 int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2256 {
2257 	int len;
2258 	const uint32_t *data;
2259 
2260 	data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2261 	if (data == NULL) {
2262 		fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2263 		return -1;
2264 	}
2265 
2266 	*entry = uimage_to_cpu (*data);
2267 	return 0;
2268 }
2269 
2270 /**
2271  * fit_image_get_data - get data property and its size for a given component image node
2272  * @fit: pointer to the FIT format image header
2273  * @noffset: component image node offset
2274  * @data: double pointer to void, will hold data property's data address
2275  * @size: pointer to size_t, will hold data property's data size
2276  *
2277  * fit_image_get_data() finds data property in a given component image node.
2278  * If the property is found its data start address and size are returned to
2279  * the caller.
2280  *
2281  * returns:
2282  *     0, on success
2283  *     -1, on failure
2284  */
2285 int fit_image_get_data (const void *fit, int noffset,
2286 		const void **data, size_t *size)
2287 {
2288 	int len;
2289 
2290 	*data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2291 	if (*data == NULL) {
2292 		fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2293 		*size = 0;
2294 		return -1;
2295 	}
2296 
2297 	*size = len;
2298 	return 0;
2299 }
2300 
2301 /**
2302  * fit_image_hash_get_algo - get hash algorithm name
2303  * @fit: pointer to the FIT format image header
2304  * @noffset: hash node offset
2305  * @algo: double pointer to char, will hold pointer to the algorithm name
2306  *
2307  * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2308  * If the property is found its data start address is returned to the caller.
2309  *
2310  * returns:
2311  *     0, on success
2312  *     -1, on failure
2313  */
2314 int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2315 {
2316 	int len;
2317 
2318 	*algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2319 	if (*algo == NULL) {
2320 		fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2321 		return -1;
2322 	}
2323 
2324 	return 0;
2325 }
2326 
2327 /**
2328  * fit_image_hash_get_value - get hash value and length
2329  * @fit: pointer to the FIT format image header
2330  * @noffset: hash node offset
2331  * @value: double pointer to uint8_t, will hold address of a hash value data
2332  * @value_len: pointer to an int, will hold hash data length
2333  *
2334  * fit_image_hash_get_value() finds hash value property in a given hash node.
2335  * If the property is found its data start address and size are returned to
2336  * the caller.
2337  *
2338  * returns:
2339  *     0, on success
2340  *     -1, on failure
2341  */
2342 int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2343 				int *value_len)
2344 {
2345 	int len;
2346 
2347 	*value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2348 	if (*value == NULL) {
2349 		fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2350 		*value_len = 0;
2351 		return -1;
2352 	}
2353 
2354 	*value_len = len;
2355 	return 0;
2356 }
2357 
2358 /**
2359  * fit_set_timestamp - set node timestamp property
2360  * @fit: pointer to the FIT format image header
2361  * @noffset: node offset
2362  * @timestamp: timestamp value to be set
2363  *
2364  * fit_set_timestamp() attempts to set timestamp property in the requested
2365  * node and returns operation status to the caller.
2366  *
2367  * returns:
2368  *     0, on success
2369  *     -1, on property read failure
2370  */
2371 int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2372 {
2373 	uint32_t t;
2374 	int ret;
2375 
2376 	t = cpu_to_uimage (timestamp);
2377 	ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2378 				sizeof (uint32_t));
2379 	if (ret) {
2380 		printf ("Can't set '%s' property for '%s' node (%s)\n",
2381 			FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2382 			fdt_strerror (ret));
2383 		return -1;
2384 	}
2385 
2386 	return 0;
2387 }
2388 
2389 /**
2390  * calculate_hash - calculate and return hash for provided input data
2391  * @data: pointer to the input data
2392  * @data_len: data length
2393  * @algo: requested hash algorithm
2394  * @value: pointer to the char, will hold hash value data (caller must
2395  * allocate enough free space)
2396  * value_len: length of the calculated hash
2397  *
2398  * calculate_hash() computes input data hash according to the requested algorithm.
2399  * Resulting hash value is placed in caller provided 'value' buffer, length
2400  * of the calculated hash is returned via value_len pointer argument.
2401  *
2402  * returns:
2403  *     0, on success
2404  *    -1, when algo is unsupported
2405  */
2406 static int calculate_hash (const void *data, int data_len, const char *algo,
2407 			uint8_t *value, int *value_len)
2408 {
2409 	if (strcmp (algo, "crc32") == 0 ) {
2410 		*((uint32_t *)value) = crc32_wd (0, data, data_len,
2411 							CHUNKSZ_CRC32);
2412 		*((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2413 		*value_len = 4;
2414 	} else if (strcmp (algo, "sha1") == 0 ) {
2415 		sha1_csum_wd ((unsigned char *) data, data_len,
2416 				(unsigned char *) value, CHUNKSZ_SHA1);
2417 		*value_len = 20;
2418 	} else if (strcmp (algo, "md5") == 0 ) {
2419 		md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
2420 		*value_len = 16;
2421 	} else {
2422 		debug ("Unsupported hash alogrithm\n");
2423 		return -1;
2424 	}
2425 	return 0;
2426 }
2427 
2428 #ifdef USE_HOSTCC
2429 /**
2430  * fit_set_hashes - process FIT component image nodes and calculate hashes
2431  * @fit: pointer to the FIT format image header
2432  *
2433  * fit_set_hashes() adds hash values for all component images in the FIT blob.
2434  * Hashes are calculated for all component images which have hash subnodes
2435  * with algorithm property set to one of the supported hash algorithms.
2436  *
2437  * returns
2438  *     0, on success
2439  *     libfdt error code, on failure
2440  */
2441 int fit_set_hashes (void *fit)
2442 {
2443 	int images_noffset;
2444 	int noffset;
2445 	int ndepth;
2446 	int ret;
2447 
2448 	/* Find images parent node offset */
2449 	images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2450 	if (images_noffset < 0) {
2451 		printf ("Can't find images parent node '%s' (%s)\n",
2452 			FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2453 		return images_noffset;
2454 	}
2455 
2456 	/* Process its subnodes, print out component images details */
2457 	for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2458 	     (noffset >= 0) && (ndepth > 0);
2459 	     noffset = fdt_next_node (fit, noffset, &ndepth)) {
2460 		if (ndepth == 1) {
2461 			/*
2462 			 * Direct child node of the images parent node,
2463 			 * i.e. component image node.
2464 			 */
2465 			ret = fit_image_set_hashes (fit, noffset);
2466 			if (ret)
2467 				return ret;
2468 		}
2469 	}
2470 
2471 	return 0;
2472 }
2473 
2474 /**
2475  * fit_image_set_hashes - calculate/set hashes for given component image node
2476  * @fit: pointer to the FIT format image header
2477  * @image_noffset: requested component image node
2478  *
2479  * fit_image_set_hashes() adds hash values for an component image node. All
2480  * existing hash subnodes are checked, if algorithm property is set to one of
2481  * the supported hash algorithms, hash value is computed and corresponding
2482  * hash node property is set, for example:
2483  *
2484  * Input component image node structure:
2485  *
2486  * o image@1 (at image_noffset)
2487  *   | - data = [binary data]
2488  *   o hash@1
2489  *     |- algo = "sha1"
2490  *
2491  * Output component image node structure:
2492  *
2493  * o image@1 (at image_noffset)
2494  *   | - data = [binary data]
2495  *   o hash@1
2496  *     |- algo = "sha1"
2497  *     |- value = sha1(data)
2498  *
2499  * returns:
2500  *     0 on sucess
2501  *    <0 on failure
2502  */
2503 int fit_image_set_hashes (void *fit, int image_noffset)
2504 {
2505 	const void *data;
2506 	size_t size;
2507 	char *algo;
2508 	uint8_t value[FIT_MAX_HASH_LEN];
2509 	int value_len;
2510 	int noffset;
2511 	int ndepth;
2512 
2513 	/* Get image data and data length */
2514 	if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2515 		printf ("Can't get image data/size\n");
2516 		return -1;
2517 	}
2518 
2519 	/* Process all hash subnodes of the component image node */
2520 	for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2521 	     (noffset >= 0) && (ndepth > 0);
2522 	     noffset = fdt_next_node (fit, noffset, &ndepth)) {
2523 		if (ndepth == 1) {
2524 			/* Direct child node of the component image node */
2525 
2526 			/*
2527 			 * Check subnode name, must be equal to "hash".
2528 			 * Multiple hash nodes require unique unit node
2529 			 * names, e.g. hash@1, hash@2, etc.
2530 			 */
2531 			if (strncmp (fit_get_name(fit, noffset, NULL),
2532 						FIT_HASH_NODENAME,
2533 						strlen(FIT_HASH_NODENAME)) != 0) {
2534 				/* Not a hash subnode, skip it */
2535 				continue;
2536 			}
2537 
2538 			if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2539 				printf ("Can't get hash algo property for "
2540 					"'%s' hash node in '%s' image node\n",
2541 					fit_get_name (fit, noffset, NULL),
2542 					fit_get_name (fit, image_noffset, NULL));
2543 				return -1;
2544 			}
2545 
2546 			if (calculate_hash (data, size, algo, value, &value_len)) {
2547 				printf ("Unsupported hash algorithm (%s) for "
2548 					"'%s' hash node in '%s' image node\n",
2549 					algo, fit_get_name (fit, noffset, NULL),
2550 					fit_get_name (fit, image_noffset, NULL));
2551 				return -1;
2552 			}
2553 
2554 			if (fit_image_hash_set_value (fit, noffset, value,
2555 							value_len)) {
2556 				printf ("Can't set hash value for "
2557 					"'%s' hash node in '%s' image node\n",
2558 					fit_get_name (fit, noffset, NULL),
2559 					fit_get_name (fit, image_noffset, NULL));
2560 				return -1;
2561 			}
2562 		}
2563 	}
2564 
2565 	return 0;
2566 }
2567 
2568 /**
2569  * fit_image_hash_set_value - set hash value in requested has node
2570  * @fit: pointer to the FIT format image header
2571  * @noffset: hash node offset
2572  * @value: hash value to be set
2573  * @value_len: hash value length
2574  *
2575  * fit_image_hash_set_value() attempts to set hash value in a node at offset
2576  * given and returns operation status to the caller.
2577  *
2578  * returns
2579  *     0, on success
2580  *     -1, on failure
2581  */
2582 int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2583 				int value_len)
2584 {
2585 	int ret;
2586 
2587 	ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2588 	if (ret) {
2589 		printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2590 			FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2591 			fdt_strerror (ret));
2592 		return -1;
2593 	}
2594 
2595 	return 0;
2596 }
2597 #endif /* USE_HOSTCC */
2598 
2599 /**
2600  * fit_image_check_hashes - verify data intergity
2601  * @fit: pointer to the FIT format image header
2602  * @image_noffset: component image node offset
2603  *
2604  * fit_image_check_hashes() goes over component image hash nodes,
2605  * re-calculates each data hash and compares with the value stored in hash
2606  * node.
2607  *
2608  * returns:
2609  *     1, if all hashes are valid
2610  *     0, otherwise (or on error)
2611  */
2612 int fit_image_check_hashes (const void *fit, int image_noffset)
2613 {
2614 	const void	*data;
2615 	size_t		size;
2616 	char		*algo;
2617 	uint8_t		*fit_value;
2618 	int		fit_value_len;
2619 	uint8_t		value[FIT_MAX_HASH_LEN];
2620 	int		value_len;
2621 	int		noffset;
2622 	int		ndepth;
2623 	char		*err_msg = "";
2624 
2625 	/* Get image data and data length */
2626 	if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2627 		printf ("Can't get image data/size\n");
2628 		return 0;
2629 	}
2630 
2631 	/* Process all hash subnodes of the component image node */
2632 	for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2633 	     (noffset >= 0) && (ndepth > 0);
2634 	     noffset = fdt_next_node (fit, noffset, &ndepth)) {
2635 		if (ndepth == 1) {
2636 			/* Direct child node of the component image node */
2637 
2638 			/*
2639 			 * Check subnode name, must be equal to "hash".
2640 			 * Multiple hash nodes require unique unit node
2641 			 * names, e.g. hash@1, hash@2, etc.
2642 			 */
2643 			if (strncmp (fit_get_name(fit, noffset, NULL),
2644 					FIT_HASH_NODENAME,
2645 					strlen(FIT_HASH_NODENAME)) != 0)
2646 				continue;
2647 
2648 			if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2649 				err_msg = " error!\nCan't get hash algo "
2650 						"property";
2651 				goto error;
2652 			}
2653 			printf ("%s", algo);
2654 
2655 			if (fit_image_hash_get_value (fit, noffset, &fit_value,
2656 							&fit_value_len)) {
2657 				err_msg = " error!\nCan't get hash value "
2658 						"property";
2659 				goto error;
2660 			}
2661 
2662 			if (calculate_hash (data, size, algo, value, &value_len)) {
2663 				err_msg = " error!\nUnsupported hash algorithm";
2664 				goto error;
2665 			}
2666 
2667 			if (value_len != fit_value_len) {
2668 				err_msg = " error !\nBad hash value len";
2669 				goto error;
2670 			} else if (memcmp (value, fit_value, value_len) != 0) {
2671 				err_msg = " error!\nBad hash value";
2672 				goto error;
2673 			}
2674 			printf ("+ ");
2675 		}
2676 	}
2677 
2678 	return 1;
2679 
2680 error:
2681 	printf ("%s for '%s' hash node in '%s' image node\n",
2682 			err_msg, fit_get_name (fit, noffset, NULL),
2683 			fit_get_name (fit, image_noffset, NULL));
2684 	return 0;
2685 }
2686 
2687 /**
2688  * fit_all_image_check_hashes - verify data intergity for all images
2689  * @fit: pointer to the FIT format image header
2690  *
2691  * fit_all_image_check_hashes() goes over all images in the FIT and
2692  * for every images checks if all it's hashes are valid.
2693  *
2694  * returns:
2695  *     1, if all hashes of all images are valid
2696  *     0, otherwise (or on error)
2697  */
2698 int fit_all_image_check_hashes (const void *fit)
2699 {
2700 	int images_noffset;
2701 	int noffset;
2702 	int ndepth;
2703 	int count;
2704 
2705 	/* Find images parent node offset */
2706 	images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2707 	if (images_noffset < 0) {
2708 		printf ("Can't find images parent node '%s' (%s)\n",
2709 			FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2710 		return 0;
2711 	}
2712 
2713 	/* Process all image subnodes, check hashes for each */
2714 	printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2715 		(ulong)fit);
2716 	for (ndepth = 0, count = 0,
2717 		noffset = fdt_next_node (fit, images_noffset, &ndepth);
2718 		(noffset >= 0) && (ndepth > 0);
2719 		noffset = fdt_next_node (fit, noffset, &ndepth)) {
2720 		if (ndepth == 1) {
2721 			/*
2722 			 * Direct child node of the images parent node,
2723 			 * i.e. component image node.
2724 			 */
2725 			printf ("   Hash(es) for Image %u (%s): ", count++,
2726 					fit_get_name (fit, noffset, NULL));
2727 
2728 			if (!fit_image_check_hashes (fit, noffset))
2729 				return 0;
2730 			printf ("\n");
2731 		}
2732 	}
2733 	return 1;
2734 }
2735 
2736 /**
2737  * fit_image_check_os - check whether image node is of a given os type
2738  * @fit: pointer to the FIT format image header
2739  * @noffset: component image node offset
2740  * @os: requested image os
2741  *
2742  * fit_image_check_os() reads image os property and compares its numeric
2743  * id with the requested os. Comparison result is returned to the caller.
2744  *
2745  * returns:
2746  *     1 if image is of given os type
2747  *     0 otherwise (or on error)
2748  */
2749 int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2750 {
2751 	uint8_t image_os;
2752 
2753 	if (fit_image_get_os (fit, noffset, &image_os))
2754 		return 0;
2755 	return (os == image_os);
2756 }
2757 
2758 /**
2759  * fit_image_check_arch - check whether image node is of a given arch
2760  * @fit: pointer to the FIT format image header
2761  * @noffset: component image node offset
2762  * @arch: requested imagearch
2763  *
2764  * fit_image_check_arch() reads image arch property and compares its numeric
2765  * id with the requested arch. Comparison result is returned to the caller.
2766  *
2767  * returns:
2768  *     1 if image is of given arch
2769  *     0 otherwise (or on error)
2770  */
2771 int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2772 {
2773 	uint8_t image_arch;
2774 
2775 	if (fit_image_get_arch (fit, noffset, &image_arch))
2776 		return 0;
2777 	return (arch == image_arch);
2778 }
2779 
2780 /**
2781  * fit_image_check_type - check whether image node is of a given type
2782  * @fit: pointer to the FIT format image header
2783  * @noffset: component image node offset
2784  * @type: requested image type
2785  *
2786  * fit_image_check_type() reads image type property and compares its numeric
2787  * id with the requested type. Comparison result is returned to the caller.
2788  *
2789  * returns:
2790  *     1 if image is of given type
2791  *     0 otherwise (or on error)
2792  */
2793 int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2794 {
2795 	uint8_t image_type;
2796 
2797 	if (fit_image_get_type (fit, noffset, &image_type))
2798 		return 0;
2799 	return (type == image_type);
2800 }
2801 
2802 /**
2803  * fit_image_check_comp - check whether image node uses given compression
2804  * @fit: pointer to the FIT format image header
2805  * @noffset: component image node offset
2806  * @comp: requested image compression type
2807  *
2808  * fit_image_check_comp() reads image compression property and compares its
2809  * numeric id with the requested compression type. Comparison result is
2810  * returned to the caller.
2811  *
2812  * returns:
2813  *     1 if image uses requested compression
2814  *     0 otherwise (or on error)
2815  */
2816 int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2817 {
2818 	uint8_t image_comp;
2819 
2820 	if (fit_image_get_comp (fit, noffset, &image_comp))
2821 		return 0;
2822 	return (comp == image_comp);
2823 }
2824 
2825 /**
2826  * fit_check_format - sanity check FIT image format
2827  * @fit: pointer to the FIT format image header
2828  *
2829  * fit_check_format() runs a basic sanity FIT image verification.
2830  * Routine checks for mandatory properties, nodes, etc.
2831  *
2832  * returns:
2833  *     1, on success
2834  *     0, on failure
2835  */
2836 int fit_check_format (const void *fit)
2837 {
2838 	/* mandatory / node 'description' property */
2839 	if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2840 		debug ("Wrong FIT format: no description\n");
2841 		return 0;
2842 	}
2843 
2844 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2845 	/* mandatory / node 'timestamp' property */
2846 	if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2847 		debug ("Wrong FIT format: no description\n");
2848 		return 0;
2849 	}
2850 #endif
2851 
2852 	/* mandatory subimages parent '/images' node */
2853 	if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2854 		debug ("Wrong FIT format: no images parent node\n");
2855 		return 0;
2856 	}
2857 
2858 	return 1;
2859 }
2860 
2861 /**
2862  * fit_conf_get_node - get node offset for configuration of a given unit name
2863  * @fit: pointer to the FIT format image header
2864  * @conf_uname: configuration node unit name
2865  *
2866  * fit_conf_get_node() finds a configuration (withing the '/configurations'
2867  * parant node) of a provided unit name. If configuration is found its node offset
2868  * is returned to the caller.
2869  *
2870  * When NULL is provided in second argument fit_conf_get_node() will search
2871  * for a default configuration node instead. Default configuration node unit name
2872  * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2873  *
2874  * returns:
2875  *     configuration node offset when found (>=0)
2876  *     negative number on failure (FDT_ERR_* code)
2877  */
2878 int fit_conf_get_node (const void *fit, const char *conf_uname)
2879 {
2880 	int noffset, confs_noffset;
2881 	int len;
2882 
2883 	confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2884 	if (confs_noffset < 0) {
2885 		debug ("Can't find configurations parent node '%s' (%s)\n",
2886 			FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2887 		return confs_noffset;
2888 	}
2889 
2890 	if (conf_uname == NULL) {
2891 		/* get configuration unit name from the default property */
2892 		debug ("No configuration specified, trying default...\n");
2893 		conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2894 		if (conf_uname == NULL) {
2895 			fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2896 			return len;
2897 		}
2898 		debug ("Found default configuration: '%s'\n", conf_uname);
2899 	}
2900 
2901 	noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2902 	if (noffset < 0) {
2903 		debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2904 			conf_uname, fdt_strerror (noffset));
2905 	}
2906 
2907 	return noffset;
2908 }
2909 
2910 static int __fit_conf_get_prop_node (const void *fit, int noffset,
2911 		const char *prop_name)
2912 {
2913 	char *uname;
2914 	int len;
2915 
2916 	/* get kernel image unit name from configuration kernel property */
2917 	uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2918 	if (uname == NULL)
2919 		return len;
2920 
2921 	return fit_image_get_node (fit, uname);
2922 }
2923 
2924 /**
2925  * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2926  * a given configuration
2927  * @fit: pointer to the FIT format image header
2928  * @noffset: configuration node offset
2929  *
2930  * fit_conf_get_kernel_node() retrives kernel image node unit name from
2931  * configuration FIT_KERNEL_PROP property and translates it to the node
2932  * offset.
2933  *
2934  * returns:
2935  *     image node offset when found (>=0)
2936  *     negative number on failure (FDT_ERR_* code)
2937  */
2938 int fit_conf_get_kernel_node (const void *fit, int noffset)
2939 {
2940 	return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2941 }
2942 
2943 /**
2944  * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2945  * a given configuration
2946  * @fit: pointer to the FIT format image header
2947  * @noffset: configuration node offset
2948  *
2949  * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2950  * configuration FIT_KERNEL_PROP property and translates it to the node
2951  * offset.
2952  *
2953  * returns:
2954  *     image node offset when found (>=0)
2955  *     negative number on failure (FDT_ERR_* code)
2956  */
2957 int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2958 {
2959 	return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2960 }
2961 
2962 /**
2963  * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2964  * a given configuration
2965  * @fit: pointer to the FIT format image header
2966  * @noffset: configuration node offset
2967  *
2968  * fit_conf_get_fdt_node() retrives fdt image node unit name from
2969  * configuration FIT_KERNEL_PROP property and translates it to the node
2970  * offset.
2971  *
2972  * returns:
2973  *     image node offset when found (>=0)
2974  *     negative number on failure (FDT_ERR_* code)
2975  */
2976 int fit_conf_get_fdt_node (const void *fit, int noffset)
2977 {
2978 	return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2979 }
2980 
2981 /**
2982  * fit_conf_print - prints out the FIT configuration details
2983  * @fit: pointer to the FIT format image header
2984  * @noffset: offset of the configuration node
2985  * @p: pointer to prefix string
2986  *
2987  * fit_conf_print() lists all mandatory properies for the processed
2988  * configuration node.
2989  *
2990  * returns:
2991  *     no returned results
2992  */
2993 void fit_conf_print (const void *fit, int noffset, const char *p)
2994 {
2995 	char *desc;
2996 	char *uname;
2997 	int ret;
2998 
2999 	/* Mandatory properties */
3000 	ret = fit_get_desc (fit, noffset, &desc);
3001 	printf ("%s  Description:  ", p);
3002 	if (ret)
3003 		printf ("unavailable\n");
3004 	else
3005 		printf ("%s\n", desc);
3006 
3007 	uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3008 	printf ("%s  Kernel:       ", p);
3009 	if (uname == NULL)
3010 		printf ("unavailable\n");
3011 	else
3012 		printf ("%s\n", uname);
3013 
3014 	/* Optional properties */
3015 	uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3016 	if (uname)
3017 		printf ("%s  Init Ramdisk: %s\n", p, uname);
3018 
3019 	uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3020 	if (uname)
3021 		printf ("%s  FDT:          %s\n", p, uname);
3022 }
3023 
3024 /**
3025  * fit_check_ramdisk - verify FIT format ramdisk subimage
3026  * @fit_hdr: pointer to the FIT ramdisk header
3027  * @rd_noffset: ramdisk subimage node offset within FIT image
3028  * @arch: requested ramdisk image architecture type
3029  * @verify: data CRC verification flag
3030  *
3031  * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3032  * specified FIT image.
3033  *
3034  * returns:
3035  *     1, on success
3036  *     0, on failure
3037  */
3038 #ifndef USE_HOSTCC
3039 static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3040 {
3041 	fit_image_print (fit, rd_noffset, "   ");
3042 
3043 	if (verify) {
3044 		puts ("   Verifying Hash Integrity ... ");
3045 		if (!fit_image_check_hashes (fit, rd_noffset)) {
3046 			puts ("Bad Data Hash\n");
3047 			show_boot_progress (-125);
3048 			return 0;
3049 		}
3050 		puts ("OK\n");
3051 	}
3052 
3053 	show_boot_progress (126);
3054 	if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3055 	    !fit_image_check_arch (fit, rd_noffset, arch) ||
3056 	    !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3057 		printf ("No Linux %s Ramdisk Image\n",
3058 				genimg_get_arch_name(arch));
3059 		show_boot_progress (-126);
3060 		return 0;
3061 	}
3062 
3063 	show_boot_progress (127);
3064 	return 1;
3065 }
3066 #endif /* USE_HOSTCC */
3067 #endif /* CONFIG_FIT */
3068