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