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 FIT image generation/list support */
149 init_fit_image_type ();
150 /* Init TI OMAP Boot image generation/list support */
151 init_omap_image_type();
152 /* Init Default image generation/list support */
153 init_default_image_type ();
154 /* Init Davinci UBL support */
155 init_ubl_image_type();
156 /* Init Davinci AIS support */
157 init_ais_image_type();
159 params.cmdname = *argv;
160 params.addr = params.ep = 0;
162 while (--argc > 0 && **++argv == '-') {
171 genimg_get_arch_id (*++argv)) < 0)
177 params.comment = *++argv;
182 genimg_get_comp_id (*++argv)) < 0)
188 params.dtc = *++argv;
194 genimg_get_os_id (*++argv)) < 0)
200 genimg_get_type_id (*++argv)) < 0)
207 params.addr = strtoul (*++argv, &ptr, 16);
210 "%s: invalid load address %s\n",
211 params.cmdname, *argv);
218 params.datafile = *++argv;
224 params.ep = strtoul (*++argv, &ptr, 16);
227 "%s: invalid entry point %s\n",
228 params.cmdname, *argv);
236 params.datafile = *++argv;
240 * The flattened image tree (FIT) format
241 * requires a flattened device tree image type
243 params.type = IH_TYPE_FLATDT;
249 params.keydir = *++argv;
254 params.keydest = *++argv;
259 params.imagename = *++argv;
262 params.require_keys = 1;
268 * This entry is for the second configuration
269 * file, if only one is not enough.
271 params.imagename2 = *++argv;
280 printf("mkimage version %s\n", PLAIN_VERSION);
295 /* set tparams as per input type_id */
296 tparams = mkimage_get_type(params.type);
297 if (tparams == NULL) {
298 fprintf (stderr, "%s: unsupported type %s\n",
299 params.cmdname, genimg_get_type_name(params.type));
304 * check the passed arguments parameters meets the requirements
305 * as per image type to be generated/listed
307 if (tparams->check_params)
308 if (tparams->check_params (¶ms))
312 params.ep = params.addr;
313 /* If XIP, entry point must be after the U-Boot header */
315 params.ep += tparams->header_size;
318 params.imagefile = *argv;
321 if (tparams->fflag_handle)
323 * in some cases, some additional processing needs
324 * to be done if fflag is defined
326 * For ex. fit_handle_file for Fit file support
328 retval = tparams->fflag_handle(¶ms);
330 if (retval != EXIT_SUCCESS)
334 if (params.lflag || params.fflag) {
335 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
337 ifd = open (params.imagefile,
338 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
342 fprintf (stderr, "%s: Can't open %s: %s\n",
343 params.cmdname, params.imagefile,
348 if (params.lflag || params.fflag) {
350 * list header information of existing image
352 if (fstat(ifd, &sbuf) < 0) {
353 fprintf (stderr, "%s: Can't stat %s: %s\n",
354 params.cmdname, params.imagefile,
359 if ((unsigned)sbuf.st_size < tparams->header_size) {
361 "%s: Bad size: \"%s\" is not valid image\n",
362 params.cmdname, params.imagefile);
366 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
367 if (ptr == MAP_FAILED) {
368 fprintf (stderr, "%s: Can't read %s: %s\n",
369 params.cmdname, params.imagefile,
375 * scan through mkimage registry for all supported image types
376 * and verify the input image file header for match
377 * Print the image information for matched image type
378 * Returns the error code if not matched
380 retval = mkimage_verify_print_header (ptr, &sbuf);
382 (void) munmap((void *)ptr, sbuf.st_size);
389 * In case there an header with a variable
390 * length will be added, the corresponding
391 * function is called. This is responsible to
392 * allocate memory for the header itself.
394 if (tparams->vrec_header)
395 pad_len = tparams->vrec_header(¶ms, tparams);
397 memset(tparams->hdr, 0, tparams->header_size);
399 if (write(ifd, tparams->hdr, tparams->header_size)
400 != tparams->header_size) {
401 fprintf (stderr, "%s: Write error on %s: %s\n",
402 params.cmdname, params.imagefile, strerror(errno));
406 if (!params.skipcpy) {
407 if (params.type == IH_TYPE_MULTI ||
408 params.type == IH_TYPE_SCRIPT) {
409 char *file = params.datafile;
416 if ((sep = strchr(file, ':')) != NULL) {
420 if (stat (file, &sbuf) < 0) {
421 fprintf (stderr, "%s: Can't stat %s: %s\n",
422 params.cmdname, file, strerror(errno));
425 size = cpu_to_uimage (sbuf.st_size);
430 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
431 fprintf (stderr, "%s: Write error on %s: %s\n",
432 params.cmdname, params.imagefile,
449 file = params.datafile;
452 char *sep = strchr(file, ':');
455 copy_file (ifd, file, 1);
459 copy_file (ifd, file, 0);
463 } else if (params.type == IH_TYPE_PBLIMAGE) {
464 /* PBL has special Image format, implements its' own */
465 pbl_load_uboot(ifd, ¶ms);
467 copy_file(ifd, params.datafile, pad_len);
471 /* We're a bit of paranoid */
472 #if defined(_POSIX_SYNCHRONIZED_IO) && \
473 !defined(__sun__) && \
474 !defined(__FreeBSD__) && \
476 (void) fdatasync (ifd);
481 if (fstat(ifd, &sbuf) < 0) {
482 fprintf (stderr, "%s: Can't stat %s: %s\n",
483 params.cmdname, params.imagefile, strerror(errno));
487 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
488 if (ptr == MAP_FAILED) {
489 fprintf (stderr, "%s: Can't map %s: %s\n",
490 params.cmdname, params.imagefile, strerror(errno));
494 /* Setup the image header as per input image type*/
495 if (tparams->set_header)
496 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
498 fprintf (stderr, "%s: Can't set header for %s: %s\n",
499 params.cmdname, tparams->name, strerror(errno));
503 /* Print the image information by processing image header */
504 if (tparams->print_header)
505 tparams->print_header (ptr);
507 fprintf (stderr, "%s: Can't print header for %s: %s\n",
508 params.cmdname, tparams->name, strerror(errno));
512 (void) munmap((void *)ptr, sbuf.st_size);
514 /* We're a bit of paranoid */
515 #if defined(_POSIX_SYNCHRONIZED_IO) && \
516 !defined(__sun__) && \
517 !defined(__FreeBSD__) && \
519 (void) fdatasync (ifd);
525 fprintf (stderr, "%s: Write error on %s: %s\n",
526 params.cmdname, params.imagefile, strerror(errno));
534 copy_file (int ifd, const char *datafile, int pad)
544 struct image_type_params *tparams = mkimage_get_type (params.type);
546 if (pad >= sizeof(zeros)) {
547 fprintf(stderr, "%s: Can't pad to %d\n",
548 params.cmdname, pad);
552 memset(zeros, 0, sizeof(zeros));
555 fprintf (stderr, "Adding Image %s\n", datafile);
558 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
559 fprintf (stderr, "%s: Can't open %s: %s\n",
560 params.cmdname, datafile, strerror(errno));
564 if (fstat(dfd, &sbuf) < 0) {
565 fprintf (stderr, "%s: Can't stat %s: %s\n",
566 params.cmdname, datafile, strerror(errno));
570 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
571 if (ptr == MAP_FAILED) {
572 fprintf (stderr, "%s: Can't read %s: %s\n",
573 params.cmdname, datafile, strerror(errno));
578 unsigned char *p = NULL;
580 * XIP: do not append the image_header_t at the
581 * beginning of the file, but consume the space
585 if ((unsigned)sbuf.st_size < tparams->header_size) {
587 "%s: Bad size: \"%s\" is too small for XIP\n",
588 params.cmdname, datafile);
592 for (p = ptr; p < ptr + tparams->header_size; p++) {
595 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
596 params.cmdname, datafile);
601 offset = tparams->header_size;
604 size = sbuf.st_size - offset;
605 if (write(ifd, ptr + offset, size) != size) {
606 fprintf (stderr, "%s: Write error on %s: %s\n",
607 params.cmdname, params.imagefile, strerror(errno));
612 if ((pad == 1) && (tail != 0)) {
614 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
615 fprintf (stderr, "%s: Write error on %s: %s\n",
616 params.cmdname, params.imagefile,
620 } else if (pad > 1) {
621 if (write(ifd, (char *)&zeros, pad) != pad) {
622 fprintf(stderr, "%s: Write error on %s: %s\n",
623 params.cmdname, params.imagefile,
629 (void) munmap((void *)ptr, sbuf.st_size);
636 fprintf (stderr, "Usage: %s -l image\n"
637 " -l ==> list image header information\n",
639 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
640 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
641 " -A ==> set architecture to 'arch'\n"
642 " -O ==> set operating system to 'os'\n"
643 " -T ==> set image type to 'type'\n"
644 " -C ==> set compression type 'comp'\n"
645 " -a ==> set load address to 'addr' (hex)\n"
646 " -e ==> set entry point to 'ep' (hex)\n"
647 " -n ==> set image name to 'name'\n"
648 " -d ==> use image data from 'datafile'\n"
649 " -x ==> set XIP (execute in place)\n",
651 fprintf(stderr, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
653 fprintf(stderr, " -D => set options for device tree compiler\n"
654 " -f => input filename for FIT source\n");
655 #ifdef CONFIG_FIT_SIGNATURE
656 fprintf(stderr, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
657 " -k => set directory containing private keys\n"
658 " -K => write public keys to this .dtb file\n"
659 " -c => add comment in signature node\n"
660 " -F => re-sign existing FIT image\n"
661 " -r => mark keys used as 'required' in dtb\n");
663 fprintf(stderr, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
665 fprintf (stderr, " %s -V ==> print version information and exit\n",