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