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