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