2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2004
5 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
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,
31 #define MAP_FAILED (void *)(-1)
34 extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
35 static void copy_file (int, const char *, int);
36 static void usage (void);
37 static void image_verify_header (char *, int);
38 static void fit_handle_file (void);
50 int opt_os = IH_OS_LINUX;
51 int opt_arch = IH_ARCH_PPC;
52 int opt_type = IH_TYPE_KERNEL;
53 int opt_comp = IH_COMP_GZIP;
54 char *opt_dtc = MKIMAGE_DEFAULT_DTC_OPTIONS;
56 image_header_t header;
57 image_header_t *hdr = &header;
60 main (int argc, char **argv)
74 while (--argc > 0 && **++argv == '-') {
82 (opt_arch = genimg_get_arch_id (*++argv)) < 0)
87 (opt_comp = genimg_get_comp_id (*++argv)) < 0)
98 (opt_os = genimg_get_os_id (*++argv)) < 0)
103 (opt_type = genimg_get_type_id (*++argv)) < 0)
110 addr = strtoul (*++argv, (char **)&ptr, 16);
113 "%s: invalid load address %s\n",
127 ep = strtoul (*++argv, (char **)&ptr, 16);
130 "%s: invalid entry point %s\n",
161 (dflag && (fflag || lflag)) ||
162 (fflag && (dflag || lflag)) ||
163 (lflag && (dflag || fflag)))
168 /* If XIP, entry point must be after the U-Boot header */
170 ep += image_get_header_size ();
174 * If XIP, ensure the entry point is equal to the load address plus
175 * the size of the U-Boot header.
178 if (ep != addr + image_get_header_size ()) {
180 "%s: For XIP, the entry point must be the load addr + %lu\n",
182 (unsigned long)image_get_header_size ());
191 ifd = open (imagefile, O_RDONLY|O_BINARY);
193 ifd = open (imagefile,
194 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
198 fprintf (stderr, "%s: Can't open %s: %s\n",
199 cmdname, imagefile, strerror(errno));
206 * list header information of existing image
208 if (fstat(ifd, &sbuf) < 0) {
209 fprintf (stderr, "%s: Can't stat %s: %s\n",
210 cmdname, imagefile, strerror(errno));
214 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
216 "%s: Bad size: \"%s\" is no valid image\n",
221 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
222 if (ptr == MAP_FAILED) {
223 fprintf (stderr, "%s: Can't read %s: %s\n",
224 cmdname, imagefile, strerror(errno));
228 if (fdt_check_header (ptr)) {
229 /* old-style image */
230 image_verify_header ((char *)ptr, sbuf.st_size);
231 image_print_contents ((image_header_t *)ptr);
234 fit_print_contents (ptr);
237 (void) munmap((void *)ptr, sbuf.st_size);
242 /* Flattened Image Tree (FIT) format handling */
243 debug ("FIT format handling\n");
251 * write dummy header, to be fixed later
253 memset (hdr, 0, image_get_header_size ());
255 if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) {
256 fprintf (stderr, "%s: Write error on %s: %s\n",
257 cmdname, imagefile, strerror(errno));
261 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
262 char *file = datafile;
269 if ((sep = strchr(file, ':')) != NULL) {
273 if (stat (file, &sbuf) < 0) {
274 fprintf (stderr, "%s: Can't stat %s: %s\n",
275 cmdname, file, strerror(errno));
278 size = cpu_to_uimage (sbuf.st_size);
283 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
284 fprintf (stderr, "%s: Write error on %s: %s\n",
285 cmdname, imagefile, strerror(errno));
304 char *sep = strchr(file, ':');
307 copy_file (ifd, file, 1);
311 copy_file (ifd, file, 0);
316 copy_file (ifd, datafile, 0);
319 /* We're a bit of paranoid */
320 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
321 (void) fdatasync (ifd);
326 if (fstat(ifd, &sbuf) < 0) {
327 fprintf (stderr, "%s: Can't stat %s: %s\n",
328 cmdname, imagefile, strerror(errno));
332 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
333 if (ptr == MAP_FAILED) {
334 fprintf (stderr, "%s: Can't map %s: %s\n",
335 cmdname, imagefile, strerror(errno));
339 hdr = (image_header_t *)ptr;
342 (const char *)(ptr + image_get_header_size ()),
343 sbuf.st_size - image_get_header_size ()
346 /* Build new header */
347 image_set_magic (hdr, IH_MAGIC);
348 image_set_time (hdr, sbuf.st_mtime);
349 image_set_size (hdr, sbuf.st_size - image_get_header_size ());
350 image_set_load (hdr, addr);
351 image_set_ep (hdr, ep);
352 image_set_dcrc (hdr, checksum);
353 image_set_os (hdr, opt_os);
354 image_set_arch (hdr, opt_arch);
355 image_set_type (hdr, opt_type);
356 image_set_comp (hdr, opt_comp);
358 image_set_name (hdr, name);
360 checksum = crc32 (0, (const char *)hdr, image_get_header_size ());
362 image_set_hcrc (hdr, checksum);
364 image_print_contents (hdr);
366 (void) munmap((void *)ptr, sbuf.st_size);
368 /* We're a bit of paranoid */
369 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
370 (void) fdatasync (ifd);
376 fprintf (stderr, "%s: Write error on %s: %s\n",
377 cmdname, imagefile, strerror(errno));
385 copy_file (int ifd, const char *datafile, int pad)
396 fprintf (stderr, "Adding Image %s\n", datafile);
399 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
400 fprintf (stderr, "%s: Can't open %s: %s\n",
401 cmdname, datafile, strerror(errno));
405 if (fstat(dfd, &sbuf) < 0) {
406 fprintf (stderr, "%s: Can't stat %s: %s\n",
407 cmdname, datafile, strerror(errno));
411 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
412 if (ptr == MAP_FAILED) {
413 fprintf (stderr, "%s: Can't read %s: %s\n",
414 cmdname, datafile, strerror(errno));
419 unsigned char *p = NULL;
421 * XIP: do not append the image_header_t at the
422 * beginning of the file, but consume the space
426 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
428 "%s: Bad size: \"%s\" is too small for XIP\n",
433 for (p = ptr; p < ptr + image_get_header_size (); p++) {
436 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
442 offset = image_get_header_size ();
445 size = sbuf.st_size - offset;
446 if (write(ifd, ptr + offset, size) != size) {
447 fprintf (stderr, "%s: Write error on %s: %s\n",
448 cmdname, imagefile, strerror(errno));
452 if (pad && ((tail = size % 4) != 0)) {
454 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
455 fprintf (stderr, "%s: Write error on %s: %s\n",
456 cmdname, imagefile, strerror(errno));
461 (void) munmap((void *)ptr, sbuf.st_size);
468 fprintf (stderr, "Usage: %s -l image\n"
469 " -l ==> list image header information\n",
471 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
472 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
473 " -A ==> set architecture to 'arch'\n"
474 " -O ==> set operating system to 'os'\n"
475 " -T ==> set image type to 'type'\n"
476 " -C ==> set compression type 'comp'\n"
477 " -a ==> set load address to 'addr' (hex)\n"
478 " -e ==> set entry point to 'ep' (hex)\n"
479 " -n ==> set image name to 'name'\n"
480 " -d ==> use image data from 'datafile'\n"
481 " -x ==> set XIP (execute in place)\n",
483 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
490 image_verify_header (char *ptr, int image_size)
495 image_header_t header;
496 image_header_t *hdr = &header;
499 * create copy of header so that we can blank out the
500 * checksum field for checking - this can't be done
501 * on the PROT_READ mapped data.
503 memcpy (hdr, ptr, sizeof(image_header_t));
505 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
507 "%s: Bad Magic Number: \"%s\" is no valid image\n",
513 len = sizeof(image_header_t);
515 checksum = ntohl(hdr->ih_hcrc);
516 hdr->ih_hcrc = htonl(0); /* clear for re-calculation */
518 if (crc32 (0, data, len) != checksum) {
520 "%s: ERROR: \"%s\" has bad header checksum!\n",
525 data = ptr + sizeof(image_header_t);
526 len = image_size - sizeof(image_header_t) ;
528 if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) {
530 "%s: ERROR: \"%s\" has corrupted data!\n",
537 * fit_handle_file - main FIT file processing function
539 * fit_handle_file() runs dtc to convert .its to .itb, includes
540 * binary data, updates timestamp property and calculates hashes.
542 * datafile - .its file
543 * imagefile - .itb file
546 * only on success, otherwise calls exit (EXIT_FAILURE);
548 static void fit_handle_file (void)
550 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
551 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
556 /* call dtc to include binary properties into the tmp file */
557 if (strlen (imagefile) + strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 >
559 fprintf (stderr, "%s: Image file name (%s) too long, "
560 "can't create tmpfile",
564 sprintf (tmpfile, "%s%s", imagefile, MKIMAGE_TMPFILE_SUFFIX);
566 /* dtc -I dts -O -p 200 datafile > tmpfile */
567 sprintf (cmd, "%s %s %s > %s",
568 MKIMAGE_DTC, opt_dtc, datafile, tmpfile);
569 debug ("Trying to execute \"%s\"\n", cmd);
570 if (system (cmd) == -1) {
571 fprintf (stderr, "%s: system(%s) failed: %s\n",
572 cmdname, cmd, strerror(errno));
577 /* load FIT blob into memory */
578 tfd = open (tmpfile, O_RDWR|O_BINARY);
581 fprintf (stderr, "%s: Can't open %s: %s\n",
582 cmdname, tmpfile, strerror(errno));
587 if (fstat (tfd, &sbuf) < 0) {
588 fprintf (stderr, "%s: Can't stat %s: %s\n",
589 cmdname, tmpfile, strerror(errno));
594 ptr = mmap (0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, tfd, 0);
595 if (ptr == MAP_FAILED) {
596 fprintf (stderr, "%s: Can't read %s: %s\n",
597 cmdname, tmpfile, strerror(errno));
602 /* check if ptr has a valid blob */
603 if (fdt_check_header (ptr)) {
604 fprintf (stderr, "%s: Invalid FIT blob\n", cmdname);
609 /* set hashes for images in the blob */
610 if (fit_set_hashes (ptr)) {
611 fprintf (stderr, "%s Can't add hashes to FIT blob", cmdname);
616 /* add a timestamp at offset 0 i.e., root */
617 if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) {
618 fprintf (stderr, "%s: Can't add image timestamp\n", cmdname);
622 debug ("Added timestamp successfully\n");
624 munmap ((void *)ptr, sbuf.st_size);
627 if (rename (tmpfile, imagefile) == -1) {
628 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
629 cmdname, tmpfile, imagefile, strerror (errno));