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