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
15 static void copy_file(int, const char *, int);
17 /* parameters initialized by core will be used by the image type code */
18 static struct image_tool_params params = {
21 .type = IH_TYPE_KERNEL,
23 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
28 static enum ih_category cur_category;
30 static int h_compare_category_name(const void *vtype1, const void *vtype2)
32 const int *type1 = vtype1;
33 const int *type2 = vtype2;
34 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
35 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
37 return strcmp(name1, name2);
40 static int show_valid_options(enum ih_category category)
47 count = genimg_get_cat_count(category);
48 order = calloc(count, sizeof(*order));
52 /* Sort the names in order of short name for easier reading */
53 for (item = 0; item < count; item++)
55 cur_category = category;
56 qsort(order, count, sizeof(int), h_compare_category_name);
58 fprintf(stderr, "\nInvalid %s, supported are:\n",
59 genimg_get_cat_desc(category));
60 for (i = 0; i < count; i++) {
62 fprintf(stderr, "\t%-15s %s\n",
63 genimg_get_cat_short_name(category, item),
64 genimg_get_cat_name(category, item));
66 fprintf(stderr, "\n");
72 static void usage(const char *msg)
74 fprintf(stderr, "Error: %s\n", msg);
75 fprintf(stderr, "Usage: %s -l image\n"
76 " -l ==> list image header information\n",
79 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
80 " -A ==> set architecture to 'arch'\n"
81 " -O ==> set operating system to 'os'\n"
82 " -T ==> set image type to 'type'\n"
83 " -C ==> set compression type 'comp'\n"
84 " -a ==> set load address to 'addr' (hex)\n"
85 " -e ==> set entry point to 'ep' (hex)\n"
86 " -n ==> set image name to 'name'\n"
87 " -d ==> use image data from 'datafile'\n"
88 " -x ==> set XIP (execute in place)\n",
91 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n"
92 " <dtb> file is used with -f auto, it may occur multiple times.\n",
95 " -D => set all options for device tree compiler\n"
96 " -f => input filename for FIT source\n"
97 " -i => input filename for ramdisk file\n");
98 #ifdef CONFIG_FIT_SIGNATURE
100 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]\n"
101 " -E => place data outside of the FIT structure\n"
102 " -k => set directory containing private keys\n"
103 " -K => write public keys to this .dtb file\n"
104 " -c => add comment in signature node\n"
105 " -F => re-sign existing FIT image\n"
106 " -p => place external data at a static position\n"
107 " -r => mark keys used as 'required' in dtb\n"
108 " -N => openssl engine to use for signing\n");
111 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
113 fprintf(stderr, " %s -V ==> print version information and exit\n",
115 fprintf(stderr, "Use '-T list' to see a list of available image types\n");
120 static int add_content(int type, const char *fname)
122 struct content_info *cont;
124 cont = calloc(1, sizeof(*cont));
129 if (params.content_tail)
130 params.content_tail->next = cont;
132 params.content_head = cont;
133 params.content_tail = cont;
138 static void process_args(int argc, char **argv)
141 int type = IH_TYPE_INVALID;
142 char *datafile = NULL;
145 while ((opt = getopt(argc, argv,
146 "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:N:p:O:rR:qsT:vVx")) != -1) {
149 params.addr = strtoull(optarg, &ptr, 16);
151 fprintf(stderr, "%s: invalid load address %s\n",
152 params.cmdname, optarg);
157 params.arch = genimg_get_arch_id(optarg);
158 if (params.arch < 0) {
159 show_valid_options(IH_ARCH);
160 usage("Invalid architecture");
164 if (add_content(IH_TYPE_FLATDT, optarg)) {
166 "%s: Out of memory adding content '%s'",
167 params.cmdname, optarg);
172 params.comment = optarg;
175 params.comp = genimg_get_comp_id(optarg);
176 if (params.comp < 0) {
177 show_valid_options(IH_COMP);
178 usage("Invalid compression type");
182 params.datafile = optarg;
189 params.ep = strtoull(optarg, &ptr, 16);
191 fprintf(stderr, "%s: invalid entry point %s\n",
192 params.cmdname, optarg);
198 params.external_data = true;
202 params.auto_its = !strcmp(datafile, "auto");
206 * The flattened image tree (FIT) format
207 * requires a flattened device tree image type
209 params.type = IH_TYPE_FLATDT;
213 params.fit_ramdisk = optarg;
216 params.keydir = optarg;
219 params.keydest = optarg;
225 params.imagename = optarg;
228 params.engine_id = optarg;
231 params.os = genimg_get_os_id(optarg);
233 show_valid_options(IH_OS);
234 usage("Invalid operating system");
238 params.external_offset = strtoull(optarg, &ptr, 16);
240 fprintf(stderr, "%s: invalid offset size %s\n",
241 params.cmdname, optarg);
249 params.require_keys = 1;
253 * This entry is for the second configuration
254 * file, if only one is not enough.
256 params.imagename2 = optarg;
262 if (strcmp(optarg, "list") == 0) {
263 show_valid_options(IH_TYPE);
266 type = genimg_get_type_id(optarg);
268 show_valid_options(IH_TYPE);
269 usage("Invalid image type");
276 printf("mkimage version %s\n", PLAIN_VERSION);
282 usage("Invalid option");
286 /* The last parameter is expected to be the imagefile */
288 params.imagefile = argv[optind];
291 * For auto-generated FIT images we need to know the image type to put
292 * in the FIT, which is separate from the file's image type (which
293 * will always be IH_TYPE_FLATDT in this case).
295 if (params.type == IH_TYPE_FLATDT) {
296 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
297 /* For auto_its, datafile is always 'auto' */
298 if (!params.auto_its)
299 params.datafile = datafile;
300 else if (!params.datafile)
301 usage("Missing data file for auto-FIT (use -d)");
302 } else if (type != IH_TYPE_INVALID) {
303 if (type == IH_TYPE_SCRIPT && !params.datafile)
304 usage("Missing data file for script (use -d)");
308 if (!params.imagefile)
309 usage("Missing output filename");
312 int main(int argc, char **argv)
318 struct image_type_params *tparams = NULL;
323 params.cmdname = *argv;
327 process_args(argc, argv);
329 /* set tparams as per input type_id */
330 tparams = imagetool_get_type(params.type);
331 if (tparams == NULL) {
332 fprintf (stderr, "%s: unsupported type %s\n",
333 params.cmdname, genimg_get_type_name(params.type));
338 * check the passed arguments parameters meets the requirements
339 * as per image type to be generated/listed
341 if (tparams->check_params)
342 if (tparams->check_params (¶ms))
343 usage("Bad parameters for image type");
346 params.ep = params.addr;
347 /* If XIP, entry point must be after the U-Boot header */
349 params.ep += tparams->header_size;
353 if (tparams->fflag_handle)
355 * in some cases, some additional processing needs
356 * to be done if fflag is defined
358 * For ex. fit_handle_file for Fit file support
360 retval = tparams->fflag_handle(¶ms);
362 if (retval != EXIT_SUCCESS)
366 if (params.lflag || params.fflag) {
367 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
369 ifd = open (params.imagefile,
370 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
374 fprintf (stderr, "%s: Can't open %s: %s\n",
375 params.cmdname, params.imagefile,
380 if (params.lflag || params.fflag) {
382 * list header information of existing image
384 if (fstat(ifd, &sbuf) < 0) {
385 fprintf (stderr, "%s: Can't stat %s: %s\n",
386 params.cmdname, params.imagefile,
391 if ((unsigned)sbuf.st_size < tparams->header_size) {
393 "%s: Bad size: \"%s\" is not valid image\n",
394 params.cmdname, params.imagefile);
398 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
399 if (ptr == MAP_FAILED) {
400 fprintf (stderr, "%s: Can't read %s: %s\n",
401 params.cmdname, params.imagefile,
408 * Verifies the header format based on the expected header for image
411 retval = imagetool_verify_print_header_by_type(ptr, &sbuf,
415 * When listing the image, we are not given the image type. Simply check all
416 * image types to find one that matches our header
418 retval = imagetool_verify_print_header(ptr, &sbuf,
422 (void) munmap((void *)ptr, sbuf.st_size);
428 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
429 dfd = open(params.datafile, O_RDONLY | O_BINARY);
431 fprintf(stderr, "%s: Can't open %s: %s\n",
432 params.cmdname, params.datafile,
437 if (fstat(dfd, &sbuf) < 0) {
438 fprintf(stderr, "%s: Can't stat %s: %s\n",
439 params.cmdname, params.datafile,
444 params.file_size = sbuf.st_size + tparams->header_size;
449 * In case there an header with a variable
450 * length will be added, the corresponding
451 * function is called. This is responsible to
452 * allocate memory for the header itself.
454 if (tparams->vrec_header)
455 pad_len = tparams->vrec_header(¶ms, tparams);
457 memset(tparams->hdr, 0, tparams->header_size);
459 if (write(ifd, tparams->hdr, tparams->header_size)
460 != tparams->header_size) {
461 fprintf (stderr, "%s: Write error on %s: %s\n",
462 params.cmdname, params.imagefile, strerror(errno));
466 if (!params.skipcpy) {
467 if (params.type == IH_TYPE_MULTI ||
468 params.type == IH_TYPE_SCRIPT) {
469 char *file = params.datafile;
476 if ((sep = strchr(file, ':')) != NULL) {
480 if (stat (file, &sbuf) < 0) {
481 fprintf (stderr, "%s: Can't stat %s: %s\n",
482 params.cmdname, file, strerror(errno));
485 size = cpu_to_uimage (sbuf.st_size);
490 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
491 fprintf (stderr, "%s: Write error on %s: %s\n",
492 params.cmdname, params.imagefile,
509 file = params.datafile;
512 char *sep = strchr(file, ':');
515 copy_file (ifd, file, 1);
519 copy_file (ifd, file, 0);
523 } else if (params.type == IH_TYPE_PBLIMAGE) {
524 /* PBL has special Image format, implements its' own */
525 pbl_load_uboot(ifd, ¶ms);
526 } else if (params.type == IH_TYPE_ZYNQMPBIF) {
527 /* Image file is meta, walk through actual targets */
530 ret = zynqmpbif_copy_image(ifd, ¶ms);
533 } else if (params.type == IH_TYPE_IMX8IMAGE) {
534 /* i.MX8/8X has special Image format */
537 ret = imx8image_copy_image(ifd, ¶ms);
540 } else if (params.type == IH_TYPE_IMX8MIMAGE) {
541 /* i.MX8M has special Image format */
544 ret = imx8mimage_copy_image(ifd, ¶ms);
547 } else if ((params.type == IH_TYPE_RKSD) ||
548 (params.type == IH_TYPE_RKSPI)) {
549 /* Rockchip has special Image format */
552 ret = rockchip_copy_image(ifd, ¶ms);
556 copy_file(ifd, params.datafile, pad_len);
558 if (params.type == IH_TYPE_FIRMWARE_IVT) {
559 /* Add alignment and IVT */
560 uint32_t aligned_filesize = (params.file_size + 0x1000
561 - 1) & ~(0x1000 - 1);
562 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
563 params.addr, 0, 0, 0, params.addr
565 - tparams->header_size,
566 params.addr + aligned_filesize
567 - tparams->header_size
569 int i = params.file_size;
570 for (; i < aligned_filesize; i++) {
571 if (write(ifd, (char *) &i, 1) != 1) {
573 "%s: Write error on %s: %s\n",
580 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
581 != sizeof(flash_header_v2_t)) {
582 fprintf(stderr, "%s: Write error on %s: %s\n",
591 /* We're a bit of paranoid */
592 #if defined(_POSIX_SYNCHRONIZED_IO) && \
593 !defined(__sun__) && \
594 !defined(__FreeBSD__) && \
595 !defined(__OpenBSD__) && \
597 (void) fdatasync (ifd);
602 if (fstat(ifd, &sbuf) < 0) {
603 fprintf (stderr, "%s: Can't stat %s: %s\n",
604 params.cmdname, params.imagefile, strerror(errno));
607 params.file_size = sbuf.st_size;
609 map_len = sbuf.st_size;
610 ptr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
611 if (ptr == MAP_FAILED) {
612 fprintf (stderr, "%s: Can't map %s: %s\n",
613 params.cmdname, params.imagefile, strerror(errno));
617 /* Setup the image header as per input image type*/
618 if (tparams->set_header)
619 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
621 fprintf (stderr, "%s: Can't set header for %s: %s\n",
622 params.cmdname, tparams->name, strerror(errno));
626 /* Print the image information by processing image header */
627 if (tparams->print_header)
628 tparams->print_header (ptr);
630 fprintf (stderr, "%s: Can't print header for %s\n",
631 params.cmdname, tparams->name);
634 (void)munmap((void *)ptr, map_len);
636 /* We're a bit of paranoid */
637 #if defined(_POSIX_SYNCHRONIZED_IO) && \
638 !defined(__sun__) && \
639 !defined(__FreeBSD__) && \
640 !defined(__OpenBSD__) && \
642 (void) fdatasync (ifd);
648 fprintf (stderr, "%s: Write error on %s: %s\n",
649 params.cmdname, params.imagefile, strerror(errno));
657 copy_file (int ifd, const char *datafile, int pad)
667 struct image_type_params *tparams = imagetool_get_type(params.type);
669 memset(zeros, 0, sizeof(zeros));
672 fprintf (stderr, "Adding Image %s\n", datafile);
675 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
676 fprintf (stderr, "%s: Can't open %s: %s\n",
677 params.cmdname, datafile, strerror(errno));
681 if (fstat(dfd, &sbuf) < 0) {
682 fprintf (stderr, "%s: Can't stat %s: %s\n",
683 params.cmdname, datafile, strerror(errno));
687 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
688 if (ptr == MAP_FAILED) {
689 fprintf (stderr, "%s: Can't read %s: %s\n",
690 params.cmdname, datafile, strerror(errno));
695 unsigned char *p = NULL;
697 * XIP: do not append the image_header_t at the
698 * beginning of the file, but consume the space
702 if ((unsigned)sbuf.st_size < tparams->header_size) {
704 "%s: Bad size: \"%s\" is too small for XIP\n",
705 params.cmdname, datafile);
709 for (p = ptr; p < ptr + tparams->header_size; p++) {
712 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
713 params.cmdname, datafile);
718 offset = tparams->header_size;
721 size = sbuf.st_size - offset;
722 if (write(ifd, ptr + offset, size) != size) {
723 fprintf (stderr, "%s: Write error on %s: %s\n",
724 params.cmdname, params.imagefile, strerror(errno));
729 if ((pad == 1) && (tail != 0)) {
731 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
732 fprintf (stderr, "%s: Write error on %s: %s\n",
733 params.cmdname, params.imagefile,
737 } else if (pad > 1) {
739 int todo = sizeof(zeros);
743 if (write(ifd, (char *)&zeros, todo) != todo) {
744 fprintf(stderr, "%s: Write error on %s: %s\n",
745 params.cmdname, params.imagefile,
753 (void) munmap((void *)ptr, sbuf.st_size);