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