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)
74 while (--argc > 0 && **++argv == '-') {
83 genimg_get_arch_id (*++argv)) < 0)
89 params.comment = *++argv;
94 genimg_get_comp_id (*++argv)) < 0)
100 params.dtc = *++argv;
106 genimg_get_os_id (*++argv)) < 0)
111 if (--argc >= 0 && argv[1]) {
113 genimg_get_type_id(*++argv);
115 if (params.type < 0) {
123 params.addr = strtoull(*++argv, &ptr, 16);
126 "%s: invalid load address %s\n",
127 params.cmdname, *argv);
134 params.datafile = *++argv;
140 params.ep = strtoull(*++argv, &ptr, 16);
143 "%s: invalid entry point %s\n",
144 params.cmdname, *argv);
152 params.datafile = *++argv;
156 * The flattened image tree (FIT) format
157 * requires a flattened device tree image type
159 params.type = IH_TYPE_FLATDT;
165 params.keydir = *++argv;
170 params.keydest = *++argv;
175 params.imagename = *++argv;
178 params.require_keys = 1;
184 * This entry is for the second configuration
185 * file, if only one is not enough.
187 params.imagename2 = *++argv;
196 printf("mkimage version %s\n", PLAIN_VERSION);
210 params.imagefile = *argv;
214 int main(int argc, char **argv)
220 struct image_type_params *tparams = NULL;
224 params.cmdname = *argv;
228 process_args(argc, argv);
230 /* set tparams as per input type_id */
231 tparams = imagetool_get_type(params.type);
232 if (tparams == NULL) {
233 fprintf (stderr, "%s: unsupported type %s\n",
234 params.cmdname, genimg_get_type_name(params.type));
239 * check the passed arguments parameters meets the requirements
240 * as per image type to be generated/listed
242 if (tparams->check_params)
243 if (tparams->check_params (¶ms))
247 params.ep = params.addr;
248 /* If XIP, entry point must be after the U-Boot header */
250 params.ep += tparams->header_size;
254 if (tparams->fflag_handle)
256 * in some cases, some additional processing needs
257 * to be done if fflag is defined
259 * For ex. fit_handle_file for Fit file support
261 retval = tparams->fflag_handle(¶ms);
263 if (retval != EXIT_SUCCESS)
267 if (params.lflag || params.fflag) {
268 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
270 ifd = open (params.imagefile,
271 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
275 fprintf (stderr, "%s: Can't open %s: %s\n",
276 params.cmdname, params.imagefile,
281 if (params.lflag || params.fflag) {
283 * list header information of existing image
285 if (fstat(ifd, &sbuf) < 0) {
286 fprintf (stderr, "%s: Can't stat %s: %s\n",
287 params.cmdname, params.imagefile,
292 if ((unsigned)sbuf.st_size < tparams->header_size) {
294 "%s: Bad size: \"%s\" is not valid image\n",
295 params.cmdname, params.imagefile);
299 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
300 if (ptr == MAP_FAILED) {
301 fprintf (stderr, "%s: Can't read %s: %s\n",
302 params.cmdname, params.imagefile,
308 * scan through mkimage registry for all supported image types
309 * and verify the input image file header for match
310 * Print the image information for matched image type
311 * Returns the error code if not matched
313 retval = imagetool_verify_print_header(ptr, &sbuf,
316 (void) munmap((void *)ptr, sbuf.st_size);
322 if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) {
323 dfd = open(params.datafile, O_RDONLY | O_BINARY);
325 fprintf(stderr, "%s: Can't open %s: %s\n",
326 params.cmdname, params.datafile,
331 if (fstat(dfd, &sbuf) < 0) {
332 fprintf(stderr, "%s: Can't stat %s: %s\n",
333 params.cmdname, params.datafile,
338 params.file_size = sbuf.st_size + tparams->header_size;
343 * In case there an header with a variable
344 * length will be added, the corresponding
345 * function is called. This is responsible to
346 * allocate memory for the header itself.
348 if (tparams->vrec_header)
349 pad_len = tparams->vrec_header(¶ms, tparams);
351 memset(tparams->hdr, 0, tparams->header_size);
353 if (write(ifd, tparams->hdr, tparams->header_size)
354 != tparams->header_size) {
355 fprintf (stderr, "%s: Write error on %s: %s\n",
356 params.cmdname, params.imagefile, strerror(errno));
360 if (!params.skipcpy) {
361 if (params.type == IH_TYPE_MULTI ||
362 params.type == IH_TYPE_SCRIPT) {
363 char *file = params.datafile;
370 if ((sep = strchr(file, ':')) != NULL) {
374 if (stat (file, &sbuf) < 0) {
375 fprintf (stderr, "%s: Can't stat %s: %s\n",
376 params.cmdname, file, strerror(errno));
379 size = cpu_to_uimage (sbuf.st_size);
384 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
385 fprintf (stderr, "%s: Write error on %s: %s\n",
386 params.cmdname, params.imagefile,
403 file = params.datafile;
406 char *sep = strchr(file, ':');
409 copy_file (ifd, file, 1);
413 copy_file (ifd, file, 0);
417 } else if (params.type == IH_TYPE_PBLIMAGE) {
418 /* PBL has special Image format, implements its' own */
419 pbl_load_uboot(ifd, ¶ms);
421 copy_file(ifd, params.datafile, pad_len);
425 /* We're a bit of paranoid */
426 #if defined(_POSIX_SYNCHRONIZED_IO) && \
427 !defined(__sun__) && \
428 !defined(__FreeBSD__) && \
429 !defined(__OpenBSD__) && \
431 (void) fdatasync (ifd);
436 if (fstat(ifd, &sbuf) < 0) {
437 fprintf (stderr, "%s: Can't stat %s: %s\n",
438 params.cmdname, params.imagefile, strerror(errno));
441 params.file_size = sbuf.st_size;
443 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
444 if (ptr == MAP_FAILED) {
445 fprintf (stderr, "%s: Can't map %s: %s\n",
446 params.cmdname, params.imagefile, strerror(errno));
450 /* Setup the image header as per input image type*/
451 if (tparams->set_header)
452 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
454 fprintf (stderr, "%s: Can't set header for %s: %s\n",
455 params.cmdname, tparams->name, strerror(errno));
459 /* Print the image information by processing image header */
460 if (tparams->print_header)
461 tparams->print_header (ptr);
463 fprintf (stderr, "%s: Can't print header for %s: %s\n",
464 params.cmdname, tparams->name, strerror(errno));
468 (void) munmap((void *)ptr, sbuf.st_size);
470 /* We're a bit of paranoid */
471 #if defined(_POSIX_SYNCHRONIZED_IO) && \
472 !defined(__sun__) && \
473 !defined(__FreeBSD__) && \
474 !defined(__OpenBSD__) && \
476 (void) fdatasync (ifd);
482 fprintf (stderr, "%s: Write error on %s: %s\n",
483 params.cmdname, params.imagefile, strerror(errno));
491 copy_file (int ifd, const char *datafile, int pad)
501 struct image_type_params *tparams = imagetool_get_type(params.type);
503 memset(zeros, 0, sizeof(zeros));
506 fprintf (stderr, "Adding Image %s\n", datafile);
509 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
510 fprintf (stderr, "%s: Can't open %s: %s\n",
511 params.cmdname, datafile, strerror(errno));
515 if (fstat(dfd, &sbuf) < 0) {
516 fprintf (stderr, "%s: Can't stat %s: %s\n",
517 params.cmdname, datafile, strerror(errno));
521 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
522 if (ptr == MAP_FAILED) {
523 fprintf (stderr, "%s: Can't read %s: %s\n",
524 params.cmdname, datafile, strerror(errno));
529 unsigned char *p = NULL;
531 * XIP: do not append the image_header_t at the
532 * beginning of the file, but consume the space
536 if ((unsigned)sbuf.st_size < tparams->header_size) {
538 "%s: Bad size: \"%s\" is too small for XIP\n",
539 params.cmdname, datafile);
543 for (p = ptr; p < ptr + tparams->header_size; p++) {
546 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
547 params.cmdname, datafile);
552 offset = tparams->header_size;
555 size = sbuf.st_size - offset;
556 if (write(ifd, ptr + offset, size) != size) {
557 fprintf (stderr, "%s: Write error on %s: %s\n",
558 params.cmdname, params.imagefile, strerror(errno));
563 if ((pad == 1) && (tail != 0)) {
565 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
566 fprintf (stderr, "%s: Write error on %s: %s\n",
567 params.cmdname, params.imagefile,
571 } else if (pad > 1) {
573 int todo = sizeof(zeros);
577 if (write(ifd, (char *)&zeros, todo) != todo) {
578 fprintf(stderr, "%s: Write error on %s: %s\n",
579 params.cmdname, params.imagefile,
587 (void) munmap((void *)ptr, sbuf.st_size);
591 static void usage(void)
593 fprintf (stderr, "Usage: %s -l image\n"
594 " -l ==> list image header information\n",
596 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
597 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
598 " -A ==> set architecture to 'arch'\n"
599 " -O ==> set operating system to 'os'\n"
600 " -T ==> set image type to 'type'\n"
601 " -C ==> set compression type 'comp'\n"
602 " -a ==> set load address to 'addr' (hex)\n"
603 " -e ==> set entry point to 'ep' (hex)\n"
604 " -n ==> set image name to 'name'\n"
605 " -d ==> use image data from 'datafile'\n"
606 " -x ==> set XIP (execute in place)\n",
608 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
610 fprintf(stderr, " -D => set all options for device tree compiler\n"
611 " -f => input filename for FIT source\n");
612 #ifdef CONFIG_FIT_SIGNATURE
613 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
614 " -k => set directory containing private keys\n"
615 " -K => write public keys to this .dtb file\n"
616 " -c => add comment in signature node\n"
617 " -F => re-sign existing FIT image\n"
618 " -r => mark keys used as 'required' in dtb\n");
620 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
622 fprintf (stderr, " %s -V ==> print version information and exit\n",
624 fprintf(stderr, "Use -T to see a list of available image types\n");