xref: /rk3399_rockchip-uboot/include/image.h (revision b97a2a0a21f279d66de8a9bdbfe21920968bcb1c)
1 /*
2  * (C) Copyright 2008 Semihalf
3  *
4  * (C) Copyright 2000-2005
5  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  *
25  ********************************************************************
26  * NOTE: This header file defines an interface to U-Boot. Including
27  * this (unmodified) header file in another file is considered normal
28  * use of U-Boot, and does *not* fall under the heading of "derived
29  * work".
30  ********************************************************************
31  */
32 
33 #ifndef __IMAGE_H__
34 #define __IMAGE_H__
35 
36 #include <asm/byteorder.h>
37 #ifndef USE_HOSTCC
38 #include <linux/string.h>
39 #endif
40 
41 /*
42  * Operating System Codes
43  */
44 #define IH_OS_INVALID		0	/* Invalid OS	*/
45 #define IH_OS_OPENBSD		1	/* OpenBSD	*/
46 #define IH_OS_NETBSD		2	/* NetBSD	*/
47 #define IH_OS_FREEBSD		3	/* FreeBSD	*/
48 #define IH_OS_4_4BSD		4	/* 4.4BSD	*/
49 #define IH_OS_LINUX		5	/* Linux	*/
50 #define IH_OS_SVR4		6	/* SVR4		*/
51 #define IH_OS_ESIX		7	/* Esix		*/
52 #define IH_OS_SOLARIS		8	/* Solaris	*/
53 #define IH_OS_IRIX		9	/* Irix		*/
54 #define IH_OS_SCO		10	/* SCO		*/
55 #define IH_OS_DELL		11	/* Dell		*/
56 #define IH_OS_NCR		12	/* NCR		*/
57 #define IH_OS_LYNXOS		13	/* LynxOS	*/
58 #define IH_OS_VXWORKS		14	/* VxWorks	*/
59 #define IH_OS_PSOS		15	/* pSOS		*/
60 #define IH_OS_QNX		16	/* QNX		*/
61 #define IH_OS_U_BOOT		17	/* Firmware	*/
62 #define IH_OS_RTEMS		18	/* RTEMS	*/
63 #define IH_OS_ARTOS		19	/* ARTOS	*/
64 #define IH_OS_UNITY		20	/* Unity OS	*/
65 
66 /*
67  * CPU Architecture Codes (supported by Linux)
68  */
69 #define IH_ARCH_INVALID		0	/* Invalid CPU	*/
70 #define IH_ARCH_ALPHA		1	/* Alpha	*/
71 #define IH_ARCH_ARM		2	/* ARM		*/
72 #define IH_ARCH_I386		3	/* Intel x86	*/
73 #define IH_ARCH_IA64		4	/* IA64		*/
74 #define IH_ARCH_MIPS		5	/* MIPS		*/
75 #define IH_ARCH_MIPS64		6	/* MIPS	 64 Bit */
76 #define IH_ARCH_PPC		7	/* PowerPC	*/
77 #define IH_ARCH_S390		8	/* IBM S390	*/
78 #define IH_ARCH_SH		9	/* SuperH	*/
79 #define IH_ARCH_SPARC		10	/* Sparc	*/
80 #define IH_ARCH_SPARC64		11	/* Sparc 64 Bit */
81 #define IH_ARCH_M68K		12	/* M68K		*/
82 #define IH_ARCH_NIOS		13	/* Nios-32	*/
83 #define IH_ARCH_MICROBLAZE	14	/* MicroBlaze   */
84 #define IH_ARCH_NIOS2		15	/* Nios-II	*/
85 #define IH_ARCH_BLACKFIN	16	/* Blackfin	*/
86 #define IH_ARCH_AVR32		17	/* AVR32	*/
87 #define IH_ARCH_ST200	        18	/* STMicroelectronics ST200  */
88 
89 /*
90  * Image Types
91  *
92  * "Standalone Programs" are directly runnable in the environment
93  *	provided by U-Boot; it is expected that (if they behave
94  *	well) you can continue to work in U-Boot after return from
95  *	the Standalone Program.
96  * "OS Kernel Images" are usually images of some Embedded OS which
97  *	will take over control completely. Usually these programs
98  *	will install their own set of exception handlers, device
99  *	drivers, set up the MMU, etc. - this means, that you cannot
100  *	expect to re-enter U-Boot except by resetting the CPU.
101  * "RAMDisk Images" are more or less just data blocks, and their
102  *	parameters (address, size) are passed to an OS kernel that is
103  *	being started.
104  * "Multi-File Images" contain several images, typically an OS
105  *	(Linux) kernel image and one or more data images like
106  *	RAMDisks. This construct is useful for instance when you want
107  *	to boot over the network using BOOTP etc., where the boot
108  *	server provides just a single image file, but you want to get
109  *	for instance an OS kernel and a RAMDisk image.
110  *
111  *	"Multi-File Images" start with a list of image sizes, each
112  *	image size (in bytes) specified by an "uint32_t" in network
113  *	byte order. This list is terminated by an "(uint32_t)0".
114  *	Immediately after the terminating 0 follow the images, one by
115  *	one, all aligned on "uint32_t" boundaries (size rounded up to
116  *	a multiple of 4 bytes - except for the last file).
117  *
118  * "Firmware Images" are binary images containing firmware (like
119  *	U-Boot or FPGA images) which usually will be programmed to
120  *	flash memory.
121  *
122  * "Script files" are command sequences that will be executed by
123  *	U-Boot's command interpreter; this feature is especially
124  *	useful when you configure U-Boot to use a real shell (hush)
125  *	as command interpreter (=> Shell Scripts).
126  */
127 
128 #define IH_TYPE_INVALID		0	/* Invalid Image		*/
129 #define IH_TYPE_STANDALONE	1	/* Standalone Program		*/
130 #define IH_TYPE_KERNEL		2	/* OS Kernel Image		*/
131 #define IH_TYPE_RAMDISK		3	/* RAMDisk Image		*/
132 #define IH_TYPE_MULTI		4	/* Multi-File Image		*/
133 #define IH_TYPE_FIRMWARE	5	/* Firmware Image		*/
134 #define IH_TYPE_SCRIPT		6	/* Script file			*/
135 #define IH_TYPE_FILESYSTEM	7	/* Filesystem Image (any type)	*/
136 #define IH_TYPE_FLATDT		8	/* Binary Flat Device Tree Blob	*/
137 
138 /*
139  * Compression Types
140  */
141 #define IH_COMP_NONE		0	/*  No	 Compression Used	*/
142 #define IH_COMP_GZIP		1	/* gzip	 Compression Used	*/
143 #define IH_COMP_BZIP2		2	/* bzip2 Compression Used	*/
144 
145 #define IH_MAGIC	0x27051956	/* Image Magic Number		*/
146 #define IH_NMLEN		32	/* Image Name Length		*/
147 
148 /*
149  * all data in network byte order (aka natural aka bigendian)
150  */
151 
152 typedef struct image_header {
153 	uint32_t	ih_magic;	/* Image Header Magic Number	*/
154 	uint32_t	ih_hcrc;	/* Image Header CRC Checksum	*/
155 	uint32_t	ih_time;	/* Image Creation Timestamp	*/
156 	uint32_t	ih_size;	/* Image Data Size		*/
157 	uint32_t	ih_load;	/* Data	 Load  Address		*/
158 	uint32_t	ih_ep;		/* Entry Point Address		*/
159 	uint32_t	ih_dcrc;	/* Image Data CRC Checksum	*/
160 	uint8_t		ih_os;		/* Operating System		*/
161 	uint8_t		ih_arch;	/* CPU architecture		*/
162 	uint8_t		ih_type;	/* Image Type			*/
163 	uint8_t		ih_comp;	/* Compression Type		*/
164 	uint8_t		ih_name[IH_NMLEN];	/* Image Name		*/
165 } image_header_t;
166 
167 #define image_to_cpu(x)		ntohl(x)
168 #define cpu_to_image(x)		htonl(x)
169 
170 static inline uint32_t image_get_header_size (void)
171 {
172 	return (sizeof (image_header_t));
173 }
174 
175 #define image_get_hdr_l(f) \
176 	static inline uint32_t image_get_##f(image_header_t *hdr) \
177 	{ \
178 		return image_to_cpu (hdr->ih_##f); \
179 	}
180 image_get_hdr_l (magic);
181 image_get_hdr_l (hcrc);
182 image_get_hdr_l (time);
183 image_get_hdr_l (size);
184 image_get_hdr_l (load);
185 image_get_hdr_l (ep);
186 image_get_hdr_l (dcrc);
187 
188 #define image_get_hdr_b(f) \
189 	static inline uint8_t image_get_##f(image_header_t *hdr) \
190 	{ \
191 		return hdr->ih_##f; \
192 	}
193 image_get_hdr_b (os);
194 image_get_hdr_b (arch);
195 image_get_hdr_b (type);
196 image_get_hdr_b (comp);
197 
198 static inline char *image_get_name (image_header_t *hdr)
199 {
200 	return (char *)hdr->ih_name;
201 }
202 
203 static inline uint32_t image_get_data_size (image_header_t *hdr)
204 {
205 	return image_get_size (hdr);
206 }
207 static inline uint32_t image_get_image_size (image_header_t *hdr)
208 {
209 	return (image_get_size (hdr) + image_get_header_size ());
210 }
211 static inline ulong image_get_data (image_header_t *hdr)
212 {
213 	return ((ulong)hdr + image_get_header_size ());
214 }
215 
216 #define image_set_hdr_l(f) \
217 	static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
218 	{ \
219 		hdr->ih_##f = cpu_to_image (val); \
220 	}
221 image_set_hdr_l (magic);
222 image_set_hdr_l (hcrc);
223 image_set_hdr_l (time);
224 image_set_hdr_l (size);
225 image_set_hdr_l (load);
226 image_set_hdr_l (ep);
227 image_set_hdr_l (dcrc);
228 
229 #define image_set_hdr_b(f) \
230 	static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
231 	{ \
232 		hdr->ih_##f = val; \
233 	}
234 image_set_hdr_b (os);
235 image_set_hdr_b (arch);
236 image_set_hdr_b (type);
237 image_set_hdr_b (comp);
238 
239 static inline void image_set_name (image_header_t *hdr, const char *name)
240 {
241 	strncpy (image_get_name (hdr), name, IH_NMLEN);
242 }
243 
244 int image_check_hcrc (image_header_t *hdr);
245 int image_check_dcrc (image_header_t *hdr);
246 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksize);
247 int getenv_verify (void);
248 
249 static inline int image_check_magic (image_header_t *hdr)
250 {
251 	return (image_get_magic (hdr) == IH_MAGIC);
252 }
253 static inline int image_check_type (image_header_t *hdr, uint8_t type)
254 {
255 	return (image_get_type (hdr) == type);
256 }
257 static inline int image_check_arch (image_header_t *hdr, uint8_t arch)
258 {
259 	return (image_get_arch (hdr) == arch);
260 }
261 static inline int image_check_os (image_header_t *hdr, uint8_t os)
262 {
263 	return (image_get_os (hdr) == os);
264 }
265 
266 #ifndef USE_HOSTCC
267 static inline int image_check_target_arch (image_header_t *hdr)
268 {
269 #if defined(__ARM__)
270 	if (!image_check_arch (hdr, IH_ARCH_ARM))
271 #elif defined(__avr32__)
272 	if (!image_check_arch (hdr, IH_ARCH_AVR32))
273 #elif defined(__bfin__)
274 	if (!image_check_arch (hdr, IH_ARCH_BLACKFIN))
275 #elif defined(__I386__)
276 	if (!image_check_arch (hdr, IH_ARCH_I386))
277 #elif defined(__M68K__)
278 	if (!image_check_arch (hdr, IH_ARCH_M68K))
279 #elif defined(__microblaze__)
280 	if (!image_check_arch (hdr, IH_ARCH_MICROBLAZE))
281 #elif defined(__mips__)
282 	if (!image_check_arch (hdr, IH_ARCH_MIPS))
283 #elif defined(__nios__)
284 	if (!image_check_arch (hdr, IH_ARCH_NIOS))
285 #elif defined(__nios2__)
286 	if (!image_check_arch (hdr, IH_ARCH_NIOS2))
287 #elif defined(__PPC__)
288 	if (!image_check_arch (hdr, IH_ARCH_PPC))
289 #elif defined(__sh__)
290 	if (!image_check_arch (hdr, IH_ARCH_SH))
291 #else
292 # error Unknown CPU type
293 #endif
294 		return 0;
295 
296 	return 1;
297 }
298 #endif
299 
300 #endif /* __IMAGE_H__ */
301