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,
27 static void copy_file(int, const char *, int);
28 static void usage(void);
30 /* image_type_params link list to maintain registered image type supports */
31 struct image_type_params
*mkimage_tparams
= NULL
;
33 /* parameters initialized by core will be used by the image type code */
34 struct mkimage_params params
= {
37 .type
= IH_TYPE_KERNEL
,
39 .dtc
= MKIMAGE_DEFAULT_DTC_OPTIONS
,
45 * It is used to register respective image generation/list support to the
48 * the input struct image_type_params is checked and appended to the link
49 * list, if the input structure is already registered, error
51 void mkimage_register (struct image_type_params
*tparams
)
53 struct image_type_params
**tp
;
56 fprintf (stderr
, "%s: %s: Null input\n",
57 params
.cmdname
, __FUNCTION__
);
61 /* scan the linked list, check for registry and point the last one */
62 for (tp
= &mkimage_tparams
; *tp
!= NULL
; tp
= &(*tp
)->next
) {
63 if (!strcmp((*tp
)->name
, tparams
->name
)) {
64 fprintf (stderr
, "%s: %s already registered\n",
65 params
.cmdname
, tparams
->name
);
70 /* add input struct entry at the end of link list */
72 /* mark input entry as last entry in the link list */
75 debug ("Registered %s\n", tparams
->name
);
81 * It scans all registers image type supports
82 * checks the input type_id for each supported image type
85 * returns respective image_type_params pointer if success
86 * if input type_id is not supported by any of image_type_support
89 struct image_type_params
*mkimage_get_type(int type
)
91 struct image_type_params
*curr
;
93 for (curr
= mkimage_tparams
; curr
!= NULL
; curr
= curr
->next
) {
94 if (curr
->check_image_type
) {
95 if (!curr
->check_image_type (type
))
103 * mkimage_verify_print_header -
105 * It scans mkimage_tparams link list,
106 * verifies image_header for each supported image type
107 * if verification is successful, prints respective header
109 * returns negative if input image format does not match with any of
110 * supported image types
112 int mkimage_verify_print_header (void *ptr
, struct stat
*sbuf
)
115 struct image_type_params
*curr
;
117 for (curr
= mkimage_tparams
; curr
!= NULL
; curr
= curr
->next
) {
118 if (curr
->verify_header
) {
119 retval
= curr
->verify_header (
120 (unsigned char *)ptr
, sbuf
->st_size
,
125 * Print the image information
126 * if verify is successful
128 if (curr
->print_header
)
129 curr
->print_header (ptr
);
132 "%s: print_header undefined for %s\n",
133 params
.cmdname
, curr
->name
);
143 main (int argc
, char **argv
)
149 struct image_type_params
*tparams
= NULL
;
151 /* Init Kirkwood Boot image generation/list support */
152 init_kwb_image_type ();
153 /* Init FIT image generation/list support */
154 init_fit_image_type ();
155 /* Init Default image generation/list support */
156 init_default_image_type ();
158 params
.cmdname
= *argv
;
159 params
.addr
= params
.ep
= 0;
161 while (--argc
> 0 && **++argv
== '-') {
170 genimg_get_arch_id (*++argv
)) < 0)
176 genimg_get_comp_id (*++argv
)) < 0)
182 params
.dtc
= *++argv
;
188 genimg_get_os_id (*++argv
)) < 0)
194 genimg_get_type_id (*++argv
)) < 0)
201 params
.addr
= strtoul (*++argv
,
205 "%s: invalid load address %s\n",
206 params
.cmdname
, *argv
);
213 params
.datafile
= *++argv
;
219 params
.ep
= strtoul (*++argv
,
223 "%s: invalid entry point %s\n",
224 params
.cmdname
, *argv
);
232 params
.type
= IH_TYPE_FLATDT
;
233 params
.datafile
= *++argv
;
237 * The flattened image tree (FIT) format
238 * requires a flattened device tree image type
240 params
.type
= IH_TYPE_FLATDT
;
245 params
.imagename
= *++argv
;
263 /* set tparams as per input type_id */
264 tparams
= mkimage_get_type(params
.type
);
265 if (tparams
== NULL
) {
266 fprintf (stderr
, "%s: unsupported type %s\n",
267 params
.cmdname
, genimg_get_type_name(params
.type
));
272 * check the passed arguments parameters meets the requirements
273 * as per image type to be generated/listed
275 if (tparams
->check_params
)
276 if (tparams
->check_params (¶ms
))
280 params
.ep
= params
.addr
;
281 /* If XIP, entry point must be after the U-Boot header */
283 params
.ep
+= tparams
->header_size
;
287 * If XIP, ensure the entry point is equal to the load address plus
288 * the size of the U-Boot header.
291 if (params
.ep
!= params
.addr
+ tparams
->header_size
) {
293 "%s: For XIP, the entry point must be the load addr + %lu\n",
295 (unsigned long)tparams
->header_size
);
300 params
.imagefile
= *argv
;
303 if (tparams
->fflag_handle
)
305 * in some cases, some additional processing needs
306 * to be done if fflag is defined
308 * For ex. fit_handle_file for Fit file support
310 retval
= tparams
->fflag_handle(¶ms
);
312 if (retval
!= EXIT_SUCCESS
)
316 if (params
.lflag
|| params
.fflag
) {
317 ifd
= open (params
.imagefile
, O_RDONLY
|O_BINARY
);
319 ifd
= open (params
.imagefile
,
320 O_RDWR
|O_CREAT
|O_TRUNC
|O_BINARY
, 0666);
324 fprintf (stderr
, "%s: Can't open %s: %s\n",
325 params
.cmdname
, params
.imagefile
,
330 if (params
.lflag
|| params
.fflag
) {
332 * list header information of existing image
334 if (fstat(ifd
, &sbuf
) < 0) {
335 fprintf (stderr
, "%s: Can't stat %s: %s\n",
336 params
.cmdname
, params
.imagefile
,
341 if ((unsigned)sbuf
.st_size
< tparams
->header_size
) {
343 "%s: Bad size: \"%s\" is not valid image\n",
344 params
.cmdname
, params
.imagefile
);
348 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, ifd
, 0);
349 if (ptr
== MAP_FAILED
) {
350 fprintf (stderr
, "%s: Can't read %s: %s\n",
351 params
.cmdname
, params
.imagefile
,
357 * scan through mkimage registry for all supported image types
358 * and verify the input image file header for match
359 * Print the image information for matched image type
360 * Returns the error code if not matched
362 retval
= mkimage_verify_print_header (ptr
, &sbuf
);
364 (void) munmap((void *)ptr
, sbuf
.st_size
);
373 * write dummy header, to be fixed later
375 memset (tparams
->hdr
, 0, tparams
->header_size
);
377 if (write(ifd
, tparams
->hdr
, tparams
->header_size
)
378 != tparams
->header_size
) {
379 fprintf (stderr
, "%s: Write error on %s: %s\n",
380 params
.cmdname
, params
.imagefile
, strerror(errno
));
384 if (params
.type
== IH_TYPE_MULTI
|| params
.type
== IH_TYPE_SCRIPT
) {
385 char *file
= params
.datafile
;
392 if ((sep
= strchr(file
, ':')) != NULL
) {
396 if (stat (file
, &sbuf
) < 0) {
397 fprintf (stderr
, "%s: Can't stat %s: %s\n",
398 params
.cmdname
, file
, strerror(errno
));
401 size
= cpu_to_uimage (sbuf
.st_size
);
406 if (write(ifd
, (char *)&size
, sizeof(size
)) != sizeof(size
)) {
407 fprintf (stderr
, "%s: Write error on %s: %s\n",
408 params
.cmdname
, params
.imagefile
,
425 file
= params
.datafile
;
428 char *sep
= strchr(file
, ':');
431 copy_file (ifd
, file
, 1);
435 copy_file (ifd
, file
, 0);
440 copy_file (ifd
, params
.datafile
, 0);
443 /* We're a bit of paranoid */
444 #if defined(_POSIX_SYNCHRONIZED_IO) && \
445 !defined(__sun__) && \
446 !defined(__FreeBSD__) && \
448 (void) fdatasync (ifd
);
453 if (fstat(ifd
, &sbuf
) < 0) {
454 fprintf (stderr
, "%s: Can't stat %s: %s\n",
455 params
.cmdname
, params
.imagefile
, strerror(errno
));
459 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, ifd
, 0);
460 if (ptr
== MAP_FAILED
) {
461 fprintf (stderr
, "%s: Can't map %s: %s\n",
462 params
.cmdname
, params
.imagefile
, strerror(errno
));
466 /* Setup the image header as per input image type*/
467 if (tparams
->set_header
)
468 tparams
->set_header (ptr
, &sbuf
, ifd
, ¶ms
);
470 fprintf (stderr
, "%s: Can't set header for %s: %s\n",
471 params
.cmdname
, tparams
->name
, strerror(errno
));
475 /* Print the image information by processing image header */
476 if (tparams
->print_header
)
477 tparams
->print_header (ptr
);
479 fprintf (stderr
, "%s: Can't print header for %s: %s\n",
480 params
.cmdname
, tparams
->name
, strerror(errno
));
484 (void) munmap((void *)ptr
, sbuf
.st_size
);
486 /* We're a bit of paranoid */
487 #if defined(_POSIX_SYNCHRONIZED_IO) && \
488 !defined(__sun__) && \
489 !defined(__FreeBSD__) && \
491 (void) fdatasync (ifd
);
497 fprintf (stderr
, "%s: Write error on %s: %s\n",
498 params
.cmdname
, params
.imagefile
, strerror(errno
));
506 copy_file (int ifd
, const char *datafile
, int pad
)
515 struct image_type_params
*tparams
= mkimage_get_type (params
.type
);
518 fprintf (stderr
, "Adding Image %s\n", datafile
);
521 if ((dfd
= open(datafile
, O_RDONLY
|O_BINARY
)) < 0) {
522 fprintf (stderr
, "%s: Can't open %s: %s\n",
523 params
.cmdname
, datafile
, strerror(errno
));
527 if (fstat(dfd
, &sbuf
) < 0) {
528 fprintf (stderr
, "%s: Can't stat %s: %s\n",
529 params
.cmdname
, datafile
, strerror(errno
));
533 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, dfd
, 0);
534 if (ptr
== MAP_FAILED
) {
535 fprintf (stderr
, "%s: Can't read %s: %s\n",
536 params
.cmdname
, datafile
, strerror(errno
));
541 unsigned char *p
= NULL
;
543 * XIP: do not append the image_header_t at the
544 * beginning of the file, but consume the space
548 if ((unsigned)sbuf
.st_size
< tparams
->header_size
) {
550 "%s: Bad size: \"%s\" is too small for XIP\n",
551 params
.cmdname
, datafile
);
555 for (p
= ptr
; p
< ptr
+ tparams
->header_size
; p
++) {
558 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
559 params
.cmdname
, datafile
);
564 offset
= tparams
->header_size
;
567 size
= sbuf
.st_size
- offset
;
568 if (write(ifd
, ptr
+ offset
, size
) != size
) {
569 fprintf (stderr
, "%s: Write error on %s: %s\n",
570 params
.cmdname
, params
.imagefile
, strerror(errno
));
574 if (pad
&& ((tail
= size
% 4) != 0)) {
576 if (write(ifd
, (char *)&zero
, 4-tail
) != 4-tail
) {
577 fprintf (stderr
, "%s: Write error on %s: %s\n",
578 params
.cmdname
, params
.imagefile
,
584 (void) munmap((void *)ptr
, sbuf
.st_size
);
591 fprintf (stderr
, "Usage: %s -l image\n"
592 " -l ==> list image header information\n",
594 fprintf (stderr
, " %s [-x] -A arch -O os -T type -C comp "
595 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
596 " -A ==> set architecture to 'arch'\n"
597 " -O ==> set operating system to 'os'\n"
598 " -T ==> set image type to 'type'\n"
599 " -C ==> set compression type 'comp'\n"
600 " -a ==> set load address to 'addr' (hex)\n"
601 " -e ==> set entry point to 'ep' (hex)\n"
602 " -n ==> set image name to 'name'\n"
603 " -d ==> use image data from 'datafile'\n"
604 " -x ==> set XIP (execute in place)\n",
606 fprintf (stderr
, " %s [-D dtc_options] -f fit-image.its fit-image\n",