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>
18 #include <sys/ioctl.h>
21 static void copy_file(int, const char *, int);
23 /* parameters initialized by core will be used by the image type code */
24 static struct image_tool_params params = {
27 .type = IH_TYPE_KERNEL,
29 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
34 static enum ih_category cur_category;
36 static int h_compare_category_name(const void *vtype1, const void *vtype2)
38 const int *type1 = vtype1;
39 const int *type2 = vtype2;
40 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
41 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
43 return strcmp(name1, name2);
46 static int show_valid_options(enum ih_category category)
53 count = genimg_get_cat_count(category);
54 order = calloc(count, sizeof(*order));
58 /* Sort the names in order of short name for easier reading */
59 for (i = 0, item = 0; i < count; i++, item++) {
60 while (!genimg_cat_has_id(category, item) && i < count) {
66 cur_category = category;
67 qsort(order, count, sizeof(int), h_compare_category_name);
69 fprintf(stderr, "\nInvalid %s, supported are:\n",
70 genimg_get_cat_desc(category));
71 for (i = 0; i < count; i++) {
73 fprintf(stderr, "\t%-15s %s\n",
74 genimg_get_cat_short_name(category, item),
75 genimg_get_cat_name(category, item));
77 fprintf(stderr, "\n");
83 static void usage(const char *msg)
85 fprintf(stderr, "Error: %s\n", msg);
86 fprintf(stderr, "Usage: %s [-T type] -l image\n"
87 " -l ==> list image header information\n"
88 " -T ==> parse image file as 'type'\n"
92 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
93 " -A ==> set architecture to 'arch'\n"
94 " -O ==> set operating system to 'os'\n"
95 " -T ==> set image type to 'type'\n"
96 " -C ==> set compression type 'comp'\n"
97 " -a ==> set load address to 'addr' (hex)\n"
98 " -e ==> set entry point to 'ep' (hex)\n"
99 " -n ==> set image name to 'name'\n"
100 " -R ==> set second image name to 'name'\n"
101 " -d ==> use image data from 'datafile'\n"
102 " -x ==> set XIP (execute in place)\n"
103 " -s ==> create an image with no data\n"
107 " %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"
108 " <dtb> file is used with -f auto, it may occur multiple times.\n",
111 " -D => set all options for device tree compiler\n"
112 " -f => input filename for FIT source\n"
113 " -i => input filename for ramdisk file\n"
114 " -E => place data outside of the FIT structure\n"
115 " -B => align size in hex for FIT structure and header\n"
116 " -b => append the device tree binary to the FIT\n"
117 " -t => update the timestamp in the FIT\n");
118 #ifdef CONFIG_FIT_SIGNATURE
120 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
121 " -k => set directory containing private keys\n"
122 " -K => write public keys to this .dtb file\n"
123 " -g => set key name hint\n"
124 " -G => use this signing key (in lieu of -k)\n"
125 " -c => add comment in signature node\n"
126 " -F => re-sign existing FIT image\n"
127 " -p => place external data at a static position\n"
128 " -r => mark keys used as 'required' in dtb\n"
129 " -N => openssl engine to use for signing\n"
130 " -o => algorithm to use for signing\n");
133 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
135 fprintf(stderr, " %s -V ==> print version information and exit\n",
137 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
138 fprintf(stderr, "Long options are available; read the man page for details\n");
143 static int add_content(int type, const char *fname)
145 struct content_info *cont;
147 cont = calloc(1, sizeof(*cont));
152 if (params.content_tail)
153 params.content_tail->next = cont;
155 params.content_head = cont;
156 params.content_tail = cont;
161 static const char optstring[] =
162 "a:A:b:B:c:C:d:D:e:Ef:Fg:G:i:k:K:ln:N:o:O:p:qrR:stT:vVx";
164 static const struct option longopts[] = {
165 { "load-address", required_argument, NULL, 'a' },
166 { "architecture", required_argument, NULL, 'A' },
167 { "device-tree", required_argument, NULL, 'b' },
168 { "alignment", required_argument, NULL, 'B' },
169 { "comment", required_argument, NULL, 'c' },
170 { "compression", required_argument, NULL, 'C' },
171 { "image", required_argument, NULL, 'd' },
172 { "dtcopts", required_argument, NULL, 'D' },
173 { "entry-point", required_argument, NULL, 'e' },
174 { "external", no_argument, NULL, 'E' },
175 { "fit", required_argument, NULL, 'f' },
176 { "update", no_argument, NULL, 'F' },
177 { "key-name-hint", required_argument, NULL, 'g' },
178 { "key-file", required_argument, NULL, 'G' },
179 { "help", no_argument, NULL, 'h' },
180 { "initramfs", required_argument, NULL, 'i' },
181 { "key-dir", required_argument, NULL, 'k' },
182 { "key-dest", required_argument, NULL, 'K' },
183 { "list", no_argument, NULL, 'l' },
184 { "config", required_argument, NULL, 'n' },
185 { "engine", required_argument, NULL, 'N' },
186 { "algo", required_argument, NULL, 'o' },
187 { "os", required_argument, NULL, 'O' },
188 { "position", required_argument, NULL, 'p' },
189 { "quiet", no_argument, NULL, 'q' },
190 { "key-required", no_argument, NULL, 'r' },
191 { "secondary-config", required_argument, NULL, 'R' },
192 { "no-copy", no_argument, NULL, 's' },
193 { "touch", no_argument, NULL, 't' },
194 { "type", required_argument, NULL, 'T' },
195 { "verbose", no_argument, NULL, 'v' },
196 { "version", no_argument, NULL, 'V' },
197 { "xip", no_argument, NULL, 'x' },
200 static void process_args(int argc, char **argv)
203 int type = IH_TYPE_INVALID;
204 char *datafile = NULL;
207 while ((opt = getopt_long(argc, argv, optstring,
208 longopts, NULL)) != -1) {
211 params.addr = strtoull(optarg, &ptr, 16);
213 fprintf(stderr, "%s: invalid load address %s\n",
214 params.cmdname, optarg);
219 params.arch = genimg_get_arch_id(optarg);
220 if (params.arch < 0) {
221 show_valid_options(IH_ARCH);
222 usage("Invalid architecture");
227 if (add_content(IH_TYPE_FLATDT, optarg)) {
229 "%s: Out of memory adding content '%s'",
230 params.cmdname, optarg);
235 params.bl_len = strtoull(optarg, &ptr, 16);
237 fprintf(stderr, "%s: invalid block length %s\n",
238 params.cmdname, optarg);
244 params.comment = optarg;
247 params.comp = genimg_get_comp_id(optarg);
248 if (params.comp < 0) {
249 show_valid_options(IH_COMP);
250 usage("Invalid compression type");
254 params.datafile = optarg;
261 params.ep = strtoull(optarg, &ptr, 16);
263 fprintf(stderr, "%s: invalid entry point %s\n",
264 params.cmdname, optarg);
270 params.external_data = true;
274 params.auto_its = !strcmp(datafile, "auto");
278 * The flattened image tree (FIT) format
279 * requires a flattened device tree image type
281 params.type = IH_TYPE_FLATDT;
285 params.keyname = optarg;
287 params.keyfile = optarg;
290 params.fit_ramdisk = optarg;
293 params.keydir = optarg;
296 params.keydest = optarg;
302 params.imagename = optarg;
305 params.engine_id = optarg;
308 params.algo_name = optarg;
311 params.os = genimg_get_os_id(optarg);
313 show_valid_options(IH_OS);
314 usage("Invalid operating system");
318 params.external_offset = strtoull(optarg, &ptr, 16);
320 fprintf(stderr, "%s: invalid offset size %s\n",
321 params.cmdname, optarg);
329 params.require_keys = 1;
333 * This entry is for the second configuration
334 * file, if only one is not enough.
336 params.imagename2 = optarg;
342 params.reset_timestamp = 1;
345 if (strcmp(optarg, "list") == 0) {
346 show_valid_options(IH_TYPE);
349 type = genimg_get_type_id(optarg);
351 show_valid_options(IH_TYPE);
352 usage("Invalid image type");
359 printf("mkimage version %s\n", PLAIN_VERSION);
365 usage("Invalid option");
369 /* The last parameter is expected to be the imagefile */
371 params.imagefile = argv[optind];
374 * For auto-generated FIT images we need to know the image type to put
375 * in the FIT, which is separate from the file's image type (which
376 * will always be IH_TYPE_FLATDT in this case).
378 if (params.type == IH_TYPE_FLATDT) {
379 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
380 /* For auto_its, datafile is always 'auto' */
381 if (!params.auto_its)
382 params.datafile = datafile;
383 else if (!params.datafile)
384 usage("Missing data file for auto-FIT (use -d)");
385 } else if (params.lflag || type != IH_TYPE_INVALID) {
386 if (type == IH_TYPE_SCRIPT && !params.datafile)
387 usage("Missing data file for script (use -d)");
391 if (!params.imagefile)
392 usage("Missing output filename");
395 static void verify_image(const struct image_type_params *tparams)
401 ifd = open(params.imagefile, O_RDONLY | O_BINARY);
403 fprintf(stderr, "%s: Can't open %s: %s\n",
404 params.cmdname, params.imagefile,
409 if (fstat(ifd, &sbuf) < 0) {
410 fprintf(stderr, "%s: Can't stat %s: %s\n",
411 params.cmdname, params.imagefile, strerror(errno));
414 params.file_size = sbuf.st_size;
416 ptr = mmap(0, params.file_size, PROT_READ, MAP_SHARED, ifd, 0);
417 if (ptr == MAP_FAILED) {
418 fprintf(stderr, "%s: Can't map %s: %s\n",
419 params.cmdname, params.imagefile, strerror(errno));
423 if (tparams->verify_header((unsigned char *)ptr, params.file_size, ¶ms) != 0) {
424 fprintf(stderr, "%s: Failed to verify header of %s\n",
425 params.cmdname, params.imagefile);
429 (void)munmap(ptr, params.file_size);
433 int main(int argc, char **argv)
439 struct image_type_params *tparams = NULL;
444 params.cmdname = *argv;
448 process_args(argc, argv);
450 /* set tparams as per input type_id */
451 tparams = imagetool_get_type(params.type);
452 if (tparams == NULL && !params.lflag) {
453 fprintf (stderr, "%s: unsupported type %s\n",
454 params.cmdname, genimg_get_type_name(params.type));
459 * check the passed arguments parameters meets the requirements
460 * as per image type to be generated/listed
462 if (tparams && tparams->check_params)
463 if (tparams->check_params (¶ms))
464 usage("Bad parameters for image type");
467 params.ep = params.addr;
468 /* If XIP, entry point must be after the U-Boot header */
469 if (params.xflag && tparams)
470 params.ep += tparams->header_size;
475 fprintf(stderr, "%s: Missing FIT support\n",
479 if (tparams->fflag_handle)
481 * in some cases, some additional processing needs
482 * to be done if fflag is defined
484 * For ex. fit_handle_file for Fit file support
486 retval = tparams->fflag_handle(¶ms);
488 if (retval != EXIT_SUCCESS)
489 usage("Bad parameters for FIT image type");
492 if (params.lflag || params.fflag) {
493 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
495 ifd = open (params.imagefile,
496 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
500 fprintf (stderr, "%s: Can't open %s: %s\n",
501 params.cmdname, params.imagefile,
506 if (params.lflag || params.fflag) {
509 * list header information of existing image
511 if (fstat(ifd, &sbuf) < 0) {
512 fprintf (stderr, "%s: Can't stat %s: %s\n",
513 params.cmdname, params.imagefile,
518 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
520 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
521 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
523 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
525 "%s: failed to get size of block device \"%s\"\n",
526 params.cmdname, params.imagefile);
531 "%s: \"%s\" is block device, don't know how to get its size\n",
532 params.cmdname, params.imagefile);
535 } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) {
537 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
538 params.cmdname, params.imagefile,
539 (unsigned long long) sbuf.st_size,
540 tparams->header_size);
546 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
547 if (ptr == MAP_FAILED) {
548 fprintf (stderr, "%s: Can't read %s: %s\n",
549 params.cmdname, params.imagefile,
555 * Verifies the header format based on the expected header for image
556 * type in tparams. If tparams is NULL simply check all image types
557 * to find one that matches our header.
559 retval = imagetool_verify_print_header(ptr, &sbuf, tparams, ¶ms);
561 (void) munmap((void *)ptr, sbuf.st_size);
564 summary_show(¶ms.summary, params.imagefile,
570 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
571 dfd = open(params.datafile, O_RDONLY | O_BINARY);
573 fprintf(stderr, "%s: Can't open %s: %s\n",
574 params.cmdname, params.datafile,
579 if (fstat(dfd, &sbuf) < 0) {
580 fprintf(stderr, "%s: Can't stat %s: %s\n",
581 params.cmdname, params.datafile,
586 params.file_size = sbuf.st_size + tparams->header_size;
591 * In case there an header with a variable
592 * length will be added, the corresponding
593 * function is called. This is responsible to
594 * allocate memory for the header itself.
596 if (tparams->vrec_header)
597 pad_len = tparams->vrec_header(¶ms, tparams);
599 memset(tparams->hdr, 0, tparams->header_size);
601 if (write(ifd, tparams->hdr, tparams->header_size)
602 != tparams->header_size) {
603 fprintf (stderr, "%s: Write error on %s: %s\n",
604 params.cmdname, params.imagefile, strerror(errno));
608 if (!params.skipcpy) {
609 if (params.type == IH_TYPE_MULTI ||
610 params.type == IH_TYPE_SCRIPT) {
611 char *file = params.datafile;
618 if ((sep = strchr(file, ':')) != NULL) {
622 if (stat (file, &sbuf) < 0) {
623 fprintf (stderr, "%s: Can't stat %s: %s\n",
624 params.cmdname, file, strerror(errno));
627 size = cpu_to_uimage (sbuf.st_size);
632 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
633 fprintf (stderr, "%s: Write error on %s: %s\n",
634 params.cmdname, params.imagefile,
651 file = params.datafile;
654 char *sep = strchr(file, ':');
657 copy_file (ifd, file, 1);
661 copy_file (ifd, file, 0);
665 } else if (params.type == IH_TYPE_PBLIMAGE) {
666 /* PBL has special Image format, implements its' own */
667 pbl_load_uboot(ifd, ¶ms);
668 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
669 /* Image file is meta, walk through actual targets */
672 ret = zynqmpbif_copy_image(ifd, ¶ms);
675 } else if (params.type == IH_TYPE_IMX8IMAGE) {
676 /* i.MX8/8X has special Image format */
679 ret = imx8image_copy_image(ifd, ¶ms);
682 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
683 /* i.MX8M has special Image format */
686 ret = imx8mimage_copy_image(ifd, ¶ms);
689 } else if ((params.type == IH_TYPE_RKSD) ||
690 (params.type == IH_TYPE_RKSPI)) {
691 /* Rockchip has special Image format */
694 ret = rockchip_copy_image(ifd, ¶ms);
698 copy_file(ifd, params.datafile, pad_len);
700 if (params.type == IH_TYPE_FIRMWARE_IVT) {
701 /* Add alignment and IVT */
702 uint32_t aligned_filesize = ALIGN(params.file_size,
704 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
705 params.addr, 0, 0, 0, params.addr
707 - tparams->header_size,
708 params.addr + aligned_filesize
709 - tparams->header_size
711 int i = params.file_size;
712 for (; i < aligned_filesize; i++) {
713 if (write(ifd, (char *) &i, 1) != 1) {
715 "%s: Write error on %s: %s\n",
722 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
723 != sizeof(flash_header_v2_t)) {
724 fprintf(stderr, "%s: Write error on %s: %s\n",
733 /* We're a bit of paranoid */
734 #if defined(_POSIX_SYNCHRONIZED_IO) && \
735 !defined(__sun__) && \
736 !defined(__FreeBSD__) && \
737 !defined(__OpenBSD__) && \
739 (void) fdatasync (ifd);
744 if (fstat(ifd, &sbuf) < 0) {
745 fprintf (stderr, "%s: Can't stat %s: %s\n",
746 params.cmdname, params.imagefile, strerror(errno));
749 params.file_size = sbuf.st_size;
751 map_len = sbuf.st_size;
752 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
753 if (ptr == MAP_FAILED) {
754 fprintf (stderr, "%s: Can't map %s: %s\n",
755 params.cmdname, params.imagefile, strerror(errno));
759 /* Setup the image header as per input image type*/
760 if (tparams->set_header)
761 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
763 fprintf (stderr, "%s: Can't set header for %s: %s\n",
764 params.cmdname, tparams->name, strerror(errno));
768 /* Print the image information by processing image header */
769 if (tparams->print_header)
770 tparams->print_header (ptr);
772 fprintf (stderr, "%s: Can't print header for %s\n",
773 params.cmdname, tparams->name);
776 (void)munmap((void *)ptr, map_len);
778 /* We're a bit of paranoid */
779 #if defined(_POSIX_SYNCHRONIZED_IO) && \
780 !defined(__sun__) && \
781 !defined(__FreeBSD__) && \
782 !defined(__OpenBSD__) && \
784 (void) fdatasync (ifd);
790 fprintf (stderr, "%s: Write error on %s: %s\n",
791 params.cmdname, params.imagefile, strerror(errno));
795 if (tparams->verify_header)
796 verify_image(tparams);
802 copy_file (int ifd, const char *datafile, int pad)
812 struct image_type_params *tparams = imagetool_get_type(params.type);
814 memset(zeros, 0, sizeof(zeros));
817 fprintf (stderr, "Adding Image %s\n", datafile);
820 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
821 fprintf (stderr, "%s: Can't open %s: %s\n",
822 params.cmdname, datafile, strerror(errno));
826 if (fstat(dfd, &sbuf) < 0) {
827 fprintf (stderr, "%s: Can't stat %s: %s\n",
828 params.cmdname, datafile, strerror(errno));
832 if (sbuf.st_size == 0) {
833 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
834 params.cmdname, datafile);
838 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
839 if (ptr == MAP_FAILED) {
840 fprintf (stderr, "%s: Can't read %s: %s\n",
841 params.cmdname, datafile, strerror(errno));
846 unsigned char *p = NULL;
848 * XIP: do not append the image_header_t at the
849 * beginning of the file, but consume the space
853 if ((unsigned)sbuf.st_size < tparams->header_size) {
855 "%s: Bad size: \"%s\" is too small for XIP\n",
856 params.cmdname, datafile);
860 for (p = ptr; p < ptr + tparams->header_size; p++) {
863 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
864 params.cmdname, datafile);
869 offset = tparams->header_size;
872 size = sbuf.st_size - offset;
874 ret = write(ifd, ptr + offset, size);
877 fprintf (stderr, "%s: Write error on %s: %s\n",
878 params.cmdname, params.imagefile, strerror(errno));
880 fprintf (stderr, "%s: Write only %d/%d bytes, "\
881 "probably no space left on the device\n",
882 params.cmdname, ret, size);
887 if ((pad == 1) && (tail != 0)) {
889 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
890 fprintf (stderr, "%s: Write error on %s: %s\n",
891 params.cmdname, params.imagefile,
895 } else if (pad > 1) {
897 int todo = sizeof(zeros);
901 if (write(ifd, (char *)&zeros, todo) != todo) {
902 fprintf(stderr, "%s: Write error on %s: %s\n",
903 params.cmdname, params.imagefile,
911 (void) munmap((void *)ptr, sbuf.st_size);