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