1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 Semihalf
5 * (C) Copyright 2000-2009
6 * DENX Software Engineering
7 * Wolfgang Denk, wd@denx.de
10 #include "imagetool.h"
13 #include <fit_common.h>
17 #include <sys/ioctl.h>
20 static void copy_file(int, const char *, int);
22 /* parameters initialized by core will be used by the image type code */
23 static struct image_tool_params params = {
26 .type = IH_TYPE_KERNEL,
28 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
33 static enum ih_category cur_category;
35 static int h_compare_category_name(const void *vtype1, const void *vtype2)
37 const int *type1 = vtype1;
38 const int *type2 = vtype2;
39 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
40 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
42 return strcmp(name1, name2);
45 static int show_valid_options(enum ih_category category)
52 count = genimg_get_cat_count(category);
53 order = calloc(count, sizeof(*order));
57 /* Sort the names in order of short name for easier reading */
58 for (i = 0, item = 0; i < count; i++, item++) {
59 while (!genimg_cat_has_id(category, item) && i < count) {
65 cur_category = category;
66 qsort(order, count, sizeof(int), h_compare_category_name);
68 fprintf(stderr, "\nInvalid %s, supported are:\n",
69 genimg_get_cat_desc(category));
70 for (i = 0; i < count; i++) {
72 fprintf(stderr, "\t%-15s %s\n",
73 genimg_get_cat_short_name(category, item),
74 genimg_get_cat_name(category, item));
76 fprintf(stderr, "\n");
82 static void usage(const char *msg)
84 fprintf(stderr, "Error: %s\n", msg);
85 fprintf(stderr, "Usage: %s [-T type] -l image\n"
86 " -l ==> list image header information\n"
87 " -T ==> parse image file as 'type'\n",
90 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
91 " -A ==> set architecture to 'arch'\n"
92 " -O ==> set operating system to 'os'\n"
93 " -T ==> set image type to 'type'\n"
94 " -C ==> set compression type 'comp'\n"
95 " -a ==> set load address to 'addr' (hex)\n"
96 " -e ==> set entry point to 'ep' (hex)\n"
97 " -n ==> set image name to 'name'\n"
98 " -d ==> use image data from 'datafile'\n"
99 " -x ==> set XIP (execute in place)\n",
102 " %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"
103 " <dtb> file is used with -f auto, it may occur multiple times.\n",
106 " -D => set all options for device tree compiler\n"
107 " -f => input filename for FIT source\n"
108 " -i => input filename for ramdisk file\n"
109 " -E => place data outside of the FIT structure\n"
110 " -B => align size in hex for FIT structure and header\n");
111 #ifdef CONFIG_FIT_SIGNATURE
113 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
114 " -k => set directory containing private keys\n"
115 " -K => write public keys to this .dtb file\n"
116 " -G => use this signing key (in lieu of -k)\n"
117 " -c => add comment in signature node\n"
118 " -F => re-sign existing FIT image\n"
119 " -p => place external data at a static position\n"
120 " -r => mark keys used as 'required' in dtb\n"
121 " -N => openssl engine to use for signing\n");
124 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
126 fprintf(stderr, " %s -V ==> print version information and exit\n",
128 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
133 static int add_content(int type, const char *fname)
135 struct content_info *cont;
137 cont = calloc(1, sizeof(*cont));
142 if (params.content_tail)
143 params.content_tail->next = cont;
145 params.content_head = cont;
146 params.content_tail = cont;
151 static void process_args(int argc, char **argv)
154 int type = IH_TYPE_INVALID;
155 char *datafile = NULL;
158 while ((opt = getopt(argc, argv,
159 "a:A:b:B:c:C:d:D:e:Ef:FG:k:i:K:ln:N:p:o:O:rR:qstT:vVx")) != -1) {
162 params.addr = strtoull(optarg, &ptr, 16);
164 fprintf(stderr, "%s: invalid load address %s\n",
165 params.cmdname, optarg);
170 params.arch = genimg_get_arch_id(optarg);
171 if (params.arch < 0) {
172 show_valid_options(IH_ARCH);
173 usage("Invalid architecture");
177 if (add_content(IH_TYPE_FLATDT, optarg)) {
179 "%s: Out of memory adding content '%s'",
180 params.cmdname, optarg);
185 params.bl_len = strtoull(optarg, &ptr, 16);
187 fprintf(stderr, "%s: invalid block length %s\n",
188 params.cmdname, optarg);
194 params.comment = optarg;
197 params.comp = genimg_get_comp_id(optarg);
198 if (params.comp < 0) {
199 show_valid_options(IH_COMP);
200 usage("Invalid compression type");
204 params.datafile = optarg;
211 params.ep = strtoull(optarg, &ptr, 16);
213 fprintf(stderr, "%s: invalid entry point %s\n",
214 params.cmdname, optarg);
220 params.external_data = true;
224 params.auto_its = !strcmp(datafile, "auto");
228 * The flattened image tree (FIT) format
229 * requires a flattened device tree image type
231 params.type = IH_TYPE_FLATDT;
235 params.keyfile = optarg;
238 params.fit_ramdisk = optarg;
241 params.keydir = optarg;
244 params.keydest = optarg;
250 params.imagename = optarg;
253 params.engine_id = optarg;
256 params.algo_name = optarg;
259 params.os = genimg_get_os_id(optarg);
261 show_valid_options(IH_OS);
262 usage("Invalid operating system");
266 params.external_offset = strtoull(optarg, &ptr, 16);
268 fprintf(stderr, "%s: invalid offset size %s\n",
269 params.cmdname, optarg);
277 params.require_keys = 1;
281 * This entry is for the second configuration
282 * file, if only one is not enough.
284 params.imagename2 = optarg;
290 params.reset_timestamp = 1;
293 if (strcmp(optarg, "list") == 0) {
294 show_valid_options(IH_TYPE);
297 type = genimg_get_type_id(optarg);
299 show_valid_options(IH_TYPE);
300 usage("Invalid image type");
307 printf("mkimage version %s\n", PLAIN_VERSION);
313 usage("Invalid option");
317 /* The last parameter is expected to be the imagefile */
319 params.imagefile = argv[optind];
322 * For auto-generated FIT images we need to know the image type to put
323 * in the FIT, which is separate from the file's image type (which
324 * will always be IH_TYPE_FLATDT in this case).
326 if (params.type == IH_TYPE_FLATDT) {
327 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
328 /* For auto_its, datafile is always 'auto' */
329 if (!params.auto_its)
330 params.datafile = datafile;
331 else if (!params.datafile)
332 usage("Missing data file for auto-FIT (use -d)");
333 } else if (params.lflag || type != IH_TYPE_INVALID) {
334 if (type == IH_TYPE_SCRIPT && !params.datafile)
335 usage("Missing data file for script (use -d)");
339 if (!params.imagefile)
340 usage("Missing output filename");
343 int main(int argc, char **argv)
349 struct image_type_params *tparams = NULL;
354 params.cmdname = *argv;
358 process_args(argc, argv);
360 /* set tparams as per input type_id */
361 tparams = imagetool_get_type(params.type);
362 if (tparams == NULL && !params.lflag) {
363 fprintf (stderr, "%s: unsupported type %s\n",
364 params.cmdname, genimg_get_type_name(params.type));
369 * check the passed arguments parameters meets the requirements
370 * as per image type to be generated/listed
372 if (tparams && tparams->check_params)
373 if (tparams->check_params (¶ms))
374 usage("Bad parameters for image type");
377 params.ep = params.addr;
378 /* If XIP, entry point must be after the U-Boot header */
379 if (params.xflag && tparams)
380 params.ep += tparams->header_size;
385 fprintf(stderr, "%s: Missing FIT support\n",
389 if (tparams->fflag_handle)
391 * in some cases, some additional processing needs
392 * to be done if fflag is defined
394 * For ex. fit_handle_file for Fit file support
396 retval = tparams->fflag_handle(¶ms);
398 if (retval != EXIT_SUCCESS)
399 usage("Bad parameters for FIT image type");
402 if (params.lflag || params.fflag) {
403 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
405 ifd = open (params.imagefile,
406 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
410 fprintf (stderr, "%s: Can't open %s: %s\n",
411 params.cmdname, params.imagefile,
416 if (params.lflag || params.fflag) {
419 * list header information of existing image
421 if (fstat(ifd, &sbuf) < 0) {
422 fprintf (stderr, "%s: Can't stat %s: %s\n",
423 params.cmdname, params.imagefile,
428 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
430 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
431 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
433 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
435 "%s: failed to get size of block device \"%s\"\n",
436 params.cmdname, params.imagefile);
441 "%s: \"%s\" is block device, don't know how to get its size\n",
442 params.cmdname, params.imagefile);
445 } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) {
447 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
448 params.cmdname, params.imagefile,
449 (unsigned long long) sbuf.st_size,
450 tparams->header_size);
456 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
457 if (ptr == MAP_FAILED) {
458 fprintf (stderr, "%s: Can't read %s: %s\n",
459 params.cmdname, params.imagefile,
465 * Verifies the header format based on the expected header for image
466 * type in tparams. If tparams is NULL simply check all image types
467 * to find one that matches our header.
469 retval = imagetool_verify_print_header(ptr, &sbuf, tparams, ¶ms);
471 (void) munmap((void *)ptr, sbuf.st_size);
474 summary_show(¶ms.summary, params.imagefile,
480 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
481 dfd = open(params.datafile, O_RDONLY | O_BINARY);
483 fprintf(stderr, "%s: Can't open %s: %s\n",
484 params.cmdname, params.datafile,
489 if (fstat(dfd, &sbuf) < 0) {
490 fprintf(stderr, "%s: Can't stat %s: %s\n",
491 params.cmdname, params.datafile,
496 params.file_size = sbuf.st_size + tparams->header_size;
501 * In case there an header with a variable
502 * length will be added, the corresponding
503 * function is called. This is responsible to
504 * allocate memory for the header itself.
506 if (tparams->vrec_header)
507 pad_len = tparams->vrec_header(¶ms, tparams);
509 memset(tparams->hdr, 0, tparams->header_size);
511 if (write(ifd, tparams->hdr, tparams->header_size)
512 != tparams->header_size) {
513 fprintf (stderr, "%s: Write error on %s: %s\n",
514 params.cmdname, params.imagefile, strerror(errno));
518 if (!params.skipcpy) {
519 if (params.type == IH_TYPE_MULTI ||
520 params.type == IH_TYPE_SCRIPT) {
521 char *file = params.datafile;
528 if ((sep = strchr(file, ':')) != NULL) {
532 if (stat (file, &sbuf) < 0) {
533 fprintf (stderr, "%s: Can't stat %s: %s\n",
534 params.cmdname, file, strerror(errno));
537 size = cpu_to_uimage (sbuf.st_size);
542 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
543 fprintf (stderr, "%s: Write error on %s: %s\n",
544 params.cmdname, params.imagefile,
561 file = params.datafile;
564 char *sep = strchr(file, ':');
567 copy_file (ifd, file, 1);
571 copy_file (ifd, file, 0);
575 } else if (params.type == IH_TYPE_PBLIMAGE) {
576 /* PBL has special Image format, implements its' own */
577 pbl_load_uboot(ifd, ¶ms);
578 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
579 /* Image file is meta, walk through actual targets */
582 ret = zynqmpbif_copy_image(ifd, ¶ms);
585 } else if (params.type == IH_TYPE_IMX8IMAGE) {
586 /* i.MX8/8X has special Image format */
589 ret = imx8image_copy_image(ifd, ¶ms);
592 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
593 /* i.MX8M has special Image format */
596 ret = imx8mimage_copy_image(ifd, ¶ms);
599 } else if ((params.type == IH_TYPE_RKSD) ||
600 (params.type == IH_TYPE_RKSPI)) {
601 /* Rockchip has special Image format */
604 ret = rockchip_copy_image(ifd, ¶ms);
608 copy_file(ifd, params.datafile, pad_len);
610 if (params.type == IH_TYPE_FIRMWARE_IVT) {
611 /* Add alignment and IVT */
612 uint32_t aligned_filesize = ALIGN(params.file_size,
614 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
615 params.addr, 0, 0, 0, params.addr
617 - tparams->header_size,
618 params.addr + aligned_filesize
619 - tparams->header_size
621 int i = params.file_size;
622 for (; i < aligned_filesize; i++) {
623 if (write(ifd, (char *) &i, 1) != 1) {
625 "%s: Write error on %s: %s\n",
632 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
633 != sizeof(flash_header_v2_t)) {
634 fprintf(stderr, "%s: Write error on %s: %s\n",
643 /* We're a bit of paranoid */
644 #if defined(_POSIX_SYNCHRONIZED_IO) && \
645 !defined(__sun__) && \
646 !defined(__FreeBSD__) && \
647 !defined(__OpenBSD__) && \
649 (void) fdatasync (ifd);
654 if (fstat(ifd, &sbuf) < 0) {
655 fprintf (stderr, "%s: Can't stat %s: %s\n",
656 params.cmdname, params.imagefile, strerror(errno));
659 params.file_size = sbuf.st_size;
661 map_len = sbuf.st_size;
662 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
663 if (ptr == MAP_FAILED) {
664 fprintf (stderr, "%s: Can't map %s: %s\n",
665 params.cmdname, params.imagefile, strerror(errno));
669 /* Setup the image header as per input image type*/
670 if (tparams->set_header)
671 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
673 fprintf (stderr, "%s: Can't set header for %s: %s\n",
674 params.cmdname, tparams->name, strerror(errno));
678 /* Print the image information by processing image header */
679 if (tparams->print_header)
680 tparams->print_header (ptr);
682 fprintf (stderr, "%s: Can't print header for %s\n",
683 params.cmdname, tparams->name);
686 (void)munmap((void *)ptr, map_len);
688 /* We're a bit of paranoid */
689 #if defined(_POSIX_SYNCHRONIZED_IO) && \
690 !defined(__sun__) && \
691 !defined(__FreeBSD__) && \
692 !defined(__OpenBSD__) && \
694 (void) fdatasync (ifd);
700 fprintf (stderr, "%s: Write error on %s: %s\n",
701 params.cmdname, params.imagefile, strerror(errno));
709 copy_file (int ifd, const char *datafile, int pad)
719 struct image_type_params *tparams = imagetool_get_type(params.type);
721 memset(zeros, 0, sizeof(zeros));
724 fprintf (stderr, "Adding Image %s\n", datafile);
727 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
728 fprintf (stderr, "%s: Can't open %s: %s\n",
729 params.cmdname, datafile, strerror(errno));
733 if (fstat(dfd, &sbuf) < 0) {
734 fprintf (stderr, "%s: Can't stat %s: %s\n",
735 params.cmdname, datafile, strerror(errno));
739 if (sbuf.st_size == 0) {
740 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
741 params.cmdname, datafile);
745 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
746 if (ptr == MAP_FAILED) {
747 fprintf (stderr, "%s: Can't read %s: %s\n",
748 params.cmdname, datafile, strerror(errno));
753 unsigned char *p = NULL;
755 * XIP: do not append the image_header_t at the
756 * beginning of the file, but consume the space
760 if ((unsigned)sbuf.st_size < tparams->header_size) {
762 "%s: Bad size: \"%s\" is too small for XIP\n",
763 params.cmdname, datafile);
767 for (p = ptr; p < ptr + tparams->header_size; p++) {
770 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
771 params.cmdname, datafile);
776 offset = tparams->header_size;
779 size = sbuf.st_size - offset;
781 ret = write(ifd, ptr + offset, size);
784 fprintf (stderr, "%s: Write error on %s: %s\n",
785 params.cmdname, params.imagefile, strerror(errno));
787 fprintf (stderr, "%s: Write only %d/%d bytes, "\
788 "probably no space left on the device\n",
789 params.cmdname, ret, size);
794 if ((pad == 1) && (tail != 0)) {
796 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
797 fprintf (stderr, "%s: Write error on %s: %s\n",
798 params.cmdname, params.imagefile,
802 } else if (pad > 1) {
804 int todo = sizeof(zeros);
808 if (write(ifd, (char *)&zeros, todo) != todo) {
809 fprintf(stderr, "%s: Write error on %s: %s\n",
810 params.cmdname, params.imagefile,
818 (void) munmap((void *)ptr, sbuf.st_size);