2 * (C) Copyright 2000-2004
3 * DENX Software Engineering
4 * Wolfgang Denk, wd@denx.de
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.
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.
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,
29 #define MAP_FAILED (-1)
34 extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
36 typedef struct table_entry {
37 int val; /* as defined in image.h */
38 char *sname; /* short (input) name */
39 char *lname; /* long (output) name */
42 table_entry_t arch_name[] = {
43 { IH_ARCH_INVALID, NULL, "Invalid CPU", },
44 { IH_ARCH_ALPHA, "alpha", "Alpha", },
45 { IH_ARCH_ARM, "arm", "ARM", },
46 { IH_ARCH_I386, "x86", "Intel x86", },
47 { IH_ARCH_IA64, "ia64", "IA64", },
48 { IH_ARCH_M68K, "m68k", "MC68000", },
49 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
50 { IH_ARCH_MIPS, "mips", "MIPS", },
51 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
52 { IH_ARCH_NIOS, "nios", "NIOS", },
53 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
54 { IH_ARCH_PPC, "ppc", "PowerPC", },
55 { IH_ARCH_S390, "s390", "IBM S390", },
56 { IH_ARCH_SH, "sh", "SuperH", },
57 { IH_ARCH_SPARC, "sparc", "SPARC", },
58 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
59 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
60 { IH_ARCH_AVR32, "avr32", "AVR32", },
64 table_entry_t os_name[] = {
65 { IH_OS_INVALID, NULL, "Invalid OS", },
66 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
67 { IH_OS_ARTOS, "artos", "ARTOS", },
68 { IH_OS_DELL, "dell", "Dell", },
69 { IH_OS_ESIX, "esix", "Esix", },
70 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
71 { IH_OS_IRIX, "irix", "Irix", },
72 { IH_OS_LINUX, "linux", "Linux", },
73 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
74 { IH_OS_NCR, "ncr", "NCR", },
75 { IH_OS_NETBSD, "netbsd", "NetBSD", },
76 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
77 { IH_OS_PSOS, "psos", "pSOS", },
78 { IH_OS_QNX, "qnx", "QNX", },
79 { IH_OS_RTEMS, "rtems", "RTEMS", },
80 { IH_OS_SCO, "sco", "SCO", },
81 { IH_OS_SOLARIS, "solaris", "Solaris", },
82 { IH_OS_SVR4, "svr4", "SVR4", },
83 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
84 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
88 table_entry_t type_name[] = {
89 { IH_TYPE_INVALID, NULL, "Invalid Image", },
90 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
91 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
92 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
93 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
94 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
95 { IH_TYPE_SCRIPT, "script", "Script", },
96 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
97 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
101 table_entry_t comp_name[] = {
102 { IH_COMP_NONE, "none", "uncompressed", },
103 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
104 { IH_COMP_GZIP, "gzip", "gzip compressed", },
108 static void copy_file (int, const char *, int);
109 static void usage (void);
110 static void print_header (image_header_t *);
111 static void print_type (image_header_t *);
112 static char *put_table_entry (table_entry_t *, char *, int);
113 static char *put_arch (int);
114 static char *put_type (int);
115 static char *put_os (int);
116 static char *put_comp (int);
117 static int get_table_entry (table_entry_t *, char *, char *);
118 static int get_arch(char *);
119 static int get_comp(char *);
120 static int get_os (char *);
121 static int get_type(char *);
132 int opt_os = IH_OS_LINUX;
133 int opt_arch = IH_ARCH_PPC;
134 int opt_type = IH_TYPE_KERNEL;
135 int opt_comp = IH_COMP_GZIP;
137 image_header_t header;
138 image_header_t *hdr = &header;
141 main (int argc, char **argv)
155 while (--argc > 0 && **++argv == '-') {
163 (opt_arch = get_arch(*++argv)) < 0)
168 (opt_comp = get_comp(*++argv)) < 0)
173 (opt_os = get_os(*++argv)) < 0)
178 (opt_type = get_type(*++argv)) < 0)
185 addr = strtoul (*++argv, (char **)&ptr, 16);
188 "%s: invalid load address %s\n",
202 ep = strtoul (*++argv, (char **)&ptr, 16);
205 "%s: invalid entry point %s\n",
229 if ((argc != 1) || ((lflag ^ dflag) == 0))
234 /* If XIP, entry point must be after the U-Boot header */
236 ep += image_get_header_size ();
240 * If XIP, ensure the entry point is equal to the load address plus
241 * the size of the U-Boot header.
244 if (ep != addr + image_get_header_size ()) {
246 "%s: For XIP, the entry point must be the load addr + %lu\n",
248 (unsigned long)image_get_header_size ());
256 ifd = open(imagefile, O_RDONLY|O_BINARY);
258 ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
262 fprintf (stderr, "%s: Can't open %s: %s\n",
263 cmdname, imagefile, strerror(errno));
271 * list header information of existing image
273 if (fstat(ifd, &sbuf) < 0) {
274 fprintf (stderr, "%s: Can't stat %s: %s\n",
275 cmdname, imagefile, strerror(errno));
279 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
281 "%s: Bad size: \"%s\" is no valid image\n",
286 ptr = (unsigned char *)mmap(0, sbuf.st_size,
287 PROT_READ, MAP_SHARED, ifd, 0);
288 if ((caddr_t)ptr == (caddr_t)-1) {
289 fprintf (stderr, "%s: Can't read %s: %s\n",
290 cmdname, imagefile, strerror(errno));
295 * image_check_hcrc() creates copy of header so that
296 * we can blank out the checksum field for checking -
297 * this can't be done on the PROT_READ mapped data.
299 hdr = (image_header_t *)ptr;
301 if (!image_check_magic (hdr)) {
303 "%s: Bad Magic Number: \"%s\" is no valid image\n",
308 if (!image_check_hcrc (hdr)) {
310 "%s: ERROR: \"%s\" has bad header checksum!\n",
315 data = (char *)image_get_data (hdr);
316 len = sbuf.st_size - image_get_header_size ();
318 if (crc32(0, data, len) != image_get_dcrc (hdr)) {
320 "%s: ERROR: \"%s\" has corrupted data!\n",
325 /* for multi-file images we need the data part, too */
326 print_header ((image_header_t *)ptr);
328 (void) munmap((void *)ptr, sbuf.st_size);
337 * write dummy header, to be fixed later
339 memset (hdr, 0, image_get_header_size ());
341 if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) {
342 fprintf (stderr, "%s: Write error on %s: %s\n",
343 cmdname, imagefile, strerror(errno));
347 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
348 char *file = datafile;
355 if ((sep = strchr(file, ':')) != NULL) {
359 if (stat (file, &sbuf) < 0) {
360 fprintf (stderr, "%s: Can't stat %s: %s\n",
361 cmdname, file, strerror(errno));
364 size = cpu_to_uimage (sbuf.st_size);
369 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
370 fprintf (stderr, "%s: Write error on %s: %s\n",
371 cmdname, imagefile, strerror(errno));
390 char *sep = strchr(file, ':');
393 copy_file (ifd, file, 1);
397 copy_file (ifd, file, 0);
402 copy_file (ifd, datafile, 0);
405 /* We're a bit of paranoid */
406 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
407 (void) fdatasync (ifd);
412 if (fstat(ifd, &sbuf) < 0) {
413 fprintf (stderr, "%s: Can't stat %s: %s\n",
414 cmdname, imagefile, strerror(errno));
418 ptr = (unsigned char *)mmap(0, sbuf.st_size,
419 PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
420 if (ptr == (unsigned char *)MAP_FAILED) {
421 fprintf (stderr, "%s: Can't map %s: %s\n",
422 cmdname, imagefile, strerror(errno));
426 hdr = (image_header_t *)ptr;
429 (const char *)(ptr + image_get_header_size ()),
430 sbuf.st_size - image_get_header_size ()
433 /* Build new header */
434 image_set_magic (hdr, IH_MAGIC);
435 image_set_time (hdr, sbuf.st_mtime);
436 image_set_size (hdr, sbuf.st_size - image_get_header_size ());
437 image_set_load (hdr, addr);
438 image_set_ep (hdr, ep);
439 image_set_dcrc (hdr, checksum);
440 image_set_os (hdr, opt_os);
441 image_set_arch (hdr, opt_arch);
442 image_set_type (hdr, opt_type);
443 image_set_comp (hdr, opt_comp);
445 image_set_name (hdr, name);
447 checksum = crc32 (0, (const char *)hdr, image_get_header_size ());
449 image_set_hcrc (hdr, checksum);
453 (void) munmap((void *)ptr, sbuf.st_size);
455 /* We're a bit of paranoid */
456 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
457 (void) fdatasync (ifd);
463 fprintf (stderr, "%s: Write error on %s: %s\n",
464 cmdname, imagefile, strerror(errno));
472 copy_file (int ifd, const char *datafile, int pad)
483 fprintf (stderr, "Adding Image %s\n", datafile);
486 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
487 fprintf (stderr, "%s: Can't open %s: %s\n",
488 cmdname, datafile, strerror(errno));
492 if (fstat(dfd, &sbuf) < 0) {
493 fprintf (stderr, "%s: Can't stat %s: %s\n",
494 cmdname, datafile, strerror(errno));
498 ptr = (unsigned char *)mmap(0, sbuf.st_size,
499 PROT_READ, MAP_SHARED, dfd, 0);
500 if (ptr == (unsigned char *)MAP_FAILED) {
501 fprintf (stderr, "%s: Can't read %s: %s\n",
502 cmdname, datafile, strerror(errno));
507 unsigned char *p = NULL;
509 * XIP: do not append the image_header_t at the
510 * beginning of the file, but consume the space
514 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
516 "%s: Bad size: \"%s\" is too small for XIP\n",
521 for (p = ptr; p < ptr + image_get_header_size (); p++) {
524 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
530 offset = image_get_header_size ();
533 size = sbuf.st_size - offset;
534 if (write(ifd, ptr + offset, size) != size) {
535 fprintf (stderr, "%s: Write error on %s: %s\n",
536 cmdname, imagefile, strerror(errno));
540 if (pad && ((tail = size % 4) != 0)) {
542 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
543 fprintf (stderr, "%s: Write error on %s: %s\n",
544 cmdname, imagefile, strerror(errno));
549 (void) munmap((void *)ptr, sbuf.st_size);
556 fprintf (stderr, "Usage: %s -l image\n"
557 " -l ==> list image header information\n"
558 " %s [-x] -A arch -O os -T type -C comp "
559 "-a addr -e ep -n name -d data_file[:data_file...] image\n",
561 fprintf (stderr, " -A ==> set architecture to 'arch'\n"
562 " -O ==> set operating system to 'os'\n"
563 " -T ==> set image type to 'type'\n"
564 " -C ==> set compression type 'comp'\n"
565 " -a ==> set load address to 'addr' (hex)\n"
566 " -e ==> set entry point to 'ep' (hex)\n"
567 " -n ==> set image name to 'name'\n"
568 " -d ==> use image data from 'datafile'\n"
569 " -x ==> set XIP (execute in place)\n"
575 print_header (image_header_t *hdr)
580 timestamp = (time_t)image_get_time (hdr);
581 size = image_get_data_size (hdr);
583 printf ("Image Name: %.*s\n", IH_NMLEN, image_get_name (hdr));
584 printf ("Created: %s", ctime(×tamp));
585 printf ("Image Type: "); print_type(hdr);
586 printf ("Data Size: %d Bytes = %.2f kB = %.2f MB\n",
587 size, (double)size / 1.024e3, (double)size / 1.048576e6 );
588 printf ("Load Address: 0x%08X\n", image_get_load (hdr));
589 printf ("Entry Point: 0x%08X\n", image_get_ep (hdr));
591 if (image_check_type (hdr, IH_TYPE_MULTI) ||
592 image_check_type (hdr, IH_TYPE_SCRIPT)) {
595 uint32_t *len_ptr = (uint32_t *) (
596 (unsigned long)hdr + image_get_header_size ()
599 /* determine number of images first (to calculate image offsets) */
600 for (i=0; len_ptr[i]; ++i) /* null pointer terminates list */
602 ptrs = i; /* null pointer terminates list */
604 pos = image_get_header_size () + ptrs * sizeof(long);
605 printf ("Contents:\n");
606 for (i=0; len_ptr[i]; ++i) {
607 size = uimage_to_cpu (len_ptr[i]);
609 printf (" Image %d: %8d Bytes = %4d kB = %d MB\n",
610 i, size, size>>10, size>>20);
611 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
613 * the user may need to know offsets
614 * if planning to do something with
617 printf (" Offset = %08X\n", pos);
619 /* copy_file() will pad the first files to even word align */
629 print_type (image_header_t *hdr)
631 printf ("%s %s %s (%s)\n",
632 put_arch (image_get_arch (hdr)),
633 put_os (image_get_os (hdr)),
634 put_type (image_get_type (hdr)),
635 put_comp (image_get_comp (hdr))
639 static char *put_arch (int arch)
641 return (put_table_entry(arch_name, "Unknown Architecture", arch));
644 static char *put_os (int os)
646 return (put_table_entry(os_name, "Unknown OS", os));
649 static char *put_type (int type)
651 return (put_table_entry(type_name, "Unknown Image", type));
654 static char *put_comp (int comp)
656 return (put_table_entry(comp_name, "Unknown Compression", comp));
659 static char *put_table_entry (table_entry_t *table, char *msg, int type)
661 for (; table->val>=0; ++table) {
662 if (table->val == type)
663 return (table->lname);
668 static int get_arch(char *name)
670 return (get_table_entry(arch_name, "CPU", name));
674 static int get_comp(char *name)
676 return (get_table_entry(comp_name, "Compression", name));
680 static int get_os (char *name)
682 return (get_table_entry(os_name, "OS", name));
686 static int get_type(char *name)
688 return (get_table_entry(type_name, "Image", name));
691 static int get_table_entry (table_entry_t *table, char *msg, char *name)
696 for (t=table; t->val>=0; ++t) {
697 if (t->sname && strcasecmp(t->sname, name)==0)
700 fprintf (stderr, "\nInvalid %s Type - valid names are", msg);
701 for (t=table; t->val>=0; ++t) {
702 if (t->sname == NULL)
704 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
707 fprintf (stderr, "\n");