blob: d35abc99e6c5e218f84e4628903deaa03e533c90 [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
wdenk0c852a22004-02-26 23:01:04 +00002 * (C) Copyright 2000-2004
wdenk5b1d7132002-11-03 00:07:02 +00003 * DENX Software Engineering
4 * Wolfgang Denk, wd@denx.de
5 * All rights reserved.
wdenk0c852a22004-02-26 23:01:04 +00006 *
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
wdenk5b1d7132002-11-03 00:07:02 +000021 */
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__
41typedef 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) ))
49typedef unsigned char uint8_t;
50typedef unsigned short uint16_t;
51typedef unsigned int uint32_t;
52
53#define ntohl(a) SWAP_LONG(a)
54#define htonl(a) SWAP_LONG(a)
55#endif /* __WIN32__ */
56
wdenkacf98e72003-09-16 11:39:10 +000057#ifndef O_BINARY /* should be define'd on __WIN32__ */
58#define O_BINARY 0
59#endif
60
wdenk5b1d7132002-11-03 00:07:02 +000061#include <image.h>
62
63extern int errno;
64
65#ifndef MAP_FAILED
66#define MAP_FAILED (-1)
67#endif
68
69char *cmdname;
70
71extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
72
73typedef 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
79table_entry_t arch_name[] = {
wdenk466b7412004-07-10 22:35:59 +000080 { 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, "", "", },
wdenk5b1d7132002-11-03 00:07:02 +000095};
96
97table_entry_t os_name[] = {
98 { IH_OS_INVALID, NULL, "Invalid OS", },
wdenk5b1d7132002-11-03 00:07:02 +000099 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
wdenk466b7412004-07-10 22:35:59 +0000100 { IH_OS_ARTOS, "artos", "ARTOS", },
wdenk5b1d7132002-11-03 00:07:02 +0000101 { IH_OS_DELL, "dell", "Dell", },
wdenk466b7412004-07-10 22:35:59 +0000102 { IH_OS_ESIX, "esix", "Esix", },
103 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
104 { IH_OS_IRIX, "irix", "Irix", },
105 { IH_OS_LINUX, "linux", "Linux", },
wdenk5b1d7132002-11-03 00:07:02 +0000106 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
wdenk466b7412004-07-10 22:35:59 +0000107 { IH_OS_NCR, "ncr", "NCR", },
108 { IH_OS_NETBSD, "netbsd", "NetBSD", },
109 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
wdenk5b1d7132002-11-03 00:07:02 +0000110 { IH_OS_PSOS, "psos", "pSOS", },
111 { IH_OS_QNX, "qnx", "QNX", },
wdenkd791b1d2003-04-20 14:04:18 +0000112 { IH_OS_RTEMS, "rtems", "RTEMS", },
wdenk466b7412004-07-10 22:35:59 +0000113 { 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", },
wdenk5b1d7132002-11-03 00:07:02 +0000118 { -1, "", "", },
119};
120
121table_entry_t type_name[] = {
122 { IH_TYPE_INVALID, NULL, "Invalid Image", },
wdenk887b3722003-10-06 22:00:45 +0000123 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
wdenk466b7412004-07-10 22:35:59 +0000124 { 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", },
wdenk5b1d7132002-11-03 00:07:02 +0000130 { -1, "", "", },
131};
132
133table_entry_t comp_name[] = {
134 { IH_COMP_NONE, "none", "uncompressed", },
wdenk5b1d7132002-11-03 00:07:02 +0000135 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
wdenk466b7412004-07-10 22:35:59 +0000136 { IH_COMP_GZIP, "gzip", "gzip compressed", },
wdenk5b1d7132002-11-03 00:07:02 +0000137 { -1, "", "", },
138};
139
140static void copy_file (int, const char *, int);
141static void usage (void);
142static void print_header (image_header_t *);
143static void print_type (image_header_t *);
144static char *put_table_entry (table_entry_t *, char *, int);
145static char *put_arch (int);
146static char *put_type (int);
147static char *put_os (int);
148static char *put_comp (int);
149static int get_table_entry (table_entry_t *, char *, char *);
150static int get_arch(char *);
151static int get_comp(char *);
152static int get_os (char *);
153static int get_type(char *);
154
155
156char *datafile;
157char *imagefile;
158
159int dflag = 0;
160int eflag = 0;
161int lflag = 0;
162int vflag = 0;
163int xflag = 0;
164int opt_os = IH_OS_LINUX;
165int opt_arch = IH_CPU_PPC;
166int opt_type = IH_TYPE_KERNEL;
167int opt_comp = IH_COMP_GZIP;
168
169image_header_t header;
170image_header_t *hdr = &header;
171
172int
173main (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 }
258NXTARG: ;
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)) {
wdenka6c7ad22002-12-03 21:28:10 +0000277 fprintf (stderr, "%s: For XIP, the entry point must be the load addr + %lu\n",
278 cmdname,
279 (unsigned long)sizeof(image_header_t));
wdenk5b1d7132002-11-03 00:07:02 +0000280 exit (EXIT_FAILURE);
281 }
282 }
283
284 imagefile = *argv;
285
286 if (lflag) {
wdenkef1464c2003-10-08 22:14:02 +0000287 ifd = open(imagefile, O_RDONLY|O_BINARY);
wdenk5b1d7132002-11-03 00:07:02 +0000288 } else {
wdenk5b1d7132002-11-03 00:07:02 +0000289 ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
wdenk5b1d7132002-11-03 00:07:02 +0000290 }
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
wdenk5bb226e2003-11-17 21:14:37 +0000310 if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
wdenk5b1d7132002-11-03 00:07:02 +0000311 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 */
wdenk147031a2003-10-07 10:33:38 +0000441#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
wdenk5b1d7132002-11-03 00:07:02 +0000442 (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 */
wdenk147031a2003-10-07 10:33:38 +0000491#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
wdenk5b1d7132002-11-03 00:07:02 +0000492 (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
506static void
507copy_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
wdenkef1464c2003-10-08 22:14:02 +0000521 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000522 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
wdenk5bb226e2003-11-17 21:14:37 +0000549 if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
wdenk5b1d7132002-11-03 00:07:02 +0000550 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
588void
589usage ()
590{
591 fprintf (stderr, "Usage: %s -l image\n"
592 " -l ==> list image header information\n"
wdenk9d5028c2004-11-21 00:06:33 +0000593 " %s [-x] -A arch -O os -T type -C comp "
wdenk5b1d7132002-11-03 00:07:02 +0000594 "-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
609static void
610print_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 );
wdenkbdccc4f2003-08-05 17:43:17 +0000623 printf ("Load Address: 0x%08X\n", ntohl(hdr->ih_load));
624 printf ("Entry Point: 0x%08X\n", ntohl(hdr->ih_ep));
wdenk5b1d7132002-11-03 00:07:02 +0000625
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 */
wdenkbdccc4f2003-08-05 17:43:17 +0000651 printf (" Offset = %08X\n", pos);
wdenk5b1d7132002-11-03 00:07:02 +0000652 }
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
662static void
663print_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
673static char *put_arch (int arch)
674{
675 return (put_table_entry(arch_name, "Unknown Architecture", arch));
676}
677
678static char *put_os (int os)
679{
680 return (put_table_entry(os_name, "Unknown OS", os));
681}
682
683static char *put_type (int type)
684{
685 return (put_table_entry(type_name, "Unknown Image", type));
686}
687
688static char *put_comp (int comp)
689{
690 return (put_table_entry(comp_name, "Unknown Compression", comp));
691}
692
693static 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
702static int get_arch(char *name)
703{
704 return (get_table_entry(arch_name, "CPU", name));
705}
706
707
708static int get_comp(char *name)
709{
710 return (get_table_entry(comp_name, "Compression", name));
711}
712
713
714static int get_os (char *name)
715{
716 return (get_table_entry(os_name, "OS", name));
717}
718
719
720static int get_type(char *name)
721{
722 return (get_table_entry(type_name, "Image", name));
723}
724
725static 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}