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