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