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;
79 params.cmdname = *argv;
80 params.addr = params.ep = 0;
82 while (--argc > 0 && **++argv == '-') {
91 genimg_get_arch_id (*++argv)) < 0)
97 params.comment = *++argv;
102 genimg_get_comp_id (*++argv)) < 0)
108 params.dtc = *++argv;
114 genimg_get_os_id (*++argv)) < 0)
119 if (--argc >= 0 && argv[1]) {
121 genimg_get_type_id(*++argv);
123 if (params.type < 0) {
131 params.addr = strtoul (*++argv, &ptr, 16);
134 "%s: invalid load address %s\n",
135 params.cmdname, *argv);
142 params.datafile = *++argv;
148 params.ep = strtoul (*++argv, &ptr, 16);
151 "%s: invalid entry point %s\n",
152 params.cmdname, *argv);
160 params.datafile = *++argv;
164 * The flattened image tree (FIT) format
165 * requires a flattened device tree image type
167 params.type = IH_TYPE_FLATDT;
173 params.keydir = *++argv;
178 params.keydest = *++argv;
183 params.imagename = *++argv;
186 params.require_keys = 1;
192 * This entry is for the second configuration
193 * file, if only one is not enough.
195 params.imagename2 = *++argv;
204 printf("mkimage version %s\n", PLAIN_VERSION);
219 /* set tparams as per input type_id */
220 tparams = imagetool_get_type(params.type);
221 if (tparams == NULL) {
222 fprintf (stderr, "%s: unsupported type %s\n",
223 params.cmdname, genimg_get_type_name(params.type));
228 * check the passed arguments parameters meets the requirements
229 * as per image type to be generated/listed
231 if (tparams->check_params)
232 if (tparams->check_params (¶ms))
236 params.ep = params.addr;
237 /* If XIP, entry point must be after the U-Boot header */
239 params.ep += tparams->header_size;
242 params.imagefile = *argv;
245 if (tparams->fflag_handle)
247 * in some cases, some additional processing needs
248 * to be done if fflag is defined
250 * For ex. fit_handle_file for Fit file support
252 retval = tparams->fflag_handle(¶ms);
254 if (retval != EXIT_SUCCESS)
258 if (params.lflag || params.fflag) {
259 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
261 ifd = open (params.imagefile,
262 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
266 fprintf (stderr, "%s: Can't open %s: %s\n",
267 params.cmdname, params.imagefile,
272 if (params.lflag || params.fflag) {
274 * list header information of existing image
276 if (fstat(ifd, &sbuf) < 0) {
277 fprintf (stderr, "%s: Can't stat %s: %s\n",
278 params.cmdname, params.imagefile,
283 if ((unsigned)sbuf.st_size < tparams->header_size) {
285 "%s: Bad size: \"%s\" is not valid image\n",
286 params.cmdname, params.imagefile);
290 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
291 if (ptr == MAP_FAILED) {
292 fprintf (stderr, "%s: Can't read %s: %s\n",
293 params.cmdname, params.imagefile,
299 * scan through mkimage registry for all supported image types
300 * and verify the input image file header for match
301 * Print the image information for matched image type
302 * Returns the error code if not matched
304 retval = imagetool_verify_print_header(ptr, &sbuf,
307 (void) munmap((void *)ptr, sbuf.st_size);
314 * In case there an header with a variable
315 * length will be added, the corresponding
316 * function is called. This is responsible to
317 * allocate memory for the header itself.
319 if (tparams->vrec_header)
320 pad_len = tparams->vrec_header(¶ms, tparams);
322 memset(tparams->hdr, 0, tparams->header_size);
324 if (write(ifd, tparams->hdr, tparams->header_size)
325 != tparams->header_size) {
326 fprintf (stderr, "%s: Write error on %s: %s\n",
327 params.cmdname, params.imagefile, strerror(errno));
331 if (!params.skipcpy) {
332 if (params.type == IH_TYPE_MULTI ||
333 params.type == IH_TYPE_SCRIPT) {
334 char *file = params.datafile;
341 if ((sep = strchr(file, ':')) != NULL) {
345 if (stat (file, &sbuf) < 0) {
346 fprintf (stderr, "%s: Can't stat %s: %s\n",
347 params.cmdname, file, strerror(errno));
350 size = cpu_to_uimage (sbuf.st_size);
355 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
356 fprintf (stderr, "%s: Write error on %s: %s\n",
357 params.cmdname, params.imagefile,
374 file = params.datafile;
377 char *sep = strchr(file, ':');
380 copy_file (ifd, file, 1);
384 copy_file (ifd, file, 0);
388 } else if (params.type == IH_TYPE_PBLIMAGE) {
389 /* PBL has special Image format, implements its' own */
390 pbl_load_uboot(ifd, ¶ms);
392 copy_file(ifd, params.datafile, pad_len);
396 /* We're a bit of paranoid */
397 #if defined(_POSIX_SYNCHRONIZED_IO) && \
398 !defined(__sun__) && \
399 !defined(__FreeBSD__) && \
400 !defined(__OpenBSD__) && \
402 (void) fdatasync (ifd);
407 if (fstat(ifd, &sbuf) < 0) {
408 fprintf (stderr, "%s: Can't stat %s: %s\n",
409 params.cmdname, params.imagefile, strerror(errno));
413 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
414 if (ptr == MAP_FAILED) {
415 fprintf (stderr, "%s: Can't map %s: %s\n",
416 params.cmdname, params.imagefile, strerror(errno));
420 /* Setup the image header as per input image type*/
421 if (tparams->set_header)
422 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
424 fprintf (stderr, "%s: Can't set header for %s: %s\n",
425 params.cmdname, tparams->name, strerror(errno));
429 /* Print the image information by processing image header */
430 if (tparams->print_header)
431 tparams->print_header (ptr);
433 fprintf (stderr, "%s: Can't print header for %s: %s\n",
434 params.cmdname, tparams->name, strerror(errno));
438 (void) munmap((void *)ptr, sbuf.st_size);
440 /* We're a bit of paranoid */
441 #if defined(_POSIX_SYNCHRONIZED_IO) && \
442 !defined(__sun__) && \
443 !defined(__FreeBSD__) && \
444 !defined(__OpenBSD__) && \
446 (void) fdatasync (ifd);
452 fprintf (stderr, "%s: Write error on %s: %s\n",
453 params.cmdname, params.imagefile, strerror(errno));
461 copy_file (int ifd, const char *datafile, int pad)
471 struct image_type_params *tparams = imagetool_get_type(params.type);
473 if (pad >= sizeof(zeros)) {
474 fprintf(stderr, "%s: Can't pad to %d\n",
475 params.cmdname, pad);
479 memset(zeros, 0, sizeof(zeros));
482 fprintf (stderr, "Adding Image %s\n", datafile);
485 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
486 fprintf (stderr, "%s: Can't open %s: %s\n",
487 params.cmdname, datafile, strerror(errno));
491 if (fstat(dfd, &sbuf) < 0) {
492 fprintf (stderr, "%s: Can't stat %s: %s\n",
493 params.cmdname, datafile, strerror(errno));
497 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
498 if (ptr == MAP_FAILED) {
499 fprintf (stderr, "%s: Can't read %s: %s\n",
500 params.cmdname, datafile, strerror(errno));
505 unsigned char *p = NULL;
507 * XIP: do not append the image_header_t at the
508 * beginning of the file, but consume the space
512 if ((unsigned)sbuf.st_size < tparams->header_size) {
514 "%s: Bad size: \"%s\" is too small for XIP\n",
515 params.cmdname, datafile);
519 for (p = ptr; p < ptr + tparams->header_size; p++) {
522 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
523 params.cmdname, datafile);
528 offset = tparams->header_size;
531 size = sbuf.st_size - offset;
532 if (write(ifd, ptr + offset, size) != size) {
533 fprintf (stderr, "%s: Write error on %s: %s\n",
534 params.cmdname, params.imagefile, strerror(errno));
539 if ((pad == 1) && (tail != 0)) {
541 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
542 fprintf (stderr, "%s: Write error on %s: %s\n",
543 params.cmdname, params.imagefile,
547 } else if (pad > 1) {
548 if (write(ifd, (char *)&zeros, pad) != pad) {
549 fprintf(stderr, "%s: Write error on %s: %s\n",
550 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 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");