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+
16 static void copy_file(int, const char *, int);
18 /* parameters initialized by core will be used by the image type code */
19 static struct image_tool_params params = {
22 .type = IH_TYPE_KERNEL,
24 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
29 static enum ih_category cur_category;
31 static int h_compare_category_name(const void *vtype1, const void *vtype2)
33 const int *type1 = vtype1;
34 const int *type2 = vtype2;
35 const char *name1 = genimg_get_cat_short_name(cur_category, *type1);
36 const char *name2 = genimg_get_cat_short_name(cur_category, *type2);
38 return strcmp(name1, name2);
41 static int show_valid_options(enum ih_category category)
48 count = genimg_get_cat_count(category);
49 order = calloc(count, sizeof(*order));
53 /* Sort the names in order of short name for easier reading */
54 for (item = 0; item < count; item++)
56 cur_category = category;
57 qsort(order, count, sizeof(int), h_compare_category_name);
59 fprintf(stderr, "\nInvalid %s, supported are:\n",
60 genimg_get_cat_desc(category));
61 for (i = 0; i < count; i++) {
63 fprintf(stderr, "\t%-15s %s\n",
64 genimg_get_cat_short_name(category, item),
65 genimg_get_cat_name(category, item));
67 fprintf(stderr, "\n");
73 static void usage(const char *msg)
75 fprintf(stderr, "Error: %s\n", msg);
76 fprintf(stderr, "Usage: %s -l image\n"
77 " -l ==> list image header information\n",
80 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
81 " -A ==> set architecture to 'arch'\n"
82 " -O ==> set operating system to 'os'\n"
83 " -T ==> set image type to 'type'\n"
84 " -C ==> set compression type 'comp'\n"
85 " -a ==> set load address to 'addr' (hex)\n"
86 " -e ==> set entry point to 'ep' (hex)\n"
87 " -n ==> set image name to 'name'\n"
88 " -d ==> use image data from 'datafile'\n"
89 " -x ==> set XIP (execute in place)\n",
92 " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-i <ramdisk.cpio.gz>] fit-image\n"
93 " <dtb> file is used with -f auto, it may occur multiple times.\n",
96 " -D => set all options for device tree compiler\n"
97 " -f => input filename for FIT source\n"
98 " -i => input filename for ramdisk file\n");
99 #ifdef CONFIG_FIT_SIGNATURE
101 "Signing / verified boot options: [-E] [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r]\n"
102 " -E => place data outside of the FIT structure\n"
103 " -k => set directory containing private keys\n"
104 " -K => write public keys to this .dtb file\n"
105 " -c => add comment in signature node\n"
106 " -F => re-sign existing FIT image\n"
107 " -p => place external data at a static position\n"
108 " -r => mark keys used as 'required' in dtb\n");
111 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
113 fprintf(stderr, " %s -V ==> print version information and exit\n",
115 fprintf(stderr, "Use -T to see a list of available image types\n");
120 static int add_content(int type, const char *fname)
122 struct content_info *cont;
124 cont = calloc(1, sizeof(*cont));
129 if (params.content_tail)
130 params.content_tail->next = cont;
132 params.content_head = cont;
133 params.content_tail = cont;
138 static void process_args(int argc, char **argv)
141 int type = IH_TYPE_INVALID;
142 char *datafile = NULL;
145 while ((opt = getopt(argc, argv,
146 "a:A:b:c:C:d:D:e:Ef:Fk:i:K:ln:p:O:rR:qsT:vVx")) != -1) {
149 params.addr = strtoull(optarg, &ptr, 16);
151 fprintf(stderr, "%s: invalid load address %s\n",
152 params.cmdname, optarg);
157 params.arch = genimg_get_arch_id(optarg);
158 if (params.arch < 0) {
159 show_valid_options(IH_ARCH);
160 usage("Invalid architecture");
164 if (add_content(IH_TYPE_FLATDT, optarg)) {
166 "%s: Out of memory adding content '%s'",
167 params.cmdname, optarg);
172 params.comment = optarg;
175 params.comp = genimg_get_comp_id(optarg);
176 if (params.comp < 0) {
177 show_valid_options(IH_COMP);
178 usage("Invalid compression type");
182 params.datafile = optarg;
189 params.ep = strtoull(optarg, &ptr, 16);
191 fprintf(stderr, "%s: invalid entry point %s\n",
192 params.cmdname, optarg);
198 params.external_data = true;
202 params.auto_its = !strcmp(datafile, "auto");
206 * The flattened image tree (FIT) format
207 * requires a flattened device tree image type
209 params.type = IH_TYPE_FLATDT;
213 params.fit_ramdisk = optarg;
216 params.keydir = optarg;
219 params.keydest = optarg;
225 params.imagename = optarg;
228 params.os = genimg_get_os_id(optarg);
230 show_valid_options(IH_OS);
231 usage("Invalid operating system");
235 params.external_offset = strtoull(optarg, &ptr, 16);
237 fprintf(stderr, "%s: invalid offset size %s\n",
238 params.cmdname, optarg);
246 params.require_keys = 1;
250 * This entry is for the second configuration
251 * file, if only one is not enough.
253 params.imagename2 = optarg;
259 type = genimg_get_type_id(optarg);
261 show_valid_options(IH_TYPE);
262 usage("Invalid image type");
269 printf("mkimage version %s\n", PLAIN_VERSION);
275 usage("Invalid option");
279 /* The last parameter is expected to be the imagefile */
281 params.imagefile = argv[optind];
284 * For auto-generated FIT images we need to know the image type to put
285 * in the FIT, which is separate from the file's image type (which
286 * will always be IH_TYPE_FLATDT in this case).
288 if (params.type == IH_TYPE_FLATDT) {
289 params.fit_image_type = type ? type : IH_TYPE_KERNEL;
290 /* For auto_its, datafile is always 'auto' */
291 if (!params.auto_its)
292 params.datafile = datafile;
293 else if (!params.datafile)
294 usage("Missing data file for auto-FIT (use -d)");
295 } else if (type != IH_TYPE_INVALID) {
299 if (!params.imagefile)
300 usage("Missing output filename");
303 int main(int argc, char **argv)
309 struct image_type_params *tparams = NULL;
313 params.cmdname = *argv;
317 process_args(argc, argv);
319 /* set tparams as per input type_id */
320 tparams = imagetool_get_type(params.type);
321 if (tparams == NULL) {
322 fprintf (stderr, "%s: unsupported type %s\n",
323 params.cmdname, genimg_get_type_name(params.type));
328 * check the passed arguments parameters meets the requirements
329 * as per image type to be generated/listed
331 if (tparams->check_params)
332 if (tparams->check_params (¶ms))
333 usage("Bad parameters for image type");
336 params.ep = params.addr;
337 /* If XIP, entry point must be after the U-Boot header */
339 params.ep += tparams->header_size;
343 if (tparams->fflag_handle)
345 * in some cases, some additional processing needs
346 * to be done if fflag is defined
348 * For ex. fit_handle_file for Fit file support
350 retval = tparams->fflag_handle(¶ms);
352 if (retval != EXIT_SUCCESS)
356 if (params.lflag || params.fflag) {
357 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
359 ifd = open (params.imagefile,
360 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
364 fprintf (stderr, "%s: Can't open %s: %s\n",
365 params.cmdname, params.imagefile,
370 if (params.lflag || params.fflag) {
372 * list header information of existing image
374 if (fstat(ifd, &sbuf) < 0) {
375 fprintf (stderr, "%s: Can't stat %s: %s\n",
376 params.cmdname, params.imagefile,
381 if ((unsigned)sbuf.st_size < tparams->header_size) {
383 "%s: Bad size: \"%s\" is not valid image\n",
384 params.cmdname, params.imagefile);
388 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
389 if (ptr == MAP_FAILED) {
390 fprintf (stderr, "%s: Can't read %s: %s\n",
391 params.cmdname, params.imagefile,
397 * scan through mkimage registry for all supported image types
398 * and verify the input image file header for match
399 * Print the image information for matched image type
400 * Returns the error code if not matched
402 retval = imagetool_verify_print_header(ptr, &sbuf,
405 (void) munmap((void *)ptr, sbuf.st_size);
411 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
412 dfd = open(params.datafile, O_RDONLY | O_BINARY);
414 fprintf(stderr, "%s: Can't open %s: %s\n",
415 params.cmdname, params.datafile,
420 if (fstat(dfd, &sbuf) < 0) {
421 fprintf(stderr, "%s: Can't stat %s: %s\n",
422 params.cmdname, params.datafile,
427 params.file_size = sbuf.st_size + tparams->header_size;
432 * In case there an header with a variable
433 * length will be added, the corresponding
434 * function is called. This is responsible to
435 * allocate memory for the header itself.
437 if (tparams->vrec_header)
438 pad_len = tparams->vrec_header(¶ms, tparams);
440 memset(tparams->hdr, 0, tparams->header_size);
442 if (write(ifd, tparams->hdr, tparams->header_size)
443 != tparams->header_size) {
444 fprintf (stderr, "%s: Write error on %s: %s\n",
445 params.cmdname, params.imagefile, strerror(errno));
449 if (!params.skipcpy) {
450 if (params.type == IH_TYPE_MULTI ||
451 params.type == IH_TYPE_SCRIPT) {
452 char *file = params.datafile;
459 if ((sep = strchr(file, ':')) != NULL) {
463 if (stat (file, &sbuf) < 0) {
464 fprintf (stderr, "%s: Can't stat %s: %s\n",
465 params.cmdname, file, strerror(errno));
468 size = cpu_to_uimage (sbuf.st_size);
473 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
474 fprintf (stderr, "%s: Write error on %s: %s\n",
475 params.cmdname, params.imagefile,
492 file = params.datafile;
495 char *sep = strchr(file, ':');
498 copy_file (ifd, file, 1);
502 copy_file (ifd, file, 0);
506 } else if (params.type == IH_TYPE_PBLIMAGE) {
507 /* PBL has special Image format, implements its' own */
508 pbl_load_uboot(ifd, ¶ms);
510 copy_file(ifd, params.datafile, pad_len);
512 if (params.type == IH_TYPE_FIRMWARE_IVT) {
513 /* Add alignment and IVT */
514 uint32_t aligned_filesize = (params.file_size + 0x1000
515 - 1) & ~(0x1000 - 1);
516 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
517 params.addr, 0, 0, 0, params.addr
519 - tparams->header_size,
520 params.addr + aligned_filesize
521 - tparams->header_size
523 int i = params.file_size;
524 for (; i < aligned_filesize; i++) {
525 if (write(ifd, &i, 1) != 1) {
527 "%s: Write error on %s: %s\n",
534 if (write(ifd, &ivt_header, sizeof(flash_header_v2_t))
535 != sizeof(flash_header_v2_t)) {
536 fprintf(stderr, "%s: Write error on %s: %s\n",
545 /* We're a bit of paranoid */
546 #if defined(_POSIX_SYNCHRONIZED_IO) && \
547 !defined(__sun__) && \
548 !defined(__FreeBSD__) && \
549 !defined(__OpenBSD__) && \
551 (void) fdatasync (ifd);
556 if (fstat(ifd, &sbuf) < 0) {
557 fprintf (stderr, "%s: Can't stat %s: %s\n",
558 params.cmdname, params.imagefile, strerror(errno));
561 params.file_size = sbuf.st_size;
563 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
564 if (ptr == MAP_FAILED) {
565 fprintf (stderr, "%s: Can't map %s: %s\n",
566 params.cmdname, params.imagefile, strerror(errno));
570 /* Setup the image header as per input image type*/
571 if (tparams->set_header)
572 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
574 fprintf (stderr, "%s: Can't set header for %s: %s\n",
575 params.cmdname, tparams->name, strerror(errno));
579 /* Print the image information by processing image header */
580 if (tparams->print_header)
581 tparams->print_header (ptr);
583 fprintf (stderr, "%s: Can't print header for %s: %s\n",
584 params.cmdname, tparams->name, strerror(errno));
588 (void) munmap((void *)ptr, sbuf.st_size);
590 /* We're a bit of paranoid */
591 #if defined(_POSIX_SYNCHRONIZED_IO) && \
592 !defined(__sun__) && \
593 !defined(__FreeBSD__) && \
594 !defined(__OpenBSD__) && \
596 (void) fdatasync (ifd);
602 fprintf (stderr, "%s: Write error on %s: %s\n",
603 params.cmdname, params.imagefile, strerror(errno));
611 copy_file (int ifd, const char *datafile, int pad)
621 struct image_type_params *tparams = imagetool_get_type(params.type);
623 memset(zeros, 0, sizeof(zeros));
626 fprintf (stderr, "Adding Image %s\n", datafile);
629 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
630 fprintf (stderr, "%s: Can't open %s: %s\n",
631 params.cmdname, datafile, strerror(errno));
635 if (fstat(dfd, &sbuf) < 0) {
636 fprintf (stderr, "%s: Can't stat %s: %s\n",
637 params.cmdname, datafile, strerror(errno));
641 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
642 if (ptr == MAP_FAILED) {
643 fprintf (stderr, "%s: Can't read %s: %s\n",
644 params.cmdname, datafile, strerror(errno));
649 unsigned char *p = NULL;
651 * XIP: do not append the image_header_t at the
652 * beginning of the file, but consume the space
656 if ((unsigned)sbuf.st_size < tparams->header_size) {
658 "%s: Bad size: \"%s\" is too small for XIP\n",
659 params.cmdname, datafile);
663 for (p = ptr; p < ptr + tparams->header_size; p++) {
666 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
667 params.cmdname, datafile);
672 offset = tparams->header_size;
675 size = sbuf.st_size - offset;
676 if (write(ifd, ptr + offset, size) != size) {
677 fprintf (stderr, "%s: Write error on %s: %s\n",
678 params.cmdname, params.imagefile, strerror(errno));
683 if ((pad == 1) && (tail != 0)) {
685 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
686 fprintf (stderr, "%s: Write error on %s: %s\n",
687 params.cmdname, params.imagefile,
691 } else if (pad > 1) {
693 int todo = sizeof(zeros);
697 if (write(ifd, (char *)&zeros, todo) != todo) {
698 fprintf(stderr, "%s: Write error on %s: %s\n",
699 params.cmdname, params.imagefile,
707 (void) munmap((void *)ptr, sbuf.st_size);