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