image.c 21 KB

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