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] [-B size] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
102 " -E => place data outside of the FIT structure\n"
103 " -B => align size in hex for FIT structure and header\n"
104 " -k => set directory containing private keys\n"
105 " -K => write public keys to this .dtb file\n"
106 " -c => add comment in signature node\n"
107 " -F => re-sign existing FIT image\n"
108 " -p => place external data at a static position\n"
109 " -r => mark keys used as 'required' in dtb\n"
110 " -N => openssl engine to use for signing\n");
113 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
115 fprintf(stderr, " %s -V ==> print version information and exit\n",
117 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
122 static int add_content(int type, const char *fname)
124 struct content_info *cont;
126 cont = calloc(1, sizeof(*cont));
131 if (params.content_tail)
132 params.content_tail->next = cont;
134 params.content_head = cont;
135 params.content_tail = cont;
140 static void process_args(int argc, char **argv)
143 int type = IH_TYPE_INVALID;
144 char *datafile = NULL;
147 while ((opt = getopt(argc, argv,
148 "a:A:b:B:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) {
151 params.addr = strtoull(optarg, &ptr, 16);
153 fprintf(stderr, "%s: invalid load address %s\n",
154 params.cmdname, optarg);
159 params.arch = genimg_get_arch_id(optarg);
160 if (params.arch < 0) {
161 show_valid_options(IH_ARCH);
162 usage("Invalid architecture");
166 if (add_content(IH_TYPE_FLATDT, optarg)) {
168 "%s: Out of memory adding content '%s'",
169 params.cmdname, optarg);
174 params.bl_len = strtoull(optarg, &ptr, 16);
176 fprintf(stderr, "%s: invalid block length %s\n",
177 params.cmdname, optarg);
183 params.comment = optarg;
186 params.comp = genimg_get_comp_id(optarg);
187 if (params.comp < 0) {
188 show_valid_options(IH_COMP);
189 usage("Invalid compression type");
193 params.datafile = optarg;
200 params.ep = strtoull(optarg, &ptr, 16);
202 fprintf(stderr, "%s: invalid entry point %s\n",
203 params.cmdname, optarg);
209 params.external_data = true;
213 params.auto_its = !strcmp(datafile, "auto");
217 * The flattened image tree (FIT) format
218 * requires a flattened device tree image type
220 params.type = IH_TYPE_FLATDT;
224 params.fit_ramdisk = optarg;
227 params.keydir = optarg;
230 params.keydest = optarg;
236 params.imagename = optarg;
239 params.engine_id = optarg;
242 params.os = genimg_get_os_id(optarg);
244 show_valid_options(IH_OS);
245 usage("Invalid operating system");
249 params.external_offset = strtoull(optarg, &ptr, 16);
251 fprintf(stderr, "%s: invalid offset size %s\n",
252 params.cmdname, optarg);
260 params.require_keys = 1;
264 * This entry is for the second configuration
265 * file, if only one is not enough.
267 params.imagename2 = optarg;
273 if (strcmp(optarg, "list") == 0) {
274 show_valid_options(IH_TYPE);
277 type = genimg_get_type_id(optarg);
279 show_valid_options(IH_TYPE);
280 usage("Invalid image type");
287 printf("mkimage version %s\n", PLAIN_VERSION);
293 usage("Invalid option");
297 /* The last parameter is expected to be the imagefile */
299 params.imagefile = argv[optind];
302 * For auto-generated FIT images we need to know the image type to put
303 * in the FIT, which is separate from the file's image type (which
304 * will always be IH_TYPE_FLATDT in this case).
306 if (params.type == IH_TYPE_FLATDT) {
307 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
308 /* For auto_its, datafile is always 'auto' */
309 if (!params.auto_its)
310 params.datafile = datafile;
311 else if (!params.datafile)
312 usage("Missing data file for auto-FIT (use -d)");
313 } else if (type != IH_TYPE_INVALID) {
314 if (type == IH_TYPE_SCRIPT && !params.datafile)
315 usage("Missing data file for script (use -d)");
319 if (!params.imagefile)
320 usage("Missing output filename");
323 int main(int argc, char **argv)
329 struct image_type_params *tparams = NULL;
334 params.cmdname = *argv;
338 process_args(argc, argv);
340 /* set tparams as per input type_id */
341 tparams = imagetool_get_type(params.type);
342 if (tparams == NULL) {
343 fprintf (stderr, "%s: unsupported type %s\n",
344 params.cmdname, genimg_get_type_name(params.type));
349 * check the passed arguments parameters meets the requirements
350 * as per image type to be generated/listed
352 if (tparams->check_params)
353 if (tparams->check_params (¶ms))
354 usage("Bad parameters for image type");
357 params.ep = params.addr;
358 /* If XIP, entry point must be after the U-Boot header */
360 params.ep += tparams->header_size;
364 if (tparams->fflag_handle)
366 * in some cases, some additional processing needs
367 * to be done if fflag is defined
369 * For ex. fit_handle_file for Fit file support
371 retval = tparams->fflag_handle(¶ms);
373 if (retval != EXIT_SUCCESS)
377 if (params.lflag || params.fflag) {
378 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
380 ifd = open (params.imagefile,
381 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
385 fprintf (stderr, "%s: Can't open %s: %s\n",
386 params.cmdname, params.imagefile,
391 if (params.lflag || params.fflag) {
393 * list header information of existing image
395 if (fstat(ifd, &sbuf) < 0) {
396 fprintf (stderr, "%s: Can't stat %s: %s\n",
397 params.cmdname, params.imagefile,
402 if ((unsigned)sbuf.st_size < tparams->header_size) {
404 "%s: Bad size: \"%s\" is not valid image\n",
405 params.cmdname, params.imagefile);
409 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
410 if (ptr == MAP_FAILED) {
411 fprintf (stderr, "%s: Can't read %s: %s\n",
412 params.cmdname, params.imagefile,
419 * Verifies the header format based on the expected header for image
422 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
426 * When listing the image, we are not given the image type. Simply check all
427 * image types to find one that matches our header
429 retval = imagetool_verify_print_header(ptr, &sbuf,
433 (void) munmap((void *)ptr, sbuf.st_size);
439 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
440 dfd = open(params.datafile, O_RDONLY | O_BINARY);
442 fprintf(stderr, "%s: Can't open %s: %s\n",
443 params.cmdname, params.datafile,
448 if (fstat(dfd, &sbuf) < 0) {
449 fprintf(stderr, "%s: Can't stat %s: %s\n",
450 params.cmdname, params.datafile,
455 params.file_size = sbuf.st_size + tparams->header_size;
460 * In case there an header with a variable
461 * length will be added, the corresponding
462 * function is called. This is responsible to
463 * allocate memory for the header itself.
465 if (tparams->vrec_header)
466 pad_len = tparams->vrec_header(¶ms, tparams);
468 memset(tparams->hdr, 0, tparams->header_size);
470 if (write(ifd, tparams->hdr, tparams->header_size)
471 != tparams->header_size) {
472 fprintf (stderr, "%s: Write error on %s: %s\n",
473 params.cmdname, params.imagefile, strerror(errno));
477 if (!params.skipcpy) {
478 if (params.type == IH_TYPE_MULTI ||
479 params.type == IH_TYPE_SCRIPT) {
480 char *file = params.datafile;
487 if ((sep = strchr(file, ':')) != NULL) {
491 if (stat (file, &sbuf) < 0) {
492 fprintf (stderr, "%s: Can't stat %s: %s\n",
493 params.cmdname, file, strerror(errno));
496 size = cpu_to_uimage (sbuf.st_size);
501 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
502 fprintf (stderr, "%s: Write error on %s: %s\n",
503 params.cmdname, params.imagefile,
520 file = params.datafile;
523 char *sep = strchr(file, ':');
526 copy_file (ifd, file, 1);
530 copy_file (ifd, file, 0);
534 } else if (params.type == IH_TYPE_PBLIMAGE) {
535 /* PBL has special Image format, implements its' own */
536 pbl_load_uboot(ifd, ¶ms);
537 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
538 /* Image file is meta, walk through actual targets */
541 ret = zynqmpbif_copy_image(ifd, ¶ms);
544 } else if (params.type == IH_TYPE_IMX8IMAGE) {
545 /* i.MX8/8X has special Image format */
548 ret = imx8image_copy_image(ifd, ¶ms);
551 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
552 /* i.MX8M has special Image format */
555 ret = imx8mimage_copy_image(ifd, ¶ms);
558 } else if ((params.type == IH_TYPE_RKSD) ||
559 (params.type == IH_TYPE_RKSPI)) {
560 /* Rockchip has special Image format */
563 ret = rockchip_copy_image(ifd, ¶ms);
567 copy_file(ifd, params.datafile, pad_len);
569 if (params.type == IH_TYPE_FIRMWARE_IVT) {
570 /* Add alignment and IVT */
571 uint32_t aligned_filesize = ALIGN(params.file_size,
573 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
574 params.addr, 0, 0, 0, params.addr
576 - tparams->header_size,
577 params.addr + aligned_filesize
578 - tparams->header_size
580 int i = params.file_size;
581 for (; i < aligned_filesize; i++) {
582 if (write(ifd, (char *) &i, 1) != 1) {
584 "%s: Write error on %s: %s\n",
591 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
592 != sizeof(flash_header_v2_t)) {
593 fprintf(stderr, "%s: Write error on %s: %s\n",
602 /* We're a bit of paranoid */
603 #if defined(_POSIX_SYNCHRONIZED_IO) && \
604 !defined(__sun__) && \
605 !defined(__FreeBSD__) && \
606 !defined(__OpenBSD__) && \
608 (void) fdatasync (ifd);
613 if (fstat(ifd, &sbuf) < 0) {
614 fprintf (stderr, "%s: Can't stat %s: %s\n",
615 params.cmdname, params.imagefile, strerror(errno));
618 params.file_size = sbuf.st_size;
620 map_len = sbuf.st_size;
621 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
622 if (ptr == MAP_FAILED) {
623 fprintf (stderr, "%s: Can't map %s: %s\n",
624 params.cmdname, params.imagefile, strerror(errno));
628 /* Setup the image header as per input image type*/
629 if (tparams->set_header)
630 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
632 fprintf (stderr, "%s: Can't set header for %s: %s\n",
633 params.cmdname, tparams->name, strerror(errno));
637 /* Print the image information by processing image header */
638 if (tparams->print_header)
639 tparams->print_header (ptr);
641 fprintf (stderr, "%s: Can't print header for %s\n",
642 params.cmdname, tparams->name);
645 (void)munmap((void *)ptr, map_len);
647 /* We're a bit of paranoid */
648 #if defined(_POSIX_SYNCHRONIZED_IO) && \
649 !defined(__sun__) && \
650 !defined(__FreeBSD__) && \
651 !defined(__OpenBSD__) && \
653 (void) fdatasync (ifd);
659 fprintf (stderr, "%s: Write error on %s: %s\n",
660 params.cmdname, params.imagefile, strerror(errno));
668 copy_file (int ifd, const char *datafile, int pad)
678 struct image_type_params *tparams = imagetool_get_type(params.type);
680 memset(zeros, 0, sizeof(zeros));
683 fprintf (stderr, "Adding Image %s\n", datafile);
686 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
687 fprintf (stderr, "%s: Can't open %s: %s\n",
688 params.cmdname, datafile, strerror(errno));
692 if (fstat(dfd, &sbuf) < 0) {
693 fprintf (stderr, "%s: Can't stat %s: %s\n",
694 params.cmdname, datafile, strerror(errno));
698 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
699 if (ptr == MAP_FAILED) {
700 fprintf (stderr, "%s: Can't read %s: %s\n",
701 params.cmdname, datafile, strerror(errno));
706 unsigned char *p = NULL;
708 * XIP: do not append the image_header_t at the
709 * beginning of the file, but consume the space
713 if ((unsigned)sbuf.st_size < tparams->header_size) {
715 "%s: Bad size: \"%s\" is too small for XIP\n",
716 params.cmdname, datafile);
720 for (p = ptr; p < ptr + tparams->header_size; p++) {
723 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
724 params.cmdname, datafile);
729 offset = tparams->header_size;
732 size = sbuf.st_size - offset;
734 ret = write(ifd, ptr + offset, size);
737 fprintf (stderr, "%s: Write error on %s: %s\n",
738 params.cmdname, params.imagefile, strerror(errno));
740 fprintf (stderr, "%s: Write only %d/%d bytes, "\
741 "probably no space left on the device\n",
742 params.cmdname, ret, size);
747 if ((pad == 1) && (tail != 0)) {
749 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
750 fprintf (stderr, "%s: Write error on %s: %s\n",
751 params.cmdname, params.imagefile,
755 } else if (pad > 1) {
757 int todo = sizeof(zeros);
761 if (write(ifd, (char *)&zeros, todo) != todo) {
762 fprintf(stderr, "%s: Write error on %s: %s\n",
763 params.cmdname, params.imagefile,
771 (void) munmap((void *)ptr, sbuf.st_size);