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] [-b <dtb_list>] fit-image\n"
89 " <dtb_list> is used with -f auto, and is a space-separated list of .dtb files\n",
92 " -D => set all options for device tree compiler\n"
93 " -f => input filename for FIT source\n");
94 #ifdef CONFIG_FIT_SIGNATURE
96 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
97 " -k => set directory containing private keys\n"
98 " -K => write public keys to this .dtb file\n"
99 " -c => add comment in signature node\n"
100 " -F => re-sign existing FIT image\n"
101 " -r => mark keys used as 'required' in dtb\n");
104 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
106 fprintf(stderr, " %s -V ==> print version information and exit\n",
108 fprintf(stderr, "Use -T to see a list of available image types\n");
113 static int add_content(int type, const char *fname)
115 struct content_info *cont;
117 cont = calloc(1, sizeof(*cont));
122 if (params.content_tail)
123 params.content_tail->next = cont;
125 params.content_head = cont;
126 params.content_tail = cont;
131 static void process_args(int argc, char **argv)
134 int type = IH_TYPE_INVALID;
135 char *datafile = NULL;
139 expecting = IH_TYPE_COUNT; /* Unknown */
140 while ((opt = getopt(argc, argv,
141 "-a:A:bcC:d:D:e:Ef:Fk:K:ln:O:rR:sT:vVx")) != -1) {
144 params.addr = strtoull(optarg, &ptr, 16);
146 fprintf(stderr, "%s: invalid load address %s\n",
147 params.cmdname, optarg);
152 params.arch = genimg_get_arch_id(optarg);
154 usage("Invalid architecture");
157 expecting = IH_TYPE_FLATDT;
160 params.comment = optarg;
163 params.comp = genimg_get_comp_id(optarg);
165 usage("Invalid compression type");
168 params.datafile = optarg;
175 params.ep = strtoull(optarg, &ptr, 16);
177 fprintf(stderr, "%s: invalid entry point %s\n",
178 params.cmdname, optarg);
184 params.external_data = true;
188 params.auto_its = !strcmp(datafile, "auto");
192 * The flattened image tree (FIT) format
193 * requires a flattened device tree image type
195 params.fit_image_type = params.type;
196 params.type = IH_TYPE_FLATDT;
200 params.keydir = optarg;
203 params.keydest = optarg;
209 params.imagename = optarg;
212 params.os = genimg_get_os_id(optarg);
214 usage("Invalid operating system");
217 params.require_keys = 1;
221 * This entry is for the second configuration
222 * file, if only one is not enough.
224 params.imagename2 = optarg;
230 type = genimg_get_type_id(optarg);
233 usage("Invalid image type");
241 printf("mkimage version %s\n", PLAIN_VERSION);
247 if (expecting == type || optind == argc) {
248 params.imagefile = optarg;
249 expecting = IH_TYPE_INVALID;
250 } else if (expecting == IH_TYPE_INVALID) {
252 "%s: Unknown content type: use -b before device tree files",
256 if (add_content(expecting, optarg)) {
258 "%s: Out of memory adding content '%s'",
259 params.cmdname, optarg);
265 usage("Invalid option");
270 * For auto-generated FIT images we need to know the image type to put
271 * in the FIT, which is separate from the file's image type (which
272 * will always be IH_TYPE_FLATDT in this case).
274 if (params.type == IH_TYPE_FLATDT) {
275 params.fit_image_type = type;
276 if (!params.auto_its)
277 params.datafile = datafile;
278 } else if (type != IH_TYPE_INVALID) {
282 if (!params.imagefile)
283 usage("Missing output filename");
287 int main(int argc, char **argv)
293 struct image_type_params *tparams = NULL;
297 params.cmdname = *argv;
301 process_args(argc, argv);
303 /* set tparams as per input type_id */
304 tparams = imagetool_get_type(params.type);
305 if (tparams == NULL) {
306 fprintf (stderr, "%s: unsupported type %s\n",
307 params.cmdname, genimg_get_type_name(params.type));
312 * check the passed arguments parameters meets the requirements
313 * as per image type to be generated/listed
315 if (tparams->check_params)
316 if (tparams->check_params (¶ms))
317 usage("Bad parameters for image type");
320 params.ep = params.addr;
321 /* If XIP, entry point must be after the U-Boot header */
323 params.ep += tparams->header_size;
327 if (tparams->fflag_handle)
329 * in some cases, some additional processing needs
330 * to be done if fflag is defined
332 * For ex. fit_handle_file for Fit file support
334 retval = tparams->fflag_handle(¶ms);
336 if (retval != EXIT_SUCCESS)
340 if (params.lflag || params.fflag) {
341 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
343 ifd = open (params.imagefile,
344 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
348 fprintf (stderr, "%s: Can't open %s: %s\n",
349 params.cmdname, params.imagefile,
354 if (params.lflag || params.fflag) {
356 * list header information of existing image
358 if (fstat(ifd, &sbuf) < 0) {
359 fprintf (stderr, "%s: Can't stat %s: %s\n",
360 params.cmdname, params.imagefile,
365 if ((unsigned)sbuf.st_size < tparams->header_size) {
367 "%s: Bad size: \"%s\" is not valid image\n",
368 params.cmdname, params.imagefile);
372 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
373 if (ptr == MAP_FAILED) {
374 fprintf (stderr, "%s: Can't read %s: %s\n",
375 params.cmdname, params.imagefile,
381 * scan through mkimage registry for all supported image types
382 * and verify the input image file header for match
383 * Print the image information for matched image type
384 * Returns the error code if not matched
386 retval = imagetool_verify_print_header(ptr, &sbuf,
389 (void) munmap((void *)ptr, sbuf.st_size);
395 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
396 dfd = open(params.datafile, O_RDONLY | O_BINARY);
398 fprintf(stderr, "%s: Can't open %s: %s\n",
399 params.cmdname, params.datafile,
404 if (fstat(dfd, &sbuf) < 0) {
405 fprintf(stderr, "%s: Can't stat %s: %s\n",
406 params.cmdname, params.datafile,
411 params.file_size = sbuf.st_size + tparams->header_size;
416 * In case there an header with a variable
417 * length will be added, the corresponding
418 * function is called. This is responsible to
419 * allocate memory for the header itself.
421 if (tparams->vrec_header)
422 pad_len = tparams->vrec_header(¶ms, tparams);
424 memset(tparams->hdr, 0, tparams->header_size);
426 if (write(ifd, tparams->hdr, tparams->header_size)
427 != tparams->header_size) {
428 fprintf (stderr, "%s: Write error on %s: %s\n",
429 params.cmdname, params.imagefile, strerror(errno));
433 if (!params.skipcpy) {
434 if (params.type == IH_TYPE_MULTI ||
435 params.type == IH_TYPE_SCRIPT) {
436 char *file = params.datafile;
443 if ((sep = strchr(file, ':')) != NULL) {
447 if (stat (file, &sbuf) < 0) {
448 fprintf (stderr, "%s: Can't stat %s: %s\n",
449 params.cmdname, file, strerror(errno));
452 size = cpu_to_uimage (sbuf.st_size);
457 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
458 fprintf (stderr, "%s: Write error on %s: %s\n",
459 params.cmdname, params.imagefile,
476 file = params.datafile;
479 char *sep = strchr(file, ':');
482 copy_file (ifd, file, 1);
486 copy_file (ifd, file, 0);
490 } else if (params.type == IH_TYPE_PBLIMAGE) {
491 /* PBL has special Image format, implements its' own */
492 pbl_load_uboot(ifd, ¶ms);
494 copy_file(ifd, params.datafile, pad_len);
498 /* We're a bit of paranoid */
499 #if defined(_POSIX_SYNCHRONIZED_IO) && \
500 !defined(__sun__) && \
501 !defined(__FreeBSD__) && \
502 !defined(__OpenBSD__) && \
504 (void) fdatasync (ifd);
509 if (fstat(ifd, &sbuf) < 0) {
510 fprintf (stderr, "%s: Can't stat %s: %s\n",
511 params.cmdname, params.imagefile, strerror(errno));
514 params.file_size = sbuf.st_size;
516 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
517 if (ptr == MAP_FAILED) {
518 fprintf (stderr, "%s: Can't map %s: %s\n",
519 params.cmdname, params.imagefile, strerror(errno));
523 /* Setup the image header as per input image type*/
524 if (tparams->set_header)
525 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
527 fprintf (stderr, "%s: Can't set header for %s: %s\n",
528 params.cmdname, tparams->name, strerror(errno));
532 /* Print the image information by processing image header */
533 if (tparams->print_header)
534 tparams->print_header (ptr);
536 fprintf (stderr, "%s: Can't print header for %s: %s\n",
537 params.cmdname, tparams->name, strerror(errno));
541 (void) munmap((void *)ptr, sbuf.st_size);
543 /* We're a bit of paranoid */
544 #if defined(_POSIX_SYNCHRONIZED_IO) && \
545 !defined(__sun__) && \
546 !defined(__FreeBSD__) && \
547 !defined(__OpenBSD__) && \
549 (void) fdatasync (ifd);
555 fprintf (stderr, "%s: Write error on %s: %s\n",
556 params.cmdname, params.imagefile, strerror(errno));
564 copy_file (int ifd, const char *datafile, int pad)
574 struct image_type_params *tparams = imagetool_get_type(params.type);
576 memset(zeros, 0, sizeof(zeros));
579 fprintf (stderr, "Adding Image %s\n", datafile);
582 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
583 fprintf (stderr, "%s: Can't open %s: %s\n",
584 params.cmdname, datafile, strerror(errno));
588 if (fstat(dfd, &sbuf) < 0) {
589 fprintf (stderr, "%s: Can't stat %s: %s\n",
590 params.cmdname, datafile, strerror(errno));
594 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
595 if (ptr == MAP_FAILED) {
596 fprintf (stderr, "%s: Can't read %s: %s\n",
597 params.cmdname, datafile, strerror(errno));
602 unsigned char *p = NULL;
604 * XIP: do not append the image_header_t at the
605 * beginning of the file, but consume the space
609 if ((unsigned)sbuf.st_size < tparams->header_size) {
611 "%s: Bad size: \"%s\" is too small for XIP\n",
612 params.cmdname, datafile);
616 for (p = ptr; p < ptr + tparams->header_size; p++) {
619 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
620 params.cmdname, datafile);
625 offset = tparams->header_size;
628 size = sbuf.st_size - offset;
629 if (write(ifd, ptr + offset, size) != size) {
630 fprintf (stderr, "%s: Write error on %s: %s\n",
631 params.cmdname, params.imagefile, strerror(errno));
636 if ((pad == 1) && (tail != 0)) {
638 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
639 fprintf (stderr, "%s: Write error on %s: %s\n",
640 params.cmdname, params.imagefile,
644 } else if (pad > 1) {
646 int todo = sizeof(zeros);
650 if (write(ifd, (char *)&zeros, todo) != todo) {
651 fprintf(stderr, "%s: Write error on %s: %s\n",
652 params.cmdname, params.imagefile,
660 (void) munmap((void *)ptr, sbuf.st_size);