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