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" |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 13 | #include <fit_common.h> |
Sean Anderson | c4567c5 | 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 | e97ab72 | 2011-02-12 10:37:11 +0100 | [diff] [blame] | 16 | #include <version.h> |
Yann Dirson | 774aed5 | 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 | fabf3cf | 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 | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 23 | /* parameters initialized by core will be used by the image type code */ |
Simon Glass | 52322d6 | 2016-02-22 22:55:38 -0700 | [diff] [blame] | 24 | static struct image_tool_params params = { |
Prafulla Wadaskar | fabf3cf | 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 | 23416b8 | 2010-03-27 23:37:46 +0100 | [diff] [blame] | 30 | .imagename = "", |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 31 | .imagename2 = "", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 32 | }; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 33 | |
Simon Glass | 1d138b5 | 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 | be42159 | 2016-06-30 10:52:18 -0600 | [diff] [blame] | 46 | static int show_valid_options(enum ih_category category) |
Simon Glass | 1d138b5 | 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 | 089fc92 | 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 | 1d138b5 | 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 | d435d9f | 2016-10-27 17:54:03 -0600 | [diff] [blame] | 78 | free(order); |
Simon Glass | 1d138b5 | 2016-06-30 10:52:17 -0600 | [diff] [blame] | 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 83 | static void usage(const char *msg) |
Simon Glass | 99a7c26 | 2016-02-22 22:55:36 -0700 | [diff] [blame] | 84 | { |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 85 | fprintf(stderr, "Error: %s\n", msg); |
Pali Rohár | cdf0d1c | 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 | 138d271 | 2022-04-08 16:08:39 -0400 | [diff] [blame] | 88 | " -T ==> parse image file as 'type'\n" |
| 89 | " -q ==> quiet\n", |
Simon Glass | 99a7c26 | 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 | 138d271 | 2022-04-08 16:08:39 -0400 | [diff] [blame] | 100 | " -R ==> set second image name to 'name'\n" |
Simon Glass | 99a7c26 | 2016-02-22 22:55:36 -0700 | [diff] [blame] | 101 | " -d ==> use image data from 'datafile'\n" |
Sean Anderson | 138d271 | 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 | 99a7c26 | 2016-02-22 22:55:36 -0700 | [diff] [blame] | 105 | params.cmdname); |
| 106 | fprintf(stderr, |
Joel Stanley | 00dffdd | 2020-12-08 14:42:15 +1030 | [diff] [blame] | 107 | " %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] | 108 | " <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] | 109 | params.cmdname); |
| 110 | fprintf(stderr, |
| 111 | " -D => set all options for device tree compiler\n" |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 112 | " -f => input filename for FIT source\n" |
Joel Stanley | 00dffdd | 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 | 138d271 | 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 | 99a7c26 | 2016-02-22 22:55:36 -0700 | [diff] [blame] | 118 | #ifdef CONFIG_FIT_SIGNATURE |
| 119 | fprintf(stderr, |
Joel Stanley | 00dffdd | 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 | 99a7c26 | 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 | 5f15029 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 123 | " -g => set key name hint\n" |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 124 | " -G => use this signing key (in lieu of -k)\n" |
Simon Glass | 99a7c26 | 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 | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 127 | " -p => place external data at a static position\n" |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 128 | " -r => mark keys used as 'required' in dtb\n" |
Sean Anderson | 138d271 | 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 | 99a7c26 | 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 | f77ab20 | 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 | c4567c5 | 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 | 99a7c26 | 2016-02-22 22:55:36 -0700 | [diff] [blame] | 139 | |
| 140 | exit(EXIT_FAILURE); |
| 141 | } |
| 142 | |
Simon Glass | bd8bc5d | 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 | c4567c5 | 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 | 7106bd4 | 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 | 632d9ca | 2010-04-04 22:36:03 -0500 | [diff] [blame] | 202 | char *ptr; |
Simon Glass | 04b4607 | 2016-02-22 22:55:48 -0700 | [diff] [blame] | 203 | int type = IH_TYPE_INVALID; |
| 204 | char *datafile = NULL; |
Simon Glass | cf76cd2 | 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 | c4567c5 | 2022-06-25 13:12:19 -0400 | [diff] [blame] | 207 | while ((opt = getopt_long(argc, argv, optstring, |
| 208 | longopts, NULL)) != -1) { |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 209 | switch (opt) { |
Simon Glass | f6fe5f6 | 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 | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 217 | break; |
| 218 | case 'A': |
| 219 | params.arch = genimg_get_arch_id(optarg); |
Simon Glass | eaa2540 | 2016-06-30 10:52:19 -0600 | [diff] [blame] | 220 | if (params.arch < 0) { |
| 221 | show_valid_options(IH_ARCH); |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 222 | usage("Invalid architecture"); |
Simon Glass | eaa2540 | 2016-06-30 10:52:19 -0600 | [diff] [blame] | 223 | } |
Icenowy Zheng | 4369c97 | 2021-10-14 20:53:04 -0500 | [diff] [blame] | 224 | params.Aflag = 1; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 225 | break; |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 226 | case 'b': |
Andreas Bießmann | ddd3477 | 2016-05-03 15:17:03 +0200 | [diff] [blame] | 227 | if (add_content(IH_TYPE_FLATDT, optarg)) { |
Andreas Bießmann | f400158 | 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 | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 233 | break; |
Kever Yang | 8e238b5 | 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 | cf76cd2 | 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 | eaa2540 | 2016-06-30 10:52:19 -0600 | [diff] [blame] | 248 | if (params.comp < 0) { |
| 249 | show_valid_options(IH_COMP); |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 250 | usage("Invalid compression type"); |
Simon Glass | eaa2540 | 2016-06-30 10:52:19 -0600 | [diff] [blame] | 251 | } |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 252 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 253 | case 'd': |
| 254 | params.datafile = optarg; |
| 255 | params.dflag = 1; |
| 256 | break; |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 257 | case 'D': |
| 258 | params.dtc = optarg; |
| 259 | break; |
Simon Glass | cf76cd2 | 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); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 266 | } |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 267 | params.eflag = 1; |
| 268 | break; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 269 | case 'E': |
| 270 | params.external_data = true; |
| 271 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 272 | case 'f': |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 273 | datafile = optarg; |
| 274 | params.auto_its = !strcmp(datafile, "auto"); |
Heinrich Schuchardt | ddc38e6 | 2020-05-09 17:10:41 +0200 | [diff] [blame] | 275 | /* fallthrough */ |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 276 | case 'F': |
| 277 | /* |
| 278 | * The flattened image tree (FIT) format |
| 279 | * requires a flattened device tree image type |
| 280 | */ |
| 281 | params.type = IH_TYPE_FLATDT; |
| 282 | params.fflag = 1; |
| 283 | break; |
Sean Anderson | 5f15029 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 284 | case 'g': |
| 285 | params.keyname = optarg; |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 286 | case 'G': |
| 287 | params.keyfile = optarg; |
| 288 | break; |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 289 | case 'i': |
| 290 | params.fit_ramdisk = optarg; |
| 291 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 292 | case 'k': |
| 293 | params.keydir = optarg; |
| 294 | break; |
| 295 | case 'K': |
| 296 | params.keydest = optarg; |
| 297 | break; |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 298 | case 'l': |
| 299 | params.lflag = 1; |
| 300 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 301 | case 'n': |
| 302 | params.imagename = optarg; |
| 303 | break; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 304 | case 'N': |
| 305 | params.engine_id = optarg; |
| 306 | break; |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 307 | case 'o': |
| 308 | params.algo_name = optarg; |
| 309 | break; |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 310 | case 'O': |
| 311 | params.os = genimg_get_os_id(optarg); |
Simon Glass | eaa2540 | 2016-06-30 10:52:19 -0600 | [diff] [blame] | 312 | if (params.os < 0) { |
| 313 | show_valid_options(IH_OS); |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 314 | usage("Invalid operating system"); |
Simon Glass | eaa2540 | 2016-06-30 10:52:19 -0600 | [diff] [blame] | 315 | } |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 316 | break; |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 317 | case 'p': |
| 318 | params.external_offset = strtoull(optarg, &ptr, 16); |
| 319 | if (*ptr) { |
| 320 | fprintf(stderr, "%s: invalid offset size %s\n", |
| 321 | params.cmdname, optarg); |
| 322 | exit(EXIT_FAILURE); |
| 323 | } |
Teddy Reed | 9e3cd3c | 2016-07-11 22:54:26 -0700 | [diff] [blame] | 324 | break; |
Simon Glass | da1805f | 2016-05-01 13:55:38 -0600 | [diff] [blame] | 325 | case 'q': |
| 326 | params.quiet = 1; |
| 327 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 328 | case 'r': |
| 329 | params.require_keys = 1; |
| 330 | break; |
| 331 | case 'R': |
| 332 | /* |
| 333 | * This entry is for the second configuration |
| 334 | * file, if only one is not enough. |
| 335 | */ |
| 336 | params.imagename2 = optarg; |
| 337 | break; |
| 338 | case 's': |
| 339 | params.skipcpy = 1; |
| 340 | break; |
Simon Glass | 472ee0c | 2020-07-09 18:39:43 -0600 | [diff] [blame] | 341 | case 't': |
| 342 | params.reset_timestamp = 1; |
| 343 | break; |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 344 | case 'T': |
Baruch Siach | f77ab20 | 2017-06-29 20:37:08 +0300 | [diff] [blame] | 345 | if (strcmp(optarg, "list") == 0) { |
| 346 | show_valid_options(IH_TYPE); |
| 347 | exit(EXIT_SUCCESS); |
| 348 | } |
Simon Glass | 04b4607 | 2016-02-22 22:55:48 -0700 | [diff] [blame] | 349 | type = genimg_get_type_id(optarg); |
| 350 | if (type < 0) { |
Simon Glass | be42159 | 2016-06-30 10:52:18 -0600 | [diff] [blame] | 351 | show_valid_options(IH_TYPE); |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 352 | usage("Invalid image type"); |
Simon Glass | f6fe5f6 | 2016-02-22 22:55:35 -0700 | [diff] [blame] | 353 | } |
| 354 | break; |
Simon Glass | cf76cd2 | 2016-02-22 22:55:34 -0700 | [diff] [blame] | 355 | case 'v': |
| 356 | params.vflag++; |
| 357 | break; |
| 358 | case 'V': |
| 359 | printf("mkimage version %s\n", PLAIN_VERSION); |
| 360 | exit(EXIT_SUCCESS); |
| 361 | case 'x': |
| 362 | params.xflag++; |
| 363 | break; |
| 364 | default: |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 365 | usage("Invalid option"); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 366 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Andreas Bießmann | ddd3477 | 2016-05-03 15:17:03 +0200 | [diff] [blame] | 369 | /* The last parameter is expected to be the imagefile */ |
| 370 | if (optind < argc) |
Andreas Bießmann | f400158 | 2016-05-01 03:01:27 +0200 | [diff] [blame] | 371 | params.imagefile = argv[optind]; |
| 372 | |
Simon Glass | 04b4607 | 2016-02-22 22:55:48 -0700 | [diff] [blame] | 373 | /* |
| 374 | * For auto-generated FIT images we need to know the image type to put |
| 375 | * in the FIT, which is separate from the file's image type (which |
| 376 | * will always be IH_TYPE_FLATDT in this case). |
| 377 | */ |
| 378 | if (params.type == IH_TYPE_FLATDT) { |
Simon Glass | a89857d | 2016-06-30 10:52:07 -0600 | [diff] [blame] | 379 | params.fit_image_type = type ? type : IH_TYPE_KERNEL; |
Simon Glass | 156c349 | 2016-06-30 10:52:08 -0600 | [diff] [blame] | 380 | /* For auto_its, datafile is always 'auto' */ |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 381 | if (!params.auto_its) |
| 382 | params.datafile = datafile; |
Simon Glass | 79b5ee5 | 2016-06-30 10:52:09 -0600 | [diff] [blame] | 383 | else if (!params.datafile) |
| 384 | usage("Missing data file for auto-FIT (use -d)"); |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 385 | } else if (params.lflag || type != IH_TYPE_INVALID) { |
Alex Kiernan | 9d74c70 | 2018-04-22 05:11:17 +0000 | [diff] [blame] | 386 | if (type == IH_TYPE_SCRIPT && !params.datafile) |
| 387 | usage("Missing data file for script (use -d)"); |
Simon Glass | 04b4607 | 2016-02-22 22:55:48 -0700 | [diff] [blame] | 388 | params.type = type; |
| 389 | } |
| 390 | |
| 391 | if (!params.imagefile) |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 392 | usage("Missing output filename"); |
Simon Glass | 7106bd4 | 2016-02-22 22:55:33 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Pali Rohár | a55eaa5 | 2022-01-14 18:34:43 +0100 | [diff] [blame] | 395 | static void verify_image(const struct image_type_params *tparams) |
| 396 | { |
| 397 | struct stat sbuf; |
| 398 | void *ptr; |
| 399 | int ifd; |
| 400 | |
| 401 | ifd = open(params.imagefile, O_RDONLY | O_BINARY); |
| 402 | if (ifd < 0) { |
| 403 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 404 | params.cmdname, params.imagefile, |
| 405 | strerror(errno)); |
| 406 | exit(EXIT_FAILURE); |
| 407 | } |
| 408 | |
| 409 | if (fstat(ifd, &sbuf) < 0) { |
| 410 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 411 | params.cmdname, params.imagefile, strerror(errno)); |
| 412 | exit(EXIT_FAILURE); |
| 413 | } |
| 414 | params.file_size = sbuf.st_size; |
| 415 | |
| 416 | ptr = mmap(0, params.file_size, PROT_READ, MAP_SHARED, ifd, 0); |
| 417 | if (ptr == MAP_FAILED) { |
| 418 | fprintf(stderr, "%s: Can't map %s: %s\n", |
| 419 | params.cmdname, params.imagefile, strerror(errno)); |
| 420 | exit(EXIT_FAILURE); |
| 421 | } |
| 422 | |
| 423 | if (tparams->verify_header((unsigned char *)ptr, params.file_size, ¶ms) != 0) { |
| 424 | fprintf(stderr, "%s: Failed to verify header of %s\n", |
| 425 | params.cmdname, params.imagefile); |
| 426 | exit(EXIT_FAILURE); |
| 427 | } |
| 428 | |
| 429 | (void)munmap(ptr, params.file_size); |
| 430 | (void)close(ifd); |
| 431 | } |
| 432 | |
Mikhail Ilin | fc0453d | 2022-11-23 12:39:36 +0300 | [diff] [blame^] | 433 | void copy_datafile(int ifd, char *file) |
| 434 | { |
| 435 | if (!file) |
| 436 | return; |
| 437 | for (;;) { |
| 438 | char *sep = strchr(file, ':'); |
| 439 | |
| 440 | if (sep) { |
| 441 | *sep = '\0'; |
| 442 | copy_file(ifd, file, 1); |
| 443 | *sep++ = ':'; |
| 444 | file = sep; |
| 445 | } else { |
| 446 | copy_file(ifd, file, 0); |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
Simon Glass | 7106bd4 | 2016-02-22 22:55:33 -0700 | [diff] [blame] | 452 | int main(int argc, char **argv) |
| 453 | { |
| 454 | int ifd = -1; |
| 455 | struct stat sbuf; |
| 456 | char *ptr; |
| 457 | int retval = 0; |
| 458 | struct image_type_params *tparams = NULL; |
| 459 | int pad_len = 0; |
| 460 | int dfd; |
Mark Tomlinson | fc49786 | 2018-08-29 10:51:14 +1200 | [diff] [blame] | 461 | size_t map_len; |
Simon Glass | 7106bd4 | 2016-02-22 22:55:33 -0700 | [diff] [blame] | 462 | |
| 463 | params.cmdname = *argv; |
| 464 | params.addr = 0; |
| 465 | params.ep = 0; |
| 466 | |
| 467 | process_args(argc, argv); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 468 | |
| 469 | /* set tparams as per input type_id */ |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 470 | tparams = imagetool_get_type(params.type); |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 471 | if (tparams == NULL && !params.lflag) { |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 472 | fprintf (stderr, "%s: unsupported type %s\n", |
| 473 | params.cmdname, genimg_get_type_name(params.type)); |
| 474 | exit (EXIT_FAILURE); |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * check the passed arguments parameters meets the requirements |
| 479 | * as per image type to be generated/listed |
| 480 | */ |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 481 | if (tparams && tparams->check_params) |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 482 | if (tparams->check_params (¶ms)) |
Simon Glass | 01168b3 | 2016-02-22 22:55:37 -0700 | [diff] [blame] | 483 | usage("Bad parameters for image type"); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 484 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 485 | if (!params.eflag) { |
| 486 | params.ep = params.addr; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 487 | /* If XIP, entry point must be after the U-Boot header */ |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 488 | if (params.xflag && tparams) |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 489 | params.ep += tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 492 | if (params.fflag){ |
Heinrich Schuchardt | 8b933b4 | 2022-03-01 08:53:56 +0100 | [diff] [blame] | 493 | if (!tparams) { |
| 494 | fprintf(stderr, "%s: Missing FIT support\n", |
| 495 | params.cmdname); |
| 496 | exit (EXIT_FAILURE); |
| 497 | } |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 498 | if (tparams->fflag_handle) |
| 499 | /* |
| 500 | * in some cases, some additional processing needs |
| 501 | * to be done if fflag is defined |
| 502 | * |
| 503 | * For ex. fit_handle_file for Fit file support |
| 504 | */ |
| 505 | retval = tparams->fflag_handle(¶ms); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 506 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 507 | if (retval != EXIT_SUCCESS) |
Heinrich Schuchardt | 8b933b4 | 2022-03-01 08:53:56 +0100 | [diff] [blame] | 508 | usage("Bad parameters for FIT image type"); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Peter Tyser | b2f8b94 | 2009-11-24 16:42:10 -0600 | [diff] [blame] | 511 | if (params.lflag || params.fflag) { |
| 512 | ifd = open (params.imagefile, O_RDONLY|O_BINARY); |
| 513 | } else { |
| 514 | ifd = open (params.imagefile, |
| 515 | O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); |
| 516 | } |
| 517 | |
| 518 | if (ifd < 0) { |
| 519 | fprintf (stderr, "%s: Can't open %s: %s\n", |
| 520 | params.cmdname, params.imagefile, |
| 521 | strerror(errno)); |
| 522 | exit (EXIT_FAILURE); |
| 523 | } |
| 524 | |
| 525 | if (params.lflag || params.fflag) { |
Yann Dirson | 774aed5 | 2021-05-18 10:59:04 +0200 | [diff] [blame] | 526 | uint64_t size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 527 | /* |
| 528 | * list header information of existing image |
| 529 | */ |
| 530 | if (fstat(ifd, &sbuf) < 0) { |
| 531 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 532 | params.cmdname, params.imagefile, |
| 533 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 534 | exit (EXIT_FAILURE); |
| 535 | } |
| 536 | |
Yann Dirson | 774aed5 | 2021-05-18 10:59:04 +0200 | [diff] [blame] | 537 | if ((sbuf.st_mode & S_IFMT) == S_IFBLK) { |
| 538 | #ifdef __linux__ |
| 539 | #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64) |
| 540 | #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */ |
| 541 | #endif |
| 542 | if (ioctl(ifd, BLKGETSIZE64, &size) < 0) { |
| 543 | fprintf (stderr, |
| 544 | "%s: failed to get size of block device \"%s\"\n", |
| 545 | params.cmdname, params.imagefile); |
| 546 | exit (EXIT_FAILURE); |
| 547 | } |
| 548 | #else |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 549 | fprintf (stderr, |
Yann Dirson | 774aed5 | 2021-05-18 10:59:04 +0200 | [diff] [blame] | 550 | "%s: \"%s\" is block device, don't know how to get its size\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 551 | params.cmdname, params.imagefile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 552 | exit (EXIT_FAILURE); |
Yann Dirson | 774aed5 | 2021-05-18 10:59:04 +0200 | [diff] [blame] | 553 | #endif |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 554 | } else if (tparams && sbuf.st_size < (off_t)tparams->header_size) { |
Yann Dirson | 774aed5 | 2021-05-18 10:59:04 +0200 | [diff] [blame] | 555 | fprintf (stderr, |
Heinrich Schuchardt | 5a3b8dc | 2022-01-15 20:12:56 +0100 | [diff] [blame] | 556 | "%s: Bad size: \"%s\" is not valid image: size %llu < %u\n", |
Yann Dirson | 774aed5 | 2021-05-18 10:59:04 +0200 | [diff] [blame] | 557 | params.cmdname, params.imagefile, |
Heinrich Schuchardt | 5a3b8dc | 2022-01-15 20:12:56 +0100 | [diff] [blame] | 558 | (unsigned long long) sbuf.st_size, |
| 559 | tparams->header_size); |
Yann Dirson | 774aed5 | 2021-05-18 10:59:04 +0200 | [diff] [blame] | 560 | exit (EXIT_FAILURE); |
| 561 | } else { |
| 562 | size = sbuf.st_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Yann Dirson | 774aed5 | 2021-05-18 10:59:04 +0200 | [diff] [blame] | 565 | ptr = mmap(0, size, PROT_READ, MAP_SHARED, ifd, 0); |
Mike Frysinger | cd7e1a3 | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 566 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 567 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 568 | params.cmdname, params.imagefile, |
| 569 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 570 | exit (EXIT_FAILURE); |
| 571 | } |
| 572 | |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 573 | /* |
| 574 | * Verifies the header format based on the expected header for image |
| 575 | * type in tparams. If tparams is NULL simply check all image types |
| 576 | * to find one that matches our header. |
| 577 | */ |
| 578 | retval = imagetool_verify_print_header(ptr, &sbuf, tparams, ¶ms); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 579 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 580 | (void) munmap((void *)ptr, sbuf.st_size); |
| 581 | (void) close (ifd); |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 582 | if (!retval) |
| 583 | summary_show(¶ms.summary, params.imagefile, |
| 584 | params.keydest); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 585 | |
Prafulla Wadaskar | 139cb08 | 2009-08-10 18:49:37 +0530 | [diff] [blame] | 586 | exit (retval); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Marek Vasut | 4cc8627 | 2015-12-07 18:01:54 +0100 | [diff] [blame] | 589 | if ((params.type != IH_TYPE_MULTI) && (params.type != IH_TYPE_SCRIPT)) { |
Philippe De Swert | 3edd041 | 2015-12-04 00:11:23 +0200 | [diff] [blame] | 590 | dfd = open(params.datafile, O_RDONLY | O_BINARY); |
| 591 | if (dfd < 0) { |
| 592 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 593 | params.cmdname, params.datafile, |
| 594 | strerror(errno)); |
| 595 | exit(EXIT_FAILURE); |
| 596 | } |
Simon Glass | 139651f | 2015-06-23 15:39:12 -0600 | [diff] [blame] | 597 | |
Philippe De Swert | 3edd041 | 2015-12-04 00:11:23 +0200 | [diff] [blame] | 598 | if (fstat(dfd, &sbuf) < 0) { |
| 599 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 600 | params.cmdname, params.datafile, |
| 601 | strerror(errno)); |
| 602 | exit(EXIT_FAILURE); |
| 603 | } |
Simon Glass | 139651f | 2015-06-23 15:39:12 -0600 | [diff] [blame] | 604 | |
Philippe De Swert | 3edd041 | 2015-12-04 00:11:23 +0200 | [diff] [blame] | 605 | params.file_size = sbuf.st_size + tparams->header_size; |
| 606 | close(dfd); |
| 607 | } |
Simon Glass | 139651f | 2015-06-23 15:39:12 -0600 | [diff] [blame] | 608 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 609 | /* |
Stefano Babic | 4a96330 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 610 | * In case there an header with a variable |
| 611 | * length will be added, the corresponding |
| 612 | * function is called. This is responsible to |
| 613 | * allocate memory for the header itself. |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 614 | */ |
Stefano Babic | 4a96330 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 615 | if (tparams->vrec_header) |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 616 | pad_len = tparams->vrec_header(¶ms, tparams); |
Stefano Babic | 4a96330 | 2011-09-15 23:50:16 +0000 | [diff] [blame] | 617 | else |
| 618 | memset(tparams->hdr, 0, tparams->header_size); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 619 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 620 | if (write(ifd, tparams->hdr, tparams->header_size) |
| 621 | != tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 622 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 623 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 624 | exit (EXIT_FAILURE); |
| 625 | } |
| 626 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 627 | if (!params.skipcpy) { |
| 628 | if (params.type == IH_TYPE_MULTI || |
| 629 | params.type == IH_TYPE_SCRIPT) { |
| 630 | char *file = params.datafile; |
| 631 | uint32_t size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 632 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 633 | for (;;) { |
| 634 | char *sep = NULL; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 635 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 636 | if (file) { |
| 637 | if ((sep = strchr(file, ':')) != NULL) { |
| 638 | *sep = '\0'; |
| 639 | } |
| 640 | |
| 641 | if (stat (file, &sbuf) < 0) { |
| 642 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
| 643 | params.cmdname, file, strerror(errno)); |
| 644 | exit (EXIT_FAILURE); |
| 645 | } |
| 646 | size = cpu_to_uimage (sbuf.st_size); |
| 647 | } else { |
| 648 | size = 0; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 651 | if (write(ifd, (char *)&size, sizeof(size)) != sizeof(size)) { |
| 652 | fprintf (stderr, "%s: Write error on %s: %s\n", |
| 653 | params.cmdname, params.imagefile, |
| 654 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 655 | exit (EXIT_FAILURE); |
| 656 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 657 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 658 | if (!file) { |
| 659 | break; |
| 660 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 661 | |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 662 | if (sep) { |
| 663 | *sep = ':'; |
| 664 | file = sep + 1; |
| 665 | } else { |
| 666 | file = NULL; |
| 667 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 668 | } |
Mikhail Ilin | fc0453d | 2022-11-23 12:39:36 +0300 | [diff] [blame^] | 669 | copy_datafile(ifd, params.datafile); |
Shaohui Xie | ea65fd8 | 2012-08-10 02:49:35 +0000 | [diff] [blame] | 670 | } else if (params.type == IH_TYPE_PBLIMAGE) { |
| 671 | /* PBL has special Image format, implements its' own */ |
| 672 | pbl_load_uboot(ifd, ¶ms); |
Alexander Graf | 5329d67 | 2018-04-13 14:18:52 +0200 | [diff] [blame] | 673 | } else if (params.type == IH_TYPE_ZYNQMPBIF) { |
| 674 | /* Image file is meta, walk through actual targets */ |
| 675 | int ret; |
| 676 | |
| 677 | ret = zynqmpbif_copy_image(ifd, ¶ms); |
| 678 | if (ret) |
| 679 | return ret; |
Peng Fan | 60a5607 | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 680 | } else if (params.type == IH_TYPE_IMX8IMAGE) { |
| 681 | /* i.MX8/8X has special Image format */ |
| 682 | int ret; |
| 683 | |
| 684 | ret = imx8image_copy_image(ifd, ¶ms); |
| 685 | if (ret) |
| 686 | return ret; |
Peng Fan | 31c51da | 2018-11-20 10:19:36 +0000 | [diff] [blame] | 687 | } else if (params.type == IH_TYPE_IMX8MIMAGE) { |
| 688 | /* i.MX8M has special Image format */ |
| 689 | int ret; |
| 690 | |
| 691 | ret = imx8mimage_copy_image(ifd, ¶ms); |
| 692 | if (ret) |
| 693 | return ret; |
Jeffy Chen | ec0d2ea | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 694 | } else if ((params.type == IH_TYPE_RKSD) || |
| 695 | (params.type == IH_TYPE_RKSPI)) { |
| 696 | /* Rockchip has special Image format */ |
| 697 | int ret; |
| 698 | |
| 699 | ret = rockchip_copy_image(ifd, ¶ms); |
| 700 | if (ret) |
| 701 | return ret; |
Christian Riesch | c14f281 | 2011-12-09 09:47:38 +0000 | [diff] [blame] | 702 | } else { |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 703 | copy_file(ifd, params.datafile, pad_len); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 704 | } |
Sven Ebenfeld | 59697a2 | 2016-11-06 16:37:56 +0100 | [diff] [blame] | 705 | if (params.type == IH_TYPE_FIRMWARE_IVT) { |
| 706 | /* Add alignment and IVT */ |
Kever Yang | 86f2601 | 2020-03-30 11:56:19 +0800 | [diff] [blame] | 707 | uint32_t aligned_filesize = ALIGN(params.file_size, |
| 708 | 0x1000); |
Sven Ebenfeld | 59697a2 | 2016-11-06 16:37:56 +0100 | [diff] [blame] | 709 | flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 }, |
| 710 | params.addr, 0, 0, 0, params.addr |
| 711 | + aligned_filesize |
| 712 | - tparams->header_size, |
| 713 | params.addr + aligned_filesize |
| 714 | - tparams->header_size |
| 715 | + 0x20, 0 }; |
| 716 | int i = params.file_size; |
| 717 | for (; i < aligned_filesize; i++) { |
Sven Ebenfeld | 3bb71e0 | 2017-01-17 19:36:51 +0100 | [diff] [blame] | 718 | if (write(ifd, (char *) &i, 1) != 1) { |
Sven Ebenfeld | 59697a2 | 2016-11-06 16:37:56 +0100 | [diff] [blame] | 719 | fprintf(stderr, |
| 720 | "%s: Write error on %s: %s\n", |
| 721 | params.cmdname, |
| 722 | params.imagefile, |
| 723 | strerror(errno)); |
| 724 | exit(EXIT_FAILURE); |
| 725 | } |
| 726 | } |
| 727 | if (write(ifd, &ivt_header, sizeof(flash_header_v2_t)) |
| 728 | != sizeof(flash_header_v2_t)) { |
| 729 | fprintf(stderr, "%s: Write error on %s: %s\n", |
| 730 | params.cmdname, |
| 731 | params.imagefile, |
| 732 | strerror(errno)); |
| 733 | exit(EXIT_FAILURE); |
| 734 | } |
| 735 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 739 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 740 | !defined(__sun__) && \ |
| 741 | !defined(__FreeBSD__) && \ |
Luka Perkov | 98c689b | 2014-08-09 18:17:47 +0200 | [diff] [blame] | 742 | !defined(__OpenBSD__) && \ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 743 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 744 | (void) fdatasync (ifd); |
| 745 | #else |
| 746 | (void) fsync (ifd); |
| 747 | #endif |
| 748 | |
| 749 | if (fstat(ifd, &sbuf) < 0) { |
| 750 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 751 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 752 | exit (EXIT_FAILURE); |
| 753 | } |
Simon Glass | 139651f | 2015-06-23 15:39:12 -0600 | [diff] [blame] | 754 | params.file_size = sbuf.st_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 755 | |
Mark Tomlinson | fc49786 | 2018-08-29 10:51:14 +1200 | [diff] [blame] | 756 | map_len = sbuf.st_size; |
| 757 | 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] | 758 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 759 | fprintf (stderr, "%s: Can't map %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 760 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 761 | exit (EXIT_FAILURE); |
| 762 | } |
| 763 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 764 | /* Setup the image header as per input image type*/ |
| 765 | if (tparams->set_header) |
| 766 | tparams->set_header (ptr, &sbuf, ifd, ¶ms); |
| 767 | else { |
| 768 | fprintf (stderr, "%s: Can't set header for %s: %s\n", |
| 769 | params.cmdname, tparams->name, strerror(errno)); |
| 770 | exit (EXIT_FAILURE); |
| 771 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 772 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 773 | /* Print the image information by processing image header */ |
| 774 | if (tparams->print_header) |
| 775 | tparams->print_header (ptr); |
| 776 | else { |
Guillaume GARDET | dfe25fa | 2018-03-30 10:28:19 +0200 | [diff] [blame] | 777 | fprintf (stderr, "%s: Can't print header for %s\n", |
| 778 | params.cmdname, tparams->name); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 779 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 780 | |
Mark Tomlinson | fc49786 | 2018-08-29 10:51:14 +1200 | [diff] [blame] | 781 | (void)munmap((void *)ptr, map_len); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 782 | |
| 783 | /* We're a bit of paranoid */ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 784 | #if defined(_POSIX_SYNCHRONIZED_IO) && \ |
| 785 | !defined(__sun__) && \ |
| 786 | !defined(__FreeBSD__) && \ |
Luka Perkov | 98c689b | 2014-08-09 18:17:47 +0200 | [diff] [blame] | 787 | !defined(__OpenBSD__) && \ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 788 | !defined(__APPLE__) |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 789 | (void) fdatasync (ifd); |
| 790 | #else |
| 791 | (void) fsync (ifd); |
| 792 | #endif |
| 793 | |
| 794 | if (close(ifd)) { |
| 795 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 796 | params.cmdname, params.imagefile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 797 | exit (EXIT_FAILURE); |
| 798 | } |
| 799 | |
Pali Rohár | a55eaa5 | 2022-01-14 18:34:43 +0100 | [diff] [blame] | 800 | if (tparams->verify_header) |
| 801 | verify_image(tparams); |
| 802 | |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 803 | exit (EXIT_SUCCESS); |
| 804 | } |
| 805 | |
| 806 | static void |
| 807 | copy_file (int ifd, const char *datafile, int pad) |
| 808 | { |
| 809 | int dfd; |
| 810 | struct stat sbuf; |
| 811 | unsigned char *ptr; |
| 812 | int tail; |
| 813 | int zero = 0; |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 814 | uint8_t zeros[4096]; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 815 | int offset = 0; |
Mylène Josserand | 9e0e9a5 | 2020-07-08 11:52:50 +0200 | [diff] [blame] | 816 | int size, ret; |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 817 | struct image_type_params *tparams = imagetool_get_type(params.type); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 818 | |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 819 | memset(zeros, 0, sizeof(zeros)); |
| 820 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 821 | if (params.vflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 822 | fprintf (stderr, "Adding Image %s\n", datafile); |
| 823 | } |
| 824 | |
wdenk | 1ee550e | 2003-10-08 22:14:02 +0000 | [diff] [blame] | 825 | if ((dfd = open(datafile, O_RDONLY|O_BINARY)) < 0) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 826 | fprintf (stderr, "%s: Can't open %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 827 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 828 | exit (EXIT_FAILURE); |
| 829 | } |
| 830 | |
| 831 | if (fstat(dfd, &sbuf) < 0) { |
| 832 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 833 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 834 | exit (EXIT_FAILURE); |
| 835 | } |
| 836 | |
Thomas Hebb | 13895a3 | 2021-08-01 15:23:13 -0700 | [diff] [blame] | 837 | if (sbuf.st_size == 0) { |
| 838 | fprintf (stderr, "%s: Input file %s is empty, bailing out\n", |
| 839 | params.cmdname, datafile); |
| 840 | exit (EXIT_FAILURE); |
| 841 | } |
| 842 | |
Mike Frysinger | cd7e1a3 | 2008-05-01 04:13:05 -0400 | [diff] [blame] | 843 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 844 | if (ptr == MAP_FAILED) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 845 | fprintf (stderr, "%s: Can't read %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 846 | params.cmdname, datafile, strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 847 | exit (EXIT_FAILURE); |
| 848 | } |
| 849 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 850 | if (params.xflag) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 851 | unsigned char *p = NULL; |
| 852 | /* |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 853 | * XIP: do not append the struct legacy_img_hdr at the |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 854 | * beginning of the file, but consume the space |
| 855 | * reserved for it. |
| 856 | */ |
| 857 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 858 | if ((unsigned)sbuf.st_size < tparams->header_size) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 859 | fprintf (stderr, |
| 860 | "%s: Bad size: \"%s\" is too small for XIP\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 861 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 862 | exit (EXIT_FAILURE); |
| 863 | } |
| 864 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 865 | for (p = ptr; p < ptr + tparams->header_size; p++) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 866 | if ( *p != 0xff ) { |
| 867 | fprintf (stderr, |
| 868 | "%s: Bad file: \"%s\" has invalid buffer for XIP\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 869 | params.cmdname, datafile); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 870 | exit (EXIT_FAILURE); |
| 871 | } |
| 872 | } |
| 873 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 874 | offset = tparams->header_size; |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | size = sbuf.st_size - offset; |
Mylène Josserand | 9e0e9a5 | 2020-07-08 11:52:50 +0200 | [diff] [blame] | 878 | |
| 879 | ret = write(ifd, ptr + offset, size); |
| 880 | if (ret != size) { |
| 881 | if (ret < 0) |
| 882 | fprintf (stderr, "%s: Write error on %s: %s\n", |
| 883 | params.cmdname, params.imagefile, strerror(errno)); |
| 884 | else if (ret < size) |
| 885 | fprintf (stderr, "%s: Write only %d/%d bytes, "\ |
| 886 | "probably no space left on the device\n", |
| 887 | params.cmdname, ret, size); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 888 | exit (EXIT_FAILURE); |
| 889 | } |
| 890 | |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 891 | tail = size % 4; |
| 892 | if ((pad == 1) && (tail != 0)) { |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 893 | |
| 894 | if (write(ifd, (char *)&zero, 4-tail) != 4-tail) { |
| 895 | fprintf (stderr, "%s: Write error on %s: %s\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 896 | params.cmdname, params.imagefile, |
| 897 | strerror(errno)); |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 898 | exit (EXIT_FAILURE); |
| 899 | } |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 900 | } else if (pad > 1) { |
Simon Glass | 46409cc | 2015-08-30 16:55:22 -0600 | [diff] [blame] | 901 | while (pad > 0) { |
| 902 | int todo = sizeof(zeros); |
| 903 | |
| 904 | if (todo > pad) |
| 905 | todo = pad; |
| 906 | if (write(ifd, (char *)&zeros, todo) != todo) { |
| 907 | fprintf(stderr, "%s: Write error on %s: %s\n", |
| 908 | params.cmdname, params.imagefile, |
| 909 | strerror(errno)); |
| 910 | exit(EXIT_FAILURE); |
| 911 | } |
| 912 | pad -= todo; |
Stefano Babic | 6531bd9 | 2013-08-19 19:03:19 +0200 | [diff] [blame] | 913 | } |
wdenk | 5b1d713 | 2002-11-03 00:07:02 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | (void) munmap((void *)ptr, sbuf.st_size); |
| 917 | (void) close (dfd); |
| 918 | } |