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) && (params.type != IH_TYPE_SCRIPT)) {
315 dfd = open(params.datafile, O_RDONLY | O_BINARY);
317 fprintf(stderr, "%s: Can't open %s: %s\n",
318 params.cmdname, params.datafile,
323 if (fstat(dfd, &sbuf) < 0) {
324 fprintf(stderr, "%s: Can't stat %s: %s\n",
325 params.cmdname, params.datafile,
330 params.file_size = sbuf.st_size + tparams->header_size;
335 * In case there an header with a variable
336 * length will be added, the corresponding
337 * function is called. This is responsible to
338 * allocate memory for the header itself.
340 if (tparams->vrec_header)
341 pad_len = tparams->vrec_header(¶ms, tparams);
343 memset(tparams->hdr, 0, tparams->header_size);
345 if (write(ifd, tparams->hdr, tparams->header_size)
346 != tparams->header_size) {
347 fprintf (stderr, "%s: Write error on %s: %s\n",
348 params.cmdname, params.imagefile, strerror(errno));
352 if (!params.skipcpy) {
353 if (params.type == IH_TYPE_MULTI ||
354 params.type == IH_TYPE_SCRIPT) {
355 char *file = params.datafile;
362 if ((sep = strchr(file, ':')) != NULL) {
366 if (stat (file, &sbuf) < 0) {
367 fprintf (stderr, "%s: Can't stat %s: %s\n",
368 params.cmdname, file, strerror(errno));
371 size = cpu_to_uimage (sbuf.st_size);
376 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
377 fprintf (stderr, "%s: Write error on %s: %s\n",
378 params.cmdname, params.imagefile,
395 file = params.datafile;
398 char *sep = strchr(file, ':');
401 copy_file (ifd, file, 1);
405 copy_file (ifd, file, 0);
409 } else if (params.type == IH_TYPE_PBLIMAGE) {
410 /* PBL has special Image format, implements its' own */
411 pbl_load_uboot(ifd, ¶ms);
413 copy_file(ifd, params.datafile, pad_len);
417 /* We're a bit of paranoid */
418 #if defined(_POSIX_SYNCHRONIZED_IO) && \
419 !defined(__sun__) && \
420 !defined(__FreeBSD__) && \
421 !defined(__OpenBSD__) && \
423 (void) fdatasync (ifd);
428 if (fstat(ifd, &sbuf) < 0) {
429 fprintf (stderr, "%s: Can't stat %s: %s\n",
430 params.cmdname, params.imagefile, strerror(errno));
433 params.file_size = sbuf.st_size;
435 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
436 if (ptr == MAP_FAILED) {
437 fprintf (stderr, "%s: Can't map %s: %s\n",
438 params.cmdname, params.imagefile, strerror(errno));
442 /* Setup the image header as per input image type*/
443 if (tparams->set_header)
444 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
446 fprintf (stderr, "%s: Can't set header for %s: %s\n",
447 params.cmdname, tparams->name, strerror(errno));
451 /* Print the image information by processing image header */
452 if (tparams->print_header)
453 tparams->print_header (ptr);
455 fprintf (stderr, "%s: Can't print header for %s: %s\n",
456 params.cmdname, tparams->name, strerror(errno));
460 (void) munmap((void *)ptr, sbuf.st_size);
462 /* We're a bit of paranoid */
463 #if defined(_POSIX_SYNCHRONIZED_IO) && \
464 !defined(__sun__) && \
465 !defined(__FreeBSD__) && \
466 !defined(__OpenBSD__) && \
468 (void) fdatasync (ifd);
474 fprintf (stderr, "%s: Write error on %s: %s\n",
475 params.cmdname, params.imagefile, strerror(errno));
483 copy_file (int ifd, const char *datafile, int pad)
493 struct image_type_params *tparams = imagetool_get_type(params.type);
495 memset(zeros, 0, sizeof(zeros));
498 fprintf (stderr, "Adding Image %s\n", datafile);
501 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
502 fprintf (stderr, "%s: Can't open %s: %s\n",
503 params.cmdname, datafile, strerror(errno));
507 if (fstat(dfd, &sbuf) < 0) {
508 fprintf (stderr, "%s: Can't stat %s: %s\n",
509 params.cmdname, datafile, strerror(errno));
513 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
514 if (ptr == MAP_FAILED) {
515 fprintf (stderr, "%s: Can't read %s: %s\n",
516 params.cmdname, datafile, strerror(errno));
521 unsigned char *p = NULL;
523 * XIP: do not append the image_header_t at the
524 * beginning of the file, but consume the space
528 if ((unsigned)sbuf.st_size < tparams->header_size) {
530 "%s: Bad size: \"%s\" is too small for XIP\n",
531 params.cmdname, datafile);
535 for (p = ptr; p < ptr + tparams->header_size; p++) {
538 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
539 params.cmdname, datafile);
544 offset = tparams->header_size;
547 size = sbuf.st_size - offset;
548 if (write(ifd, ptr + offset, size) != size) {
549 fprintf (stderr, "%s: Write error on %s: %s\n",
550 params.cmdname, params.imagefile, strerror(errno));
555 if ((pad == 1) && (tail != 0)) {
557 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
558 fprintf (stderr, "%s: Write error on %s: %s\n",
559 params.cmdname, params.imagefile,
563 } else if (pad > 1) {
565 int todo = sizeof(zeros);
569 if (write(ifd, (char *)&zeros, todo) != todo) {
570 fprintf(stderr, "%s: Write error on %s: %s\n",
571 params.cmdname, params.imagefile,
579 (void) munmap((void *)ptr, sbuf.st_size);
583 static void usage(void)
585 fprintf (stderr, "Usage: %s -l image\n"
586 " -l ==> list image header information\n",
588 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
589 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
590 " -A ==> set architecture to 'arch'\n"
591 " -O ==> set operating system to 'os'\n"
592 " -T ==> set image type to 'type'\n"
593 " -C ==> set compression type 'comp'\n"
594 " -a ==> set load address to 'addr' (hex)\n"
595 " -e ==> set entry point to 'ep' (hex)\n"
596 " -n ==> set image name to 'name'\n"
597 " -d ==> use image data from 'datafile'\n"
598 " -x ==> set XIP (execute in place)\n",
600 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
602 fprintf(stderr, " -D => set all options for device tree compiler\n"
603 " -f => input filename for FIT source\n");
604 #ifdef CONFIG_FIT_SIGNATURE
605 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
606 " -k => set directory containing private keys\n"
607 " -K => write public keys to this .dtb file\n"
608 " -c => add comment in signature node\n"
609 " -F => re-sign existing FIT image\n"
610 " -r => mark keys used as 'required' in dtb\n");
612 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
614 fprintf (stderr, " %s -V ==> print version information and exit\n",
616 fprintf(stderr, "Use -T to see a list of available image types\n");