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"
91 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
92 " -A ==> set architecture to 'arch'\n"
93 " -O ==> set operating system to 'os'\n"
94 " -T ==> set image type to 'type'\n"
95 " -C ==> set compression type 'comp'\n"
96 " -a ==> set load address to 'addr' (hex)\n"
97 " -e ==> set entry point to 'ep' (hex)\n"
98 " -n ==> set image name to 'name'\n"
99 " -R ==> set second image name to 'name'\n"
100 " -d ==> use image data from 'datafile'\n"
101 " -x ==> set XIP (execute in place)\n"
102 " -s ==> create an image with no data\n"
106 " %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"
107 " <dtb> file is used with -f auto, it may occur multiple times.\n",
110 " -D => set all options for device tree compiler\n"
111 " -f => input filename for FIT source\n"
112 " -i => input filename for ramdisk file\n"
113 " -E => place data outside of the FIT structure\n"
114 " -B => align size in hex for FIT structure and header\n"
115 " -b => append the device tree binary to the FIT\n"
116 " -t => update the timestamp in the FIT\n");
117 #ifdef CONFIG_FIT_SIGNATURE
119 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
120 " -k => set directory containing private keys\n"
121 " -K => write public keys to this .dtb file\n"
122 " -G => use this signing key (in lieu of -k)\n"
123 " -c => add comment in signature node\n"
124 " -F => re-sign existing FIT image\n"
125 " -p => place external data at a static position\n"
126 " -r => mark keys used as 'required' in dtb\n"
127 " -N => openssl engine to use for signing\n"
128 " -o => algorithm to use for signing\n");
131 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
133 fprintf(stderr, " %s -V ==> print version information and exit\n",
135 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
140 static int add_content(int type, const char *fname)
142 struct content_info *cont;
144 cont = calloc(1, sizeof(*cont));
149 if (params.content_tail)
150 params.content_tail->next = cont;
152 params.content_head = cont;
153 params.content_tail = cont;
158 static void process_args(int argc, char **argv)
161 int type = IH_TYPE_INVALID;
162 char *datafile = NULL;
165 while ((opt = getopt(argc, argv,
166 "a:A:b:B:c:C:d:D:e:Ef:FG:k:i:K:ln:N:p:o:O:rR:qstT:vVx")) != -1) {
169 params.addr = strtoull(optarg, &ptr, 16);
171 fprintf(stderr, "%s: invalid load address %s\n",
172 params.cmdname, optarg);
177 params.arch = genimg_get_arch_id(optarg);
178 if (params.arch < 0) {
179 show_valid_options(IH_ARCH);
180 usage("Invalid architecture");
185 if (add_content(IH_TYPE_FLATDT, optarg)) {
187 "%s: Out of memory adding content '%s'",
188 params.cmdname, optarg);
193 params.bl_len = strtoull(optarg, &ptr, 16);
195 fprintf(stderr, "%s: invalid block length %s\n",
196 params.cmdname, optarg);
202 params.comment = optarg;
205 params.comp = genimg_get_comp_id(optarg);
206 if (params.comp < 0) {
207 show_valid_options(IH_COMP);
208 usage("Invalid compression type");
212 params.datafile = optarg;
219 params.ep = strtoull(optarg, &ptr, 16);
221 fprintf(stderr, "%s: invalid entry point %s\n",
222 params.cmdname, optarg);
228 params.external_data = true;
232 params.auto_its = !strcmp(datafile, "auto");
236 * The flattened image tree (FIT) format
237 * requires a flattened device tree image type
239 params.type = IH_TYPE_FLATDT;
243 params.keyfile = optarg;
246 params.fit_ramdisk = optarg;
249 params.keydir = optarg;
252 params.keydest = optarg;
258 params.imagename = optarg;
261 params.engine_id = optarg;
264 params.algo_name = optarg;
267 params.os = genimg_get_os_id(optarg);
269 show_valid_options(IH_OS);
270 usage("Invalid operating system");
274 params.external_offset = strtoull(optarg, &ptr, 16);
276 fprintf(stderr, "%s: invalid offset size %s\n",
277 params.cmdname, optarg);
285 params.require_keys = 1;
289 * This entry is for the second configuration
290 * file, if only one is not enough.
292 params.imagename2 = optarg;
298 params.reset_timestamp = 1;
301 if (strcmp(optarg, "list") == 0) {
302 show_valid_options(IH_TYPE);
305 type = genimg_get_type_id(optarg);
307 show_valid_options(IH_TYPE);
308 usage("Invalid image type");
315 printf("mkimage version %s\n", PLAIN_VERSION);
321 usage("Invalid option");
325 /* The last parameter is expected to be the imagefile */
327 params.imagefile = argv[optind];
330 * For auto-generated FIT images we need to know the image type to put
331 * in the FIT, which is separate from the file's image type (which
332 * will always be IH_TYPE_FLATDT in this case).
334 if (params.type == IH_TYPE_FLATDT) {
335 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
336 /* For auto_its, datafile is always 'auto' */
337 if (!params.auto_its)
338 params.datafile = datafile;
339 else if (!params.datafile)
340 usage("Missing data file for auto-FIT (use -d)");
341 } else if (params.lflag || type != IH_TYPE_INVALID) {
342 if (type == IH_TYPE_SCRIPT && !params.datafile)
343 usage("Missing data file for script (use -d)");
347 if (!params.imagefile)
348 usage("Missing output filename");
351 static void verify_image(const struct image_type_params *tparams)
357 ifd = open(params.imagefile, O_RDONLY | O_BINARY);
359 fprintf(stderr, "%s: Can't open %s: %s\n",
360 params.cmdname, params.imagefile,
365 if (fstat(ifd, &sbuf) < 0) {
366 fprintf(stderr, "%s: Can't stat %s: %s\n",
367 params.cmdname, params.imagefile, strerror(errno));
370 params.file_size = sbuf.st_size;
372 ptr = mmap(0, params.file_size, PROT_READ, MAP_SHARED, ifd, 0);
373 if (ptr == MAP_FAILED) {
374 fprintf(stderr, "%s: Can't map %s: %s\n",
375 params.cmdname, params.imagefile, strerror(errno));
379 if (tparams->verify_header((unsigned char *)ptr, params.file_size, ¶ms) != 0) {
380 fprintf(stderr, "%s: Failed to verify header of %s\n",
381 params.cmdname, params.imagefile);
385 (void)munmap(ptr, params.file_size);
389 int main(int argc, char **argv)
395 struct image_type_params *tparams = NULL;
400 params.cmdname = *argv;
404 process_args(argc, argv);
406 /* set tparams as per input type_id */
407 tparams = imagetool_get_type(params.type);
408 if (tparams == NULL && !params.lflag) {
409 fprintf (stderr, "%s: unsupported type %s\n",
410 params.cmdname, genimg_get_type_name(params.type));
415 * check the passed arguments parameters meets the requirements
416 * as per image type to be generated/listed
418 if (tparams && tparams->check_params)
419 if (tparams->check_params (¶ms))
420 usage("Bad parameters for image type");
423 params.ep = params.addr;
424 /* If XIP, entry point must be after the U-Boot header */
425 if (params.xflag && tparams)
426 params.ep += tparams->header_size;
431 fprintf(stderr, "%s: Missing FIT support\n",
435 if (tparams->fflag_handle)
437 * in some cases, some additional processing needs
438 * to be done if fflag is defined
440 * For ex. fit_handle_file for Fit file support
442 retval = tparams->fflag_handle(¶ms);
444 if (retval != EXIT_SUCCESS)
445 usage("Bad parameters for FIT image type");
448 if (params.lflag || params.fflag) {
449 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
451 ifd = open (params.imagefile,
452 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
456 fprintf (stderr, "%s: Can't open %s: %s\n",
457 params.cmdname, params.imagefile,
462 if (params.lflag || params.fflag) {
465 * list header information of existing image
467 if (fstat(ifd, &sbuf) < 0) {
468 fprintf (stderr, "%s: Can't stat %s: %s\n",
469 params.cmdname, params.imagefile,
474 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
476 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
477 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
479 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
481 "%s: failed to get size of block device \"%s\"\n",
482 params.cmdname, params.imagefile);
487 "%s: \"%s\" is block device, don't know how to get its size\n",
488 params.cmdname, params.imagefile);
491 } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) {
493 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
494 params.cmdname, params.imagefile,
495 (unsigned long long) sbuf.st_size,
496 tparams->header_size);
502 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
503 if (ptr == MAP_FAILED) {
504 fprintf (stderr, "%s: Can't read %s: %s\n",
505 params.cmdname, params.imagefile,
511 * Verifies the header format based on the expected header for image
512 * type in tparams. If tparams is NULL simply check all image types
513 * to find one that matches our header.
515 retval = imagetool_verify_print_header(ptr, &sbuf, tparams, ¶ms);
517 (void) munmap((void *)ptr, sbuf.st_size);
520 summary_show(¶ms.summary, params.imagefile,
526 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
527 dfd = open(params.datafile, O_RDONLY | O_BINARY);
529 fprintf(stderr, "%s: Can't open %s: %s\n",
530 params.cmdname, params.datafile,
535 if (fstat(dfd, &sbuf) < 0) {
536 fprintf(stderr, "%s: Can't stat %s: %s\n",
537 params.cmdname, params.datafile,
542 params.file_size = sbuf.st_size + tparams->header_size;
547 * In case there an header with a variable
548 * length will be added, the corresponding
549 * function is called. This is responsible to
550 * allocate memory for the header itself.
552 if (tparams->vrec_header)
553 pad_len = tparams->vrec_header(¶ms, tparams);
555 memset(tparams->hdr, 0, tparams->header_size);
557 if (write(ifd, tparams->hdr, tparams->header_size)
558 != tparams->header_size) {
559 fprintf (stderr, "%s: Write error on %s: %s\n",
560 params.cmdname, params.imagefile, strerror(errno));
564 if (!params.skipcpy) {
565 if (params.type == IH_TYPE_MULTI ||
566 params.type == IH_TYPE_SCRIPT) {
567 char *file = params.datafile;
574 if ((sep = strchr(file, ':')) != NULL) {
578 if (stat (file, &sbuf) < 0) {
579 fprintf (stderr, "%s: Can't stat %s: %s\n",
580 params.cmdname, file, strerror(errno));
583 size = cpu_to_uimage (sbuf.st_size);
588 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
589 fprintf (stderr, "%s: Write error on %s: %s\n",
590 params.cmdname, params.imagefile,
607 file = params.datafile;
610 char *sep = strchr(file, ':');
613 copy_file (ifd, file, 1);
617 copy_file (ifd, file, 0);
621 } else if (params.type == IH_TYPE_PBLIMAGE) {
622 /* PBL has special Image format, implements its' own */
623 pbl_load_uboot(ifd, ¶ms);
624 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
625 /* Image file is meta, walk through actual targets */
628 ret = zynqmpbif_copy_image(ifd, ¶ms);
631 } else if (params.type == IH_TYPE_IMX8IMAGE) {
632 /* i.MX8/8X has special Image format */
635 ret = imx8image_copy_image(ifd, ¶ms);
638 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
639 /* i.MX8M has special Image format */
642 ret = imx8mimage_copy_image(ifd, ¶ms);
645 } else if ((params.type == IH_TYPE_RKSD) ||
646 (params.type == IH_TYPE_RKSPI)) {
647 /* Rockchip has special Image format */
650 ret = rockchip_copy_image(ifd, ¶ms);
654 copy_file(ifd, params.datafile, pad_len);
656 if (params.type == IH_TYPE_FIRMWARE_IVT) {
657 /* Add alignment and IVT */
658 uint32_t aligned_filesize = ALIGN(params.file_size,
660 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
661 params.addr, 0, 0, 0, params.addr
663 - tparams->header_size,
664 params.addr + aligned_filesize
665 - tparams->header_size
667 int i = params.file_size;
668 for (; i < aligned_filesize; i++) {
669 if (write(ifd, (char *) &i, 1) != 1) {
671 "%s: Write error on %s: %s\n",
678 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
679 != sizeof(flash_header_v2_t)) {
680 fprintf(stderr, "%s: Write error on %s: %s\n",
689 /* We're a bit of paranoid */
690 #if defined(_POSIX_SYNCHRONIZED_IO) && \
691 !defined(__sun__) && \
692 !defined(__FreeBSD__) && \
693 !defined(__OpenBSD__) && \
695 (void) fdatasync (ifd);
700 if (fstat(ifd, &sbuf) < 0) {
701 fprintf (stderr, "%s: Can't stat %s: %s\n",
702 params.cmdname, params.imagefile, strerror(errno));
705 params.file_size = sbuf.st_size;
707 map_len = sbuf.st_size;
708 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
709 if (ptr == MAP_FAILED) {
710 fprintf (stderr, "%s: Can't map %s: %s\n",
711 params.cmdname, params.imagefile, strerror(errno));
715 /* Setup the image header as per input image type*/
716 if (tparams->set_header)
717 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
719 fprintf (stderr, "%s: Can't set header for %s: %s\n",
720 params.cmdname, tparams->name, strerror(errno));
724 /* Print the image information by processing image header */
725 if (tparams->print_header)
726 tparams->print_header (ptr);
728 fprintf (stderr, "%s: Can't print header for %s\n",
729 params.cmdname, tparams->name);
732 (void)munmap((void *)ptr, map_len);
734 /* We're a bit of paranoid */
735 #if defined(_POSIX_SYNCHRONIZED_IO) && \
736 !defined(__sun__) && \
737 !defined(__FreeBSD__) && \
738 !defined(__OpenBSD__) && \
740 (void) fdatasync (ifd);
746 fprintf (stderr, "%s: Write error on %s: %s\n",
747 params.cmdname, params.imagefile, strerror(errno));
751 if (tparams->verify_header)
752 verify_image(tparams);
758 copy_file (int ifd, const char *datafile, int pad)
768 struct image_type_params *tparams = imagetool_get_type(params.type);
770 memset(zeros, 0, sizeof(zeros));
773 fprintf (stderr, "Adding Image %s\n", datafile);
776 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
777 fprintf (stderr, "%s: Can't open %s: %s\n",
778 params.cmdname, datafile, strerror(errno));
782 if (fstat(dfd, &sbuf) < 0) {
783 fprintf (stderr, "%s: Can't stat %s: %s\n",
784 params.cmdname, datafile, strerror(errno));
788 if (sbuf.st_size == 0) {
789 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
790 params.cmdname, datafile);
794 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
795 if (ptr == MAP_FAILED) {
796 fprintf (stderr, "%s: Can't read %s: %s\n",
797 params.cmdname, datafile, strerror(errno));
802 unsigned char *p = NULL;
804 * XIP: do not append the image_header_t at the
805 * beginning of the file, but consume the space
809 if ((unsigned)sbuf.st_size < tparams->header_size) {
811 "%s: Bad size: \"%s\" is too small for XIP\n",
812 params.cmdname, datafile);
816 for (p = ptr; p < ptr + tparams->header_size; p++) {
819 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
820 params.cmdname, datafile);
825 offset = tparams->header_size;
828 size = sbuf.st_size - offset;
830 ret = write(ifd, ptr + offset, size);
833 fprintf (stderr, "%s: Write error on %s: %s\n",
834 params.cmdname, params.imagefile, strerror(errno));
836 fprintf (stderr, "%s: Write only %d/%d bytes, "\
837 "probably no space left on the device\n",
838 params.cmdname, ret, size);
843 if ((pad == 1) && (tail != 0)) {
845 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
846 fprintf (stderr, "%s: Write error on %s: %s\n",
847 params.cmdname, params.imagefile,
851 } else if (pad > 1) {
853 int todo = sizeof(zeros);
857 if (write(ifd, (char *)&zeros, todo) != todo) {
858 fprintf(stderr, "%s: Write error on %s: %s\n",
859 params.cmdname, params.imagefile,
867 (void) munmap((void *)ptr, sbuf.st_size);