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