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