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