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
,
47 * It is used to register respective image generation/list support to the
50 * the input struct image_type_params is checked and appended to the link
51 * list, if the input structure is already registered, error
53 void mkimage_register (struct image_type_params
*tparams
)
55 struct image_type_params
**tp
;
58 fprintf (stderr
, "%s: %s: Null input\n",
59 params
.cmdname
, __FUNCTION__
);
63 /* scan the linked list, check for registry and point the last one */
64 for (tp
= &mkimage_tparams
; *tp
!= NULL
; tp
= &(*tp
)->next
) {
65 if (!strcmp((*tp
)->name
, tparams
->name
)) {
66 fprintf (stderr
, "%s: %s already registered\n",
67 params
.cmdname
, tparams
->name
);
72 /* add input struct entry at the end of link list */
74 /* mark input entry as last entry in the link list */
77 debug ("Registered %s\n", tparams
->name
);
83 * It scans all registers image type supports
84 * checks the input type_id for each supported image type
87 * returns respective image_type_params pointer if success
88 * if input type_id is not supported by any of image_type_support
91 struct image_type_params
*mkimage_get_type(int type
)
93 struct image_type_params
*curr
;
95 for (curr
= mkimage_tparams
; curr
!= NULL
; curr
= curr
->next
) {
96 if (curr
->check_image_type
) {
97 if (!curr
->check_image_type (type
))
105 * mkimage_verify_print_header -
107 * It scans mkimage_tparams link list,
108 * verifies image_header for each supported image type
109 * if verification is successful, prints respective header
111 * returns negative if input image format does not match with any of
112 * supported image types
114 int mkimage_verify_print_header (void *ptr
, struct stat
*sbuf
)
117 struct image_type_params
*curr
;
119 for (curr
= mkimage_tparams
; curr
!= NULL
; curr
= curr
->next
) {
120 if (curr
->verify_header
) {
121 retval
= curr
->verify_header (
122 (unsigned char *)ptr
, sbuf
->st_size
,
127 * Print the image information
128 * if verify is successful
130 if (curr
->print_header
)
131 curr
->print_header (ptr
);
134 "%s: print_header undefined for %s\n",
135 params
.cmdname
, curr
->name
);
145 main (int argc
, char **argv
)
151 struct image_type_params
*tparams
= NULL
;
153 /* Init Kirkwood Boot image generation/list support */
154 init_kwb_image_type ();
155 /* Init Freescale imx Boot image generation/list support */
156 init_imx_image_type ();
157 /* Init FIT image generation/list support */
158 init_fit_image_type ();
159 /* Init Default image generation/list support */
160 init_default_image_type ();
161 /* Init Davinci UBL support */
162 init_ubl_image_type();
164 params
.cmdname
= *argv
;
165 params
.addr
= params
.ep
= 0;
167 while (--argc
> 0 && **++argv
== '-') {
176 genimg_get_arch_id (*++argv
)) < 0)
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
);
237 * The flattened image tree (FIT) format
238 * requires a flattened device tree image type
240 params
.type
= IH_TYPE_FLATDT
;
241 params
.datafile
= *++argv
;
247 params
.imagename
= *++argv
;
253 printf("mkimage version %s\n", PLAIN_VERSION
);
268 /* set tparams as per input type_id */
269 tparams
= mkimage_get_type(params
.type
);
270 if (tparams
== NULL
) {
271 fprintf (stderr
, "%s: unsupported type %s\n",
272 params
.cmdname
, genimg_get_type_name(params
.type
));
277 * check the passed arguments parameters meets the requirements
278 * as per image type to be generated/listed
280 if (tparams
->check_params
)
281 if (tparams
->check_params (¶ms
))
285 params
.ep
= params
.addr
;
286 /* If XIP, entry point must be after the U-Boot header */
288 params
.ep
+= tparams
->header_size
;
291 params
.imagefile
= *argv
;
294 if (tparams
->fflag_handle
)
296 * in some cases, some additional processing needs
297 * to be done if fflag is defined
299 * For ex. fit_handle_file for Fit file support
301 retval
= tparams
->fflag_handle(¶ms
);
303 if (retval
!= EXIT_SUCCESS
)
307 if (params
.lflag
|| params
.fflag
) {
308 ifd
= open (params
.imagefile
, O_RDONLY
|O_BINARY
);
310 ifd
= open (params
.imagefile
,
311 O_RDWR
|O_CREAT
|O_TRUNC
|O_BINARY
, 0666);
315 fprintf (stderr
, "%s: Can't open %s: %s\n",
316 params
.cmdname
, params
.imagefile
,
321 if (params
.lflag
|| params
.fflag
) {
323 * list header information of existing image
325 if (fstat(ifd
, &sbuf
) < 0) {
326 fprintf (stderr
, "%s: Can't stat %s: %s\n",
327 params
.cmdname
, params
.imagefile
,
332 if ((unsigned)sbuf
.st_size
< tparams
->header_size
) {
334 "%s: Bad size: \"%s\" is not valid image\n",
335 params
.cmdname
, params
.imagefile
);
339 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, ifd
, 0);
340 if (ptr
== MAP_FAILED
) {
341 fprintf (stderr
, "%s: Can't read %s: %s\n",
342 params
.cmdname
, params
.imagefile
,
348 * scan through mkimage registry for all supported image types
349 * and verify the input image file header for match
350 * Print the image information for matched image type
351 * Returns the error code if not matched
353 retval
= mkimage_verify_print_header (ptr
, &sbuf
);
355 (void) munmap((void *)ptr
, sbuf
.st_size
);
364 * write dummy header, to be fixed later
366 memset (tparams
->hdr
, 0, tparams
->header_size
);
368 if (write(ifd
, tparams
->hdr
, tparams
->header_size
)
369 != tparams
->header_size
) {
370 fprintf (stderr
, "%s: Write error on %s: %s\n",
371 params
.cmdname
, params
.imagefile
, strerror(errno
));
375 if (params
.type
== IH_TYPE_MULTI
|| params
.type
== IH_TYPE_SCRIPT
) {
376 char *file
= params
.datafile
;
383 if ((sep
= strchr(file
, ':')) != NULL
) {
387 if (stat (file
, &sbuf
) < 0) {
388 fprintf (stderr
, "%s: Can't stat %s: %s\n",
389 params
.cmdname
, file
, strerror(errno
));
392 size
= cpu_to_uimage (sbuf
.st_size
);
397 if (write(ifd
, (char *)&size
, sizeof(size
)) != sizeof(size
)) {
398 fprintf (stderr
, "%s: Write error on %s: %s\n",
399 params
.cmdname
, params
.imagefile
,
416 file
= params
.datafile
;
419 char *sep
= strchr(file
, ':');
422 copy_file (ifd
, file
, 1);
426 copy_file (ifd
, file
, 0);
431 copy_file (ifd
, params
.datafile
, 0);
434 /* We're a bit of paranoid */
435 #if defined(_POSIX_SYNCHRONIZED_IO) && \
436 !defined(__sun__) && \
437 !defined(__FreeBSD__) && \
439 (void) fdatasync (ifd
);
444 if (fstat(ifd
, &sbuf
) < 0) {
445 fprintf (stderr
, "%s: Can't stat %s: %s\n",
446 params
.cmdname
, params
.imagefile
, strerror(errno
));
450 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, ifd
, 0);
451 if (ptr
== MAP_FAILED
) {
452 fprintf (stderr
, "%s: Can't map %s: %s\n",
453 params
.cmdname
, params
.imagefile
, strerror(errno
));
457 /* Setup the image header as per input image type*/
458 if (tparams
->set_header
)
459 tparams
->set_header (ptr
, &sbuf
, ifd
, ¶ms
);
461 fprintf (stderr
, "%s: Can't set header for %s: %s\n",
462 params
.cmdname
, tparams
->name
, strerror(errno
));
466 /* Print the image information by processing image header */
467 if (tparams
->print_header
)
468 tparams
->print_header (ptr
);
470 fprintf (stderr
, "%s: Can't print header for %s: %s\n",
471 params
.cmdname
, tparams
->name
, strerror(errno
));
475 (void) munmap((void *)ptr
, sbuf
.st_size
);
477 /* We're a bit of paranoid */
478 #if defined(_POSIX_SYNCHRONIZED_IO) && \
479 !defined(__sun__) && \
480 !defined(__FreeBSD__) && \
482 (void) fdatasync (ifd
);
488 fprintf (stderr
, "%s: Write error on %s: %s\n",
489 params
.cmdname
, params
.imagefile
, strerror(errno
));
497 copy_file (int ifd
, const char *datafile
, int pad
)
506 struct image_type_params
*tparams
= mkimage_get_type (params
.type
);
509 fprintf (stderr
, "Adding Image %s\n", datafile
);
512 if ((dfd
= open(datafile
, O_RDONLY
|O_BINARY
)) < 0) {
513 fprintf (stderr
, "%s: Can't open %s: %s\n",
514 params
.cmdname
, datafile
, strerror(errno
));
518 if (fstat(dfd
, &sbuf
) < 0) {
519 fprintf (stderr
, "%s: Can't stat %s: %s\n",
520 params
.cmdname
, datafile
, strerror(errno
));
524 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, dfd
, 0);
525 if (ptr
== MAP_FAILED
) {
526 fprintf (stderr
, "%s: Can't read %s: %s\n",
527 params
.cmdname
, datafile
, strerror(errno
));
532 unsigned char *p
= NULL
;
534 * XIP: do not append the image_header_t at the
535 * beginning of the file, but consume the space
539 if ((unsigned)sbuf
.st_size
< tparams
->header_size
) {
541 "%s: Bad size: \"%s\" is too small for XIP\n",
542 params
.cmdname
, datafile
);
546 for (p
= ptr
; p
< ptr
+ tparams
->header_size
; p
++) {
549 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
550 params
.cmdname
, datafile
);
555 offset
= tparams
->header_size
;
558 size
= sbuf
.st_size
- offset
;
559 if (write(ifd
, ptr
+ offset
, size
) != size
) {
560 fprintf (stderr
, "%s: Write error on %s: %s\n",
561 params
.cmdname
, params
.imagefile
, strerror(errno
));
565 if (pad
&& ((tail
= size
% 4) != 0)) {
567 if (write(ifd
, (char *)&zero
, 4-tail
) != 4-tail
) {
568 fprintf (stderr
, "%s: Write error on %s: %s\n",
569 params
.cmdname
, params
.imagefile
,
575 (void) munmap((void *)ptr
, sbuf
.st_size
);
582 fprintf (stderr
, "Usage: %s -l image\n"
583 " -l ==> list image header information\n",
585 fprintf (stderr
, " %s [-x] -A arch -O os -T type -C comp "
586 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
587 " -A ==> set architecture to 'arch'\n"
588 " -O ==> set operating system to 'os'\n"
589 " -T ==> set image type to 'type'\n"
590 " -C ==> set compression type 'comp'\n"
591 " -a ==> set load address to 'addr' (hex)\n"
592 " -e ==> set entry point to 'ep' (hex)\n"
593 " -n ==> set image name to 'name'\n"
594 " -d ==> use image data from 'datafile'\n"
595 " -x ==> set XIP (execute in place)\n",
597 fprintf (stderr
, " %s [-D dtc_options] -f fit-image.its fit-image\n",
599 fprintf (stderr
, " %s -V ==> print version information and exit\n",