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"
16 #include <sys/ioctl.h>
19 static void copy_file(int, const char *, int);
21 /* parameters initialized by core will be used by the image type code */
22 static struct image_tool_params params = {
25 .type = IH_TYPE_KERNEL,
27 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
32 static enum ih_category cur_category;
34 static int h_compare_category_name(const void *vtype1, const void *vtype2)
36 const int *type1 = vtype1;
37 const int *type2 = vtype2;
38 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
39 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
41 return strcmp(name1, name2);
44 static int show_valid_options(enum ih_category category)
51 count = genimg_get_cat_count(category);
52 order = calloc(count, sizeof(*order));
56 /* Sort the names in order of short name for easier reading */
57 for (i = 0, item = 0; i < count; i++, item++) {
58 while (!genimg_cat_has_id(category, item) && i < count) {
64 cur_category = category;
65 qsort(order, count, sizeof(int), h_compare_category_name);
67 fprintf(stderr, "\nInvalid %s, supported are:\n",
68 genimg_get_cat_desc(category));
69 for (i = 0; i < count; i++) {
71 fprintf(stderr, "\t%-15s %s\n",
72 genimg_get_cat_short_name(category, item),
73 genimg_get_cat_name(category, item));
75 fprintf(stderr, "\n");
81 static void usage(const char *msg)
83 fprintf(stderr, "Error: %s\n", msg);
84 fprintf(stderr, "Usage: %s -l image\n"
85 " -l ==> list image header information\n",
88 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
89 " -A ==> set architecture to 'arch'\n"
90 " -O ==> set operating system to 'os'\n"
91 " -T ==> set image type to 'type'\n"
92 " -C ==> set compression type 'comp'\n"
93 " -a ==> set load address to 'addr' (hex)\n"
94 " -e ==> set entry point to 'ep' (hex)\n"
95 " -n ==> set image name to 'name'\n"
96 " -d ==> use image data from 'datafile'\n"
97 " -x ==> set XIP (execute in place)\n",
100 " %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"
101 " <dtb> file is used with -f auto, it may occur multiple times.\n",
104 " -D => set all options for device tree compiler\n"
105 " -f => input filename for FIT source\n"
106 " -i => input filename for ramdisk file\n"
107 " -E => place data outside of the FIT structure\n"
108 " -B => align size in hex for FIT structure and header\n");
109 #ifdef CONFIG_FIT_SIGNATURE
111 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
112 " -k => set directory containing private keys\n"
113 " -K => write public keys to this .dtb file\n"
114 " -G => use this signing key (in lieu of -k)\n"
115 " -c => add comment in signature node\n"
116 " -F => re-sign existing FIT image\n"
117 " -p => place external data at a static position\n"
118 " -r => mark keys used as 'required' in dtb\n"
119 " -N => openssl engine to use for signing\n");
122 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
124 fprintf(stderr, " %s -V ==> print version information and exit\n",
126 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
131 static int add_content(int type, const char *fname)
133 struct content_info *cont;
135 cont = calloc(1, sizeof(*cont));
140 if (params.content_tail)
141 params.content_tail->next = cont;
143 params.content_head = cont;
144 params.content_tail = cont;
149 static void process_args(int argc, char **argv)
152 int type = IH_TYPE_INVALID;
153 char *datafile = NULL;
156 while ((opt = getopt(argc, argv,
157 "a:A:b:B:c:C:d:D:e:Ef:FG:k:i:K:ln:N:p:o:O:rR:qstT:vVx")) != -1) {
160 params.addr = strtoull(optarg, &ptr, 16);
162 fprintf(stderr, "%s: invalid load address %s\n",
163 params.cmdname, optarg);
168 params.arch = genimg_get_arch_id(optarg);
169 if (params.arch < 0) {
170 show_valid_options(IH_ARCH);
171 usage("Invalid architecture");
175 if (add_content(IH_TYPE_FLATDT, optarg)) {
177 "%s: Out of memory adding content '%s'",
178 params.cmdname, optarg);
183 params.bl_len = strtoull(optarg, &ptr, 16);
185 fprintf(stderr, "%s: invalid block length %s\n",
186 params.cmdname, optarg);
192 params.comment = optarg;
195 params.comp = genimg_get_comp_id(optarg);
196 if (params.comp < 0) {
197 show_valid_options(IH_COMP);
198 usage("Invalid compression type");
202 params.datafile = optarg;
209 params.ep = strtoull(optarg, &ptr, 16);
211 fprintf(stderr, "%s: invalid entry point %s\n",
212 params.cmdname, optarg);
218 params.external_data = true;
222 params.auto_its = !strcmp(datafile, "auto");
226 * The flattened image tree (FIT) format
227 * requires a flattened device tree image type
229 params.type = IH_TYPE_FLATDT;
233 params.keyfile = optarg;
236 params.fit_ramdisk = optarg;
239 params.keydir = optarg;
242 params.keydest = optarg;
248 params.imagename = optarg;
251 params.engine_id = optarg;
254 params.algo_name = optarg;
257 params.os = genimg_get_os_id(optarg);
259 show_valid_options(IH_OS);
260 usage("Invalid operating system");
264 params.external_offset = strtoull(optarg, &ptr, 16);
266 fprintf(stderr, "%s: invalid offset size %s\n",
267 params.cmdname, optarg);
275 params.require_keys = 1;
279 * This entry is for the second configuration
280 * file, if only one is not enough.
282 params.imagename2 = optarg;
288 params.reset_timestamp = 1;
291 if (strcmp(optarg, "list") == 0) {
292 show_valid_options(IH_TYPE);
295 type = genimg_get_type_id(optarg);
297 show_valid_options(IH_TYPE);
298 usage("Invalid image type");
305 printf("mkimage version %s\n", PLAIN_VERSION);
311 usage("Invalid option");
315 /* The last parameter is expected to be the imagefile */
317 params.imagefile = argv[optind];
320 * For auto-generated FIT images we need to know the image type to put
321 * in the FIT, which is separate from the file's image type (which
322 * will always be IH_TYPE_FLATDT in this case).
324 if (params.type == IH_TYPE_FLATDT) {
325 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
326 /* For auto_its, datafile is always 'auto' */
327 if (!params.auto_its)
328 params.datafile = datafile;
329 else if (!params.datafile)
330 usage("Missing data file for auto-FIT (use -d)");
331 } else if (type != IH_TYPE_INVALID) {
332 if (type == IH_TYPE_SCRIPT && !params.datafile)
333 usage("Missing data file for script (use -d)");
337 if (!params.imagefile)
338 usage("Missing output filename");
341 int main(int argc, char **argv)
347 struct image_type_params *tparams = NULL;
352 params.cmdname = *argv;
356 process_args(argc, argv);
358 /* set tparams as per input type_id */
359 tparams = imagetool_get_type(params.type);
360 if (tparams == NULL) {
361 fprintf (stderr, "%s: unsupported type %s\n",
362 params.cmdname, genimg_get_type_name(params.type));
367 * check the passed arguments parameters meets the requirements
368 * as per image type to be generated/listed
370 if (tparams->check_params)
371 if (tparams->check_params (¶ms))
372 usage("Bad parameters for image type");
375 params.ep = params.addr;
376 /* If XIP, entry point must be after the U-Boot header */
378 params.ep += tparams->header_size;
382 if (tparams->fflag_handle)
384 * in some cases, some additional processing needs
385 * to be done if fflag is defined
387 * For ex. fit_handle_file for Fit file support
389 retval = tparams->fflag_handle(¶ms);
391 if (retval != EXIT_SUCCESS)
395 if (params.lflag || params.fflag) {
396 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
398 ifd = open (params.imagefile,
399 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
403 fprintf (stderr, "%s: Can't open %s: %s\n",
404 params.cmdname, params.imagefile,
409 if (params.lflag || params.fflag) {
412 * list header information of existing image
414 if (fstat(ifd, &sbuf) < 0) {
415 fprintf (stderr, "%s: Can't stat %s: %s\n",
416 params.cmdname, params.imagefile,
421 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
423 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
424 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
426 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
428 "%s: failed to get size of block device \"%s\"\n",
429 params.cmdname, params.imagefile);
434 "%s: \"%s\" is block device, don't know how to get its size\n",
435 params.cmdname, params.imagefile);
438 } else if ((unsigned)sbuf.st_size < tparams->header_size) {
440 "%s: Bad size: \"%s\" is not valid image: size %ld < %u\n",
441 params.cmdname, params.imagefile,
442 sbuf.st_size, tparams->header_size);
448 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
449 if (ptr == MAP_FAILED) {
450 fprintf (stderr, "%s: Can't read %s: %s\n",
451 params.cmdname, params.imagefile,
458 * Verifies the header format based on the expected header for image
461 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
465 * When listing the image, we are not given the image type. Simply check all
466 * image types to find one that matches our header
468 retval = imagetool_verify_print_header(ptr, &sbuf,
472 (void) munmap((void *)ptr, sbuf.st_size);
478 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
479 dfd = open(params.datafile, O_RDONLY | O_BINARY);
481 fprintf(stderr, "%s: Can't open %s: %s\n",
482 params.cmdname, params.datafile,
487 if (fstat(dfd, &sbuf) < 0) {
488 fprintf(stderr, "%s: Can't stat %s: %s\n",
489 params.cmdname, params.datafile,
494 params.file_size = sbuf.st_size + tparams->header_size;
499 * In case there an header with a variable
500 * length will be added, the corresponding
501 * function is called. This is responsible to
502 * allocate memory for the header itself.
504 if (tparams->vrec_header)
505 pad_len = tparams->vrec_header(¶ms, tparams);
507 memset(tparams->hdr, 0, tparams->header_size);
509 if (write(ifd, tparams->hdr, tparams->header_size)
510 != tparams->header_size) {
511 fprintf (stderr, "%s: Write error on %s: %s\n",
512 params.cmdname, params.imagefile, strerror(errno));
516 if (!params.skipcpy) {
517 if (params.type == IH_TYPE_MULTI ||
518 params.type == IH_TYPE_SCRIPT) {
519 char *file = params.datafile;
526 if ((sep = strchr(file, ':')) != NULL) {
530 if (stat (file, &sbuf) < 0) {
531 fprintf (stderr, "%s: Can't stat %s: %s\n",
532 params.cmdname, file, strerror(errno));
535 size = cpu_to_uimage (sbuf.st_size);
540 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
541 fprintf (stderr, "%s: Write error on %s: %s\n",
542 params.cmdname, params.imagefile,
559 file = params.datafile;
562 char *sep = strchr(file, ':');
565 copy_file (ifd, file, 1);
569 copy_file (ifd, file, 0);
573 } else if (params.type == IH_TYPE_PBLIMAGE) {
574 /* PBL has special Image format, implements its' own */
575 pbl_load_uboot(ifd, ¶ms);
576 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
577 /* Image file is meta, walk through actual targets */
580 ret = zynqmpbif_copy_image(ifd, ¶ms);
583 } else if (params.type == IH_TYPE_IMX8IMAGE) {
584 /* i.MX8/8X has special Image format */
587 ret = imx8image_copy_image(ifd, ¶ms);
590 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
591 /* i.MX8M has special Image format */
594 ret = imx8mimage_copy_image(ifd, ¶ms);
597 } else if ((params.type == IH_TYPE_RKSD) ||
598 (params.type == IH_TYPE_RKSPI)) {
599 /* Rockchip has special Image format */
602 ret = rockchip_copy_image(ifd, ¶ms);
606 copy_file(ifd, params.datafile, pad_len);
608 if (params.type == IH_TYPE_FIRMWARE_IVT) {
609 /* Add alignment and IVT */
610 uint32_t aligned_filesize = ALIGN(params.file_size,
612 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
613 params.addr, 0, 0, 0, params.addr
615 - tparams->header_size,
616 params.addr + aligned_filesize
617 - tparams->header_size
619 int i = params.file_size;
620 for (; i < aligned_filesize; i++) {
621 if (write(ifd, (char *) &i, 1) != 1) {
623 "%s: Write error on %s: %s\n",
630 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
631 != sizeof(flash_header_v2_t)) {
632 fprintf(stderr, "%s: Write error on %s: %s\n",
641 /* We're a bit of paranoid */
642 #if defined(_POSIX_SYNCHRONIZED_IO) && \
643 !defined(__sun__) && \
644 !defined(__FreeBSD__) && \
645 !defined(__OpenBSD__) && \
647 (void) fdatasync (ifd);
652 if (fstat(ifd, &sbuf) < 0) {
653 fprintf (stderr, "%s: Can't stat %s: %s\n",
654 params.cmdname, params.imagefile, strerror(errno));
657 params.file_size = sbuf.st_size;
659 map_len = sbuf.st_size;
660 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
661 if (ptr == MAP_FAILED) {
662 fprintf (stderr, "%s: Can't map %s: %s\n",
663 params.cmdname, params.imagefile, strerror(errno));
667 /* Setup the image header as per input image type*/
668 if (tparams->set_header)
669 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
671 fprintf (stderr, "%s: Can't set header for %s: %s\n",
672 params.cmdname, tparams->name, strerror(errno));
676 /* Print the image information by processing image header */
677 if (tparams->print_header)
678 tparams->print_header (ptr);
680 fprintf (stderr, "%s: Can't print header for %s\n",
681 params.cmdname, tparams->name);
684 (void)munmap((void *)ptr, map_len);
686 /* We're a bit of paranoid */
687 #if defined(_POSIX_SYNCHRONIZED_IO) && \
688 !defined(__sun__) && \
689 !defined(__FreeBSD__) && \
690 !defined(__OpenBSD__) && \
692 (void) fdatasync (ifd);
698 fprintf (stderr, "%s: Write error on %s: %s\n",
699 params.cmdname, params.imagefile, strerror(errno));
707 copy_file (int ifd, const char *datafile, int pad)
717 struct image_type_params *tparams = imagetool_get_type(params.type);
719 memset(zeros, 0, sizeof(zeros));
722 fprintf (stderr, "Adding Image %s\n", datafile);
725 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
726 fprintf (stderr, "%s: Can't open %s: %s\n",
727 params.cmdname, datafile, strerror(errno));
731 if (fstat(dfd, &sbuf) < 0) {
732 fprintf (stderr, "%s: Can't stat %s: %s\n",
733 params.cmdname, datafile, strerror(errno));
737 if (sbuf.st_size == 0) {
738 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
739 params.cmdname, datafile);
743 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
744 if (ptr == MAP_FAILED) {
745 fprintf (stderr, "%s: Can't read %s: %s\n",
746 params.cmdname, datafile, strerror(errno));
751 unsigned char *p = NULL;
753 * XIP: do not append the image_header_t at the
754 * beginning of the file, but consume the space
758 if ((unsigned)sbuf.st_size < tparams->header_size) {
760 "%s: Bad size: \"%s\" is too small for XIP\n",
761 params.cmdname, datafile);
765 for (p = ptr; p < ptr + tparams->header_size; p++) {
768 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
769 params.cmdname, datafile);
774 offset = tparams->header_size;
777 size = sbuf.st_size - offset;
779 ret = write(ifd, ptr + offset, size);
782 fprintf (stderr, "%s: Write error on %s: %s\n",
783 params.cmdname, params.imagefile, strerror(errno));
785 fprintf (stderr, "%s: Write only %d/%d bytes, "\
786 "probably no space left on the device\n",
787 params.cmdname, ret, size);
792 if ((pad == 1) && (tail != 0)) {
794 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
795 fprintf (stderr, "%s: Write error on %s: %s\n",
796 params.cmdname, params.imagefile,
800 } else if (pad > 1) {
802 int todo = sizeof(zeros);
806 if (write(ifd, (char *)&zeros, todo) != todo) {
807 fprintf(stderr, "%s: Write error on %s: %s\n",
808 params.cmdname, params.imagefile,
816 (void) munmap((void *)ptr, sbuf.st_size);