wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 1 | /* |
wdenk | 0c852a2 | 2004-02-26 23:01:04 +0000 | [diff] [blame] | 2 | * (C) Copyright 2000-2004 |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 3 | * DENX Software Engineering |
| 4 | * Wolfgang Denk, wd@denx.de |
| 5 | * All rights reserved. |
wdenk | 0c852a2 | 2004-02-26 23:01:04 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #ifndef __WIN32__ |
| 29 | #include <netinet/in.h> /* for host / network byte order conversions */ |
| 30 | #endif |
| 31 | #include <sys/mman.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <time.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | #if defined(__BEOS__) || defined(__NetBSD__) || defined(__APPLE__) |
| 37 | #include <inttypes.h> |
| 38 | #endif |
| 39 | |
| 40 | #ifdef __WIN32__ |
| 41 | typedef unsigned int __u32; |
| 42 | |
| 43 | #define SWAP_LONG(x) \ |
| 44 | ((__u32)( \ |
| 45 | (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \ |
| 46 | (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \ |
| 47 | (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \ |
| 48 | (((__u32)(x) & (__u32)0xff000000UL) >> 24) )) |
| 49 | typedef unsigned char uint8_t; |
| 50 | typedef unsigned short uint16_t; |
| 51 | typedef unsigned int uint32_t; |
| 52 | |
| 53 | #define ntohl(a) SWAP_LONG(a) |
| 54 | #define htonl(a) SWAP_LONG(a) |
| 55 | #endif /* __WIN32__ */ |
| 56 | |
wdenk | acf98e7 | 2003-09-16 11:39:10 +0000 | [diff] [blame] | 57 | #ifndef O_BINARY /* should be define'd on __WIN32__ */ |
| 58 | #define O_BINARY 0 |
| 59 | #endif |
| 60 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 61 | #include <image.h> |
| 62 | |
| 63 | extern int errno; |
| 64 | |
| 65 | #ifndef MAP_FAILED |
| 66 | #define MAP_FAILED (-1) |
| 67 | #endif |
| 68 | |
| 69 | char *cmdname; |
| 70 | |
| 71 | extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len); |
| 72 | |
| 73 | typedef struct table_entry { |
| 74 | int val; /* as defined in image.h */ |
| 75 | char *sname; /* short (input) name */ |
| 76 | char *lname; /* long (output) name */ |
| 77 | } table_entry_t; |
| 78 | |
| 79 | table_entry_t arch_name[] = { |
wdenk | 466b741 | 2004-07-10 22:35:59 +0000 | [diff] [blame] | 80 | { IH_CPU_INVALID, NULL, "Invalid CPU", }, |
| 81 | { IH_CPU_ALPHA, "alpha", "Alpha", }, |
| 82 | { IH_CPU_ARM, "arm", "ARM", }, |
| 83 | { IH_CPU_I386, "x86", "Intel x86", }, |
| 84 | { IH_CPU_IA64, "ia64", "IA64", }, |
| 85 | { IH_CPU_M68K, "m68k", "MC68000", }, |
| 86 | { IH_CPU_MICROBLAZE, "microblaze", "MicroBlaze", }, |
| 87 | { IH_CPU_MIPS, "mips", "MIPS", }, |
| 88 | { IH_CPU_MIPS64, "mips64", "MIPS 64 Bit", }, |
wdenk | c1a11c1 | 2005-04-03 21:11:16 +0000 | [diff] [blame] | 89 | { IH_CPU_NIOS, "nios", "NIOS", }, |
| 90 | { IH_CPU_NIOS2, "nios2", "NIOS II", }, |
wdenk | 466b741 | 2004-07-10 22:35:59 +0000 | [diff] [blame] | 91 | { IH_CPU_PPC, "ppc", "PowerPC", }, |
| 92 | { IH_CPU_S390, "s390", "IBM S390", }, |
| 93 | { IH_CPU_SH, "sh", "SuperH", }, |
| 94 | { IH_CPU_SPARC, "sparc", "SPARC", }, |
| 95 | { IH_CPU_SPARC64, "sparc64", "SPARC 64 Bit", }, |
| 96 | { -1, "", "", }, |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | table_entry_t os_name[] = { |
| 100 | { IH_OS_INVALID, NULL, "Invalid OS", }, |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 101 | { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", }, |
wdenk | 466b741 | 2004-07-10 22:35:59 +0000 | [diff] [blame] | 102 | { IH_OS_ARTOS, "artos", "ARTOS", }, |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 103 | { IH_OS_DELL, "dell", "Dell", }, |
wdenk | 466b741 | 2004-07-10 22:35:59 +0000 | [diff] [blame] | 104 | { IH_OS_ESIX, "esix", "Esix", }, |
| 105 | { IH_OS_FREEBSD, "freebsd", "FreeBSD", }, |
| 106 | { IH_OS_IRIX, "irix", "Irix", }, |
| 107 | { IH_OS_LINUX, "linux", "Linux", }, |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 108 | { IH_OS_LYNXOS, "lynxos", "LynxOS", }, |
wdenk | 466b741 | 2004-07-10 22:35:59 +0000 | [diff] [blame] | 109 | { IH_OS_NCR, "ncr", "NCR", }, |
| 110 | { IH_OS_NETBSD, "netbsd", "NetBSD", }, |
| 111 | { IH_OS_OPENBSD, "openbsd", "OpenBSD", }, |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 112 | { IH_OS_PSOS, "psos", "pSOS", }, |
| 113 | { IH_OS_QNX, "qnx", "QNX", }, |
wdenk | d791b1d | 2003-04-20 14:04:18 +0000 | [diff] [blame] | 114 | { IH_OS_RTEMS, "rtems", "RTEMS", }, |
wdenk | 466b741 | 2004-07-10 22:35:59 +0000 | [diff] [blame] | 115 | { IH_OS_SCO, "sco", "SCO", }, |
| 116 | { IH_OS_SOLARIS, "solaris", "Solaris", }, |
| 117 | { IH_OS_SVR4, "svr4", "SVR4", }, |
| 118 | { IH_OS_U_BOOT, "u-boot", "U-Boot", }, |
| 119 | { IH_OS_VXWORKS, "vxworks", "VxWorks", }, |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 120 | { -1, "", "", }, |
| 121 | }; |
| 122 | |
| 123 | table_entry_t type_name[] = { |
| 124 | { IH_TYPE_INVALID, NULL, "Invalid Image", }, |
wdenk | 887b372 | 2003-10-06 22:00:45 +0000 | [diff] [blame] | 125 | { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", }, |
wdenk | 466b741 | 2004-07-10 22:35:59 +0000 | [diff] [blame] | 126 | { IH_TYPE_FIRMWARE, "firmware", "Firmware", }, |
| 127 | { IH_TYPE_KERNEL, "kernel", "Kernel Image", }, |
| 128 | { IH_TYPE_MULTI, "multi", "Multi-File Image", }, |
| 129 | { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", }, |
| 130 | { IH_TYPE_SCRIPT, "script", "Script", }, |
| 131 | { IH_TYPE_STANDALONE, "standalone", "Standalone Program", }, |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 132 | { -1, "", "", }, |
| 133 | }; |
| 134 | |
| 135 | table_entry_t comp_name[] = { |
| 136 | { IH_COMP_NONE, "none", "uncompressed", }, |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 137 | { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", }, |
wdenk | 466b741 | 2004-07-10 22:35:59 +0000 | [diff] [blame] | 138 | { IH_COMP_GZIP, "gzip", "gzip compressed", }, |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 139 | { -1, "", "", }, |
| 140 | }; |
| 141 | |
| 142 | static void copy_file (int, const char *, int); |
| 143 | static void usage (void); |
| 144 | static void print_header (image_header_t *); |
| 145 | static void print_type (image_header_t *); |
| 146 | static char *put_table_entry (table_entry_t *, char *, int); |
| 147 | static char *put_arch (int); |
| 148 | static char *put_type (int); |
| 149 | static char *put_os (int); |
| 150 | static char *put_comp (int); |
| 151 | static int get_table_entry (table_entry_t *, char *, char *); |
| 152 | static int get_arch(char *); |
| 153 | static int get_comp(char *); |
| 154 | static int get_os (char *); |
| 155 | static int get_type(char *); |
| 156 | |
| 157 | |
| 158 | char *datafile; |
| 159 | char *imagefile; |
| 160 | |
| 161 | int dflag = 0; |
| 162 | int eflag = 0; |
| 163 | int lflag = 0; |
| 164 | int vflag = 0; |
| 165 | int xflag = 0; |
| 166 | int opt_os = IH_OS_LINUX; |
| 167 | int opt_arch = IH_CPU_PPC; |
| 168 | int opt_type = IH_TYPE_KERNEL; |
| 169 | int opt_comp = IH_COMP_GZIP; |
| 170 | |
| 171 | image_header_t header; |
| 172 | image_header_t *hdr = &header; |
| 173 | |
| 174 | int |
| 175 | main (int argc, char **argv) |
| 176 | { |
| 177 | int ifd; |
| 178 | uint32_t checksum; |
| 179 | uint32_t addr; |
| 180 | uint32_t ep; |
| 181 | struct stat sbuf; |
| 182 | unsigned char *ptr; |
| 183 | char *name = ""; |
| 184 | |
| 185 | cmdname = *argv; |
| 186 | |
| 187 | addr = ep = 0; |
| 188 | |
| 189 | while (--argc > 0 && **++argv == '-') { |
| 190 | while (*++*argv) { |
| 191 | switch (**argv) { |
| 192 | case 'l': |
| 193 | lflag = 1; |
| 194 | break; |
| 195 | case 'A': |
| 196 | if ((--argc <= 0) || |
| 197 | (opt_arch = get_arch(*++argv)) < 0) |
| 198 | usage (); |
| 199 | goto NXTARG; |
| 200 | case 'C': |
| 201 | if ((--argc <= 0) || |
| 202 | (opt_comp = get_comp(*++argv)) < 0) |
| 203 | usage (); |
| 204 | goto NXTARG; |
| 205 | case 'O': |
| 206 | if ((--argc <= 0) || |
| 207 | (opt_os = get_os(*++argv)) < 0) |
| 208 | usage (); |
| 209 | goto NXTARG; |
| 210 | case 'T': |
| 211 | if ((--argc <= 0) || |
| 212 | (opt_type = get_type(*++argv)) < 0) |
| 213 | usage (); |
| 214 | goto NXTARG; |
| 215 | |
| 216 | case 'a': |
| 217 | if (--argc <= 0) |
| 218 | usage (); |
| 219 | addr = strtoul (*++argv, (char **)&ptr, 16); |
| 220 | if (*ptr) { |
| 221 | fprintf (stderr, |
| 222 | "%s: invalid load address %s\n", |
| 223 | cmdname, *argv); |
| 224 | exit (EXIT_FAILURE); |
| 225 | } |
| 226 | goto NXTARG; |
| 227 | case 'd': |
| 228 | if (--argc <= 0) |
| 229 | usage (); |
| 230 | datafile = *++argv; |
| 231 | dflag = 1; |
| 232 | goto NXTARG; |
| 233 | case 'e': |
| 234 | if (--argc <= 0) |
| 235 | usage (); |
| 236 | ep = strtoul (*++argv, (char **)&ptr, 16); |
| 237 | if (*ptr) { |
| 238 | fprintf (stderr, |
| 239 | "%s: invalid entry point %s\n", |
| 240 | cmdname, *argv); |
| 241 | exit (EXIT_FAILURE); |
| 242 | } |
| 243 | eflag = 1; |
| 244 | goto NXTARG; |
| 245 | case 'n': |
| 246 | if (--argc <= 0) |
| 247 | usage (); |
| 248 | name = *++argv; |
| 249 | goto NXTARG; |
| 250 | case 'v': |
| 251 | vflag++; |
| 252 | break; |
| 253 | case 'x': |
| 254 | xflag++; |
| 255 | break; |
| 256 | default: |
| 257 | usage (); |
| 258 | } |
| 259 | } |
| 260 | NXTARG: ; |
| 261 | } |
| 262 | |
| 263 | if ((argc != 1) || ((lflag ^ dflag) == 0)) |
| 264 | usage(); |
| 265 | |
| 266 | if (!eflag) { |
| 267 | ep = addr; |
| 268 | /* If XIP, entry point must be after the U-Boot header */ |
| 269 | if (xflag) |
| 270 | ep += sizeof(image_header_t); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * If XIP, ensure the entry point is equal to the load address plus |
| 275 | * the size of the U-Boot header. |
| 276 | */ |
| 277 | if (xflag) { |
| 278 | if (ep != addr + sizeof(image_header_t)) { |
wdenk | a6c7ad2 | 2002-12-03 21:28:10 +0000 | [diff] [blame] | 279 | fprintf (stderr, "%s: For XIP, the entry point must be the load addr + %lu\n", |
| 280 | cmdname, |
| 281 | (unsigned long)sizeof(image_header_t)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 282 | exit (EXIT_FAILURE); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | imagefile = *argv; |
| 287 | |
| 288 | if (lflag) { |
wdenk | ef1464c | 2003-10-08 22:14:02 +0000 | [diff] [blame] | 289 | ifd = open(imagefile, O_RDONLY|O_BINARY); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 290 | } else { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 291 | ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | if (ifd < 0) { |
| 295 | fprintf (stderr, "%s: Can't open %s: %s\n", |
| 296 | cmdname, imagefile, strerror(errno)); |
| 297 | exit (EXIT_FAILURE); |
| 298 | } |
| 299 | |
| 300 | if (lflag) { |
| 301 | int len; |
| 302 | char *data; |
| 303 | /* |
| 304 | * list header information of existing image |
| 305 | */ |
| 306 | if (fstat(ifd, &sbuf) < 0) { |
| 307 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
| 308 | cmdname, imagefile, strerror(errno)); |
| 309 | exit (EXIT_FAILURE); |
| 310 | } |
| 311 | |
wdenk | 5bb226e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 312 | if ((unsigned)sbuf.st_size < sizeof(image_header_t)) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 313 | fprintf (stderr, |
| 314 | "%s: Bad size: \"%s\" is no valid image\n", |
| 315 | cmdname, imagefile); |
| 316 | exit (EXIT_FAILURE); |
| 317 | } |
| 318 | |
| 319 | ptr = (unsigned char *)mmap(0, sbuf.st_size, |
| 320 | PROT_READ, MAP_SHARED, ifd, 0); |
| 321 | if ((caddr_t)ptr == (caddr_t)-1) { |
| 322 | fprintf (stderr, "%s: Can't read %s: %s\n", |
| 323 | cmdname, imagefile, strerror(errno)); |
| 324 | exit (EXIT_FAILURE); |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * create copy of header so that we can blank out the |
| 329 | * checksum field for checking - this can't be done |
| 330 | * on the PROT_READ mapped data. |
| 331 | */ |
| 332 | memcpy (hdr, ptr, sizeof(image_header_t)); |
| 333 | |
| 334 | if (ntohl(hdr->ih_magic) != IH_MAGIC) { |
| 335 | fprintf (stderr, |
| 336 | "%s: Bad Magic Number: \"%s\" is no valid image\n", |
| 337 | cmdname, imagefile); |
| 338 | exit (EXIT_FAILURE); |
| 339 | } |
| 340 | |
| 341 | data = (char *)hdr; |
| 342 | len = sizeof(image_header_t); |
| 343 | |
| 344 | checksum = ntohl(hdr->ih_hcrc); |
| 345 | hdr->ih_hcrc = htonl(0); /* clear for re-calculation */ |
| 346 | |
| 347 | if (crc32 (0, data, len) != checksum) { |
| 348 | fprintf (stderr, |
| 349 | "*** Warning: \"%s\" has bad header checksum!\n", |
| 350 | imagefile); |
| 351 | } |
| 352 | |
| 353 | data = (char *)(ptr + sizeof(image_header_t)); |
| 354 | len = sbuf.st_size - sizeof(image_header_t) ; |
| 355 | |
| 356 | if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) { |
| 357 | fprintf (stderr, |
| 358 | "*** Warning: \"%s\" has corrupted data!\n", |
| 359 | imagefile); |
| 360 | } |
| 361 | |
| 362 | /* for multi-file images we need the data part, too */ |
| 363 | print_header ((image_header_t *)ptr); |
| 364 | |
| 365 | (void) munmap((void *)ptr, sbuf.st_size); |
| 366 | (void) close (ifd); |
| 367 | |
| 368 | exit (EXIT_SUCCESS); |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Must be -w then: |
| 373 | * |
| 374 | * write dummy header, to be fixed later |
| 375 | */ |
| 376 | memset (hdr, 0, sizeof(image_header_t)); |
| 377 | |
| 378 | if (write(ifd, hdr, sizeof(image_header_t)) != sizeof(image_header_t)) { |
| 379 | fprintf (stderr, "%s: Write error on %s: %s\n", |
| 380 | cmdname, imagefile, strerror(errno)); |
| 381 | exit (EXIT_FAILURE); |
| 382 | } |
| 383 | |
| 384 | if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) { |
| 385 | char *file = datafile; |
Wolfgang Denk | 3bb6680 | 2006-01-11 13:03:54 +0100 | [diff] [blame] | 386 | uint32_t size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 387 | |
| 388 | for (;;) { |
| 389 | char *sep = NULL; |
| 390 | |
| 391 | if (file) { |
| 392 | if ((sep = strchr(file, ':')) != NULL) { |
| 393 | *sep = '\0'; |
| 394 | } |
| 395 | |
| 396 | if (stat (file, &sbuf) < 0) { |
| 397 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
| 398 | cmdname, file, strerror(errno)); |
| 399 | exit (EXIT_FAILURE); |
| 400 | } |
| 401 | size = htonl(sbuf.st_size); |
| 402 | } else { |
| 403 | size = 0; |
| 404 | } |
| 405 | |
| 406 | if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { |
| 407 | fprintf (stderr, "%s: Write error on %s: %s\n", |
| 408 | cmdname, imagefile, strerror(errno)); |
| 409 | exit (EXIT_FAILURE); |
| 410 | } |
| 411 | |
| 412 | if (!file) { |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | if (sep) { |
| 417 | *sep = ':'; |
| 418 | file = sep + 1; |
| 419 | } else { |
| 420 | file = NULL; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | file = datafile; |
| 425 | |
| 426 | for (;;) { |
| 427 | char *sep = strchr(file, ':'); |
| 428 | if (sep) { |
| 429 | *sep = '\0'; |
| 430 | copy_file (ifd, file, 1); |
| 431 | *sep++ = ':'; |
| 432 | file = sep; |
| 433 | } else { |
| 434 | copy_file (ifd, file, 0); |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | } else { |
| 439 | copy_file (ifd, datafile, 0); |
| 440 | } |
| 441 | |
| 442 | /* We're a bit of paranoid */ |
wdenk | 147031a | 2003-10-07 10:33:38 +0000 | [diff] [blame] | 443 | #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 444 | (void) fdatasync (ifd); |
| 445 | #else |
| 446 | (void) fsync (ifd); |
| 447 | #endif |
| 448 | |
| 449 | if (fstat(ifd, &sbuf) < 0) { |
| 450 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
| 451 | cmdname, imagefile, strerror(errno)); |
| 452 | exit (EXIT_FAILURE); |
| 453 | } |
| 454 | |
| 455 | ptr = (unsigned char *)mmap(0, sbuf.st_size, |
| 456 | PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); |
| 457 | if (ptr == (unsigned char *)MAP_FAILED) { |
| 458 | fprintf (stderr, "%s: Can't map %s: %s\n", |
| 459 | cmdname, imagefile, strerror(errno)); |
| 460 | exit (EXIT_FAILURE); |
| 461 | } |
| 462 | |
| 463 | hdr = (image_header_t *)ptr; |
| 464 | |
| 465 | checksum = crc32 (0, |
| 466 | (const char *)(ptr + sizeof(image_header_t)), |
| 467 | sbuf.st_size - sizeof(image_header_t) |
| 468 | ); |
| 469 | |
| 470 | /* Build new header */ |
| 471 | hdr->ih_magic = htonl(IH_MAGIC); |
| 472 | hdr->ih_time = htonl(sbuf.st_mtime); |
| 473 | hdr->ih_size = htonl(sbuf.st_size - sizeof(image_header_t)); |
| 474 | hdr->ih_load = htonl(addr); |
| 475 | hdr->ih_ep = htonl(ep); |
| 476 | hdr->ih_dcrc = htonl(checksum); |
| 477 | hdr->ih_os = opt_os; |
| 478 | hdr->ih_arch = opt_arch; |
| 479 | hdr->ih_type = opt_type; |
| 480 | hdr->ih_comp = opt_comp; |
| 481 | |
| 482 | strncpy((char *)hdr->ih_name, name, IH_NMLEN); |
| 483 | |
| 484 | checksum = crc32(0,(const char *)hdr,sizeof(image_header_t)); |
| 485 | |
| 486 | hdr->ih_hcrc = htonl(checksum); |
| 487 | |
| 488 | print_header (hdr); |
| 489 | |
| 490 | (void) munmap((void *)ptr, sbuf.st_size); |
| 491 | |
| 492 | /* We're a bit of paranoid */ |
wdenk | 147031a | 2003-10-07 10:33:38 +0000 | [diff] [blame] | 493 | #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 494 | (void) fdatasync (ifd); |
| 495 | #else |
| 496 | (void) fsync (ifd); |
| 497 | #endif |
| 498 | |
| 499 | if (close(ifd)) { |
| 500 | fprintf (stderr, "%s: Write error on %s: %s\n", |
| 501 | cmdname, imagefile, strerror(errno)); |
| 502 | exit (EXIT_FAILURE); |
| 503 | } |
| 504 | |
| 505 | exit (EXIT_SUCCESS); |
| 506 | } |
| 507 | |
| 508 | static void |
| 509 | copy_file (int ifd, const char *datafile, int pad) |
| 510 | { |
| 511 | int dfd; |
| 512 | struct stat sbuf; |
| 513 | unsigned char *ptr; |
| 514 | int tail; |
| 515 | int zero = 0; |
| 516 | int offset = 0; |
| 517 | int size; |
| 518 | |
| 519 | if (vflag) { |
| 520 | fprintf (stderr, "Adding Image %s\n", datafile); |
| 521 | } |
| 522 | |
wdenk | ef1464c | 2003-10-08 22:14:02 +0000 | [diff] [blame] | 523 | if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 524 | fprintf (stderr, "%s: Can't open %s: %s\n", |
| 525 | cmdname, datafile, strerror(errno)); |
| 526 | exit (EXIT_FAILURE); |
| 527 | } |
| 528 | |
| 529 | if (fstat(dfd, &sbuf) < 0) { |
| 530 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
| 531 | cmdname, datafile, strerror(errno)); |
| 532 | exit (EXIT_FAILURE); |
| 533 | } |
| 534 | |
| 535 | ptr = (unsigned char *)mmap(0, sbuf.st_size, |
| 536 | PROT_READ, MAP_SHARED, dfd, 0); |
| 537 | if (ptr == (unsigned char *)MAP_FAILED) { |
| 538 | fprintf (stderr, "%s: Can't read %s: %s\n", |
| 539 | cmdname, datafile, strerror(errno)); |
| 540 | exit (EXIT_FAILURE); |
| 541 | } |
| 542 | |
| 543 | if (xflag) { |
| 544 | unsigned char *p = NULL; |
| 545 | /* |
| 546 | * XIP: do not append the image_header_t at the |
| 547 | * beginning of the file, but consume the space |
| 548 | * reserved for it. |
| 549 | */ |
| 550 | |
wdenk | 5bb226e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 551 | if ((unsigned)sbuf.st_size < sizeof(image_header_t)) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 552 | fprintf (stderr, |
| 553 | "%s: Bad size: \"%s\" is too small for XIP\n", |
| 554 | cmdname, datafile); |
| 555 | exit (EXIT_FAILURE); |
| 556 | } |
| 557 | |
| 558 | for (p=ptr; p < ptr+sizeof(image_header_t); p++) { |
| 559 | if ( *p != 0xff ) { |
| 560 | fprintf (stderr, |
| 561 | "%s: Bad file: \"%s\" has invalid buffer for XIP\n", |
| 562 | cmdname, datafile); |
| 563 | exit (EXIT_FAILURE); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | offset = sizeof(image_header_t); |
| 568 | } |
| 569 | |
| 570 | size = sbuf.st_size - offset; |
| 571 | if (write(ifd, ptr + offset, size) != size) { |
| 572 | fprintf (stderr, "%s: Write error on %s: %s\n", |
| 573 | cmdname, imagefile, strerror(errno)); |
| 574 | exit (EXIT_FAILURE); |
| 575 | } |
| 576 | |
| 577 | if (pad && ((tail = size % 4) != 0)) { |
| 578 | |
| 579 | if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { |
| 580 | fprintf (stderr, "%s: Write error on %s: %s\n", |
| 581 | cmdname, imagefile, strerror(errno)); |
| 582 | exit (EXIT_FAILURE); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | (void) munmap((void *)ptr, sbuf.st_size); |
| 587 | (void) close (dfd); |
| 588 | } |
| 589 | |
| 590 | void |
| 591 | usage () |
| 592 | { |
| 593 | fprintf (stderr, "Usage: %s -l image\n" |
| 594 | " -l ==> list image header information\n" |
wdenk | 9d5028c | 2004-11-21 00:06:33 +0000 | [diff] [blame] | 595 | " %s [-x] -A arch -O os -T type -C comp " |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 596 | "-a addr -e ep -n name -d data_file[:data_file...] image\n", |
| 597 | cmdname, cmdname); |
| 598 | fprintf (stderr, " -A ==> set architecture to 'arch'\n" |
| 599 | " -O ==> set operating system to 'os'\n" |
| 600 | " -T ==> set image type to 'type'\n" |
| 601 | " -C ==> set compression type 'comp'\n" |
| 602 | " -a ==> set load address to 'addr' (hex)\n" |
| 603 | " -e ==> set entry point to 'ep' (hex)\n" |
| 604 | " -n ==> set image name to 'name'\n" |
| 605 | " -d ==> use image data from 'datafile'\n" |
| 606 | " -x ==> set XIP (execute in place)\n" |
| 607 | ); |
| 608 | exit (EXIT_FAILURE); |
| 609 | } |
| 610 | |
| 611 | static void |
| 612 | print_header (image_header_t *hdr) |
| 613 | { |
| 614 | time_t timestamp; |
| 615 | uint32_t size; |
| 616 | |
| 617 | timestamp = (time_t)ntohl(hdr->ih_time); |
| 618 | size = ntohl(hdr->ih_size); |
| 619 | |
| 620 | printf ("Image Name: %.*s\n", IH_NMLEN, hdr->ih_name); |
| 621 | printf ("Created: %s", ctime(×tamp)); |
| 622 | printf ("Image Type: "); print_type(hdr); |
| 623 | printf ("Data Size: %d Bytes = %.2f kB = %.2f MB\n", |
| 624 | size, (double)size / 1.024e3, (double)size / 1.048576e6 ); |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 625 | printf ("Load Address: 0x%08X\n", ntohl(hdr->ih_load)); |
| 626 | printf ("Entry Point: 0x%08X\n", ntohl(hdr->ih_ep)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 627 | |
| 628 | if (hdr->ih_type == IH_TYPE_MULTI || hdr->ih_type == IH_TYPE_SCRIPT) { |
| 629 | int i, ptrs; |
| 630 | uint32_t pos; |
| 631 | unsigned long *len_ptr = (unsigned long *) ( |
| 632 | (unsigned long)hdr + sizeof(image_header_t) |
| 633 | ); |
| 634 | |
| 635 | /* determine number of images first (to calculate image offsets) */ |
| 636 | for (i=0; len_ptr[i]; ++i) /* null pointer terminates list */ |
| 637 | ; |
| 638 | ptrs = i; /* null pointer terminates list */ |
| 639 | |
| 640 | pos = sizeof(image_header_t) + ptrs * sizeof(long); |
| 641 | printf ("Contents:\n"); |
| 642 | for (i=0; len_ptr[i]; ++i) { |
| 643 | size = ntohl(len_ptr[i]); |
| 644 | |
| 645 | printf (" Image %d: %8d Bytes = %4d kB = %d MB\n", |
| 646 | i, size, size>>10, size>>20); |
| 647 | if (hdr->ih_type == IH_TYPE_SCRIPT && i > 0) { |
| 648 | /* |
| 649 | * the user may need to know offsets |
| 650 | * if planning to do something with |
| 651 | * multiple files |
| 652 | */ |
wdenk | bdccc4f | 2003-08-05 17:43:17 +0000 | [diff] [blame] | 653 | printf (" Offset = %08X\n", pos); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 654 | } |
| 655 | /* copy_file() will pad the first files to even word align */ |
| 656 | size += 3; |
| 657 | size &= ~3; |
| 658 | pos += size; |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | |
| 664 | static void |
| 665 | print_type (image_header_t *hdr) |
| 666 | { |
| 667 | printf ("%s %s %s (%s)\n", |
| 668 | put_arch (hdr->ih_arch), |
| 669 | put_os (hdr->ih_os ), |
| 670 | put_type (hdr->ih_type), |
| 671 | put_comp (hdr->ih_comp) |
| 672 | ); |
| 673 | } |
| 674 | |
| 675 | static char *put_arch (int arch) |
| 676 | { |
| 677 | return (put_table_entry(arch_name, "Unknown Architecture", arch)); |
| 678 | } |
| 679 | |
| 680 | static char *put_os (int os) |
| 681 | { |
| 682 | return (put_table_entry(os_name, "Unknown OS", os)); |
| 683 | } |
| 684 | |
| 685 | static char *put_type (int type) |
| 686 | { |
| 687 | return (put_table_entry(type_name, "Unknown Image", type)); |
| 688 | } |
| 689 | |
| 690 | static char *put_comp (int comp) |
| 691 | { |
| 692 | return (put_table_entry(comp_name, "Unknown Compression", comp)); |
| 693 | } |
| 694 | |
| 695 | static char *put_table_entry (table_entry_t *table, char *msg, int type) |
| 696 | { |
| 697 | for (; table->val>=0; ++table) { |
| 698 | if (table->val == type) |
| 699 | return (table->lname); |
| 700 | } |
| 701 | return (msg); |
| 702 | } |
| 703 | |
| 704 | static int get_arch(char *name) |
| 705 | { |
| 706 | return (get_table_entry(arch_name, "CPU", name)); |
| 707 | } |
| 708 | |
| 709 | |
| 710 | static int get_comp(char *name) |
| 711 | { |
| 712 | return (get_table_entry(comp_name, "Compression", name)); |
| 713 | } |
| 714 | |
| 715 | |
| 716 | static int get_os (char *name) |
| 717 | { |
| 718 | return (get_table_entry(os_name, "OS", name)); |
| 719 | } |
| 720 | |
| 721 | |
| 722 | static int get_type(char *name) |
| 723 | { |
| 724 | return (get_table_entry(type_name, "Image", name)); |
| 725 | } |
| 726 | |
| 727 | static int get_table_entry (table_entry_t *table, char *msg, char *name) |
| 728 | { |
| 729 | table_entry_t *t; |
| 730 | int first = 1; |
| 731 | |
| 732 | for (t=table; t->val>=0; ++t) { |
| 733 | if (t->sname && strcasecmp(t->sname, name)==0) |
| 734 | return (t->val); |
| 735 | } |
| 736 | fprintf (stderr, "\nInvalid %s Type - valid names are", msg); |
| 737 | for (t=table; t->val>=0; ++t) { |
| 738 | if (t->sname == NULL) |
| 739 | continue; |
| 740 | fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname); |
| 741 | first = 0; |
| 742 | } |
| 743 | fprintf (stderr, "\n"); |
| 744 | return (-1); |
| 745 | } |