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