]> git.sur5r.net Git - u-boot/blob - include/image.h
Merge git://git.denx.de/u-boot-rockchip
[u-boot] / include / image.h
1 /*
2  * (C) Copyright 2008 Semihalf
3  *
4  * (C) Copyright 2000-2005
5  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  ********************************************************************
9  * NOTE: This header file defines an interface to U-Boot. Including
10  * this (unmodified) header file in another file is considered normal
11  * use of U-Boot, and does *not* fall under the heading of "derived
12  * work".
13  ********************************************************************
14  */
15
16 #ifndef __IMAGE_H__
17 #define __IMAGE_H__
18
19 #include "compiler.h"
20 #include <asm/byteorder.h>
21
22 /* Define this to avoid #ifdefs later on */
23 struct lmb;
24
25 #ifdef USE_HOSTCC
26 #include <sys/types.h>
27
28 /* new uImage format support enabled on host */
29 #define IMAGE_ENABLE_FIT        1
30 #define IMAGE_ENABLE_OF_LIBFDT  1
31 #define CONFIG_FIT_VERBOSE      1 /* enable fit_format_{error,warning}() */
32
33 #define IMAGE_ENABLE_IGNORE     0
34 #define IMAGE_INDENT_STRING     ""
35
36 #else
37
38 #include <lmb.h>
39 #include <asm/u-boot.h>
40 #include <command.h>
41
42 /* Take notice of the 'ignore' property for hashes */
43 #define IMAGE_ENABLE_IGNORE     1
44 #define IMAGE_INDENT_STRING     "   "
45
46 #define IMAGE_ENABLE_FIT        CONFIG_IS_ENABLED(FIT)
47 #define IMAGE_ENABLE_OF_LIBFDT  CONFIG_IS_ENABLED(OF_LIBFDT)
48
49 #endif /* USE_HOSTCC */
50
51 #if IMAGE_ENABLE_FIT
52 #include <hash.h>
53 #include <libfdt.h>
54 #include <fdt_support.h>
55 # ifdef CONFIG_FIT_DISABLE_SHA256
56 #  undef CONFIG_SHA256
57 #  undef IMAGE_ENABLE_SHA256
58 # endif
59 # ifdef CONFIG_SPL_BUILD
60 #  define IMAGE_ENABLE_CRC32    CONFIG_IS_ENABLED(SPL_CRC32_SUPPORT)
61 #  define IMAGE_ENABLE_MD5      CONFIG_IS_ENABLED(SPL_MD5_SUPPORT)
62 #  define IMAGE_ENABLE_SHA1     CONFIG_IS_ENABLED(SPL_SHA1_SUPPORT)
63 #  define IMAGE_ENABLE_SHA256   CONFIG_IS_ENABLED(SPL_SHA256_SUPPORT)
64 # else
65 #  define CONFIG_CRC32          /* FIT images need CRC32 support */
66 #  define CONFIG_MD5            /* and MD5 */
67 #  define CONFIG_SHA1           /* and SHA1 */
68 #  define CONFIG_SHA256         /* and SHA256 */
69 #  define IMAGE_ENABLE_CRC32    1
70 #  define IMAGE_ENABLE_MD5      1
71 #  define IMAGE_ENABLE_SHA1     1
72 #  define IMAGE_ENABLE_SHA256   1
73 # endif
74 #endif /* IMAGE_ENABLE_FIT */
75
76 #define IMAGE_ENABLE_RAMDISK_HIGH       CONFIG_IS_ENABLED(SYS_BOOT_RAMDISK_HIGH)
77 #define IMAGE_BOOT_GET_CMDLINE          CONFIG_IS_ENABLED(SYS_BOOT_GET_CMDLINE)
78 #define IMAGE_OF_BOARD_SETUP            CONFIG_IS_ENABLED(OF_BOARD_SETUP)
79 #define IMAGE_OF_SYSTEM_SETUP           CONFIG_IS_ENABLED(OF_SYSTEM_SETUP)
80
81 /*
82  * Operating System Codes
83  */
84 #define IH_OS_INVALID           0       /* Invalid OS   */
85 #define IH_OS_OPENBSD           1       /* OpenBSD      */
86 #define IH_OS_NETBSD            2       /* NetBSD       */
87 #define IH_OS_FREEBSD           3       /* FreeBSD      */
88 #define IH_OS_4_4BSD            4       /* 4.4BSD       */
89 #define IH_OS_LINUX             5       /* Linux        */
90 #define IH_OS_SVR4              6       /* SVR4         */
91 #define IH_OS_ESIX              7       /* Esix         */
92 #define IH_OS_SOLARIS           8       /* Solaris      */
93 #define IH_OS_IRIX              9       /* Irix         */
94 #define IH_OS_SCO               10      /* SCO          */
95 #define IH_OS_DELL              11      /* Dell         */
96 #define IH_OS_NCR               12      /* NCR          */
97 #define IH_OS_LYNXOS            13      /* LynxOS       */
98 #define IH_OS_VXWORKS           14      /* VxWorks      */
99 #define IH_OS_PSOS              15      /* pSOS         */
100 #define IH_OS_QNX               16      /* QNX          */
101 #define IH_OS_U_BOOT            17      /* Firmware     */
102 #define IH_OS_RTEMS             18      /* RTEMS        */
103 #define IH_OS_ARTOS             19      /* ARTOS        */
104 #define IH_OS_UNITY             20      /* Unity OS     */
105 #define IH_OS_INTEGRITY         21      /* INTEGRITY    */
106 #define IH_OS_OSE               22      /* OSE          */
107 #define IH_OS_PLAN9             23      /* Plan 9       */
108 #define IH_OS_OPENRTOS          24      /* OpenRTOS     */
109
110 /*
111  * CPU Architecture Codes (supported by Linux)
112  */
113 #define IH_ARCH_INVALID         0       /* Invalid CPU  */
114 #define IH_ARCH_ALPHA           1       /* Alpha        */
115 #define IH_ARCH_ARM             2       /* ARM          */
116 #define IH_ARCH_I386            3       /* Intel x86    */
117 #define IH_ARCH_IA64            4       /* IA64         */
118 #define IH_ARCH_MIPS            5       /* MIPS         */
119 #define IH_ARCH_MIPS64          6       /* MIPS  64 Bit */
120 #define IH_ARCH_PPC             7       /* PowerPC      */
121 #define IH_ARCH_S390            8       /* IBM S390     */
122 #define IH_ARCH_SH              9       /* SuperH       */
123 #define IH_ARCH_SPARC           10      /* Sparc        */
124 #define IH_ARCH_SPARC64         11      /* Sparc 64 Bit */
125 #define IH_ARCH_M68K            12      /* M68K         */
126 #define IH_ARCH_MICROBLAZE      14      /* MicroBlaze   */
127 #define IH_ARCH_NIOS2           15      /* Nios-II      */
128 #define IH_ARCH_BLACKFIN        16      /* Blackfin     */
129 #define IH_ARCH_AVR32           17      /* AVR32        */
130 #define IH_ARCH_ST200           18      /* STMicroelectronics ST200  */
131 #define IH_ARCH_SANDBOX         19      /* Sandbox architecture (test only) */
132 #define IH_ARCH_NDS32           20      /* ANDES Technology - NDS32  */
133 #define IH_ARCH_OPENRISC        21      /* OpenRISC 1000  */
134 #define IH_ARCH_ARM64           22      /* ARM64        */
135 #define IH_ARCH_ARC             23      /* Synopsys DesignWare ARC */
136 #define IH_ARCH_X86_64          24      /* AMD x86_64, Intel and Via */
137
138 /*
139  * Image Types
140  *
141  * "Standalone Programs" are directly runnable in the environment
142  *      provided by U-Boot; it is expected that (if they behave
143  *      well) you can continue to work in U-Boot after return from
144  *      the Standalone Program.
145  * "OS Kernel Images" are usually images of some Embedded OS which
146  *      will take over control completely. Usually these programs
147  *      will install their own set of exception handlers, device
148  *      drivers, set up the MMU, etc. - this means, that you cannot
149  *      expect to re-enter U-Boot except by resetting the CPU.
150  * "RAMDisk Images" are more or less just data blocks, and their
151  *      parameters (address, size) are passed to an OS kernel that is
152  *      being started.
153  * "Multi-File Images" contain several images, typically an OS
154  *      (Linux) kernel image and one or more data images like
155  *      RAMDisks. This construct is useful for instance when you want
156  *      to boot over the network using BOOTP etc., where the boot
157  *      server provides just a single image file, but you want to get
158  *      for instance an OS kernel and a RAMDisk image.
159  *
160  *      "Multi-File Images" start with a list of image sizes, each
161  *      image size (in bytes) specified by an "uint32_t" in network
162  *      byte order. This list is terminated by an "(uint32_t)0".
163  *      Immediately after the terminating 0 follow the images, one by
164  *      one, all aligned on "uint32_t" boundaries (size rounded up to
165  *      a multiple of 4 bytes - except for the last file).
166  *
167  * "Firmware Images" are binary images containing firmware (like
168  *      U-Boot or FPGA images) which usually will be programmed to
169  *      flash memory.
170  *
171  * "Script files" are command sequences that will be executed by
172  *      U-Boot's command interpreter; this feature is especially
173  *      useful when you configure U-Boot to use a real shell (hush)
174  *      as command interpreter (=> Shell Scripts).
175  */
176
177 #define IH_TYPE_INVALID         0       /* Invalid Image                */
178 #define IH_TYPE_STANDALONE      1       /* Standalone Program           */
179 #define IH_TYPE_KERNEL          2       /* OS Kernel Image              */
180 #define IH_TYPE_RAMDISK         3       /* RAMDisk Image                */
181 #define IH_TYPE_MULTI           4       /* Multi-File Image             */
182 #define IH_TYPE_FIRMWARE        5       /* Firmware Image               */
183 #define IH_TYPE_SCRIPT          6       /* Script file                  */
184 #define IH_TYPE_FILESYSTEM      7       /* Filesystem Image (any type)  */
185 #define IH_TYPE_FLATDT          8       /* Binary Flat Device Tree Blob */
186 #define IH_TYPE_KWBIMAGE        9       /* Kirkwood Boot Image          */
187 #define IH_TYPE_IMXIMAGE        10      /* Freescale IMXBoot Image      */
188 #define IH_TYPE_UBLIMAGE        11      /* Davinci UBL Image            */
189 #define IH_TYPE_OMAPIMAGE       12      /* TI OMAP Config Header Image  */
190 #define IH_TYPE_AISIMAGE        13      /* TI Davinci AIS Image         */
191 #define IH_TYPE_KERNEL_NOLOAD   14      /* OS Kernel Image, can run from any load address */
192 #define IH_TYPE_PBLIMAGE        15      /* Freescale PBL Boot Image     */
193 #define IH_TYPE_MXSIMAGE        16      /* Freescale MXSBoot Image      */
194 #define IH_TYPE_GPIMAGE         17      /* TI Keystone GPHeader Image   */
195 #define IH_TYPE_ATMELIMAGE      18      /* ATMEL ROM bootable Image     */
196 #define IH_TYPE_SOCFPGAIMAGE    19      /* Altera SOCFPGA Preloader     */
197 #define IH_TYPE_X86_SETUP       20      /* x86 setup.bin Image          */
198 #define IH_TYPE_LPC32XXIMAGE    21      /* x86 setup.bin Image          */
199 #define IH_TYPE_LOADABLE        22      /* A list of typeless images    */
200 #define IH_TYPE_RKIMAGE         23      /* Rockchip Boot Image          */
201 #define IH_TYPE_RKSD            24      /* Rockchip SD card             */
202 #define IH_TYPE_RKSPI           25      /* Rockchip SPI image           */
203 #define IH_TYPE_ZYNQIMAGE       26      /* Xilinx Zynq Boot Image */
204 #define IH_TYPE_ZYNQMPIMAGE     27      /* Xilinx ZynqMP Boot Image */
205 #define IH_TYPE_FPGA            28      /* FPGA Image */
206
207 #define IH_TYPE_COUNT           29      /* Number of image types */
208
209 /*
210  * Compression Types
211  */
212 #define IH_COMP_NONE            0       /*  No   Compression Used       */
213 #define IH_COMP_GZIP            1       /* gzip  Compression Used       */
214 #define IH_COMP_BZIP2           2       /* bzip2 Compression Used       */
215 #define IH_COMP_LZMA            3       /* lzma  Compression Used       */
216 #define IH_COMP_LZO             4       /* lzo   Compression Used       */
217 #define IH_COMP_LZ4             5       /* lz4   Compression Used       */
218
219 #define IH_MAGIC        0x27051956      /* Image Magic Number           */
220 #define IH_NMLEN                32      /* Image Name Length            */
221
222 /* Reused from common.h */
223 #define ROUND(a, b)             (((a) + (b) - 1) & ~((b) - 1))
224
225 /*
226  * Legacy format image header,
227  * all data in network byte order (aka natural aka bigendian).
228  */
229 typedef struct image_header {
230         __be32          ih_magic;       /* Image Header Magic Number    */
231         __be32          ih_hcrc;        /* Image Header CRC Checksum    */
232         __be32          ih_time;        /* Image Creation Timestamp     */
233         __be32          ih_size;        /* Image Data Size              */
234         __be32          ih_load;        /* Data  Load  Address          */
235         __be32          ih_ep;          /* Entry Point Address          */
236         __be32          ih_dcrc;        /* Image Data CRC Checksum      */
237         uint8_t         ih_os;          /* Operating System             */
238         uint8_t         ih_arch;        /* CPU architecture             */
239         uint8_t         ih_type;        /* Image Type                   */
240         uint8_t         ih_comp;        /* Compression Type             */
241         uint8_t         ih_name[IH_NMLEN];      /* Image Name           */
242 } image_header_t;
243
244 typedef struct image_info {
245         ulong           start, end;             /* start/end of blob */
246         ulong           image_start, image_len; /* start of image within blob, len of image */
247         ulong           load;                   /* load addr for the image */
248         uint8_t         comp, type, os;         /* compression, type of image, os type */
249         uint8_t         arch;                   /* CPU architecture */
250 } image_info_t;
251
252 /*
253  * Legacy and FIT format headers used by do_bootm() and do_bootm_<os>()
254  * routines.
255  */
256 typedef struct bootm_headers {
257         /*
258          * Legacy os image header, if it is a multi component image
259          * then boot_get_ramdisk() and get_fdt() will attempt to get
260          * data from second and third component accordingly.
261          */
262         image_header_t  *legacy_hdr_os;         /* image header pointer */
263         image_header_t  legacy_hdr_os_copy;     /* header copy */
264         ulong           legacy_hdr_valid;
265
266 #if IMAGE_ENABLE_FIT
267         const char      *fit_uname_cfg; /* configuration node unit name */
268
269         void            *fit_hdr_os;    /* os FIT image header */
270         const char      *fit_uname_os;  /* os subimage node unit name */
271         int             fit_noffset_os; /* os subimage node offset */
272
273         void            *fit_hdr_rd;    /* init ramdisk FIT image header */
274         const char      *fit_uname_rd;  /* init ramdisk subimage node unit name */
275         int             fit_noffset_rd; /* init ramdisk subimage node offset */
276
277         void            *fit_hdr_fdt;   /* FDT blob FIT image header */
278         const char      *fit_uname_fdt; /* FDT blob subimage node unit name */
279         int             fit_noffset_fdt;/* FDT blob subimage node offset */
280
281         void            *fit_hdr_setup; /* x86 setup FIT image header */
282         const char      *fit_uname_setup; /* x86 setup subimage node name */
283         int             fit_noffset_setup;/* x86 setup subimage node offset */
284 #endif
285
286 #ifndef USE_HOSTCC
287         image_info_t    os;             /* os image info */
288         ulong           ep;             /* entry point of OS */
289
290         ulong           rd_start, rd_end;/* ramdisk start/end */
291
292         char            *ft_addr;       /* flat dev tree address */
293         ulong           ft_len;         /* length of flat device tree */
294
295         ulong           initrd_start;
296         ulong           initrd_end;
297         ulong           cmdline_start;
298         ulong           cmdline_end;
299         bd_t            *kbd;
300 #endif
301
302         int             verify;         /* getenv("verify")[0] != 'n' */
303
304 #define BOOTM_STATE_START       (0x00000001)
305 #define BOOTM_STATE_FINDOS      (0x00000002)
306 #define BOOTM_STATE_FINDOTHER   (0x00000004)
307 #define BOOTM_STATE_LOADOS      (0x00000008)
308 #define BOOTM_STATE_RAMDISK     (0x00000010)
309 #define BOOTM_STATE_FDT         (0x00000020)
310 #define BOOTM_STATE_OS_CMDLINE  (0x00000040)
311 #define BOOTM_STATE_OS_BD_T     (0x00000080)
312 #define BOOTM_STATE_OS_PREP     (0x00000100)
313 #define BOOTM_STATE_OS_FAKE_GO  (0x00000200)    /* 'Almost' run the OS */
314 #define BOOTM_STATE_OS_GO       (0x00000400)
315         int             state;
316
317 #ifdef CONFIG_LMB
318         struct lmb      lmb;            /* for memory mgmt */
319 #endif
320 } bootm_headers_t;
321
322 extern bootm_headers_t images;
323
324 /*
325  * Some systems (for example LWMON) have very short watchdog periods;
326  * we must make sure to split long operations like memmove() or
327  * checksum calculations into reasonable chunks.
328  */
329 #ifndef CHUNKSZ
330 #define CHUNKSZ (64 * 1024)
331 #endif
332
333 #ifndef CHUNKSZ_CRC32
334 #define CHUNKSZ_CRC32 (64 * 1024)
335 #endif
336
337 #ifndef CHUNKSZ_MD5
338 #define CHUNKSZ_MD5 (64 * 1024)
339 #endif
340
341 #ifndef CHUNKSZ_SHA1
342 #define CHUNKSZ_SHA1 (64 * 1024)
343 #endif
344
345 #define uimage_to_cpu(x)                be32_to_cpu(x)
346 #define cpu_to_uimage(x)                cpu_to_be32(x)
347
348 /*
349  * Translation table for entries of a specific type; used by
350  * get_table_entry_id() and get_table_entry_name().
351  */
352 typedef struct table_entry {
353         int     id;
354         char    *sname;         /* short (input) name to find table entry */
355         char    *lname;         /* long (output) name to print for messages */
356 } table_entry_t;
357
358 /*
359  * get_table_entry_id() scans the translation table trying to find an
360  * entry that matches the given short name. If a matching entry is
361  * found, it's id is returned to the caller.
362  */
363 int get_table_entry_id(const table_entry_t *table,
364                 const char *table_name, const char *name);
365 /*
366  * get_table_entry_name() scans the translation table trying to find
367  * an entry that matches the given id. If a matching entry is found,
368  * its long name is returned to the caller.
369  */
370 char *get_table_entry_name(const table_entry_t *table, char *msg, int id);
371
372 const char *genimg_get_os_name(uint8_t os);
373
374 /**
375  * genimg_get_os_short_name() - get the short name for an OS
376  *
377  * @param os    OS (IH_OS_...)
378  * @return OS short name, or "unknown" if unknown
379  */
380 const char *genimg_get_os_short_name(uint8_t comp);
381
382 const char *genimg_get_arch_name(uint8_t arch);
383
384 /**
385  * genimg_get_arch_short_name() - get the short name for an architecture
386  *
387  * @param arch  Architecture type (IH_ARCH_...)
388  * @return architecture short name, or "unknown" if unknown
389  */
390 const char *genimg_get_arch_short_name(uint8_t arch);
391
392 const char *genimg_get_type_name(uint8_t type);
393
394 /**
395  * genimg_get_type_short_name() - get the short name for an image type
396  *
397  * @param type  Image type (IH_TYPE_...)
398  * @return image short name, or "unknown" if unknown
399  */
400 const char *genimg_get_type_short_name(uint8_t type);
401
402 const char *genimg_get_comp_name(uint8_t comp);
403
404 /**
405  * genimg_get_comp_short_name() - get the short name for a compression method
406  *
407  * @param comp  compression method (IH_COMP_...)
408  * @return compression method short name, or "unknown" if unknown
409  */
410 const char *genimg_get_comp_short_name(uint8_t comp);
411
412 int genimg_get_os_id(const char *name);
413 int genimg_get_arch_id(const char *name);
414 int genimg_get_type_id(const char *name);
415 int genimg_get_comp_id(const char *name);
416 void genimg_print_size(uint32_t size);
417
418 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || \
419         defined(USE_HOSTCC)
420 #define IMAGE_ENABLE_TIMESTAMP 1
421 #else
422 #define IMAGE_ENABLE_TIMESTAMP 0
423 #endif
424 void genimg_print_time(time_t timestamp);
425
426 /* What to do with a image load address ('load = <> 'in the FIT) */
427 enum fit_load_op {
428         FIT_LOAD_IGNORED,       /* Ignore load address */
429         FIT_LOAD_OPTIONAL,      /* Can be provided, but optional */
430         FIT_LOAD_OPTIONAL_NON_ZERO,     /* Optional, a value of 0 is ignored */
431         FIT_LOAD_REQUIRED,      /* Must be provided */
432 };
433
434 int boot_get_setup(bootm_headers_t *images, uint8_t arch, ulong *setup_start,
435                    ulong *setup_len);
436
437 #ifndef USE_HOSTCC
438 /* Image format types, returned by _get_format() routine */
439 #define IMAGE_FORMAT_INVALID    0x00
440 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
441 #define IMAGE_FORMAT_LEGACY     0x01    /* legacy image_header based format */
442 #endif
443 #define IMAGE_FORMAT_FIT        0x02    /* new, libfdt based format */
444 #define IMAGE_FORMAT_ANDROID    0x03    /* Android boot image */
445
446 ulong genimg_get_kernel_addr_fit(char * const img_addr,
447                                  const char **fit_uname_config,
448                                  const char **fit_uname_kernel);
449 ulong genimg_get_kernel_addr(char * const img_addr);
450 int genimg_get_format(const void *img_addr);
451 int genimg_has_config(bootm_headers_t *images);
452 ulong genimg_get_image(ulong img_addr);
453
454 int boot_get_fpga(int argc, char * const argv[], bootm_headers_t *images,
455                 uint8_t arch, const ulong *ld_start, ulong * const ld_len);
456 int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
457                 uint8_t arch, ulong *rd_start, ulong *rd_end);
458
459 /**
460  * boot_get_loadable - routine to load a list of binaries to memory
461  * @argc: Ignored Argument
462  * @argv: Ignored Argument
463  * @images: pointer to the bootm images structure
464  * @arch: expected architecture for the image
465  * @ld_start: Ignored Argument
466  * @ld_len: Ignored Argument
467  *
468  * boot_get_loadable() will take the given FIT configuration, and look
469  * for a field named "loadables".  Loadables, is a list of elements in
470  * the FIT given as strings.  exe:
471  *   loadables = "linux_kernel@1", "fdt@2";
472  * this function will attempt to parse each string, and load the
473  * corresponding element from the FIT into memory.  Once placed,
474  * no aditional actions are taken.
475  *
476  * @return:
477  *     0, if only valid images or no images are found
478  *     error code, if an error occurs during fit_image_load
479  */
480 int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
481                 uint8_t arch, const ulong *ld_start, ulong * const ld_len);
482 #endif /* !USE_HOSTCC */
483
484 int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
485                        ulong *setup_start, ulong *setup_len);
486
487 /**
488  * fit_image_load() - load an image from a FIT
489  *
490  * This deals with all aspects of loading an image from a FIT, including
491  * selecting the right image based on configuration, verifying it, printing
492  * out progress messages, checking the type/arch/os and optionally copying it
493  * to the right load address.
494  *
495  * The property to look up is defined by image_type.
496  *
497  * @param images        Boot images structure
498  * @param addr          Address of FIT in memory
499  * @param fit_unamep    On entry this is the requested image name
500  *                      (e.g. "kernel@1") or NULL to use the default. On exit
501  *                      points to the selected image name
502  * @param fit_uname_configp     On entry this is the requested configuration
503  *                      name (e.g. "conf@1") or NULL to use the default. On
504  *                      exit points to the selected configuration name.
505  * @param arch          Expected architecture (IH_ARCH_...)
506  * @param image_type    Required image type (IH_TYPE_...). If this is
507  *                      IH_TYPE_KERNEL then we allow IH_TYPE_KERNEL_NOLOAD
508  *                      also.
509  * @param bootstage_id  ID of starting bootstage to use for progress updates.
510  *                      This will be added to the BOOTSTAGE_SUB values when
511  *                      calling bootstage_mark()
512  * @param load_op       Decribes what to do with the load address
513  * @param datap         Returns address of loaded image
514  * @param lenp          Returns length of loaded image
515  * @return node offset of image, or -ve error code on error
516  */
517 int fit_image_load(bootm_headers_t *images, ulong addr,
518                    const char **fit_unamep, const char **fit_uname_configp,
519                    int arch, int image_type, int bootstage_id,
520                    enum fit_load_op load_op, ulong *datap, ulong *lenp);
521
522 #ifndef USE_HOSTCC
523 /**
524  * fit_get_node_from_config() - Look up an image a FIT by type
525  *
526  * This looks in the selected conf@ node (images->fit_uname_cfg) for a
527  * particular image type (e.g. "kernel") and then finds the image that is
528  * referred to.
529  *
530  * For example, for something like:
531  *
532  * images {
533  *      kernel@1 {
534  *              ...
535  *      };
536  * };
537  * configurations {
538  *      conf@1 {
539  *              kernel = "kernel@1";
540  *      };
541  * };
542  *
543  * the function will return the node offset of the kernel@1 node, assuming
544  * that conf@1 is the chosen configuration.
545  *
546  * @param images        Boot images structure
547  * @param prop_name     Property name to look up (FIT_..._PROP)
548  * @param addr          Address of FIT in memory
549  */
550 int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
551                         ulong addr);
552
553 int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
554                  bootm_headers_t *images,
555                  char **of_flat_tree, ulong *of_size);
556 void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
557 int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size);
558
559 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
560                   ulong *initrd_start, ulong *initrd_end);
561 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end);
562 #ifdef CONFIG_SYS_BOOT_GET_KBD
563 int boot_get_kbd(struct lmb *lmb, bd_t **kbd);
564 #endif /* CONFIG_SYS_BOOT_GET_KBD */
565 #endif /* !USE_HOSTCC */
566
567 /*******************************************************************/
568 /* Legacy format specific code (prefixed with image_) */
569 /*******************************************************************/
570 static inline uint32_t image_get_header_size(void)
571 {
572         return (sizeof(image_header_t));
573 }
574
575 #define image_get_hdr_l(f) \
576         static inline uint32_t image_get_##f(const image_header_t *hdr) \
577         { \
578                 return uimage_to_cpu(hdr->ih_##f); \
579         }
580 image_get_hdr_l(magic)          /* image_get_magic */
581 image_get_hdr_l(hcrc)           /* image_get_hcrc */
582 image_get_hdr_l(time)           /* image_get_time */
583 image_get_hdr_l(size)           /* image_get_size */
584 image_get_hdr_l(load)           /* image_get_load */
585 image_get_hdr_l(ep)             /* image_get_ep */
586 image_get_hdr_l(dcrc)           /* image_get_dcrc */
587
588 #define image_get_hdr_b(f) \
589         static inline uint8_t image_get_##f(const image_header_t *hdr) \
590         { \
591                 return hdr->ih_##f; \
592         }
593 image_get_hdr_b(os)             /* image_get_os */
594 image_get_hdr_b(arch)           /* image_get_arch */
595 image_get_hdr_b(type)           /* image_get_type */
596 image_get_hdr_b(comp)           /* image_get_comp */
597
598 static inline char *image_get_name(const image_header_t *hdr)
599 {
600         return (char *)hdr->ih_name;
601 }
602
603 static inline uint32_t image_get_data_size(const image_header_t *hdr)
604 {
605         return image_get_size(hdr);
606 }
607
608 /**
609  * image_get_data - get image payload start address
610  * @hdr: image header
611  *
612  * image_get_data() returns address of the image payload. For single
613  * component images it is image data start. For multi component
614  * images it points to the null terminated table of sub-images sizes.
615  *
616  * returns:
617  *     image payload data start address
618  */
619 static inline ulong image_get_data(const image_header_t *hdr)
620 {
621         return ((ulong)hdr + image_get_header_size());
622 }
623
624 static inline uint32_t image_get_image_size(const image_header_t *hdr)
625 {
626         return (image_get_size(hdr) + image_get_header_size());
627 }
628 static inline ulong image_get_image_end(const image_header_t *hdr)
629 {
630         return ((ulong)hdr + image_get_image_size(hdr));
631 }
632
633 #define image_set_hdr_l(f) \
634         static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
635         { \
636                 hdr->ih_##f = cpu_to_uimage(val); \
637         }
638 image_set_hdr_l(magic)          /* image_set_magic */
639 image_set_hdr_l(hcrc)           /* image_set_hcrc */
640 image_set_hdr_l(time)           /* image_set_time */
641 image_set_hdr_l(size)           /* image_set_size */
642 image_set_hdr_l(load)           /* image_set_load */
643 image_set_hdr_l(ep)             /* image_set_ep */
644 image_set_hdr_l(dcrc)           /* image_set_dcrc */
645
646 #define image_set_hdr_b(f) \
647         static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
648         { \
649                 hdr->ih_##f = val; \
650         }
651 image_set_hdr_b(os)             /* image_set_os */
652 image_set_hdr_b(arch)           /* image_set_arch */
653 image_set_hdr_b(type)           /* image_set_type */
654 image_set_hdr_b(comp)           /* image_set_comp */
655
656 static inline void image_set_name(image_header_t *hdr, const char *name)
657 {
658         strncpy(image_get_name(hdr), name, IH_NMLEN);
659 }
660
661 int image_check_hcrc(const image_header_t *hdr);
662 int image_check_dcrc(const image_header_t *hdr);
663 #ifndef USE_HOSTCC
664 ulong getenv_bootm_low(void);
665 phys_size_t getenv_bootm_size(void);
666 phys_size_t getenv_bootm_mapsize(void);
667 #endif
668 void memmove_wd(void *to, void *from, size_t len, ulong chunksz);
669
670 static inline int image_check_magic(const image_header_t *hdr)
671 {
672         return (image_get_magic(hdr) == IH_MAGIC);
673 }
674 static inline int image_check_type(const image_header_t *hdr, uint8_t type)
675 {
676         return (image_get_type(hdr) == type);
677 }
678 static inline int image_check_arch(const image_header_t *hdr, uint8_t arch)
679 {
680         return (image_get_arch(hdr) == arch);
681 }
682 static inline int image_check_os(const image_header_t *hdr, uint8_t os)
683 {
684         return (image_get_os(hdr) == os);
685 }
686
687 ulong image_multi_count(const image_header_t *hdr);
688 void image_multi_getimg(const image_header_t *hdr, ulong idx,
689                         ulong *data, ulong *len);
690
691 void image_print_contents(const void *hdr);
692
693 #ifndef USE_HOSTCC
694 static inline int image_check_target_arch(const image_header_t *hdr)
695 {
696 #ifndef IH_ARCH_DEFAULT
697 # error "please define IH_ARCH_DEFAULT in your arch asm/u-boot.h"
698 #endif
699         return image_check_arch(hdr, IH_ARCH_DEFAULT);
700 }
701 #endif /* USE_HOSTCC */
702
703 /**
704  * Set up properties in the FDT
705  *
706  * This sets up properties in the FDT that is to be passed to linux.
707  *
708  * @images:     Images information
709  * @blob:       FDT to update
710  * @of_size:    Size of the FDT
711  * @lmb:        Points to logical memory block structure
712  * @return 0 if ok, <0 on failure
713  */
714 int image_setup_libfdt(bootm_headers_t *images, void *blob,
715                        int of_size, struct lmb *lmb);
716
717 /**
718  * Set up the FDT to use for booting a kernel
719  *
720  * This performs ramdisk setup, sets up the FDT if required, and adds
721  * paramters to the FDT if libfdt is available.
722  *
723  * @param images        Images information
724  * @return 0 if ok, <0 on failure
725  */
726 int image_setup_linux(bootm_headers_t *images);
727
728 /**
729  * bootz_setup() - Extract stat and size of a Linux xImage
730  *
731  * @image: Address of image
732  * @start: Returns start address of image
733  * @end : Returns end address of image
734  * @return 0 if OK, 1 if the image was not recognised
735  */
736 int bootz_setup(ulong image, ulong *start, ulong *end);
737
738
739 /*******************************************************************/
740 /* New uImage format specific code (prefixed with fit_) */
741 /*******************************************************************/
742
743 #define FIT_IMAGES_PATH         "/images"
744 #define FIT_CONFS_PATH          "/configurations"
745
746 /* hash/signature node */
747 #define FIT_HASH_NODENAME       "hash"
748 #define FIT_ALGO_PROP           "algo"
749 #define FIT_VALUE_PROP          "value"
750 #define FIT_IGNORE_PROP         "uboot-ignore"
751 #define FIT_SIG_NODENAME        "signature"
752
753 /* image node */
754 #define FIT_DATA_PROP           "data"
755 #define FIT_TIMESTAMP_PROP      "timestamp"
756 #define FIT_DESC_PROP           "description"
757 #define FIT_ARCH_PROP           "arch"
758 #define FIT_TYPE_PROP           "type"
759 #define FIT_OS_PROP             "os"
760 #define FIT_COMP_PROP           "compression"
761 #define FIT_ENTRY_PROP          "entry"
762 #define FIT_LOAD_PROP           "load"
763
764 /* configuration node */
765 #define FIT_KERNEL_PROP         "kernel"
766 #define FIT_RAMDISK_PROP        "ramdisk"
767 #define FIT_FDT_PROP            "fdt"
768 #define FIT_LOADABLE_PROP       "loadables"
769 #define FIT_DEFAULT_PROP        "default"
770 #define FIT_SETUP_PROP          "setup"
771 #define FIT_FPGA_PROP           "fpga"
772
773 #define FIT_MAX_HASH_LEN        HASH_MAX_DIGEST_SIZE
774
775 #if IMAGE_ENABLE_FIT
776 /* cmdline argument format parsing */
777 int fit_parse_conf(const char *spec, ulong addr_curr,
778                 ulong *addr, const char **conf_name);
779 int fit_parse_subimage(const char *spec, ulong addr_curr,
780                 ulong *addr, const char **image_name);
781
782 int fit_get_subimage_count(const void *fit, int images_noffset);
783 void fit_print_contents(const void *fit);
784 void fit_image_print(const void *fit, int noffset, const char *p);
785
786 /**
787  * fit_get_end - get FIT image size
788  * @fit: pointer to the FIT format image header
789  *
790  * returns:
791  *     size of the FIT image (blob) in memory
792  */
793 static inline ulong fit_get_size(const void *fit)
794 {
795         return fdt_totalsize(fit);
796 }
797
798 /**
799  * fit_get_end - get FIT image end
800  * @fit: pointer to the FIT format image header
801  *
802  * returns:
803  *     end address of the FIT image (blob) in memory
804  */
805 ulong fit_get_end(const void *fit);
806
807 /**
808  * fit_get_name - get FIT node name
809  * @fit: pointer to the FIT format image header
810  *
811  * returns:
812  *     NULL, on error
813  *     pointer to node name, on success
814  */
815 static inline const char *fit_get_name(const void *fit_hdr,
816                 int noffset, int *len)
817 {
818         return fdt_get_name(fit_hdr, noffset, len);
819 }
820
821 int fit_get_desc(const void *fit, int noffset, char **desc);
822 int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp);
823
824 int fit_image_get_node(const void *fit, const char *image_uname);
825 int fit_image_get_os(const void *fit, int noffset, uint8_t *os);
826 int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch);
827 int fit_image_get_type(const void *fit, int noffset, uint8_t *type);
828 int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp);
829 int fit_image_get_load(const void *fit, int noffset, ulong *load);
830 int fit_image_get_entry(const void *fit, int noffset, ulong *entry);
831 int fit_image_get_data(const void *fit, int noffset,
832                                 const void **data, size_t *size);
833
834 int fit_image_hash_get_algo(const void *fit, int noffset, char **algo);
835 int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
836                                 int *value_len);
837
838 int fit_set_timestamp(void *fit, int noffset, time_t timestamp);
839
840 /**
841  * fit_add_verification_data() - add verification data to FIT image nodes
842  *
843  * @keydir:     Directory containing keys
844  * @kwydest:    FDT blob to write public key information to
845  * @fit:        Pointer to the FIT format image header
846  * @comment:    Comment to add to signature nodes
847  * @require_keys: Mark all keys as 'required'
848  *
849  * Adds hash values for all component images in the FIT blob.
850  * Hashes are calculated for all component images which have hash subnodes
851  * with algorithm property set to one of the supported hash algorithms.
852  *
853  * Also add signatures if signature nodes are present.
854  *
855  * returns
856  *     0, on success
857  *     libfdt error code, on failure
858  */
859 int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
860                               const char *comment, int require_keys);
861
862 int fit_image_verify(const void *fit, int noffset);
863 int fit_config_verify(const void *fit, int conf_noffset);
864 int fit_all_image_verify(const void *fit);
865 int fit_image_check_os(const void *fit, int noffset, uint8_t os);
866 int fit_image_check_arch(const void *fit, int noffset, uint8_t arch);
867 int fit_image_check_type(const void *fit, int noffset, uint8_t type);
868 int fit_image_check_comp(const void *fit, int noffset, uint8_t comp);
869 int fit_check_format(const void *fit);
870
871 int fit_conf_find_compat(const void *fit, const void *fdt);
872 int fit_conf_get_node(const void *fit, const char *conf_uname);
873
874 /**
875  * fit_conf_get_prop_node() - Get node refered to by a configuration
876  * @fit:        FIT to check
877  * @noffset:    Offset of conf@xxx node to check
878  * @prop_name:  Property to read from the conf node
879  *
880  * The conf@ nodes contain references to other nodes, using properties
881  * like 'kernel = "kernel@1"'. Given such a property name (e.g. "kernel"),
882  * return the offset of the node referred to (e.g. offset of node
883  * "/images/kernel@1".
884  */
885 int fit_conf_get_prop_node(const void *fit, int noffset,
886                 const char *prop_name);
887
888 void fit_conf_print(const void *fit, int noffset, const char *p);
889
890 int fit_check_ramdisk(const void *fit, int os_noffset,
891                 uint8_t arch, int verify);
892
893 int calculate_hash(const void *data, int data_len, const char *algo,
894                         uint8_t *value, int *value_len);
895
896 /*
897  * At present we only support signing on the host, and verification on the
898  * device
899  */
900 #if defined(CONFIG_FIT_SIGNATURE)
901 # ifdef USE_HOSTCC
902 #  define IMAGE_ENABLE_SIGN     1
903 #  define IMAGE_ENABLE_VERIFY   1
904 # include  <openssl/evp.h>
905 #else
906 #  define IMAGE_ENABLE_SIGN     0
907 #  define IMAGE_ENABLE_VERIFY   1
908 # endif
909 #else
910 # define IMAGE_ENABLE_SIGN      0
911 # define IMAGE_ENABLE_VERIFY    0
912 #endif
913
914 #ifdef USE_HOSTCC
915 void *image_get_host_blob(void);
916 void image_set_host_blob(void *host_blob);
917 # define gd_fdt_blob()          image_get_host_blob()
918 #else
919 # define gd_fdt_blob()          (gd->fdt_blob)
920 #endif
921
922 #ifdef CONFIG_FIT_BEST_MATCH
923 #define IMAGE_ENABLE_BEST_MATCH 1
924 #else
925 #define IMAGE_ENABLE_BEST_MATCH 0
926 #endif
927
928 /* Information passed to the signing routines */
929 struct image_sign_info {
930         const char *keydir;             /* Directory conaining keys */
931         const char *keyname;            /* Name of key to use */
932         void *fit;                      /* Pointer to FIT blob */
933         int node_offset;                /* Offset of signature node */
934         struct image_sig_algo *algo;    /* Algorithm information */
935         const void *fdt_blob;           /* FDT containing public keys */
936         int required_keynode;           /* Node offset of key to use: -1=any */
937         const char *require_keys;       /* Value for 'required' property */
938 };
939 #endif /* Allow struct image_region to always be defined for rsa.h */
940
941 /* A part of an image, used for hashing */
942 struct image_region {
943         const void *data;
944         int size;
945 };
946
947 #if IMAGE_ENABLE_FIT
948
949 #if IMAGE_ENABLE_VERIFY
950 # include <u-boot/rsa-checksum.h>
951 #endif
952 struct checksum_algo {
953         const char *name;
954         const int checksum_len;
955         const int pad_len;
956 #if IMAGE_ENABLE_SIGN
957         const EVP_MD *(*calculate_sign)(void);
958 #endif
959         int (*calculate)(const char *name,
960                          const struct image_region region[],
961                          int region_count, uint8_t *checksum);
962         const uint8_t *rsa_padding;
963 };
964
965 struct image_sig_algo {
966         const char *name;               /* Name of algorithm */
967
968         /**
969          * sign() - calculate and return signature for given input data
970          *
971          * @info:       Specifies key and FIT information
972          * @data:       Pointer to the input data
973          * @data_len:   Data length
974          * @sigp:       Set to an allocated buffer holding the signature
975          * @sig_len:    Set to length of the calculated hash
976          *
977          * This computes input data signature according to selected algorithm.
978          * Resulting signature value is placed in an allocated buffer, the
979          * pointer is returned as *sigp. The length of the calculated
980          * signature is returned via the sig_len pointer argument. The caller
981          * should free *sigp.
982          *
983          * @return: 0, on success, -ve on error
984          */
985         int (*sign)(struct image_sign_info *info,
986                     const struct image_region region[],
987                     int region_count, uint8_t **sigp, uint *sig_len);
988
989         /**
990          * add_verify_data() - Add verification information to FDT
991          *
992          * Add public key information to the FDT node, suitable for
993          * verification at run-time. The information added depends on the
994          * algorithm being used.
995          *
996          * @info:       Specifies key and FIT information
997          * @keydest:    Destination FDT blob for public key data
998          * @return: 0, on success, -ve on error
999          */
1000         int (*add_verify_data)(struct image_sign_info *info, void *keydest);
1001
1002         /**
1003          * verify() - Verify a signature against some data
1004          *
1005          * @info:       Specifies key and FIT information
1006          * @data:       Pointer to the input data
1007          * @data_len:   Data length
1008          * @sig:        Signature
1009          * @sig_len:    Number of bytes in signature
1010          * @return 0 if verified, -ve on error
1011          */
1012         int (*verify)(struct image_sign_info *info,
1013                       const struct image_region region[], int region_count,
1014                       uint8_t *sig, uint sig_len);
1015
1016         /* pointer to checksum algorithm */
1017         struct checksum_algo *checksum;
1018 };
1019
1020 /**
1021  * image_get_sig_algo() - Look up a signature algortihm
1022  *
1023  * @param name          Name of algorithm
1024  * @return pointer to algorithm information, or NULL if not found
1025  */
1026 struct image_sig_algo *image_get_sig_algo(const char *name);
1027
1028 /**
1029  * fit_image_verify_required_sigs() - Verify signatures marked as 'required'
1030  *
1031  * @fit:                FIT to check
1032  * @image_noffset:      Offset of image node to check
1033  * @data:               Image data to check
1034  * @size:               Size of image data
1035  * @sig_blob:           FDT containing public keys
1036  * @no_sigsp:           Returns 1 if no signatures were required, and
1037  *                      therefore nothing was checked. The caller may wish
1038  *                      to fall back to other mechanisms, or refuse to
1039  *                      boot.
1040  * @return 0 if all verified ok, <0 on error
1041  */
1042 int fit_image_verify_required_sigs(const void *fit, int image_noffset,
1043                 const char *data, size_t size, const void *sig_blob,
1044                 int *no_sigsp);
1045
1046 /**
1047  * fit_image_check_sig() - Check a single image signature node
1048  *
1049  * @fit:                FIT to check
1050  * @noffset:            Offset of signature node to check
1051  * @data:               Image data to check
1052  * @size:               Size of image data
1053  * @required_keynode:   Offset in the control FDT of the required key node,
1054  *                      if any. If this is given, then the image wil not
1055  *                      pass verification unless that key is used. If this is
1056  *                      -1 then any signature will do.
1057  * @err_msgp:           In the event of an error, this will be pointed to a
1058  *                      help error string to display to the user.
1059  * @return 0 if all verified ok, <0 on error
1060  */
1061 int fit_image_check_sig(const void *fit, int noffset, const void *data,
1062                 size_t size, int required_keynode, char **err_msgp);
1063
1064 /**
1065  * fit_region_make_list() - Make a list of regions to hash
1066  *
1067  * Given a list of FIT regions (offset, size) provided by libfdt, create
1068  * a list of regions (void *, size) for use by the signature creationg
1069  * and verification code.
1070  *
1071  * @fit:                FIT image to process
1072  * @fdt_regions:        Regions as returned by libfdt
1073  * @count:              Number of regions returned by libfdt
1074  * @region:             Place to put list of regions (NULL to allocate it)
1075  * @return pointer to list of regions, or NULL if out of memory
1076  */
1077 struct image_region *fit_region_make_list(const void *fit,
1078                 struct fdt_region *fdt_regions, int count,
1079                 struct image_region *region);
1080
1081 static inline int fit_image_check_target_arch(const void *fdt, int node)
1082 {
1083 #ifndef USE_HOSTCC
1084         return fit_image_check_arch(fdt, node, IH_ARCH_DEFAULT);
1085 #else
1086         return 0;
1087 #endif
1088 }
1089
1090 #ifdef CONFIG_FIT_VERBOSE
1091 #define fit_unsupported(msg)    printf("! %s:%d " \
1092                                 "FIT images not supported for '%s'\n", \
1093                                 __FILE__, __LINE__, (msg))
1094
1095 #define fit_unsupported_reset(msg)      printf("! %s:%d " \
1096                                 "FIT images not supported for '%s' " \
1097                                 "- must reset board to recover!\n", \
1098                                 __FILE__, __LINE__, (msg))
1099 #else
1100 #define fit_unsupported(msg)
1101 #define fit_unsupported_reset(msg)
1102 #endif /* CONFIG_FIT_VERBOSE */
1103 #endif /* CONFIG_FIT */
1104
1105 #if defined(CONFIG_ANDROID_BOOT_IMAGE)
1106 struct andr_img_hdr;
1107 int android_image_check_header(const struct andr_img_hdr *hdr);
1108 int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
1109                              ulong *os_data, ulong *os_len);
1110 int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
1111                               ulong *rd_data, ulong *rd_len);
1112 ulong android_image_get_end(const struct andr_img_hdr *hdr);
1113 ulong android_image_get_kload(const struct andr_img_hdr *hdr);
1114
1115 #endif /* CONFIG_ANDROID_BOOT_IMAGE */
1116
1117 /**
1118  * board_fit_config_name_match() - Check for a matching board name
1119  *
1120  * This is used when SPL loads a FIT containing multiple device tree files
1121  * and wants to work out which one to use. The description of each one is
1122  * passed to this function. The description comes from the 'description' field
1123  * in each (FDT) image node.
1124  *
1125  * @name: Device tree description
1126  * @return 0 if this device tree should be used, non-zero to try the next
1127  */
1128 int board_fit_config_name_match(const char *name);
1129
1130 #endif  /* __IMAGE_H__ */