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,
27 extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
28 static void copy_file (int, const char *, int);
29 static void usage (void);
30 static void 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)
67 while (--argc > 0 && **++argv == '-') {
75 (opt_arch = genimg_get_arch_id (*++argv)) < 0)
80 (opt_comp = genimg_get_comp_id (*++argv)) < 0)
91 (opt_os = genimg_get_os_id (*++argv)) < 0)
96 (opt_type = genimg_get_type_id (*++argv)) < 0)
103 addr = strtoul (*++argv, (char **)&ptr, 16);
106 "%s: invalid load address %s\n",
120 ep = strtoul (*++argv, (char **)&ptr, 16);
123 "%s: invalid entry point %s\n",
154 (dflag && (fflag || lflag)) ||
155 (fflag && (dflag || lflag)) ||
156 (lflag && (dflag || fflag)))
161 /* If XIP, entry point must be after the U-Boot header */
163 ep += image_get_header_size ();
167 * If XIP, ensure the entry point is equal to the load address plus
168 * the size of the U-Boot header.
171 if (ep != addr + image_get_header_size ()) {
173 "%s: For XIP, the entry point must be the load addr + %lu\n",
175 (unsigned long)image_get_header_size ());
184 ifd = open (imagefile, O_RDONLY|O_BINARY);
186 ifd = open (imagefile,
187 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
191 fprintf (stderr, "%s: Can't open %s: %s\n",
192 cmdname, imagefile, strerror(errno));
199 * list header information of existing image
201 if (fstat(ifd, &sbuf) < 0) {
202 fprintf (stderr, "%s: Can't stat %s: %s\n",
203 cmdname, imagefile, strerror(errno));
207 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
209 "%s: Bad size: \"%s\" is no valid image\n",
214 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
215 if (ptr == MAP_FAILED) {
216 fprintf (stderr, "%s: Can't read %s: %s\n",
217 cmdname, imagefile, strerror(errno));
221 if (fdt_check_header (ptr)) {
222 /* old-style image */
223 image_verify_header ((char *)ptr, sbuf.st_size);
224 image_print_contents ((image_header_t *)ptr);
227 fit_print_contents (ptr);
230 (void) munmap((void *)ptr, sbuf.st_size);
235 /* Flattened Image Tree (FIT) format handling */
236 debug ("FIT format handling\n");
244 * write dummy header, to be fixed later
246 memset (hdr, 0, image_get_header_size ());
248 if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) {
249 fprintf (stderr, "%s: Write error on %s: %s\n",
250 cmdname, imagefile, strerror(errno));
254 if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
255 char *file = datafile;
262 if ((sep = strchr(file, ':')) != NULL) {
266 if (stat (file, &sbuf) < 0) {
267 fprintf (stderr, "%s: Can't stat %s: %s\n",
268 cmdname, file, strerror(errno));
271 size = cpu_to_uimage (sbuf.st_size);
276 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
277 fprintf (stderr, "%s: Write error on %s: %s\n",
278 cmdname, imagefile, strerror(errno));
297 char *sep = strchr(file, ':');
300 copy_file (ifd, file, 1);
304 copy_file (ifd, file, 0);
309 copy_file (ifd, datafile, 0);
312 /* We're a bit of paranoid */
313 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
314 (void) fdatasync (ifd);
319 if (fstat(ifd, &sbuf) < 0) {
320 fprintf (stderr, "%s: Can't stat %s: %s\n",
321 cmdname, imagefile, strerror(errno));
325 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
326 if (ptr == MAP_FAILED) {
327 fprintf (stderr, "%s: Can't map %s: %s\n",
328 cmdname, imagefile, strerror(errno));
332 hdr = (image_header_t *)ptr;
335 (const char *)(ptr + image_get_header_size ()),
336 sbuf.st_size - image_get_header_size ()
339 /* Build new header */
340 image_set_magic (hdr, IH_MAGIC);
341 image_set_time (hdr, sbuf.st_mtime);
342 image_set_size (hdr, sbuf.st_size - image_get_header_size ());
343 image_set_load (hdr, addr);
344 image_set_ep (hdr, ep);
345 image_set_dcrc (hdr, checksum);
346 image_set_os (hdr, opt_os);
347 image_set_arch (hdr, opt_arch);
348 image_set_type (hdr, opt_type);
349 image_set_comp (hdr, opt_comp);
351 image_set_name (hdr, name);
353 checksum = crc32 (0, (const char *)hdr, image_get_header_size ());
355 image_set_hcrc (hdr, checksum);
357 image_print_contents (hdr);
359 (void) munmap((void *)ptr, sbuf.st_size);
361 /* We're a bit of paranoid */
362 #if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
363 (void) fdatasync (ifd);
369 fprintf (stderr, "%s: Write error on %s: %s\n",
370 cmdname, imagefile, strerror(errno));
378 copy_file (int ifd, const char *datafile, int pad)
389 fprintf (stderr, "Adding Image %s\n", datafile);
392 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
393 fprintf (stderr, "%s: Can't open %s: %s\n",
394 cmdname, datafile, strerror(errno));
398 if (fstat(dfd, &sbuf) < 0) {
399 fprintf (stderr, "%s: Can't stat %s: %s\n",
400 cmdname, datafile, strerror(errno));
404 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
405 if (ptr == MAP_FAILED) {
406 fprintf (stderr, "%s: Can't read %s: %s\n",
407 cmdname, datafile, strerror(errno));
412 unsigned char *p = NULL;
414 * XIP: do not append the image_header_t at the
415 * beginning of the file, but consume the space
419 if ((unsigned)sbuf.st_size < image_get_header_size ()) {
421 "%s: Bad size: \"%s\" is too small for XIP\n",
426 for (p = ptr; p < ptr + image_get_header_size (); p++) {
429 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
435 offset = image_get_header_size ();
438 size = sbuf.st_size - offset;
439 if (write(ifd, ptr + offset, size) != size) {
440 fprintf (stderr, "%s: Write error on %s: %s\n",
441 cmdname, imagefile, strerror(errno));
445 if (pad && ((tail = size % 4) != 0)) {
447 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
448 fprintf (stderr, "%s: Write error on %s: %s\n",
449 cmdname, imagefile, strerror(errno));
454 (void) munmap((void *)ptr, sbuf.st_size);
461 fprintf (stderr, "Usage: %s -l image\n"
462 " -l ==> list image header information\n",
464 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
465 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
466 " -A ==> set architecture to 'arch'\n"
467 " -O ==> set operating system to 'os'\n"
468 " -T ==> set image type to 'type'\n"
469 " -C ==> set compression type 'comp'\n"
470 " -a ==> set load address to 'addr' (hex)\n"
471 " -e ==> set entry point to 'ep' (hex)\n"
472 " -n ==> set image name to 'name'\n"
473 " -d ==> use image data from 'datafile'\n"
474 " -x ==> set XIP (execute in place)\n",
476 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
483 image_verify_header (char *ptr, int image_size)
488 image_header_t header;
489 image_header_t *hdr = &header;
492 * create copy of header so that we can blank out the
493 * checksum field for checking - this can't be done
494 * on the PROT_READ mapped data.
496 memcpy (hdr, ptr, sizeof(image_header_t));
498 if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
500 "%s: Bad Magic Number: \"%s\" is no valid image\n",
506 len = sizeof(image_header_t);
508 checksum = be32_to_cpu(hdr->ih_hcrc);
509 hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
511 if (crc32 (0, data, len) != checksum) {
513 "%s: ERROR: \"%s\" has bad header checksum!\n",
518 data = ptr + sizeof(image_header_t);
519 len = image_size - sizeof(image_header_t) ;
521 if (crc32 (0, data, len) != be32_to_cpu(hdr->ih_dcrc)) {
523 "%s: ERROR: \"%s\" has corrupted data!\n",
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
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];
549 /* call dtc to include binary properties into the tmp file */
550 if (strlen (imagefile) + strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 >
552 fprintf (stderr, "%s: Image file name (%s) too long, "
553 "can't create tmpfile",
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));
570 /* load FIT blob into memory */
571 tfd = open (tmpfile, O_RDWR|O_BINARY);
574 fprintf (stderr, "%s: Can't open %s: %s\n",
575 cmdname, tmpfile, strerror(errno));
580 if (fstat (tfd, &sbuf) < 0) {
581 fprintf (stderr, "%s: Can't stat %s: %s\n",
582 cmdname, tmpfile, strerror(errno));
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));
595 /* check if ptr has a valid blob */
596 if (fdt_check_header (ptr)) {
597 fprintf (stderr, "%s: Invalid FIT blob\n", cmdname);
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);
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);
615 debug ("Added timestamp successfully\n");
617 munmap ((void *)ptr, sbuf.st_size);
620 if (rename (tmpfile, imagefile) == -1) {
621 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
622 cmdname, tmpfile, imagefile, strerror (errno));