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