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