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 static 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");
72 static void usage(const char *msg)
74 fprintf(stderr, "Error: %s\n", msg);
75 fprintf(stderr, "Usage: %s -l image\n"
76 " -l ==> list image header information\n",
79 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
80 " -A ==> set architecture to 'arch'\n"
81 " -O ==> set operating system to 'os'\n"
82 " -T ==> set image type to 'type'\n"
83 " -C ==> set compression type 'comp'\n"
84 " -a ==> set load address to 'addr' (hex)\n"
85 " -e ==> set entry point to 'ep' (hex)\n"
86 " -n ==> set image name to 'name'\n"
87 " -d ==> use image data from 'datafile'\n"
88 " -x ==> set XIP (execute in place)\n",
91 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n"
92 " <dtb> file is used with -f auto, it may occur multiple times.\n",
95 " -D => set all options for device tree compiler\n"
96 " -f => input filename for FIT source\n"
97 " -i => input filename for ramdisk file\n");
98 #ifdef CONFIG_FIT_SIGNATURE
100 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
101 " -E => place data outside of the FIT structure\n"
102 " -k => set directory containing private keys\n"
103 " -K => write public keys to this .dtb file\n"
104 " -c => add comment in signature node\n"
105 " -F => re-sign existing FIT image\n"
106 " -p => place external data at a static position\n"
107 " -r => mark keys used as 'required' in dtb\n");
110 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
112 fprintf(stderr, " %s -V ==> print version information and exit\n",
114 fprintf(stderr, "Use -T to see a list of available image types\n");
119 static int add_content(int type, const char *fname)
121 struct content_info *cont;
123 cont = calloc(1, sizeof(*cont));
128 if (params.content_tail)
129 params.content_tail->next = cont;
131 params.content_head = cont;
132 params.content_tail = cont;
137 static void process_args(int argc, char **argv)
140 int type = IH_TYPE_INVALID;
141 char *datafile = NULL;
144 while ((opt = getopt(argc, argv,
145 "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:p:O:rR:qsT:vVx")) != -1) {
148 params.addr = strtoull(optarg, &ptr, 16);
150 fprintf(stderr, "%s: invalid load address %s\n",
151 params.cmdname, optarg);
156 params.arch = genimg_get_arch_id(optarg);
157 if (params.arch < 0) {
158 show_valid_options(IH_ARCH);
159 usage("Invalid architecture");
163 if (add_content(IH_TYPE_FLATDT, optarg)) {
165 "%s: Out of memory adding content '%s'",
166 params.cmdname, optarg);
171 params.comment = optarg;
174 params.comp = genimg_get_comp_id(optarg);
175 if (params.comp < 0) {
176 show_valid_options(IH_COMP);
177 usage("Invalid compression type");
181 params.datafile = optarg;
188 params.ep = strtoull(optarg, &ptr, 16);
190 fprintf(stderr, "%s: invalid entry point %s\n",
191 params.cmdname, optarg);
197 params.external_data = true;
201 params.auto_its = !strcmp(datafile, "auto");
205 * The flattened image tree (FIT) format
206 * requires a flattened device tree image type
208 params.type = IH_TYPE_FLATDT;
212 params.fit_ramdisk = optarg;
215 params.keydir = optarg;
218 params.keydest = optarg;
224 params.imagename = optarg;
227 params.os = genimg_get_os_id(optarg);
229 show_valid_options(IH_OS);
230 usage("Invalid operating system");
234 params.external_offset = strtoull(optarg, &ptr, 16);
236 fprintf(stderr, "%s: invalid offset size %s\n",
237 params.cmdname, optarg);
245 params.require_keys = 1;
249 * This entry is for the second configuration
250 * file, if only one is not enough.
252 params.imagename2 = optarg;
258 type = genimg_get_type_id(optarg);
260 show_valid_options(IH_TYPE);
261 usage("Invalid image type");
268 printf("mkimage version %s\n", PLAIN_VERSION);
274 usage("Invalid option");
278 /* The last parameter is expected to be the imagefile */
280 params.imagefile = argv[optind];
283 * For auto-generated FIT images we need to know the image type to put
284 * in the FIT, which is separate from the file's image type (which
285 * will always be IH_TYPE_FLATDT in this case).
287 if (params.type == IH_TYPE_FLATDT) {
288 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
289 /* For auto_its, datafile is always 'auto' */
290 if (!params.auto_its)
291 params.datafile = datafile;
292 else if (!params.datafile)
293 usage("Missing data file for auto-FIT (use -d)");
294 } else if (type != IH_TYPE_INVALID) {
298 if (!params.imagefile)
299 usage("Missing output filename");
302 int main(int argc, char **argv)
308 struct image_type_params *tparams = NULL;
312 params.cmdname = *argv;
316 process_args(argc, argv);
318 /* set tparams as per input type_id */
319 tparams = imagetool_get_type(params.type);
320 if (tparams == NULL) {
321 fprintf (stderr, "%s: unsupported type %s\n",
322 params.cmdname, genimg_get_type_name(params.type));
327 * check the passed arguments parameters meets the requirements
328 * as per image type to be generated/listed
330 if (tparams->check_params)
331 if (tparams->check_params (¶ms))
332 usage("Bad parameters for image type");
335 params.ep = params.addr;
336 /* If XIP, entry point must be after the U-Boot header */
338 params.ep += tparams->header_size;
342 if (tparams->fflag_handle)
344 * in some cases, some additional processing needs
345 * to be done if fflag is defined
347 * For ex. fit_handle_file for Fit file support
349 retval = tparams->fflag_handle(¶ms);
351 if (retval != EXIT_SUCCESS)
355 if (params.lflag || params.fflag) {
356 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
358 ifd = open (params.imagefile,
359 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
363 fprintf (stderr, "%s: Can't open %s: %s\n",
364 params.cmdname, params.imagefile,
369 if (params.lflag || params.fflag) {
371 * list header information of existing image
373 if (fstat(ifd, &sbuf) < 0) {
374 fprintf (stderr, "%s: Can't stat %s: %s\n",
375 params.cmdname, params.imagefile,
380 if ((unsigned)sbuf.st_size < tparams->header_size) {
382 "%s: Bad size: \"%s\" is not valid image\n",
383 params.cmdname, params.imagefile);
387 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
388 if (ptr == MAP_FAILED) {
389 fprintf (stderr, "%s: Can't read %s: %s\n",
390 params.cmdname, params.imagefile,
396 * scan through mkimage registry for all supported image types
397 * and verify the input image file header for match
398 * Print the image information for matched image type
399 * Returns the error code if not matched
401 retval = imagetool_verify_print_header(ptr, &sbuf,
404 (void) munmap((void *)ptr, sbuf.st_size);
410 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
411 dfd = open(params.datafile, O_RDONLY | O_BINARY);
413 fprintf(stderr, "%s: Can't open %s: %s\n",
414 params.cmdname, params.datafile,
419 if (fstat(dfd, &sbuf) < 0) {
420 fprintf(stderr, "%s: Can't stat %s: %s\n",
421 params.cmdname, params.datafile,
426 params.file_size = sbuf.st_size + tparams->header_size;
431 * In case there an header with a variable
432 * length will be added, the corresponding
433 * function is called. This is responsible to
434 * allocate memory for the header itself.
436 if (tparams->vrec_header)
437 pad_len = tparams->vrec_header(¶ms, tparams);
439 memset(tparams->hdr, 0, tparams->header_size);
441 if (write(ifd, tparams->hdr, tparams->header_size)
442 != tparams->header_size) {
443 fprintf (stderr, "%s: Write error on %s: %s\n",
444 params.cmdname, params.imagefile, strerror(errno));
448 if (!params.skipcpy) {
449 if (params.type == IH_TYPE_MULTI ||
450 params.type == IH_TYPE_SCRIPT) {
451 char *file = params.datafile;
458 if ((sep = strchr(file, ':')) != NULL) {
462 if (stat (file, &sbuf) < 0) {
463 fprintf (stderr, "%s: Can't stat %s: %s\n",
464 params.cmdname, file, strerror(errno));
467 size = cpu_to_uimage (sbuf.st_size);
472 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
473 fprintf (stderr, "%s: Write error on %s: %s\n",
474 params.cmdname, params.imagefile,
491 file = params.datafile;
494 char *sep = strchr(file, ':');
497 copy_file (ifd, file, 1);
501 copy_file (ifd, file, 0);
505 } else if (params.type == IH_TYPE_PBLIMAGE) {
506 /* PBL has special Image format, implements its' own */
507 pbl_load_uboot(ifd, ¶ms);
509 copy_file(ifd, params.datafile, pad_len);
513 /* We're a bit of paranoid */
514 #if defined(_POSIX_SYNCHRONIZED_IO) && \
515 !defined(__sun__) && \
516 !defined(__FreeBSD__) && \
517 !defined(__OpenBSD__) && \
519 (void) fdatasync (ifd);
524 if (fstat(ifd, &sbuf) < 0) {
525 fprintf (stderr, "%s: Can't stat %s: %s\n",
526 params.cmdname, params.imagefile, strerror(errno));
529 params.file_size = sbuf.st_size;
531 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
532 if (ptr == MAP_FAILED) {
533 fprintf (stderr, "%s: Can't map %s: %s\n",
534 params.cmdname, params.imagefile, strerror(errno));
538 /* Setup the image header as per input image type*/
539 if (tparams->set_header)
540 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
542 fprintf (stderr, "%s: Can't set header for %s: %s\n",
543 params.cmdname, tparams->name, strerror(errno));
547 /* Print the image information by processing image header */
548 if (tparams->print_header)
549 tparams->print_header (ptr);
551 fprintf (stderr, "%s: Can't print header for %s: %s\n",
552 params.cmdname, tparams->name, strerror(errno));
556 (void) munmap((void *)ptr, sbuf.st_size);
558 /* We're a bit of paranoid */
559 #if defined(_POSIX_SYNCHRONIZED_IO) && \
560 !defined(__sun__) && \
561 !defined(__FreeBSD__) && \
562 !defined(__OpenBSD__) && \
564 (void) fdatasync (ifd);
570 fprintf (stderr, "%s: Write error on %s: %s\n",
571 params.cmdname, params.imagefile, strerror(errno));
579 copy_file (int ifd, const char *datafile, int pad)
589 struct image_type_params *tparams = imagetool_get_type(params.type);
591 memset(zeros, 0, sizeof(zeros));
594 fprintf (stderr, "Adding Image %s\n", datafile);
597 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
598 fprintf (stderr, "%s: Can't open %s: %s\n",
599 params.cmdname, datafile, strerror(errno));
603 if (fstat(dfd, &sbuf) < 0) {
604 fprintf (stderr, "%s: Can't stat %s: %s\n",
605 params.cmdname, datafile, strerror(errno));
609 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
610 if (ptr == MAP_FAILED) {
611 fprintf (stderr, "%s: Can't read %s: %s\n",
612 params.cmdname, datafile, strerror(errno));
617 unsigned char *p = NULL;
619 * XIP: do not append the image_header_t at the
620 * beginning of the file, but consume the space
624 if ((unsigned)sbuf.st_size < tparams->header_size) {
626 "%s: Bad size: \"%s\" is too small for XIP\n",
627 params.cmdname, datafile);
631 for (p = ptr; p < ptr + tparams->header_size; p++) {
634 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
635 params.cmdname, datafile);
640 offset = tparams->header_size;
643 size = sbuf.st_size - offset;
644 if (write(ifd, ptr + offset, size) != size) {
645 fprintf (stderr, "%s: Write error on %s: %s\n",
646 params.cmdname, params.imagefile, strerror(errno));
651 if ((pad == 1) && (tail != 0)) {
653 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
654 fprintf (stderr, "%s: Write error on %s: %s\n",
655 params.cmdname, params.imagefile,
659 } else if (pad > 1) {
661 int todo = sizeof(zeros);
665 if (write(ifd, (char *)&zeros, todo) != todo) {
666 fprintf(stderr, "%s: Write error on %s: %s\n",
667 params.cmdname, params.imagefile,
675 (void) munmap((void *)ptr, sbuf.st_size);