tools: mkimage : bugfix returns correct value for list command
[u-boot-kw.git] / tools / mkimage.c
blobb7b383a705ae37fc38c52a4cb52f73bc5f1c62f9
1 /*
2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2004
5 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
7 * All rights reserved.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
25 #include "mkimage.h"
26 #include <image.h>
28 extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
29 static void copy_file (int, const char *, int);
30 static void usage (void);
31 static int image_verify_header (char *, int);
32 static void fit_handle_file (void);
34 char *datafile;
35 char *imagefile;
36 char *cmdname;
38 int dflag = 0;
39 int eflag = 0;
40 int fflag = 0;
41 int lflag = 0;
42 int vflag = 0;
43 int xflag = 0;
44 int opt_os = IH_OS_LINUX;
45 int opt_arch = IH_ARCH_PPC;
46 int opt_type = IH_TYPE_KERNEL;
47 int opt_comp = IH_COMP_GZIP;
48 char *opt_dtc = MKIMAGE_DEFAULT_DTC_OPTIONS;
50 image_header_t header;
51 image_header_t *hdr = &header;
53 int
54 main (int argc, char **argv)
56 int ifd = -1;
57 uint32_t checksum;
58 uint32_t addr;
59 uint32_t ep;
60 struct stat sbuf;
61 unsigned char *ptr;
62 char *name = "";
63 int retval;
65 cmdname = *argv;
67 addr = ep = 0;
69 while (--argc > 0 && **++argv == '-') {
70 while (*++*argv) {
71 switch (**argv) {
72 case 'l':
73 lflag = 1;
74 break;
75 case 'A':
76 if ((--argc <= 0) ||
77 (opt_arch = genimg_get_arch_id (*++argv)) < 0)
78 usage ();
79 goto NXTARG;
80 case 'C':
81 if ((--argc <= 0) ||
82 (opt_comp = genimg_get_comp_id (*++argv)) < 0)
83 usage ();
84 goto NXTARG;
85 case 'D':
86 if (--argc <= 0)
87 usage ();
88 opt_dtc = *++argv;
89 goto NXTARG;
91 case 'O':
92 if ((--argc <= 0) ||
93 (opt_os = genimg_get_os_id (*++argv)) < 0)
94 usage ();
95 goto NXTARG;
96 case 'T':
97 if ((--argc <= 0) ||
98 (opt_type = genimg_get_type_id (*++argv)) < 0)
99 usage ();
100 goto NXTARG;
102 case 'a':
103 if (--argc <= 0)
104 usage ();
105 addr = strtoul (*++argv, (char **)&ptr, 16);
106 if (*ptr) {
107 fprintf (stderr,
108 "%s: invalid load address %s\n",
109 cmdname, *argv);
110 exit (EXIT_FAILURE);
112 goto NXTARG;
113 case 'd':
114 if (--argc <= 0)
115 usage ();
116 datafile = *++argv;
117 dflag = 1;
118 goto NXTARG;
119 case 'e':
120 if (--argc <= 0)
121 usage ();
122 ep = strtoul (*++argv, (char **)&ptr, 16);
123 if (*ptr) {
124 fprintf (stderr,
125 "%s: invalid entry point %s\n",
126 cmdname, *argv);
127 exit (EXIT_FAILURE);
129 eflag = 1;
130 goto NXTARG;
131 case 'f':
132 if (--argc <= 0)
133 usage ();
134 datafile = *++argv;
135 fflag = 1;
136 goto NXTARG;
137 case 'n':
138 if (--argc <= 0)
139 usage ();
140 name = *++argv;
141 goto NXTARG;
142 case 'v':
143 vflag++;
144 break;
145 case 'x':
146 xflag++;
147 break;
148 default:
149 usage ();
152 NXTARG: ;
155 if ((argc != 1) ||
156 (dflag && (fflag || lflag)) ||
157 (fflag && (dflag || lflag)) ||
158 (lflag && (dflag || fflag)))
159 usage();
161 if (!eflag) {
162 ep = addr;
163 /* If XIP, entry point must be after the U-Boot header */
164 if (xflag)
165 ep += image_get_header_size ();
169 * If XIP, ensure the entry point is equal to the load address plus
170 * the size of the U-Boot header.
172 if (xflag) {
173 if (ep != addr + image_get_header_size ()) {
174 fprintf (stderr,
175 "%s: For XIP, the entry point must be the load addr + %lu\n",
176 cmdname,
177 (unsigned long)image_get_header_size ());
178 exit (EXIT_FAILURE);
182 imagefile = *argv;
184 if (!fflag){
185 if (lflag) {
186 ifd = open (imagefile, O_RDONLY|O_BINARY);
187 } else {
188 ifd = open (imagefile,
189 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
192 if (ifd < 0) {
193 fprintf (stderr, "%s: Can't open %s: %s\n",
194 cmdname, imagefile, strerror(errno));
195 exit (EXIT_FAILURE);
199 if (lflag) {
201 * list header information of existing image
203 if (fstat(ifd, &sbuf) < 0) {
204 fprintf (stderr, "%s: Can't stat %s: %s\n",
205 cmdname, imagefile, strerror(errno));
206 exit (EXIT_FAILURE);
209 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
210 fprintf (stderr,
211 "%s: Bad size: \"%s\" is no valid image\n",
212 cmdname, imagefile);
213 exit (EXIT_FAILURE);
216 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
217 if (ptr == MAP_FAILED) {
218 fprintf (stderr, "%s: Can't read %s: %s\n",
219 cmdname, imagefile, strerror(errno));
220 exit (EXIT_FAILURE);
223 if (!(retval = fdt_check_header (ptr))) /* FIT image */
224 fit_print_contents (ptr);
225 else if (!(retval = image_verify_header (
226 (char *)ptr, sbuf.st_size))) /* old-style image */
227 image_print_contents ((image_header_t *)ptr);
229 (void) munmap((void *)ptr, sbuf.st_size);
230 (void) close (ifd);
232 exit (retval);
233 } else if (fflag) {
234 /* Flattened Image Tree (FIT) format handling */
235 debug ("FIT format handling\n");
236 fit_handle_file ();
237 exit (retval);
241 * Must be -w then:
243 * write dummy header, to be fixed later
245 memset (hdr, 0, image_get_header_size ());
247 if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) {
248 fprintf (stderr, "%s: Write error on %s: %s\n",
249 cmdname, imagefile, strerror(errno));
250 exit (EXIT_FAILURE);
253 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
254 char *file = datafile;
255 uint32_t size;
257 for (;;) {
258 char *sep = NULL;
260 if (file) {
261 if ((sep = strchr(file, ':')) != NULL) {
262 *sep = '\0';
265 if (stat (file, &sbuf) < 0) {
266 fprintf (stderr, "%s: Can't stat %s: %s\n",
267 cmdname, file, strerror(errno));
268 exit (EXIT_FAILURE);
270 size = cpu_to_uimage (sbuf.st_size);
271 } else {
272 size = 0;
275 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
276 fprintf (stderr, "%s: Write error on %s: %s\n",
277 cmdname, imagefile, strerror(errno));
278 exit (EXIT_FAILURE);
281 if (!file) {
282 break;
285 if (sep) {
286 *sep = ':';
287 file = sep + 1;
288 } else {
289 file = NULL;
293 file = datafile;
295 for (;;) {
296 char *sep = strchr(file, ':');
297 if (sep) {
298 *sep = '\0';
299 copy_file (ifd, file, 1);
300 *sep++ = ':';
301 file = sep;
302 } else {
303 copy_file (ifd, file, 0);
304 break;
307 } else {
308 copy_file (ifd, datafile, 0);
311 /* We're a bit of paranoid */
312 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
313 (void) fdatasync (ifd);
314 #else
315 (void) fsync (ifd);
316 #endif
318 if (fstat(ifd, &sbuf) < 0) {
319 fprintf (stderr, "%s: Can't stat %s: %s\n",
320 cmdname, imagefile, strerror(errno));
321 exit (EXIT_FAILURE);
324 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
325 if (ptr == MAP_FAILED) {
326 fprintf (stderr, "%s: Can't map %s: %s\n",
327 cmdname, imagefile, strerror(errno));
328 exit (EXIT_FAILURE);
331 hdr = (image_header_t *)ptr;
333 checksum = crc32 (0,
334 (const char *)(ptr + image_get_header_size ()),
335 sbuf.st_size - image_get_header_size ()
338 /* Build new header */
339 image_set_magic (hdr, IH_MAGIC);
340 image_set_time (hdr, sbuf.st_mtime);
341 image_set_size (hdr, sbuf.st_size - image_get_header_size ());
342 image_set_load (hdr, addr);
343 image_set_ep (hdr, ep);
344 image_set_dcrc (hdr, checksum);
345 image_set_os (hdr, opt_os);
346 image_set_arch (hdr, opt_arch);
347 image_set_type (hdr, opt_type);
348 image_set_comp (hdr, opt_comp);
350 image_set_name (hdr, name);
352 checksum = crc32 (0, (const char *)hdr, image_get_header_size ());
354 image_set_hcrc (hdr, checksum);
356 image_print_contents (hdr);
358 (void) munmap((void *)ptr, sbuf.st_size);
360 /* We're a bit of paranoid */
361 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
362 (void) fdatasync (ifd);
363 #else
364 (void) fsync (ifd);
365 #endif
367 if (close(ifd)) {
368 fprintf (stderr, "%s: Write error on %s: %s\n",
369 cmdname, imagefile, strerror(errno));
370 exit (EXIT_FAILURE);
373 exit (EXIT_SUCCESS);
376 static void
377 copy_file (int ifd, const char *datafile, int pad)
379 int dfd;
380 struct stat sbuf;
381 unsigned char *ptr;
382 int tail;
383 int zero = 0;
384 int offset = 0;
385 int size;
387 if (vflag) {
388 fprintf (stderr, "Adding Image %s\n", datafile);
391 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
392 fprintf (stderr, "%s: Can't open %s: %s\n",
393 cmdname, datafile, strerror(errno));
394 exit (EXIT_FAILURE);
397 if (fstat(dfd, &sbuf) < 0) {
398 fprintf (stderr, "%s: Can't stat %s: %s\n",
399 cmdname, datafile, strerror(errno));
400 exit (EXIT_FAILURE);
403 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
404 if (ptr == MAP_FAILED) {
405 fprintf (stderr, "%s: Can't read %s: %s\n",
406 cmdname, datafile, strerror(errno));
407 exit (EXIT_FAILURE);
410 if (xflag) {
411 unsigned char *p = NULL;
413 * XIP: do not append the image_header_t at the
414 * beginning of the file, but consume the space
415 * reserved for it.
418 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
419 fprintf (stderr,
420 "%s: Bad size: \"%s\" is too small for XIP\n",
421 cmdname, datafile);
422 exit (EXIT_FAILURE);
425 for (p = ptr; p < ptr + image_get_header_size (); p++) {
426 if ( *p != 0xff ) {
427 fprintf (stderr,
428 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
429 cmdname, datafile);
430 exit (EXIT_FAILURE);
434 offset = image_get_header_size ();
437 size = sbuf.st_size - offset;
438 if (write(ifd, ptr + offset, size) != size) {
439 fprintf (stderr, "%s: Write error on %s: %s\n",
440 cmdname, imagefile, strerror(errno));
441 exit (EXIT_FAILURE);
444 if (pad && ((tail = size % 4) != 0)) {
446 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
447 fprintf (stderr, "%s: Write error on %s: %s\n",
448 cmdname, imagefile, strerror(errno));
449 exit (EXIT_FAILURE);
453 (void) munmap((void *)ptr, sbuf.st_size);
454 (void) close (dfd);
457 void
458 usage ()
460 fprintf (stderr, "Usage: %s -l image\n"
461 " -l ==> list image header information\n",
462 cmdname);
463 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
464 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
465 " -A ==> set architecture to 'arch'\n"
466 " -O ==> set operating system to 'os'\n"
467 " -T ==> set image type to 'type'\n"
468 " -C ==> set compression type 'comp'\n"
469 " -a ==> set load address to 'addr' (hex)\n"
470 " -e ==> set entry point to 'ep' (hex)\n"
471 " -n ==> set image name to 'name'\n"
472 " -d ==> use image data from 'datafile'\n"
473 " -x ==> set XIP (execute in place)\n",
474 cmdname);
475 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
476 cmdname);
478 exit (EXIT_FAILURE);
481 static int
482 image_verify_header (char *ptr, int image_size)
484 int len;
485 char *data;
486 uint32_t checksum;
487 image_header_t header;
488 image_header_t *hdr = &header;
491 * create copy of header so that we can blank out the
492 * checksum field for checking - this can't be done
493 * on the PROT_READ mapped data.
495 memcpy (hdr, ptr, sizeof(image_header_t));
497 if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
498 fprintf (stderr,
499 "%s: Bad Magic Number: \"%s\" is no valid image\n",
500 cmdname, imagefile);
501 return -FDT_ERR_BADMAGIC;
504 data = (char *)hdr;
505 len = sizeof(image_header_t);
507 checksum = be32_to_cpu(hdr->ih_hcrc);
508 hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
510 if (crc32 (0, data, len) != checksum) {
511 fprintf (stderr,
512 "%s: ERROR: \"%s\" has bad header checksum!\n",
513 cmdname, imagefile);
514 return -FDT_ERR_BADSTATE;
517 data = ptr + sizeof(image_header_t);
518 len = image_size - sizeof(image_header_t) ;
520 if (crc32 (0, data, len) != be32_to_cpu(hdr->ih_dcrc)) {
521 fprintf (stderr,
522 "%s: ERROR: \"%s\" has corrupted data!\n",
523 cmdname, imagefile);
524 return -FDT_ERR_BADSTRUCTURE;
526 return 0;
530 * fit_handle_file - main FIT file processing function
532 * fit_handle_file() runs dtc to convert .its to .itb, includes
533 * binary data, updates timestamp property and calculates hashes.
535 * datafile - .its file
536 * imagefile - .itb file
538 * returns:
539 * only on success, otherwise calls exit (EXIT_FAILURE);
541 static void fit_handle_file (void)
543 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
544 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
545 int tfd;
546 struct stat sbuf;
547 unsigned char *ptr;
549 /* call dtc to include binary properties into the tmp file */
550 if (strlen (imagefile) + strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 >
551 sizeof (tmpfile)) {
552 fprintf (stderr, "%s: Image file name (%s) too long, "
553 "can't create tmpfile",
554 imagefile, cmdname);
555 exit (EXIT_FAILURE);
557 sprintf (tmpfile, "%s%s", imagefile, MKIMAGE_TMPFILE_SUFFIX);
559 /* dtc -I dts -O -p 200 datafile > tmpfile */
560 sprintf (cmd, "%s %s %s > %s",
561 MKIMAGE_DTC, opt_dtc, datafile, tmpfile);
562 debug ("Trying to execute \"%s\"\n", cmd);
563 if (system (cmd) == -1) {
564 fprintf (stderr, "%s: system(%s) failed: %s\n",
565 cmdname, cmd, strerror(errno));
566 unlink (tmpfile);
567 exit (EXIT_FAILURE);
570 /* load FIT blob into memory */
571 tfd = open (tmpfile, O_RDWR|O_BINARY);
573 if (tfd < 0) {
574 fprintf (stderr, "%s: Can't open %s: %s\n",
575 cmdname, tmpfile, strerror(errno));
576 unlink (tmpfile);
577 exit (EXIT_FAILURE);
580 if (fstat (tfd, &sbuf) < 0) {
581 fprintf (stderr, "%s: Can't stat %s: %s\n",
582 cmdname, tmpfile, strerror(errno));
583 unlink (tmpfile);
584 exit (EXIT_FAILURE);
587 ptr = mmap (0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, tfd, 0);
588 if (ptr == MAP_FAILED) {
589 fprintf (stderr, "%s: Can't read %s: %s\n",
590 cmdname, tmpfile, strerror(errno));
591 unlink (tmpfile);
592 exit (EXIT_FAILURE);
595 /* check if ptr has a valid blob */
596 if (fdt_check_header (ptr)) {
597 fprintf (stderr, "%s: Invalid FIT blob\n", cmdname);
598 unlink (tmpfile);
599 exit (EXIT_FAILURE);
602 /* set hashes for images in the blob */
603 if (fit_set_hashes (ptr)) {
604 fprintf (stderr, "%s Can't add hashes to FIT blob", cmdname);
605 unlink (tmpfile);
606 exit (EXIT_FAILURE);
609 /* add a timestamp at offset 0 i.e., root */
610 if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) {
611 fprintf (stderr, "%s: Can't add image timestamp\n", cmdname);
612 unlink (tmpfile);
613 exit (EXIT_FAILURE);
615 debug ("Added timestamp successfully\n");
617 munmap ((void *)ptr, sbuf.st_size);
618 close (tfd);
620 if (rename (tmpfile, imagefile) == -1) {
621 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
622 cmdname, tmpfile, imagefile, strerror (errno));
623 unlink (tmpfile);
624 unlink (imagefile);
625 exit (EXIT_FAILURE);