image.c 22 KB

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