]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - tools/mkimage.c
Merge branch 'mpc86xx'
[karo-tx-uboot.git] / 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     {   IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",   },
134     {   -1,                 "",           "",                   },
135 };
136
137 table_entry_t comp_name[] = {
138     {   IH_COMP_NONE,   "none",         "uncompressed",         },
139     {   IH_COMP_BZIP2,  "bzip2",        "bzip2 compressed",     },
140     {   IH_COMP_GZIP,   "gzip",         "gzip 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,
282                                 "%s: For XIP, the entry point must be the load addr + %lu\n",
283                                 cmdname,
284                                 (unsigned long)sizeof(image_header_t));
285                         exit (EXIT_FAILURE);
286                 }
287         }
288
289         imagefile = *argv;
290
291         if (lflag) {
292                 ifd = open(imagefile, O_RDONLY|O_BINARY);
293         } else {
294                 ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
295         }
296
297         if (ifd < 0) {
298                 fprintf (stderr, "%s: Can't open %s: %s\n",
299                         cmdname, imagefile, strerror(errno));
300                 exit (EXIT_FAILURE);
301         }
302
303         if (lflag) {
304                 int len;
305                 char *data;
306                 /*
307                  * list header information of existing image
308                  */
309                 if (fstat(ifd, &sbuf) < 0) {
310                         fprintf (stderr, "%s: Can't stat %s: %s\n",
311                                 cmdname, imagefile, strerror(errno));
312                         exit (EXIT_FAILURE);
313                 }
314
315                 if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
316                         fprintf (stderr,
317                                 "%s: Bad size: \"%s\" is no valid image\n",
318                                 cmdname, imagefile);
319                         exit (EXIT_FAILURE);
320                 }
321
322                 ptr = (unsigned char *)mmap(0, sbuf.st_size,
323                                             PROT_READ, MAP_SHARED, ifd, 0);
324                 if ((caddr_t)ptr == (caddr_t)-1) {
325                         fprintf (stderr, "%s: Can't read %s: %s\n",
326                                 cmdname, imagefile, strerror(errno));
327                         exit (EXIT_FAILURE);
328                 }
329
330                 /*
331                  * create copy of header so that we can blank out the
332                  * checksum field for checking - this can't be done
333                  * on the PROT_READ mapped data.
334                  */
335                 memcpy (hdr, ptr, sizeof(image_header_t));
336
337                 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
338                         fprintf (stderr,
339                                 "%s: Bad Magic Number: \"%s\" is no valid image\n",
340                                 cmdname, imagefile);
341                         exit (EXIT_FAILURE);
342                 }
343
344                 data = (char *)hdr;
345                 len  = sizeof(image_header_t);
346
347                 checksum = ntohl(hdr->ih_hcrc);
348                 hdr->ih_hcrc = htonl(0);        /* clear for re-calculation */
349
350                 if (crc32 (0, data, len) != checksum) {
351                         fprintf (stderr,
352                                 "%s: ERROR: \"%s\" has bad header checksum!\n",
353                                 cmdname, imagefile);
354                         exit (EXIT_FAILURE);
355                 }
356
357                 data = (char *)(ptr + sizeof(image_header_t));
358                 len  = sbuf.st_size - sizeof(image_header_t) ;
359
360                 if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) {
361                         fprintf (stderr,
362                                 "%s: ERROR: \"%s\" has corrupted data!\n",
363                                 cmdname, imagefile);
364                         exit (EXIT_FAILURE);
365                 }
366
367                 /* for multi-file images we need the data part, too */
368                 print_header ((image_header_t *)ptr);
369
370                 (void) munmap((void *)ptr, sbuf.st_size);
371                 (void) close (ifd);
372
373                 exit (EXIT_SUCCESS);
374         }
375
376         /*
377          * Must be -w then:
378          *
379          * write dummy header, to be fixed later
380          */
381         memset (hdr, 0, sizeof(image_header_t));
382
383         if (write(ifd, hdr, sizeof(image_header_t)) != sizeof(image_header_t)) {
384                 fprintf (stderr, "%s: Write error on %s: %s\n",
385                         cmdname, imagefile, strerror(errno));
386                 exit (EXIT_FAILURE);
387         }
388
389         if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
390                 char *file = datafile;
391                 uint32_t size;
392
393                 for (;;) {
394                         char *sep = NULL;
395
396                         if (file) {
397                                 if ((sep = strchr(file, ':')) != NULL) {
398                                         *sep = '\0';
399                                 }
400
401                                 if (stat (file, &sbuf) < 0) {
402                                         fprintf (stderr, "%s: Can't stat %s: %s\n",
403                                                 cmdname, file, strerror(errno));
404                                         exit (EXIT_FAILURE);
405                                 }
406                                 size = htonl(sbuf.st_size);
407                         } else {
408                                 size = 0;
409                         }
410
411                         if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
412                                 fprintf (stderr, "%s: Write error on %s: %s\n",
413                                         cmdname, imagefile, strerror(errno));
414                                 exit (EXIT_FAILURE);
415                         }
416
417                         if (!file) {
418                                 break;
419                         }
420
421                         if (sep) {
422                                 *sep = ':';
423                                 file = sep + 1;
424                         } else {
425                                 file = NULL;
426                         }
427                 }
428
429                 file = datafile;
430
431                 for (;;) {
432                         char *sep = strchr(file, ':');
433                         if (sep) {
434                                 *sep = '\0';
435                                 copy_file (ifd, file, 1);
436                                 *sep++ = ':';
437                                 file = sep;
438                         } else {
439                                 copy_file (ifd, file, 0);
440                                 break;
441                         }
442                 }
443         } else {
444                 copy_file (ifd, datafile, 0);
445         }
446
447         /* We're a bit of paranoid */
448 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
449         (void) fdatasync (ifd);
450 #else
451         (void) fsync (ifd);
452 #endif
453
454         if (fstat(ifd, &sbuf) < 0) {
455                 fprintf (stderr, "%s: Can't stat %s: %s\n",
456                         cmdname, imagefile, strerror(errno));
457                 exit (EXIT_FAILURE);
458         }
459
460         ptr = (unsigned char *)mmap(0, sbuf.st_size,
461                                     PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
462         if (ptr == (unsigned char *)MAP_FAILED) {
463                 fprintf (stderr, "%s: Can't map %s: %s\n",
464                         cmdname, imagefile, strerror(errno));
465                 exit (EXIT_FAILURE);
466         }
467
468         hdr = (image_header_t *)ptr;
469
470         checksum = crc32 (0,
471                           (const char *)(ptr + sizeof(image_header_t)),
472                           sbuf.st_size - sizeof(image_header_t)
473                          );
474
475         /* Build new header */
476         hdr->ih_magic = htonl(IH_MAGIC);
477         hdr->ih_time  = htonl(sbuf.st_mtime);
478         hdr->ih_size  = htonl(sbuf.st_size - sizeof(image_header_t));
479         hdr->ih_load  = htonl(addr);
480         hdr->ih_ep    = htonl(ep);
481         hdr->ih_dcrc  = htonl(checksum);
482         hdr->ih_os    = opt_os;
483         hdr->ih_arch  = opt_arch;
484         hdr->ih_type  = opt_type;
485         hdr->ih_comp  = opt_comp;
486
487         strncpy((char *)hdr->ih_name, name, IH_NMLEN);
488
489         checksum = crc32(0,(const char *)hdr,sizeof(image_header_t));
490
491         hdr->ih_hcrc = htonl(checksum);
492
493         print_header (hdr);
494
495         (void) munmap((void *)ptr, sbuf.st_size);
496
497         /* We're a bit of paranoid */
498 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
499         (void) fdatasync (ifd);
500 #else
501         (void) fsync (ifd);
502 #endif
503
504         if (close(ifd)) {
505                 fprintf (stderr, "%s: Write error on %s: %s\n",
506                         cmdname, imagefile, strerror(errno));
507                 exit (EXIT_FAILURE);
508         }
509
510         exit (EXIT_SUCCESS);
511 }
512
513 static void
514 copy_file (int ifd, const char *datafile, int pad)
515 {
516         int dfd;
517         struct stat sbuf;
518         unsigned char *ptr;
519         int tail;
520         int zero = 0;
521         int offset = 0;
522         int size;
523
524         if (vflag) {
525                 fprintf (stderr, "Adding Image %s\n", datafile);
526         }
527
528         if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
529                 fprintf (stderr, "%s: Can't open %s: %s\n",
530                         cmdname, datafile, strerror(errno));
531                 exit (EXIT_FAILURE);
532         }
533
534         if (fstat(dfd, &sbuf) < 0) {
535                 fprintf (stderr, "%s: Can't stat %s: %s\n",
536                         cmdname, datafile, strerror(errno));
537                 exit (EXIT_FAILURE);
538         }
539
540         ptr = (unsigned char *)mmap(0, sbuf.st_size,
541                                     PROT_READ, MAP_SHARED, dfd, 0);
542         if (ptr == (unsigned char *)MAP_FAILED) {
543                 fprintf (stderr, "%s: Can't read %s: %s\n",
544                         cmdname, datafile, strerror(errno));
545                 exit (EXIT_FAILURE);
546         }
547
548         if (xflag) {
549                 unsigned char *p = NULL;
550                 /*
551                  * XIP: do not append the image_header_t at the
552                  * beginning of the file, but consume the space
553                  * reserved for it.
554                  */
555
556                 if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
557                         fprintf (stderr,
558                                 "%s: Bad size: \"%s\" is too small for XIP\n",
559                                 cmdname, datafile);
560                         exit (EXIT_FAILURE);
561                 }
562
563                 for (p=ptr; p < ptr+sizeof(image_header_t); p++) {
564                         if ( *p != 0xff ) {
565                                 fprintf (stderr,
566                                         "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
567                                         cmdname, datafile);
568                                 exit (EXIT_FAILURE);
569                         }
570                 }
571
572                 offset = sizeof(image_header_t);
573         }
574
575         size = sbuf.st_size - offset;
576         if (write(ifd, ptr + offset, size) != size) {
577                 fprintf (stderr, "%s: Write error on %s: %s\n",
578                         cmdname, imagefile, strerror(errno));
579                 exit (EXIT_FAILURE);
580         }
581
582         if (pad && ((tail = size % 4) != 0)) {
583
584                 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
585                         fprintf (stderr, "%s: Write error on %s: %s\n",
586                                 cmdname, imagefile, strerror(errno));
587                         exit (EXIT_FAILURE);
588                 }
589         }
590
591         (void) munmap((void *)ptr, sbuf.st_size);
592         (void) close (dfd);
593 }
594
595 void
596 usage ()
597 {
598         fprintf (stderr, "Usage: %s -l image\n"
599                          "          -l ==> list image header information\n"
600                          "       %s [-x] -A arch -O os -T type -C comp "
601                          "-a addr -e ep -n name -d data_file[:data_file...] image\n",
602                 cmdname, cmdname);
603         fprintf (stderr, "          -A ==> set architecture to 'arch'\n"
604                          "          -O ==> set operating system to 'os'\n"
605                          "          -T ==> set image type to 'type'\n"
606                          "          -C ==> set compression type 'comp'\n"
607                          "          -a ==> set load address to 'addr' (hex)\n"
608                          "          -e ==> set entry point to 'ep' (hex)\n"
609                          "          -n ==> set image name to 'name'\n"
610                          "          -d ==> use image data from 'datafile'\n"
611                          "          -x ==> set XIP (execute in place)\n"
612                 );
613         exit (EXIT_FAILURE);
614 }
615
616 static void
617 print_header (image_header_t *hdr)
618 {
619         time_t timestamp;
620         uint32_t size;
621
622         timestamp = (time_t)ntohl(hdr->ih_time);
623         size = ntohl(hdr->ih_size);
624
625         printf ("Image Name:   %.*s\n", IH_NMLEN, hdr->ih_name);
626         printf ("Created:      %s", ctime(&timestamp));
627         printf ("Image Type:   "); print_type(hdr);
628         printf ("Data Size:    %d Bytes = %.2f kB = %.2f MB\n",
629                 size, (double)size / 1.024e3, (double)size / 1.048576e6 );
630         printf ("Load Address: 0x%08X\n", ntohl(hdr->ih_load));
631         printf ("Entry Point:  0x%08X\n", ntohl(hdr->ih_ep));
632
633         if (hdr->ih_type == IH_TYPE_MULTI || hdr->ih_type == IH_TYPE_SCRIPT) {
634                 int i, ptrs;
635                 uint32_t pos;
636                 unsigned long *len_ptr = (unsigned long *) (
637                                         (unsigned long)hdr + sizeof(image_header_t)
638                                 );
639
640                 /* determine number of images first (to calculate image offsets) */
641                 for (i=0; len_ptr[i]; ++i)      /* null pointer terminates list */
642                         ;
643                 ptrs = i;               /* null pointer terminates list */
644
645                 pos = sizeof(image_header_t) + ptrs * sizeof(long);
646                 printf ("Contents:\n");
647                 for (i=0; len_ptr[i]; ++i) {
648                         size = ntohl(len_ptr[i]);
649
650                         printf ("   Image %d: %8d Bytes = %4d kB = %d MB\n",
651                                 i, size, size>>10, size>>20);
652                         if (hdr->ih_type == IH_TYPE_SCRIPT && i > 0) {
653                                 /*
654                                  * the user may need to know offsets
655                                  * if planning to do something with
656                                  * multiple files
657                                  */
658                                 printf ("    Offset = %08X\n", pos);
659                         }
660                         /* copy_file() will pad the first files to even word align */
661                         size += 3;
662                         size &= ~3;
663                         pos += size;
664                 }
665         }
666 }
667
668
669 static void
670 print_type (image_header_t *hdr)
671 {
672         printf ("%s %s %s (%s)\n",
673                 put_arch (hdr->ih_arch),
674                 put_os   (hdr->ih_os  ),
675                 put_type (hdr->ih_type),
676                 put_comp (hdr->ih_comp)
677         );
678 }
679
680 static char *put_arch (int arch)
681 {
682         return (put_table_entry(arch_name, "Unknown Architecture", arch));
683 }
684
685 static char *put_os (int os)
686 {
687         return (put_table_entry(os_name, "Unknown OS", os));
688 }
689
690 static char *put_type (int type)
691 {
692         return (put_table_entry(type_name, "Unknown Image", type));
693 }
694
695 static char *put_comp (int comp)
696 {
697         return (put_table_entry(comp_name, "Unknown Compression", comp));
698 }
699
700 static char *put_table_entry (table_entry_t *table, char *msg, int type)
701 {
702         for (; table->val>=0; ++table) {
703                 if (table->val == type)
704                         return (table->lname);
705         }
706         return (msg);
707 }
708
709 static int get_arch(char *name)
710 {
711         return (get_table_entry(arch_name, "CPU", name));
712 }
713
714
715 static int get_comp(char *name)
716 {
717         return (get_table_entry(comp_name, "Compression", name));
718 }
719
720
721 static int get_os (char *name)
722 {
723         return (get_table_entry(os_name, "OS", name));
724 }
725
726
727 static int get_type(char *name)
728 {
729         return (get_table_entry(type_name, "Image", name));
730 }
731
732 static int get_table_entry (table_entry_t *table, char *msg, char *name)
733 {
734         table_entry_t *t;
735         int first = 1;
736
737         for (t=table; t->val>=0; ++t) {
738                 if (t->sname && strcasecmp(t->sname, name)==0)
739                         return (t->val);
740         }
741         fprintf (stderr, "\nInvalid %s Type - valid names are", msg);
742         for (t=table; t->val>=0; ++t) {
743                 if (t->sname == NULL)
744                         continue;
745                 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
746                 first = 0;
747         }
748         fprintf (stderr, "\n");
749         return (-1);
750 }