xref: /rk3399_rockchip-uboot/include/image.h (revision 6da15b9fe596d4f34da437e4a653269bf193e684)
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"
200ee21b67SJason Zhu #include <stdbool.h>
212f220500SKim Phillips #include <asm/byteorder.h>
2237566090SMike Frysinger 
2313d06981SSimon Glass /* Define this to avoid #ifdefs later on */
2413d06981SSimon Glass struct lmb;
2513d06981SSimon Glass 
2637566090SMike Frysinger #ifdef USE_HOSTCC
2726e355d1SJörg Krause #include <sys/types.h>
289d25438fSBartlomiej Sieka 
299d25438fSBartlomiej Sieka /* new uImage format support enabled on host */
3073223f0eSSimon Glass #define IMAGE_ENABLE_FIT	1
31aa34fbc0SSimon Glass #define IMAGE_ENABLE_OF_LIBFDT	1
329d25438fSBartlomiej Sieka #define CONFIG_FIT_VERBOSE	1 /* enable fit_format_{error,warning}() */
3385289e9dSPhilippe Reynes #define CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT 1
340db7f685STom Rini #define CONFIG_FIT_ENABLE_SHA256_SUPPORT
35089df18bSTom Rini #define CONFIG_SHA1
36089df18bSTom Rini #define CONFIG_SHA256
379d25438fSBartlomiej Sieka 
38ab9efc66SSimon Glass #define IMAGE_ENABLE_IGNORE	0
391fe7d938SSimon Glass #define IMAGE_INDENT_STRING	""
40ab9efc66SSimon Glass 
41def0819eSWolfgang Denk #else
42def0819eSWolfgang Denk 
43def0819eSWolfgang Denk #include <lmb.h>
44def0819eSWolfgang Denk #include <asm/u-boot.h>
455daa1c18SMike Frysinger #include <command.h>
46def0819eSWolfgang Denk 
47ab9efc66SSimon Glass /* Take notice of the 'ignore' property for hashes */
48ab9efc66SSimon Glass #define IMAGE_ENABLE_IGNORE	1
491fe7d938SSimon Glass #define IMAGE_INDENT_STRING	"   "
50ab9efc66SSimon Glass 
5173223f0eSSimon Glass #define IMAGE_ENABLE_FIT	CONFIG_IS_ENABLED(FIT)
52aa34fbc0SSimon Glass #define IMAGE_ENABLE_OF_LIBFDT	CONFIG_IS_ENABLED(OF_LIBFDT)
5373223f0eSSimon Glass 
549d25438fSBartlomiej Sieka #endif /* USE_HOSTCC */
55d5934ad7SMarian Balakowicz 
5673223f0eSSimon Glass #if IMAGE_ENABLE_FIT
571de7bb4fSMichael van der Westhuizen #include <hash.h>
580e00a84cSMasahiro Yamada #include <linux/libfdt.h>
595dfb5213SMarian Balakowicz #include <fdt_support.h>
6087ebee39SSimon Glass # ifdef CONFIG_SPL_BUILD
616f41751fSMasahiro Yamada #  ifdef CONFIG_SPL_CRC32_SUPPORT
626f41751fSMasahiro Yamada #   define IMAGE_ENABLE_CRC32	1
636f41751fSMasahiro Yamada #  endif
646f41751fSMasahiro Yamada #  ifdef CONFIG_SPL_MD5_SUPPORT
656f41751fSMasahiro Yamada #   define IMAGE_ENABLE_MD5	1
666f41751fSMasahiro Yamada #  endif
676f41751fSMasahiro Yamada #  ifdef CONFIG_SPL_SHA1_SUPPORT
686f41751fSMasahiro Yamada #   define IMAGE_ENABLE_SHA1	1
696f41751fSMasahiro Yamada #  endif
7087ebee39SSimon Glass # else
7187ebee39SSimon Glass #  define CONFIG_CRC32		/* FIT images need CRC32 support */
7287ebee39SSimon Glass #  define IMAGE_ENABLE_CRC32	1
7387ebee39SSimon Glass #  define IMAGE_ENABLE_MD5	1
7487ebee39SSimon Glass #  define IMAGE_ENABLE_SHA1	1
756f41751fSMasahiro Yamada # endif
766f41751fSMasahiro Yamada 
776f41751fSMasahiro Yamada #ifndef IMAGE_ENABLE_CRC32
786f41751fSMasahiro Yamada #define IMAGE_ENABLE_CRC32	0
796f41751fSMasahiro Yamada #endif
806f41751fSMasahiro Yamada 
816f41751fSMasahiro Yamada #ifndef IMAGE_ENABLE_MD5
826f41751fSMasahiro Yamada #define IMAGE_ENABLE_MD5	0
836f41751fSMasahiro Yamada #endif
846f41751fSMasahiro Yamada 
856f41751fSMasahiro Yamada #ifndef IMAGE_ENABLE_SHA1
866f41751fSMasahiro Yamada #define IMAGE_ENABLE_SHA1	0
876f41751fSMasahiro Yamada #endif
886f41751fSMasahiro Yamada 
890db7f685STom Rini #if defined(CONFIG_FIT_ENABLE_SHA256_SUPPORT) || \
900db7f685STom Rini 	defined(CONFIG_SPL_SHA256_SUPPORT)
910db7f685STom Rini #define IMAGE_ENABLE_SHA256	1
920db7f685STom Rini #else
936f41751fSMasahiro Yamada #define IMAGE_ENABLE_SHA256	0
946f41751fSMasahiro Yamada #endif
956f41751fSMasahiro Yamada 
9673223f0eSSimon Glass #endif /* IMAGE_ENABLE_FIT */
9713d06981SSimon Glass 
986f41751fSMasahiro Yamada #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
996f41751fSMasahiro Yamada # define IMAGE_BOOT_GET_CMDLINE		1
1006f41751fSMasahiro Yamada #else
1016f41751fSMasahiro Yamada # define IMAGE_BOOT_GET_CMDLINE		0
1026f41751fSMasahiro Yamada #endif
1036f41751fSMasahiro Yamada 
1046f41751fSMasahiro Yamada #ifdef CONFIG_OF_BOARD_SETUP
1056f41751fSMasahiro Yamada # define IMAGE_OF_BOARD_SETUP		1
1066f41751fSMasahiro Yamada #else
1076f41751fSMasahiro Yamada # define IMAGE_OF_BOARD_SETUP		0
1086f41751fSMasahiro Yamada #endif
1096f41751fSMasahiro Yamada 
1106f41751fSMasahiro Yamada #ifdef CONFIG_OF_SYSTEM_SETUP
1116f41751fSMasahiro Yamada # define IMAGE_OF_SYSTEM_SETUP	1
1126f41751fSMasahiro Yamada #else
1136f41751fSMasahiro Yamada # define IMAGE_OF_SYSTEM_SETUP	0
1146f41751fSMasahiro Yamada #endif
115c654b517SSimon Glass 
11656d7ab74SSimon Glass enum ih_category {
11756d7ab74SSimon Glass 	IH_ARCH,
11856d7ab74SSimon Glass 	IH_COMP,
11956d7ab74SSimon Glass 	IH_OS,
12056d7ab74SSimon Glass 	IH_TYPE,
12156d7ab74SSimon Glass 
12256d7ab74SSimon Glass 	IH_COUNT,
12356d7ab74SSimon Glass };
12456d7ab74SSimon Glass 
1255b1d7137Swdenk /*
1265b1d7137Swdenk  * Operating System Codes
127ff87b081SMasahiro Yamada  *
128ff87b081SMasahiro Yamada  * The following are exposed to uImage header.
129ff87b081SMasahiro Yamada  * Do not change values for backward compatibility.
1305b1d7137Swdenk  */
131555f45d8SSimon Glass enum {
132555f45d8SSimon Glass 	IH_OS_INVALID		= 0,	/* Invalid OS	*/
133555f45d8SSimon Glass 	IH_OS_OPENBSD,			/* OpenBSD	*/
134555f45d8SSimon Glass 	IH_OS_NETBSD,			/* NetBSD	*/
135555f45d8SSimon Glass 	IH_OS_FREEBSD,			/* FreeBSD	*/
136555f45d8SSimon Glass 	IH_OS_4_4BSD,			/* 4.4BSD	*/
137555f45d8SSimon Glass 	IH_OS_LINUX,			/* Linux	*/
138555f45d8SSimon Glass 	IH_OS_SVR4,			/* SVR4		*/
139555f45d8SSimon Glass 	IH_OS_ESIX,			/* Esix		*/
140555f45d8SSimon Glass 	IH_OS_SOLARIS,			/* Solaris	*/
141555f45d8SSimon Glass 	IH_OS_IRIX,			/* Irix		*/
142555f45d8SSimon Glass 	IH_OS_SCO,			/* SCO		*/
143555f45d8SSimon Glass 	IH_OS_DELL,			/* Dell		*/
144555f45d8SSimon Glass 	IH_OS_NCR,			/* NCR		*/
145555f45d8SSimon Glass 	IH_OS_LYNXOS,			/* LynxOS	*/
146555f45d8SSimon Glass 	IH_OS_VXWORKS,			/* VxWorks	*/
147555f45d8SSimon Glass 	IH_OS_PSOS,			/* pSOS		*/
148555f45d8SSimon Glass 	IH_OS_QNX,			/* QNX		*/
149555f45d8SSimon Glass 	IH_OS_U_BOOT,			/* Firmware	*/
150555f45d8SSimon Glass 	IH_OS_RTEMS,			/* RTEMS	*/
151555f45d8SSimon Glass 	IH_OS_ARTOS,			/* ARTOS	*/
152555f45d8SSimon Glass 	IH_OS_UNITY,			/* Unity OS	*/
153555f45d8SSimon Glass 	IH_OS_INTEGRITY,		/* INTEGRITY	*/
154555f45d8SSimon Glass 	IH_OS_OSE,			/* OSE		*/
155555f45d8SSimon Glass 	IH_OS_PLAN9,			/* Plan 9	*/
156555f45d8SSimon Glass 	IH_OS_OPENRTOS,		/* OpenRTOS	*/
1574233e0b1SPhilipp Tomsich 	IH_OS_ARM_TRUSTED_FIRMWARE,     /* ARM Trusted Firmware */
158820a0a12SKever Yang 	IH_OS_OP_TEE,			/* OP-TEE	*/
159555f45d8SSimon Glass 
160555f45d8SSimon Glass 	IH_OS_COUNT,
161555f45d8SSimon Glass };
1625b1d7137Swdenk 
1635b1d7137Swdenk /*
1645b1d7137Swdenk  * CPU Architecture Codes (supported by Linux)
165ff87b081SMasahiro Yamada  *
166ff87b081SMasahiro Yamada  * The following are exposed to uImage header.
167ff87b081SMasahiro Yamada  * Do not change values for backward compatibility.
1685b1d7137Swdenk  */
169555f45d8SSimon Glass enum {
170555f45d8SSimon Glass 	IH_ARCH_INVALID		= 0,	/* Invalid CPU	*/
171555f45d8SSimon Glass 	IH_ARCH_ALPHA,			/* Alpha	*/
172555f45d8SSimon Glass 	IH_ARCH_ARM,			/* ARM		*/
173555f45d8SSimon Glass 	IH_ARCH_I386,			/* Intel x86	*/
174555f45d8SSimon Glass 	IH_ARCH_IA64,			/* IA64		*/
175555f45d8SSimon Glass 	IH_ARCH_MIPS,			/* MIPS		*/
176555f45d8SSimon Glass 	IH_ARCH_MIPS64,			/* MIPS	 64 Bit */
177555f45d8SSimon Glass 	IH_ARCH_PPC,			/* PowerPC	*/
178555f45d8SSimon Glass 	IH_ARCH_S390,			/* IBM S390	*/
179555f45d8SSimon Glass 	IH_ARCH_SH,			/* SuperH	*/
180555f45d8SSimon Glass 	IH_ARCH_SPARC,			/* Sparc	*/
181555f45d8SSimon Glass 	IH_ARCH_SPARC64,		/* Sparc 64 Bit */
182555f45d8SSimon Glass 	IH_ARCH_M68K,			/* M68K		*/
183ff87b081SMasahiro Yamada 	IH_ARCH_NIOS,			/* Nios-32	*/
184555f45d8SSimon Glass 	IH_ARCH_MICROBLAZE,		/* MicroBlaze   */
185555f45d8SSimon Glass 	IH_ARCH_NIOS2,			/* Nios-II	*/
186555f45d8SSimon Glass 	IH_ARCH_BLACKFIN,		/* Blackfin	*/
187555f45d8SSimon Glass 	IH_ARCH_AVR32,			/* AVR32	*/
188555f45d8SSimon Glass 	IH_ARCH_ST200,			/* STMicroelectronics ST200  */
189555f45d8SSimon Glass 	IH_ARCH_SANDBOX,		/* Sandbox architecture (test only) */
190555f45d8SSimon Glass 	IH_ARCH_NDS32,			/* ANDES Technology - NDS32  */
191555f45d8SSimon Glass 	IH_ARCH_OPENRISC,		/* OpenRISC 1000  */
192555f45d8SSimon Glass 	IH_ARCH_ARM64,			/* ARM64	*/
193555f45d8SSimon Glass 	IH_ARCH_ARC,			/* Synopsys DesignWare ARC */
194555f45d8SSimon Glass 	IH_ARCH_X86_64,			/* AMD x86_64, Intel and Via */
195de5e5ceaSChris Zankel 	IH_ARCH_XTENSA,			/* Xtensa	*/
196ade0ea07SRick Chen 	IH_ARCH_RISCV,			/* RISC-V */
197555f45d8SSimon Glass 
198555f45d8SSimon Glass 	IH_ARCH_COUNT,
199555f45d8SSimon Glass };
2005b1d7137Swdenk 
2015b1d7137Swdenk /*
2025b1d7137Swdenk  * Image Types
2035b1d7137Swdenk  *
2045b1d7137Swdenk  * "Standalone Programs" are directly runnable in the environment
2055b1d7137Swdenk  *	provided by U-Boot; it is expected that (if they behave
2065b1d7137Swdenk  *	well) you can continue to work in U-Boot after return from
2075b1d7137Swdenk  *	the Standalone Program.
2085b1d7137Swdenk  * "OS Kernel Images" are usually images of some Embedded OS which
2095b1d7137Swdenk  *	will take over control completely. Usually these programs
2105b1d7137Swdenk  *	will install their own set of exception handlers, device
2115b1d7137Swdenk  *	drivers, set up the MMU, etc. - this means, that you cannot
2125b1d7137Swdenk  *	expect to re-enter U-Boot except by resetting the CPU.
2135b1d7137Swdenk  * "RAMDisk Images" are more or less just data blocks, and their
2145b1d7137Swdenk  *	parameters (address, size) are passed to an OS kernel that is
2155b1d7137Swdenk  *	being started.
2165b1d7137Swdenk  * "Multi-File Images" contain several images, typically an OS
2175b1d7137Swdenk  *	(Linux) kernel image and one or more data images like
2185b1d7137Swdenk  *	RAMDisks. This construct is useful for instance when you want
2195b1d7137Swdenk  *	to boot over the network using BOOTP etc., where the boot
2205b1d7137Swdenk  *	server provides just a single image file, but you want to get
2215b1d7137Swdenk  *	for instance an OS kernel and a RAMDisk image.
2225b1d7137Swdenk  *
2235b1d7137Swdenk  *	"Multi-File Images" start with a list of image sizes, each
2245b1d7137Swdenk  *	image size (in bytes) specified by an "uint32_t" in network
2255b1d7137Swdenk  *	byte order. This list is terminated by an "(uint32_t)0".
2265b1d7137Swdenk  *	Immediately after the terminating 0 follow the images, one by
2275b1d7137Swdenk  *	one, all aligned on "uint32_t" boundaries (size rounded up to
228e1599e83Swdenk  *	a multiple of 4 bytes - except for the last file).
2295b1d7137Swdenk  *
2305b1d7137Swdenk  * "Firmware Images" are binary images containing firmware (like
2315b1d7137Swdenk  *	U-Boot or FPGA images) which usually will be programmed to
2325b1d7137Swdenk  *	flash memory.
2335b1d7137Swdenk  *
2345b1d7137Swdenk  * "Script files" are command sequences that will be executed by
2355b1d7137Swdenk  *	U-Boot's command interpreter; this feature is especially
2365b1d7137Swdenk  *	useful when you configure U-Boot to use a real shell (hush)
23727b207fdSwdenk  *	as command interpreter (=> Shell Scripts).
238ff87b081SMasahiro Yamada  *
239ff87b081SMasahiro Yamada  * The following are exposed to uImage header.
240ff87b081SMasahiro Yamada  * Do not change values for backward compatibility.
2415b1d7137Swdenk  */
2425b1d7137Swdenk 
243555f45d8SSimon Glass enum {
244555f45d8SSimon Glass 	IH_TYPE_INVALID		= 0,	/* Invalid Image		*/
245555f45d8SSimon Glass 	IH_TYPE_STANDALONE,		/* Standalone Program		*/
246555f45d8SSimon Glass 	IH_TYPE_KERNEL,			/* OS Kernel Image		*/
247555f45d8SSimon Glass 	IH_TYPE_RAMDISK,		/* RAMDisk Image		*/
248555f45d8SSimon Glass 	IH_TYPE_MULTI,			/* Multi-File Image		*/
249555f45d8SSimon Glass 	IH_TYPE_FIRMWARE,		/* Firmware Image		*/
250555f45d8SSimon Glass 	IH_TYPE_SCRIPT,			/* Script file			*/
251555f45d8SSimon Glass 	IH_TYPE_FILESYSTEM,		/* Filesystem Image (any type)	*/
252555f45d8SSimon Glass 	IH_TYPE_FLATDT,			/* Binary Flat Device Tree Blob	*/
253555f45d8SSimon Glass 	IH_TYPE_KWBIMAGE,		/* Kirkwood Boot Image		*/
254555f45d8SSimon Glass 	IH_TYPE_IMXIMAGE,		/* Freescale IMXBoot Image	*/
255555f45d8SSimon Glass 	IH_TYPE_UBLIMAGE,		/* Davinci UBL Image		*/
256555f45d8SSimon Glass 	IH_TYPE_OMAPIMAGE,		/* TI OMAP Config Header Image	*/
257555f45d8SSimon Glass 	IH_TYPE_AISIMAGE,		/* TI Davinci AIS Image		*/
258555f45d8SSimon Glass 	/* OS Kernel Image, can run from any load address */
259555f45d8SSimon Glass 	IH_TYPE_KERNEL_NOLOAD,
260555f45d8SSimon Glass 	IH_TYPE_PBLIMAGE,		/* Freescale PBL Boot Image	*/
261555f45d8SSimon Glass 	IH_TYPE_MXSIMAGE,		/* Freescale MXSBoot Image	*/
262555f45d8SSimon Glass 	IH_TYPE_GPIMAGE,		/* TI Keystone GPHeader Image	*/
263555f45d8SSimon Glass 	IH_TYPE_ATMELIMAGE,		/* ATMEL ROM bootable Image	*/
264555f45d8SSimon Glass 	IH_TYPE_SOCFPGAIMAGE,		/* Altera SOCFPGA Preloader	*/
265555f45d8SSimon Glass 	IH_TYPE_X86_SETUP,		/* x86 setup.bin Image		*/
266555f45d8SSimon Glass 	IH_TYPE_LPC32XXIMAGE,		/* x86 setup.bin Image		*/
267555f45d8SSimon Glass 	IH_TYPE_LOADABLE,		/* A list of typeless images	*/
268555f45d8SSimon Glass 	IH_TYPE_RKIMAGE,		/* Rockchip Boot Image		*/
269555f45d8SSimon Glass 	IH_TYPE_RKSD,			/* Rockchip SD card		*/
270555f45d8SSimon Glass 	IH_TYPE_RKSPI,			/* Rockchip SPI image		*/
271555f45d8SSimon Glass 	IH_TYPE_ZYNQIMAGE,		/* Xilinx Zynq Boot Image */
272555f45d8SSimon Glass 	IH_TYPE_ZYNQMPIMAGE,		/* Xilinx ZynqMP Boot Image */
273555f45d8SSimon Glass 	IH_TYPE_FPGA,			/* FPGA Image */
274ed0c2c0aSAlbert ARIBAUD \(3ADEV\) 	IH_TYPE_VYBRIDIMAGE,	/* VYBRID .vyb Image */
2757e719ee7SAndrew F. Davis 	IH_TYPE_TEE,            /* Trusted Execution Environment OS Image */
276d21bd69bSSven Ebenfeld 	IH_TYPE_FIRMWARE_IVT,		/* Firmware Image with HABv4 IVT */
277ee0f94feSAndrew F. Davis 	IH_TYPE_PMMC,            /* TI Power Management Micro-Controller Firmware */
2786f14746bSPaweł Jarosz 	IH_TYPE_RKNAND,			/* Rockchip NAND Boot Image	*/
2795b1d7137Swdenk 
280555f45d8SSimon Glass 	IH_TYPE_COUNT,			/* Number of image types */
281555f45d8SSimon Glass };
2825b9d44dfSSimon Glass 
2835b1d7137Swdenk /*
2845b1d7137Swdenk  * Compression Types
285ff87b081SMasahiro Yamada  *
286ff87b081SMasahiro Yamada  * The following are exposed to uImage header.
287ff87b081SMasahiro Yamada  * Do not change values for backward compatibility.
2885b1d7137Swdenk  */
289555f45d8SSimon Glass enum {
290555f45d8SSimon Glass 	IH_COMP_NONE		= 0,	/*  No	 Compression Used	*/
291555f45d8SSimon Glass 	IH_COMP_GZIP,			/* gzip	 Compression Used	*/
292555f45d8SSimon Glass 	IH_COMP_BZIP2,			/* bzip2 Compression Used	*/
293555f45d8SSimon Glass 	IH_COMP_LZMA,			/* lzma  Compression Used	*/
294555f45d8SSimon Glass 	IH_COMP_LZO,			/* lzo   Compression Used	*/
295555f45d8SSimon Glass 	IH_COMP_LZ4,			/* lz4   Compression Used	*/
296008aee87SAndy Yan 	IH_COMP_ZIMAGE,			/* zImage Decompressed itself   */
297555f45d8SSimon Glass 
298555f45d8SSimon Glass 	IH_COMP_COUNT,
299555f45d8SSimon Glass };
3005b1d7137Swdenk 
3015b1d7137Swdenk #define IH_MAGIC	0x27051956	/* Image Magic Number		*/
3025b1d7137Swdenk #define IH_NMLEN		32	/* Image Name Length		*/
3035b1d7137Swdenk 
3041411fb37SFabio Estevam /* Reused from common.h */
3051411fb37SFabio Estevam #define ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
3061411fb37SFabio Estevam 
3075b1d7137Swdenk /*
3089a4daad0SMarian Balakowicz  * Legacy format image header,
3099a4daad0SMarian Balakowicz  * all data in network byte order (aka natural aka bigendian).
3105b1d7137Swdenk  */
3115b1d7137Swdenk typedef struct image_header {
3122f220500SKim Phillips 	__be32		ih_magic;	/* Image Header Magic Number	*/
3132f220500SKim Phillips 	__be32		ih_hcrc;	/* Image Header CRC Checksum	*/
3142f220500SKim Phillips 	__be32		ih_time;	/* Image Creation Timestamp	*/
3152f220500SKim Phillips 	__be32		ih_size;	/* Image Data Size		*/
3162f220500SKim Phillips 	__be32		ih_load;	/* Data	 Load  Address		*/
3172f220500SKim Phillips 	__be32		ih_ep;		/* Entry Point Address		*/
3182f220500SKim Phillips 	__be32		ih_dcrc;	/* Image Data CRC Checksum	*/
3195b1d7137Swdenk 	uint8_t		ih_os;		/* Operating System		*/
3205b1d7137Swdenk 	uint8_t		ih_arch;	/* CPU architecture		*/
3215b1d7137Swdenk 	uint8_t		ih_type;	/* Image Type			*/
3225b1d7137Swdenk 	uint8_t		ih_comp;	/* Compression Type		*/
3235b1d7137Swdenk 	uint8_t		ih_name[IH_NMLEN];	/* Image Name		*/
3245b1d7137Swdenk } image_header_t;
3255b1d7137Swdenk 
326396f635bSKumar Gala typedef struct image_info {
327396f635bSKumar Gala 	ulong		start, end;		/* start/end of blob */
328396f635bSKumar Gala 	ulong		image_start, image_len; /* start of image within blob, len of image */
329396f635bSKumar Gala 	ulong		load;			/* load addr for the image */
330396f635bSKumar Gala 	uint8_t		comp, type, os;		/* compression, type of image, os type */
33190268b87SSimon Glass 	uint8_t		arch;			/* CPU architecture */
332396f635bSKumar Gala } image_info_t;
333396f635bSKumar Gala 
334559316faSMarian Balakowicz /*
335d5934ad7SMarian Balakowicz  * Legacy and FIT format headers used by do_bootm() and do_bootm_<os>()
336d5934ad7SMarian Balakowicz  * routines.
337d5934ad7SMarian Balakowicz  */
338d5934ad7SMarian Balakowicz typedef struct bootm_headers {
339d5934ad7SMarian Balakowicz 	/*
340d5934ad7SMarian Balakowicz 	 * Legacy os image header, if it is a multi component image
3419a4daad0SMarian Balakowicz 	 * then boot_get_ramdisk() and get_fdt() will attempt to get
342d5934ad7SMarian Balakowicz 	 * data from second and third component accordingly.
343d5934ad7SMarian Balakowicz 	 */
344cb1c4896SMarian Balakowicz 	image_header_t	*legacy_hdr_os;		/* image header pointer */
345cb1c4896SMarian Balakowicz 	image_header_t	legacy_hdr_os_copy;	/* header copy */
346d5934ad7SMarian Balakowicz 	ulong		legacy_hdr_valid;
347d5934ad7SMarian Balakowicz 
34873223f0eSSimon Glass #if IMAGE_ENABLE_FIT
349f773bea8SMarian Balakowicz 	const char	*fit_uname_cfg;	/* configuration node unit name */
350f773bea8SMarian Balakowicz 
351d5934ad7SMarian Balakowicz 	void		*fit_hdr_os;	/* os FIT image header */
352eb6175edSMarian Balakowicz 	const char	*fit_uname_os;	/* os subimage node unit name */
3533dfe1101SMarian Balakowicz 	int		fit_noffset_os;	/* os subimage node offset */
354d5934ad7SMarian Balakowicz 
355d5934ad7SMarian Balakowicz 	void		*fit_hdr_rd;	/* init ramdisk FIT image header */
3563dfe1101SMarian Balakowicz 	const char	*fit_uname_rd;	/* init ramdisk subimage node unit name */
3573dfe1101SMarian Balakowicz 	int		fit_noffset_rd;	/* init ramdisk subimage node offset */
358d5934ad7SMarian Balakowicz 
359d5934ad7SMarian Balakowicz 	void		*fit_hdr_fdt;	/* FDT blob FIT image header */
3603dfe1101SMarian Balakowicz 	const char	*fit_uname_fdt;	/* FDT blob subimage node unit name */
3613dfe1101SMarian Balakowicz 	int		fit_noffset_fdt;/* FDT blob subimage node offset */
36290268b87SSimon Glass 
36390268b87SSimon Glass 	void		*fit_hdr_setup;	/* x86 setup FIT image header */
36490268b87SSimon Glass 	const char	*fit_uname_setup; /* x86 setup subimage node name */
36590268b87SSimon Glass 	int		fit_noffset_setup;/* x86 setup subimage node offset */
366d5934ad7SMarian Balakowicz #endif
3671ec73761SMarian Balakowicz 
36849c3a861SKumar Gala #ifndef USE_HOSTCC
369396f635bSKumar Gala 	image_info_t	os;		/* os image info */
370c160a954SKumar Gala 	ulong		ep;		/* entry point of OS */
371c160a954SKumar Gala 
372c4f9419cSKumar Gala 	ulong		rd_start, rd_end;/* ramdisk start/end */
373c4f9419cSKumar Gala 
37406a09918SKumar Gala 	char		*ft_addr;	/* flat dev tree address */
37506a09918SKumar Gala 	ulong		ft_len;		/* length of flat device tree */
37606a09918SKumar Gala 
37749c3a861SKumar Gala 	ulong		initrd_start;
37849c3a861SKumar Gala 	ulong		initrd_end;
37949c3a861SKumar Gala 	ulong		cmdline_start;
38049c3a861SKumar Gala 	ulong		cmdline_end;
38149c3a861SKumar Gala 	bd_t		*kbd;
38249c3a861SKumar Gala #endif
38349c3a861SKumar Gala 
38400caae6dSSimon Glass 	int		verify;		/* env_get("verify")[0] != 'n' */
38549c3a861SKumar Gala 
38649c3a861SKumar Gala #define	BOOTM_STATE_START	(0x00000001)
38735fc84faSSimon Glass #define	BOOTM_STATE_FINDOS	(0x00000002)
38835fc84faSSimon Glass #define	BOOTM_STATE_FINDOTHER	(0x00000004)
38935fc84faSSimon Glass #define	BOOTM_STATE_LOADOS	(0x00000008)
39035fc84faSSimon Glass #define	BOOTM_STATE_RAMDISK	(0x00000010)
39135fc84faSSimon Glass #define	BOOTM_STATE_FDT		(0x00000020)
39235fc84faSSimon Glass #define	BOOTM_STATE_OS_CMDLINE	(0x00000040)
39335fc84faSSimon Glass #define	BOOTM_STATE_OS_BD_T	(0x00000080)
39435fc84faSSimon Glass #define	BOOTM_STATE_OS_PREP	(0x00000100)
395d0ae31ebSSimon Glass #define	BOOTM_STATE_OS_FAKE_GO	(0x00000200)	/* 'Almost' run the OS */
396d0ae31ebSSimon Glass #define	BOOTM_STATE_OS_GO	(0x00000400)
39749c3a861SKumar Gala 	int		state;
39849c3a861SKumar Gala 
399a16028daSMike Frysinger #ifdef CONFIG_LMB
400e906cfaeSKumar Gala 	struct lmb	lmb;		/* for memory mgmt */
401e906cfaeSKumar Gala #endif
4024181c9faSJoseph Chen 
4034181c9faSJoseph Chen #ifdef CONFIG_FIT_ROLLBACK_PROTECT
4044181c9faSJoseph Chen 	u32 rollback_index;
4054181c9faSJoseph Chen #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 
57362afc601SMichal Simek int boot_get_fpga(int argc, char * const argv[], bootm_headers_t *images,
57462afc601SMichal Simek 		uint8_t arch, const ulong *ld_start, ulong * const ld_len);
57554841ab5SWolfgang Denk int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
576d985c849SMarian Balakowicz 		uint8_t arch, ulong *rd_start, ulong *rd_end);
57784a07dbfSKarl Apsite 
57884a07dbfSKarl Apsite /**
57984a07dbfSKarl Apsite  * boot_get_loadable - routine to load a list of binaries to memory
58084a07dbfSKarl Apsite  * @argc: Ignored Argument
58184a07dbfSKarl Apsite  * @argv: Ignored Argument
58284a07dbfSKarl Apsite  * @images: pointer to the bootm images structure
58384a07dbfSKarl Apsite  * @arch: expected architecture for the image
58484a07dbfSKarl Apsite  * @ld_start: Ignored Argument
58584a07dbfSKarl Apsite  * @ld_len: Ignored Argument
58684a07dbfSKarl Apsite  *
58784a07dbfSKarl Apsite  * boot_get_loadable() will take the given FIT configuration, and look
58884a07dbfSKarl Apsite  * for a field named "loadables".  Loadables, is a list of elements in
58984a07dbfSKarl Apsite  * the FIT given as strings.  exe:
59084a07dbfSKarl Apsite  *   loadables = "linux_kernel@1", "fdt@2";
59184a07dbfSKarl Apsite  * this function will attempt to parse each string, and load the
59284a07dbfSKarl Apsite  * corresponding element from the FIT into memory.  Once placed,
59384a07dbfSKarl Apsite  * no aditional actions are taken.
59484a07dbfSKarl Apsite  *
59584a07dbfSKarl Apsite  * @return:
59684a07dbfSKarl Apsite  *     0, if only valid images or no images are found
59784a07dbfSKarl Apsite  *     error code, if an error occurs during fit_image_load
59884a07dbfSKarl Apsite  */
59984a07dbfSKarl Apsite int boot_get_loadable(int argc, char * const argv[], bootm_headers_t *images,
60084a07dbfSKarl Apsite 		uint8_t arch, const ulong *ld_start, ulong * const ld_len);
60184a07dbfSKarl Apsite #endif /* !USE_HOSTCC */
6029a4daad0SMarian Balakowicz 
60390268b87SSimon Glass int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
60490268b87SSimon Glass 		       ulong *setup_start, ulong *setup_len);
60590268b87SSimon Glass 
606782cfbb2SSimon Glass /**
607988a4042SPantelis Antoniou  * boot_get_fdt_fit() - load a DTB from a FIT file (applying overlays)
608988a4042SPantelis Antoniou  *
609988a4042SPantelis Antoniou  * This deals with all aspects of loading an DTB from a FIT.
610988a4042SPantelis Antoniou  * The correct base image based on configuration will be selected, and
611988a4042SPantelis Antoniou  * then any overlays specified will be applied (as present in fit_uname_configp).
612988a4042SPantelis Antoniou  *
613988a4042SPantelis Antoniou  * @param images	Boot images structure
614988a4042SPantelis Antoniou  * @param addr		Address of FIT in memory
615988a4042SPantelis Antoniou  * @param fit_unamep	On entry this is the requested image name
616988a4042SPantelis Antoniou  *			(e.g. "kernel@1") or NULL to use the default. On exit
617988a4042SPantelis Antoniou  *			points to the selected image name
618988a4042SPantelis Antoniou  * @param fit_uname_configp	On entry this is the requested configuration
619988a4042SPantelis Antoniou  *			name (e.g. "conf@1") or NULL to use the default. On
620988a4042SPantelis Antoniou  *			exit points to the selected configuration name.
621988a4042SPantelis Antoniou  * @param arch		Expected architecture (IH_ARCH_...)
622988a4042SPantelis Antoniou  * @param datap		Returns address of loaded image
623988a4042SPantelis Antoniou  * @param lenp		Returns length of loaded image
624988a4042SPantelis Antoniou  *
625988a4042SPantelis Antoniou  * @return node offset of base image, or -ve error code on error
626988a4042SPantelis Antoniou  */
627988a4042SPantelis Antoniou int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
628988a4042SPantelis Antoniou 		   const char **fit_unamep, const char **fit_uname_configp,
629988a4042SPantelis Antoniou 		   int arch, ulong *datap, ulong *lenp);
630988a4042SPantelis Antoniou 
631988a4042SPantelis Antoniou /**
632782cfbb2SSimon Glass  * fit_image_load() - load an image from a FIT
633782cfbb2SSimon Glass  *
634782cfbb2SSimon Glass  * This deals with all aspects of loading an image from a FIT, including
635782cfbb2SSimon Glass  * selecting the right image based on configuration, verifying it, printing
636782cfbb2SSimon Glass  * out progress messages, checking the type/arch/os and optionally copying it
637782cfbb2SSimon Glass  * to the right load address.
638782cfbb2SSimon Glass  *
639126cc864SSimon Glass  * The property to look up is defined by image_type.
640126cc864SSimon Glass  *
641782cfbb2SSimon Glass  * @param images	Boot images structure
642782cfbb2SSimon Glass  * @param addr		Address of FIT in memory
643782cfbb2SSimon Glass  * @param fit_unamep	On entry this is the requested image name
644782cfbb2SSimon Glass  *			(e.g. "kernel@1") or NULL to use the default. On exit
645782cfbb2SSimon Glass  *			points to the selected image name
646f320a4d8SSimon Glass  * @param fit_uname_configp	On entry this is the requested configuration
647f320a4d8SSimon Glass  *			name (e.g. "conf@1") or NULL to use the default. On
648f320a4d8SSimon Glass  *			exit points to the selected configuration name.
649782cfbb2SSimon Glass  * @param arch		Expected architecture (IH_ARCH_...)
650782cfbb2SSimon Glass  * @param image_type	Required image type (IH_TYPE_...). If this is
651782cfbb2SSimon Glass  *			IH_TYPE_KERNEL then we allow IH_TYPE_KERNEL_NOLOAD
652782cfbb2SSimon Glass  *			also.
653782cfbb2SSimon Glass  * @param bootstage_id	ID of starting bootstage to use for progress updates.
654782cfbb2SSimon Glass  *			This will be added to the BOOTSTAGE_SUB values when
655782cfbb2SSimon Glass  *			calling bootstage_mark()
656782cfbb2SSimon Glass  * @param load_op	Decribes what to do with the load address
657782cfbb2SSimon Glass  * @param datap		Returns address of loaded image
658782cfbb2SSimon Glass  * @param lenp		Returns length of loaded image
659ce1400f6SSimon Glass  * @return node offset of image, or -ve error code on error
660782cfbb2SSimon Glass  */
661126cc864SSimon Glass int fit_image_load(bootm_headers_t *images, ulong addr,
662f320a4d8SSimon Glass 		   const char **fit_unamep, const char **fit_uname_configp,
663782cfbb2SSimon Glass 		   int arch, int image_type, int bootstage_id,
664782cfbb2SSimon Glass 		   enum fit_load_op load_op, ulong *datap, ulong *lenp);
665782cfbb2SSimon Glass 
66696f5441eSJoseph Chen int fit_image_load_index(bootm_headers_t *images, ulong addr,
66796f5441eSJoseph Chen 		   const char **fit_unamep, const char **fit_uname_configp,
66896f5441eSJoseph Chen 		   int arch, int image_type, int image_index, int bootstage_id,
66996f5441eSJoseph Chen 		   enum fit_load_op load_op, ulong *datap, ulong *lenp);
67096f5441eSJoseph Chen 
671ce1400f6SSimon Glass #ifndef USE_HOSTCC
672782cfbb2SSimon Glass /**
673782cfbb2SSimon Glass  * fit_get_node_from_config() - Look up an image a FIT by type
674782cfbb2SSimon Glass  *
675782cfbb2SSimon Glass  * This looks in the selected conf@ node (images->fit_uname_cfg) for a
676782cfbb2SSimon Glass  * particular image type (e.g. "kernel") and then finds the image that is
677782cfbb2SSimon Glass  * referred to.
678782cfbb2SSimon Glass  *
679782cfbb2SSimon Glass  * For example, for something like:
680782cfbb2SSimon Glass  *
681782cfbb2SSimon Glass  * images {
682782cfbb2SSimon Glass  *	kernel@1 {
683782cfbb2SSimon Glass  *		...
684782cfbb2SSimon Glass  *	};
685782cfbb2SSimon Glass  * };
686782cfbb2SSimon Glass  * configurations {
687782cfbb2SSimon Glass  *	conf@1 {
688782cfbb2SSimon Glass  *		kernel = "kernel@1";
689782cfbb2SSimon Glass  *	};
690782cfbb2SSimon Glass  * };
691782cfbb2SSimon Glass  *
692782cfbb2SSimon Glass  * the function will return the node offset of the kernel@1 node, assuming
693782cfbb2SSimon Glass  * that conf@1 is the chosen configuration.
694782cfbb2SSimon Glass  *
695782cfbb2SSimon Glass  * @param images	Boot images structure
696782cfbb2SSimon Glass  * @param prop_name	Property name to look up (FIT_..._PROP)
697782cfbb2SSimon Glass  * @param addr		Address of FIT in memory
698782cfbb2SSimon Glass  */
699782cfbb2SSimon Glass int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
700782cfbb2SSimon Glass 			ulong addr);
701782cfbb2SSimon Glass 
70253f375faSSimon Glass int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
70353f375faSSimon Glass 		 bootm_headers_t *images,
70453f375faSSimon Glass 		 char **of_flat_tree, ulong *of_size);
70555b0a393SGrant Likely void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
7062ee484faSJoseph Chen void boot_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
707a384e3f1SJoseph Chen #ifdef CONFIG_SYSMEM
708a384e3f1SJoseph Chen int boot_fdt_add_sysmem_rsv_regions(void *fdt_blob);
7096e15146eSJoseph Chen #else
boot_fdt_add_sysmem_rsv_regions(void * fdt_blob)7106e15146eSJoseph Chen static inline int boot_fdt_add_sysmem_rsv_regions(void *fdt_blob) { return 0; }
711a384e3f1SJoseph Chen #endif
712590d3cacSGrant Likely int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size);
71306a09918SKumar Gala 
7149a4daad0SMarian Balakowicz int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
7159a4daad0SMarian Balakowicz 		  ulong *initrd_start, ulong *initrd_end);
716590d3cacSGrant Likely int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end);
717fca43cc8SJohn Rigby #ifdef CONFIG_SYS_BOOT_GET_KBD
718590d3cacSGrant Likely int boot_get_kbd(struct lmb *lmb, bd_t **kbd);
719fca43cc8SJohn Rigby #endif /* CONFIG_SYS_BOOT_GET_KBD */
720570abb0aSMarian Balakowicz #endif /* !USE_HOSTCC */
7219a4daad0SMarian Balakowicz 
7229a4daad0SMarian Balakowicz /*******************************************************************/
7239a4daad0SMarian Balakowicz /* Legacy format specific code (prefixed with image_) */
7249a4daad0SMarian Balakowicz /*******************************************************************/
72540d08d0fSJoseph Chen #define IMAGE_PARAM_INVAL	0xffffffff
72640d08d0fSJoseph Chen 
image_get_header_size(void)727b97a2a0aSMarian Balakowicz static inline uint32_t image_get_header_size(void)
728b97a2a0aSMarian Balakowicz {
729b97a2a0aSMarian Balakowicz 	return (sizeof(image_header_t));
730b97a2a0aSMarian Balakowicz }
731b97a2a0aSMarian Balakowicz 
732b97a2a0aSMarian Balakowicz #define image_get_hdr_l(f) \
7333a2003f6SWolfgang Denk 	static inline uint32_t image_get_##f(const image_header_t *hdr) \
734b97a2a0aSMarian Balakowicz 	{ \
7359a4daad0SMarian Balakowicz 		return uimage_to_cpu(hdr->ih_##f); \
736b97a2a0aSMarian Balakowicz 	}
737178e26d7SWolfgang Denk image_get_hdr_l(magic)		/* image_get_magic */
image_get_hdr_l(hcrc)738178e26d7SWolfgang Denk image_get_hdr_l(hcrc)		/* image_get_hcrc */
739178e26d7SWolfgang Denk image_get_hdr_l(time)		/* image_get_time */
740178e26d7SWolfgang Denk image_get_hdr_l(size)		/* image_get_size */
741957222d7SJoseph Chen image_get_hdr_l(dcrc)		/* image_get_dcrc */
742957222d7SJoseph Chen #ifdef USE_HOSTCC
743178e26d7SWolfgang Denk image_get_hdr_l(load)		/* image_get_load */
744178e26d7SWolfgang Denk image_get_hdr_l(ep)		/* image_get_ep */
745957222d7SJoseph Chen #elif defined(CONFIG_SPL_BUILD)
746957222d7SJoseph Chen image_get_hdr_l(load)		/* image_get_load */
747957222d7SJoseph Chen image_get_hdr_l(ep)		/* image_get_ep */
748957222d7SJoseph Chen #else
749957222d7SJoseph Chen uint32_t image_get_load(const image_header_t *hdr);
750957222d7SJoseph Chen uint32_t image_get_ep(const image_header_t *hdr);
751957222d7SJoseph Chen #endif
752b97a2a0aSMarian Balakowicz 
753b97a2a0aSMarian Balakowicz #define image_get_hdr_b(f) \
7543a2003f6SWolfgang Denk 	static inline uint8_t image_get_##f(const image_header_t *hdr) \
755b97a2a0aSMarian Balakowicz 	{ \
756b97a2a0aSMarian Balakowicz 		return hdr->ih_##f; \
757b97a2a0aSMarian Balakowicz 	}
758178e26d7SWolfgang Denk image_get_hdr_b(os)		/* image_get_os */
759178e26d7SWolfgang Denk image_get_hdr_b(arch)		/* image_get_arch */
760178e26d7SWolfgang Denk image_get_hdr_b(type)		/* image_get_type */
761178e26d7SWolfgang Denk image_get_hdr_b(comp)		/* image_get_comp */
762b97a2a0aSMarian Balakowicz 
7633a2003f6SWolfgang Denk static inline char *image_get_name(const image_header_t *hdr)
764b97a2a0aSMarian Balakowicz {
765b97a2a0aSMarian Balakowicz 	return (char *)hdr->ih_name;
766b97a2a0aSMarian Balakowicz }
767b97a2a0aSMarian Balakowicz 
image_get_data_size(const image_header_t * hdr)7683a2003f6SWolfgang Denk static inline uint32_t image_get_data_size(const image_header_t *hdr)
769b97a2a0aSMarian Balakowicz {
770b97a2a0aSMarian Balakowicz 	return image_get_size(hdr);
771b97a2a0aSMarian Balakowicz }
772f13e7b2eSMarian Balakowicz 
773f13e7b2eSMarian Balakowicz /**
774f13e7b2eSMarian Balakowicz  * image_get_data - get image payload start address
775f13e7b2eSMarian Balakowicz  * @hdr: image header
776f13e7b2eSMarian Balakowicz  *
777f13e7b2eSMarian Balakowicz  * image_get_data() returns address of the image payload. For single
778f13e7b2eSMarian Balakowicz  * component images it is image data start. For multi component
779f13e7b2eSMarian Balakowicz  * images it points to the null terminated table of sub-images sizes.
780f13e7b2eSMarian Balakowicz  *
781f13e7b2eSMarian Balakowicz  * returns:
782f13e7b2eSMarian Balakowicz  *     image payload data start address
783f13e7b2eSMarian Balakowicz  */
image_get_data(const image_header_t * hdr)7843a2003f6SWolfgang Denk static inline ulong image_get_data(const image_header_t *hdr)
785f13e7b2eSMarian Balakowicz {
786f13e7b2eSMarian Balakowicz 	return ((ulong)hdr + image_get_header_size());
787f13e7b2eSMarian Balakowicz }
788f13e7b2eSMarian Balakowicz 
image_get_image_size(const image_header_t * hdr)7893a2003f6SWolfgang Denk static inline uint32_t image_get_image_size(const image_header_t *hdr)
790b97a2a0aSMarian Balakowicz {
791b97a2a0aSMarian Balakowicz 	return (image_get_size(hdr) + image_get_header_size());
792b97a2a0aSMarian Balakowicz }
image_get_image_end(const image_header_t * hdr)7933a2003f6SWolfgang Denk static inline ulong image_get_image_end(const image_header_t *hdr)
794b97a2a0aSMarian Balakowicz {
795f13e7b2eSMarian Balakowicz 	return ((ulong)hdr + image_get_image_size(hdr));
796b97a2a0aSMarian Balakowicz }
797b97a2a0aSMarian Balakowicz 
798b97a2a0aSMarian Balakowicz #define image_set_hdr_l(f) \
799b97a2a0aSMarian Balakowicz 	static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
800b97a2a0aSMarian Balakowicz 	{ \
8019a4daad0SMarian Balakowicz 		hdr->ih_##f = cpu_to_uimage(val); \
802b97a2a0aSMarian Balakowicz 	}
803178e26d7SWolfgang Denk image_set_hdr_l(magic)		/* image_set_magic */
image_set_hdr_l(hcrc)804178e26d7SWolfgang Denk image_set_hdr_l(hcrc)		/* image_set_hcrc */
805178e26d7SWolfgang Denk image_set_hdr_l(time)		/* image_set_time */
806178e26d7SWolfgang Denk image_set_hdr_l(size)		/* image_set_size */
807178e26d7SWolfgang Denk image_set_hdr_l(load)		/* image_set_load */
808178e26d7SWolfgang Denk image_set_hdr_l(ep)		/* image_set_ep */
809178e26d7SWolfgang Denk image_set_hdr_l(dcrc)		/* image_set_dcrc */
810b97a2a0aSMarian Balakowicz 
811b97a2a0aSMarian Balakowicz #define image_set_hdr_b(f) \
812b97a2a0aSMarian Balakowicz 	static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
813b97a2a0aSMarian Balakowicz 	{ \
814b97a2a0aSMarian Balakowicz 		hdr->ih_##f = val; \
815b97a2a0aSMarian Balakowicz 	}
816178e26d7SWolfgang Denk image_set_hdr_b(os)		/* image_set_os */
817178e26d7SWolfgang Denk image_set_hdr_b(arch)		/* image_set_arch */
818178e26d7SWolfgang Denk image_set_hdr_b(type)		/* image_set_type */
819178e26d7SWolfgang Denk image_set_hdr_b(comp)		/* image_set_comp */
820b97a2a0aSMarian Balakowicz 
821b97a2a0aSMarian Balakowicz static inline void image_set_name(image_header_t *hdr, const char *name)
822b97a2a0aSMarian Balakowicz {
823b97a2a0aSMarian Balakowicz 	strncpy(image_get_name(hdr), name, IH_NMLEN);
824b97a2a0aSMarian Balakowicz }
825b97a2a0aSMarian Balakowicz 
8263a2003f6SWolfgang Denk int image_check_hcrc(const image_header_t *hdr);
8273a2003f6SWolfgang Denk int image_check_dcrc(const image_header_t *hdr);
828af13cdbcSMarian Balakowicz #ifndef USE_HOSTCC
829723806ccSSimon Glass ulong env_get_bootm_low(void);
830723806ccSSimon Glass phys_size_t env_get_bootm_size(void);
831723806ccSSimon Glass phys_size_t env_get_bootm_mapsize(void);
832af13cdbcSMarian Balakowicz #endif
833ce1400f6SSimon Glass void memmove_wd(void *to, void *from, size_t len, ulong chunksz);
834b97a2a0aSMarian Balakowicz 
image_check_magic(const image_header_t * hdr)8353a2003f6SWolfgang Denk static inline int image_check_magic(const image_header_t *hdr)
836b97a2a0aSMarian Balakowicz {
837b97a2a0aSMarian Balakowicz 	return (image_get_magic(hdr) == IH_MAGIC);
838b97a2a0aSMarian Balakowicz }
image_check_type(const image_header_t * hdr,uint8_t type)8393a2003f6SWolfgang Denk static inline int image_check_type(const image_header_t *hdr, uint8_t type)
840b97a2a0aSMarian Balakowicz {
841b97a2a0aSMarian Balakowicz 	return (image_get_type(hdr) == type);
842b97a2a0aSMarian Balakowicz }
image_check_arch(const image_header_t * hdr,uint8_t arch)8433a2003f6SWolfgang Denk static inline int image_check_arch(const image_header_t *hdr, uint8_t arch)
844b97a2a0aSMarian Balakowicz {
8454ac0a32eSAlison Wang 	return (image_get_arch(hdr) == arch) ||
8464ac0a32eSAlison Wang 		(image_get_arch(hdr) == IH_ARCH_ARM && arch == IH_ARCH_ARM64);
847b97a2a0aSMarian Balakowicz }
image_check_os(const image_header_t * hdr,uint8_t os)8483a2003f6SWolfgang Denk static inline int image_check_os(const image_header_t *hdr, uint8_t os)
849b97a2a0aSMarian Balakowicz {
850b97a2a0aSMarian Balakowicz 	return (image_get_os(hdr) == os);
851b97a2a0aSMarian Balakowicz }
852b97a2a0aSMarian Balakowicz 
8533a2003f6SWolfgang Denk ulong image_multi_count(const image_header_t *hdr);
8543a2003f6SWolfgang Denk void image_multi_getimg(const image_header_t *hdr, ulong idx,
85542b73e8eSMarian Balakowicz 			ulong *data, ulong *len);
85642b73e8eSMarian Balakowicz 
8573a2003f6SWolfgang Denk void image_print_contents(const void *hdr);
858570abb0aSMarian Balakowicz 
859b97a2a0aSMarian Balakowicz #ifndef USE_HOSTCC
image_check_target_arch(const image_header_t * hdr)8603a2003f6SWolfgang Denk static inline int image_check_target_arch(const image_header_t *hdr)
861b97a2a0aSMarian Balakowicz {
862476af299SMike Frysinger #ifndef IH_ARCH_DEFAULT
863476af299SMike Frysinger # error "please define IH_ARCH_DEFAULT in your arch asm/u-boot.h"
864b97a2a0aSMarian Balakowicz #endif
865476af299SMike Frysinger 	return image_check_arch(hdr, IH_ARCH_DEFAULT);
866b97a2a0aSMarian Balakowicz }
8675dfb5213SMarian Balakowicz #endif /* USE_HOSTCC */
8685b1d7137Swdenk 
86913d06981SSimon Glass /**
87013d06981SSimon Glass  * Set up properties in the FDT
87113d06981SSimon Glass  *
87213d06981SSimon Glass  * This sets up properties in the FDT that is to be passed to linux.
87313d06981SSimon Glass  *
87413d06981SSimon Glass  * @images:	Images information
87513d06981SSimon Glass  * @blob:	FDT to update
87613d06981SSimon Glass  * @of_size:	Size of the FDT
87713d06981SSimon Glass  * @lmb:	Points to logical memory block structure
87813d06981SSimon Glass  * @return 0 if ok, <0 on failure
87913d06981SSimon Glass  */
88013d06981SSimon Glass int image_setup_libfdt(bootm_headers_t *images, void *blob,
88113d06981SSimon Glass 		       int of_size, struct lmb *lmb);
88213d06981SSimon Glass 
88313d06981SSimon Glass /**
88413d06981SSimon Glass  * Set up the FDT to use for booting a kernel
88513d06981SSimon Glass  *
88613d06981SSimon Glass  * This performs ramdisk setup, sets up the FDT if required, and adds
88713d06981SSimon Glass  * paramters to the FDT if libfdt is available.
88813d06981SSimon Glass  *
88913d06981SSimon Glass  * @param images	Images information
89013d06981SSimon Glass  * @return 0 if ok, <0 on failure
89113d06981SSimon Glass  */
89213d06981SSimon Glass int image_setup_linux(bootm_headers_t *images);
89313d06981SSimon Glass 
894a5266d6bSSimon Glass /**
895a5266d6bSSimon Glass  * bootz_setup() - Extract stat and size of a Linux xImage
896a5266d6bSSimon Glass  *
897a5266d6bSSimon Glass  * @image: Address of image
898a5266d6bSSimon Glass  * @start: Returns start address of image
899a5266d6bSSimon Glass  * @end : Returns end address of image
900a5266d6bSSimon Glass  * @return 0 if OK, 1 if the image was not recognised
901a5266d6bSSimon Glass  */
902a5266d6bSSimon Glass int bootz_setup(ulong image, ulong *start, ulong *end);
903a5266d6bSSimon Glass 
904a5266d6bSSimon Glass 
905d5934ad7SMarian Balakowicz /*******************************************************************/
9069a4daad0SMarian Balakowicz /* New uImage format specific code (prefixed with fit_) */
907d5934ad7SMarian Balakowicz /*******************************************************************/
9085dfb5213SMarian Balakowicz 
9095dfb5213SMarian Balakowicz #define FIT_IMAGES_PATH		"/images"
9105dfb5213SMarian Balakowicz #define FIT_CONFS_PATH		"/configurations"
9115dfb5213SMarian Balakowicz 
9123e569a6bSSimon Glass /* hash/signature node */
9135dfb5213SMarian Balakowicz #define FIT_HASH_NODENAME	"hash"
9145dfb5213SMarian Balakowicz #define FIT_ALGO_PROP		"algo"
9155dfb5213SMarian Balakowicz #define FIT_VALUE_PROP		"value"
9168ac88f2dSJoe Hershberger #define FIT_IGNORE_PROP		"uboot-ignore"
9173e569a6bSSimon Glass #define FIT_SIG_NODENAME	"signature"
9185dfb5213SMarian Balakowicz 
9191427f7c6SJoseph Chen /* cipher node */
9201427f7c6SJoseph Chen #define FIT_CIPHER_NODENAME	"cipher"
9211427f7c6SJoseph Chen #define FIT_ALGO_PROP		"algo"
9221427f7c6SJoseph Chen 
9235dfb5213SMarian Balakowicz /* image node */
9245dfb5213SMarian Balakowicz #define FIT_DATA_PROP		"data"
925b86dc419SPeng Fan #define FIT_DATA_POSITION_PROP	"data-position"
926db1b79b8Stomas.melin@vaisala.com #define FIT_DATA_OFFSET_PROP	"data-offset"
927db1b79b8Stomas.melin@vaisala.com #define FIT_DATA_SIZE_PROP	"data-size"
9285dfb5213SMarian Balakowicz #define FIT_TIMESTAMP_PROP	"timestamp"
929c516743fSJoseph Chen #define FIT_TOTALSIZE_PROP	"totalsize"
930ad07c38fSJoseph Chen #define FIT_VERSION_PROP	"version"
9315dfb5213SMarian Balakowicz #define FIT_DESC_PROP		"description"
9325dfb5213SMarian Balakowicz #define FIT_ARCH_PROP		"arch"
9335dfb5213SMarian Balakowicz #define FIT_TYPE_PROP		"type"
9345dfb5213SMarian Balakowicz #define FIT_OS_PROP		"os"
9355dfb5213SMarian Balakowicz #define FIT_COMP_PROP		"compression"
9367385816bSJoseph Chen #define FIT_COMP_ADDR_PROP	"comp"
9378f805b78SXuhui Lin #define FIT_CIPHER_ADDR_PROP	"cipher"
9385dfb5213SMarian Balakowicz #define FIT_ENTRY_PROP		"entry"
9395dfb5213SMarian Balakowicz #define FIT_LOAD_PROP		"load"
9402201a451SJason Zhu #define FIT_PRE_LOAD_PROP	"preload"
941d871c071SJoseph Chen #define FIT_ROLLBACK_PROP	"rollback-index"
9425dfb5213SMarian Balakowicz 
9435dfb5213SMarian Balakowicz /* configuration node */
9445dfb5213SMarian Balakowicz #define FIT_KERNEL_PROP		"kernel"
9455dfb5213SMarian Balakowicz #define FIT_RAMDISK_PROP	"ramdisk"
9465dfb5213SMarian Balakowicz #define FIT_FDT_PROP		"fdt"
947ac459efdSJoseph Chen #define FIT_MULTI_PROP		"multi"
948ecf8cd65SKarl Apsite #define FIT_LOADABLE_PROP	"loadables"
9495dfb5213SMarian Balakowicz #define FIT_DEFAULT_PROP	"default"
95090268b87SSimon Glass #define FIT_SETUP_PROP		"setup"
951ed0cea7cSMichal Simek #define FIT_FPGA_PROP		"fpga"
952c04fe8bfSMichal Simek #define FIT_FIRMWARE_PROP	"firmware"
953f2413b2fSMarek Vasut #define FIT_STANDALONE_PROP	"standalone"
9545dfb5213SMarian Balakowicz 
9551de7bb4fSMichael van der Westhuizen #define FIT_MAX_HASH_LEN	HASH_MAX_DIGEST_SIZE
9563e9875cdSJoseph Chen #define FIT_MAX_SPL_IMAGE_SZ	SZ_2M
9575dfb5213SMarian Balakowicz 
958f1dcee59SSimon Glass #if IMAGE_ENABLE_FIT
959acd43290SJoseph Chen 
960acd43290SJoseph Chen #ifndef IMAGE_ALIGN_SIZE
961acd43290SJoseph Chen #define IMAGE_ALIGN_SIZE	512
962acd43290SJoseph Chen #endif
963acd43290SJoseph Chen #define FIT_ALIGN(x)		(((x)+IMAGE_ALIGN_SIZE-1)&~(IMAGE_ALIGN_SIZE-1))
964acd43290SJoseph Chen 
9657a137075SJoseph Chen /* fit rollback index file description magic */
9667a137075SJoseph Chen #define FIT_ROLLBACK_INDEX	0xf1de0001
9677a137075SJoseph Chen #define FIT_ROLLBACK_INDEX_SPL	0xf1de8002
9687a137075SJoseph Chen 
9695dfb5213SMarian Balakowicz /* cmdline argument format parsing */
970314f634bSMike Frysinger int fit_parse_conf(const char *spec, ulong addr_curr,
971f50433d6SMarian Balakowicz 		ulong *addr, const char **conf_name);
972314f634bSMike Frysinger int fit_parse_subimage(const char *spec, ulong addr_curr,
973f50433d6SMarian Balakowicz 		ulong *addr, const char **image_name);
974d5934ad7SMarian Balakowicz 
97539931f96SGuilherme Maciel Ferreira int fit_get_subimage_count(const void *fit, int images_noffset);
976edbed247SBartlomiej Sieka void fit_print_contents(const void *fit);
9775dfb5213SMarian Balakowicz void fit_image_print(const void *fit, int noffset, const char *p);
9785dfb5213SMarian Balakowicz 
9795dfb5213SMarian Balakowicz /**
9805dfb5213SMarian Balakowicz  * fit_get_end - get FIT image size
9815dfb5213SMarian Balakowicz  * @fit: pointer to the FIT format image header
9825dfb5213SMarian Balakowicz  *
9835dfb5213SMarian Balakowicz  * returns:
9845dfb5213SMarian Balakowicz  *     size of the FIT image (blob) in memory
9855dfb5213SMarian Balakowicz  */
fit_get_size(const void * fit)9865dfb5213SMarian Balakowicz static inline ulong fit_get_size(const void *fit)
9875dfb5213SMarian Balakowicz {
9885dfb5213SMarian Balakowicz 	return fdt_totalsize(fit);
9895dfb5213SMarian Balakowicz }
9905dfb5213SMarian Balakowicz 
9915dfb5213SMarian Balakowicz /**
9925dfb5213SMarian Balakowicz  * fit_get_end - get FIT image end
9935dfb5213SMarian Balakowicz  * @fit: pointer to the FIT format image header
9945dfb5213SMarian Balakowicz  *
9955dfb5213SMarian Balakowicz  * returns:
9965dfb5213SMarian Balakowicz  *     end address of the FIT image (blob) in memory
9975dfb5213SMarian Balakowicz  */
9987a80de46SSimon Glass ulong fit_get_end(const void *fit);
9995dfb5213SMarian Balakowicz 
10005dfb5213SMarian Balakowicz /**
10015dfb5213SMarian Balakowicz  * fit_get_name - get FIT node name
10025dfb5213SMarian Balakowicz  * @fit: pointer to the FIT format image header
10035dfb5213SMarian Balakowicz  *
10045dfb5213SMarian Balakowicz  * returns:
10055dfb5213SMarian Balakowicz  *     NULL, on error
10065dfb5213SMarian Balakowicz  *     pointer to node name, on success
10075dfb5213SMarian Balakowicz  */
fit_get_name(const void * fit_hdr,int noffset,int * len)10085dfb5213SMarian Balakowicz static inline const char *fit_get_name(const void *fit_hdr,
10095dfb5213SMarian Balakowicz 		int noffset, int *len)
10105dfb5213SMarian Balakowicz {
10115dfb5213SMarian Balakowicz 	return fdt_get_name(fit_hdr, noffset, len);
10125dfb5213SMarian Balakowicz }
10135dfb5213SMarian Balakowicz 
10145dfb5213SMarian Balakowicz int fit_get_desc(const void *fit, int noffset, char **desc);
10155dfb5213SMarian Balakowicz int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp);
10164fe117beSJoseph Chen int fit_get_totalsize(const void *fit, int *totalsize);
10175dfb5213SMarian Balakowicz 
10185dfb5213SMarian Balakowicz int fit_image_get_node(const void *fit, const char *image_uname);
10195dfb5213SMarian Balakowicz int fit_image_get_os(const void *fit, int noffset, uint8_t *os);
10205dfb5213SMarian Balakowicz int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch);
10215dfb5213SMarian Balakowicz int fit_image_get_type(const void *fit, int noffset, uint8_t *type);
10225dfb5213SMarian Balakowicz int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp);
10232201a451SJason Zhu bool fit_image_is_preload(const void *fit, int noffset);
10245dfb5213SMarian Balakowicz int fit_image_get_load(const void *fit, int noffset, ulong *load);
10255dfb5213SMarian Balakowicz int fit_image_get_entry(const void *fit, int noffset, ulong *entry);
10267385816bSJoseph Chen int fit_image_get_comp_addr(const void *fit, int noffset, ulong *comp);
10278f805b78SXuhui Lin int fit_image_get_cipher_addr(const void *fit, int noffset, ulong *cipher);
1028ac459efdSJoseph Chen int fit_image_set_load(const void *fit, int noffset, ulong load);
1029ac459efdSJoseph Chen int fit_image_set_entry(const void *fit, int noffset, ulong entry);
10305dfb5213SMarian Balakowicz int fit_image_get_data(const void *fit, int noffset,
10315dfb5213SMarian Balakowicz 				const void **data, size_t *size);
1032db1b79b8Stomas.melin@vaisala.com int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset);
1033b86dc419SPeng Fan int fit_image_get_data_position(const void *fit, int noffset,
1034b86dc419SPeng Fan 				int *data_position);
1035db1b79b8Stomas.melin@vaisala.com int fit_image_get_data_size(const void *fit, int noffset, int *data_size);
1036d871c071SJoseph Chen int fit_image_get_rollback_index(const void *fit, int noffset, uint32_t *index);
10375dfb5213SMarian Balakowicz 
10385dfb5213SMarian Balakowicz int fit_image_hash_get_algo(const void *fit, int noffset, char **algo);
10395dfb5213SMarian Balakowicz int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
10405dfb5213SMarian Balakowicz 				int *value_len);
1041d4229e77SJoseph Chen int fit_image_check_hash(const void *fit, int noffset, const void *data,
1042d4229e77SJoseph Chen 			 size_t size, char **err_msgp);
10435dfb5213SMarian Balakowicz 
10445dfb5213SMarian Balakowicz int fit_set_timestamp(void *fit, int noffset, time_t timestamp);
1045ca0130caSJoseph Chen int fit_set_totalsize(void *fit, int noffset, int totalsize);
1046ad07c38fSJoseph Chen int fit_set_version(void *fit, int noffset, int version);
1047bbb467dcSSimon Glass 
1048bbb467dcSSimon Glass /**
104956518e71SSimon Glass  * fit_add_verification_data() - add verification data to FIT image nodes
1050bbb467dcSSimon Glass  *
105156518e71SSimon Glass  * @keydir:	Directory containing keys
105256518e71SSimon Glass  * @kwydest:	FDT blob to write public key information to
105356518e71SSimon Glass  * @fit:	Pointer to the FIT format image header
105456518e71SSimon Glass  * @comment:	Comment to add to signature nodes
105556518e71SSimon Glass  * @require_keys: Mark all keys as 'required'
1056f1ca1fdeSGeorge McCollister  * @engine_id:	Engine to use for signing
105756518e71SSimon Glass  *
105856518e71SSimon Glass  * Adds hash values for all component images in the FIT blob.
105956518e71SSimon Glass  * Hashes are calculated for all component images which have hash subnodes
106056518e71SSimon Glass  * with algorithm property set to one of the supported hash algorithms.
106156518e71SSimon Glass  *
106256518e71SSimon Glass  * Also add signatures if signature nodes are present.
106356518e71SSimon Glass  *
106456518e71SSimon Glass  * returns
106556518e71SSimon Glass  *     0, on success
106656518e71SSimon Glass  *     libfdt error code, on failure
1067bbb467dcSSimon Glass  */
106856518e71SSimon Glass int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
1069f1ca1fdeSGeorge McCollister 			      const char *comment, int require_keys,
1070f1ca1fdeSGeorge McCollister 			      const char *engine_id);
10715dfb5213SMarian Balakowicz 
10720833bf5eSJun Nie int fit_image_verify_with_data(const void *fit, int image_noffset,
10730833bf5eSJun Nie 			       const void *data, size_t size);
1074b8da8366SSimon Glass int fit_image_verify(const void *fit, int noffset);
1075782cfbb2SSimon Glass int fit_config_verify(const void *fit, int conf_noffset);
1076b8da8366SSimon Glass int fit_all_image_verify(const void *fit);
10771f452cbfSJoseph Chen int fit_board_verify_required_sigs(void);
10781f452cbfSJoseph Chen 
10795dfb5213SMarian Balakowicz int fit_image_check_os(const void *fit, int noffset, uint8_t os);
10805dfb5213SMarian Balakowicz int fit_image_check_arch(const void *fit, int noffset, uint8_t arch);
10815dfb5213SMarian Balakowicz int fit_image_check_type(const void *fit, int noffset, uint8_t type);
10825dfb5213SMarian Balakowicz int fit_image_check_comp(const void *fit, int noffset, uint8_t comp);
10835dfb5213SMarian Balakowicz int fit_check_format(const void *fit);
10845dfb5213SMarian Balakowicz 
1085d95f6ec7SGabe Black int fit_conf_find_compat(const void *fit, const void *fdt);
10865dfb5213SMarian Balakowicz int fit_conf_get_node(const void *fit, const char *conf_uname);
10879db5ffebSJoseph Chen 
10889db5ffebSJoseph Chen /* __weak function */
10897a137075SJoseph Chen int fit_rollback_index_verify(const void *fit, uint32_t rollback_fd,
1090e5ca21e8SJoseph Chen 			      uint32_t *fit_index, uint32_t *otp_index);
10919db5ffebSJoseph Chen int fit_read_otp_rollback_index(uint32_t fit_index, uint32_t *otp_index);
10925dfb5213SMarian Balakowicz 
1093003efd7dSSimon Glass /**
1094003efd7dSSimon Glass  * fit_conf_get_prop_node() - Get node refered to by a configuration
1095003efd7dSSimon Glass  * @fit:	FIT to check
1096003efd7dSSimon Glass  * @noffset:	Offset of conf@xxx node to check
1097003efd7dSSimon Glass  * @prop_name:	Property to read from the conf node
1098003efd7dSSimon Glass  *
1099003efd7dSSimon Glass  * The conf@ nodes contain references to other nodes, using properties
1100003efd7dSSimon Glass  * like 'kernel = "kernel@1"'. Given such a property name (e.g. "kernel"),
1101003efd7dSSimon Glass  * return the offset of the node referred to (e.g. offset of node
1102003efd7dSSimon Glass  * "/images/kernel@1".
1103003efd7dSSimon Glass  */
1104003efd7dSSimon Glass int fit_conf_get_prop_node(const void *fit, int noffset,
1105003efd7dSSimon Glass 		const char *prop_name);
1106003efd7dSSimon Glass 
1107583377c4SJoseph Chen int fit_conf_get_prop_node_index(const void *fit, int noffset,
1108583377c4SJoseph Chen 		const char *prop_name, int index);
1109583377c4SJoseph Chen 
11105dfb5213SMarian Balakowicz void fit_conf_print(const void *fit, int noffset, const char *p);
11115dfb5213SMarian Balakowicz 
111261a439a8SSimon Glass int fit_check_ramdisk(const void *fit, int os_noffset,
111361a439a8SSimon Glass 		uint8_t arch, int verify);
111461a439a8SSimon Glass 
1115604f23ddSSimon Glass int calculate_hash(const void *data, int data_len, const char *algo,
1116604f23ddSSimon Glass 			uint8_t *value, int *value_len);
1117604f23ddSSimon Glass 
11181427f7c6SJoseph Chen int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo);
11191427f7c6SJoseph Chen 
1120782cfbb2SSimon Glass /*
11213e569a6bSSimon Glass  * At present we only support signing on the host, and verification on the
11223e569a6bSSimon Glass  * device
1123782cfbb2SSimon Glass  */
1124782cfbb2SSimon Glass #if defined(CONFIG_FIT_SIGNATURE)
1125782cfbb2SSimon Glass # ifdef USE_HOSTCC
11263e569a6bSSimon Glass #  define IMAGE_ENABLE_SIGN	1
112729a23f9dSHeiko Schocher #  define IMAGE_ENABLE_VERIFY	1
1128646257d1SHeiko Schocher # include  <openssl/evp.h>
1129782cfbb2SSimon Glass #else
11303e569a6bSSimon Glass #  define IMAGE_ENABLE_SIGN	0
1131782cfbb2SSimon Glass #  define IMAGE_ENABLE_VERIFY	1
1132782cfbb2SSimon Glass # endif
1133782cfbb2SSimon Glass #else
11343e569a6bSSimon Glass # define IMAGE_ENABLE_SIGN	0
1135782cfbb2SSimon Glass # define IMAGE_ENABLE_VERIFY	0
1136782cfbb2SSimon Glass #endif
1137782cfbb2SSimon Glass 
1138782cfbb2SSimon Glass #ifdef USE_HOSTCC
113929a23f9dSHeiko Schocher void *image_get_host_blob(void);
114029a23f9dSHeiko Schocher void image_set_host_blob(void *host_blob);
114129a23f9dSHeiko Schocher # define gd_fdt_blob()		image_get_host_blob()
1142782cfbb2SSimon Glass #else
1143e750e261SJoseph Chen #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USING_KERNEL_DTB)
1144e750e261SJoseph Chen # define gd_fdt_blob()		(gd->ufdt_blob)
1145e750e261SJoseph Chen #else
1146782cfbb2SSimon Glass # define gd_fdt_blob()		(gd->fdt_blob)
1147782cfbb2SSimon Glass #endif
1148e750e261SJoseph Chen #endif
1149782cfbb2SSimon Glass 
1150782cfbb2SSimon Glass #ifdef CONFIG_FIT_BEST_MATCH
1151782cfbb2SSimon Glass #define IMAGE_ENABLE_BEST_MATCH	1
1152782cfbb2SSimon Glass #else
1153782cfbb2SSimon Glass #define IMAGE_ENABLE_BEST_MATCH	0
1154782cfbb2SSimon Glass #endif
1155782cfbb2SSimon Glass 
11563e569a6bSSimon Glass /* Information passed to the signing routines */
11573e569a6bSSimon Glass struct image_sign_info {
11583e569a6bSSimon Glass 	const char *keydir;		/* Directory conaining keys */
11593e569a6bSSimon Glass 	const char *keyname;		/* Name of key to use */
11603e569a6bSSimon Glass 	void *fit;			/* Pointer to FIT blob */
11613e569a6bSSimon Glass 	int node_offset;		/* Offset of signature node */
116283dd98e0SAndrew Duda 	const char *name;		/* Algorithm name */
116383dd98e0SAndrew Duda 	struct checksum_algo *checksum;	/* Checksum algorithm information */
1164219050bfSPhilippe Reynes 	struct padding_algo *padding;	/* Padding algorithm information */
116583dd98e0SAndrew Duda 	struct crypto_algo *crypto;	/* Crypto algorithm information */
11663e569a6bSSimon Glass 	const void *fdt_blob;		/* FDT containing public keys */
11673e569a6bSSimon Glass 	int required_keynode;		/* Node offset of key to use: -1=any */
11683e569a6bSSimon Glass 	const char *require_keys;	/* Value for 'required' property */
1169f1ca1fdeSGeorge McCollister 	const char *engine_id;		/* Engine to use for signing */
11703e569a6bSSimon Glass };
117173223f0eSSimon Glass #endif /* Allow struct image_region to always be defined for rsa.h */
11723e569a6bSSimon Glass 
11733e569a6bSSimon Glass /* A part of an image, used for hashing */
11743e569a6bSSimon Glass struct image_region {
11753e569a6bSSimon Glass 	const void *data;
11763e569a6bSSimon Glass 	int size;
11773e569a6bSSimon Glass };
11783e569a6bSSimon Glass 
117973223f0eSSimon Glass #if IMAGE_ENABLE_FIT
118073223f0eSSimon Glass 
1181646257d1SHeiko Schocher #if IMAGE_ENABLE_VERIFY
11822b9912e6SJeroen Hofstee # include <u-boot/rsa-checksum.h>
1183646257d1SHeiko Schocher #endif
1184646257d1SHeiko Schocher struct checksum_algo {
1185646257d1SHeiko Schocher 	const char *name;
1186646257d1SHeiko Schocher 	const int checksum_len;
1187da29f299SAndrew Duda 	const int der_len;
1188da29f299SAndrew Duda 	const uint8_t *der_prefix;
1189646257d1SHeiko Schocher #if IMAGE_ENABLE_SIGN
119029a23f9dSHeiko Schocher 	const EVP_MD *(*calculate_sign)(void);
119129a23f9dSHeiko Schocher #endif
1192b37b46f0SRuchika Gupta 	int (*calculate)(const char *name,
1193b37b46f0SRuchika Gupta 			 const struct image_region region[],
1194646257d1SHeiko Schocher 			 int region_count, uint8_t *checksum);
1195646257d1SHeiko Schocher };
1196646257d1SHeiko Schocher 
11970c1d74fdSAndrew Duda struct crypto_algo {
11983e569a6bSSimon Glass 	const char *name;		/* Name of algorithm */
11990c1d74fdSAndrew Duda 	const int key_len;
12003e569a6bSSimon Glass 
12013e569a6bSSimon Glass 	/**
12023e569a6bSSimon Glass 	 * sign() - calculate and return signature for given input data
12033e569a6bSSimon Glass 	 *
12043e569a6bSSimon Glass 	 * @info:	Specifies key and FIT information
12053e569a6bSSimon Glass 	 * @data:	Pointer to the input data
12063e569a6bSSimon Glass 	 * @data_len:	Data length
12073e569a6bSSimon Glass 	 * @sigp:	Set to an allocated buffer holding the signature
12083e569a6bSSimon Glass 	 * @sig_len:	Set to length of the calculated hash
12093e569a6bSSimon Glass 	 *
12103e569a6bSSimon Glass 	 * This computes input data signature according to selected algorithm.
12113e569a6bSSimon Glass 	 * Resulting signature value is placed in an allocated buffer, the
12123e569a6bSSimon Glass 	 * pointer is returned as *sigp. The length of the calculated
12133e569a6bSSimon Glass 	 * signature is returned via the sig_len pointer argument. The caller
12143e569a6bSSimon Glass 	 * should free *sigp.
12153e569a6bSSimon Glass 	 *
12163e569a6bSSimon Glass 	 * @return: 0, on success, -ve on error
12173e569a6bSSimon Glass 	 */
12183e569a6bSSimon Glass 	int (*sign)(struct image_sign_info *info,
12193e569a6bSSimon Glass 		    const struct image_region region[],
12203e569a6bSSimon Glass 		    int region_count, uint8_t **sigp, uint *sig_len);
12213e569a6bSSimon Glass 
12223e569a6bSSimon Glass 	/**
12233e569a6bSSimon Glass 	 * add_verify_data() - Add verification information to FDT
12243e569a6bSSimon Glass 	 *
12253e569a6bSSimon Glass 	 * Add public key information to the FDT node, suitable for
12263e569a6bSSimon Glass 	 * verification at run-time. The information added depends on the
12273e569a6bSSimon Glass 	 * algorithm being used.
12283e569a6bSSimon Glass 	 *
12293e569a6bSSimon Glass 	 * @info:	Specifies key and FIT information
12303e569a6bSSimon Glass 	 * @keydest:	Destination FDT blob for public key data
12313e569a6bSSimon Glass 	 * @return: 0, on success, -ve on error
12323e569a6bSSimon Glass 	 */
12333e569a6bSSimon Glass 	int (*add_verify_data)(struct image_sign_info *info, void *keydest);
12343e569a6bSSimon Glass 
12353e569a6bSSimon Glass 	/**
12363e569a6bSSimon Glass 	 * verify() - Verify a signature against some data
12373e569a6bSSimon Glass 	 *
12383e569a6bSSimon Glass 	 * @info:	Specifies key and FIT information
12393e569a6bSSimon Glass 	 * @data:	Pointer to the input data
12403e569a6bSSimon Glass 	 * @data_len:	Data length
12413e569a6bSSimon Glass 	 * @sig:	Signature
12423e569a6bSSimon Glass 	 * @sig_len:	Number of bytes in signature
12433e569a6bSSimon Glass 	 * @return 0 if verified, -ve on error
12443e569a6bSSimon Glass 	 */
12453e569a6bSSimon Glass 	int (*verify)(struct image_sign_info *info,
12463e569a6bSSimon Glass 		      const struct image_region region[], int region_count,
12473e569a6bSSimon Glass 		      uint8_t *sig, uint sig_len);
12480c1d74fdSAndrew Duda };
1249646257d1SHeiko Schocher 
1250219050bfSPhilippe Reynes struct padding_algo {
1251219050bfSPhilippe Reynes 	const char *name;
1252219050bfSPhilippe Reynes 	int (*verify)(struct image_sign_info *info,
1253219050bfSPhilippe Reynes 		      uint8_t *pad, int pad_len,
1254219050bfSPhilippe Reynes 		      const uint8_t *hash, int hash_len);
1255219050bfSPhilippe Reynes };
1256219050bfSPhilippe Reynes 
12573e569a6bSSimon Glass /**
125883dd98e0SAndrew Duda  * image_get_checksum_algo() - Look up a checksum algorithm
12593e569a6bSSimon Glass  *
126083dd98e0SAndrew Duda  * @param full_name	Name of algorithm in the form "checksum,crypto"
12613e569a6bSSimon Glass  * @return pointer to algorithm information, or NULL if not found
12623e569a6bSSimon Glass  */
126383dd98e0SAndrew Duda struct checksum_algo *image_get_checksum_algo(const char *full_name);
126483dd98e0SAndrew Duda 
126583dd98e0SAndrew Duda /**
126683dd98e0SAndrew Duda  * image_get_crypto_algo() - Look up a cryptosystem algorithm
126783dd98e0SAndrew Duda  *
126883dd98e0SAndrew Duda  * @param full_name	Name of algorithm in the form "checksum,crypto"
126983dd98e0SAndrew Duda  * @return pointer to algorithm information, or NULL if not found
127083dd98e0SAndrew Duda  */
127183dd98e0SAndrew Duda struct crypto_algo *image_get_crypto_algo(const char *full_name);
12723e569a6bSSimon Glass 
127356518e71SSimon Glass /**
1274219050bfSPhilippe Reynes  * image_get_padding_algo() - Look up a padding algorithm
1275219050bfSPhilippe Reynes  *
1276219050bfSPhilippe Reynes  * @param name		Name of padding algorithm
1277219050bfSPhilippe Reynes  * @return pointer to algorithm information, or NULL if not found
1278219050bfSPhilippe Reynes  */
1279219050bfSPhilippe Reynes struct padding_algo *image_get_padding_algo(const char *name);
1280219050bfSPhilippe Reynes 
1281219050bfSPhilippe Reynes /**
128256518e71SSimon Glass  * fit_image_verify_required_sigs() - Verify signatures marked as 'required'
128356518e71SSimon Glass  *
128456518e71SSimon Glass  * @fit:		FIT to check
128556518e71SSimon Glass  * @image_noffset:	Offset of image node to check
128656518e71SSimon Glass  * @data:		Image data to check
128756518e71SSimon Glass  * @size:		Size of image data
128856518e71SSimon Glass  * @sig_blob:		FDT containing public keys
128956518e71SSimon Glass  * @no_sigsp:		Returns 1 if no signatures were required, and
129056518e71SSimon Glass  *			therefore nothing was checked. The caller may wish
129156518e71SSimon Glass  *			to fall back to other mechanisms, or refuse to
129256518e71SSimon Glass  *			boot.
129356518e71SSimon Glass  * @return 0 if all verified ok, <0 on error
129456518e71SSimon Glass  */
129556518e71SSimon Glass int fit_image_verify_required_sigs(const void *fit, int image_noffset,
129656518e71SSimon Glass 		const char *data, size_t size, const void *sig_blob,
129756518e71SSimon Glass 		int *no_sigsp);
129856518e71SSimon Glass 
129956518e71SSimon Glass /**
130056518e71SSimon Glass  * fit_image_check_sig() - Check a single image signature node
130156518e71SSimon Glass  *
130256518e71SSimon Glass  * @fit:		FIT to check
130356518e71SSimon Glass  * @noffset:		Offset of signature node to check
130456518e71SSimon Glass  * @data:		Image data to check
130556518e71SSimon Glass  * @size:		Size of image data
130656518e71SSimon Glass  * @required_keynode:	Offset in the control FDT of the required key node,
130756518e71SSimon Glass  *			if any. If this is given, then the image wil not
130856518e71SSimon Glass  *			pass verification unless that key is used. If this is
130956518e71SSimon Glass  *			-1 then any signature will do.
131056518e71SSimon Glass  * @err_msgp:		In the event of an error, this will be pointed to a
131156518e71SSimon Glass  *			help error string to display to the user.
131256518e71SSimon Glass  * @return 0 if all verified ok, <0 on error
131356518e71SSimon Glass  */
131456518e71SSimon Glass int fit_image_check_sig(const void *fit, int noffset, const void *data,
131556518e71SSimon Glass 		size_t size, int required_keynode, char **err_msgp);
131656518e71SSimon Glass 
13174d098529SSimon Glass /**
13184d098529SSimon Glass  * fit_region_make_list() - Make a list of regions to hash
13194d098529SSimon Glass  *
13204d098529SSimon Glass  * Given a list of FIT regions (offset, size) provided by libfdt, create
13214d098529SSimon Glass  * a list of regions (void *, size) for use by the signature creationg
13224d098529SSimon Glass  * and verification code.
13234d098529SSimon Glass  *
13244d098529SSimon Glass  * @fit:		FIT image to process
13254d098529SSimon Glass  * @fdt_regions:	Regions as returned by libfdt
13264d098529SSimon Glass  * @count:		Number of regions returned by libfdt
13274d098529SSimon Glass  * @region:		Place to put list of regions (NULL to allocate it)
13284d098529SSimon Glass  * @return pointer to list of regions, or NULL if out of memory
13294d098529SSimon Glass  */
13304d098529SSimon Glass struct image_region *fit_region_make_list(const void *fit,
13314d098529SSimon Glass 		struct fdt_region *fdt_regions, int count,
13324d098529SSimon Glass 		struct image_region *region);
133356518e71SSimon Glass 
fit_image_check_target_arch(const void * fdt,int node)13345dfb5213SMarian Balakowicz static inline int fit_image_check_target_arch(const void *fdt, int node)
13355dfb5213SMarian Balakowicz {
133629a23f9dSHeiko Schocher #ifndef USE_HOSTCC
13377566832aSThierry Reding 	return fit_image_check_arch(fdt, node, IH_ARCH_DEFAULT);
133829a23f9dSHeiko Schocher #else
133929a23f9dSHeiko Schocher 	return 0;
134029a23f9dSHeiko Schocher #endif
13415dfb5213SMarian Balakowicz }
13425dfb5213SMarian Balakowicz 
1343d5934ad7SMarian Balakowicz #ifdef CONFIG_FIT_VERBOSE
1344d5934ad7SMarian Balakowicz #define fit_unsupported(msg)	printf("! %s:%d " \
1345d5934ad7SMarian Balakowicz 				"FIT images not supported for '%s'\n", \
1346d5934ad7SMarian Balakowicz 				__FILE__, __LINE__, (msg))
1347d5934ad7SMarian Balakowicz 
1348d5934ad7SMarian Balakowicz #define fit_unsupported_reset(msg)	printf("! %s:%d " \
1349d5934ad7SMarian Balakowicz 				"FIT images not supported for '%s' " \
1350d5934ad7SMarian Balakowicz 				"- must reset board to recover!\n", \
1351d5934ad7SMarian Balakowicz 				__FILE__, __LINE__, (msg))
1352d5934ad7SMarian Balakowicz #else
1353d5934ad7SMarian Balakowicz #define fit_unsupported(msg)
1354d5934ad7SMarian Balakowicz #define fit_unsupported_reset(msg)
1355d5934ad7SMarian Balakowicz #endif /* CONFIG_FIT_VERBOSE */
1356f50433d6SMarian Balakowicz #endif /* CONFIG_FIT */
13575ad03eb3SMarian Balakowicz 
13589ace3fc8SSebastian Siewior #if defined(CONFIG_ANDROID_BOOT_IMAGE)
135928317110SJoseph Chen #include <android_image.h>
136028317110SJoseph Chen 
13619ace3fc8SSebastian Siewior struct andr_img_hdr;
1362c092b139SJoseph Chen u32 android_bcb_msg_sector_offset(void);
13636ea28a6cSJoseph Chen int android_image_init_resource(struct blk_desc *desc,
13646ea28a6cSJoseph Chen 				disk_partition_t *out_part,
13656ea28a6cSJoseph Chen 				ulong *out_blk_offset);
13669ace3fc8SSebastian Siewior int android_image_check_header(const struct andr_img_hdr *hdr);
13679ace3fc8SSebastian Siewior int android_image_get_kernel(const struct andr_img_hdr *hdr, int verify,
13689ace3fc8SSebastian Siewior 			     ulong *os_data, ulong *os_len);
13699ace3fc8SSebastian Siewior int android_image_get_ramdisk(const struct andr_img_hdr *hdr,
13709ace3fc8SSebastian Siewior 			      ulong *rd_data, ulong *rd_len);
1371270f9eacSKever Yang int android_image_get_fdt(const struct andr_img_hdr *hdr,
1372270f9eacSKever Yang 			      ulong *rd_data);
1373008aee87SAndy Yan u32 android_image_get_comp(const struct andr_img_hdr *hdr);
13749ace3fc8SSebastian Siewior ulong android_image_get_end(const struct andr_img_hdr *hdr);
13759ace3fc8SSebastian Siewior ulong android_image_get_kload(const struct andr_img_hdr *hdr);
13764f1318b2SMichael Trimarchi void android_print_contents(const struct andr_img_hdr *hdr);
13779ace3fc8SSebastian Siewior 
1378e1b9a842SJoseph Chen void android_image_set_decomp(struct andr_img_hdr *hdr, int comp);
1379e1b9a842SJoseph Chen int android_image_parse_comp(struct andr_img_hdr *hdr, ulong *load_addr);
1380e1b9a842SJoseph Chen int android_image_memcpy_separate(struct andr_img_hdr *hdr, ulong *load_address);
138198c12997SJoseph Chen 
1382745aeb1aSJoseph Chen struct andr_img_hdr *populate_andr_img_hdr(struct blk_desc *dev_desc,
1383745aeb1aSJoseph Chen 					   disk_partition_t *part_boot);
138428317110SJoseph Chen int populate_boot_info(const struct boot_img_hdr_v34 *boot_hdr,
138528317110SJoseph Chen 		       const struct vendor_boot_img_hdr_v34 *vendor_boot_hdr,
138628317110SJoseph Chen 		       const struct boot_img_hdr_v34 *init_boot_hdr,
138728317110SJoseph Chen 		       struct andr_img_hdr *hdr, bool save_hdr);
1388745aeb1aSJoseph Chen 
138976e2a5a6SAlex Deymo /** android_image_load - Load an Android Image from storage.
139076e2a5a6SAlex Deymo  *
1391008aee87SAndy Yan  * Load an Android Image based on the header size in the storage.
1392008aee87SAndy Yan  * Return the final load address, which could be a different address
1393008aee87SAndy Yan  * of argument load_address, if the Kernel Image is compressed. In case
1394008aee87SAndy Yan  * of error reading the image or if the image size needed to be read
1395008aee87SAndy Yan  * from disk is bigger than the passed |max_size| a negative number
1396008aee87SAndy Yan  * is returned.
139776e2a5a6SAlex Deymo  *
139876e2a5a6SAlex Deymo  * @dev_desc:		The device where to read the image from
139976e2a5a6SAlex Deymo  * @part_info:		The partition in |dev_desc| where to read the image from
140076e2a5a6SAlex Deymo  * @load_address:	The address where the image will be loaded
140176e2a5a6SAlex Deymo  * @max_size:		The maximum loaded size, in bytes
1402008aee87SAndy Yan  * @return the final load address or a negative number in case of error.
140376e2a5a6SAlex Deymo  */
140476e2a5a6SAlex Deymo long android_image_load(struct blk_desc *dev_desc,
140576e2a5a6SAlex Deymo 			const disk_partition_t *part_info,
140676e2a5a6SAlex Deymo 			unsigned long load_address,
140776e2a5a6SAlex Deymo 			unsigned long max_size);
140876e2a5a6SAlex Deymo 
14096527836dSJoseph Chen int android_image_load_by_partname(struct blk_desc *dev_desc,
14106527836dSJoseph Chen 				   const char *boot_partname,
14116527836dSJoseph Chen 				   unsigned long *load_address);
141236e836f2SJoseph Chen 
141336e836f2SJoseph Chen int android_image_verify_resource(const char *boot_part, ulong *resc_buf);
141436e836f2SJoseph Chen 
14159ace3fc8SSebastian Siewior #endif /* CONFIG_ANDROID_BOOT_IMAGE */
14169ace3fc8SSebastian Siewior 
1417008aee87SAndy Yan int bootm_parse_comp(const unsigned char *hdr);
1418008aee87SAndy Yan 
14194b307f23SSimon Glass /**
14204b307f23SSimon Glass  * board_fit_config_name_match() - Check for a matching board name
14214b307f23SSimon Glass  *
14224b307f23SSimon Glass  * This is used when SPL loads a FIT containing multiple device tree files
14234b307f23SSimon Glass  * and wants to work out which one to use. The description of each one is
14244b307f23SSimon Glass  * passed to this function. The description comes from the 'description' field
14254b307f23SSimon Glass  * in each (FDT) image node.
14264b307f23SSimon Glass  *
14274b307f23SSimon Glass  * @name: Device tree description
14284b307f23SSimon Glass  * @return 0 if this device tree should be used, non-zero to try the next
14294b307f23SSimon Glass  */
14304b307f23SSimon Glass int board_fit_config_name_match(const char *name);
14314b307f23SSimon Glass 
1432a5c7e41bSMadan Srinivas #if defined(CONFIG_SPL_FIT_IMAGE_POST_PROCESS) || \
1433a5c7e41bSMadan Srinivas 	defined(CONFIG_FIT_IMAGE_POST_PROCESS)
1434da74d1f3SDaniel Allred /**
1435da74d1f3SDaniel Allred  * board_fit_image_post_process() - Do any post-process on FIT binary data
1436da74d1f3SDaniel Allred  *
1437da74d1f3SDaniel Allred  * This is used to do any sort of image manipulation, verification, decryption
1438da74d1f3SDaniel Allred  * etc. in a platform or board specific way. Obviously, anything done here would
1439da74d1f3SDaniel Allred  * need to be comprehended in how the images were prepared before being injected
1440da74d1f3SDaniel Allred  * into the FIT creation (i.e. the binary blobs would have been pre-processed
1441da74d1f3SDaniel Allred  * before being added to the FIT image).
1442da74d1f3SDaniel Allred  *
14439c633288SJoseph Chen  * @fit: fit blob
1444da74d1f3SDaniel Allred  * @image: pointer to the image start pointer
14459c633288SJoseph Chen  * @load_addr: load address pointer to image(Uncompressed)
14469c633288SJoseph Chen  * @src_addr: source address pointer to image(Compressed maybe)
1447da74d1f3SDaniel Allred  * @size: pointer to the image size
14489e67e3ddSJoseph Chen  * @spec: special data. SPL: struct spl_load_info info, U-Boot: NULL.
14499e67e3ddSJoseph Chen  *
1450*6da15b9fSXuhui Lin  * @return 0 if ok, <0 on failure
1451da74d1f3SDaniel Allred  */
1452*6da15b9fSXuhui Lin int board_fit_image_post_process(void *fit, int node, ulong *load_addr,
14539e67e3ddSJoseph Chen 				  ulong **src_addr, size_t *size, void *spec);
14549c633288SJoseph Chen 
1455ebb7c7ecSXuhui Lin int rk_board_fit_image_post_process(void *fit, int node, ulong *load_addr,
1456f9d8fee0SXuhui Lin 				     ulong **src_addr, size_t *src_len);
1457da74d1f3SDaniel Allred #endif /* CONFIG_SPL_FIT_IMAGE_POST_PROCESS */
1458da74d1f3SDaniel Allred 
14593863f840SCooper Jr., Franklin #define FDT_ERROR	((ulong)(-1))
14603863f840SCooper Jr., Franklin 
146192926bc8SCooper Jr., Franklin ulong fdt_getprop_u32(const void *fdt, int node, const char *prop);
14620079efa1SJean-Jacques Hiblot 
14630079efa1SJean-Jacques Hiblot /**
14640079efa1SJean-Jacques Hiblot  * fit_find_config_node() - Find the node for the best DTB in a FIT image
14650079efa1SJean-Jacques Hiblot  *
14660079efa1SJean-Jacques Hiblot  * A FIT image contains one or more DTBs. This function parses the
14670079efa1SJean-Jacques Hiblot  * configurations described in the FIT images and returns the node of
14680079efa1SJean-Jacques Hiblot  * the first matching DTB. To check if a DTB matches a board, this function
14690079efa1SJean-Jacques Hiblot  * calls board_fit_config_name_match(). If no matching DTB is found, it returns
14700079efa1SJean-Jacques Hiblot  * the node described by the default configuration if it exists.
14710079efa1SJean-Jacques Hiblot  *
14720079efa1SJean-Jacques Hiblot  * @fdt: pointer to flat device tree
14730079efa1SJean-Jacques Hiblot  * @return the node if found, -ve otherwise
14740079efa1SJean-Jacques Hiblot  */
147592926bc8SCooper Jr., Franklin int fit_find_config_node(const void *fdt);
147692926bc8SCooper Jr., Franklin 
1477d7be5092SAndrew F. Davis /**
1478d7be5092SAndrew F. Davis  * Mapping of image types to function handlers to be invoked on the associated
1479d7be5092SAndrew F. Davis  * loaded images
1480d7be5092SAndrew F. Davis  *
1481d7be5092SAndrew F. Davis  * @type: Type of image, I.E. IH_TYPE_*
1482d7be5092SAndrew F. Davis  * @handler: Function to call on loaded image
1483d7be5092SAndrew F. Davis  */
1484d7be5092SAndrew F. Davis struct fit_loadable_tbl {
1485d7be5092SAndrew F. Davis 	int type;
1486d7be5092SAndrew F. Davis 	/**
1487d7be5092SAndrew F. Davis 	 * handler() - Process a loaded image
1488d7be5092SAndrew F. Davis 	 *
1489d7be5092SAndrew F. Davis 	 * @data: Pointer to start of loaded image data
1490d7be5092SAndrew F. Davis 	 * @size: Size of loaded image data
1491d7be5092SAndrew F. Davis 	 */
1492d7be5092SAndrew F. Davis 	void (*handler)(ulong data, size_t size);
1493d7be5092SAndrew F. Davis };
1494d7be5092SAndrew F. Davis 
1495d7be5092SAndrew F. Davis /*
1496d7be5092SAndrew F. Davis  * Define a FIT loadable image type handler
1497d7be5092SAndrew F. Davis  *
1498d7be5092SAndrew F. Davis  * _type is a valid uimage_type ID as defined in the "Image Type" enum above
1499d7be5092SAndrew F. Davis  * _handler is the handler function to call after this image type is loaded
1500d7be5092SAndrew F. Davis  */
1501d7be5092SAndrew F. Davis #define U_BOOT_FIT_LOADABLE_HANDLER(_type, _handler) \
1502d7be5092SAndrew F. Davis 	ll_entry_declare(struct fit_loadable_tbl, _function, fit_loadable) = { \
1503d7be5092SAndrew F. Davis 		.type = _type, \
1504d7be5092SAndrew F. Davis 		.handler = _handler, \
1505d7be5092SAndrew F. Davis 	}
1506d7be5092SAndrew F. Davis 
15075b1d7137Swdenk #endif	/* __IMAGE_H__ */
1508