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