Merge tag 'u-boot-imx-20230923' of https://source.denx.de/u-boot/custodians/u-boot-imx
[platform/kernel/u-boot.git] / tools / mkimage.c
index cc7b242..6dfe3e1 100644 (file)
 #include "imagetool.h"
 #include "mkimage.h"
 #include "imximage.h"
+#include <fit_common.h>
+#include <getopt.h>
 #include <image.h>
 #include <version.h>
+#ifdef __linux__
+#include <sys/ioctl.h>
+#endif
 
 static void copy_file(int, const char *, int);
 
@@ -78,8 +83,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"
@@ -90,11 +97,14 @@ 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>]] [-E] [-B size] [-i <ramdisk.cpio.gz>] fit-image\n"
+               "       %s [-D dtc_options] [-f fit-image.its|-f auto|-f auto-conf|-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,
@@ -102,18 +112,22 @@ static void usage(const char *msg)
                "          -f => input filename for FIT source\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 => 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: [-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 => set key name hint\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 => openssl engine to use for signing\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");
@@ -121,6 +135,7 @@ static void usage(const char *msg)
        fprintf(stderr, "       %s -V ==> print version information and exit\n",
                params.cmdname);
        fprintf(stderr, "Use '-T list' to see a list of available image types\n");
+       fprintf(stderr, "Long options are available; read the man page for details\n");
 
        exit(EXIT_FAILURE);
 }
@@ -143,7 +158,45 @@ static int add_content(int type, const char *fname)
        return 0;
 }
 
-#define OPT_STRING "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx"
+static const char optstring[] =
+       "a:A:b:B:c:C:d:D:e:Ef:Fg:G:i:k:K:ln:N:o:O:p:qrR:stT:vVx";
+
+static const struct option longopts[] = {
+       { "load-address", required_argument, NULL, 'a' },
+       { "architecture", required_argument, NULL, 'A' },
+       { "device-tree", required_argument, NULL, 'b' },
+       { "alignment", required_argument, NULL, 'B' },
+       { "comment", required_argument, NULL, 'c' },
+       { "compression", required_argument, NULL, 'C' },
+       { "image", required_argument, NULL, 'd' },
+       { "dtcopts", required_argument, NULL, 'D' },
+       { "entry-point", required_argument, NULL, 'e' },
+       { "external", no_argument, NULL, 'E' },
+       { "fit", required_argument, NULL, 'f' },
+       { "update", no_argument, NULL, 'F' },
+       { "key-name-hint", required_argument, NULL, 'g' },
+       { "key-file", required_argument, NULL, 'G' },
+       { "help", no_argument, NULL, 'h' },
+       { "initramfs", required_argument, NULL, 'i' },
+       { "key-dir", required_argument, NULL, 'k' },
+       { "key-dest", required_argument, NULL, 'K' },
+       { "list", no_argument, NULL, 'l' },
+       { "config", required_argument, NULL, 'n' },
+       { "engine", required_argument, NULL, 'N' },
+       { "algo", required_argument, NULL, 'o' },
+       { "os", required_argument, NULL, 'O' },
+       { "position", required_argument, NULL, 'p' },
+       { "quiet", no_argument, NULL, 'q' },
+       { "key-required", no_argument, NULL, 'r' },
+       { "secondary-config", required_argument, NULL, 'R' },
+       { "no-copy", no_argument, NULL, 's' },
+       { "touch", no_argument, NULL, 't' },
+       { "type", required_argument, NULL, 'T' },
+       { "verbose", no_argument, NULL, 'v' },
+       { "version", no_argument, NULL, 'V' },
+       { "xip", no_argument, NULL, 'x' },
+};
+
 static void process_args(int argc, char **argv)
 {
        char *ptr;
@@ -151,8 +204,8 @@ static void process_args(int argc, char **argv)
        char *datafile = NULL;
        int opt;
 
-       while ((opt = getopt(argc, argv,
-                  "a:A:b:B:c:C:d:D:e:Ef:FG:k:i:K:ln:N:p:O:rR:qstT:vVx")) != -1) {
+       while ((opt = getopt_long(argc, argv, optstring,
+                                 longopts, NULL)) != -1) {
                switch (opt) {
                case 'a':
                        params.addr = strtoull(optarg, &ptr, 16);
@@ -168,6 +221,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)) {
@@ -217,7 +271,10 @@ static void process_args(int argc, char **argv)
                        break;
                case 'f':
                        datafile = optarg;
-                       params.auto_its = !strcmp(datafile, "auto");
+                       if (!strcmp(datafile, "auto"))
+                               params.auto_fit = AF_HASHED_IMG;
+                       else if (!strcmp(datafile, "auto-conf"))
+                               params.auto_fit = AF_SIGNED_CONF;
                        /* fallthrough */
                case 'F':
                        /*
@@ -227,6 +284,9 @@ static void process_args(int argc, char **argv)
                        params.type = IH_TYPE_FLATDT;
                        params.fflag = 1;
                        break;
+               case 'g':
+                       params.keyname = optarg;
+                       break;
                case 'G':
                        params.keyfile = optarg;
                        break;
@@ -248,6 +308,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) {
@@ -311,6 +374,15 @@ static void process_args(int argc, char **argv)
        if (optind < argc)
                params.imagefile = argv[optind];
 
+       if (params.auto_fit == AF_SIGNED_CONF) {
+               if (!params.keyname || !params.algo_name)
+                       usage("Missing key/algo for auto-FIT with signed configs (use -g -o)");
+       } else if (params.auto_fit == AF_HASHED_IMG && params.keyname) {
+               params.auto_fit = AF_SIGNED_IMG;
+               if (!params.algo_name)
+                       usage("Missing algorithm for auto-FIT with signed images (use -g)");
+       }
+
        /*
         * For auto-generated FIT images we need to know the image type to put
         * in the FIT, which is separate from the file's image type (which
@@ -318,12 +390,12 @@ static void process_args(int argc, char **argv)
         */
        if (params.type == IH_TYPE_FLATDT) {
                params.fit_image_type = type ? type : IH_TYPE_KERNEL;
-               /* For auto_its, datafile is always 'auto' */
-               if (!params.auto_its)
+               /* For auto-FIT, datafile has to be provided with -d */
+               if (!params.auto_fit)
                        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;
@@ -333,6 +405,63 @@ 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);
+}
+
+void copy_datafile(int ifd, char *file)
+{
+       if (!file)
+               return;
+       for (;;) {
+               char *sep = strchr(file, ':');
+
+               if (sep) {
+                       *sep = '\0';
+                       copy_file(ifd, file, 1);
+                       *sep++ = ':';
+                       file = sep;
+               } else {
+                       copy_file(ifd, file, 0);
+                       break;
+               }
+       }
+}
+
 int main(int argc, char **argv)
 {
        int ifd = -1;
@@ -352,7 +481,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);
@@ -362,18 +491,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
@@ -384,7 +518,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) {
@@ -402,6 +536,7 @@ int main(int argc, char **argv)
        }
 
        if (params.lflag || params.fflag) {
+               uint64_t size;
                /*
                 * list header information of existing image
                 */
@@ -412,14 +547,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,
@@ -427,29 +583,28 @@ int main(int argc, char **argv)
                        exit (EXIT_FAILURE);
                }
 
-               if (params.fflag) {
-                       /*
-                        * Verifies the header format based on the expected header for image
-                        * type in tparams
-                        */
-                       retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
-                                       tparams, &params);
-               } else {
-                       /**
-                        * When listing the image, we are not given the image type. Simply check all
-                        * image types to find one that matches our header
-                        */
-                       retval = imagetool_verify_print_header(ptr, &sbuf,
-                                       tparams, &params);
-               }
+               /*
+                * 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(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);
        }
 
-       if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
+       if (!params.skipcpy && params.type != IH_TYPE_MULTI && params.type != IH_TYPE_SCRIPT) {
+               if (!params.datafile) {
+                       fprintf(stderr, "%s: Option -d with image data file was not specified\n",
+                               params.cmdname);
+                       exit(EXIT_FAILURE);
+               }
                dfd = open(params.datafile, O_RDONLY | O_BINARY);
                if (dfd < 0) {
                        fprintf(stderr, "%s: Can't open %s: %s\n",
@@ -529,21 +684,7 @@ int main(int argc, char **argv)
                                        file = NULL;
                                }
                        }
-
-                       file = params.datafile;
-
-                       for (;;) {
-                               char *sep = strchr(file, ':');
-                               if (sep) {
-                                       *sep = '\0';
-                                       copy_file (ifd, file, 1);
-                                       *sep++ = ':';
-                                       file = sep;
-                               } else {
-                                       copy_file (ifd, file, 0);
-                                       break;
-                               }
-                       }
+                       copy_datafile(ifd, params.datafile);
                } else if (params.type == IH_TYPE_PBLIMAGE) {
                        /* PBL has special Image format, implements its' own */
                        pbl_load_uboot(ifd, &params);
@@ -642,14 +783,14 @@ int main(int argc, char **argv)
        if (tparams->set_header)
                tparams->set_header (ptr, &sbuf, ifd, &params);
        else {
-               fprintf (stderr, "%s: Can't set header for %s: %s\n",
-                       params.cmdname, tparams->name, strerror(errno));
+               fprintf (stderr, "%s: Can't set header for %s\n",
+                       params.cmdname, tparams->name);
                exit (EXIT_FAILURE);
        }
 
        /* Print the image information by processing image header */
        if (tparams->print_header)
-               tparams->print_header (ptr);
+               tparams->print_header (ptr, &params);
        else {
                fprintf (stderr, "%s: Can't print header for %s\n",
                        params.cmdname, tparams->name);
@@ -674,6 +815,9 @@ int main(int argc, char **argv)
                exit (EXIT_FAILURE);
        }
 
+       if (tparams->verify_header)
+               verify_image(tparams);
+
        exit (EXIT_SUCCESS);
 }
 
@@ -708,6 +852,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",
@@ -715,10 +865,12 @@ copy_file (int ifd, const char *datafile, int pad)
                exit (EXIT_FAILURE);
        }
 
-       if (params.xflag) {
+       if (params.xflag &&
+           (((params.type > IH_TYPE_INVALID) && (params.type < IH_TYPE_FLATDT)) ||
+            (params.type == IH_TYPE_KERNEL_NOLOAD) || (params.type == IH_TYPE_FIRMWARE_IVT))) {
                unsigned char *p = NULL;
                /*
-                * XIP: do not append the image_header_t at the
+                * XIP: do not append the struct legacy_img_hdr at the
                 * beginning of the file, but consume the space
                 * reserved for it.
                 */