Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2008 Semihalf |
| 4 | * |
| 5 | * (C) Copyright 2000-2004 |
| 6 | * DENX Software Engineering |
| 7 | * Wolfgang Denk, wd@denx.de |
| 8 | * |
| 9 | * Updated-by: Prafulla Wadaskar <prafulla@marvell.com> |
| 10 | * FIT image specific code abstracted from mkimage.c |
| 11 | * some functions added to address abstraction |
| 12 | * |
| 13 | * All rights reserved. |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 14 | */ |
| 15 | |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 16 | #include "imagetool.h" |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 17 | #include "fit_common.h" |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 18 | #include "mkimage.h" |
| 19 | #include <image.h> |
Sven Roederer | cbcb399 | 2020-04-27 02:08:39 +0200 | [diff] [blame] | 20 | #include <string.h> |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 21 | #include <stdarg.h> |
| 22 | #include <version.h> |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 23 | #include <u-boot/crc.h> |
| 24 | |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 25 | static struct legacy_img_hdr header; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 26 | |
Rasmus Villemoes | 9cc1970 | 2025-06-10 14:27:48 +0200 | [diff] [blame] | 27 | static int fit_estimate_hash_sig_size(struct image_tool_params *params, const char *fname) |
| 28 | { |
| 29 | bool signing = IMAGE_ENABLE_SIGN && (params->keydir || params->keyfile); |
| 30 | struct stat sbuf; |
| 31 | void *fdt; |
| 32 | int fd; |
| 33 | int estimate = 0; |
| 34 | int depth, noffset; |
| 35 | const char *name; |
| 36 | |
| 37 | fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, true); |
| 38 | if (fd < 0) |
| 39 | return -EIO; |
| 40 | |
| 41 | /* |
| 42 | * Walk the FIT image, looking for nodes named hash* and |
| 43 | * signature*. Since the interesting nodes are subnodes of an |
| 44 | * image or configuration node, we are only interested in |
| 45 | * those at depth exactly 3. |
| 46 | * |
| 47 | * The estimate for a hash node is based on a sha512 digest |
| 48 | * being 64 bytes, with another 64 bytes added to account for |
| 49 | * fdt structure overhead (the tags and the name of the |
| 50 | * "value" property). |
| 51 | * |
| 52 | * The estimate for a signature node is based on an rsa4096 |
| 53 | * signature being 512 bytes, with another 512 bytes to |
| 54 | * account for fdt overhead and the various other properties |
| 55 | * (hashed-nodes etc.) that will also be filled in. |
| 56 | * |
| 57 | * One could try to be more precise in the estimates by |
| 58 | * looking at the "algo" property and, in the case of |
| 59 | * configuration signatures, the sign-images property. Also, |
| 60 | * when signing an already created FIT image, the hash nodes |
| 61 | * already have properly sized value properties, so one could |
| 62 | * also take pre-existence of "value" properties in hash nodes |
| 63 | * into account. But this rather simple approach should work |
| 64 | * well enough in practice. |
| 65 | */ |
| 66 | for (depth = 0, noffset = fdt_next_node(fdt, 0, &depth); |
| 67 | noffset >= 0 && depth > 0; |
| 68 | noffset = fdt_next_node(fdt, noffset, &depth)) { |
| 69 | if (depth != 3) |
| 70 | continue; |
| 71 | |
| 72 | name = fdt_get_name(fdt, noffset, NULL); |
| 73 | if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) |
| 74 | estimate += 128; |
| 75 | |
| 76 | if (signing && !strncmp(name, FIT_SIG_NODENAME, strlen(FIT_SIG_NODENAME))) |
| 77 | estimate += 1024; |
| 78 | } |
| 79 | |
| 80 | munmap(fdt, sbuf.st_size); |
| 81 | close(fd); |
| 82 | |
| 83 | return estimate; |
| 84 | } |
| 85 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 86 | static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, |
| 87 | const char *tmpfile) |
| 88 | { |
| 89 | int tfd, destfd = 0; |
| 90 | void *dest_blob = NULL; |
| 91 | off_t destfd_size = 0; |
| 92 | struct stat sbuf; |
| 93 | void *ptr; |
| 94 | int ret = 0; |
| 95 | |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 96 | tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true, |
| 97 | false); |
Simon Glass | 3541407 | 2022-12-21 16:08:23 -0700 | [diff] [blame] | 98 | if (tfd < 0) { |
| 99 | fprintf(stderr, "Cannot map FDT file '%s'\n", tmpfile); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 100 | return -EIO; |
Simon Glass | 3541407 | 2022-12-21 16:08:23 -0700 | [diff] [blame] | 101 | } |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 102 | |
| 103 | if (params->keydest) { |
| 104 | struct stat dest_sbuf; |
| 105 | |
| 106 | destfd = mmap_fdt(params->cmdname, params->keydest, size_inc, |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 107 | &dest_blob, &dest_sbuf, false, |
| 108 | false); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 109 | if (destfd < 0) { |
| 110 | ret = -EIO; |
| 111 | goto err_keydest; |
| 112 | } |
| 113 | destfd_size = dest_sbuf.st_size; |
| 114 | } |
| 115 | |
| 116 | /* for first image creation, add a timestamp at offset 0 i.e., root */ |
Simon Glass | 472ee0c | 2020-07-09 18:39:43 -0600 | [diff] [blame] | 117 | if (params->datafile || params->reset_timestamp) { |
Alex Kiernan | c4c7e0d | 2018-06-20 20:10:51 +0000 | [diff] [blame] | 118 | time_t time = imagetool_get_source_date(params->cmdname, |
| 119 | sbuf.st_mtime); |
Vagrant Cascadian | f8e066c | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 120 | ret = fit_set_timestamp(ptr, 0, time); |
| 121 | } |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 122 | |
Paul-Erwan Rio | dcfb633 | 2023-12-21 08:26:11 +0100 | [diff] [blame] | 123 | if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && !ret) |
Philippe Reynes | 3e3899c | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 124 | ret = fit_pre_load_data(params->keydir, dest_blob, ptr); |
| 125 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 126 | if (!ret) { |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 127 | ret = fit_cipher_data(params->keydir, dest_blob, ptr, |
| 128 | params->comment, |
| 129 | params->require_keys, |
| 130 | params->engine_id, |
| 131 | params->cmdname); |
| 132 | } |
| 133 | |
| 134 | if (!ret) { |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 135 | ret = fit_add_verification_data(params->keydir, |
| 136 | params->keyfile, dest_blob, ptr, |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 137 | params->comment, |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 138 | params->require_keys, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 139 | params->engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 140 | params->cmdname, |
Simon Glass | e460726 | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 141 | params->algo_name, |
| 142 | ¶ms->summary); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if (dest_blob) { |
| 146 | munmap(dest_blob, destfd_size); |
| 147 | close(destfd); |
| 148 | } |
| 149 | |
| 150 | err_keydest: |
| 151 | munmap(ptr, sbuf.st_size); |
| 152 | close(tfd); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 153 | return ret; |
| 154 | } |
| 155 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 156 | /** |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 157 | * fit_calc_size() - Calculate the approximate size of the FIT we will generate |
| 158 | */ |
| 159 | static int fit_calc_size(struct image_tool_params *params) |
| 160 | { |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 161 | struct content_info *cont; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 162 | int size, total_size; |
| 163 | |
| 164 | size = imagetool_get_filesize(params, params->datafile); |
| 165 | if (size < 0) |
| 166 | return -1; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 167 | total_size = size; |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 168 | |
| 169 | if (params->fit_ramdisk) { |
| 170 | size = imagetool_get_filesize(params, params->fit_ramdisk); |
| 171 | if (size < 0) |
| 172 | return -1; |
| 173 | total_size += size; |
| 174 | } |
| 175 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 176 | for (cont = params->content_head; cont; cont = cont->next) { |
| 177 | size = imagetool_get_filesize(params, cont->fname); |
| 178 | if (size < 0) |
| 179 | return -1; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 180 | |
Simon Glass | 611fd91 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 181 | /* Add space for properties and hash node */ |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 182 | total_size += size + 300; |
| 183 | } |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 184 | |
| 185 | /* Add plenty of space for headers, properties, nodes, etc. */ |
| 186 | total_size += 4096; |
| 187 | |
| 188 | return total_size; |
| 189 | } |
| 190 | |
| 191 | static int fdt_property_file(struct image_tool_params *params, |
| 192 | void *fdt, const char *name, const char *fname) |
| 193 | { |
| 194 | struct stat sbuf; |
| 195 | void *ptr; |
| 196 | int ret; |
| 197 | int fd; |
| 198 | |
Ahelenia Ziemiańska | c668b15 | 2024-03-21 19:31:54 +0100 | [diff] [blame] | 199 | fd = open(fname, O_RDONLY | O_BINARY); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 200 | if (fd < 0) { |
| 201 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 202 | params->cmdname, fname, strerror(errno)); |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | if (fstat(fd, &sbuf) < 0) { |
| 207 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 208 | params->cmdname, fname, strerror(errno)); |
| 209 | goto err; |
| 210 | } |
| 211 | |
| 212 | ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr); |
| 213 | if (ret) |
Simon Glass | 9ab7f05 | 2016-03-16 07:45:42 -0600 | [diff] [blame] | 214 | goto err; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 215 | ret = read(fd, ptr, sbuf.st_size); |
| 216 | if (ret != sbuf.st_size) { |
| 217 | fprintf(stderr, "%s: Can't read %s: %s\n", |
| 218 | params->cmdname, fname, strerror(errno)); |
| 219 | goto err; |
| 220 | } |
Simon Glass | 9ab7f05 | 2016-03-16 07:45:42 -0600 | [diff] [blame] | 221 | close(fd); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | err: |
| 225 | close(fd); |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...) |
| 230 | { |
| 231 | char str[100]; |
| 232 | va_list ptr; |
| 233 | |
| 234 | va_start(ptr, fmt); |
| 235 | vsnprintf(str, sizeof(str), fmt, ptr); |
| 236 | va_end(ptr); |
| 237 | return fdt_property_string(fdt, name, str); |
| 238 | } |
| 239 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 240 | static void get_basename(char *str, int size, const char *fname) |
| 241 | { |
| 242 | const char *p, *start, *end; |
| 243 | int len; |
| 244 | |
| 245 | /* |
| 246 | * Use the base name as the 'name' field. So for example: |
| 247 | * |
| 248 | * "arch/arm/dts/sun7i-a20-bananapro.dtb" |
| 249 | * becomes "sun7i-a20-bananapro" |
| 250 | */ |
| 251 | p = strrchr(fname, '/'); |
| 252 | start = p ? p + 1 : fname; |
| 253 | p = strrchr(fname, '.'); |
| 254 | end = p ? p : fname + strlen(fname); |
| 255 | len = end - start; |
| 256 | if (len >= size) |
| 257 | len = size - 1; |
| 258 | memcpy(str, start, len); |
| 259 | str[len] = '\0'; |
| 260 | } |
| 261 | |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 262 | /** |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 263 | * fit_add_hash_or_sign() - Add a hash or signature node |
Simon Glass | 611fd91 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 264 | * |
Sean Anderson | 5f15029 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 265 | * @params: Image parameters |
Simon Glass | 611fd91 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 266 | * @fdt: Device tree to add to (in sequential-write mode) |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 267 | * @is_images_subnode: true to add hash even if key name hint is provided |
Sean Anderson | 5f15029 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 268 | * |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 269 | * If do_add_hash is false (default) and there is a key name hint, try to add |
| 270 | * a sign node to parent. Otherwise, just add a CRC. Rationale: if conf have |
| 271 | * to be signed, image/dt have to be hashed even if there is a key name hint. |
Simon Glass | 611fd91 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 272 | */ |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 273 | static void fit_add_hash_or_sign(struct image_tool_params *params, void *fdt, |
| 274 | bool is_images_subnode) |
Simon Glass | 611fd91 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 275 | { |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 276 | const char *hash_algo = "crc32"; |
| 277 | bool do_hash = false; |
| 278 | bool do_sign = false; |
| 279 | |
| 280 | switch (params->auto_fit) { |
| 281 | case AF_OFF: |
| 282 | break; |
| 283 | case AF_HASHED_IMG: |
| 284 | do_hash = is_images_subnode; |
| 285 | break; |
| 286 | case AF_SIGNED_IMG: |
| 287 | do_sign = is_images_subnode; |
| 288 | break; |
| 289 | case AF_SIGNED_CONF: |
| 290 | if (is_images_subnode) { |
| 291 | do_hash = true; |
| 292 | hash_algo = "sha1"; |
| 293 | } else { |
| 294 | do_sign = true; |
Sean Anderson | 5f15029 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 295 | } |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 296 | break; |
| 297 | default: |
| 298 | fprintf(stderr, |
| 299 | "%s: Unsupported auto FIT mode %u\n", |
| 300 | params->cmdname, params->auto_fit); |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | if (do_hash) { |
| 305 | fdt_begin_node(fdt, FIT_HASH_NODENAME); |
| 306 | fdt_property_string(fdt, FIT_ALGO_PROP, hash_algo); |
| 307 | fdt_end_node(fdt); |
| 308 | } |
Sean Anderson | 5f15029 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 309 | |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 310 | if (do_sign) { |
| 311 | fdt_begin_node(fdt, FIT_SIG_NODENAME); |
Sean Anderson | 5f15029 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 312 | fdt_property_string(fdt, FIT_ALGO_PROP, params->algo_name); |
| 313 | fdt_property_string(fdt, FIT_KEY_HINT, params->keyname); |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 314 | fdt_end_node(fdt); |
Sean Anderson | 5f15029 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 315 | } |
Simon Glass | 611fd91 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 319 | * fit_write_images() - Write out a list of images to the FIT |
| 320 | * |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 321 | * We always include the main image (params->datafile). If there are device |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 322 | * tree files, we include an fdt- node for each of those too. |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 323 | */ |
| 324 | static int fit_write_images(struct image_tool_params *params, char *fdt) |
| 325 | { |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 326 | struct content_info *cont; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 327 | const char *typename; |
| 328 | char str[100]; |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 329 | int upto; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 330 | int ret; |
| 331 | |
| 332 | fdt_begin_node(fdt, "images"); |
| 333 | |
| 334 | /* First the main image */ |
| 335 | typename = genimg_get_type_short_name(params->fit_image_type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 336 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 337 | fdt_begin_node(fdt, str); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 338 | fdt_property_string(fdt, FIT_DESC_PROP, params->imagename); |
| 339 | fdt_property_string(fdt, FIT_TYPE_PROP, typename); |
| 340 | fdt_property_string(fdt, FIT_ARCH_PROP, |
Simon Glass | aeeea7f | 2016-06-30 10:52:12 -0600 | [diff] [blame] | 341 | genimg_get_arch_short_name(params->arch)); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 342 | fdt_property_string(fdt, FIT_OS_PROP, |
| 343 | genimg_get_os_short_name(params->os)); |
| 344 | fdt_property_string(fdt, FIT_COMP_PROP, |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 345 | genimg_get_comp_short_name(params->comp)); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 346 | fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr); |
| 347 | fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 348 | |
| 349 | /* |
| 350 | * Put data last since it is large. SPL may only load the first part |
| 351 | * of the DT, so this way it can access all the above fields. |
| 352 | */ |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 353 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 354 | if (ret) |
| 355 | return ret; |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 356 | fit_add_hash_or_sign(params, fdt, true); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 357 | fdt_end_node(fdt); |
| 358 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 359 | /* Now the device tree files if available */ |
| 360 | upto = 0; |
| 361 | for (cont = params->content_head; cont; cont = cont->next) { |
| 362 | if (cont->type != IH_TYPE_FLATDT) |
| 363 | continue; |
Michal Sojka | 8a90ce8 | 2019-09-13 12:43:12 +0200 | [diff] [blame] | 364 | typename = genimg_get_type_short_name(cont->type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 365 | snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 366 | fdt_begin_node(fdt, str); |
| 367 | |
| 368 | get_basename(str, sizeof(str), cont->fname); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 369 | fdt_property_string(fdt, FIT_DESC_PROP, str); |
| 370 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, |
| 371 | cont->fname); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 372 | if (ret) |
| 373 | return ret; |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 374 | fdt_property_string(fdt, FIT_TYPE_PROP, typename); |
| 375 | fdt_property_string(fdt, FIT_ARCH_PROP, |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 376 | genimg_get_arch_short_name(params->arch)); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 377 | fdt_property_string(fdt, FIT_COMP_PROP, |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 378 | genimg_get_comp_short_name(IH_COMP_NONE)); |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 379 | fit_add_hash_or_sign(params, fdt, true); |
Sean Anderson | 5f15029 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 380 | if (ret) |
| 381 | return ret; |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 382 | fdt_end_node(fdt); |
| 383 | } |
| 384 | |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 385 | /* And a ramdisk file if available */ |
| 386 | if (params->fit_ramdisk) { |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 387 | fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1"); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 388 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 389 | fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP); |
| 390 | fdt_property_string(fdt, FIT_OS_PROP, |
| 391 | genimg_get_os_short_name(params->os)); |
Michal Sojka | 8a90ce8 | 2019-09-13 12:43:12 +0200 | [diff] [blame] | 392 | fdt_property_string(fdt, FIT_ARCH_PROP, |
| 393 | genimg_get_arch_short_name(params->arch)); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 394 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 395 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, |
| 396 | params->fit_ramdisk); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 397 | if (ret) |
| 398 | return ret; |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 399 | fit_add_hash_or_sign(params, fdt, true); |
Sean Anderson | 5f15029 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 400 | if (ret) |
| 401 | return ret; |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 402 | fdt_end_node(fdt); |
| 403 | } |
| 404 | |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 405 | fdt_end_node(fdt); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * fit_write_configs() - Write out a list of configurations to the FIT |
| 412 | * |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 413 | * If there are device tree files, we include a configuration for each, which |
| 414 | * selects the main image (params->datafile) and its corresponding device |
| 415 | * tree file. |
| 416 | * |
| 417 | * Otherwise we just create a configuration with the main image in it. |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 418 | */ |
| 419 | static void fit_write_configs(struct image_tool_params *params, char *fdt) |
| 420 | { |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 421 | struct content_info *cont; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 422 | const char *typename; |
| 423 | char str[100]; |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 424 | int upto; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 425 | |
| 426 | fdt_begin_node(fdt, "configurations"); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 427 | fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1"); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 428 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 429 | upto = 0; |
| 430 | for (cont = params->content_head; cont; cont = cont->next) { |
| 431 | if (cont->type != IH_TYPE_FLATDT) |
| 432 | continue; |
| 433 | typename = genimg_get_type_short_name(cont->type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 434 | snprintf(str, sizeof(str), "conf-%d", ++upto); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 435 | fdt_begin_node(fdt, str); |
| 436 | |
| 437 | get_basename(str, sizeof(str), cont->fname); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 438 | fdt_property_string(fdt, FIT_DESC_PROP, str); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 439 | |
| 440 | typename = genimg_get_type_short_name(params->fit_image_type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 441 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 442 | fdt_property_string(fdt, typename, str); |
Abel Vesa | 7e0cb5e | 2019-03-12 08:34:32 +0000 | [diff] [blame] | 443 | fdt_property_string(fdt, FIT_LOADABLE_PROP, str); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 444 | |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 445 | if (params->fit_ramdisk) |
| 446 | fdt_property_string(fdt, FIT_RAMDISK_PROP, |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 447 | FIT_RAMDISK_PROP "-1"); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 448 | |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 449 | snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 450 | fdt_property_string(fdt, FIT_FDT_PROP, str); |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 451 | fit_add_hash_or_sign(params, fdt, false); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 452 | fdt_end_node(fdt); |
| 453 | } |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 454 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 455 | if (!upto) { |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 456 | fdt_begin_node(fdt, "conf-1"); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 457 | typename = genimg_get_type_short_name(params->fit_image_type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 458 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 459 | fdt_property_string(fdt, typename, str); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 460 | |
| 461 | if (params->fit_ramdisk) |
| 462 | fdt_property_string(fdt, FIT_RAMDISK_PROP, |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 463 | FIT_RAMDISK_PROP "-1"); |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 464 | fit_add_hash_or_sign(params, fdt, false); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 465 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 466 | fdt_end_node(fdt); |
| 467 | } |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 468 | |
| 469 | fdt_end_node(fdt); |
| 470 | } |
| 471 | |
| 472 | static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size) |
| 473 | { |
| 474 | int ret; |
| 475 | |
| 476 | ret = fdt_create(fdt, size); |
| 477 | if (ret) |
| 478 | return ret; |
| 479 | fdt_finish_reservemap(fdt); |
| 480 | fdt_begin_node(fdt, ""); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 481 | fdt_property_strf(fdt, FIT_DESC_PROP, |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 482 | "%s image with one or more FDT blobs", |
| 483 | genimg_get_type_name(params->fit_image_type)); |
| 484 | fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION); |
| 485 | fdt_property_u32(fdt, "#address-cells", 1); |
| 486 | ret = fit_write_images(params, fdt); |
| 487 | if (ret) |
| 488 | return ret; |
| 489 | fit_write_configs(params, fdt); |
| 490 | fdt_end_node(fdt); |
| 491 | ret = fdt_finish(fdt); |
| 492 | if (ret) |
| 493 | return ret; |
| 494 | |
| 495 | return fdt_totalsize(fdt); |
| 496 | } |
| 497 | |
| 498 | static int fit_build(struct image_tool_params *params, const char *fname) |
| 499 | { |
| 500 | char *buf; |
| 501 | int size; |
| 502 | int ret; |
| 503 | int fd; |
| 504 | |
| 505 | size = fit_calc_size(params); |
| 506 | if (size < 0) |
| 507 | return -1; |
Fabio Estevam | c2248cb | 2020-07-27 21:03:13 -0300 | [diff] [blame] | 508 | buf = calloc(1, size); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 509 | if (!buf) { |
| 510 | fprintf(stderr, "%s: Out of memory (%d bytes)\n", |
| 511 | params->cmdname, size); |
| 512 | return -1; |
| 513 | } |
| 514 | ret = fit_build_fdt(params, buf, size); |
| 515 | if (ret < 0) { |
| 516 | fprintf(stderr, "%s: Failed to build FIT image\n", |
| 517 | params->cmdname); |
Simon Glass | c0ee1ca | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 518 | goto err_buf; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 519 | } |
| 520 | size = ret; |
| 521 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); |
| 522 | if (fd < 0) { |
| 523 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 524 | params->cmdname, fname, strerror(errno)); |
Tom Rini | b828665 | 2017-09-26 22:14:44 -0400 | [diff] [blame] | 525 | goto err_buf; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 526 | } |
| 527 | ret = write(fd, buf, size); |
| 528 | if (ret != size) { |
| 529 | fprintf(stderr, "%s: Can't write %s: %s\n", |
| 530 | params->cmdname, fname, strerror(errno)); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 531 | goto err; |
| 532 | } |
| 533 | close(fd); |
Simon Glass | c0ee1ca | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 534 | free(buf); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 535 | |
| 536 | return 0; |
| 537 | err: |
Simon Glass | c0ee1ca | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 538 | close(fd); |
| 539 | err_buf: |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 540 | free(buf); |
| 541 | return -1; |
| 542 | } |
| 543 | |
| 544 | /** |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 545 | * fit_extract_data() - Move all data outside the FIT |
| 546 | * |
| 547 | * This takes a normal FIT file and removes all the 'data' properties from it. |
| 548 | * The data is placed in an area after the FIT so that it can be accessed |
| 549 | * using an offset into that area. The 'data' properties turn into |
| 550 | * 'data-offset' properties. |
| 551 | * |
| 552 | * This function cannot cope with FITs with 'data-offset' properties. All |
| 553 | * data must be in 'data' properties on entry. |
| 554 | */ |
| 555 | static int fit_extract_data(struct image_tool_params *params, const char *fname) |
| 556 | { |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 557 | void *buf = NULL; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 558 | int buf_ptr; |
Roman Azarenko | 9ffe4d6 | 2023-08-25 10:10:14 +0200 | [diff] [blame] | 559 | int fit_size, unpadded_size, new_size, pad_boundary; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 560 | int fd; |
| 561 | struct stat sbuf; |
| 562 | void *fdt; |
| 563 | int ret; |
| 564 | int images; |
| 565 | int node; |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 566 | int image_number; |
| 567 | int align_size; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 568 | |
Tom Rini | 9664297 | 2020-05-06 11:05:17 -0400 | [diff] [blame] | 569 | align_size = params->bl_len ? params->bl_len : 4; |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 570 | fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 571 | if (fd < 0) |
| 572 | return -EIO; |
| 573 | fit_size = fdt_totalsize(fdt); |
| 574 | |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 575 | images = fdt_path_offset(fdt, FIT_IMAGES_PATH); |
| 576 | if (images < 0) { |
| 577 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 578 | ret = -EINVAL; |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 579 | goto err_munmap; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 580 | } |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 581 | image_number = fdtdec_get_child_count(fdt, images); |
| 582 | |
| 583 | /* |
| 584 | * Allocate space to hold the image data we will extract, |
| 585 | * extral space allocate for image alignment to prevent overflow. |
| 586 | */ |
Fabio Estevam | c2248cb | 2020-07-27 21:03:13 -0300 | [diff] [blame] | 587 | buf = calloc(1, fit_size + (align_size * image_number)); |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 588 | if (!buf) { |
| 589 | ret = -ENOMEM; |
| 590 | goto err_munmap; |
| 591 | } |
| 592 | buf_ptr = 0; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 593 | |
| 594 | for (node = fdt_first_subnode(fdt, images); |
| 595 | node >= 0; |
| 596 | node = fdt_next_subnode(fdt, node)) { |
| 597 | const char *data; |
| 598 | int len; |
| 599 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 600 | data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 601 | if (!data) |
| 602 | continue; |
| 603 | memcpy(buf + buf_ptr, data, len); |
| 604 | debug("Extracting data size %x\n", len); |
| 605 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 606 | ret = fdt_delprop(fdt, node, FIT_DATA_PROP); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 607 | if (ret) { |
| 608 | ret = -EPERM; |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 609 | goto err_munmap; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 610 | } |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 611 | if (params->external_offset > 0) { |
| 612 | /* An external offset positions the data absolutely. */ |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 613 | fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP, |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 614 | params->external_offset + buf_ptr); |
| 615 | } else { |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 616 | fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP, |
| 617 | buf_ptr); |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 618 | } |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 619 | fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len); |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 620 | buf_ptr += ALIGN(len, align_size); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /* Pack the FDT and place the data after it */ |
| 624 | fdt_pack(fdt); |
| 625 | |
Roman Azarenko | 9ffe4d6 | 2023-08-25 10:10:14 +0200 | [diff] [blame] | 626 | unpadded_size = fdt_totalsize(fdt); |
| 627 | new_size = ALIGN(unpadded_size, align_size); |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 628 | fdt_set_totalsize(fdt, new_size); |
Roman Azarenko | 9ffe4d6 | 2023-08-25 10:10:14 +0200 | [diff] [blame] | 629 | if (unpadded_size < fit_size) { |
| 630 | pad_boundary = new_size < fit_size ? new_size : fit_size; |
| 631 | memset(fdt + unpadded_size, 0, pad_boundary - unpadded_size); |
| 632 | } |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 633 | debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt)); |
| 634 | debug("External data size %x\n", buf_ptr); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 635 | munmap(fdt, sbuf.st_size); |
| 636 | |
| 637 | if (ftruncate(fd, new_size)) { |
| 638 | debug("%s: Failed to truncate file: %s\n", __func__, |
| 639 | strerror(errno)); |
| 640 | ret = -EIO; |
| 641 | goto err; |
| 642 | } |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 643 | |
| 644 | /* Check if an offset for the external data was set. */ |
| 645 | if (params->external_offset > 0) { |
| 646 | if (params->external_offset < new_size) { |
Simon Glass | 00c0677 | 2022-01-09 20:13:38 -0700 | [diff] [blame] | 647 | fprintf(stderr, |
| 648 | "External offset %x overlaps FIT length %x\n", |
| 649 | params->external_offset, new_size); |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 650 | ret = -EINVAL; |
| 651 | goto err; |
| 652 | } |
| 653 | new_size = params->external_offset; |
| 654 | } |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 655 | if (lseek(fd, new_size, SEEK_SET) < 0) { |
| 656 | debug("%s: Failed to seek to end of file: %s\n", __func__, |
| 657 | strerror(errno)); |
| 658 | ret = -EIO; |
| 659 | goto err; |
| 660 | } |
| 661 | if (write(fd, buf, buf_ptr) != buf_ptr) { |
| 662 | debug("%s: Failed to write external data to file %s\n", |
| 663 | __func__, strerror(errno)); |
| 664 | ret = -EIO; |
| 665 | goto err; |
| 666 | } |
Tom Rini | b828665 | 2017-09-26 22:14:44 -0400 | [diff] [blame] | 667 | free(buf); |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 668 | close(fd); |
| 669 | return 0; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 670 | |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 671 | err_munmap: |
| 672 | munmap(fdt, sbuf.st_size); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 673 | err: |
Bin Meng | 6eb23874 | 2020-04-18 01:59:11 -0700 | [diff] [blame] | 674 | free(buf); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 675 | close(fd); |
| 676 | return ret; |
| 677 | } |
| 678 | |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 679 | static int fit_import_data(struct image_tool_params *params, const char *fname) |
| 680 | { |
| 681 | void *fdt, *old_fdt; |
Lars Feyaerts | adbc90f | 2023-10-02 10:00:14 +0200 | [diff] [blame] | 682 | void *data = NULL; |
| 683 | const char *ext_data_prop = NULL; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 684 | int fit_size, new_size, size, data_base; |
| 685 | int fd; |
| 686 | struct stat sbuf; |
| 687 | int ret; |
| 688 | int images; |
Aristo Chen | feb939c6 | 2025-06-10 07:41:18 +0000 | [diff] [blame] | 689 | int confs; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 690 | int node; |
| 691 | |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 692 | fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false); |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 693 | if (fd < 0) |
| 694 | return -EIO; |
| 695 | fit_size = fdt_totalsize(old_fdt); |
Kever Yang | 8c11bc1 | 2020-03-30 11:56:22 +0800 | [diff] [blame] | 696 | data_base = ALIGN(fit_size, 4); |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 697 | |
| 698 | /* Allocate space to hold the new FIT */ |
| 699 | size = sbuf.st_size + 16384; |
Fabio Estevam | c2248cb | 2020-07-27 21:03:13 -0300 | [diff] [blame] | 700 | fdt = calloc(1, size); |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 701 | if (!fdt) { |
| 702 | fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n", |
| 703 | __func__, size); |
| 704 | ret = -ENOMEM; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 705 | goto err_munmap; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 706 | } |
| 707 | ret = fdt_open_into(old_fdt, fdt, size); |
| 708 | if (ret) { |
| 709 | debug("%s: Failed to expand FIT: %s\n", __func__, |
| 710 | fdt_strerror(errno)); |
| 711 | ret = -EINVAL; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 712 | goto err_munmap; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | images = fdt_path_offset(fdt, FIT_IMAGES_PATH); |
| 716 | if (images < 0) { |
| 717 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 718 | ret = -EINVAL; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 719 | goto err_munmap; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | for (node = fdt_first_subnode(fdt, images); |
| 723 | node >= 0; |
| 724 | node = fdt_next_subnode(fdt, node)) { |
| 725 | int buf_ptr; |
| 726 | int len; |
| 727 | |
Lars Feyaerts | adbc90f | 2023-10-02 10:00:14 +0200 | [diff] [blame] | 728 | /* |
| 729 | * FIT_DATA_OFFSET_PROP and FIT_DATA_POSITION_PROP are never both present, |
| 730 | * but if they are, prefer FIT_DATA_OFFSET_PROP as it was there first |
| 731 | */ |
| 732 | buf_ptr = fdtdec_get_int(fdt, node, FIT_DATA_POSITION_PROP, -1); |
| 733 | if (buf_ptr != -1) { |
| 734 | ext_data_prop = FIT_DATA_POSITION_PROP; |
| 735 | data = old_fdt + buf_ptr; |
| 736 | } |
| 737 | buf_ptr = fdtdec_get_int(fdt, node, FIT_DATA_OFFSET_PROP, -1); |
| 738 | if (buf_ptr != -1) { |
| 739 | ext_data_prop = FIT_DATA_OFFSET_PROP; |
| 740 | data = old_fdt + data_base + buf_ptr; |
| 741 | } |
| 742 | len = fdtdec_get_int(fdt, node, FIT_DATA_SIZE_PROP, -1); |
| 743 | if (!data || len == -1) |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 744 | continue; |
| 745 | debug("Importing data size %x\n", len); |
| 746 | |
Lars Feyaerts | adbc90f | 2023-10-02 10:00:14 +0200 | [diff] [blame] | 747 | ret = fdt_setprop(fdt, node, FIT_DATA_PROP, data, len); |
| 748 | ret = fdt_delprop(fdt, node, ext_data_prop); |
| 749 | |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 750 | if (ret) { |
| 751 | debug("%s: Failed to write property: %s\n", __func__, |
| 752 | fdt_strerror(ret)); |
| 753 | ret = -EINVAL; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 754 | goto err_munmap; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
Aristo Chen | feb939c6 | 2025-06-10 07:41:18 +0000 | [diff] [blame] | 758 | confs = fdt_path_offset(fdt, FIT_CONFS_PATH); |
| 759 | static const char * const props[] = { FIT_KERNEL_PROP, |
| 760 | FIT_RAMDISK_PROP, |
| 761 | FIT_FDT_PROP, |
| 762 | FIT_LOADABLE_PROP, |
| 763 | FIT_FPGA_PROP, |
| 764 | FIT_FIRMWARE_PROP, |
| 765 | FIT_SCRIPT_PROP}; |
| 766 | |
| 767 | fdt_for_each_subnode(node, fdt, confs) { |
| 768 | const char *conf_name = fdt_get_name(fdt, node, NULL); |
| 769 | |
| 770 | for (int i = 0; i < ARRAY_SIZE(props); i++) { |
| 771 | int count = fdt_stringlist_count(fdt, node, props[i]); |
| 772 | |
| 773 | if (count < 0) |
| 774 | continue; |
| 775 | |
| 776 | for (int j = 0; j < count; j++) { |
| 777 | const char *img_name = |
| 778 | fdt_stringlist_get(fdt, node, props[i], j, NULL); |
| 779 | if (!img_name || !*img_name) |
| 780 | continue; |
| 781 | |
| 782 | int img = fdt_subnode_offset(fdt, images, img_name); |
| 783 | |
| 784 | if (img < 0) { |
| 785 | fprintf(stderr, |
| 786 | "Error: configuration '%s' references undefined image '%s' in property '%s'\n", |
| 787 | conf_name, img_name, props[i]); |
| 788 | ret = FDT_ERR_NOTFOUND; |
| 789 | goto err_munmap; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 795 | munmap(old_fdt, sbuf.st_size); |
| 796 | |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 797 | /* Close the old fd so we can re-use it. */ |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 798 | close(fd); |
| 799 | |
| 800 | /* Pack the FDT and place the data after it */ |
| 801 | fdt_pack(fdt); |
| 802 | |
| 803 | new_size = fdt_totalsize(fdt); |
| 804 | debug("Size expanded from %x to %x\n", fit_size, new_size); |
| 805 | |
| 806 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); |
| 807 | if (fd < 0) { |
| 808 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 809 | params->cmdname, fname, strerror(errno)); |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 810 | ret = -EIO; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 811 | goto err; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 812 | } |
| 813 | if (write(fd, fdt, new_size) != new_size) { |
| 814 | debug("%s: Failed to write external data to file %s\n", |
| 815 | __func__, strerror(errno)); |
| 816 | ret = -EIO; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 817 | goto err; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 818 | } |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 819 | |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 820 | free(fdt); |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 821 | close(fd); |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 822 | return 0; |
| 823 | |
| 824 | err_munmap: |
Tom Rini | b828665 | 2017-09-26 22:14:44 -0400 | [diff] [blame] | 825 | munmap(old_fdt, sbuf.st_size); |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 826 | err: |
Simon Glass | 5014cc1 | 2016-03-16 07:45:38 -0600 | [diff] [blame] | 827 | free(fdt); |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 828 | close(fd); |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 829 | return ret; |
| 830 | } |
| 831 | |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 832 | /** |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 833 | * fit_handle_file - main FIT file processing function |
| 834 | * |
| 835 | * fit_handle_file() runs dtc to convert .its to .itb, includes |
| 836 | * binary data, updates timestamp property and calculates hashes. |
| 837 | * |
| 838 | * datafile - .its file |
| 839 | * imagefile - .itb file |
| 840 | * |
| 841 | * returns: |
| 842 | * only on success, otherwise calls exit (EXIT_FAILURE); |
| 843 | */ |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 844 | static int fit_handle_file(struct image_tool_params *params) |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 845 | { |
| 846 | char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 847 | char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0}; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 848 | char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 849 | size_t size_inc; |
Aristo Chen | 48f5c27 | 2025-06-10 07:41:17 +0000 | [diff] [blame] | 850 | int ret = EXIT_FAILURE; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 851 | |
| 852 | /* Flattened Image Tree (FIT) format handling */ |
| 853 | debug ("FIT format handling\n"); |
| 854 | |
| 855 | /* call dtc to include binary properties into the tmp file */ |
| 856 | if (strlen (params->imagefile) + |
| 857 | strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) { |
| 858 | fprintf (stderr, "%s: Image file name (%s) too long, " |
Sven Roederer | 88be5d3 | 2021-05-25 23:15:27 +0200 | [diff] [blame] | 859 | "can't create tmpfile.\n", |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 860 | params->imagefile, params->cmdname); |
| 861 | return (EXIT_FAILURE); |
| 862 | } |
| 863 | sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX); |
| 864 | |
Simon Glass | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 865 | /* We either compile the source file, or use the existing FIT image */ |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 866 | if (params->auto_fit) { |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 867 | if (fit_build(params, tmpfile)) { |
| 868 | fprintf(stderr, "%s: failed to build FIT\n", |
| 869 | params->cmdname); |
| 870 | return EXIT_FAILURE; |
| 871 | } |
| 872 | *cmd = '\0'; |
| 873 | } else if (params->datafile) { |
Stefan Theil | ff6c3b2 | 2018-03-08 09:00:13 +0100 | [diff] [blame] | 874 | /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */ |
| 875 | snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"", |
| 876 | MKIMAGE_DTC, params->dtc, tmpfile, params->datafile); |
Simon Glass | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 877 | debug("Trying to execute \"%s\"\n", cmd); |
| 878 | } else { |
Mirza, Taimoor | 74a21dd | 2017-10-04 20:28:03 +0500 | [diff] [blame] | 879 | snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"", |
Simon Glass | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 880 | params->imagefile, tmpfile); |
| 881 | } |
Sven Roederer | cbcb399 | 2020-04-27 02:08:39 +0200 | [diff] [blame] | 882 | if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) { |
| 883 | fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n"); |
| 884 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 885 | |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 886 | if (*cmd && system(cmd) == -1) { |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 887 | fprintf (stderr, "%s: system(%s) failed: %s\n", |
| 888 | params->cmdname, cmd, strerror(errno)); |
Simon Glass | 4161c41 | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 889 | goto err_system; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 890 | } |
| 891 | |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 892 | /* Move the data so it is internal to the FIT, if needed */ |
| 893 | ret = fit_import_data(params, tmpfile); |
| 894 | if (ret) |
| 895 | goto err_system; |
| 896 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 897 | /* |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 898 | * Copy the tmpfile to bakfile, then in the following loop |
| 899 | * we copy bakfile to tmpfile. So we always start from the |
| 900 | * beginning. |
| 901 | */ |
| 902 | sprintf(bakfile, "%s%s", tmpfile, ".bak"); |
| 903 | rename(tmpfile, bakfile); |
| 904 | |
| 905 | /* |
Rasmus Villemoes | 9cc1970 | 2025-06-10 14:27:48 +0200 | [diff] [blame] | 906 | * Set hashes for images in the blob and compute |
| 907 | * signatures. We do an attempt at estimating the expected |
| 908 | * extra size, but just in case that is not sufficient, keep |
| 909 | * trying adding 1K, with a reasonable upper bound of 64K |
| 910 | * total, until we succeed. |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 911 | */ |
Rasmus Villemoes | 9cc1970 | 2025-06-10 14:27:48 +0200 | [diff] [blame] | 912 | size_inc = fit_estimate_hash_sig_size(params, bakfile); |
| 913 | if (size_inc < 0) |
| 914 | goto err_system; |
| 915 | do { |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 916 | if (copyfile(bakfile, tmpfile) < 0) { |
| 917 | printf("Can't copy %s to %s\n", bakfile, tmpfile); |
| 918 | ret = -EIO; |
| 919 | break; |
| 920 | } |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 921 | ret = fit_add_file_data(params, size_inc, tmpfile); |
| 922 | if (!ret || ret != -ENOSPC) |
| 923 | break; |
Rasmus Villemoes | 9cc1970 | 2025-06-10 14:27:48 +0200 | [diff] [blame] | 924 | size_inc += 1024; |
| 925 | } while (size_inc < 64 * 1024); |
Simon Glass | b4d8b09 | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 926 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 927 | if (ret) { |
Simon Glass | 061752e | 2016-07-03 09:40:43 -0600 | [diff] [blame] | 928 | fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n", |
| 929 | params->cmdname, ret); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 930 | goto err_system; |
Simon Glass | b4d8b09 | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 931 | } |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 932 | |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 933 | /* Move the data so it is external to the FIT, if requested */ |
| 934 | if (params->external_data) { |
| 935 | ret = fit_extract_data(params, tmpfile); |
| 936 | if (ret) |
| 937 | goto err_system; |
| 938 | } |
| 939 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 940 | if (rename (tmpfile, params->imagefile) == -1) { |
| 941 | fprintf (stderr, "%s: Can't rename %s to %s: %s\n", |
| 942 | params->cmdname, tmpfile, params->imagefile, |
| 943 | strerror (errno)); |
| 944 | unlink (tmpfile); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 945 | unlink(bakfile); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 946 | unlink (params->imagefile); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 947 | return EXIT_FAILURE; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 948 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 949 | unlink(bakfile); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 950 | return EXIT_SUCCESS; |
Simon Glass | 4161c41 | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 951 | |
Simon Glass | 4161c41 | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 952 | err_system: |
| 953 | unlink(tmpfile); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 954 | unlink(bakfile); |
Aristo Chen | 48f5c27 | 2025-06-10 07:41:17 +0000 | [diff] [blame] | 955 | return ret; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 956 | } |
| 957 | |
Guilherme Maciel Ferreira | 3c46bcd | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 958 | /** |
| 959 | * fit_image_extract - extract a FIT component image |
| 960 | * @fit: pointer to the FIT format image header |
| 961 | * @image_noffset: offset of the component image node |
| 962 | * @file_name: name of the file to store the FIT sub-image |
| 963 | * |
| 964 | * returns: |
| 965 | * zero in case of success or a negative value if fail. |
| 966 | */ |
| 967 | static int fit_image_extract( |
| 968 | const void *fit, |
| 969 | int image_noffset, |
| 970 | const char *file_name) |
| 971 | { |
| 972 | const void *file_data; |
| 973 | size_t file_size = 0; |
Andrew F. Davis | 166d5e9 | 2019-09-17 17:09:34 -0400 | [diff] [blame] | 974 | int ret; |
Guilherme Maciel Ferreira | 3c46bcd | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 975 | |
Andrew F. Davis | 166d5e9 | 2019-09-17 17:09:34 -0400 | [diff] [blame] | 976 | /* get the data address and size of component at offset "image_noffset" */ |
Simon Glass | 833445d | 2025-01-10 17:00:13 -0700 | [diff] [blame] | 977 | ret = fit_image_get_data(fit, image_noffset, &file_data, &file_size); |
Andrew F. Davis | 166d5e9 | 2019-09-17 17:09:34 -0400 | [diff] [blame] | 978 | if (ret) { |
| 979 | fprintf(stderr, "Could not get component information\n"); |
| 980 | return ret; |
| 981 | } |
Guilherme Maciel Ferreira | 3c46bcd | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 982 | |
| 983 | /* save the "file_data" into the file specified by "file_name" */ |
| 984 | return imagetool_save_subimage(file_name, (ulong) file_data, file_size); |
| 985 | } |
| 986 | |
| 987 | /** |
| 988 | * fit_extract_contents - retrieve a sub-image component from the FIT image |
| 989 | * @ptr: pointer to the FIT format image header |
| 990 | * @params: command line parameters |
| 991 | * |
| 992 | * returns: |
| 993 | * zero in case of success or a negative value if fail. |
| 994 | */ |
| 995 | static int fit_extract_contents(void *ptr, struct image_tool_params *params) |
| 996 | { |
| 997 | int images_noffset; |
| 998 | int noffset; |
| 999 | int ndepth; |
| 1000 | const void *fit = ptr; |
| 1001 | int count = 0; |
| 1002 | const char *p; |
| 1003 | |
| 1004 | /* Indent string is defined in header image.h */ |
| 1005 | p = IMAGE_INDENT_STRING; |
| 1006 | |
Guilherme Maciel Ferreira | 3c46bcd | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 1007 | /* Find images parent node offset */ |
| 1008 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 1009 | if (images_noffset < 0) { |
| 1010 | printf("Can't find images parent node '%s' (%s)\n", |
| 1011 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 1012 | return -1; |
| 1013 | } |
| 1014 | |
| 1015 | /* Avoid any overrun */ |
| 1016 | count = fit_get_subimage_count(fit, images_noffset); |
| 1017 | if ((params->pflag < 0) || (count <= params->pflag)) { |
| 1018 | printf("No such component at '%d'\n", params->pflag); |
| 1019 | return -1; |
| 1020 | } |
| 1021 | |
| 1022 | /* Process its subnodes, extract the desired component from image */ |
| 1023 | for (ndepth = 0, count = 0, |
| 1024 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 1025 | (noffset >= 0) && (ndepth > 0); |
| 1026 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 1027 | if (ndepth == 1) { |
| 1028 | /* |
| 1029 | * Direct child node of the images parent node, |
| 1030 | * i.e. component image node. |
| 1031 | */ |
| 1032 | if (params->pflag == count) { |
| 1033 | printf("Extracted:\n%s Image %u (%s)\n", p, |
| 1034 | count, fit_get_name(fit, noffset, NULL)); |
| 1035 | |
| 1036 | fit_image_print(fit, noffset, p); |
| 1037 | |
| 1038 | return fit_image_extract(fit, noffset, |
| 1039 | params->outfile); |
| 1040 | } |
| 1041 | |
| 1042 | count++; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 1049 | static int fit_check_params(struct image_tool_params *params) |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 1050 | { |
Massimo Pegorer | 13878dd | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 1051 | if (params->auto_fit) |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 1052 | return 0; |
Heinrich Schuchardt | 915f5aa | 2019-12-11 13:51:38 +0100 | [diff] [blame] | 1053 | return ((params->dflag && params->fflag) || |
| 1054 | (params->fflag && params->lflag) || |
| 1055 | (params->lflag && params->dflag)); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 1056 | } |
| 1057 | |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 1058 | U_BOOT_IMAGE_TYPE( |
| 1059 | fitimage, |
| 1060 | "FIT Image support", |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 1061 | sizeof(struct legacy_img_hdr), |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 1062 | (void *)&header, |
| 1063 | fit_check_params, |
| 1064 | fit_verify_header, |
Pali Rohár | 0ed41e2 | 2023-03-29 21:25:54 +0200 | [diff] [blame] | 1065 | fit_print_header, |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 1066 | NULL, |
Guilherme Maciel Ferreira | 3c46bcd | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 1067 | fit_extract_contents, |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 1068 | fit_check_image_types, |
| 1069 | fit_handle_file, |
| 1070 | NULL /* FIT images use DTB header */ |
| 1071 | ); |