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(void)
71 fprintf(stderr, "Usage: %s -l image\n"
72 " -l ==> list image header information\n",
75 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
76 " -A ==> set architecture to 'arch'\n"
77 " -O ==> set operating system to 'os'\n"
78 " -T ==> set image type to 'type'\n"
79 " -C ==> set compression type 'comp'\n"
80 " -a ==> set load address to 'addr' (hex)\n"
81 " -e ==> set entry point to 'ep' (hex)\n"
82 " -n ==> set image name to 'name'\n"
83 " -d ==> use image data from 'datafile'\n"
84 " -x ==> set XIP (execute in place)\n",
87 " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
90 " -D => set all options for device tree compiler\n"
91 " -f => input filename for FIT source\n");
92 #ifdef CONFIG_FIT_SIGNATURE
94 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
95 " -k => set directory containing private keys\n"
96 " -K => write public keys to this .dtb file\n"
97 " -c => add comment in signature node\n"
98 " -F => re-sign existing FIT image\n"
99 " -r => mark keys used as 'required' in dtb\n");
102 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
104 fprintf(stderr, " %s -V ==> print version information and exit\n",
106 fprintf(stderr, "Use -T to see a list of available image types\n");
111 static void process_args(int argc, char **argv)
116 while ((opt = getopt(argc, argv,
117 "a:A:cC:d:D:e:f:Fk:K:ln:O:rR:sT:vVx")) != -1) {
120 params.addr = strtoull(optarg, &ptr, 16);
122 fprintf(stderr, "%s: invalid load address %s\n",
123 params.cmdname, optarg);
128 params.arch = genimg_get_arch_id(optarg);
133 params.comment = optarg;
136 params.comp = genimg_get_comp_id(optarg);
141 params.datafile = optarg;
148 params.ep = strtoull(optarg, &ptr, 16);
150 fprintf(stderr, "%s: invalid entry point %s\n",
151 params.cmdname, optarg);
157 params.datafile = optarg;
161 * The flattened image tree (FIT) format
162 * requires a flattened device tree image type
164 params.type = IH_TYPE_FLATDT;
168 params.keydir = optarg;
171 params.keydest = optarg;
177 params.imagename = optarg;
180 params.os = genimg_get_os_id(optarg);
185 params.require_keys = 1;
189 * This entry is for the second configuration
190 * file, if only one is not enough.
192 params.imagename2 = optarg;
198 params.type = genimg_get_type_id(optarg);
199 if (params.type < 0) {
208 printf("mkimage version %s\n", PLAIN_VERSION);
220 params.imagefile = argv[optind];
224 int main(int argc, char **argv)
230 struct image_type_params *tparams = NULL;
234 params.cmdname = *argv;
238 process_args(argc, argv);
240 /* set tparams as per input type_id */
241 tparams = imagetool_get_type(params.type);
242 if (tparams == NULL) {
243 fprintf (stderr, "%s: unsupported type %s\n",
244 params.cmdname, genimg_get_type_name(params.type));
249 * check the passed arguments parameters meets the requirements
250 * as per image type to be generated/listed
252 if (tparams->check_params)
253 if (tparams->check_params (¶ms))
257 params.ep = params.addr;
258 /* If XIP, entry point must be after the U-Boot header */
260 params.ep += tparams->header_size;
264 if (tparams->fflag_handle)
266 * in some cases, some additional processing needs
267 * to be done if fflag is defined
269 * For ex. fit_handle_file for Fit file support
271 retval = tparams->fflag_handle(¶ms);
273 if (retval != EXIT_SUCCESS)
277 if (params.lflag || params.fflag) {
278 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
280 ifd = open (params.imagefile,
281 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
285 fprintf (stderr, "%s: Can't open %s: %s\n",
286 params.cmdname, params.imagefile,
291 if (params.lflag || params.fflag) {
293 * list header information of existing image
295 if (fstat(ifd, &sbuf) < 0) {
296 fprintf (stderr, "%s: Can't stat %s: %s\n",
297 params.cmdname, params.imagefile,
302 if ((unsigned)sbuf.st_size < tparams->header_size) {
304 "%s: Bad size: \"%s\" is not valid image\n",
305 params.cmdname, params.imagefile);
309 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
310 if (ptr == MAP_FAILED) {
311 fprintf (stderr, "%s: Can't read %s: %s\n",
312 params.cmdname, params.imagefile,
318 * scan through mkimage registry for all supported image types
319 * and verify the input image file header for match
320 * Print the image information for matched image type
321 * Returns the error code if not matched
323 retval = imagetool_verify_print_header(ptr, &sbuf,
326 (void) munmap((void *)ptr, sbuf.st_size);
332 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
333 dfd = open(params.datafile, O_RDONLY | O_BINARY);
335 fprintf(stderr, "%s: Can't open %s: %s\n",
336 params.cmdname, params.datafile,
341 if (fstat(dfd, &sbuf) < 0) {
342 fprintf(stderr, "%s: Can't stat %s: %s\n",
343 params.cmdname, params.datafile,
348 params.file_size = sbuf.st_size + tparams->header_size;
353 * In case there an header with a variable
354 * length will be added, the corresponding
355 * function is called. This is responsible to
356 * allocate memory for the header itself.
358 if (tparams->vrec_header)
359 pad_len = tparams->vrec_header(¶ms, tparams);
361 memset(tparams->hdr, 0, tparams->header_size);
363 if (write(ifd, tparams->hdr, tparams->header_size)
364 != tparams->header_size) {
365 fprintf (stderr, "%s: Write error on %s: %s\n",
366 params.cmdname, params.imagefile, strerror(errno));
370 if (!params.skipcpy) {
371 if (params.type == IH_TYPE_MULTI ||
372 params.type == IH_TYPE_SCRIPT) {
373 char *file = params.datafile;
380 if ((sep = strchr(file, ':')) != NULL) {
384 if (stat (file, &sbuf) < 0) {
385 fprintf (stderr, "%s: Can't stat %s: %s\n",
386 params.cmdname, file, strerror(errno));
389 size = cpu_to_uimage (sbuf.st_size);
394 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
395 fprintf (stderr, "%s: Write error on %s: %s\n",
396 params.cmdname, params.imagefile,
413 file = params.datafile;
416 char *sep = strchr(file, ':');
419 copy_file (ifd, file, 1);
423 copy_file (ifd, file, 0);
427 } else if (params.type == IH_TYPE_PBLIMAGE) {
428 /* PBL has special Image format, implements its' own */
429 pbl_load_uboot(ifd, ¶ms);
431 copy_file(ifd, params.datafile, pad_len);
435 /* We're a bit of paranoid */
436 #if defined(_POSIX_SYNCHRONIZED_IO) && \
437 !defined(__sun__) && \
438 !defined(__FreeBSD__) && \
439 !defined(__OpenBSD__) && \
441 (void) fdatasync (ifd);
446 if (fstat(ifd, &sbuf) < 0) {
447 fprintf (stderr, "%s: Can't stat %s: %s\n",
448 params.cmdname, params.imagefile, strerror(errno));
451 params.file_size = sbuf.st_size;
453 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
454 if (ptr == MAP_FAILED) {
455 fprintf (stderr, "%s: Can't map %s: %s\n",
456 params.cmdname, params.imagefile, strerror(errno));
460 /* Setup the image header as per input image type*/
461 if (tparams->set_header)
462 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
464 fprintf (stderr, "%s: Can't set header for %s: %s\n",
465 params.cmdname, tparams->name, strerror(errno));
469 /* Print the image information by processing image header */
470 if (tparams->print_header)
471 tparams->print_header (ptr);
473 fprintf (stderr, "%s: Can't print header for %s: %s\n",
474 params.cmdname, tparams->name, strerror(errno));
478 (void) munmap((void *)ptr, sbuf.st_size);
480 /* We're a bit of paranoid */
481 #if defined(_POSIX_SYNCHRONIZED_IO) && \
482 !defined(__sun__) && \
483 !defined(__FreeBSD__) && \
484 !defined(__OpenBSD__) && \
486 (void) fdatasync (ifd);
492 fprintf (stderr, "%s: Write error on %s: %s\n",
493 params.cmdname, params.imagefile, strerror(errno));
501 copy_file (int ifd, const char *datafile, int pad)
511 struct image_type_params *tparams = imagetool_get_type(params.type);
513 memset(zeros, 0, sizeof(zeros));
516 fprintf (stderr, "Adding Image %s\n", datafile);
519 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
520 fprintf (stderr, "%s: Can't open %s: %s\n",
521 params.cmdname, datafile, strerror(errno));
525 if (fstat(dfd, &sbuf) < 0) {
526 fprintf (stderr, "%s: Can't stat %s: %s\n",
527 params.cmdname, datafile, strerror(errno));
531 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
532 if (ptr == MAP_FAILED) {
533 fprintf (stderr, "%s: Can't read %s: %s\n",
534 params.cmdname, datafile, strerror(errno));
539 unsigned char *p = NULL;
541 * XIP: do not append the image_header_t at the
542 * beginning of the file, but consume the space
546 if ((unsigned)sbuf.st_size < tparams->header_size) {
548 "%s: Bad size: \"%s\" is too small for XIP\n",
549 params.cmdname, datafile);
553 for (p = ptr; p < ptr + tparams->header_size; p++) {
556 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
557 params.cmdname, datafile);
562 offset = tparams->header_size;
565 size = sbuf.st_size - offset;
566 if (write(ifd, ptr + offset, size) != size) {
567 fprintf (stderr, "%s: Write error on %s: %s\n",
568 params.cmdname, params.imagefile, strerror(errno));
573 if ((pad == 1) && (tail != 0)) {
575 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
576 fprintf (stderr, "%s: Write error on %s: %s\n",
577 params.cmdname, params.imagefile,
581 } else if (pad > 1) {
583 int todo = sizeof(zeros);
587 if (write(ifd, (char *)&zeros, todo) != todo) {
588 fprintf(stderr, "%s: Write error on %s: %s\n",
589 params.cmdname, params.imagefile,
597 (void) munmap((void *)ptr, sbuf.st_size);