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>]] [-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 #ifdef CONFIG_FIT_SIGNATURE
106 "Signing / verified boot options: [-E] [-B size] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
107 " -E => place data outside of the FIT structure\n"
108 " -B => align size in hex for FIT structure and header\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 static void process_args(int argc, char **argv)
148 int type = IH_TYPE_INVALID;
149 char *datafile = NULL;
152 while ((opt = getopt(argc, argv,
153 "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qstT:vVx")) != -1) {
156 params.addr = strtoull(optarg, &ptr, 16);
158 fprintf(stderr, "%s: invalid load address %s\n",
159 params.cmdname, optarg);
164 params.arch = genimg_get_arch_id(optarg);
165 if (params.arch < 0) {
166 show_valid_options(IH_ARCH);
167 usage("Invalid architecture");
171 if (add_content(IH_TYPE_FLATDT, optarg)) {
173 "%s: Out of memory adding content '%s'",
174 params.cmdname, optarg);
179 params.bl_len = strtoull(optarg, &ptr, 16);
181 fprintf(stderr, "%s: invalid block length %s\n",
182 params.cmdname, optarg);
188 params.comment = optarg;
191 params.comp = genimg_get_comp_id(optarg);
192 if (params.comp < 0) {
193 show_valid_options(IH_COMP);
194 usage("Invalid compression type");
198 params.datafile = optarg;
205 params.ep = strtoull(optarg, &ptr, 16);
207 fprintf(stderr, "%s: invalid entry point %s\n",
208 params.cmdname, optarg);
214 params.external_data = true;
218 params.auto_its = !strcmp(datafile, "auto");
222 * The flattened image tree (FIT) format
223 * requires a flattened device tree image type
225 params.type = IH_TYPE_FLATDT;
229 params.fit_ramdisk = optarg;
232 params.keydir = optarg;
235 params.keydest = optarg;
241 params.imagename = optarg;
244 params.engine_id = optarg;
247 params.os = genimg_get_os_id(optarg);
249 show_valid_options(IH_OS);
250 usage("Invalid operating system");
254 params.external_offset = strtoull(optarg, &ptr, 16);
256 fprintf(stderr, "%s: invalid offset size %s\n",
257 params.cmdname, optarg);
265 params.require_keys = 1;
269 * This entry is for the second configuration
270 * file, if only one is not enough.
272 params.imagename2 = optarg;
278 params.reset_timestamp = 1;
281 if (strcmp(optarg, "list") == 0) {
282 show_valid_options(IH_TYPE);
285 type = genimg_get_type_id(optarg);
287 show_valid_options(IH_TYPE);
288 usage("Invalid image type");
295 printf("mkimage version %s\n", PLAIN_VERSION);
301 usage("Invalid option");
305 /* The last parameter is expected to be the imagefile */
307 params.imagefile = argv[optind];
310 * For auto-generated FIT images we need to know the image type to put
311 * in the FIT, which is separate from the file's image type (which
312 * will always be IH_TYPE_FLATDT in this case).
314 if (params.type == IH_TYPE_FLATDT) {
315 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
316 /* For auto_its, datafile is always 'auto' */
317 if (!params.auto_its)
318 params.datafile = datafile;
319 else if (!params.datafile)
320 usage("Missing data file for auto-FIT (use -d)");
321 } else if (type != IH_TYPE_INVALID) {
322 if (type == IH_TYPE_SCRIPT && !params.datafile)
323 usage("Missing data file for script (use -d)");
327 if (!params.imagefile)
328 usage("Missing output filename");
331 int main(int argc, char **argv)
337 struct image_type_params *tparams = NULL;
342 params.cmdname = *argv;
346 process_args(argc, argv);
348 /* set tparams as per input type_id */
349 tparams = imagetool_get_type(params.type);
350 if (tparams == NULL) {
351 fprintf (stderr, "%s: unsupported type %s\n",
352 params.cmdname, genimg_get_type_name(params.type));
357 * check the passed arguments parameters meets the requirements
358 * as per image type to be generated/listed
360 if (tparams->check_params)
361 if (tparams->check_params (¶ms))
362 usage("Bad parameters for image type");
365 params.ep = params.addr;
366 /* If XIP, entry point must be after the U-Boot header */
368 params.ep += tparams->header_size;
372 if (tparams->fflag_handle)
374 * in some cases, some additional processing needs
375 * to be done if fflag is defined
377 * For ex. fit_handle_file for Fit file support
379 retval = tparams->fflag_handle(¶ms);
381 if (retval != EXIT_SUCCESS)
385 if (params.lflag || params.fflag) {
386 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
388 ifd = open (params.imagefile,
389 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
393 fprintf (stderr, "%s: Can't open %s: %s\n",
394 params.cmdname, params.imagefile,
399 if (params.lflag || params.fflag) {
401 * list header information of existing image
403 if (fstat(ifd, &sbuf) < 0) {
404 fprintf (stderr, "%s: Can't stat %s: %s\n",
405 params.cmdname, params.imagefile,
410 if ((unsigned)sbuf.st_size < tparams->header_size) {
412 "%s: Bad size: \"%s\" is not valid image\n",
413 params.cmdname, params.imagefile);
417 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
418 if (ptr == MAP_FAILED) {
419 fprintf (stderr, "%s: Can't read %s: %s\n",
420 params.cmdname, params.imagefile,
427 * Verifies the header format based on the expected header for image
430 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
434 * When listing the image, we are not given the image type. Simply check all
435 * image types to find one that matches our header
437 retval = imagetool_verify_print_header(ptr, &sbuf,
441 (void) munmap((void *)ptr, sbuf.st_size);
447 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
448 dfd = open(params.datafile, O_RDONLY | O_BINARY);
450 fprintf(stderr, "%s: Can't open %s: %s\n",
451 params.cmdname, params.datafile,
456 if (fstat(dfd, &sbuf) < 0) {
457 fprintf(stderr, "%s: Can't stat %s: %s\n",
458 params.cmdname, params.datafile,
463 params.file_size = sbuf.st_size + tparams->header_size;
468 * In case there an header with a variable
469 * length will be added, the corresponding
470 * function is called. This is responsible to
471 * allocate memory for the header itself.
473 if (tparams->vrec_header)
474 pad_len = tparams->vrec_header(¶ms, tparams);
476 memset(tparams->hdr, 0, tparams->header_size);
478 if (write(ifd, tparams->hdr, tparams->header_size)
479 != tparams->header_size) {
480 fprintf (stderr, "%s: Write error on %s: %s\n",
481 params.cmdname, params.imagefile, strerror(errno));
485 if (!params.skipcpy) {
486 if (params.type == IH_TYPE_MULTI ||
487 params.type == IH_TYPE_SCRIPT) {
488 char *file = params.datafile;
495 if ((sep = strchr(file, ':')) != NULL) {
499 if (stat (file, &sbuf) < 0) {
500 fprintf (stderr, "%s: Can't stat %s: %s\n",
501 params.cmdname, file, strerror(errno));
504 size = cpu_to_uimage (sbuf.st_size);
509 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
510 fprintf (stderr, "%s: Write error on %s: %s\n",
511 params.cmdname, params.imagefile,
528 file = params.datafile;
531 char *sep = strchr(file, ':');
534 copy_file (ifd, file, 1);
538 copy_file (ifd, file, 0);
542 } else if (params.type == IH_TYPE_PBLIMAGE) {
543 /* PBL has special Image format, implements its' own */
544 pbl_load_uboot(ifd, ¶ms);
545 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
546 /* Image file is meta, walk through actual targets */
549 ret = zynqmpbif_copy_image(ifd, ¶ms);
552 } else if (params.type == IH_TYPE_IMX8IMAGE) {
553 /* i.MX8/8X has special Image format */
556 ret = imx8image_copy_image(ifd, ¶ms);
559 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
560 /* i.MX8M has special Image format */
563 ret = imx8mimage_copy_image(ifd, ¶ms);
566 } else if ((params.type == IH_TYPE_RKSD) ||
567 (params.type == IH_TYPE_RKSPI)) {
568 /* Rockchip has special Image format */
571 ret = rockchip_copy_image(ifd, ¶ms);
575 copy_file(ifd, params.datafile, pad_len);
577 if (params.type == IH_TYPE_FIRMWARE_IVT) {
578 /* Add alignment and IVT */
579 uint32_t aligned_filesize = ALIGN(params.file_size,
581 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
582 params.addr, 0, 0, 0, params.addr
584 - tparams->header_size,
585 params.addr + aligned_filesize
586 - tparams->header_size
588 int i = params.file_size;
589 for (; i < aligned_filesize; i++) {
590 if (write(ifd, (char *) &i, 1) != 1) {
592 "%s: Write error on %s: %s\n",
599 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
600 != sizeof(flash_header_v2_t)) {
601 fprintf(stderr, "%s: Write error on %s: %s\n",
610 /* We're a bit of paranoid */
611 #if defined(_POSIX_SYNCHRONIZED_IO) && \
612 !defined(__sun__) && \
613 !defined(__FreeBSD__) && \
614 !defined(__OpenBSD__) && \
616 (void) fdatasync (ifd);
621 if (fstat(ifd, &sbuf) < 0) {
622 fprintf (stderr, "%s: Can't stat %s: %s\n",
623 params.cmdname, params.imagefile, strerror(errno));
626 params.file_size = sbuf.st_size;
628 map_len = sbuf.st_size;
629 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
630 if (ptr == MAP_FAILED) {
631 fprintf (stderr, "%s: Can't map %s: %s\n",
632 params.cmdname, params.imagefile, strerror(errno));
636 /* Setup the image header as per input image type*/
637 if (tparams->set_header)
638 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
640 fprintf (stderr, "%s: Can't set header for %s: %s\n",
641 params.cmdname, tparams->name, strerror(errno));
645 /* Print the image information by processing image header */
646 if (tparams->print_header)
647 tparams->print_header (ptr);
649 fprintf (stderr, "%s: Can't print header for %s\n",
650 params.cmdname, tparams->name);
653 (void)munmap((void *)ptr, map_len);
655 /* We're a bit of paranoid */
656 #if defined(_POSIX_SYNCHRONIZED_IO) && \
657 !defined(__sun__) && \
658 !defined(__FreeBSD__) && \
659 !defined(__OpenBSD__) && \
661 (void) fdatasync (ifd);
667 fprintf (stderr, "%s: Write error on %s: %s\n",
668 params.cmdname, params.imagefile, strerror(errno));
676 copy_file (int ifd, const char *datafile, int pad)
686 struct image_type_params *tparams = imagetool_get_type(params.type);
688 memset(zeros, 0, sizeof(zeros));
691 fprintf (stderr, "Adding Image %s\n", datafile);
694 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
695 fprintf (stderr, "%s: Can't open %s: %s\n",
696 params.cmdname, datafile, strerror(errno));
700 if (fstat(dfd, &sbuf) < 0) {
701 fprintf (stderr, "%s: Can't stat %s: %s\n",
702 params.cmdname, datafile, strerror(errno));
706 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
707 if (ptr == MAP_FAILED) {
708 fprintf (stderr, "%s: Can't read %s: %s\n",
709 params.cmdname, datafile, strerror(errno));
714 unsigned char *p = NULL;
716 * XIP: do not append the image_header_t at the
717 * beginning of the file, but consume the space
721 if ((unsigned)sbuf.st_size < tparams->header_size) {
723 "%s: Bad size: \"%s\" is too small for XIP\n",
724 params.cmdname, datafile);
728 for (p = ptr; p < ptr + tparams->header_size; p++) {
731 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
732 params.cmdname, datafile);
737 offset = tparams->header_size;
740 size = sbuf.st_size - offset;
742 ret = write(ifd, ptr + offset, size);
745 fprintf (stderr, "%s: Write error on %s: %s\n",
746 params.cmdname, params.imagefile, strerror(errno));
748 fprintf (stderr, "%s: Write only %d/%d bytes, "\
749 "probably no space left on the device\n",
750 params.cmdname, ret, size);
755 if ((pad == 1) && (tail != 0)) {
757 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
758 fprintf (stderr, "%s: Write error on %s: %s\n",
759 params.cmdname, params.imagefile,
763 } else if (pad > 1) {
765 int todo = sizeof(zeros);
769 if (write(ifd, (char *)&zeros, todo) != todo) {
770 fprintf(stderr, "%s: Write error on %s: %s\n",
771 params.cmdname, params.imagefile,
779 (void) munmap((void *)ptr, sbuf.st_size);