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