wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 1 | /* |
Bartlomiej Sieka | d7816fb | 2008-03-11 12:34:47 +0100 | [diff] [blame] | 2 | * (C) Copyright 2008 Semihalf |
| 3 | * |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 4 | * (C) Copyright 2000-2009 |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 5 | * DENX Software Engineering |
| 6 | * Wolfgang Denk, wd@denx.de |
wdenk | db82c8e | 2004-02-26 23:01:04 +0000 | [diff] [blame] | 7 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Marian Balakowicz | 41d71ed | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 11 | #include "mkimage.h" |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 12 | #include <image.h> |
Wolfgang Denk | e97ab72 | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 13 | #include <version.h> |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 14 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 15 | static void copy_file(int, const char *, int); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 16 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 17 | /* parameters initialized by core will be used by the image type code */ |
Simon Glass | 52322d6 | 2016-02-22 22:55:38 -0700 | [diff] [blame] | 18 | static struct image_tool_params params = { |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 19 | .os = IH_OS_LINUX, |
| 20 | .arch = IH_ARCH_PPC, |
| 21 | .type = IH_TYPE_KERNEL, |
| 22 | .comp = IH_COMP_GZIP, |
| 23 | .dtc = MKIMAGE_DEFAULT_DTC_OPTIONS, |
Wolfgang Denk | 23416b8 | 2010-03-27 23:37:46 +0100 | [diff] [blame] | 24 | .imagename = "", |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 25 | .imagename2 = "", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 26 | }; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 27 | |
Simon Glass | 434a31b | 2015-06-23 15:38:25 -0600 | [diff] [blame] | 28 | static int h_compare_image_name(const void *vtype1, const void *vtype2) |
| 29 | { |
| 30 | const int *type1 = vtype1; |
| 31 | const int *type2 = vtype2; |
| 32 | const char *name1 = genimg_get_type_short_name(*type1); |
| 33 | const char *name2 = genimg_get_type_short_name(*type2); |
| 34 | |
| 35 | return strcmp(name1, name2); |
| 36 | } |
| 37 | |
| 38 | /* Show all image types supported by mkimage */ |
| 39 | static void show_image_types(void) |
| 40 | { |
| 41 | struct image_type_params *tparams; |
| 42 | int order[IH_TYPE_COUNT]; |
| 43 | int count; |
| 44 | int type; |
| 45 | int i; |
| 46 | |
| 47 | /* Sort the names in order of short name for easier reading */ |
| 48 | memset(order, '\0', sizeof(order)); |
| 49 | for (count = 0, type = 0; type < IH_TYPE_COUNT; type++) { |
| 50 | tparams = imagetool_get_type(type); |
| 51 | if (tparams) |
| 52 | order[count++] = type; |
| 53 | } |
| 54 | qsort(order, count, sizeof(int), h_compare_image_name); |
| 55 | |
| 56 | fprintf(stderr, "\nInvalid image type. Supported image types:\n"); |
| 57 | for (i = 0; i < count; i++) { |
| 58 | type = order[i]; |
| 59 | tparams = imagetool_get_type(type); |
| 60 | if (tparams) { |
| 61 | fprintf(stderr, "\t%-15s %s\n", |
| 62 | genimg_get_type_short_name(type), |
| 63 | genimg_get_type_name(type)); |
| 64 | } |
| 65 | } |
| 66 | fprintf(stderr, "\n"); |
| 67 | } |
| 68 | |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 69 | static void usage(const char *msg) |
Simon Glass | 99a7c26 | 2016-02-22 22:55:36 -0700 | [diff] [blame] | 70 | { |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 71 | fprintf(stderr, "Error: %s\n", msg); |
Simon Glass | 99a7c26 | 2016-02-22 22:55:36 -0700 | [diff] [blame] | 72 | fprintf(stderr, "Usage: %s -l image\n" |
| 73 | " -l ==> list image header information\n", |
| 74 | params.cmdname); |
| 75 | fprintf(stderr, |
| 76 | " %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n" |
| 77 | " -A ==> set architecture to 'arch'\n" |
| 78 | " -O ==> set operating system to 'os'\n" |
| 79 | " -T ==> set image type to 'type'\n" |
| 80 | " -C ==> set compression type 'comp'\n" |
| 81 | " -a ==> set load address to 'addr' (hex)\n" |
| 82 | " -e ==> set entry point to 'ep' (hex)\n" |
| 83 | " -n ==> set image name to 'name'\n" |
| 84 | " -d ==> use image data from 'datafile'\n" |
| 85 | " -x ==> set XIP (execute in place)\n", |
| 86 | params.cmdname); |
| 87 | fprintf(stderr, |
Andreas Bießmann | f400158 | 2016-05-01 03:01:27 +0200 | [diff] [blame] | 88 | " %s [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] fit-image\n" |
| 89 | " <dtb> file is used with -f auto, it may occour multiple times.\n", |
Simon Glass | 99a7c26 | 2016-02-22 22:55:36 -0700 | [diff] [blame] | 90 | params.cmdname); |
| 91 | fprintf(stderr, |
| 92 | " -D => set all options for device tree compiler\n" |
| 93 | " -f => input filename for FIT source\n"); |
| 94 | #ifdef CONFIG_FIT_SIGNATURE |
| 95 | fprintf(stderr, |
| 96 | "Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-r]\n" |
| 97 | " -k => set directory containing private keys\n" |
| 98 | " -K => write public keys to this .dtb file\n" |
| 99 | " -c => add comment in signature node\n" |
| 100 | " -F => re-sign existing FIT image\n" |
| 101 | " -r => mark keys used as 'required' in dtb\n"); |
| 102 | #else |
| 103 | fprintf(stderr, |
| 104 | "Signing / verified boot not supported (CONFIG_FIT_SIGNATURE undefined)\n"); |
| 105 | #endif |
| 106 | fprintf(stderr, " %s -V ==> print version information and exit\n", |
| 107 | params.cmdname); |
| 108 | fprintf(stderr, "Use -T to see a list of available image types\n"); |
| 109 | |
| 110 | exit(EXIT_FAILURE); |
| 111 | } |
| 112 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 113 | static int add_content(int type, const char *fname) |
| 114 | { |
| 115 | struct content_info *cont; |
| 116 | |
| 117 | cont = calloc(1, sizeof(*cont)); |
| 118 | if (!cont) |
| 119 | return -1; |
| 120 | cont->type = type; |
| 121 | cont->fname = fname; |
| 122 | if (params.content_tail) |
| 123 | params.content_tail->next = cont; |
| 124 | else |
| 125 | params.content_head = cont; |
| 126 | params.content_tail = cont; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Simon Glass | 7106bd4 | 2016-02-22 22:55:33 -0700 | [diff] [blame] | 131 | static void process_args(int argc, char **argv) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 132 | { |
Peter Tyser | 632d9ca | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 133 | char *ptr; |
Simon Glass | 04b4607 | 2016-02-22 22:55:48 -0700 | [diff] [blame] | 134 | int type = IH_TYPE_INVALID; |
| 135 | char *datafile = NULL; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 136 | int opt; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 137 | |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 138 | while ((opt = getopt(argc, argv, |
Andreas Bießmann | f400158 | 2016-05-01 03:01:27 +0200 | [diff] [blame] | 139 | "a:A:b:cC:d:D:e:Ef:Fk:K:ln:O:rR:sT:vVx")) != -1) { |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 140 | switch (opt) { |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 141 | case 'a': |
| 142 | params.addr = strtoull(optarg, &ptr, 16); |
| 143 | if (*ptr) { |
| 144 | fprintf(stderr, "%s: invalid load address %s\n", |
| 145 | params.cmdname, optarg); |
| 146 | exit(EXIT_FAILURE); |
| 147 | } |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 148 | break; |
| 149 | case 'A': |
| 150 | params.arch = genimg_get_arch_id(optarg); |
| 151 | if (params.arch < 0) |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 152 | usage("Invalid architecture"); |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 153 | break; |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 154 | case 'b': |
Andreas Bießmann | ddd3477 | 2016-05-03 15:17:03 +0200 | [diff] [blame] | 155 | if (add_content(IH_TYPE_FLATDT, optarg)) { |
Andreas Bießmann | f400158 | 2016-05-01 03:01:27 +0200 | [diff] [blame] | 156 | fprintf(stderr, |
| 157 | "%s: Out of memory adding content '%s'", |
| 158 | params.cmdname, optarg); |
| 159 | exit(EXIT_FAILURE); |
| 160 | } |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 161 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 162 | case 'c': |
| 163 | params.comment = optarg; |
| 164 | break; |
| 165 | case 'C': |
| 166 | params.comp = genimg_get_comp_id(optarg); |
| 167 | if (params.comp < 0) |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 168 | usage("Invalid compression type"); |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 169 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 170 | case 'd': |
| 171 | params.datafile = optarg; |
| 172 | params.dflag = 1; |
| 173 | break; |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 174 | case 'D': |
| 175 | params.dtc = optarg; |
| 176 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 177 | case 'e': |
| 178 | params.ep = strtoull(optarg, &ptr, 16); |
| 179 | if (*ptr) { |
| 180 | fprintf(stderr, "%s: invalid entry point %s\n", |
| 181 | params.cmdname, optarg); |
| 182 | exit(EXIT_FAILURE); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 183 | } |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 184 | params.eflag = 1; |
| 185 | break; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 186 | case 'E': |
| 187 | params.external_data = true; |
| 188 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 189 | case 'f': |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 190 | datafile = optarg; |
| 191 | params.auto_its = !strcmp(datafile, "auto"); |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 192 | /* no break */ |
| 193 | case 'F': |
| 194 | /* |
| 195 | * The flattened image tree (FIT) format |
| 196 | * requires a flattened device tree image type |
| 197 | */ |
Simon Glass | 04b4607 | 2016-02-22 22:55:48 -0700 | [diff] [blame] | 198 | params.fit_image_type = params.type; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 199 | params.type = IH_TYPE_FLATDT; |
| 200 | params.fflag = 1; |
| 201 | break; |
| 202 | case 'k': |
| 203 | params.keydir = optarg; |
| 204 | break; |
| 205 | case 'K': |
| 206 | params.keydest = optarg; |
| 207 | break; |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 208 | case 'l': |
| 209 | params.lflag = 1; |
| 210 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 211 | case 'n': |
| 212 | params.imagename = optarg; |
| 213 | break; |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 214 | case 'O': |
| 215 | params.os = genimg_get_os_id(optarg); |
| 216 | if (params.os < 0) |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 217 | usage("Invalid operating system"); |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 218 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 219 | case 'r': |
| 220 | params.require_keys = 1; |
| 221 | break; |
| 222 | case 'R': |
| 223 | /* |
| 224 | * This entry is for the second configuration |
| 225 | * file, if only one is not enough. |
| 226 | */ |
| 227 | params.imagename2 = optarg; |
| 228 | break; |
| 229 | case 's': |
| 230 | params.skipcpy = 1; |
| 231 | break; |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 232 | case 'T': |
Simon Glass | 04b4607 | 2016-02-22 22:55:48 -0700 | [diff] [blame] | 233 | type = genimg_get_type_id(optarg); |
| 234 | if (type < 0) { |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 235 | show_image_types(); |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 236 | usage("Invalid image type"); |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 237 | } |
| 238 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 239 | case 'v': |
| 240 | params.vflag++; |
| 241 | break; |
| 242 | case 'V': |
| 243 | printf("mkimage version %s\n", PLAIN_VERSION); |
| 244 | exit(EXIT_SUCCESS); |
| 245 | case 'x': |
| 246 | params.xflag++; |
| 247 | break; |
| 248 | default: |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 249 | usage("Invalid option"); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 250 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Andreas Bießmann | ddd3477 | 2016-05-03 15:17:03 +0200 | [diff] [blame] | 253 | /* The last parameter is expected to be the imagefile */ |
| 254 | if (optind < argc) |
Andreas Bießmann | f400158 | 2016-05-01 03:01:27 +0200 | [diff] [blame] | 255 | params.imagefile = argv[optind]; |
| 256 | |
Simon Glass | 04b4607 | 2016-02-22 22:55:48 -0700 | [diff] [blame] | 257 | /* |
| 258 | * For auto-generated FIT images we need to know the image type to put |
| 259 | * in the FIT, which is separate from the file's image type (which |
| 260 | * will always be IH_TYPE_FLATDT in this case). |
| 261 | */ |
| 262 | if (params.type == IH_TYPE_FLATDT) { |
| 263 | params.fit_image_type = type; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 264 | if (!params.auto_its) |
| 265 | params.datafile = datafile; |
Simon Glass | 04b4607 | 2016-02-22 22:55:48 -0700 | [diff] [blame] | 266 | } else if (type != IH_TYPE_INVALID) { |
| 267 | params.type = type; |
| 268 | } |
| 269 | |
| 270 | if (!params.imagefile) |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 271 | usage("Missing output filename"); |
Simon Glass | 7106bd4 | 2016-02-22 22:55:33 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | int main(int argc, char **argv) |
| 276 | { |
| 277 | int ifd = -1; |
| 278 | struct stat sbuf; |
| 279 | char *ptr; |
| 280 | int retval = 0; |
| 281 | struct image_type_params *tparams = NULL; |
| 282 | int pad_len = 0; |
| 283 | int dfd; |
| 284 | |
| 285 | params.cmdname = *argv; |
| 286 | params.addr = 0; |
| 287 | params.ep = 0; |
| 288 | |
| 289 | process_args(argc, argv); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 290 | |
| 291 | /* set tparams as per input type_id */ |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 292 | tparams = imagetool_get_type(params.type); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 293 | if (tparams == NULL) { |
| 294 | fprintf (stderr, "%s: unsupported type %s\n", |
| 295 | params.cmdname, genimg_get_type_name(params.type)); |
| 296 | exit (EXIT_FAILURE); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * check the passed arguments parameters meets the requirements |
| 301 | * as per image type to be generated/listed |
| 302 | */ |
| 303 | if (tparams->check_params) |
| 304 | if (tparams->check_params (¶ms)) |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 305 | usage("Bad parameters for image type"); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 306 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 307 | if (!params.eflag) { |
| 308 | params.ep = params.addr; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 309 | /* If XIP, entry point must be after the U-Boot header */ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 310 | if (params.xflag) |
| 311 | params.ep += tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 314 | if (params.fflag){ |
| 315 | if (tparams->fflag_handle) |
| 316 | /* |
| 317 | * in some cases, some additional processing needs |
| 318 | * to be done if fflag is defined |
| 319 | * |
| 320 | * For ex. fit_handle_file for Fit file support |
| 321 | */ |
| 322 | retval = tparams->fflag_handle(¶ms); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 323 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 324 | if (retval != EXIT_SUCCESS) |
| 325 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 328 | if (params.lflag || params.fflag) { |
| 329 | ifd = open (params.imagefile, O_RDONLY|O_BINARY); |
| 330 | } else { |
| 331 | ifd = open (params.imagefile, |
| 332 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); |
| 333 | } |
| 334 | |
| 335 | if (ifd < 0) { |
| 336 | fprintf (stderr, "%s: Can't open %s: %s\n", |
| 337 | params.cmdname, params.imagefile, |
| 338 | strerror(errno)); |
| 339 | exit (EXIT_FAILURE); |
| 340 | } |
| 341 | |
| 342 | if (params.lflag || params.fflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 343 | /* |
| 344 | * list header information of existing image |
| 345 | */ |
| 346 | if (fstat(ifd, &sbuf) < 0) { |
| 347 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 348 | params.cmdname, params.imagefile, |
| 349 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 350 | exit (EXIT_FAILURE); |
| 351 | } |
| 352 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 353 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 354 | fprintf (stderr, |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 355 | "%s: Bad size: \"%s\" is not valid image\n", |
| 356 | params.cmdname, params.imagefile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 357 | exit (EXIT_FAILURE); |
| 358 | } |
| 359 | |
Mike Frysinger | cd7e1a3 | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 360 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); |
| 361 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 362 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 363 | params.cmdname, params.imagefile, |
| 364 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 365 | exit (EXIT_FAILURE); |
| 366 | } |
| 367 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 368 | /* |
| 369 | * scan through mkimage registry for all supported image types |
| 370 | * and verify the input image file header for match |
| 371 | * Print the image information for matched image type |
| 372 | * Returns the error code if not matched |
| 373 | */ |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 374 | retval = imagetool_verify_print_header(ptr, &sbuf, |
| 375 | tparams, ¶ms); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 376 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 377 | (void) munmap((void *)ptr, sbuf.st_size); |
| 378 | (void) close (ifd); |
| 379 | |
Prafulla Wadaskar | 139cb08 | 2009-08-10 18:49:37 +0530 | [diff] [blame] | 380 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Marek Vasut | 4cc8627 | 2015-12-07 18:01:54 +0100 | [diff] [blame] | 383 | if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) { |
Philippe De Swert | 3edd041 | 2015-12-04 00:11:23 +0200 | [diff] [blame] | 384 | dfd = open(params.datafile, O_RDONLY | O_BINARY); |
| 385 | if (dfd < 0) { |
| 386 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 387 | params.cmdname, params.datafile, |
| 388 | strerror(errno)); |
| 389 | exit(EXIT_FAILURE); |
| 390 | } |
Simon Glass | 139651f | 2015-06-23 15:39:12 -0600 | [diff] [blame] | 391 | |
Philippe De Swert | 3edd041 | 2015-12-04 00:11:23 +0200 | [diff] [blame] | 392 | if (fstat(dfd, &sbuf) < 0) { |
| 393 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 394 | params.cmdname, params.datafile, |
| 395 | strerror(errno)); |
| 396 | exit(EXIT_FAILURE); |
| 397 | } |
Simon Glass | 139651f | 2015-06-23 15:39:12 -0600 | [diff] [blame] | 398 | |
Philippe De Swert | 3edd041 | 2015-12-04 00:11:23 +0200 | [diff] [blame] | 399 | params.file_size = sbuf.st_size + tparams->header_size; |
| 400 | close(dfd); |
| 401 | } |
Simon Glass | 139651f | 2015-06-23 15:39:12 -0600 | [diff] [blame] | 402 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 403 | /* |
Stefano Babic | 4a96330 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 404 | * In case there an header with a variable |
| 405 | * length will be added, the corresponding |
| 406 | * function is called. This is responsible to |
| 407 | * allocate memory for the header itself. |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 408 | */ |
Stefano Babic | 4a96330 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 409 | if (tparams->vrec_header) |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 410 | pad_len = tparams->vrec_header(¶ms, tparams); |
Stefano Babic | 4a96330 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 411 | else |
| 412 | memset(tparams->hdr, 0, tparams->header_size); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 413 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 414 | if (write(ifd, tparams->hdr, tparams->header_size) |
| 415 | != tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 416 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 417 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 418 | exit (EXIT_FAILURE); |
| 419 | } |
| 420 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 421 | if (!params.skipcpy) { |
| 422 | if (params.type == IH_TYPE_MULTI || |
| 423 | params.type == IH_TYPE_SCRIPT) { |
| 424 | char *file = params.datafile; |
| 425 | uint32_t size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 426 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 427 | for (;;) { |
| 428 | char *sep = NULL; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 429 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 430 | if (file) { |
| 431 | if ((sep = strchr(file, ':')) != NULL) { |
| 432 | *sep = '\0'; |
| 433 | } |
| 434 | |
| 435 | if (stat (file, &sbuf) < 0) { |
| 436 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
| 437 | params.cmdname, file, strerror(errno)); |
| 438 | exit (EXIT_FAILURE); |
| 439 | } |
| 440 | size = cpu_to_uimage (sbuf.st_size); |
| 441 | } else { |
| 442 | size = 0; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 445 | if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { |
| 446 | fprintf (stderr, "%s: Write error on %s: %s\n", |
| 447 | params.cmdname, params.imagefile, |
| 448 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 449 | exit (EXIT_FAILURE); |
| 450 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 451 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 452 | if (!file) { |
| 453 | break; |
| 454 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 455 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 456 | if (sep) { |
| 457 | *sep = ':'; |
| 458 | file = sep + 1; |
| 459 | } else { |
| 460 | file = NULL; |
| 461 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 462 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 463 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 464 | file = params.datafile; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 465 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 466 | for (;;) { |
| 467 | char *sep = strchr(file, ':'); |
| 468 | if (sep) { |
| 469 | *sep = '\0'; |
| 470 | copy_file (ifd, file, 1); |
| 471 | *sep++ = ':'; |
| 472 | file = sep; |
| 473 | } else { |
| 474 | copy_file (ifd, file, 0); |
| 475 | break; |
| 476 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 477 | } |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 478 | } else if (params.type == IH_TYPE_PBLIMAGE) { |
| 479 | /* PBL has special Image format, implements its' own */ |
| 480 | pbl_load_uboot(ifd, ¶ms); |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 481 | } else { |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 482 | copy_file(ifd, params.datafile, pad_len); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 483 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 487 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 488 | !defined(__sun__) && \ |
| 489 | !defined(__FreeBSD__) && \ |
Luka Perkov | 98c689b | 2014-08-09 18:17:47 +0200 | [diff] [blame] | 490 | !defined(__OpenBSD__) && \ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 491 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 492 | (void) fdatasync (ifd); |
| 493 | #else |
| 494 | (void) fsync (ifd); |
| 495 | #endif |
| 496 | |
| 497 | if (fstat(ifd, &sbuf) < 0) { |
| 498 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 499 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 500 | exit (EXIT_FAILURE); |
| 501 | } |
Simon Glass | 139651f | 2015-06-23 15:39:12 -0600 | [diff] [blame] | 502 | params.file_size = sbuf.st_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 503 | |
Mike Frysinger | cd7e1a3 | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 504 | ptr = mmap(0, sbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, ifd, 0); |
| 505 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 506 | fprintf (stderr, "%s: Can't map %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 507 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 508 | exit (EXIT_FAILURE); |
| 509 | } |
| 510 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 511 | /* Setup the image header as per input image type*/ |
| 512 | if (tparams->set_header) |
| 513 | tparams->set_header (ptr, &sbuf, ifd, ¶ms); |
| 514 | else { |
| 515 | fprintf (stderr, "%s: Can't set header for %s: %s\n", |
| 516 | params.cmdname, tparams->name, strerror(errno)); |
| 517 | exit (EXIT_FAILURE); |
| 518 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 519 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 520 | /* Print the image information by processing image header */ |
| 521 | if (tparams->print_header) |
| 522 | tparams->print_header (ptr); |
| 523 | else { |
| 524 | fprintf (stderr, "%s: Can't print header for %s: %s\n", |
| 525 | params.cmdname, tparams->name, strerror(errno)); |
| 526 | exit (EXIT_FAILURE); |
| 527 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 528 | |
| 529 | (void) munmap((void *)ptr, sbuf.st_size); |
| 530 | |
| 531 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 532 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 533 | !defined(__sun__) && \ |
| 534 | !defined(__FreeBSD__) && \ |
Luka Perkov | 98c689b | 2014-08-09 18:17:47 +0200 | [diff] [blame] | 535 | !defined(__OpenBSD__) && \ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 536 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 537 | (void) fdatasync (ifd); |
| 538 | #else |
| 539 | (void) fsync (ifd); |
| 540 | #endif |
| 541 | |
| 542 | if (close(ifd)) { |
| 543 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 544 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 545 | exit (EXIT_FAILURE); |
| 546 | } |
| 547 | |
| 548 | exit (EXIT_SUCCESS); |
| 549 | } |
| 550 | |
| 551 | static void |
| 552 | copy_file (int ifd, const char *datafile, int pad) |
| 553 | { |
| 554 | int dfd; |
| 555 | struct stat sbuf; |
| 556 | unsigned char *ptr; |
| 557 | int tail; |
| 558 | int zero = 0; |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 559 | uint8_t zeros[4096]; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 560 | int offset = 0; |
| 561 | int size; |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 562 | struct image_type_params *tparams = imagetool_get_type(params.type); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 563 | |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 564 | memset(zeros, 0, sizeof(zeros)); |
| 565 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 566 | if (params.vflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 567 | fprintf (stderr, "Adding Image %s\n", datafile); |
| 568 | } |
| 569 | |
wdenk | 1ee550e | 2003-10-08 22:14:02 +0000 | [diff] [blame] | 570 | if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 571 | fprintf (stderr, "%s: Can't open %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 572 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 573 | exit (EXIT_FAILURE); |
| 574 | } |
| 575 | |
| 576 | if (fstat(dfd, &sbuf) < 0) { |
| 577 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 578 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 579 | exit (EXIT_FAILURE); |
| 580 | } |
| 581 | |
Mike Frysinger | cd7e1a3 | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 582 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 583 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 584 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 585 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 586 | exit (EXIT_FAILURE); |
| 587 | } |
| 588 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 589 | if (params.xflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 590 | unsigned char *p = NULL; |
| 591 | /* |
| 592 | * XIP: do not append the image_header_t at the |
| 593 | * beginning of the file, but consume the space |
| 594 | * reserved for it. |
| 595 | */ |
| 596 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 597 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 598 | fprintf (stderr, |
| 599 | "%s: Bad size: \"%s\" is too small for XIP\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 600 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 601 | exit (EXIT_FAILURE); |
| 602 | } |
| 603 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 604 | for (p = ptr; p < ptr + tparams->header_size; p++) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 605 | if ( *p != 0xff ) { |
| 606 | fprintf (stderr, |
| 607 | "%s: Bad file: \"%s\" has invalid buffer for XIP\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 608 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 609 | exit (EXIT_FAILURE); |
| 610 | } |
| 611 | } |
| 612 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 613 | offset = tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | size = sbuf.st_size - offset; |
| 617 | if (write(ifd, ptr + offset, size) != size) { |
| 618 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 619 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 620 | exit (EXIT_FAILURE); |
| 621 | } |
| 622 | |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 623 | tail = size % 4; |
| 624 | if ((pad == 1) && (tail != 0)) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 625 | |
| 626 | if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { |
| 627 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 628 | params.cmdname, params.imagefile, |
| 629 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 630 | exit (EXIT_FAILURE); |
| 631 | } |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 632 | } else if (pad > 1) { |
Simon Glass | 46409cc | 2015-08-30 16:55:22 -0600 | [diff] [blame] | 633 | while (pad > 0) { |
| 634 | int todo = sizeof(zeros); |
| 635 | |
| 636 | if (todo > pad) |
| 637 | todo = pad; |
| 638 | if (write(ifd, (char *)&zeros, todo) != todo) { |
| 639 | fprintf(stderr, "%s: Write error on %s: %s\n", |
| 640 | params.cmdname, params.imagefile, |
| 641 | strerror(errno)); |
| 642 | exit(EXIT_FAILURE); |
| 643 | } |
| 644 | pad -= todo; |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 645 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | (void) munmap((void *)ptr, sbuf.st_size); |
| 649 | (void) close (dfd); |
| 650 | } |