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