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