image.c 21 KB

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