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 <env.h>
11 #include <display_options.h>
12 #include <init.h>
13 #include <lmb.h>
14 #include <log.h>
15 #include <malloc.h>
16 #include <u-boot/crc.h>
17 
18 #ifdef CONFIG_SHOW_BOOT_PROGRESS
19 #include <status_led.h>
20 #endif
21 
22 #if CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)
23 #include <linux/libfdt.h>
24 #include <fdt_support.h>
25 #endif
26 
27 #include <asm/global_data.h>
28 #include <linux/errno.h>
29 #include <asm/io.h>
30 
31 DECLARE_GLOBAL_DATA_PTR;
32 
33 /* Set this if we have less than 4 MB of malloc() space */
34 #if CONFIG_SYS_MALLOC_LEN < (4096 * 1024)
35 #define CONSERVE_MEMORY		true
36 #else
37 #define CONSERVE_MEMORY		false
38 #endif
39 
40 #else /* USE_HOSTCC */
41 #include "mkimage.h"
42 #include <linux/kconfig.h>
43 #include <u-boot/md5.h>
44 #include <time.h>
45 
46 #ifndef __maybe_unused
47 # define __maybe_unused		/* unimplemented */
48 #endif
49 
50 #define CONSERVE_MEMORY		false
51 
52 #endif /* !USE_HOSTCC*/
53 
54 #include <abuf.h>
55 #include <bzlib.h>
56 #include <display_options.h>
57 #include <gzip.h>
58 #include <image.h>
59 #include <imximage.h>
60 #include <relocate.h>
61 #include <linux/lzo.h>
62 #include <linux/zstd.h>
63 #include <lzma/LzmaTypes.h>
64 #include <lzma/LzmaDec.h>
65 #include <lzma/LzmaTools.h>
66 #include <u-boot/crc.h>
67 #include <u-boot/lz4.h>
68 
69 static const table_entry_t uimage_arch[] = {
70 	{	IH_ARCH_INVALID,	"invalid",	"Invalid ARCH",	},
71 	{	IH_ARCH_ALPHA,		"alpha",	"Alpha",	},
72 	{	IH_ARCH_ARM,		"arm",		"ARM",		},
73 	{	IH_ARCH_I386,		"x86",		"Intel x86",	},
74 	{	IH_ARCH_IA64,		"ia64",		"IA64",		},
75 	{	IH_ARCH_M68K,		"m68k",		"M68K",		},
76 	{	IH_ARCH_MICROBLAZE,	"microblaze",	"MicroBlaze",	},
77 	{	IH_ARCH_MIPS,		"mips",		"MIPS",		},
78 	{	IH_ARCH_MIPS64,		"mips64",	"MIPS 64 Bit",	},
79 	{	IH_ARCH_NIOS2,		"nios2",	"NIOS II",	},
80 	{	IH_ARCH_PPC,		"powerpc",	"PowerPC",	},
81 	{	IH_ARCH_PPC,		"ppc",		"PowerPC",	},
82 	{	IH_ARCH_S390,		"s390",		"IBM S390",	},
83 	{	IH_ARCH_SH,		"sh",		"SuperH",	},
84 	{	IH_ARCH_SPARC,		"sparc",	"SPARC",	},
85 	{	IH_ARCH_SPARC64,	"sparc64",	"SPARC 64 Bit",	},
86 	{	IH_ARCH_BLACKFIN,	"blackfin",	"Blackfin",	},
87 	{	IH_ARCH_AVR32,		"avr32",	"AVR32",	},
88 	{	IH_ARCH_NDS32,		"nds32",	"NDS32",	},
89 	{	IH_ARCH_OPENRISC,	"or1k",		"OpenRISC 1000",},
90 	{	IH_ARCH_SANDBOX,	"sandbox",	"Sandbox",	},
91 	{	IH_ARCH_ARM64,		"arm64",	"AArch64",	},
92 	{	IH_ARCH_ARC,		"arc",		"ARC",		},
93 	{	IH_ARCH_X86_64,		"x86_64",	"AMD x86_64",	},
94 	{	IH_ARCH_XTENSA,		"xtensa",	"Xtensa",	},
95 	{	IH_ARCH_RISCV,		"riscv",	"RISC-V",	},
96 	{	-1,			"",		"",		},
97 };
98 
99 static const table_entry_t uimage_os[] = {
100 	{	IH_OS_INVALID,	"invalid",	"Invalid OS",		},
101 	{       IH_OS_ARM_TRUSTED_FIRMWARE, "arm-trusted-firmware", "ARM Trusted Firmware"  },
102 	{	IH_OS_LINUX,	"linux",	"Linux",		},
103 	{	IH_OS_NETBSD,	"netbsd",	"NetBSD",		},
104 	{	IH_OS_OSE,	"ose",		"Enea OSE",		},
105 	{	IH_OS_PLAN9,	"plan9",	"Plan 9",		},
106 	{	IH_OS_RTEMS,	"rtems",	"RTEMS",		},
107 	{	IH_OS_TEE,	"tee",		"Trusted Execution Environment" },
108 	{	IH_OS_U_BOOT,	"u-boot",	"U-Boot",		},
109 	{	IH_OS_VXWORKS,	"vxworks",	"VxWorks",		},
110 #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
111 	{	IH_OS_QNX,	"qnx",		"QNX",			},
112 #endif
113 #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
114 	{	IH_OS_INTEGRITY,"integrity",	"INTEGRITY",		},
115 #endif
116 #ifdef USE_HOSTCC
117 	{	IH_OS_4_4BSD,	"4_4bsd",	"4_4BSD",		},
118 	{	IH_OS_DELL,	"dell",		"Dell",			},
119 	{	IH_OS_ESIX,	"esix",		"Esix",			},
120 	{	IH_OS_FREEBSD,	"freebsd",	"FreeBSD",		},
121 	{	IH_OS_IRIX,	"irix",		"Irix",			},
122 	{	IH_OS_NCR,	"ncr",		"NCR",			},
123 	{	IH_OS_OPENBSD,	"openbsd",	"OpenBSD",		},
124 	{	IH_OS_PSOS,	"psos",		"pSOS",			},
125 	{	IH_OS_SCO,	"sco",		"SCO",			},
126 	{	IH_OS_SOLARIS,	"solaris",	"Solaris",		},
127 	{	IH_OS_SVR4,	"svr4",		"SVR4",			},
128 #endif
129 #if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
130 	{	IH_OS_OPENRTOS,	"openrtos",	"OpenRTOS",		},
131 #endif
132 	{	IH_OS_OPENSBI,	"opensbi",	"RISC-V OpenSBI",	},
133 	{	IH_OS_EFI,	"efi",		"EFI Firmware"		},
134 #ifdef CONFIG_BOOTM_ELF
135 	{	IH_OS_ELF,	"elf",		"ELF Image"		},
136 #endif
137 
138 	{	-1,		"",		"",			},
139 };
140 
141 static const table_entry_t uimage_type[] = {
142 	{	IH_TYPE_AISIMAGE,   "aisimage",   "Davinci AIS image",},
143 	{	IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image",	},
144 	{	IH_TYPE_FIRMWARE,   "firmware",	  "Firmware",		},
145 	{	IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",	},
146 	{	IH_TYPE_GPIMAGE,    "gpimage",    "TI Keystone SPL Image",},
147 	{	IH_TYPE_KERNEL,	    "kernel",	  "Kernel Image",	},
148 	{	IH_TYPE_KERNEL_NOLOAD, "kernel_noload",  "Kernel Image (no loading done)", },
149 	{	IH_TYPE_KWBIMAGE,   "kwbimage",   "Kirkwood Boot Image",},
150 	{	IH_TYPE_IMXIMAGE,   "imximage",   "Freescale i.MX Boot Image",},
151 	{	IH_TYPE_IMX8IMAGE,  "imx8image",  "NXP i.MX8 Boot Image",},
152 	{	IH_TYPE_IMX8MIMAGE, "imx8mimage", "NXP i.MX8M Boot Image",},
153 	{	IH_TYPE_INVALID,    "invalid",	  "Invalid Image",	},
154 	{	IH_TYPE_MULTI,	    "multi",	  "Multi-File Image",	},
155 	{	IH_TYPE_OMAPIMAGE,  "omapimage",  "TI OMAP SPL With GP CH",},
156 	{	IH_TYPE_PBLIMAGE,   "pblimage",   "Freescale PBL Boot Image",},
157 	{	IH_TYPE_RAMDISK,    "ramdisk",	  "RAMDisk Image",	},
158 	{	IH_TYPE_SCRIPT,     "script",	  "Script",		},
159 	{	IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SoCFPGA CV/AV preloader",},
160 	{	IH_TYPE_SOCFPGAIMAGE_V1, "socfpgaimage_v1", "Altera SoCFPGA A10 preloader",},
161 	{	IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
162 	{	IH_TYPE_UBLIMAGE,   "ublimage",   "Davinci UBL image",},
163 	{	IH_TYPE_MXSIMAGE,   "mxsimage",   "Freescale MXS Boot Image",},
164 	{	IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
165 	{	IH_TYPE_X86_SETUP,  "x86_setup",  "x86 setup.bin",    },
166 	{	IH_TYPE_LPC32XXIMAGE, "lpc32xximage",  "LPC32XX Boot Image", },
167 	{	IH_TYPE_RKIMAGE,    "rkimage",    "Rockchip Boot Image" },
168 	{	IH_TYPE_RKSD,       "rksd",       "Rockchip SD Boot Image" },
169 	{	IH_TYPE_RKSPI,      "rkspi",      "Rockchip SPI Boot Image" },
170 	{	IH_TYPE_VYBRIDIMAGE, "vybridimage",  "Vybrid Boot Image", },
171 	{	IH_TYPE_ZYNQIMAGE,  "zynqimage",  "Xilinx Zynq Boot Image" },
172 	{	IH_TYPE_ZYNQMPIMAGE, "zynqmpimage", "Xilinx ZynqMP Boot Image" },
173 	{	IH_TYPE_ZYNQMPBIF,  "zynqmpbif",  "Xilinx ZynqMP Boot Image (bif)" },
174 	{	IH_TYPE_FPGA,       "fpga",       "FPGA Image" },
175 	{       IH_TYPE_TEE,        "tee",        "Trusted Execution Environment Image",},
176 	{	IH_TYPE_FIRMWARE_IVT, "firmware_ivt", "Firmware with HABv4 IVT" },
177 	{       IH_TYPE_PMMC,        "pmmc",        "TI Power Management Micro-Controller Firmware",},
178 	{	IH_TYPE_STM32IMAGE, "stm32image", "STMicroelectronics STM32 Image" },
179 	{	IH_TYPE_MTKIMAGE,   "mtk_image",   "MediaTek BootROM loadable Image" },
180 	{	IH_TYPE_COPRO, "copro", "Coprocessor Image"},
181 	{	IH_TYPE_SUNXI_EGON, "sunxi_egon",  "Allwinner eGON Boot Image" },
182 	{	IH_TYPE_SUNXI_TOC0, "sunxi_toc0",  "Allwinner TOC0 Boot Image" },
183 	{	IH_TYPE_FDT_LEGACY, "fdt_legacy", "legacy Image with Flat Device Tree ", },
184 	{	IH_TYPE_RENESAS_SPKG, "spkgimage", "Renesas SPKG Image" },
185 	{	IH_TYPE_STARFIVE_SPL, "sfspl", "StarFive SPL Image" },
186 	{	IH_TYPE_TFA_BL31, "tfa-bl31",  "TFA BL31 Image", },
187 	{	IH_TYPE_STM32IMAGE_V2, "stm32imagev2", "STMicroelectronics STM32 Image V2.0" },
188 	{	-1,		    "",		  "",			},
189 };
190 
191 static const table_entry_t uimage_comp[] = {
192 	{	IH_COMP_NONE,	"none",		"uncompressed",		},
193 	{	IH_COMP_BZIP2,	"bzip2",	"bzip2 compressed",	},
194 	{	IH_COMP_GZIP,	"gzip",		"gzip compressed",	},
195 	{	IH_COMP_LZMA,	"lzma",		"lzma compressed",	},
196 	{	IH_COMP_LZO,	"lzo",		"lzo compressed",	},
197 	{	IH_COMP_LZ4,	"lz4",		"lz4 compressed",	},
198 	{	IH_COMP_ZSTD,	"zstd",		"zstd compressed",	},
199 	{	-1,		"",		"",			},
200 };
201 
202 static const table_entry_t uimage_phase[] = {
203 	{	IH_PHASE_NONE,	"none",		"any",		},
204 	{	IH_PHASE_U_BOOT, "u-boot",	"U-Boot phase",	},
205 	{	IH_PHASE_SPL,	"spl",		"SPL Phase",	},
206 	{	-1,		"",		"",		},
207 };
208 
209 struct table_info {
210 	const char *desc;
211 	int count;
212 	const table_entry_t *table;
213 };
214 
215 static const struct comp_magic_map image_comp[] = {
216 	{	IH_COMP_BZIP2,	"bzip2",	{0x42, 0x5a},},
217 	{	IH_COMP_GZIP,	"gzip",		{0x1f, 0x8b},},
218 	{	IH_COMP_LZMA,	"lzma",		{0x5d, 0x00},},
219 	{	IH_COMP_LZO,	"lzo",		{0x89, 0x4c},},
220 	{	IH_COMP_LZ4,    "lz4",          {0x04, 0x22},},
221 	{	IH_COMP_ZSTD,   "zstd",         {0x28, 0xb5},},
222 	{	IH_COMP_NONE,	"none",		{},	},
223 };
224 
225 static const struct table_info table_info[IH_COUNT] = {
226 	{ "architecture", IH_ARCH_COUNT, uimage_arch },
227 	{ "compression", IH_COMP_COUNT, uimage_comp },
228 	{ "operating system", IH_OS_COUNT, uimage_os },
229 	{ "image type", IH_TYPE_COUNT, uimage_type },
230 	{ "phase", IH_PHASE_COUNT, uimage_phase },
231 };
232 
233 /*****************************************************************************/
234 /* Legacy format routines */
235 /*****************************************************************************/
image_check_hcrc(const struct legacy_img_hdr * hdr)236 int image_check_hcrc(const struct legacy_img_hdr *hdr)
237 {
238 	ulong hcrc;
239 	ulong len = image_get_header_size();
240 	struct legacy_img_hdr header;
241 
242 	/* Copy header so we can blank CRC field for re-calculation */
243 	memmove(&header, (char *)hdr, image_get_header_size());
244 	image_set_hcrc(&header, 0);
245 
246 	hcrc = crc32(0, (unsigned char *)&header, len);
247 
248 	return (hcrc == image_get_hcrc(hdr));
249 }
250 
image_check_dcrc(const struct legacy_img_hdr * hdr)251 int image_check_dcrc(const struct legacy_img_hdr *hdr)
252 {
253 	ulong data = image_get_data(hdr);
254 	ulong len = image_get_data_size(hdr);
255 	ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
256 
257 	return (dcrc == image_get_dcrc(hdr));
258 }
259 
260 /**
261  * image_multi_count - get component (sub-image) count
262  * @hdr: pointer to the header of the multi component image
263  *
264  * image_multi_count() returns number of components in a multi
265  * component image.
266  *
267  * Note: no checking of the image type is done, caller must pass
268  * a valid multi component image.
269  *
270  * returns:
271  *     number of components
272  */
image_multi_count(const struct legacy_img_hdr * hdr)273 ulong image_multi_count(const struct legacy_img_hdr *hdr)
274 {
275 	ulong i, count = 0;
276 	uint32_t *size;
277 
278 	/* get start of the image payload, which in case of multi
279 	 * component images that points to a table of component sizes */
280 	size = (uint32_t *)image_get_data(hdr);
281 
282 	/* count non empty slots */
283 	for (i = 0; size[i]; ++i)
284 		count++;
285 
286 	return count;
287 }
288 
289 /**
290  * image_multi_getimg - get component data address and size
291  * @hdr: pointer to the header of the multi component image
292  * @idx: index of the requested component
293  * @data: pointer to a ulong variable, will hold component data address
294  * @len: pointer to a ulong variable, will hold component size
295  *
296  * image_multi_getimg() returns size and data address for the requested
297  * component in a multi component image.
298  *
299  * Note: no checking of the image type is done, caller must pass
300  * a valid multi component image.
301  *
302  * returns:
303  *     data address and size of the component, if idx is valid
304  *     0 in data and len, if idx is out of range
305  */
image_multi_getimg(const struct legacy_img_hdr * hdr,ulong idx,ulong * data,ulong * len)306 void image_multi_getimg(const struct legacy_img_hdr *hdr, ulong idx,
307 			ulong *data, ulong *len)
308 {
309 	int i;
310 	uint32_t *size;
311 	ulong offset, count, img_data;
312 
313 	/* get number of component */
314 	count = image_multi_count(hdr);
315 
316 	/* get start of the image payload, which in case of multi
317 	 * component images that points to a table of component sizes */
318 	size = (uint32_t *)image_get_data(hdr);
319 
320 	/* get address of the proper component data start, which means
321 	 * skipping sizes table (add 1 for last, null entry) */
322 	img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
323 
324 	if (idx < count) {
325 		*len = uimage_to_cpu(size[idx]);
326 		offset = 0;
327 
328 		/* go over all indices preceding requested component idx */
329 		for (i = 0; i < idx; i++) {
330 			/* add up i-th component size, rounding up to 4 bytes */
331 			offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
332 		}
333 
334 		/* calculate idx-th component data address */
335 		*data = img_data + offset;
336 	} else {
337 		*len = 0;
338 		*data = 0;
339 	}
340 }
341 
image_print_type(const struct legacy_img_hdr * hdr)342 static void image_print_type(const struct legacy_img_hdr *hdr)
343 {
344 	const char __maybe_unused *os, *arch, *type, *comp;
345 
346 	os = genimg_get_os_name(image_get_os(hdr));
347 	arch = genimg_get_arch_name(image_get_arch(hdr));
348 	type = genimg_get_type_name(image_get_type(hdr));
349 	comp = genimg_get_comp_name(image_get_comp(hdr));
350 
351 	printf("%s %s %s (%s)\n", arch, os, type, comp);
352 }
353 
354 /**
355  * image_print_contents - prints out the contents of the legacy format image
356  * @ptr: pointer to the legacy format image header
357  * @p: pointer to prefix string
358  *
359  * image_print_contents() formats a multi line legacy image contents description.
360  * The routine prints out all header fields followed by the size/offset data
361  * for MULTI/SCRIPT images.
362  *
363  * returns:
364  *     no returned results
365  */
image_print_contents(const void * ptr)366 void image_print_contents(const void *ptr)
367 {
368 	const struct legacy_img_hdr *hdr = (const struct legacy_img_hdr *)ptr;
369 	const char __maybe_unused *p;
370 
371 	p = IMAGE_INDENT_STRING;
372 	printf("%sImage Name:   %.*s\n", p, IH_NMLEN, image_get_name(hdr));
373 	if (IMAGE_ENABLE_TIMESTAMP) {
374 		printf("%sCreated:      ", p);
375 		genimg_print_time((time_t)image_get_time(hdr));
376 	}
377 	printf("%sImage Type:   ", p);
378 	image_print_type(hdr);
379 	printf("%sData Size:    ", p);
380 	genimg_print_size(image_get_data_size(hdr));
381 	printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
382 	printf("%sEntry Point:  %08x\n", p, image_get_ep(hdr));
383 
384 	if (image_check_type(hdr, IH_TYPE_MULTI) ||
385 			image_check_type(hdr, IH_TYPE_SCRIPT)) {
386 		int i;
387 		ulong data, len;
388 		ulong count = image_multi_count(hdr);
389 
390 		printf("%sContents:\n", p);
391 		for (i = 0; i < count; i++) {
392 			image_multi_getimg(hdr, i, &data, &len);
393 
394 			printf("%s   Image %d: ", p, i);
395 			genimg_print_size(len);
396 
397 			if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
398 				/*
399 				 * the user may need to know offsets
400 				 * if planning to do something with
401 				 * multiple files
402 				 */
403 				printf("%s    Offset = 0x%08lx\n", p, data);
404 			}
405 		}
406 	} else if (image_check_type(hdr, IH_TYPE_FIRMWARE_IVT)) {
407 		printf("HAB Blocks:   0x%08x   0x0000   0x%08x\n",
408 			image_get_load(hdr) - image_get_header_size(),
409 			(int)(image_get_size(hdr) + image_get_header_size()
410 			+ sizeof(flash_header_v2_t) - 0x2060));
411 	}
412 }
413 
414 /**
415  * print_decomp_msg() - Print a suitable decompression/loading message
416  *
417  * @type:	OS type (IH_OS_...)
418  * @comp_type:	Compression type being used (IH_COMP_...)
419  * @is_xip:	true if the load address matches the image start
420  * @load:	Load address for printing
421  */
print_decomp_msg(int comp_type,int type,bool is_xip,ulong load)422 static void print_decomp_msg(int comp_type, int type, bool is_xip,
423 			     ulong load)
424 {
425 	const char *name = genimg_get_type_name(type);
426 
427 	/* Shows "Loading Kernel Image" for example */
428 	if (comp_type == IH_COMP_NONE)
429 		printf("   %s %s", is_xip ? "XIP" : "Loading", name);
430 	else
431 		printf("   Uncompressing %s", name);
432 
433 	printf(" to %lx\n", load);
434 }
435 
image_decomp_type(const unsigned char * buf,ulong len)436 int image_decomp_type(const unsigned char *buf, ulong len)
437 {
438 	const struct comp_magic_map *cmagic = image_comp;
439 
440 	if (len < 2)
441 		return -EINVAL;
442 
443 	for (; cmagic->comp_id > 0; cmagic++) {
444 		if (!memcmp(buf, cmagic->magic, 2))
445 			break;
446 	}
447 
448 	return cmagic->comp_id;
449 }
450 
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)451 int image_decomp(int comp, ulong load, ulong image_start, int type,
452 		 void *load_buf, void *image_buf, ulong image_len,
453 		 uint unc_len, ulong *load_end)
454 {
455 	int ret = -ENOSYS;
456 
457 	*load_end = load;
458 	print_decomp_msg(comp, type, load == image_start, load);
459 
460 	/*
461 	 * Load the image to the right place, decompressing if needed. After
462 	 * this, image_len will be set to the number of uncompressed bytes
463 	 * loaded, ret will be non-zero on error.
464 	 */
465 	switch (comp) {
466 	case IH_COMP_NONE:
467 		ret = 0;
468 		if (load == image_start)
469 			break;
470 		if (image_len <= unc_len)
471 			memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
472 		else
473 			ret = -ENOSPC;
474 		break;
475 	case IH_COMP_GZIP:
476 		if (!tools_build() && CONFIG_IS_ENABLED(GZIP))
477 			ret = gunzip(load_buf, unc_len, image_buf, &image_len);
478 		break;
479 	case IH_COMP_BZIP2:
480 		if (!tools_build() && CONFIG_IS_ENABLED(BZIP2)) {
481 			uint size = unc_len;
482 
483 			/*
484 			 * If we've got less than 4 MB of malloc() space,
485 			 * use slower decompression algorithm which requires
486 			 * at most 2300 KB of memory.
487 			 */
488 			ret = BZ2_bzBuffToBuffDecompress(load_buf, &size,
489 				image_buf, image_len, CONSERVE_MEMORY, 0);
490 			image_len = size;
491 		}
492 		break;
493 	case IH_COMP_LZMA:
494 		if (!tools_build() && CONFIG_IS_ENABLED(LZMA)) {
495 			SizeT lzma_len = unc_len;
496 
497 			ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
498 						       image_buf, image_len);
499 			image_len = lzma_len;
500 		}
501 		break;
502 	case IH_COMP_LZO:
503 		if (!tools_build() && CONFIG_IS_ENABLED(LZO)) {
504 			size_t size = unc_len;
505 
506 			ret = lzop_decompress(image_buf, image_len, load_buf, &size);
507 			image_len = size;
508 		}
509 		break;
510 	case IH_COMP_LZ4:
511 		if (!tools_build() && CONFIG_IS_ENABLED(LZ4)) {
512 			size_t size = unc_len;
513 
514 			ret = ulz4fn(image_buf, image_len, load_buf, &size);
515 			image_len = size;
516 		}
517 		break;
518 	case IH_COMP_ZSTD:
519 		if (!tools_build() && CONFIG_IS_ENABLED(ZSTD)) {
520 			struct abuf in, out;
521 
522 			abuf_init_set(&in, image_buf, image_len);
523 			abuf_init_set(&out, load_buf, unc_len);
524 			ret = zstd_decompress(&in, &out);
525 			if (ret >= 0) {
526 				image_len = ret;
527 				ret = 0;
528 			}
529 		}
530 		break;
531 	}
532 	if (ret == -ENOSYS) {
533 		printf("Unimplemented compression type %d\n", comp);
534 		return ret;
535 	}
536 
537 	*load_end = load + image_len;
538 	if (ret)
539 		return ret;
540 
541 	return 0;
542 }
543 
get_table_entry(const table_entry_t * table,int id)544 const table_entry_t *get_table_entry(const table_entry_t *table, int id)
545 {
546 	for (; table->id >= 0; ++table) {
547 		if (table->id == id)
548 			return table;
549 	}
550 	return NULL;
551 }
552 
unknown_msg(enum ih_category category)553 static const char *unknown_msg(enum ih_category category)
554 {
555 	static const char unknown_str[] = "Unknown ";
556 	static char msg[30];
557 
558 	strcpy(msg, unknown_str);
559 	strncat(msg, table_info[category].desc,
560 		sizeof(msg) - sizeof(unknown_str));
561 
562 	return msg;
563 }
564 
565 /**
566  * genimg_get_cat_name - translate entry id to long name
567  * @category: category to look up (enum ih_category)
568  * @id: entry id to be translated
569  *
570  * This will scan the translation table trying to find the entry that matches
571  * the given id.
572  *
573  * Return: long entry name if translation succeeds; error string on failure
574  */
genimg_get_cat_name(enum ih_category category,uint id)575 const char *genimg_get_cat_name(enum ih_category category, uint id)
576 {
577 	const table_entry_t *entry;
578 
579 	entry = get_table_entry(table_info[category].table, id);
580 	if (!entry)
581 		return unknown_msg(category);
582 	return entry->lname;
583 }
584 
585 /**
586  * genimg_get_cat_short_name - translate entry id to short name
587  * @category: category to look up (enum ih_category)
588  * @id: entry id to be translated
589  *
590  * This will scan the translation table trying to find the entry that matches
591  * the given id.
592  *
593  * Return: short entry name if translation succeeds; error string on failure
594  */
genimg_get_cat_short_name(enum ih_category category,uint id)595 const char *genimg_get_cat_short_name(enum ih_category category, uint id)
596 {
597 	const table_entry_t *entry;
598 
599 	entry = get_table_entry(table_info[category].table, id);
600 	if (!entry)
601 		return unknown_msg(category);
602 	return entry->sname;
603 }
604 
genimg_get_cat_count(enum ih_category category)605 int genimg_get_cat_count(enum ih_category category)
606 {
607 	return table_info[category].count;
608 }
609 
genimg_get_cat_desc(enum ih_category category)610 const char *genimg_get_cat_desc(enum ih_category category)
611 {
612 	return table_info[category].desc;
613 }
614 
615 /**
616  * genimg_cat_has_id - check whether category has entry id
617  * @category: category to look up (enum ih_category)
618  * @id: entry id to be checked
619  *
620  * This will scan the translation table trying to find the entry that matches
621  * the given id.
622  *
623  * Return: true if category has entry id; false if not
624  */
genimg_cat_has_id(enum ih_category category,uint id)625 bool genimg_cat_has_id(enum ih_category category, uint id)
626 {
627 	if (get_table_entry(table_info[category].table, id))
628 		return true;
629 
630 	return false;
631 }
632 
633 /**
634  * get_table_entry_name - translate entry id to long name
635  * @table: pointer to a translation table for entries of a specific type
636  * @msg: message to be returned when translation fails
637  * @id: entry id to be translated
638  *
639  * get_table_entry_name() will go over translation table trying to find
640  * entry that matches given id. If matching entry is found, its long
641  * name is returned to the caller.
642  *
643  * returns:
644  *     long entry name if translation succeeds
645  *     msg otherwise
646  */
get_table_entry_name(const table_entry_t * table,char * msg,int id)647 char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
648 {
649 	table = get_table_entry(table, id);
650 	if (!table)
651 		return msg;
652 	return table->lname;
653 }
654 
genimg_get_os_name(uint8_t os)655 const char *genimg_get_os_name(uint8_t os)
656 {
657 	return (get_table_entry_name(uimage_os, "Unknown OS", os));
658 }
659 
genimg_get_arch_name(uint8_t arch)660 const char *genimg_get_arch_name(uint8_t arch)
661 {
662 	return (get_table_entry_name(uimage_arch, "Unknown Architecture",
663 					arch));
664 }
665 
genimg_get_type_name(uint8_t type)666 const char *genimg_get_type_name(uint8_t type)
667 {
668 	return (get_table_entry_name(uimage_type, "Unknown Image", type));
669 }
670 
genimg_get_comp_name(uint8_t comp)671 const char *genimg_get_comp_name(uint8_t comp)
672 {
673 	return (get_table_entry_name(uimage_comp, "Unknown Compression",
674 					comp));
675 }
676 
genimg_get_phase_name(enum image_phase_t phase)677 const char *genimg_get_phase_name(enum image_phase_t phase)
678 {
679 	return get_table_entry_name(uimage_phase, "Unknown Phase", phase);
680 }
681 
genimg_get_short_name(const table_entry_t * table,int val)682 static const char *genimg_get_short_name(const table_entry_t *table, int val)
683 {
684 	table = get_table_entry(table, val);
685 	if (!table)
686 		return "unknown";
687 	return table->sname;
688 }
689 
genimg_get_type_short_name(uint8_t type)690 const char *genimg_get_type_short_name(uint8_t type)
691 {
692 	return genimg_get_short_name(uimage_type, type);
693 }
694 
genimg_get_comp_short_name(uint8_t comp)695 const char *genimg_get_comp_short_name(uint8_t comp)
696 {
697 	return genimg_get_short_name(uimage_comp, comp);
698 }
699 
genimg_get_os_short_name(uint8_t os)700 const char *genimg_get_os_short_name(uint8_t os)
701 {
702 	return genimg_get_short_name(uimage_os, os);
703 }
704 
genimg_get_arch_short_name(uint8_t arch)705 const char *genimg_get_arch_short_name(uint8_t arch)
706 {
707 	return genimg_get_short_name(uimage_arch, arch);
708 }
709 
710 /**
711  * get_table_entry_id - translate short entry name to id
712  * @table: pointer to a translation table for entries of a specific type
713  * @table_name: to be used in case of error
714  * @name: entry short name to be translated
715  *
716  * get_table_entry_id() will go over translation table trying to find
717  * entry that matches given short name. If matching entry is found,
718  * its id returned to the caller.
719  *
720  * returns:
721  *     entry id if translation succeeds
722  *     -1 otherwise
723  */
get_table_entry_id(const table_entry_t * table,const char * table_name,const char * name)724 int get_table_entry_id(const table_entry_t *table,
725 		const char *table_name, const char *name)
726 {
727 	const table_entry_t *t;
728 
729 	for (t = table; t->id >= 0; ++t) {
730 		if (t->sname && !strcasecmp(t->sname, name))
731 			return t->id;
732 	}
733 	debug("Invalid %s Type: %s\n", table_name, name);
734 
735 	return -1;
736 }
737 
genimg_get_os_id(const char * name)738 int genimg_get_os_id(const char *name)
739 {
740 	return (get_table_entry_id(uimage_os, "OS", name));
741 }
742 
genimg_get_arch_id(const char * name)743 int genimg_get_arch_id(const char *name)
744 {
745 	return (get_table_entry_id(uimage_arch, "CPU", name));
746 }
747 
genimg_get_type_id(const char * name)748 int genimg_get_type_id(const char *name)
749 {
750 	return (get_table_entry_id(uimage_type, "Image", name));
751 }
752 
genimg_get_comp_id(const char * name)753 int genimg_get_comp_id(const char *name)
754 {
755 	return (get_table_entry_id(uimage_comp, "Compression", name));
756 }
757 
genimg_get_phase_id(const char * name)758 int genimg_get_phase_id(const char *name)
759 {
760 	return get_table_entry_id(uimage_phase, "Phase", name);
761 }
762