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] 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)
115 int type = IH_TYPE_INVALID;
116 char *datafile = NULL;
120 expecting = IH_TYPE_COUNT; /* Unknown */
121 while ((opt = getopt(argc, argv,
122 "-a:A:cC:d:D:e:f:Fk:K:ln:O:rR:sT:vVx")) != -1) {
125 params.addr = strtoull(optarg, &ptr, 16);
127 fprintf(stderr, "%s: invalid load address %s\n",
128 params.cmdname, optarg);
133 params.arch = genimg_get_arch_id(optarg);
135 usage("Invalid architecture");
138 params.comment = optarg;
141 params.comp = genimg_get_comp_id(optarg);
143 usage("Invalid compression type");
146 params.datafile = optarg;
153 params.ep = strtoull(optarg, &ptr, 16);
155 fprintf(stderr, "%s: invalid entry point %s\n",
156 params.cmdname, optarg);
163 params.auto_its = !strcmp(datafile, "auto");
167 * The flattened image tree (FIT) format
168 * requires a flattened device tree image type
170 params.fit_image_type = params.type;
171 params.type = IH_TYPE_FLATDT;
175 params.keydir = optarg;
178 params.keydest = optarg;
184 params.imagename = optarg;
187 params.os = genimg_get_os_id(optarg);
189 usage("Invalid operating system");
192 params.require_keys = 1;
196 * This entry is for the second configuration
197 * file, if only one is not enough.
199 params.imagename2 = optarg;
205 type = genimg_get_type_id(optarg);
208 usage("Invalid image type");
216 printf("mkimage version %s\n", PLAIN_VERSION);
222 if (expecting == type || optind == argc) {
223 params.imagefile = optarg;
224 expecting = IH_TYPE_INVALID;
228 usage("Invalid option");
233 * For auto-generated FIT images we need to know the image type to put
234 * in the FIT, which is separate from the file's image type (which
235 * will always be IH_TYPE_FLATDT in this case).
237 if (params.type == IH_TYPE_FLATDT) {
238 params.fit_image_type = type;
239 if (!params.auto_its)
240 params.datafile = datafile;
241 } else if (type != IH_TYPE_INVALID) {
245 if (!params.imagefile)
246 usage("Missing output filename");
250 int main(int argc, char **argv)
256 struct image_type_params *tparams = NULL;
260 params.cmdname = *argv;
264 process_args(argc, argv);
266 /* set tparams as per input type_id */
267 tparams = imagetool_get_type(params.type);
268 if (tparams == NULL) {
269 fprintf (stderr, "%s: unsupported type %s\n",
270 params.cmdname, genimg_get_type_name(params.type));
275 * check the passed arguments parameters meets the requirements
276 * as per image type to be generated/listed
278 if (tparams->check_params)
279 if (tparams->check_params (¶ms))
280 usage("Bad parameters for image type");
283 params.ep = params.addr;
284 /* If XIP, entry point must be after the U-Boot header */
286 params.ep += tparams->header_size;
290 if (tparams->fflag_handle)
292 * in some cases, some additional processing needs
293 * to be done if fflag is defined
295 * For ex. fit_handle_file for Fit file support
297 retval = tparams->fflag_handle(¶ms);
299 if (retval != EXIT_SUCCESS)
303 if (params.lflag || params.fflag) {
304 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
306 ifd = open (params.imagefile,
307 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
311 fprintf (stderr, "%s: Can't open %s: %s\n",
312 params.cmdname, params.imagefile,
317 if (params.lflag || params.fflag) {
319 * list header information of existing image
321 if (fstat(ifd, &sbuf) < 0) {
322 fprintf (stderr, "%s: Can't stat %s: %s\n",
323 params.cmdname, params.imagefile,
328 if ((unsigned)sbuf.st_size < tparams->header_size) {
330 "%s: Bad size: \"%s\" is not valid image\n",
331 params.cmdname, params.imagefile);
335 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
336 if (ptr == MAP_FAILED) {
337 fprintf (stderr, "%s: Can't read %s: %s\n",
338 params.cmdname, params.imagefile,
344 * scan through mkimage registry for all supported image types
345 * and verify the input image file header for match
346 * Print the image information for matched image type
347 * Returns the error code if not matched
349 retval = imagetool_verify_print_header(ptr, &sbuf,
352 (void) munmap((void *)ptr, sbuf.st_size);
358 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
359 dfd = open(params.datafile, O_RDONLY | O_BINARY);
361 fprintf(stderr, "%s: Can't open %s: %s\n",
362 params.cmdname, params.datafile,
367 if (fstat(dfd, &sbuf) < 0) {
368 fprintf(stderr, "%s: Can't stat %s: %s\n",
369 params.cmdname, params.datafile,
374 params.file_size = sbuf.st_size + tparams->header_size;
379 * In case there an header with a variable
380 * length will be added, the corresponding
381 * function is called. This is responsible to
382 * allocate memory for the header itself.
384 if (tparams->vrec_header)
385 pad_len = tparams->vrec_header(¶ms, tparams);
387 memset(tparams->hdr, 0, tparams->header_size);
389 if (write(ifd, tparams->hdr, tparams->header_size)
390 != tparams->header_size) {
391 fprintf (stderr, "%s: Write error on %s: %s\n",
392 params.cmdname, params.imagefile, strerror(errno));
396 if (!params.skipcpy) {
397 if (params.type == IH_TYPE_MULTI ||
398 params.type == IH_TYPE_SCRIPT) {
399 char *file = params.datafile;
406 if ((sep = strchr(file, ':')) != NULL) {
410 if (stat (file, &sbuf) < 0) {
411 fprintf (stderr, "%s: Can't stat %s: %s\n",
412 params.cmdname, file, strerror(errno));
415 size = cpu_to_uimage (sbuf.st_size);
420 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
421 fprintf (stderr, "%s: Write error on %s: %s\n",
422 params.cmdname, params.imagefile,
439 file = params.datafile;
442 char *sep = strchr(file, ':');
445 copy_file (ifd, file, 1);
449 copy_file (ifd, file, 0);
453 } else if (params.type == IH_TYPE_PBLIMAGE) {
454 /* PBL has special Image format, implements its' own */
455 pbl_load_uboot(ifd, ¶ms);
457 copy_file(ifd, params.datafile, pad_len);
461 /* We're a bit of paranoid */
462 #if defined(_POSIX_SYNCHRONIZED_IO) && \
463 !defined(__sun__) && \
464 !defined(__FreeBSD__) && \
465 !defined(__OpenBSD__) && \
467 (void) fdatasync (ifd);
472 if (fstat(ifd, &sbuf) < 0) {
473 fprintf (stderr, "%s: Can't stat %s: %s\n",
474 params.cmdname, params.imagefile, strerror(errno));
477 params.file_size = sbuf.st_size;
479 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
480 if (ptr == MAP_FAILED) {
481 fprintf (stderr, "%s: Can't map %s: %s\n",
482 params.cmdname, params.imagefile, strerror(errno));
486 /* Setup the image header as per input image type*/
487 if (tparams->set_header)
488 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
490 fprintf (stderr, "%s: Can't set header for %s: %s\n",
491 params.cmdname, tparams->name, strerror(errno));
495 /* Print the image information by processing image header */
496 if (tparams->print_header)
497 tparams->print_header (ptr);
499 fprintf (stderr, "%s: Can't print header for %s: %s\n",
500 params.cmdname, tparams->name, strerror(errno));
504 (void) munmap((void *)ptr, sbuf.st_size);
506 /* We're a bit of paranoid */
507 #if defined(_POSIX_SYNCHRONIZED_IO) && \
508 !defined(__sun__) && \
509 !defined(__FreeBSD__) && \
510 !defined(__OpenBSD__) && \
512 (void) fdatasync (ifd);
518 fprintf (stderr, "%s: Write error on %s: %s\n",
519 params.cmdname, params.imagefile, strerror(errno));
527 copy_file (int ifd, const char *datafile, int pad)
537 struct image_type_params *tparams = imagetool_get_type(params.type);
539 memset(zeros, 0, sizeof(zeros));
542 fprintf (stderr, "Adding Image %s\n", datafile);
545 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
546 fprintf (stderr, "%s: Can't open %s: %s\n",
547 params.cmdname, datafile, strerror(errno));
551 if (fstat(dfd, &sbuf) < 0) {
552 fprintf (stderr, "%s: Can't stat %s: %s\n",
553 params.cmdname, datafile, strerror(errno));
557 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
558 if (ptr == MAP_FAILED) {
559 fprintf (stderr, "%s: Can't read %s: %s\n",
560 params.cmdname, datafile, strerror(errno));
565 unsigned char *p = NULL;
567 * XIP: do not append the image_header_t at the
568 * beginning of the file, but consume the space
572 if ((unsigned)sbuf.st_size < tparams->header_size) {
574 "%s: Bad size: \"%s\" is too small for XIP\n",
575 params.cmdname, datafile);
579 for (p = ptr; p < ptr + tparams->header_size; p++) {
582 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
583 params.cmdname, datafile);
588 offset = tparams->header_size;
591 size = sbuf.st_size - offset;
592 if (write(ifd, ptr + offset, size) != size) {
593 fprintf (stderr, "%s: Write error on %s: %s\n",
594 params.cmdname, params.imagefile, strerror(errno));
599 if ((pad == 1) && (tail != 0)) {
601 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
602 fprintf (stderr, "%s: Write error on %s: %s\n",
603 params.cmdname, params.imagefile,
607 } else if (pad > 1) {
609 int todo = sizeof(zeros);
613 if (write(ifd, (char *)&zeros, todo) != todo) {
614 fprintf(stderr, "%s: Write error on %s: %s\n",
615 params.cmdname, params.imagefile,
623 (void) munmap((void *)ptr, sbuf.st_size);