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 " -G => use this signing key (in lieu of -k)\n"
112 " -c => add comment in signature node\n"
113 " -F => re-sign existing FIT image\n"
114 " -p => place external data at a static position\n"
115 " -r => mark keys used as 'required' in dtb\n"
116 " -N => openssl engine to use for signing\n");
119 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
121 fprintf(stderr, " %s -V ==> print version information and exit\n",
123 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
128 static int add_content(int type, const char *fname)
130 struct content_info *cont;
132 cont = calloc(1, sizeof(*cont));
137 if (params.content_tail)
138 params.content_tail->next = cont;
140 params.content_head = cont;
141 params.content_tail = cont;
146 #define OPT_STRING "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx"
147 static void process_args(int argc, char **argv)
150 int type = IH_TYPE_INVALID;
151 char *datafile = NULL;
154 while ((opt = getopt(argc, argv,
155 "a:A:b:B:c:C:d:D:e:Ef:FG:k:i:K:ln:N:p:O:rR:qstT:vVx")) != -1) {
158 params.addr = strtoull(optarg, &ptr, 16);
160 fprintf(stderr, "%s: invalid load address %s\n",
161 params.cmdname, optarg);
166 params.arch = genimg_get_arch_id(optarg);
167 if (params.arch < 0) {
168 show_valid_options(IH_ARCH);
169 usage("Invalid architecture");
173 if (add_content(IH_TYPE_FLATDT, optarg)) {
175 "%s: Out of memory adding content '%s'",
176 params.cmdname, optarg);
181 params.bl_len = strtoull(optarg, &ptr, 16);
183 fprintf(stderr, "%s: invalid block length %s\n",
184 params.cmdname, optarg);
190 params.comment = optarg;
193 params.comp = genimg_get_comp_id(optarg);
194 if (params.comp < 0) {
195 show_valid_options(IH_COMP);
196 usage("Invalid compression type");
200 params.datafile = optarg;
207 params.ep = strtoull(optarg, &ptr, 16);
209 fprintf(stderr, "%s: invalid entry point %s\n",
210 params.cmdname, optarg);
216 params.external_data = true;
220 params.auto_its = !strcmp(datafile, "auto");
224 * The flattened image tree (FIT) format
225 * requires a flattened device tree image type
227 params.type = IH_TYPE_FLATDT;
231 params.keyfile = optarg;
234 params.fit_ramdisk = optarg;
237 params.keydir = optarg;
240 params.keydest = optarg;
246 params.imagename = optarg;
249 params.engine_id = optarg;
252 params.os = genimg_get_os_id(optarg);
254 show_valid_options(IH_OS);
255 usage("Invalid operating system");
259 params.external_offset = strtoull(optarg, &ptr, 16);
261 fprintf(stderr, "%s: invalid offset size %s\n",
262 params.cmdname, optarg);
270 params.require_keys = 1;
274 * This entry is for the second configuration
275 * file, if only one is not enough.
277 params.imagename2 = optarg;
283 params.reset_timestamp = 1;
286 if (strcmp(optarg, "list") == 0) {
287 show_valid_options(IH_TYPE);
290 type = genimg_get_type_id(optarg);
292 show_valid_options(IH_TYPE);
293 usage("Invalid image type");
300 printf("mkimage version %s\n", PLAIN_VERSION);
306 usage("Invalid option");
310 /* The last parameter is expected to be the imagefile */
312 params.imagefile = argv[optind];
315 * For auto-generated FIT images we need to know the image type to put
316 * in the FIT, which is separate from the file's image type (which
317 * will always be IH_TYPE_FLATDT in this case).
319 if (params.type == IH_TYPE_FLATDT) {
320 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
321 /* For auto_its, datafile is always 'auto' */
322 if (!params.auto_its)
323 params.datafile = datafile;
324 else if (!params.datafile)
325 usage("Missing data file for auto-FIT (use -d)");
326 } else if (type != IH_TYPE_INVALID) {
327 if (type == IH_TYPE_SCRIPT && !params.datafile)
328 usage("Missing data file for script (use -d)");
332 if (!params.imagefile)
333 usage("Missing output filename");
336 int main(int argc, char **argv)
342 struct image_type_params *tparams = NULL;
347 params.cmdname = *argv;
351 process_args(argc, argv);
353 /* set tparams as per input type_id */
354 tparams = imagetool_get_type(params.type);
355 if (tparams == NULL) {
356 fprintf (stderr, "%s: unsupported type %s\n",
357 params.cmdname, genimg_get_type_name(params.type));
362 * check the passed arguments parameters meets the requirements
363 * as per image type to be generated/listed
365 if (tparams->check_params)
366 if (tparams->check_params (¶ms))
367 usage("Bad parameters for image type");
370 params.ep = params.addr;
371 /* If XIP, entry point must be after the U-Boot header */
373 params.ep += tparams->header_size;
377 if (tparams->fflag_handle)
379 * in some cases, some additional processing needs
380 * to be done if fflag is defined
382 * For ex. fit_handle_file for Fit file support
384 retval = tparams->fflag_handle(¶ms);
386 if (retval != EXIT_SUCCESS)
390 if (params.lflag || params.fflag) {
391 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
393 ifd = open (params.imagefile,
394 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
398 fprintf (stderr, "%s: Can't open %s: %s\n",
399 params.cmdname, params.imagefile,
404 if (params.lflag || params.fflag) {
406 * list header information of existing image
408 if (fstat(ifd, &sbuf) < 0) {
409 fprintf (stderr, "%s: Can't stat %s: %s\n",
410 params.cmdname, params.imagefile,
415 if ((unsigned)sbuf.st_size < tparams->header_size) {
417 "%s: Bad size: \"%s\" is not valid image\n",
418 params.cmdname, params.imagefile);
422 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
423 if (ptr == MAP_FAILED) {
424 fprintf (stderr, "%s: Can't read %s: %s\n",
425 params.cmdname, params.imagefile,
432 * Verifies the header format based on the expected header for image
435 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
439 * When listing the image, we are not given the image type. Simply check all
440 * image types to find one that matches our header
442 retval = imagetool_verify_print_header(ptr, &sbuf,
446 (void) munmap((void *)ptr, sbuf.st_size);
452 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
453 dfd = open(params.datafile, O_RDONLY | O_BINARY);
455 fprintf(stderr, "%s: Can't open %s: %s\n",
456 params.cmdname, params.datafile,
461 if (fstat(dfd, &sbuf) < 0) {
462 fprintf(stderr, "%s: Can't stat %s: %s\n",
463 params.cmdname, params.datafile,
468 params.file_size = sbuf.st_size + tparams->header_size;
473 * In case there an header with a variable
474 * length will be added, the corresponding
475 * function is called. This is responsible to
476 * allocate memory for the header itself.
478 if (tparams->vrec_header)
479 pad_len = tparams->vrec_header(¶ms, tparams);
481 memset(tparams->hdr, 0, tparams->header_size);
483 if (write(ifd, tparams->hdr, tparams->header_size)
484 != tparams->header_size) {
485 fprintf (stderr, "%s: Write error on %s: %s\n",
486 params.cmdname, params.imagefile, strerror(errno));
490 if (!params.skipcpy) {
491 if (params.type == IH_TYPE_MULTI ||
492 params.type == IH_TYPE_SCRIPT) {
493 char *file = params.datafile;
500 if ((sep = strchr(file, ':')) != NULL) {
504 if (stat (file, &sbuf) < 0) {
505 fprintf (stderr, "%s: Can't stat %s: %s\n",
506 params.cmdname, file, strerror(errno));
509 size = cpu_to_uimage (sbuf.st_size);
514 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
515 fprintf (stderr, "%s: Write error on %s: %s\n",
516 params.cmdname, params.imagefile,
533 file = params.datafile;
536 char *sep = strchr(file, ':');
539 copy_file (ifd, file, 1);
543 copy_file (ifd, file, 0);
547 } else if (params.type == IH_TYPE_PBLIMAGE) {
548 /* PBL has special Image format, implements its' own */
549 pbl_load_uboot(ifd, ¶ms);
550 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
551 /* Image file is meta, walk through actual targets */
554 ret = zynqmpbif_copy_image(ifd, ¶ms);
557 } else if (params.type == IH_TYPE_IMX8IMAGE) {
558 /* i.MX8/8X has special Image format */
561 ret = imx8image_copy_image(ifd, ¶ms);
564 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
565 /* i.MX8M has special Image format */
568 ret = imx8mimage_copy_image(ifd, ¶ms);
571 } else if ((params.type == IH_TYPE_RKSD) ||
572 (params.type == IH_TYPE_RKSPI)) {
573 /* Rockchip has special Image format */
576 ret = rockchip_copy_image(ifd, ¶ms);
580 copy_file(ifd, params.datafile, pad_len);
582 if (params.type == IH_TYPE_FIRMWARE_IVT) {
583 /* Add alignment and IVT */
584 uint32_t aligned_filesize = ALIGN(params.file_size,
586 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
587 params.addr, 0, 0, 0, params.addr
589 - tparams->header_size,
590 params.addr + aligned_filesize
591 - tparams->header_size
593 int i = params.file_size;
594 for (; i < aligned_filesize; i++) {
595 if (write(ifd, (char *) &i, 1) != 1) {
597 "%s: Write error on %s: %s\n",
604 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
605 != sizeof(flash_header_v2_t)) {
606 fprintf(stderr, "%s: Write error on %s: %s\n",
615 /* We're a bit of paranoid */
616 #if defined(_POSIX_SYNCHRONIZED_IO) && \
617 !defined(__sun__) && \
618 !defined(__FreeBSD__) && \
619 !defined(__OpenBSD__) && \
621 (void) fdatasync (ifd);
626 if (fstat(ifd, &sbuf) < 0) {
627 fprintf (stderr, "%s: Can't stat %s: %s\n",
628 params.cmdname, params.imagefile, strerror(errno));
631 params.file_size = sbuf.st_size;
633 map_len = sbuf.st_size;
634 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
635 if (ptr == MAP_FAILED) {
636 fprintf (stderr, "%s: Can't map %s: %s\n",
637 params.cmdname, params.imagefile, strerror(errno));
641 /* Setup the image header as per input image type*/
642 if (tparams->set_header)
643 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
645 fprintf (stderr, "%s: Can't set header for %s: %s\n",
646 params.cmdname, tparams->name, strerror(errno));
650 /* Print the image information by processing image header */
651 if (tparams->print_header)
652 tparams->print_header (ptr);
654 fprintf (stderr, "%s: Can't print header for %s\n",
655 params.cmdname, tparams->name);
658 (void)munmap((void *)ptr, map_len);
660 /* We're a bit of paranoid */
661 #if defined(_POSIX_SYNCHRONIZED_IO) && \
662 !defined(__sun__) && \
663 !defined(__FreeBSD__) && \
664 !defined(__OpenBSD__) && \
666 (void) fdatasync (ifd);
672 fprintf (stderr, "%s: Write error on %s: %s\n",
673 params.cmdname, params.imagefile, strerror(errno));
681 copy_file (int ifd, const char *datafile, int pad)
691 struct image_type_params *tparams = imagetool_get_type(params.type);
693 memset(zeros, 0, sizeof(zeros));
696 fprintf (stderr, "Adding Image %s\n", datafile);
699 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
700 fprintf (stderr, "%s: Can't open %s: %s\n",
701 params.cmdname, datafile, strerror(errno));
705 if (fstat(dfd, &sbuf) < 0) {
706 fprintf (stderr, "%s: Can't stat %s: %s\n",
707 params.cmdname, datafile, strerror(errno));
711 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
712 if (ptr == MAP_FAILED) {
713 fprintf (stderr, "%s: Can't read %s: %s\n",
714 params.cmdname, datafile, strerror(errno));
719 unsigned char *p = NULL;
721 * XIP: do not append the image_header_t at the
722 * beginning of the file, but consume the space
726 if ((unsigned)sbuf.st_size < tparams->header_size) {
728 "%s: Bad size: \"%s\" is too small for XIP\n",
729 params.cmdname, datafile);
733 for (p = ptr; p < ptr + tparams->header_size; p++) {
736 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
737 params.cmdname, datafile);
742 offset = tparams->header_size;
745 size = sbuf.st_size - offset;
747 ret = write(ifd, ptr + offset, size);
750 fprintf (stderr, "%s: Write error on %s: %s\n",
751 params.cmdname, params.imagefile, strerror(errno));
753 fprintf (stderr, "%s: Write only %d/%d bytes, "\
754 "probably no space left on the device\n",
755 params.cmdname, ret, size);
760 if ((pad == 1) && (tail != 0)) {
762 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
763 fprintf (stderr, "%s: Write error on %s: %s\n",
764 params.cmdname, params.imagefile,
768 } else if (pad > 1) {
770 int todo = sizeof(zeros);
774 if (write(ifd, (char *)&zeros, todo) != todo) {
775 fprintf(stderr, "%s: Write error on %s: %s\n",
776 params.cmdname, params.imagefile,
784 (void) munmap((void *)ptr, sbuf.st_size);