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