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