2 * (C) Copyright 2000-2002
3 * DENX Software Engineering
4 * Wolfgang Denk, wd@denx.de
14 #include <netinet/in.h> /* for host / network byte order conversions */
21 #if defined(__BEOS__) || defined(__NetBSD__) || defined(__APPLE__)
26 typedef unsigned int __u32;
28 #define SWAP_LONG(x) \
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;
38 #define ntohl(a) SWAP_LONG(a)
39 #define htonl(a) SWAP_LONG(a)
40 #endif /* __WIN32__ */
47 #define MAP_FAILED (-1)
52 extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
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 */
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", },
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", },
98 table_entry_t type_name[] = {
99 { IH_TYPE_INVALID, NULL, "Invalid Image", },
100 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
101 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
102 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
103 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
104 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
105 { IH_TYPE_SCRIPT, "script", "Script", },
109 table_entry_t comp_name[] = {
110 { IH_COMP_NONE, "none", "uncompressed", },
111 { IH_COMP_GZIP, "gzip", "gzip compressed", },
112 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
116 static void copy_file (int, const char *, int);
117 static void usage (void);
118 static void print_header (image_header_t *);
119 static void print_type (image_header_t *);
120 static char *put_table_entry (table_entry_t *, char *, int);
121 static char *put_arch (int);
122 static char *put_type (int);
123 static char *put_os (int);
124 static char *put_comp (int);
125 static int get_table_entry (table_entry_t *, char *, char *);
126 static int get_arch(char *);
127 static int get_comp(char *);
128 static int get_os (char *);
129 static int get_type(char *);
140 int opt_os = IH_OS_LINUX;
141 int opt_arch = IH_CPU_PPC;
142 int opt_type = IH_TYPE_KERNEL;
143 int opt_comp = IH_COMP_GZIP;
145 image_header_t header;
146 image_header_t *hdr = &header;
149 main (int argc, char **argv)
163 while (--argc > 0 && **++argv == '-') {
171 (opt_arch = get_arch(*++argv)) < 0)
176 (opt_comp = get_comp(*++argv)) < 0)
181 (opt_os = get_os(*++argv)) < 0)
186 (opt_type = get_type(*++argv)) < 0)
193 addr = strtoul (*++argv, (char **)&ptr, 16);
196 "%s: invalid load address %s\n",
210 ep = strtoul (*++argv, (char **)&ptr, 16);
213 "%s: invalid entry point %s\n",
237 if ((argc != 1) || ((lflag ^ dflag) == 0))
242 /* If XIP, entry point must be after the U-Boot header */
244 ep += sizeof(image_header_t);
248 * If XIP, ensure the entry point is equal to the load address plus
249 * the size of the U-Boot header.
252 if (ep != addr + sizeof(image_header_t)) {
253 fprintf (stderr, "%s: For XIP, the entry point must be the load addr + %d\n",
254 cmdname, sizeof(image_header_t));
262 ifd = open(imagefile, O_RDONLY);
265 ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
267 ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC, 0666);
272 fprintf (stderr, "%s: Can't open %s: %s\n",
273 cmdname, imagefile, strerror(errno));
281 * list header information of existing image
283 if (fstat(ifd, &sbuf) < 0) {
284 fprintf (stderr, "%s: Can't stat %s: %s\n",
285 cmdname, imagefile, strerror(errno));
289 if (sbuf.st_size < sizeof(image_header_t)) {
291 "%s: Bad size: \"%s\" is no valid image\n",
296 ptr = (unsigned char *)mmap(0, sbuf.st_size,
297 PROT_READ, MAP_SHARED, ifd, 0);
298 if ((caddr_t)ptr == (caddr_t)-1) {
299 fprintf (stderr, "%s: Can't read %s: %s\n",
300 cmdname, imagefile, strerror(errno));
305 * create copy of header so that we can blank out the
306 * checksum field for checking - this can't be done
307 * on the PROT_READ mapped data.
309 memcpy (hdr, ptr, sizeof(image_header_t));
311 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
313 "%s: Bad Magic Number: \"%s\" is no valid image\n",
319 len = sizeof(image_header_t);
321 checksum = ntohl(hdr->ih_hcrc);
322 hdr->ih_hcrc = htonl(0); /* clear for re-calculation */
324 if (crc32 (0, data, len) != checksum) {
326 "*** Warning: \"%s\" has bad header checksum!\n",
330 data = (char *)(ptr + sizeof(image_header_t));
331 len = sbuf.st_size - sizeof(image_header_t) ;
333 if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) {
335 "*** Warning: \"%s\" has corrupted data!\n",
339 /* for multi-file images we need the data part, too */
340 print_header ((image_header_t *)ptr);
342 (void) munmap((void *)ptr, sbuf.st_size);
351 * write dummy header, to be fixed later
353 memset (hdr, 0, sizeof(image_header_t));
355 if (write(ifd, hdr, sizeof(image_header_t)) != sizeof(image_header_t)) {
356 fprintf (stderr, "%s: Write error on %s: %s\n",
357 cmdname, imagefile, strerror(errno));
361 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
362 char *file = datafile;
369 if ((sep = strchr(file, ':')) != NULL) {
373 if (stat (file, &sbuf) < 0) {
374 fprintf (stderr, "%s: Can't stat %s: %s\n",
375 cmdname, file, strerror(errno));
378 size = htonl(sbuf.st_size);
383 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
384 fprintf (stderr, "%s: Write error on %s: %s\n",
385 cmdname, imagefile, strerror(errno));
404 char *sep = strchr(file, ':');
407 copy_file (ifd, file, 1);
411 copy_file (ifd, file, 0);
416 copy_file (ifd, datafile, 0);
419 /* We're a bit of paranoid */
420 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__)
421 (void) fdatasync (ifd);
426 if (fstat(ifd, &sbuf) < 0) {
427 fprintf (stderr, "%s: Can't stat %s: %s\n",
428 cmdname, imagefile, strerror(errno));
432 ptr = (unsigned char *)mmap(0, sbuf.st_size,
433 PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
434 if (ptr == (unsigned char *)MAP_FAILED) {
435 fprintf (stderr, "%s: Can't map %s: %s\n",
436 cmdname, imagefile, strerror(errno));
440 hdr = (image_header_t *)ptr;
443 (const char *)(ptr + sizeof(image_header_t)),
444 sbuf.st_size - sizeof(image_header_t)
447 /* Build new header */
448 hdr->ih_magic = htonl(IH_MAGIC);
449 hdr->ih_time = htonl(sbuf.st_mtime);
450 hdr->ih_size = htonl(sbuf.st_size - sizeof(image_header_t));
451 hdr->ih_load = htonl(addr);
452 hdr->ih_ep = htonl(ep);
453 hdr->ih_dcrc = htonl(checksum);
455 hdr->ih_arch = opt_arch;
456 hdr->ih_type = opt_type;
457 hdr->ih_comp = opt_comp;
459 strncpy((char *)hdr->ih_name, name, IH_NMLEN);
461 checksum = crc32(0,(const char *)hdr,sizeof(image_header_t));
463 hdr->ih_hcrc = htonl(checksum);
467 (void) munmap((void *)ptr, sbuf.st_size);
469 /* We're a bit of paranoid */
470 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__)
471 (void) fdatasync (ifd);
477 fprintf (stderr, "%s: Write error on %s: %s\n",
478 cmdname, imagefile, strerror(errno));
486 copy_file (int ifd, const char *datafile, int pad)
497 fprintf (stderr, "Adding Image %s\n", datafile);
500 if ((dfd = open(datafile, O_RDONLY)) < 0) {
501 fprintf (stderr, "%s: Can't open %s: %s\n",
502 cmdname, datafile, strerror(errno));
506 if (fstat(dfd, &sbuf) < 0) {
507 fprintf (stderr, "%s: Can't stat %s: %s\n",
508 cmdname, datafile, strerror(errno));
512 ptr = (unsigned char *)mmap(0, sbuf.st_size,
513 PROT_READ, MAP_SHARED, dfd, 0);
514 if (ptr == (unsigned char *)MAP_FAILED) {
515 fprintf (stderr, "%s: Can't read %s: %s\n",
516 cmdname, datafile, strerror(errno));
521 unsigned char *p = NULL;
523 * XIP: do not append the image_header_t at the
524 * beginning of the file, but consume the space
528 if (sbuf.st_size < sizeof(image_header_t)) {
530 "%s: Bad size: \"%s\" is too small for XIP\n",
535 for (p=ptr; p < ptr+sizeof(image_header_t); p++) {
538 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
544 offset = sizeof(image_header_t);
547 size = sbuf.st_size - offset;
548 if (write(ifd, ptr + offset, size) != size) {
549 fprintf (stderr, "%s: Write error on %s: %s\n",
550 cmdname, imagefile, strerror(errno));
554 if (pad && ((tail = size % 4) != 0)) {
556 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
557 fprintf (stderr, "%s: Write error on %s: %s\n",
558 cmdname, imagefile, strerror(errno));
563 (void) munmap((void *)ptr, sbuf.st_size);
570 fprintf (stderr, "Usage: %s -l image\n"
571 " -l ==> list image header information\n"
572 " %s -A arch -O os -T type -C comp "
573 "-a addr -e ep -n name -d data_file[:data_file...] image\n",
575 fprintf (stderr, " -A ==> set architecture to 'arch'\n"
576 " -O ==> set operating system to 'os'\n"
577 " -T ==> set image type to 'type'\n"
578 " -C ==> set compression type 'comp'\n"
579 " -a ==> set load address to 'addr' (hex)\n"
580 " -e ==> set entry point to 'ep' (hex)\n"
581 " -n ==> set image name to 'name'\n"
582 " -d ==> use image data from 'datafile'\n"
583 " -x ==> set XIP (execute in place)\n"
589 print_header (image_header_t *hdr)
594 timestamp = (time_t)ntohl(hdr->ih_time);
595 size = ntohl(hdr->ih_size);
597 printf ("Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
598 printf ("Created: %s", ctime(×tamp));
599 printf ("Image Type: "); print_type(hdr);
600 printf ("Data Size: %d Bytes = %.2f kB = %.2f MB\n",
601 size, (double)size / 1.024e3, (double)size / 1.048576e6 );
602 printf ("Load Address: 0x%08x\n", ntohl(hdr->ih_load));
603 printf ("Entry Point: 0x%08x\n", ntohl(hdr->ih_ep));
605 if (hdr->ih_type == IH_TYPE_MULTI || hdr->ih_type == IH_TYPE_SCRIPT) {
608 unsigned long *len_ptr = (unsigned long *) (
609 (unsigned long)hdr + sizeof(image_header_t)
612 /* determine number of images first (to calculate image offsets) */
613 for (i=0; len_ptr[i]; ++i) /* null pointer terminates list */
615 ptrs = i; /* null pointer terminates list */
617 pos = sizeof(image_header_t) + ptrs * sizeof(long);
618 printf ("Contents:\n");
619 for (i=0; len_ptr[i]; ++i) {
620 size = ntohl(len_ptr[i]);
622 printf (" Image %d: %8d Bytes = %4d kB = %d MB\n",
623 i, size, size>>10, size>>20);
624 if (hdr->ih_type == IH_TYPE_SCRIPT && i > 0) {
626 * the user may need to know offsets
627 * if planning to do something with
630 printf (" Offset = %08x\n", pos);
632 /* copy_file() will pad the first files to even word align */
642 print_type (image_header_t *hdr)
644 printf ("%s %s %s (%s)\n",
645 put_arch (hdr->ih_arch),
646 put_os (hdr->ih_os ),
647 put_type (hdr->ih_type),
648 put_comp (hdr->ih_comp)
652 static char *put_arch (int arch)
654 return (put_table_entry(arch_name, "Unknown Architecture", arch));
657 static char *put_os (int os)
659 return (put_table_entry(os_name, "Unknown OS", os));
662 static char *put_type (int type)
664 return (put_table_entry(type_name, "Unknown Image", type));
667 static char *put_comp (int comp)
669 return (put_table_entry(comp_name, "Unknown Compression", comp));
672 static char *put_table_entry (table_entry_t *table, char *msg, int type)
674 for (; table->val>=0; ++table) {
675 if (table->val == type)
676 return (table->lname);
681 static int get_arch(char *name)
683 return (get_table_entry(arch_name, "CPU", name));
687 static int get_comp(char *name)
689 return (get_table_entry(comp_name, "Compression", name));
693 static int get_os (char *name)
695 return (get_table_entry(os_name, "OS", name));
699 static int get_type(char *name)
701 return (get_table_entry(type_name, "Image", name));
704 static int get_table_entry (table_entry_t *table, char *msg, char *name)
709 for (t=table; t->val>=0; ++t) {
710 if (t->sname && strcasecmp(t->sname, name)==0)
713 fprintf (stderr, "\nInvalid %s Type - valid names are", msg);
714 for (t=table; t->val>=0; ++t) {
715 if (t->sname == NULL)
717 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
720 fprintf (stderr, "\n");