blob: 0f99f3b4ac0cb2adbb7545361699b7913569d12c [file] [log] [blame]
wdenk5b1d7132002-11-03 00:07:02 +00001/*
2 * (C) Copyright 2000-2002
3 * DENX Software Engineering
4 * Wolfgang Denk, wd@denx.de
5 * All rights reserved.
6 */
7
8#include <errno.h>
9#include <fcntl.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#ifndef __WIN32__
14#include <netinet/in.h> /* for host / network byte order conversions */
15#endif
16#include <sys/mman.h>
17#include <sys/stat.h>
18#include <time.h>
19#include <unistd.h>
20
21#if defined(__BEOS__) || defined(__NetBSD__) || defined(__APPLE__)
22#include <inttypes.h>
23#endif
24
25#ifdef __WIN32__
26typedef unsigned int __u32;
27
28#define SWAP_LONG(x) \
29 ((__u32)( \
30 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
31 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
32 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
33 (((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
34typedef unsigned char uint8_t;
35typedef unsigned short uint16_t;
36typedef unsigned int uint32_t;
37
38#define ntohl(a) SWAP_LONG(a)
39#define htonl(a) SWAP_LONG(a)
40#endif /* __WIN32__ */
41
42#include <image.h>
43
44extern int errno;
45
46#ifndef MAP_FAILED
47#define MAP_FAILED (-1)
48#endif
49
50char *cmdname;
51
52extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
53
54typedef struct table_entry {
55 int val; /* as defined in image.h */
56 char *sname; /* short (input) name */
57 char *lname; /* long (output) name */
58} table_entry_t;
59
60table_entry_t arch_name[] = {
61 { IH_CPU_INVALID, NULL, "Invalid CPU", },
62 { IH_CPU_ALPHA, "alpha", "Alpha", },
63 { IH_CPU_ARM, "arm", "ARM", },
64 { IH_CPU_I386, "x86", "Intel x86", },
65 { IH_CPU_IA64, "ia64", "IA64", },
66 { IH_CPU_MIPS, "mips", "MIPS", },
67 { IH_CPU_MIPS64, "mips64", "MIPS 64 Bit", },
68 { IH_CPU_PPC, "ppc", "PowerPC", },
69 { IH_CPU_S390, "s390", "IBM S390", },
70 { IH_CPU_SH, "sh", "SuperH", },
71 { IH_CPU_SPARC, "sparc", "SPARC", },
72 { IH_CPU_SPARC64, "sparc64", "SPARC 64 Bit", },
73 { -1, "", "", },
74};
75
76table_entry_t os_name[] = {
77 { IH_OS_INVALID, NULL, "Invalid OS", },
78 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
79 { IH_OS_NETBSD, "netbsd", "NetBSD", },
80 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
81 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
82 { IH_OS_LINUX, "linux", "Linux", },
83 { IH_OS_SVR4, "svr4", "SVR4", },
84 { IH_OS_ESIX, "esix", "Esix", },
85 { IH_OS_SOLARIS, "solaris", "Solaris", },
86 { IH_OS_IRIX, "irix", "Irix", },
87 { IH_OS_SCO, "sco", "SCO", },
88 { IH_OS_DELL, "dell", "Dell", },
89 { IH_OS_NCR, "ncr", "NCR", },
90 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
91 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
92 { IH_OS_PSOS, "psos", "pSOS", },
93 { IH_OS_QNX, "qnx", "QNX", },
94 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
95 { -1, "", "", },
96};
97
98table_entry_t type_name[] = {
99 { IH_TYPE_INVALID, NULL, "Invalid Image", },
100 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
101 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
102 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
103 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
104 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
105 { IH_TYPE_SCRIPT, "script", "Script", },
106 { -1, "", "", },
107};
108
109table_entry_t comp_name[] = {
110 { IH_COMP_NONE, "none", "uncompressed", },
111 { IH_COMP_GZIP, "gzip", "gzip compressed", },
112 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
113 { -1, "", "", },
114};
115
116static void copy_file (int, const char *, int);
117static void usage (void);
118static void print_header (image_header_t *);
119static void print_type (image_header_t *);
120static char *put_table_entry (table_entry_t *, char *, int);
121static char *put_arch (int);
122static char *put_type (int);
123static char *put_os (int);
124static char *put_comp (int);
125static int get_table_entry (table_entry_t *, char *, char *);
126static int get_arch(char *);
127static int get_comp(char *);
128static int get_os (char *);
129static int get_type(char *);
130
131
132char *datafile;
133char *imagefile;
134
135int dflag = 0;
136int eflag = 0;
137int lflag = 0;
138int vflag = 0;
139int xflag = 0;
140int opt_os = IH_OS_LINUX;
141int opt_arch = IH_CPU_PPC;
142int opt_type = IH_TYPE_KERNEL;
143int opt_comp = IH_COMP_GZIP;
144
145image_header_t header;
146image_header_t *hdr = &header;
147
148int
149main (int argc, char **argv)
150{
151 int ifd;
152 uint32_t checksum;
153 uint32_t addr;
154 uint32_t ep;
155 struct stat sbuf;
156 unsigned char *ptr;
157 char *name = "";
158
159 cmdname = *argv;
160
161 addr = ep = 0;
162
163 while (--argc > 0 && **++argv == '-') {
164 while (*++*argv) {
165 switch (**argv) {
166 case 'l':
167 lflag = 1;
168 break;
169 case 'A':
170 if ((--argc <= 0) ||
171 (opt_arch = get_arch(*++argv)) < 0)
172 usage ();
173 goto NXTARG;
174 case 'C':
175 if ((--argc <= 0) ||
176 (opt_comp = get_comp(*++argv)) < 0)
177 usage ();
178 goto NXTARG;
179 case 'O':
180 if ((--argc <= 0) ||
181 (opt_os = get_os(*++argv)) < 0)
182 usage ();
183 goto NXTARG;
184 case 'T':
185 if ((--argc <= 0) ||
186 (opt_type = get_type(*++argv)) < 0)
187 usage ();
188 goto NXTARG;
189
190 case 'a':
191 if (--argc <= 0)
192 usage ();
193 addr = strtoul (*++argv, (char **)&ptr, 16);
194 if (*ptr) {
195 fprintf (stderr,
196 "%s: invalid load address %s\n",
197 cmdname, *argv);
198 exit (EXIT_FAILURE);
199 }
200 goto NXTARG;
201 case 'd':
202 if (--argc <= 0)
203 usage ();
204 datafile = *++argv;
205 dflag = 1;
206 goto NXTARG;
207 case 'e':
208 if (--argc <= 0)
209 usage ();
210 ep = strtoul (*++argv, (char **)&ptr, 16);
211 if (*ptr) {
212 fprintf (stderr,
213 "%s: invalid entry point %s\n",
214 cmdname, *argv);
215 exit (EXIT_FAILURE);
216 }
217 eflag = 1;
218 goto NXTARG;
219 case 'n':
220 if (--argc <= 0)
221 usage ();
222 name = *++argv;
223 goto NXTARG;
224 case 'v':
225 vflag++;
226 break;
227 case 'x':
228 xflag++;
229 break;
230 default:
231 usage ();
232 }
233 }
234NXTARG: ;
235 }
236
237 if ((argc != 1) || ((lflag ^ dflag) == 0))
238 usage();
239
240 if (!eflag) {
241 ep = addr;
242 /* If XIP, entry point must be after the U-Boot header */
243 if (xflag)
244 ep += sizeof(image_header_t);
245 }
246
247 /*
248 * If XIP, ensure the entry point is equal to the load address plus
249 * the size of the U-Boot header.
250 */
251 if (xflag) {
252 if (ep != addr + sizeof(image_header_t)) {
253 fprintf (stderr, "%s: For XIP, the entry point must be the load addr + %d\n",
254 cmdname, sizeof(image_header_t));
255 exit (EXIT_FAILURE);
256 }
257 }
258
259 imagefile = *argv;
260
261 if (lflag) {
262 ifd = open(imagefile, O_RDONLY);
263 } else {
264#ifdef __WIN32__
265 ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
266#else
267 ifd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC, 0666);
268#endif
269 }
270
271 if (ifd < 0) {
272 fprintf (stderr, "%s: Can't open %s: %s\n",
273 cmdname, imagefile, strerror(errno));
274 exit (EXIT_FAILURE);
275 }
276
277 if (lflag) {
278 int len;
279 char *data;
280 /*
281 * list header information of existing image
282 */
283 if (fstat(ifd, &sbuf) < 0) {
284 fprintf (stderr, "%s: Can't stat %s: %s\n",
285 cmdname, imagefile, strerror(errno));
286 exit (EXIT_FAILURE);
287 }
288
289 if (sbuf.st_size < sizeof(image_header_t)) {
290 fprintf (stderr,
291 "%s: Bad size: \"%s\" is no valid image\n",
292 cmdname, imagefile);
293 exit (EXIT_FAILURE);
294 }
295
296 ptr = (unsigned char *)mmap(0, sbuf.st_size,
297 PROT_READ, MAP_SHARED, ifd, 0);
298 if ((caddr_t)ptr == (caddr_t)-1) {
299 fprintf (stderr, "%s: Can't read %s: %s\n",
300 cmdname, imagefile, strerror(errno));
301 exit (EXIT_FAILURE);
302 }
303
304 /*
305 * create copy of header so that we can blank out the
306 * checksum field for checking - this can't be done
307 * on the PROT_READ mapped data.
308 */
309 memcpy (hdr, ptr, sizeof(image_header_t));
310
311 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
312 fprintf (stderr,
313 "%s: Bad Magic Number: \"%s\" is no valid image\n",
314 cmdname, imagefile);
315 exit (EXIT_FAILURE);
316 }
317
318 data = (char *)hdr;
319 len = sizeof(image_header_t);
320
321 checksum = ntohl(hdr->ih_hcrc);
322 hdr->ih_hcrc = htonl(0); /* clear for re-calculation */
323
324 if (crc32 (0, data, len) != checksum) {
325 fprintf (stderr,
326 "*** Warning: \"%s\" has bad header checksum!\n",
327 imagefile);
328 }
329
330 data = (char *)(ptr + sizeof(image_header_t));
331 len = sbuf.st_size - sizeof(image_header_t) ;
332
333 if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) {
334 fprintf (stderr,
335 "*** Warning: \"%s\" has corrupted data!\n",
336 imagefile);
337 }
338
339 /* for multi-file images we need the data part, too */
340 print_header ((image_header_t *)ptr);
341
342 (void) munmap((void *)ptr, sbuf.st_size);
343 (void) close (ifd);
344
345 exit (EXIT_SUCCESS);
346 }
347
348 /*
349 * Must be -w then:
350 *
351 * write dummy header, to be fixed later
352 */
353 memset (hdr, 0, sizeof(image_header_t));
354
355 if (write(ifd, hdr, sizeof(image_header_t)) != sizeof(image_header_t)) {
356 fprintf (stderr, "%s: Write error on %s: %s\n",
357 cmdname, imagefile, strerror(errno));
358 exit (EXIT_FAILURE);
359 }
360
361 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
362 char *file = datafile;
363 unsigned long size;
364
365 for (;;) {
366 char *sep = NULL;
367
368 if (file) {
369 if ((sep = strchr(file, ':')) != NULL) {
370 *sep = '\0';
371 }
372
373 if (stat (file, &sbuf) < 0) {
374 fprintf (stderr, "%s: Can't stat %s: %s\n",
375 cmdname, file, strerror(errno));
376 exit (EXIT_FAILURE);
377 }
378 size = htonl(sbuf.st_size);
379 } else {
380 size = 0;
381 }
382
383 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
384 fprintf (stderr, "%s: Write error on %s: %s\n",
385 cmdname, imagefile, strerror(errno));
386 exit (EXIT_FAILURE);
387 }
388
389 if (!file) {
390 break;
391 }
392
393 if (sep) {
394 *sep = ':';
395 file = sep + 1;
396 } else {
397 file = NULL;
398 }
399 }
400
401 file = datafile;
402
403 for (;;) {
404 char *sep = strchr(file, ':');
405 if (sep) {
406 *sep = '\0';
407 copy_file (ifd, file, 1);
408 *sep++ = ':';
409 file = sep;
410 } else {
411 copy_file (ifd, file, 0);
412 break;
413 }
414 }
415 } else {
416 copy_file (ifd, datafile, 0);
417 }
418
419 /* We're a bit of paranoid */
420#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__)
421 (void) fdatasync (ifd);
422#else
423 (void) fsync (ifd);
424#endif
425
426 if (fstat(ifd, &sbuf) < 0) {
427 fprintf (stderr, "%s: Can't stat %s: %s\n",
428 cmdname, imagefile, strerror(errno));
429 exit (EXIT_FAILURE);
430 }
431
432 ptr = (unsigned char *)mmap(0, sbuf.st_size,
433 PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
434 if (ptr == (unsigned char *)MAP_FAILED) {
435 fprintf (stderr, "%s: Can't map %s: %s\n",
436 cmdname, imagefile, strerror(errno));
437 exit (EXIT_FAILURE);
438 }
439
440 hdr = (image_header_t *)ptr;
441
442 checksum = crc32 (0,
443 (const char *)(ptr + sizeof(image_header_t)),
444 sbuf.st_size - sizeof(image_header_t)
445 );
446
447 /* Build new header */
448 hdr->ih_magic = htonl(IH_MAGIC);
449 hdr->ih_time = htonl(sbuf.st_mtime);
450 hdr->ih_size = htonl(sbuf.st_size - sizeof(image_header_t));
451 hdr->ih_load = htonl(addr);
452 hdr->ih_ep = htonl(ep);
453 hdr->ih_dcrc = htonl(checksum);
454 hdr->ih_os = opt_os;
455 hdr->ih_arch = opt_arch;
456 hdr->ih_type = opt_type;
457 hdr->ih_comp = opt_comp;
458
459 strncpy((char *)hdr->ih_name, name, IH_NMLEN);
460
461 checksum = crc32(0,(const char *)hdr,sizeof(image_header_t));
462
463 hdr->ih_hcrc = htonl(checksum);
464
465 print_header (hdr);
466
467 (void) munmap((void *)ptr, sbuf.st_size);
468
469 /* We're a bit of paranoid */
470#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__)
471 (void) fdatasync (ifd);
472#else
473 (void) fsync (ifd);
474#endif
475
476 if (close(ifd)) {
477 fprintf (stderr, "%s: Write error on %s: %s\n",
478 cmdname, imagefile, strerror(errno));
479 exit (EXIT_FAILURE);
480 }
481
482 exit (EXIT_SUCCESS);
483}
484
485static void
486copy_file (int ifd, const char *datafile, int pad)
487{
488 int dfd;
489 struct stat sbuf;
490 unsigned char *ptr;
491 int tail;
492 int zero = 0;
493 int offset = 0;
494 int size;
495
496 if (vflag) {
497 fprintf (stderr, "Adding Image %s\n", datafile);
498 }
499
500 if ((dfd = open(datafile, O_RDONLY)) < 0) {
501 fprintf (stderr, "%s: Can't open %s: %s\n",
502 cmdname, datafile, strerror(errno));
503 exit (EXIT_FAILURE);
504 }
505
506 if (fstat(dfd, &sbuf) < 0) {
507 fprintf (stderr, "%s: Can't stat %s: %s\n",
508 cmdname, datafile, strerror(errno));
509 exit (EXIT_FAILURE);
510 }
511
512 ptr = (unsigned char *)mmap(0, sbuf.st_size,
513 PROT_READ, MAP_SHARED, dfd, 0);
514 if (ptr == (unsigned char *)MAP_FAILED) {
515 fprintf (stderr, "%s: Can't read %s: %s\n",
516 cmdname, datafile, strerror(errno));
517 exit (EXIT_FAILURE);
518 }
519
520 if (xflag) {
521 unsigned char *p = NULL;
522 /*
523 * XIP: do not append the image_header_t at the
524 * beginning of the file, but consume the space
525 * reserved for it.
526 */
527
528 if (sbuf.st_size < sizeof(image_header_t)) {
529 fprintf (stderr,
530 "%s: Bad size: \"%s\" is too small for XIP\n",
531 cmdname, datafile);
532 exit (EXIT_FAILURE);
533 }
534
535 for (p=ptr; p < ptr+sizeof(image_header_t); p++) {
536 if ( *p != 0xff ) {
537 fprintf (stderr,
538 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
539 cmdname, datafile);
540 exit (EXIT_FAILURE);
541 }
542 }
543
544 offset = sizeof(image_header_t);
545 }
546
547 size = sbuf.st_size - offset;
548 if (write(ifd, ptr + offset, size) != size) {
549 fprintf (stderr, "%s: Write error on %s: %s\n",
550 cmdname, imagefile, strerror(errno));
551 exit (EXIT_FAILURE);
552 }
553
554 if (pad && ((tail = size % 4) != 0)) {
555
556 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
557 fprintf (stderr, "%s: Write error on %s: %s\n",
558 cmdname, imagefile, strerror(errno));
559 exit (EXIT_FAILURE);
560 }
561 }
562
563 (void) munmap((void *)ptr, sbuf.st_size);
564 (void) close (dfd);
565}
566
567void
568usage ()
569{
570 fprintf (stderr, "Usage: %s -l image\n"
571 " -l ==> list image header information\n"
572 " %s -A arch -O os -T type -C comp "
573 "-a addr -e ep -n name -d data_file[:data_file...] image\n",
574 cmdname, cmdname);
575 fprintf (stderr, " -A ==> set architecture to 'arch'\n"
576 " -O ==> set operating system to 'os'\n"
577 " -T ==> set image type to 'type'\n"
578 " -C ==> set compression type 'comp'\n"
579 " -a ==> set load address to 'addr' (hex)\n"
580 " -e ==> set entry point to 'ep' (hex)\n"
581 " -n ==> set image name to 'name'\n"
582 " -d ==> use image data from 'datafile'\n"
583 " -x ==> set XIP (execute in place)\n"
584 );
585 exit (EXIT_FAILURE);
586}
587
588static void
589print_header (image_header_t *hdr)
590{
591 time_t timestamp;
592 uint32_t size;
593
594 timestamp = (time_t)ntohl(hdr->ih_time);
595 size = ntohl(hdr->ih_size);
596
597 printf ("Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
598 printf ("Created: %s", ctime(&timestamp));
599 printf ("Image Type: "); print_type(hdr);
600 printf ("Data Size: %d Bytes = %.2f kB = %.2f MB\n",
601 size, (double)size / 1.024e3, (double)size / 1.048576e6 );
602 printf ("Load Address: 0x%08x\n", ntohl(hdr->ih_load));
603 printf ("Entry Point: 0x%08x\n", ntohl(hdr->ih_ep));
604
605 if (hdr->ih_type == IH_TYPE_MULTI || hdr->ih_type == IH_TYPE_SCRIPT) {
606 int i, ptrs;
607 uint32_t pos;
608 unsigned long *len_ptr = (unsigned long *) (
609 (unsigned long)hdr + sizeof(image_header_t)
610 );
611
612 /* determine number of images first (to calculate image offsets) */
613 for (i=0; len_ptr[i]; ++i) /* null pointer terminates list */
614 ;
615 ptrs = i; /* null pointer terminates list */
616
617 pos = sizeof(image_header_t) + ptrs * sizeof(long);
618 printf ("Contents:\n");
619 for (i=0; len_ptr[i]; ++i) {
620 size = ntohl(len_ptr[i]);
621
622 printf (" Image %d: %8d Bytes = %4d kB = %d MB\n",
623 i, size, size>>10, size>>20);
624 if (hdr->ih_type == IH_TYPE_SCRIPT && i > 0) {
625 /*
626 * the user may need to know offsets
627 * if planning to do something with
628 * multiple files
629 */
630 printf (" Offset = %08x\n", pos);
631 }
632 /* copy_file() will pad the first files to even word align */
633 size += 3;
634 size &= ~3;
635 pos += size;
636 }
637 }
638}
639
640
641static void
642print_type (image_header_t *hdr)
643{
644 printf ("%s %s %s (%s)\n",
645 put_arch (hdr->ih_arch),
646 put_os (hdr->ih_os ),
647 put_type (hdr->ih_type),
648 put_comp (hdr->ih_comp)
649 );
650}
651
652static char *put_arch (int arch)
653{
654 return (put_table_entry(arch_name, "Unknown Architecture", arch));
655}
656
657static char *put_os (int os)
658{
659 return (put_table_entry(os_name, "Unknown OS", os));
660}
661
662static char *put_type (int type)
663{
664 return (put_table_entry(type_name, "Unknown Image", type));
665}
666
667static char *put_comp (int comp)
668{
669 return (put_table_entry(comp_name, "Unknown Compression", comp));
670}
671
672static char *put_table_entry (table_entry_t *table, char *msg, int type)
673{
674 for (; table->val>=0; ++table) {
675 if (table->val == type)
676 return (table->lname);
677 }
678 return (msg);
679}
680
681static int get_arch(char *name)
682{
683 return (get_table_entry(arch_name, "CPU", name));
684}
685
686
687static int get_comp(char *name)
688{
689 return (get_table_entry(comp_name, "Compression", name));
690}
691
692
693static int get_os (char *name)
694{
695 return (get_table_entry(os_name, "OS", name));
696}
697
698
699static int get_type(char *name)
700{
701 return (get_table_entry(type_name, "Image", name));
702}
703
704static int get_table_entry (table_entry_t *table, char *msg, char *name)
705{
706 table_entry_t *t;
707 int first = 1;
708
709 for (t=table; t->val>=0; ++t) {
710 if (t->sname && strcasecmp(t->sname, name)==0)
711 return (t->val);
712 }
713 fprintf (stderr, "\nInvalid %s Type - valid names are", msg);
714 for (t=table; t->val>=0; ++t) {
715 if (t->sname == NULL)
716 continue;
717 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
718 first = 0;
719 }
720 fprintf (stderr, "\n");
721 return (-1);
722}