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 => set key name hint\n"
123 " -G => use this signing key (in lieu of -k)\n"
124 " -c => add comment in signature node\n"
125 " -F => re-sign existing FIT image\n"
126 " -p => place external data at a static position\n"
127 " -r => mark keys used as 'required' in dtb\n"
128 " -N => openssl engine to use for signing\n"
129 " -o => algorithm to use for signing\n");
132 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
134 fprintf(stderr, " %s -V ==> print version information and exit\n",
136 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
141 static int add_content(int type, const char *fname)
143 struct content_info *cont;
145 cont = calloc(1, sizeof(*cont));
150 if (params.content_tail)
151 params.content_tail->next = cont;
153 params.content_head = cont;
154 params.content_tail = cont;
159 static void process_args(int argc, char **argv)
162 int type = IH_TYPE_INVALID;
163 char *datafile = NULL;
166 while ((opt = getopt(argc, argv,
167 "a:A:b:B:c:C:d:D:e:Ef:Fg:G:k:i:K:ln:N:p:o:O:rR:qstT:vVx")) != -1) {
170 params.addr = strtoull(optarg, &ptr, 16);
172 fprintf(stderr, "%s: invalid load address %s\n",
173 params.cmdname, optarg);
178 params.arch = genimg_get_arch_id(optarg);
179 if (params.arch < 0) {
180 show_valid_options(IH_ARCH);
181 usage("Invalid architecture");
186 if (add_content(IH_TYPE_FLATDT, optarg)) {
188 "%s: Out of memory adding content '%s'",
189 params.cmdname, optarg);
194 params.bl_len = strtoull(optarg, &ptr, 16);
196 fprintf(stderr, "%s: invalid block length %s\n",
197 params.cmdname, optarg);
203 params.comment = optarg;
206 params.comp = genimg_get_comp_id(optarg);
207 if (params.comp < 0) {
208 show_valid_options(IH_COMP);
209 usage("Invalid compression type");
213 params.datafile = optarg;
220 params.ep = strtoull(optarg, &ptr, 16);
222 fprintf(stderr, "%s: invalid entry point %s\n",
223 params.cmdname, optarg);
229 params.external_data = true;
233 params.auto_its = !strcmp(datafile, "auto");
237 * The flattened image tree (FIT) format
238 * requires a flattened device tree image type
240 params.type = IH_TYPE_FLATDT;
244 params.keyname = optarg;
246 params.keyfile = optarg;
249 params.fit_ramdisk = optarg;
252 params.keydir = optarg;
255 params.keydest = optarg;
261 params.imagename = optarg;
264 params.engine_id = optarg;
267 params.algo_name = optarg;
270 params.os = genimg_get_os_id(optarg);
272 show_valid_options(IH_OS);
273 usage("Invalid operating system");
277 params.external_offset = strtoull(optarg, &ptr, 16);
279 fprintf(stderr, "%s: invalid offset size %s\n",
280 params.cmdname, optarg);
288 params.require_keys = 1;
292 * This entry is for the second configuration
293 * file, if only one is not enough.
295 params.imagename2 = optarg;
301 params.reset_timestamp = 1;
304 if (strcmp(optarg, "list") == 0) {
305 show_valid_options(IH_TYPE);
308 type = genimg_get_type_id(optarg);
310 show_valid_options(IH_TYPE);
311 usage("Invalid image type");
318 printf("mkimage version %s\n", PLAIN_VERSION);
324 usage("Invalid option");
328 /* The last parameter is expected to be the imagefile */
330 params.imagefile = argv[optind];
333 * For auto-generated FIT images we need to know the image type to put
334 * in the FIT, which is separate from the file's image type (which
335 * will always be IH_TYPE_FLATDT in this case).
337 if (params.type == IH_TYPE_FLATDT) {
338 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
339 /* For auto_its, datafile is always 'auto' */
340 if (!params.auto_its)
341 params.datafile = datafile;
342 else if (!params.datafile)
343 usage("Missing data file for auto-FIT (use -d)");
344 } else if (params.lflag || type != IH_TYPE_INVALID) {
345 if (type == IH_TYPE_SCRIPT && !params.datafile)
346 usage("Missing data file for script (use -d)");
350 if (!params.imagefile)
351 usage("Missing output filename");
354 static void verify_image(const struct image_type_params *tparams)
360 ifd = open(params.imagefile, O_RDONLY | O_BINARY);
362 fprintf(stderr, "%s: Can't open %s: %s\n",
363 params.cmdname, params.imagefile,
368 if (fstat(ifd, &sbuf) < 0) {
369 fprintf(stderr, "%s: Can't stat %s: %s\n",
370 params.cmdname, params.imagefile, strerror(errno));
373 params.file_size = sbuf.st_size;
375 ptr = mmap(0, params.file_size, PROT_READ, MAP_SHARED, ifd, 0);
376 if (ptr == MAP_FAILED) {
377 fprintf(stderr, "%s: Can't map %s: %s\n",
378 params.cmdname, params.imagefile, strerror(errno));
382 if (tparams->verify_header((unsigned char *)ptr, params.file_size, ¶ms) != 0) {
383 fprintf(stderr, "%s: Failed to verify header of %s\n",
384 params.cmdname, params.imagefile);
388 (void)munmap(ptr, params.file_size);
392 int main(int argc, char **argv)
398 struct image_type_params *tparams = NULL;
403 params.cmdname = *argv;
407 process_args(argc, argv);
409 /* set tparams as per input type_id */
410 tparams = imagetool_get_type(params.type);
411 if (tparams == NULL && !params.lflag) {
412 fprintf (stderr, "%s: unsupported type %s\n",
413 params.cmdname, genimg_get_type_name(params.type));
418 * check the passed arguments parameters meets the requirements
419 * as per image type to be generated/listed
421 if (tparams && tparams->check_params)
422 if (tparams->check_params (¶ms))
423 usage("Bad parameters for image type");
426 params.ep = params.addr;
427 /* If XIP, entry point must be after the U-Boot header */
428 if (params.xflag && tparams)
429 params.ep += tparams->header_size;
434 fprintf(stderr, "%s: Missing FIT support\n",
438 if (tparams->fflag_handle)
440 * in some cases, some additional processing needs
441 * to be done if fflag is defined
443 * For ex. fit_handle_file for Fit file support
445 retval = tparams->fflag_handle(¶ms);
447 if (retval != EXIT_SUCCESS)
448 usage("Bad parameters for FIT image type");
451 if (params.lflag || params.fflag) {
452 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
454 ifd = open (params.imagefile,
455 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
459 fprintf (stderr, "%s: Can't open %s: %s\n",
460 params.cmdname, params.imagefile,
465 if (params.lflag || params.fflag) {
468 * list header information of existing image
470 if (fstat(ifd, &sbuf) < 0) {
471 fprintf (stderr, "%s: Can't stat %s: %s\n",
472 params.cmdname, params.imagefile,
477 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
479 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
480 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
482 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
484 "%s: failed to get size of block device \"%s\"\n",
485 params.cmdname, params.imagefile);
490 "%s: \"%s\" is block device, don't know how to get its size\n",
491 params.cmdname, params.imagefile);
494 } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) {
496 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
497 params.cmdname, params.imagefile,
498 (unsigned long long) sbuf.st_size,
499 tparams->header_size);
505 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
506 if (ptr == MAP_FAILED) {
507 fprintf (stderr, "%s: Can't read %s: %s\n",
508 params.cmdname, params.imagefile,
514 * Verifies the header format based on the expected header for image
515 * type in tparams. If tparams is NULL simply check all image types
516 * to find one that matches our header.
518 retval = imagetool_verify_print_header(ptr, &sbuf, tparams, ¶ms);
520 (void) munmap((void *)ptr, sbuf.st_size);
523 summary_show(¶ms.summary, params.imagefile,
529 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
530 dfd = open(params.datafile, O_RDONLY | O_BINARY);
532 fprintf(stderr, "%s: Can't open %s: %s\n",
533 params.cmdname, params.datafile,
538 if (fstat(dfd, &sbuf) < 0) {
539 fprintf(stderr, "%s: Can't stat %s: %s\n",
540 params.cmdname, params.datafile,
545 params.file_size = sbuf.st_size + tparams->header_size;
550 * In case there an header with a variable
551 * length will be added, the corresponding
552 * function is called. This is responsible to
553 * allocate memory for the header itself.
555 if (tparams->vrec_header)
556 pad_len = tparams->vrec_header(¶ms, tparams);
558 memset(tparams->hdr, 0, tparams->header_size);
560 if (write(ifd, tparams->hdr, tparams->header_size)
561 != tparams->header_size) {
562 fprintf (stderr, "%s: Write error on %s: %s\n",
563 params.cmdname, params.imagefile, strerror(errno));
567 if (!params.skipcpy) {
568 if (params.type == IH_TYPE_MULTI ||
569 params.type == IH_TYPE_SCRIPT) {
570 char *file = params.datafile;
577 if ((sep = strchr(file, ':')) != NULL) {
581 if (stat (file, &sbuf) < 0) {
582 fprintf (stderr, "%s: Can't stat %s: %s\n",
583 params.cmdname, file, strerror(errno));
586 size = cpu_to_uimage (sbuf.st_size);
591 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
592 fprintf (stderr, "%s: Write error on %s: %s\n",
593 params.cmdname, params.imagefile,
610 file = params.datafile;
613 char *sep = strchr(file, ':');
616 copy_file (ifd, file, 1);
620 copy_file (ifd, file, 0);
624 } else if (params.type == IH_TYPE_PBLIMAGE) {
625 /* PBL has special Image format, implements its' own */
626 pbl_load_uboot(ifd, ¶ms);
627 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
628 /* Image file is meta, walk through actual targets */
631 ret = zynqmpbif_copy_image(ifd, ¶ms);
634 } else if (params.type == IH_TYPE_IMX8IMAGE) {
635 /* i.MX8/8X has special Image format */
638 ret = imx8image_copy_image(ifd, ¶ms);
641 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
642 /* i.MX8M has special Image format */
645 ret = imx8mimage_copy_image(ifd, ¶ms);
648 } else if ((params.type == IH_TYPE_RKSD) ||
649 (params.type == IH_TYPE_RKSPI)) {
650 /* Rockchip has special Image format */
653 ret = rockchip_copy_image(ifd, ¶ms);
657 copy_file(ifd, params.datafile, pad_len);
659 if (params.type == IH_TYPE_FIRMWARE_IVT) {
660 /* Add alignment and IVT */
661 uint32_t aligned_filesize = ALIGN(params.file_size,
663 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
664 params.addr, 0, 0, 0, params.addr
666 - tparams->header_size,
667 params.addr + aligned_filesize
668 - tparams->header_size
670 int i = params.file_size;
671 for (; i < aligned_filesize; i++) {
672 if (write(ifd, (char *) &i, 1) != 1) {
674 "%s: Write error on %s: %s\n",
681 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
682 != sizeof(flash_header_v2_t)) {
683 fprintf(stderr, "%s: Write error on %s: %s\n",
692 /* We're a bit of paranoid */
693 #if defined(_POSIX_SYNCHRONIZED_IO) && \
694 !defined(__sun__) && \
695 !defined(__FreeBSD__) && \
696 !defined(__OpenBSD__) && \
698 (void) fdatasync (ifd);
703 if (fstat(ifd, &sbuf) < 0) {
704 fprintf (stderr, "%s: Can't stat %s: %s\n",
705 params.cmdname, params.imagefile, strerror(errno));
708 params.file_size = sbuf.st_size;
710 map_len = sbuf.st_size;
711 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
712 if (ptr == MAP_FAILED) {
713 fprintf (stderr, "%s: Can't map %s: %s\n",
714 params.cmdname, params.imagefile, strerror(errno));
718 /* Setup the image header as per input image type*/
719 if (tparams->set_header)
720 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
722 fprintf (stderr, "%s: Can't set header for %s: %s\n",
723 params.cmdname, tparams->name, strerror(errno));
727 /* Print the image information by processing image header */
728 if (tparams->print_header)
729 tparams->print_header (ptr);
731 fprintf (stderr, "%s: Can't print header for %s\n",
732 params.cmdname, tparams->name);
735 (void)munmap((void *)ptr, map_len);
737 /* We're a bit of paranoid */
738 #if defined(_POSIX_SYNCHRONIZED_IO) && \
739 !defined(__sun__) && \
740 !defined(__FreeBSD__) && \
741 !defined(__OpenBSD__) && \
743 (void) fdatasync (ifd);
749 fprintf (stderr, "%s: Write error on %s: %s\n",
750 params.cmdname, params.imagefile, strerror(errno));
754 if (tparams->verify_header)
755 verify_image(tparams);
761 copy_file (int ifd, const char *datafile, int pad)
771 struct image_type_params *tparams = imagetool_get_type(params.type);
773 memset(zeros, 0, sizeof(zeros));
776 fprintf (stderr, "Adding Image %s\n", datafile);
779 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
780 fprintf (stderr, "%s: Can't open %s: %s\n",
781 params.cmdname, datafile, strerror(errno));
785 if (fstat(dfd, &sbuf) < 0) {
786 fprintf (stderr, "%s: Can't stat %s: %s\n",
787 params.cmdname, datafile, strerror(errno));
791 if (sbuf.st_size == 0) {
792 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
793 params.cmdname, datafile);
797 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
798 if (ptr == MAP_FAILED) {
799 fprintf (stderr, "%s: Can't read %s: %s\n",
800 params.cmdname, datafile, strerror(errno));
805 unsigned char *p = NULL;
807 * XIP: do not append the image_header_t at the
808 * beginning of the file, but consume the space
812 if ((unsigned)sbuf.st_size < tparams->header_size) {
814 "%s: Bad size: \"%s\" is too small for XIP\n",
815 params.cmdname, datafile);
819 for (p = ptr; p < ptr + tparams->header_size; p++) {
822 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
823 params.cmdname, datafile);
828 offset = tparams->header_size;
831 size = sbuf.st_size - offset;
833 ret = write(ifd, ptr + offset, size);
836 fprintf (stderr, "%s: Write error on %s: %s\n",
837 params.cmdname, params.imagefile, strerror(errno));
839 fprintf (stderr, "%s: Write only %d/%d bytes, "\
840 "probably no space left on the device\n",
841 params.cmdname, ret, size);
846 if ((pad == 1) && (tail != 0)) {
848 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
849 fprintf (stderr, "%s: Write error on %s: %s\n",
850 params.cmdname, params.imagefile,
854 } else if (pad > 1) {
856 int todo = sizeof(zeros);
860 if (write(ifd, (char *)&zeros, todo) != todo) {
861 fprintf(stderr, "%s: Write error on %s: %s\n",
862 params.cmdname, params.imagefile,
870 (void) munmap((void *)ptr, sbuf.st_size);