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