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