eukrea_cpuimx27: update defconfig
[barebox-mini2440.git] / scripts / mkimage.c
blob240c5cf5d54099696b7a8829ea629a1dcde59150
1 /*
2 * (C) Copyright 2000-2004
3 * DENX Software Engineering
4 * Wolfgang Denk, wd@denx.de
5 * All rights reserved.
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,
20 * MA 02111-1307 USA
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>
36 #if defined(__BEOS__) || defined(__NetBSD__) || defined(__APPLE__)
37 #include <inttypes.h>
38 #endif
40 #ifdef __WIN32__
41 typedef unsigned int __u32;
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;
53 #define ntohl(a) SWAP_LONG(a)
54 #define htonl(a) SWAP_LONG(a)
55 #endif /* __WIN32__ */
57 #ifndef O_BINARY /* should be define'd on __WIN32__ */
58 #define O_BINARY 0
59 #endif
61 #include "../include/image.h"
63 #ifndef MAP_FAILED
64 #define MAP_FAILED (-1)
65 #endif
67 char *cmdname;
69 #include "../include/zlib.h"
70 #include "../lib/crc32.c"
72 //extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
74 typedef struct table_entry {
75 int val; /* as defined in image.h */
76 char *sname; /* short (input) name */
77 char *lname; /* long (output) name */
78 } table_entry_t;
80 table_entry_t arch_name[] = {
81 { IH_CPU_INVALID, NULL, "Invalid CPU", },
82 { IH_CPU_ALPHA, "alpha", "Alpha", },
83 { IH_CPU_ARM, "arm", "ARM", },
84 { IH_CPU_I386, "x86", "Intel x86", },
85 { IH_CPU_IA64, "ia64", "IA64", },
86 { IH_CPU_M68K, "m68k", "MC68000", },
87 { IH_CPU_MICROBLAZE, "microblaze", "MicroBlaze", },
88 { IH_CPU_MIPS, "mips", "MIPS", },
89 { IH_CPU_MIPS64, "mips64", "MIPS 64 Bit", },
90 { IH_CPU_NIOS, "nios", "NIOS", },
91 { IH_CPU_NIOS2, "nios2", "NIOS II", },
92 { IH_CPU_PPC, "ppc", "PowerPC", },
93 { IH_CPU_S390, "s390", "IBM S390", },
94 { IH_CPU_SH, "sh", "SuperH", },
95 { IH_CPU_SPARC, "sparc", "SPARC", },
96 { IH_CPU_SPARC64, "sparc64", "SPARC 64 Bit", },
97 { IH_CPU_BLACKFIN, "blackfin", "Blackfin", },
98 { IH_CPU_AVR32, "avr32", "AVR32", },
99 { -1, "", "", },
102 table_entry_t os_name[] = {
103 { IH_OS_INVALID, NULL, "Invalid OS", },
104 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
105 { IH_OS_ARTOS, "artos", "ARTOS", },
106 { IH_OS_DELL, "dell", "Dell", },
107 { IH_OS_ESIX, "esix", "Esix", },
108 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
109 { IH_OS_IRIX, "irix", "Irix", },
110 { IH_OS_LINUX, "linux", "Linux", },
111 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
112 { IH_OS_NCR, "ncr", "NCR", },
113 { IH_OS_NETBSD, "netbsd", "NetBSD", },
114 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
115 { IH_OS_PSOS, "psos", "pSOS", },
116 { IH_OS_QNX, "qnx", "QNX", },
117 { IH_OS_RTEMS, "rtems", "RTEMS", },
118 { IH_OS_SCO, "sco", "SCO", },
119 { IH_OS_SOLARIS, "solaris", "Solaris", },
120 { IH_OS_SVR4, "svr4", "SVR4", },
121 { IH_OS_BAREBOX, "barebox", "barebox", },
122 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
123 { -1, "", "", },
126 table_entry_t type_name[] = {
127 { IH_TYPE_INVALID, NULL, "Invalid Image", },
128 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
129 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
130 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
131 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
132 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
133 { IH_TYPE_SCRIPT, "script", "Script", },
134 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
135 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
136 { -1, "", "", },
139 table_entry_t comp_name[] = {
140 { IH_COMP_NONE, "none", "uncompressed", },
141 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
142 { IH_COMP_GZIP, "gzip", "gzip compressed", },
143 { -1, "", "", },
146 static void copy_file (int, const char *, int);
147 static void usage (void);
148 static void print_header (image_header_t *);
149 static void print_type (image_header_t *);
150 static char *put_table_entry (table_entry_t *, char *, int);
151 static char *put_arch (int);
152 static char *put_type (int);
153 static char *put_os (int);
154 static char *put_comp (int);
155 static int get_table_entry (table_entry_t *, char *, char *);
156 static int get_arch(char *);
157 static int get_comp(char *);
158 static int get_os (char *);
159 static int get_type(char *);
162 char *datafile;
163 char *imagefile;
165 int dflag = 0;
166 int eflag = 0;
167 int lflag = 0;
168 int vflag = 0;
169 int xflag = 0;
170 int opt_os = IH_OS_LINUX;
171 int opt_arch = IH_CPU_PPC;
172 int opt_type = IH_TYPE_KERNEL;
173 int opt_comp = IH_COMP_GZIP;
175 image_header_t header;
176 image_header_t *hdr = &header;
179 main (int argc, char **argv)
181 int ifd;
182 uint32_t checksum;
183 uint32_t addr;
184 uint32_t ep;
185 struct stat sbuf;
186 char *ptr;
187 char *name = "";
189 cmdname = *argv;
191 addr = ep = 0;
193 while (--argc > 0 && **++argv == '-') {
194 while (*++*argv) {
195 switch (**argv) {
196 case 'l':
197 lflag = 1;
198 break;
199 case 'A':
200 if ((--argc <= 0) ||
201 (opt_arch = get_arch(*++argv)) < 0)
202 usage ();
203 goto NXTARG;
204 case 'C':
205 if ((--argc <= 0) ||
206 (opt_comp = get_comp(*++argv)) < 0)
207 usage ();
208 goto NXTARG;
209 case 'O':
210 if ((--argc <= 0) ||
211 (opt_os = get_os(*++argv)) < 0)
212 usage ();
213 goto NXTARG;
214 case 'T':
215 if ((--argc <= 0) ||
216 (opt_type = get_type(*++argv)) < 0)
217 usage ();
218 goto NXTARG;
220 case 'a':
221 if (--argc <= 0)
222 usage ();
223 addr = strtoul (*++argv, (char **)&ptr, 16);
224 if (*ptr) {
225 fprintf (stderr,
226 "%s: invalid load address %s\n",
227 cmdname, *argv);
228 exit (EXIT_FAILURE);
230 goto NXTARG;
231 case 'd':
232 if (--argc <= 0)
233 usage ();
234 datafile = *++argv;
235 dflag = 1;
236 goto NXTARG;
237 case 'e':
238 if (--argc <= 0)
239 usage ();
240 ep = strtoul (*++argv, (char **)&ptr, 16);
241 if (*ptr) {
242 fprintf (stderr,
243 "%s: invalid entry point %s\n",
244 cmdname, *argv);
245 exit (EXIT_FAILURE);
247 eflag = 1;
248 goto NXTARG;
249 case 'n':
250 if (--argc <= 0)
251 usage ();
252 name = *++argv;
253 goto NXTARG;
254 case 'v':
255 vflag++;
256 break;
257 case 'x':
258 xflag++;
259 break;
260 default:
261 usage ();
264 NXTARG: ;
267 if ((argc != 1) || ((lflag ^ dflag) == 0))
268 usage();
270 if (!eflag) {
271 ep = addr;
272 /* If XIP, entry point must be after the barebox header */
273 if (xflag)
274 ep += sizeof(image_header_t);
278 * If XIP, ensure the entry point is equal to the load address plus
279 * the size of the barebox header.
281 if (xflag) {
282 if (ep != addr + sizeof(image_header_t)) {
283 fprintf (stderr,
284 "%s: For XIP, the entry point must be the load addr + %lu\n",
285 cmdname,
286 (unsigned long)sizeof(image_header_t));
287 exit (EXIT_FAILURE);
291 imagefile = *argv;
293 if (lflag) {
294 ifd = open(imagefile, O_RDONLY|O_BINARY);
295 } else {
296 ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
299 if (ifd < 0) {
300 fprintf (stderr, "%s: Can't open %s: %s\n",
301 cmdname, imagefile, strerror(errno));
302 exit (EXIT_FAILURE);
305 if (lflag) {
306 int len;
307 char *data;
309 * list header information of existing image
311 if (fstat(ifd, &sbuf) < 0) {
312 fprintf (stderr, "%s: Can't stat %s: %s\n",
313 cmdname, imagefile, strerror(errno));
314 exit (EXIT_FAILURE);
317 if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
318 fprintf (stderr,
319 "%s: Bad size: \"%s\" is no valid image\n",
320 cmdname, imagefile);
321 exit (EXIT_FAILURE);
324 ptr = mmap(0, sbuf.st_size,
325 PROT_READ, MAP_SHARED, ifd, 0);
326 if ((caddr_t)ptr == (caddr_t)-1) {
327 fprintf (stderr, "%s: Can't read %s: %s\n",
328 cmdname, imagefile, strerror(errno));
329 exit (EXIT_FAILURE);
333 * create copy of header so that we can blank out the
334 * checksum field for checking - this can't be done
335 * on the PROT_READ mapped data.
337 memcpy (hdr, ptr, sizeof(image_header_t));
339 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
340 fprintf (stderr,
341 "%s: Bad Magic Number: \"%s\" is no valid image\n",
342 cmdname, imagefile);
343 exit (EXIT_FAILURE);
346 data = (char *)hdr;
347 len = sizeof(image_header_t);
349 checksum = ntohl(hdr->ih_hcrc);
350 hdr->ih_hcrc = htonl(0); /* clear for re-calculation */
352 if (crc32 (0, (unsigned char *)data, len) != checksum) {
353 fprintf (stderr,
354 "%s: ERROR: \"%s\" has bad header checksum!\n",
355 cmdname, imagefile);
356 exit (EXIT_FAILURE);
359 data = (char *)(ptr + sizeof(image_header_t));
360 len = sbuf.st_size - sizeof(image_header_t) ;
362 if (crc32 (0, (unsigned char *)data, len) != ntohl(hdr->ih_dcrc)) {
363 fprintf (stderr,
364 "%s: ERROR: \"%s\" has corrupted data!\n",
365 cmdname, imagefile);
366 exit (EXIT_FAILURE);
369 /* for multi-file images we need the data part, too */
370 print_header ((image_header_t *)ptr);
372 (void) munmap((void *)ptr, sbuf.st_size);
373 (void) close (ifd);
375 exit (EXIT_SUCCESS);
379 * Must be -w then:
381 * write dummy header, to be fixed later
383 memset (hdr, 0, sizeof(image_header_t));
385 if (write(ifd, hdr, sizeof(image_header_t)) != sizeof(image_header_t)) {
386 fprintf (stderr, "%s: Write error on %s: %s\n",
387 cmdname, imagefile, strerror(errno));
388 exit (EXIT_FAILURE);
391 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
392 char *file = datafile;
393 uint32_t size;
395 for (;;) {
396 char *sep = NULL;
398 if (file) {
399 if ((sep = strchr(file, ':')) != NULL) {
400 *sep = '\0';
403 if (stat (file, &sbuf) < 0) {
404 fprintf (stderr, "%s: Can't stat %s: %s\n",
405 cmdname, file, strerror(errno));
406 exit (EXIT_FAILURE);
408 size = htonl(sbuf.st_size);
409 } else {
410 size = 0;
413 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
414 fprintf (stderr, "%s: Write error on %s: %s\n",
415 cmdname, imagefile, strerror(errno));
416 exit (EXIT_FAILURE);
419 if (!file) {
420 break;
423 if (sep) {
424 *sep = ':';
425 file = sep + 1;
426 } else {
427 file = NULL;
431 file = datafile;
433 for (;;) {
434 char *sep = strchr(file, ':');
435 if (sep) {
436 *sep = '\0';
437 copy_file (ifd, file, 1);
438 *sep++ = ':';
439 file = sep;
440 } else {
441 copy_file (ifd, file, 0);
442 break;
445 } else {
446 copy_file (ifd, datafile, 0);
449 /* We're a bit of paranoid */
450 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
451 (void) fdatasync (ifd);
452 #else
453 (void) fsync (ifd);
454 #endif
456 if (fstat(ifd, &sbuf) < 0) {
457 fprintf (stderr, "%s: Can't stat %s: %s\n",
458 cmdname, imagefile, strerror(errno));
459 exit (EXIT_FAILURE);
462 ptr = mmap(0, sbuf.st_size,
463 PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
464 if (ptr == MAP_FAILED) {
465 fprintf (stderr, "%s: Can't map %s: %s\n",
466 cmdname, imagefile, strerror(errno));
467 exit (EXIT_FAILURE);
470 hdr = (image_header_t *)ptr;
472 checksum = crc32 (0,
473 (unsigned char *)(ptr + sizeof(image_header_t)),
474 sbuf.st_size - sizeof(image_header_t)
477 /* Build new header */
478 hdr->ih_magic = htonl(IH_MAGIC);
479 hdr->ih_time = htonl(sbuf.st_mtime);
480 hdr->ih_size = htonl(sbuf.st_size - sizeof(image_header_t));
481 hdr->ih_load = htonl(addr);
482 hdr->ih_ep = htonl(ep);
483 hdr->ih_dcrc = htonl(checksum);
484 hdr->ih_os = opt_os;
485 hdr->ih_arch = opt_arch;
486 hdr->ih_type = opt_type;
487 hdr->ih_comp = opt_comp;
489 strncpy((char *)hdr->ih_name, name, IH_NMLEN);
491 checksum = crc32(0,(unsigned char *)hdr,sizeof(image_header_t));
493 hdr->ih_hcrc = htonl(checksum);
495 print_header (hdr);
497 (void) munmap((void *)ptr, sbuf.st_size);
499 /* We're a bit of paranoid */
500 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__)
501 (void) fdatasync (ifd);
502 #else
503 (void) fsync (ifd);
504 #endif
506 if (close(ifd)) {
507 fprintf (stderr, "%s: Write error on %s: %s\n",
508 cmdname, imagefile, strerror(errno));
509 exit (EXIT_FAILURE);
512 exit (EXIT_SUCCESS);
515 static void
516 copy_file (int ifd, const char *datafile, int pad)
518 int dfd;
519 struct stat sbuf;
520 unsigned char *ptr;
521 int tail;
522 int zero = 0;
523 int offset = 0;
524 int size;
526 if (vflag) {
527 fprintf (stderr, "Adding Image %s\n", datafile);
530 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
531 fprintf (stderr, "%s: Can't open %s: %s\n",
532 cmdname, datafile, strerror(errno));
533 exit (EXIT_FAILURE);
536 if (fstat(dfd, &sbuf) < 0) {
537 fprintf (stderr, "%s: Can't stat %s: %s\n",
538 cmdname, datafile, strerror(errno));
539 exit (EXIT_FAILURE);
542 ptr = (unsigned char *)mmap(0, sbuf.st_size,
543 PROT_READ, MAP_SHARED, dfd, 0);
544 if (ptr == (unsigned char *)MAP_FAILED) {
545 fprintf (stderr, "%s: Can't read %s: %s\n",
546 cmdname, datafile, strerror(errno));
547 exit (EXIT_FAILURE);
550 if (xflag) {
551 unsigned char *p = NULL;
553 * XIP: do not append the image_header_t at the
554 * beginning of the file, but consume the space
555 * reserved for it.
558 if ((unsigned)sbuf.st_size < sizeof(image_header_t)) {
559 fprintf (stderr,
560 "%s: Bad size: \"%s\" is too small for XIP\n",
561 cmdname, datafile);
562 exit (EXIT_FAILURE);
565 for (p=ptr; p < ptr+sizeof(image_header_t); p++) {
566 if ( *p != 0xff ) {
567 fprintf (stderr,
568 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
569 cmdname, datafile);
570 exit (EXIT_FAILURE);
574 offset = sizeof(image_header_t);
577 size = sbuf.st_size - offset;
578 if (write(ifd, ptr + offset, size) != size) {
579 fprintf (stderr, "%s: Write error on %s: %s\n",
580 cmdname, imagefile, strerror(errno));
581 exit (EXIT_FAILURE);
584 if (pad && ((tail = size % 4) != 0)) {
586 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
587 fprintf (stderr, "%s: Write error on %s: %s\n",
588 cmdname, imagefile, strerror(errno));
589 exit (EXIT_FAILURE);
593 (void) munmap((void *)ptr, sbuf.st_size);
594 (void) close (dfd);
597 void
598 usage ()
600 fprintf (stderr, "Usage: %s -l image\n"
601 " -l ==> list image header information\n"
602 " %s [-x] -A arch -O os -T type -C comp "
603 "-a addr -e ep -n name -d data_file[:data_file...] image\n",
604 cmdname, cmdname);
605 fprintf (stderr, " -A ==> set architecture to 'arch'\n"
606 " -O ==> set operating system to 'os'\n"
607 " -T ==> set image type to 'type'\n"
608 " -C ==> set compression type 'comp'\n"
609 " -a ==> set load address to 'addr' (hex)\n"
610 " -e ==> set entry point to 'ep' (hex)\n"
611 " -n ==> set image name to 'name'\n"
612 " -d ==> use image data from 'datafile'\n"
613 " -x ==> set XIP (execute in place)\n"
615 exit (EXIT_FAILURE);
618 static void
619 print_header (image_header_t *hdr)
621 time_t timestamp;
622 uint32_t size;
624 timestamp = (time_t)ntohl(hdr->ih_time);
625 size = ntohl(hdr->ih_size);
627 printf ("Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
628 printf ("Created: %s", ctime(&timestamp));
629 printf ("Image Type: "); print_type(hdr);
630 printf ("Data Size: %d Bytes = %.2f kB = %.2f MB\n",
631 size, (double)size / 1.024e3, (double)size / 1.048576e6 );
632 printf ("Load Address: 0x%08X\n", ntohl(hdr->ih_load));
633 printf ("Entry Point: 0x%08X\n", ntohl(hdr->ih_ep));
635 if (hdr->ih_type == IH_TYPE_MULTI || hdr->ih_type == IH_TYPE_SCRIPT) {
636 int i, ptrs;
637 uint32_t pos;
638 uint32_t *len_ptr = (uint32_t *) (
639 (unsigned long)hdr + sizeof(image_header_t)
642 /* determine number of images first (to calculate image offsets) */
643 for (i=0; len_ptr[i]; ++i) /* null pointer terminates list */
645 ptrs = i; /* null pointer terminates list */
647 pos = sizeof(image_header_t) + ptrs * sizeof(long);
648 printf ("Contents:\n");
649 for (i=0; len_ptr[i]; ++i) {
650 size = ntohl(len_ptr[i]);
652 printf (" Image %d: %8d Bytes = %4d kB = %d MB\n",
653 i, size, size>>10, size>>20);
654 if (hdr->ih_type == IH_TYPE_SCRIPT && i > 0) {
656 * the user may need to know offsets
657 * if planning to do something with
658 * multiple files
660 printf (" Offset = %08X\n", pos);
662 /* copy_file() will pad the first files to even word align */
663 size += 3;
664 size &= ~3;
665 pos += size;
671 static void
672 print_type (image_header_t *hdr)
674 printf ("%s %s %s (%s)\n",
675 put_arch (hdr->ih_arch),
676 put_os (hdr->ih_os ),
677 put_type (hdr->ih_type),
678 put_comp (hdr->ih_comp)
682 static char *put_arch (int arch)
684 return (put_table_entry(arch_name, "Unknown Architecture", arch));
687 static char *put_os (int os)
689 return (put_table_entry(os_name, "Unknown OS", os));
692 static char *put_type (int type)
694 return (put_table_entry(type_name, "Unknown Image", type));
697 static char *put_comp (int comp)
699 return (put_table_entry(comp_name, "Unknown Compression", comp));
702 static char *put_table_entry (table_entry_t *table, char *msg, int type)
704 for (; table->val>=0; ++table) {
705 if (table->val == type)
706 return (table->lname);
708 return (msg);
711 static int get_arch(char *name)
713 return (get_table_entry(arch_name, "CPU", name));
717 static int get_comp(char *name)
719 return (get_table_entry(comp_name, "Compression", name));
723 static int get_os (char *name)
725 return (get_table_entry(os_name, "OS", name));
729 static int get_type(char *name)
731 return (get_table_entry(type_name, "Image", name));
734 static int get_table_entry (table_entry_t *table, char *msg, char *name)
736 table_entry_t *t;
737 int first = 1;
739 for (t=table; t->val>=0; ++t) {
740 if (t->sname && strcasecmp(t->sname, name)==0)
741 return (t->val);
743 fprintf (stderr, "\nInvalid %s Type - valid names are", msg);
744 for (t=table; t->val>=0; ++t) {
745 if (t->sname == NULL)
746 continue;
747 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
748 first = 0;
750 fprintf (stderr, "\n");
751 return (-1);