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");
71 static void usage(const char *msg)
73 fprintf(stderr, "Error: %s\n", msg);
74 fprintf(stderr, "Usage: %s -l image\n"
75 " -l ==> list image header information\n",
78 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
79 " -A ==> set architecture to 'arch'\n"
80 " -O ==> set operating system to 'os'\n"
81 " -T ==> set image type to 'type'\n"
82 " -C ==> set compression type 'comp'\n"
83 " -a ==> set load address to 'addr' (hex)\n"
84 " -e ==> set entry point to 'ep' (hex)\n"
85 " -n ==> set image name to 'name'\n"
86 " -d ==> use image data from 'datafile'\n"
87 " -x ==> set XIP (execute in place)\n",
90 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] fit-image\n"
91 " <dtb> file is used with -f auto, it may occour multiple times.\n",
94 " -D => set all options for device tree compiler\n"
95 " -f => input filename for FIT source\n");
96 #ifdef CONFIG_FIT_SIGNATURE
98 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
99 " -E => place data outside of the FIT structure\n"
100 " -k => set directory containing private keys\n"
101 " -K => write public keys to this .dtb file\n"
102 " -c => add comment in signature node\n"
103 " -F => re-sign existing FIT image\n"
104 " -p => place external data at a static position\n"
105 " -r => mark keys used as 'required' in dtb\n");
108 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
110 fprintf(stderr, " %s -V ==> print version information and exit\n",
112 fprintf(stderr, "Use -T to see a list of available image types\n");
117 static int add_content(int type, const char *fname)
119 struct content_info *cont;
121 cont = calloc(1, sizeof(*cont));
126 if (params.content_tail)
127 params.content_tail->next = cont;
129 params.content_head = cont;
130 params.content_tail = cont;
135 static void process_args(int argc, char **argv)
138 int type = IH_TYPE_INVALID;
139 char *datafile = NULL;
142 while ((opt = getopt(argc, argv,
143 "a:A:b:cC:d:D:e:Ef:Fk:K:ln:p:O:rR:qsT:vVx")) != -1) {
146 params.addr = strtoull(optarg, &ptr, 16);
148 fprintf(stderr, "%s: invalid load address %s\n",
149 params.cmdname, optarg);
154 params.arch = genimg_get_arch_id(optarg);
155 if (params.arch < 0) {
156 show_valid_options(IH_ARCH);
157 usage("Invalid architecture");
161 if (add_content(IH_TYPE_FLATDT, optarg)) {
163 "%s: Out of memory adding content '%s'",
164 params.cmdname, optarg);
169 params.comment = optarg;
172 params.comp = genimg_get_comp_id(optarg);
173 if (params.comp < 0) {
174 show_valid_options(IH_COMP);
175 usage("Invalid compression type");
179 params.datafile = optarg;
186 params.ep = strtoull(optarg, &ptr, 16);
188 fprintf(stderr, "%s: invalid entry point %s\n",
189 params.cmdname, optarg);
195 params.external_data = true;
199 params.auto_its = !strcmp(datafile, "auto");
203 * The flattened image tree (FIT) format
204 * requires a flattened device tree image type
206 params.type = IH_TYPE_FLATDT;
210 params.keydir = optarg;
213 params.keydest = optarg;
219 params.imagename = optarg;
222 params.os = genimg_get_os_id(optarg);
224 show_valid_options(IH_OS);
225 usage("Invalid operating system");
229 params.external_offset = strtoull(optarg, &ptr, 16);
231 fprintf(stderr, "%s: invalid offset size %s\n",
232 params.cmdname, optarg);
239 params.require_keys = 1;
243 * This entry is for the second configuration
244 * file, if only one is not enough.
246 params.imagename2 = optarg;
252 type = genimg_get_type_id(optarg);
254 show_valid_options(IH_TYPE);
255 usage("Invalid image type");
262 printf("mkimage version %s\n", PLAIN_VERSION);
268 usage("Invalid option");
272 /* The last parameter is expected to be the imagefile */
274 params.imagefile = argv[optind];
277 * For auto-generated FIT images we need to know the image type to put
278 * in the FIT, which is separate from the file's image type (which
279 * will always be IH_TYPE_FLATDT in this case).
281 if (params.type == IH_TYPE_FLATDT) {
282 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
283 /* For auto_its, datafile is always 'auto' */
284 if (!params.auto_its)
285 params.datafile = datafile;
286 else if (!params.datafile)
287 usage("Missing data file for auto-FIT (use -d)");
288 } else if (type != IH_TYPE_INVALID) {
292 if (!params.imagefile)
293 usage("Missing output filename");
296 int main(int argc, char **argv)
302 struct image_type_params *tparams = NULL;
306 params.cmdname = *argv;
310 process_args(argc, argv);
312 /* set tparams as per input type_id */
313 tparams = imagetool_get_type(params.type);
314 if (tparams == NULL) {
315 fprintf (stderr, "%s: unsupported type %s\n",
316 params.cmdname, genimg_get_type_name(params.type));
321 * check the passed arguments parameters meets the requirements
322 * as per image type to be generated/listed
324 if (tparams->check_params)
325 if (tparams->check_params (¶ms))
326 usage("Bad parameters for image type");
329 params.ep = params.addr;
330 /* If XIP, entry point must be after the U-Boot header */
332 params.ep += tparams->header_size;
336 if (tparams->fflag_handle)
338 * in some cases, some additional processing needs
339 * to be done if fflag is defined
341 * For ex. fit_handle_file for Fit file support
343 retval = tparams->fflag_handle(¶ms);
345 if (retval != EXIT_SUCCESS)
349 if (params.lflag || params.fflag) {
350 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
352 ifd = open (params.imagefile,
353 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
357 fprintf (stderr, "%s: Can't open %s: %s\n",
358 params.cmdname, params.imagefile,
363 if (params.lflag || params.fflag) {
365 * list header information of existing image
367 if (fstat(ifd, &sbuf) < 0) {
368 fprintf (stderr, "%s: Can't stat %s: %s\n",
369 params.cmdname, params.imagefile,
374 if ((unsigned)sbuf.st_size < tparams->header_size) {
376 "%s: Bad size: \"%s\" is not valid image\n",
377 params.cmdname, params.imagefile);
381 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
382 if (ptr == MAP_FAILED) {
383 fprintf (stderr, "%s: Can't read %s: %s\n",
384 params.cmdname, params.imagefile,
390 * scan through mkimage registry for all supported image types
391 * and verify the input image file header for match
392 * Print the image information for matched image type
393 * Returns the error code if not matched
395 retval = imagetool_verify_print_header(ptr, &sbuf,
398 (void) munmap((void *)ptr, sbuf.st_size);
404 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
405 dfd = open(params.datafile, O_RDONLY | O_BINARY);
407 fprintf(stderr, "%s: Can't open %s: %s\n",
408 params.cmdname, params.datafile,
413 if (fstat(dfd, &sbuf) < 0) {
414 fprintf(stderr, "%s: Can't stat %s: %s\n",
415 params.cmdname, params.datafile,
420 params.file_size = sbuf.st_size + tparams->header_size;
425 * In case there an header with a variable
426 * length will be added, the corresponding
427 * function is called. This is responsible to
428 * allocate memory for the header itself.
430 if (tparams->vrec_header)
431 pad_len = tparams->vrec_header(¶ms, tparams);
433 memset(tparams->hdr, 0, tparams->header_size);
435 if (write(ifd, tparams->hdr, tparams->header_size)
436 != tparams->header_size) {
437 fprintf (stderr, "%s: Write error on %s: %s\n",
438 params.cmdname, params.imagefile, strerror(errno));
442 if (!params.skipcpy) {
443 if (params.type == IH_TYPE_MULTI ||
444 params.type == IH_TYPE_SCRIPT) {
445 char *file = params.datafile;
452 if ((sep = strchr(file, ':')) != NULL) {
456 if (stat (file, &sbuf) < 0) {
457 fprintf (stderr, "%s: Can't stat %s: %s\n",
458 params.cmdname, file, strerror(errno));
461 size = cpu_to_uimage (sbuf.st_size);
466 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
467 fprintf (stderr, "%s: Write error on %s: %s\n",
468 params.cmdname, params.imagefile,
485 file = params.datafile;
488 char *sep = strchr(file, ':');
491 copy_file (ifd, file, 1);
495 copy_file (ifd, file, 0);
499 } else if (params.type == IH_TYPE_PBLIMAGE) {
500 /* PBL has special Image format, implements its' own */
501 pbl_load_uboot(ifd, ¶ms);
503 copy_file(ifd, params.datafile, pad_len);
507 /* We're a bit of paranoid */
508 #if defined(_POSIX_SYNCHRONIZED_IO) && \
509 !defined(__sun__) && \
510 !defined(__FreeBSD__) && \
511 !defined(__OpenBSD__) && \
513 (void) fdatasync (ifd);
518 if (fstat(ifd, &sbuf) < 0) {
519 fprintf (stderr, "%s: Can't stat %s: %s\n",
520 params.cmdname, params.imagefile, strerror(errno));
523 params.file_size = sbuf.st_size;
525 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
526 if (ptr == MAP_FAILED) {
527 fprintf (stderr, "%s: Can't map %s: %s\n",
528 params.cmdname, params.imagefile, strerror(errno));
532 /* Setup the image header as per input image type*/
533 if (tparams->set_header)
534 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
536 fprintf (stderr, "%s: Can't set header for %s: %s\n",
537 params.cmdname, tparams->name, strerror(errno));
541 /* Print the image information by processing image header */
542 if (tparams->print_header)
543 tparams->print_header (ptr);
545 fprintf (stderr, "%s: Can't print header for %s: %s\n",
546 params.cmdname, tparams->name, strerror(errno));
550 (void) munmap((void *)ptr, sbuf.st_size);
552 /* We're a bit of paranoid */
553 #if defined(_POSIX_SYNCHRONIZED_IO) && \
554 !defined(__sun__) && \
555 !defined(__FreeBSD__) && \
556 !defined(__OpenBSD__) && \
558 (void) fdatasync (ifd);
564 fprintf (stderr, "%s: Write error on %s: %s\n",
565 params.cmdname, params.imagefile, strerror(errno));
573 copy_file (int ifd, const char *datafile, int pad)
583 struct image_type_params *tparams = imagetool_get_type(params.type);
585 memset(zeros, 0, sizeof(zeros));
588 fprintf (stderr, "Adding Image %s\n", datafile);
591 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
592 fprintf (stderr, "%s: Can't open %s: %s\n",
593 params.cmdname, datafile, strerror(errno));
597 if (fstat(dfd, &sbuf) < 0) {
598 fprintf (stderr, "%s: Can't stat %s: %s\n",
599 params.cmdname, datafile, strerror(errno));
603 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
604 if (ptr == MAP_FAILED) {
605 fprintf (stderr, "%s: Can't read %s: %s\n",
606 params.cmdname, datafile, strerror(errno));
611 unsigned char *p = NULL;
613 * XIP: do not append the image_header_t at the
614 * beginning of the file, but consume the space
618 if ((unsigned)sbuf.st_size < tparams->header_size) {
620 "%s: Bad size: \"%s\" is too small for XIP\n",
621 params.cmdname, datafile);
625 for (p = ptr; p < ptr + tparams->header_size; p++) {
628 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
629 params.cmdname, datafile);
634 offset = tparams->header_size;
637 size = sbuf.st_size - offset;
638 if (write(ifd, ptr + offset, size) != size) {
639 fprintf (stderr, "%s: Write error on %s: %s\n",
640 params.cmdname, params.imagefile, strerror(errno));
645 if ((pad == 1) && (tail != 0)) {
647 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
648 fprintf (stderr, "%s: Write error on %s: %s\n",
649 params.cmdname, params.imagefile,
653 } else if (pad > 1) {
655 int todo = sizeof(zeros);
659 if (write(ifd, (char *)&zeros, todo) != todo) {
660 fprintf(stderr, "%s: Write error on %s: %s\n",
661 params.cmdname, params.imagefile,
669 (void) munmap((void *)ptr, sbuf.st_size);