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",
90 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
91 " -A ==> set architecture to 'arch'\n"
92 " -O ==> set operating system to 'os'\n"
93 " -T ==> set image type to 'type'\n"
94 " -C ==> set compression type 'comp'\n"
95 " -a ==> set load address to 'addr' (hex)\n"
96 " -e ==> set entry point to 'ep' (hex)\n"
97 " -n ==> set image name to 'name'\n"
98 " -d ==> use image data from 'datafile'\n"
99 " -x ==> set XIP (execute in place)\n",
102 " %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"
103 " <dtb> file is used with -f auto, it may occur multiple times.\n",
106 " -D => set all options for device tree compiler\n"
107 " -f => input filename for FIT source\n"
108 " -i => input filename for ramdisk file\n"
109 " -E => place data outside of the FIT structure\n"
110 " -B => align size in hex for FIT structure and header\n");
111 #ifdef CONFIG_FIT_SIGNATURE
113 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
114 " -k => set directory containing private keys\n"
115 " -K => write public keys to this .dtb file\n"
116 " -G => use this signing key (in lieu of -k)\n"
117 " -c => add comment in signature node\n"
118 " -F => re-sign existing FIT image\n"
119 " -p => place external data at a static position\n"
120 " -r => mark keys used as 'required' in dtb\n"
121 " -N => openssl engine to use for signing\n");
124 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
126 fprintf(stderr, " %s -V ==> print version information and exit\n",
128 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
133 static int add_content(int type, const char *fname)
135 struct content_info *cont;
137 cont = calloc(1, sizeof(*cont));
142 if (params.content_tail)
143 params.content_tail->next = cont;
145 params.content_head = cont;
146 params.content_tail = cont;
151 static void process_args(int argc, char **argv)
154 int type = IH_TYPE_INVALID;
155 char *datafile = NULL;
158 while ((opt = getopt(argc, argv,
159 "a:A:b:B:c:C:d:D:e:Ef:FG:k:i:K:ln:N:p:o:O:rR:qstT:vVx")) != -1) {
162 params.addr = strtoull(optarg, &ptr, 16);
164 fprintf(stderr, "%s: invalid load address %s\n",
165 params.cmdname, optarg);
170 params.arch = genimg_get_arch_id(optarg);
171 if (params.arch < 0) {
172 show_valid_options(IH_ARCH);
173 usage("Invalid architecture");
178 if (add_content(IH_TYPE_FLATDT, optarg)) {
180 "%s: Out of memory adding content '%s'",
181 params.cmdname, optarg);
186 params.bl_len = strtoull(optarg, &ptr, 16);
188 fprintf(stderr, "%s: invalid block length %s\n",
189 params.cmdname, optarg);
195 params.comment = optarg;
198 params.comp = genimg_get_comp_id(optarg);
199 if (params.comp < 0) {
200 show_valid_options(IH_COMP);
201 usage("Invalid compression type");
205 params.datafile = optarg;
212 params.ep = strtoull(optarg, &ptr, 16);
214 fprintf(stderr, "%s: invalid entry point %s\n",
215 params.cmdname, optarg);
221 params.external_data = true;
225 params.auto_its = !strcmp(datafile, "auto");
229 * The flattened image tree (FIT) format
230 * requires a flattened device tree image type
232 params.type = IH_TYPE_FLATDT;
236 params.keyfile = optarg;
239 params.fit_ramdisk = optarg;
242 params.keydir = optarg;
245 params.keydest = optarg;
251 params.imagename = optarg;
254 params.engine_id = optarg;
257 params.algo_name = optarg;
260 params.os = genimg_get_os_id(optarg);
262 show_valid_options(IH_OS);
263 usage("Invalid operating system");
267 params.external_offset = strtoull(optarg, &ptr, 16);
269 fprintf(stderr, "%s: invalid offset size %s\n",
270 params.cmdname, optarg);
278 params.require_keys = 1;
282 * This entry is for the second configuration
283 * file, if only one is not enough.
285 params.imagename2 = optarg;
291 params.reset_timestamp = 1;
294 if (strcmp(optarg, "list") == 0) {
295 show_valid_options(IH_TYPE);
298 type = genimg_get_type_id(optarg);
300 show_valid_options(IH_TYPE);
301 usage("Invalid image type");
308 printf("mkimage version %s\n", PLAIN_VERSION);
314 usage("Invalid option");
318 /* The last parameter is expected to be the imagefile */
320 params.imagefile = argv[optind];
323 * For auto-generated FIT images we need to know the image type to put
324 * in the FIT, which is separate from the file's image type (which
325 * will always be IH_TYPE_FLATDT in this case).
327 if (params.type == IH_TYPE_FLATDT) {
328 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
329 /* For auto_its, datafile is always 'auto' */
330 if (!params.auto_its)
331 params.datafile = datafile;
332 else if (!params.datafile)
333 usage("Missing data file for auto-FIT (use -d)");
334 } else if (params.lflag || type != IH_TYPE_INVALID) {
335 if (type == IH_TYPE_SCRIPT && !params.datafile)
336 usage("Missing data file for script (use -d)");
340 if (!params.imagefile)
341 usage("Missing output filename");
344 int main(int argc, char **argv)
350 struct image_type_params *tparams = NULL;
355 params.cmdname = *argv;
359 process_args(argc, argv);
361 /* set tparams as per input type_id */
362 tparams = imagetool_get_type(params.type);
363 if (tparams == NULL && !params.lflag) {
364 fprintf (stderr, "%s: unsupported type %s\n",
365 params.cmdname, genimg_get_type_name(params.type));
370 * check the passed arguments parameters meets the requirements
371 * as per image type to be generated/listed
373 if (tparams && tparams->check_params)
374 if (tparams->check_params (¶ms))
375 usage("Bad parameters for image type");
378 params.ep = params.addr;
379 /* If XIP, entry point must be after the U-Boot header */
380 if (params.xflag && tparams)
381 params.ep += tparams->header_size;
386 fprintf(stderr, "%s: Missing FIT support\n",
390 if (tparams->fflag_handle)
392 * in some cases, some additional processing needs
393 * to be done if fflag is defined
395 * For ex. fit_handle_file for Fit file support
397 retval = tparams->fflag_handle(¶ms);
399 if (retval != EXIT_SUCCESS)
400 usage("Bad parameters for FIT image type");
403 if (params.lflag || params.fflag) {
404 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
406 ifd = open (params.imagefile,
407 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
411 fprintf (stderr, "%s: Can't open %s: %s\n",
412 params.cmdname, params.imagefile,
417 if (params.lflag || params.fflag) {
420 * list header information of existing image
422 if (fstat(ifd, &sbuf) < 0) {
423 fprintf (stderr, "%s: Can't stat %s: %s\n",
424 params.cmdname, params.imagefile,
429 if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
431 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
432 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
434 if (ioctl(ifd, BLKGETSIZE64, &size) < 0) {
436 "%s: failed to get size of block device \"%s\"\n",
437 params.cmdname, params.imagefile);
442 "%s: \"%s\" is block device, don't know how to get its size\n",
443 params.cmdname, params.imagefile);
446 } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) {
448 "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n",
449 params.cmdname, params.imagefile,
450 (unsigned long long) sbuf.st_size,
451 tparams->header_size);
457 ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0);
458 if (ptr == MAP_FAILED) {
459 fprintf (stderr, "%s: Can't read %s: %s\n",
460 params.cmdname, params.imagefile,
466 * Verifies the header format based on the expected header for image
467 * type in tparams. If tparams is NULL simply check all image types
468 * to find one that matches our header.
470 retval = imagetool_verify_print_header(ptr, &sbuf, tparams, ¶ms);
472 (void) munmap((void *)ptr, sbuf.st_size);
475 summary_show(¶ms.summary, params.imagefile,
481 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
482 dfd = open(params.datafile, O_RDONLY | O_BINARY);
484 fprintf(stderr, "%s: Can't open %s: %s\n",
485 params.cmdname, params.datafile,
490 if (fstat(dfd, &sbuf) < 0) {
491 fprintf(stderr, "%s: Can't stat %s: %s\n",
492 params.cmdname, params.datafile,
497 params.file_size = sbuf.st_size + tparams->header_size;
502 * In case there an header with a variable
503 * length will be added, the corresponding
504 * function is called. This is responsible to
505 * allocate memory for the header itself.
507 if (tparams->vrec_header)
508 pad_len = tparams->vrec_header(¶ms, tparams);
510 memset(tparams->hdr, 0, tparams->header_size);
512 if (write(ifd, tparams->hdr, tparams->header_size)
513 != tparams->header_size) {
514 fprintf (stderr, "%s: Write error on %s: %s\n",
515 params.cmdname, params.imagefile, strerror(errno));
519 if (!params.skipcpy) {
520 if (params.type == IH_TYPE_MULTI ||
521 params.type == IH_TYPE_SCRIPT) {
522 char *file = params.datafile;
529 if ((sep = strchr(file, ':')) != NULL) {
533 if (stat (file, &sbuf) < 0) {
534 fprintf (stderr, "%s: Can't stat %s: %s\n",
535 params.cmdname, file, strerror(errno));
538 size = cpu_to_uimage (sbuf.st_size);
543 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
544 fprintf (stderr, "%s: Write error on %s: %s\n",
545 params.cmdname, params.imagefile,
562 file = params.datafile;
565 char *sep = strchr(file, ':');
568 copy_file (ifd, file, 1);
572 copy_file (ifd, file, 0);
576 } else if (params.type == IH_TYPE_PBLIMAGE) {
577 /* PBL has special Image format, implements its' own */
578 pbl_load_uboot(ifd, ¶ms);
579 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
580 /* Image file is meta, walk through actual targets */
583 ret = zynqmpbif_copy_image(ifd, ¶ms);
586 } else if (params.type == IH_TYPE_IMX8IMAGE) {
587 /* i.MX8/8X has special Image format */
590 ret = imx8image_copy_image(ifd, ¶ms);
593 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
594 /* i.MX8M has special Image format */
597 ret = imx8mimage_copy_image(ifd, ¶ms);
600 } else if ((params.type == IH_TYPE_RKSD) ||
601 (params.type == IH_TYPE_RKSPI)) {
602 /* Rockchip has special Image format */
605 ret = rockchip_copy_image(ifd, ¶ms);
609 copy_file(ifd, params.datafile, pad_len);
611 if (params.type == IH_TYPE_FIRMWARE_IVT) {
612 /* Add alignment and IVT */
613 uint32_t aligned_filesize = ALIGN(params.file_size,
615 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
616 params.addr, 0, 0, 0, params.addr
618 - tparams->header_size,
619 params.addr + aligned_filesize
620 - tparams->header_size
622 int i = params.file_size;
623 for (; i < aligned_filesize; i++) {
624 if (write(ifd, (char *) &i, 1) != 1) {
626 "%s: Write error on %s: %s\n",
633 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
634 != sizeof(flash_header_v2_t)) {
635 fprintf(stderr, "%s: Write error on %s: %s\n",
644 /* We're a bit of paranoid */
645 #if defined(_POSIX_SYNCHRONIZED_IO) && \
646 !defined(__sun__) && \
647 !defined(__FreeBSD__) && \
648 !defined(__OpenBSD__) && \
650 (void) fdatasync (ifd);
655 if (fstat(ifd, &sbuf) < 0) {
656 fprintf (stderr, "%s: Can't stat %s: %s\n",
657 params.cmdname, params.imagefile, strerror(errno));
660 params.file_size = sbuf.st_size;
662 map_len = sbuf.st_size;
663 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
664 if (ptr == MAP_FAILED) {
665 fprintf (stderr, "%s: Can't map %s: %s\n",
666 params.cmdname, params.imagefile, strerror(errno));
670 /* Setup the image header as per input image type*/
671 if (tparams->set_header)
672 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
674 fprintf (stderr, "%s: Can't set header for %s: %s\n",
675 params.cmdname, tparams->name, strerror(errno));
679 /* Print the image information by processing image header */
680 if (tparams->print_header)
681 tparams->print_header (ptr);
683 fprintf (stderr, "%s: Can't print header for %s\n",
684 params.cmdname, tparams->name);
687 (void)munmap((void *)ptr, map_len);
689 /* We're a bit of paranoid */
690 #if defined(_POSIX_SYNCHRONIZED_IO) && \
691 !defined(__sun__) && \
692 !defined(__FreeBSD__) && \
693 !defined(__OpenBSD__) && \
695 (void) fdatasync (ifd);
701 fprintf (stderr, "%s: Write error on %s: %s\n",
702 params.cmdname, params.imagefile, strerror(errno));
710 copy_file (int ifd, const char *datafile, int pad)
720 struct image_type_params *tparams = imagetool_get_type(params.type);
722 memset(zeros, 0, sizeof(zeros));
725 fprintf (stderr, "Adding Image %s\n", datafile);
728 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
729 fprintf (stderr, "%s: Can't open %s: %s\n",
730 params.cmdname, datafile, strerror(errno));
734 if (fstat(dfd, &sbuf) < 0) {
735 fprintf (stderr, "%s: Can't stat %s: %s\n",
736 params.cmdname, datafile, strerror(errno));
740 if (sbuf.st_size == 0) {
741 fprintf (stderr, "%s: Input file %s is empty, bailing out\n",
742 params.cmdname, datafile);
746 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
747 if (ptr == MAP_FAILED) {
748 fprintf (stderr, "%s: Can't read %s: %s\n",
749 params.cmdname, datafile, strerror(errno));
754 unsigned char *p = NULL;
756 * XIP: do not append the image_header_t at the
757 * beginning of the file, but consume the space
761 if ((unsigned)sbuf.st_size < tparams->header_size) {
763 "%s: Bad size: \"%s\" is too small for XIP\n",
764 params.cmdname, datafile);
768 for (p = ptr; p < ptr + tparams->header_size; p++) {
771 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
772 params.cmdname, datafile);
777 offset = tparams->header_size;
780 size = sbuf.st_size - offset;
782 ret = write(ifd, ptr + offset, size);
785 fprintf (stderr, "%s: Write error on %s: %s\n",
786 params.cmdname, params.imagefile, strerror(errno));
788 fprintf (stderr, "%s: Write only %d/%d bytes, "\
789 "probably no space left on the device\n",
790 params.cmdname, ret, size);
795 if ((pad == 1) && (tail != 0)) {
797 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
798 fprintf (stderr, "%s: Write error on %s: %s\n",
799 params.cmdname, params.imagefile,
803 } else if (pad > 1) {
805 int todo = sizeof(zeros);
809 if (write(ifd, (char *)&zeros, todo) != todo) {
810 fprintf(stderr, "%s: Write error on %s: %s\n",
811 params.cmdname, params.imagefile,
819 (void) munmap((void *)ptr, sbuf.st_size);