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,
46 * It is used to register respective image generation/list support to the
49 * the input struct image_type_params is checked and appended to the link
50 * list, if the input structure is already registered, error
52 void mkimage_register (struct image_type_params *tparams)
54 struct image_type_params **tp;
57 fprintf (stderr, "%s: %s: Null input\n",
58 params.cmdname, __FUNCTION__);
62 /* scan the linked list, check for registry and point the last one */
63 for (tp = &mkimage_tparams; *tp != NULL; tp = &(*tp)->next) {
64 if (!strcmp((*tp)->name, tparams->name)) {
65 fprintf (stderr, "%s: %s already registered\n",
66 params.cmdname, tparams->name);
71 /* add input struct entry at the end of link list */
73 /* mark input entry as last entry in the link list */
76 debug ("Registered %s\n", tparams->name);
82 * It scans all registers image type supports
83 * checks the input type_id for each supported image type
86 * returns respective image_type_params pointer if success
87 * if input type_id is not supported by any of image_type_support
90 struct image_type_params *mkimage_get_type(int type)
92 struct image_type_params *curr;
94 for (curr = mkimage_tparams; curr != NULL; curr = curr->next) {
95 if (curr->check_image_type) {
96 if (!curr->check_image_type (type))
104 * mkimage_verify_print_header -
106 * It scans mkimage_tparams link list,
107 * verifies image_header for each supported image type
108 * if verification is successful, prints respective header
110 * returns negative if input image format does not match with any of
111 * supported image types
113 int mkimage_verify_print_header (void *ptr, struct stat *sbuf)
116 struct image_type_params *curr;
118 for (curr = mkimage_tparams; curr != NULL; curr = curr->next ) {
119 if (curr->verify_header) {
120 retval = curr->verify_header (
121 (unsigned char *)ptr, sbuf->st_size,
126 * Print the image information
127 * if verify is successful
129 if (curr->print_header)
130 curr->print_header (ptr);
133 "%s: print_header undefined for %s\n",
134 params.cmdname, curr->name);
144 main (int argc, char **argv)
150 struct image_type_params *tparams = NULL;
152 /* Init Kirkwood Boot image generation/list support */
153 init_kwb_image_type ();
154 /* Init Freescale imx Boot image generation/list support */
155 init_imx_image_type ();
156 /* Init FIT image generation/list support */
157 init_fit_image_type ();
158 /* Init Default image generation/list support */
159 init_default_image_type ();
161 params.cmdname = *argv;
162 params.addr = params.ep = 0;
164 while (--argc > 0 && **++argv == '-') {
173 genimg_get_arch_id (*++argv)) < 0)
179 genimg_get_comp_id (*++argv)) < 0)
185 params.dtc = *++argv;
191 genimg_get_os_id (*++argv)) < 0)
197 genimg_get_type_id (*++argv)) < 0)
204 params.addr = strtoul (*++argv, &ptr, 16);
207 "%s: invalid load address %s\n",
208 params.cmdname, *argv);
215 params.datafile = *++argv;
221 params.ep = strtoul (*++argv, &ptr, 16);
224 "%s: invalid entry point %s\n",
225 params.cmdname, *argv);
234 * The flattened image tree (FIT) format
235 * requires a flattened device tree image type
237 params.type = IH_TYPE_FLATDT;
238 params.datafile = *++argv;
244 params.imagename = *++argv;
262 /* set tparams as per input type_id */
263 tparams = mkimage_get_type(params.type);
264 if (tparams == NULL) {
265 fprintf (stderr, "%s: unsupported type %s\n",
266 params.cmdname, genimg_get_type_name(params.type));
271 * check the passed arguments parameters meets the requirements
272 * as per image type to be generated/listed
274 if (tparams->check_params)
275 if (tparams->check_params (¶ms))
279 params.ep = params.addr;
280 /* If XIP, entry point must be after the U-Boot header */
282 params.ep += tparams->header_size;
285 params.imagefile = *argv;
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 = mkimage_verify_print_header (ptr, &sbuf);
349 (void) munmap((void *)ptr, sbuf.st_size);
358 * write dummy header, to be fixed later
360 memset (tparams->hdr, 0, tparams->header_size);
362 if (write(ifd, tparams->hdr, tparams->header_size)
363 != tparams->header_size) {
364 fprintf (stderr, "%s: Write error on %s: %s\n",
365 params.cmdname, params.imagefile, strerror(errno));
369 if (params.type == IH_TYPE_MULTI || params.type == IH_TYPE_SCRIPT) {
370 char *file = params.datafile;
377 if ((sep = strchr(file, ':')) != NULL) {
381 if (stat (file, &sbuf) < 0) {
382 fprintf (stderr, "%s: Can't stat %s: %s\n",
383 params.cmdname, file, strerror(errno));
386 size = cpu_to_uimage (sbuf.st_size);
391 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
392 fprintf (stderr, "%s: Write error on %s: %s\n",
393 params.cmdname, params.imagefile,
410 file = params.datafile;
413 char *sep = strchr(file, ':');
416 copy_file (ifd, file, 1);
420 copy_file (ifd, file, 0);
425 copy_file (ifd, params.datafile, 0);
428 /* We're a bit of paranoid */
429 #if defined(_POSIX_SYNCHRONIZED_IO) && \
430 !defined(__sun__) && \
431 !defined(__FreeBSD__) && \
433 (void) fdatasync (ifd);
438 if (fstat(ifd, &sbuf) < 0) {
439 fprintf (stderr, "%s: Can't stat %s: %s\n",
440 params.cmdname, params.imagefile, strerror(errno));
444 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
445 if (ptr == MAP_FAILED) {
446 fprintf (stderr, "%s: Can't map %s: %s\n",
447 params.cmdname, params.imagefile, strerror(errno));
451 /* Setup the image header as per input image type*/
452 if (tparams->set_header)
453 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
455 fprintf (stderr, "%s: Can't set header for %s: %s\n",
456 params.cmdname, tparams->name, strerror(errno));
460 /* Print the image information by processing image header */
461 if (tparams->print_header)
462 tparams->print_header (ptr);
464 fprintf (stderr, "%s: Can't print header for %s: %s\n",
465 params.cmdname, tparams->name, strerror(errno));
469 (void) munmap((void *)ptr, sbuf.st_size);
471 /* We're a bit of paranoid */
472 #if defined(_POSIX_SYNCHRONIZED_IO) && \
473 !defined(__sun__) && \
474 !defined(__FreeBSD__) && \
476 (void) fdatasync (ifd);
482 fprintf (stderr, "%s: Write error on %s: %s\n",
483 params.cmdname, params.imagefile, strerror(errno));
491 copy_file (int ifd, const char *datafile, int pad)
500 struct image_type_params *tparams = mkimage_get_type (params.type);
503 fprintf (stderr, "Adding Image %s\n", datafile);
506 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
507 fprintf (stderr, "%s: Can't open %s: %s\n",
508 params.cmdname, datafile, strerror(errno));
512 if (fstat(dfd, &sbuf) < 0) {
513 fprintf (stderr, "%s: Can't stat %s: %s\n",
514 params.cmdname, datafile, strerror(errno));
518 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
519 if (ptr == MAP_FAILED) {
520 fprintf (stderr, "%s: Can't read %s: %s\n",
521 params.cmdname, datafile, strerror(errno));
526 unsigned char *p = NULL;
528 * XIP: do not append the image_header_t at the
529 * beginning of the file, but consume the space
533 if ((unsigned)sbuf.st_size < tparams->header_size) {
535 "%s: Bad size: \"%s\" is too small for XIP\n",
536 params.cmdname, datafile);
540 for (p = ptr; p < ptr + tparams->header_size; p++) {
543 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
544 params.cmdname, datafile);
549 offset = tparams->header_size;
552 size = sbuf.st_size - offset;
553 if (write(ifd, ptr + offset, size) != size) {
554 fprintf (stderr, "%s: Write error on %s: %s\n",
555 params.cmdname, params.imagefile, strerror(errno));
559 if (pad && ((tail = size % 4) != 0)) {
561 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
562 fprintf (stderr, "%s: Write error on %s: %s\n",
563 params.cmdname, params.imagefile,
569 (void) munmap((void *)ptr, sbuf.st_size);
576 fprintf (stderr, "Usage: %s -l image\n"
577 " -l ==> list image header information\n",
579 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
580 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
581 " -A ==> set architecture to 'arch'\n"
582 " -O ==> set operating system to 'os'\n"
583 " -T ==> set image type to 'type'\n"
584 " -C ==> set compression type 'comp'\n"
585 " -a ==> set load address to 'addr' (hex)\n"
586 " -e ==> set entry point to 'ep' (hex)\n"
587 " -n ==> set image name to 'name'\n"
588 " -d ==> use image data from 'datafile'\n"
589 " -x ==> set XIP (execute in place)\n",
591 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",