Commit 89a4d6b12fd6394898b8a454cbabeaf1cd59bae5

Authored by Prafulla Wadaskar
Committed by Wolfgang Denk
1 parent 449609f5b1

tools: mkimage: split code into core, default and FIT image specific

This is a first step towards reorganizing the mkimage code to make it
easier to add support for additional images types. Current mkimage
code is specific to generating uImage and FIT image files, but the
same framework can be used to generate other image types like
Kirkwood boot images (kwbimage-TBD). For this, the mkimage code gets
reworked:

Here is the brief plan for the same:-
a) Split mkimage code into core and image specific support
b) Implement callback functions for image specific code
c) Move image type specific code to respective C files
       Currently there are two types of file generation/list
       supported (i.e uImage, FIT), the code is abstracted from
       mkimage.c/.h and put in default_image.c and fit_image.c;
       all code in these file is static except init function call
d) mkimage_register API is added to add new image type support
All above is addressed in this patch
e) Add kwbimage type support to this new framework (TBD)
This will be implemented in a following commit.

Signed-off-by: Prafulla Wadaskar <prafulla@marvell.com>
Edit commit message, fix coding style and typos.
Signed-off-by: Wolfgang Denk <wd@denx.de>

Showing 5 changed files with 736 additions and 294 deletions Side-by-side Diff

... ... @@ -183,8 +183,15 @@
183 183 $(CC) $(CFLAGS) $(HOST_LDFLAGS) -o $@ $^
184 184 $(STRIP) $@
185 185  
186   -$(obj)mkimage$(SFX): $(obj)crc32.o $(obj)mkimage.o $(obj)image.o $(obj)md5.o $(obj)mkimage.o \
187   - $(obj)os_support.o $(obj)sha1.o $(LIBFDT_OBJS)
  186 +$(obj)mkimage$(SFX): $(obj)crc32.o \
  187 + $(obj)default_image.o \
  188 + $(obj)fit_image.o \
  189 + $(obj)image.o \
  190 + $(obj)md5.o \
  191 + $(obj)mkimage.o \
  192 + $(obj)os_support.o \
  193 + $(obj)sha1.o \
  194 + $(LIBFDT_OBJS)
188 195 $(CC) $(CFLAGS) $(HOST_LDFLAGS) -o $@ $^
189 196 $(STRIP) $@
190 197  
... ... @@ -200,6 +207,12 @@
200 207 $(CC) $(CFLAGS) -o $@ $^
201 208  
202 209 # Some files complain if compiled with -pedantic, use FIT_CFLAGS
  210 +$(obj)default_image.o: $(SRCTREE)/tools/default_image.c
  211 + $(CC) -g $(FIT_CFLAGS) -c -o $@ $<
  212 +
  213 +$(obj)fit_image.o: $(SRCTREE)/tools/fit_image.c
  214 + $(CC) -g $(FIT_CFLAGS) -c -o $@ $<
  215 +
203 216 $(obj)image.o: $(SRCTREE)/common/image.c
204 217 $(CC) -g $(FIT_CFLAGS) -c -o $@ $<
205 218  
tools/default_image.c
  1 +/*
  2 + * (C) Copyright 2008 Semihalf
  3 + *
  4 + * (C) Copyright 2000-2004
  5 + * DENX Software Engineering
  6 + * Wolfgang Denk, wd@denx.de
  7 + *
  8 + * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
  9 + * default_image specific code abstracted from mkimage.c
  10 + * some functions added to address abstraction
  11 + *
  12 + * All rights reserved.
  13 + *
  14 + * This program is free software; you can redistribute it and/or
  15 + * modify it under the terms of the GNU General Public License as
  16 + * published by the Free Software Foundation; either version 2 of
  17 + * the License, or (at your option) any later version.
  18 + *
  19 + * This program is distributed in the hope that it will be useful,
  20 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22 + * GNU General Public License for more details.
  23 + *
  24 + * You should have received a copy of the GNU General Public License
  25 + * along with this program; if not, write to the Free Software
  26 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27 + * MA 02111-1307 USA
  28 + */
  29 +
  30 +#include "mkimage.h"
  31 +#include <image.h>
  32 +#include <u-boot/crc.h>
  33 +
  34 +static image_header_t header;
  35 +
  36 +static int image_check_image_types (uint8_t type)
  37 +{
  38 + if ((type > IH_TYPE_INVALID) && (type < IH_TYPE_FLATDT))
  39 + return EXIT_SUCCESS;
  40 + else
  41 + return EXIT_FAILURE;
  42 +}
  43 +
  44 +static int image_check_params (struct mkimage_params *params)
  45 +{
  46 + return ((params->dflag && (params->fflag || params->lflag)) ||
  47 + (params->fflag && (params->dflag || params->lflag)) ||
  48 + (params->lflag && (params->dflag || params->fflag)));
  49 +}
  50 +
  51 +static int image_verify_header (unsigned char *ptr, int image_size,
  52 + struct mkimage_params *params)
  53 +{
  54 + uint32_t len;
  55 + const unsigned char *data;
  56 + uint32_t checksum;
  57 + image_header_t header;
  58 + image_header_t *hdr = &header;
  59 +
  60 + /*
  61 + * create copy of header so that we can blank out the
  62 + * checksum field for checking - this can't be done
  63 + * on the PROT_READ mapped data.
  64 + */
  65 + memcpy (hdr, ptr, sizeof(image_header_t));
  66 +
  67 + if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
  68 + fprintf (stderr,
  69 + "%s: Bad Magic Number: \"%s\" is no valid image\n",
  70 + params->cmdname, params->imagefile);
  71 + return -FDT_ERR_BADMAGIC;
  72 + }
  73 +
  74 + data = (const unsigned char *)hdr;
  75 + len = sizeof(image_header_t);
  76 +
  77 + checksum = be32_to_cpu(hdr->ih_hcrc);
  78 + hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
  79 +
  80 + if (crc32 (0, data, len) != checksum) {
  81 + fprintf (stderr,
  82 + "%s: ERROR: \"%s\" has bad header checksum!\n",
  83 + params->cmdname, params->imagefile);
  84 + return -FDT_ERR_BADSTATE;
  85 + }
  86 +
  87 + data = (const unsigned char *)ptr + sizeof(image_header_t);
  88 + len = image_size - sizeof(image_header_t) ;
  89 +
  90 + checksum = be32_to_cpu(hdr->ih_dcrc);
  91 + if (crc32 (0, data, len) != checksum) {
  92 + fprintf (stderr,
  93 + "%s: ERROR: \"%s\" has corrupted data!\n",
  94 + params->cmdname, params->imagefile);
  95 + return -FDT_ERR_BADSTRUCTURE;
  96 + }
  97 + return 0;
  98 +}
  99 +
  100 +static void image_set_header (void *ptr, struct stat *sbuf, int ifd,
  101 + struct mkimage_params *params)
  102 +{
  103 + uint32_t checksum;
  104 +
  105 + image_header_t * hdr = (image_header_t *)ptr;
  106 +
  107 + checksum = crc32 (0,
  108 + (const unsigned char *)(ptr +
  109 + sizeof(image_header_t)),
  110 + sbuf->st_size - sizeof(image_header_t));
  111 +
  112 + /* Build new header */
  113 + image_set_magic (hdr, IH_MAGIC);
  114 + image_set_time (hdr, sbuf->st_mtime);
  115 + image_set_size (hdr, sbuf->st_size - sizeof(image_header_t));
  116 + image_set_load (hdr, params->addr);
  117 + image_set_ep (hdr, params->ep);
  118 + image_set_dcrc (hdr, checksum);
  119 + image_set_os (hdr, params->os);
  120 + image_set_arch (hdr, params->arch);
  121 + image_set_type (hdr, params->type);
  122 + image_set_comp (hdr, params->comp);
  123 +
  124 + image_set_name (hdr, params->imagename);
  125 +
  126 + checksum = crc32 (0, (const unsigned char *)hdr,
  127 + sizeof(image_header_t));
  128 +
  129 + image_set_hcrc (hdr, checksum);
  130 +}
  131 +
  132 +/*
  133 + * Default image type parameters definition
  134 + */
  135 +static struct image_type_params defimage_params = {
  136 + .name = "Default Image support",
  137 + .header_size = sizeof(image_header_t),
  138 + .hdr = (void*)&header,
  139 + .check_image_type = image_check_image_types,
  140 + .verify_header = image_verify_header,
  141 + .print_header = image_print_contents,
  142 + .set_header = image_set_header,
  143 + .check_params = image_check_params,
  144 +};
  145 +
  146 +void init_default_image_type (void)
  147 +{
  148 + mkimage_register (&defimage_params);
  149 +}
  1 +/*
  2 + * (C) Copyright 2008 Semihalf
  3 + *
  4 + * (C) Copyright 2000-2004
  5 + * DENX Software Engineering
  6 + * Wolfgang Denk, wd@denx.de
  7 + *
  8 + * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
  9 + * FIT image specific code abstracted from mkimage.c
  10 + * some functions added to address abstraction
  11 + *
  12 + * All rights reserved.
  13 + *
  14 + * This program is free software; you can redistribute it and/or
  15 + * modify it under the terms of the GNU General Public License as
  16 + * published by the Free Software Foundation; either version 2 of
  17 + * the License, or (at your option) any later version.
  18 + *
  19 + * This program is distributed in the hope that it will be useful,
  20 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22 + * GNU General Public License for more details.
  23 + *
  24 + * You should have received a copy of the GNU General Public License
  25 + * along with this program; if not, write to the Free Software
  26 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27 + * MA 02111-1307 USA
  28 + */
  29 +
  30 +#include "mkimage.h"
  31 +#include <image.h>
  32 +#include <u-boot/crc.h>
  33 +
  34 +static image_header_t header;
  35 +
  36 +static int fit_verify_header (unsigned char *ptr, int image_size,
  37 + struct mkimage_params *params)
  38 +{
  39 + return fdt_check_header ((void *)ptr);
  40 +}
  41 +
  42 +static int fit_check_image_types (uint8_t type)
  43 +{
  44 + if (type == IH_TYPE_FLATDT)
  45 + return EXIT_SUCCESS;
  46 + else
  47 + return EXIT_FAILURE;
  48 +}
  49 +
  50 +/**
  51 + * fit_handle_file - main FIT file processing function
  52 + *
  53 + * fit_handle_file() runs dtc to convert .its to .itb, includes
  54 + * binary data, updates timestamp property and calculates hashes.
  55 + *
  56 + * datafile - .its file
  57 + * imagefile - .itb file
  58 + *
  59 + * returns:
  60 + * only on success, otherwise calls exit (EXIT_FAILURE);
  61 + */
  62 +static int fit_handle_file (struct mkimage_params *params)
  63 +{
  64 + char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
  65 + char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
  66 + int tfd;
  67 + struct stat sbuf;
  68 + unsigned char *ptr;
  69 +
  70 + /* Flattened Image Tree (FIT) format handling */
  71 + debug ("FIT format handling\n");
  72 +
  73 + /* call dtc to include binary properties into the tmp file */
  74 + if (strlen (params->imagefile) +
  75 + strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
  76 + fprintf (stderr, "%s: Image file name (%s) too long, "
  77 + "can't create tmpfile",
  78 + params->imagefile, params->cmdname);
  79 + return (EXIT_FAILURE);
  80 + }
  81 + sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
  82 +
  83 + /* dtc -I dts -O -p 200 datafile > tmpfile */
  84 + sprintf (cmd, "%s %s %s > %s",
  85 + MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
  86 + debug ("Trying to execute \"%s\"\n", cmd);
  87 + if (system (cmd) == -1) {
  88 + fprintf (stderr, "%s: system(%s) failed: %s\n",
  89 + params->cmdname, cmd, strerror(errno));
  90 + unlink (tmpfile);
  91 + return (EXIT_FAILURE);
  92 + }
  93 +
  94 + /* load FIT blob into memory */
  95 + tfd = open (tmpfile, O_RDWR|O_BINARY);
  96 +
  97 + if (tfd < 0) {
  98 + fprintf (stderr, "%s: Can't open %s: %s\n",
  99 + params->cmdname, tmpfile, strerror(errno));
  100 + unlink (tmpfile);
  101 + return (EXIT_FAILURE);
  102 + }
  103 +
  104 + if (fstat (tfd, &sbuf) < 0) {
  105 + fprintf (stderr, "%s: Can't stat %s: %s\n",
  106 + params->cmdname, tmpfile, strerror(errno));
  107 + unlink (tmpfile);
  108 + return (EXIT_FAILURE);
  109 + }
  110 +
  111 + ptr = mmap (0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
  112 + tfd, 0);
  113 + if (ptr == MAP_FAILED) {
  114 + fprintf (stderr, "%s: Can't read %s: %s\n",
  115 + params->cmdname, tmpfile, strerror(errno));
  116 + unlink (tmpfile);
  117 + return (EXIT_FAILURE);
  118 + }
  119 +
  120 + /* check if ptr has a valid blob */
  121 + if (fdt_check_header (ptr)) {
  122 + fprintf (stderr, "%s: Invalid FIT blob\n", params->cmdname);
  123 + unlink (tmpfile);
  124 + return (EXIT_FAILURE);
  125 + }
  126 +
  127 + /* set hashes for images in the blob */
  128 + if (fit_set_hashes (ptr)) {
  129 + fprintf (stderr, "%s Can't add hashes to FIT blob",
  130 + params->cmdname);
  131 + unlink (tmpfile);
  132 + return (EXIT_FAILURE);
  133 + }
  134 +
  135 + /* add a timestamp at offset 0 i.e., root */
  136 + if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) {
  137 + fprintf (stderr, "%s: Can't add image timestamp\n",
  138 + params->cmdname);
  139 + unlink (tmpfile);
  140 + return (EXIT_FAILURE);
  141 + }
  142 + debug ("Added timestamp successfully\n");
  143 +
  144 + munmap ((void *)ptr, sbuf.st_size);
  145 + close (tfd);
  146 +
  147 + if (rename (tmpfile, params->imagefile) == -1) {
  148 + fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
  149 + params->cmdname, tmpfile, params->imagefile,
  150 + strerror (errno));
  151 + unlink (tmpfile);
  152 + unlink (params->imagefile);
  153 + return (EXIT_FAILURE);
  154 + }
  155 + return (EXIT_SUCCESS);
  156 +}
  157 +
  158 +static void fit_set_header (void *ptr, struct stat *sbuf, int ifd,
  159 + struct mkimage_params *params)
  160 +{
  161 + uint32_t checksum;
  162 +
  163 + image_header_t * hdr = (image_header_t *)ptr;
  164 +
  165 + checksum = crc32 (0,
  166 + (const unsigned char *)(ptr +
  167 + sizeof(image_header_t)),
  168 + sbuf->st_size - sizeof(image_header_t));
  169 +
  170 + /* Build new header */
  171 + image_set_magic (hdr, IH_MAGIC);
  172 + image_set_time (hdr, sbuf->st_mtime);
  173 + image_set_size (hdr, sbuf->st_size - sizeof(image_header_t));
  174 + image_set_load (hdr, params->addr);
  175 + image_set_ep (hdr, params->ep);
  176 + image_set_dcrc (hdr, checksum);
  177 + image_set_os (hdr, params->os);
  178 + image_set_arch (hdr, params->arch);
  179 + image_set_type (hdr, params->type);
  180 + image_set_comp (hdr, params->comp);
  181 +
  182 + image_set_name (hdr, params->imagename);
  183 +
  184 + checksum = crc32 (0, (const unsigned char *)hdr,
  185 + sizeof(image_header_t));
  186 +
  187 + image_set_hcrc (hdr, checksum);
  188 +}
  189 +
  190 +static int fit_check_params (struct mkimage_params *params)
  191 +{
  192 + return ((params->dflag && (params->fflag || params->lflag)) ||
  193 + (params->fflag && (params->dflag || params->lflag)) ||
  194 + (params->lflag && (params->dflag || params->fflag)));
  195 +}
  196 +
  197 +static struct image_type_params fitimage_params = {
  198 + .name = "FIT Image support",
  199 + .header_size = sizeof(image_header_t),
  200 + .hdr = (void*)&header,
  201 + .verify_header = fit_verify_header,
  202 + .print_header = fit_print_contents,
  203 + .check_image_type = fit_check_image_types,
  204 + .fflag_handle = fit_handle_file,
  205 + .set_header = fit_set_header,
  206 + .check_params = fit_check_params,
  207 +};
  208 +
  209 +void init_fit_image_type (void)
  210 +{
  211 + mkimage_register (&fitimage_params);
  212 +}
1 1 /*
2 2 * (C) Copyright 2008 Semihalf
3 3 *
4   - * (C) Copyright 2000-2004
  4 + * (C) Copyright 2000-2009
5 5 * DENX Software Engineering
6 6 * Wolfgang Denk, wd@denx.de
7 7 *
8 8  
9 9  
10 10  
11 11  
12 12  
13 13  
14 14  
15 15  
16 16  
17 17  
18 18  
19 19  
20 20  
21 21  
22 22  
23 23  
24 24  
25 25  
26 26  
27 27  
28 28  
29 29  
30 30  
31 31  
32 32  
33 33  
... ... @@ -23,126 +23,223 @@
23 23  
24 24 #include "mkimage.h"
25 25 #include <image.h>
26   -#include <u-boot/crc.h>
27 26  
28   -static void copy_file (int, const char *, int);
29   -static void usage (void);
30   -static int image_verify_header (char *, int);
31   -static void fit_handle_file (void);
  27 +static void copy_file(int, const char *, int);
  28 +static void usage(void);
32 29  
33   -char *datafile;
34   -char *imagefile;
35   -char *cmdname;
  30 +/* image_type_params link list to maintain registered image type supports */
  31 +struct image_type_params *mkimage_tparams = NULL;
36 32  
37   -int dflag = 0;
38   -int eflag = 0;
39   -int fflag = 0;
40   -int lflag = 0;
41   -int vflag = 0;
42   -int xflag = 0;
43   -int opt_os = IH_OS_LINUX;
44   -int opt_arch = IH_ARCH_PPC;
45   -int opt_type = IH_TYPE_KERNEL;
46   -int opt_comp = IH_COMP_GZIP;
47   -char *opt_dtc = MKIMAGE_DEFAULT_DTC_OPTIONS;
  33 +/* parameters initialized by core will be used by the image type code */
  34 +struct mkimage_params params = {
  35 + .os = IH_OS_LINUX,
  36 + .arch = IH_ARCH_PPC,
  37 + .type = IH_TYPE_KERNEL,
  38 + .comp = IH_COMP_GZIP,
  39 + .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
  40 +};
48 41  
49   -image_header_t header;
50   -image_header_t *hdr = &header;
  42 +/*
  43 + * mkimage_register -
  44 + *
  45 + * It is used to register respective image generation/list support to the
  46 + * mkimage core
  47 + *
  48 + * the input struct image_type_params is checked and appended to the link
  49 + * list, if the input structure is already registered, error
  50 + */
  51 +void mkimage_register (struct image_type_params *tparams)
  52 +{
  53 + struct image_type_params **tp;
51 54  
  55 + if (!tparams) {
  56 + fprintf (stderr, "%s: %s: Null input\n",
  57 + params.cmdname, __FUNCTION__);
  58 + exit (EXIT_FAILURE);
  59 + }
  60 +
  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);
  66 + return;
  67 + }
  68 + }
  69 +
  70 + /* add input struct entry at the end of link list */
  71 + *tp = tparams;
  72 + /* mark input entry as last entry in the link list */
  73 + tparams->next = NULL;
  74 +
  75 + debug ("Registered %s\n", tparams->name);
  76 +}
  77 +
  78 +/*
  79 + * mkimage_get_type -
  80 + *
  81 + * It scans all registers image type supports
  82 + * checks the input type_id for each supported image type
  83 + *
  84 + * if successful,
  85 + * returns respective image_type_params pointer if success
  86 + * if input type_id is not supported by any of image_type_support
  87 + * returns NULL
  88 + */
  89 +struct image_type_params *mkimage_get_type(int type)
  90 +{
  91 + struct image_type_params *curr;
  92 +
  93 + for (curr = mkimage_tparams; curr != NULL; curr = curr->next) {
  94 + if (curr->check_image_type) {
  95 + if (!curr->check_image_type (type))
  96 + return curr;
  97 + }
  98 + }
  99 + return NULL;
  100 +}
  101 +
  102 +/*
  103 + * mkimage_verify_print_header -
  104 + *
  105 + * It scans mkimage_tparams link list,
  106 + * verifies image_header for each supported image type
  107 + * if verification is successful, prints respective header
  108 + *
  109 + * returns negative if input image format does not match with any of
  110 + * supported image types
  111 + */
  112 +int mkimage_verify_print_header (void *ptr, struct stat *sbuf)
  113 +{
  114 + int retval = -1;
  115 + struct image_type_params *curr;
  116 +
  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,
  121 + &params);
  122 +
  123 + if (retval == 0) {
  124 + /*
  125 + * Print the image information
  126 + * if verify is successful
  127 + */
  128 + if (curr->print_header)
  129 + curr->print_header (ptr);
  130 + else {
  131 + fprintf (stderr,
  132 + "%s: print_header undefined for %s\n",
  133 + params.cmdname, curr->name);
  134 + }
  135 + break;
  136 + }
  137 + }
  138 + }
  139 + return retval;
  140 +}
  141 +
52 142 int
53 143 main (int argc, char **argv)
54 144 {
55 145 int ifd = -1;
56   - uint32_t checksum;
57   - uint32_t addr;
58   - uint32_t ep;
59 146 struct stat sbuf;
60 147 unsigned char *ptr;
61   - char *name = "";
62 148 int retval = 0;
  149 + struct image_type_params *tparams = NULL;
63 150  
64   - cmdname = *argv;
  151 + /* Init FIT image generation/list support */
  152 + init_fit_image_type ();
  153 + /* Init Default image generation/list support */
  154 + init_default_image_type ();
65 155  
66   - addr = ep = 0;
  156 + params.cmdname = *argv;
  157 + params.addr = params.ep = 0;
67 158  
68 159 while (--argc > 0 && **++argv == '-') {
69 160 while (*++*argv) {
70 161 switch (**argv) {
71 162 case 'l':
72   - lflag = 1;
  163 + params.lflag = 1;
73 164 break;
74 165 case 'A':
75 166 if ((--argc <= 0) ||
76   - (opt_arch = genimg_get_arch_id (*++argv)) < 0)
  167 + (params.arch =
  168 + genimg_get_arch_id (*++argv)) < 0)
77 169 usage ();
78 170 goto NXTARG;
79 171 case 'C':
80 172 if ((--argc <= 0) ||
81   - (opt_comp = genimg_get_comp_id (*++argv)) < 0)
  173 + (params.comp =
  174 + genimg_get_comp_id (*++argv)) < 0)
82 175 usage ();
83 176 goto NXTARG;
84 177 case 'D':
85 178 if (--argc <= 0)
86 179 usage ();
87   - opt_dtc = *++argv;
  180 + params.dtc = *++argv;
88 181 goto NXTARG;
89 182  
90 183 case 'O':
91 184 if ((--argc <= 0) ||
92   - (opt_os = genimg_get_os_id (*++argv)) < 0)
  185 + (params.os =
  186 + genimg_get_os_id (*++argv)) < 0)
93 187 usage ();
94 188 goto NXTARG;
95 189 case 'T':
96 190 if ((--argc <= 0) ||
97   - (opt_type = genimg_get_type_id (*++argv)) < 0)
  191 + (params.type =
  192 + genimg_get_type_id (*++argv)) < 0)
98 193 usage ();
99 194 goto NXTARG;
100 195  
101 196 case 'a':
102 197 if (--argc <= 0)
103 198 usage ();
104   - addr = strtoul (*++argv, (char **)&ptr, 16);
  199 + params.addr = strtoul (*++argv,
  200 + (char **)&ptr, 16);
105 201 if (*ptr) {
106 202 fprintf (stderr,
107 203 "%s: invalid load address %s\n",
108   - cmdname, *argv);
  204 + params.cmdname, *argv);
109 205 exit (EXIT_FAILURE);
110 206 }
111 207 goto NXTARG;
112 208 case 'd':
113 209 if (--argc <= 0)
114 210 usage ();
115   - datafile = *++argv;
116   - dflag = 1;
  211 + params.datafile = *++argv;
  212 + params.dflag = 1;
117 213 goto NXTARG;
118 214 case 'e':
119 215 if (--argc <= 0)
120 216 usage ();
121   - ep = strtoul (*++argv, (char **)&ptr, 16);
  217 + params.ep = strtoul (*++argv,
  218 + (char **)&ptr, 16);
122 219 if (*ptr) {
123 220 fprintf (stderr,
124 221 "%s: invalid entry point %s\n",
125   - cmdname, *argv);
  222 + params.cmdname, *argv);
126 223 exit (EXIT_FAILURE);
127 224 }
128   - eflag = 1;
  225 + params.eflag = 1;
129 226 goto NXTARG;
130 227 case 'f':
131 228 if (--argc <= 0)
132 229 usage ();
133   - datafile = *++argv;
134   - fflag = 1;
  230 + params.datafile = *++argv;
  231 + params.fflag = 1;
135 232 goto NXTARG;
136 233 case 'n':
137 234 if (--argc <= 0)
138 235 usage ();
139   - name = *++argv;
  236 + params.imagename = *++argv;
140 237 goto NXTARG;
141 238 case 'v':
142   - vflag++;
  239 + params.vflag++;
143 240 break;
144 241 case 'x':
145   - xflag++;
  242 + params.xflag++;
146 243 break;
147 244 default:
148 245 usage ();
149 246  
150 247  
151 248  
152 249  
153 250  
154 251  
155 252  
156 253  
157 254  
158 255  
159 256  
160 257  
161 258  
162 259  
163 260  
... ... @@ -151,91 +248,112 @@
151 248 NXTARG: ;
152 249 }
153 250  
154   - if ((argc != 1) ||
155   - (dflag && (fflag || lflag)) ||
156   - (fflag && (dflag || lflag)) ||
157   - (lflag && (dflag || fflag)))
158   - usage();
  251 + if (argc != 1)
  252 + usage ();
159 253  
160   - if (!eflag) {
161   - ep = addr;
  254 + /* set tparams as per input type_id */
  255 + tparams = mkimage_get_type(params.type);
  256 + if (tparams == NULL) {
  257 + fprintf (stderr, "%s: unsupported type %s\n",
  258 + params.cmdname, genimg_get_type_name(params.type));
  259 + exit (EXIT_FAILURE);
  260 + }
  261 +
  262 + /*
  263 + * check the passed arguments parameters meets the requirements
  264 + * as per image type to be generated/listed
  265 + */
  266 + if (tparams->check_params)
  267 + if (tparams->check_params (&params))
  268 + usage ();
  269 +
  270 + if (!params.eflag) {
  271 + params.ep = params.addr;
162 272 /* If XIP, entry point must be after the U-Boot header */
163   - if (xflag)
164   - ep += image_get_header_size ();
  273 + if (params.xflag)
  274 + params.ep += tparams->header_size;
165 275 }
166 276  
167 277 /*
168 278 * If XIP, ensure the entry point is equal to the load address plus
169 279 * the size of the U-Boot header.
170 280 */
171   - if (xflag) {
172   - if (ep != addr + image_get_header_size ()) {
  281 + if (params.xflag) {
  282 + if (params.ep != params.addr + tparams->header_size) {
173 283 fprintf (stderr,
174 284 "%s: For XIP, the entry point must be the load addr + %lu\n",
175   - cmdname,
176   - (unsigned long)image_get_header_size ());
  285 + params.cmdname,
  286 + (unsigned long)tparams->header_size);
177 287 exit (EXIT_FAILURE);
178 288 }
179 289 }
180 290  
181   - imagefile = *argv;
  291 + params.imagefile = *argv;
182 292  
183   - if (!fflag){
184   - if (lflag) {
185   - ifd = open (imagefile, O_RDONLY|O_BINARY);
  293 + if (!params.fflag){
  294 + if (params.lflag) {
  295 + ifd = open (params.imagefile, O_RDONLY|O_BINARY);
186 296 } else {
187   - ifd = open (imagefile,
  297 + ifd = open (params.imagefile,
188 298 O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666);
189 299 }
190 300  
191 301 if (ifd < 0) {
192 302 fprintf (stderr, "%s: Can't open %s: %s\n",
193   - cmdname, imagefile, strerror(errno));
  303 + params.cmdname, params.imagefile,
  304 + strerror(errno));
194 305 exit (EXIT_FAILURE);
195 306 }
196 307 }
197 308  
198   - if (lflag) {
  309 + if (params.lflag) {
199 310 /*
200 311 * list header information of existing image
201 312 */
202 313 if (fstat(ifd, &sbuf) < 0) {
203 314 fprintf (stderr, "%s: Can't stat %s: %s\n",
204   - cmdname, imagefile, strerror(errno));
  315 + params.cmdname, params.imagefile,
  316 + strerror(errno));
205 317 exit (EXIT_FAILURE);
206 318 }
207 319  
208   - if ((unsigned)sbuf.st_size < image_get_header_size ()) {
  320 + if ((unsigned)sbuf.st_size < tparams->header_size) {
209 321 fprintf (stderr,
210   - "%s: Bad size: \"%s\" is no valid image\n",
211   - cmdname, imagefile);
  322 + "%s: Bad size: \"%s\" is not valid image\n",
  323 + params.cmdname, params.imagefile);
212 324 exit (EXIT_FAILURE);
213 325 }
214 326  
215 327 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
216 328 if (ptr == MAP_FAILED) {
217 329 fprintf (stderr, "%s: Can't read %s: %s\n",
218   - cmdname, imagefile, strerror(errno));
  330 + params.cmdname, params.imagefile,
  331 + strerror(errno));
219 332 exit (EXIT_FAILURE);
220 333 }
221 334  
222   - if (!(retval = fdt_check_header (ptr))) {
223   - /* FIT image */
224   - fit_print_contents (ptr);
225   - } else if (!(retval = image_verify_header ((char *)ptr,
226   - sbuf.st_size))) {
227   - /* old-style image */
228   - image_print_contents ((image_header_t *)ptr);
229   - }
  335 + /*
  336 + * scan through mkimage registry for all supported image types
  337 + * and verify the input image file header for match
  338 + * Print the image information for matched image type
  339 + * Returns the error code if not matched
  340 + */
  341 + retval = mkimage_verify_print_header (ptr, &sbuf);
230 342  
231 343 (void) munmap((void *)ptr, sbuf.st_size);
232 344 (void) close (ifd);
233 345  
234 346 exit (retval);
235   - } else if (fflag) {
236   - /* Flattened Image Tree (FIT) format handling */
237   - debug ("FIT format handling\n");
238   - fit_handle_file ();
  347 + } else if (params.fflag) {
  348 + if (tparams->fflag_handle)
  349 + /*
  350 + * in some cases, some additional processing needs
  351 + * to be done if fflag is defined
  352 + *
  353 + * For ex. fit_handle_file for Fit file support
  354 + */
  355 + retval = tparams->fflag_handle(&params);
  356 +
239 357 exit (retval);
240 358 }
241 359  
242 360  
243 361  
244 362  
... ... @@ -244,16 +362,17 @@
244 362 *
245 363 * write dummy header, to be fixed later
246 364 */
247   - memset (hdr, 0, image_get_header_size ());
  365 + memset (tparams->hdr, 0, tparams->header_size);
248 366  
249   - if (write(ifd, hdr, image_get_header_size ()) != image_get_header_size ()) {
  367 + if (write(ifd, tparams->hdr, tparams->header_size)
  368 + != tparams->header_size) {
250 369 fprintf (stderr, "%s: Write error on %s: %s\n",
251   - cmdname, imagefile, strerror(errno));
  370 + params.cmdname, params.imagefile, strerror(errno));
252 371 exit (EXIT_FAILURE);
253 372 }
254 373  
255   - if (opt_type == IH_TYPE_MULTI || opt_type == IH_TYPE_SCRIPT) {
256   - char *file = datafile;
  374 + if (params.type == IH_TYPE_MULTI || params.type == IH_TYPE_SCRIPT) {
  375 + char *file = params.datafile;
257 376 uint32_t size;
258 377  
259 378 for (;;) {
... ... @@ -266,7 +385,7 @@
266 385  
267 386 if (stat (file, &sbuf) < 0) {
268 387 fprintf (stderr, "%s: Can't stat %s: %s\n",
269   - cmdname, file, strerror(errno));
  388 + params.cmdname, file, strerror(errno));
270 389 exit (EXIT_FAILURE);
271 390 }
272 391 size = cpu_to_uimage (sbuf.st_size);
... ... @@ -276,7 +395,8 @@
276 395  
277 396 if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) {
278 397 fprintf (stderr, "%s: Write error on %s: %s\n",
279   - cmdname, imagefile, strerror(errno));
  398 + params.cmdname, params.imagefile,
  399 + strerror(errno));
280 400 exit (EXIT_FAILURE);
281 401 }
282 402  
... ... @@ -292,7 +412,7 @@
292 412 }
293 413 }
294 414  
295   - file = datafile;
  415 + file = params.datafile;
296 416  
297 417 for (;;) {
298 418 char *sep = strchr(file, ':');
299 419  
... ... @@ -307,11 +427,14 @@
307 427 }
308 428 }
309 429 } else {
310   - copy_file (ifd, datafile, 0);
  430 + copy_file (ifd, params.datafile, 0);
311 431 }
312 432  
313 433 /* We're a bit of paranoid */
314   -#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
  434 +#if defined(_POSIX_SYNCHRONIZED_IO) && \
  435 + !defined(__sun__) && \
  436 + !defined(__FreeBSD__) && \
  437 + !defined(__APPLE__)
315 438 (void) fdatasync (ifd);
316 439 #else
317 440 (void) fsync (ifd);
318 441  
319 442  
320 443  
321 444  
322 445  
... ... @@ -319,50 +442,42 @@
319 442  
320 443 if (fstat(ifd, &sbuf) < 0) {
321 444 fprintf (stderr, "%s: Can't stat %s: %s\n",
322   - cmdname, imagefile, strerror(errno));
  445 + params.cmdname, params.imagefile, strerror(errno));
323 446 exit (EXIT_FAILURE);
324 447 }
325 448  
326 449 ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0);
327 450 if (ptr == MAP_FAILED) {
328 451 fprintf (stderr, "%s: Can't map %s: %s\n",
329   - cmdname, imagefile, strerror(errno));
  452 + params.cmdname, params.imagefile, strerror(errno));
330 453 exit (EXIT_FAILURE);
331 454 }
332 455  
333   - hdr = (image_header_t *)ptr;
  456 + /* Setup the image header as per input image type*/
  457 + if (tparams->set_header)
  458 + tparams->set_header (ptr, &sbuf, ifd, &params);
  459 + else {
  460 + fprintf (stderr, "%s: Can't set header for %s: %s\n",
  461 + params.cmdname, tparams->name, strerror(errno));
  462 + exit (EXIT_FAILURE);
  463 + }
334 464  
335   - checksum = crc32 (0,
336   - (const unsigned char *)(ptr +
337   - image_get_header_size ()),
338   - sbuf.st_size - image_get_header_size ()
339   - );
  465 + /* Print the image information by processing image header */
  466 + if (tparams->print_header)
  467 + tparams->print_header (ptr);
  468 + else {
  469 + fprintf (stderr, "%s: Can't print header for %s: %s\n",
  470 + params.cmdname, tparams->name, strerror(errno));
  471 + exit (EXIT_FAILURE);
  472 + }
340 473  
341   - /* Build new header */
342   - image_set_magic (hdr, IH_MAGIC);
343   - image_set_time (hdr, sbuf.st_mtime);
344   - image_set_size (hdr, sbuf.st_size - image_get_header_size ());
345   - image_set_load (hdr, addr);
346   - image_set_ep (hdr, ep);
347   - image_set_dcrc (hdr, checksum);
348   - image_set_os (hdr, opt_os);
349   - image_set_arch (hdr, opt_arch);
350   - image_set_type (hdr, opt_type);
351   - image_set_comp (hdr, opt_comp);
352   -
353   - image_set_name (hdr, name);
354   -
355   - checksum = crc32 (0, (const unsigned char *)hdr,
356   - image_get_header_size ());
357   -
358   - image_set_hcrc (hdr, checksum);
359   -
360   - image_print_contents (hdr);
361   -
362 474 (void) munmap((void *)ptr, sbuf.st_size);
363 475  
364 476 /* We're a bit of paranoid */
365   -#if defined(_POSIX_SYNCHRONIZED_IO) && !defined(__sun__) && !defined(__FreeBSD__) && !defined(__APPLE__)
  477 +#if defined(_POSIX_SYNCHRONIZED_IO) && \
  478 + !defined(__sun__) && \
  479 + !defined(__FreeBSD__) && \
  480 + !defined(__APPLE__)
366 481 (void) fdatasync (ifd);
367 482 #else
368 483 (void) fsync (ifd);
... ... @@ -370,7 +485,7 @@
370 485  
371 486 if (close(ifd)) {
372 487 fprintf (stderr, "%s: Write error on %s: %s\n",
373   - cmdname, imagefile, strerror(errno));
  488 + params.cmdname, params.imagefile, strerror(errno));
374 489 exit (EXIT_FAILURE);
375 490 }
376 491  
377 492  
378 493  
379 494  
380 495  
381 496  
... ... @@ -387,31 +502,32 @@
387 502 int zero = 0;
388 503 int offset = 0;
389 504 int size;
  505 + struct image_type_params *tparams = mkimage_get_type (params.type);
390 506  
391   - if (vflag) {
  507 + if (params.vflag) {
392 508 fprintf (stderr, "Adding Image %s\n", datafile);
393 509 }
394 510  
395 511 if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) {
396 512 fprintf (stderr, "%s: Can't open %s: %s\n",
397   - cmdname, datafile, strerror(errno));
  513 + params.cmdname, datafile, strerror(errno));
398 514 exit (EXIT_FAILURE);
399 515 }
400 516  
401 517 if (fstat(dfd, &sbuf) < 0) {
402 518 fprintf (stderr, "%s: Can't stat %s: %s\n",
403   - cmdname, datafile, strerror(errno));
  519 + params.cmdname, datafile, strerror(errno));
404 520 exit (EXIT_FAILURE);
405 521 }
406 522  
407 523 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
408 524 if (ptr == MAP_FAILED) {
409 525 fprintf (stderr, "%s: Can't read %s: %s\n",
410   - cmdname, datafile, strerror(errno));
  526 + params.cmdname, datafile, strerror(errno));
411 527 exit (EXIT_FAILURE);
412 528 }
413 529  
414   - if (xflag) {
  530 + if (params.xflag) {
415 531 unsigned char *p = NULL;
416 532 /*
417 533 * XIP: do not append the image_header_t at the
418 534  
419 535  
420 536  
421 537  
422 538  
... ... @@ -419,29 +535,29 @@
419 535 * reserved for it.
420 536 */
421 537  
422   - if ((unsigned)sbuf.st_size < image_get_header_size ()) {
  538 + if ((unsigned)sbuf.st_size < tparams->header_size) {
423 539 fprintf (stderr,
424 540 "%s: Bad size: \"%s\" is too small for XIP\n",
425   - cmdname, datafile);
  541 + params.cmdname, datafile);
426 542 exit (EXIT_FAILURE);
427 543 }
428 544  
429   - for (p = ptr; p < ptr + image_get_header_size (); p++) {
  545 + for (p = ptr; p < ptr + tparams->header_size; p++) {
430 546 if ( *p != 0xff ) {
431 547 fprintf (stderr,
432 548 "%s: Bad file: \"%s\" has invalid buffer for XIP\n",
433   - cmdname, datafile);
  549 + params.cmdname, datafile);
434 550 exit (EXIT_FAILURE);
435 551 }
436 552 }
437 553  
438   - offset = image_get_header_size ();
  554 + offset = tparams->header_size;
439 555 }
440 556  
441 557 size = sbuf.st_size - offset;
442 558 if (write(ifd, ptr + offset, size) != size) {
443 559 fprintf (stderr, "%s: Write error on %s: %s\n",
444   - cmdname, imagefile, strerror(errno));
  560 + params.cmdname, params.imagefile, strerror(errno));
445 561 exit (EXIT_FAILURE);
446 562 }
447 563  
... ... @@ -449,7 +565,8 @@
449 565  
450 566 if (write(ifd, (char *)&zero, 4-tail) != 4-tail) {
451 567 fprintf (stderr, "%s: Write error on %s: %s\n",
452   - cmdname, imagefile, strerror(errno));
  568 + params.cmdname, params.imagefile,
  569 + strerror(errno));
453 570 exit (EXIT_FAILURE);
454 571 }
455 572 }
... ... @@ -463,7 +580,7 @@
463 580 {
464 581 fprintf (stderr, "Usage: %s -l image\n"
465 582 " -l ==> list image header information\n",
466   - cmdname);
  583 + params.cmdname);
467 584 fprintf (stderr, " %s [-x] -A arch -O os -T type -C comp "
468 585 "-a addr -e ep -n name -d data_file[:data_file...] image\n"
469 586 " -A ==> set architecture to 'arch'\n"
470 587  
471 588  
... ... @@ -475,158 +592,10 @@
475 592 " -n ==> set image name to 'name'\n"
476 593 " -d ==> use image data from 'datafile'\n"
477 594 " -x ==> set XIP (execute in place)\n",
478   - cmdname);
  595 + params.cmdname);
479 596 fprintf (stderr, " %s [-D dtc_options] -f fit-image.its fit-image\n",
480   - cmdname);
  597 + params.cmdname);
481 598  
482 599 exit (EXIT_FAILURE);
483   -}
484   -
485   -static int
486   -image_verify_header (char *ptr, int image_size)
487   -{
488   - int len;
489   - const unsigned char *data;
490   - uint32_t checksum;
491   - image_header_t header;
492   - image_header_t *hdr = &header;
493   -
494   - /*
495   - * create copy of header so that we can blank out the
496   - * checksum field for checking - this can't be done
497   - * on the PROT_READ mapped data.
498   - */
499   - memcpy (hdr, ptr, sizeof(image_header_t));
500   -
501   - if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
502   - fprintf (stderr,
503   - "%s: Bad Magic Number: \"%s\" is no valid image\n",
504   - cmdname, imagefile);
505   - return -FDT_ERR_BADMAGIC;
506   - }
507   -
508   - data = (const unsigned char *)hdr;
509   - len = sizeof(image_header_t);
510   -
511   - checksum = be32_to_cpu(hdr->ih_hcrc);
512   - hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
513   -
514   - if (crc32 (0, data, len) != checksum) {
515   - fprintf (stderr,
516   - "%s: ERROR: \"%s\" has bad header checksum!\n",
517   - cmdname, imagefile);
518   - return -FDT_ERR_BADSTATE;
519   - }
520   -
521   - data = (const unsigned char *)ptr + sizeof(image_header_t);
522   - len = image_size - sizeof(image_header_t) ;
523   -
524   - if (crc32 (0, data, len) != be32_to_cpu(hdr->ih_dcrc)) {
525   - fprintf (stderr,
526   - "%s: ERROR: \"%s\" has corrupted data!\n",
527   - cmdname, imagefile);
528   - return -FDT_ERR_BADSTRUCTURE;
529   - }
530   - return 0;
531   -}
532   -
533   -/**
534   - * fit_handle_file - main FIT file processing function
535   - *
536   - * fit_handle_file() runs dtc to convert .its to .itb, includes
537   - * binary data, updates timestamp property and calculates hashes.
538   - *
539   - * datafile - .its file
540   - * imagefile - .itb file
541   - *
542   - * returns:
543   - * only on success, otherwise calls exit (EXIT_FAILURE);
544   - */
545   -static void fit_handle_file (void)
546   -{
547   - char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
548   - char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
549   - int tfd;
550   - struct stat sbuf;
551   - unsigned char *ptr;
552   -
553   - /* call dtc to include binary properties into the tmp file */
554   - if (strlen (imagefile) + strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 >
555   - sizeof (tmpfile)) {
556   - fprintf (stderr, "%s: Image file name (%s) too long, "
557   - "can't create tmpfile",
558   - imagefile, cmdname);
559   - exit (EXIT_FAILURE);
560   - }
561   - sprintf (tmpfile, "%s%s", imagefile, MKIMAGE_TMPFILE_SUFFIX);
562   -
563   - /* dtc -I dts -O -p 200 datafile > tmpfile */
564   - sprintf (cmd, "%s %s %s > %s",
565   - MKIMAGE_DTC, opt_dtc, datafile, tmpfile);
566   - debug ("Trying to execute \"%s\"\n", cmd);
567   - if (system (cmd) == -1) {
568   - fprintf (stderr, "%s: system(%s) failed: %s\n",
569   - cmdname, cmd, strerror(errno));
570   - unlink (tmpfile);
571   - exit (EXIT_FAILURE);
572   - }
573   -
574   - /* load FIT blob into memory */
575   - tfd = open (tmpfile, O_RDWR|O_BINARY);
576   -
577   - if (tfd < 0) {
578   - fprintf (stderr, "%s: Can't open %s: %s\n",
579   - cmdname, tmpfile, strerror(errno));
580   - unlink (tmpfile);
581   - exit (EXIT_FAILURE);
582   - }
583   -
584   - if (fstat (tfd, &sbuf) < 0) {
585   - fprintf (stderr, "%s: Can't stat %s: %s\n",
586   - cmdname, tmpfile, strerror(errno));
587   - unlink (tmpfile);
588   - exit (EXIT_FAILURE);
589   - }
590   -
591   - ptr = mmap (0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, tfd, 0);
592   - if (ptr == MAP_FAILED) {
593   - fprintf (stderr, "%s: Can't read %s: %s\n",
594   - cmdname, tmpfile, strerror(errno));
595   - unlink (tmpfile);
596   - exit (EXIT_FAILURE);
597   - }
598   -
599   - /* check if ptr has a valid blob */
600   - if (fdt_check_header (ptr)) {
601   - fprintf (stderr, "%s: Invalid FIT blob\n", cmdname);
602   - unlink (tmpfile);
603   - exit (EXIT_FAILURE);
604   - }
605   -
606   - /* set hashes for images in the blob */
607   - if (fit_set_hashes (ptr)) {
608   - fprintf (stderr, "%s Can't add hashes to FIT blob", cmdname);
609   - unlink (tmpfile);
610   - exit (EXIT_FAILURE);
611   - }
612   -
613   - /* add a timestamp at offset 0 i.e., root */
614   - if (fit_set_timestamp (ptr, 0, sbuf.st_mtime)) {
615   - fprintf (stderr, "%s: Can't add image timestamp\n", cmdname);
616   - unlink (tmpfile);
617   - exit (EXIT_FAILURE);
618   - }
619   - debug ("Added timestamp successfully\n");
620   -
621   - munmap ((void *)ptr, sbuf.st_size);
622   - close (tfd);
623   -
624   - if (rename (tmpfile, imagefile) == -1) {
625   - fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
626   - cmdname, tmpfile, imagefile, strerror (errno));
627   - unlink (tmpfile);
628   - unlink (imagefile);
629   - exit (EXIT_FAILURE);
630   - }
631 600 }
... ... @@ -19,6 +19,9 @@
19 19 * MA 02111-1307 USA
20 20 */
21 21  
  22 +#ifndef _MKIIMAGE_H_
  23 +#define _MKIIMAGE_H_
  24 +
22 25 #include "os_support.h"
23 26 #include <errno.h>
24 27 #include <fcntl.h>
... ... @@ -31,7 +34,7 @@
31 34 #include <sha1.h>
32 35 #include "fdt_host.h"
33 36  
34   -#define MKIMAGE_DEBUG
  37 +#undef MKIMAGE_DEBUG
35 38  
36 39 #ifdef MKIMAGE_DEBUG
37 40 #define debug(fmt,args...) printf (fmt ,##args)
... ... @@ -44,4 +47,100 @@
44 47 #define MKIMAGE_DEFAULT_DTC_OPTIONS "-I dts -O dtb -p 500"
45 48 #define MKIMAGE_MAX_DTC_CMDLINE_LEN 512
46 49 #define MKIMAGE_DTC "dtc" /* assume dtc is in $PATH */
  50 +
  51 +/*
  52 + * This structure defines all such variables those are initialized by
  53 + * mkimage main core and need to be referred by image type specific
  54 + * functions
  55 + */
  56 +struct mkimage_params {
  57 + int dflag;
  58 + int eflag;
  59 + int fflag;
  60 + int lflag;
  61 + int vflag;
  62 + int xflag;
  63 + int os;
  64 + int arch;
  65 + int type;
  66 + int comp;
  67 + char *dtc;
  68 + unsigned int addr;
  69 + unsigned int ep;
  70 + char *imagename;
  71 + char *datafile;
  72 + char *imagefile;
  73 + char *cmdname;
  74 +};
  75 +
  76 +/*
  77 + * image type specific variables and callback functions
  78 + */
  79 +struct image_type_params {
  80 + /* name is an identification tag string for added support */
  81 + char *name;
  82 + /*
  83 + * header size is local to the specific image type to be supported,
  84 + * mkimage core treats this as number of bytes
  85 + */
  86 + uint32_t header_size;
  87 + /* Image type header pointer */
  88 + void *hdr;
  89 + /*
  90 + * There are several arguments that are passed on the command line
  91 + * and are registered as flags in mkimage_params structure.
  92 + * This callback function can be used to check the passed arguments
  93 + * are in-lined with the image type to be supported
  94 + *
  95 + * Returns 1 if parameter check is successful
  96 + */
  97 + int (*check_params) (struct mkimage_params *);
  98 + /*
  99 + * This function is used by list command (i.e. mkimage -l <filename>)
  100 + * image type verification code must be put here
  101 + *
  102 + * Returns 0 if image header verification is successful
  103 + * otherwise, returns respective negative error codes
  104 + */
  105 + int (*verify_header) (unsigned char *, int, struct mkimage_params *);
  106 + /* Prints image information abstracting from image header */
  107 + void (*print_header) (void *);
  108 + /*
  109 + * The header or image contents need to be set as per image type to
  110 + * be generated using this callback function.
  111 + * further output file post processing (for ex. checksum calculation,
  112 + * padding bytes etc..) can also be done in this callback function.
  113 + */
  114 + void (*set_header) (void *, struct stat *, int,
  115 + struct mkimage_params *);
  116 + /*
  117 + * Some image generation support for ex (default image type) supports
  118 + * more than one type_ids, this callback function is used to check
  119 + * whether input (-T <image_type>) is supported by registered image
  120 + * generation/list low level code
  121 + */
  122 + int (*check_image_type) (uint8_t);
  123 + /* This callback function will be executed if fflag is defined */
  124 + int (*fflag_handle) (struct mkimage_params *);
  125 + /* pointer to the next registered entry in linked list */
  126 + struct image_type_params *next;
  127 +};
  128 +
  129 +/*
  130 + * Exported functions
  131 + */
  132 +void mkimage_register (struct image_type_params *tparams);
  133 +
  134 +/*
  135 + * There is a c file associated with supported image type low level code
  136 + * for ex. default_image.c, fit_image.c
  137 + * init is the only function referred by mkimage core.
  138 + * to avoid a single lined header file, you can define them here
  139 + *
  140 + * Supported image types init functions
  141 + */
  142 +void init_default_image_type (void);
  143 +void init_fit_image_type (void);
  144 +
  145 +#endif /* _MKIIMAGE_H_ */