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 TI OMAP Boot image generation/list support */
160 init_omap_image_type();
161 /* Init Default image generation/list support */
162 init_default_image_type ();
163 /* Init Davinci UBL support */
164 init_ubl_image_type();
166 params.cmdname = *argv;
167 params.addr = params.ep = 0;
169 while (--argc > 0 && **++argv == '-') {
178 genimg_get_arch_id (*++argv)) < 0)
184 genimg_get_comp_id (*++argv)) < 0)
190 params.dtc = *++argv;
196 genimg_get_os_id (*++argv)) < 0)
202 genimg_get_type_id (*++argv)) < 0)
209 params.addr = strtoul (*++argv, &ptr, 16);
212 "%s: invalid load address %s\n",
213 params.cmdname, *argv);
220 params.datafile = *++argv;
226 params.ep = strtoul (*++argv, &ptr, 16);
229 "%s: invalid entry point %s\n",
230 params.cmdname, *argv);
239 * The flattened image tree (FIT) format
240 * requires a flattened device tree image type
242 params.type = IH_TYPE_FLATDT;
243 params.datafile = *++argv;
249 params.imagename = *++argv;
258 printf("mkimage version %s\n", PLAIN_VERSION);
273 /* set tparams as per input type_id */
274 tparams = mkimage_get_type(params.type);
275 if (tparams == NULL) {
276 fprintf (stderr, "%s: unsupported type %s\n",
277 params.cmdname, genimg_get_type_name(params.type));
282 * check the passed arguments parameters meets the requirements
283 * as per image type to be generated/listed
285 if (tparams->check_params)
286 if (tparams->check_params (¶ms))
290 params.ep = params.addr;
291 /* If XIP, entry point must be after the U-Boot header */
293 params.ep += tparams->header_size;
296 params.imagefile = *argv;
299 if (tparams->fflag_handle)
301 * in some cases, some additional processing needs
302 * to be done if fflag is defined
304 * For ex. fit_handle_file for Fit file support
306 retval = tparams->fflag_handle(¶ms);
308 if (retval != EXIT_SUCCESS)
312 if (params.lflag || params.fflag) {
313 ifd = open (params.imagefile, O_RDONLY|O_BINARY);
315 ifd = open (params.imagefile,
316 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
320 fprintf (stderr, "%s: Can't open %s: %s\n",
321 params.cmdname, params.imagefile,
326 if (params.lflag || params.fflag) {
328 * list header information of existing image
330 if (fstat(ifd, &sbuf) < 0) {
331 fprintf (stderr, "%s: Can't stat %s: %s\n",
332 params.cmdname, params.imagefile,
337 if ((unsigned)sbuf.st_size < tparams->header_size) {
339 "%s: Bad size: \"%s\" is not valid image\n",
340 params.cmdname, params.imagefile);
344 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
345 if (ptr == MAP_FAILED) {
346 fprintf (stderr, "%s: Can't read %s: %s\n",
347 params.cmdname, params.imagefile,
353 * scan through mkimage registry for all supported image types
354 * and verify the input image file header for match
355 * Print the image information for matched image type
356 * Returns the error code if not matched
358 retval = mkimage_verify_print_header (ptr, &sbuf);
360 (void) munmap((void *)ptr, sbuf.st_size);
367 * In case there an header with a variable
368 * length will be added, the corresponding
369 * function is called. This is responsible to
370 * allocate memory for the header itself.
372 if (tparams->vrec_header)
373 tparams->vrec_header(¶ms, tparams);
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.skipcpy &&
385 (params.type == IH_TYPE_MULTI ||
386 params.type == IH_TYPE_SCRIPT)) {
387 char *file = params.datafile;
394 if ((sep = strchr(file, ':')) != NULL) {
398 if (stat (file, &sbuf) < 0) {
399 fprintf (stderr, "%s: Can't stat %s: %s\n",
400 params.cmdname, file, strerror(errno));
403 size = cpu_to_uimage (sbuf.st_size);
408 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
409 fprintf (stderr, "%s: Write error on %s: %s\n",
410 params.cmdname, params.imagefile,
427 file = params.datafile;
430 char *sep = strchr(file, ':');
433 copy_file (ifd, file, 1);
437 copy_file (ifd, file, 0);
442 copy_file (ifd, params.datafile, 0);
445 /* We're a bit of paranoid */
446 #if defined(_POSIX_SYNCHRONIZED_IO) && \
447 !defined(__sun__) && \
448 !defined(__FreeBSD__) && \
450 (void) fdatasync (ifd);
455 if (fstat(ifd, &sbuf) < 0) {
456 fprintf (stderr, "%s: Can't stat %s: %s\n",
457 params.cmdname, params.imagefile, strerror(errno));
461 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
462 if (ptr == MAP_FAILED) {
463 fprintf (stderr, "%s: Can't map %s: %s\n",
464 params.cmdname, params.imagefile, strerror(errno));
468 /* Setup the image header as per input image type*/
469 if (tparams->set_header)
470 tparams->set_header (ptr, &sbuf, ifd, ¶ms);
472 fprintf (stderr, "%s: Can't set header for %s: %s\n",
473 params.cmdname, tparams->name, strerror(errno));
477 /* Print the image information by processing image header */
478 if (tparams->print_header)
479 tparams->print_header (ptr);
481 fprintf (stderr, "%s: Can't print header for %s: %s\n",
482 params.cmdname, tparams->name, strerror(errno));
486 (void) munmap((void *)ptr, sbuf.st_size);
488 /* We're a bit of paranoid */
489 #if defined(_POSIX_SYNCHRONIZED_IO) && \
490 !defined(__sun__) && \
491 !defined(__FreeBSD__) && \
493 (void) fdatasync (ifd);
499 fprintf (stderr, "%s: Write error on %s: %s\n",
500 params.cmdname, params.imagefile, strerror(errno));
508 copy_file (int ifd, const char *datafile, int pad)
517 struct image_type_params *tparams = mkimage_get_type (params.type);
520 fprintf (stderr, "Adding Image %s\n", datafile);
523 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
524 fprintf (stderr, "%s: Can't open %s: %s\n",
525 params.cmdname, datafile, strerror(errno));
529 if (fstat(dfd, &sbuf) < 0) {
530 fprintf (stderr, "%s: Can't stat %s: %s\n",
531 params.cmdname, datafile, strerror(errno));
535 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
536 if (ptr == MAP_FAILED) {
537 fprintf (stderr, "%s: Can't read %s: %s\n",
538 params.cmdname, datafile, strerror(errno));
543 unsigned char *p = NULL;
545 * XIP: do not append the image_header_t at the
546 * beginning of the file, but consume the space
550 if ((unsigned)sbuf.st_size < tparams->header_size) {
552 "%s: Bad size: \"%s\" is too small for XIP\n",
553 params.cmdname, datafile);
557 for (p = ptr; p < ptr + tparams->header_size; p++) {
560 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
561 params.cmdname, datafile);
566 offset = tparams->header_size;
569 size = sbuf.st_size - offset;
570 if (write(ifd, ptr + offset, size) != size) {
571 fprintf (stderr, "%s: Write error on %s: %s\n",
572 params.cmdname, params.imagefile, strerror(errno));
576 if (pad && ((tail = size % 4) != 0)) {
578 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
579 fprintf (stderr, "%s: Write error on %s: %s\n",
580 params.cmdname, params.imagefile,
586 (void) munmap((void *)ptr, sbuf.st_size);
593 fprintf (stderr, "Usage: %s -l image\n"
594 " -l ==> list image header information\n",
596 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
597 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
598 " -A ==> set architecture to 'arch'\n"
599 " -O ==> set operating system to 'os'\n"
600 " -T ==> set image type to 'type'\n"
601 " -C ==> set compression type 'comp'\n"
602 " -a ==> set load address to 'addr' (hex)\n"
603 " -e ==> set entry point to 'ep' (hex)\n"
604 " -n ==> set image name to 'name'\n"
605 " -d ==> use image data from 'datafile'\n"
606 " -x ==> set XIP (execute in place)\n",
608 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
610 fprintf (stderr, " %s -V ==> print version information and exit\n",