Convert CONFIG_SYS_FLASH_CFI_WIDTH to Kconfig
[platform/kernel/u-boot.git] / tools / mkimage.c
index 2899adf..5c6a60e 100644 (file)
@@ -7,10 +7,15 @@
  * Wolfgang Denk, wd@denx.de
  */
 
+#include "imagetool.h"
 #include "mkimage.h"
 #include "imximage.h"
+#include <fit_common.h>
 #include <image.h>
 #include <version.h>
+#ifdef __linux__
+#include <sys/ioctl.h>
+#endif
 
 static void copy_file(int, const char *, int);
 
@@ -50,8 +55,13 @@ static int show_valid_options(enum ih_category category)
                return -ENOMEM;
 
        /* Sort the names in order of short name for easier reading */
-       for (item = 0; item < count; item++)
-               order[item] = item;
+       for (i = 0, item = 0; i < count; i++, item++) {
+               while (!genimg_cat_has_id(category, item) && i < count) {
+                       item++;
+                       count--;
+               }
+               order[i] = item;
+       }
        cur_category = category;
        qsort(order, count, sizeof(int), h_compare_category_name);
 
@@ -72,8 +82,10 @@ static int show_valid_options(enum ih_category category)
 static void usage(const char *msg)
 {
        fprintf(stderr, "Error: %s\n", msg);
-       fprintf(stderr, "Usage: %s -l image\n"
-                        "          -l ==> list image header information\n",
+       fprintf(stderr, "Usage: %s [-T type] -l image\n"
+                        "          -l ==> list image header information\n"
+                        "          -T ==> parse image file as 'type'\n"
+                        "          -q ==> quiet\n",
                params.cmdname);
        fprintf(stderr,
                "       %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
@@ -84,28 +96,36 @@ static void usage(const char *msg)
                "          -a ==> set load address to 'addr' (hex)\n"
                "          -e ==> set entry point to 'ep' (hex)\n"
                "          -n ==> set image name to 'name'\n"
+               "          -R ==> set second image name to 'name'\n"
                "          -d ==> use image data from 'datafile'\n"
-               "          -x ==> set XIP (execute in place)\n",
+               "          -x ==> set XIP (execute in place)\n"
+               "          -s ==> create an image with no data\n"
+               "          -v ==> verbose\n",
                params.cmdname);
        fprintf(stderr,
-               "       %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n"
+               "       %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-E] [-B size] [-i <ramdisk.cpio.gz>] fit-image\n"
                "           <dtb> file is used with -f auto, it may occur multiple times.\n",
                params.cmdname);
        fprintf(stderr,
                "          -D => set all options for device tree compiler\n"
                "          -f => input filename for FIT source\n"
-               "          -i => input filename for ramdisk file\n");
+               "          -i => input filename for ramdisk file\n"
+               "          -E => place data outside of the FIT structure\n"
+               "          -B => align size in hex for FIT structure and header\n"
+               "          -b => append the device tree binary to the FIT\n"
+               "          -t => update the timestamp in the FIT\n");
 #ifdef CONFIG_FIT_SIGNATURE
        fprintf(stderr,
-               "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
-               "          -E => place data outside of the FIT structure\n"
+               "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
                "          -k => set directory containing private keys\n"
                "          -K => write public keys to this .dtb file\n"
+               "          -G => use this signing key (in lieu of -k)\n"
                "          -c => add comment in signature node\n"
                "          -F => re-sign existing FIT image\n"
                "          -p => place external data at a static position\n"
                "          -r => mark keys used as 'required' in dtb\n"
-               "          -N => engine to use for signing (pkcs11)\n");
+               "          -N => openssl engine to use for signing\n"
+               "          -o => algorithm to use for signing\n");
 #else
        fprintf(stderr,
                "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
@@ -143,7 +163,7 @@ static void process_args(int argc, char **argv)
        int opt;
 
        while ((opt = getopt(argc, argv,
-                            "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) {
+                  "a:A:b:B:c:C:d:D:e:Ef:FG:k:i:K:ln:N:p:o:O:rR:qstT:vVx")) != -1) {
                switch (opt) {
                case 'a':
                        params.addr = strtoull(optarg, &ptr, 16);
@@ -159,6 +179,7 @@ static void process_args(int argc, char **argv)
                                show_valid_options(IH_ARCH);
                                usage("Invalid architecture");
                        }
+                       params.Aflag = 1;
                        break;
                case 'b':
                        if (add_content(IH_TYPE_FLATDT, optarg)) {
@@ -168,6 +189,15 @@ static void process_args(int argc, char **argv)
                                exit(EXIT_FAILURE);
                        }
                        break;
+               case 'B':
+                       params.bl_len = strtoull(optarg, &ptr, 16);
+                       if (*ptr) {
+                               fprintf(stderr, "%s: invalid block length %s\n",
+                                       params.cmdname, optarg);
+                               exit(EXIT_FAILURE);
+                       }
+
+                       break;
                case 'c':
                        params.comment = optarg;
                        break;
@@ -200,7 +230,7 @@ static void process_args(int argc, char **argv)
                case 'f':
                        datafile = optarg;
                        params.auto_its = !strcmp(datafile, "auto");
-                       /* no break */
+                       /* fallthrough */
                case 'F':
                        /*
                         * The flattened image tree (FIT) format
@@ -209,6 +239,9 @@ static void process_args(int argc, char **argv)
                        params.type = IH_TYPE_FLATDT;
                        params.fflag = 1;
                        break;
+               case 'G':
+                       params.keyfile = optarg;
+                       break;
                case 'i':
                        params.fit_ramdisk = optarg;
                        break;
@@ -227,6 +260,9 @@ static void process_args(int argc, char **argv)
                case 'N':
                        params.engine_id = optarg;
                        break;
+               case 'o':
+                       params.algo_name = optarg;
+                       break;
                case 'O':
                        params.os = genimg_get_os_id(optarg);
                        if (params.os < 0) {
@@ -258,6 +294,9 @@ static void process_args(int argc, char **argv)
                case 's':
                        params.skipcpy = 1;
                        break;
+               case 't':
+                       params.reset_timestamp = 1;
+                       break;
                case 'T':
                        if (strcmp(optarg, "list") == 0) {
                                show_valid_options(IH_TYPE);
@@ -299,7 +338,7 @@ static void process_args(int argc, char **argv)
                        params.datafile = datafile;
                else if (!params.datafile)
                        usage("Missing data file for auto-FIT (use -d)");
-       } else if (type != IH_TYPE_INVALID) {
+       } else if (params.lflag || type != IH_TYPE_INVALID) {
                if (type == IH_TYPE_SCRIPT && !params.datafile)
                        usage("Missing data file for script (use -d)");
                params.type = type;
@@ -309,6 +348,44 @@ static void process_args(int argc, char **argv)
                usage("Missing output filename");
 }
 
+static void verify_image(const struct image_type_params *tparams)
+{
+       struct stat sbuf;
+       void *ptr;
+       int ifd;
+
+       ifd = open(params.imagefile, O_RDONLY | O_BINARY);
+       if (ifd < 0) {
+               fprintf(stderr, "%s: Can't open %s: %s\n",
+                       params.cmdname, params.imagefile,
+                       strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if (fstat(ifd, &sbuf) < 0) {
+               fprintf(stderr, "%s: Can't stat %s: %s\n",
+                       params.cmdname, params.imagefile, strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       params.file_size = sbuf.st_size;
+
+       ptr = mmap(0, params.file_size, PROT_READ, MAP_SHARED, ifd, 0);
+       if (ptr == MAP_FAILED) {
+               fprintf(stderr, "%s: Can't map %s: %s\n",
+                       params.cmdname, params.imagefile, strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+
+       if (tparams->verify_header((unsigned char *)ptr, params.file_size, &params) != 0) {
+               fprintf(stderr, "%s: Failed to verify header of %s\n",
+                       params.cmdname, params.imagefile);
+               exit(EXIT_FAILURE);
+       }
+
+       (void)munmap(ptr, params.file_size);
+       (void)close(ifd);
+}
+
 int main(int argc, char **argv)
 {
        int ifd = -1;
@@ -328,7 +405,7 @@ int main(int argc, char **argv)
 
        /* set tparams as per input type_id */
        tparams = imagetool_get_type(params.type);
-       if (tparams == NULL) {
+       if (tparams == NULL && !params.lflag) {
                fprintf (stderr, "%s: unsupported type %s\n",
                        params.cmdname, genimg_get_type_name(params.type));
                exit (EXIT_FAILURE);
@@ -338,18 +415,23 @@ int main(int argc, char **argv)
         * check the passed arguments parameters meets the requirements
         * as per image type to be generated/listed
         */
-       if (tparams->check_params)
+       if (tparams && tparams->check_params)
                if (tparams->check_params (&params))
                        usage("Bad parameters for image type");
 
        if (!params.eflag) {
                params.ep = params.addr;
                /* If XIP, entry point must be after the U-Boot header */
-               if (params.xflag)
+               if (params.xflag && tparams)
                        params.ep += tparams->header_size;
        }
 
        if (params.fflag){
+               if (!tparams) {
+                       fprintf(stderr, "%s: Missing FIT support\n",
+                               params.cmdname);
+                       exit (EXIT_FAILURE);
+               }
                if (tparams->fflag_handle)
                        /*
                         * in some cases, some additional processing needs
@@ -360,7 +442,7 @@ int main(int argc, char **argv)
                        retval = tparams->fflag_handle(&params);
 
                if (retval != EXIT_SUCCESS)
-                       exit (retval);
+                       usage("Bad parameters for FIT image type");
        }
 
        if (params.lflag || params.fflag) {
@@ -378,6 +460,7 @@ int main(int argc, char **argv)
        }
 
        if (params.lflag || params.fflag) {
+               uint64_t size;
                /*
                 * list header information of existing image
                 */
@@ -388,14 +471,35 @@ int main(int argc, char **argv)
                        exit (EXIT_FAILURE);
                }
 
-               if ((unsigned)sbuf.st_size < tparams->header_size) {
+               if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
+#ifdef __linux__
+#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
+#define BLKGETSIZE64 _IOR(0x12,114,size_t)     /* return device size in bytes (u64 *arg) */
+#endif
+                       if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
+                               fprintf (stderr,
+                                       "%s: failed to get size of block device \"%s\"\n",
+                                       params.cmdname, params.imagefile);
+                               exit (EXIT_FAILURE);
+                       }
+#else
                        fprintf (stderr,
-                               "%s: Bad size: \"%s\" is not valid image\n",
+                               "%s: \"%s\" is block device, don't know how to get its size\n",
                                params.cmdname, params.imagefile);
                        exit (EXIT_FAILURE);
+#endif
+               } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) {
+                       fprintf (stderr,
+                               "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
+                               params.cmdname, params.imagefile,
+                               (unsigned long long) sbuf.st_size,
+                               tparams->header_size);
+                       exit (EXIT_FAILURE);
+               } else {
+                       size = sbuf.st_size;
                }
 
-               ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
+               ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
                if (ptr == MAP_FAILED) {
                        fprintf (stderr, "%s: Can't read %s: %s\n",
                                params.cmdname, params.imagefile,
@@ -404,16 +508,17 @@ int main(int argc, char **argv)
                }
 
                /*
-                * scan through mkimage registry for all supported image types
-                * and verify the input image file header for match
-                * Print the image information for matched image type
-                * Returns the error code if not matched
+                * Verifies the header format based on the expected header for image
+                * type in tparams. If tparams is NULL simply check all image types
+                * to find one that matches our header.
                 */
-               retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
-                               tparams, &params);
+               retval = imagetool_verify_print_header(ptr, &sbuf, tparams, &params);
 
                (void) munmap((void *)ptr, sbuf.st_size);
                (void) close (ifd);
+               if (!retval)
+                       summary_show(&params.summary, params.imagefile,
+                                    params.keydest);
 
                exit (retval);
        }
@@ -537,13 +642,21 @@ int main(int argc, char **argv)
                        ret = imx8mimage_copy_image(ifd, &params);
                        if (ret)
                                return ret;
+               } else if ((params.type == IH_TYPE_RKSD) ||
+                               (params.type == IH_TYPE_RKSPI)) {
+                       /* Rockchip has special Image format */
+                       int ret;
+
+                       ret = rockchip_copy_image(ifd, &params);
+                       if (ret)
+                               return ret;
                } else {
                        copy_file(ifd, params.datafile, pad_len);
                }
                if (params.type == IH_TYPE_FIRMWARE_IVT) {
                        /* Add alignment and IVT */
-                       uint32_t aligned_filesize = (params.file_size + 0x1000
-                                       - 1) & ~(0x1000 - 1);
+                       uint32_t aligned_filesize = ALIGN(params.file_size,
+                                                         0x1000);
                        flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
                                        params.addr, 0, 0, 0, params.addr
                                                        + aligned_filesize
@@ -635,6 +748,9 @@ int main(int argc, char **argv)
                exit (EXIT_FAILURE);
        }
 
+       if (tparams->verify_header)
+               verify_image(tparams);
+
        exit (EXIT_SUCCESS);
 }
 
@@ -648,7 +764,7 @@ copy_file (int ifd, const char *datafile, int pad)
        int zero = 0;
        uint8_t zeros[4096];
        int offset = 0;
-       int size;
+       int size, ret;
        struct image_type_params *tparams = imagetool_get_type(params.type);
 
        memset(zeros, 0, sizeof(zeros));
@@ -669,6 +785,12 @@ copy_file (int ifd, const char *datafile, int pad)
                exit (EXIT_FAILURE);
        }
 
+       if (sbuf.st_size == 0) {
+               fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
+                       params.cmdname, datafile);
+               exit (EXIT_FAILURE);
+       }
+
        ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
        if (ptr == MAP_FAILED) {
                fprintf (stderr, "%s: Can't read %s: %s\n",
@@ -704,9 +826,16 @@ copy_file (int ifd, const char *datafile, int pad)
        }
 
        size = sbuf.st_size - offset;
-       if (write(ifd, ptr + offset, size) != size) {
-               fprintf (stderr, "%s: Write error on %s: %s\n",
-                       params.cmdname, params.imagefile, strerror(errno));
+
+       ret = write(ifd, ptr + offset, size);
+       if (ret != size) {
+               if (ret < 0)
+                       fprintf (stderr, "%s: Write error on %s: %s\n",
+                                params.cmdname, params.imagefile, strerror(errno));
+               else if (ret < size)
+                       fprintf (stderr, "%s: Write only %d/%d bytes, "\
+                                "probably no space left on the device\n",
+                                params.cmdname, ret, size);
                exit (EXIT_FAILURE);
        }