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