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);
17 /* parameters initialized by core will be used by the image type code */
18 static struct image_tool_params params
= {
21 .type
= IH_TYPE_KERNEL
,
23 .dtc
= MKIMAGE_DEFAULT_DTC_OPTIONS
,
28 static int h_compare_image_name(const void *vtype1
, const void *vtype2
)
30 const int *type1
= vtype1
;
31 const int *type2
= vtype2
;
32 const char *name1
= genimg_get_type_short_name(*type1
);
33 const char *name2
= genimg_get_type_short_name(*type2
);
35 return strcmp(name1
, name2
);
38 /* Show all image types supported by mkimage */
39 static void show_image_types(void)
41 struct image_type_params
*tparams
;
42 int order
[IH_TYPE_COUNT
];
47 /* Sort the names in order of short name for easier reading */
48 memset(order
, '\0', sizeof(order
));
49 for (count
= 0, type
= 0; type
< IH_TYPE_COUNT
; type
++) {
50 tparams
= imagetool_get_type(type
);
52 order
[count
++] = type
;
54 qsort(order
, count
, sizeof(int), h_compare_image_name
);
56 fprintf(stderr
, "\nInvalid image type. Supported image types:\n");
57 for (i
= 0; i
< count
; i
++) {
59 tparams
= imagetool_get_type(type
);
61 fprintf(stderr
, "\t%-15s %s\n",
62 genimg_get_type_short_name(type
),
63 genimg_get_type_name(type
));
66 fprintf(stderr
, "\n");
69 static void usage(const char *msg
)
71 fprintf(stderr
, "Error: %s\n", msg
);
72 fprintf(stderr
, "Usage: %s -l image\n"
73 " -l ==> list image header information\n",
76 " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
77 " -A ==> set architecture to 'arch'\n"
78 " -O ==> set operating system to 'os'\n"
79 " -T ==> set image type to 'type'\n"
80 " -C ==> set compression type 'comp'\n"
81 " -a ==> set load address to 'addr' (hex)\n"
82 " -e ==> set entry point to 'ep' (hex)\n"
83 " -n ==> set image name to 'name'\n"
84 " -d ==> use image data from 'datafile'\n"
85 " -x ==> set XIP (execute in place)\n",
88 " %s [-D dtc_options] [-f fit-image.its|-F] fit-image\n",
91 " -D => set all options for device tree compiler\n"
92 " -f => input filename for FIT source\n");
93 #ifdef CONFIG_FIT_SIGNATURE
95 "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n"
96 " -k => set directory containing private keys\n"
97 " -K => write public keys to this .dtb file\n"
98 " -c => add comment in signature node\n"
99 " -F => re-sign existing FIT image\n"
100 " -r => mark keys used as 'required' in dtb\n");
103 "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n");
105 fprintf(stderr
, " %s -V ==> print version information and exit\n",
107 fprintf(stderr
, "Use -T to see a list of available image types\n");
112 static void process_args(int argc
, char **argv
)
115 int type
= IH_TYPE_INVALID
;
116 char *datafile
= NULL
;
120 expecting
= IH_TYPE_COUNT
; /* Unknown */
121 while ((opt
= getopt(argc
, argv
,
122 "-a:A:cC:d:D:e:f:Fk:K:ln:O:rR:sT:vVx")) != -1) {
125 params
.addr
= strtoull(optarg
, &ptr
, 16);
127 fprintf(stderr
, "%s: invalid load address %s\n",
128 params
.cmdname
, optarg
);
133 params
.arch
= genimg_get_arch_id(optarg
);
135 usage("Invalid architecture");
138 params
.comment
= optarg
;
141 params
.comp
= genimg_get_comp_id(optarg
);
143 usage("Invalid compression type");
146 params
.datafile
= optarg
;
153 params
.ep
= strtoull(optarg
, &ptr
, 16);
155 fprintf(stderr
, "%s: invalid entry point %s\n",
156 params
.cmdname
, optarg
);
162 params
.datafile
= optarg
;
166 * The flattened image tree (FIT) format
167 * requires a flattened device tree image type
169 params
.fit_image_type
= params
.type
;
170 params
.type
= IH_TYPE_FLATDT
;
174 params
.keydir
= optarg
;
177 params
.keydest
= optarg
;
183 params
.imagename
= optarg
;
186 params
.os
= genimg_get_os_id(optarg
);
188 usage("Invalid operating system");
191 params
.require_keys
= 1;
195 * This entry is for the second configuration
196 * file, if only one is not enough.
198 params
.imagename2
= optarg
;
204 type
= genimg_get_type_id(optarg
);
207 usage("Invalid image type");
215 printf("mkimage version %s\n", PLAIN_VERSION
);
221 if (expecting
== type
|| optind
== argc
) {
222 params
.imagefile
= optarg
;
223 expecting
= IH_TYPE_INVALID
;
227 usage("Invalid option");
232 * For auto-generated FIT images we need to know the image type to put
233 * in the FIT, which is separate from the file's image type (which
234 * will always be IH_TYPE_FLATDT in this case).
236 if (params
.type
== IH_TYPE_FLATDT
) {
237 params
.fit_image_type
= type
;
238 params
.datafile
= datafile
;
239 } else if (type
!= IH_TYPE_INVALID
) {
243 if (!params
.imagefile
)
244 usage("Missing output filename");
248 int main(int argc
, char **argv
)
254 struct image_type_params
*tparams
= NULL
;
258 params
.cmdname
= *argv
;
262 process_args(argc
, argv
);
264 /* set tparams as per input type_id */
265 tparams
= imagetool_get_type(params
.type
);
266 if (tparams
== NULL
) {
267 fprintf (stderr
, "%s: unsupported type %s\n",
268 params
.cmdname
, genimg_get_type_name(params
.type
));
273 * check the passed arguments parameters meets the requirements
274 * as per image type to be generated/listed
276 if (tparams
->check_params
)
277 if (tparams
->check_params (¶ms
))
278 usage("Bad parameters for image type");
281 params
.ep
= params
.addr
;
282 /* If XIP, entry point must be after the U-Boot header */
284 params
.ep
+= tparams
->header_size
;
288 if (tparams
->fflag_handle
)
290 * in some cases, some additional processing needs
291 * to be done if fflag is defined
293 * For ex. fit_handle_file for Fit file support
295 retval
= tparams
->fflag_handle(¶ms
);
297 if (retval
!= EXIT_SUCCESS
)
301 if (params
.lflag
|| params
.fflag
) {
302 ifd
= open (params
.imagefile
, O_RDONLY
|O_BINARY
);
304 ifd
= open (params
.imagefile
,
305 O_RDWR
|O_CREAT
|O_TRUNC
|O_BINARY
, 0666);
309 fprintf (stderr
, "%s: Can't open %s: %s\n",
310 params
.cmdname
, params
.imagefile
,
315 if (params
.lflag
|| params
.fflag
) {
317 * list header information of existing image
319 if (fstat(ifd
, &sbuf
) < 0) {
320 fprintf (stderr
, "%s: Can't stat %s: %s\n",
321 params
.cmdname
, params
.imagefile
,
326 if ((unsigned)sbuf
.st_size
< tparams
->header_size
) {
328 "%s: Bad size: \"%s\" is not valid image\n",
329 params
.cmdname
, params
.imagefile
);
333 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, ifd
, 0);
334 if (ptr
== MAP_FAILED
) {
335 fprintf (stderr
, "%s: Can't read %s: %s\n",
336 params
.cmdname
, params
.imagefile
,
342 * scan through mkimage registry for all supported image types
343 * and verify the input image file header for match
344 * Print the image information for matched image type
345 * Returns the error code if not matched
347 retval
= imagetool_verify_print_header(ptr
, &sbuf
,
350 (void) munmap((void *)ptr
, sbuf
.st_size
);
356 if ((params
.type
!= IH_TYPE_MULTI
) && (params
.type
!= IH_TYPE_SCRIPT
)) {
357 dfd
= open(params
.datafile
, O_RDONLY
| O_BINARY
);
359 fprintf(stderr
, "%s: Can't open %s: %s\n",
360 params
.cmdname
, params
.datafile
,
365 if (fstat(dfd
, &sbuf
) < 0) {
366 fprintf(stderr
, "%s: Can't stat %s: %s\n",
367 params
.cmdname
, params
.datafile
,
372 params
.file_size
= sbuf
.st_size
+ tparams
->header_size
;
377 * In case there an header with a variable
378 * length will be added, the corresponding
379 * function is called. This is responsible to
380 * allocate memory for the header itself.
382 if (tparams
->vrec_header
)
383 pad_len
= tparams
->vrec_header(¶ms
, tparams
);
385 memset(tparams
->hdr
, 0, tparams
->header_size
);
387 if (write(ifd
, tparams
->hdr
, tparams
->header_size
)
388 != tparams
->header_size
) {
389 fprintf (stderr
, "%s: Write error on %s: %s\n",
390 params
.cmdname
, params
.imagefile
, strerror(errno
));
394 if (!params
.skipcpy
) {
395 if (params
.type
== IH_TYPE_MULTI
||
396 params
.type
== IH_TYPE_SCRIPT
) {
397 char *file
= params
.datafile
;
404 if ((sep
= strchr(file
, ':')) != NULL
) {
408 if (stat (file
, &sbuf
) < 0) {
409 fprintf (stderr
, "%s: Can't stat %s: %s\n",
410 params
.cmdname
, file
, strerror(errno
));
413 size
= cpu_to_uimage (sbuf
.st_size
);
418 if (write(ifd
, (char *)&size
, sizeof(size
)) != sizeof(size
)) {
419 fprintf (stderr
, "%s: Write error on %s: %s\n",
420 params
.cmdname
, params
.imagefile
,
437 file
= params
.datafile
;
440 char *sep
= strchr(file
, ':');
443 copy_file (ifd
, file
, 1);
447 copy_file (ifd
, file
, 0);
451 } else if (params
.type
== IH_TYPE_PBLIMAGE
) {
452 /* PBL has special Image format, implements its' own */
453 pbl_load_uboot(ifd
, ¶ms
);
455 copy_file(ifd
, params
.datafile
, pad_len
);
459 /* We're a bit of paranoid */
460 #if defined(_POSIX_SYNCHRONIZED_IO) && \
461 !defined(__sun__) && \
462 !defined(__FreeBSD__) && \
463 !defined(__OpenBSD__) && \
465 (void) fdatasync (ifd
);
470 if (fstat(ifd
, &sbuf
) < 0) {
471 fprintf (stderr
, "%s: Can't stat %s: %s\n",
472 params
.cmdname
, params
.imagefile
, strerror(errno
));
475 params
.file_size
= sbuf
.st_size
;
477 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, ifd
, 0);
478 if (ptr
== MAP_FAILED
) {
479 fprintf (stderr
, "%s: Can't map %s: %s\n",
480 params
.cmdname
, params
.imagefile
, strerror(errno
));
484 /* Setup the image header as per input image type*/
485 if (tparams
->set_header
)
486 tparams
->set_header (ptr
, &sbuf
, ifd
, ¶ms
);
488 fprintf (stderr
, "%s: Can't set header for %s: %s\n",
489 params
.cmdname
, tparams
->name
, strerror(errno
));
493 /* Print the image information by processing image header */
494 if (tparams
->print_header
)
495 tparams
->print_header (ptr
);
497 fprintf (stderr
, "%s: Can't print header for %s: %s\n",
498 params
.cmdname
, tparams
->name
, strerror(errno
));
502 (void) munmap((void *)ptr
, sbuf
.st_size
);
504 /* We're a bit of paranoid */
505 #if defined(_POSIX_SYNCHRONIZED_IO) && \
506 !defined(__sun__) && \
507 !defined(__FreeBSD__) && \
508 !defined(__OpenBSD__) && \
510 (void) fdatasync (ifd
);
516 fprintf (stderr
, "%s: Write error on %s: %s\n",
517 params
.cmdname
, params
.imagefile
, strerror(errno
));
525 copy_file (int ifd
, const char *datafile
, int pad
)
535 struct image_type_params
*tparams
= imagetool_get_type(params
.type
);
537 memset(zeros
, 0, sizeof(zeros
));
540 fprintf (stderr
, "Adding Image %s\n", datafile
);
543 if ((dfd
= open(datafile
, O_RDONLY
|O_BINARY
)) < 0) {
544 fprintf (stderr
, "%s: Can't open %s: %s\n",
545 params
.cmdname
, datafile
, strerror(errno
));
549 if (fstat(dfd
, &sbuf
) < 0) {
550 fprintf (stderr
, "%s: Can't stat %s: %s\n",
551 params
.cmdname
, datafile
, strerror(errno
));
555 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, dfd
, 0);
556 if (ptr
== MAP_FAILED
) {
557 fprintf (stderr
, "%s: Can't read %s: %s\n",
558 params
.cmdname
, datafile
, strerror(errno
));
563 unsigned char *p
= NULL
;
565 * XIP: do not append the image_header_t at the
566 * beginning of the file, but consume the space
570 if ((unsigned)sbuf
.st_size
< tparams
->header_size
) {
572 "%s: Bad size: \"%s\" is too small for XIP\n",
573 params
.cmdname
, datafile
);
577 for (p
= ptr
; p
< ptr
+ tparams
->header_size
; p
++) {
580 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
581 params
.cmdname
, datafile
);
586 offset
= tparams
->header_size
;
589 size
= sbuf
.st_size
- offset
;
590 if (write(ifd
, ptr
+ offset
, size
) != size
) {
591 fprintf (stderr
, "%s: Write error on %s: %s\n",
592 params
.cmdname
, params
.imagefile
, strerror(errno
));
597 if ((pad
== 1) && (tail
!= 0)) {
599 if (write(ifd
, (char *)&zero
, 4-tail
) != 4-tail
) {
600 fprintf (stderr
, "%s: Write error on %s: %s\n",
601 params
.cmdname
, params
.imagefile
,
605 } else if (pad
> 1) {
607 int todo
= sizeof(zeros
);
611 if (write(ifd
, (char *)&zeros
, todo
) != todo
) {
612 fprintf(stderr
, "%s: Write error on %s: %s\n",
613 params
.cmdname
, params
.imagefile
,
621 (void) munmap((void *)ptr
, sbuf
.st_size
);