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 image_tool_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 all image generation/list support */
143 register_image_tool(mkimage_register
);
145 params
.cmdname
= *argv
;
146 params
.addr
= params
.ep
= 0;
148 while (--argc
> 0 && **++argv
== '-') {
157 genimg_get_arch_id (*++argv
)) < 0)
163 params
.comment
= *++argv
;
168 genimg_get_comp_id (*++argv
)) < 0)
174 params
.dtc
= *++argv
;
180 genimg_get_os_id (*++argv
)) < 0)
186 genimg_get_type_id (*++argv
)) < 0)
193 params
.addr
= strtoul (*++argv
, &ptr
, 16);
196 "%s: invalid load address %s\n",
197 params
.cmdname
, *argv
);
204 params
.datafile
= *++argv
;
210 params
.ep
= strtoul (*++argv
, &ptr
, 16);
213 "%s: invalid entry point %s\n",
214 params
.cmdname
, *argv
);
222 params
.datafile
= *++argv
;
226 * The flattened image tree (FIT) format
227 * requires a flattened device tree image type
229 params
.type
= IH_TYPE_FLATDT
;
235 params
.keydir
= *++argv
;
240 params
.keydest
= *++argv
;
245 params
.imagename
= *++argv
;
248 params
.require_keys
= 1;
254 * This entry is for the second configuration
255 * file, if only one is not enough.
257 params
.imagename2
= *++argv
;
266 printf("mkimage version %s\n", PLAIN_VERSION
);
281 /* set tparams as per input type_id */
282 tparams
= mkimage_get_type(params
.type
);
283 if (tparams
== NULL
) {
284 fprintf (stderr
, "%s: unsupported type %s\n",
285 params
.cmdname
, genimg_get_type_name(params
.type
));
290 * check the passed arguments parameters meets the requirements
291 * as per image type to be generated/listed
293 if (tparams
->check_params
)
294 if (tparams
->check_params (¶ms
))
298 params
.ep
= params
.addr
;
299 /* If XIP, entry point must be after the U-Boot header */
301 params
.ep
+= tparams
->header_size
;
304 params
.imagefile
= *argv
;
307 if (tparams
->fflag_handle
)
309 * in some cases, some additional processing needs
310 * to be done if fflag is defined
312 * For ex. fit_handle_file for Fit file support
314 retval
= tparams
->fflag_handle(¶ms
);
316 if (retval
!= EXIT_SUCCESS
)
320 if (params
.lflag
|| params
.fflag
) {
321 ifd
= open (params
.imagefile
, O_RDONLY
|O_BINARY
);
323 ifd
= open (params
.imagefile
,
324 O_RDWR
|O_CREAT
|O_TRUNC
|O_BINARY
, 0666);
328 fprintf (stderr
, "%s: Can't open %s: %s\n",
329 params
.cmdname
, params
.imagefile
,
334 if (params
.lflag
|| params
.fflag
) {
336 * list header information of existing image
338 if (fstat(ifd
, &sbuf
) < 0) {
339 fprintf (stderr
, "%s: Can't stat %s: %s\n",
340 params
.cmdname
, params
.imagefile
,
345 if ((unsigned)sbuf
.st_size
< tparams
->header_size
) {
347 "%s: Bad size: \"%s\" is not valid image\n",
348 params
.cmdname
, params
.imagefile
);
352 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, ifd
, 0);
353 if (ptr
== MAP_FAILED
) {
354 fprintf (stderr
, "%s: Can't read %s: %s\n",
355 params
.cmdname
, params
.imagefile
,
361 * scan through mkimage registry for all supported image types
362 * and verify the input image file header for match
363 * Print the image information for matched image type
364 * Returns the error code if not matched
366 retval
= mkimage_verify_print_header (ptr
, &sbuf
);
368 (void) munmap((void *)ptr
, sbuf
.st_size
);
375 * In case there an header with a variable
376 * length will be added, the corresponding
377 * function is called. This is responsible to
378 * allocate memory for the header itself.
380 if (tparams
->vrec_header
)
381 pad_len
= tparams
->vrec_header(¶ms
, tparams
);
383 memset(tparams
->hdr
, 0, tparams
->header_size
);
385 if (write(ifd
, tparams
->hdr
, tparams
->header_size
)
386 != tparams
->header_size
) {
387 fprintf (stderr
, "%s: Write error on %s: %s\n",
388 params
.cmdname
, params
.imagefile
, strerror(errno
));
392 if (!params
.skipcpy
) {
393 if (params
.type
== IH_TYPE_MULTI
||
394 params
.type
== IH_TYPE_SCRIPT
) {
395 char *file
= params
.datafile
;
402 if ((sep
= strchr(file
, ':')) != NULL
) {
406 if (stat (file
, &sbuf
) < 0) {
407 fprintf (stderr
, "%s: Can't stat %s: %s\n",
408 params
.cmdname
, file
, strerror(errno
));
411 size
= cpu_to_uimage (sbuf
.st_size
);
416 if (write(ifd
, (char *)&size
, sizeof(size
)) != sizeof(size
)) {
417 fprintf (stderr
, "%s: Write error on %s: %s\n",
418 params
.cmdname
, params
.imagefile
,
435 file
= params
.datafile
;
438 char *sep
= strchr(file
, ':');
441 copy_file (ifd
, file
, 1);
445 copy_file (ifd
, file
, 0);
449 } else if (params
.type
== IH_TYPE_PBLIMAGE
) {
450 /* PBL has special Image format, implements its' own */
451 pbl_load_uboot(ifd
, ¶ms
);
453 copy_file(ifd
, params
.datafile
, pad_len
);
457 /* We're a bit of paranoid */
458 #if defined(_POSIX_SYNCHRONIZED_IO) && \
459 !defined(__sun__) && \
460 !defined(__FreeBSD__) && \
461 !defined(__OpenBSD__) && \
463 (void) fdatasync (ifd
);
468 if (fstat(ifd
, &sbuf
) < 0) {
469 fprintf (stderr
, "%s: Can't stat %s: %s\n",
470 params
.cmdname
, params
.imagefile
, strerror(errno
));
474 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, ifd
, 0);
475 if (ptr
== MAP_FAILED
) {
476 fprintf (stderr
, "%s: Can't map %s: %s\n",
477 params
.cmdname
, params
.imagefile
, strerror(errno
));
481 /* Setup the image header as per input image type*/
482 if (tparams
->set_header
)
483 tparams
->set_header (ptr
, &sbuf
, ifd
, ¶ms
);
485 fprintf (stderr
, "%s: Can't set header for %s: %s\n",
486 params
.cmdname
, tparams
->name
, strerror(errno
));
490 /* Print the image information by processing image header */
491 if (tparams
->print_header
)
492 tparams
->print_header (ptr
);
494 fprintf (stderr
, "%s: Can't print header for %s: %s\n",
495 params
.cmdname
, tparams
->name
, strerror(errno
));
499 (void) munmap((void *)ptr
, sbuf
.st_size
);
501 /* We're a bit of paranoid */
502 #if defined(_POSIX_SYNCHRONIZED_IO) && \
503 !defined(__sun__) && \
504 !defined(__FreeBSD__) && \
505 !defined(__OpenBSD__) && \
507 (void) fdatasync (ifd
);
513 fprintf (stderr
, "%s: Write error on %s: %s\n",
514 params
.cmdname
, params
.imagefile
, strerror(errno
));
522 copy_file (int ifd
, const char *datafile
, int pad
)
532 struct image_type_params
*tparams
= mkimage_get_type (params
.type
);
534 if (pad
>= sizeof(zeros
)) {
535 fprintf(stderr
, "%s: Can't pad to %d\n",
536 params
.cmdname
, pad
);
540 memset(zeros
, 0, sizeof(zeros
));
543 fprintf (stderr
, "Adding Image %s\n", datafile
);
546 if ((dfd
= open(datafile
, O_RDONLY
|O_BINARY
)) < 0) {
547 fprintf (stderr
, "%s: Can't open %s: %s\n",
548 params
.cmdname
, datafile
, strerror(errno
));
552 if (fstat(dfd
, &sbuf
) < 0) {
553 fprintf (stderr
, "%s: Can't stat %s: %s\n",
554 params
.cmdname
, datafile
, strerror(errno
));
558 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, dfd
, 0);
559 if (ptr
== MAP_FAILED
) {
560 fprintf (stderr
, "%s: Can't read %s: %s\n",
561 params
.cmdname
, datafile
, strerror(errno
));
566 unsigned char *p
= NULL
;
568 * XIP: do not append the image_header_t at the
569 * beginning of the file, but consume the space
573 if ((unsigned)sbuf
.st_size
< tparams
->header_size
) {
575 "%s: Bad size: \"%s\" is too small for XIP\n",
576 params
.cmdname
, datafile
);
580 for (p
= ptr
; p
< ptr
+ tparams
->header_size
; p
++) {
583 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
584 params
.cmdname
, datafile
);
589 offset
= tparams
->header_size
;
592 size
= sbuf
.st_size
- offset
;
593 if (write(ifd
, ptr
+ offset
, size
) != size
) {
594 fprintf (stderr
, "%s: Write error on %s: %s\n",
595 params
.cmdname
, params
.imagefile
, strerror(errno
));
600 if ((pad
== 1) && (tail
!= 0)) {
602 if (write(ifd
, (char *)&zero
, 4-tail
) != 4-tail
) {
603 fprintf (stderr
, "%s: Write error on %s: %s\n",
604 params
.cmdname
, params
.imagefile
,
608 } else if (pad
> 1) {
609 if (write(ifd
, (char *)&zeros
, pad
) != pad
) {
610 fprintf(stderr
, "%s: Write error on %s: %s\n",
611 params
.cmdname
, params
.imagefile
,
617 (void) munmap((void *)ptr
, sbuf
.st_size
);
621 static void usage(void)
623 fprintf (stderr
, "Usage: %s -l image\n"
624 " -l ==> list image header information\n",
626 fprintf (stderr
, " %s [-x] -A arch -O os -T type -C comp "
627 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
628 " -A ==> set architecture to 'arch'\n"
629 " -O ==> set operating system to 'os'\n"
630 " -T ==> set image type to 'type'\n"
631 " -C ==> set compression type 'comp'\n"
632 " -a ==> set load address to 'addr' (hex)\n"
633 " -e ==> set entry point to 'ep' (hex)\n"
634 " -n ==> set image name to 'name'\n"
635 " -d ==> use image data from 'datafile'\n"
636 " -x ==> set XIP (execute in place)\n",
638 fprintf(stderr
, " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
640 fprintf(stderr
, " -D => set options for device tree compiler\n"
641 " -f => input filename for FIT source\n");
642 #ifdef CONFIG_FIT_SIGNATURE
643 fprintf(stderr
, "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
644 " -k => set directory containing private keys\n"
645 " -K => write public keys to this .dtb file\n"
646 " -c => add comment in signature node\n"
647 " -F => re-sign existing FIT image\n"
648 " -r => mark keys used as 'required' in dtb\n");
650 fprintf(stderr
, "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
652 fprintf (stderr
, " %s -V ==> print version information and exit\n",