blob: d6000e04bf533f10ed1dd01628d08797cb6a31ab [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", },
wdenkc1a11c12005-04-03 21:11:16 +000089 { IH_CPU_NIOS, "nios", "NIOS", },
90 { IH_CPU_NIOS2, "nios2", "NIOS II", },
wdenk466b7412004-07-10 22:35:59 +000091 { IH_CPU_PPC, "ppc", "PowerPC", },
92 { IH_CPU_S390, "s390", "IBM S390", },
93 { IH_CPU_SH, "sh", "SuperH", },
94 { IH_CPU_SPARC, "sparc", "SPARC", },
95 { IH_CPU_SPARC64, "sparc64", "SPARC 64 Bit", },
Wolfgang Denk0afe5192006-03-12 02:10:00 +010096 { IH_CPU_BLACKFIN, "blackfin", "Blackfin", },
wdenk466b7412004-07-10 22:35:59 +000097 { -1, "", "", },
wdenk5b1d7132002-11-03 00:07:02 +000098};
99
100table_entry_t os_name[] = {
101 { IH_OS_INVALID, NULL, "Invalid OS", },
wdenk5b1d7132002-11-03 00:07:02 +0000102 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
wdenk466b7412004-07-10 22:35:59 +0000103 { IH_OS_ARTOS, "artos", "ARTOS", },
wdenk5b1d7132002-11-03 00:07:02 +0000104 { IH_OS_DELL, "dell", "Dell", },
wdenk466b7412004-07-10 22:35:59 +0000105 { IH_OS_ESIX, "esix", "Esix", },
106 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
107 { IH_OS_IRIX, "irix", "Irix", },
108 { IH_OS_LINUX, "linux", "Linux", },
wdenk5b1d7132002-11-03 00:07:02 +0000109 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
wdenk466b7412004-07-10 22:35:59 +0000110 { IH_OS_NCR, "ncr", "NCR", },
111 { IH_OS_NETBSD, "netbsd", "NetBSD", },
112 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
wdenk5b1d7132002-11-03 00:07:02 +0000113 { IH_OS_PSOS, "psos", "pSOS", },
114 { IH_OS_QNX, "qnx", "QNX", },
wdenkd791b1d2003-04-20 14:04:18 +0000115 { IH_OS_RTEMS, "rtems", "RTEMS", },
wdenk466b7412004-07-10 22:35:59 +0000116 { IH_OS_SCO, "sco", "SCO", },
117 { IH_OS_SOLARIS, "solaris", "Solaris", },
118 { IH_OS_SVR4, "svr4", "SVR4", },
119 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
120 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
wdenk5b1d7132002-11-03 00:07:02 +0000121 { -1, "", "", },
122};
123
124table_entry_t type_name[] = {
125 { IH_TYPE_INVALID, NULL, "Invalid Image", },
wdenk887b3722003-10-06 22:00:45 +0000126 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
wdenk466b7412004-07-10 22:35:59 +0000127 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
128 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
129 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
130 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
131 { IH_TYPE_SCRIPT, "script", "Script", },
132 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Matthew McClintock25c751e2006-08-16 10:54:09 -0500133 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
wdenk5b1d7132002-11-03 00:07:02 +0000134 { -1, "", "", },
135};
136
137table_entry_t comp_name[] = {
138 { IH_COMP_NONE, "none", "uncompressed", },
wdenk5b1d7132002-11-03 00:07:02 +0000139 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
wdenk466b7412004-07-10 22:35:59 +0000140 { IH_COMP_GZIP, "gzip", "gzip compressed", },
wdenk5b1d7132002-11-03 00:07:02 +0000141 { -1, "", "", },
142};
143
144static void copy_file (int, const char *, int);
145static void usage (void);
146static void print_header (image_header_t *);
147static void print_type (image_header_t *);
148static char *put_table_entry (table_entry_t *, char *, int);
149static char *put_arch (int);
150static char *put_type (int);
151static char *put_os (int);
152static char *put_comp (int);
153static int get_table_entry (table_entry_t *, char *, char *);
154static int get_arch(char *);
155static int get_comp(char *);
156static int get_os (char *);
157static int get_type(char *);
158
159
160char *datafile;
161char *imagefile;
162
163int dflag = 0;
164int eflag = 0;
165int lflag = 0;
166int vflag = 0;
167int xflag = 0;
168int opt_os = IH_OS_LINUX;
169int opt_arch = IH_CPU_PPC;
170int opt_type = IH_TYPE_KERNEL;
171int opt_comp = IH_COMP_GZIP;
172
173image_header_t header;
174image_header_t *hdr = &header;
175
176int
177main (int argc, char **argv)
178{
179 int ifd;
180 uint32_t checksum;
181 uint32_t addr;
182 uint32_t ep;
183 struct stat sbuf;
184 unsigned char *ptr;
185 char *name = "";
186
187 cmdname = *argv;
188
189 addr = ep = 0;
190
191 while (--argc > 0 && **++argv == '-') {
192 while (*++*argv) {
193 switch (**argv) {
194 case 'l':
195 lflag = 1;
196 break;
197 case 'A':
198 if ((--argc <= 0) ||
199 (opt_arch = get_arch(*++argv)) < 0)
200 usage ();
201 goto NXTARG;
202 case 'C':
203 if ((--argc <= 0) ||
204 (opt_comp = get_comp(*++argv)) < 0)
205 usage ();
206 goto NXTARG;
207 case 'O':
208 if ((--argc <= 0) ||
209 (opt_os = get_os(*++argv)) < 0)
210 usage ();
211 goto NXTARG;
212 case 'T':
213 if ((--argc <= 0) ||
214 (opt_type = get_type(*++argv)) < 0)
215 usage ();
216 goto NXTARG;
217
218 case 'a':
219 if (--argc <= 0)
220 usage ();
221 addr = strtoul (*++argv, (char **)&ptr, 16);
222 if (*ptr) {
223 fprintf (stderr,
224 "%s: invalid load address %s\n",
225 cmdname, *argv);
226 exit (EXIT_FAILURE);
227 }
228 goto NXTARG;
229 case 'd':
230 if (--argc <= 0)
231 usage ();
232 datafile = *++argv;
233 dflag = 1;
234 goto NXTARG;
235 case 'e':
236 if (--argc <= 0)
237 usage ();
238 ep = strtoul (*++argv, (char **)&ptr, 16);
239 if (*ptr) {
240 fprintf (stderr,
241 "%s: invalid entry point %s\n",
242 cmdname, *argv);
243 exit (EXIT_FAILURE);
244 }
245 eflag = 1;
246 goto NXTARG;
247 case 'n':
248 if (--argc <= 0)
249 usage ();
250 name = *++argv;
251 goto NXTARG;
252 case 'v':
253 vflag++;
254 break;
255 case 'x':
256 xflag++;
257 break;
258 default:
259 usage ();
260 }
261 }
262NXTARG: ;
263 }
264
265 if ((argc != 1) || ((lflag ^ dflag) == 0))
266 usage();
267
268 if (!eflag) {
269 ep = addr;
270 /* If XIP, entry point must be after the U-Boot header */
271 if (xflag)
272 ep += sizeof(image_header_t);
273 }
274
275 /*
276 * If XIP, ensure the entry point is equal to the load address plus
277 * the size of the U-Boot header.
278 */
279 if (xflag) {
280 if (ep != addr + sizeof(image_header_t)) {
Wolfgang Denk3577d3a2006-04-28 21:24:32 +0200281 fprintf (stderr,
282 "%s: For XIP, the entry point must be the load addr + %lu\n",
wdenka6c7ad22002-12-03 21:28:10 +0000283 cmdname,
284 (unsigned long)sizeof(image_header_t));
wdenk5b1d7132002-11-03 00:07:02 +0000285 exit (EXIT_FAILURE);
286 }
287 }
288
289 imagefile = *argv;
290
291 if (lflag) {
wdenkef1464c2003-10-08 22:14:02 +0000292 ifd = open(imagefile, O_RDONLY|O_BINARY);
wdenk5b1d7132002-11-03 00:07:02 +0000293 } else {
wdenk5b1d7132002-11-03 00:07:02 +0000294 ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
wdenk5b1d7132002-11-03 00:07:02 +0000295 }
296
297 if (ifd < 0) {
298 fprintf (stderr, "%s: Can't open %s: %s\n",
299 cmdname, imagefile, strerror(errno));
300 exit (EXIT_FAILURE);
301 }
302
303 if (lflag) {
304 int len;
305 char *data;
306 /*
307 * list header information of existing image
308 */
309 if (fstat(ifd, &sbuf) < 0) {
310 fprintf (stderr, "%s: Can't stat %s: %s\n",
311 cmdname, imagefile, strerror(errno));
312 exit (EXIT_FAILURE);
313 }
314
wdenk5bb226e2003-11-17 21:14:37 +0000315 if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
wdenk5b1d7132002-11-03 00:07:02 +0000316 fprintf (stderr,
317 "%s: Bad size: \"%s\" is no valid image\n",
318 cmdname, imagefile);
319 exit (EXIT_FAILURE);
320 }
321
322 ptr = (unsigned char *)mmap(0, sbuf.st_size,
323 PROT_READ, MAP_SHARED, ifd, 0);
324 if ((caddr_t)ptr == (caddr_t)-1) {
325 fprintf (stderr, "%s: Can't read %s: %s\n",
326 cmdname, imagefile, strerror(errno));
327 exit (EXIT_FAILURE);
328 }
329
330 /*
331 * create copy of header so that we can blank out the
332 * checksum field for checking - this can't be done
333 * on the PROT_READ mapped data.
334 */
335 memcpy (hdr, ptr, sizeof(image_header_t));
336
337 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
338 fprintf (stderr,
339 "%s: Bad Magic Number: \"%s\" is no valid image\n",
340 cmdname, imagefile);
341 exit (EXIT_FAILURE);
342 }
343
344 data = (char *)hdr;
345 len = sizeof(image_header_t);
346
347 checksum = ntohl(hdr->ih_hcrc);
348 hdr->ih_hcrc = htonl(0); /* clear for re-calculation */
349
350 if (crc32 (0, data, len) != checksum) {
351 fprintf (stderr,
Wolfgang Denk3577d3a2006-04-28 21:24:32 +0200352 "%s: ERROR: \"%s\" has bad header checksum!\n",
353 cmdname, imagefile);
354 exit (EXIT_FAILURE);
wdenk5b1d7132002-11-03 00:07:02 +0000355 }
356
357 data = (char *)(ptr + sizeof(image_header_t));
358 len = sbuf.st_size - sizeof(image_header_t) ;
359
360 if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) {
361 fprintf (stderr,
Wolfgang Denk3577d3a2006-04-28 21:24:32 +0200362 "%s: ERROR: \"%s\" has corrupted data!\n",
363 cmdname, imagefile);
364 exit (EXIT_FAILURE);
wdenk5b1d7132002-11-03 00:07:02 +0000365 }
366
367 /* for multi-file images we need the data part, too */
368 print_header ((image_header_t *)ptr);
369
370 (void) munmap((void *)ptr, sbuf.st_size);
371 (void) close (ifd);
372
373 exit (EXIT_SUCCESS);
374 }
375
376 /*
377 * Must be -w then:
378 *
379 * write dummy header, to be fixed later
380 */
381 memset (hdr, 0, sizeof(image_header_t));
382
383 if (write(ifd, hdr, sizeof(image_header_t)) != sizeof(image_header_t)) {
384 fprintf (stderr, "%s: Write error on %s: %s\n",
385 cmdname, imagefile, strerror(errno));
386 exit (EXIT_FAILURE);
387 }
388
389 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
390 char *file = datafile;
Wolfgang Denk3bb66802006-01-11 13:03:54 +0100391 uint32_t size;
wdenk5b1d7132002-11-03 00:07:02 +0000392
393 for (;;) {
394 char *sep = NULL;
395
396 if (file) {
397 if ((sep = strchr(file, ':')) != NULL) {
398 *sep = '\0';
399 }
400
401 if (stat (file, &sbuf) < 0) {
402 fprintf (stderr, "%s: Can't stat %s: %s\n",
403 cmdname, file, strerror(errno));
404 exit (EXIT_FAILURE);
405 }
406 size = htonl(sbuf.st_size);
407 } else {
408 size = 0;
409 }
410
411 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
412 fprintf (stderr, "%s: Write error on %s: %s\n",
413 cmdname, imagefile, strerror(errno));
414 exit (EXIT_FAILURE);
415 }
416
417 if (!file) {
418 break;
419 }
420
421 if (sep) {
422 *sep = ':';
423 file = sep + 1;
424 } else {
425 file = NULL;
426 }
427 }
428
429 file = datafile;
430
431 for (;;) {
432 char *sep = strchr(file, ':');
433 if (sep) {
434 *sep = '\0';
435 copy_file (ifd, file, 1);
436 *sep++ = ':';
437 file = sep;
438 } else {
439 copy_file (ifd, file, 0);
440 break;
441 }
442 }
443 } else {
444 copy_file (ifd, datafile, 0);
445 }
446
447 /* We're a bit of paranoid */
wdenk147031a2003-10-07 10:33:38 +0000448#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
wdenk5b1d7132002-11-03 00:07:02 +0000449 (void) fdatasync (ifd);
450#else
451 (void) fsync (ifd);
452#endif
453
454 if (fstat(ifd, &sbuf) < 0) {
455 fprintf (stderr, "%s: Can't stat %s: %s\n",
456 cmdname, imagefile, strerror(errno));
457 exit (EXIT_FAILURE);
458 }
459
460 ptr = (unsigned char *)mmap(0, sbuf.st_size,
461 PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
462 if (ptr == (unsigned char *)MAP_FAILED) {
463 fprintf (stderr, "%s: Can't map %s: %s\n",
464 cmdname, imagefile, strerror(errno));
465 exit (EXIT_FAILURE);
466 }
467
468 hdr = (image_header_t *)ptr;
469
470 checksum = crc32 (0,
471 (const char *)(ptr + sizeof(image_header_t)),
472 sbuf.st_size - sizeof(image_header_t)
473 );
474
475 /* Build new header */
476 hdr->ih_magic = htonl(IH_MAGIC);
477 hdr->ih_time = htonl(sbuf.st_mtime);
478 hdr->ih_size = htonl(sbuf.st_size - sizeof(image_header_t));
479 hdr->ih_load = htonl(addr);
480 hdr->ih_ep = htonl(ep);
481 hdr->ih_dcrc = htonl(checksum);
482 hdr->ih_os = opt_os;
483 hdr->ih_arch = opt_arch;
484 hdr->ih_type = opt_type;
485 hdr->ih_comp = opt_comp;
486
487 strncpy((char *)hdr->ih_name, name, IH_NMLEN);
488
489 checksum = crc32(0,(const char *)hdr,sizeof(image_header_t));
490
491 hdr->ih_hcrc = htonl(checksum);
492
493 print_header (hdr);
494
495 (void) munmap((void *)ptr, sbuf.st_size);
496
497 /* We're a bit of paranoid */
wdenk147031a2003-10-07 10:33:38 +0000498#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
wdenk5b1d7132002-11-03 00:07:02 +0000499 (void) fdatasync (ifd);
500#else
501 (void) fsync (ifd);
502#endif
503
504 if (close(ifd)) {
505 fprintf (stderr, "%s: Write error on %s: %s\n",
506 cmdname, imagefile, strerror(errno));
507 exit (EXIT_FAILURE);
508 }
509
510 exit (EXIT_SUCCESS);
511}
512
513static void
514copy_file (int ifd, const char *datafile, int pad)
515{
516 int dfd;
517 struct stat sbuf;
518 unsigned char *ptr;
519 int tail;
520 int zero = 0;
521 int offset = 0;
522 int size;
523
524 if (vflag) {
525 fprintf (stderr, "Adding Image %s\n", datafile);
526 }
527
wdenkef1464c2003-10-08 22:14:02 +0000528 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
wdenk5b1d7132002-11-03 00:07:02 +0000529 fprintf (stderr, "%s: Can't open %s: %s\n",
530 cmdname, datafile, strerror(errno));
531 exit (EXIT_FAILURE);
532 }
533
534 if (fstat(dfd, &sbuf) < 0) {
535 fprintf (stderr, "%s: Can't stat %s: %s\n",
536 cmdname, datafile, strerror(errno));
537 exit (EXIT_FAILURE);
538 }
539
540 ptr = (unsigned char *)mmap(0, sbuf.st_size,
541 PROT_READ, MAP_SHARED, dfd, 0);
542 if (ptr == (unsigned char *)MAP_FAILED) {
543 fprintf (stderr, "%s: Can't read %s: %s\n",
544 cmdname, datafile, strerror(errno));
545 exit (EXIT_FAILURE);
546 }
547
548 if (xflag) {
549 unsigned char *p = NULL;
550 /*
551 * XIP: do not append the image_header_t at the
552 * beginning of the file, but consume the space
553 * reserved for it.
554 */
555
wdenk5bb226e2003-11-17 21:14:37 +0000556 if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
wdenk5b1d7132002-11-03 00:07:02 +0000557 fprintf (stderr,
558 "%s: Bad size: \"%s\" is too small for XIP\n",
559 cmdname, datafile);
560 exit (EXIT_FAILURE);
561 }
562
563 for (p=ptr; p < ptr+sizeof(image_header_t); p++) {
564 if ( *p != 0xff ) {
565 fprintf (stderr,
566 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
567 cmdname, datafile);
568 exit (EXIT_FAILURE);
569 }
570 }
571
572 offset = sizeof(image_header_t);
573 }
574
575 size = sbuf.st_size - offset;
576 if (write(ifd, ptr + offset, size) != size) {
577 fprintf (stderr, "%s: Write error on %s: %s\n",
578 cmdname, imagefile, strerror(errno));
579 exit (EXIT_FAILURE);
580 }
581
582 if (pad && ((tail = size % 4) != 0)) {
583
584 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
585 fprintf (stderr, "%s: Write error on %s: %s\n",
586 cmdname, imagefile, strerror(errno));
587 exit (EXIT_FAILURE);
588 }
589 }
590
591 (void) munmap((void *)ptr, sbuf.st_size);
592 (void) close (dfd);
593}
594
595void
596usage ()
597{
598 fprintf (stderr, "Usage: %s -l image\n"
599 " -l ==> list image header information\n"
wdenk9d5028c2004-11-21 00:06:33 +0000600 " %s [-x] -A arch -O os -T type -C comp "
wdenk5b1d7132002-11-03 00:07:02 +0000601 "-a addr -e ep -n name -d data_file[:data_file...] image\n",
602 cmdname, cmdname);
603 fprintf (stderr, " -A ==> set architecture to 'arch'\n"
604 " -O ==> set operating system to 'os'\n"
605 " -T ==> set image type to 'type'\n"
606 " -C ==> set compression type 'comp'\n"
607 " -a ==> set load address to 'addr' (hex)\n"
608 " -e ==> set entry point to 'ep' (hex)\n"
609 " -n ==> set image name to 'name'\n"
610 " -d ==> use image data from 'datafile'\n"
611 " -x ==> set XIP (execute in place)\n"
612 );
613 exit (EXIT_FAILURE);
614}
615
616static void
617print_header (image_header_t *hdr)
618{
619 time_t timestamp;
620 uint32_t size;
621
622 timestamp = (time_t)ntohl(hdr->ih_time);
623 size = ntohl(hdr->ih_size);
624
625 printf ("Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
626 printf ("Created: %s", ctime(&timestamp));
627 printf ("Image Type: "); print_type(hdr);
628 printf ("Data Size: %d Bytes = %.2f kB = %.2f MB\n",
629 size, (double)size / 1.024e3, (double)size / 1.048576e6 );
wdenkbdccc4f2003-08-05 17:43:17 +0000630 printf ("Load Address: 0x%08X\n", ntohl(hdr->ih_load));
631 printf ("Entry Point: 0x%08X\n", ntohl(hdr->ih_ep));
wdenk5b1d7132002-11-03 00:07:02 +0000632
633 if (hdr->ih_type == IH_TYPE_MULTI || hdr->ih_type == IH_TYPE_SCRIPT) {
634 int i, ptrs;
635 uint32_t pos;
636 unsigned long *len_ptr = (unsigned long *) (
637 (unsigned long)hdr + sizeof(image_header_t)
638 );
639
640 /* determine number of images first (to calculate image offsets) */
641 for (i=0; len_ptr[i]; ++i) /* null pointer terminates list */
642 ;
643 ptrs = i; /* null pointer terminates list */
644
645 pos = sizeof(image_header_t) + ptrs * sizeof(long);
646 printf ("Contents:\n");
647 for (i=0; len_ptr[i]; ++i) {
648 size = ntohl(len_ptr[i]);
649
650 printf (" Image %d: %8d Bytes = %4d kB = %d MB\n",
651 i, size, size>>10, size>>20);
652 if (hdr->ih_type == IH_TYPE_SCRIPT && i > 0) {
653 /*
654 * the user may need to know offsets
655 * if planning to do something with
656 * multiple files
657 */
wdenkbdccc4f2003-08-05 17:43:17 +0000658 printf (" Offset = %08X\n", pos);
wdenk5b1d7132002-11-03 00:07:02 +0000659 }
660 /* copy_file() will pad the first files to even word align */
661 size += 3;
662 size &= ~3;
663 pos += size;
664 }
665 }
666}
667
668
669static void
670print_type (image_header_t *hdr)
671{
672 printf ("%s %s %s (%s)\n",
673 put_arch (hdr->ih_arch),
674 put_os (hdr->ih_os ),
675 put_type (hdr->ih_type),
676 put_comp (hdr->ih_comp)
677 );
678}
679
680static char *put_arch (int arch)
681{
682 return (put_table_entry(arch_name, "Unknown Architecture", arch));
683}
684
685static char *put_os (int os)
686{
687 return (put_table_entry(os_name, "Unknown OS", os));
688}
689
690static char *put_type (int type)
691{
692 return (put_table_entry(type_name, "Unknown Image", type));
693}
694
695static char *put_comp (int comp)
696{
697 return (put_table_entry(comp_name, "Unknown Compression", comp));
698}
699
700static char *put_table_entry (table_entry_t *table, char *msg, int type)
701{
702 for (; table->val>=0; ++table) {
703 if (table->val == type)
704 return (table->lname);
705 }
706 return (msg);
707}
708
709static int get_arch(char *name)
710{
711 return (get_table_entry(arch_name, "CPU", name));
712}
713
714
715static int get_comp(char *name)
716{
717 return (get_table_entry(comp_name, "Compression", name));
718}
719
720
721static int get_os (char *name)
722{
723 return (get_table_entry(os_name, "OS", name));
724}
725
726
727static int get_type(char *name)
728{
729 return (get_table_entry(type_name, "Image", name));
730}
731
732static int get_table_entry (table_entry_t *table, char *msg, char *name)
733{
734 table_entry_t *t;
735 int first = 1;
736
737 for (t=table; t->val>=0; ++t) {
738 if (t->sname && strcasecmp(t->sname, name)==0)
739 return (t->val);
740 }
741 fprintf (stderr, "\nInvalid %s Type - valid names are", msg);
742 for (t=table; t->val>=0; ++t) {
743 if (t->sname == NULL)
744 continue;
745 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
746 first = 0;
747 }
748 fprintf (stderr, "\n");
749 return (-1);
750}