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 (-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 = (unsigned char *)mmap(0, sbuf.st_size,
222 PROT_READ, MAP_SHARED, ifd, 0);
223 if ((caddr_t)ptr == (caddr_t)-1) {
224 fprintf (stderr, "%s: Can't read %s: %s\n",
225 cmdname, imagefile, strerror(errno));
229 if (fdt_check_header (ptr)) {
230 /* old-style image */
231 image_verify_header ((char *)ptr, sbuf.st_size);
232 image_print_contents ((image_header_t *)ptr);
235 fit_print_contents (ptr);
238 (void) munmap((void *)ptr, sbuf.st_size);
243 /* Flattened Image Tree (FIT) format handling */
244 debug ("FIT format handling\n");
252 * write dummy header, to be fixed later
254 memset (hdr, 0, image_get_header_size ());
256 if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) {
257 fprintf (stderr, "%s: Write error on %s: %s\n",
258 cmdname, imagefile, strerror(errno));
262 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
263 char *file = datafile;
270 if ((sep = strchr(file, ':')) != NULL) {
274 if (stat (file, &sbuf) < 0) {
275 fprintf (stderr, "%s: Can't stat %s: %s\n",
276 cmdname, file, strerror(errno));
279 size = cpu_to_uimage (sbuf.st_size);
284 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
285 fprintf (stderr, "%s: Write error on %s: %s\n",
286 cmdname, imagefile, strerror(errno));
305 char *sep = strchr(file, ':');
308 copy_file (ifd, file, 1);
312 copy_file (ifd, file, 0);
317 copy_file (ifd, datafile, 0);
320 /* We're a bit of paranoid */
321 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
322 (void) fdatasync (ifd);
327 if (fstat(ifd, &sbuf) < 0) {
328 fprintf (stderr, "%s: Can't stat %s: %s\n",
329 cmdname, imagefile, strerror(errno));
333 ptr = (unsigned char *)mmap(0, sbuf.st_size,
334 PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
335 if (ptr == (unsigned char *)MAP_FAILED) {
336 fprintf (stderr, "%s: Can't map %s: %s\n",
337 cmdname, imagefile, strerror(errno));
341 hdr = (image_header_t *)ptr;
344 (const char *)(ptr + image_get_header_size ()),
345 sbuf.st_size - image_get_header_size ()
348 /* Build new header */
349 image_set_magic (hdr, IH_MAGIC);
350 image_set_time (hdr, sbuf.st_mtime);
351 image_set_size (hdr, sbuf.st_size - image_get_header_size ());
352 image_set_load (hdr, addr);
353 image_set_ep (hdr, ep);
354 image_set_dcrc (hdr, checksum);
355 image_set_os (hdr, opt_os);
356 image_set_arch (hdr, opt_arch);
357 image_set_type (hdr, opt_type);
358 image_set_comp (hdr, opt_comp);
360 image_set_name (hdr, name);
362 checksum = crc32 (0, (const char *)hdr, image_get_header_size ());
364 image_set_hcrc (hdr, checksum);
366 image_print_contents (hdr);
368 (void) munmap((void *)ptr, sbuf.st_size);
370 /* We're a bit of paranoid */
371 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
372 (void) fdatasync (ifd);
378 fprintf (stderr, "%s: Write error on %s: %s\n",
379 cmdname, imagefile, strerror(errno));
387 copy_file (int ifd, const char *datafile, int pad)
398 fprintf (stderr, "Adding Image %s\n", datafile);
401 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
402 fprintf (stderr, "%s: Can't open %s: %s\n",
403 cmdname, datafile, strerror(errno));
407 if (fstat(dfd, &sbuf) < 0) {
408 fprintf (stderr, "%s: Can't stat %s: %s\n",
409 cmdname, datafile, strerror(errno));
413 ptr = (unsigned char *)mmap(0, sbuf.st_size,
414 PROT_READ, MAP_SHARED, dfd, 0);
415 if (ptr == (unsigned char *)MAP_FAILED) {
416 fprintf (stderr, "%s: Can't read %s: %s\n",
417 cmdname, datafile, strerror(errno));
422 unsigned char *p = NULL;
424 * XIP: do not append the image_header_t at the
425 * beginning of the file, but consume the space
429 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
431 "%s: Bad size: \"%s\" is too small for XIP\n",
436 for (p = ptr; p < ptr + image_get_header_size (); p++) {
439 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
445 offset = image_get_header_size ();
448 size = sbuf.st_size - offset;
449 if (write(ifd, ptr + offset, size) != size) {
450 fprintf (stderr, "%s: Write error on %s: %s\n",
451 cmdname, imagefile, strerror(errno));
455 if (pad && ((tail = size % 4) != 0)) {
457 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
458 fprintf (stderr, "%s: Write error on %s: %s\n",
459 cmdname, imagefile, strerror(errno));
464 (void) munmap((void *)ptr, sbuf.st_size);
471 fprintf (stderr, "Usage: %s -l image\n"
472 " -l ==> list image header information\n",
474 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
475 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
476 " -A ==> set architecture to 'arch'\n"
477 " -O ==> set operating system to 'os'\n"
478 " -T ==> set image type to 'type'\n"
479 " -C ==> set compression type 'comp'\n"
480 " -a ==> set load address to 'addr' (hex)\n"
481 " -e ==> set entry point to 'ep' (hex)\n"
482 " -n ==> set image name to 'name'\n"
483 " -d ==> use image data from 'datafile'\n"
484 " -x ==> set XIP (execute in place)\n",
486 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
493 image_verify_header (char *ptr, int image_size)
498 image_header_t header;
499 image_header_t *hdr = &header;
502 * create copy of header so that we can blank out the
503 * checksum field for checking - this can't be done
504 * on the PROT_READ mapped data.
506 memcpy (hdr, ptr, sizeof(image_header_t));
508 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
510 "%s: Bad Magic Number: \"%s\" is no valid image\n",
516 len = sizeof(image_header_t);
518 checksum = ntohl(hdr->ih_hcrc);
519 hdr->ih_hcrc = htonl(0); /* clear for re-calculation */
521 if (crc32 (0, data, len) != checksum) {
523 "%s: ERROR: \"%s\" has bad header checksum!\n",
528 data = ptr + sizeof(image_header_t);
529 len = image_size - sizeof(image_header_t) ;
531 if (crc32 (0, data, len) != ntohl(hdr->ih_dcrc)) {
533 "%s: ERROR: \"%s\" has corrupted data!\n",
540 * fit_handle_file - main FIT file processing function
542 * fit_handle_file() runs dtc to convert .its to .itb, includes
543 * binary data, updates timestamp property and calculates hashes.
545 * datafile - .its file
546 * imagefile - .itb file
549 * only on success, otherwise calls exit (EXIT_FAILURE);
551 static void fit_handle_file (void)
553 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
554 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
559 /* call dtc to include binary properties into the tmp file */
560 if (strlen (imagefile) + strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 >
562 fprintf (stderr, "%s: Image file name (%s) too long, "
563 "can't create tmpfile",
567 sprintf (tmpfile, "%s%s", imagefile, MKIMAGE_TMPFILE_SUFFIX);
569 /* dtc -I dts -O -p 200 datafile > tmpfile */
570 sprintf (cmd, "%s %s %s > %s",
571 MKIMAGE_DTC, opt_dtc, datafile, tmpfile);
572 debug ("Trying to execute \"%s\"\n", cmd);
573 if (system (cmd) == -1) {
574 fprintf (stderr, "%s: system(%s) failed: %s\n",
575 cmdname, cmd, strerror(errno));
580 /* load FIT blob into memory */
581 tfd = open (tmpfile, O_RDWR|O_BINARY);
584 fprintf (stderr, "%s: Can't open %s: %s\n",
585 cmdname, tmpfile, strerror(errno));
590 if (fstat (tfd, &sbuf) < 0) {
591 fprintf (stderr, "%s: Can't stat %s: %s\n",
592 cmdname, tmpfile, strerror(errno));
597 ptr = (unsigned char *)mmap (0, sbuf.st_size,
598 PROT_READ|PROT_WRITE, MAP_SHARED, tfd, 0);
599 if ((caddr_t)ptr == (caddr_t)-1) {
600 fprintf (stderr, "%s: Can't read %s: %s\n",
601 cmdname, tmpfile, strerror(errno));
606 /* check if ptr has a valid blob */
607 if (fdt_check_header (ptr)) {
608 fprintf (stderr, "%s: Invalid FIT blob\n", cmdname);
613 /* set hashes for images in the blob */
614 if (fit_set_hashes (ptr)) {
615 fprintf (stderr, "%s Can't add hashes to FIT blob", cmdname);
620 /* add a timestamp at offset 0 i.e., root */
621 if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) {
622 fprintf (stderr, "%s: Can't add image timestamp\n", cmdname);
626 debug ("Added timestamp successfully\n");
628 munmap ((void *)ptr, sbuf.st_size);
631 if (rename (tmpfile, imagefile) == -1) {
632 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
633 cmdname, tmpfile, imagefile, strerror (errno));