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 #define OPT_STRING "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx"
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: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.os = genimg_get_os_id(optarg);
257 show_valid_options(IH_OS);
258 usage("Invalid operating system");
262 params.external_offset = strtoull(optarg, &ptr, 16);
264 fprintf(stderr, "%s: invalid offset size %s\n",
265 params.cmdname, optarg);
273 params.require_keys = 1;
277 * This entry is for the second configuration
278 * file, if only one is not enough.
280 params.imagename2 = optarg;
286 params.reset_timestamp = 1;
289 if (strcmp(optarg, "list") == 0) {
290 show_valid_options(IH_TYPE);
293 type = genimg_get_type_id(optarg);
295 show_valid_options(IH_TYPE);
296 usage("Invalid image type");
303 printf("mkimage version %s\n", PLAIN_VERSION);
309 usage("Invalid option");
313 /* The last parameter is expected to be the imagefile */
315 params.imagefile = argv[optind];
318 * For auto-generated FIT images we need to know the image type to put
319 * in the FIT, which is separate from the file's image type (which
320 * will always be IH_TYPE_FLATDT in this case).
322 if (params.type == IH_TYPE_FLATDT) {
323 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
324 /* For auto_its, datafile is always 'auto' */
325 if (!params.auto_its)
326 params.datafile = datafile;
327 else if (!params.datafile)
328 usage("Missing data file for auto-FIT (use -d)");
329 } else if (type != IH_TYPE_INVALID) {
330 if (type == IH_TYPE_SCRIPT && !params.datafile)
331 usage("Missing data file for script (use -d)");
335 if (!params.imagefile)
336 usage("Missing output filename");
339 int main(int argc, char **argv)
345 struct image_type_params *tparams = NULL;
350 params.cmdname = *argv;
354 process_args(argc, argv);
356 /* set tparams as per input type_id */
357 tparams = imagetool_get_type(params.type);
358 if (tparams == NULL) {
359 fprintf (stderr, "%s: unsupported type %s\n",
360 params.cmdname, genimg_get_type_name(params.type));
365 * check the passed arguments parameters meets the requirements
366 * as per image type to be generated/listed
368 if (tparams->check_params)
369 if (tparams->check_params (¶ms))
370 usage("Bad parameters for image type");
373 params.ep = params.addr;
374 /* If XIP, entry point must be after the U-Boot header */
376 params.ep += tparams->header_size;
380 if (tparams->fflag_handle)
382 * in some cases, some additional processing needs
383 * to be done if fflag is defined
385 * For ex. fit_handle_file for Fit file support
387 retval = tparams->fflag_handle(¶ms);
389 if (retval != EXIT_SUCCESS)
393 if (params.lflag || params.fflag) {
394 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
396 ifd = open (params.imagefile,
397 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
401 fprintf (stderr, "%s: Can't open %s: %s\n",
402 params.cmdname, params.imagefile,
407 if (params.lflag || params.fflag) {
410 * list header information of existing image
412 if (fstat(ifd, &sbuf) < 0) {
413 fprintf (stderr, "%s: Can't stat %s: %s\n",
414 params.cmdname, params.imagefile,
419 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
421 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
422 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
424 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
426 "%s: failed to get size of block device \"%s\"\n",
427 params.cmdname, params.imagefile);
432 "%s: \"%s\" is block device, don't know how to get its size\n",
433 params.cmdname, params.imagefile);
436 } else if ((unsigned)sbuf.st_size < tparams->header_size) {
438 "%s: Bad size: \"%s\" is not valid image: size %ld < %u\n",
439 params.cmdname, params.imagefile,
440 sbuf.st_size, tparams->header_size);
446 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
447 if (ptr == MAP_FAILED) {
448 fprintf (stderr, "%s: Can't read %s: %s\n",
449 params.cmdname, params.imagefile,
456 * Verifies the header format based on the expected header for image
459 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
463 * When listing the image, we are not given the image type. Simply check all
464 * image types to find one that matches our header
466 retval = imagetool_verify_print_header(ptr, &sbuf,
470 (void) munmap((void *)ptr, sbuf.st_size);
476 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
477 dfd = open(params.datafile, O_RDONLY | O_BINARY);
479 fprintf(stderr, "%s: Can't open %s: %s\n",
480 params.cmdname, params.datafile,
485 if (fstat(dfd, &sbuf) < 0) {
486 fprintf(stderr, "%s: Can't stat %s: %s\n",
487 params.cmdname, params.datafile,
492 params.file_size = sbuf.st_size + tparams->header_size;
497 * In case there an header with a variable
498 * length will be added, the corresponding
499 * function is called. This is responsible to
500 * allocate memory for the header itself.
502 if (tparams->vrec_header)
503 pad_len = tparams->vrec_header(¶ms, tparams);
505 memset(tparams->hdr, 0, tparams->header_size);
507 if (write(ifd, tparams->hdr, tparams->header_size)
508 != tparams->header_size) {
509 fprintf (stderr, "%s: Write error on %s: %s\n",
510 params.cmdname, params.imagefile, strerror(errno));
514 if (!params.skipcpy) {
515 if (params.type == IH_TYPE_MULTI ||
516 params.type == IH_TYPE_SCRIPT) {
517 char *file = params.datafile;
524 if ((sep = strchr(file, ':')) != NULL) {
528 if (stat (file, &sbuf) < 0) {
529 fprintf (stderr, "%s: Can't stat %s: %s\n",
530 params.cmdname, file, strerror(errno));
533 size = cpu_to_uimage (sbuf.st_size);
538 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
539 fprintf (stderr, "%s: Write error on %s: %s\n",
540 params.cmdname, params.imagefile,
557 file = params.datafile;
560 char *sep = strchr(file, ':');
563 copy_file (ifd, file, 1);
567 copy_file (ifd, file, 0);
571 } else if (params.type == IH_TYPE_PBLIMAGE) {
572 /* PBL has special Image format, implements its' own */
573 pbl_load_uboot(ifd, ¶ms);
574 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
575 /* Image file is meta, walk through actual targets */
578 ret = zynqmpbif_copy_image(ifd, ¶ms);
581 } else if (params.type == IH_TYPE_IMX8IMAGE) {
582 /* i.MX8/8X has special Image format */
585 ret = imx8image_copy_image(ifd, ¶ms);
588 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
589 /* i.MX8M has special Image format */
592 ret = imx8mimage_copy_image(ifd, ¶ms);
595 } else if ((params.type == IH_TYPE_RKSD) ||
596 (params.type == IH_TYPE_RKSPI)) {
597 /* Rockchip has special Image format */
600 ret = rockchip_copy_image(ifd, ¶ms);
604 copy_file(ifd, params.datafile, pad_len);
606 if (params.type == IH_TYPE_FIRMWARE_IVT) {
607 /* Add alignment and IVT */
608 uint32_t aligned_filesize = ALIGN(params.file_size,
610 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
611 params.addr, 0, 0, 0, params.addr
613 - tparams->header_size,
614 params.addr + aligned_filesize
615 - tparams->header_size
617 int i = params.file_size;
618 for (; i < aligned_filesize; i++) {
619 if (write(ifd, (char *) &i, 1) != 1) {
621 "%s: Write error on %s: %s\n",
628 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
629 != sizeof(flash_header_v2_t)) {
630 fprintf(stderr, "%s: Write error on %s: %s\n",
639 /* We're a bit of paranoid */
640 #if defined(_POSIX_SYNCHRONIZED_IO) && \
641 !defined(__sun__) && \
642 !defined(__FreeBSD__) && \
643 !defined(__OpenBSD__) && \
645 (void) fdatasync (ifd);
650 if (fstat(ifd, &sbuf) < 0) {
651 fprintf (stderr, "%s: Can't stat %s: %s\n",
652 params.cmdname, params.imagefile, strerror(errno));
655 params.file_size = sbuf.st_size;
657 map_len = sbuf.st_size;
658 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
659 if (ptr == MAP_FAILED) {
660 fprintf (stderr, "%s: Can't map %s: %s\n",
661 params.cmdname, params.imagefile, strerror(errno));
665 /* Setup the image header as per input image type*/
666 if (tparams->set_header)
667 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
669 fprintf (stderr, "%s: Can't set header for %s: %s\n",
670 params.cmdname, tparams->name, strerror(errno));
674 /* Print the image information by processing image header */
675 if (tparams->print_header)
676 tparams->print_header (ptr);
678 fprintf (stderr, "%s: Can't print header for %s\n",
679 params.cmdname, tparams->name);
682 (void)munmap((void *)ptr, map_len);
684 /* We're a bit of paranoid */
685 #if defined(_POSIX_SYNCHRONIZED_IO) && \
686 !defined(__sun__) && \
687 !defined(__FreeBSD__) && \
688 !defined(__OpenBSD__) && \
690 (void) fdatasync (ifd);
696 fprintf (stderr, "%s: Write error on %s: %s\n",
697 params.cmdname, params.imagefile, strerror(errno));
705 copy_file (int ifd, const char *datafile, int pad)
715 struct image_type_params *tparams = imagetool_get_type(params.type);
717 memset(zeros, 0, sizeof(zeros));
720 fprintf (stderr, "Adding Image %s\n", datafile);
723 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
724 fprintf (stderr, "%s: Can't open %s: %s\n",
725 params.cmdname, datafile, strerror(errno));
729 if (fstat(dfd, &sbuf) < 0) {
730 fprintf (stderr, "%s: Can't stat %s: %s\n",
731 params.cmdname, datafile, strerror(errno));
735 if (sbuf.st_size == 0) {
736 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
737 params.cmdname, datafile);
741 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
742 if (ptr == MAP_FAILED) {
743 fprintf (stderr, "%s: Can't read %s: %s\n",
744 params.cmdname, datafile, strerror(errno));
749 unsigned char *p = NULL;
751 * XIP: do not append the image_header_t at the
752 * beginning of the file, but consume the space
756 if ((unsigned)sbuf.st_size < tparams->header_size) {
758 "%s: Bad size: \"%s\" is too small for XIP\n",
759 params.cmdname, datafile);
763 for (p = ptr; p < ptr + tparams->header_size; p++) {
766 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
767 params.cmdname, datafile);
772 offset = tparams->header_size;
775 size = sbuf.st_size - offset;
777 ret = write(ifd, ptr + offset, size);
780 fprintf (stderr, "%s: Write error on %s: %s\n",
781 params.cmdname, params.imagefile, strerror(errno));
783 fprintf (stderr, "%s: Write only %d/%d bytes, "\
784 "probably no space left on the device\n",
785 params.cmdname, ret, size);
790 if ((pad == 1) && (tail != 0)) {
792 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
793 fprintf (stderr, "%s: Write error on %s: %s\n",
794 params.cmdname, params.imagefile,
798 } else if (pad > 1) {
800 int todo = sizeof(zeros);
804 if (write(ifd, (char *)&zeros, todo) != todo) {
805 fprintf(stderr, "%s: Write error on %s: %s\n",
806 params.cmdname, params.imagefile,
814 (void) munmap((void *)ptr, sbuf.st_size);