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 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] fit-image\n",
91 " -D => set all options for device tree compiler\n"
92 " -f => input filename for FIT source\n");
93 #ifdef CONFIG_FIT_SIGNATURE
95 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
96 " -k => set directory containing private keys\n"
97 " -K => write public keys to this .dtb file\n"
98 " -c => add comment in signature node\n"
99 " -F => re-sign existing FIT image\n"
100 " -r => mark keys used as 'required' in dtb\n");
103 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
105 fprintf(stderr, " %s -V ==> print version information and exit\n",
107 fprintf(stderr, "Use -T to see a list of available image types\n");
112 static void process_args(int argc, char **argv)
117 while ((opt = getopt(argc, argv,
118 "a:A:cC:d:D:e:f:Fk:K:ln:O:rR:sT:vVx")) != -1) {
121 params.addr = strtoull(optarg, &ptr, 16);
123 fprintf(stderr, "%s: invalid load address %s\n",
124 params.cmdname, optarg);
129 params.arch = genimg_get_arch_id(optarg);
131 usage("Invalid architecture");
134 params.comment = optarg;
137 params.comp = genimg_get_comp_id(optarg);
139 usage("Invalid compression type");
142 params.datafile = optarg;
149 params.ep = strtoull(optarg, &ptr, 16);
151 fprintf(stderr, "%s: invalid entry point %s\n",
152 params.cmdname, optarg);
158 params.datafile = optarg;
162 * The flattened image tree (FIT) format
163 * requires a flattened device tree image type
165 params.type = IH_TYPE_FLATDT;
169 params.keydir = optarg;
172 params.keydest = optarg;
178 params.imagename = optarg;
181 params.os = genimg_get_os_id(optarg);
183 usage("Invalid operating system");
186 params.require_keys = 1;
190 * This entry is for the second configuration
191 * file, if only one is not enough.
193 params.imagename2 = optarg;
199 params.type = genimg_get_type_id(optarg);
200 if (params.type < 0) {
202 usage("Invalid image type");
209 printf("mkimage version %s\n", PLAIN_VERSION);
215 usage("Invalid option");
220 usage("Missing output filename");
221 params.imagefile = argv[optind];
225 int main(int argc, char **argv)
231 struct image_type_params *tparams = NULL;
235 params.cmdname = *argv;
239 process_args(argc, argv);
241 /* set tparams as per input type_id */
242 tparams = imagetool_get_type(params.type);
243 if (tparams == NULL) {
244 fprintf (stderr, "%s: unsupported type %s\n",
245 params.cmdname, genimg_get_type_name(params.type));
250 * check the passed arguments parameters meets the requirements
251 * as per image type to be generated/listed
253 if (tparams->check_params)
254 if (tparams->check_params (¶ms))
255 usage("Bad parameters for image type");
258 params.ep = params.addr;
259 /* If XIP, entry point must be after the U-Boot header */
261 params.ep += tparams->header_size;
265 if (tparams->fflag_handle)
267 * in some cases, some additional processing needs
268 * to be done if fflag is defined
270 * For ex. fit_handle_file for Fit file support
272 retval = tparams->fflag_handle(¶ms);
274 if (retval != EXIT_SUCCESS)
278 if (params.lflag || params.fflag) {
279 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
281 ifd = open (params.imagefile,
282 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
286 fprintf (stderr, "%s: Can't open %s: %s\n",
287 params.cmdname, params.imagefile,
292 if (params.lflag || params.fflag) {
294 * list header information of existing image
296 if (fstat(ifd, &sbuf) < 0) {
297 fprintf (stderr, "%s: Can't stat %s: %s\n",
298 params.cmdname, params.imagefile,
303 if ((unsigned)sbuf.st_size < tparams->header_size) {
305 "%s: Bad size: \"%s\" is not valid image\n",
306 params.cmdname, params.imagefile);
310 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
311 if (ptr == MAP_FAILED) {
312 fprintf (stderr, "%s: Can't read %s: %s\n",
313 params.cmdname, params.imagefile,
319 * scan through mkimage registry for all supported image types
320 * and verify the input image file header for match
321 * Print the image information for matched image type
322 * Returns the error code if not matched
324 retval = imagetool_verify_print_header(ptr, &sbuf,
327 (void) munmap((void *)ptr, sbuf.st_size);
333 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
334 dfd = open(params.datafile, O_RDONLY | O_BINARY);
336 fprintf(stderr, "%s: Can't open %s: %s\n",
337 params.cmdname, params.datafile,
342 if (fstat(dfd, &sbuf) < 0) {
343 fprintf(stderr, "%s: Can't stat %s: %s\n",
344 params.cmdname, params.datafile,
349 params.file_size = sbuf.st_size + tparams->header_size;
354 * In case there an header with a variable
355 * length will be added, the corresponding
356 * function is called. This is responsible to
357 * allocate memory for the header itself.
359 if (tparams->vrec_header)
360 pad_len = tparams->vrec_header(¶ms, tparams);
362 memset(tparams->hdr, 0, tparams->header_size);
364 if (write(ifd, tparams->hdr, tparams->header_size)
365 != tparams->header_size) {
366 fprintf (stderr, "%s: Write error on %s: %s\n",
367 params.cmdname, params.imagefile, strerror(errno));
371 if (!params.skipcpy) {
372 if (params.type == IH_TYPE_MULTI ||
373 params.type == IH_TYPE_SCRIPT) {
374 char *file = params.datafile;
381 if ((sep = strchr(file, ':')) != NULL) {
385 if (stat (file, &sbuf) < 0) {
386 fprintf (stderr, "%s: Can't stat %s: %s\n",
387 params.cmdname, file, strerror(errno));
390 size = cpu_to_uimage (sbuf.st_size);
395 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
396 fprintf (stderr, "%s: Write error on %s: %s\n",
397 params.cmdname, params.imagefile,
414 file = params.datafile;
417 char *sep = strchr(file, ':');
420 copy_file (ifd, file, 1);
424 copy_file (ifd, file, 0);
428 } else if (params.type == IH_TYPE_PBLIMAGE) {
429 /* PBL has special Image format, implements its' own */
430 pbl_load_uboot(ifd, ¶ms);
432 copy_file(ifd, params.datafile, pad_len);
436 /* We're a bit of paranoid */
437 #if defined(_POSIX_SYNCHRONIZED_IO) && \
438 !defined(__sun__) && \
439 !defined(__FreeBSD__) && \
440 !defined(__OpenBSD__) && \
442 (void) fdatasync (ifd);
447 if (fstat(ifd, &sbuf) < 0) {
448 fprintf (stderr, "%s: Can't stat %s: %s\n",
449 params.cmdname, params.imagefile, strerror(errno));
452 params.file_size = sbuf.st_size;
454 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
455 if (ptr == MAP_FAILED) {
456 fprintf (stderr, "%s: Can't map %s: %s\n",
457 params.cmdname, params.imagefile, strerror(errno));
461 /* Setup the image header as per input image type*/
462 if (tparams->set_header)
463 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
465 fprintf (stderr, "%s: Can't set header for %s: %s\n",
466 params.cmdname, tparams->name, strerror(errno));
470 /* Print the image information by processing image header */
471 if (tparams->print_header)
472 tparams->print_header (ptr);
474 fprintf (stderr, "%s: Can't print header for %s: %s\n",
475 params.cmdname, tparams->name, strerror(errno));
479 (void) munmap((void *)ptr, sbuf.st_size);
481 /* We're a bit of paranoid */
482 #if defined(_POSIX_SYNCHRONIZED_IO) && \
483 !defined(__sun__) && \
484 !defined(__FreeBSD__) && \
485 !defined(__OpenBSD__) && \
487 (void) fdatasync (ifd);
493 fprintf (stderr, "%s: Write error on %s: %s\n",
494 params.cmdname, params.imagefile, strerror(errno));
502 copy_file (int ifd, const char *datafile, int pad)
512 struct image_type_params *tparams = imagetool_get_type(params.type);
514 memset(zeros, 0, sizeof(zeros));
517 fprintf (stderr, "Adding Image %s\n", datafile);
520 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
521 fprintf (stderr, "%s: Can't open %s: %s\n",
522 params.cmdname, datafile, strerror(errno));
526 if (fstat(dfd, &sbuf) < 0) {
527 fprintf (stderr, "%s: Can't stat %s: %s\n",
528 params.cmdname, datafile, strerror(errno));
532 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
533 if (ptr == MAP_FAILED) {
534 fprintf (stderr, "%s: Can't read %s: %s\n",
535 params.cmdname, datafile, strerror(errno));
540 unsigned char *p = NULL;
542 * XIP: do not append the image_header_t at the
543 * beginning of the file, but consume the space
547 if ((unsigned)sbuf.st_size < tparams->header_size) {
549 "%s: Bad size: \"%s\" is too small for XIP\n",
550 params.cmdname, datafile);
554 for (p = ptr; p < ptr + tparams->header_size; p++) {
557 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
558 params.cmdname, datafile);
563 offset = tparams->header_size;
566 size = sbuf.st_size - offset;
567 if (write(ifd, ptr + offset, size) != size) {
568 fprintf (stderr, "%s: Write error on %s: %s\n",
569 params.cmdname, params.imagefile, strerror(errno));
574 if ((pad == 1) && (tail != 0)) {
576 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
577 fprintf (stderr, "%s: Write error on %s: %s\n",
578 params.cmdname, params.imagefile,
582 } else if (pad > 1) {
584 int todo = sizeof(zeros);
588 if (write(ifd, (char *)&zeros, todo) != todo) {
589 fprintf(stderr, "%s: Write error on %s: %s\n",
590 params.cmdname, params.imagefile,
598 (void) munmap((void *)ptr, sbuf.st_size);