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