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