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>]] 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 #ifdef CONFIG_FIT_SIGNATURE
99 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
100 " -E => place data outside of the FIT structure\n"
101 " -k => set directory containing private keys\n"
102 " -K => write public keys to this .dtb file\n"
103 " -c => add comment in signature node\n"
104 " -F => re-sign existing FIT image\n"
105 " -p => place external data at a static position\n"
106 " -r => mark keys used as 'required' in dtb\n");
109 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
111 fprintf(stderr, " %s -V ==> print version information and exit\n",
113 fprintf(stderr, "Use -T to see a list of available image types\n");
118 static int add_content(int type, const char *fname)
120 struct content_info *cont;
122 cont = calloc(1, sizeof(*cont));
127 if (params.content_tail)
128 params.content_tail->next = cont;
130 params.content_head = cont;
131 params.content_tail = cont;
136 static void process_args(int argc, char **argv)
139 int type = IH_TYPE_INVALID;
140 char *datafile = NULL;
143 while ((opt = getopt(argc, argv,
144 "a:A:b:c:C:d:D:e:Ef:Fk:K:ln:p:O:rR:qsT:vVx")) != -1) {
147 params.addr = strtoull(optarg, &ptr, 16);
149 fprintf(stderr, "%s: invalid load address %s\n",
150 params.cmdname, optarg);
155 params.arch = genimg_get_arch_id(optarg);
156 if (params.arch < 0) {
157 show_valid_options(IH_ARCH);
158 usage("Invalid architecture");
162 if (add_content(IH_TYPE_FLATDT, optarg)) {
164 "%s: Out of memory adding content '%s'",
165 params.cmdname, optarg);
170 params.comment = optarg;
173 params.comp = genimg_get_comp_id(optarg);
174 if (params.comp < 0) {
175 show_valid_options(IH_COMP);
176 usage("Invalid compression type");
180 params.datafile = optarg;
187 params.ep = strtoull(optarg, &ptr, 16);
189 fprintf(stderr, "%s: invalid entry point %s\n",
190 params.cmdname, optarg);
196 params.external_data = true;
200 params.auto_its = !strcmp(datafile, "auto");
204 * The flattened image tree (FIT) format
205 * requires a flattened device tree image type
207 params.type = IH_TYPE_FLATDT;
211 params.keydir = optarg;
214 params.keydest = optarg;
220 params.imagename = optarg;
223 params.os = genimg_get_os_id(optarg);
225 show_valid_options(IH_OS);
226 usage("Invalid operating system");
230 params.external_offset = strtoull(optarg, &ptr, 16);
232 fprintf(stderr, "%s: invalid offset size %s\n",
233 params.cmdname, optarg);
241 params.require_keys = 1;
245 * This entry is for the second configuration
246 * file, if only one is not enough.
248 params.imagename2 = optarg;
254 type = genimg_get_type_id(optarg);
256 show_valid_options(IH_TYPE);
257 usage("Invalid image type");
264 printf("mkimage version %s\n", PLAIN_VERSION);
270 usage("Invalid option");
274 /* The last parameter is expected to be the imagefile */
276 params.imagefile = argv[optind];
279 * For auto-generated FIT images we need to know the image type to put
280 * in the FIT, which is separate from the file's image type (which
281 * will always be IH_TYPE_FLATDT in this case).
283 if (params.type == IH_TYPE_FLATDT) {
284 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
285 /* For auto_its, datafile is always 'auto' */
286 if (!params.auto_its)
287 params.datafile = datafile;
288 else if (!params.datafile)
289 usage("Missing data file for auto-FIT (use -d)");
290 } else if (type != IH_TYPE_INVALID) {
294 if (!params.imagefile)
295 usage("Missing output filename");
298 int main(int argc, char **argv)
304 struct image_type_params *tparams = NULL;
308 params.cmdname = *argv;
312 process_args(argc, argv);
314 /* set tparams as per input type_id */
315 tparams = imagetool_get_type(params.type);
316 if (tparams == NULL) {
317 fprintf (stderr, "%s: unsupported type %s\n",
318 params.cmdname, genimg_get_type_name(params.type));
323 * check the passed arguments parameters meets the requirements
324 * as per image type to be generated/listed
326 if (tparams->check_params)
327 if (tparams->check_params (¶ms))
328 usage("Bad parameters for image type");
331 params.ep = params.addr;
332 /* If XIP, entry point must be after the U-Boot header */
334 params.ep += tparams->header_size;
338 if (tparams->fflag_handle)
340 * in some cases, some additional processing needs
341 * to be done if fflag is defined
343 * For ex. fit_handle_file for Fit file support
345 retval = tparams->fflag_handle(¶ms);
347 if (retval != EXIT_SUCCESS)
351 if (params.lflag || params.fflag) {
352 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
354 ifd = open (params.imagefile,
355 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
359 fprintf (stderr, "%s: Can't open %s: %s\n",
360 params.cmdname, params.imagefile,
365 if (params.lflag || params.fflag) {
367 * list header information of existing image
369 if (fstat(ifd, &sbuf) < 0) {
370 fprintf (stderr, "%s: Can't stat %s: %s\n",
371 params.cmdname, params.imagefile,
376 if ((unsigned)sbuf.st_size < tparams->header_size) {
378 "%s: Bad size: \"%s\" is not valid image\n",
379 params.cmdname, params.imagefile);
383 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
384 if (ptr == MAP_FAILED) {
385 fprintf (stderr, "%s: Can't read %s: %s\n",
386 params.cmdname, params.imagefile,
392 * scan through mkimage registry for all supported image types
393 * and verify the input image file header for match
394 * Print the image information for matched image type
395 * Returns the error code if not matched
397 retval = imagetool_verify_print_header(ptr, &sbuf,
400 (void) munmap((void *)ptr, sbuf.st_size);
406 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
407 dfd = open(params.datafile, O_RDONLY | O_BINARY);
409 fprintf(stderr, "%s: Can't open %s: %s\n",
410 params.cmdname, params.datafile,
415 if (fstat(dfd, &sbuf) < 0) {
416 fprintf(stderr, "%s: Can't stat %s: %s\n",
417 params.cmdname, params.datafile,
422 params.file_size = sbuf.st_size + tparams->header_size;
427 * In case there an header with a variable
428 * length will be added, the corresponding
429 * function is called. This is responsible to
430 * allocate memory for the header itself.
432 if (tparams->vrec_header)
433 pad_len = tparams->vrec_header(¶ms, tparams);
435 memset(tparams->hdr, 0, tparams->header_size);
437 if (write(ifd, tparams->hdr, tparams->header_size)
438 != tparams->header_size) {
439 fprintf (stderr, "%s: Write error on %s: %s\n",
440 params.cmdname, params.imagefile, strerror(errno));
444 if (!params.skipcpy) {
445 if (params.type == IH_TYPE_MULTI ||
446 params.type == IH_TYPE_SCRIPT) {
447 char *file = params.datafile;
454 if ((sep = strchr(file, ':')) != NULL) {
458 if (stat (file, &sbuf) < 0) {
459 fprintf (stderr, "%s: Can't stat %s: %s\n",
460 params.cmdname, file, strerror(errno));
463 size = cpu_to_uimage (sbuf.st_size);
468 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
469 fprintf (stderr, "%s: Write error on %s: %s\n",
470 params.cmdname, params.imagefile,
487 file = params.datafile;
490 char *sep = strchr(file, ':');
493 copy_file (ifd, file, 1);
497 copy_file (ifd, file, 0);
501 } else if (params.type == IH_TYPE_PBLIMAGE) {
502 /* PBL has special Image format, implements its' own */
503 pbl_load_uboot(ifd, ¶ms);
505 copy_file(ifd, params.datafile, pad_len);
509 /* We're a bit of paranoid */
510 #if defined(_POSIX_SYNCHRONIZED_IO) && \
511 !defined(__sun__) && \
512 !defined(__FreeBSD__) && \
513 !defined(__OpenBSD__) && \
515 (void) fdatasync (ifd);
520 if (fstat(ifd, &sbuf) < 0) {
521 fprintf (stderr, "%s: Can't stat %s: %s\n",
522 params.cmdname, params.imagefile, strerror(errno));
525 params.file_size = sbuf.st_size;
527 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
528 if (ptr == MAP_FAILED) {
529 fprintf (stderr, "%s: Can't map %s: %s\n",
530 params.cmdname, params.imagefile, strerror(errno));
534 /* Setup the image header as per input image type*/
535 if (tparams->set_header)
536 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
538 fprintf (stderr, "%s: Can't set header for %s: %s\n",
539 params.cmdname, tparams->name, strerror(errno));
543 /* Print the image information by processing image header */
544 if (tparams->print_header)
545 tparams->print_header (ptr);
547 fprintf (stderr, "%s: Can't print header for %s: %s\n",
548 params.cmdname, tparams->name, strerror(errno));
552 (void) munmap((void *)ptr, sbuf.st_size);
554 /* We're a bit of paranoid */
555 #if defined(_POSIX_SYNCHRONIZED_IO) && \
556 !defined(__sun__) && \
557 !defined(__FreeBSD__) && \
558 !defined(__OpenBSD__) && \
560 (void) fdatasync (ifd);
566 fprintf (stderr, "%s: Write error on %s: %s\n",
567 params.cmdname, params.imagefile, strerror(errno));
575 copy_file (int ifd, const char *datafile, int pad)
585 struct image_type_params *tparams = imagetool_get_type(params.type);
587 memset(zeros, 0, sizeof(zeros));
590 fprintf (stderr, "Adding Image %s\n", datafile);
593 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
594 fprintf (stderr, "%s: Can't open %s: %s\n",
595 params.cmdname, datafile, strerror(errno));
599 if (fstat(dfd, &sbuf) < 0) {
600 fprintf (stderr, "%s: Can't stat %s: %s\n",
601 params.cmdname, datafile, strerror(errno));
605 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
606 if (ptr == MAP_FAILED) {
607 fprintf (stderr, "%s: Can't read %s: %s\n",
608 params.cmdname, datafile, strerror(errno));
613 unsigned char *p = NULL;
615 * XIP: do not append the image_header_t at the
616 * beginning of the file, but consume the space
620 if ((unsigned)sbuf.st_size < tparams->header_size) {
622 "%s: Bad size: \"%s\" is too small for XIP\n",
623 params.cmdname, datafile);
627 for (p = ptr; p < ptr + tparams->header_size; p++) {
630 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
631 params.cmdname, datafile);
636 offset = tparams->header_size;
639 size = sbuf.st_size - offset;
640 if (write(ifd, ptr + offset, size) != size) {
641 fprintf (stderr, "%s: Write error on %s: %s\n",
642 params.cmdname, params.imagefile, strerror(errno));
647 if ((pad == 1) && (tail != 0)) {
649 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
650 fprintf (stderr, "%s: Write error on %s: %s\n",
651 params.cmdname, params.imagefile,
655 } else if (pad > 1) {
657 int todo = sizeof(zeros);
661 if (write(ifd, (char *)&zeros, todo) != todo) {
662 fprintf(stderr, "%s: Write error on %s: %s\n",
663 params.cmdname, params.imagefile,
671 (void) munmap((void *)ptr, sbuf.st_size);