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