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