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 -l image\n"
86 " -l ==> list image header information\n",
89 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
90 " -A ==> set architecture to 'arch'\n"
91 " -O ==> set operating system to 'os'\n"
92 " -T ==> set image type to 'type'\n"
93 " -C ==> set compression type 'comp'\n"
94 " -a ==> set load address to 'addr' (hex)\n"
95 " -e ==> set entry point to 'ep' (hex)\n"
96 " -n ==> set image name to 'name'\n"
97 " -d ==> use image data from 'datafile'\n"
98 " -x ==> set XIP (execute in place)\n",
101 " %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"
102 " <dtb> file is used with -f auto, it may occur multiple times.\n",
105 " -D => set all options for device tree compiler\n"
106 " -f => input filename for FIT source\n"
107 " -i => input filename for ramdisk file\n"
108 " -E => place data outside of the FIT structure\n"
109 " -B => align size in hex for FIT structure and header\n");
110 #ifdef CONFIG_FIT_SIGNATURE
112 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
113 " -k => set directory containing private keys\n"
114 " -K => write public keys to this .dtb file\n"
115 " -G => use this signing key (in lieu of -k)\n"
116 " -c => add comment in signature node\n"
117 " -F => re-sign existing FIT image\n"
118 " -p => place external data at a static position\n"
119 " -r => mark keys used as 'required' in dtb\n"
120 " -N => openssl engine to use for signing\n");
123 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
125 fprintf(stderr, " %s -V ==> print version information and exit\n",
127 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
132 static int add_content(int type, const char *fname)
134 struct content_info *cont;
136 cont = calloc(1, sizeof(*cont));
141 if (params.content_tail)
142 params.content_tail->next = cont;
144 params.content_head = cont;
145 params.content_tail = cont;
150 static void process_args(int argc, char **argv)
153 int type = IH_TYPE_INVALID;
154 char *datafile = NULL;
157 while ((opt = getopt(argc, argv,
158 "a:A:b:B:c:C:d:D:e:Ef:FG:k:i:K:ln:N:p:o:O:rR:qstT:vVx")) != -1) {
161 params.addr = strtoull(optarg, &ptr, 16);
163 fprintf(stderr, "%s: invalid load address %s\n",
164 params.cmdname, optarg);
169 params.arch = genimg_get_arch_id(optarg);
170 if (params.arch < 0) {
171 show_valid_options(IH_ARCH);
172 usage("Invalid architecture");
176 if (add_content(IH_TYPE_FLATDT, optarg)) {
178 "%s: Out of memory adding content '%s'",
179 params.cmdname, optarg);
184 params.bl_len = strtoull(optarg, &ptr, 16);
186 fprintf(stderr, "%s: invalid block length %s\n",
187 params.cmdname, optarg);
193 params.comment = optarg;
196 params.comp = genimg_get_comp_id(optarg);
197 if (params.comp < 0) {
198 show_valid_options(IH_COMP);
199 usage("Invalid compression type");
203 params.datafile = optarg;
210 params.ep = strtoull(optarg, &ptr, 16);
212 fprintf(stderr, "%s: invalid entry point %s\n",
213 params.cmdname, optarg);
219 params.external_data = true;
223 params.auto_its = !strcmp(datafile, "auto");
227 * The flattened image tree (FIT) format
228 * requires a flattened device tree image type
230 params.type = IH_TYPE_FLATDT;
234 params.keyfile = optarg;
237 params.fit_ramdisk = optarg;
240 params.keydir = optarg;
243 params.keydest = optarg;
249 params.imagename = optarg;
252 params.engine_id = optarg;
255 params.algo_name = optarg;
258 params.os = genimg_get_os_id(optarg);
260 show_valid_options(IH_OS);
261 usage("Invalid operating system");
265 params.external_offset = strtoull(optarg, &ptr, 16);
267 fprintf(stderr, "%s: invalid offset size %s\n",
268 params.cmdname, optarg);
276 params.require_keys = 1;
280 * This entry is for the second configuration
281 * file, if only one is not enough.
283 params.imagename2 = optarg;
289 params.reset_timestamp = 1;
292 if (strcmp(optarg, "list") == 0) {
293 show_valid_options(IH_TYPE);
296 type = genimg_get_type_id(optarg);
298 show_valid_options(IH_TYPE);
299 usage("Invalid image type");
306 printf("mkimage version %s\n", PLAIN_VERSION);
312 usage("Invalid option");
316 /* The last parameter is expected to be the imagefile */
318 params.imagefile = argv[optind];
321 * For auto-generated FIT images we need to know the image type to put
322 * in the FIT, which is separate from the file's image type (which
323 * will always be IH_TYPE_FLATDT in this case).
325 if (params.type == IH_TYPE_FLATDT) {
326 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
327 /* For auto_its, datafile is always 'auto' */
328 if (!params.auto_its)
329 params.datafile = datafile;
330 else if (!params.datafile)
331 usage("Missing data file for auto-FIT (use -d)");
332 } else if (type != IH_TYPE_INVALID) {
333 if (type == IH_TYPE_SCRIPT && !params.datafile)
334 usage("Missing data file for script (use -d)");
338 if (!params.imagefile)
339 usage("Missing output filename");
342 int main(int argc, char **argv)
348 struct image_type_params *tparams = NULL;
353 params.cmdname = *argv;
357 process_args(argc, argv);
359 /* set tparams as per input type_id */
360 tparams = imagetool_get_type(params.type);
361 if (tparams == NULL) {
362 fprintf (stderr, "%s: unsupported type %s\n",
363 params.cmdname, genimg_get_type_name(params.type));
368 * check the passed arguments parameters meets the requirements
369 * as per image type to be generated/listed
371 if (tparams->check_params)
372 if (tparams->check_params (¶ms))
373 usage("Bad parameters for image type");
376 params.ep = params.addr;
377 /* If XIP, entry point must be after the U-Boot header */
379 params.ep += tparams->header_size;
383 if (tparams->fflag_handle)
385 * in some cases, some additional processing needs
386 * to be done if fflag is defined
388 * For ex. fit_handle_file for Fit file support
390 retval = tparams->fflag_handle(¶ms);
392 if (retval != EXIT_SUCCESS)
396 if (params.lflag || params.fflag) {
397 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
399 ifd = open (params.imagefile,
400 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
404 fprintf (stderr, "%s: Can't open %s: %s\n",
405 params.cmdname, params.imagefile,
410 if (params.lflag || params.fflag) {
413 * list header information of existing image
415 if (fstat(ifd, &sbuf) < 0) {
416 fprintf (stderr, "%s: Can't stat %s: %s\n",
417 params.cmdname, params.imagefile,
422 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
424 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
425 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
427 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
429 "%s: failed to get size of block device \"%s\"\n",
430 params.cmdname, params.imagefile);
435 "%s: \"%s\" is block device, don't know how to get its size\n",
436 params.cmdname, params.imagefile);
439 } else if (sbuf.st_size < (off_t)tparams->header_size) {
441 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
442 params.cmdname, params.imagefile,
443 (unsigned long long) sbuf.st_size,
444 tparams->header_size);
450 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
451 if (ptr == MAP_FAILED) {
452 fprintf (stderr, "%s: Can't read %s: %s\n",
453 params.cmdname, params.imagefile,
460 * Verifies the header format based on the expected header for image
463 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
467 * When listing the image, we are not given the image type. Simply check all
468 * image types to find one that matches our header
470 retval = imagetool_verify_print_header(ptr, &sbuf,
474 (void) munmap((void *)ptr, sbuf.st_size);
477 summary_show(¶ms.summary, params.imagefile,
483 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
484 dfd = open(params.datafile, O_RDONLY | O_BINARY);
486 fprintf(stderr, "%s: Can't open %s: %s\n",
487 params.cmdname, params.datafile,
492 if (fstat(dfd, &sbuf) < 0) {
493 fprintf(stderr, "%s: Can't stat %s: %s\n",
494 params.cmdname, params.datafile,
499 params.file_size = sbuf.st_size + tparams->header_size;
504 * In case there an header with a variable
505 * length will be added, the corresponding
506 * function is called. This is responsible to
507 * allocate memory for the header itself.
509 if (tparams->vrec_header)
510 pad_len = tparams->vrec_header(¶ms, tparams);
512 memset(tparams->hdr, 0, tparams->header_size);
514 if (write(ifd, tparams->hdr, tparams->header_size)
515 != tparams->header_size) {
516 fprintf (stderr, "%s: Write error on %s: %s\n",
517 params.cmdname, params.imagefile, strerror(errno));
521 if (!params.skipcpy) {
522 if (params.type == IH_TYPE_MULTI ||
523 params.type == IH_TYPE_SCRIPT) {
524 char *file = params.datafile;
531 if ((sep = strchr(file, ':')) != NULL) {
535 if (stat (file, &sbuf) < 0) {
536 fprintf (stderr, "%s: Can't stat %s: %s\n",
537 params.cmdname, file, strerror(errno));
540 size = cpu_to_uimage (sbuf.st_size);
545 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
546 fprintf (stderr, "%s: Write error on %s: %s\n",
547 params.cmdname, params.imagefile,
564 file = params.datafile;
567 char *sep = strchr(file, ':');
570 copy_file (ifd, file, 1);
574 copy_file (ifd, file, 0);
578 } else if (params.type == IH_TYPE_PBLIMAGE) {
579 /* PBL has special Image format, implements its' own */
580 pbl_load_uboot(ifd, ¶ms);
581 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
582 /* Image file is meta, walk through actual targets */
585 ret = zynqmpbif_copy_image(ifd, ¶ms);
588 } else if (params.type == IH_TYPE_IMX8IMAGE) {
589 /* i.MX8/8X has special Image format */
592 ret = imx8image_copy_image(ifd, ¶ms);
595 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
596 /* i.MX8M has special Image format */
599 ret = imx8mimage_copy_image(ifd, ¶ms);
602 } else if ((params.type == IH_TYPE_RKSD) ||
603 (params.type == IH_TYPE_RKSPI)) {
604 /* Rockchip has special Image format */
607 ret = rockchip_copy_image(ifd, ¶ms);
611 copy_file(ifd, params.datafile, pad_len);
613 if (params.type == IH_TYPE_FIRMWARE_IVT) {
614 /* Add alignment and IVT */
615 uint32_t aligned_filesize = ALIGN(params.file_size,
617 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
618 params.addr, 0, 0, 0, params.addr
620 - tparams->header_size,
621 params.addr + aligned_filesize
622 - tparams->header_size
624 int i = params.file_size;
625 for (; i < aligned_filesize; i++) {
626 if (write(ifd, (char *) &i, 1) != 1) {
628 "%s: Write error on %s: %s\n",
635 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
636 != sizeof(flash_header_v2_t)) {
637 fprintf(stderr, "%s: Write error on %s: %s\n",
646 /* We're a bit of paranoid */
647 #if defined(_POSIX_SYNCHRONIZED_IO) && \
648 !defined(__sun__) && \
649 !defined(__FreeBSD__) && \
650 !defined(__OpenBSD__) && \
652 (void) fdatasync (ifd);
657 if (fstat(ifd, &sbuf) < 0) {
658 fprintf (stderr, "%s: Can't stat %s: %s\n",
659 params.cmdname, params.imagefile, strerror(errno));
662 params.file_size = sbuf.st_size;
664 map_len = sbuf.st_size;
665 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
666 if (ptr == MAP_FAILED) {
667 fprintf (stderr, "%s: Can't map %s: %s\n",
668 params.cmdname, params.imagefile, strerror(errno));
672 /* Setup the image header as per input image type*/
673 if (tparams->set_header)
674 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
676 fprintf (stderr, "%s: Can't set header for %s: %s\n",
677 params.cmdname, tparams->name, strerror(errno));
681 /* Print the image information by processing image header */
682 if (tparams->print_header)
683 tparams->print_header (ptr);
685 fprintf (stderr, "%s: Can't print header for %s\n",
686 params.cmdname, tparams->name);
689 (void)munmap((void *)ptr, map_len);
691 /* We're a bit of paranoid */
692 #if defined(_POSIX_SYNCHRONIZED_IO) && \
693 !defined(__sun__) && \
694 !defined(__FreeBSD__) && \
695 !defined(__OpenBSD__) && \
697 (void) fdatasync (ifd);
703 fprintf (stderr, "%s: Write error on %s: %s\n",
704 params.cmdname, params.imagefile, strerror(errno));
712 copy_file (int ifd, const char *datafile, int pad)
722 struct image_type_params *tparams = imagetool_get_type(params.type);
724 memset(zeros, 0, sizeof(zeros));
727 fprintf (stderr, "Adding Image %s\n", datafile);
730 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
731 fprintf (stderr, "%s: Can't open %s: %s\n",
732 params.cmdname, datafile, strerror(errno));
736 if (fstat(dfd, &sbuf) < 0) {
737 fprintf (stderr, "%s: Can't stat %s: %s\n",
738 params.cmdname, datafile, strerror(errno));
742 if (sbuf.st_size == 0) {
743 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
744 params.cmdname, datafile);
748 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
749 if (ptr == MAP_FAILED) {
750 fprintf (stderr, "%s: Can't read %s: %s\n",
751 params.cmdname, datafile, strerror(errno));
756 unsigned char *p = NULL;
758 * XIP: do not append the image_header_t at the
759 * beginning of the file, but consume the space
763 if ((unsigned)sbuf.st_size < tparams->header_size) {
765 "%s: Bad size: \"%s\" is too small for XIP\n",
766 params.cmdname, datafile);
770 for (p = ptr; p < ptr + tparams->header_size; p++) {
773 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
774 params.cmdname, datafile);
779 offset = tparams->header_size;
782 size = sbuf.st_size - offset;
784 ret = write(ifd, ptr + offset, size);
787 fprintf (stderr, "%s: Write error on %s: %s\n",
788 params.cmdname, params.imagefile, strerror(errno));
790 fprintf (stderr, "%s: Write only %d/%d bytes, "\
791 "probably no space left on the device\n",
792 params.cmdname, ret, size);
797 if ((pad == 1) && (tail != 0)) {
799 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
800 fprintf (stderr, "%s: Write error on %s: %s\n",
801 params.cmdname, params.imagefile,
805 } else if (pad > 1) {
807 int todo = sizeof(zeros);
811 if (write(ifd, (char *)&zeros, todo) != todo) {
812 fprintf(stderr, "%s: Write error on %s: %s\n",
813 params.cmdname, params.imagefile,
821 (void) munmap((void *)ptr, sbuf.st_size);