2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2004
5 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <u-boot/crc.h>
28 static void copy_file (int, const char *, int);
29 static void usage (void);
30 static int image_verify_header (char *, int);
31 static void fit_handle_file (void);
43 int opt_os = IH_OS_LINUX;
44 int opt_arch = IH_ARCH_PPC;
45 int opt_type = IH_TYPE_KERNEL;
46 int opt_comp = IH_COMP_GZIP;
47 char *opt_dtc = MKIMAGE_DEFAULT_DTC_OPTIONS;
49 image_header_t header;
50 image_header_t *hdr = &header;
53 main (int argc, char **argv)
68 while (--argc > 0 && **++argv == '-') {
76 (opt_arch = genimg_get_arch_id (*++argv)) < 0)
81 (opt_comp = genimg_get_comp_id (*++argv)) < 0)
92 (opt_os = genimg_get_os_id (*++argv)) < 0)
97 (opt_type = genimg_get_type_id (*++argv)) < 0)
104 addr = strtoul (*++argv, (char **)&ptr, 16);
107 "%s: invalid load address %s\n",
121 ep = strtoul (*++argv, (char **)&ptr, 16);
124 "%s: invalid entry point %s\n",
155 (dflag && (fflag || lflag)) ||
156 (fflag && (dflag || lflag)) ||
157 (lflag && (dflag || fflag)))
162 /* If XIP, entry point must be after the U-Boot header */
164 ep += image_get_header_size ();
168 * If XIP, ensure the entry point is equal to the load address plus
169 * the size of the U-Boot header.
172 if (ep != addr + image_get_header_size ()) {
174 "%s: For XIP, the entry point must be the load addr + %lu\n",
176 (unsigned long)image_get_header_size ());
185 ifd = open (imagefile, O_RDONLY|O_BINARY);
187 ifd = open (imagefile,
188 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
192 fprintf (stderr, "%s: Can't open %s: %s\n",
193 cmdname, imagefile, strerror(errno));
200 * list header information of existing image
202 if (fstat(ifd, &sbuf) < 0) {
203 fprintf (stderr, "%s: Can't stat %s: %s\n",
204 cmdname, imagefile, strerror(errno));
208 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
210 "%s: Bad size: \"%s\" is no valid image\n",
215 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
216 if (ptr == MAP_FAILED) {
217 fprintf (stderr, "%s: Can't read %s: %s\n",
218 cmdname, imagefile, strerror(errno));
222 if (!(retval = fdt_check_header (ptr))) {
224 fit_print_contents (ptr);
225 } else if (!(retval = image_verify_header ((char *)ptr,
227 /* old-style image */
228 image_print_contents ((image_header_t *)ptr);
231 (void) munmap((void *)ptr, sbuf.st_size);
236 /* Flattened Image Tree (FIT) format handling */
237 debug ("FIT format handling\n");
245 * write dummy header, to be fixed later
247 memset (hdr, 0, image_get_header_size ());
249 if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) {
250 fprintf (stderr, "%s: Write error on %s: %s\n",
251 cmdname, imagefile, strerror(errno));
255 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
256 char *file = datafile;
263 if ((sep = strchr(file, ':')) != NULL) {
267 if (stat (file, &sbuf) < 0) {
268 fprintf (stderr, "%s: Can't stat %s: %s\n",
269 cmdname, file, strerror(errno));
272 size = cpu_to_uimage (sbuf.st_size);
277 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
278 fprintf (stderr, "%s: Write error on %s: %s\n",
279 cmdname, imagefile, strerror(errno));
298 char *sep = strchr(file, ':');
301 copy_file (ifd, file, 1);
305 copy_file (ifd, file, 0);
310 copy_file (ifd, datafile, 0);
313 /* We're a bit of paranoid */
314 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
315 (void) fdatasync (ifd);
320 if (fstat(ifd, &sbuf) < 0) {
321 fprintf (stderr, "%s: Can't stat %s: %s\n",
322 cmdname, imagefile, strerror(errno));
326 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
327 if (ptr == MAP_FAILED) {
328 fprintf (stderr, "%s: Can't map %s: %s\n",
329 cmdname, imagefile, strerror(errno));
333 hdr = (image_header_t *)ptr;
336 (const unsigned char *)(ptr +
337 image_get_header_size ()),
338 sbuf.st_size - image_get_header_size ()
341 /* Build new header */
342 image_set_magic (hdr, IH_MAGIC);
343 image_set_time (hdr, sbuf.st_mtime);
344 image_set_size (hdr, sbuf.st_size - image_get_header_size ());
345 image_set_load (hdr, addr);
346 image_set_ep (hdr, ep);
347 image_set_dcrc (hdr, checksum);
348 image_set_os (hdr, opt_os);
349 image_set_arch (hdr, opt_arch);
350 image_set_type (hdr, opt_type);
351 image_set_comp (hdr, opt_comp);
353 image_set_name (hdr, name);
355 checksum = crc32 (0, (const unsigned char *)hdr,
356 image_get_header_size ());
358 image_set_hcrc (hdr, checksum);
360 image_print_contents (hdr);
362 (void) munmap((void *)ptr, sbuf.st_size);
364 /* We're a bit of paranoid */
365 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
366 (void) fdatasync (ifd);
372 fprintf (stderr, "%s: Write error on %s: %s\n",
373 cmdname, imagefile, strerror(errno));
381 copy_file (int ifd, const char *datafile, int pad)
392 fprintf (stderr, "Adding Image %s\n", datafile);
395 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
396 fprintf (stderr, "%s: Can't open %s: %s\n",
397 cmdname, datafile, strerror(errno));
401 if (fstat(dfd, &sbuf) < 0) {
402 fprintf (stderr, "%s: Can't stat %s: %s\n",
403 cmdname, datafile, strerror(errno));
407 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
408 if (ptr == MAP_FAILED) {
409 fprintf (stderr, "%s: Can't read %s: %s\n",
410 cmdname, datafile, strerror(errno));
415 unsigned char *p = NULL;
417 * XIP: do not append the image_header_t at the
418 * beginning of the file, but consume the space
422 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
424 "%s: Bad size: \"%s\" is too small for XIP\n",
429 for (p = ptr; p < ptr + image_get_header_size (); p++) {
432 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
438 offset = image_get_header_size ();
441 size = sbuf.st_size - offset;
442 if (write(ifd, ptr + offset, size) != size) {
443 fprintf (stderr, "%s: Write error on %s: %s\n",
444 cmdname, imagefile, strerror(errno));
448 if (pad && ((tail = size % 4) != 0)) {
450 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
451 fprintf (stderr, "%s: Write error on %s: %s\n",
452 cmdname, imagefile, strerror(errno));
457 (void) munmap((void *)ptr, sbuf.st_size);
464 fprintf (stderr, "Usage: %s -l image\n"
465 " -l ==> list image header information\n",
467 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
468 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
469 " -A ==> set architecture to 'arch'\n"
470 " -O ==> set operating system to 'os'\n"
471 " -T ==> set image type to 'type'\n"
472 " -C ==> set compression type 'comp'\n"
473 " -a ==> set load address to 'addr' (hex)\n"
474 " -e ==> set entry point to 'ep' (hex)\n"
475 " -n ==> set image name to 'name'\n"
476 " -d ==> use image data from 'datafile'\n"
477 " -x ==> set XIP (execute in place)\n",
479 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
486 image_verify_header (char *ptr, int image_size)
489 const unsigned char *data;
491 image_header_t header;
492 image_header_t *hdr = &header;
495 * create copy of header so that we can blank out the
496 * checksum field for checking - this can't be done
497 * on the PROT_READ mapped data.
499 memcpy (hdr, ptr, sizeof(image_header_t));
501 if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
503 "%s: Bad Magic Number: \"%s\" is no valid image\n",
505 return -FDT_ERR_BADMAGIC;
508 data = (const unsigned char *)hdr;
509 len = sizeof(image_header_t);
511 checksum = be32_to_cpu(hdr->ih_hcrc);
512 hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
514 if (crc32 (0, data, len) != checksum) {
516 "%s: ERROR: \"%s\" has bad header checksum!\n",
518 return -FDT_ERR_BADSTATE;
521 data = (const unsigned char *)ptr + sizeof(image_header_t);
522 len = image_size - sizeof(image_header_t) ;
524 if (crc32 (0, data, len) != be32_to_cpu(hdr->ih_dcrc)) {
526 "%s: ERROR: \"%s\" has corrupted data!\n",
528 return -FDT_ERR_BADSTRUCTURE;
534 * fit_handle_file - main FIT file processing function
536 * fit_handle_file() runs dtc to convert .its to .itb, includes
537 * binary data, updates timestamp property and calculates hashes.
539 * datafile - .its file
540 * imagefile - .itb file
543 * only on success, otherwise calls exit (EXIT_FAILURE);
545 static void fit_handle_file (void)
547 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
548 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
553 /* call dtc to include binary properties into the tmp file */
554 if (strlen (imagefile) + strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 >
556 fprintf (stderr, "%s: Image file name (%s) too long, "
557 "can't create tmpfile",
561 sprintf (tmpfile, "%s%s", imagefile, MKIMAGE_TMPFILE_SUFFIX);
563 /* dtc -I dts -O -p 200 datafile > tmpfile */
564 sprintf (cmd, "%s %s %s > %s",
565 MKIMAGE_DTC, opt_dtc, datafile, tmpfile);
566 debug ("Trying to execute \"%s\"\n", cmd);
567 if (system (cmd) == -1) {
568 fprintf (stderr, "%s: system(%s) failed: %s\n",
569 cmdname, cmd, strerror(errno));
574 /* load FIT blob into memory */
575 tfd = open (tmpfile, O_RDWR|O_BINARY);
578 fprintf (stderr, "%s: Can't open %s: %s\n",
579 cmdname, tmpfile, strerror(errno));
584 if (fstat (tfd, &sbuf) < 0) {
585 fprintf (stderr, "%s: Can't stat %s: %s\n",
586 cmdname, tmpfile, strerror(errno));
591 ptr = mmap (0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, tfd, 0);
592 if (ptr == MAP_FAILED) {
593 fprintf (stderr, "%s: Can't read %s: %s\n",
594 cmdname, tmpfile, strerror(errno));
599 /* check if ptr has a valid blob */
600 if (fdt_check_header (ptr)) {
601 fprintf (stderr, "%s: Invalid FIT blob\n", cmdname);
606 /* set hashes for images in the blob */
607 if (fit_set_hashes (ptr)) {
608 fprintf (stderr, "%s Can't add hashes to FIT blob", cmdname);
613 /* add a timestamp at offset 0 i.e., root */
614 if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) {
615 fprintf (stderr, "%s: Can't add image timestamp\n", cmdname);
619 debug ("Added timestamp successfully\n");
621 munmap ((void *)ptr, sbuf.st_size);
624 if (rename (tmpfile, imagefile) == -1) {
625 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
626 cmdname, tmpfile, imagefile, strerror (errno));