2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2009
5 * DENX Software Engineering
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;
141 /* Init Freescale PBL Boot image generation/list support */
142 init_pbl_image_type();
143 /* Init Kirkwood Boot image generation/list support */
144 init_kwb_image_type ();
145 /* Init Freescale imx Boot image generation/list support */
146 init_imx_image_type ();
147 /* Init FIT image generation/list support */
148 init_fit_image_type ();
149 /* Init TI OMAP Boot image generation/list support */
150 init_omap_image_type();
151 /* Init Default image generation/list support */
152 init_default_image_type ();
153 /* Init Davinci UBL support */
154 init_ubl_image_type();
155 /* Init Davinci AIS support */
156 init_ais_image_type();
158 params.cmdname = *argv;
159 params.addr = params.ep = 0;
161 while (--argc > 0 && **++argv == '-') {
170 genimg_get_arch_id (*++argv)) < 0)
176 params.comment = *++argv;
181 genimg_get_comp_id (*++argv)) < 0)
187 params.dtc = *++argv;
193 genimg_get_os_id (*++argv)) < 0)
199 genimg_get_type_id (*++argv)) < 0)
206 params.addr = strtoul (*++argv, &ptr, 16);
209 "%s: invalid load address %s\n",
210 params.cmdname, *argv);
217 params.datafile = *++argv;
223 params.ep = strtoul (*++argv, &ptr, 16);
226 "%s: invalid entry point %s\n",
227 params.cmdname, *argv);
235 params.datafile = *++argv;
239 * The flattened image tree (FIT) format
240 * requires a flattened device tree image type
242 params.type = IH_TYPE_FLATDT;
248 params.keydir = *++argv;
253 params.keydest = *++argv;
258 params.imagename = *++argv;
261 params.require_keys = 1;
267 * This entry is for the second configuration
268 * file, if only one is not enough.
270 params.imagename2 = *++argv;
279 printf("mkimage version %s\n", PLAIN_VERSION);
294 /* set tparams as per input type_id */
295 tparams = mkimage_get_type(params.type);
296 if (tparams == NULL) {
297 fprintf (stderr, "%s: unsupported type %s\n",
298 params.cmdname, genimg_get_type_name(params.type));
303 * check the passed arguments parameters meets the requirements
304 * as per image type to be generated/listed
306 if (tparams->check_params)
307 if (tparams->check_params (¶ms))
311 params.ep = params.addr;
312 /* If XIP, entry point must be after the U-Boot header */
314 params.ep += tparams->header_size;
317 params.imagefile = *argv;
320 if (tparams->fflag_handle)
322 * in some cases, some additional processing needs
323 * to be done if fflag is defined
325 * For ex. fit_handle_file for Fit file support
327 retval = tparams->fflag_handle(¶ms);
329 if (retval != EXIT_SUCCESS)
333 if (params.lflag || params.fflag) {
334 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
336 ifd = open (params.imagefile,
337 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
341 fprintf (stderr, "%s: Can't open %s: %s\n",
342 params.cmdname, params.imagefile,
347 if (params.lflag || params.fflag) {
349 * list header information of existing image
351 if (fstat(ifd, &sbuf) < 0) {
352 fprintf (stderr, "%s: Can't stat %s: %s\n",
353 params.cmdname, params.imagefile,
358 if ((unsigned)sbuf.st_size < tparams->header_size) {
360 "%s: Bad size: \"%s\" is not valid image\n",
361 params.cmdname, params.imagefile);
365 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
366 if (ptr == MAP_FAILED) {
367 fprintf (stderr, "%s: Can't read %s: %s\n",
368 params.cmdname, params.imagefile,
374 * scan through mkimage registry for all supported image types
375 * and verify the input image file header for match
376 * Print the image information for matched image type
377 * Returns the error code if not matched
379 retval = mkimage_verify_print_header (ptr, &sbuf);
381 (void) munmap((void *)ptr, sbuf.st_size);
388 * In case there an header with a variable
389 * length will be added, the corresponding
390 * function is called. This is responsible to
391 * allocate memory for the header itself.
393 if (tparams->vrec_header)
394 tparams->vrec_header(¶ms, tparams);
396 memset(tparams->hdr, 0, tparams->header_size);
398 if (write(ifd, tparams->hdr, tparams->header_size)
399 != tparams->header_size) {
400 fprintf (stderr, "%s: Write error on %s: %s\n",
401 params.cmdname, params.imagefile, strerror(errno));
405 if (!params.skipcpy) {
406 if (params.type == IH_TYPE_MULTI ||
407 params.type == IH_TYPE_SCRIPT) {
408 char *file = params.datafile;
415 if ((sep = strchr(file, ':')) != NULL) {
419 if (stat (file, &sbuf) < 0) {
420 fprintf (stderr, "%s: Can't stat %s: %s\n",
421 params.cmdname, file, strerror(errno));
424 size = cpu_to_uimage (sbuf.st_size);
429 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
430 fprintf (stderr, "%s: Write error on %s: %s\n",
431 params.cmdname, params.imagefile,
448 file = params.datafile;
451 char *sep = strchr(file, ':');
454 copy_file (ifd, file, 1);
458 copy_file (ifd, file, 0);
462 } else if (params.type == IH_TYPE_PBLIMAGE) {
463 /* PBL has special Image format, implements its' own */
464 pbl_load_uboot(ifd, ¶ms);
466 copy_file (ifd, params.datafile, 0);
470 /* We're a bit of paranoid */
471 #if defined(_POSIX_SYNCHRONIZED_IO) && \
472 !defined(__sun__) && \
473 !defined(__FreeBSD__) && \
475 (void) fdatasync (ifd);
480 if (fstat(ifd, &sbuf) < 0) {
481 fprintf (stderr, "%s: Can't stat %s: %s\n",
482 params.cmdname, params.imagefile, strerror(errno));
486 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
487 if (ptr == MAP_FAILED) {
488 fprintf (stderr, "%s: Can't map %s: %s\n",
489 params.cmdname, params.imagefile, strerror(errno));
493 /* Setup the image header as per input image type*/
494 if (tparams->set_header)
495 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
497 fprintf (stderr, "%s: Can't set header for %s: %s\n",
498 params.cmdname, tparams->name, strerror(errno));
502 /* Print the image information by processing image header */
503 if (tparams->print_header)
504 tparams->print_header (ptr);
506 fprintf (stderr, "%s: Can't print header for %s: %s\n",
507 params.cmdname, tparams->name, strerror(errno));
511 (void) munmap((void *)ptr, sbuf.st_size);
513 /* We're a bit of paranoid */
514 #if defined(_POSIX_SYNCHRONIZED_IO) && \
515 !defined(__sun__) && \
516 !defined(__FreeBSD__) && \
518 (void) fdatasync (ifd);
524 fprintf (stderr, "%s: Write error on %s: %s\n",
525 params.cmdname, params.imagefile, strerror(errno));
533 copy_file (int ifd, const char *datafile, int pad)
542 struct image_type_params *tparams = mkimage_get_type (params.type);
545 fprintf (stderr, "Adding Image %s\n", datafile);
548 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
549 fprintf (stderr, "%s: Can't open %s: %s\n",
550 params.cmdname, datafile, strerror(errno));
554 if (fstat(dfd, &sbuf) < 0) {
555 fprintf (stderr, "%s: Can't stat %s: %s\n",
556 params.cmdname, datafile, strerror(errno));
560 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
561 if (ptr == MAP_FAILED) {
562 fprintf (stderr, "%s: Can't read %s: %s\n",
563 params.cmdname, datafile, strerror(errno));
568 unsigned char *p = NULL;
570 * XIP: do not append the image_header_t at the
571 * beginning of the file, but consume the space
575 if ((unsigned)sbuf.st_size < tparams->header_size) {
577 "%s: Bad size: \"%s\" is too small for XIP\n",
578 params.cmdname, datafile);
582 for (p = ptr; p < ptr + tparams->header_size; p++) {
585 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
586 params.cmdname, datafile);
591 offset = tparams->header_size;
594 size = sbuf.st_size - offset;
595 if (write(ifd, ptr + offset, size) != size) {
596 fprintf (stderr, "%s: Write error on %s: %s\n",
597 params.cmdname, params.imagefile, strerror(errno));
601 if (pad && ((tail = size % 4) != 0)) {
603 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
604 fprintf (stderr, "%s: Write error on %s: %s\n",
605 params.cmdname, params.imagefile,
611 (void) munmap((void *)ptr, sbuf.st_size);
618 fprintf (stderr, "Usage: %s -l image\n"
619 " -l ==> list image header information\n",
621 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
622 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
623 " -A ==> set architecture to 'arch'\n"
624 " -O ==> set operating system to 'os'\n"
625 " -T ==> set image type to 'type'\n"
626 " -C ==> set compression type 'comp'\n"
627 " -a ==> set load address to 'addr' (hex)\n"
628 " -e ==> set entry point to 'ep' (hex)\n"
629 " -n ==> set image name to 'name'\n"
630 " -d ==> use image data from 'datafile'\n"
631 " -x ==> set XIP (execute in place)\n",
633 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
635 fprintf(stderr, " -D => set options for device tree compiler\n"
636 " -f => input filename for FIT source\n");
637 #ifdef CONFIG_FIT_SIGNATURE
638 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
639 " -k => set directory containing private keys\n"
640 " -K => write public keys to this .dtb file\n"
641 " -c => add comment in signature node\n"
642 " -F => re-sign existing FIT image\n"
643 " -r => mark keys used as 'required' in dtb\n");
645 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
647 fprintf (stderr, " %s -V ==> print version information and exit\n",