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