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 static void copy_file(int, const char *, int);
18 /* parameters initialized by core will be used by the image type code */
19 static struct image_tool_params params = {
22 .type = IH_TYPE_KERNEL,
24 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
29 static enum ih_category cur_category;
31 static int h_compare_category_name(const void *vtype1, const void *vtype2)
33 const int *type1 = vtype1;
34 const int *type2 = vtype2;
35 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
36 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
38 return strcmp(name1, name2);
41 static int show_valid_options(enum ih_category category)
48 count = genimg_get_cat_count(category);
49 order = calloc(count, sizeof(*order));
53 /* Sort the names in order of short name for easier reading */
54 for (i = 0, item = 0; i < count; i++, item++) {
55 while (!genimg_cat_has_id(category, item) && i < count) {
61 cur_category = category;
62 qsort(order, count, sizeof(int), h_compare_category_name);
64 fprintf(stderr, "\nInvalid %s, supported are:\n",
65 genimg_get_cat_desc(category));
66 for (i = 0; i < count; i++) {
68 fprintf(stderr, "\t%-15s %s\n",
69 genimg_get_cat_short_name(category, item),
70 genimg_get_cat_name(category, item));
72 fprintf(stderr, "\n");
78 static void usage(const char *msg)
80 fprintf(stderr, "Error: %s\n", msg);
81 fprintf(stderr, "Usage: %s -l image\n"
82 " -l ==> list image header information\n",
85 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
86 " -A ==> set architecture to 'arch'\n"
87 " -O ==> set operating system to 'os'\n"
88 " -T ==> set image type to 'type'\n"
89 " -C ==> set compression type 'comp'\n"
90 " -a ==> set load address to 'addr' (hex)\n"
91 " -e ==> set entry point to 'ep' (hex)\n"
92 " -n ==> set image name to 'name'\n"
93 " -d ==> use image data from 'datafile'\n"
94 " -x ==> set XIP (execute in place)\n",
97 " %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"
98 " <dtb> file is used with -f auto, it may occur multiple times.\n",
101 " -D => set all options for device tree compiler\n"
102 " -f => input filename for FIT source\n"
103 " -i => input filename for ramdisk file\n"
104 " -E => place data outside of the FIT structure\n"
105 " -B => align size in hex for FIT structure and header\n");
106 #ifdef CONFIG_FIT_SIGNATURE
108 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
109 " -k => set directory containing private keys\n"
110 " -K => write public keys to this .dtb file\n"
111 " -c => add comment in signature node\n"
112 " -F => re-sign existing FIT image\n"
113 " -p => place external data at a static position\n"
114 " -r => mark keys used as 'required' in dtb\n"
115 " -N => openssl engine to use for signing\n");
118 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
120 fprintf(stderr, " %s -V ==> print version information and exit\n",
122 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
127 static int add_content(int type, const char *fname)
129 struct content_info *cont;
131 cont = calloc(1, sizeof(*cont));
136 if (params.content_tail)
137 params.content_tail->next = cont;
139 params.content_head = cont;
140 params.content_tail = cont;
145 #define OPT_STRING "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx"
146 static void process_args(int argc, char **argv)
149 int type = IH_TYPE_INVALID;
150 char *datafile = NULL;
153 while ((opt = getopt(argc, argv,
154 "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx")) != -1) {
157 params.addr = strtoull(optarg, &ptr, 16);
159 fprintf(stderr, "%s: invalid load address %s\n",
160 params.cmdname, optarg);
165 params.arch = genimg_get_arch_id(optarg);
166 if (params.arch < 0) {
167 show_valid_options(IH_ARCH);
168 usage("Invalid architecture");
172 if (add_content(IH_TYPE_FLATDT, optarg)) {
174 "%s: Out of memory adding content '%s'",
175 params.cmdname, optarg);
180 params.bl_len = strtoull(optarg, &ptr, 16);
182 fprintf(stderr, "%s: invalid block length %s\n",
183 params.cmdname, optarg);
189 params.comment = optarg;
192 params.comp = genimg_get_comp_id(optarg);
193 if (params.comp < 0) {
194 show_valid_options(IH_COMP);
195 usage("Invalid compression type");
199 params.datafile = optarg;
206 params.ep = strtoull(optarg, &ptr, 16);
208 fprintf(stderr, "%s: invalid entry point %s\n",
209 params.cmdname, optarg);
215 params.external_data = true;
219 params.auto_its = !strcmp(datafile, "auto");
223 * The flattened image tree (FIT) format
224 * requires a flattened device tree image type
226 params.type = IH_TYPE_FLATDT;
230 params.fit_ramdisk = optarg;
233 params.keydir = optarg;
236 params.keydest = optarg;
242 params.imagename = optarg;
245 params.engine_id = optarg;
248 params.os = genimg_get_os_id(optarg);
250 show_valid_options(IH_OS);
251 usage("Invalid operating system");
255 params.external_offset = strtoull(optarg, &ptr, 16);
257 fprintf(stderr, "%s: invalid offset size %s\n",
258 params.cmdname, optarg);
266 params.require_keys = 1;
270 * This entry is for the second configuration
271 * file, if only one is not enough.
273 params.imagename2 = optarg;
279 params.reset_timestamp = 1;
282 if (strcmp(optarg, "list") == 0) {
283 show_valid_options(IH_TYPE);
286 type = genimg_get_type_id(optarg);
288 show_valid_options(IH_TYPE);
289 usage("Invalid image type");
296 printf("mkimage version %s\n", PLAIN_VERSION);
302 usage("Invalid option");
306 /* The last parameter is expected to be the imagefile */
308 params.imagefile = argv[optind];
311 * For auto-generated FIT images we need to know the image type to put
312 * in the FIT, which is separate from the file's image type (which
313 * will always be IH_TYPE_FLATDT in this case).
315 if (params.type == IH_TYPE_FLATDT) {
316 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
317 /* For auto_its, datafile is always 'auto' */
318 if (!params.auto_its)
319 params.datafile = datafile;
320 else if (!params.datafile)
321 usage("Missing data file for auto-FIT (use -d)");
322 } else if (type != IH_TYPE_INVALID) {
323 if (type == IH_TYPE_SCRIPT && !params.datafile)
324 usage("Missing data file for script (use -d)");
328 if (!params.imagefile)
329 usage("Missing output filename");
332 int main(int argc, char **argv)
338 struct image_type_params *tparams = NULL;
343 params.cmdname = *argv;
347 process_args(argc, argv);
349 /* set tparams as per input type_id */
350 tparams = imagetool_get_type(params.type);
351 if (tparams == NULL) {
352 fprintf (stderr, "%s: unsupported type %s\n",
353 params.cmdname, genimg_get_type_name(params.type));
358 * check the passed arguments parameters meets the requirements
359 * as per image type to be generated/listed
361 if (tparams->check_params)
362 if (tparams->check_params (¶ms))
363 usage("Bad parameters for image type");
366 params.ep = params.addr;
367 /* If XIP, entry point must be after the U-Boot header */
369 params.ep += tparams->header_size;
373 if (tparams->fflag_handle)
375 * in some cases, some additional processing needs
376 * to be done if fflag is defined
378 * For ex. fit_handle_file for Fit file support
380 retval = tparams->fflag_handle(¶ms);
382 if (retval != EXIT_SUCCESS)
386 if (params.lflag || params.fflag) {
387 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
389 ifd = open (params.imagefile,
390 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
394 fprintf (stderr, "%s: Can't open %s: %s\n",
395 params.cmdname, params.imagefile,
400 if (params.lflag || params.fflag) {
402 * list header information of existing image
404 if (fstat(ifd, &sbuf) < 0) {
405 fprintf (stderr, "%s: Can't stat %s: %s\n",
406 params.cmdname, params.imagefile,
411 if ((unsigned)sbuf.st_size < tparams->header_size) {
413 "%s: Bad size: \"%s\" is not valid image\n",
414 params.cmdname, params.imagefile);
418 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
419 if (ptr == MAP_FAILED) {
420 fprintf (stderr, "%s: Can't read %s: %s\n",
421 params.cmdname, params.imagefile,
428 * Verifies the header format based on the expected header for image
431 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
435 * When listing the image, we are not given the image type. Simply check all
436 * image types to find one that matches our header
438 retval = imagetool_verify_print_header(ptr, &sbuf,
442 (void) munmap((void *)ptr, sbuf.st_size);
448 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
449 dfd = open(params.datafile, O_RDONLY | O_BINARY);
451 fprintf(stderr, "%s: Can't open %s: %s\n",
452 params.cmdname, params.datafile,
457 if (fstat(dfd, &sbuf) < 0) {
458 fprintf(stderr, "%s: Can't stat %s: %s\n",
459 params.cmdname, params.datafile,
464 params.file_size = sbuf.st_size + tparams->header_size;
469 * In case there an header with a variable
470 * length will be added, the corresponding
471 * function is called. This is responsible to
472 * allocate memory for the header itself.
474 if (tparams->vrec_header)
475 pad_len = tparams->vrec_header(¶ms, tparams);
477 memset(tparams->hdr, 0, tparams->header_size);
479 if (write(ifd, tparams->hdr, tparams->header_size)
480 != tparams->header_size) {
481 fprintf (stderr, "%s: Write error on %s: %s\n",
482 params.cmdname, params.imagefile, strerror(errno));
486 if (!params.skipcpy) {
487 if (params.type == IH_TYPE_MULTI ||
488 params.type == IH_TYPE_SCRIPT) {
489 char *file = params.datafile;
496 if ((sep = strchr(file, ':')) != NULL) {
500 if (stat (file, &sbuf) < 0) {
501 fprintf (stderr, "%s: Can't stat %s: %s\n",
502 params.cmdname, file, strerror(errno));
505 size = cpu_to_uimage (sbuf.st_size);
510 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
511 fprintf (stderr, "%s: Write error on %s: %s\n",
512 params.cmdname, params.imagefile,
529 file = params.datafile;
532 char *sep = strchr(file, ':');
535 copy_file (ifd, file, 1);
539 copy_file (ifd, file, 0);
543 } else if (params.type == IH_TYPE_PBLIMAGE) {
544 /* PBL has special Image format, implements its' own */
545 pbl_load_uboot(ifd, ¶ms);
546 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
547 /* Image file is meta, walk through actual targets */
550 ret = zynqmpbif_copy_image(ifd, ¶ms);
553 } else if (params.type == IH_TYPE_IMX8IMAGE) {
554 /* i.MX8/8X has special Image format */
557 ret = imx8image_copy_image(ifd, ¶ms);
560 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
561 /* i.MX8M has special Image format */
564 ret = imx8mimage_copy_image(ifd, ¶ms);
567 } else if ((params.type == IH_TYPE_RKSD) ||
568 (params.type == IH_TYPE_RKSPI)) {
569 /* Rockchip has special Image format */
572 ret = rockchip_copy_image(ifd, ¶ms);
576 copy_file(ifd, params.datafile, pad_len);
578 if (params.type == IH_TYPE_FIRMWARE_IVT) {
579 /* Add alignment and IVT */
580 uint32_t aligned_filesize = ALIGN(params.file_size,
582 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
583 params.addr, 0, 0, 0, params.addr
585 - tparams->header_size,
586 params.addr + aligned_filesize
587 - tparams->header_size
589 int i = params.file_size;
590 for (; i < aligned_filesize; i++) {
591 if (write(ifd, (char *) &i, 1) != 1) {
593 "%s: Write error on %s: %s\n",
600 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
601 != sizeof(flash_header_v2_t)) {
602 fprintf(stderr, "%s: Write error on %s: %s\n",
611 /* We're a bit of paranoid */
612 #if defined(_POSIX_SYNCHRONIZED_IO) && \
613 !defined(__sun__) && \
614 !defined(__FreeBSD__) && \
615 !defined(__OpenBSD__) && \
617 (void) fdatasync (ifd);
622 if (fstat(ifd, &sbuf) < 0) {
623 fprintf (stderr, "%s: Can't stat %s: %s\n",
624 params.cmdname, params.imagefile, strerror(errno));
627 params.file_size = sbuf.st_size;
629 map_len = sbuf.st_size;
630 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
631 if (ptr == MAP_FAILED) {
632 fprintf (stderr, "%s: Can't map %s: %s\n",
633 params.cmdname, params.imagefile, strerror(errno));
637 /* Setup the image header as per input image type*/
638 if (tparams->set_header)
639 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
641 fprintf (stderr, "%s: Can't set header for %s: %s\n",
642 params.cmdname, tparams->name, strerror(errno));
646 /* Print the image information by processing image header */
647 if (tparams->print_header)
648 tparams->print_header (ptr);
650 fprintf (stderr, "%s: Can't print header for %s\n",
651 params.cmdname, tparams->name);
654 (void)munmap((void *)ptr, map_len);
656 /* We're a bit of paranoid */
657 #if defined(_POSIX_SYNCHRONIZED_IO) && \
658 !defined(__sun__) && \
659 !defined(__FreeBSD__) && \
660 !defined(__OpenBSD__) && \
662 (void) fdatasync (ifd);
668 fprintf (stderr, "%s: Write error on %s: %s\n",
669 params.cmdname, params.imagefile, strerror(errno));
677 copy_file (int ifd, const char *datafile, int pad)
687 struct image_type_params *tparams = imagetool_get_type(params.type);
689 memset(zeros, 0, sizeof(zeros));
692 fprintf (stderr, "Adding Image %s\n", datafile);
695 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
696 fprintf (stderr, "%s: Can't open %s: %s\n",
697 params.cmdname, datafile, strerror(errno));
701 if (fstat(dfd, &sbuf) < 0) {
702 fprintf (stderr, "%s: Can't stat %s: %s\n",
703 params.cmdname, datafile, strerror(errno));
707 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
708 if (ptr == MAP_FAILED) {
709 fprintf (stderr, "%s: Can't read %s: %s\n",
710 params.cmdname, datafile, strerror(errno));
715 unsigned char *p = NULL;
717 * XIP: do not append the image_header_t at the
718 * beginning of the file, but consume the space
722 if ((unsigned)sbuf.st_size < tparams->header_size) {
724 "%s: Bad size: \"%s\" is too small for XIP\n",
725 params.cmdname, datafile);
729 for (p = ptr; p < ptr + tparams->header_size; p++) {
732 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
733 params.cmdname, datafile);
738 offset = tparams->header_size;
741 size = sbuf.st_size - offset;
743 ret = write(ifd, ptr + offset, size);
746 fprintf (stderr, "%s: Write error on %s: %s\n",
747 params.cmdname, params.imagefile, strerror(errno));
749 fprintf (stderr, "%s: Write only %d/%d bytes, "\
750 "probably no space left on the device\n",
751 params.cmdname, ret, size);
756 if ((pad == 1) && (tail != 0)) {
758 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
759 fprintf (stderr, "%s: Write error on %s: %s\n",
760 params.cmdname, params.imagefile,
764 } else if (pad > 1) {
766 int todo = sizeof(zeros);
770 if (write(ifd, (char *)&zeros, todo) != todo) {
771 fprintf(stderr, "%s: Write error on %s: %s\n",
772 params.cmdname, params.imagefile,
780 (void) munmap((void *)ptr, sbuf.st_size);