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);
16 static void usage(void);
18 /* parameters initialized by core will be used by the image type code */
19 struct image_tool_params params = {
22 .type = IH_TYPE_KERNEL,
24 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
29 static int h_compare_image_name(const void *vtype1, const void *vtype2)
31 const int *type1 = vtype1;
32 const int *type2 = vtype2;
33 const char *name1 = genimg_get_type_short_name(*type1);
34 const char *name2 = genimg_get_type_short_name(*type2);
36 return strcmp(name1, name2);
39 /* Show all image types supported by mkimage */
40 static void show_image_types(void)
42 struct image_type_params *tparams;
43 int order[IH_TYPE_COUNT];
48 /* Sort the names in order of short name for easier reading */
49 memset(order, '\0', sizeof(order));
50 for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) {
51 tparams = imagetool_get_type(type);
53 order[count++] = type;
55 qsort(order, count, sizeof(int), h_compare_image_name);
57 fprintf(stderr, "\nInvalid image type. Supported image types:\n");
58 for (i = 0; i < count; i++) {
60 tparams = imagetool_get_type(type);
62 fprintf(stderr, "\t%-15s %s\n",
63 genimg_get_type_short_name(type),
64 genimg_get_type_name(type));
67 fprintf(stderr, "\n");
70 static void process_args(int argc, char **argv)
75 while ((opt = getopt(argc, argv,
76 "a:A:cC:d:D:e:f:Fk:K:ln:O:rR:sT:vVx")) != -1) {
82 params.arch = genimg_get_arch_id(optarg);
87 params.comment = optarg;
90 params.comp = genimg_get_comp_id(optarg);
98 params.os = genimg_get_os_id(optarg);
103 params.type = genimg_get_type_id(optarg);
104 if (params.type < 0) {
110 params.addr = strtoull(optarg, &ptr, 16);
112 fprintf(stderr, "%s: invalid load address %s\n",
113 params.cmdname, optarg);
118 params.datafile = optarg;
122 params.ep = strtoull(optarg, &ptr, 16);
124 fprintf(stderr, "%s: invalid entry point %s\n",
125 params.cmdname, optarg);
131 params.datafile = optarg;
135 * The flattened image tree (FIT) format
136 * requires a flattened device tree image type
138 params.type = IH_TYPE_FLATDT;
142 params.keydir = optarg;
145 params.keydest = optarg;
148 params.imagename = optarg;
151 params.require_keys = 1;
155 * This entry is for the second configuration
156 * file, if only one is not enough.
158 params.imagename2 = optarg;
167 printf("mkimage version %s\n", PLAIN_VERSION);
179 params.imagefile = argv[optind];
183 int main(int argc, char **argv)
189 struct image_type_params *tparams = NULL;
193 params.cmdname = *argv;
197 process_args(argc, argv);
199 /* set tparams as per input type_id */
200 tparams = imagetool_get_type(params.type);
201 if (tparams == NULL) {
202 fprintf (stderr, "%s: unsupported type %s\n",
203 params.cmdname, genimg_get_type_name(params.type));
208 * check the passed arguments parameters meets the requirements
209 * as per image type to be generated/listed
211 if (tparams->check_params)
212 if (tparams->check_params (¶ms))
216 params.ep = params.addr;
217 /* If XIP, entry point must be after the U-Boot header */
219 params.ep += tparams->header_size;
223 if (tparams->fflag_handle)
225 * in some cases, some additional processing needs
226 * to be done if fflag is defined
228 * For ex. fit_handle_file for Fit file support
230 retval = tparams->fflag_handle(¶ms);
232 if (retval != EXIT_SUCCESS)
236 if (params.lflag || params.fflag) {
237 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
239 ifd = open (params.imagefile,
240 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
244 fprintf (stderr, "%s: Can't open %s: %s\n",
245 params.cmdname, params.imagefile,
250 if (params.lflag || params.fflag) {
252 * list header information of existing image
254 if (fstat(ifd, &sbuf) < 0) {
255 fprintf (stderr, "%s: Can't stat %s: %s\n",
256 params.cmdname, params.imagefile,
261 if ((unsigned)sbuf.st_size < tparams->header_size) {
263 "%s: Bad size: \"%s\" is not valid image\n",
264 params.cmdname, params.imagefile);
268 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
269 if (ptr == MAP_FAILED) {
270 fprintf (stderr, "%s: Can't read %s: %s\n",
271 params.cmdname, params.imagefile,
277 * scan through mkimage registry for all supported image types
278 * and verify the input image file header for match
279 * Print the image information for matched image type
280 * Returns the error code if not matched
282 retval = imagetool_verify_print_header(ptr, &sbuf,
285 (void) munmap((void *)ptr, sbuf.st_size);
291 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
292 dfd = open(params.datafile, O_RDONLY | O_BINARY);
294 fprintf(stderr, "%s: Can't open %s: %s\n",
295 params.cmdname, params.datafile,
300 if (fstat(dfd, &sbuf) < 0) {
301 fprintf(stderr, "%s: Can't stat %s: %s\n",
302 params.cmdname, params.datafile,
307 params.file_size = sbuf.st_size + tparams->header_size;
312 * In case there an header with a variable
313 * length will be added, the corresponding
314 * function is called. This is responsible to
315 * allocate memory for the header itself.
317 if (tparams->vrec_header)
318 pad_len = tparams->vrec_header(¶ms, tparams);
320 memset(tparams->hdr, 0, tparams->header_size);
322 if (write(ifd, tparams->hdr, tparams->header_size)
323 != tparams->header_size) {
324 fprintf (stderr, "%s: Write error on %s: %s\n",
325 params.cmdname, params.imagefile, strerror(errno));
329 if (!params.skipcpy) {
330 if (params.type == IH_TYPE_MULTI ||
331 params.type == IH_TYPE_SCRIPT) {
332 char *file = params.datafile;
339 if ((sep = strchr(file, ':')) != NULL) {
343 if (stat (file, &sbuf) < 0) {
344 fprintf (stderr, "%s: Can't stat %s: %s\n",
345 params.cmdname, file, strerror(errno));
348 size = cpu_to_uimage (sbuf.st_size);
353 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
354 fprintf (stderr, "%s: Write error on %s: %s\n",
355 params.cmdname, params.imagefile,
372 file = params.datafile;
375 char *sep = strchr(file, ':');
378 copy_file (ifd, file, 1);
382 copy_file (ifd, file, 0);
386 } else if (params.type == IH_TYPE_PBLIMAGE) {
387 /* PBL has special Image format, implements its' own */
388 pbl_load_uboot(ifd, ¶ms);
390 copy_file(ifd, params.datafile, pad_len);
394 /* We're a bit of paranoid */
395 #if defined(_POSIX_SYNCHRONIZED_IO) && \
396 !defined(__sun__) && \
397 !defined(__FreeBSD__) && \
398 !defined(__OpenBSD__) && \
400 (void) fdatasync (ifd);
405 if (fstat(ifd, &sbuf) < 0) {
406 fprintf (stderr, "%s: Can't stat %s: %s\n",
407 params.cmdname, params.imagefile, strerror(errno));
410 params.file_size = sbuf.st_size;
412 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
413 if (ptr == MAP_FAILED) {
414 fprintf (stderr, "%s: Can't map %s: %s\n",
415 params.cmdname, params.imagefile, strerror(errno));
419 /* Setup the image header as per input image type*/
420 if (tparams->set_header)
421 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
423 fprintf (stderr, "%s: Can't set header for %s: %s\n",
424 params.cmdname, tparams->name, strerror(errno));
428 /* Print the image information by processing image header */
429 if (tparams->print_header)
430 tparams->print_header (ptr);
432 fprintf (stderr, "%s: Can't print header for %s: %s\n",
433 params.cmdname, tparams->name, strerror(errno));
437 (void) munmap((void *)ptr, sbuf.st_size);
439 /* We're a bit of paranoid */
440 #if defined(_POSIX_SYNCHRONIZED_IO) && \
441 !defined(__sun__) && \
442 !defined(__FreeBSD__) && \
443 !defined(__OpenBSD__) && \
445 (void) fdatasync (ifd);
451 fprintf (stderr, "%s: Write error on %s: %s\n",
452 params.cmdname, params.imagefile, strerror(errno));
460 copy_file (int ifd, const char *datafile, int pad)
470 struct image_type_params *tparams = imagetool_get_type(params.type);
472 memset(zeros, 0, sizeof(zeros));
475 fprintf (stderr, "Adding Image %s\n", datafile);
478 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
479 fprintf (stderr, "%s: Can't open %s: %s\n",
480 params.cmdname, datafile, strerror(errno));
484 if (fstat(dfd, &sbuf) < 0) {
485 fprintf (stderr, "%s: Can't stat %s: %s\n",
486 params.cmdname, datafile, strerror(errno));
490 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
491 if (ptr == MAP_FAILED) {
492 fprintf (stderr, "%s: Can't read %s: %s\n",
493 params.cmdname, datafile, strerror(errno));
498 unsigned char *p = NULL;
500 * XIP: do not append the image_header_t at the
501 * beginning of the file, but consume the space
505 if ((unsigned)sbuf.st_size < tparams->header_size) {
507 "%s: Bad size: \"%s\" is too small for XIP\n",
508 params.cmdname, datafile);
512 for (p = ptr; p < ptr + tparams->header_size; p++) {
515 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
516 params.cmdname, datafile);
521 offset = tparams->header_size;
524 size = sbuf.st_size - offset;
525 if (write(ifd, ptr + offset, size) != size) {
526 fprintf (stderr, "%s: Write error on %s: %s\n",
527 params.cmdname, params.imagefile, strerror(errno));
532 if ((pad == 1) && (tail != 0)) {
534 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
535 fprintf (stderr, "%s: Write error on %s: %s\n",
536 params.cmdname, params.imagefile,
540 } else if (pad > 1) {
542 int todo = sizeof(zeros);
546 if (write(ifd, (char *)&zeros, todo) != todo) {
547 fprintf(stderr, "%s: Write error on %s: %s\n",
548 params.cmdname, params.imagefile,
556 (void) munmap((void *)ptr, sbuf.st_size);
560 static void usage(void)
562 fprintf (stderr, "Usage: %s -l image\n"
563 " -l ==> list image header information\n",
565 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
566 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
567 " -A ==> set architecture to 'arch'\n"
568 " -O ==> set operating system to 'os'\n"
569 " -T ==> set image type to 'type'\n"
570 " -C ==> set compression type 'comp'\n"
571 " -a ==> set load address to 'addr' (hex)\n"
572 " -e ==> set entry point to 'ep' (hex)\n"
573 " -n ==> set image name to 'name'\n"
574 " -d ==> use image data from 'datafile'\n"
575 " -x ==> set XIP (execute in place)\n",
577 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
579 fprintf(stderr, " -D => set all options for device tree compiler\n"
580 " -f => input filename for FIT source\n");
581 #ifdef CONFIG_FIT_SIGNATURE
582 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
583 " -k => set directory containing private keys\n"
584 " -K => write public keys to this .dtb file\n"
585 " -c => add comment in signature node\n"
586 " -F => re-sign existing FIT image\n"
587 " -r => mark keys used as 'required' in dtb\n");
589 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
591 fprintf (stderr, " %s -V ==> print version information and exit\n",
593 fprintf(stderr, "Use -T to see a list of available image types\n");