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 int main(int argc, char **argv)
76 struct image_type_params *tparams = NULL;
80 params.cmdname = *argv;
81 params.addr = params.ep = 0;
83 while (--argc > 0 && **++argv == '-') {
92 genimg_get_arch_id (*++argv)) < 0)
98 params.comment = *++argv;
103 genimg_get_comp_id (*++argv)) < 0)
109 params.dtc = *++argv;
115 genimg_get_os_id (*++argv)) < 0)
120 if (--argc >= 0 && argv[1]) {
122 genimg_get_type_id(*++argv);
124 if (params.type < 0) {
132 params.addr = strtoul (*++argv, &ptr, 16);
135 "%s: invalid load address %s\n",
136 params.cmdname, *argv);
143 params.datafile = *++argv;
149 params.ep = strtoul (*++argv, &ptr, 16);
152 "%s: invalid entry point %s\n",
153 params.cmdname, *argv);
161 params.datafile = *++argv;
165 * The flattened image tree (FIT) format
166 * requires a flattened device tree image type
168 params.type = IH_TYPE_FLATDT;
174 params.keydir = *++argv;
179 params.keydest = *++argv;
184 params.imagename = *++argv;
187 params.require_keys = 1;
193 * This entry is for the second configuration
194 * file, if only one is not enough.
196 params.imagename2 = *++argv;
205 printf("mkimage version %s\n", PLAIN_VERSION);
220 /* set tparams as per input type_id */
221 tparams = imagetool_get_type(params.type);
222 if (tparams == NULL) {
223 fprintf (stderr, "%s: unsupported type %s\n",
224 params.cmdname, genimg_get_type_name(params.type));
229 * check the passed arguments parameters meets the requirements
230 * as per image type to be generated/listed
232 if (tparams->check_params)
233 if (tparams->check_params (¶ms))
237 params.ep = params.addr;
238 /* If XIP, entry point must be after the U-Boot header */
240 params.ep += tparams->header_size;
243 params.imagefile = *argv;
246 if (tparams->fflag_handle)
248 * in some cases, some additional processing needs
249 * to be done if fflag is defined
251 * For ex. fit_handle_file for Fit file support
253 retval = tparams->fflag_handle(¶ms);
255 if (retval != EXIT_SUCCESS)
259 if (params.lflag || params.fflag) {
260 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
262 ifd = open (params.imagefile,
263 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
267 fprintf (stderr, "%s: Can't open %s: %s\n",
268 params.cmdname, params.imagefile,
273 if (params.lflag || params.fflag) {
275 * list header information of existing image
277 if (fstat(ifd, &sbuf) < 0) {
278 fprintf (stderr, "%s: Can't stat %s: %s\n",
279 params.cmdname, params.imagefile,
284 if ((unsigned)sbuf.st_size < tparams->header_size) {
286 "%s: Bad size: \"%s\" is not valid image\n",
287 params.cmdname, params.imagefile);
291 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
292 if (ptr == MAP_FAILED) {
293 fprintf (stderr, "%s: Can't read %s: %s\n",
294 params.cmdname, params.imagefile,
300 * scan through mkimage registry for all supported image types
301 * and verify the input image file header for match
302 * Print the image information for matched image type
303 * Returns the error code if not matched
305 retval = imagetool_verify_print_header(ptr, &sbuf,
308 (void) munmap((void *)ptr, sbuf.st_size);
314 if (!params.type == IH_TYPE_MULTI ||
315 !params.type == IH_TYPE_SCRIPT) {
316 dfd = open(params.datafile, O_RDONLY | O_BINARY);
318 fprintf(stderr, "%s: Can't open %s: %s\n",
319 params.cmdname, params.datafile,
324 if (fstat(dfd, &sbuf) < 0) {
325 fprintf(stderr, "%s: Can't stat %s: %s\n",
326 params.cmdname, params.datafile,
331 params.file_size = sbuf.st_size + tparams->header_size;
336 * In case there an header with a variable
337 * length will be added, the corresponding
338 * function is called. This is responsible to
339 * allocate memory for the header itself.
341 if (tparams->vrec_header)
342 pad_len = tparams->vrec_header(¶ms, tparams);
344 memset(tparams->hdr, 0, tparams->header_size);
346 if (write(ifd, tparams->hdr, tparams->header_size)
347 != tparams->header_size) {
348 fprintf (stderr, "%s: Write error on %s: %s\n",
349 params.cmdname, params.imagefile, strerror(errno));
353 if (!params.skipcpy) {
354 if (params.type == IH_TYPE_MULTI ||
355 params.type == IH_TYPE_SCRIPT) {
356 char *file = params.datafile;
363 if ((sep = strchr(file, ':')) != NULL) {
367 if (stat (file, &sbuf) < 0) {
368 fprintf (stderr, "%s: Can't stat %s: %s\n",
369 params.cmdname, file, strerror(errno));
372 size = cpu_to_uimage (sbuf.st_size);
377 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
378 fprintf (stderr, "%s: Write error on %s: %s\n",
379 params.cmdname, params.imagefile,
396 file = params.datafile;
399 char *sep = strchr(file, ':');
402 copy_file (ifd, file, 1);
406 copy_file (ifd, file, 0);
410 } else if (params.type == IH_TYPE_PBLIMAGE) {
411 /* PBL has special Image format, implements its' own */
412 pbl_load_uboot(ifd, ¶ms);
414 copy_file(ifd, params.datafile, pad_len);
418 /* We're a bit of paranoid */
419 #if defined(_POSIX_SYNCHRONIZED_IO) && \
420 !defined(__sun__) && \
421 !defined(__FreeBSD__) && \
422 !defined(__OpenBSD__) && \
424 (void) fdatasync (ifd);
429 if (fstat(ifd, &sbuf) < 0) {
430 fprintf (stderr, "%s: Can't stat %s: %s\n",
431 params.cmdname, params.imagefile, strerror(errno));
434 params.file_size = sbuf.st_size;
436 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
437 if (ptr == MAP_FAILED) {
438 fprintf (stderr, "%s: Can't map %s: %s\n",
439 params.cmdname, params.imagefile, strerror(errno));
443 /* Setup the image header as per input image type*/
444 if (tparams->set_header)
445 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
447 fprintf (stderr, "%s: Can't set header for %s: %s\n",
448 params.cmdname, tparams->name, strerror(errno));
452 /* Print the image information by processing image header */
453 if (tparams->print_header)
454 tparams->print_header (ptr);
456 fprintf (stderr, "%s: Can't print header for %s: %s\n",
457 params.cmdname, tparams->name, strerror(errno));
461 (void) munmap((void *)ptr, sbuf.st_size);
463 /* We're a bit of paranoid */
464 #if defined(_POSIX_SYNCHRONIZED_IO) && \
465 !defined(__sun__) && \
466 !defined(__FreeBSD__) && \
467 !defined(__OpenBSD__) && \
469 (void) fdatasync (ifd);
475 fprintf (stderr, "%s: Write error on %s: %s\n",
476 params.cmdname, params.imagefile, strerror(errno));
484 copy_file (int ifd, const char *datafile, int pad)
494 struct image_type_params *tparams = imagetool_get_type(params.type);
496 memset(zeros, 0, sizeof(zeros));
499 fprintf (stderr, "Adding Image %s\n", datafile);
502 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
503 fprintf (stderr, "%s: Can't open %s: %s\n",
504 params.cmdname, datafile, strerror(errno));
508 if (fstat(dfd, &sbuf) < 0) {
509 fprintf (stderr, "%s: Can't stat %s: %s\n",
510 params.cmdname, datafile, strerror(errno));
514 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
515 if (ptr == MAP_FAILED) {
516 fprintf (stderr, "%s: Can't read %s: %s\n",
517 params.cmdname, datafile, strerror(errno));
522 unsigned char *p = NULL;
524 * XIP: do not append the image_header_t at the
525 * beginning of the file, but consume the space
529 if ((unsigned)sbuf.st_size < tparams->header_size) {
531 "%s: Bad size: \"%s\" is too small for XIP\n",
532 params.cmdname, datafile);
536 for (p = ptr; p < ptr + tparams->header_size; p++) {
539 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
540 params.cmdname, datafile);
545 offset = tparams->header_size;
548 size = sbuf.st_size - offset;
549 if (write(ifd, ptr + offset, size) != size) {
550 fprintf (stderr, "%s: Write error on %s: %s\n",
551 params.cmdname, params.imagefile, strerror(errno));
556 if ((pad == 1) && (tail != 0)) {
558 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
559 fprintf (stderr, "%s: Write error on %s: %s\n",
560 params.cmdname, params.imagefile,
564 } else if (pad > 1) {
566 int todo = sizeof(zeros);
570 if (write(ifd, (char *)&zeros, todo) != todo) {
571 fprintf(stderr, "%s: Write error on %s: %s\n",
572 params.cmdname, params.imagefile,
580 (void) munmap((void *)ptr, sbuf.st_size);
584 static void usage(void)
586 fprintf (stderr, "Usage: %s -l image\n"
587 " -l ==> list image header information\n",
589 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
590 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
591 " -A ==> set architecture to 'arch'\n"
592 " -O ==> set operating system to 'os'\n"
593 " -T ==> set image type to 'type'\n"
594 " -C ==> set compression type 'comp'\n"
595 " -a ==> set load address to 'addr' (hex)\n"
596 " -e ==> set entry point to 'ep' (hex)\n"
597 " -n ==> set image name to 'name'\n"
598 " -d ==> use image data from 'datafile'\n"
599 " -x ==> set XIP (execute in place)\n",
601 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
603 fprintf(stderr, " -D => set all options for device tree compiler\n"
604 " -f => input filename for FIT source\n");
605 #ifdef CONFIG_FIT_SIGNATURE
606 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
607 " -k => set directory containing private keys\n"
608 " -K => write public keys to this .dtb file\n"
609 " -c => add comment in signature node\n"
610 " -F => re-sign existing FIT image\n"
611 " -r => mark keys used as 'required' in dtb\n");
613 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
615 fprintf (stderr, " %s -V ==> print version information and exit\n",
617 fprintf(stderr, "Use -T to see a list of available image types\n");