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