xref: /rk3399_rockchip-uboot/common/image.c (revision bbb467dc3c79537eebc4d3551576a17f627e5e41)
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 #include <rtc.h>
43 
44 #include <environment.h>
45 #include <image.h>
46 
47 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
48 #include <libfdt.h>
49 #include <fdt_support.h>
50 #endif
51 
52 #include <u-boot/md5.h>
53 #include <sha1.h>
54 
55 #ifdef CONFIG_CMD_BDI
56 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
57 #endif
58 
59 DECLARE_GLOBAL_DATA_PTR;
60 
61 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
62 						int verify);
63 #else
64 #include "mkimage.h"
65 #include <u-boot/md5.h>
66 #include <time.h>
67 #include <image.h>
68 #endif /* !USE_HOSTCC*/
69 
70 #include <u-boot/crc.h>
71 
72 static const table_entry_t uimage_arch[] = {
73 	{	IH_ARCH_INVALID,	NULL,		"Invalid ARCH",	},
74 	{	IH_ARCH_ALPHA,		"alpha",	"Alpha",	},
75 	{	IH_ARCH_ARM,		"arm",		"ARM",		},
76 	{	IH_ARCH_I386,		"x86",		"Intel x86",	},
77 	{	IH_ARCH_IA64,		"ia64",		"IA64",		},
78 	{	IH_ARCH_M68K,		"m68k",		"M68K",		},
79 	{	IH_ARCH_MICROBLAZE,	"microblaze",	"MicroBlaze",	},
80 	{	IH_ARCH_MIPS,		"mips",		"MIPS",		},
81 	{	IH_ARCH_MIPS64,		"mips64",	"MIPS 64 Bit",	},
82 	{	IH_ARCH_NIOS2,		"nios2",	"NIOS II",	},
83 	{	IH_ARCH_PPC,		"powerpc",	"PowerPC",	},
84 	{	IH_ARCH_PPC,		"ppc",		"PowerPC",	},
85 	{	IH_ARCH_S390,		"s390",		"IBM S390",	},
86 	{	IH_ARCH_SH,		"sh",		"SuperH",	},
87 	{	IH_ARCH_SPARC,		"sparc",	"SPARC",	},
88 	{	IH_ARCH_SPARC64,	"sparc64",	"SPARC 64 Bit",	},
89 	{	IH_ARCH_BLACKFIN,	"blackfin",	"Blackfin",	},
90 	{	IH_ARCH_AVR32,		"avr32",	"AVR32",	},
91 	{	IH_ARCH_NDS32,		"nds32",	"NDS32",	},
92 	{	IH_ARCH_OPENRISC,	"or1k",		"OpenRISC 1000",},
93 	{	-1,			"",		"",		},
94 };
95 
96 static const table_entry_t uimage_os[] = {
97 	{	IH_OS_INVALID,	NULL,		"Invalid OS",		},
98 	{	IH_OS_LINUX,	"linux",	"Linux",		},
99 #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
100 	{	IH_OS_LYNXOS,	"lynxos",	"LynxOS",		},
101 #endif
102 	{	IH_OS_NETBSD,	"netbsd",	"NetBSD",		},
103 	{	IH_OS_OSE,	"ose",		"Enea OSE",		},
104 	{	IH_OS_PLAN9,	"plan9",	"Plan 9",		},
105 	{	IH_OS_RTEMS,	"rtems",	"RTEMS",		},
106 	{	IH_OS_U_BOOT,	"u-boot",	"U-Boot",		},
107 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
108 	{	IH_OS_QNX,	"qnx",		"QNX",			},
109 	{	IH_OS_VXWORKS,	"vxworks",	"VxWorks",		},
110 #endif
111 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
112 	{	IH_OS_INTEGRITY,"integrity",	"INTEGRITY",		},
113 #endif
114 #ifdef USE_HOSTCC
115 	{	IH_OS_4_4BSD,	"4_4bsd",	"4_4BSD",		},
116 	{	IH_OS_DELL,	"dell",		"Dell",			},
117 	{	IH_OS_ESIX,	"esix",		"Esix",			},
118 	{	IH_OS_FREEBSD,	"freebsd",	"FreeBSD",		},
119 	{	IH_OS_IRIX,	"irix",		"Irix",			},
120 	{	IH_OS_NCR,	"ncr",		"NCR",			},
121 	{	IH_OS_OPENBSD,	"openbsd",	"OpenBSD",		},
122 	{	IH_OS_PSOS,	"psos",		"pSOS",			},
123 	{	IH_OS_SCO,	"sco",		"SCO",			},
124 	{	IH_OS_SOLARIS,	"solaris",	"Solaris",		},
125 	{	IH_OS_SVR4,	"svr4",		"SVR4",			},
126 #endif
127 	{	-1,		"",		"",			},
128 };
129 
130 static const table_entry_t uimage_type[] = {
131 	{	IH_TYPE_AISIMAGE,   "aisimage",   "Davinci AIS image",},
132 	{	IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image",	},
133 	{	IH_TYPE_FIRMWARE,   "firmware",	  "Firmware",		},
134 	{	IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",	},
135 	{	IH_TYPE_KERNEL,	    "kernel",	  "Kernel Image",	},
136 	{	IH_TYPE_KERNEL_NOLOAD, "kernel_noload",  "Kernel Image (no loading done)", },
137 	{	IH_TYPE_KWBIMAGE,   "kwbimage",   "Kirkwood Boot Image",},
138 	{	IH_TYPE_IMXIMAGE,   "imximage",   "Freescale i.MX Boot Image",},
139 	{	IH_TYPE_INVALID,    NULL,	  "Invalid Image",	},
140 	{	IH_TYPE_MULTI,	    "multi",	  "Multi-File Image",	},
141 	{	IH_TYPE_OMAPIMAGE,  "omapimage",  "TI OMAP SPL With GP CH",},
142 	{	IH_TYPE_PBLIMAGE,   "pblimage",   "Freescale PBL Boot Image",},
143 	{	IH_TYPE_RAMDISK,    "ramdisk",	  "RAMDisk Image",	},
144 	{	IH_TYPE_SCRIPT,     "script",	  "Script",		},
145 	{	IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
146 	{	IH_TYPE_UBLIMAGE,   "ublimage",   "Davinci UBL image",},
147 	{	-1,		    "",		  "",			},
148 };
149 
150 static const table_entry_t uimage_comp[] = {
151 	{	IH_COMP_NONE,	"none",		"uncompressed",		},
152 	{	IH_COMP_BZIP2,	"bzip2",	"bzip2 compressed",	},
153 	{	IH_COMP_GZIP,	"gzip",		"gzip compressed",	},
154 	{	IH_COMP_LZMA,	"lzma",		"lzma compressed",	},
155 	{	IH_COMP_LZO,	"lzo",		"lzo compressed",	},
156 	{	-1,		"",		"",			},
157 };
158 
159 /*****************************************************************************/
160 /* Legacy format routines */
161 /*****************************************************************************/
162 int image_check_hcrc(const image_header_t *hdr)
163 {
164 	ulong hcrc;
165 	ulong len = image_get_header_size();
166 	image_header_t header;
167 
168 	/* Copy header so we can blank CRC field for re-calculation */
169 	memmove(&header, (char *)hdr, image_get_header_size());
170 	image_set_hcrc(&header, 0);
171 
172 	hcrc = crc32(0, (unsigned char *)&header, len);
173 
174 	return (hcrc == image_get_hcrc(hdr));
175 }
176 
177 int image_check_dcrc(const image_header_t *hdr)
178 {
179 	ulong data = image_get_data(hdr);
180 	ulong len = image_get_data_size(hdr);
181 	ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
182 
183 	return (dcrc == image_get_dcrc(hdr));
184 }
185 
186 /**
187  * image_multi_count - get component (sub-image) count
188  * @hdr: pointer to the header of the multi component image
189  *
190  * image_multi_count() returns number of components in a multi
191  * component image.
192  *
193  * Note: no checking of the image type is done, caller must pass
194  * a valid multi component image.
195  *
196  * returns:
197  *     number of components
198  */
199 ulong image_multi_count(const image_header_t *hdr)
200 {
201 	ulong i, count = 0;
202 	uint32_t *size;
203 
204 	/* get start of the image payload, which in case of multi
205 	 * component images that points to a table of component sizes */
206 	size = (uint32_t *)image_get_data(hdr);
207 
208 	/* count non empty slots */
209 	for (i = 0; size[i]; ++i)
210 		count++;
211 
212 	return count;
213 }
214 
215 /**
216  * image_multi_getimg - get component data address and size
217  * @hdr: pointer to the header of the multi component image
218  * @idx: index of the requested component
219  * @data: pointer to a ulong variable, will hold component data address
220  * @len: pointer to a ulong variable, will hold component size
221  *
222  * image_multi_getimg() returns size and data address for the requested
223  * component in a multi component image.
224  *
225  * Note: no checking of the image type is done, caller must pass
226  * a valid multi component image.
227  *
228  * returns:
229  *     data address and size of the component, if idx is valid
230  *     0 in data and len, if idx is out of range
231  */
232 void image_multi_getimg(const image_header_t *hdr, ulong idx,
233 			ulong *data, ulong *len)
234 {
235 	int i;
236 	uint32_t *size;
237 	ulong offset, count, img_data;
238 
239 	/* get number of component */
240 	count = image_multi_count(hdr);
241 
242 	/* get start of the image payload, which in case of multi
243 	 * component images that points to a table of component sizes */
244 	size = (uint32_t *)image_get_data(hdr);
245 
246 	/* get address of the proper component data start, which means
247 	 * skipping sizes table (add 1 for last, null entry) */
248 	img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
249 
250 	if (idx < count) {
251 		*len = uimage_to_cpu(size[idx]);
252 		offset = 0;
253 
254 		/* go over all indices preceding requested component idx */
255 		for (i = 0; i < idx; i++) {
256 			/* add up i-th component size, rounding up to 4 bytes */
257 			offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
258 		}
259 
260 		/* calculate idx-th component data address */
261 		*data = img_data + offset;
262 	} else {
263 		*len = 0;
264 		*data = 0;
265 	}
266 }
267 
268 static void image_print_type(const image_header_t *hdr)
269 {
270 	const char *os, *arch, *type, *comp;
271 
272 	os = genimg_get_os_name(image_get_os(hdr));
273 	arch = genimg_get_arch_name(image_get_arch(hdr));
274 	type = genimg_get_type_name(image_get_type(hdr));
275 	comp = genimg_get_comp_name(image_get_comp(hdr));
276 
277 	printf("%s %s %s (%s)\n", arch, os, type, comp);
278 }
279 
280 /**
281  * image_print_contents - prints out the contents of the legacy format image
282  * @ptr: pointer to the legacy format image header
283  * @p: pointer to prefix string
284  *
285  * image_print_contents() formats a multi line legacy image contents description.
286  * The routine prints out all header fields followed by the size/offset data
287  * for MULTI/SCRIPT images.
288  *
289  * returns:
290  *     no returned results
291  */
292 void image_print_contents(const void *ptr)
293 {
294 	const image_header_t *hdr = (const image_header_t *)ptr;
295 	const char *p;
296 
297 #ifdef USE_HOSTCC
298 	p = "";
299 #else
300 	p = "   ";
301 #endif
302 
303 	printf("%sImage Name:   %.*s\n", p, IH_NMLEN, image_get_name(hdr));
304 	if (IMAGE_ENABLE_TIMESTAMP) {
305 		printf("%sCreated:      ", p);
306 		genimg_print_time((time_t)image_get_time(hdr));
307 	}
308 	printf("%sImage Type:   ", p);
309 	image_print_type(hdr);
310 	printf("%sData Size:    ", p);
311 	genimg_print_size(image_get_data_size(hdr));
312 	printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
313 	printf("%sEntry Point:  %08x\n", p, image_get_ep(hdr));
314 
315 	if (image_check_type(hdr, IH_TYPE_MULTI) ||
316 			image_check_type(hdr, IH_TYPE_SCRIPT)) {
317 		int i;
318 		ulong data, len;
319 		ulong count = image_multi_count(hdr);
320 
321 		printf("%sContents:\n", p);
322 		for (i = 0; i < count; i++) {
323 			image_multi_getimg(hdr, i, &data, &len);
324 
325 			printf("%s   Image %d: ", p, i);
326 			genimg_print_size(len);
327 
328 			if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
329 				/*
330 				 * the user may need to know offsets
331 				 * if planning to do something with
332 				 * multiple files
333 				 */
334 				printf("%s    Offset = 0x%08lx\n", p, data);
335 			}
336 		}
337 	}
338 }
339 
340 
341 #ifndef USE_HOSTCC
342 /**
343  * image_get_ramdisk - get and verify ramdisk image
344  * @rd_addr: ramdisk image start address
345  * @arch: expected ramdisk architecture
346  * @verify: checksum verification flag
347  *
348  * image_get_ramdisk() returns a pointer to the verified ramdisk image
349  * header. Routine receives image start address and expected architecture
350  * flag. Verification done covers data and header integrity and os/type/arch
351  * fields checking.
352  *
353  * If dataflash support is enabled routine checks for dataflash addresses
354  * and handles required dataflash reads.
355  *
356  * returns:
357  *     pointer to a ramdisk image header, if image was found and valid
358  *     otherwise, return NULL
359  */
360 static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
361 						int verify)
362 {
363 	const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
364 
365 	if (!image_check_magic(rd_hdr)) {
366 		puts("Bad Magic Number\n");
367 		bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
368 		return NULL;
369 	}
370 
371 	if (!image_check_hcrc(rd_hdr)) {
372 		puts("Bad Header Checksum\n");
373 		bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
374 		return NULL;
375 	}
376 
377 	bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
378 	image_print_contents(rd_hdr);
379 
380 	if (verify) {
381 		puts("   Verifying Checksum ... ");
382 		if (!image_check_dcrc(rd_hdr)) {
383 			puts("Bad Data CRC\n");
384 			bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
385 			return NULL;
386 		}
387 		puts("OK\n");
388 	}
389 
390 	bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
391 
392 	if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
393 	    !image_check_arch(rd_hdr, arch) ||
394 	    !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
395 		printf("No Linux %s Ramdisk Image\n",
396 				genimg_get_arch_name(arch));
397 		bootstage_error(BOOTSTAGE_ID_RAMDISK);
398 		return NULL;
399 	}
400 
401 	return rd_hdr;
402 }
403 #endif /* !USE_HOSTCC */
404 
405 /*****************************************************************************/
406 /* Shared dual-format routines */
407 /*****************************************************************************/
408 #ifndef USE_HOSTCC
409 ulong load_addr = CONFIG_SYS_LOAD_ADDR;	/* Default Load Address */
410 ulong save_addr;			/* Default Save Address */
411 ulong save_size;			/* Default Save Size (in bytes) */
412 
413 static int on_loadaddr(const char *name, const char *value, enum env_op op,
414 	int flags)
415 {
416 	switch (op) {
417 	case env_op_create:
418 	case env_op_overwrite:
419 		load_addr = simple_strtoul(value, NULL, 16);
420 		break;
421 	default:
422 		break;
423 	}
424 
425 	return 0;
426 }
427 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
428 
429 ulong getenv_bootm_low(void)
430 {
431 	char *s = getenv("bootm_low");
432 	if (s) {
433 		ulong tmp = simple_strtoul(s, NULL, 16);
434 		return tmp;
435 	}
436 
437 #if defined(CONFIG_SYS_SDRAM_BASE)
438 	return CONFIG_SYS_SDRAM_BASE;
439 #elif defined(CONFIG_ARM)
440 	return gd->bd->bi_dram[0].start;
441 #else
442 	return 0;
443 #endif
444 }
445 
446 phys_size_t getenv_bootm_size(void)
447 {
448 	phys_size_t tmp;
449 	char *s = getenv("bootm_size");
450 	if (s) {
451 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
452 		return tmp;
453 	}
454 	s = getenv("bootm_low");
455 	if (s)
456 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
457 	else
458 		tmp = 0;
459 
460 
461 #if defined(CONFIG_ARM)
462 	return gd->bd->bi_dram[0].size - tmp;
463 #else
464 	return gd->bd->bi_memsize - tmp;
465 #endif
466 }
467 
468 phys_size_t getenv_bootm_mapsize(void)
469 {
470 	phys_size_t tmp;
471 	char *s = getenv("bootm_mapsize");
472 	if (s) {
473 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
474 		return tmp;
475 	}
476 
477 #if defined(CONFIG_SYS_BOOTMAPSZ)
478 	return CONFIG_SYS_BOOTMAPSZ;
479 #else
480 	return getenv_bootm_size();
481 #endif
482 }
483 
484 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
485 {
486 	if (to == from)
487 		return;
488 
489 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
490 	while (len > 0) {
491 		size_t tail = (len > chunksz) ? chunksz : len;
492 		WATCHDOG_RESET();
493 		memmove(to, from, tail);
494 		to += tail;
495 		from += tail;
496 		len -= tail;
497 	}
498 #else	/* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
499 	memmove(to, from, len);
500 #endif	/* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
501 }
502 #endif /* !USE_HOSTCC */
503 
504 void genimg_print_size(uint32_t size)
505 {
506 #ifndef USE_HOSTCC
507 	printf("%d Bytes = ", size);
508 	print_size(size, "\n");
509 #else
510 	printf("%d Bytes = %.2f kB = %.2f MB\n",
511 			size, (double)size / 1.024e3,
512 			(double)size / 1.048576e6);
513 #endif
514 }
515 
516 #if IMAGE_ENABLE_TIMESTAMP
517 void genimg_print_time(time_t timestamp)
518 {
519 #ifndef USE_HOSTCC
520 	struct rtc_time tm;
521 
522 	to_tm(timestamp, &tm);
523 	printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
524 			tm.tm_year, tm.tm_mon, tm.tm_mday,
525 			tm.tm_hour, tm.tm_min, tm.tm_sec);
526 #else
527 	printf("%s", ctime(&timestamp));
528 #endif
529 }
530 #endif
531 
532 /**
533  * get_table_entry_name - translate entry id to long name
534  * @table: pointer to a translation table for entries of a specific type
535  * @msg: message to be returned when translation fails
536  * @id: entry id to be translated
537  *
538  * get_table_entry_name() will go over translation table trying to find
539  * entry that matches given id. If matching entry is found, its long
540  * name is returned to the caller.
541  *
542  * returns:
543  *     long entry name if translation succeeds
544  *     msg otherwise
545  */
546 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
547 {
548 	for (; table->id >= 0; ++table) {
549 		if (table->id == id)
550 #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
551 			return table->lname;
552 #else
553 			return table->lname + gd->reloc_off;
554 #endif
555 	}
556 	return (msg);
557 }
558 
559 const char *genimg_get_os_name(uint8_t os)
560 {
561 	return (get_table_entry_name(uimage_os, "Unknown OS", os));
562 }
563 
564 const char *genimg_get_arch_name(uint8_t arch)
565 {
566 	return (get_table_entry_name(uimage_arch, "Unknown Architecture",
567 					arch));
568 }
569 
570 const char *genimg_get_type_name(uint8_t type)
571 {
572 	return (get_table_entry_name(uimage_type, "Unknown Image", type));
573 }
574 
575 const char *genimg_get_comp_name(uint8_t comp)
576 {
577 	return (get_table_entry_name(uimage_comp, "Unknown Compression",
578 					comp));
579 }
580 
581 /**
582  * get_table_entry_id - translate short entry name to id
583  * @table: pointer to a translation table for entries of a specific type
584  * @table_name: to be used in case of error
585  * @name: entry short name to be translated
586  *
587  * get_table_entry_id() will go over translation table trying to find
588  * entry that matches given short name. If matching entry is found,
589  * its id returned to the caller.
590  *
591  * returns:
592  *     entry id if translation succeeds
593  *     -1 otherwise
594  */
595 int get_table_entry_id(const table_entry_t *table,
596 		const char *table_name, const char *name)
597 {
598 	const table_entry_t *t;
599 #ifdef USE_HOSTCC
600 	int first = 1;
601 
602 	for (t = table; t->id >= 0; ++t) {
603 		if (t->sname && strcasecmp(t->sname, name) == 0)
604 			return(t->id);
605 	}
606 
607 	fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
608 	for (t = table; t->id >= 0; ++t) {
609 		if (t->sname == NULL)
610 			continue;
611 		fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
612 		first = 0;
613 	}
614 	fprintf(stderr, "\n");
615 #else
616 	for (t = table; t->id >= 0; ++t) {
617 #ifdef CONFIG_NEEDS_MANUAL_RELOC
618 		if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
619 #else
620 		if (t->sname && strcmp(t->sname, name) == 0)
621 #endif
622 			return (t->id);
623 	}
624 	debug("Invalid %s Type: %s\n", table_name, name);
625 #endif /* USE_HOSTCC */
626 	return (-1);
627 }
628 
629 int genimg_get_os_id(const char *name)
630 {
631 	return (get_table_entry_id(uimage_os, "OS", name));
632 }
633 
634 int genimg_get_arch_id(const char *name)
635 {
636 	return (get_table_entry_id(uimage_arch, "CPU", name));
637 }
638 
639 int genimg_get_type_id(const char *name)
640 {
641 	return (get_table_entry_id(uimage_type, "Image", name));
642 }
643 
644 int genimg_get_comp_id(const char *name)
645 {
646 	return (get_table_entry_id(uimage_comp, "Compression", name));
647 }
648 
649 #ifndef USE_HOSTCC
650 /**
651  * genimg_get_format - get image format type
652  * @img_addr: image start address
653  *
654  * genimg_get_format() checks whether provided address points to a valid
655  * legacy or FIT image.
656  *
657  * New uImage format and FDT blob are based on a libfdt. FDT blob
658  * may be passed directly or embedded in a FIT image. In both situations
659  * genimg_get_format() must be able to dectect libfdt header.
660  *
661  * returns:
662  *     image format type or IMAGE_FORMAT_INVALID if no image is present
663  */
664 int genimg_get_format(void *img_addr)
665 {
666 	ulong format = IMAGE_FORMAT_INVALID;
667 	const image_header_t *hdr;
668 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
669 	char *fit_hdr;
670 #endif
671 
672 	hdr = (const image_header_t *)img_addr;
673 	if (image_check_magic(hdr))
674 		format = IMAGE_FORMAT_LEGACY;
675 #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
676 	else {
677 		fit_hdr = (char *)img_addr;
678 		if (fdt_check_header(fit_hdr) == 0)
679 			format = IMAGE_FORMAT_FIT;
680 	}
681 #endif
682 
683 	return format;
684 }
685 
686 /**
687  * genimg_get_image - get image from special storage (if necessary)
688  * @img_addr: image start address
689  *
690  * genimg_get_image() checks if provided image start adddress is located
691  * in a dataflash storage. If so, image is moved to a system RAM memory.
692  *
693  * returns:
694  *     image start address after possible relocation from special storage
695  */
696 ulong genimg_get_image(ulong img_addr)
697 {
698 	ulong ram_addr = img_addr;
699 
700 #ifdef CONFIG_HAS_DATAFLASH
701 	ulong h_size, d_size;
702 
703 	if (addr_dataflash(img_addr)) {
704 		/* ger RAM address */
705 		ram_addr = CONFIG_SYS_LOAD_ADDR;
706 
707 		/* get header size */
708 		h_size = image_get_header_size();
709 #if defined(CONFIG_FIT)
710 		if (sizeof(struct fdt_header) > h_size)
711 			h_size = sizeof(struct fdt_header);
712 #endif
713 
714 		/* read in header */
715 		debug("   Reading image header from dataflash address "
716 			"%08lx to RAM address %08lx\n", img_addr, ram_addr);
717 
718 		read_dataflash(img_addr, h_size, (char *)ram_addr);
719 
720 		/* get data size */
721 		switch (genimg_get_format((void *)ram_addr)) {
722 		case IMAGE_FORMAT_LEGACY:
723 			d_size = image_get_data_size(
724 					(const image_header_t *)ram_addr);
725 			debug("   Legacy format image found at 0x%08lx, "
726 					"size 0x%08lx\n",
727 					ram_addr, d_size);
728 			break;
729 #if defined(CONFIG_FIT)
730 		case IMAGE_FORMAT_FIT:
731 			d_size = fit_get_size((const void *)ram_addr) - h_size;
732 			debug("   FIT/FDT format image found at 0x%08lx, "
733 					"size 0x%08lx\n",
734 					ram_addr, d_size);
735 			break;
736 #endif
737 		default:
738 			printf("   No valid image found at 0x%08lx\n",
739 				img_addr);
740 			return ram_addr;
741 		}
742 
743 		/* read in image data */
744 		debug("   Reading image remaining data from dataflash address "
745 			"%08lx to RAM address %08lx\n", img_addr + h_size,
746 			ram_addr + h_size);
747 
748 		read_dataflash(img_addr + h_size, d_size,
749 				(char *)(ram_addr + h_size));
750 
751 	}
752 #endif /* CONFIG_HAS_DATAFLASH */
753 
754 	return ram_addr;
755 }
756 
757 /**
758  * fit_has_config - check if there is a valid FIT configuration
759  * @images: pointer to the bootm command headers structure
760  *
761  * fit_has_config() checks if there is a FIT configuration in use
762  * (if FTI support is present).
763  *
764  * returns:
765  *     0, no FIT support or no configuration found
766  *     1, configuration found
767  */
768 int genimg_has_config(bootm_headers_t *images)
769 {
770 #if defined(CONFIG_FIT)
771 	if (images->fit_uname_cfg)
772 		return 1;
773 #endif
774 	return 0;
775 }
776 
777 /**
778  * boot_get_ramdisk - main ramdisk handling routine
779  * @argc: command argument count
780  * @argv: command argument list
781  * @images: pointer to the bootm images structure
782  * @arch: expected ramdisk architecture
783  * @rd_start: pointer to a ulong variable, will hold ramdisk start address
784  * @rd_end: pointer to a ulong variable, will hold ramdisk end
785  *
786  * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
787  * Curently supported are the following ramdisk sources:
788  *      - multicomponent kernel/ramdisk image,
789  *      - commandline provided address of decicated ramdisk image.
790  *
791  * returns:
792  *     0, if ramdisk image was found and valid, or skiped
793  *     rd_start and rd_end are set to ramdisk start/end addresses if
794  *     ramdisk image is found and valid
795  *
796  *     1, if ramdisk image is found but corrupted, or invalid
797  *     rd_start and rd_end are set to 0 if no ramdisk exists
798  */
799 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
800 		uint8_t arch, ulong *rd_start, ulong *rd_end)
801 {
802 	ulong rd_addr, rd_load;
803 	ulong rd_data, rd_len;
804 	const image_header_t *rd_hdr;
805 #ifdef CONFIG_SUPPORT_RAW_INITRD
806 	char *end;
807 #endif
808 #if defined(CONFIG_FIT)
809 	void		*fit_hdr;
810 	const char	*fit_uname_config = NULL;
811 	const char	*fit_uname_ramdisk = NULL;
812 	ulong		default_addr;
813 	int		rd_noffset;
814 	int		cfg_noffset;
815 	const void	*data;
816 	size_t		size;
817 #endif
818 
819 	*rd_start = 0;
820 	*rd_end = 0;
821 
822 	/*
823 	 * Look for a '-' which indicates to ignore the
824 	 * ramdisk argument
825 	 */
826 	if ((argc >= 3) && (strcmp(argv[2], "-") ==  0)) {
827 		debug("## Skipping init Ramdisk\n");
828 		rd_len = rd_data = 0;
829 	} else if (argc >= 3 || genimg_has_config(images)) {
830 #if defined(CONFIG_FIT)
831 		if (argc >= 3) {
832 			/*
833 			 * If the init ramdisk comes from the FIT image and
834 			 * the FIT image address is omitted in the command
835 			 * line argument, try to use os FIT image address or
836 			 * default load address.
837 			 */
838 			if (images->fit_uname_os)
839 				default_addr = (ulong)images->fit_hdr_os;
840 			else
841 				default_addr = load_addr;
842 
843 			if (fit_parse_conf(argv[2], default_addr,
844 						&rd_addr, &fit_uname_config)) {
845 				debug("*  ramdisk: config '%s' from image at "
846 						"0x%08lx\n",
847 						fit_uname_config, rd_addr);
848 			} else if (fit_parse_subimage(argv[2], default_addr,
849 						&rd_addr, &fit_uname_ramdisk)) {
850 				debug("*  ramdisk: subimage '%s' from image at "
851 						"0x%08lx\n",
852 						fit_uname_ramdisk, rd_addr);
853 			} else
854 #endif
855 			{
856 				rd_addr = simple_strtoul(argv[2], NULL, 16);
857 				debug("*  ramdisk: cmdline image address = "
858 						"0x%08lx\n",
859 						rd_addr);
860 			}
861 #if defined(CONFIG_FIT)
862 		} else {
863 			/* use FIT configuration provided in first bootm
864 			 * command argument
865 			 */
866 			rd_addr = (ulong)images->fit_hdr_os;
867 			fit_uname_config = images->fit_uname_cfg;
868 			debug("*  ramdisk: using config '%s' from image "
869 					"at 0x%08lx\n",
870 					fit_uname_config, rd_addr);
871 
872 			/*
873 			 * Check whether configuration has ramdisk defined,
874 			 * if not, don't try to use it, quit silently.
875 			 */
876 			fit_hdr = (void *)rd_addr;
877 			cfg_noffset = fit_conf_get_node(fit_hdr,
878 							fit_uname_config);
879 			if (cfg_noffset < 0) {
880 				debug("*  ramdisk: no such config\n");
881 				return 1;
882 			}
883 
884 			rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
885 								cfg_noffset);
886 			if (rd_noffset < 0) {
887 				debug("*  ramdisk: no ramdisk in config\n");
888 				return 0;
889 			}
890 		}
891 #endif
892 
893 		/* copy from dataflash if needed */
894 		rd_addr = genimg_get_image(rd_addr);
895 
896 		/*
897 		 * Check if there is an initrd image at the
898 		 * address provided in the second bootm argument
899 		 * check image type, for FIT images get FIT node.
900 		 */
901 		switch (genimg_get_format((void *)rd_addr)) {
902 		case IMAGE_FORMAT_LEGACY:
903 			printf("## Loading init Ramdisk from Legacy "
904 					"Image at %08lx ...\n", rd_addr);
905 
906 			bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
907 			rd_hdr = image_get_ramdisk(rd_addr, arch,
908 							images->verify);
909 
910 			if (rd_hdr == NULL)
911 				return 1;
912 
913 			rd_data = image_get_data(rd_hdr);
914 			rd_len = image_get_data_size(rd_hdr);
915 			rd_load = image_get_load(rd_hdr);
916 			break;
917 #if defined(CONFIG_FIT)
918 		case IMAGE_FORMAT_FIT:
919 			fit_hdr = (void *)rd_addr;
920 			printf("## Loading init Ramdisk from FIT "
921 					"Image at %08lx ...\n", rd_addr);
922 
923 			bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
924 			if (!fit_check_format(fit_hdr)) {
925 				puts("Bad FIT ramdisk image format!\n");
926 				bootstage_error(
927 					BOOTSTAGE_ID_FIT_RD_FORMAT);
928 				return 1;
929 			}
930 			bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
931 
932 			if (!fit_uname_ramdisk) {
933 				/*
934 				 * no ramdisk image node unit name, try to get config
935 				 * node first. If config unit node name is NULL
936 				 * fit_conf_get_node() will try to find default config node
937 				 */
938 				bootstage_mark(
939 					BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
940 				cfg_noffset = fit_conf_get_node(fit_hdr,
941 							fit_uname_config);
942 				if (cfg_noffset < 0) {
943 					puts("Could not find configuration "
944 						"node\n");
945 					bootstage_error(
946 					BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
947 					return 1;
948 				}
949 				fit_uname_config = fdt_get_name(fit_hdr,
950 							cfg_noffset, NULL);
951 				printf("   Using '%s' configuration\n",
952 					fit_uname_config);
953 
954 				rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
955 							cfg_noffset);
956 				fit_uname_ramdisk = fit_get_name(fit_hdr,
957 							rd_noffset, NULL);
958 			} else {
959 				/* get ramdisk component image node offset */
960 				bootstage_mark(
961 					BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
962 				rd_noffset = fit_image_get_node(fit_hdr,
963 						fit_uname_ramdisk);
964 			}
965 			if (rd_noffset < 0) {
966 				puts("Could not find subimage node\n");
967 				bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
968 				return 1;
969 			}
970 
971 			printf("   Trying '%s' ramdisk subimage\n",
972 				fit_uname_ramdisk);
973 
974 			bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
975 			if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
976 						images->verify))
977 				return 1;
978 
979 			/* get ramdisk image data address and length */
980 			if (fit_image_get_data(fit_hdr, rd_noffset, &data,
981 						&size)) {
982 				puts("Could not find ramdisk subimage data!\n");
983 				bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
984 				return 1;
985 			}
986 			bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
987 
988 			rd_data = (ulong)data;
989 			rd_len = size;
990 
991 			if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
992 				puts("Can't get ramdisk subimage load "
993 					"address!\n");
994 				bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
995 				return 1;
996 			}
997 			bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
998 
999 			images->fit_hdr_rd = fit_hdr;
1000 			images->fit_uname_rd = fit_uname_ramdisk;
1001 			images->fit_noffset_rd = rd_noffset;
1002 			break;
1003 #endif
1004 		default:
1005 #ifdef CONFIG_SUPPORT_RAW_INITRD
1006 			if (argc >= 3 && (end = strchr(argv[2], ':'))) {
1007 				rd_len = simple_strtoul(++end, NULL, 16);
1008 				rd_data = rd_addr;
1009 			} else
1010 #endif
1011 			{
1012 				puts("Wrong Ramdisk Image Format\n");
1013 				rd_data = rd_len = rd_load = 0;
1014 				return 1;
1015 			}
1016 		}
1017 	} else if (images->legacy_hdr_valid &&
1018 			image_check_type(&images->legacy_hdr_os_copy,
1019 						IH_TYPE_MULTI)) {
1020 
1021 		/*
1022 		 * Now check if we have a legacy mult-component image,
1023 		 * get second entry data start address and len.
1024 		 */
1025 		bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1026 		printf("## Loading init Ramdisk from multi component "
1027 				"Legacy Image at %08lx ...\n",
1028 				(ulong)images->legacy_hdr_os);
1029 
1030 		image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1031 	} else {
1032 		/*
1033 		 * no initrd image
1034 		 */
1035 		bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1036 		rd_len = rd_data = 0;
1037 	}
1038 
1039 	if (!rd_data) {
1040 		debug("## No init Ramdisk\n");
1041 	} else {
1042 		*rd_start = rd_data;
1043 		*rd_end = rd_data + rd_len;
1044 	}
1045 	debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1046 			*rd_start, *rd_end);
1047 
1048 	return 0;
1049 }
1050 
1051 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1052 /**
1053  * boot_ramdisk_high - relocate init ramdisk
1054  * @lmb: pointer to lmb handle, will be used for memory mgmt
1055  * @rd_data: ramdisk data start address
1056  * @rd_len: ramdisk data length
1057  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1058  *      start address (after possible relocation)
1059  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1060  *      end address (after possible relocation)
1061  *
1062  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1063  * variable and if requested ramdisk data is moved to a specified location.
1064  *
1065  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1066  * start/end addresses if ramdisk image start and len were provided,
1067  * otherwise set initrd_start and initrd_end set to zeros.
1068  *
1069  * returns:
1070  *      0 - success
1071  *     -1 - failure
1072  */
1073 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1074 		  ulong *initrd_start, ulong *initrd_end)
1075 {
1076 	char	*s;
1077 	ulong	initrd_high;
1078 	int	initrd_copy_to_ram = 1;
1079 
1080 	if ((s = getenv("initrd_high")) != NULL) {
1081 		/* a value of "no" or a similar string will act like 0,
1082 		 * turning the "load high" feature off. This is intentional.
1083 		 */
1084 		initrd_high = simple_strtoul(s, NULL, 16);
1085 		if (initrd_high == ~0)
1086 			initrd_copy_to_ram = 0;
1087 	} else {
1088 		/* not set, no restrictions to load high */
1089 		initrd_high = ~0;
1090 	}
1091 
1092 
1093 #ifdef CONFIG_LOGBUFFER
1094 	/* Prevent initrd from overwriting logbuffer */
1095 	lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1096 #endif
1097 
1098 	debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1099 			initrd_high, initrd_copy_to_ram);
1100 
1101 	if (rd_data) {
1102 		if (!initrd_copy_to_ram) {	/* zero-copy ramdisk support */
1103 			debug("   in-place initrd\n");
1104 			*initrd_start = rd_data;
1105 			*initrd_end = rd_data + rd_len;
1106 			lmb_reserve(lmb, rd_data, rd_len);
1107 		} else {
1108 			if (initrd_high)
1109 				*initrd_start = (ulong)lmb_alloc_base(lmb,
1110 						rd_len, 0x1000, initrd_high);
1111 			else
1112 				*initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1113 								 0x1000);
1114 
1115 			if (*initrd_start == 0) {
1116 				puts("ramdisk - allocation error\n");
1117 				goto error;
1118 			}
1119 			bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1120 
1121 			*initrd_end = *initrd_start + rd_len;
1122 			printf("   Loading Ramdisk to %08lx, end %08lx ... ",
1123 					*initrd_start, *initrd_end);
1124 
1125 			memmove_wd((void *)*initrd_start,
1126 					(void *)rd_data, rd_len, CHUNKSZ);
1127 
1128 #ifdef CONFIG_MP
1129 			/*
1130 			 * Ensure the image is flushed to memory to handle
1131 			 * AMP boot scenarios in which we might not be
1132 			 * HW cache coherent
1133 			 */
1134 			flush_cache((unsigned long)*initrd_start, rd_len);
1135 #endif
1136 			puts("OK\n");
1137 		}
1138 	} else {
1139 		*initrd_start = 0;
1140 		*initrd_end = 0;
1141 	}
1142 	debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1143 			*initrd_start, *initrd_end);
1144 
1145 	return 0;
1146 
1147 error:
1148 	return -1;
1149 }
1150 #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1151 
1152 #ifdef CONFIG_OF_LIBFDT
1153 static void fdt_error(const char *msg)
1154 {
1155 	puts("ERROR: ");
1156 	puts(msg);
1157 	puts(" - must RESET the board to recover.\n");
1158 }
1159 
1160 static const image_header_t *image_get_fdt(ulong fdt_addr)
1161 {
1162 	const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1163 
1164 	image_print_contents(fdt_hdr);
1165 
1166 	puts("   Verifying Checksum ... ");
1167 	if (!image_check_hcrc(fdt_hdr)) {
1168 		fdt_error("fdt header checksum invalid");
1169 		return NULL;
1170 	}
1171 
1172 	if (!image_check_dcrc(fdt_hdr)) {
1173 		fdt_error("fdt checksum invalid");
1174 		return NULL;
1175 	}
1176 	puts("OK\n");
1177 
1178 	if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1179 		fdt_error("uImage is not a fdt");
1180 		return NULL;
1181 	}
1182 	if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1183 		fdt_error("uImage is compressed");
1184 		return NULL;
1185 	}
1186 	if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1187 		fdt_error("uImage data is not a fdt");
1188 		return NULL;
1189 	}
1190 	return fdt_hdr;
1191 }
1192 
1193 /**
1194  * fit_check_fdt - verify FIT format FDT subimage
1195  * @fit_hdr: pointer to the FIT  header
1196  * fdt_noffset: FDT subimage node offset within FIT image
1197  * @verify: data CRC verification flag
1198  *
1199  * fit_check_fdt() verifies integrity of the FDT subimage and from
1200  * specified FIT image.
1201  *
1202  * returns:
1203  *     1, on success
1204  *     0, on failure
1205  */
1206 #if defined(CONFIG_FIT)
1207 static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
1208 {
1209 	fit_image_print(fit, fdt_noffset, "   ");
1210 
1211 	if (verify) {
1212 		puts("   Verifying Hash Integrity ... ");
1213 		if (!fit_image_verify(fit, fdt_noffset)) {
1214 			fdt_error("Bad Data Hash");
1215 			return 0;
1216 		}
1217 		puts("OK\n");
1218 	}
1219 
1220 	if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1221 		fdt_error("Not a FDT image");
1222 		return 0;
1223 	}
1224 
1225 	if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1226 		fdt_error("FDT image is compressed");
1227 		return 0;
1228 	}
1229 
1230 	return 1;
1231 }
1232 #endif /* CONFIG_FIT */
1233 
1234 #ifndef CONFIG_SYS_FDT_PAD
1235 #define CONFIG_SYS_FDT_PAD 0x3000
1236 #endif
1237 
1238 #if defined(CONFIG_OF_LIBFDT)
1239 /**
1240  * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1241  * @lmb: pointer to lmb handle, will be used for memory mgmt
1242  * @fdt_blob: pointer to fdt blob base address
1243  *
1244  * Adds the memreserve regions in the dtb to the lmb block.  Adding the
1245  * memreserve regions prevents u-boot from using them to store the initrd
1246  * or the fdt blob.
1247  */
1248 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1249 {
1250 	uint64_t addr, size;
1251 	int i, total;
1252 
1253 	if (fdt_check_header(fdt_blob) != 0)
1254 		return;
1255 
1256 	total = fdt_num_mem_rsv(fdt_blob);
1257 	for (i = 0; i < total; i++) {
1258 		if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1259 			continue;
1260 		printf("   reserving fdt memory region: addr=%llx size=%llx\n",
1261 			(unsigned long long)addr, (unsigned long long)size);
1262 		lmb_reserve(lmb, addr, size);
1263 	}
1264 }
1265 
1266 /**
1267  * boot_relocate_fdt - relocate flat device tree
1268  * @lmb: pointer to lmb handle, will be used for memory mgmt
1269  * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1270  * @of_size: pointer to a ulong variable, will hold fdt length
1271  *
1272  * boot_relocate_fdt() allocates a region of memory within the bootmap and
1273  * relocates the of_flat_tree into that region, even if the fdt is already in
1274  * the bootmap.  It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1275  * bytes.
1276  *
1277  * of_flat_tree and of_size are set to final (after relocation) values
1278  *
1279  * returns:
1280  *      0 - success
1281  *      1 - failure
1282  */
1283 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1284 {
1285 	void	*fdt_blob = *of_flat_tree;
1286 	void	*of_start = NULL;
1287 	char	*fdt_high;
1288 	ulong	of_len = 0;
1289 	int	err;
1290 	int	disable_relocation = 0;
1291 
1292 	/* nothing to do */
1293 	if (*of_size == 0)
1294 		return 0;
1295 
1296 	if (fdt_check_header(fdt_blob) != 0) {
1297 		fdt_error("image is not a fdt");
1298 		goto error;
1299 	}
1300 
1301 	/* position on a 4K boundary before the alloc_current */
1302 	/* Pad the FDT by a specified amount */
1303 	of_len = *of_size + CONFIG_SYS_FDT_PAD;
1304 
1305 	/* If fdt_high is set use it to select the relocation address */
1306 	fdt_high = getenv("fdt_high");
1307 	if (fdt_high) {
1308 		void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1309 
1310 		if (((ulong) desired_addr) == ~0UL) {
1311 			/* All ones means use fdt in place */
1312 			of_start = fdt_blob;
1313 			lmb_reserve(lmb, (ulong)of_start, of_len);
1314 			disable_relocation = 1;
1315 		} else if (desired_addr) {
1316 			of_start =
1317 			    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1318 							   (ulong)desired_addr);
1319 			if (of_start == NULL) {
1320 				puts("Failed using fdt_high value for Device Tree");
1321 				goto error;
1322 			}
1323 		} else {
1324 			of_start =
1325 			    (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
1326 		}
1327 	} else {
1328 		of_start =
1329 		    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1330 						   getenv_bootm_mapsize()
1331 						   + getenv_bootm_low());
1332 	}
1333 
1334 	if (of_start == NULL) {
1335 		puts("device tree - allocation error\n");
1336 		goto error;
1337 	}
1338 
1339 	if (disable_relocation) {
1340 		/* We assume there is space after the existing fdt to use for padding */
1341 		fdt_set_totalsize(of_start, of_len);
1342 		printf("   Using Device Tree in place at %p, end %p\n",
1343 		       of_start, of_start + of_len - 1);
1344 	} else {
1345 		debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1346 			fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1347 
1348 		printf("   Loading Device Tree to %p, end %p ... ",
1349 			of_start, of_start + of_len - 1);
1350 
1351 		err = fdt_open_into(fdt_blob, of_start, of_len);
1352 		if (err != 0) {
1353 			fdt_error("fdt move failed");
1354 			goto error;
1355 		}
1356 		puts("OK\n");
1357 	}
1358 
1359 	*of_flat_tree = of_start;
1360 	*of_size = of_len;
1361 
1362 	set_working_fdt_addr(*of_flat_tree);
1363 	return 0;
1364 
1365 error:
1366 	return 1;
1367 }
1368 #endif /* CONFIG_OF_LIBFDT */
1369 
1370 /**
1371  * boot_get_fdt - main fdt handling routine
1372  * @argc: command argument count
1373  * @argv: command argument list
1374  * @images: pointer to the bootm images structure
1375  * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1376  * @of_size: pointer to a ulong variable, will hold fdt length
1377  *
1378  * boot_get_fdt() is responsible for finding a valid flat device tree image.
1379  * Curently supported are the following ramdisk sources:
1380  *      - multicomponent kernel/ramdisk image,
1381  *      - commandline provided address of decicated ramdisk image.
1382  *
1383  * returns:
1384  *     0, if fdt image was found and valid, or skipped
1385  *     of_flat_tree and of_size are set to fdt start address and length if
1386  *     fdt image is found and valid
1387  *
1388  *     1, if fdt image is found but corrupted
1389  *     of_flat_tree and of_size are set to 0 if no fdt exists
1390  */
1391 int boot_get_fdt(int flag, int argc, char * const argv[],
1392 		bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
1393 {
1394 	const image_header_t *fdt_hdr;
1395 	ulong		fdt_addr;
1396 	char		*fdt_blob = NULL;
1397 	ulong		image_start, image_data, image_end;
1398 	ulong		load_start, load_end;
1399 #if defined(CONFIG_FIT)
1400 	void		*fit_hdr;
1401 	const char	*fit_uname_config = NULL;
1402 	const char	*fit_uname_fdt = NULL;
1403 	ulong		default_addr;
1404 	int		cfg_noffset;
1405 	int		fdt_noffset;
1406 	const void	*data;
1407 	size_t		size;
1408 #endif
1409 
1410 	*of_flat_tree = NULL;
1411 	*of_size = 0;
1412 
1413 	if (argc > 3 || genimg_has_config(images)) {
1414 #if defined(CONFIG_FIT)
1415 		if (argc > 3) {
1416 			/*
1417 			 * If the FDT blob comes from the FIT image and the
1418 			 * FIT image address is omitted in the command line
1419 			 * argument, try to use ramdisk or os FIT image
1420 			 * address or default load address.
1421 			 */
1422 			if (images->fit_uname_rd)
1423 				default_addr = (ulong)images->fit_hdr_rd;
1424 			else if (images->fit_uname_os)
1425 				default_addr = (ulong)images->fit_hdr_os;
1426 			else
1427 				default_addr = load_addr;
1428 
1429 			if (fit_parse_conf(argv[3], default_addr,
1430 						&fdt_addr, &fit_uname_config)) {
1431 				debug("*  fdt: config '%s' from image at "
1432 						"0x%08lx\n",
1433 						fit_uname_config, fdt_addr);
1434 			} else if (fit_parse_subimage(argv[3], default_addr,
1435 						&fdt_addr, &fit_uname_fdt)) {
1436 				debug("*  fdt: subimage '%s' from image at "
1437 						"0x%08lx\n",
1438 						fit_uname_fdt, fdt_addr);
1439 			} else
1440 #endif
1441 			{
1442 				fdt_addr = simple_strtoul(argv[3], NULL, 16);
1443 				debug("*  fdt: cmdline image address = "
1444 						"0x%08lx\n",
1445 						fdt_addr);
1446 			}
1447 #if defined(CONFIG_FIT)
1448 		} else {
1449 			/* use FIT configuration provided in first bootm
1450 			 * command argument
1451 			 */
1452 			fdt_addr = (ulong)images->fit_hdr_os;
1453 			fit_uname_config = images->fit_uname_cfg;
1454 			debug("*  fdt: using config '%s' from image "
1455 					"at 0x%08lx\n",
1456 					fit_uname_config, fdt_addr);
1457 
1458 			/*
1459 			 * Check whether configuration has FDT blob defined,
1460 			 * if not quit silently.
1461 			 */
1462 			fit_hdr = (void *)fdt_addr;
1463 			cfg_noffset = fit_conf_get_node(fit_hdr,
1464 					fit_uname_config);
1465 			if (cfg_noffset < 0) {
1466 				debug("*  fdt: no such config\n");
1467 				return 0;
1468 			}
1469 
1470 			fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
1471 					cfg_noffset);
1472 			if (fdt_noffset < 0) {
1473 				debug("*  fdt: no fdt in config\n");
1474 				return 0;
1475 			}
1476 		}
1477 #endif
1478 
1479 		debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1480 				fdt_addr);
1481 
1482 		/* copy from dataflash if needed */
1483 		fdt_addr = genimg_get_image(fdt_addr);
1484 
1485 		/*
1486 		 * Check if there is an FDT image at the
1487 		 * address provided in the second bootm argument
1488 		 * check image type, for FIT images get a FIT node.
1489 		 */
1490 		switch (genimg_get_format((void *)fdt_addr)) {
1491 		case IMAGE_FORMAT_LEGACY:
1492 			/* verify fdt_addr points to a valid image header */
1493 			printf("## Flattened Device Tree from Legacy Image "
1494 					"at %08lx\n",
1495 					fdt_addr);
1496 			fdt_hdr = image_get_fdt(fdt_addr);
1497 			if (!fdt_hdr)
1498 				goto error;
1499 
1500 			/*
1501 			 * move image data to the load address,
1502 			 * make sure we don't overwrite initial image
1503 			 */
1504 			image_start = (ulong)fdt_hdr;
1505 			image_data = (ulong)image_get_data(fdt_hdr);
1506 			image_end = image_get_image_end(fdt_hdr);
1507 
1508 			load_start = image_get_load(fdt_hdr);
1509 			load_end = load_start + image_get_data_size(fdt_hdr);
1510 
1511 			if (load_start == image_start ||
1512 			    load_start == image_data) {
1513 				fdt_blob = (char *)image_data;
1514 				break;
1515 			}
1516 
1517 			if ((load_start < image_end) && (load_end > image_start)) {
1518 				fdt_error("fdt overwritten");
1519 				goto error;
1520 			}
1521 
1522 			debug("   Loading FDT from 0x%08lx to 0x%08lx\n",
1523 					image_data, load_start);
1524 
1525 			memmove((void *)load_start,
1526 					(void *)image_data,
1527 					image_get_data_size(fdt_hdr));
1528 
1529 			fdt_blob = (char *)load_start;
1530 			break;
1531 		case IMAGE_FORMAT_FIT:
1532 			/*
1533 			 * This case will catch both: new uImage format
1534 			 * (libfdt based) and raw FDT blob (also libfdt
1535 			 * based).
1536 			 */
1537 #if defined(CONFIG_FIT)
1538 			/* check FDT blob vs FIT blob */
1539 			if (fit_check_format((const void *)fdt_addr)) {
1540 				/*
1541 				 * FIT image
1542 				 */
1543 				fit_hdr = (void *)fdt_addr;
1544 				printf("## Flattened Device Tree from FIT "
1545 						"Image at %08lx\n",
1546 						fdt_addr);
1547 
1548 				if (!fit_uname_fdt) {
1549 					/*
1550 					 * no FDT blob image node unit name,
1551 					 * try to get config node first. If
1552 					 * config unit node name is NULL
1553 					 * fit_conf_get_node() will try to
1554 					 * find default config node
1555 					 */
1556 					cfg_noffset = fit_conf_get_node(fit_hdr,
1557 							fit_uname_config);
1558 
1559 					if (cfg_noffset < 0) {
1560 						fdt_error("Could not find "
1561 							    "configuration "
1562 							    "node\n");
1563 						goto error;
1564 					}
1565 
1566 					fit_uname_config = fdt_get_name(fit_hdr,
1567 							cfg_noffset, NULL);
1568 					printf("   Using '%s' configuration\n",
1569 							fit_uname_config);
1570 
1571 					fdt_noffset = fit_conf_get_fdt_node(
1572 							fit_hdr,
1573 							cfg_noffset);
1574 					fit_uname_fdt = fit_get_name(fit_hdr,
1575 							fdt_noffset, NULL);
1576 				} else {
1577 					/* get FDT component image node offset */
1578 					fdt_noffset = fit_image_get_node(
1579 								fit_hdr,
1580 								fit_uname_fdt);
1581 				}
1582 				if (fdt_noffset < 0) {
1583 					fdt_error("Could not find subimage "
1584 							"node\n");
1585 					goto error;
1586 				}
1587 
1588 				printf("   Trying '%s' FDT blob subimage\n",
1589 						fit_uname_fdt);
1590 
1591 				if (!fit_check_fdt(fit_hdr, fdt_noffset,
1592 							images->verify))
1593 					goto error;
1594 
1595 				/* get ramdisk image data address and length */
1596 				if (fit_image_get_data(fit_hdr, fdt_noffset,
1597 							&data, &size)) {
1598 					fdt_error("Could not find FDT "
1599 							"subimage data");
1600 					goto error;
1601 				}
1602 
1603 				/* verift that image data is a proper FDT blob */
1604 				if (fdt_check_header((char *)data) != 0) {
1605 					fdt_error("Subimage data is not a FTD");
1606 					goto error;
1607 				}
1608 
1609 				/*
1610 				 * move image data to the load address,
1611 				 * make sure we don't overwrite initial image
1612 				 */
1613 				image_start = (ulong)fit_hdr;
1614 				image_end = fit_get_end(fit_hdr);
1615 
1616 				if (fit_image_get_load(fit_hdr, fdt_noffset,
1617 							&load_start) == 0) {
1618 					load_end = load_start + size;
1619 
1620 					if ((load_start < image_end) &&
1621 							(load_end > image_start)) {
1622 						fdt_error("FDT overwritten");
1623 						goto error;
1624 					}
1625 
1626 					printf("   Loading FDT from 0x%08lx "
1627 							"to 0x%08lx\n",
1628 							(ulong)data,
1629 							load_start);
1630 
1631 					memmove((void *)load_start,
1632 							(void *)data, size);
1633 
1634 					fdt_blob = (char *)load_start;
1635 				} else {
1636 					fdt_blob = (char *)data;
1637 				}
1638 
1639 				images->fit_hdr_fdt = fit_hdr;
1640 				images->fit_uname_fdt = fit_uname_fdt;
1641 				images->fit_noffset_fdt = fdt_noffset;
1642 				break;
1643 			} else
1644 #endif
1645 			{
1646 				/*
1647 				 * FDT blob
1648 				 */
1649 				fdt_blob = (char *)fdt_addr;
1650 				debug("*  fdt: raw FDT blob\n");
1651 				printf("## Flattened Device Tree blob at "
1652 					"%08lx\n", (long)fdt_blob);
1653 			}
1654 			break;
1655 		default:
1656 			puts("ERROR: Did not find a cmdline Flattened Device "
1657 				"Tree\n");
1658 			goto error;
1659 		}
1660 
1661 		printf("   Booting using the fdt blob at 0x%p\n", fdt_blob);
1662 
1663 	} else if (images->legacy_hdr_valid &&
1664 			image_check_type(&images->legacy_hdr_os_copy,
1665 						IH_TYPE_MULTI)) {
1666 
1667 		ulong fdt_data, fdt_len;
1668 
1669 		/*
1670 		 * Now check if we have a legacy multi-component image,
1671 		 * get second entry data start address and len.
1672 		 */
1673 		printf("## Flattened Device Tree from multi "
1674 			"component Image at %08lX\n",
1675 			(ulong)images->legacy_hdr_os);
1676 
1677 		image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1678 					&fdt_len);
1679 		if (fdt_len) {
1680 
1681 			fdt_blob = (char *)fdt_data;
1682 			printf("   Booting using the fdt at 0x%p\n", fdt_blob);
1683 
1684 			if (fdt_check_header(fdt_blob) != 0) {
1685 				fdt_error("image is not a fdt");
1686 				goto error;
1687 			}
1688 
1689 			if (fdt_totalsize(fdt_blob) != fdt_len) {
1690 				fdt_error("fdt size != image size");
1691 				goto error;
1692 			}
1693 		} else {
1694 			debug("## No Flattened Device Tree\n");
1695 			return 0;
1696 		}
1697 	} else {
1698 		debug("## No Flattened Device Tree\n");
1699 		return 0;
1700 	}
1701 
1702 	*of_flat_tree = fdt_blob;
1703 	*of_size = fdt_totalsize(fdt_blob);
1704 	debug("   of_flat_tree at 0x%08lx size 0x%08lx\n",
1705 			(ulong)*of_flat_tree, *of_size);
1706 
1707 	return 0;
1708 
1709 error:
1710 	*of_flat_tree = NULL;
1711 	*of_size = 0;
1712 	return 1;
1713 }
1714 #endif /* CONFIG_OF_LIBFDT */
1715 
1716 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1717 /**
1718  * boot_get_cmdline - allocate and initialize kernel cmdline
1719  * @lmb: pointer to lmb handle, will be used for memory mgmt
1720  * @cmd_start: pointer to a ulong variable, will hold cmdline start
1721  * @cmd_end: pointer to a ulong variable, will hold cmdline end
1722  *
1723  * boot_get_cmdline() allocates space for kernel command line below
1724  * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1725  * variable is present its contents is copied to allocated kernel
1726  * command line.
1727  *
1728  * returns:
1729  *      0 - success
1730  *     -1 - failure
1731  */
1732 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1733 {
1734 	char *cmdline;
1735 	char *s;
1736 
1737 	cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1738 				getenv_bootm_mapsize() + getenv_bootm_low());
1739 
1740 	if (cmdline == NULL)
1741 		return -1;
1742 
1743 	if ((s = getenv("bootargs")) == NULL)
1744 		s = "";
1745 
1746 	strcpy(cmdline, s);
1747 
1748 	*cmd_start = (ulong) & cmdline[0];
1749 	*cmd_end = *cmd_start + strlen(cmdline);
1750 
1751 	debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1752 
1753 	return 0;
1754 }
1755 #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1756 
1757 #ifdef CONFIG_SYS_BOOT_GET_KBD
1758 /**
1759  * boot_get_kbd - allocate and initialize kernel copy of board info
1760  * @lmb: pointer to lmb handle, will be used for memory mgmt
1761  * @kbd: double pointer to board info data
1762  *
1763  * boot_get_kbd() allocates space for kernel copy of board info data below
1764  * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1765  * with the current u-boot board info data.
1766  *
1767  * returns:
1768  *      0 - success
1769  *     -1 - failure
1770  */
1771 int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1772 {
1773 	*kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1774 				getenv_bootm_mapsize() + getenv_bootm_low());
1775 	if (*kbd == NULL)
1776 		return -1;
1777 
1778 	**kbd = *(gd->bd);
1779 
1780 	debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1781 
1782 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1783 	do_bdinfo(NULL, 0, 0, NULL);
1784 #endif
1785 
1786 	return 0;
1787 }
1788 #endif /* CONFIG_SYS_BOOT_GET_KBD */
1789 #endif /* !USE_HOSTCC */
1790