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