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