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 int h_compare_image_name(const void *vtype1, const void *vtype2)
30 const int *type1 = vtype1;
31 const int *type2 = vtype2;
32 const char *name1 = genimg_get_type_short_name(*type1);
33 const char *name2 = genimg_get_type_short_name(*type2);
35 return strcmp(name1, name2);
38 /* Show all image types supported by mkimage */
39 static void show_image_types(void)
41 struct image_type_params *tparams;
42 int order[IH_TYPE_COUNT];
47 /* Sort the names in order of short name for easier reading */
48 memset(order, '\0', sizeof(order));
49 for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) {
50 tparams = imagetool_get_type(type);
52 order[count++] = type;
54 qsort(order, count, sizeof(int), h_compare_image_name);
56 fprintf(stderr, "\nInvalid image type. Supported image types:\n");
57 for (i = 0; i < count; i++) {
59 tparams = imagetool_get_type(type);
61 fprintf(stderr, "\t%-15s %s\n",
62 genimg_get_type_short_name(type),
63 genimg_get_type_name(type));
66 fprintf(stderr, "\n");
69 static void usage(const char *msg)
71 fprintf(stderr, "Error: %s\n", msg);
72 fprintf(stderr, "Usage: %s -l image\n"
73 " -l ==> list image header information\n",
76 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
77 " -A ==> set architecture to 'arch'\n"
78 " -O ==> set operating system to 'os'\n"
79 " -T ==> set image type to 'type'\n"
80 " -C ==> set compression type 'comp'\n"
81 " -a ==> set load address to 'addr' (hex)\n"
82 " -e ==> set entry point to 'ep' (hex)\n"
83 " -n ==> set image name to 'name'\n"
84 " -d ==> use image data from 'datafile'\n"
85 " -x ==> set XIP (execute in place)\n",
88 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] fit-image\n"
89 " <dtb> file is used with -f auto, it may occour multiple times.\n",
92 " -D => set all options for device tree compiler\n"
93 " -f => input filename for FIT source\n");
94 #ifdef CONFIG_FIT_SIGNATURE
96 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
97 " -E => place data outside of the FIT structure\n"
98 " -k => set directory containing private keys\n"
99 " -K => write public keys to this .dtb file\n"
100 " -c => add comment in signature node\n"
101 " -F => re-sign existing FIT image\n"
102 " -p => place external data at a static position\n"
103 " -r => mark keys used as 'required' in dtb\n");
106 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
108 fprintf(stderr, " %s -V ==> print version information and exit\n",
110 fprintf(stderr, "Use -T to see a list of available image types\n");
115 static int add_content(int type, const char *fname)
117 struct content_info *cont;
119 cont = calloc(1, sizeof(*cont));
124 if (params.content_tail)
125 params.content_tail->next = cont;
127 params.content_head = cont;
128 params.content_tail = cont;
133 static void process_args(int argc, char **argv)
136 int type = IH_TYPE_INVALID;
137 char *datafile = NULL;
140 while ((opt = getopt(argc, argv,
141 "a:A:b:cC:d:D:e:Ef:Fk:K:ln:p:O:rR:qsT:vVx")) != -1) {
144 params.addr = strtoull(optarg, &ptr, 16);
146 fprintf(stderr, "%s: invalid load address %s\n",
147 params.cmdname, optarg);
152 params.arch = genimg_get_arch_id(optarg);
154 usage("Invalid architecture");
157 if (add_content(IH_TYPE_FLATDT, optarg)) {
159 "%s: Out of memory adding content '%s'",
160 params.cmdname, optarg);
165 params.comment = optarg;
168 params.comp = genimg_get_comp_id(optarg);
170 usage("Invalid compression type");
173 params.datafile = optarg;
180 params.ep = strtoull(optarg, &ptr, 16);
182 fprintf(stderr, "%s: invalid entry point %s\n",
183 params.cmdname, optarg);
189 params.external_data = true;
193 params.auto_its = !strcmp(datafile, "auto");
197 * The flattened image tree (FIT) format
198 * requires a flattened device tree image type
200 params.type = IH_TYPE_FLATDT;
204 params.keydir = optarg;
207 params.keydest = optarg;
213 params.imagename = optarg;
216 params.os = genimg_get_os_id(optarg);
218 usage("Invalid operating system");
221 params.external_offset = strtoull(optarg, &ptr, 16);
223 fprintf(stderr, "%s: invalid offset size %s\n",
224 params.cmdname, optarg);
231 params.require_keys = 1;
235 * This entry is for the second configuration
236 * file, if only one is not enough.
238 params.imagename2 = optarg;
244 type = genimg_get_type_id(optarg);
247 usage("Invalid image type");
254 printf("mkimage version %s\n", PLAIN_VERSION);
260 usage("Invalid option");
264 /* The last parameter is expected to be the imagefile */
266 params.imagefile = argv[optind];
269 * For auto-generated FIT images we need to know the image type to put
270 * in the FIT, which is separate from the file's image type (which
271 * will always be IH_TYPE_FLATDT in this case).
273 if (params.type == IH_TYPE_FLATDT) {
274 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
275 /* For auto_its, datafile is always 'auto' */
276 if (!params.auto_its)
277 params.datafile = datafile;
278 else if (!params.datafile)
279 usage("Missing data file for auto-FIT (use -d)");
280 } else if (type != IH_TYPE_INVALID) {
284 if (!params.imagefile)
285 usage("Missing output filename");
288 int main(int argc, char **argv)
294 struct image_type_params *tparams = NULL;
298 params.cmdname = *argv;
302 process_args(argc, argv);
304 /* set tparams as per input type_id */
305 tparams = imagetool_get_type(params.type);
306 if (tparams == NULL) {
307 fprintf (stderr, "%s: unsupported type %s\n",
308 params.cmdname, genimg_get_type_name(params.type));
313 * check the passed arguments parameters meets the requirements
314 * as per image type to be generated/listed
316 if (tparams->check_params)
317 if (tparams->check_params (¶ms))
318 usage("Bad parameters for image type");
321 params.ep = params.addr;
322 /* If XIP, entry point must be after the U-Boot header */
324 params.ep += tparams->header_size;
328 if (tparams->fflag_handle)
330 * in some cases, some additional processing needs
331 * to be done if fflag is defined
333 * For ex. fit_handle_file for Fit file support
335 retval = tparams->fflag_handle(¶ms);
337 if (retval != EXIT_SUCCESS)
341 if (params.lflag || params.fflag) {
342 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
344 ifd = open (params.imagefile,
345 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
349 fprintf (stderr, "%s: Can't open %s: %s\n",
350 params.cmdname, params.imagefile,
355 if (params.lflag || params.fflag) {
357 * list header information of existing image
359 if (fstat(ifd, &sbuf) < 0) {
360 fprintf (stderr, "%s: Can't stat %s: %s\n",
361 params.cmdname, params.imagefile,
366 if ((unsigned)sbuf.st_size < tparams->header_size) {
368 "%s: Bad size: \"%s\" is not valid image\n",
369 params.cmdname, params.imagefile);
373 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
374 if (ptr == MAP_FAILED) {
375 fprintf (stderr, "%s: Can't read %s: %s\n",
376 params.cmdname, params.imagefile,
382 * scan through mkimage registry for all supported image types
383 * and verify the input image file header for match
384 * Print the image information for matched image type
385 * Returns the error code if not matched
387 retval = imagetool_verify_print_header(ptr, &sbuf,
390 (void) munmap((void *)ptr, sbuf.st_size);
396 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
397 dfd = open(params.datafile, O_RDONLY | O_BINARY);
399 fprintf(stderr, "%s: Can't open %s: %s\n",
400 params.cmdname, params.datafile,
405 if (fstat(dfd, &sbuf) < 0) {
406 fprintf(stderr, "%s: Can't stat %s: %s\n",
407 params.cmdname, params.datafile,
412 params.file_size = sbuf.st_size + tparams->header_size;
417 * In case there an header with a variable
418 * length will be added, the corresponding
419 * function is called. This is responsible to
420 * allocate memory for the header itself.
422 if (tparams->vrec_header)
423 pad_len = tparams->vrec_header(¶ms, tparams);
425 memset(tparams->hdr, 0, tparams->header_size);
427 if (write(ifd, tparams->hdr, tparams->header_size)
428 != tparams->header_size) {
429 fprintf (stderr, "%s: Write error on %s: %s\n",
430 params.cmdname, params.imagefile, strerror(errno));
434 if (!params.skipcpy) {
435 if (params.type == IH_TYPE_MULTI ||
436 params.type == IH_TYPE_SCRIPT) {
437 char *file = params.datafile;
444 if ((sep = strchr(file, ':')) != NULL) {
448 if (stat (file, &sbuf) < 0) {
449 fprintf (stderr, "%s: Can't stat %s: %s\n",
450 params.cmdname, file, strerror(errno));
453 size = cpu_to_uimage (sbuf.st_size);
458 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
459 fprintf (stderr, "%s: Write error on %s: %s\n",
460 params.cmdname, params.imagefile,
477 file = params.datafile;
480 char *sep = strchr(file, ':');
483 copy_file (ifd, file, 1);
487 copy_file (ifd, file, 0);
491 } else if (params.type == IH_TYPE_PBLIMAGE) {
492 /* PBL has special Image format, implements its' own */
493 pbl_load_uboot(ifd, ¶ms);
495 copy_file(ifd, params.datafile, pad_len);
499 /* We're a bit of paranoid */
500 #if defined(_POSIX_SYNCHRONIZED_IO) && \
501 !defined(__sun__) && \
502 !defined(__FreeBSD__) && \
503 !defined(__OpenBSD__) && \
505 (void) fdatasync (ifd);
510 if (fstat(ifd, &sbuf) < 0) {
511 fprintf (stderr, "%s: Can't stat %s: %s\n",
512 params.cmdname, params.imagefile, strerror(errno));
515 params.file_size = sbuf.st_size;
517 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
518 if (ptr == MAP_FAILED) {
519 fprintf (stderr, "%s: Can't map %s: %s\n",
520 params.cmdname, params.imagefile, strerror(errno));
524 /* Setup the image header as per input image type*/
525 if (tparams->set_header)
526 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
528 fprintf (stderr, "%s: Can't set header for %s: %s\n",
529 params.cmdname, tparams->name, strerror(errno));
533 /* Print the image information by processing image header */
534 if (tparams->print_header)
535 tparams->print_header (ptr);
537 fprintf (stderr, "%s: Can't print header for %s: %s\n",
538 params.cmdname, tparams->name, strerror(errno));
542 (void) munmap((void *)ptr, sbuf.st_size);
544 /* We're a bit of paranoid */
545 #if defined(_POSIX_SYNCHRONIZED_IO) && \
546 !defined(__sun__) && \
547 !defined(__FreeBSD__) && \
548 !defined(__OpenBSD__) && \
550 (void) fdatasync (ifd);
556 fprintf (stderr, "%s: Write error on %s: %s\n",
557 params.cmdname, params.imagefile, strerror(errno));
565 copy_file (int ifd, const char *datafile, int pad)
575 struct image_type_params *tparams = imagetool_get_type(params.type);
577 memset(zeros, 0, sizeof(zeros));
580 fprintf (stderr, "Adding Image %s\n", datafile);
583 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
584 fprintf (stderr, "%s: Can't open %s: %s\n",
585 params.cmdname, datafile, strerror(errno));
589 if (fstat(dfd, &sbuf) < 0) {
590 fprintf (stderr, "%s: Can't stat %s: %s\n",
591 params.cmdname, datafile, strerror(errno));
595 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
596 if (ptr == MAP_FAILED) {
597 fprintf (stderr, "%s: Can't read %s: %s\n",
598 params.cmdname, datafile, strerror(errno));
603 unsigned char *p = NULL;
605 * XIP: do not append the image_header_t at the
606 * beginning of the file, but consume the space
610 if ((unsigned)sbuf.st_size < tparams->header_size) {
612 "%s: Bad size: \"%s\" is too small for XIP\n",
613 params.cmdname, datafile);
617 for (p = ptr; p < ptr + tparams->header_size; p++) {
620 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
621 params.cmdname, datafile);
626 offset = tparams->header_size;
629 size = sbuf.st_size - offset;
630 if (write(ifd, ptr + offset, size) != size) {
631 fprintf (stderr, "%s: Write error on %s: %s\n",
632 params.cmdname, params.imagefile, strerror(errno));
637 if ((pad == 1) && (tail != 0)) {
639 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
640 fprintf (stderr, "%s: Write error on %s: %s\n",
641 params.cmdname, params.imagefile,
645 } else if (pad > 1) {
647 int todo = sizeof(zeros);
651 if (write(ifd, (char *)&zeros, todo) != todo) {
652 fprintf(stderr, "%s: Write error on %s: %s\n",
653 params.cmdname, params.imagefile,
661 (void) munmap((void *)ptr, sbuf.st_size);