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