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> [-b <dtb>]] fit-image\n"
89 " <dtb> file is used with -f auto, it may occour multiple times.\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;
138 while ((opt = getopt(argc, argv,
139 "a:A:b:cC:d:D:e:Ef:Fk:K:ln:O:rR:qsT:vVx")) != -1) {
142 params.addr = strtoull(optarg, &ptr, 16);
144 fprintf(stderr, "%s: invalid load address %s\n",
145 params.cmdname, optarg);
150 params.arch = genimg_get_arch_id(optarg);
152 usage("Invalid architecture");
155 if (add_content(IH_TYPE_FLATDT, optarg)) {
157 "%s: Out of memory adding content '%s'",
158 params.cmdname, optarg);
163 params.comment = optarg;
166 params.comp = genimg_get_comp_id(optarg);
168 usage("Invalid compression type");
171 params.datafile = optarg;
178 params.ep = strtoull(optarg, &ptr, 16);
180 fprintf(stderr, "%s: invalid entry point %s\n",
181 params.cmdname, optarg);
187 params.external_data = true;
191 params.auto_its = !strcmp(datafile, "auto");
195 * The flattened image tree (FIT) format
196 * requires a flattened device tree image type
198 params.fit_image_type = params.type;
199 params.type = IH_TYPE_FLATDT;
203 params.keydir = optarg;
206 params.keydest = optarg;
212 params.imagename = optarg;
215 params.os = genimg_get_os_id(optarg);
217 usage("Invalid operating system");
223 params.require_keys = 1;
227 * This entry is for the second configuration
228 * file, if only one is not enough.
230 params.imagename2 = optarg;
236 type = genimg_get_type_id(optarg);
239 usage("Invalid image type");
246 printf("mkimage version %s\n", PLAIN_VERSION);
252 usage("Invalid option");
256 /* The last parameter is expected to be the imagefile */
258 params.imagefile = argv[optind];
261 * For auto-generated FIT images we need to know the image type to put
262 * in the FIT, which is separate from the file's image type (which
263 * will always be IH_TYPE_FLATDT in this case).
265 if (params.type == IH_TYPE_FLATDT) {
266 params.fit_image_type = type;
267 if (!params.auto_its)
268 params.datafile = datafile;
269 } else if (type != IH_TYPE_INVALID) {
273 if (!params.imagefile)
274 usage("Missing output filename");
278 int main(int argc, char **argv)
284 struct image_type_params *tparams = NULL;
288 params.cmdname = *argv;
292 process_args(argc, argv);
294 /* set tparams as per input type_id */
295 tparams = imagetool_get_type(params.type);
296 if (tparams == NULL) {
297 fprintf (stderr, "%s: unsupported type %s\n",
298 params.cmdname, genimg_get_type_name(params.type));
303 * check the passed arguments parameters meets the requirements
304 * as per image type to be generated/listed
306 if (tparams->check_params)
307 if (tparams->check_params (¶ms))
308 usage("Bad parameters for image type");
311 params.ep = params.addr;
312 /* If XIP, entry point must be after the U-Boot header */
314 params.ep += tparams->header_size;
318 if (tparams->fflag_handle)
320 * in some cases, some additional processing needs
321 * to be done if fflag is defined
323 * For ex. fit_handle_file for Fit file support
325 retval = tparams->fflag_handle(¶ms);
327 if (retval != EXIT_SUCCESS)
331 if (params.lflag || params.fflag) {
332 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
334 ifd = open (params.imagefile,
335 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
339 fprintf (stderr, "%s: Can't open %s: %s\n",
340 params.cmdname, params.imagefile,
345 if (params.lflag || params.fflag) {
347 * list header information of existing image
349 if (fstat(ifd, &sbuf) < 0) {
350 fprintf (stderr, "%s: Can't stat %s: %s\n",
351 params.cmdname, params.imagefile,
356 if ((unsigned)sbuf.st_size < tparams->header_size) {
358 "%s: Bad size: \"%s\" is not valid image\n",
359 params.cmdname, params.imagefile);
363 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
364 if (ptr == MAP_FAILED) {
365 fprintf (stderr, "%s: Can't read %s: %s\n",
366 params.cmdname, params.imagefile,
372 * scan through mkimage registry for all supported image types
373 * and verify the input image file header for match
374 * Print the image information for matched image type
375 * Returns the error code if not matched
377 retval = imagetool_verify_print_header(ptr, &sbuf,
380 (void) munmap((void *)ptr, sbuf.st_size);
386 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
387 dfd = open(params.datafile, O_RDONLY | O_BINARY);
389 fprintf(stderr, "%s: Can't open %s: %s\n",
390 params.cmdname, params.datafile,
395 if (fstat(dfd, &sbuf) < 0) {
396 fprintf(stderr, "%s: Can't stat %s: %s\n",
397 params.cmdname, params.datafile,
402 params.file_size = sbuf.st_size + tparams->header_size;
407 * In case there an header with a variable
408 * length will be added, the corresponding
409 * function is called. This is responsible to
410 * allocate memory for the header itself.
412 if (tparams->vrec_header)
413 pad_len = tparams->vrec_header(¶ms, tparams);
415 memset(tparams->hdr, 0, tparams->header_size);
417 if (write(ifd, tparams->hdr, tparams->header_size)
418 != tparams->header_size) {
419 fprintf (stderr, "%s: Write error on %s: %s\n",
420 params.cmdname, params.imagefile, strerror(errno));
424 if (!params.skipcpy) {
425 if (params.type == IH_TYPE_MULTI ||
426 params.type == IH_TYPE_SCRIPT) {
427 char *file = params.datafile;
434 if ((sep = strchr(file, ':')) != NULL) {
438 if (stat (file, &sbuf) < 0) {
439 fprintf (stderr, "%s: Can't stat %s: %s\n",
440 params.cmdname, file, strerror(errno));
443 size = cpu_to_uimage (sbuf.st_size);
448 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
449 fprintf (stderr, "%s: Write error on %s: %s\n",
450 params.cmdname, params.imagefile,
467 file = params.datafile;
470 char *sep = strchr(file, ':');
473 copy_file (ifd, file, 1);
477 copy_file (ifd, file, 0);
481 } else if (params.type == IH_TYPE_PBLIMAGE) {
482 /* PBL has special Image format, implements its' own */
483 pbl_load_uboot(ifd, ¶ms);
485 copy_file(ifd, params.datafile, pad_len);
489 /* We're a bit of paranoid */
490 #if defined(_POSIX_SYNCHRONIZED_IO) && \
491 !defined(__sun__) && \
492 !defined(__FreeBSD__) && \
493 !defined(__OpenBSD__) && \
495 (void) fdatasync (ifd);
500 if (fstat(ifd, &sbuf) < 0) {
501 fprintf (stderr, "%s: Can't stat %s: %s\n",
502 params.cmdname, params.imagefile, strerror(errno));
505 params.file_size = sbuf.st_size;
507 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
508 if (ptr == MAP_FAILED) {
509 fprintf (stderr, "%s: Can't map %s: %s\n",
510 params.cmdname, params.imagefile, strerror(errno));
514 /* Setup the image header as per input image type*/
515 if (tparams->set_header)
516 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
518 fprintf (stderr, "%s: Can't set header for %s: %s\n",
519 params.cmdname, tparams->name, strerror(errno));
523 /* Print the image information by processing image header */
524 if (tparams->print_header)
525 tparams->print_header (ptr);
527 fprintf (stderr, "%s: Can't print header for %s: %s\n",
528 params.cmdname, tparams->name, strerror(errno));
532 (void) munmap((void *)ptr, sbuf.st_size);
534 /* We're a bit of paranoid */
535 #if defined(_POSIX_SYNCHRONIZED_IO) && \
536 !defined(__sun__) && \
537 !defined(__FreeBSD__) && \
538 !defined(__OpenBSD__) && \
540 (void) fdatasync (ifd);
546 fprintf (stderr, "%s: Write error on %s: %s\n",
547 params.cmdname, params.imagefile, strerror(errno));
555 copy_file (int ifd, const char *datafile, int pad)
565 struct image_type_params *tparams = imagetool_get_type(params.type);
567 memset(zeros, 0, sizeof(zeros));
570 fprintf (stderr, "Adding Image %s\n", datafile);
573 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
574 fprintf (stderr, "%s: Can't open %s: %s\n",
575 params.cmdname, datafile, strerror(errno));
579 if (fstat(dfd, &sbuf) < 0) {
580 fprintf (stderr, "%s: Can't stat %s: %s\n",
581 params.cmdname, datafile, strerror(errno));
585 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
586 if (ptr == MAP_FAILED) {
587 fprintf (stderr, "%s: Can't read %s: %s\n",
588 params.cmdname, datafile, strerror(errno));
593 unsigned char *p = NULL;
595 * XIP: do not append the image_header_t at the
596 * beginning of the file, but consume the space
600 if ((unsigned)sbuf.st_size < tparams->header_size) {
602 "%s: Bad size: \"%s\" is too small for XIP\n",
603 params.cmdname, datafile);
607 for (p = ptr; p < ptr + tparams->header_size; p++) {
610 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
611 params.cmdname, datafile);
616 offset = tparams->header_size;
619 size = sbuf.st_size - offset;
620 if (write(ifd, ptr + offset, size) != size) {
621 fprintf (stderr, "%s: Write error on %s: %s\n",
622 params.cmdname, params.imagefile, strerror(errno));
627 if ((pad == 1) && (tail != 0)) {
629 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
630 fprintf (stderr, "%s: Write error on %s: %s\n",
631 params.cmdname, params.imagefile,
635 } else if (pad > 1) {
637 int todo = sizeof(zeros);
641 if (write(ifd, (char *)&zeros, todo) != todo) {
642 fprintf(stderr, "%s: Write error on %s: %s\n",
643 params.cmdname, params.imagefile,
651 (void) munmap((void *)ptr, sbuf.st_size);