xref: /rk3399_rockchip-uboot/include/image.h (revision db1b79b886f085b5af09db9378f5c53417c3ecde)
15b1d7137Swdenk /*
2b97a2a0aSMarian Balakowicz  * (C) Copyright 2008 Semihalf
3b97a2a0aSMarian Balakowicz  *
4f08abe31SWolfgang Denk  * (C) Copyright 2000-2005
55b1d7137Swdenk  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
65b1d7137Swdenk  *
71a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
8f08abe31SWolfgang Denk  ********************************************************************
9f08abe31SWolfgang Denk  * NOTE: This header file defines an interface to U-Boot. Including
10f08abe31SWolfgang Denk  * this (unmodified) header file in another file is considered normal
11f08abe31SWolfgang Denk  * use of U-Boot, and does *not* fall under the heading of "derived
12f08abe31SWolfgang Denk  * work".
13f08abe31SWolfgang Denk  ********************************************************************
145b1d7137Swdenk  */
155b1d7137Swdenk 
165b1d7137Swdenk #ifndef __IMAGE_H__
175b1d7137Swdenk #define __IMAGE_H__
185b1d7137Swdenk 
1937566090SMike Frysinger #include "compiler.h"
202f220500SKim Phillips #include <asm/byteorder.h>
2137566090SMike Frysinger 
2213d06981SSimon Glass /* Define this to avoid #ifdefs later on */
2313d06981SSimon Glass struct lmb;
2413d06981SSimon Glass 
2537566090SMike Frysinger #ifdef USE_HOSTCC
2626e355d1SJörg Krause #include <sys/types.h>
279d25438fSBartlomiej Sieka 
289d25438fSBartlomiej Sieka /* new uImage format support enabled on host */
2973223f0eSSimon Glass #define IMAGE_ENABLE_FIT	1
30aa34fbc0SSimon Glass #define IMAGE_ENABLE_OF_LIBFDT	1
319d25438fSBartlomiej Sieka #define CONFIG_FIT_VERBOSE	1 /* enable fit_format_{error,warning}() */
329d25438fSBartlomiej Sieka 
33ab9efc66SSimon Glass #define IMAGE_ENABLE_IGNORE	0
341fe7d938SSimon Glass #define IMAGE_INDENT_STRING	""
35ab9efc66SSimon Glass 
36def0819eSWolfgang Denk #else
37def0819eSWolfgang Denk 
38def0819eSWolfgang Denk #include <lmb.h>
39def0819eSWolfgang Denk #include <asm/u-boot.h>
405daa1c18SMike Frysinger #include <command.h>
41def0819eSWolfgang Denk 
42ab9efc66SSimon Glass /* Take notice of the 'ignore' property for hashes */
43ab9efc66SSimon Glass #define IMAGE_ENABLE_IGNORE	1
441fe7d938SSimon Glass #define IMAGE_INDENT_STRING	"   "
45ab9efc66SSimon Glass 
4673223f0eSSimon Glass #define IMAGE_ENABLE_FIT	CONFIG_IS_ENABLED(FIT)
47aa34fbc0SSimon Glass #define IMAGE_ENABLE_OF_LIBFDT	CONFIG_IS_ENABLED(OF_LIBFDT)
4873223f0eSSimon Glass 
499d25438fSBartlomiej Sieka #endif /* USE_HOSTCC */
50d5934ad7SMarian Balakowicz 
5173223f0eSSimon Glass #if IMAGE_ENABLE_FIT
521de7bb4fSMichael van der Westhuizen #include <hash.h>
535dfb5213SMarian Balakowicz #include <libfdt.h>
545dfb5213SMarian Balakowicz #include <fdt_support.h>
5587ebee39SSimon Glass # ifdef CONFIG_SPL_BUILD
566f41751fSMasahiro Yamada #  ifdef CONFIG_SPL_CRC32_SUPPORT
576f41751fSMasahiro Yamada #   define IMAGE_ENABLE_CRC32	1
586f41751fSMasahiro Yamada #  endif
596f41751fSMasahiro Yamada #  ifdef CONFIG_SPL_MD5_SUPPORT
606f41751fSMasahiro Yamada #   define IMAGE_ENABLE_MD5	1
616f41751fSMasahiro Yamada #  endif
626f41751fSMasahiro Yamada #  ifdef CONFIG_SPL_SHA1_SUPPORT
636f41751fSMasahiro Yamada #   define IMAGE_ENABLE_SHA1	1
646f41751fSMasahiro Yamada #  endif
656f41751fSMasahiro Yamada #  ifdef CONFIG_SPL_SHA256_SUPPORT
666f41751fSMasahiro Yamada #   define IMAGE_ENABLE_SHA256	1
676f41751fSMasahiro Yamada #  endif
6887ebee39SSimon Glass # else
6987ebee39SSimon Glass #  define CONFIG_CRC32		/* FIT images need CRC32 support */
7087ebee39SSimon Glass #  define CONFIG_MD5		/* and MD5 */
71dac4d7e8SMike Frysinger #  define CONFIG_SHA1		/* and SHA1 */
722842c1c2SHeiko Schocher #  define CONFIG_SHA256		/* and SHA256 */
7387ebee39SSimon Glass #  define IMAGE_ENABLE_CRC32	1
7487ebee39SSimon Glass #  define IMAGE_ENABLE_MD5	1
7587ebee39SSimon Glass #  define IMAGE_ENABLE_SHA1	1
762842c1c2SHeiko Schocher #  define IMAGE_ENABLE_SHA256	1
7787ebee39SSimon Glass # endif
786f41751fSMasahiro Yamada 
796f41751fSMasahiro Yamada #ifdef CONFIG_FIT_DISABLE_SHA256
806f41751fSMasahiro Yamada #undef CONFIG_SHA256
816f41751fSMasahiro Yamada #undef IMAGE_ENABLE_SHA256
826f41751fSMasahiro Yamada #endif
836f41751fSMasahiro Yamada 
846f41751fSMasahiro Yamada #ifndef IMAGE_ENABLE_CRC32
856f41751fSMasahiro Yamada #define IMAGE_ENABLE_CRC32	0
866f41751fSMasahiro Yamada #endif
876f41751fSMasahiro Yamada 
886f41751fSMasahiro Yamada #ifndef IMAGE_ENABLE_MD5
896f41751fSMasahiro Yamada #define IMAGE_ENABLE_MD5	0
906f41751fSMasahiro Yamada #endif
916f41751fSMasahiro Yamada 
926f41751fSMasahiro Yamada #ifndef IMAGE_ENABLE_SHA1
936f41751fSMasahiro Yamada #define IMAGE_ENABLE_SHA1	0
946f41751fSMasahiro Yamada #endif
956f41751fSMasahiro Yamada 
966f41751fSMasahiro Yamada #ifndef IMAGE_ENABLE_SHA256
976f41751fSMasahiro Yamada #define IMAGE_ENABLE_SHA256	0
986f41751fSMasahiro Yamada #endif
996f41751fSMasahiro Yamada 
10073223f0eSSimon Glass #endif /* IMAGE_ENABLE_FIT */
10113d06981SSimon Glass 
1026f41751fSMasahiro Yamada #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1036f41751fSMasahiro Yamada # define IMAGE_ENABLE_RAMDISK_HIGH	1
1046f41751fSMasahiro Yamada #else
1056f41751fSMasahiro Yamada # define IMAGE_ENABLE_RAMDISK_HIGH	0
1066f41751fSMasahiro Yamada #endif
1076f41751fSMasahiro Yamada 
1086f41751fSMasahiro Yamada #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1096f41751fSMasahiro Yamada # define IMAGE_BOOT_GET_CMDLINE		1
1106f41751fSMasahiro Yamada #else
1116f41751fSMasahiro Yamada # define IMAGE_BOOT_GET_CMDLINE		0
1126f41751fSMasahiro Yamada #endif
1136f41751fSMasahiro Yamada 
1146f41751fSMasahiro Yamada #ifdef CONFIG_OF_BOARD_SETUP
1156f41751fSMasahiro Yamada # define IMAGE_OF_BOARD_SETUP		1
1166f41751fSMasahiro Yamada #else
1176f41751fSMasahiro Yamada # define IMAGE_OF_BOARD_SETUP		0
1186f41751fSMasahiro Yamada #endif
1196f41751fSMasahiro Yamada 
1206f41751fSMasahiro Yamada #ifdef CONFIG_OF_SYSTEM_SETUP
1216f41751fSMasahiro Yamada # define IMAGE_OF_SYSTEM_SETUP	1
1226f41751fSMasahiro Yamada #else
1236f41751fSMasahiro Yamada # define IMAGE_OF_SYSTEM_SETUP	0
1246f41751fSMasahiro Yamada #endif
125c654b517SSimon Glass 
12656d7ab74SSimon Glass enum ih_category {
12756d7ab74SSimon Glass 	IH_ARCH,
12856d7ab74SSimon Glass 	IH_COMP,
12956d7ab74SSimon Glass 	IH_OS,
13056d7ab74SSimon Glass 	IH_TYPE,
13156d7ab74SSimon Glass 
13256d7ab74SSimon Glass 	IH_COUNT,
13356d7ab74SSimon Glass };
13456d7ab74SSimon Glass 
1355b1d7137Swdenk /*
1365b1d7137Swdenk  * Operating System Codes
137ff87b081SMasahiro Yamada  *
138ff87b081SMasahiro Yamada  * The following are exposed to uImage header.
139ff87b081SMasahiro Yamada  * Do not change values for backward compatibility.
1405b1d7137Swdenk  */
141555f45d8SSimon Glass enum {
142555f45d8SSimon Glass 	IH_OS_INVALID		= 0,	/* Invalid OS	*/
143555f45d8SSimon Glass 	IH_OS_OPENBSD,			/* OpenBSD	*/
144555f45d8SSimon Glass 	IH_OS_NETBSD,			/* NetBSD	*/
145555f45d8SSimon Glass 	IH_OS_FREEBSD,			/* FreeBSD	*/
146555f45d8SSimon Glass 	IH_OS_4_4BSD,			/* 4.4BSD	*/
147555f45d8SSimon Glass 	IH_OS_LINUX,			/* Linux	*/
148555f45d8SSimon Glass 	IH_OS_SVR4,			/* SVR4		*/
149555f45d8SSimon Glass 	IH_OS_ESIX,			/* Esix		*/
150555f45d8SSimon Glass 	IH_OS_SOLARIS,			/* Solaris	*/
151555f45d8SSimon Glass 	IH_OS_IRIX,			/* Irix		*/
152555f45d8SSimon Glass 	IH_OS_SCO,			/* SCO		*/
153555f45d8SSimon Glass 	IH_OS_DELL,			/* Dell		*/
154555f45d8SSimon Glass 	IH_OS_NCR,			/* NCR		*/
155555f45d8SSimon Glass 	IH_OS_LYNXOS,			/* LynxOS	*/
156555f45d8SSimon Glass 	IH_OS_VXWORKS,			/* VxWorks	*/
157555f45d8SSimon Glass 	IH_OS_PSOS,			/* pSOS		*/
158555f45d8SSimon Glass 	IH_OS_QNX,			/* QNX		*/
159555f45d8SSimon Glass 	IH_OS_U_BOOT,			/* Firmware	*/
160555f45d8SSimon Glass 	IH_OS_RTEMS,			/* RTEMS	*/
161555f45d8SSimon Glass 	IH_OS_ARTOS,			/* ARTOS	*/
162555f45d8SSimon Glass 	IH_OS_UNITY,			/* Unity OS	*/
163555f45d8SSimon Glass 	IH_OS_INTEGRITY,		/* INTEGRITY	*/
164555f45d8SSimon Glass 	IH_OS_OSE,			/* OSE		*/
165555f45d8SSimon Glass 	IH_OS_PLAN9,			/* Plan 9	*/
166555f45d8SSimon Glass 	IH_OS_OPENRTOS,		/* OpenRTOS	*/
167555f45d8SSimon Glass 
168555f45d8SSimon Glass 	IH_OS_COUNT,
169555f45d8SSimon Glass };
1705b1d7137Swdenk 
1715b1d7137Swdenk /*
1725b1d7137Swdenk  * CPU Architecture Codes (supported by Linux)
173ff87b081SMasahiro Yamada  *
174ff87b081SMasahiro Yamada  * The following are exposed to uImage header.
175ff87b081SMasahiro Yamada  * Do not change values for backward compatibility.
1765b1d7137Swdenk  */
177555f45d8SSimon Glass enum {
178555f45d8SSimon Glass 	IH_ARCH_INVALID		= 0,	/* Invalid CPU	*/
179555f45d8SSimon Glass 	IH_ARCH_ALPHA,			/* Alpha	*/
180555f45d8SSimon Glass 	IH_ARCH_ARM,			/* ARM		*/
181555f45d8SSimon Glass 	IH_ARCH_I386,			/* Intel x86	*/
182555f45d8SSimon Glass 	IH_ARCH_IA64,			/* IA64		*/
183555f45d8SSimon Glass 	IH_ARCH_MIPS,			/* MIPS		*/
184555f45d8SSimon Glass 	IH_ARCH_MIPS64,			/* MIPS	 64 Bit */
185555f45d8SSimon Glass 	IH_ARCH_PPC,			/* PowerPC	*/
186555f45d8SSimon Glass 	IH_ARCH_S390,			/* IBM S390	*/
187555f45d8SSimon Glass 	IH_ARCH_SH,			/* SuperH	*/
188555f45d8SSimon Glass 	IH_ARCH_SPARC,			/* Sparc	*/
189555f45d8SSimon Glass 	IH_ARCH_SPARC64,		/* Sparc 64 Bit */
190555f45d8SSimon Glass 	IH_ARCH_M68K,			/* M68K		*/
191ff87b081SMasahiro Yamada 	IH_ARCH_NIOS,			/* Nios-32	*/
192555f45d8SSimon Glass 	IH_ARCH_MICROBLAZE,		/* MicroBlaze   */
193555f45d8SSimon Glass 	IH_ARCH_NIOS2,			/* Nios-II	*/
194555f45d8SSimon Glass 	IH_ARCH_BLACKFIN,		/* Blackfin	*/
195555f45d8SSimon Glass 	IH_ARCH_AVR32,			/* AVR32	*/
196555f45d8SSimon Glass 	IH_ARCH_ST200,			/* STMicroelectronics ST200  */
197555f45d8SSimon Glass 	IH_ARCH_SANDBOX,		/* Sandbox architecture (test only) */
198555f45d8SSimon Glass 	IH_ARCH_NDS32,			/* ANDES Technology - NDS32  */
199555f45d8SSimon Glass 	IH_ARCH_OPENRISC,		/* OpenRISC 1000  */
200555f45d8SSimon Glass 	IH_ARCH_ARM64,			/* ARM64	*/
201555f45d8SSimon Glass 	IH_ARCH_ARC,			/* Synopsys DesignWare ARC */
202555f45d8SSimon Glass 	IH_ARCH_X86_64,			/* AMD x86_64, Intel and Via */
203de5e5ceaSChris Zankel 	IH_ARCH_XTENSA,			/* Xtensa	*/
204555f45d8SSimon Glass 
205555f45d8SSimon Glass 	IH_ARCH_COUNT,
206555f45d8SSimon Glass };
2075b1d7137Swdenk 
2085b1d7137Swdenk /*
2095b1d7137Swdenk  * Image Types
2105b1d7137Swdenk  *
2115b1d7137Swdenk  * "Standalone Programs" are directly runnable in the environment
2125b1d7137Swdenk  *	provided by U-Boot; it is expected that (if they behave
2135b1d7137Swdenk  *	well) you can continue to work in U-Boot after return from
2145b1d7137Swdenk  *	the Standalone Program.
2155b1d7137Swdenk  * "OS Kernel Images" are usually images of some Embedded OS which
2165b1d7137Swdenk  *	will take over control completely. Usually these programs
2175b1d7137Swdenk  *	will install their own set of exception handlers, device
2185b1d7137Swdenk  *	drivers, set up the MMU, etc. - this means, that you cannot
2195b1d7137Swdenk  *	expect to re-enter U-Boot except by resetting the CPU.
2205b1d7137Swdenk  * "RAMDisk Images" are more or less just data blocks, and their
2215b1d7137Swdenk  *	parameters (address, size) are passed to an OS kernel that is
2225b1d7137Swdenk  *	being started.
2235b1d7137Swdenk  * "Multi-File Images" contain several images, typically an OS
2245b1d7137Swdenk  *	(Linux) kernel image and one or more data images like
2255b1d7137Swdenk  *	RAMDisks. This construct is useful for instance when you want
2265b1d7137Swdenk  *	to boot over the network using BOOTP etc., where the boot
2275b1d7137Swdenk  *	server provides just a single image file, but you want to get
2285b1d7137Swdenk  *	for instance an OS kernel and a RAMDisk image.
2295b1d7137Swdenk  *
2305b1d7137Swdenk  *	"Multi-File Images" start with a list of image sizes, each
2315b1d7137Swdenk  *	image size (in bytes) specified by an "uint32_t" in network
2325b1d7137Swdenk  *	byte order. This list is terminated by an "(uint32_t)0".
2335b1d7137Swdenk  *	Immediately after the terminating 0 follow the images, one by
2345b1d7137Swdenk  *	one, all aligned on "uint32_t" boundaries (size rounded up to
235e1599e83Swdenk  *	a multiple of 4 bytes - except for the last file).
2365b1d7137Swdenk  *
2375b1d7137Swdenk  * "Firmware Images" are binary images containing firmware (like
2385b1d7137Swdenk  *	U-Boot or FPGA images) which usually will be programmed to
2395b1d7137Swdenk  *	flash memory.
2405b1d7137Swdenk  *
2415b1d7137Swdenk  * "Script files" are command sequences that will be executed by
2425b1d7137Swdenk  *	U-Boot's command interpreter; this feature is especially
2435b1d7137Swdenk  *	useful when you configure U-Boot to use a real shell (hush)
24427b207fdSwdenk  *	as command interpreter (=> Shell Scripts).
245ff87b081SMasahiro Yamada  *
246ff87b081SMasahiro Yamada  * The following are exposed to uImage header.
247ff87b081SMasahiro Yamada  * Do not change values for backward compatibility.
2485b1d7137Swdenk  */
2495b1d7137Swdenk 
250555f45d8SSimon Glass enum {
251555f45d8SSimon Glass 	IH_TYPE_INVALID		= 0,	/* Invalid Image		*/
252555f45d8SSimon Glass 	IH_TYPE_STANDALONE,		/* Standalone Program		*/
253555f45d8SSimon Glass 	IH_TYPE_KERNEL,			/* OS Kernel Image		*/
254555f45d8SSimon Glass 	IH_TYPE_RAMDISK,		/* RAMDisk Image		*/
255555f45d8SSimon Glass 	IH_TYPE_MULTI,			/* Multi-File Image		*/
256555f45d8SSimon Glass 	IH_TYPE_FIRMWARE,		/* Firmware Image		*/
257555f45d8SSimon Glass 	IH_TYPE_SCRIPT,			/* Script file			*/
258555f45d8SSimon Glass 	IH_TYPE_FILESYSTEM,		/* Filesystem Image (any type)	*/
259555f45d8SSimon Glass 	IH_TYPE_FLATDT,			/* Binary Flat Device Tree Blob	*/
260555f45d8SSimon Glass 	IH_TYPE_KWBIMAGE,		/* Kirkwood Boot Image		*/
261555f45d8SSimon Glass 	IH_TYPE_IMXIMAGE,		/* Freescale IMXBoot Image	*/
262555f45d8SSimon Glass 	IH_TYPE_UBLIMAGE,		/* Davinci UBL Image		*/
263555f45d8SSimon Glass 	IH_TYPE_OMAPIMAGE,		/* TI OMAP Config Header Image	*/
264555f45d8SSimon Glass 	IH_TYPE_AISIMAGE,		/* TI Davinci AIS Image		*/
265555f45d8SSimon Glass 	/* OS Kernel Image, can run from any load address */
266555f45d8SSimon Glass 	IH_TYPE_KERNEL_NOLOAD,
267555f45d8SSimon Glass 	IH_TYPE_PBLIMAGE,		/* Freescale PBL Boot Image	*/
268555f45d8SSimon Glass 	IH_TYPE_MXSIMAGE,		/* Freescale MXSBoot Image	*/
269555f45d8SSimon Glass 	IH_TYPE_GPIMAGE,		/* TI Keystone GPHeader Image	*/
270555f45d8SSimon Glass 	IH_TYPE_ATMELIMAGE,		/* ATMEL ROM bootable Image	*/
271555f45d8SSimon Glass 	IH_TYPE_SOCFPGAIMAGE,		/* Altera SOCFPGA Preloader	*/
272555f45d8SSimon Glass 	IH_TYPE_X86_SETUP,		/* x86 setup.bin Image		*/
273555f45d8SSimon Glass 	IH_TYPE_LPC32XXIMAGE,		/* x86 setup.bin Image		*/
274555f45d8SSimon Glass 	IH_TYPE_LOADABLE,		/* A list of typeless images	*/
275555f45d8SSimon Glass 	IH_TYPE_RKIMAGE,		/* Rockchip Boot Image		*/
276555f45d8SSimon Glass 	IH_TYPE_RKSD,			/* Rockchip SD card		*/
277555f45d8SSimon Glass 	IH_TYPE_RKSPI,			/* Rockchip SPI image		*/
278555f45d8SSimon Glass 	IH_TYPE_ZYNQIMAGE,		/* Xilinx Zynq Boot Image */
279555f45d8SSimon Glass 	IH_TYPE_ZYNQMPIMAGE,		/* Xilinx ZynqMP Boot Image */
280555f45d8SSimon Glass 	IH_TYPE_FPGA,			/* FPGA Image */
281ed0c2c0aSAlbert ARIBAUD \(3ADEV\) 	IH_TYPE_VYBRIDIMAGE,	/* VYBRID .vyb Image */
2827e719ee7SAndrew F. Davis 	IH_TYPE_TEE,            /* Trusted Execution Environment OS Image */
283d21bd69bSSven Ebenfeld 	IH_TYPE_FIRMWARE_IVT,		/* Firmware Image with HABv4 IVT */
2845b1d7137Swdenk 
285555f45d8SSimon Glass 	IH_TYPE_COUNT,			/* Number of image types */
286555f45d8SSimon Glass };
2875b9d44dfSSimon Glass 
2885b1d7137Swdenk /*
2895b1d7137Swdenk  * Compression Types
290ff87b081SMasahiro Yamada  *
291ff87b081SMasahiro Yamada  * The following are exposed to uImage header.
292ff87b081SMasahiro Yamada  * Do not change values for backward compatibility.
2935b1d7137Swdenk  */
294555f45d8SSimon Glass enum {
295555f45d8SSimon Glass 	IH_COMP_NONE		= 0,	/*  No	 Compression Used	*/
296555f45d8SSimon Glass 	IH_COMP_GZIP,			/* gzip	 Compression Used	*/
297555f45d8SSimon Glass 	IH_COMP_BZIP2,			/* bzip2 Compression Used	*/
298555f45d8SSimon Glass 	IH_COMP_LZMA,			/* lzma  Compression Used	*/
299555f45d8SSimon Glass 	IH_COMP_LZO,			/* lzo   Compression Used	*/
300555f45d8SSimon Glass 	IH_COMP_LZ4,			/* lz4   Compression Used	*/
301555f45d8SSimon Glass 
302555f45d8SSimon Glass 	IH_COMP_COUNT,
303555f45d8SSimon Glass };
3045b1d7137Swdenk 
3055b1d7137Swdenk #define IH_MAGIC	0x27051956	/* Image Magic Number		*/
3065b1d7137Swdenk #define IH_NMLEN		32	/* Image Name Length		*/
3075b1d7137Swdenk 
3081411fb37SFabio Estevam /* Reused from common.h */
3091411fb37SFabio Estevam #define ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
3101411fb37SFabio Estevam 
3115b1d7137Swdenk /*
3129a4daad0SMarian Balakowicz  * Legacy format image header,
3139a4daad0SMarian Balakowicz  * all data in network byte order (aka natural aka bigendian).
3145b1d7137Swdenk  */
3155b1d7137Swdenk typedef struct image_header {
3162f220500SKim Phillips 	__be32		ih_magic;	/* Image Header Magic Number	*/
3172f220500SKim Phillips 	__be32		ih_hcrc;	/* Image Header CRC Checksum	*/
3182f220500SKim Phillips 	__be32		ih_time;	/* Image Creation Timestamp	*/
3192f220500SKim Phillips 	__be32		ih_size;	/* Image Data Size		*/
3202f220500SKim Phillips 	__be32		ih_load;	/* Data	 Load  Address		*/
3212f220500SKim Phillips 	__be32		ih_ep;		/* Entry Point Address		*/
3222f220500SKim Phillips 	__be32		ih_dcrc;	/* Image Data CRC Checksum	*/
3235b1d7137Swdenk 	uint8_t		ih_os;		/* Operating System		*/
3245b1d7137Swdenk 	uint8_t		ih_arch;	/* CPU architecture		*/
3255b1d7137Swdenk 	uint8_t		ih_type;	/* Image Type			*/
3265b1d7137Swdenk 	uint8_t		ih_comp;	/* Compression Type		*/
3275b1d7137Swdenk 	uint8_t		ih_name[IH_NMLEN];	/* Image Name		*/
3285b1d7137Swdenk } image_header_t;
3295b1d7137Swdenk 
330396f635bSKumar Gala typedef struct image_info {
331396f635bSKumar Gala 	ulong		start, end;		/* start/end of blob */
332396f635bSKumar Gala 	ulong		image_start, image_len; /* start of image within blob, len of image */
333396f635bSKumar Gala 	ulong		load;			/* load addr for the image */
334396f635bSKumar Gala 	uint8_t		comp, type, os;		/* compression, type of image, os type */
33590268b87SSimon Glass 	uint8_t		arch;			/* CPU architecture */
336396f635bSKumar Gala } image_info_t;
337396f635bSKumar Gala 
338559316faSMarian Balakowicz /*
339d5934ad7SMarian Balakowicz  * Legacy and FIT format headers used by do_bootm() and do_bootm_<os>()
340d5934ad7SMarian Balakowicz  * routines.
341d5934ad7SMarian Balakowicz  */
342d5934ad7SMarian Balakowicz typedef struct bootm_headers {
343d5934ad7SMarian Balakowicz 	/*
344d5934ad7SMarian Balakowicz 	 * Legacy os image header, if it is a multi component image
3459a4daad0SMarian Balakowicz 	 * then boot_get_ramdisk() and get_fdt() will attempt to get
346d5934ad7SMarian Balakowicz 	 * data from second and third component accordingly.
347d5934ad7SMarian Balakowicz 	 */
348cb1c4896SMarian Balakowicz 	image_header_t	*legacy_hdr_os;		/* image header pointer */
349cb1c4896SMarian Balakowicz 	image_header_t	legacy_hdr_os_copy;	/* header copy */
350d5934ad7SMarian Balakowicz 	ulong		legacy_hdr_valid;
351d5934ad7SMarian Balakowicz 
35273223f0eSSimon Glass #if IMAGE_ENABLE_FIT
353f773bea8SMarian Balakowicz 	const char	*fit_uname_cfg;	/* configuration node unit name */
354f773bea8SMarian Balakowicz 
355d5934ad7SMarian Balakowicz 	void		*fit_hdr_os;	/* os FIT image header */
356eb6175edSMarian Balakowicz 	const char	*fit_uname_os;	/* os subimage node unit name */
3573dfe1101SMarian Balakowicz 	int		fit_noffset_os;	/* os subimage node offset */
358d5934ad7SMarian Balakowicz 
359d5934ad7SMarian Balakowicz 	void		*fit_hdr_rd;	/* init ramdisk FIT image header */
3603dfe1101SMarian Balakowicz 	const char	*fit_uname_rd;	/* init ramdisk subimage node unit name */
3613dfe1101SMarian Balakowicz 	int		fit_noffset_rd;	/* init ramdisk subimage node offset */
362d5934ad7SMarian Balakowicz 
363d5934ad7SMarian Balakowicz 	void		*fit_hdr_fdt;	/* FDT blob FIT image header */
3643dfe1101SMarian Balakowicz 	const char	*fit_uname_fdt;	/* FDT blob subimage node unit name */
3653dfe1101SMarian Balakowicz 	int		fit_noffset_fdt;/* FDT blob subimage node offset */
36690268b87SSimon Glass 
36790268b87SSimon Glass 	void		*fit_hdr_setup;	/* x86 setup FIT image header */
36890268b87SSimon Glass 	const char	*fit_uname_setup; /* x86 setup subimage node name */
36990268b87SSimon Glass 	int		fit_noffset_setup;/* x86 setup subimage node offset */
370d5934ad7SMarian Balakowicz #endif
3711ec73761SMarian Balakowicz 
37249c3a861SKumar Gala #ifndef USE_HOSTCC
373396f635bSKumar Gala 	image_info_t	os;		/* os image info */
374c160a954SKumar Gala 	ulong		ep;		/* entry point of OS */
375c160a954SKumar Gala 
376c4f9419cSKumar Gala 	ulong		rd_start, rd_end;/* ramdisk start/end */
377c4f9419cSKumar Gala 
37806a09918SKumar Gala 	char		*ft_addr;	/* flat dev tree address */
37906a09918SKumar Gala 	ulong		ft_len;		/* length of flat device tree */
38006a09918SKumar Gala 
38149c3a861SKumar Gala 	ulong		initrd_start;
38249c3a861SKumar Gala 	ulong		initrd_end;
38349c3a861SKumar Gala 	ulong		cmdline_start;
38449c3a861SKumar Gala 	ulong		cmdline_end;
38549c3a861SKumar Gala 	bd_t		*kbd;
38649c3a861SKumar Gala #endif
38749c3a861SKumar Gala 
3888a5ea3e6SMarian Balakowicz 	int		verify;		/* getenv("verify")[0] != 'n' */
38949c3a861SKumar Gala 
39049c3a861SKumar Gala #define	BOOTM_STATE_START	(0x00000001)
39135fc84faSSimon Glass #define	BOOTM_STATE_FINDOS	(0x00000002)
39235fc84faSSimon Glass #define	BOOTM_STATE_FINDOTHER	(0x00000004)
39335fc84faSSimon Glass #define	BOOTM_STATE_LOADOS	(0x00000008)
39435fc84faSSimon Glass #define	BOOTM_STATE_RAMDISK	(0x00000010)
39535fc84faSSimon Glass #define	BOOTM_STATE_FDT		(0x00000020)
39635fc84faSSimon Glass #define	BOOTM_STATE_OS_CMDLINE	(0x00000040)
39735fc84faSSimon Glass #define	BOOTM_STATE_OS_BD_T	(0x00000080)
39835fc84faSSimon Glass #define	BOOTM_STATE_OS_PREP	(0x00000100)
399d0ae31ebSSimon Glass #define	BOOTM_STATE_OS_FAKE_GO	(0x00000200)	/* 'Almost' run the OS */
400d0ae31ebSSimon Glass #define	BOOTM_STATE_OS_GO	(0x00000400)
40149c3a861SKumar Gala 	int		state;
40249c3a861SKumar Gala 
403a16028daSMike Frysinger #ifdef CONFIG_LMB
404e906cfaeSKumar Gala 	struct lmb	lmb;		/* for memory mgmt */
405e906cfaeSKumar Gala #endif
406d5934ad7SMarian Balakowicz } bootm_headers_t;
407d5934ad7SMarian Balakowicz 
4081648a375SSimon Schwarz extern bootm_headers_t images;
4091648a375SSimon Schwarz 
410d5934ad7SMarian Balakowicz /*
411559316faSMarian Balakowicz  * Some systems (for example LWMON) have very short watchdog periods;
412559316faSMarian Balakowicz  * we must make sure to split long operations like memmove() or
4137590378fSBartlomiej Sieka  * checksum calculations into reasonable chunks.
414559316faSMarian Balakowicz  */
4157590378fSBartlomiej Sieka #ifndef CHUNKSZ
416559316faSMarian Balakowicz #define CHUNKSZ (64 * 1024)
4177590378fSBartlomiej Sieka #endif
4187590378fSBartlomiej Sieka 
4197590378fSBartlomiej Sieka #ifndef CHUNKSZ_CRC32
4207590378fSBartlomiej Sieka #define CHUNKSZ_CRC32 (64 * 1024)
4217590378fSBartlomiej Sieka #endif
4227590378fSBartlomiej Sieka 
4237590378fSBartlomiej Sieka #ifndef CHUNKSZ_MD5
4247590378fSBartlomiej Sieka #define CHUNKSZ_MD5 (64 * 1024)
4257590378fSBartlomiej Sieka #endif
4267590378fSBartlomiej Sieka 
4277590378fSBartlomiej Sieka #ifndef CHUNKSZ_SHA1
4287590378fSBartlomiej Sieka #define CHUNKSZ_SHA1 (64 * 1024)
4297590378fSBartlomiej Sieka #endif
430559316faSMarian Balakowicz 
43137566090SMike Frysinger #define uimage_to_cpu(x)		be32_to_cpu(x)
43237566090SMike Frysinger #define cpu_to_uimage(x)		cpu_to_be32(x)
433b97a2a0aSMarian Balakowicz 
434b029dddcSPrafulla Wadaskar /*
435b029dddcSPrafulla Wadaskar  * Translation table for entries of a specific type; used by
436b029dddcSPrafulla Wadaskar  * get_table_entry_id() and get_table_entry_name().
437b029dddcSPrafulla Wadaskar  */
438b029dddcSPrafulla Wadaskar typedef struct table_entry {
439b029dddcSPrafulla Wadaskar 	int	id;
440b029dddcSPrafulla Wadaskar 	char	*sname;		/* short (input) name to find table entry */
441b029dddcSPrafulla Wadaskar 	char	*lname;		/* long (output) name to print for messages */
442b029dddcSPrafulla Wadaskar } table_entry_t;
443b029dddcSPrafulla Wadaskar 
444b029dddcSPrafulla Wadaskar /*
445b029dddcSPrafulla Wadaskar  * get_table_entry_id() scans the translation table trying to find an
446b029dddcSPrafulla Wadaskar  * entry that matches the given short name. If a matching entry is
447b029dddcSPrafulla Wadaskar  * found, it's id is returned to the caller.
448b029dddcSPrafulla Wadaskar  */
4497edb186fSMike Frysinger int get_table_entry_id(const table_entry_t *table,
450b029dddcSPrafulla Wadaskar 		const char *table_name, const char *name);
451b029dddcSPrafulla Wadaskar /*
452b029dddcSPrafulla Wadaskar  * get_table_entry_name() scans the translation table trying to find
453b029dddcSPrafulla Wadaskar  * an entry that matches the given id. If a matching entry is found,
454b029dddcSPrafulla Wadaskar  * its long name is returned to the caller.
455b029dddcSPrafulla Wadaskar  */
4567edb186fSMike Frysinger char *get_table_entry_name(const table_entry_t *table, char *msg, int id);
457b029dddcSPrafulla Wadaskar 
458570abb0aSMarian Balakowicz const char *genimg_get_os_name(uint8_t os);
459cef2e514SSimon Glass 
460cef2e514SSimon Glass /**
461cef2e514SSimon Glass  * genimg_get_os_short_name() - get the short name for an OS
462cef2e514SSimon Glass  *
463cef2e514SSimon Glass  * @param os	OS (IH_OS_...)
464cef2e514SSimon Glass  * @return OS short name, or "unknown" if unknown
465cef2e514SSimon Glass  */
466cef2e514SSimon Glass const char *genimg_get_os_short_name(uint8_t comp);
467cef2e514SSimon Glass 
468570abb0aSMarian Balakowicz const char *genimg_get_arch_name(uint8_t arch);
469cef2e514SSimon Glass 
470cef2e514SSimon Glass /**
471cef2e514SSimon Glass  * genimg_get_arch_short_name() - get the short name for an architecture
472cef2e514SSimon Glass  *
473cef2e514SSimon Glass  * @param arch	Architecture type (IH_ARCH_...)
474cef2e514SSimon Glass  * @return architecture short name, or "unknown" if unknown
475cef2e514SSimon Glass  */
476cef2e514SSimon Glass const char *genimg_get_arch_short_name(uint8_t arch);
477cef2e514SSimon Glass 
478570abb0aSMarian Balakowicz const char *genimg_get_type_name(uint8_t type);
4795b9d44dfSSimon Glass 
4805b9d44dfSSimon Glass /**
4815b9d44dfSSimon Glass  * genimg_get_type_short_name() - get the short name for an image type
4825b9d44dfSSimon Glass  *
4835b9d44dfSSimon Glass  * @param type	Image type (IH_TYPE_...)
4845b9d44dfSSimon Glass  * @return image short name, or "unknown" if unknown
4855b9d44dfSSimon Glass  */
4865b9d44dfSSimon Glass const char *genimg_get_type_short_name(uint8_t type);
4875b9d44dfSSimon Glass 
488570abb0aSMarian Balakowicz const char *genimg_get_comp_name(uint8_t comp);
489cef2e514SSimon Glass 
490cef2e514SSimon Glass /**
491cef2e514SSimon Glass  * genimg_get_comp_short_name() - get the short name for a compression method
492cef2e514SSimon Glass  *
493cef2e514SSimon Glass  * @param comp	compression method (IH_COMP_...)
494cef2e514SSimon Glass  * @return compression method short name, or "unknown" if unknown
495cef2e514SSimon Glass  */
496cef2e514SSimon Glass const char *genimg_get_comp_short_name(uint8_t comp);
497cef2e514SSimon Glass 
4981426220bSSimon Glass /**
4991426220bSSimon Glass  * genimg_get_cat_name() - Get the name of an item in a category
5001426220bSSimon Glass  *
5011426220bSSimon Glass  * @category:	Category of item
5021426220bSSimon Glass  * @id:		Item ID
5031426220bSSimon Glass  * @return name of item, or "Unknown ..." if unknown
5041426220bSSimon Glass  */
5051426220bSSimon Glass const char *genimg_get_cat_name(enum ih_category category, uint id);
5061426220bSSimon Glass 
5071426220bSSimon Glass /**
5081426220bSSimon Glass  * genimg_get_cat_short_name() - Get the short name of an item in a category
5091426220bSSimon Glass  *
5101426220bSSimon Glass  * @category:	Category of item
5111426220bSSimon Glass  * @id:		Item ID
5121426220bSSimon Glass  * @return short name of item, or "Unknown ..." if unknown
5131426220bSSimon Glass  */
5141426220bSSimon Glass const char *genimg_get_cat_short_name(enum ih_category category, uint id);
5151426220bSSimon Glass 
5161426220bSSimon Glass /**
5171426220bSSimon Glass  * genimg_get_cat_count() - Get the number of items in a category
5181426220bSSimon Glass  *
5191426220bSSimon Glass  * @category:	Category to check
5201426220bSSimon Glass  * @return the number of items in the category (IH_xxx_COUNT)
5211426220bSSimon Glass  */
5221426220bSSimon Glass int genimg_get_cat_count(enum ih_category category);
5231426220bSSimon Glass 
5241426220bSSimon Glass /**
5251426220bSSimon Glass  * genimg_get_cat_desc() - Get the description of a category
5261426220bSSimon Glass  *
5271426220bSSimon Glass  * @return the description of a category, e.g. "architecture". This
5281426220bSSimon Glass  * effectively converts the enum to a string.
5291426220bSSimon Glass  */
5301426220bSSimon Glass const char *genimg_get_cat_desc(enum ih_category category);
5311426220bSSimon Glass 
532570abb0aSMarian Balakowicz int genimg_get_os_id(const char *name);
533570abb0aSMarian Balakowicz int genimg_get_arch_id(const char *name);
534570abb0aSMarian Balakowicz int genimg_get_type_id(const char *name);
535570abb0aSMarian Balakowicz int genimg_get_comp_id(const char *name);
536f666dea8SPrafulla Wadaskar void genimg_print_size(uint32_t size);
537570abb0aSMarian Balakowicz 
538859e92b7SSimon Glass #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || \
539859e92b7SSimon Glass 	defined(USE_HOSTCC)
540859e92b7SSimon Glass #define IMAGE_ENABLE_TIMESTAMP 1
541859e92b7SSimon Glass #else
542859e92b7SSimon Glass #define IMAGE_ENABLE_TIMESTAMP 0
543859e92b7SSimon Glass #endif
544859e92b7SSimon Glass void genimg_print_time(time_t timestamp);
545859e92b7SSimon Glass 
546782cfbb2SSimon Glass /* What to do with a image load address ('load = <> 'in the FIT) */
547782cfbb2SSimon Glass enum fit_load_op {
548782cfbb2SSimon Glass 	FIT_LOAD_IGNORED,	/* Ignore load address */
549782cfbb2SSimon Glass 	FIT_LOAD_OPTIONAL,	/* Can be provided, but optional */
550fe20a81aSSimon Glass 	FIT_LOAD_OPTIONAL_NON_ZERO,	/* Optional, a value of 0 is ignored */
551782cfbb2SSimon Glass 	FIT_LOAD_REQUIRED,	/* Must be provided */
552782cfbb2SSimon Glass };
553782cfbb2SSimon Glass 
55490268b87SSimon Glass int boot_get_setup(bootm_headers_t *images, uint8_t arch, ulong *setup_start,
55590268b87SSimon Glass 		   ulong *setup_len);
55690268b87SSimon Glass 
5579a4daad0SMarian Balakowicz #ifndef USE_HOSTCC
5589a4daad0SMarian Balakowicz /* Image format types, returned by _get_format() routine */
5599a4daad0SMarian Balakowicz #define IMAGE_FORMAT_INVALID	0x00
56021d29f7fSHeiko Schocher #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
5619a4daad0SMarian Balakowicz #define IMAGE_FORMAT_LEGACY	0x01	/* legacy image_header based format */
56221d29f7fSHeiko Schocher #endif
5639a4daad0SMarian Balakowicz #define IMAGE_FORMAT_FIT	0x02	/* new, libfdt based format */
5649ace3fc8SSebastian Siewior #define IMAGE_FORMAT_ANDROID	0x03	/* Android boot image */
5659a4daad0SMarian Balakowicz 
5666c454fedSBryan Wu ulong genimg_get_kernel_addr_fit(char * const img_addr,
5676c454fedSBryan Wu 			         const char **fit_uname_config,
5686c454fedSBryan Wu 			         const char **fit_uname_kernel);
5690f64140bSBryan Wu ulong genimg_get_kernel_addr(char * const img_addr);
57035e7b0f1SSimon Glass int genimg_get_format(const void *img_addr);
571f773bea8SMarian Balakowicz int genimg_has_config(bootm_headers_t *images);
5729a4daad0SMarian Balakowicz ulong genimg_get_image(ulong img_addr);
5739a4daad0SMarian Balakowicz 
57462afc601SMichal Simek int boot_get_fpga(int argc, char * const argv[], bootm_headers_t *images,
57562afc601SMichal Simek 		uint8_t arch, const ulong *ld_start, ulong * const ld_len);
57654841ab5SWolfgang Denk int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
577d985c849SMarian Balakowicz 		uint8_t arch, ulong *rd_start, ulong *rd_end);
57884a07dbfSKarl Apsite 
57984a07dbfSKarl Apsite /**
58084a07dbfSKarl Apsite  * boot_get_loadable - routine to load a list of binaries to memory
58184a07dbfSKarl Apsite  * @argc: Ignored Argument
58284a07dbfSKarl Apsite  * @argv: Ignored Argument
58384a07dbfSKarl Apsite  * @images: pointer to the bootm images structure
58484a07dbfSKarl Apsite  * @arch: expected architecture for the image
58584a07dbfSKarl Apsite  * @ld_start: Ignored Argument
58684a07dbfSKarl Apsite  * @ld_len: Ignored Argument
58784a07dbfSKarl Apsite  *
58884a07dbfSKarl Apsite  * boot_get_loadable() will take the given FIT configuration, and look
58984a07dbfSKarl Apsite  * for a field named "loadables".  Loadables, is a list of elements in
59084a07dbfSKarl Apsite  * the FIT given as strings.  exe:
59184a07dbfSKarl Apsite  *   loadables = "linux_kernel@1", "fdt@2";
59284a07dbfSKarl Apsite  * this function will attempt to parse each string, and load the
59384a07dbfSKarl Apsite  * corresponding element from the FIT into memory.  Once placed,
59484a07dbfSKarl Apsite  * no aditional actions are taken.
59584a07dbfSKarl Apsite  *
59684a07dbfSKarl Apsite  * @return:
59784a07dbfSKarl Apsite  *     0, if only valid images or no images are found
59884a07dbfSKarl Apsite  *     error code, if an error occurs during fit_image_load
59984a07dbfSKarl Apsite  */
60084a07dbfSKarl Apsite int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
60184a07dbfSKarl Apsite 		uint8_t arch, const ulong *ld_start, ulong * const ld_len);
60284a07dbfSKarl Apsite #endif /* !USE_HOSTCC */
6039a4daad0SMarian Balakowicz 
60490268b87SSimon Glass int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
60590268b87SSimon Glass 		       ulong *setup_start, ulong *setup_len);
60690268b87SSimon Glass 
607782cfbb2SSimon Glass /**
608782cfbb2SSimon Glass  * fit_image_load() - load an image from a FIT
609782cfbb2SSimon Glass  *
610782cfbb2SSimon Glass  * This deals with all aspects of loading an image from a FIT, including
611782cfbb2SSimon Glass  * selecting the right image based on configuration, verifying it, printing
612782cfbb2SSimon Glass  * out progress messages, checking the type/arch/os and optionally copying it
613782cfbb2SSimon Glass  * to the right load address.
614782cfbb2SSimon Glass  *
615126cc864SSimon Glass  * The property to look up is defined by image_type.
616126cc864SSimon Glass  *
617782cfbb2SSimon Glass  * @param images	Boot images structure
618782cfbb2SSimon Glass  * @param addr		Address of FIT in memory
619782cfbb2SSimon Glass  * @param fit_unamep	On entry this is the requested image name
620782cfbb2SSimon Glass  *			(e.g. "kernel@1") or NULL to use the default. On exit
621782cfbb2SSimon Glass  *			points to the selected image name
622f320a4d8SSimon Glass  * @param fit_uname_configp	On entry this is the requested configuration
623f320a4d8SSimon Glass  *			name (e.g. "conf@1") or NULL to use the default. On
624f320a4d8SSimon Glass  *			exit points to the selected configuration name.
625782cfbb2SSimon Glass  * @param arch		Expected architecture (IH_ARCH_...)
626782cfbb2SSimon Glass  * @param image_type	Required image type (IH_TYPE_...). If this is
627782cfbb2SSimon Glass  *			IH_TYPE_KERNEL then we allow IH_TYPE_KERNEL_NOLOAD
628782cfbb2SSimon Glass  *			also.
629782cfbb2SSimon Glass  * @param bootstage_id	ID of starting bootstage to use for progress updates.
630782cfbb2SSimon Glass  *			This will be added to the BOOTSTAGE_SUB values when
631782cfbb2SSimon Glass  *			calling bootstage_mark()
632782cfbb2SSimon Glass  * @param load_op	Decribes what to do with the load address
633782cfbb2SSimon Glass  * @param datap		Returns address of loaded image
634782cfbb2SSimon Glass  * @param lenp		Returns length of loaded image
635ce1400f6SSimon Glass  * @return node offset of image, or -ve error code on error
636782cfbb2SSimon Glass  */
637126cc864SSimon Glass int fit_image_load(bootm_headers_t *images, ulong addr,
638f320a4d8SSimon Glass 		   const char **fit_unamep, const char **fit_uname_configp,
639782cfbb2SSimon Glass 		   int arch, int image_type, int bootstage_id,
640782cfbb2SSimon Glass 		   enum fit_load_op load_op, ulong *datap, ulong *lenp);
641782cfbb2SSimon Glass 
642ce1400f6SSimon Glass #ifndef USE_HOSTCC
643782cfbb2SSimon Glass /**
644782cfbb2SSimon Glass  * fit_get_node_from_config() - Look up an image a FIT by type
645782cfbb2SSimon Glass  *
646782cfbb2SSimon Glass  * This looks in the selected conf@ node (images->fit_uname_cfg) for a
647782cfbb2SSimon Glass  * particular image type (e.g. "kernel") and then finds the image that is
648782cfbb2SSimon Glass  * referred to.
649782cfbb2SSimon Glass  *
650782cfbb2SSimon Glass  * For example, for something like:
651782cfbb2SSimon Glass  *
652782cfbb2SSimon Glass  * images {
653782cfbb2SSimon Glass  *	kernel@1 {
654782cfbb2SSimon Glass  *		...
655782cfbb2SSimon Glass  *	};
656782cfbb2SSimon Glass  * };
657782cfbb2SSimon Glass  * configurations {
658782cfbb2SSimon Glass  *	conf@1 {
659782cfbb2SSimon Glass  *		kernel = "kernel@1";
660782cfbb2SSimon Glass  *	};
661782cfbb2SSimon Glass  * };
662782cfbb2SSimon Glass  *
663782cfbb2SSimon Glass  * the function will return the node offset of the kernel@1 node, assuming
664782cfbb2SSimon Glass  * that conf@1 is the chosen configuration.
665782cfbb2SSimon Glass  *
666782cfbb2SSimon Glass  * @param images	Boot images structure
667782cfbb2SSimon Glass  * @param prop_name	Property name to look up (FIT_..._PROP)
668782cfbb2SSimon Glass  * @param addr		Address of FIT in memory
669782cfbb2SSimon Glass  */
670782cfbb2SSimon Glass int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
671782cfbb2SSimon Glass 			ulong addr);
672782cfbb2SSimon Glass 
67353f375faSSimon Glass int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
67453f375faSSimon Glass 		 bootm_headers_t *images,
67553f375faSSimon Glass 		 char **of_flat_tree, ulong *of_size);
67655b0a393SGrant Likely void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
677590d3cacSGrant Likely int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size);
67806a09918SKumar Gala 
6799a4daad0SMarian Balakowicz int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
6809a4daad0SMarian Balakowicz 		  ulong *initrd_start, ulong *initrd_end);
681590d3cacSGrant Likely int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end);
682fca43cc8SJohn Rigby #ifdef CONFIG_SYS_BOOT_GET_KBD
683590d3cacSGrant Likely int boot_get_kbd(struct lmb *lmb, bd_t **kbd);
684fca43cc8SJohn Rigby #endif /* CONFIG_SYS_BOOT_GET_KBD */
685570abb0aSMarian Balakowicz #endif /* !USE_HOSTCC */
6869a4daad0SMarian Balakowicz 
6879a4daad0SMarian Balakowicz /*******************************************************************/
6889a4daad0SMarian Balakowicz /* Legacy format specific code (prefixed with image_) */
6899a4daad0SMarian Balakowicz /*******************************************************************/
690b97a2a0aSMarian Balakowicz static inline uint32_t image_get_header_size(void)
691b97a2a0aSMarian Balakowicz {
692b97a2a0aSMarian Balakowicz 	return (sizeof(image_header_t));
693b97a2a0aSMarian Balakowicz }
694b97a2a0aSMarian Balakowicz 
695b97a2a0aSMarian Balakowicz #define image_get_hdr_l(f) \
6963a2003f6SWolfgang Denk 	static inline uint32_t image_get_##f(const image_header_t *hdr) \
697b97a2a0aSMarian Balakowicz 	{ \
6989a4daad0SMarian Balakowicz 		return uimage_to_cpu(hdr->ih_##f); \
699b97a2a0aSMarian Balakowicz 	}
700178e26d7SWolfgang Denk image_get_hdr_l(magic)		/* image_get_magic */
701178e26d7SWolfgang Denk image_get_hdr_l(hcrc)		/* image_get_hcrc */
702178e26d7SWolfgang Denk image_get_hdr_l(time)		/* image_get_time */
703178e26d7SWolfgang Denk image_get_hdr_l(size)		/* image_get_size */
704178e26d7SWolfgang Denk image_get_hdr_l(load)		/* image_get_load */
705178e26d7SWolfgang Denk image_get_hdr_l(ep)		/* image_get_ep */
706178e26d7SWolfgang Denk image_get_hdr_l(dcrc)		/* image_get_dcrc */
707b97a2a0aSMarian Balakowicz 
708b97a2a0aSMarian Balakowicz #define image_get_hdr_b(f) \
7093a2003f6SWolfgang Denk 	static inline uint8_t image_get_##f(const image_header_t *hdr) \
710b97a2a0aSMarian Balakowicz 	{ \
711b97a2a0aSMarian Balakowicz 		return hdr->ih_##f; \
712b97a2a0aSMarian Balakowicz 	}
713178e26d7SWolfgang Denk image_get_hdr_b(os)		/* image_get_os */
714178e26d7SWolfgang Denk image_get_hdr_b(arch)		/* image_get_arch */
715178e26d7SWolfgang Denk image_get_hdr_b(type)		/* image_get_type */
716178e26d7SWolfgang Denk image_get_hdr_b(comp)		/* image_get_comp */
717b97a2a0aSMarian Balakowicz 
7183a2003f6SWolfgang Denk static inline char *image_get_name(const image_header_t *hdr)
719b97a2a0aSMarian Balakowicz {
720b97a2a0aSMarian Balakowicz 	return (char *)hdr->ih_name;
721b97a2a0aSMarian Balakowicz }
722b97a2a0aSMarian Balakowicz 
7233a2003f6SWolfgang Denk static inline uint32_t image_get_data_size(const image_header_t *hdr)
724b97a2a0aSMarian Balakowicz {
725b97a2a0aSMarian Balakowicz 	return image_get_size(hdr);
726b97a2a0aSMarian Balakowicz }
727f13e7b2eSMarian Balakowicz 
728f13e7b2eSMarian Balakowicz /**
729f13e7b2eSMarian Balakowicz  * image_get_data - get image payload start address
730f13e7b2eSMarian Balakowicz  * @hdr: image header
731f13e7b2eSMarian Balakowicz  *
732f13e7b2eSMarian Balakowicz  * image_get_data() returns address of the image payload. For single
733f13e7b2eSMarian Balakowicz  * component images it is image data start. For multi component
734f13e7b2eSMarian Balakowicz  * images it points to the null terminated table of sub-images sizes.
735f13e7b2eSMarian Balakowicz  *
736f13e7b2eSMarian Balakowicz  * returns:
737f13e7b2eSMarian Balakowicz  *     image payload data start address
738f13e7b2eSMarian Balakowicz  */
7393a2003f6SWolfgang Denk static inline ulong image_get_data(const image_header_t *hdr)
740f13e7b2eSMarian Balakowicz {
741f13e7b2eSMarian Balakowicz 	return ((ulong)hdr + image_get_header_size());
742f13e7b2eSMarian Balakowicz }
743f13e7b2eSMarian Balakowicz 
7443a2003f6SWolfgang Denk static inline uint32_t image_get_image_size(const image_header_t *hdr)
745b97a2a0aSMarian Balakowicz {
746b97a2a0aSMarian Balakowicz 	return (image_get_size(hdr) + image_get_header_size());
747b97a2a0aSMarian Balakowicz }
7483a2003f6SWolfgang Denk static inline ulong image_get_image_end(const image_header_t *hdr)
749b97a2a0aSMarian Balakowicz {
750f13e7b2eSMarian Balakowicz 	return ((ulong)hdr + image_get_image_size(hdr));
751b97a2a0aSMarian Balakowicz }
752b97a2a0aSMarian Balakowicz 
753b97a2a0aSMarian Balakowicz #define image_set_hdr_l(f) \
754b97a2a0aSMarian Balakowicz 	static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
755b97a2a0aSMarian Balakowicz 	{ \
7569a4daad0SMarian Balakowicz 		hdr->ih_##f = cpu_to_uimage(val); \
757b97a2a0aSMarian Balakowicz 	}
758178e26d7SWolfgang Denk image_set_hdr_l(magic)		/* image_set_magic */
759178e26d7SWolfgang Denk image_set_hdr_l(hcrc)		/* image_set_hcrc */
760178e26d7SWolfgang Denk image_set_hdr_l(time)		/* image_set_time */
761178e26d7SWolfgang Denk image_set_hdr_l(size)		/* image_set_size */
762178e26d7SWolfgang Denk image_set_hdr_l(load)		/* image_set_load */
763178e26d7SWolfgang Denk image_set_hdr_l(ep)		/* image_set_ep */
764178e26d7SWolfgang Denk image_set_hdr_l(dcrc)		/* image_set_dcrc */
765b97a2a0aSMarian Balakowicz 
766b97a2a0aSMarian Balakowicz #define image_set_hdr_b(f) \
767b97a2a0aSMarian Balakowicz 	static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
768b97a2a0aSMarian Balakowicz 	{ \
769b97a2a0aSMarian Balakowicz 		hdr->ih_##f = val; \
770b97a2a0aSMarian Balakowicz 	}
771178e26d7SWolfgang Denk image_set_hdr_b(os)		/* image_set_os */
772178e26d7SWolfgang Denk image_set_hdr_b(arch)		/* image_set_arch */
773178e26d7SWolfgang Denk image_set_hdr_b(type)		/* image_set_type */
774178e26d7SWolfgang Denk image_set_hdr_b(comp)		/* image_set_comp */
775b97a2a0aSMarian Balakowicz 
776b97a2a0aSMarian Balakowicz static inline void image_set_name(image_header_t *hdr, const char *name)
777b97a2a0aSMarian Balakowicz {
778b97a2a0aSMarian Balakowicz 	strncpy(image_get_name(hdr), name, IH_NMLEN);
779b97a2a0aSMarian Balakowicz }
780b97a2a0aSMarian Balakowicz 
7813a2003f6SWolfgang Denk int image_check_hcrc(const image_header_t *hdr);
7823a2003f6SWolfgang Denk int image_check_dcrc(const image_header_t *hdr);
783af13cdbcSMarian Balakowicz #ifndef USE_HOSTCC
784d3f2fa0dSKumar Gala ulong getenv_bootm_low(void);
785391fd93aSBecky Bruce phys_size_t getenv_bootm_size(void);
786c3624e6eSGrant Likely phys_size_t getenv_bootm_mapsize(void);
787af13cdbcSMarian Balakowicz #endif
788ce1400f6SSimon Glass void memmove_wd(void *to, void *from, size_t len, ulong chunksz);
789b97a2a0aSMarian Balakowicz 
7903a2003f6SWolfgang Denk static inline int image_check_magic(const image_header_t *hdr)
791b97a2a0aSMarian Balakowicz {
792b97a2a0aSMarian Balakowicz 	return (image_get_magic(hdr) == IH_MAGIC);
793b97a2a0aSMarian Balakowicz }
7943a2003f6SWolfgang Denk static inline int image_check_type(const image_header_t *hdr, uint8_t type)
795b97a2a0aSMarian Balakowicz {
796b97a2a0aSMarian Balakowicz 	return (image_get_type(hdr) == type);
797b97a2a0aSMarian Balakowicz }
7983a2003f6SWolfgang Denk static inline int image_check_arch(const image_header_t *hdr, uint8_t arch)
799b97a2a0aSMarian Balakowicz {
800b97a2a0aSMarian Balakowicz 	return (image_get_arch(hdr) == arch);
801b97a2a0aSMarian Balakowicz }
8023a2003f6SWolfgang Denk static inline int image_check_os(const image_header_t *hdr, uint8_t os)
803b97a2a0aSMarian Balakowicz {
804b97a2a0aSMarian Balakowicz 	return (image_get_os(hdr) == os);
805b97a2a0aSMarian Balakowicz }
806b97a2a0aSMarian Balakowicz 
8073a2003f6SWolfgang Denk ulong image_multi_count(const image_header_t *hdr);
8083a2003f6SWolfgang Denk void image_multi_getimg(const image_header_t *hdr, ulong idx,
80942b73e8eSMarian Balakowicz 			ulong *data, ulong *len);
81042b73e8eSMarian Balakowicz 
8113a2003f6SWolfgang Denk void image_print_contents(const void *hdr);
812570abb0aSMarian Balakowicz 
813b97a2a0aSMarian Balakowicz #ifndef USE_HOSTCC
8143a2003f6SWolfgang Denk static inline int image_check_target_arch(const image_header_t *hdr)
815b97a2a0aSMarian Balakowicz {
816476af299SMike Frysinger #ifndef IH_ARCH_DEFAULT
817476af299SMike Frysinger # error "please define IH_ARCH_DEFAULT in your arch asm/u-boot.h"
818b97a2a0aSMarian Balakowicz #endif
819476af299SMike Frysinger 	return image_check_arch(hdr, IH_ARCH_DEFAULT);
820b97a2a0aSMarian Balakowicz }
8215dfb5213SMarian Balakowicz #endif /* USE_HOSTCC */
8225b1d7137Swdenk 
82313d06981SSimon Glass /**
82413d06981SSimon Glass  * Set up properties in the FDT
82513d06981SSimon Glass  *
82613d06981SSimon Glass  * This sets up properties in the FDT that is to be passed to linux.
82713d06981SSimon Glass  *
82813d06981SSimon Glass  * @images:	Images information
82913d06981SSimon Glass  * @blob:	FDT to update
83013d06981SSimon Glass  * @of_size:	Size of the FDT
83113d06981SSimon Glass  * @lmb:	Points to logical memory block structure
83213d06981SSimon Glass  * @return 0 if ok, <0 on failure
83313d06981SSimon Glass  */
83413d06981SSimon Glass int image_setup_libfdt(bootm_headers_t *images, void *blob,
83513d06981SSimon Glass 		       int of_size, struct lmb *lmb);
83613d06981SSimon Glass 
83713d06981SSimon Glass /**
83813d06981SSimon Glass  * Set up the FDT to use for booting a kernel
83913d06981SSimon Glass  *
84013d06981SSimon Glass  * This performs ramdisk setup, sets up the FDT if required, and adds
84113d06981SSimon Glass  * paramters to the FDT if libfdt is available.
84213d06981SSimon Glass  *
84313d06981SSimon Glass  * @param images	Images information
84413d06981SSimon Glass  * @return 0 if ok, <0 on failure
84513d06981SSimon Glass  */
84613d06981SSimon Glass int image_setup_linux(bootm_headers_t *images);
84713d06981SSimon Glass 
848a5266d6bSSimon Glass /**
849a5266d6bSSimon Glass  * bootz_setup() - Extract stat and size of a Linux xImage
850a5266d6bSSimon Glass  *
851a5266d6bSSimon Glass  * @image: Address of image
852a5266d6bSSimon Glass  * @start: Returns start address of image
853a5266d6bSSimon Glass  * @end : Returns end address of image
854a5266d6bSSimon Glass  * @return 0 if OK, 1 if the image was not recognised
855a5266d6bSSimon Glass  */
856a5266d6bSSimon Glass int bootz_setup(ulong image, ulong *start, ulong *end);
857a5266d6bSSimon Glass 
858a5266d6bSSimon Glass 
859d5934ad7SMarian Balakowicz /*******************************************************************/
8609a4daad0SMarian Balakowicz /* New uImage format specific code (prefixed with fit_) */
861d5934ad7SMarian Balakowicz /*******************************************************************/
8625dfb5213SMarian Balakowicz 
8635dfb5213SMarian Balakowicz #define FIT_IMAGES_PATH		"/images"
8645dfb5213SMarian Balakowicz #define FIT_CONFS_PATH		"/configurations"
8655dfb5213SMarian Balakowicz 
8663e569a6bSSimon Glass /* hash/signature node */
8675dfb5213SMarian Balakowicz #define FIT_HASH_NODENAME	"hash"
8685dfb5213SMarian Balakowicz #define FIT_ALGO_PROP		"algo"
8695dfb5213SMarian Balakowicz #define FIT_VALUE_PROP		"value"
8708ac88f2dSJoe Hershberger #define FIT_IGNORE_PROP		"uboot-ignore"
8713e569a6bSSimon Glass #define FIT_SIG_NODENAME	"signature"
8725dfb5213SMarian Balakowicz 
8735dfb5213SMarian Balakowicz /* image node */
8745dfb5213SMarian Balakowicz #define FIT_DATA_PROP		"data"
875*db1b79b8Stomas.melin@vaisala.com #define FIT_DATA_OFFSET_PROP	"data-offset"
876*db1b79b8Stomas.melin@vaisala.com #define FIT_DATA_SIZE_PROP	"data-size"
8775dfb5213SMarian Balakowicz #define FIT_TIMESTAMP_PROP	"timestamp"
8785dfb5213SMarian Balakowicz #define FIT_DESC_PROP		"description"
8795dfb5213SMarian Balakowicz #define FIT_ARCH_PROP		"arch"
8805dfb5213SMarian Balakowicz #define FIT_TYPE_PROP		"type"
8815dfb5213SMarian Balakowicz #define FIT_OS_PROP		"os"
8825dfb5213SMarian Balakowicz #define FIT_COMP_PROP		"compression"
8835dfb5213SMarian Balakowicz #define FIT_ENTRY_PROP		"entry"
8845dfb5213SMarian Balakowicz #define FIT_LOAD_PROP		"load"
8855dfb5213SMarian Balakowicz 
8865dfb5213SMarian Balakowicz /* configuration node */
8875dfb5213SMarian Balakowicz #define FIT_KERNEL_PROP		"kernel"
8885dfb5213SMarian Balakowicz #define FIT_RAMDISK_PROP	"ramdisk"
8895dfb5213SMarian Balakowicz #define FIT_FDT_PROP		"fdt"
890ecf8cd65SKarl Apsite #define FIT_LOADABLE_PROP	"loadables"
8915dfb5213SMarian Balakowicz #define FIT_DEFAULT_PROP	"default"
89290268b87SSimon Glass #define FIT_SETUP_PROP		"setup"
893ed0cea7cSMichal Simek #define FIT_FPGA_PROP		"fpga"
8945dfb5213SMarian Balakowicz 
8951de7bb4fSMichael van der Westhuizen #define FIT_MAX_HASH_LEN	HASH_MAX_DIGEST_SIZE
8965dfb5213SMarian Balakowicz 
897f1dcee59SSimon Glass #if IMAGE_ENABLE_FIT
8985dfb5213SMarian Balakowicz /* cmdline argument format parsing */
899314f634bSMike Frysinger int fit_parse_conf(const char *spec, ulong addr_curr,
900f50433d6SMarian Balakowicz 		ulong *addr, const char **conf_name);
901314f634bSMike Frysinger int fit_parse_subimage(const char *spec, ulong addr_curr,
902f50433d6SMarian Balakowicz 		ulong *addr, const char **image_name);
903d5934ad7SMarian Balakowicz 
90439931f96SGuilherme Maciel Ferreira int fit_get_subimage_count(const void *fit, int images_noffset);
905edbed247SBartlomiej Sieka void fit_print_contents(const void *fit);
9065dfb5213SMarian Balakowicz void fit_image_print(const void *fit, int noffset, const char *p);
9075dfb5213SMarian Balakowicz 
9085dfb5213SMarian Balakowicz /**
9095dfb5213SMarian Balakowicz  * fit_get_end - get FIT image size
9105dfb5213SMarian Balakowicz  * @fit: pointer to the FIT format image header
9115dfb5213SMarian Balakowicz  *
9125dfb5213SMarian Balakowicz  * returns:
9135dfb5213SMarian Balakowicz  *     size of the FIT image (blob) in memory
9145dfb5213SMarian Balakowicz  */
9155dfb5213SMarian Balakowicz static inline ulong fit_get_size(const void *fit)
9165dfb5213SMarian Balakowicz {
9175dfb5213SMarian Balakowicz 	return fdt_totalsize(fit);
9185dfb5213SMarian Balakowicz }
9195dfb5213SMarian Balakowicz 
9205dfb5213SMarian Balakowicz /**
9215dfb5213SMarian Balakowicz  * fit_get_end - get FIT image end
9225dfb5213SMarian Balakowicz  * @fit: pointer to the FIT format image header
9235dfb5213SMarian Balakowicz  *
9245dfb5213SMarian Balakowicz  * returns:
9255dfb5213SMarian Balakowicz  *     end address of the FIT image (blob) in memory
9265dfb5213SMarian Balakowicz  */
9277a80de46SSimon Glass ulong fit_get_end(const void *fit);
9285dfb5213SMarian Balakowicz 
9295dfb5213SMarian Balakowicz /**
9305dfb5213SMarian Balakowicz  * fit_get_name - get FIT node name
9315dfb5213SMarian Balakowicz  * @fit: pointer to the FIT format image header
9325dfb5213SMarian Balakowicz  *
9335dfb5213SMarian Balakowicz  * returns:
9345dfb5213SMarian Balakowicz  *     NULL, on error
9355dfb5213SMarian Balakowicz  *     pointer to node name, on success
9365dfb5213SMarian Balakowicz  */
9375dfb5213SMarian Balakowicz static inline const char *fit_get_name(const void *fit_hdr,
9385dfb5213SMarian Balakowicz 		int noffset, int *len)
9395dfb5213SMarian Balakowicz {
9405dfb5213SMarian Balakowicz 	return fdt_get_name(fit_hdr, noffset, len);
9415dfb5213SMarian Balakowicz }
9425dfb5213SMarian Balakowicz 
9435dfb5213SMarian Balakowicz int fit_get_desc(const void *fit, int noffset, char **desc);
9445dfb5213SMarian Balakowicz int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp);
9455dfb5213SMarian Balakowicz 
9465dfb5213SMarian Balakowicz int fit_image_get_node(const void *fit, const char *image_uname);
9475dfb5213SMarian Balakowicz int fit_image_get_os(const void *fit, int noffset, uint8_t *os);
9485dfb5213SMarian Balakowicz int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch);
9495dfb5213SMarian Balakowicz int fit_image_get_type(const void *fit, int noffset, uint8_t *type);
9505dfb5213SMarian Balakowicz int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp);
9515dfb5213SMarian Balakowicz int fit_image_get_load(const void *fit, int noffset, ulong *load);
9525dfb5213SMarian Balakowicz int fit_image_get_entry(const void *fit, int noffset, ulong *entry);
9535dfb5213SMarian Balakowicz int fit_image_get_data(const void *fit, int noffset,
9545dfb5213SMarian Balakowicz 				const void **data, size_t *size);
955*db1b79b8Stomas.melin@vaisala.com int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset);
956*db1b79b8Stomas.melin@vaisala.com int fit_image_get_data_size(const void *fit, int noffset, int *data_size);
9575dfb5213SMarian Balakowicz 
9585dfb5213SMarian Balakowicz int fit_image_hash_get_algo(const void *fit, int noffset, char **algo);
9595dfb5213SMarian Balakowicz int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
9605dfb5213SMarian Balakowicz 				int *value_len);
9615dfb5213SMarian Balakowicz 
9625dfb5213SMarian Balakowicz int fit_set_timestamp(void *fit, int noffset, time_t timestamp);
963bbb467dcSSimon Glass 
964bbb467dcSSimon Glass /**
96556518e71SSimon Glass  * fit_add_verification_data() - add verification data to FIT image nodes
966bbb467dcSSimon Glass  *
96756518e71SSimon Glass  * @keydir:	Directory containing keys
96856518e71SSimon Glass  * @kwydest:	FDT blob to write public key information to
96956518e71SSimon Glass  * @fit:	Pointer to the FIT format image header
97056518e71SSimon Glass  * @comment:	Comment to add to signature nodes
97156518e71SSimon Glass  * @require_keys: Mark all keys as 'required'
97256518e71SSimon Glass  *
97356518e71SSimon Glass  * Adds hash values for all component images in the FIT blob.
97456518e71SSimon Glass  * Hashes are calculated for all component images which have hash subnodes
97556518e71SSimon Glass  * with algorithm property set to one of the supported hash algorithms.
97656518e71SSimon Glass  *
97756518e71SSimon Glass  * Also add signatures if signature nodes are present.
97856518e71SSimon Glass  *
97956518e71SSimon Glass  * returns
98056518e71SSimon Glass  *     0, on success
98156518e71SSimon Glass  *     libfdt error code, on failure
982bbb467dcSSimon Glass  */
98356518e71SSimon Glass int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
98456518e71SSimon Glass 			      const char *comment, int require_keys);
9855dfb5213SMarian Balakowicz 
986b8da8366SSimon Glass int fit_image_verify(const void *fit, int noffset);
987782cfbb2SSimon Glass int fit_config_verify(const void *fit, int conf_noffset);
988b8da8366SSimon Glass int fit_all_image_verify(const void *fit);
9895dfb5213SMarian Balakowicz int fit_image_check_os(const void *fit, int noffset, uint8_t os);
9905dfb5213SMarian Balakowicz int fit_image_check_arch(const void *fit, int noffset, uint8_t arch);
9915dfb5213SMarian Balakowicz int fit_image_check_type(const void *fit, int noffset, uint8_t type);
9925dfb5213SMarian Balakowicz int fit_image_check_comp(const void *fit, int noffset, uint8_t comp);
9935dfb5213SMarian Balakowicz int fit_check_format(const void *fit);
9945dfb5213SMarian Balakowicz 
995d95f6ec7SGabe Black int fit_conf_find_compat(const void *fit, const void *fdt);
9965dfb5213SMarian Balakowicz int fit_conf_get_node(const void *fit, const char *conf_uname);
9975dfb5213SMarian Balakowicz 
998003efd7dSSimon Glass /**
999003efd7dSSimon Glass  * fit_conf_get_prop_node() - Get node refered to by a configuration
1000003efd7dSSimon Glass  * @fit:	FIT to check
1001003efd7dSSimon Glass  * @noffset:	Offset of conf@xxx node to check
1002003efd7dSSimon Glass  * @prop_name:	Property to read from the conf node
1003003efd7dSSimon Glass  *
1004003efd7dSSimon Glass  * The conf@ nodes contain references to other nodes, using properties
1005003efd7dSSimon Glass  * like 'kernel = "kernel@1"'. Given such a property name (e.g. "kernel"),
1006003efd7dSSimon Glass  * return the offset of the node referred to (e.g. offset of node
1007003efd7dSSimon Glass  * "/images/kernel@1".
1008003efd7dSSimon Glass  */
1009003efd7dSSimon Glass int fit_conf_get_prop_node(const void *fit, int noffset,
1010003efd7dSSimon Glass 		const char *prop_name);
1011003efd7dSSimon Glass 
10125dfb5213SMarian Balakowicz void fit_conf_print(const void *fit, int noffset, const char *p);
10135dfb5213SMarian Balakowicz 
101461a439a8SSimon Glass int fit_check_ramdisk(const void *fit, int os_noffset,
101561a439a8SSimon Glass 		uint8_t arch, int verify);
101661a439a8SSimon Glass 
1017604f23ddSSimon Glass int calculate_hash(const void *data, int data_len, const char *algo,
1018604f23ddSSimon Glass 			uint8_t *value, int *value_len);
1019604f23ddSSimon Glass 
1020782cfbb2SSimon Glass /*
10213e569a6bSSimon Glass  * At present we only support signing on the host, and verification on the
10223e569a6bSSimon Glass  * device
1023782cfbb2SSimon Glass  */
1024782cfbb2SSimon Glass #if defined(CONFIG_FIT_SIGNATURE)
1025782cfbb2SSimon Glass # ifdef USE_HOSTCC
10263e569a6bSSimon Glass #  define IMAGE_ENABLE_SIGN	1
102729a23f9dSHeiko Schocher #  define IMAGE_ENABLE_VERIFY	1
1028646257d1SHeiko Schocher # include  <openssl/evp.h>
1029782cfbb2SSimon Glass #else
10303e569a6bSSimon Glass #  define IMAGE_ENABLE_SIGN	0
1031782cfbb2SSimon Glass #  define IMAGE_ENABLE_VERIFY	1
1032782cfbb2SSimon Glass # endif
1033782cfbb2SSimon Glass #else
10343e569a6bSSimon Glass # define IMAGE_ENABLE_SIGN	0
1035782cfbb2SSimon Glass # define IMAGE_ENABLE_VERIFY	0
1036782cfbb2SSimon Glass #endif
1037782cfbb2SSimon Glass 
1038782cfbb2SSimon Glass #ifdef USE_HOSTCC
103929a23f9dSHeiko Schocher void *image_get_host_blob(void);
104029a23f9dSHeiko Schocher void image_set_host_blob(void *host_blob);
104129a23f9dSHeiko Schocher # define gd_fdt_blob()		image_get_host_blob()
1042782cfbb2SSimon Glass #else
1043782cfbb2SSimon Glass # define gd_fdt_blob()		(gd->fdt_blob)
1044782cfbb2SSimon Glass #endif
1045782cfbb2SSimon Glass 
1046782cfbb2SSimon Glass #ifdef CONFIG_FIT_BEST_MATCH
1047782cfbb2SSimon Glass #define IMAGE_ENABLE_BEST_MATCH	1
1048782cfbb2SSimon Glass #else
1049782cfbb2SSimon Glass #define IMAGE_ENABLE_BEST_MATCH	0
1050782cfbb2SSimon Glass #endif
1051782cfbb2SSimon Glass 
10523e569a6bSSimon Glass /* Information passed to the signing routines */
10533e569a6bSSimon Glass struct image_sign_info {
10543e569a6bSSimon Glass 	const char *keydir;		/* Directory conaining keys */
10553e569a6bSSimon Glass 	const char *keyname;		/* Name of key to use */
10563e569a6bSSimon Glass 	void *fit;			/* Pointer to FIT blob */
10573e569a6bSSimon Glass 	int node_offset;		/* Offset of signature node */
105883dd98e0SAndrew Duda 	const char *name;		/* Algorithm name */
105983dd98e0SAndrew Duda 	struct checksum_algo *checksum;	/* Checksum algorithm information */
106083dd98e0SAndrew Duda 	struct crypto_algo *crypto;	/* Crypto algorithm information */
10613e569a6bSSimon Glass 	const void *fdt_blob;		/* FDT containing public keys */
10623e569a6bSSimon Glass 	int required_keynode;		/* Node offset of key to use: -1=any */
10633e569a6bSSimon Glass 	const char *require_keys;	/* Value for 'required' property */
10643e569a6bSSimon Glass };
106573223f0eSSimon Glass #endif /* Allow struct image_region to always be defined for rsa.h */
10663e569a6bSSimon Glass 
10673e569a6bSSimon Glass /* A part of an image, used for hashing */
10683e569a6bSSimon Glass struct image_region {
10693e569a6bSSimon Glass 	const void *data;
10703e569a6bSSimon Glass 	int size;
10713e569a6bSSimon Glass };
10723e569a6bSSimon Glass 
107373223f0eSSimon Glass #if IMAGE_ENABLE_FIT
107473223f0eSSimon Glass 
1075646257d1SHeiko Schocher #if IMAGE_ENABLE_VERIFY
10762b9912e6SJeroen Hofstee # include <u-boot/rsa-checksum.h>
1077646257d1SHeiko Schocher #endif
1078646257d1SHeiko Schocher struct checksum_algo {
1079646257d1SHeiko Schocher 	const char *name;
1080646257d1SHeiko Schocher 	const int checksum_len;
1081da29f299SAndrew Duda 	const int der_len;
1082da29f299SAndrew Duda 	const uint8_t *der_prefix;
1083646257d1SHeiko Schocher #if IMAGE_ENABLE_SIGN
108429a23f9dSHeiko Schocher 	const EVP_MD *(*calculate_sign)(void);
108529a23f9dSHeiko Schocher #endif
1086b37b46f0SRuchika Gupta 	int (*calculate)(const char *name,
1087b37b46f0SRuchika Gupta 			 const struct image_region region[],
1088646257d1SHeiko Schocher 			 int region_count, uint8_t *checksum);
1089646257d1SHeiko Schocher };
1090646257d1SHeiko Schocher 
10910c1d74fdSAndrew Duda struct crypto_algo {
10923e569a6bSSimon Glass 	const char *name;		/* Name of algorithm */
10930c1d74fdSAndrew Duda 	const int key_len;
10943e569a6bSSimon Glass 
10953e569a6bSSimon Glass 	/**
10963e569a6bSSimon Glass 	 * sign() - calculate and return signature for given input data
10973e569a6bSSimon Glass 	 *
10983e569a6bSSimon Glass 	 * @info:	Specifies key and FIT information
10993e569a6bSSimon Glass 	 * @data:	Pointer to the input data
11003e569a6bSSimon Glass 	 * @data_len:	Data length
11013e569a6bSSimon Glass 	 * @sigp:	Set to an allocated buffer holding the signature
11023e569a6bSSimon Glass 	 * @sig_len:	Set to length of the calculated hash
11033e569a6bSSimon Glass 	 *
11043e569a6bSSimon Glass 	 * This computes input data signature according to selected algorithm.
11053e569a6bSSimon Glass 	 * Resulting signature value is placed in an allocated buffer, the
11063e569a6bSSimon Glass 	 * pointer is returned as *sigp. The length of the calculated
11073e569a6bSSimon Glass 	 * signature is returned via the sig_len pointer argument. The caller
11083e569a6bSSimon Glass 	 * should free *sigp.
11093e569a6bSSimon Glass 	 *
11103e569a6bSSimon Glass 	 * @return: 0, on success, -ve on error
11113e569a6bSSimon Glass 	 */
11123e569a6bSSimon Glass 	int (*sign)(struct image_sign_info *info,
11133e569a6bSSimon Glass 		    const struct image_region region[],
11143e569a6bSSimon Glass 		    int region_count, uint8_t **sigp, uint *sig_len);
11153e569a6bSSimon Glass 
11163e569a6bSSimon Glass 	/**
11173e569a6bSSimon Glass 	 * add_verify_data() - Add verification information to FDT
11183e569a6bSSimon Glass 	 *
11193e569a6bSSimon Glass 	 * Add public key information to the FDT node, suitable for
11203e569a6bSSimon Glass 	 * verification at run-time. The information added depends on the
11213e569a6bSSimon Glass 	 * algorithm being used.
11223e569a6bSSimon Glass 	 *
11233e569a6bSSimon Glass 	 * @info:	Specifies key and FIT information
11243e569a6bSSimon Glass 	 * @keydest:	Destination FDT blob for public key data
11253e569a6bSSimon Glass 	 * @return: 0, on success, -ve on error
11263e569a6bSSimon Glass 	 */
11273e569a6bSSimon Glass 	int (*add_verify_data)(struct image_sign_info *info, void *keydest);
11283e569a6bSSimon Glass 
11293e569a6bSSimon Glass 	/**
11303e569a6bSSimon Glass 	 * verify() - Verify a signature against some data
11313e569a6bSSimon Glass 	 *
11323e569a6bSSimon Glass 	 * @info:	Specifies key and FIT information
11333e569a6bSSimon Glass 	 * @data:	Pointer to the input data
11343e569a6bSSimon Glass 	 * @data_len:	Data length
11353e569a6bSSimon Glass 	 * @sig:	Signature
11363e569a6bSSimon Glass 	 * @sig_len:	Number of bytes in signature
11373e569a6bSSimon Glass 	 * @return 0 if verified, -ve on error
11383e569a6bSSimon Glass 	 */
11393e569a6bSSimon Glass 	int (*verify)(struct image_sign_info *info,
11403e569a6bSSimon Glass 		      const struct image_region region[], int region_count,
11413e569a6bSSimon Glass 		      uint8_t *sig, uint sig_len);
11420c1d74fdSAndrew Duda };
1143646257d1SHeiko Schocher 
11443e569a6bSSimon Glass /**
114583dd98e0SAndrew Duda  * image_get_checksum_algo() - Look up a checksum algorithm
11463e569a6bSSimon Glass  *
114783dd98e0SAndrew Duda  * @param full_name	Name of algorithm in the form "checksum,crypto"
11483e569a6bSSimon Glass  * @return pointer to algorithm information, or NULL if not found
11493e569a6bSSimon Glass  */
115083dd98e0SAndrew Duda struct checksum_algo *image_get_checksum_algo(const char *full_name);
115183dd98e0SAndrew Duda 
115283dd98e0SAndrew Duda /**
115383dd98e0SAndrew Duda  * image_get_crypto_algo() - Look up a cryptosystem algorithm
115483dd98e0SAndrew Duda  *
115583dd98e0SAndrew Duda  * @param full_name	Name of algorithm in the form "checksum,crypto"
115683dd98e0SAndrew Duda  * @return pointer to algorithm information, or NULL if not found
115783dd98e0SAndrew Duda  */
115883dd98e0SAndrew Duda struct crypto_algo *image_get_crypto_algo(const char *full_name);
11593e569a6bSSimon Glass 
116056518e71SSimon Glass /**
116156518e71SSimon Glass  * fit_image_verify_required_sigs() - Verify signatures marked as 'required'
116256518e71SSimon Glass  *
116356518e71SSimon Glass  * @fit:		FIT to check
116456518e71SSimon Glass  * @image_noffset:	Offset of image node to check
116556518e71SSimon Glass  * @data:		Image data to check
116656518e71SSimon Glass  * @size:		Size of image data
116756518e71SSimon Glass  * @sig_blob:		FDT containing public keys
116856518e71SSimon Glass  * @no_sigsp:		Returns 1 if no signatures were required, and
116956518e71SSimon Glass  *			therefore nothing was checked. The caller may wish
117056518e71SSimon Glass  *			to fall back to other mechanisms, or refuse to
117156518e71SSimon Glass  *			boot.
117256518e71SSimon Glass  * @return 0 if all verified ok, <0 on error
117356518e71SSimon Glass  */
117456518e71SSimon Glass int fit_image_verify_required_sigs(const void *fit, int image_noffset,
117556518e71SSimon Glass 		const char *data, size_t size, const void *sig_blob,
117656518e71SSimon Glass 		int *no_sigsp);
117756518e71SSimon Glass 
117856518e71SSimon Glass /**
117956518e71SSimon Glass  * fit_image_check_sig() - Check a single image signature node
118056518e71SSimon Glass  *
118156518e71SSimon Glass  * @fit:		FIT to check
118256518e71SSimon Glass  * @noffset:		Offset of signature node to check
118356518e71SSimon Glass  * @data:		Image data to check
118456518e71SSimon Glass  * @size:		Size of image data
118556518e71SSimon Glass  * @required_keynode:	Offset in the control FDT of the required key node,
118656518e71SSimon Glass  *			if any. If this is given, then the image wil not
118756518e71SSimon Glass  *			pass verification unless that key is used. If this is
118856518e71SSimon Glass  *			-1 then any signature will do.
118956518e71SSimon Glass  * @err_msgp:		In the event of an error, this will be pointed to a
119056518e71SSimon Glass  *			help error string to display to the user.
119156518e71SSimon Glass  * @return 0 if all verified ok, <0 on error
119256518e71SSimon Glass  */
119356518e71SSimon Glass int fit_image_check_sig(const void *fit, int noffset, const void *data,
119456518e71SSimon Glass 		size_t size, int required_keynode, char **err_msgp);
119556518e71SSimon Glass 
11964d098529SSimon Glass /**
11974d098529SSimon Glass  * fit_region_make_list() - Make a list of regions to hash
11984d098529SSimon Glass  *
11994d098529SSimon Glass  * Given a list of FIT regions (offset, size) provided by libfdt, create
12004d098529SSimon Glass  * a list of regions (void *, size) for use by the signature creationg
12014d098529SSimon Glass  * and verification code.
12024d098529SSimon Glass  *
12034d098529SSimon Glass  * @fit:		FIT image to process
12044d098529SSimon Glass  * @fdt_regions:	Regions as returned by libfdt
12054d098529SSimon Glass  * @count:		Number of regions returned by libfdt
12064d098529SSimon Glass  * @region:		Place to put list of regions (NULL to allocate it)
12074d098529SSimon Glass  * @return pointer to list of regions, or NULL if out of memory
12084d098529SSimon Glass  */
12094d098529SSimon Glass struct image_region *fit_region_make_list(const void *fit,
12104d098529SSimon Glass 		struct fdt_region *fdt_regions, int count,
12114d098529SSimon Glass 		struct image_region *region);
121256518e71SSimon Glass 
12135dfb5213SMarian Balakowicz static inline int fit_image_check_target_arch(const void *fdt, int node)
12145dfb5213SMarian Balakowicz {
121529a23f9dSHeiko Schocher #ifndef USE_HOSTCC
12167566832aSThierry Reding 	return fit_image_check_arch(fdt, node, IH_ARCH_DEFAULT);
121729a23f9dSHeiko Schocher #else
121829a23f9dSHeiko Schocher 	return 0;
121929a23f9dSHeiko Schocher #endif
12205dfb5213SMarian Balakowicz }
12215dfb5213SMarian Balakowicz 
1222d5934ad7SMarian Balakowicz #ifdef CONFIG_FIT_VERBOSE
1223d5934ad7SMarian Balakowicz #define fit_unsupported(msg)	printf("! %s:%d " \
1224d5934ad7SMarian Balakowicz 				"FIT images not supported for '%s'\n", \
1225d5934ad7SMarian Balakowicz 				__FILE__, __LINE__, (msg))
1226d5934ad7SMarian Balakowicz 
1227d5934ad7SMarian Balakowicz #define fit_unsupported_reset(msg)	printf("! %s:%d " \
1228d5934ad7SMarian Balakowicz 				"FIT images not supported for '%s' " \
1229d5934ad7SMarian Balakowicz 				"- must reset board to recover!\n", \
1230d5934ad7SMarian Balakowicz 				__FILE__, __LINE__, (msg))
1231d5934ad7SMarian Balakowicz #else
1232d5934ad7SMarian Balakowicz #define fit_unsupported(msg)
1233d5934ad7SMarian Balakowicz #define fit_unsupported_reset(msg)
1234d5934ad7SMarian Balakowicz #endif /* CONFIG_FIT_VERBOSE */
1235f50433d6SMarian Balakowicz #endif /* CONFIG_FIT */
12365ad03eb3SMarian Balakowicz 
12379ace3fc8SSebastian Siewior #if defined(CONFIG_ANDROID_BOOT_IMAGE)
12389ace3fc8SSebastian Siewior struct andr_img_hdr;
12399ace3fc8SSebastian Siewior int android_image_check_header(const struct andr_img_hdr *hdr);
12409ace3fc8SSebastian Siewior int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
12419ace3fc8SSebastian Siewior 			     ulong *os_data, ulong *os_len);
12429ace3fc8SSebastian Siewior int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
12439ace3fc8SSebastian Siewior 			      ulong *rd_data, ulong *rd_len);
12449ace3fc8SSebastian Siewior ulong android_image_get_end(const struct andr_img_hdr *hdr);
12459ace3fc8SSebastian Siewior ulong android_image_get_kload(const struct andr_img_hdr *hdr);
12464f1318b2SMichael Trimarchi void android_print_contents(const struct andr_img_hdr *hdr);
12479ace3fc8SSebastian Siewior 
12489ace3fc8SSebastian Siewior #endif /* CONFIG_ANDROID_BOOT_IMAGE */
12499ace3fc8SSebastian Siewior 
12504b307f23SSimon Glass /**
12514b307f23SSimon Glass  * board_fit_config_name_match() - Check for a matching board name
12524b307f23SSimon Glass  *
12534b307f23SSimon Glass  * This is used when SPL loads a FIT containing multiple device tree files
12544b307f23SSimon Glass  * and wants to work out which one to use. The description of each one is
12554b307f23SSimon Glass  * passed to this function. The description comes from the 'description' field
12564b307f23SSimon Glass  * in each (FDT) image node.
12574b307f23SSimon Glass  *
12584b307f23SSimon Glass  * @name: Device tree description
12594b307f23SSimon Glass  * @return 0 if this device tree should be used, non-zero to try the next
12604b307f23SSimon Glass  */
12614b307f23SSimon Glass int board_fit_config_name_match(const char *name);
12624b307f23SSimon Glass 
1263da74d1f3SDaniel Allred #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
1264da74d1f3SDaniel Allred /**
1265da74d1f3SDaniel Allred  * board_fit_image_post_process() - Do any post-process on FIT binary data
1266da74d1f3SDaniel Allred  *
1267da74d1f3SDaniel Allred  * This is used to do any sort of image manipulation, verification, decryption
1268da74d1f3SDaniel Allred  * etc. in a platform or board specific way. Obviously, anything done here would
1269da74d1f3SDaniel Allred  * need to be comprehended in how the images were prepared before being injected
1270da74d1f3SDaniel Allred  * into the FIT creation (i.e. the binary blobs would have been pre-processed
1271da74d1f3SDaniel Allred  * before being added to the FIT image).
1272da74d1f3SDaniel Allred  *
1273da74d1f3SDaniel Allred  * @image: pointer to the image start pointer
1274da74d1f3SDaniel Allred  * @size: pointer to the image size
1275da74d1f3SDaniel Allred  * @return no return value (failure should be handled internally)
1276da74d1f3SDaniel Allred  */
1277da74d1f3SDaniel Allred void board_fit_image_post_process(void **p_image, size_t *p_size);
1278da74d1f3SDaniel Allred #endif /* CONFIG_SPL_FIT_IMAGE_POST_PROCESS */
1279da74d1f3SDaniel Allred 
1280d7be5092SAndrew F. Davis /**
1281d7be5092SAndrew F. Davis  * Mapping of image types to function handlers to be invoked on the associated
1282d7be5092SAndrew F. Davis  * loaded images
1283d7be5092SAndrew F. Davis  *
1284d7be5092SAndrew F. Davis  * @type: Type of image, I.E. IH_TYPE_*
1285d7be5092SAndrew F. Davis  * @handler: Function to call on loaded image
1286d7be5092SAndrew F. Davis  */
1287d7be5092SAndrew F. Davis struct fit_loadable_tbl {
1288d7be5092SAndrew F. Davis 	int type;
1289d7be5092SAndrew F. Davis 	/**
1290d7be5092SAndrew F. Davis 	 * handler() - Process a loaded image
1291d7be5092SAndrew F. Davis 	 *
1292d7be5092SAndrew F. Davis 	 * @data: Pointer to start of loaded image data
1293d7be5092SAndrew F. Davis 	 * @size: Size of loaded image data
1294d7be5092SAndrew F. Davis 	 */
1295d7be5092SAndrew F. Davis 	void (*handler)(ulong data, size_t size);
1296d7be5092SAndrew F. Davis };
1297d7be5092SAndrew F. Davis 
1298d7be5092SAndrew F. Davis /*
1299d7be5092SAndrew F. Davis  * Define a FIT loadable image type handler
1300d7be5092SAndrew F. Davis  *
1301d7be5092SAndrew F. Davis  * _type is a valid uimage_type ID as defined in the "Image Type" enum above
1302d7be5092SAndrew F. Davis  * _handler is the handler function to call after this image type is loaded
1303d7be5092SAndrew F. Davis  */
1304d7be5092SAndrew F. Davis #define U_BOOT_FIT_LOADABLE_HANDLER(_type, _handler) \
1305d7be5092SAndrew F. Davis 	ll_entry_declare(struct fit_loadable_tbl, _function, fit_loadable) = { \
1306d7be5092SAndrew F. Davis 		.type = _type, \
1307d7be5092SAndrew F. Davis 		.handler = _handler, \
1308d7be5092SAndrew F. Davis 	}
1309d7be5092SAndrew F. Davis 
13105b1d7137Swdenk #endif	/* __IMAGE_H__ */
1311