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 (item = 0; item < count; item++)
56 cur_category = category;
57 qsort(order, count, sizeof(int), h_compare_category_name);
59 fprintf(stderr, "\nInvalid %s, supported are:\n",
60 genimg_get_cat_desc(category));
61 for (i = 0; i < count; i++) {
63 fprintf(stderr, "\t%-15s %s\n",
64 genimg_get_cat_short_name(category, item),
65 genimg_get_cat_name(category, item));
67 fprintf(stderr, "\n");
73 static void usage(const char *msg)
75 fprintf(stderr, "Error: %s\n", msg);
76 fprintf(stderr, "Usage: %s -l image\n"
77 " -l ==> list image header information\n",
80 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
81 " -A ==> set architecture to 'arch'\n"
82 " -O ==> set operating system to 'os'\n"
83 " -T ==> set image type to 'type'\n"
84 " -C ==> set compression type 'comp'\n"
85 " -a ==> set load address to 'addr' (hex)\n"
86 " -e ==> set entry point to 'ep' (hex)\n"
87 " -n ==> set image name to 'name'\n"
88 " -d ==> use image data from 'datafile'\n"
89 " -x ==> set XIP (execute in place)\n",
92 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n"
93 " <dtb> file is used with -f auto, it may occur multiple times.\n",
96 " -D => set all options for device tree compiler\n"
97 " -f => input filename for FIT source\n"
98 " -i => input filename for ramdisk file\n");
99 #ifdef CONFIG_FIT_SIGNATURE
101 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
102 " -E => place data outside of the FIT structure\n"
103 " -k => set directory containing private keys\n"
104 " -K => write public keys to this .dtb file\n"
105 " -c => add comment in signature node\n"
106 " -F => re-sign existing FIT image\n"
107 " -p => place external data at a static position\n"
108 " -r => mark keys used as 'required' in dtb\n"
109 " -N => openssl engine to use for signing\n");
112 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
114 fprintf(stderr, " %s -V ==> print version information and exit\n",
116 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
121 static int add_content(int type, const char *fname)
123 struct content_info *cont;
125 cont = calloc(1, sizeof(*cont));
130 if (params.content_tail)
131 params.content_tail->next = cont;
133 params.content_head = cont;
134 params.content_tail = cont;
139 static void process_args(int argc, char **argv)
142 int type = IH_TYPE_INVALID;
143 char *datafile = NULL;
146 while ((opt = getopt(argc, argv,
147 "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) {
150 params.addr = strtoull(optarg, &ptr, 16);
152 fprintf(stderr, "%s: invalid load address %s\n",
153 params.cmdname, optarg);
158 params.arch = genimg_get_arch_id(optarg);
159 if (params.arch < 0) {
160 show_valid_options(IH_ARCH);
161 usage("Invalid architecture");
165 if (add_content(IH_TYPE_FLATDT, optarg)) {
167 "%s: Out of memory adding content '%s'",
168 params.cmdname, optarg);
173 params.comment = optarg;
176 params.comp = genimg_get_comp_id(optarg);
177 if (params.comp < 0) {
178 show_valid_options(IH_COMP);
179 usage("Invalid compression type");
183 params.datafile = optarg;
190 params.ep = strtoull(optarg, &ptr, 16);
192 fprintf(stderr, "%s: invalid entry point %s\n",
193 params.cmdname, optarg);
199 params.external_data = true;
203 params.auto_its = !strcmp(datafile, "auto");
207 * The flattened image tree (FIT) format
208 * requires a flattened device tree image type
210 params.type = IH_TYPE_FLATDT;
214 params.fit_ramdisk = optarg;
217 params.keydir = optarg;
220 params.keydest = optarg;
226 params.imagename = optarg;
229 params.engine_id = optarg;
232 params.os = genimg_get_os_id(optarg);
234 show_valid_options(IH_OS);
235 usage("Invalid operating system");
239 params.external_offset = strtoull(optarg, &ptr, 16);
241 fprintf(stderr, "%s: invalid offset size %s\n",
242 params.cmdname, optarg);
250 params.require_keys = 1;
254 * This entry is for the second configuration
255 * file, if only one is not enough.
257 params.imagename2 = optarg;
263 if (strcmp(optarg, "list") == 0) {
264 show_valid_options(IH_TYPE);
267 type = genimg_get_type_id(optarg);
269 show_valid_options(IH_TYPE);
270 usage("Invalid image type");
277 printf("mkimage version %s\n", PLAIN_VERSION);
283 usage("Invalid option");
287 /* The last parameter is expected to be the imagefile */
289 params.imagefile = argv[optind];
292 * For auto-generated FIT images we need to know the image type to put
293 * in the FIT, which is separate from the file's image type (which
294 * will always be IH_TYPE_FLATDT in this case).
296 if (params.type == IH_TYPE_FLATDT) {
297 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
298 /* For auto_its, datafile is always 'auto' */
299 if (!params.auto_its)
300 params.datafile = datafile;
301 else if (!params.datafile)
302 usage("Missing data file for auto-FIT (use -d)");
303 } else if (type != IH_TYPE_INVALID) {
304 if (type == IH_TYPE_SCRIPT && !params.datafile)
305 usage("Missing data file for script (use -d)");
309 if (!params.imagefile)
310 usage("Missing output filename");
313 int main(int argc, char **argv)
319 struct image_type_params *tparams = NULL;
324 params.cmdname = *argv;
328 process_args(argc, argv);
330 /* set tparams as per input type_id */
331 tparams = imagetool_get_type(params.type);
332 if (tparams == NULL) {
333 fprintf (stderr, "%s: unsupported type %s\n",
334 params.cmdname, genimg_get_type_name(params.type));
339 * check the passed arguments parameters meets the requirements
340 * as per image type to be generated/listed
342 if (tparams->check_params)
343 if (tparams->check_params (¶ms))
344 usage("Bad parameters for image type");
347 params.ep = params.addr;
348 /* If XIP, entry point must be after the U-Boot header */
350 params.ep += tparams->header_size;
354 if (tparams->fflag_handle)
356 * in some cases, some additional processing needs
357 * to be done if fflag is defined
359 * For ex. fit_handle_file for Fit file support
361 retval = tparams->fflag_handle(¶ms);
363 if (retval != EXIT_SUCCESS)
367 if (params.lflag || params.fflag) {
368 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
370 ifd = open (params.imagefile,
371 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
375 fprintf (stderr, "%s: Can't open %s: %s\n",
376 params.cmdname, params.imagefile,
381 if (params.lflag || params.fflag) {
383 * list header information of existing image
385 if (fstat(ifd, &sbuf) < 0) {
386 fprintf (stderr, "%s: Can't stat %s: %s\n",
387 params.cmdname, params.imagefile,
392 if ((unsigned)sbuf.st_size < tparams->header_size) {
394 "%s: Bad size: \"%s\" is not valid image\n",
395 params.cmdname, params.imagefile);
399 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
400 if (ptr == MAP_FAILED) {
401 fprintf (stderr, "%s: Can't read %s: %s\n",
402 params.cmdname, params.imagefile,
409 * Verifies the header format based on the expected header for image
412 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
416 * When listing the image, we are not given the image type. Simply check all
417 * image types to find one that matches our header
419 retval = imagetool_verify_print_header(ptr, &sbuf,
423 (void) munmap((void *)ptr, sbuf.st_size);
429 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
430 dfd = open(params.datafile, O_RDONLY | O_BINARY);
432 fprintf(stderr, "%s: Can't open %s: %s\n",
433 params.cmdname, params.datafile,
438 if (fstat(dfd, &sbuf) < 0) {
439 fprintf(stderr, "%s: Can't stat %s: %s\n",
440 params.cmdname, params.datafile,
445 params.file_size = sbuf.st_size + tparams->header_size;
450 * In case there an header with a variable
451 * length will be added, the corresponding
452 * function is called. This is responsible to
453 * allocate memory for the header itself.
455 if (tparams->vrec_header)
456 pad_len = tparams->vrec_header(¶ms, tparams);
458 memset(tparams->hdr, 0, tparams->header_size);
460 if (write(ifd, tparams->hdr, tparams->header_size)
461 != tparams->header_size) {
462 fprintf (stderr, "%s: Write error on %s: %s\n",
463 params.cmdname, params.imagefile, strerror(errno));
467 if (!params.skipcpy) {
468 if (params.type == IH_TYPE_MULTI ||
469 params.type == IH_TYPE_SCRIPT) {
470 char *file = params.datafile;
477 if ((sep = strchr(file, ':')) != NULL) {
481 if (stat (file, &sbuf) < 0) {
482 fprintf (stderr, "%s: Can't stat %s: %s\n",
483 params.cmdname, file, strerror(errno));
486 size = cpu_to_uimage (sbuf.st_size);
491 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
492 fprintf (stderr, "%s: Write error on %s: %s\n",
493 params.cmdname, params.imagefile,
510 file = params.datafile;
513 char *sep = strchr(file, ':');
516 copy_file (ifd, file, 1);
520 copy_file (ifd, file, 0);
524 } else if (params.type == IH_TYPE_PBLIMAGE) {
525 /* PBL has special Image format, implements its' own */
526 pbl_load_uboot(ifd, ¶ms);
527 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
528 /* Image file is meta, walk through actual targets */
531 ret = zynqmpbif_copy_image(ifd, ¶ms);
534 } else if (params.type == IH_TYPE_IMX8IMAGE) {
535 /* i.MX8/8X has special Image format */
538 ret = imx8image_copy_image(ifd, ¶ms);
541 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
542 /* i.MX8M has special Image format */
545 ret = imx8mimage_copy_image(ifd, ¶ms);
548 } else if ((params.type == IH_TYPE_RKSD) ||
549 (params.type == IH_TYPE_RKSPI)) {
550 /* Rockchip has special Image format */
553 ret = rockchip_copy_image(ifd, ¶ms);
557 copy_file(ifd, params.datafile, pad_len);
559 if (params.type == IH_TYPE_FIRMWARE_IVT) {
560 /* Add alignment and IVT */
561 uint32_t aligned_filesize = ALIGN(params.file_size,
563 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
564 params.addr, 0, 0, 0, params.addr
566 - tparams->header_size,
567 params.addr + aligned_filesize
568 - tparams->header_size
570 int i = params.file_size;
571 for (; i < aligned_filesize; i++) {
572 if (write(ifd, (char *) &i, 1) != 1) {
574 "%s: Write error on %s: %s\n",
581 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
582 != sizeof(flash_header_v2_t)) {
583 fprintf(stderr, "%s: Write error on %s: %s\n",
592 /* We're a bit of paranoid */
593 #if defined(_POSIX_SYNCHRONIZED_IO) && \
594 !defined(__sun__) && \
595 !defined(__FreeBSD__) && \
596 !defined(__OpenBSD__) && \
598 (void) fdatasync (ifd);
603 if (fstat(ifd, &sbuf) < 0) {
604 fprintf (stderr, "%s: Can't stat %s: %s\n",
605 params.cmdname, params.imagefile, strerror(errno));
608 params.file_size = sbuf.st_size;
610 map_len = sbuf.st_size;
611 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
612 if (ptr == MAP_FAILED) {
613 fprintf (stderr, "%s: Can't map %s: %s\n",
614 params.cmdname, params.imagefile, strerror(errno));
618 /* Setup the image header as per input image type*/
619 if (tparams->set_header)
620 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
622 fprintf (stderr, "%s: Can't set header for %s: %s\n",
623 params.cmdname, tparams->name, strerror(errno));
627 /* Print the image information by processing image header */
628 if (tparams->print_header)
629 tparams->print_header (ptr);
631 fprintf (stderr, "%s: Can't print header for %s\n",
632 params.cmdname, tparams->name);
635 (void)munmap((void *)ptr, map_len);
637 /* We're a bit of paranoid */
638 #if defined(_POSIX_SYNCHRONIZED_IO) && \
639 !defined(__sun__) && \
640 !defined(__FreeBSD__) && \
641 !defined(__OpenBSD__) && \
643 (void) fdatasync (ifd);
649 fprintf (stderr, "%s: Write error on %s: %s\n",
650 params.cmdname, params.imagefile, strerror(errno));
658 copy_file (int ifd, const char *datafile, int pad)
668 struct image_type_params *tparams = imagetool_get_type(params.type);
670 memset(zeros, 0, sizeof(zeros));
673 fprintf (stderr, "Adding Image %s\n", datafile);
676 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
677 fprintf (stderr, "%s: Can't open %s: %s\n",
678 params.cmdname, datafile, strerror(errno));
682 if (fstat(dfd, &sbuf) < 0) {
683 fprintf (stderr, "%s: Can't stat %s: %s\n",
684 params.cmdname, datafile, strerror(errno));
688 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
689 if (ptr == MAP_FAILED) {
690 fprintf (stderr, "%s: Can't read %s: %s\n",
691 params.cmdname, datafile, strerror(errno));
696 unsigned char *p = NULL;
698 * XIP: do not append the image_header_t at the
699 * beginning of the file, but consume the space
703 if ((unsigned)sbuf.st_size < tparams->header_size) {
705 "%s: Bad size: \"%s\" is too small for XIP\n",
706 params.cmdname, datafile);
710 for (p = ptr; p < ptr + tparams->header_size; p++) {
713 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
714 params.cmdname, datafile);
719 offset = tparams->header_size;
722 size = sbuf.st_size - offset;
723 if (write(ifd, ptr + offset, size) != size) {
724 fprintf (stderr, "%s: Write error on %s: %s\n",
725 params.cmdname, params.imagefile, strerror(errno));
730 if ((pad == 1) && (tail != 0)) {
732 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
733 fprintf (stderr, "%s: Write error on %s: %s\n",
734 params.cmdname, params.imagefile,
738 } else if (pad > 1) {
740 int todo = sizeof(zeros);
744 if (write(ifd, (char *)&zeros, todo) != todo) {
745 fprintf(stderr, "%s: Write error on %s: %s\n",
746 params.cmdname, params.imagefile,
754 (void) munmap((void *)ptr, sbuf.st_size);