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