xref: /OK3568_Linux_fs/u-boot/common/image.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2008 Semihalf
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * (C) Copyright 2000-2006
5*4882a593Smuzhiyun  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #ifndef USE_HOSTCC
11*4882a593Smuzhiyun #include <common.h>
12*4882a593Smuzhiyun #include <watchdog.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #ifdef CONFIG_SHOW_BOOT_PROGRESS
15*4882a593Smuzhiyun #include <status_led.h>
16*4882a593Smuzhiyun #endif
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <rtc.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <environment.h>
21*4882a593Smuzhiyun #include <image.h>
22*4882a593Smuzhiyun #include <mapmem.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
25*4882a593Smuzhiyun #include <linux/libfdt.h>
26*4882a593Smuzhiyun #include <fdt_support.h>
27*4882a593Smuzhiyun #include <fpga.h>
28*4882a593Smuzhiyun #include <xilinx.h>
29*4882a593Smuzhiyun #endif
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <u-boot/md5.h>
32*4882a593Smuzhiyun #include <u-boot/sha1.h>
33*4882a593Smuzhiyun #include <linux/errno.h>
34*4882a593Smuzhiyun #include <asm/io.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #ifdef CONFIG_CMD_BDI
37*4882a593Smuzhiyun extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
38*4882a593Smuzhiyun #endif
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
43*4882a593Smuzhiyun static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
44*4882a593Smuzhiyun 						int verify);
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun #else
47*4882a593Smuzhiyun #include "mkimage.h"
48*4882a593Smuzhiyun #include <u-boot/md5.h>
49*4882a593Smuzhiyun #include <time.h>
50*4882a593Smuzhiyun #include <image.h>
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #ifndef __maybe_unused
53*4882a593Smuzhiyun # define __maybe_unused		/* unimplemented */
54*4882a593Smuzhiyun #endif
55*4882a593Smuzhiyun #endif /* !USE_HOSTCC*/
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #include <u-boot/crc.h>
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun #ifndef CONFIG_SYS_BARGSIZE
60*4882a593Smuzhiyun #define CONFIG_SYS_BARGSIZE 512
61*4882a593Smuzhiyun #endif
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun static const table_entry_t uimage_arch[] = {
64*4882a593Smuzhiyun 	{	IH_ARCH_INVALID,	"invalid",	"Invalid ARCH",	},
65*4882a593Smuzhiyun 	{	IH_ARCH_ALPHA,		"alpha",	"Alpha",	},
66*4882a593Smuzhiyun 	{	IH_ARCH_ARM,		"arm",		"ARM",		},
67*4882a593Smuzhiyun 	{	IH_ARCH_I386,		"x86",		"Intel x86",	},
68*4882a593Smuzhiyun 	{	IH_ARCH_IA64,		"ia64",		"IA64",		},
69*4882a593Smuzhiyun 	{	IH_ARCH_M68K,		"m68k",		"M68K",		},
70*4882a593Smuzhiyun 	{	IH_ARCH_MICROBLAZE,	"microblaze",	"MicroBlaze",	},
71*4882a593Smuzhiyun 	{	IH_ARCH_MIPS,		"mips",		"MIPS",		},
72*4882a593Smuzhiyun 	{	IH_ARCH_MIPS64,		"mips64",	"MIPS 64 Bit",	},
73*4882a593Smuzhiyun 	{	IH_ARCH_NIOS2,		"nios2",	"NIOS II",	},
74*4882a593Smuzhiyun 	{	IH_ARCH_PPC,		"powerpc",	"PowerPC",	},
75*4882a593Smuzhiyun 	{	IH_ARCH_PPC,		"ppc",		"PowerPC",	},
76*4882a593Smuzhiyun 	{	IH_ARCH_S390,		"s390",		"IBM S390",	},
77*4882a593Smuzhiyun 	{	IH_ARCH_SH,		"sh",		"SuperH",	},
78*4882a593Smuzhiyun 	{	IH_ARCH_SPARC,		"sparc",	"SPARC",	},
79*4882a593Smuzhiyun 	{	IH_ARCH_SPARC64,	"sparc64",	"SPARC 64 Bit",	},
80*4882a593Smuzhiyun 	{	IH_ARCH_BLACKFIN,	"blackfin",	"Blackfin",	},
81*4882a593Smuzhiyun 	{	IH_ARCH_AVR32,		"avr32",	"AVR32",	},
82*4882a593Smuzhiyun 	{	IH_ARCH_NDS32,		"nds32",	"NDS32",	},
83*4882a593Smuzhiyun 	{	IH_ARCH_OPENRISC,	"or1k",		"OpenRISC 1000",},
84*4882a593Smuzhiyun 	{	IH_ARCH_SANDBOX,	"sandbox",	"Sandbox",	},
85*4882a593Smuzhiyun 	{	IH_ARCH_ARM64,		"arm64",	"AArch64",	},
86*4882a593Smuzhiyun 	{	IH_ARCH_ARC,		"arc",		"ARC",		},
87*4882a593Smuzhiyun 	{	IH_ARCH_X86_64,		"x86_64",	"AMD x86_64",	},
88*4882a593Smuzhiyun 	{	IH_ARCH_XTENSA,		"xtensa",	"Xtensa",	},
89*4882a593Smuzhiyun 	{	IH_ARCH_RISCV,		"riscv",	"RISC-V",	},
90*4882a593Smuzhiyun 	{	-1,			"",		"",		},
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun static const table_entry_t uimage_os[] = {
94*4882a593Smuzhiyun 	{	IH_OS_INVALID,	"invalid",	"Invalid OS",		},
95*4882a593Smuzhiyun 	{       IH_OS_OP_TEE, "op-tee", "OP-TEE"  },
96*4882a593Smuzhiyun 	{       IH_OS_ARM_TRUSTED_FIRMWARE, "arm-trusted-firmware", "ARM Trusted Firmware"  },
97*4882a593Smuzhiyun 	{	IH_OS_LINUX,	"linux",	"Linux",		},
98*4882a593Smuzhiyun #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
99*4882a593Smuzhiyun 	{	IH_OS_LYNXOS,	"lynxos",	"LynxOS",		},
100*4882a593Smuzhiyun #endif
101*4882a593Smuzhiyun 	{	IH_OS_NETBSD,	"netbsd",	"NetBSD",		},
102*4882a593Smuzhiyun 	{	IH_OS_OSE,	"ose",		"Enea OSE",		},
103*4882a593Smuzhiyun 	{	IH_OS_PLAN9,	"plan9",	"Plan 9",		},
104*4882a593Smuzhiyun 	{	IH_OS_RTEMS,	"rtems",	"RTEMS",		},
105*4882a593Smuzhiyun 	{	IH_OS_U_BOOT,	"u-boot",	"U-Boot",		},
106*4882a593Smuzhiyun 	{	IH_OS_VXWORKS,	"vxworks",	"VxWorks",		},
107*4882a593Smuzhiyun #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
108*4882a593Smuzhiyun 	{	IH_OS_QNX,	"qnx",		"QNX",			},
109*4882a593Smuzhiyun #endif
110*4882a593Smuzhiyun #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
111*4882a593Smuzhiyun 	{	IH_OS_INTEGRITY,"integrity",	"INTEGRITY",		},
112*4882a593Smuzhiyun #endif
113*4882a593Smuzhiyun #ifdef USE_HOSTCC
114*4882a593Smuzhiyun 	{	IH_OS_4_4BSD,	"4_4bsd",	"4_4BSD",		},
115*4882a593Smuzhiyun 	{	IH_OS_DELL,	"dell",		"Dell",			},
116*4882a593Smuzhiyun 	{	IH_OS_ESIX,	"esix",		"Esix",			},
117*4882a593Smuzhiyun 	{	IH_OS_FREEBSD,	"freebsd",	"FreeBSD",		},
118*4882a593Smuzhiyun 	{	IH_OS_IRIX,	"irix",		"Irix",			},
119*4882a593Smuzhiyun 	{	IH_OS_NCR,	"ncr",		"NCR",			},
120*4882a593Smuzhiyun 	{	IH_OS_OPENBSD,	"openbsd",	"OpenBSD",		},
121*4882a593Smuzhiyun 	{	IH_OS_PSOS,	"psos",		"pSOS",			},
122*4882a593Smuzhiyun 	{	IH_OS_SCO,	"sco",		"SCO",			},
123*4882a593Smuzhiyun 	{	IH_OS_SOLARIS,	"solaris",	"Solaris",		},
124*4882a593Smuzhiyun 	{	IH_OS_SVR4,	"svr4",		"SVR4",			},
125*4882a593Smuzhiyun #endif
126*4882a593Smuzhiyun #if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
127*4882a593Smuzhiyun 	{	IH_OS_OPENRTOS,	"openrtos",	"OpenRTOS",		},
128*4882a593Smuzhiyun #endif
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	{	-1,		"",		"",			},
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun static const table_entry_t uimage_type[] = {
134*4882a593Smuzhiyun 	{	IH_TYPE_AISIMAGE,   "aisimage",   "Davinci AIS image",},
135*4882a593Smuzhiyun 	{	IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image",	},
136*4882a593Smuzhiyun 	{	IH_TYPE_FIRMWARE,   "firmware",	  "Firmware",		},
137*4882a593Smuzhiyun 	{	IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",	},
138*4882a593Smuzhiyun 	{	IH_TYPE_GPIMAGE,    "gpimage",    "TI Keystone SPL Image",},
139*4882a593Smuzhiyun 	{	IH_TYPE_KERNEL,	    "kernel",	  "Kernel Image",	},
140*4882a593Smuzhiyun 	{	IH_TYPE_KERNEL_NOLOAD, "kernel_noload",  "Kernel Image (no loading done)", },
141*4882a593Smuzhiyun 	{	IH_TYPE_KWBIMAGE,   "kwbimage",   "Kirkwood Boot Image",},
142*4882a593Smuzhiyun 	{	IH_TYPE_IMXIMAGE,   "imximage",   "Freescale i.MX Boot Image",},
143*4882a593Smuzhiyun 	{	IH_TYPE_INVALID,    "invalid",	  "Invalid Image",	},
144*4882a593Smuzhiyun 	{	IH_TYPE_MULTI,	    "multi",	  "Multi-File Image",	},
145*4882a593Smuzhiyun 	{	IH_TYPE_OMAPIMAGE,  "omapimage",  "TI OMAP SPL With GP CH",},
146*4882a593Smuzhiyun 	{	IH_TYPE_PBLIMAGE,   "pblimage",   "Freescale PBL Boot Image",},
147*4882a593Smuzhiyun 	{	IH_TYPE_RAMDISK,    "ramdisk",	  "RAMDisk Image",	},
148*4882a593Smuzhiyun 	{	IH_TYPE_SCRIPT,     "script",	  "Script",		},
149*4882a593Smuzhiyun 	{	IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SOCFPGA preloader",},
150*4882a593Smuzhiyun 	{	IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
151*4882a593Smuzhiyun 	{	IH_TYPE_UBLIMAGE,   "ublimage",   "Davinci UBL image",},
152*4882a593Smuzhiyun 	{	IH_TYPE_MXSIMAGE,   "mxsimage",   "Freescale MXS Boot Image",},
153*4882a593Smuzhiyun 	{	IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
154*4882a593Smuzhiyun 	{	IH_TYPE_X86_SETUP,  "x86_setup",  "x86 setup.bin",    },
155*4882a593Smuzhiyun 	{	IH_TYPE_LPC32XXIMAGE, "lpc32xximage",  "LPC32XX Boot Image", },
156*4882a593Smuzhiyun 	{	IH_TYPE_RKIMAGE,    "rkimage",    "Rockchip Boot Image" },
157*4882a593Smuzhiyun 	{	IH_TYPE_RKSD,       "rksd",       "Rockchip SD Boot Image" },
158*4882a593Smuzhiyun 	{	IH_TYPE_RKSPI,      "rkspi",      "Rockchip SPI Boot Image" },
159*4882a593Smuzhiyun 	{	IH_TYPE_VYBRIDIMAGE, "vybridimage",  "Vybrid Boot Image", },
160*4882a593Smuzhiyun 	{	IH_TYPE_ZYNQIMAGE,  "zynqimage",  "Xilinx Zynq Boot Image" },
161*4882a593Smuzhiyun 	{	IH_TYPE_ZYNQMPIMAGE, "zynqmpimage", "Xilinx ZynqMP Boot Image" },
162*4882a593Smuzhiyun 	{	IH_TYPE_FPGA,       "fpga",       "FPGA Image" },
163*4882a593Smuzhiyun 	{       IH_TYPE_TEE,        "tee",        "Trusted Execution Environment Image",},
164*4882a593Smuzhiyun 	{	IH_TYPE_FIRMWARE_IVT, "firmware_ivt", "Firmware with HABv4 IVT" },
165*4882a593Smuzhiyun 	{       IH_TYPE_PMMC,        "pmmc",        "TI Power Management Micro-Controller Firmware",},
166*4882a593Smuzhiyun 	{	IH_TYPE_RKNAND,     "rknand",     "Rockchip NAND Boot Image" },
167*4882a593Smuzhiyun 	{	-1,		    "",		  "",			},
168*4882a593Smuzhiyun };
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun static const table_entry_t uimage_comp[] = {
171*4882a593Smuzhiyun 	{	IH_COMP_NONE,	"none",		"uncompressed",		},
172*4882a593Smuzhiyun 	{	IH_COMP_BZIP2,	"bzip2",	"bzip2 compressed",	},
173*4882a593Smuzhiyun 	{	IH_COMP_GZIP,	"gzip",		"gzip compressed",	},
174*4882a593Smuzhiyun 	{	IH_COMP_LZMA,	"lzma",		"lzma compressed",	},
175*4882a593Smuzhiyun 	{	IH_COMP_LZO,	"lzo",		"lzo compressed",	},
176*4882a593Smuzhiyun 	{	IH_COMP_LZ4,	"lz4",		"lz4 compressed",	},
177*4882a593Smuzhiyun 	{	-1,		"",		"",			},
178*4882a593Smuzhiyun };
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun struct table_info {
181*4882a593Smuzhiyun 	const char *desc;
182*4882a593Smuzhiyun 	int count;
183*4882a593Smuzhiyun 	const table_entry_t *table;
184*4882a593Smuzhiyun };
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun static const struct table_info table_info[IH_COUNT] = {
187*4882a593Smuzhiyun 	{ "architecture", IH_ARCH_COUNT, uimage_arch },
188*4882a593Smuzhiyun 	{ "compression", IH_COMP_COUNT, uimage_comp },
189*4882a593Smuzhiyun 	{ "operating system", IH_OS_COUNT, uimage_os },
190*4882a593Smuzhiyun 	{ "image type", IH_TYPE_COUNT, uimage_type },
191*4882a593Smuzhiyun };
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun /*****************************************************************************/
194*4882a593Smuzhiyun /* Legacy format routines */
195*4882a593Smuzhiyun /*****************************************************************************/
196*4882a593Smuzhiyun #ifndef USE_HOSTCC
197*4882a593Smuzhiyun #ifndef CONFIG_SPL_BUILD
image_get_load(const image_header_t * hdr)198*4882a593Smuzhiyun uint32_t image_get_load(const image_header_t *hdr)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	uint32_t load = uimage_to_cpu(hdr->ih_load);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	return (load == IMAGE_PARAM_INVAL) ?
203*4882a593Smuzhiyun 		env_get_ulong("kernel_addr_r", 16, 0) : load;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
image_get_ep(const image_header_t * hdr)206*4882a593Smuzhiyun uint32_t image_get_ep(const image_header_t *hdr)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	uint32_t ep = uimage_to_cpu(hdr->ih_ep);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	return (ep == IMAGE_PARAM_INVAL) ?
211*4882a593Smuzhiyun 		env_get_ulong("kernel_addr_r", 16, 0) : ep;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun #endif
214*4882a593Smuzhiyun #endif
215*4882a593Smuzhiyun 
image_check_hcrc(const image_header_t * hdr)216*4882a593Smuzhiyun int image_check_hcrc(const image_header_t *hdr)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	ulong hcrc;
219*4882a593Smuzhiyun 	ulong len = image_get_header_size();
220*4882a593Smuzhiyun 	image_header_t header;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	/* Copy header so we can blank CRC field for re-calculation */
223*4882a593Smuzhiyun 	memmove(&header, (char *)hdr, image_get_header_size());
224*4882a593Smuzhiyun 	image_set_hcrc(&header, 0);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	hcrc = crc32(0, (unsigned char *)&header, len);
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	return (hcrc == image_get_hcrc(hdr));
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun 
image_check_dcrc(const image_header_t * hdr)231*4882a593Smuzhiyun int image_check_dcrc(const image_header_t *hdr)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun 	ulong data = image_get_data(hdr);
234*4882a593Smuzhiyun 	ulong len = image_get_data_size(hdr);
235*4882a593Smuzhiyun 	ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	return (dcrc == image_get_dcrc(hdr));
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun /**
241*4882a593Smuzhiyun  * image_multi_count - get component (sub-image) count
242*4882a593Smuzhiyun  * @hdr: pointer to the header of the multi component image
243*4882a593Smuzhiyun  *
244*4882a593Smuzhiyun  * image_multi_count() returns number of components in a multi
245*4882a593Smuzhiyun  * component image.
246*4882a593Smuzhiyun  *
247*4882a593Smuzhiyun  * Note: no checking of the image type is done, caller must pass
248*4882a593Smuzhiyun  * a valid multi component image.
249*4882a593Smuzhiyun  *
250*4882a593Smuzhiyun  * returns:
251*4882a593Smuzhiyun  *     number of components
252*4882a593Smuzhiyun  */
image_multi_count(const image_header_t * hdr)253*4882a593Smuzhiyun ulong image_multi_count(const image_header_t *hdr)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	ulong i, count = 0;
256*4882a593Smuzhiyun 	uint32_t *size;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	/* get start of the image payload, which in case of multi
259*4882a593Smuzhiyun 	 * component images that points to a table of component sizes */
260*4882a593Smuzhiyun 	size = (uint32_t *)image_get_data(hdr);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* count non empty slots */
263*4882a593Smuzhiyun 	for (i = 0; size[i] != IMAGE_PARAM_INVAL; ++i)
264*4882a593Smuzhiyun 		count++;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	return count;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun /**
270*4882a593Smuzhiyun  * image_multi_getimg - get component data address and size
271*4882a593Smuzhiyun  * @hdr: pointer to the header of the multi component image
272*4882a593Smuzhiyun  * @idx: index of the requested component
273*4882a593Smuzhiyun  * @data: pointer to a ulong variable, will hold component data address
274*4882a593Smuzhiyun  * @len: pointer to a ulong variable, will hold component size
275*4882a593Smuzhiyun  *
276*4882a593Smuzhiyun  * image_multi_getimg() returns size and data address for the requested
277*4882a593Smuzhiyun  * component in a multi component image.
278*4882a593Smuzhiyun  *
279*4882a593Smuzhiyun  * Note: no checking of the image type is done, caller must pass
280*4882a593Smuzhiyun  * a valid multi component image.
281*4882a593Smuzhiyun  *
282*4882a593Smuzhiyun  * returns:
283*4882a593Smuzhiyun  *     data address and size of the component, if idx is valid
284*4882a593Smuzhiyun  *     0 in data and len, if idx is out of range
285*4882a593Smuzhiyun  */
image_multi_getimg(const image_header_t * hdr,ulong idx,ulong * data,ulong * len)286*4882a593Smuzhiyun void image_multi_getimg(const image_header_t *hdr, ulong idx,
287*4882a593Smuzhiyun 			ulong *data, ulong *len)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun 	int i;
290*4882a593Smuzhiyun 	uint32_t *size;
291*4882a593Smuzhiyun 	ulong offset, count, img_data;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	/* get number of component */
294*4882a593Smuzhiyun 	count = image_multi_count(hdr);
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	/* get start of the image payload, which in case of multi
297*4882a593Smuzhiyun 	 * component images that points to a table of component sizes */
298*4882a593Smuzhiyun 	size = (uint32_t *)image_get_data(hdr);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/* get address of the proper component data start, which means
301*4882a593Smuzhiyun 	 * skipping sizes table (add 1 for last, null entry) */
302*4882a593Smuzhiyun 	img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	if (idx < count) {
305*4882a593Smuzhiyun 		*len = uimage_to_cpu(size[idx]);
306*4882a593Smuzhiyun 		offset = 0;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 		/* go over all indices preceding requested component idx */
309*4882a593Smuzhiyun 		for (i = 0; i < idx; i++) {
310*4882a593Smuzhiyun 			/* add up i-th component size, rounding up to 4 bytes */
311*4882a593Smuzhiyun 			offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
312*4882a593Smuzhiyun 		}
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 		/* calculate idx-th component data address */
315*4882a593Smuzhiyun 		*data = img_data + offset;
316*4882a593Smuzhiyun 	} else {
317*4882a593Smuzhiyun 		*len = 0;
318*4882a593Smuzhiyun 		*data = 0;
319*4882a593Smuzhiyun 	}
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun 
image_print_type(const image_header_t * hdr)322*4882a593Smuzhiyun static void image_print_type(const image_header_t *hdr)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	const char __maybe_unused *os, *arch, *type, *comp;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	os = genimg_get_os_name(image_get_os(hdr));
327*4882a593Smuzhiyun 	arch = genimg_get_arch_name(image_get_arch(hdr));
328*4882a593Smuzhiyun 	type = genimg_get_type_name(image_get_type(hdr));
329*4882a593Smuzhiyun 	comp = genimg_get_comp_name(image_get_comp(hdr));
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	printf("%s %s %s (%s)\n", arch, os, type, comp);
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun /**
335*4882a593Smuzhiyun  * image_print_contents - prints out the contents of the legacy format image
336*4882a593Smuzhiyun  * @ptr: pointer to the legacy format image header
337*4882a593Smuzhiyun  * @p: pointer to prefix string
338*4882a593Smuzhiyun  *
339*4882a593Smuzhiyun  * image_print_contents() formats a multi line legacy image contents description.
340*4882a593Smuzhiyun  * The routine prints out all header fields followed by the size/offset data
341*4882a593Smuzhiyun  * for MULTI/SCRIPT images.
342*4882a593Smuzhiyun  *
343*4882a593Smuzhiyun  * returns:
344*4882a593Smuzhiyun  *     no returned results
345*4882a593Smuzhiyun  */
image_print_contents(const void * ptr)346*4882a593Smuzhiyun void image_print_contents(const void *ptr)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	const image_header_t *hdr = (const image_header_t *)ptr;
349*4882a593Smuzhiyun 	const char __maybe_unused *p;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	p = IMAGE_INDENT_STRING;
352*4882a593Smuzhiyun 	printf("%sImage Name:   %.*s\n", p, IH_NMLEN, image_get_name(hdr));
353*4882a593Smuzhiyun 	if (IMAGE_ENABLE_TIMESTAMP) {
354*4882a593Smuzhiyun 		printf("%sCreated:      ", p);
355*4882a593Smuzhiyun 		genimg_print_time((time_t)image_get_time(hdr));
356*4882a593Smuzhiyun 	}
357*4882a593Smuzhiyun 	printf("%sImage Type:   ", p);
358*4882a593Smuzhiyun 	image_print_type(hdr);
359*4882a593Smuzhiyun 	printf("%sData Size:    ", p);
360*4882a593Smuzhiyun 	genimg_print_size(image_get_data_size(hdr));
361*4882a593Smuzhiyun 	printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
362*4882a593Smuzhiyun 	printf("%sEntry Point:  %08x\n", p, image_get_ep(hdr));
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	if (image_check_type(hdr, IH_TYPE_MULTI) ||
365*4882a593Smuzhiyun 			image_check_type(hdr, IH_TYPE_SCRIPT)) {
366*4882a593Smuzhiyun 		int i;
367*4882a593Smuzhiyun 		ulong data, len;
368*4882a593Smuzhiyun 		ulong count = image_multi_count(hdr);
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 		printf("%sContents:\n", p);
371*4882a593Smuzhiyun 		for (i = 0; i < count; i++) {
372*4882a593Smuzhiyun 			image_multi_getimg(hdr, i, &data, &len);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 			printf("%s   Image %d: ", p, i);
375*4882a593Smuzhiyun 			genimg_print_size(len);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 			if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
378*4882a593Smuzhiyun 				/*
379*4882a593Smuzhiyun 				 * the user may need to know offsets
380*4882a593Smuzhiyun 				 * if planning to do something with
381*4882a593Smuzhiyun 				 * multiple files
382*4882a593Smuzhiyun 				 */
383*4882a593Smuzhiyun 				printf("%s    Offset = 0x%08lx\n", p, data);
384*4882a593Smuzhiyun 			}
385*4882a593Smuzhiyun 		}
386*4882a593Smuzhiyun 	} else if (image_check_type(hdr, IH_TYPE_FIRMWARE_IVT)) {
387*4882a593Smuzhiyun 		printf("HAB Blocks:   0x%08x   0x0000   0x%08x\n",
388*4882a593Smuzhiyun 				image_get_load(hdr) - image_get_header_size(),
389*4882a593Smuzhiyun 				image_get_size(hdr) + image_get_header_size()
390*4882a593Smuzhiyun 						- 0x1FE0);
391*4882a593Smuzhiyun 	}
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun #ifndef USE_HOSTCC
396*4882a593Smuzhiyun #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
397*4882a593Smuzhiyun /**
398*4882a593Smuzhiyun  * image_get_ramdisk - get and verify ramdisk image
399*4882a593Smuzhiyun  * @rd_addr: ramdisk image start address
400*4882a593Smuzhiyun  * @arch: expected ramdisk architecture
401*4882a593Smuzhiyun  * @verify: checksum verification flag
402*4882a593Smuzhiyun  *
403*4882a593Smuzhiyun  * image_get_ramdisk() returns a pointer to the verified ramdisk image
404*4882a593Smuzhiyun  * header. Routine receives image start address and expected architecture
405*4882a593Smuzhiyun  * flag. Verification done covers data and header integrity and os/type/arch
406*4882a593Smuzhiyun  * fields checking.
407*4882a593Smuzhiyun  *
408*4882a593Smuzhiyun  * returns:
409*4882a593Smuzhiyun  *     pointer to a ramdisk image header, if image was found and valid
410*4882a593Smuzhiyun  *     otherwise, return NULL
411*4882a593Smuzhiyun  */
image_get_ramdisk(ulong rd_addr,uint8_t arch,int verify)412*4882a593Smuzhiyun static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
413*4882a593Smuzhiyun 						int verify)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	if (!image_check_magic(rd_hdr)) {
418*4882a593Smuzhiyun 		puts("Bad Magic Number\n");
419*4882a593Smuzhiyun 		bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
420*4882a593Smuzhiyun 		return NULL;
421*4882a593Smuzhiyun 	}
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (!image_check_hcrc(rd_hdr)) {
424*4882a593Smuzhiyun 		puts("Bad Header Checksum\n");
425*4882a593Smuzhiyun 		bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
426*4882a593Smuzhiyun 		return NULL;
427*4882a593Smuzhiyun 	}
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
430*4882a593Smuzhiyun 	image_print_contents(rd_hdr);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	if (verify) {
433*4882a593Smuzhiyun 		puts("   Verifying Checksum ... ");
434*4882a593Smuzhiyun 		if (!image_check_dcrc(rd_hdr)) {
435*4882a593Smuzhiyun 			puts("Bad Data CRC\n");
436*4882a593Smuzhiyun 			bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
437*4882a593Smuzhiyun 			return NULL;
438*4882a593Smuzhiyun 		}
439*4882a593Smuzhiyun 		puts("OK\n");
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
445*4882a593Smuzhiyun 	    !image_check_arch(rd_hdr, arch) ||
446*4882a593Smuzhiyun 	    !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
447*4882a593Smuzhiyun 		printf("No Linux %s Ramdisk Image\n",
448*4882a593Smuzhiyun 				genimg_get_arch_name(arch));
449*4882a593Smuzhiyun 		bootstage_error(BOOTSTAGE_ID_RAMDISK);
450*4882a593Smuzhiyun 		return NULL;
451*4882a593Smuzhiyun 	}
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	return rd_hdr;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun #endif
456*4882a593Smuzhiyun #endif /* !USE_HOSTCC */
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun /*****************************************************************************/
459*4882a593Smuzhiyun /* Shared dual-format routines */
460*4882a593Smuzhiyun /*****************************************************************************/
461*4882a593Smuzhiyun #ifndef USE_HOSTCC
462*4882a593Smuzhiyun ulong load_addr = CONFIG_SYS_LOAD_ADDR;	/* Default Load Address */
463*4882a593Smuzhiyun ulong save_addr;			/* Default Save Address */
464*4882a593Smuzhiyun ulong save_size;			/* Default Save Size (in bytes) */
465*4882a593Smuzhiyun 
on_loadaddr(const char * name,const char * value,enum env_op op,int flags)466*4882a593Smuzhiyun static int on_loadaddr(const char *name, const char *value, enum env_op op,
467*4882a593Smuzhiyun 	int flags)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun 	switch (op) {
470*4882a593Smuzhiyun 	case env_op_create:
471*4882a593Smuzhiyun 	case env_op_overwrite:
472*4882a593Smuzhiyun 		load_addr = simple_strtoul(value, NULL, 16);
473*4882a593Smuzhiyun 		break;
474*4882a593Smuzhiyun 	default:
475*4882a593Smuzhiyun 		break;
476*4882a593Smuzhiyun 	}
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	return 0;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
481*4882a593Smuzhiyun 
env_get_bootm_low(void)482*4882a593Smuzhiyun ulong env_get_bootm_low(void)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun 	char *s = env_get("bootm_low");
485*4882a593Smuzhiyun 	if (s) {
486*4882a593Smuzhiyun 		ulong tmp = simple_strtoul(s, NULL, 16);
487*4882a593Smuzhiyun 		return tmp;
488*4882a593Smuzhiyun 	}
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun #if defined(CONFIG_SYS_SDRAM_BASE)
491*4882a593Smuzhiyun 	return CONFIG_SYS_SDRAM_BASE;
492*4882a593Smuzhiyun #elif defined(CONFIG_ARM)
493*4882a593Smuzhiyun 	return gd->bd->bi_dram[0].start;
494*4882a593Smuzhiyun #else
495*4882a593Smuzhiyun 	return 0;
496*4882a593Smuzhiyun #endif
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun 
env_get_bootm_size(void)499*4882a593Smuzhiyun phys_size_t env_get_bootm_size(void)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	phys_size_t tmp, size;
502*4882a593Smuzhiyun 	phys_addr_t start;
503*4882a593Smuzhiyun 	char *s = env_get("bootm_size");
504*4882a593Smuzhiyun 	if (s) {
505*4882a593Smuzhiyun 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
506*4882a593Smuzhiyun 		return tmp;
507*4882a593Smuzhiyun 	}
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun #if defined(CONFIG_ARM) && defined(CONFIG_NR_DRAM_BANKS)
510*4882a593Smuzhiyun 	start = gd->bd->bi_dram[0].start;
511*4882a593Smuzhiyun 	size = gd->bd->bi_dram[0].size;
512*4882a593Smuzhiyun #else
513*4882a593Smuzhiyun 	start = gd->bd->bi_memstart;
514*4882a593Smuzhiyun 	size = gd->bd->bi_memsize;
515*4882a593Smuzhiyun #endif
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	s = env_get("bootm_low");
518*4882a593Smuzhiyun 	if (s)
519*4882a593Smuzhiyun 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
520*4882a593Smuzhiyun 	else
521*4882a593Smuzhiyun 		tmp = start;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	return size - (tmp - start);
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
env_get_bootm_mapsize(void)526*4882a593Smuzhiyun phys_size_t env_get_bootm_mapsize(void)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	phys_size_t tmp;
529*4882a593Smuzhiyun 	char *s = env_get("bootm_mapsize");
530*4882a593Smuzhiyun 	if (s) {
531*4882a593Smuzhiyun 		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
532*4882a593Smuzhiyun 		return tmp;
533*4882a593Smuzhiyun 	}
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun #if defined(CONFIG_SYS_BOOTMAPSZ)
536*4882a593Smuzhiyun 	return CONFIG_SYS_BOOTMAPSZ;
537*4882a593Smuzhiyun #else
538*4882a593Smuzhiyun 	return env_get_bootm_size();
539*4882a593Smuzhiyun #endif
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun 
memmove_wd(void * to,void * from,size_t len,ulong chunksz)542*4882a593Smuzhiyun void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun 	if (to == from)
545*4882a593Smuzhiyun 		return;
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
548*4882a593Smuzhiyun 	if (to > from) {
549*4882a593Smuzhiyun 		from += len;
550*4882a593Smuzhiyun 		to += len;
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun 	while (len > 0) {
553*4882a593Smuzhiyun 		size_t tail = (len > chunksz) ? chunksz : len;
554*4882a593Smuzhiyun 		WATCHDOG_RESET();
555*4882a593Smuzhiyun 		if (to > from) {
556*4882a593Smuzhiyun 			to -= tail;
557*4882a593Smuzhiyun 			from -= tail;
558*4882a593Smuzhiyun 		}
559*4882a593Smuzhiyun 		memmove(to, from, tail);
560*4882a593Smuzhiyun 		if (to < from) {
561*4882a593Smuzhiyun 			to += tail;
562*4882a593Smuzhiyun 			from += tail;
563*4882a593Smuzhiyun 		}
564*4882a593Smuzhiyun 		len -= tail;
565*4882a593Smuzhiyun 	}
566*4882a593Smuzhiyun #else	/* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
567*4882a593Smuzhiyun 	memmove(to, from, len);
568*4882a593Smuzhiyun #endif	/* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun #endif /* !USE_HOSTCC */
571*4882a593Smuzhiyun 
genimg_print_size(uint32_t size)572*4882a593Smuzhiyun void genimg_print_size(uint32_t size)
573*4882a593Smuzhiyun {
574*4882a593Smuzhiyun #ifndef USE_HOSTCC
575*4882a593Smuzhiyun 	printf("%d Bytes = ", size);
576*4882a593Smuzhiyun 	print_size(size, "\n");
577*4882a593Smuzhiyun #else
578*4882a593Smuzhiyun 	printf("%d Bytes = %.2f KiB = %.2f MiB\n",
579*4882a593Smuzhiyun 			size, (double)size / 1.024e3,
580*4882a593Smuzhiyun 			(double)size / 1.048576e6);
581*4882a593Smuzhiyun #endif
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun #if IMAGE_ENABLE_TIMESTAMP
genimg_print_time(time_t timestamp)585*4882a593Smuzhiyun void genimg_print_time(time_t timestamp)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun #ifndef USE_HOSTCC
588*4882a593Smuzhiyun 	struct rtc_time tm;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	rtc_to_tm(timestamp, &tm);
591*4882a593Smuzhiyun 	printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
592*4882a593Smuzhiyun 			tm.tm_year, tm.tm_mon, tm.tm_mday,
593*4882a593Smuzhiyun 			tm.tm_hour, tm.tm_min, tm.tm_sec);
594*4882a593Smuzhiyun #else
595*4882a593Smuzhiyun 	printf("%s", ctime(&timestamp));
596*4882a593Smuzhiyun #endif
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun #endif
599*4882a593Smuzhiyun 
get_table_entry(const table_entry_t * table,int id)600*4882a593Smuzhiyun const table_entry_t *get_table_entry(const table_entry_t *table, int id)
601*4882a593Smuzhiyun {
602*4882a593Smuzhiyun 	for (; table->id >= 0; ++table) {
603*4882a593Smuzhiyun 		if (table->id == id)
604*4882a593Smuzhiyun 			return table;
605*4882a593Smuzhiyun 	}
606*4882a593Smuzhiyun 	return NULL;
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun 
unknown_msg(enum ih_category category)609*4882a593Smuzhiyun static const char *unknown_msg(enum ih_category category)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	static const char unknown_str[] = "Unknown ";
612*4882a593Smuzhiyun 	static char msg[30];
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	strcpy(msg, unknown_str);
615*4882a593Smuzhiyun 	strncat(msg, table_info[category].desc,
616*4882a593Smuzhiyun 		sizeof(msg) - sizeof(unknown_str));
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	return msg;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun /**
622*4882a593Smuzhiyun  * get_cat_table_entry_name - translate entry id to long name
623*4882a593Smuzhiyun  * @category: category to look up (enum ih_category)
624*4882a593Smuzhiyun  * @id: entry id to be translated
625*4882a593Smuzhiyun  *
626*4882a593Smuzhiyun  * This will scan the translation table trying to find the entry that matches
627*4882a593Smuzhiyun  * the given id.
628*4882a593Smuzhiyun  *
629*4882a593Smuzhiyun  * @retur long entry name if translation succeeds; error string on failure
630*4882a593Smuzhiyun  */
genimg_get_cat_name(enum ih_category category,uint id)631*4882a593Smuzhiyun const char *genimg_get_cat_name(enum ih_category category, uint id)
632*4882a593Smuzhiyun {
633*4882a593Smuzhiyun 	const table_entry_t *entry;
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	entry = get_table_entry(table_info[category].table, id);
636*4882a593Smuzhiyun 	if (!entry)
637*4882a593Smuzhiyun 		return unknown_msg(category);
638*4882a593Smuzhiyun #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
639*4882a593Smuzhiyun 	return entry->lname;
640*4882a593Smuzhiyun #else
641*4882a593Smuzhiyun 	return entry->lname + gd->reloc_off;
642*4882a593Smuzhiyun #endif
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun /**
646*4882a593Smuzhiyun  * get_cat_table_entry_short_name - translate entry id to short name
647*4882a593Smuzhiyun  * @category: category to look up (enum ih_category)
648*4882a593Smuzhiyun  * @id: entry id to be translated
649*4882a593Smuzhiyun  *
650*4882a593Smuzhiyun  * This will scan the translation table trying to find the entry that matches
651*4882a593Smuzhiyun  * the given id.
652*4882a593Smuzhiyun  *
653*4882a593Smuzhiyun  * @retur short entry name if translation succeeds; error string on failure
654*4882a593Smuzhiyun  */
genimg_get_cat_short_name(enum ih_category category,uint id)655*4882a593Smuzhiyun const char *genimg_get_cat_short_name(enum ih_category category, uint id)
656*4882a593Smuzhiyun {
657*4882a593Smuzhiyun 	const table_entry_t *entry;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	entry = get_table_entry(table_info[category].table, id);
660*4882a593Smuzhiyun 	if (!entry)
661*4882a593Smuzhiyun 		return unknown_msg(category);
662*4882a593Smuzhiyun #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
663*4882a593Smuzhiyun 	return entry->sname;
664*4882a593Smuzhiyun #else
665*4882a593Smuzhiyun 	return entry->sname + gd->reloc_off;
666*4882a593Smuzhiyun #endif
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun 
genimg_get_cat_count(enum ih_category category)669*4882a593Smuzhiyun int genimg_get_cat_count(enum ih_category category)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun 	return table_info[category].count;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
genimg_get_cat_desc(enum ih_category category)674*4882a593Smuzhiyun const char *genimg_get_cat_desc(enum ih_category category)
675*4882a593Smuzhiyun {
676*4882a593Smuzhiyun 	return table_info[category].desc;
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun /**
680*4882a593Smuzhiyun  * get_table_entry_name - translate entry id to long name
681*4882a593Smuzhiyun  * @table: pointer to a translation table for entries of a specific type
682*4882a593Smuzhiyun  * @msg: message to be returned when translation fails
683*4882a593Smuzhiyun  * @id: entry id to be translated
684*4882a593Smuzhiyun  *
685*4882a593Smuzhiyun  * get_table_entry_name() will go over translation table trying to find
686*4882a593Smuzhiyun  * entry that matches given id. If matching entry is found, its long
687*4882a593Smuzhiyun  * name is returned to the caller.
688*4882a593Smuzhiyun  *
689*4882a593Smuzhiyun  * returns:
690*4882a593Smuzhiyun  *     long entry name if translation succeeds
691*4882a593Smuzhiyun  *     msg otherwise
692*4882a593Smuzhiyun  */
get_table_entry_name(const table_entry_t * table,char * msg,int id)693*4882a593Smuzhiyun char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
694*4882a593Smuzhiyun {
695*4882a593Smuzhiyun 	table = get_table_entry(table, id);
696*4882a593Smuzhiyun 	if (!table)
697*4882a593Smuzhiyun 		return msg;
698*4882a593Smuzhiyun #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
699*4882a593Smuzhiyun 	return table->lname;
700*4882a593Smuzhiyun #else
701*4882a593Smuzhiyun 	return table->lname + gd->reloc_off;
702*4882a593Smuzhiyun #endif
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun 
genimg_get_os_name(uint8_t os)705*4882a593Smuzhiyun const char *genimg_get_os_name(uint8_t os)
706*4882a593Smuzhiyun {
707*4882a593Smuzhiyun 	return (get_table_entry_name(uimage_os, "Unknown OS", os));
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun 
genimg_get_arch_name(uint8_t arch)710*4882a593Smuzhiyun const char *genimg_get_arch_name(uint8_t arch)
711*4882a593Smuzhiyun {
712*4882a593Smuzhiyun 	return (get_table_entry_name(uimage_arch, "Unknown Architecture",
713*4882a593Smuzhiyun 					arch));
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun 
genimg_get_type_name(uint8_t type)716*4882a593Smuzhiyun const char *genimg_get_type_name(uint8_t type)
717*4882a593Smuzhiyun {
718*4882a593Smuzhiyun 	return (get_table_entry_name(uimage_type, "Unknown Image", type));
719*4882a593Smuzhiyun }
720*4882a593Smuzhiyun 
genimg_get_short_name(const table_entry_t * table,int val)721*4882a593Smuzhiyun static const char *genimg_get_short_name(const table_entry_t *table, int val)
722*4882a593Smuzhiyun {
723*4882a593Smuzhiyun 	table = get_table_entry(table, val);
724*4882a593Smuzhiyun 	if (!table)
725*4882a593Smuzhiyun 		return "unknown";
726*4882a593Smuzhiyun #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
727*4882a593Smuzhiyun 	return table->sname;
728*4882a593Smuzhiyun #else
729*4882a593Smuzhiyun 	return table->sname + gd->reloc_off;
730*4882a593Smuzhiyun #endif
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun 
genimg_get_type_short_name(uint8_t type)733*4882a593Smuzhiyun const char *genimg_get_type_short_name(uint8_t type)
734*4882a593Smuzhiyun {
735*4882a593Smuzhiyun 	return genimg_get_short_name(uimage_type, type);
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
genimg_get_comp_name(uint8_t comp)738*4882a593Smuzhiyun const char *genimg_get_comp_name(uint8_t comp)
739*4882a593Smuzhiyun {
740*4882a593Smuzhiyun 	return (get_table_entry_name(uimage_comp, "Unknown Compression",
741*4882a593Smuzhiyun 					comp));
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun 
genimg_get_comp_short_name(uint8_t comp)744*4882a593Smuzhiyun const char *genimg_get_comp_short_name(uint8_t comp)
745*4882a593Smuzhiyun {
746*4882a593Smuzhiyun 	return genimg_get_short_name(uimage_comp, comp);
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun 
genimg_get_os_short_name(uint8_t os)749*4882a593Smuzhiyun const char *genimg_get_os_short_name(uint8_t os)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun 	return genimg_get_short_name(uimage_os, os);
752*4882a593Smuzhiyun }
753*4882a593Smuzhiyun 
genimg_get_arch_short_name(uint8_t arch)754*4882a593Smuzhiyun const char *genimg_get_arch_short_name(uint8_t arch)
755*4882a593Smuzhiyun {
756*4882a593Smuzhiyun 	return genimg_get_short_name(uimage_arch, arch);
757*4882a593Smuzhiyun }
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun /**
760*4882a593Smuzhiyun  * get_table_entry_id - translate short entry name to id
761*4882a593Smuzhiyun  * @table: pointer to a translation table for entries of a specific type
762*4882a593Smuzhiyun  * @table_name: to be used in case of error
763*4882a593Smuzhiyun  * @name: entry short name to be translated
764*4882a593Smuzhiyun  *
765*4882a593Smuzhiyun  * get_table_entry_id() will go over translation table trying to find
766*4882a593Smuzhiyun  * entry that matches given short name. If matching entry is found,
767*4882a593Smuzhiyun  * its id returned to the caller.
768*4882a593Smuzhiyun  *
769*4882a593Smuzhiyun  * returns:
770*4882a593Smuzhiyun  *     entry id if translation succeeds
771*4882a593Smuzhiyun  *     -1 otherwise
772*4882a593Smuzhiyun  */
get_table_entry_id(const table_entry_t * table,const char * table_name,const char * name)773*4882a593Smuzhiyun int get_table_entry_id(const table_entry_t *table,
774*4882a593Smuzhiyun 		const char *table_name, const char *name)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun 	const table_entry_t *t;
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	for (t = table; t->id >= 0; ++t) {
779*4882a593Smuzhiyun #ifdef CONFIG_NEEDS_MANUAL_RELOC
780*4882a593Smuzhiyun 		if (t->sname && strcasecmp(t->sname + gd->reloc_off, name) == 0)
781*4882a593Smuzhiyun #else
782*4882a593Smuzhiyun 		if (t->sname && strcasecmp(t->sname, name) == 0)
783*4882a593Smuzhiyun #endif
784*4882a593Smuzhiyun 			return (t->id);
785*4882a593Smuzhiyun 	}
786*4882a593Smuzhiyun 	debug("Invalid %s Type: %s\n", table_name, name);
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	return -1;
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun 
genimg_get_os_id(const char * name)791*4882a593Smuzhiyun int genimg_get_os_id(const char *name)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun 	return (get_table_entry_id(uimage_os, "OS", name));
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun 
genimg_get_arch_id(const char * name)796*4882a593Smuzhiyun int genimg_get_arch_id(const char *name)
797*4882a593Smuzhiyun {
798*4882a593Smuzhiyun 	return (get_table_entry_id(uimage_arch, "CPU", name));
799*4882a593Smuzhiyun }
800*4882a593Smuzhiyun 
genimg_get_type_id(const char * name)801*4882a593Smuzhiyun int genimg_get_type_id(const char *name)
802*4882a593Smuzhiyun {
803*4882a593Smuzhiyun 	return (get_table_entry_id(uimage_type, "Image", name));
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun 
genimg_get_comp_id(const char * name)806*4882a593Smuzhiyun int genimg_get_comp_id(const char *name)
807*4882a593Smuzhiyun {
808*4882a593Smuzhiyun 	return (get_table_entry_id(uimage_comp, "Compression", name));
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun #ifndef USE_HOSTCC
812*4882a593Smuzhiyun /**
813*4882a593Smuzhiyun  * genimg_get_kernel_addr_fit - get the real kernel address and return 2
814*4882a593Smuzhiyun  *                              FIT strings
815*4882a593Smuzhiyun  * @img_addr: a string might contain real image address
816*4882a593Smuzhiyun  * @fit_uname_config: double pointer to a char, will hold pointer to a
817*4882a593Smuzhiyun  *                    configuration unit name
818*4882a593Smuzhiyun  * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
819*4882a593Smuzhiyun  *                    name
820*4882a593Smuzhiyun  *
821*4882a593Smuzhiyun  * genimg_get_kernel_addr_fit get the real kernel start address from a string
822*4882a593Smuzhiyun  * which is normally the first argv of bootm/bootz
823*4882a593Smuzhiyun  *
824*4882a593Smuzhiyun  * returns:
825*4882a593Smuzhiyun  *     kernel start address
826*4882a593Smuzhiyun  */
genimg_get_kernel_addr_fit(char * const img_addr,const char ** fit_uname_config,const char ** fit_uname_kernel)827*4882a593Smuzhiyun ulong genimg_get_kernel_addr_fit(char * const img_addr,
828*4882a593Smuzhiyun 			     const char **fit_uname_config,
829*4882a593Smuzhiyun 			     const char **fit_uname_kernel)
830*4882a593Smuzhiyun {
831*4882a593Smuzhiyun 	ulong kernel_addr;
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	/* find out kernel image address */
834*4882a593Smuzhiyun 	if (!img_addr) {
835*4882a593Smuzhiyun 		kernel_addr = load_addr;
836*4882a593Smuzhiyun 		debug("*  kernel: default image load address = 0x%08lx\n",
837*4882a593Smuzhiyun 		      load_addr);
838*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(FIT)
839*4882a593Smuzhiyun 	} else if (fit_parse_conf(img_addr, load_addr, &kernel_addr,
840*4882a593Smuzhiyun 				  fit_uname_config)) {
841*4882a593Smuzhiyun 		debug("*  kernel: config '%s' from image at 0x%08lx\n",
842*4882a593Smuzhiyun 		      *fit_uname_config, kernel_addr);
843*4882a593Smuzhiyun 	} else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
844*4882a593Smuzhiyun 				     fit_uname_kernel)) {
845*4882a593Smuzhiyun 		debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
846*4882a593Smuzhiyun 		      *fit_uname_kernel, kernel_addr);
847*4882a593Smuzhiyun #endif
848*4882a593Smuzhiyun 	} else {
849*4882a593Smuzhiyun 		kernel_addr = simple_strtoul(img_addr, NULL, 16);
850*4882a593Smuzhiyun 		debug("*  kernel: cmdline image address = 0x%08lx\n",
851*4882a593Smuzhiyun 		      kernel_addr);
852*4882a593Smuzhiyun 	}
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	return kernel_addr;
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun /**
858*4882a593Smuzhiyun  * genimg_get_kernel_addr() is the simple version of
859*4882a593Smuzhiyun  * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
860*4882a593Smuzhiyun  */
genimg_get_kernel_addr(char * const img_addr)861*4882a593Smuzhiyun ulong genimg_get_kernel_addr(char * const img_addr)
862*4882a593Smuzhiyun {
863*4882a593Smuzhiyun 	const char *fit_uname_config = NULL;
864*4882a593Smuzhiyun 	const char *fit_uname_kernel = NULL;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
867*4882a593Smuzhiyun 					  &fit_uname_kernel);
868*4882a593Smuzhiyun }
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun /**
871*4882a593Smuzhiyun  * genimg_get_format - get image format type
872*4882a593Smuzhiyun  * @img_addr: image start address
873*4882a593Smuzhiyun  *
874*4882a593Smuzhiyun  * genimg_get_format() checks whether provided address points to a valid
875*4882a593Smuzhiyun  * legacy or FIT image.
876*4882a593Smuzhiyun  *
877*4882a593Smuzhiyun  * New uImage format and FDT blob are based on a libfdt. FDT blob
878*4882a593Smuzhiyun  * may be passed directly or embedded in a FIT image. In both situations
879*4882a593Smuzhiyun  * genimg_get_format() must be able to dectect libfdt header.
880*4882a593Smuzhiyun  *
881*4882a593Smuzhiyun  * returns:
882*4882a593Smuzhiyun  *     image format type or IMAGE_FORMAT_INVALID if no image is present
883*4882a593Smuzhiyun  */
genimg_get_format(const void * img_addr)884*4882a593Smuzhiyun int genimg_get_format(const void *img_addr)
885*4882a593Smuzhiyun {
886*4882a593Smuzhiyun #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
887*4882a593Smuzhiyun 	const image_header_t *hdr;
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	hdr = (const image_header_t *)img_addr;
890*4882a593Smuzhiyun 	if (image_check_magic(hdr))
891*4882a593Smuzhiyun 		return IMAGE_FORMAT_LEGACY;
892*4882a593Smuzhiyun #endif
893*4882a593Smuzhiyun #if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
894*4882a593Smuzhiyun 	if (fdt_check_header(img_addr) == 0)
895*4882a593Smuzhiyun 		return IMAGE_FORMAT_FIT;
896*4882a593Smuzhiyun #endif
897*4882a593Smuzhiyun #ifdef CONFIG_ANDROID_BOOT_IMAGE
898*4882a593Smuzhiyun 	if (android_image_check_header(img_addr) == 0)
899*4882a593Smuzhiyun 		return IMAGE_FORMAT_ANDROID;
900*4882a593Smuzhiyun #endif
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	return IMAGE_FORMAT_INVALID;
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun /**
906*4882a593Smuzhiyun  * fit_has_config - check if there is a valid FIT configuration
907*4882a593Smuzhiyun  * @images: pointer to the bootm command headers structure
908*4882a593Smuzhiyun  *
909*4882a593Smuzhiyun  * fit_has_config() checks if there is a FIT configuration in use
910*4882a593Smuzhiyun  * (if FTI support is present).
911*4882a593Smuzhiyun  *
912*4882a593Smuzhiyun  * returns:
913*4882a593Smuzhiyun  *     0, no FIT support or no configuration found
914*4882a593Smuzhiyun  *     1, configuration found
915*4882a593Smuzhiyun  */
genimg_has_config(bootm_headers_t * images)916*4882a593Smuzhiyun int genimg_has_config(bootm_headers_t *images)
917*4882a593Smuzhiyun {
918*4882a593Smuzhiyun #if IMAGE_ENABLE_FIT
919*4882a593Smuzhiyun 	if (images->fit_uname_cfg)
920*4882a593Smuzhiyun 		return 1;
921*4882a593Smuzhiyun #endif
922*4882a593Smuzhiyun 	return 0;
923*4882a593Smuzhiyun }
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun /**
926*4882a593Smuzhiyun  * boot_get_ramdisk - main ramdisk handling routine
927*4882a593Smuzhiyun  * @argc: command argument count
928*4882a593Smuzhiyun  * @argv: command argument list
929*4882a593Smuzhiyun  * @images: pointer to the bootm images structure
930*4882a593Smuzhiyun  * @arch: expected ramdisk architecture
931*4882a593Smuzhiyun  * @rd_start: pointer to a ulong variable, will hold ramdisk start address
932*4882a593Smuzhiyun  * @rd_end: pointer to a ulong variable, will hold ramdisk end
933*4882a593Smuzhiyun  *
934*4882a593Smuzhiyun  * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
935*4882a593Smuzhiyun  * Curently supported are the following ramdisk sources:
936*4882a593Smuzhiyun  *      - multicomponent kernel/ramdisk image,
937*4882a593Smuzhiyun  *      - commandline provided address of decicated ramdisk image.
938*4882a593Smuzhiyun  *
939*4882a593Smuzhiyun  * returns:
940*4882a593Smuzhiyun  *     0, if ramdisk image was found and valid, or skiped
941*4882a593Smuzhiyun  *     rd_start and rd_end are set to ramdisk start/end addresses if
942*4882a593Smuzhiyun  *     ramdisk image is found and valid
943*4882a593Smuzhiyun  *
944*4882a593Smuzhiyun  *     1, if ramdisk image is found but corrupted, or invalid
945*4882a593Smuzhiyun  *     rd_start and rd_end are set to 0 if no ramdisk exists
946*4882a593Smuzhiyun  */
boot_get_ramdisk(int argc,char * const argv[],bootm_headers_t * images,uint8_t arch,ulong * rd_start,ulong * rd_end)947*4882a593Smuzhiyun int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
948*4882a593Smuzhiyun 		uint8_t arch, ulong *rd_start, ulong *rd_end)
949*4882a593Smuzhiyun {
950*4882a593Smuzhiyun 	ulong rd_addr, rd_load;
951*4882a593Smuzhiyun 	ulong rd_data, rd_len;
952*4882a593Smuzhiyun #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
953*4882a593Smuzhiyun 	const image_header_t *rd_hdr;
954*4882a593Smuzhiyun #endif
955*4882a593Smuzhiyun 	void *buf;
956*4882a593Smuzhiyun #ifdef CONFIG_SUPPORT_RAW_INITRD
957*4882a593Smuzhiyun 	char *end;
958*4882a593Smuzhiyun #endif
959*4882a593Smuzhiyun #if IMAGE_ENABLE_FIT
960*4882a593Smuzhiyun 	const char	*fit_uname_config = images->fit_uname_cfg;
961*4882a593Smuzhiyun 	const char	*fit_uname_ramdisk = NULL;
962*4882a593Smuzhiyun 	ulong		default_addr;
963*4882a593Smuzhiyun 	int		rd_noffset;
964*4882a593Smuzhiyun #endif
965*4882a593Smuzhiyun 	const char *select = NULL;
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	*rd_start = 0;
968*4882a593Smuzhiyun 	*rd_end = 0;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun #ifdef CONFIG_ANDROID_BOOT_IMAGE
971*4882a593Smuzhiyun 	/*
972*4882a593Smuzhiyun 	 * Look for an Android boot image.
973*4882a593Smuzhiyun 	 */
974*4882a593Smuzhiyun 	buf = map_sysmem(images->os.start, 0);
975*4882a593Smuzhiyun 	if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
976*4882a593Smuzhiyun 		select = argv[0];
977*4882a593Smuzhiyun #endif
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 	if (argc >= 2)
980*4882a593Smuzhiyun 		select = argv[1];
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	/*
983*4882a593Smuzhiyun 	 * Look for a '-' which indicates to ignore the
984*4882a593Smuzhiyun 	 * ramdisk argument
985*4882a593Smuzhiyun 	 */
986*4882a593Smuzhiyun 	if (select && strcmp(select, "-") ==  0) {
987*4882a593Smuzhiyun 		debug("## Skipping init Ramdisk\n");
988*4882a593Smuzhiyun 		rd_len = rd_data = 0;
989*4882a593Smuzhiyun 	} else if (select || genimg_has_config(images)) {
990*4882a593Smuzhiyun #if IMAGE_ENABLE_FIT
991*4882a593Smuzhiyun 		if (select) {
992*4882a593Smuzhiyun 			/*
993*4882a593Smuzhiyun 			 * If the init ramdisk comes from the FIT image and
994*4882a593Smuzhiyun 			 * the FIT image address is omitted in the command
995*4882a593Smuzhiyun 			 * line argument, try to use os FIT image address or
996*4882a593Smuzhiyun 			 * default load address.
997*4882a593Smuzhiyun 			 */
998*4882a593Smuzhiyun 			if (images->fit_uname_os)
999*4882a593Smuzhiyun 				default_addr = (ulong)images->fit_hdr_os;
1000*4882a593Smuzhiyun 			else
1001*4882a593Smuzhiyun 				default_addr = load_addr;
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 			if (fit_parse_conf(select, default_addr,
1004*4882a593Smuzhiyun 					   &rd_addr, &fit_uname_config)) {
1005*4882a593Smuzhiyun 				debug("*  ramdisk: config '%s' from image at "
1006*4882a593Smuzhiyun 						"0x%08lx\n",
1007*4882a593Smuzhiyun 						fit_uname_config, rd_addr);
1008*4882a593Smuzhiyun 			} else if (fit_parse_subimage(select, default_addr,
1009*4882a593Smuzhiyun 						&rd_addr, &fit_uname_ramdisk)) {
1010*4882a593Smuzhiyun 				debug("*  ramdisk: subimage '%s' from image at "
1011*4882a593Smuzhiyun 						"0x%08lx\n",
1012*4882a593Smuzhiyun 						fit_uname_ramdisk, rd_addr);
1013*4882a593Smuzhiyun 			} else
1014*4882a593Smuzhiyun #endif
1015*4882a593Smuzhiyun 			{
1016*4882a593Smuzhiyun 				rd_addr = simple_strtoul(select, NULL, 16);
1017*4882a593Smuzhiyun 				debug("*  ramdisk: cmdline image address = "
1018*4882a593Smuzhiyun 						"0x%08lx\n",
1019*4882a593Smuzhiyun 						rd_addr);
1020*4882a593Smuzhiyun 			}
1021*4882a593Smuzhiyun #if IMAGE_ENABLE_FIT
1022*4882a593Smuzhiyun 		} else {
1023*4882a593Smuzhiyun 			/* use FIT configuration provided in first bootm
1024*4882a593Smuzhiyun 			 * command argument. If the property is not defined,
1025*4882a593Smuzhiyun 			 * quit silently.
1026*4882a593Smuzhiyun 			 */
1027*4882a593Smuzhiyun 			rd_addr = map_to_sysmem(images->fit_hdr_os);
1028*4882a593Smuzhiyun 			rd_noffset = fit_get_node_from_config(images,
1029*4882a593Smuzhiyun 					FIT_RAMDISK_PROP, rd_addr);
1030*4882a593Smuzhiyun 			if (rd_noffset == -ENOENT)
1031*4882a593Smuzhiyun 				return 0;
1032*4882a593Smuzhiyun 			else if (rd_noffset < 0)
1033*4882a593Smuzhiyun 				return 1;
1034*4882a593Smuzhiyun 		}
1035*4882a593Smuzhiyun #endif
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 		/*
1038*4882a593Smuzhiyun 		 * Check if there is an initrd image at the
1039*4882a593Smuzhiyun 		 * address provided in the second bootm argument
1040*4882a593Smuzhiyun 		 * check image type, for FIT images get FIT node.
1041*4882a593Smuzhiyun 		 */
1042*4882a593Smuzhiyun 		buf = map_sysmem(rd_addr, 0);
1043*4882a593Smuzhiyun 		switch (genimg_get_format(buf)) {
1044*4882a593Smuzhiyun #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
1045*4882a593Smuzhiyun 		case IMAGE_FORMAT_LEGACY:
1046*4882a593Smuzhiyun 			printf("## Loading init Ramdisk from Legacy "
1047*4882a593Smuzhiyun 					"Image at %08lx ...\n", rd_addr);
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 			bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
1050*4882a593Smuzhiyun 			rd_hdr = image_get_ramdisk(rd_addr, arch,
1051*4882a593Smuzhiyun 							images->verify);
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 			if (rd_hdr == NULL)
1054*4882a593Smuzhiyun 				return 1;
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 			rd_data = image_get_data(rd_hdr);
1057*4882a593Smuzhiyun 			rd_len = image_get_data_size(rd_hdr);
1058*4882a593Smuzhiyun 			rd_load = image_get_load(rd_hdr);
1059*4882a593Smuzhiyun 			break;
1060*4882a593Smuzhiyun #endif
1061*4882a593Smuzhiyun #if IMAGE_ENABLE_FIT
1062*4882a593Smuzhiyun 		case IMAGE_FORMAT_FIT:
1063*4882a593Smuzhiyun 			rd_noffset = fit_image_load(images,
1064*4882a593Smuzhiyun 					rd_addr, &fit_uname_ramdisk,
1065*4882a593Smuzhiyun 					&fit_uname_config, arch,
1066*4882a593Smuzhiyun 					IH_TYPE_RAMDISK,
1067*4882a593Smuzhiyun 					BOOTSTAGE_ID_FIT_RD_START,
1068*4882a593Smuzhiyun 					FIT_LOAD_OPTIONAL_NON_ZERO,
1069*4882a593Smuzhiyun 					&rd_data, &rd_len);
1070*4882a593Smuzhiyun 			if (rd_noffset < 0)
1071*4882a593Smuzhiyun 				return 1;
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 			images->fit_hdr_rd = map_sysmem(rd_addr, 0);
1074*4882a593Smuzhiyun 			images->fit_uname_rd = fit_uname_ramdisk;
1075*4882a593Smuzhiyun 			images->fit_noffset_rd = rd_noffset;
1076*4882a593Smuzhiyun 			break;
1077*4882a593Smuzhiyun #endif
1078*4882a593Smuzhiyun #ifdef CONFIG_ANDROID_BOOT_IMAGE
1079*4882a593Smuzhiyun 		case IMAGE_FORMAT_ANDROID:
1080*4882a593Smuzhiyun 			android_image_get_ramdisk((void *)images->os.start,
1081*4882a593Smuzhiyun 				&rd_data, &rd_len);
1082*4882a593Smuzhiyun 			break;
1083*4882a593Smuzhiyun #endif
1084*4882a593Smuzhiyun 		default:
1085*4882a593Smuzhiyun #ifdef CONFIG_SUPPORT_RAW_INITRD
1086*4882a593Smuzhiyun 			end = NULL;
1087*4882a593Smuzhiyun 			if (select)
1088*4882a593Smuzhiyun 				end = strchr(select, ':');
1089*4882a593Smuzhiyun 			if (end) {
1090*4882a593Smuzhiyun 				rd_len = simple_strtoul(++end, NULL, 16);
1091*4882a593Smuzhiyun 				rd_data = rd_addr;
1092*4882a593Smuzhiyun 			} else
1093*4882a593Smuzhiyun #endif
1094*4882a593Smuzhiyun 			{
1095*4882a593Smuzhiyun 				puts("Wrong Ramdisk Image Format\n");
1096*4882a593Smuzhiyun 				rd_data = rd_len = rd_load = 0;
1097*4882a593Smuzhiyun 				return 1;
1098*4882a593Smuzhiyun 			}
1099*4882a593Smuzhiyun 		}
1100*4882a593Smuzhiyun 	} else if (images->legacy_hdr_valid &&
1101*4882a593Smuzhiyun 			image_check_type(&images->legacy_hdr_os_copy,
1102*4882a593Smuzhiyun 						IH_TYPE_MULTI)) {
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 		/*
1105*4882a593Smuzhiyun 		 * Now check if we have a legacy mult-component image,
1106*4882a593Smuzhiyun 		 * get second entry data start address and len.
1107*4882a593Smuzhiyun 		 */
1108*4882a593Smuzhiyun 		bootstage_mark(BOOTSTAGE_ID_RAMDISK);
1109*4882a593Smuzhiyun 		printf("## Loading init Ramdisk from multi component "
1110*4882a593Smuzhiyun 				"Legacy Image at %08lx ...\n",
1111*4882a593Smuzhiyun 				(ulong)images->legacy_hdr_os);
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 		image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1114*4882a593Smuzhiyun 	} else {
1115*4882a593Smuzhiyun 		/*
1116*4882a593Smuzhiyun 		 * no initrd image
1117*4882a593Smuzhiyun 		 */
1118*4882a593Smuzhiyun 		bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1119*4882a593Smuzhiyun 		rd_len = rd_data = 0;
1120*4882a593Smuzhiyun 	}
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	if (!rd_data) {
1123*4882a593Smuzhiyun 		debug("## No init Ramdisk\n");
1124*4882a593Smuzhiyun 	} else {
1125*4882a593Smuzhiyun 		*rd_start = rd_data;
1126*4882a593Smuzhiyun 		*rd_end = rd_data + rd_len;
1127*4882a593Smuzhiyun 	}
1128*4882a593Smuzhiyun 	debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1129*4882a593Smuzhiyun 			*rd_start, *rd_end);
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	return 0;
1132*4882a593Smuzhiyun }
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1135*4882a593Smuzhiyun /**
1136*4882a593Smuzhiyun  * boot_ramdisk_high - relocate init ramdisk
1137*4882a593Smuzhiyun  * @lmb: pointer to lmb handle, will be used for memory mgmt
1138*4882a593Smuzhiyun  * @rd_data: ramdisk data start address
1139*4882a593Smuzhiyun  * @rd_len: ramdisk data length
1140*4882a593Smuzhiyun  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1141*4882a593Smuzhiyun  *      start address (after possible relocation)
1142*4882a593Smuzhiyun  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1143*4882a593Smuzhiyun  *      end address (after possible relocation)
1144*4882a593Smuzhiyun  *
1145*4882a593Smuzhiyun  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
1146*4882a593Smuzhiyun  * variable and if requested ramdisk data is moved to a specified location.
1147*4882a593Smuzhiyun  *
1148*4882a593Smuzhiyun  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1149*4882a593Smuzhiyun  * start/end addresses if ramdisk image start and len were provided,
1150*4882a593Smuzhiyun  * otherwise set initrd_start and initrd_end set to zeros.
1151*4882a593Smuzhiyun  *
1152*4882a593Smuzhiyun  * returns:
1153*4882a593Smuzhiyun  *      0 - success
1154*4882a593Smuzhiyun  *     -1 - failure
1155*4882a593Smuzhiyun  */
boot_ramdisk_high(struct lmb * lmb,ulong rd_data,ulong rd_len,ulong * initrd_start,ulong * initrd_end)1156*4882a593Smuzhiyun int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1157*4882a593Smuzhiyun 		  ulong *initrd_start, ulong *initrd_end)
1158*4882a593Smuzhiyun {
1159*4882a593Smuzhiyun 	char	*s;
1160*4882a593Smuzhiyun 	ulong	initrd_high;
1161*4882a593Smuzhiyun 	int	initrd_copy_to_ram = 1;
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	s = env_get("initrd_high");
1164*4882a593Smuzhiyun 	if (s) {
1165*4882a593Smuzhiyun 		/* a value of "no" or a similar string will act like 0,
1166*4882a593Smuzhiyun 		 * turning the "load high" feature off. This is intentional.
1167*4882a593Smuzhiyun 		 */
1168*4882a593Smuzhiyun 		initrd_high = simple_strtoul(s, NULL, 16);
1169*4882a593Smuzhiyun 		if (initrd_high == ~0)
1170*4882a593Smuzhiyun 			initrd_copy_to_ram = 0;
1171*4882a593Smuzhiyun 	} else {
1172*4882a593Smuzhiyun 		initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
1173*4882a593Smuzhiyun 	}
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1177*4882a593Smuzhiyun 			initrd_high, initrd_copy_to_ram);
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun 	if (rd_data) {
1180*4882a593Smuzhiyun 		if (!initrd_copy_to_ram) {	/* zero-copy ramdisk support */
1181*4882a593Smuzhiyun 			debug("   in-place initrd\n");
1182*4882a593Smuzhiyun 			*initrd_start = rd_data;
1183*4882a593Smuzhiyun 			*initrd_end = rd_data + rd_len;
1184*4882a593Smuzhiyun 			lmb_reserve(lmb, rd_data, rd_len);
1185*4882a593Smuzhiyun 		} else {
1186*4882a593Smuzhiyun 			if (initrd_high && env_get_yesno("bootm-reloc-at"))
1187*4882a593Smuzhiyun 				initrd_high += rd_len;
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 			if (initrd_high)
1190*4882a593Smuzhiyun 				*initrd_start = (ulong)lmb_alloc_base(lmb,
1191*4882a593Smuzhiyun 						rd_len, 0x1000, initrd_high);
1192*4882a593Smuzhiyun 			else
1193*4882a593Smuzhiyun 				*initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1194*4882a593Smuzhiyun 								 0x1000);
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 			if (*initrd_start == 0) {
1197*4882a593Smuzhiyun 				puts("ramdisk - allocation error\n");
1198*4882a593Smuzhiyun 				goto error;
1199*4882a593Smuzhiyun 			}
1200*4882a593Smuzhiyun 			bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 			*initrd_end = *initrd_start + rd_len;
1203*4882a593Smuzhiyun 			printf("   Loading Ramdisk to %08lx, end %08lx ... ",
1204*4882a593Smuzhiyun 					*initrd_start, *initrd_end);
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 			memmove_wd((void *)*initrd_start,
1207*4882a593Smuzhiyun 					(void *)rd_data, rd_len, CHUNKSZ);
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun #ifdef CONFIG_MP
1210*4882a593Smuzhiyun 			/*
1211*4882a593Smuzhiyun 			 * Ensure the image is flushed to memory to handle
1212*4882a593Smuzhiyun 			 * AMP boot scenarios in which we might not be
1213*4882a593Smuzhiyun 			 * HW cache coherent
1214*4882a593Smuzhiyun 			 */
1215*4882a593Smuzhiyun 			flush_cache((unsigned long)*initrd_start, rd_len);
1216*4882a593Smuzhiyun #endif
1217*4882a593Smuzhiyun 			puts("OK\n");
1218*4882a593Smuzhiyun 		}
1219*4882a593Smuzhiyun 	} else {
1220*4882a593Smuzhiyun 		*initrd_start = 0;
1221*4882a593Smuzhiyun 		*initrd_end = 0;
1222*4882a593Smuzhiyun 	}
1223*4882a593Smuzhiyun 	debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1224*4882a593Smuzhiyun 			*initrd_start, *initrd_end);
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 	return 0;
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun error:
1229*4882a593Smuzhiyun 	return -1;
1230*4882a593Smuzhiyun }
1231*4882a593Smuzhiyun #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1232*4882a593Smuzhiyun 
boot_get_setup(bootm_headers_t * images,uint8_t arch,ulong * setup_start,ulong * setup_len)1233*4882a593Smuzhiyun int boot_get_setup(bootm_headers_t *images, uint8_t arch,
1234*4882a593Smuzhiyun 		   ulong *setup_start, ulong *setup_len)
1235*4882a593Smuzhiyun {
1236*4882a593Smuzhiyun #if IMAGE_ENABLE_FIT
1237*4882a593Smuzhiyun 	return boot_get_setup_fit(images, arch, setup_start, setup_len);
1238*4882a593Smuzhiyun #else
1239*4882a593Smuzhiyun 	return -ENOENT;
1240*4882a593Smuzhiyun #endif
1241*4882a593Smuzhiyun }
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun #if IMAGE_ENABLE_FIT
1244*4882a593Smuzhiyun #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_XILINX)
boot_get_fpga(int argc,char * const argv[],bootm_headers_t * images,uint8_t arch,const ulong * ld_start,ulong * const ld_len)1245*4882a593Smuzhiyun int boot_get_fpga(int argc, char * const argv[], bootm_headers_t *images,
1246*4882a593Smuzhiyun 		  uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1247*4882a593Smuzhiyun {
1248*4882a593Smuzhiyun 	ulong tmp_img_addr, img_data, img_len;
1249*4882a593Smuzhiyun 	void *buf;
1250*4882a593Smuzhiyun 	int conf_noffset;
1251*4882a593Smuzhiyun 	int fit_img_result;
1252*4882a593Smuzhiyun 	const char *uname, *name;
1253*4882a593Smuzhiyun 	int err;
1254*4882a593Smuzhiyun 	int devnum = 0; /* TODO support multi fpga platforms */
1255*4882a593Smuzhiyun 	const fpga_desc * const desc = fpga_get_desc(devnum);
1256*4882a593Smuzhiyun 	xilinx_desc *desc_xilinx = desc->devdesc;
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun 	/* Check to see if the images struct has a FIT configuration */
1259*4882a593Smuzhiyun 	if (!genimg_has_config(images)) {
1260*4882a593Smuzhiyun 		debug("## FIT configuration was not specified\n");
1261*4882a593Smuzhiyun 		return 0;
1262*4882a593Smuzhiyun 	}
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 	/*
1265*4882a593Smuzhiyun 	 * Obtain the os FIT header from the images struct
1266*4882a593Smuzhiyun 	 */
1267*4882a593Smuzhiyun 	tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
1268*4882a593Smuzhiyun 	buf = map_sysmem(tmp_img_addr, 0);
1269*4882a593Smuzhiyun 	/*
1270*4882a593Smuzhiyun 	 * Check image type. For FIT images get FIT node
1271*4882a593Smuzhiyun 	 * and attempt to locate a generic binary.
1272*4882a593Smuzhiyun 	 */
1273*4882a593Smuzhiyun 	switch (genimg_get_format(buf)) {
1274*4882a593Smuzhiyun 	case IMAGE_FORMAT_FIT:
1275*4882a593Smuzhiyun 		conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 		uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
1278*4882a593Smuzhiyun 					   NULL);
1279*4882a593Smuzhiyun 		if (!uname) {
1280*4882a593Smuzhiyun 			debug("## FPGA image is not specified\n");
1281*4882a593Smuzhiyun 			return 0;
1282*4882a593Smuzhiyun 		}
1283*4882a593Smuzhiyun 		fit_img_result = fit_image_load(images,
1284*4882a593Smuzhiyun 						tmp_img_addr,
1285*4882a593Smuzhiyun 						(const char **)&uname,
1286*4882a593Smuzhiyun 						&(images->fit_uname_cfg),
1287*4882a593Smuzhiyun 						arch,
1288*4882a593Smuzhiyun 						IH_TYPE_FPGA,
1289*4882a593Smuzhiyun 						BOOTSTAGE_ID_FPGA_INIT,
1290*4882a593Smuzhiyun 						FIT_LOAD_OPTIONAL_NON_ZERO,
1291*4882a593Smuzhiyun 						&img_data, &img_len);
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 		debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
1294*4882a593Smuzhiyun 		      uname, img_data, img_len);
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun 		if (fit_img_result < 0) {
1297*4882a593Smuzhiyun 			/* Something went wrong! */
1298*4882a593Smuzhiyun 			return fit_img_result;
1299*4882a593Smuzhiyun 		}
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun 		if (img_len >= desc_xilinx->size) {
1302*4882a593Smuzhiyun 			name = "full";
1303*4882a593Smuzhiyun 			err = fpga_loadbitstream(devnum, (char *)img_data,
1304*4882a593Smuzhiyun 						 img_len, BIT_FULL);
1305*4882a593Smuzhiyun 			if (err)
1306*4882a593Smuzhiyun 				err = fpga_load(devnum, (const void *)img_data,
1307*4882a593Smuzhiyun 						img_len, BIT_FULL);
1308*4882a593Smuzhiyun 		} else {
1309*4882a593Smuzhiyun 			name = "partial";
1310*4882a593Smuzhiyun 			err = fpga_loadbitstream(devnum, (char *)img_data,
1311*4882a593Smuzhiyun 						 img_len, BIT_PARTIAL);
1312*4882a593Smuzhiyun 			if (err)
1313*4882a593Smuzhiyun 				err = fpga_load(devnum, (const void *)img_data,
1314*4882a593Smuzhiyun 						img_len, BIT_PARTIAL);
1315*4882a593Smuzhiyun 		}
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 		if (err)
1318*4882a593Smuzhiyun 			return err;
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 		printf("   Programming %s bitstream... OK\n", name);
1321*4882a593Smuzhiyun 		break;
1322*4882a593Smuzhiyun 	default:
1323*4882a593Smuzhiyun 		printf("The given image format is not supported (corrupt?)\n");
1324*4882a593Smuzhiyun 		return 1;
1325*4882a593Smuzhiyun 	}
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 	return 0;
1328*4882a593Smuzhiyun }
1329*4882a593Smuzhiyun #endif
1330*4882a593Smuzhiyun 
fit_loadable_process(uint8_t img_type,ulong img_data,ulong img_len)1331*4882a593Smuzhiyun static void fit_loadable_process(uint8_t img_type,
1332*4882a593Smuzhiyun 				 ulong img_data,
1333*4882a593Smuzhiyun 				 ulong img_len)
1334*4882a593Smuzhiyun {
1335*4882a593Smuzhiyun 	int i;
1336*4882a593Smuzhiyun 	const unsigned int count =
1337*4882a593Smuzhiyun 			ll_entry_count(struct fit_loadable_tbl, fit_loadable);
1338*4882a593Smuzhiyun 	struct fit_loadable_tbl *fit_loadable_handler =
1339*4882a593Smuzhiyun 			ll_entry_start(struct fit_loadable_tbl, fit_loadable);
1340*4882a593Smuzhiyun 	/* For each loadable handler */
1341*4882a593Smuzhiyun 	for (i = 0; i < count; i++, fit_loadable_handler++)
1342*4882a593Smuzhiyun 		/* matching this type */
1343*4882a593Smuzhiyun 		if (fit_loadable_handler->type == img_type)
1344*4882a593Smuzhiyun 			/* call that handler with this image data */
1345*4882a593Smuzhiyun 			fit_loadable_handler->handler(img_data, img_len);
1346*4882a593Smuzhiyun }
1347*4882a593Smuzhiyun 
boot_get_loadable(int argc,char * const argv[],bootm_headers_t * images,uint8_t arch,const ulong * ld_start,ulong * const ld_len)1348*4882a593Smuzhiyun int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
1349*4882a593Smuzhiyun 		uint8_t arch, const ulong *ld_start, ulong * const ld_len)
1350*4882a593Smuzhiyun {
1351*4882a593Smuzhiyun 	/*
1352*4882a593Smuzhiyun 	 * These variables are used to hold the current image location
1353*4882a593Smuzhiyun 	 * in system memory.
1354*4882a593Smuzhiyun 	 */
1355*4882a593Smuzhiyun 	ulong tmp_img_addr;
1356*4882a593Smuzhiyun 	/*
1357*4882a593Smuzhiyun 	 * These two variables are requirements for fit_image_load, but
1358*4882a593Smuzhiyun 	 * their values are not used
1359*4882a593Smuzhiyun 	 */
1360*4882a593Smuzhiyun 	ulong img_data, img_len;
1361*4882a593Smuzhiyun 	void *buf;
1362*4882a593Smuzhiyun 	int loadables_index;
1363*4882a593Smuzhiyun 	int conf_noffset;
1364*4882a593Smuzhiyun 	int fit_img_result;
1365*4882a593Smuzhiyun 	const char *uname;
1366*4882a593Smuzhiyun 	uint8_t img_type;
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun 	/* Check to see if the images struct has a FIT configuration */
1369*4882a593Smuzhiyun 	if (!genimg_has_config(images)) {
1370*4882a593Smuzhiyun 		debug("## FIT configuration was not specified\n");
1371*4882a593Smuzhiyun 		return 0;
1372*4882a593Smuzhiyun 	}
1373*4882a593Smuzhiyun 
1374*4882a593Smuzhiyun 	/*
1375*4882a593Smuzhiyun 	 * Obtain the os FIT header from the images struct
1376*4882a593Smuzhiyun 	 */
1377*4882a593Smuzhiyun 	tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
1378*4882a593Smuzhiyun 	buf = map_sysmem(tmp_img_addr, 0);
1379*4882a593Smuzhiyun 	/*
1380*4882a593Smuzhiyun 	 * Check image type. For FIT images get FIT node
1381*4882a593Smuzhiyun 	 * and attempt to locate a generic binary.
1382*4882a593Smuzhiyun 	 */
1383*4882a593Smuzhiyun 	switch (genimg_get_format(buf)) {
1384*4882a593Smuzhiyun 	case IMAGE_FORMAT_FIT:
1385*4882a593Smuzhiyun 		conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 		for (loadables_index = 0;
1388*4882a593Smuzhiyun 		     uname = fdt_stringlist_get(buf, conf_noffset,
1389*4882a593Smuzhiyun 					FIT_LOADABLE_PROP, loadables_index,
1390*4882a593Smuzhiyun 					NULL), uname;
1391*4882a593Smuzhiyun 		     loadables_index++)
1392*4882a593Smuzhiyun 		{
1393*4882a593Smuzhiyun 			fit_img_result = fit_image_load(images,
1394*4882a593Smuzhiyun 				tmp_img_addr,
1395*4882a593Smuzhiyun 				&uname,
1396*4882a593Smuzhiyun 				&(images->fit_uname_cfg), arch,
1397*4882a593Smuzhiyun 				IH_TYPE_LOADABLE,
1398*4882a593Smuzhiyun 				BOOTSTAGE_ID_FIT_LOADABLE_START,
1399*4882a593Smuzhiyun 				FIT_LOAD_OPTIONAL_NON_ZERO,
1400*4882a593Smuzhiyun 				&img_data, &img_len);
1401*4882a593Smuzhiyun 			if (fit_img_result < 0) {
1402*4882a593Smuzhiyun 				/* Something went wrong! */
1403*4882a593Smuzhiyun 				return fit_img_result;
1404*4882a593Smuzhiyun 			}
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 			fit_img_result = fit_image_get_node(buf, uname);
1407*4882a593Smuzhiyun 			if (fit_img_result < 0) {
1408*4882a593Smuzhiyun 				/* Something went wrong! */
1409*4882a593Smuzhiyun 				return fit_img_result;
1410*4882a593Smuzhiyun 			}
1411*4882a593Smuzhiyun 			fit_img_result = fit_image_get_type(buf,
1412*4882a593Smuzhiyun 							    fit_img_result,
1413*4882a593Smuzhiyun 							    &img_type);
1414*4882a593Smuzhiyun 			if (fit_img_result < 0) {
1415*4882a593Smuzhiyun 				/* Something went wrong! */
1416*4882a593Smuzhiyun 				return fit_img_result;
1417*4882a593Smuzhiyun 			}
1418*4882a593Smuzhiyun 
1419*4882a593Smuzhiyun 			fit_loadable_process(img_type, img_data, img_len);
1420*4882a593Smuzhiyun 		}
1421*4882a593Smuzhiyun 		break;
1422*4882a593Smuzhiyun 	default:
1423*4882a593Smuzhiyun 		printf("The given image format is not supported (corrupt?)\n");
1424*4882a593Smuzhiyun 		return 1;
1425*4882a593Smuzhiyun 	}
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun 	return 0;
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun #endif
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1432*4882a593Smuzhiyun /**
1433*4882a593Smuzhiyun  * boot_get_cmdline - allocate and initialize kernel cmdline
1434*4882a593Smuzhiyun  * @lmb: pointer to lmb handle, will be used for memory mgmt
1435*4882a593Smuzhiyun  * @cmd_start: pointer to a ulong variable, will hold cmdline start
1436*4882a593Smuzhiyun  * @cmd_end: pointer to a ulong variable, will hold cmdline end
1437*4882a593Smuzhiyun  *
1438*4882a593Smuzhiyun  * boot_get_cmdline() allocates space for kernel command line below
1439*4882a593Smuzhiyun  * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environemnt
1440*4882a593Smuzhiyun  * variable is present its contents is copied to allocated kernel
1441*4882a593Smuzhiyun  * command line.
1442*4882a593Smuzhiyun  *
1443*4882a593Smuzhiyun  * returns:
1444*4882a593Smuzhiyun  *      0 - success
1445*4882a593Smuzhiyun  *     -1 - failure
1446*4882a593Smuzhiyun  */
boot_get_cmdline(struct lmb * lmb,ulong * cmd_start,ulong * cmd_end)1447*4882a593Smuzhiyun int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1448*4882a593Smuzhiyun {
1449*4882a593Smuzhiyun 	char *cmdline;
1450*4882a593Smuzhiyun 	char *s;
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun 	cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1453*4882a593Smuzhiyun 				env_get_bootm_mapsize() + env_get_bootm_low());
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 	if (cmdline == NULL)
1456*4882a593Smuzhiyun 		return -1;
1457*4882a593Smuzhiyun 
1458*4882a593Smuzhiyun 	s = env_get("bootargs");
1459*4882a593Smuzhiyun 	if (!s)
1460*4882a593Smuzhiyun 		s = "";
1461*4882a593Smuzhiyun 
1462*4882a593Smuzhiyun 	strcpy(cmdline, s);
1463*4882a593Smuzhiyun 
1464*4882a593Smuzhiyun 	*cmd_start = (ulong) & cmdline[0];
1465*4882a593Smuzhiyun 	*cmd_end = *cmd_start + strlen(cmdline);
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun 	debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1468*4882a593Smuzhiyun 
1469*4882a593Smuzhiyun 	return 0;
1470*4882a593Smuzhiyun }
1471*4882a593Smuzhiyun #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1472*4882a593Smuzhiyun 
1473*4882a593Smuzhiyun #ifdef CONFIG_SYS_BOOT_GET_KBD
1474*4882a593Smuzhiyun /**
1475*4882a593Smuzhiyun  * boot_get_kbd - allocate and initialize kernel copy of board info
1476*4882a593Smuzhiyun  * @lmb: pointer to lmb handle, will be used for memory mgmt
1477*4882a593Smuzhiyun  * @kbd: double pointer to board info data
1478*4882a593Smuzhiyun  *
1479*4882a593Smuzhiyun  * boot_get_kbd() allocates space for kernel copy of board info data below
1480*4882a593Smuzhiyun  * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
1481*4882a593Smuzhiyun  * with the current u-boot board info data.
1482*4882a593Smuzhiyun  *
1483*4882a593Smuzhiyun  * returns:
1484*4882a593Smuzhiyun  *      0 - success
1485*4882a593Smuzhiyun  *     -1 - failure
1486*4882a593Smuzhiyun  */
boot_get_kbd(struct lmb * lmb,bd_t ** kbd)1487*4882a593Smuzhiyun int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1488*4882a593Smuzhiyun {
1489*4882a593Smuzhiyun 	*kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1490*4882a593Smuzhiyun 				env_get_bootm_mapsize() + env_get_bootm_low());
1491*4882a593Smuzhiyun 	if (*kbd == NULL)
1492*4882a593Smuzhiyun 		return -1;
1493*4882a593Smuzhiyun 
1494*4882a593Smuzhiyun 	**kbd = *(gd->bd);
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1497*4882a593Smuzhiyun 
1498*4882a593Smuzhiyun #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1499*4882a593Smuzhiyun 	do_bdinfo(NULL, 0, 0, NULL);
1500*4882a593Smuzhiyun #endif
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun 	return 0;
1503*4882a593Smuzhiyun }
1504*4882a593Smuzhiyun #endif /* CONFIG_SYS_BOOT_GET_KBD */
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun #ifdef CONFIG_LMB
image_setup_linux(bootm_headers_t * images)1507*4882a593Smuzhiyun int image_setup_linux(bootm_headers_t *images)
1508*4882a593Smuzhiyun {
1509*4882a593Smuzhiyun 	ulong of_size = images->ft_len;
1510*4882a593Smuzhiyun 	char **of_flat_tree = &images->ft_addr;
1511*4882a593Smuzhiyun 	struct lmb *lmb = &images->lmb;
1512*4882a593Smuzhiyun 	int ret;
1513*4882a593Smuzhiyun 
1514*4882a593Smuzhiyun 	if (IMAGE_ENABLE_OF_LIBFDT)
1515*4882a593Smuzhiyun 		boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 	if (IMAGE_BOOT_GET_CMDLINE) {
1518*4882a593Smuzhiyun 		ret = boot_get_cmdline(lmb, &images->cmdline_start,
1519*4882a593Smuzhiyun 				&images->cmdline_end);
1520*4882a593Smuzhiyun 		if (ret) {
1521*4882a593Smuzhiyun 			puts("ERROR with allocation of cmdline\n");
1522*4882a593Smuzhiyun 			return ret;
1523*4882a593Smuzhiyun 		}
1524*4882a593Smuzhiyun 	}
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun 	if (IMAGE_ENABLE_OF_LIBFDT) {
1527*4882a593Smuzhiyun 		ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1528*4882a593Smuzhiyun 		if (ret)
1529*4882a593Smuzhiyun 			return ret;
1530*4882a593Smuzhiyun 	}
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 	if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
1533*4882a593Smuzhiyun 		ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
1534*4882a593Smuzhiyun 		if (ret)
1535*4882a593Smuzhiyun 			return ret;
1536*4882a593Smuzhiyun 	}
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun 	return 0;
1539*4882a593Smuzhiyun }
1540*4882a593Smuzhiyun #endif /* CONFIG_LMB */
1541*4882a593Smuzhiyun #endif /* !USE_HOSTCC */
1542