2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2009
5 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 static void copy_file(int, const char *, int);
29 static void usage(void);
31 /* image_type_params link list to maintain registered image type supports */
32 struct image_type_params
*mkimage_tparams
= NULL
;
34 /* parameters initialized by core will be used by the image type code */
35 struct mkimage_params params
= {
38 .type
= IH_TYPE_KERNEL
,
40 .dtc
= MKIMAGE_DEFAULT_DTC_OPTIONS
,
48 * It is used to register respective image generation/list support to the
51 * the input struct image_type_params is checked and appended to the link
52 * list, if the input structure is already registered, error
54 void mkimage_register (struct image_type_params
*tparams
)
56 struct image_type_params
**tp
;
59 fprintf (stderr
, "%s: %s: Null input\n",
60 params
.cmdname
, __FUNCTION__
);
64 /* scan the linked list, check for registry and point the last one */
65 for (tp
= &mkimage_tparams
; *tp
!= NULL
; tp
= &(*tp
)->next
) {
66 if (!strcmp((*tp
)->name
, tparams
->name
)) {
67 fprintf (stderr
, "%s: %s already registered\n",
68 params
.cmdname
, tparams
->name
);
73 /* add input struct entry at the end of link list */
75 /* mark input entry as last entry in the link list */
78 debug ("Registered %s\n", tparams
->name
);
84 * It scans all registers image type supports
85 * checks the input type_id for each supported image type
88 * returns respective image_type_params pointer if success
89 * if input type_id is not supported by any of image_type_support
92 struct image_type_params
*mkimage_get_type(int type
)
94 struct image_type_params
*curr
;
96 for (curr
= mkimage_tparams
; curr
!= NULL
; curr
= curr
->next
) {
97 if (curr
->check_image_type
) {
98 if (!curr
->check_image_type (type
))
106 * mkimage_verify_print_header -
108 * It scans mkimage_tparams link list,
109 * verifies image_header for each supported image type
110 * if verification is successful, prints respective header
112 * returns negative if input image format does not match with any of
113 * supported image types
115 int mkimage_verify_print_header (void *ptr
, struct stat
*sbuf
)
118 struct image_type_params
*curr
;
120 for (curr
= mkimage_tparams
; curr
!= NULL
; curr
= curr
->next
) {
121 if (curr
->verify_header
) {
122 retval
= curr
->verify_header (
123 (unsigned char *)ptr
, sbuf
->st_size
,
128 * Print the image information
129 * if verify is successful
131 if (curr
->print_header
)
132 curr
->print_header (ptr
);
135 "%s: print_header undefined for %s\n",
136 params
.cmdname
, curr
->name
);
146 main (int argc
, char **argv
)
152 struct image_type_params
*tparams
= NULL
;
154 /* Init Freescale PBL Boot image generation/list support */
155 init_pbl_image_type();
156 /* Init Kirkwood Boot image generation/list support */
157 init_kwb_image_type ();
158 /* Init Freescale imx Boot image generation/list support */
159 init_imx_image_type ();
160 /* Init FIT image generation/list support */
161 init_fit_image_type ();
162 /* Init TI OMAP Boot image generation/list support */
163 init_omap_image_type();
164 /* Init Default image generation/list support */
165 init_default_image_type ();
166 /* Init Davinci UBL support */
167 init_ubl_image_type();
168 /* Init Davinci AIS support */
169 init_ais_image_type();
171 params
.cmdname
= *argv
;
172 params
.addr
= params
.ep
= 0;
174 while (--argc
> 0 && **++argv
== '-') {
183 genimg_get_arch_id (*++argv
)) < 0)
189 genimg_get_comp_id (*++argv
)) < 0)
195 params
.dtc
= *++argv
;
201 genimg_get_os_id (*++argv
)) < 0)
207 genimg_get_type_id (*++argv
)) < 0)
214 params
.addr
= strtoul (*++argv
, &ptr
, 16);
217 "%s: invalid load address %s\n",
218 params
.cmdname
, *argv
);
225 params
.datafile
= *++argv
;
231 params
.ep
= strtoul (*++argv
, &ptr
, 16);
234 "%s: invalid entry point %s\n",
235 params
.cmdname
, *argv
);
244 * The flattened image tree (FIT) format
245 * requires a flattened device tree image type
247 params
.type
= IH_TYPE_FLATDT
;
248 params
.datafile
= *++argv
;
254 params
.keydir
= *++argv
;
259 params
.keydest
= *++argv
;
264 params
.imagename
= *++argv
;
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 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
, 0);
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
)
545 struct image_type_params
*tparams
= mkimage_get_type (params
.type
);
548 fprintf (stderr
, "Adding Image %s\n", datafile
);
551 if ((dfd
= open(datafile
, O_RDONLY
|O_BINARY
)) < 0) {
552 fprintf (stderr
, "%s: Can't open %s: %s\n",
553 params
.cmdname
, datafile
, strerror(errno
));
557 if (fstat(dfd
, &sbuf
) < 0) {
558 fprintf (stderr
, "%s: Can't stat %s: %s\n",
559 params
.cmdname
, datafile
, strerror(errno
));
563 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, dfd
, 0);
564 if (ptr
== MAP_FAILED
) {
565 fprintf (stderr
, "%s: Can't read %s: %s\n",
566 params
.cmdname
, datafile
, strerror(errno
));
571 unsigned char *p
= NULL
;
573 * XIP: do not append the image_header_t at the
574 * beginning of the file, but consume the space
578 if ((unsigned)sbuf
.st_size
< tparams
->header_size
) {
580 "%s: Bad size: \"%s\" is too small for XIP\n",
581 params
.cmdname
, datafile
);
585 for (p
= ptr
; p
< ptr
+ tparams
->header_size
; p
++) {
588 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
589 params
.cmdname
, datafile
);
594 offset
= tparams
->header_size
;
597 size
= sbuf
.st_size
- offset
;
598 if (write(ifd
, ptr
+ offset
, size
) != size
) {
599 fprintf (stderr
, "%s: Write error on %s: %s\n",
600 params
.cmdname
, params
.imagefile
, strerror(errno
));
604 if (pad
&& ((tail
= size
% 4) != 0)) {
606 if (write(ifd
, (char *)&zero
, 4-tail
) != 4-tail
) {
607 fprintf (stderr
, "%s: Write error on %s: %s\n",
608 params
.cmdname
, params
.imagefile
,
614 (void) munmap((void *)ptr
, sbuf
.st_size
);
621 fprintf (stderr
, "Usage: %s -l image\n"
622 " -l ==> list image header information\n",
624 fprintf (stderr
, " %s [-x] -A arch -O os -T type -C comp "
625 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
626 " -A ==> set architecture to 'arch'\n"
627 " -O ==> set operating system to 'os'\n"
628 " -T ==> set image type to 'type'\n"
629 " -C ==> set compression type 'comp'\n"
630 " -a ==> set load address to 'addr' (hex)\n"
631 " -e ==> set entry point to 'ep' (hex)\n"
632 " -n ==> set image name to 'name'\n"
633 " -d ==> use image data from 'datafile'\n"
634 " -x ==> set XIP (execute in place)\n",
636 fprintf(stderr
, " %s [-D dtc_options] -f fit-image.its fit-image\n",
638 fprintf(stderr
, " -D => set options for device tree compiler\n"
639 " -f => input filename for FIT source\n");
640 #ifdef CONFIG_FIT_SIGNATURE
641 fprintf(stderr
, "Signing / verified boot options: [-k keydir] [-K dtb]\n"
642 " -k => set directory containing private keys\n"
643 " -K => write public keys to this .dtb file\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",