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