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 /* image_type_params link list to maintain registered image type supports */
19 struct image_type_params *mkimage_tparams = NULL;
21 /* parameters initialized by core will be used by the image type code */
22 struct mkimage_params params = {
25 .type = IH_TYPE_KERNEL,
27 .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
35 * It is used to register respective image generation/list support to the
38 * the input struct image_type_params is checked and appended to the link
39 * list, if the input structure is already registered, error
41 void mkimage_register (struct image_type_params *tparams)
43 struct image_type_params **tp;
46 fprintf (stderr, "%s: %s: Null input\n",
47 params.cmdname, __FUNCTION__);
51 /* scan the linked list, check for registry and point the last one */
52 for (tp = &mkimage_tparams; *tp != NULL; tp = &(*tp)->next) {
53 if (!strcmp((*tp)->name, tparams->name)) {
54 fprintf (stderr, "%s: %s already registered\n",
55 params.cmdname, tparams->name);
60 /* add input struct entry at the end of link list */
62 /* mark input entry as last entry in the link list */
65 debug ("Registered %s\n", tparams->name);
71 * It scans all registers image type supports
72 * checks the input type_id for each supported image type
75 * returns respective image_type_params pointer if success
76 * if input type_id is not supported by any of image_type_support
79 struct image_type_params *mkimage_get_type(int type)
81 struct image_type_params *curr;
83 for (curr = mkimage_tparams; curr != NULL; curr = curr->next) {
84 if (curr->check_image_type) {
85 if (!curr->check_image_type (type))
93 * mkimage_verify_print_header -
95 * It scans mkimage_tparams link list,
96 * verifies image_header for each supported image type
97 * if verification is successful, prints respective header
99 * returns negative if input image format does not match with any of
100 * supported image types
102 int mkimage_verify_print_header (void *ptr, struct stat *sbuf)
105 struct image_type_params *curr;
107 for (curr = mkimage_tparams; curr != NULL; curr = curr->next ) {
108 if (curr->verify_header) {
109 retval = curr->verify_header (
110 (unsigned char *)ptr, sbuf->st_size,
115 * Print the image information
116 * if verify is successful
118 if (curr->print_header)
119 curr->print_header (ptr);
122 "%s: print_header undefined for %s\n",
123 params.cmdname, curr->name);
133 main (int argc, char **argv)
139 struct image_type_params *tparams = NULL;
142 /* Init Freescale PBL Boot image generation/list support */
143 init_pbl_image_type();
144 /* Init Kirkwood Boot image generation/list support */
145 init_kwb_image_type ();
146 /* Init Freescale imx Boot image generation/list support */
147 init_imx_image_type ();
148 /* Init Freescale mxs Boot image generation/list support */
149 init_mxs_image_type();
150 /* Init FIT image generation/list support */
151 init_fit_image_type ();
152 /* Init TI OMAP Boot image generation/list support */
153 init_omap_image_type();
154 /* Init Default image generation/list support */
155 init_default_image_type ();
156 /* Init Davinci UBL support */
157 init_ubl_image_type();
158 /* Init Davinci AIS support */
159 init_ais_image_type();
161 params.cmdname = *argv;
162 params.addr = params.ep = 0;
164 while (--argc > 0 && **++argv == '-') {
173 genimg_get_arch_id (*++argv)) < 0)
179 params.comment = *++argv;
184 genimg_get_comp_id (*++argv)) < 0)
190 params.dtc = *++argv;
196 genimg_get_os_id (*++argv)) < 0)
202 genimg_get_type_id (*++argv)) < 0)
209 params.addr = strtoul (*++argv, &ptr, 16);
212 "%s: invalid load address %s\n",
213 params.cmdname, *argv);
220 params.datafile = *++argv;
226 params.ep = strtoul (*++argv, &ptr, 16);
229 "%s: invalid entry point %s\n",
230 params.cmdname, *argv);
238 params.datafile = *++argv;
242 * The flattened image tree (FIT) format
243 * requires a flattened device tree image type
245 params.type = IH_TYPE_FLATDT;
251 params.keydir = *++argv;
256 params.keydest = *++argv;
261 params.imagename = *++argv;
264 params.require_keys = 1;
270 * This entry is for the second configuration
271 * file, if only one is not enough.
273 params.imagename2 = *++argv;
282 printf("mkimage version %s\n", PLAIN_VERSION);
297 /* set tparams as per input type_id */
298 tparams = mkimage_get_type(params.type);
299 if (tparams == NULL) {
300 fprintf (stderr, "%s: unsupported type %s\n",
301 params.cmdname, genimg_get_type_name(params.type));
306 * check the passed arguments parameters meets the requirements
307 * as per image type to be generated/listed
309 if (tparams->check_params)
310 if (tparams->check_params (¶ms))
314 params.ep = params.addr;
315 /* If XIP, entry point must be after the U-Boot header */
317 params.ep += tparams->header_size;
320 params.imagefile = *argv;
323 if (tparams->fflag_handle)
325 * in some cases, some additional processing needs
326 * to be done if fflag is defined
328 * For ex. fit_handle_file for Fit file support
330 retval = tparams->fflag_handle(¶ms);
332 if (retval != EXIT_SUCCESS)
336 if (params.lflag || params.fflag) {
337 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
339 ifd = open (params.imagefile,
340 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
344 fprintf (stderr, "%s: Can't open %s: %s\n",
345 params.cmdname, params.imagefile,
350 if (params.lflag || params.fflag) {
352 * list header information of existing image
354 if (fstat(ifd, &sbuf) < 0) {
355 fprintf (stderr, "%s: Can't stat %s: %s\n",
356 params.cmdname, params.imagefile,
361 if ((unsigned)sbuf.st_size < tparams->header_size) {
363 "%s: Bad size: \"%s\" is not valid image\n",
364 params.cmdname, params.imagefile);
368 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
369 if (ptr == MAP_FAILED) {
370 fprintf (stderr, "%s: Can't read %s: %s\n",
371 params.cmdname, params.imagefile,
377 * scan through mkimage registry for all supported image types
378 * and verify the input image file header for match
379 * Print the image information for matched image type
380 * Returns the error code if not matched
382 retval = mkimage_verify_print_header (ptr, &sbuf);
384 (void) munmap((void *)ptr, sbuf.st_size);
391 * In case there an header with a variable
392 * length will be added, the corresponding
393 * function is called. This is responsible to
394 * allocate memory for the header itself.
396 if (tparams->vrec_header)
397 pad_len = tparams->vrec_header(¶ms, tparams);
399 memset(tparams->hdr, 0, tparams->header_size);
401 if (write(ifd, tparams->hdr, tparams->header_size)
402 != tparams->header_size) {
403 fprintf (stderr, "%s: Write error on %s: %s\n",
404 params.cmdname, params.imagefile, strerror(errno));
408 if (!params.skipcpy) {
409 if (params.type == IH_TYPE_MULTI ||
410 params.type == IH_TYPE_SCRIPT) {
411 char *file = params.datafile;
418 if ((sep = strchr(file, ':')) != NULL) {
422 if (stat (file, &sbuf) < 0) {
423 fprintf (stderr, "%s: Can't stat %s: %s\n",
424 params.cmdname, file, strerror(errno));
427 size = cpu_to_uimage (sbuf.st_size);
432 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
433 fprintf (stderr, "%s: Write error on %s: %s\n",
434 params.cmdname, params.imagefile,
451 file = params.datafile;
454 char *sep = strchr(file, ':');
457 copy_file (ifd, file, 1);
461 copy_file (ifd, file, 0);
465 } else if (params.type == IH_TYPE_PBLIMAGE) {
466 /* PBL has special Image format, implements its' own */
467 pbl_load_uboot(ifd, ¶ms);
469 copy_file(ifd, params.datafile, pad_len);
473 /* We're a bit of paranoid */
474 #if defined(_POSIX_SYNCHRONIZED_IO) && \
475 !defined(__sun__) && \
476 !defined(__FreeBSD__) && \
478 (void) fdatasync (ifd);
483 if (fstat(ifd, &sbuf) < 0) {
484 fprintf (stderr, "%s: Can't stat %s: %s\n",
485 params.cmdname, params.imagefile, strerror(errno));
489 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
490 if (ptr == MAP_FAILED) {
491 fprintf (stderr, "%s: Can't map %s: %s\n",
492 params.cmdname, params.imagefile, strerror(errno));
496 /* Setup the image header as per input image type*/
497 if (tparams->set_header)
498 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
500 fprintf (stderr, "%s: Can't set header for %s: %s\n",
501 params.cmdname, tparams->name, strerror(errno));
505 /* Print the image information by processing image header */
506 if (tparams->print_header)
507 tparams->print_header (ptr);
509 fprintf (stderr, "%s: Can't print header for %s: %s\n",
510 params.cmdname, tparams->name, strerror(errno));
514 (void) munmap((void *)ptr, sbuf.st_size);
516 /* We're a bit of paranoid */
517 #if defined(_POSIX_SYNCHRONIZED_IO) && \
518 !defined(__sun__) && \
519 !defined(__FreeBSD__) && \
521 (void) fdatasync (ifd);
527 fprintf (stderr, "%s: Write error on %s: %s\n",
528 params.cmdname, params.imagefile, strerror(errno));
536 copy_file (int ifd, const char *datafile, int pad)
546 struct image_type_params *tparams = mkimage_get_type (params.type);
548 if (pad >= sizeof(zeros)) {
549 fprintf(stderr, "%s: Can't pad to %d\n",
550 params.cmdname, pad);
554 memset(zeros, 0, sizeof(zeros));
557 fprintf (stderr, "Adding Image %s\n", datafile);
560 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
561 fprintf (stderr, "%s: Can't open %s: %s\n",
562 params.cmdname, datafile, strerror(errno));
566 if (fstat(dfd, &sbuf) < 0) {
567 fprintf (stderr, "%s: Can't stat %s: %s\n",
568 params.cmdname, datafile, strerror(errno));
572 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
573 if (ptr == MAP_FAILED) {
574 fprintf (stderr, "%s: Can't read %s: %s\n",
575 params.cmdname, datafile, strerror(errno));
580 unsigned char *p = NULL;
582 * XIP: do not append the image_header_t at the
583 * beginning of the file, but consume the space
587 if ((unsigned)sbuf.st_size < tparams->header_size) {
589 "%s: Bad size: \"%s\" is too small for XIP\n",
590 params.cmdname, datafile);
594 for (p = ptr; p < ptr + tparams->header_size; p++) {
597 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
598 params.cmdname, datafile);
603 offset = tparams->header_size;
606 size = sbuf.st_size - offset;
607 if (write(ifd, ptr + offset, size) != size) {
608 fprintf (stderr, "%s: Write error on %s: %s\n",
609 params.cmdname, params.imagefile, strerror(errno));
614 if ((pad == 1) && (tail != 0)) {
616 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
617 fprintf (stderr, "%s: Write error on %s: %s\n",
618 params.cmdname, params.imagefile,
622 } else if (pad > 1) {
623 if (write(ifd, (char *)&zeros, pad) != pad) {
624 fprintf(stderr, "%s: Write error on %s: %s\n",
625 params.cmdname, params.imagefile,
631 (void) munmap((void *)ptr, sbuf.st_size);
638 fprintf (stderr, "Usage: %s -l image\n"
639 " -l ==> list image header information\n",
641 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
642 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
643 " -A ==> set architecture to 'arch'\n"
644 " -O ==> set operating system to 'os'\n"
645 " -T ==> set image type to 'type'\n"
646 " -C ==> set compression type 'comp'\n"
647 " -a ==> set load address to 'addr' (hex)\n"
648 " -e ==> set entry point to 'ep' (hex)\n"
649 " -n ==> set image name to 'name'\n"
650 " -d ==> use image data from 'datafile'\n"
651 " -x ==> set XIP (execute in place)\n",
653 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
655 fprintf(stderr, " -D => set options for device tree compiler\n"
656 " -f => input filename for FIT source\n");
657 #ifdef CONFIG_FIT_SIGNATURE
658 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
659 " -k => set directory containing private keys\n"
660 " -K => write public keys to this .dtb file\n"
661 " -c => add comment in signature node\n"
662 " -F => re-sign existing FIT image\n"
663 " -r => mark keys used as 'required' in dtb\n");
665 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
667 fprintf (stderr, " %s -V ==> print version information and exit\n",