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