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