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