blob: b438564cfb9c1a7cbe2b84e9dc7861a5ffa41a14 [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01002 * (C) Copyright 2008 Semihalf
3 *
Wolfgang Denkf08abe32005-11-25 16:38:03 +01004 * (C) Copyright 2000-2005
wdenk5b1d7132002-11-03 00:07:02 +00005 * 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
Wolfgang Denkf08abe32005-11-25 16:38:03 +010024 *
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 ********************************************************************
wdenk5b1d7132002-11-03 00:07:02 +000031 */
32
33#ifndef __IMAGE_H__
34#define __IMAGE_H__
35
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010036#include <asm/byteorder.h>
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010037#include <command.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010038#ifndef USE_HOSTCC
39#include <linux/string.h>
40#endif
41
wdenk5b1d7132002-11-03 00:07:02 +000042/*
43 * Operating System Codes
44 */
45#define IH_OS_INVALID 0 /* Invalid OS */
46#define IH_OS_OPENBSD 1 /* OpenBSD */
47#define IH_OS_NETBSD 2 /* NetBSD */
48#define IH_OS_FREEBSD 3 /* FreeBSD */
49#define IH_OS_4_4BSD 4 /* 4.4BSD */
50#define IH_OS_LINUX 5 /* Linux */
51#define IH_OS_SVR4 6 /* SVR4 */
52#define IH_OS_ESIX 7 /* Esix */
53#define IH_OS_SOLARIS 8 /* Solaris */
54#define IH_OS_IRIX 9 /* Irix */
55#define IH_OS_SCO 10 /* SCO */
56#define IH_OS_DELL 11 /* Dell */
57#define IH_OS_NCR 12 /* NCR */
58#define IH_OS_LYNXOS 13 /* LynxOS */
59#define IH_OS_VXWORKS 14 /* VxWorks */
60#define IH_OS_PSOS 15 /* pSOS */
61#define IH_OS_QNX 16 /* QNX */
62#define IH_OS_U_BOOT 17 /* Firmware */
wdenkd791b1d2003-04-20 14:04:18 +000063#define IH_OS_RTEMS 18 /* RTEMS */
wdenk7f70e852003-05-20 14:25:27 +000064#define IH_OS_ARTOS 19 /* ARTOS */
wdenk27b207f2003-07-24 23:38:38 +000065#define IH_OS_UNITY 20 /* Unity OS */
wdenk5b1d7132002-11-03 00:07:02 +000066
67/*
68 * CPU Architecture Codes (supported by Linux)
69 */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010070#define IH_ARCH_INVALID 0 /* Invalid CPU */
71#define IH_ARCH_ALPHA 1 /* Alpha */
72#define IH_ARCH_ARM 2 /* ARM */
73#define IH_ARCH_I386 3 /* Intel x86 */
74#define IH_ARCH_IA64 4 /* IA64 */
75#define IH_ARCH_MIPS 5 /* MIPS */
76#define IH_ARCH_MIPS64 6 /* MIPS 64 Bit */
77#define IH_ARCH_PPC 7 /* PowerPC */
78#define IH_ARCH_S390 8 /* IBM S390 */
79#define IH_ARCH_SH 9 /* SuperH */
80#define IH_ARCH_SPARC 10 /* Sparc */
81#define IH_ARCH_SPARC64 11 /* Sparc 64 Bit */
82#define IH_ARCH_M68K 12 /* M68K */
83#define IH_ARCH_NIOS 13 /* Nios-32 */
84#define IH_ARCH_MICROBLAZE 14 /* MicroBlaze */
85#define IH_ARCH_NIOS2 15 /* Nios-II */
86#define IH_ARCH_BLACKFIN 16 /* Blackfin */
87#define IH_ARCH_AVR32 17 /* AVR32 */
88#define IH_ARCH_ST200 18 /* STMicroelectronics ST200 */
wdenk5b1d7132002-11-03 00:07:02 +000089
90/*
91 * Image Types
92 *
93 * "Standalone Programs" are directly runnable in the environment
94 * provided by U-Boot; it is expected that (if they behave
95 * well) you can continue to work in U-Boot after return from
96 * the Standalone Program.
97 * "OS Kernel Images" are usually images of some Embedded OS which
98 * will take over control completely. Usually these programs
99 * will install their own set of exception handlers, device
100 * drivers, set up the MMU, etc. - this means, that you cannot
101 * expect to re-enter U-Boot except by resetting the CPU.
102 * "RAMDisk Images" are more or less just data blocks, and their
103 * parameters (address, size) are passed to an OS kernel that is
104 * being started.
105 * "Multi-File Images" contain several images, typically an OS
106 * (Linux) kernel image and one or more data images like
107 * RAMDisks. This construct is useful for instance when you want
108 * to boot over the network using BOOTP etc., where the boot
109 * server provides just a single image file, but you want to get
110 * for instance an OS kernel and a RAMDisk image.
111 *
112 * "Multi-File Images" start with a list of image sizes, each
113 * image size (in bytes) specified by an "uint32_t" in network
114 * byte order. This list is terminated by an "(uint32_t)0".
115 * Immediately after the terminating 0 follow the images, one by
116 * one, all aligned on "uint32_t" boundaries (size rounded up to
wdenke1599e82004-10-10 23:27:33 +0000117 * a multiple of 4 bytes - except for the last file).
wdenk5b1d7132002-11-03 00:07:02 +0000118 *
119 * "Firmware Images" are binary images containing firmware (like
120 * U-Boot or FPGA images) which usually will be programmed to
121 * flash memory.
122 *
123 * "Script files" are command sequences that will be executed by
124 * U-Boot's command interpreter; this feature is especially
125 * useful when you configure U-Boot to use a real shell (hush)
wdenk27b207f2003-07-24 23:38:38 +0000126 * as command interpreter (=> Shell Scripts).
wdenk5b1d7132002-11-03 00:07:02 +0000127 */
128
129#define IH_TYPE_INVALID 0 /* Invalid Image */
130#define IH_TYPE_STANDALONE 1 /* Standalone Program */
131#define IH_TYPE_KERNEL 2 /* OS Kernel Image */
132#define IH_TYPE_RAMDISK 3 /* RAMDisk Image */
133#define IH_TYPE_MULTI 4 /* Multi-File Image */
134#define IH_TYPE_FIRMWARE 5 /* Firmware Image */
135#define IH_TYPE_SCRIPT 6 /* Script file */
wdenkfbe4b5c2003-10-06 21:55:32 +0000136#define IH_TYPE_FILESYSTEM 7 /* Filesystem Image (any type) */
Matthew McClintock25c751e2006-08-16 10:54:09 -0500137#define IH_TYPE_FLATDT 8 /* Binary Flat Device Tree Blob */
wdenk5b1d7132002-11-03 00:07:02 +0000138
139/*
140 * Compression Types
141 */
142#define IH_COMP_NONE 0 /* No Compression Used */
143#define IH_COMP_GZIP 1 /* gzip Compression Used */
144#define IH_COMP_BZIP2 2 /* bzip2 Compression Used */
145
146#define IH_MAGIC 0x27051956 /* Image Magic Number */
147#define IH_NMLEN 32 /* Image Name Length */
148
wdenk5b1d7132002-11-03 00:07:02 +0000149/*
150 * all data in network byte order (aka natural aka bigendian)
151 */
152
153typedef struct image_header {
154 uint32_t ih_magic; /* Image Header Magic Number */
155 uint32_t ih_hcrc; /* Image Header CRC Checksum */
156 uint32_t ih_time; /* Image Creation Timestamp */
157 uint32_t ih_size; /* Image Data Size */
158 uint32_t ih_load; /* Data Load Address */
159 uint32_t ih_ep; /* Entry Point Address */
160 uint32_t ih_dcrc; /* Image Data CRC Checksum */
161 uint8_t ih_os; /* Operating System */
162 uint8_t ih_arch; /* CPU architecture */
163 uint8_t ih_type; /* Image Type */
164 uint8_t ih_comp; /* Compression Type */
165 uint8_t ih_name[IH_NMLEN]; /* Image Name */
166} image_header_t;
167
Marian Balakowicz559316f2008-01-08 18:11:44 +0100168/*
169 * Some systems (for example LWMON) have very short watchdog periods;
170 * we must make sure to split long operations like memmove() or
171 * crc32() into reasonable chunks.
172 */
173#define CHUNKSZ (64 * 1024)
174
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100175#define image_to_cpu(x) ntohl(x)
176#define cpu_to_image(x) htonl(x)
wdenk5b1d7132002-11-03 00:07:02 +0000177
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100178static inline uint32_t image_get_header_size (void)
179{
180 return (sizeof (image_header_t));
181}
182
183#define image_get_hdr_l(f) \
184 static inline uint32_t image_get_##f(image_header_t *hdr) \
185 { \
186 return image_to_cpu (hdr->ih_##f); \
187 }
188image_get_hdr_l (magic);
189image_get_hdr_l (hcrc);
190image_get_hdr_l (time);
191image_get_hdr_l (size);
192image_get_hdr_l (load);
193image_get_hdr_l (ep);
194image_get_hdr_l (dcrc);
195
196#define image_get_hdr_b(f) \
197 static inline uint8_t image_get_##f(image_header_t *hdr) \
198 { \
199 return hdr->ih_##f; \
200 }
201image_get_hdr_b (os);
202image_get_hdr_b (arch);
203image_get_hdr_b (type);
204image_get_hdr_b (comp);
205
206static inline char *image_get_name (image_header_t *hdr)
207{
208 return (char *)hdr->ih_name;
209}
210
211static inline uint32_t image_get_data_size (image_header_t *hdr)
212{
213 return image_get_size (hdr);
214}
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100215
216/**
217 * image_get_data - get image payload start address
218 * @hdr: image header
219 *
220 * image_get_data() returns address of the image payload. For single
221 * component images it is image data start. For multi component
222 * images it points to the null terminated table of sub-images sizes.
223 *
224 * returns:
225 * image payload data start address
226 */
227static inline ulong image_get_data (image_header_t *hdr)
228{
229 return ((ulong)hdr + image_get_header_size ());
230}
231
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100232static inline uint32_t image_get_image_size (image_header_t *hdr)
233{
234 return (image_get_size (hdr) + image_get_header_size ());
235}
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100236static inline ulong image_get_image_end (image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100237{
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100238 return ((ulong)hdr + image_get_image_size (hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100239}
240
241#define image_set_hdr_l(f) \
242 static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
243 { \
244 hdr->ih_##f = cpu_to_image (val); \
245 }
246image_set_hdr_l (magic);
247image_set_hdr_l (hcrc);
248image_set_hdr_l (time);
249image_set_hdr_l (size);
250image_set_hdr_l (load);
251image_set_hdr_l (ep);
252image_set_hdr_l (dcrc);
253
254#define image_set_hdr_b(f) \
255 static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
256 { \
257 hdr->ih_##f = val; \
258 }
259image_set_hdr_b (os);
260image_set_hdr_b (arch);
261image_set_hdr_b (type);
262image_set_hdr_b (comp);
263
264static inline void image_set_name (image_header_t *hdr, const char *name)
265{
266 strncpy (image_get_name (hdr), name, IH_NMLEN);
267}
268
269int image_check_hcrc (image_header_t *hdr);
270int image_check_dcrc (image_header_t *hdr);
Marian Balakowiczaf13cdb2008-01-08 18:11:45 +0100271#ifndef USE_HOSTCC
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100272int image_check_dcrc_wd (image_header_t *hdr, ulong chunksize);
273int getenv_verify (void);
Marian Balakowiczaf13cdb2008-01-08 18:11:45 +0100274void memmove_wd (void *to, void *from, size_t len, ulong chunksz);
275#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100276
277static inline int image_check_magic (image_header_t *hdr)
278{
279 return (image_get_magic (hdr) == IH_MAGIC);
280}
281static inline int image_check_type (image_header_t *hdr, uint8_t type)
282{
283 return (image_get_type (hdr) == type);
284}
285static inline int image_check_arch (image_header_t *hdr, uint8_t arch)
286{
287 return (image_get_arch (hdr) == arch);
288}
289static inline int image_check_os (image_header_t *hdr, uint8_t os)
290{
291 return (image_get_os (hdr) == os);
292}
293
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100294ulong image_multi_count (image_header_t *hdr);
295void image_multi_getimg (image_header_t *hdr, ulong idx,
296 ulong *data, ulong *len);
297
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100298#ifndef USE_HOSTCC
299static inline int image_check_target_arch (image_header_t *hdr)
300{
301#if defined(__ARM__)
302 if (!image_check_arch (hdr, IH_ARCH_ARM))
303#elif defined(__avr32__)
304 if (!image_check_arch (hdr, IH_ARCH_AVR32))
305#elif defined(__bfin__)
306 if (!image_check_arch (hdr, IH_ARCH_BLACKFIN))
307#elif defined(__I386__)
308 if (!image_check_arch (hdr, IH_ARCH_I386))
309#elif defined(__M68K__)
310 if (!image_check_arch (hdr, IH_ARCH_M68K))
311#elif defined(__microblaze__)
312 if (!image_check_arch (hdr, IH_ARCH_MICROBLAZE))
313#elif defined(__mips__)
314 if (!image_check_arch (hdr, IH_ARCH_MIPS))
315#elif defined(__nios__)
316 if (!image_check_arch (hdr, IH_ARCH_NIOS))
317#elif defined(__nios2__)
318 if (!image_check_arch (hdr, IH_ARCH_NIOS2))
319#elif defined(__PPC__)
320 if (!image_check_arch (hdr, IH_ARCH_PPC))
321#elif defined(__sh__)
322 if (!image_check_arch (hdr, IH_ARCH_SH))
323#else
324# error Unknown CPU type
325#endif
326 return 0;
327
328 return 1;
329}
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100330
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100331const char* image_get_os_name (uint8_t os);
332const char* image_get_arch_name (uint8_t arch);
333const char* image_get_type_name (uint8_t type);
334const char* image_get_comp_name (uint8_t comp);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100335
336image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
337 int argc, char *argv[],
338 ulong rd_addr, uint8_t arch, int verify);
339
340void get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
341 image_header_t *hdr, int verify, uint8_t arch,
342 ulong *rd_start, ulong *rd_end);
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100343#endif /* USE_HOSTCCa */
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100344
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100345
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100346#endif /* __IMAGE_H__ */