image: Drop IMAGE_ENABLE_SHA1
[platform/kernel/u-boot.git] / include / image.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2008 Semihalf
4  *
5  * (C) Copyright 2000-2005
6  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7  ********************************************************************
8  * NOTE: This header file defines an interface to U-Boot. Including
9  * this (unmodified) header file in another file is considered normal
10  * use of U-Boot, and does *not* fall under the heading of "derived
11  * work".
12  ********************************************************************
13  */
14
15 #ifndef __IMAGE_H__
16 #define __IMAGE_H__
17
18 #include "compiler.h"
19 #include <asm/byteorder.h>
20 #include <stdbool.h>
21
22 /* Define this to avoid #ifdefs later on */
23 struct lmb;
24 struct fdt_region;
25
26 #ifdef USE_HOSTCC
27 #include <sys/types.h>
28
29 /* new uImage format support enabled on host */
30 #define IMAGE_ENABLE_FIT        1
31 #define IMAGE_ENABLE_OF_LIBFDT  1
32 #define CONFIG_FIT_VERBOSE      1 /* enable fit_format_{error,warning}() */
33 #define CONFIG_FIT_RSASSA_PSS 1
34 #define CONFIG_FIT_SHA256
35 #define CONFIG_FIT_SHA384
36 #define CONFIG_FIT_SHA512
37 #define CONFIG_SHA1
38 #define CONFIG_SHA256
39 #define CONFIG_SHA384
40 #define CONFIG_SHA512
41
42 #define IMAGE_ENABLE_IGNORE     0
43 #define IMAGE_INDENT_STRING     ""
44
45 #else
46
47 #include <lmb.h>
48 #include <asm/u-boot.h>
49 #include <command.h>
50
51 /* Take notice of the 'ignore' property for hashes */
52 #define IMAGE_ENABLE_IGNORE     1
53 #define IMAGE_INDENT_STRING     "   "
54
55 #define IMAGE_ENABLE_FIT        CONFIG_IS_ENABLED(FIT)
56 #define IMAGE_ENABLE_OF_LIBFDT  CONFIG_IS_ENABLED(OF_LIBFDT)
57
58 #endif /* USE_HOSTCC */
59
60 #if IMAGE_ENABLE_FIT
61 #include <hash.h>
62 #include <linux/libfdt.h>
63 #include <fdt_support.h>
64 # ifdef CONFIG_SPL_BUILD
65 #  ifdef CONFIG_SPL_CRC32
66 #   define IMAGE_ENABLE_CRC32   1
67 #  endif
68 #  ifdef CONFIG_SPL_MD5
69 #   define IMAGE_ENABLE_MD5     1
70 #  endif
71 # else
72 #  define IMAGE_ENABLE_CRC32    1
73 #  define IMAGE_ENABLE_MD5      1
74 # endif
75
76 #ifndef IMAGE_ENABLE_CRC32
77 #define IMAGE_ENABLE_CRC32      0
78 #endif
79
80 #ifndef IMAGE_ENABLE_MD5
81 #define IMAGE_ENABLE_MD5        0
82 #endif
83
84 #if defined(CONFIG_FIT_SHA256) || \
85         defined(CONFIG_SPL_FIT_SHA256)
86 #define IMAGE_ENABLE_SHA256     1
87 #else
88 #define IMAGE_ENABLE_SHA256     0
89 #endif
90
91 #if defined(CONFIG_FIT_SHA384) || \
92         defined(CONFIG_SPL_FIT_SHA384)
93 #define IMAGE_ENABLE_SHA384     1
94 #else
95 #define IMAGE_ENABLE_SHA384     0
96 #endif
97
98 #if defined(CONFIG_FIT_SHA512) || \
99         defined(CONFIG_SPL_FIT_SHA512)
100 #define IMAGE_ENABLE_SHA512     1
101 #else
102 #define IMAGE_ENABLE_SHA512     0
103 #endif
104
105 #endif /* IMAGE_ENABLE_FIT */
106
107 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
108 # define IMAGE_BOOT_GET_CMDLINE         1
109 #else
110 # define IMAGE_BOOT_GET_CMDLINE         0
111 #endif
112
113 #ifdef CONFIG_OF_BOARD_SETUP
114 # define IMAGE_OF_BOARD_SETUP           1
115 #else
116 # define IMAGE_OF_BOARD_SETUP           0
117 #endif
118
119 #ifdef CONFIG_OF_SYSTEM_SETUP
120 # define IMAGE_OF_SYSTEM_SETUP  1
121 #else
122 # define IMAGE_OF_SYSTEM_SETUP  0
123 #endif
124
125 extern ulong image_load_addr;           /* Default Load Address */
126 extern ulong image_save_addr;           /* Default Save Address */
127 extern ulong image_save_size;           /* Default Save Size */
128
129 /* An invalid size, meaning that the image size is not known */
130 #define IMAGE_SIZE_INVAL        (-1UL)
131
132 enum ih_category {
133         IH_ARCH,
134         IH_COMP,
135         IH_OS,
136         IH_TYPE,
137
138         IH_COUNT,
139 };
140
141 /*
142  * Operating System Codes
143  *
144  * The following are exposed to uImage header.
145  * New IDs *MUST* be appended at the end of the list and *NEVER*
146  * inserted for backward compatibility.
147  */
148 enum {
149         IH_OS_INVALID           = 0,    /* Invalid OS   */
150         IH_OS_OPENBSD,                  /* OpenBSD      */
151         IH_OS_NETBSD,                   /* NetBSD       */
152         IH_OS_FREEBSD,                  /* FreeBSD      */
153         IH_OS_4_4BSD,                   /* 4.4BSD       */
154         IH_OS_LINUX,                    /* Linux        */
155         IH_OS_SVR4,                     /* SVR4         */
156         IH_OS_ESIX,                     /* Esix         */
157         IH_OS_SOLARIS,                  /* Solaris      */
158         IH_OS_IRIX,                     /* Irix         */
159         IH_OS_SCO,                      /* SCO          */
160         IH_OS_DELL,                     /* Dell         */
161         IH_OS_NCR,                      /* NCR          */
162         IH_OS_LYNXOS,                   /* LynxOS       */
163         IH_OS_VXWORKS,                  /* VxWorks      */
164         IH_OS_PSOS,                     /* pSOS         */
165         IH_OS_QNX,                      /* QNX          */
166         IH_OS_U_BOOT,                   /* Firmware     */
167         IH_OS_RTEMS,                    /* RTEMS        */
168         IH_OS_ARTOS,                    /* ARTOS        */
169         IH_OS_UNITY,                    /* Unity OS     */
170         IH_OS_INTEGRITY,                /* INTEGRITY    */
171         IH_OS_OSE,                      /* OSE          */
172         IH_OS_PLAN9,                    /* Plan 9       */
173         IH_OS_OPENRTOS,         /* OpenRTOS     */
174         IH_OS_ARM_TRUSTED_FIRMWARE,     /* ARM Trusted Firmware */
175         IH_OS_TEE,                      /* Trusted Execution Environment */
176         IH_OS_OPENSBI,                  /* RISC-V OpenSBI */
177         IH_OS_EFI,                      /* EFI Firmware (e.g. GRUB2) */
178
179         IH_OS_COUNT,
180 };
181
182 /*
183  * CPU Architecture Codes (supported by Linux)
184  *
185  * The following are exposed to uImage header.
186  * New IDs *MUST* be appended at the end of the list and *NEVER*
187  * inserted for backward compatibility.
188  */
189 enum {
190         IH_ARCH_INVALID         = 0,    /* Invalid CPU  */
191         IH_ARCH_ALPHA,                  /* Alpha        */
192         IH_ARCH_ARM,                    /* ARM          */
193         IH_ARCH_I386,                   /* Intel x86    */
194         IH_ARCH_IA64,                   /* IA64         */
195         IH_ARCH_MIPS,                   /* MIPS         */
196         IH_ARCH_MIPS64,                 /* MIPS  64 Bit */
197         IH_ARCH_PPC,                    /* PowerPC      */
198         IH_ARCH_S390,                   /* IBM S390     */
199         IH_ARCH_SH,                     /* SuperH       */
200         IH_ARCH_SPARC,                  /* Sparc        */
201         IH_ARCH_SPARC64,                /* Sparc 64 Bit */
202         IH_ARCH_M68K,                   /* M68K         */
203         IH_ARCH_NIOS,                   /* Nios-32      */
204         IH_ARCH_MICROBLAZE,             /* MicroBlaze   */
205         IH_ARCH_NIOS2,                  /* Nios-II      */
206         IH_ARCH_BLACKFIN,               /* Blackfin     */
207         IH_ARCH_AVR32,                  /* AVR32        */
208         IH_ARCH_ST200,                  /* STMicroelectronics ST200  */
209         IH_ARCH_SANDBOX,                /* Sandbox architecture (test only) */
210         IH_ARCH_NDS32,                  /* ANDES Technology - NDS32  */
211         IH_ARCH_OPENRISC,               /* OpenRISC 1000  */
212         IH_ARCH_ARM64,                  /* ARM64        */
213         IH_ARCH_ARC,                    /* Synopsys DesignWare ARC */
214         IH_ARCH_X86_64,                 /* AMD x86_64, Intel and Via */
215         IH_ARCH_XTENSA,                 /* Xtensa       */
216         IH_ARCH_RISCV,                  /* RISC-V */
217
218         IH_ARCH_COUNT,
219 };
220
221 /*
222  * Image Types
223  *
224  * "Standalone Programs" are directly runnable in the environment
225  *      provided by U-Boot; it is expected that (if they behave
226  *      well) you can continue to work in U-Boot after return from
227  *      the Standalone Program.
228  * "OS Kernel Images" are usually images of some Embedded OS which
229  *      will take over control completely. Usually these programs
230  *      will install their own set of exception handlers, device
231  *      drivers, set up the MMU, etc. - this means, that you cannot
232  *      expect to re-enter U-Boot except by resetting the CPU.
233  * "RAMDisk Images" are more or less just data blocks, and their
234  *      parameters (address, size) are passed to an OS kernel that is
235  *      being started.
236  * "Multi-File Images" contain several images, typically an OS
237  *      (Linux) kernel image and one or more data images like
238  *      RAMDisks. This construct is useful for instance when you want
239  *      to boot over the network using BOOTP etc., where the boot
240  *      server provides just a single image file, but you want to get
241  *      for instance an OS kernel and a RAMDisk image.
242  *
243  *      "Multi-File Images" start with a list of image sizes, each
244  *      image size (in bytes) specified by an "uint32_t" in network
245  *      byte order. This list is terminated by an "(uint32_t)0".
246  *      Immediately after the terminating 0 follow the images, one by
247  *      one, all aligned on "uint32_t" boundaries (size rounded up to
248  *      a multiple of 4 bytes - except for the last file).
249  *
250  * "Firmware Images" are binary images containing firmware (like
251  *      U-Boot or FPGA images) which usually will be programmed to
252  *      flash memory.
253  *
254  * "Script files" are command sequences that will be executed by
255  *      U-Boot's command interpreter; this feature is especially
256  *      useful when you configure U-Boot to use a real shell (hush)
257  *      as command interpreter (=> Shell Scripts).
258  *
259  * The following are exposed to uImage header.
260  * New IDs *MUST* be appended at the end of the list and *NEVER*
261  * inserted for backward compatibility.
262  */
263
264 enum {
265         IH_TYPE_INVALID         = 0,    /* Invalid Image                */
266         IH_TYPE_STANDALONE,             /* Standalone Program           */
267         IH_TYPE_KERNEL,                 /* OS Kernel Image              */
268         IH_TYPE_RAMDISK,                /* RAMDisk Image                */
269         IH_TYPE_MULTI,                  /* Multi-File Image             */
270         IH_TYPE_FIRMWARE,               /* Firmware Image               */
271         IH_TYPE_SCRIPT,                 /* Script file                  */
272         IH_TYPE_FILESYSTEM,             /* Filesystem Image (any type)  */
273         IH_TYPE_FLATDT,                 /* Binary Flat Device Tree Blob */
274         IH_TYPE_KWBIMAGE,               /* Kirkwood Boot Image          */
275         IH_TYPE_IMXIMAGE,               /* Freescale IMXBoot Image      */
276         IH_TYPE_UBLIMAGE,               /* Davinci UBL Image            */
277         IH_TYPE_OMAPIMAGE,              /* TI OMAP Config Header Image  */
278         IH_TYPE_AISIMAGE,               /* TI Davinci AIS Image         */
279         /* OS Kernel Image, can run from any load address */
280         IH_TYPE_KERNEL_NOLOAD,
281         IH_TYPE_PBLIMAGE,               /* Freescale PBL Boot Image     */
282         IH_TYPE_MXSIMAGE,               /* Freescale MXSBoot Image      */
283         IH_TYPE_GPIMAGE,                /* TI Keystone GPHeader Image   */
284         IH_TYPE_ATMELIMAGE,             /* ATMEL ROM bootable Image     */
285         IH_TYPE_SOCFPGAIMAGE,           /* Altera SOCFPGA CV/AV Preloader */
286         IH_TYPE_X86_SETUP,              /* x86 setup.bin Image          */
287         IH_TYPE_LPC32XXIMAGE,           /* x86 setup.bin Image          */
288         IH_TYPE_LOADABLE,               /* A list of typeless images    */
289         IH_TYPE_RKIMAGE,                /* Rockchip Boot Image          */
290         IH_TYPE_RKSD,                   /* Rockchip SD card             */
291         IH_TYPE_RKSPI,                  /* Rockchip SPI image           */
292         IH_TYPE_ZYNQIMAGE,              /* Xilinx Zynq Boot Image */
293         IH_TYPE_ZYNQMPIMAGE,            /* Xilinx ZynqMP Boot Image */
294         IH_TYPE_ZYNQMPBIF,              /* Xilinx ZynqMP Boot Image (bif) */
295         IH_TYPE_FPGA,                   /* FPGA Image */
296         IH_TYPE_VYBRIDIMAGE,    /* VYBRID .vyb Image */
297         IH_TYPE_TEE,            /* Trusted Execution Environment OS Image */
298         IH_TYPE_FIRMWARE_IVT,           /* Firmware Image with HABv4 IVT */
299         IH_TYPE_PMMC,            /* TI Power Management Micro-Controller Firmware */
300         IH_TYPE_STM32IMAGE,             /* STMicroelectronics STM32 Image */
301         IH_TYPE_SOCFPGAIMAGE_V1,        /* Altera SOCFPGA A10 Preloader */
302         IH_TYPE_MTKIMAGE,               /* MediaTek BootROM loadable Image */
303         IH_TYPE_IMX8MIMAGE,             /* Freescale IMX8MBoot Image    */
304         IH_TYPE_IMX8IMAGE,              /* Freescale IMX8Boot Image     */
305         IH_TYPE_COPRO,                  /* Coprocessor Image for remoteproc*/
306         IH_TYPE_SUNXI_EGON,             /* Allwinner eGON Boot Image */
307
308         IH_TYPE_COUNT,                  /* Number of image types */
309 };
310
311 /*
312  * Compression Types
313  *
314  * The following are exposed to uImage header.
315  * New IDs *MUST* be appended at the end of the list and *NEVER*
316  * inserted for backward compatibility.
317  */
318 enum {
319         IH_COMP_NONE            = 0,    /*  No   Compression Used       */
320         IH_COMP_GZIP,                   /* gzip  Compression Used       */
321         IH_COMP_BZIP2,                  /* bzip2 Compression Used       */
322         IH_COMP_LZMA,                   /* lzma  Compression Used       */
323         IH_COMP_LZO,                    /* lzo   Compression Used       */
324         IH_COMP_LZ4,                    /* lz4   Compression Used       */
325         IH_COMP_ZSTD,                   /* zstd   Compression Used      */
326
327         IH_COMP_COUNT,
328 };
329
330 #define LZ4F_MAGIC      0x184D2204      /* LZ4 Magic Number             */
331 #define IH_MAGIC        0x27051956      /* Image Magic Number           */
332 #define IH_NMLEN                32      /* Image Name Length            */
333
334 /* Reused from common.h */
335 #define ROUND(a, b)             (((a) + (b) - 1) & ~((b) - 1))
336
337 /*
338  * Legacy format image header,
339  * all data in network byte order (aka natural aka bigendian).
340  */
341 typedef struct image_header {
342         uint32_t        ih_magic;       /* Image Header Magic Number    */
343         uint32_t        ih_hcrc;        /* Image Header CRC Checksum    */
344         uint32_t        ih_time;        /* Image Creation Timestamp     */
345         uint32_t        ih_size;        /* Image Data Size              */
346         uint32_t        ih_load;        /* Data  Load  Address          */
347         uint32_t        ih_ep;          /* Entry Point Address          */
348         uint32_t        ih_dcrc;        /* Image Data CRC Checksum      */
349         uint8_t         ih_os;          /* Operating System             */
350         uint8_t         ih_arch;        /* CPU architecture             */
351         uint8_t         ih_type;        /* Image Type                   */
352         uint8_t         ih_comp;        /* Compression Type             */
353         uint8_t         ih_name[IH_NMLEN];      /* Image Name           */
354 } image_header_t;
355
356 typedef struct image_info {
357         ulong           start, end;             /* start/end of blob */
358         ulong           image_start, image_len; /* start of image within blob, len of image */
359         ulong           load;                   /* load addr for the image */
360         uint8_t         comp, type, os;         /* compression, type of image, os type */
361         uint8_t         arch;                   /* CPU architecture */
362 } image_info_t;
363
364 /*
365  * Legacy and FIT format headers used by do_bootm() and do_bootm_<os>()
366  * routines.
367  */
368 typedef struct bootm_headers {
369         /*
370          * Legacy os image header, if it is a multi component image
371          * then boot_get_ramdisk() and get_fdt() will attempt to get
372          * data from second and third component accordingly.
373          */
374         image_header_t  *legacy_hdr_os;         /* image header pointer */
375         image_header_t  legacy_hdr_os_copy;     /* header copy */
376         ulong           legacy_hdr_valid;
377
378 #if IMAGE_ENABLE_FIT
379         const char      *fit_uname_cfg; /* configuration node unit name */
380
381         void            *fit_hdr_os;    /* os FIT image header */
382         const char      *fit_uname_os;  /* os subimage node unit name */
383         int             fit_noffset_os; /* os subimage node offset */
384
385         void            *fit_hdr_rd;    /* init ramdisk FIT image header */
386         const char      *fit_uname_rd;  /* init ramdisk subimage node unit name */
387         int             fit_noffset_rd; /* init ramdisk subimage node offset */
388
389         void            *fit_hdr_fdt;   /* FDT blob FIT image header */
390         const char      *fit_uname_fdt; /* FDT blob subimage node unit name */
391         int             fit_noffset_fdt;/* FDT blob subimage node offset */
392
393         void            *fit_hdr_setup; /* x86 setup FIT image header */
394         const char      *fit_uname_setup; /* x86 setup subimage node name */
395         int             fit_noffset_setup;/* x86 setup subimage node offset */
396 #endif
397
398 #ifndef USE_HOSTCC
399         image_info_t    os;             /* os image info */
400         ulong           ep;             /* entry point of OS */
401
402         ulong           rd_start, rd_end;/* ramdisk start/end */
403
404         char            *ft_addr;       /* flat dev tree address */
405         ulong           ft_len;         /* length of flat device tree */
406
407         ulong           initrd_start;
408         ulong           initrd_end;
409         ulong           cmdline_start;
410         ulong           cmdline_end;
411         struct bd_info          *kbd;
412 #endif
413
414         int             verify;         /* env_get("verify")[0] != 'n' */
415
416 #define BOOTM_STATE_START       (0x00000001)
417 #define BOOTM_STATE_FINDOS      (0x00000002)
418 #define BOOTM_STATE_FINDOTHER   (0x00000004)
419 #define BOOTM_STATE_LOADOS      (0x00000008)
420 #define BOOTM_STATE_RAMDISK     (0x00000010)
421 #define BOOTM_STATE_FDT         (0x00000020)
422 #define BOOTM_STATE_OS_CMDLINE  (0x00000040)
423 #define BOOTM_STATE_OS_BD_T     (0x00000080)
424 #define BOOTM_STATE_OS_PREP     (0x00000100)
425 #define BOOTM_STATE_OS_FAKE_GO  (0x00000200)    /* 'Almost' run the OS */
426 #define BOOTM_STATE_OS_GO       (0x00000400)
427         int             state;
428
429 #if defined(CONFIG_LMB) && !defined(USE_HOSTCC)
430         struct lmb      lmb;            /* for memory mgmt */
431 #endif
432 } bootm_headers_t;
433
434 extern bootm_headers_t images;
435
436 /*
437  * Some systems (for example LWMON) have very short watchdog periods;
438  * we must make sure to split long operations like memmove() or
439  * checksum calculations into reasonable chunks.
440  */
441 #ifndef CHUNKSZ
442 #define CHUNKSZ (64 * 1024)
443 #endif
444
445 #ifndef CHUNKSZ_CRC32
446 #define CHUNKSZ_CRC32 (64 * 1024)
447 #endif
448
449 #ifndef CHUNKSZ_MD5
450 #define CHUNKSZ_MD5 (64 * 1024)
451 #endif
452
453 #ifndef CHUNKSZ_SHA1
454 #define CHUNKSZ_SHA1 (64 * 1024)
455 #endif
456
457 #define uimage_to_cpu(x)                be32_to_cpu(x)
458 #define cpu_to_uimage(x)                cpu_to_be32(x)
459
460 /*
461  * Translation table for entries of a specific type; used by
462  * get_table_entry_id() and get_table_entry_name().
463  */
464 typedef struct table_entry {
465         int     id;
466         char    *sname;         /* short (input) name to find table entry */
467         char    *lname;         /* long (output) name to print for messages */
468 } table_entry_t;
469
470 /*
471  * Compression type and magic number mapping table.
472  */
473 struct comp_magic_map {
474         int             comp_id;
475         const char      *name;
476         unsigned char   magic[2];
477 };
478
479 /*
480  * get_table_entry_id() scans the translation table trying to find an
481  * entry that matches the given short name. If a matching entry is
482  * found, it's id is returned to the caller.
483  */
484 int get_table_entry_id(const table_entry_t *table,
485                 const char *table_name, const char *name);
486 /*
487  * get_table_entry_name() scans the translation table trying to find
488  * an entry that matches the given id. If a matching entry is found,
489  * its long name is returned to the caller.
490  */
491 char *get_table_entry_name(const table_entry_t *table, char *msg, int id);
492
493 const char *genimg_get_os_name(uint8_t os);
494
495 /**
496  * genimg_get_os_short_name() - get the short name for an OS
497  *
498  * @param os    OS (IH_OS_...)
499  * @return OS short name, or "unknown" if unknown
500  */
501 const char *genimg_get_os_short_name(uint8_t comp);
502
503 const char *genimg_get_arch_name(uint8_t arch);
504
505 /**
506  * genimg_get_arch_short_name() - get the short name for an architecture
507  *
508  * @param arch  Architecture type (IH_ARCH_...)
509  * @return architecture short name, or "unknown" if unknown
510  */
511 const char *genimg_get_arch_short_name(uint8_t arch);
512
513 const char *genimg_get_type_name(uint8_t type);
514
515 /**
516  * genimg_get_type_short_name() - get the short name for an image type
517  *
518  * @param type  Image type (IH_TYPE_...)
519  * @return image short name, or "unknown" if unknown
520  */
521 const char *genimg_get_type_short_name(uint8_t type);
522
523 const char *genimg_get_comp_name(uint8_t comp);
524
525 /**
526  * genimg_get_comp_short_name() - get the short name for a compression method
527  *
528  * @param comp  compression method (IH_COMP_...)
529  * @return compression method short name, or "unknown" if unknown
530  */
531 const char *genimg_get_comp_short_name(uint8_t comp);
532
533 /**
534  * genimg_get_cat_name() - Get the name of an item in a category
535  *
536  * @category:   Category of item
537  * @id:         Item ID
538  * @return name of item, or "Unknown ..." if unknown
539  */
540 const char *genimg_get_cat_name(enum ih_category category, uint id);
541
542 /**
543  * genimg_get_cat_short_name() - Get the short name of an item in a category
544  *
545  * @category:   Category of item
546  * @id:         Item ID
547  * @return short name of item, or "Unknown ..." if unknown
548  */
549 const char *genimg_get_cat_short_name(enum ih_category category, uint id);
550
551 /**
552  * genimg_get_cat_count() - Get the number of items in a category
553  *
554  * @category:   Category to check
555  * @return the number of items in the category (IH_xxx_COUNT)
556  */
557 int genimg_get_cat_count(enum ih_category category);
558
559 /**
560  * genimg_get_cat_desc() - Get the description of a category
561  *
562  * @category:   Category to check
563  * @return the description of a category, e.g. "architecture". This
564  * effectively converts the enum to a string.
565  */
566 const char *genimg_get_cat_desc(enum ih_category category);
567
568 /**
569  * genimg_cat_has_id() - Check whether a category has an item
570  *
571  * @category:   Category to check
572  * @id:         Item ID
573  * @return true or false as to whether a category has an item
574  */
575 bool genimg_cat_has_id(enum ih_category category, uint id);
576
577 int genimg_get_os_id(const char *name);
578 int genimg_get_arch_id(const char *name);
579 int genimg_get_type_id(const char *name);
580 int genimg_get_comp_id(const char *name);
581 void genimg_print_size(uint32_t size);
582
583 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || \
584         defined(USE_HOSTCC)
585 #define IMAGE_ENABLE_TIMESTAMP 1
586 #else
587 #define IMAGE_ENABLE_TIMESTAMP 0
588 #endif
589 void genimg_print_time(time_t timestamp);
590
591 /* What to do with a image load address ('load = <> 'in the FIT) */
592 enum fit_load_op {
593         FIT_LOAD_IGNORED,       /* Ignore load address */
594         FIT_LOAD_OPTIONAL,      /* Can be provided, but optional */
595         FIT_LOAD_OPTIONAL_NON_ZERO,     /* Optional, a value of 0 is ignored */
596         FIT_LOAD_REQUIRED,      /* Must be provided */
597 };
598
599 int boot_get_setup(bootm_headers_t *images, uint8_t arch, ulong *setup_start,
600                    ulong *setup_len);
601
602 #ifndef USE_HOSTCC
603 /* Image format types, returned by _get_format() routine */
604 #define IMAGE_FORMAT_INVALID    0x00
605 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
606 #define IMAGE_FORMAT_LEGACY     0x01    /* legacy image_header based format */
607 #endif
608 #define IMAGE_FORMAT_FIT        0x02    /* new, libfdt based format */
609 #define IMAGE_FORMAT_ANDROID    0x03    /* Android boot image */
610
611 ulong genimg_get_kernel_addr_fit(char * const img_addr,
612                                  const char **fit_uname_config,
613                                  const char **fit_uname_kernel);
614 ulong genimg_get_kernel_addr(char * const img_addr);
615 int genimg_get_format(const void *img_addr);
616 int genimg_has_config(bootm_headers_t *images);
617
618 int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
619                   uint8_t arch, const ulong *ld_start, ulong * const ld_len);
620 int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
621                      uint8_t arch, ulong *rd_start, ulong *rd_end);
622
623 /**
624  * boot_get_loadable - routine to load a list of binaries to memory
625  * @argc: Ignored Argument
626  * @argv: Ignored Argument
627  * @images: pointer to the bootm images structure
628  * @arch: expected architecture for the image
629  * @ld_start: Ignored Argument
630  * @ld_len: Ignored Argument
631  *
632  * boot_get_loadable() will take the given FIT configuration, and look
633  * for a field named "loadables".  Loadables, is a list of elements in
634  * the FIT given as strings.  exe:
635  *   loadables = "linux_kernel", "fdt-2";
636  * this function will attempt to parse each string, and load the
637  * corresponding element from the FIT into memory.  Once placed,
638  * no aditional actions are taken.
639  *
640  * @return:
641  *     0, if only valid images or no images are found
642  *     error code, if an error occurs during fit_image_load
643  */
644 int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
645                       uint8_t arch, const ulong *ld_start, ulong *const ld_len);
646 #endif /* !USE_HOSTCC */
647
648 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
649                        ulong *setup_start, ulong *setup_len);
650
651 /**
652  * boot_get_fdt_fit() - load a DTB from a FIT file (applying overlays)
653  *
654  * This deals with all aspects of loading an DTB from a FIT.
655  * The correct base image based on configuration will be selected, and
656  * then any overlays specified will be applied (as present in fit_uname_configp).
657  *
658  * @param images        Boot images structure
659  * @param addr          Address of FIT in memory
660  * @param fit_unamep    On entry this is the requested image name
661  *                      (e.g. "kernel") or NULL to use the default. On exit
662  *                      points to the selected image name
663  * @param fit_uname_configp     On entry this is the requested configuration
664  *                      name (e.g. "conf-1") or NULL to use the default. On
665  *                      exit points to the selected configuration name.
666  * @param arch          Expected architecture (IH_ARCH_...)
667  * @param datap         Returns address of loaded image
668  * @param lenp          Returns length of loaded image
669  *
670  * @return node offset of base image, or -ve error code on error
671  */
672 int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
673                    const char **fit_unamep, const char **fit_uname_configp,
674                    int arch, ulong *datap, ulong *lenp);
675
676 /**
677  * fit_image_load() - load an image from a FIT
678  *
679  * This deals with all aspects of loading an image from a FIT, including
680  * selecting the right image based on configuration, verifying it, printing
681  * out progress messages, checking the type/arch/os and optionally copying it
682  * to the right load address.
683  *
684  * The property to look up is defined by image_type.
685  *
686  * @param images        Boot images structure
687  * @param addr          Address of FIT in memory
688  * @param fit_unamep    On entry this is the requested image name
689  *                      (e.g. "kernel") or NULL to use the default. On exit
690  *                      points to the selected image name
691  * @param fit_uname_configp     On entry this is the requested configuration
692  *                      name (e.g. "conf-1") or NULL to use the default. On
693  *                      exit points to the selected configuration name.
694  * @param arch          Expected architecture (IH_ARCH_...)
695  * @param image_type    Required image type (IH_TYPE_...). If this is
696  *                      IH_TYPE_KERNEL then we allow IH_TYPE_KERNEL_NOLOAD
697  *                      also.
698  * @param bootstage_id  ID of starting bootstage to use for progress updates.
699  *                      This will be added to the BOOTSTAGE_SUB values when
700  *                      calling bootstage_mark()
701  * @param load_op       Decribes what to do with the load address
702  * @param datap         Returns address of loaded image
703  * @param lenp          Returns length of loaded image
704  * @return node offset of image, or -ve error code on error
705  */
706 int fit_image_load(bootm_headers_t *images, ulong addr,
707                    const char **fit_unamep, const char **fit_uname_configp,
708                    int arch, int image_type, int bootstage_id,
709                    enum fit_load_op load_op, ulong *datap, ulong *lenp);
710
711 /**
712  * image_source_script() - Execute a script
713  *
714  * Executes a U-Boot script at a particular address in memory. The script should
715  * have a header (FIT or legacy) with the script type (IH_TYPE_SCRIPT).
716  *
717  * @addr: Address of script
718  * @fit_uname: FIT subimage name
719  * @return result code (enum command_ret_t)
720  */
721 int image_source_script(ulong addr, const char *fit_uname);
722
723 #ifndef USE_HOSTCC
724 /**
725  * fit_get_node_from_config() - Look up an image a FIT by type
726  *
727  * This looks in the selected conf- node (images->fit_uname_cfg) for a
728  * particular image type (e.g. "kernel") and then finds the image that is
729  * referred to.
730  *
731  * For example, for something like:
732  *
733  * images {
734  *      kernel {
735  *              ...
736  *      };
737  * };
738  * configurations {
739  *      conf-1 {
740  *              kernel = "kernel";
741  *      };
742  * };
743  *
744  * the function will return the node offset of the kernel@1 node, assuming
745  * that conf-1 is the chosen configuration.
746  *
747  * @param images        Boot images structure
748  * @param prop_name     Property name to look up (FIT_..._PROP)
749  * @param addr          Address of FIT in memory
750  */
751 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
752                         ulong addr);
753
754 int boot_get_fdt(int flag, int argc, char *const argv[], uint8_t arch,
755                  bootm_headers_t *images,
756                  char **of_flat_tree, ulong *of_size);
757 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
758 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size);
759
760 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
761                   ulong *initrd_start, ulong *initrd_end);
762 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end);
763 #ifdef CONFIG_SYS_BOOT_GET_KBD
764 int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd);
765 #endif /* CONFIG_SYS_BOOT_GET_KBD */
766 #endif /* !USE_HOSTCC */
767
768 /*******************************************************************/
769 /* Legacy format specific code (prefixed with image_) */
770 /*******************************************************************/
771 static inline uint32_t image_get_header_size(void)
772 {
773         return (sizeof(image_header_t));
774 }
775
776 #define image_get_hdr_l(f) \
777         static inline uint32_t image_get_##f(const image_header_t *hdr) \
778         { \
779                 return uimage_to_cpu(hdr->ih_##f); \
780         }
781 image_get_hdr_l(magic)          /* image_get_magic */
782 image_get_hdr_l(hcrc)           /* image_get_hcrc */
783 image_get_hdr_l(time)           /* image_get_time */
784 image_get_hdr_l(size)           /* image_get_size */
785 image_get_hdr_l(load)           /* image_get_load */
786 image_get_hdr_l(ep)             /* image_get_ep */
787 image_get_hdr_l(dcrc)           /* image_get_dcrc */
788
789 #define image_get_hdr_b(f) \
790         static inline uint8_t image_get_##f(const image_header_t *hdr) \
791         { \
792                 return hdr->ih_##f; \
793         }
794 image_get_hdr_b(os)             /* image_get_os */
795 image_get_hdr_b(arch)           /* image_get_arch */
796 image_get_hdr_b(type)           /* image_get_type */
797 image_get_hdr_b(comp)           /* image_get_comp */
798
799 static inline char *image_get_name(const image_header_t *hdr)
800 {
801         return (char *)hdr->ih_name;
802 }
803
804 static inline uint32_t image_get_data_size(const image_header_t *hdr)
805 {
806         return image_get_size(hdr);
807 }
808
809 /**
810  * image_get_data - get image payload start address
811  * @hdr: image header
812  *
813  * image_get_data() returns address of the image payload. For single
814  * component images it is image data start. For multi component
815  * images it points to the null terminated table of sub-images sizes.
816  *
817  * returns:
818  *     image payload data start address
819  */
820 static inline ulong image_get_data(const image_header_t *hdr)
821 {
822         return ((ulong)hdr + image_get_header_size());
823 }
824
825 static inline uint32_t image_get_image_size(const image_header_t *hdr)
826 {
827         return (image_get_size(hdr) + image_get_header_size());
828 }
829 static inline ulong image_get_image_end(const image_header_t *hdr)
830 {
831         return ((ulong)hdr + image_get_image_size(hdr));
832 }
833
834 #define image_set_hdr_l(f) \
835         static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
836         { \
837                 hdr->ih_##f = cpu_to_uimage(val); \
838         }
839 image_set_hdr_l(magic)          /* image_set_magic */
840 image_set_hdr_l(hcrc)           /* image_set_hcrc */
841 image_set_hdr_l(time)           /* image_set_time */
842 image_set_hdr_l(size)           /* image_set_size */
843 image_set_hdr_l(load)           /* image_set_load */
844 image_set_hdr_l(ep)             /* image_set_ep */
845 image_set_hdr_l(dcrc)           /* image_set_dcrc */
846
847 #define image_set_hdr_b(f) \
848         static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
849         { \
850                 hdr->ih_##f = val; \
851         }
852 image_set_hdr_b(os)             /* image_set_os */
853 image_set_hdr_b(arch)           /* image_set_arch */
854 image_set_hdr_b(type)           /* image_set_type */
855 image_set_hdr_b(comp)           /* image_set_comp */
856
857 static inline void image_set_name(image_header_t *hdr, const char *name)
858 {
859         strncpy(image_get_name(hdr), name, IH_NMLEN);
860 }
861
862 int image_check_hcrc(const image_header_t *hdr);
863 int image_check_dcrc(const image_header_t *hdr);
864 #ifndef USE_HOSTCC
865 ulong env_get_bootm_low(void);
866 phys_size_t env_get_bootm_size(void);
867 phys_size_t env_get_bootm_mapsize(void);
868 #endif
869 void memmove_wd(void *to, void *from, size_t len, ulong chunksz);
870
871 static inline int image_check_magic(const image_header_t *hdr)
872 {
873         return (image_get_magic(hdr) == IH_MAGIC);
874 }
875 static inline int image_check_type(const image_header_t *hdr, uint8_t type)
876 {
877         return (image_get_type(hdr) == type);
878 }
879 static inline int image_check_arch(const image_header_t *hdr, uint8_t arch)
880 {
881 #ifndef USE_HOSTCC
882         /* Let's assume that sandbox can load any architecture */
883         if (IS_ENABLED(CONFIG_SANDBOX))
884                 return true;
885 #endif
886         return (image_get_arch(hdr) == arch) ||
887                 (image_get_arch(hdr) == IH_ARCH_ARM && arch == IH_ARCH_ARM64);
888 }
889 static inline int image_check_os(const image_header_t *hdr, uint8_t os)
890 {
891         return (image_get_os(hdr) == os);
892 }
893
894 ulong image_multi_count(const image_header_t *hdr);
895 void image_multi_getimg(const image_header_t *hdr, ulong idx,
896                         ulong *data, ulong *len);
897
898 void image_print_contents(const void *hdr);
899
900 #ifndef USE_HOSTCC
901 static inline int image_check_target_arch(const image_header_t *hdr)
902 {
903 #ifndef IH_ARCH_DEFAULT
904 # error "please define IH_ARCH_DEFAULT in your arch asm/u-boot.h"
905 #endif
906         return image_check_arch(hdr, IH_ARCH_DEFAULT);
907 }
908 #endif /* USE_HOSTCC */
909
910 /**
911  * image_decomp_type() - Find out compression type of an image
912  *
913  * @buf:        Address in U-Boot memory where image is loaded.
914  * @len:        Length of the compressed image.
915  * @return      compression type or IH_COMP_NONE if not compressed.
916  *
917  * Note: Only following compression types are supported now.
918  * lzo, lzma, gzip, bzip2
919  */
920 int image_decomp_type(const unsigned char *buf, ulong len);
921
922 /**
923  * image_decomp() - decompress an image
924  *
925  * @comp:       Compression algorithm that is used (IH_COMP_...)
926  * @load:       Destination load address in U-Boot memory
927  * @image_start Image start address (where we are decompressing from)
928  * @type:       OS type (IH_OS_...)
929  * @load_bug:   Place to decompress to
930  * @image_buf:  Address to decompress from
931  * @image_len:  Number of bytes in @image_buf to decompress
932  * @unc_len:    Available space for decompression
933  * @return 0 if OK, -ve on error (BOOTM_ERR_...)
934  */
935 int image_decomp(int comp, ulong load, ulong image_start, int type,
936                  void *load_buf, void *image_buf, ulong image_len,
937                  uint unc_len, ulong *load_end);
938
939 /**
940  * Set up properties in the FDT
941  *
942  * This sets up properties in the FDT that is to be passed to linux.
943  *
944  * @images:     Images information
945  * @blob:       FDT to update
946  * @of_size:    Size of the FDT
947  * @lmb:        Points to logical memory block structure
948  * @return 0 if ok, <0 on failure
949  */
950 int image_setup_libfdt(bootm_headers_t *images, void *blob,
951                        int of_size, struct lmb *lmb);
952
953 /**
954  * Set up the FDT to use for booting a kernel
955  *
956  * This performs ramdisk setup, sets up the FDT if required, and adds
957  * paramters to the FDT if libfdt is available.
958  *
959  * @param images        Images information
960  * @return 0 if ok, <0 on failure
961  */
962 int image_setup_linux(bootm_headers_t *images);
963
964 /**
965  * bootz_setup() - Extract stat and size of a Linux xImage
966  *
967  * @image: Address of image
968  * @start: Returns start address of image
969  * @end : Returns end address of image
970  * @return 0 if OK, 1 if the image was not recognised
971  */
972 int bootz_setup(ulong image, ulong *start, ulong *end);
973
974 /**
975  * Return the correct start address and size of a Linux aarch64 Image.
976  *
977  * @image: Address of image
978  * @start: Returns start address of image
979  * @size : Returns size image
980  * @force_reloc: Ignore image->ep field, always place image to RAM start
981  * @return 0 if OK, 1 if the image was not recognised
982  */
983 int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
984                 bool force_reloc);
985
986 /*******************************************************************/
987 /* New uImage format specific code (prefixed with fit_) */
988 /*******************************************************************/
989
990 #define FIT_IMAGES_PATH         "/images"
991 #define FIT_CONFS_PATH          "/configurations"
992
993 /* hash/signature/key node */
994 #define FIT_HASH_NODENAME       "hash"
995 #define FIT_ALGO_PROP           "algo"
996 #define FIT_VALUE_PROP          "value"
997 #define FIT_IGNORE_PROP         "uboot-ignore"
998 #define FIT_SIG_NODENAME        "signature"
999 #define FIT_KEY_REQUIRED        "required"
1000 #define FIT_KEY_HINT            "key-name-hint"
1001
1002 /* cipher node */
1003 #define FIT_CIPHER_NODENAME     "cipher"
1004 #define FIT_ALGO_PROP           "algo"
1005
1006 /* image node */
1007 #define FIT_DATA_PROP           "data"
1008 #define FIT_DATA_POSITION_PROP  "data-position"
1009 #define FIT_DATA_OFFSET_PROP    "data-offset"
1010 #define FIT_DATA_SIZE_PROP      "data-size"
1011 #define FIT_TIMESTAMP_PROP      "timestamp"
1012 #define FIT_DESC_PROP           "description"
1013 #define FIT_ARCH_PROP           "arch"
1014 #define FIT_TYPE_PROP           "type"
1015 #define FIT_OS_PROP             "os"
1016 #define FIT_COMP_PROP           "compression"
1017 #define FIT_ENTRY_PROP          "entry"
1018 #define FIT_LOAD_PROP           "load"
1019
1020 /* configuration node */
1021 #define FIT_KERNEL_PROP         "kernel"
1022 #define FIT_RAMDISK_PROP        "ramdisk"
1023 #define FIT_FDT_PROP            "fdt"
1024 #define FIT_LOADABLE_PROP       "loadables"
1025 #define FIT_DEFAULT_PROP        "default"
1026 #define FIT_SETUP_PROP          "setup"
1027 #define FIT_FPGA_PROP           "fpga"
1028 #define FIT_FIRMWARE_PROP       "firmware"
1029 #define FIT_STANDALONE_PROP     "standalone"
1030
1031 #define FIT_MAX_HASH_LEN        HASH_MAX_DIGEST_SIZE
1032
1033 #if IMAGE_ENABLE_FIT
1034 /* cmdline argument format parsing */
1035 int fit_parse_conf(const char *spec, ulong addr_curr,
1036                 ulong *addr, const char **conf_name);
1037 int fit_parse_subimage(const char *spec, ulong addr_curr,
1038                 ulong *addr, const char **image_name);
1039
1040 int fit_get_subimage_count(const void *fit, int images_noffset);
1041 void fit_print_contents(const void *fit);
1042 void fit_image_print(const void *fit, int noffset, const char *p);
1043
1044 /**
1045  * fit_get_end - get FIT image size
1046  * @fit: pointer to the FIT format image header
1047  *
1048  * returns:
1049  *     size of the FIT image (blob) in memory
1050  */
1051 static inline ulong fit_get_size(const void *fit)
1052 {
1053         return fdt_totalsize(fit);
1054 }
1055
1056 /**
1057  * fit_get_end - get FIT image end
1058  * @fit: pointer to the FIT format image header
1059  *
1060  * returns:
1061  *     end address of the FIT image (blob) in memory
1062  */
1063 ulong fit_get_end(const void *fit);
1064
1065 /**
1066  * fit_get_name - get FIT node name
1067  * @fit: pointer to the FIT format image header
1068  *
1069  * returns:
1070  *     NULL, on error
1071  *     pointer to node name, on success
1072  */
1073 static inline const char *fit_get_name(const void *fit_hdr,
1074                 int noffset, int *len)
1075 {
1076         return fdt_get_name(fit_hdr, noffset, len);
1077 }
1078
1079 int fit_get_desc(const void *fit, int noffset, char **desc);
1080 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp);
1081
1082 int fit_image_get_node(const void *fit, const char *image_uname);
1083 int fit_image_get_os(const void *fit, int noffset, uint8_t *os);
1084 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch);
1085 int fit_image_get_type(const void *fit, int noffset, uint8_t *type);
1086 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp);
1087 int fit_image_get_load(const void *fit, int noffset, ulong *load);
1088 int fit_image_get_entry(const void *fit, int noffset, ulong *entry);
1089 int fit_image_get_data(const void *fit, int noffset,
1090                                 const void **data, size_t *size);
1091 int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset);
1092 int fit_image_get_data_position(const void *fit, int noffset,
1093                                 int *data_position);
1094 int fit_image_get_data_size(const void *fit, int noffset, int *data_size);
1095 int fit_image_get_data_size_unciphered(const void *fit, int noffset,
1096                                        size_t *data_size);
1097 int fit_image_get_data_and_size(const void *fit, int noffset,
1098                                 const void **data, size_t *size);
1099
1100 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo);
1101 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
1102                                 int *value_len);
1103
1104 int fit_set_timestamp(void *fit, int noffset, time_t timestamp);
1105
1106 int fit_cipher_data(const char *keydir, void *keydest, void *fit,
1107                     const char *comment, int require_keys,
1108                     const char *engine_id, const char *cmdname);
1109
1110 /**
1111  * fit_add_verification_data() - add verification data to FIT image nodes
1112  *
1113  * @keydir:     Directory containing keys
1114  * @kwydest:    FDT blob to write public key information to
1115  * @fit:        Pointer to the FIT format image header
1116  * @comment:    Comment to add to signature nodes
1117  * @require_keys: Mark all keys as 'required'
1118  * @engine_id:  Engine to use for signing
1119  * @cmdname:    Command name used when reporting errors
1120  *
1121  * Adds hash values for all component images in the FIT blob.
1122  * Hashes are calculated for all component images which have hash subnodes
1123  * with algorithm property set to one of the supported hash algorithms.
1124  *
1125  * Also add signatures if signature nodes are present.
1126  *
1127  * returns
1128  *     0, on success
1129  *     libfdt error code, on failure
1130  */
1131 int fit_add_verification_data(const char *keydir, const char *keyfile,
1132                               void *keydest, void *fit, const char *comment,
1133                               int require_keys, const char *engine_id,
1134                               const char *cmdname);
1135
1136 int fit_image_verify_with_data(const void *fit, int image_noffset,
1137                                const void *data, size_t size);
1138 int fit_image_verify(const void *fit, int noffset);
1139 int fit_config_verify(const void *fit, int conf_noffset);
1140 int fit_all_image_verify(const void *fit);
1141 int fit_config_decrypt(const void *fit, int conf_noffset);
1142 int fit_image_check_os(const void *fit, int noffset, uint8_t os);
1143 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch);
1144 int fit_image_check_type(const void *fit, int noffset, uint8_t type);
1145 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp);
1146
1147 /**
1148  * fit_check_format() - Check that the FIT is valid
1149  *
1150  * This performs various checks on the FIT to make sure it is suitable for
1151  * use, looking for mandatory properties, nodes, etc.
1152  *
1153  * If FIT_FULL_CHECK is enabled, it also runs it through libfdt to make
1154  * sure that there are no strange tags or broken nodes in the FIT.
1155  *
1156  * @fit: pointer to the FIT format image header
1157  * @return 0 if OK, -ENOEXEC if not an FDT file, -EINVAL if the full FDT check
1158  *      failed (e.g. due to bad structure), -ENOMSG if the description is
1159  *      missing, -EBADMSG if the timestamp is missing, -ENOENT if the /images
1160  *      path is missing
1161  */
1162 int fit_check_format(const void *fit, ulong size);
1163
1164 int fit_conf_find_compat(const void *fit, const void *fdt);
1165
1166 /**
1167  * fit_conf_get_node - get node offset for configuration of a given unit name
1168  * @fit: pointer to the FIT format image header
1169  * @conf_uname: configuration node unit name (NULL to use default)
1170  *
1171  * fit_conf_get_node() finds a configuration (within the '/configurations'
1172  * parent node) of a provided unit name. If configuration is found its node
1173  * offset is returned to the caller.
1174  *
1175  * When NULL is provided in second argument fit_conf_get_node() will search
1176  * for a default configuration node instead. Default configuration node unit
1177  * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
1178  * node.
1179  *
1180  * returns:
1181  *     configuration node offset when found (>=0)
1182  *     negative number on failure (FDT_ERR_* code)
1183  */
1184 int fit_conf_get_node(const void *fit, const char *conf_uname);
1185
1186 int fit_conf_get_prop_node_count(const void *fit, int noffset,
1187                 const char *prop_name);
1188 int fit_conf_get_prop_node_index(const void *fit, int noffset,
1189                 const char *prop_name, int index);
1190
1191 /**
1192  * fit_conf_get_prop_node() - Get node refered to by a configuration
1193  * @fit:        FIT to check
1194  * @noffset:    Offset of conf@xxx node to check
1195  * @prop_name:  Property to read from the conf node
1196  *
1197  * The conf- nodes contain references to other nodes, using properties
1198  * like 'kernel = "kernel"'. Given such a property name (e.g. "kernel"),
1199  * return the offset of the node referred to (e.g. offset of node
1200  * "/images/kernel".
1201  */
1202 int fit_conf_get_prop_node(const void *fit, int noffset,
1203                 const char *prop_name);
1204
1205 int fit_check_ramdisk(const void *fit, int os_noffset,
1206                 uint8_t arch, int verify);
1207 #endif /* IMAGE_ENABLE_FIT */
1208
1209 int calculate_hash(const void *data, int data_len, const char *algo,
1210                         uint8_t *value, int *value_len);
1211
1212 /*
1213  * At present we only support signing on the host, and verification on the
1214  * device
1215  */
1216 #if defined(USE_HOSTCC)
1217 # if defined(CONFIG_FIT_SIGNATURE)
1218 #  define IMAGE_ENABLE_SIGN     1
1219 #  define IMAGE_ENABLE_VERIFY   1
1220 #  define IMAGE_ENABLE_VERIFY_ECDSA     1
1221 #  define FIT_IMAGE_ENABLE_VERIFY       1
1222 #  include <openssl/evp.h>
1223 # else
1224 #  define IMAGE_ENABLE_SIGN     0
1225 #  define IMAGE_ENABLE_VERIFY   0
1226 # define IMAGE_ENABLE_VERIFY_ECDSA      0
1227 #  define FIT_IMAGE_ENABLE_VERIFY       0
1228 # endif
1229 #else
1230 # define IMAGE_ENABLE_SIGN      0
1231 # define IMAGE_ENABLE_VERIFY            CONFIG_IS_ENABLED(RSA_VERIFY)
1232 # define IMAGE_ENABLE_VERIFY_ECDSA      0
1233 # define FIT_IMAGE_ENABLE_VERIFY        CONFIG_IS_ENABLED(FIT_SIGNATURE)
1234 #endif
1235
1236 #if IMAGE_ENABLE_FIT
1237 #ifdef USE_HOSTCC
1238 void *image_get_host_blob(void);
1239 void image_set_host_blob(void *host_blob);
1240 # define gd_fdt_blob()          image_get_host_blob()
1241 #else
1242 # define gd_fdt_blob()          (gd->fdt_blob)
1243 #endif
1244
1245 #ifdef CONFIG_FIT_BEST_MATCH
1246 #define IMAGE_ENABLE_BEST_MATCH 1
1247 #else
1248 #define IMAGE_ENABLE_BEST_MATCH 0
1249 #endif
1250 #endif /* IMAGE_ENABLE_FIT */
1251
1252 /*
1253  * Information passed to the signing routines
1254  *
1255  * Either 'keydir',  'keyname', or 'keyfile' can be NULL. However, either
1256  * 'keyfile', or both 'keydir' and 'keyname' should have valid values. If
1257  * neither are valid, some operations might fail with EINVAL.
1258  */
1259 struct image_sign_info {
1260         const char *keydir;             /* Directory conaining keys */
1261         const char *keyname;            /* Name of key to use */
1262         const char *keyfile;            /* Filename of private or public key */
1263         void *fit;                      /* Pointer to FIT blob */
1264         int node_offset;                /* Offset of signature node */
1265         const char *name;               /* Algorithm name */
1266         struct checksum_algo *checksum; /* Checksum algorithm information */
1267         struct padding_algo *padding;   /* Padding algorithm information */
1268         struct crypto_algo *crypto;     /* Crypto algorithm information */
1269         const void *fdt_blob;           /* FDT containing public keys */
1270         int required_keynode;           /* Node offset of key to use: -1=any */
1271         const char *require_keys;       /* Value for 'required' property */
1272         const char *engine_id;          /* Engine to use for signing */
1273         /*
1274          * Note: the following two fields are always valid even w/o
1275          * RSA_VERIFY_WITH_PKEY in order to make sure this structure is
1276          * the same on target and host. Otherwise, vboot test may fail.
1277          */
1278         const void *key;                /* Pointer to public key in DER */
1279         int keylen;                     /* Length of public key */
1280 };
1281
1282 /* A part of an image, used for hashing */
1283 struct image_region {
1284         const void *data;
1285         int size;
1286 };
1287
1288 #if IMAGE_ENABLE_VERIFY
1289 # include <u-boot/hash-checksum.h>
1290 #endif
1291 struct checksum_algo {
1292         const char *name;
1293         const int checksum_len;
1294         const int der_len;
1295         const uint8_t *der_prefix;
1296 #if IMAGE_ENABLE_SIGN
1297         const EVP_MD *(*calculate_sign)(void);
1298 #endif
1299         int (*calculate)(const char *name,
1300                          const struct image_region region[],
1301                          int region_count, uint8_t *checksum);
1302 };
1303
1304 struct crypto_algo {
1305         const char *name;               /* Name of algorithm */
1306         const int key_len;
1307
1308         /**
1309          * sign() - calculate and return signature for given input data
1310          *
1311          * @info:       Specifies key and FIT information
1312          * @data:       Pointer to the input data
1313          * @data_len:   Data length
1314          * @sigp:       Set to an allocated buffer holding the signature
1315          * @sig_len:    Set to length of the calculated hash
1316          *
1317          * This computes input data signature according to selected algorithm.
1318          * Resulting signature value is placed in an allocated buffer, the
1319          * pointer is returned as *sigp. The length of the calculated
1320          * signature is returned via the sig_len pointer argument. The caller
1321          * should free *sigp.
1322          *
1323          * @return: 0, on success, -ve on error
1324          */
1325         int (*sign)(struct image_sign_info *info,
1326                     const struct image_region region[],
1327                     int region_count, uint8_t **sigp, uint *sig_len);
1328
1329         /**
1330          * add_verify_data() - Add verification information to FDT
1331          *
1332          * Add public key information to the FDT node, suitable for
1333          * verification at run-time. The information added depends on the
1334          * algorithm being used.
1335          *
1336          * @info:       Specifies key and FIT information
1337          * @keydest:    Destination FDT blob for public key data
1338          * @return: 0, on success, -ve on error
1339          */
1340         int (*add_verify_data)(struct image_sign_info *info, void *keydest);
1341
1342         /**
1343          * verify() - Verify a signature against some data
1344          *
1345          * @info:       Specifies key and FIT information
1346          * @data:       Pointer to the input data
1347          * @data_len:   Data length
1348          * @sig:        Signature
1349          * @sig_len:    Number of bytes in signature
1350          * @return 0 if verified, -ve on error
1351          */
1352         int (*verify)(struct image_sign_info *info,
1353                       const struct image_region region[], int region_count,
1354                       uint8_t *sig, uint sig_len);
1355 };
1356
1357 struct padding_algo {
1358         const char *name;
1359         int (*verify)(struct image_sign_info *info,
1360                       uint8_t *pad, int pad_len,
1361                       const uint8_t *hash, int hash_len);
1362 };
1363
1364 /**
1365  * image_get_checksum_algo() - Look up a checksum algorithm
1366  *
1367  * @param full_name     Name of algorithm in the form "checksum,crypto"
1368  * @return pointer to algorithm information, or NULL if not found
1369  */
1370 struct checksum_algo *image_get_checksum_algo(const char *full_name);
1371
1372 /**
1373  * image_get_crypto_algo() - Look up a cryptosystem algorithm
1374  *
1375  * @param full_name     Name of algorithm in the form "checksum,crypto"
1376  * @return pointer to algorithm information, or NULL if not found
1377  */
1378 struct crypto_algo *image_get_crypto_algo(const char *full_name);
1379
1380 /**
1381  * image_get_padding_algo() - Look up a padding algorithm
1382  *
1383  * @param name          Name of padding algorithm
1384  * @return pointer to algorithm information, or NULL if not found
1385  */
1386 struct padding_algo *image_get_padding_algo(const char *name);
1387
1388 #if IMAGE_ENABLE_FIT
1389
1390 /**
1391  * fit_image_verify_required_sigs() - Verify signatures marked as 'required'
1392  *
1393  * @fit:                FIT to check
1394  * @image_noffset:      Offset of image node to check
1395  * @data:               Image data to check
1396  * @size:               Size of image data
1397  * @sig_blob:           FDT containing public keys
1398  * @no_sigsp:           Returns 1 if no signatures were required, and
1399  *                      therefore nothing was checked. The caller may wish
1400  *                      to fall back to other mechanisms, or refuse to
1401  *                      boot.
1402  * @return 0 if all verified ok, <0 on error
1403  */
1404 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
1405                 const char *data, size_t size, const void *sig_blob,
1406                 int *no_sigsp);
1407
1408 /**
1409  * fit_image_check_sig() - Check a single image signature node
1410  *
1411  * @fit:                FIT to check
1412  * @noffset:            Offset of signature node to check
1413  * @data:               Image data to check
1414  * @size:               Size of image data
1415  * @required_keynode:   Offset in the control FDT of the required key node,
1416  *                      if any. If this is given, then the image wil not
1417  *                      pass verification unless that key is used. If this is
1418  *                      -1 then any signature will do.
1419  * @err_msgp:           In the event of an error, this will be pointed to a
1420  *                      help error string to display to the user.
1421  * @return 0 if all verified ok, <0 on error
1422  */
1423 int fit_image_check_sig(const void *fit, int noffset, const void *data,
1424                 size_t size, int required_keynode, char **err_msgp);
1425
1426 int fit_image_decrypt_data(const void *fit,
1427                            int image_noffset, int cipher_noffset,
1428                            const void *data, size_t size,
1429                            void **data_unciphered, size_t *size_unciphered);
1430
1431 /**
1432  * fit_region_make_list() - Make a list of regions to hash
1433  *
1434  * Given a list of FIT regions (offset, size) provided by libfdt, create
1435  * a list of regions (void *, size) for use by the signature creationg
1436  * and verification code.
1437  *
1438  * @fit:                FIT image to process
1439  * @fdt_regions:        Regions as returned by libfdt
1440  * @count:              Number of regions returned by libfdt
1441  * @region:             Place to put list of regions (NULL to allocate it)
1442  * @return pointer to list of regions, or NULL if out of memory
1443  */
1444 struct image_region *fit_region_make_list(const void *fit,
1445                 struct fdt_region *fdt_regions, int count,
1446                 struct image_region *region);
1447
1448 static inline int fit_image_check_target_arch(const void *fdt, int node)
1449 {
1450 #ifndef USE_HOSTCC
1451         return fit_image_check_arch(fdt, node, IH_ARCH_DEFAULT);
1452 #else
1453         return 0;
1454 #endif
1455 }
1456
1457 /*
1458  * At present we only support ciphering on the host, and unciphering on the
1459  * device
1460  */
1461 #if defined(USE_HOSTCC)
1462 # if defined(CONFIG_FIT_CIPHER)
1463 #  define IMAGE_ENABLE_ENCRYPT  1
1464 #  define IMAGE_ENABLE_DECRYPT  1
1465 #  include <openssl/evp.h>
1466 # else
1467 #  define IMAGE_ENABLE_ENCRYPT  0
1468 #  define IMAGE_ENABLE_DECRYPT  0
1469 # endif
1470 #else
1471 # define IMAGE_ENABLE_ENCRYPT   0
1472 # define IMAGE_ENABLE_DECRYPT   CONFIG_IS_ENABLED(FIT_CIPHER)
1473 #endif
1474
1475 /* Information passed to the ciphering routines */
1476 struct image_cipher_info {
1477         const char *keydir;             /* Directory containing keys */
1478         const char *keyname;            /* Name of key to use */
1479         const char *ivname;             /* Name of IV to use */
1480         const void *fit;                /* Pointer to FIT blob */
1481         int node_noffset;               /* Offset of the cipher node */
1482         const char *name;               /* Algorithm name */
1483         struct cipher_algo *cipher;     /* Cipher algorithm information */
1484         const void *fdt_blob;           /* FDT containing key and IV */
1485         const void *key;                /* Value of the key */
1486         const void *iv;                 /* Value of the IV */
1487         size_t size_unciphered;         /* Size of the unciphered data */
1488 };
1489
1490 struct cipher_algo {
1491         const char *name;               /* Name of algorithm */
1492         int key_len;                    /* Length of the key */
1493         int iv_len;                     /* Length of the IV */
1494
1495 #if IMAGE_ENABLE_ENCRYPT
1496         const EVP_CIPHER * (*calculate_type)(void);
1497 #endif
1498
1499         int (*encrypt)(struct image_cipher_info *info,
1500                        const unsigned char *data, int data_len,
1501                        unsigned char **cipher, int *cipher_len);
1502
1503         int (*add_cipher_data)(struct image_cipher_info *info,
1504                                void *keydest, void *fit, int node_noffset);
1505
1506         int (*decrypt)(struct image_cipher_info *info,
1507                        const void *cipher, size_t cipher_len,
1508                        void **data, size_t *data_len);
1509 };
1510
1511 int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo);
1512
1513 struct cipher_algo *image_get_cipher_algo(const char *full_name);
1514
1515 #ifdef CONFIG_FIT_VERBOSE
1516 #define fit_unsupported(msg)    printf("! %s:%d " \
1517                                 "FIT images not supported for '%s'\n", \
1518                                 __FILE__, __LINE__, (msg))
1519
1520 #define fit_unsupported_reset(msg)      printf("! %s:%d " \
1521                                 "FIT images not supported for '%s' " \
1522                                 "- must reset board to recover!\n", \
1523                                 __FILE__, __LINE__, (msg))
1524 #else
1525 #define fit_unsupported(msg)
1526 #define fit_unsupported_reset(msg)
1527 #endif /* CONFIG_FIT_VERBOSE */
1528 #endif /* CONFIG_FIT */
1529
1530 #if !defined(USE_HOSTCC)
1531 #if defined(CONFIG_ANDROID_BOOT_IMAGE)
1532 struct andr_img_hdr;
1533 int android_image_check_header(const struct andr_img_hdr *hdr);
1534 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
1535                              ulong *os_data, ulong *os_len);
1536 int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
1537                               ulong *rd_data, ulong *rd_len);
1538 int android_image_get_second(const struct andr_img_hdr *hdr,
1539                               ulong *second_data, ulong *second_len);
1540 bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size);
1541 bool android_image_get_dtb_by_index(ulong hdr_addr, u32 index, ulong *addr,
1542                                     u32 *size);
1543 ulong android_image_get_end(const struct andr_img_hdr *hdr);
1544 ulong android_image_get_kload(const struct andr_img_hdr *hdr);
1545 ulong android_image_get_kcomp(const struct andr_img_hdr *hdr);
1546 void android_print_contents(const struct andr_img_hdr *hdr);
1547 #if !defined(CONFIG_SPL_BUILD)
1548 bool android_image_print_dtb_contents(ulong hdr_addr);
1549 #endif
1550
1551 #endif /* CONFIG_ANDROID_BOOT_IMAGE */
1552 #endif /* !USE_HOSTCC */
1553
1554 /**
1555  * board_fit_config_name_match() - Check for a matching board name
1556  *
1557  * This is used when SPL loads a FIT containing multiple device tree files
1558  * and wants to work out which one to use. The description of each one is
1559  * passed to this function. The description comes from the 'description' field
1560  * in each (FDT) image node.
1561  *
1562  * @name: Device tree description
1563  * @return 0 if this device tree should be used, non-zero to try the next
1564  */
1565 int board_fit_config_name_match(const char *name);
1566
1567 /**
1568  * board_fit_image_post_process() - Do any post-process on FIT binary data
1569  *
1570  * This is used to do any sort of image manipulation, verification, decryption
1571  * etc. in a platform or board specific way. Obviously, anything done here would
1572  * need to be comprehended in how the images were prepared before being injected
1573  * into the FIT creation (i.e. the binary blobs would have been pre-processed
1574  * before being added to the FIT image).
1575  *
1576  * @fit: pointer to fit image
1577  * @node: offset of image node
1578  * @image: pointer to the image start pointer
1579  * @size: pointer to the image size
1580  * @return no return value (failure should be handled internally)
1581  */
1582 void board_fit_image_post_process(const void *fit, int node, void **p_image,
1583                                   size_t *p_size);
1584
1585 #define FDT_ERROR       ((ulong)(-1))
1586
1587 ulong fdt_getprop_u32(const void *fdt, int node, const char *prop);
1588
1589 /**
1590  * fit_find_config_node() - Find the node for the best DTB in a FIT image
1591  *
1592  * A FIT image contains one or more DTBs. This function parses the
1593  * configurations described in the FIT images and returns the node of
1594  * the first matching DTB. To check if a DTB matches a board, this function
1595  * calls board_fit_config_name_match(). If no matching DTB is found, it returns
1596  * the node described by the default configuration if it exists.
1597  *
1598  * @fdt: pointer to flat device tree
1599  * @return the node if found, -ve otherwise
1600  */
1601 int fit_find_config_node(const void *fdt);
1602
1603 /**
1604  * Mapping of image types to function handlers to be invoked on the associated
1605  * loaded images
1606  *
1607  * @type: Type of image, I.E. IH_TYPE_*
1608  * @handler: Function to call on loaded image
1609  */
1610 struct fit_loadable_tbl {
1611         int type;
1612         /**
1613          * handler() - Process a loaded image
1614          *
1615          * @data: Pointer to start of loaded image data
1616          * @size: Size of loaded image data
1617          */
1618         void (*handler)(ulong data, size_t size);
1619 };
1620
1621 /*
1622  * Define a FIT loadable image type handler
1623  *
1624  * _type is a valid uimage_type ID as defined in the "Image Type" enum above
1625  * _handler is the handler function to call after this image type is loaded
1626  */
1627 #define U_BOOT_FIT_LOADABLE_HANDLER(_type, _handler) \
1628         ll_entry_declare(struct fit_loadable_tbl, _function, fit_loadable) = { \
1629                 .type = _type, \
1630                 .handler = _handler, \
1631         }
1632
1633 /**
1634  * fit_update - update storage with FIT image
1635  * @fit:        Pointer to FIT image
1636  *
1637  * Update firmware on storage using FIT image as input.
1638  * The storage area to be update will be identified by the name
1639  * in FIT and matching it to "dfu_alt_info" variable.
1640  *
1641  * Return:      0 on success, non-zero otherwise
1642  */
1643 int fit_update(const void *fit);
1644
1645 #endif  /* __IMAGE_H__ */