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");
178 if (add_content(IH_TYPE_FLATDT, optarg)) {
180 "%s: Out of memory adding content '%s'",
181 params.cmdname, optarg);
186 params.bl_len = strtoull(optarg, &ptr, 16);
188 fprintf(stderr, "%s: invalid block length %s\n",
189 params.cmdname, optarg);
195 params.comment = optarg;
198 params.comp = genimg_get_comp_id(optarg);
199 if (params.comp < 0) {
200 show_valid_options(IH_COMP);
201 usage("Invalid compression type");
205 params.datafile = optarg;
212 params.ep = strtoull(optarg, &ptr, 16);
214 fprintf(stderr, "%s: invalid entry point %s\n",
215 params.cmdname, optarg);
221 params.external_data = true;
225 params.auto_its = !strcmp(datafile, "auto");
229 * The flattened image tree (FIT) format
230 * requires a flattened device tree image type
232 params.type = IH_TYPE_FLATDT;
236 params.keyfile = optarg;
239 params.fit_ramdisk = optarg;
242 params.keydir = optarg;
245 params.keydest = optarg;
251 params.imagename = optarg;
254 params.engine_id = optarg;
257 params.algo_name = optarg;
260 params.os = genimg_get_os_id(optarg);
262 show_valid_options(IH_OS);
263 usage("Invalid operating system");
267 params.external_offset = strtoull(optarg, &ptr, 16);
269 fprintf(stderr, "%s: invalid offset size %s\n",
270 params.cmdname, optarg);
278 params.require_keys = 1;
282 * This entry is for the second configuration
283 * file, if only one is not enough.
285 params.imagename2 = optarg;
291 params.reset_timestamp = 1;
294 if (strcmp(optarg, "list") == 0) {
295 show_valid_options(IH_TYPE);
298 type = genimg_get_type_id(optarg);
300 show_valid_options(IH_TYPE);
301 usage("Invalid image type");
308 printf("mkimage version %s\n", PLAIN_VERSION);
314 usage("Invalid option");
318 /* The last parameter is expected to be the imagefile */
320 params.imagefile = argv[optind];
323 * For auto-generated FIT images we need to know the image type to put
324 * in the FIT, which is separate from the file's image type (which
325 * will always be IH_TYPE_FLATDT in this case).
327 if (params.type == IH_TYPE_FLATDT) {
328 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
329 /* For auto_its, datafile is always 'auto' */
330 if (!params.auto_its)
331 params.datafile = datafile;
332 else if (!params.datafile)
333 usage("Missing data file for auto-FIT (use -d)");
334 } else if (params.lflag || type != IH_TYPE_INVALID) {
335 if (type == IH_TYPE_SCRIPT && !params.datafile)
336 usage("Missing data file for script (use -d)");
340 if (!params.imagefile)
341 usage("Missing output filename");
344 static void verify_image(const struct image_type_params *tparams)
350 ifd = open(params.imagefile, O_RDONLY | O_BINARY);
352 fprintf(stderr, "%s: Can't open %s: %s\n",
353 params.cmdname, params.imagefile,
358 if (fstat(ifd, &sbuf) < 0) {
359 fprintf(stderr, "%s: Can't stat %s: %s\n",
360 params.cmdname, params.imagefile, strerror(errno));
363 params.file_size = sbuf.st_size;
365 ptr = mmap(0, params.file_size, PROT_READ, MAP_SHARED, ifd, 0);
366 if (ptr == MAP_FAILED) {
367 fprintf(stderr, "%s: Can't map %s: %s\n",
368 params.cmdname, params.imagefile, strerror(errno));
372 if (tparams->verify_header((unsigned char *)ptr, params.file_size, ¶ms) != 0) {
373 fprintf(stderr, "%s: Failed to verify header of %s\n",
374 params.cmdname, params.imagefile);
378 (void)munmap(ptr, params.file_size);
382 int main(int argc, char **argv)
388 struct image_type_params *tparams = NULL;
393 params.cmdname = *argv;
397 process_args(argc, argv);
399 /* set tparams as per input type_id */
400 tparams = imagetool_get_type(params.type);
401 if (tparams == NULL && !params.lflag) {
402 fprintf (stderr, "%s: unsupported type %s\n",
403 params.cmdname, genimg_get_type_name(params.type));
408 * check the passed arguments parameters meets the requirements
409 * as per image type to be generated/listed
411 if (tparams && tparams->check_params)
412 if (tparams->check_params (¶ms))
413 usage("Bad parameters for image type");
416 params.ep = params.addr;
417 /* If XIP, entry point must be after the U-Boot header */
418 if (params.xflag && tparams)
419 params.ep += tparams->header_size;
424 fprintf(stderr, "%s: Missing FIT support\n",
428 if (tparams->fflag_handle)
430 * in some cases, some additional processing needs
431 * to be done if fflag is defined
433 * For ex. fit_handle_file for Fit file support
435 retval = tparams->fflag_handle(¶ms);
437 if (retval != EXIT_SUCCESS)
438 usage("Bad parameters for FIT image type");
441 if (params.lflag || params.fflag) {
442 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
444 ifd = open (params.imagefile,
445 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
449 fprintf (stderr, "%s: Can't open %s: %s\n",
450 params.cmdname, params.imagefile,
455 if (params.lflag || params.fflag) {
458 * list header information of existing image
460 if (fstat(ifd, &sbuf) < 0) {
461 fprintf (stderr, "%s: Can't stat %s: %s\n",
462 params.cmdname, params.imagefile,
467 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
469 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
470 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
472 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
474 "%s: failed to get size of block device \"%s\"\n",
475 params.cmdname, params.imagefile);
480 "%s: \"%s\" is block device, don't know how to get its size\n",
481 params.cmdname, params.imagefile);
484 } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) {
486 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
487 params.cmdname, params.imagefile,
488 (unsigned long long) sbuf.st_size,
489 tparams->header_size);
495 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
496 if (ptr == MAP_FAILED) {
497 fprintf (stderr, "%s: Can't read %s: %s\n",
498 params.cmdname, params.imagefile,
504 * Verifies the header format based on the expected header for image
505 * type in tparams. If tparams is NULL simply check all image types
506 * to find one that matches our header.
508 retval = imagetool_verify_print_header(ptr, &sbuf, tparams, ¶ms);
510 (void) munmap((void *)ptr, sbuf.st_size);
513 summary_show(¶ms.summary, params.imagefile,
519 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
520 dfd = open(params.datafile, O_RDONLY | O_BINARY);
522 fprintf(stderr, "%s: Can't open %s: %s\n",
523 params.cmdname, params.datafile,
528 if (fstat(dfd, &sbuf) < 0) {
529 fprintf(stderr, "%s: Can't stat %s: %s\n",
530 params.cmdname, params.datafile,
535 params.file_size = sbuf.st_size + tparams->header_size;
540 * In case there an header with a variable
541 * length will be added, the corresponding
542 * function is called. This is responsible to
543 * allocate memory for the header itself.
545 if (tparams->vrec_header)
546 pad_len = tparams->vrec_header(¶ms, tparams);
548 memset(tparams->hdr, 0, tparams->header_size);
550 if (write(ifd, tparams->hdr, tparams->header_size)
551 != tparams->header_size) {
552 fprintf (stderr, "%s: Write error on %s: %s\n",
553 params.cmdname, params.imagefile, strerror(errno));
557 if (!params.skipcpy) {
558 if (params.type == IH_TYPE_MULTI ||
559 params.type == IH_TYPE_SCRIPT) {
560 char *file = params.datafile;
567 if ((sep = strchr(file, ':')) != NULL) {
571 if (stat (file, &sbuf) < 0) {
572 fprintf (stderr, "%s: Can't stat %s: %s\n",
573 params.cmdname, file, strerror(errno));
576 size = cpu_to_uimage (sbuf.st_size);
581 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
582 fprintf (stderr, "%s: Write error on %s: %s\n",
583 params.cmdname, params.imagefile,
600 file = params.datafile;
603 char *sep = strchr(file, ':');
606 copy_file (ifd, file, 1);
610 copy_file (ifd, file, 0);
614 } else if (params.type == IH_TYPE_PBLIMAGE) {
615 /* PBL has special Image format, implements its' own */
616 pbl_load_uboot(ifd, ¶ms);
617 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
618 /* Image file is meta, walk through actual targets */
621 ret = zynqmpbif_copy_image(ifd, ¶ms);
624 } else if (params.type == IH_TYPE_IMX8IMAGE) {
625 /* i.MX8/8X has special Image format */
628 ret = imx8image_copy_image(ifd, ¶ms);
631 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
632 /* i.MX8M has special Image format */
635 ret = imx8mimage_copy_image(ifd, ¶ms);
638 } else if ((params.type == IH_TYPE_RKSD) ||
639 (params.type == IH_TYPE_RKSPI)) {
640 /* Rockchip has special Image format */
643 ret = rockchip_copy_image(ifd, ¶ms);
647 copy_file(ifd, params.datafile, pad_len);
649 if (params.type == IH_TYPE_FIRMWARE_IVT) {
650 /* Add alignment and IVT */
651 uint32_t aligned_filesize = ALIGN(params.file_size,
653 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
654 params.addr, 0, 0, 0, params.addr
656 - tparams->header_size,
657 params.addr + aligned_filesize
658 - tparams->header_size
660 int i = params.file_size;
661 for (; i < aligned_filesize; i++) {
662 if (write(ifd, (char *) &i, 1) != 1) {
664 "%s: Write error on %s: %s\n",
671 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
672 != sizeof(flash_header_v2_t)) {
673 fprintf(stderr, "%s: Write error on %s: %s\n",
682 /* We're a bit of paranoid */
683 #if defined(_POSIX_SYNCHRONIZED_IO) && \
684 !defined(__sun__) && \
685 !defined(__FreeBSD__) && \
686 !defined(__OpenBSD__) && \
688 (void) fdatasync (ifd);
693 if (fstat(ifd, &sbuf) < 0) {
694 fprintf (stderr, "%s: Can't stat %s: %s\n",
695 params.cmdname, params.imagefile, strerror(errno));
698 params.file_size = sbuf.st_size;
700 map_len = sbuf.st_size;
701 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
702 if (ptr == MAP_FAILED) {
703 fprintf (stderr, "%s: Can't map %s: %s\n",
704 params.cmdname, params.imagefile, strerror(errno));
708 /* Setup the image header as per input image type*/
709 if (tparams->set_header)
710 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
712 fprintf (stderr, "%s: Can't set header for %s: %s\n",
713 params.cmdname, tparams->name, strerror(errno));
717 /* Print the image information by processing image header */
718 if (tparams->print_header)
719 tparams->print_header (ptr);
721 fprintf (stderr, "%s: Can't print header for %s\n",
722 params.cmdname, tparams->name);
725 (void)munmap((void *)ptr, map_len);
727 /* We're a bit of paranoid */
728 #if defined(_POSIX_SYNCHRONIZED_IO) && \
729 !defined(__sun__) && \
730 !defined(__FreeBSD__) && \
731 !defined(__OpenBSD__) && \
733 (void) fdatasync (ifd);
739 fprintf (stderr, "%s: Write error on %s: %s\n",
740 params.cmdname, params.imagefile, strerror(errno));
744 if (tparams->verify_header)
745 verify_image(tparams);
751 copy_file (int ifd, const char *datafile, int pad)
761 struct image_type_params *tparams = imagetool_get_type(params.type);
763 memset(zeros, 0, sizeof(zeros));
766 fprintf (stderr, "Adding Image %s\n", datafile);
769 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
770 fprintf (stderr, "%s: Can't open %s: %s\n",
771 params.cmdname, datafile, strerror(errno));
775 if (fstat(dfd, &sbuf) < 0) {
776 fprintf (stderr, "%s: Can't stat %s: %s\n",
777 params.cmdname, datafile, strerror(errno));
781 if (sbuf.st_size == 0) {
782 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
783 params.cmdname, datafile);
787 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
788 if (ptr == MAP_FAILED) {
789 fprintf (stderr, "%s: Can't read %s: %s\n",
790 params.cmdname, datafile, strerror(errno));
795 unsigned char *p = NULL;
797 * XIP: do not append the image_header_t at the
798 * beginning of the file, but consume the space
802 if ((unsigned)sbuf.st_size < tparams->header_size) {
804 "%s: Bad size: \"%s\" is too small for XIP\n",
805 params.cmdname, datafile);
809 for (p = ptr; p < ptr + tparams->header_size; p++) {
812 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
813 params.cmdname, datafile);
818 offset = tparams->header_size;
821 size = sbuf.st_size - offset;
823 ret = write(ifd, ptr + offset, size);
826 fprintf (stderr, "%s: Write error on %s: %s\n",
827 params.cmdname, params.imagefile, strerror(errno));
829 fprintf (stderr, "%s: Write only %d/%d bytes, "\
830 "probably no space left on the device\n",
831 params.cmdname, ret, size);
836 if ((pad == 1) && (tail != 0)) {
838 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
839 fprintf (stderr, "%s: Write error on %s: %s\n",
840 params.cmdname, params.imagefile,
844 } else if (pad > 1) {
846 int todo = sizeof(zeros);
850 if (write(ifd, (char *)&zeros, todo) != todo) {
851 fprintf(stderr, "%s: Write error on %s: %s\n",
852 params.cmdname, params.imagefile,
860 (void) munmap((void *)ptr, sbuf.st_size);