image.c 21 KB

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