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