2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2009
5 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
8 * SPDX-License-Identifier: GPL-2.0+
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 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");
71 static int h_compare_image_name(const void *vtype1, const void *vtype2)
73 const int *type1 = vtype1;
74 const int *type2 = vtype2;
75 const char *name1 = genimg_get_type_short_name(*type1);
76 const char *name2 = genimg_get_type_short_name(*type2);
78 return strcmp(name1, name2);
81 /* Show all image types supported by mkimage */
82 static void show_image_types(void)
84 struct image_type_params *tparams;
85 int order[IH_TYPE_COUNT];
90 /* Sort the names in order of short name for easier reading */
91 memset(order, '\0', sizeof(order));
92 for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) {
93 tparams = imagetool_get_type(type);
95 order[count++] = type;
97 qsort(order, count, sizeof(int), h_compare_image_name);
99 fprintf(stderr, "\nInvalid image type. Supported image types:\n");
100 for (i = 0; i < count; i++) {
102 tparams = imagetool_get_type(type);
104 fprintf(stderr, "\t%-15s %s\n",
105 genimg_get_type_short_name(type),
106 genimg_get_type_name(type));
109 fprintf(stderr, "\n");
112 static void usage(const char *msg)
114 fprintf(stderr, "Error: %s\n", msg);
115 fprintf(stderr, "Usage: %s -l image\n"
116 " -l ==> list image header information\n",
119 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
120 " -A ==> set architecture to 'arch'\n"
121 " -O ==> set operating system to 'os'\n"
122 " -T ==> set image type to 'type'\n"
123 " -C ==> set compression type 'comp'\n"
124 " -a ==> set load address to 'addr' (hex)\n"
125 " -e ==> set entry point to 'ep' (hex)\n"
126 " -n ==> set image name to 'name'\n"
127 " -d ==> use image data from 'datafile'\n"
128 " -x ==> set XIP (execute in place)\n",
131 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] fit-image\n"
132 " <dtb> file is used with -f auto, it may occour multiple times.\n",
135 " -D => set all options for device tree compiler\n"
136 " -f => input filename for FIT source\n");
137 #ifdef CONFIG_FIT_SIGNATURE
139 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
140 " -E => place data outside of the FIT structure\n"
141 " -k => set directory containing private keys\n"
142 " -K => write public keys to this .dtb file\n"
143 " -c => add comment in signature node\n"
144 " -F => re-sign existing FIT image\n"
145 " -p => place external data at a static position\n"
146 " -r => mark keys used as 'required' in dtb\n");
149 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
151 fprintf(stderr, " %s -V ==> print version information and exit\n",
153 fprintf(stderr, "Use -T to see a list of available image types\n");
158 static int add_content(int type, const char *fname)
160 struct content_info *cont;
162 cont = calloc(1, sizeof(*cont));
167 if (params.content_tail)
168 params.content_tail->next = cont;
170 params.content_head = cont;
171 params.content_tail = cont;
176 static void process_args(int argc, char **argv)
179 int type = IH_TYPE_INVALID;
180 char *datafile = NULL;
183 while ((opt = getopt(argc, argv,
184 "a:A:b:cC:d:D:e:Ef:Fk:K:ln:p:O:rR:qsT:vVx")) != -1) {
187 params.addr = strtoull(optarg, &ptr, 16);
189 fprintf(stderr, "%s: invalid load address %s\n",
190 params.cmdname, optarg);
195 params.arch = genimg_get_arch_id(optarg);
197 usage("Invalid architecture");
200 if (add_content(IH_TYPE_FLATDT, optarg)) {
202 "%s: Out of memory adding content '%s'",
203 params.cmdname, optarg);
208 params.comment = optarg;
211 params.comp = genimg_get_comp_id(optarg);
213 usage("Invalid compression type");
216 params.datafile = optarg;
223 params.ep = strtoull(optarg, &ptr, 16);
225 fprintf(stderr, "%s: invalid entry point %s\n",
226 params.cmdname, optarg);
232 params.external_data = true;
236 params.auto_its = !strcmp(datafile, "auto");
240 * The flattened image tree (FIT) format
241 * requires a flattened device tree image type
243 params.type = IH_TYPE_FLATDT;
247 params.keydir = optarg;
250 params.keydest = optarg;
256 params.imagename = optarg;
259 params.os = genimg_get_os_id(optarg);
261 usage("Invalid operating system");
264 params.external_offset = strtoull(optarg, &ptr, 16);
266 fprintf(stderr, "%s: invalid offset size %s\n",
267 params.cmdname, optarg);
274 params.require_keys = 1;
278 * This entry is for the second configuration
279 * file, if only one is not enough.
281 params.imagename2 = optarg;
287 type = genimg_get_type_id(optarg);
290 usage("Invalid image type");
297 printf("mkimage version %s\n", PLAIN_VERSION);
303 usage("Invalid option");
307 /* The last parameter is expected to be the imagefile */
309 params.imagefile = argv[optind];
312 * For auto-generated FIT images we need to know the image type to put
313 * in the FIT, which is separate from the file's image type (which
314 * will always be IH_TYPE_FLATDT in this case).
316 if (params.type == IH_TYPE_FLATDT) {
317 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
318 /* For auto_its, datafile is always 'auto' */
319 if (!params.auto_its)
320 params.datafile = datafile;
321 else if (!params.datafile)
322 usage("Missing data file for auto-FIT (use -d)");
323 } else if (type != IH_TYPE_INVALID) {
327 if (!params.imagefile)
328 usage("Missing output filename");
331 int main(int argc, char **argv)
337 struct image_type_params *tparams = NULL;
341 params.cmdname = *argv;
345 process_args(argc, argv);
347 /* set tparams as per input type_id */
348 tparams = imagetool_get_type(params.type);
349 if (tparams == NULL) {
350 fprintf (stderr, "%s: unsupported type %s\n",
351 params.cmdname, genimg_get_type_name(params.type));
356 * check the passed arguments parameters meets the requirements
357 * as per image type to be generated/listed
359 if (tparams->check_params)
360 if (tparams->check_params (¶ms))
361 usage("Bad parameters for image type");
364 params.ep = params.addr;
365 /* If XIP, entry point must be after the U-Boot header */
367 params.ep += tparams->header_size;
371 if (tparams->fflag_handle)
373 * in some cases, some additional processing needs
374 * to be done if fflag is defined
376 * For ex. fit_handle_file for Fit file support
378 retval = tparams->fflag_handle(¶ms);
380 if (retval != EXIT_SUCCESS)
384 if (params.lflag || params.fflag) {
385 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
387 ifd = open (params.imagefile,
388 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
392 fprintf (stderr, "%s: Can't open %s: %s\n",
393 params.cmdname, params.imagefile,
398 if (params.lflag || params.fflag) {
400 * list header information of existing image
402 if (fstat(ifd, &sbuf) < 0) {
403 fprintf (stderr, "%s: Can't stat %s: %s\n",
404 params.cmdname, params.imagefile,
409 if ((unsigned)sbuf.st_size < tparams->header_size) {
411 "%s: Bad size: \"%s\" is not valid image\n",
412 params.cmdname, params.imagefile);
416 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
417 if (ptr == MAP_FAILED) {
418 fprintf (stderr, "%s: Can't read %s: %s\n",
419 params.cmdname, params.imagefile,
425 * scan through mkimage registry for all supported image types
426 * and verify the input image file header for match
427 * Print the image information for matched image type
428 * Returns the error code if not matched
430 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);
538 copy_file(ifd, params.datafile, pad_len);
542 /* We're a bit of paranoid */
543 #if defined(_POSIX_SYNCHRONIZED_IO) && \
544 !defined(__sun__) && \
545 !defined(__FreeBSD__) && \
546 !defined(__OpenBSD__) && \
548 (void) fdatasync (ifd);
553 if (fstat(ifd, &sbuf) < 0) {
554 fprintf (stderr, "%s: Can't stat %s: %s\n",
555 params.cmdname, params.imagefile, strerror(errno));
558 params.file_size = sbuf.st_size;
560 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
561 if (ptr == MAP_FAILED) {
562 fprintf (stderr, "%s: Can't map %s: %s\n",
563 params.cmdname, params.imagefile, strerror(errno));
567 /* Setup the image header as per input image type*/
568 if (tparams->set_header)
569 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
571 fprintf (stderr, "%s: Can't set header for %s: %s\n",
572 params.cmdname, tparams->name, strerror(errno));
576 /* Print the image information by processing image header */
577 if (tparams->print_header)
578 tparams->print_header (ptr);
580 fprintf (stderr, "%s: Can't print header for %s: %s\n",
581 params.cmdname, tparams->name, strerror(errno));
585 (void) munmap((void *)ptr, sbuf.st_size);
587 /* We're a bit of paranoid */
588 #if defined(_POSIX_SYNCHRONIZED_IO) && \
589 !defined(__sun__) && \
590 !defined(__FreeBSD__) && \
591 !defined(__OpenBSD__) && \
593 (void) fdatasync (ifd);
599 fprintf (stderr, "%s: Write error on %s: %s\n",
600 params.cmdname, params.imagefile, strerror(errno));
608 copy_file (int ifd, const char *datafile, int pad)
618 struct image_type_params *tparams = imagetool_get_type(params.type);
620 memset(zeros, 0, sizeof(zeros));
623 fprintf (stderr, "Adding Image %s\n", datafile);
626 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
627 fprintf (stderr, "%s: Can't open %s: %s\n",
628 params.cmdname, datafile, strerror(errno));
632 if (fstat(dfd, &sbuf) < 0) {
633 fprintf (stderr, "%s: Can't stat %s: %s\n",
634 params.cmdname, datafile, strerror(errno));
638 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
639 if (ptr == MAP_FAILED) {
640 fprintf (stderr, "%s: Can't read %s: %s\n",
641 params.cmdname, datafile, strerror(errno));
646 unsigned char *p = NULL;
648 * XIP: do not append the image_header_t at the
649 * beginning of the file, but consume the space
653 if ((unsigned)sbuf.st_size < tparams->header_size) {
655 "%s: Bad size: \"%s\" is too small for XIP\n",
656 params.cmdname, datafile);
660 for (p = ptr; p < ptr + tparams->header_size; p++) {
663 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
664 params.cmdname, datafile);
669 offset = tparams->header_size;
672 size = sbuf.st_size - offset;
673 if (write(ifd, ptr + offset, size) != size) {
674 fprintf (stderr, "%s: Write error on %s: %s\n",
675 params.cmdname, params.imagefile, strerror(errno));
680 if ((pad == 1) && (tail != 0)) {
682 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
683 fprintf (stderr, "%s: Write error on %s: %s\n",
684 params.cmdname, params.imagefile,
688 } else if (pad > 1) {
690 int todo = sizeof(zeros);
694 if (write(ifd, (char *)&zeros, todo) != todo) {
695 fprintf(stderr, "%s: Write error on %s: %s\n",
696 params.cmdname, params.imagefile,
704 (void) munmap((void *)ptr, sbuf.st_size);