Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Prafulla Wadaskar | 89a4d6b | 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 | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 14 | */ |
| 15 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 16 | #include "imagetool.h" |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 17 | #include "fit_common.h" |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 18 | #include "mkimage.h" |
| 19 | #include <image.h> |
Sven Roederer | ea5d373 | 2020-04-27 02:08:39 +0200 | [diff] [blame] | 20 | #include <string.h> |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 21 | #include <stdarg.h> |
| 22 | #include <version.h> |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 23 | #include <u-boot/crc.h> |
| 24 | |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 25 | static struct legacy_img_hdr header; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 26 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 27 | static int fit_add_file_data(struct image_tool_params *params, size_t size_inc, |
| 28 | const char *tmpfile) |
| 29 | { |
| 30 | int tfd, destfd = 0; |
| 31 | void *dest_blob = NULL; |
| 32 | off_t destfd_size = 0; |
| 33 | struct stat sbuf; |
| 34 | void *ptr; |
| 35 | int ret = 0; |
| 36 | |
Luca Boccassi | 7d57485 | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 37 | tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true, |
| 38 | false); |
Simon Glass | 90cfae2 | 2022-12-21 16:08:23 -0700 | [diff] [blame] | 39 | if (tfd < 0) { |
| 40 | fprintf(stderr, "Cannot map FDT file '%s'\n", tmpfile); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 41 | return -EIO; |
Simon Glass | 90cfae2 | 2022-12-21 16:08:23 -0700 | [diff] [blame] | 42 | } |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 43 | |
| 44 | if (params->keydest) { |
| 45 | struct stat dest_sbuf; |
| 46 | |
| 47 | destfd = mmap_fdt(params->cmdname, params->keydest, size_inc, |
Luca Boccassi | 7d57485 | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 48 | &dest_blob, &dest_sbuf, false, |
| 49 | false); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 50 | if (destfd < 0) { |
| 51 | ret = -EIO; |
| 52 | goto err_keydest; |
| 53 | } |
| 54 | destfd_size = dest_sbuf.st_size; |
| 55 | } |
| 56 | |
| 57 | /* for first image creation, add a timestamp at offset 0 i.e., root */ |
Simon Glass | 152b246 | 2020-07-09 18:39:43 -0600 | [diff] [blame] | 58 | if (params->datafile || params->reset_timestamp) { |
Alex Kiernan | 87925df | 2018-06-20 20:10:51 +0000 | [diff] [blame] | 59 | time_t time = imagetool_get_source_date(params->cmdname, |
| 60 | sbuf.st_mtime); |
Vagrant Cascadian | 5847084 | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 61 | ret = fit_set_timestamp(ptr, 0, time); |
| 62 | } |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 63 | |
Philippe Reynes | 6e052d1 | 2022-03-28 22:57:02 +0200 | [diff] [blame] | 64 | if (!ret) |
| 65 | ret = fit_pre_load_data(params->keydir, dest_blob, ptr); |
| 66 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 67 | if (!ret) { |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 68 | ret = fit_cipher_data(params->keydir, dest_blob, ptr, |
| 69 | params->comment, |
| 70 | params->require_keys, |
| 71 | params->engine_id, |
| 72 | params->cmdname); |
| 73 | } |
| 74 | |
| 75 | if (!ret) { |
Alexandru Gagniuc | 36bfcb6 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 76 | ret = fit_add_verification_data(params->keydir, |
| 77 | params->keyfile, dest_blob, ptr, |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 78 | params->comment, |
George McCollister | f1ca1fd | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 79 | params->require_keys, |
Alex Kiernan | 795f452 | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 80 | params->engine_id, |
Jan Kiszka | 5902a39 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 81 | params->cmdname, |
Simon Glass | 2d2384b | 2021-11-12 12:28:13 -0700 | [diff] [blame] | 82 | params->algo_name, |
| 83 | ¶ms->summary); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | if (dest_blob) { |
| 87 | munmap(dest_blob, destfd_size); |
| 88 | close(destfd); |
| 89 | } |
| 90 | |
| 91 | err_keydest: |
| 92 | munmap(ptr, sbuf.st_size); |
| 93 | close(tfd); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 94 | return ret; |
| 95 | } |
| 96 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 97 | /** |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 98 | * fit_calc_size() - Calculate the approximate size of the FIT we will generate |
| 99 | */ |
| 100 | static int fit_calc_size(struct image_tool_params *params) |
| 101 | { |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 102 | struct content_info *cont; |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 103 | int size, total_size; |
| 104 | |
| 105 | size = imagetool_get_filesize(params, params->datafile); |
| 106 | if (size < 0) |
| 107 | return -1; |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 108 | total_size = size; |
Tomeu Vizoso | 0f7c6cd | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 109 | |
| 110 | if (params->fit_ramdisk) { |
| 111 | size = imagetool_get_filesize(params, params->fit_ramdisk); |
| 112 | if (size < 0) |
| 113 | return -1; |
| 114 | total_size += size; |
| 115 | } |
| 116 | |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 117 | for (cont = params->content_head; cont; cont = cont->next) { |
| 118 | size = imagetool_get_filesize(params, cont->fname); |
| 119 | if (size < 0) |
| 120 | return -1; |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 121 | |
Simon Glass | 3172911 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 122 | /* Add space for properties and hash node */ |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 123 | total_size += size + 300; |
| 124 | } |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 125 | |
| 126 | /* Add plenty of space for headers, properties, nodes, etc. */ |
| 127 | total_size += 4096; |
| 128 | |
| 129 | return total_size; |
| 130 | } |
| 131 | |
| 132 | static int fdt_property_file(struct image_tool_params *params, |
| 133 | void *fdt, const char *name, const char *fname) |
| 134 | { |
| 135 | struct stat sbuf; |
| 136 | void *ptr; |
| 137 | int ret; |
| 138 | int fd; |
| 139 | |
| 140 | fd = open(fname, O_RDWR | O_BINARY); |
| 141 | if (fd < 0) { |
| 142 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 143 | params->cmdname, fname, strerror(errno)); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | if (fstat(fd, &sbuf) < 0) { |
| 148 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 149 | params->cmdname, fname, strerror(errno)); |
| 150 | goto err; |
| 151 | } |
| 152 | |
| 153 | ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr); |
| 154 | if (ret) |
Simon Glass | 3bd3a54 | 2016-03-16 07:45:42 -0600 | [diff] [blame] | 155 | goto err; |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 156 | ret = read(fd, ptr, sbuf.st_size); |
| 157 | if (ret != sbuf.st_size) { |
| 158 | fprintf(stderr, "%s: Can't read %s: %s\n", |
| 159 | params->cmdname, fname, strerror(errno)); |
| 160 | goto err; |
| 161 | } |
Simon Glass | 3bd3a54 | 2016-03-16 07:45:42 -0600 | [diff] [blame] | 162 | close(fd); |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | err: |
| 166 | close(fd); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...) |
| 171 | { |
| 172 | char str[100]; |
| 173 | va_list ptr; |
| 174 | |
| 175 | va_start(ptr, fmt); |
| 176 | vsnprintf(str, sizeof(str), fmt, ptr); |
| 177 | va_end(ptr); |
| 178 | return fdt_property_string(fdt, name, str); |
| 179 | } |
| 180 | |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 181 | static void get_basename(char *str, int size, const char *fname) |
| 182 | { |
| 183 | const char *p, *start, *end; |
| 184 | int len; |
| 185 | |
| 186 | /* |
| 187 | * Use the base name as the 'name' field. So for example: |
| 188 | * |
| 189 | * "arch/arm/dts/sun7i-a20-bananapro.dtb" |
| 190 | * becomes "sun7i-a20-bananapro" |
| 191 | */ |
| 192 | p = strrchr(fname, '/'); |
| 193 | start = p ? p + 1 : fname; |
| 194 | p = strrchr(fname, '.'); |
| 195 | end = p ? p : fname + strlen(fname); |
| 196 | len = end - start; |
| 197 | if (len >= size) |
| 198 | len = size - 1; |
| 199 | memcpy(str, start, len); |
| 200 | str[len] = '\0'; |
| 201 | } |
| 202 | |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 203 | /** |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 204 | * fit_add_hash_or_sign() - Add a hash or signature node |
Simon Glass | 3172911 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 205 | * |
Sean Anderson | 87b0af9 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 206 | * @params: Image parameters |
Simon Glass | 3172911 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 207 | * @fdt: Device tree to add to (in sequential-write mode) |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 208 | * @is_images_subnode: true to add hash even if key name hint is provided |
Sean Anderson | 87b0af9 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 209 | * |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 210 | * If do_add_hash is false (default) and there is a key name hint, try to add |
| 211 | * a sign node to parent. Otherwise, just add a CRC. Rationale: if conf have |
| 212 | * to be signed, image/dt have to be hashed even if there is a key name hint. |
Simon Glass | 3172911 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 213 | */ |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 214 | static void fit_add_hash_or_sign(struct image_tool_params *params, void *fdt, |
| 215 | bool is_images_subnode) |
Simon Glass | 3172911 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 216 | { |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 217 | const char *hash_algo = "crc32"; |
| 218 | bool do_hash = false; |
| 219 | bool do_sign = false; |
Sean Anderson | 87b0af9 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 220 | |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 221 | switch (params->auto_fit) { |
| 222 | case AF_OFF: |
| 223 | break; |
| 224 | case AF_HASHED_IMG: |
| 225 | do_hash = is_images_subnode; |
| 226 | break; |
| 227 | case AF_SIGNED_IMG: |
| 228 | do_sign = is_images_subnode; |
| 229 | break; |
| 230 | case AF_SIGNED_CONF: |
| 231 | if (is_images_subnode) { |
| 232 | do_hash = true; |
| 233 | hash_algo = "sha1"; |
| 234 | } else { |
| 235 | do_sign = true; |
| 236 | } |
| 237 | break; |
| 238 | default: |
| 239 | fprintf(stderr, |
| 240 | "%s: Unsupported auto FIT mode %u\n", |
| 241 | params->cmdname, params->auto_fit); |
| 242 | break; |
Sean Anderson | 87b0af9 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 245 | if (do_hash) { |
| 246 | fdt_begin_node(fdt, FIT_HASH_NODENAME); |
| 247 | fdt_property_string(fdt, FIT_ALGO_PROP, hash_algo); |
| 248 | fdt_end_node(fdt); |
| 249 | } |
| 250 | |
| 251 | if (do_sign) { |
| 252 | fdt_begin_node(fdt, FIT_SIG_NODENAME); |
| 253 | fdt_property_string(fdt, FIT_ALGO_PROP, params->algo_name); |
| 254 | fdt_property_string(fdt, FIT_KEY_HINT, params->keyname); |
| 255 | fdt_end_node(fdt); |
| 256 | } |
Simon Glass | 3172911 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 260 | * fit_write_images() - Write out a list of images to the FIT |
| 261 | * |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 262 | * We always include the main image (params->datafile). If there are device |
Andre Przywara | 2eda8e9 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 263 | * tree files, we include an fdt- node for each of those too. |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 264 | */ |
| 265 | static int fit_write_images(struct image_tool_params *params, char *fdt) |
| 266 | { |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 267 | struct content_info *cont; |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 268 | const char *typename; |
| 269 | char str[100]; |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 270 | int upto; |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 271 | int ret; |
| 272 | |
| 273 | fdt_begin_node(fdt, "images"); |
| 274 | |
| 275 | /* First the main image */ |
| 276 | typename = genimg_get_type_short_name(params->fit_image_type); |
Andre Przywara | 2eda8e9 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 277 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 278 | fdt_begin_node(fdt, str); |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 279 | fdt_property_string(fdt, FIT_DESC_PROP, params->imagename); |
| 280 | fdt_property_string(fdt, FIT_TYPE_PROP, typename); |
| 281 | fdt_property_string(fdt, FIT_ARCH_PROP, |
Simon Glass | 3a45f38 | 2016-06-30 10:52:12 -0600 | [diff] [blame] | 282 | genimg_get_arch_short_name(params->arch)); |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 283 | fdt_property_string(fdt, FIT_OS_PROP, |
| 284 | genimg_get_os_short_name(params->os)); |
| 285 | fdt_property_string(fdt, FIT_COMP_PROP, |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 286 | genimg_get_comp_short_name(params->comp)); |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 287 | fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr); |
| 288 | fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep); |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * Put data last since it is large. SPL may only load the first part |
| 292 | * of the DT, so this way it can access all the above fields. |
| 293 | */ |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 294 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile); |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 295 | if (ret) |
| 296 | return ret; |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 297 | fit_add_hash_or_sign(params, fdt, true); |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 298 | fdt_end_node(fdt); |
| 299 | |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 300 | /* Now the device tree files if available */ |
| 301 | upto = 0; |
| 302 | for (cont = params->content_head; cont; cont = cont->next) { |
| 303 | if (cont->type != IH_TYPE_FLATDT) |
| 304 | continue; |
Michal Sojka | 12e288a | 2019-09-13 12:43:12 +0200 | [diff] [blame] | 305 | typename = genimg_get_type_short_name(cont->type); |
Andre Przywara | 2eda8e9 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 306 | snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto); |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 307 | fdt_begin_node(fdt, str); |
| 308 | |
| 309 | get_basename(str, sizeof(str), cont->fname); |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 310 | fdt_property_string(fdt, FIT_DESC_PROP, str); |
| 311 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, |
| 312 | cont->fname); |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 313 | if (ret) |
| 314 | return ret; |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 315 | fdt_property_string(fdt, FIT_TYPE_PROP, typename); |
| 316 | fdt_property_string(fdt, FIT_ARCH_PROP, |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 317 | genimg_get_arch_short_name(params->arch)); |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 318 | fdt_property_string(fdt, FIT_COMP_PROP, |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 319 | genimg_get_comp_short_name(IH_COMP_NONE)); |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 320 | fit_add_hash_or_sign(params, fdt, true); |
Sean Anderson | 87b0af9 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 321 | if (ret) |
| 322 | return ret; |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 323 | fdt_end_node(fdt); |
| 324 | } |
| 325 | |
Tomeu Vizoso | 0f7c6cd | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 326 | /* And a ramdisk file if available */ |
| 327 | if (params->fit_ramdisk) { |
Andre Przywara | 2eda8e9 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 328 | fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1"); |
Tomeu Vizoso | 0f7c6cd | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 329 | |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 330 | fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP); |
| 331 | fdt_property_string(fdt, FIT_OS_PROP, |
| 332 | genimg_get_os_short_name(params->os)); |
Michal Sojka | 12e288a | 2019-09-13 12:43:12 +0200 | [diff] [blame] | 333 | fdt_property_string(fdt, FIT_ARCH_PROP, |
| 334 | genimg_get_arch_short_name(params->arch)); |
Tomeu Vizoso | 0f7c6cd | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 335 | |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 336 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, |
| 337 | params->fit_ramdisk); |
Tomeu Vizoso | 0f7c6cd | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 338 | if (ret) |
| 339 | return ret; |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 340 | fit_add_hash_or_sign(params, fdt, true); |
Sean Anderson | 87b0af9 | 2022-05-16 16:11:08 -0400 | [diff] [blame] | 341 | if (ret) |
| 342 | return ret; |
Tomeu Vizoso | 0f7c6cd | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 343 | fdt_end_node(fdt); |
| 344 | } |
| 345 | |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 346 | fdt_end_node(fdt); |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * fit_write_configs() - Write out a list of configurations to the FIT |
| 353 | * |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 354 | * If there are device tree files, we include a configuration for each, which |
| 355 | * selects the main image (params->datafile) and its corresponding device |
| 356 | * tree file. |
| 357 | * |
| 358 | * Otherwise we just create a configuration with the main image in it. |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 359 | */ |
| 360 | static void fit_write_configs(struct image_tool_params *params, char *fdt) |
| 361 | { |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 362 | struct content_info *cont; |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 363 | const char *typename; |
| 364 | char str[100]; |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 365 | int upto; |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 366 | |
| 367 | fdt_begin_node(fdt, "configurations"); |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 368 | fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1"); |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 369 | |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 370 | upto = 0; |
| 371 | for (cont = params->content_head; cont; cont = cont->next) { |
| 372 | if (cont->type != IH_TYPE_FLATDT) |
| 373 | continue; |
| 374 | typename = genimg_get_type_short_name(cont->type); |
Andre Przywara | 2eda8e9 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 375 | snprintf(str, sizeof(str), "conf-%d", ++upto); |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 376 | fdt_begin_node(fdt, str); |
| 377 | |
| 378 | get_basename(str, sizeof(str), cont->fname); |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 379 | fdt_property_string(fdt, FIT_DESC_PROP, str); |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 380 | |
| 381 | typename = genimg_get_type_short_name(params->fit_image_type); |
Andre Przywara | 2eda8e9 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 382 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 383 | fdt_property_string(fdt, typename, str); |
Abel Vesa | cabde44 | 2019-03-12 08:34:32 +0000 | [diff] [blame] | 384 | fdt_property_string(fdt, FIT_LOADABLE_PROP, str); |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 385 | |
Tomeu Vizoso | 0f7c6cd | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 386 | if (params->fit_ramdisk) |
| 387 | fdt_property_string(fdt, FIT_RAMDISK_PROP, |
Andre Przywara | 2eda8e9 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 388 | FIT_RAMDISK_PROP "-1"); |
Tomeu Vizoso | 0f7c6cd | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 389 | |
Andre Przywara | 2eda8e9 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 390 | snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto); |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 391 | fdt_property_string(fdt, FIT_FDT_PROP, str); |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 392 | fit_add_hash_or_sign(params, fdt, false); |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 393 | fdt_end_node(fdt); |
| 394 | } |
Tomeu Vizoso | 0f7c6cd | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 395 | |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 396 | if (!upto) { |
Andre Przywara | 2eda8e9 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 397 | fdt_begin_node(fdt, "conf-1"); |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 398 | typename = genimg_get_type_short_name(params->fit_image_type); |
Andre Przywara | 2eda8e9 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 399 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 400 | fdt_property_string(fdt, typename, str); |
Tomeu Vizoso | 0f7c6cd | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 401 | |
| 402 | if (params->fit_ramdisk) |
| 403 | fdt_property_string(fdt, FIT_RAMDISK_PROP, |
Andre Przywara | 2eda8e9 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 404 | FIT_RAMDISK_PROP "-1"); |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 405 | fit_add_hash_or_sign(params, fdt, false); |
Tomeu Vizoso | 0f7c6cd | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 406 | |
Simon Glass | fb4cce0 | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 407 | fdt_end_node(fdt); |
| 408 | } |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 409 | |
| 410 | fdt_end_node(fdt); |
| 411 | } |
| 412 | |
| 413 | static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size) |
| 414 | { |
| 415 | int ret; |
| 416 | |
| 417 | ret = fdt_create(fdt, size); |
| 418 | if (ret) |
| 419 | return ret; |
| 420 | fdt_finish_reservemap(fdt); |
| 421 | fdt_begin_node(fdt, ""); |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 422 | fdt_property_strf(fdt, FIT_DESC_PROP, |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 423 | "%s image with one or more FDT blobs", |
| 424 | genimg_get_type_name(params->fit_image_type)); |
| 425 | fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION); |
| 426 | fdt_property_u32(fdt, "#address-cells", 1); |
| 427 | ret = fit_write_images(params, fdt); |
| 428 | if (ret) |
| 429 | return ret; |
| 430 | fit_write_configs(params, fdt); |
| 431 | fdt_end_node(fdt); |
| 432 | ret = fdt_finish(fdt); |
| 433 | if (ret) |
| 434 | return ret; |
| 435 | |
| 436 | return fdt_totalsize(fdt); |
| 437 | } |
| 438 | |
| 439 | static int fit_build(struct image_tool_params *params, const char *fname) |
| 440 | { |
| 441 | char *buf; |
| 442 | int size; |
| 443 | int ret; |
| 444 | int fd; |
| 445 | |
| 446 | size = fit_calc_size(params); |
| 447 | if (size < 0) |
| 448 | return -1; |
Fabio Estevam | aaa91a4 | 2020-07-27 21:03:13 -0300 | [diff] [blame] | 449 | buf = calloc(1, size); |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 450 | if (!buf) { |
| 451 | fprintf(stderr, "%s: Out of memory (%d bytes)\n", |
| 452 | params->cmdname, size); |
| 453 | return -1; |
| 454 | } |
| 455 | ret = fit_build_fdt(params, buf, size); |
| 456 | if (ret < 0) { |
| 457 | fprintf(stderr, "%s: Failed to build FIT image\n", |
| 458 | params->cmdname); |
Simon Glass | 7b0bbd8 | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 459 | goto err_buf; |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 460 | } |
| 461 | size = ret; |
| 462 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); |
| 463 | if (fd < 0) { |
| 464 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 465 | params->cmdname, fname, strerror(errno)); |
Tom Rini | 3c2dff5 | 2017-09-26 22:14:44 -0400 | [diff] [blame] | 466 | goto err_buf; |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 467 | } |
| 468 | ret = write(fd, buf, size); |
| 469 | if (ret != size) { |
| 470 | fprintf(stderr, "%s: Can't write %s: %s\n", |
| 471 | params->cmdname, fname, strerror(errno)); |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 472 | goto err; |
| 473 | } |
| 474 | close(fd); |
Simon Glass | 7b0bbd8 | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 475 | free(buf); |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 476 | |
| 477 | return 0; |
| 478 | err: |
Simon Glass | 7b0bbd8 | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 479 | close(fd); |
| 480 | err_buf: |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 481 | free(buf); |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | /** |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 486 | * fit_extract_data() - Move all data outside the FIT |
| 487 | * |
| 488 | * This takes a normal FIT file and removes all the 'data' properties from it. |
| 489 | * The data is placed in an area after the FIT so that it can be accessed |
| 490 | * using an offset into that area. The 'data' properties turn into |
| 491 | * 'data-offset' properties. |
| 492 | * |
| 493 | * This function cannot cope with FITs with 'data-offset' properties. All |
| 494 | * data must be in 'data' properties on entry. |
| 495 | */ |
| 496 | static int fit_extract_data(struct image_tool_params *params, const char *fname) |
| 497 | { |
Kever Yang | ebfe611 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 498 | void *buf = NULL; |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 499 | int buf_ptr; |
| 500 | int fit_size, new_size; |
| 501 | int fd; |
| 502 | struct stat sbuf; |
| 503 | void *fdt; |
| 504 | int ret; |
| 505 | int images; |
| 506 | int node; |
Kever Yang | ebfe611 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 507 | int image_number; |
| 508 | int align_size; |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 509 | |
Tom Rini | 7946a81 | 2020-05-06 11:05:17 -0400 | [diff] [blame] | 510 | align_size = params->bl_len ? params->bl_len : 4; |
Luca Boccassi | 7d57485 | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 511 | fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false); |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 512 | if (fd < 0) |
| 513 | return -EIO; |
| 514 | fit_size = fdt_totalsize(fdt); |
| 515 | |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 516 | images = fdt_path_offset(fdt, FIT_IMAGES_PATH); |
| 517 | if (images < 0) { |
| 518 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 519 | ret = -EINVAL; |
Simon Glass | b97d71e | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 520 | goto err_munmap; |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 521 | } |
Kever Yang | ebfe611 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 522 | image_number = fdtdec_get_child_count(fdt, images); |
| 523 | |
| 524 | /* |
| 525 | * Allocate space to hold the image data we will extract, |
| 526 | * extral space allocate for image alignment to prevent overflow. |
| 527 | */ |
Fabio Estevam | aaa91a4 | 2020-07-27 21:03:13 -0300 | [diff] [blame] | 528 | buf = calloc(1, fit_size + (align_size * image_number)); |
Kever Yang | ebfe611 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 529 | if (!buf) { |
| 530 | ret = -ENOMEM; |
| 531 | goto err_munmap; |
| 532 | } |
| 533 | buf_ptr = 0; |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 534 | |
| 535 | for (node = fdt_first_subnode(fdt, images); |
| 536 | node >= 0; |
| 537 | node = fdt_next_subnode(fdt, node)) { |
| 538 | const char *data; |
| 539 | int len; |
| 540 | |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 541 | data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len); |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 542 | if (!data) |
| 543 | continue; |
| 544 | memcpy(buf + buf_ptr, data, len); |
| 545 | debug("Extracting data size %x\n", len); |
| 546 | |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 547 | ret = fdt_delprop(fdt, node, FIT_DATA_PROP); |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 548 | if (ret) { |
| 549 | ret = -EPERM; |
Simon Glass | b97d71e | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 550 | goto err_munmap; |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 551 | } |
Teddy Reed | f8f9107 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 552 | if (params->external_offset > 0) { |
| 553 | /* An external offset positions the data absolutely. */ |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 554 | fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP, |
Teddy Reed | f8f9107 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 555 | params->external_offset + buf_ptr); |
| 556 | } else { |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 557 | fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP, |
| 558 | buf_ptr); |
Teddy Reed | f8f9107 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 559 | } |
Michal Simek | 4a8b6e0 | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 560 | fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len); |
Kever Yang | ebfe611 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 561 | buf_ptr += ALIGN(len, align_size); |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* Pack the FDT and place the data after it */ |
| 565 | fdt_pack(fdt); |
| 566 | |
Kever Yang | ebfe611 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 567 | new_size = fdt_totalsize(fdt); |
Tom Rini | 7946a81 | 2020-05-06 11:05:17 -0400 | [diff] [blame] | 568 | new_size = ALIGN(new_size, align_size); |
Kever Yang | ebfe611 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 569 | fdt_set_totalsize(fdt, new_size); |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 570 | debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt)); |
| 571 | debug("External data size %x\n", buf_ptr); |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 572 | munmap(fdt, sbuf.st_size); |
| 573 | |
| 574 | if (ftruncate(fd, new_size)) { |
| 575 | debug("%s: Failed to truncate file: %s\n", __func__, |
| 576 | strerror(errno)); |
| 577 | ret = -EIO; |
| 578 | goto err; |
| 579 | } |
Teddy Reed | f8f9107 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 580 | |
| 581 | /* Check if an offset for the external data was set. */ |
| 582 | if (params->external_offset > 0) { |
| 583 | if (params->external_offset < new_size) { |
Simon Glass | 206117a | 2022-01-09 20:13:38 -0700 | [diff] [blame] | 584 | fprintf(stderr, |
| 585 | "External offset %x overlaps FIT length %x\n", |
| 586 | params->external_offset, new_size); |
Teddy Reed | f8f9107 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 587 | ret = -EINVAL; |
| 588 | goto err; |
| 589 | } |
| 590 | new_size = params->external_offset; |
| 591 | } |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 592 | if (lseek(fd, new_size, SEEK_SET) < 0) { |
| 593 | debug("%s: Failed to seek to end of file: %s\n", __func__, |
| 594 | strerror(errno)); |
| 595 | ret = -EIO; |
| 596 | goto err; |
| 597 | } |
| 598 | if (write(fd, buf, buf_ptr) != buf_ptr) { |
| 599 | debug("%s: Failed to write external data to file %s\n", |
| 600 | __func__, strerror(errno)); |
| 601 | ret = -EIO; |
| 602 | goto err; |
| 603 | } |
Tom Rini | 3c2dff5 | 2017-09-26 22:14:44 -0400 | [diff] [blame] | 604 | free(buf); |
Simon Glass | b97d71e | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 605 | close(fd); |
| 606 | return 0; |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 607 | |
Simon Glass | b97d71e | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 608 | err_munmap: |
| 609 | munmap(fdt, sbuf.st_size); |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 610 | err: |
Bin Meng | 0dbd6e3 | 2020-04-18 01:59:11 -0700 | [diff] [blame] | 611 | free(buf); |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 612 | close(fd); |
| 613 | return ret; |
| 614 | } |
| 615 | |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 616 | static int fit_import_data(struct image_tool_params *params, const char *fname) |
| 617 | { |
| 618 | void *fdt, *old_fdt; |
| 619 | int fit_size, new_size, size, data_base; |
| 620 | int fd; |
| 621 | struct stat sbuf; |
| 622 | int ret; |
| 623 | int images; |
| 624 | int node; |
| 625 | |
Luca Boccassi | 7d57485 | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 626 | fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false); |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 627 | if (fd < 0) |
| 628 | return -EIO; |
| 629 | fit_size = fdt_totalsize(old_fdt); |
Kever Yang | 02560b1 | 2020-03-30 11:56:22 +0800 | [diff] [blame] | 630 | data_base = ALIGN(fit_size, 4); |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 631 | |
| 632 | /* Allocate space to hold the new FIT */ |
| 633 | size = sbuf.st_size + 16384; |
Fabio Estevam | aaa91a4 | 2020-07-27 21:03:13 -0300 | [diff] [blame] | 634 | fdt = calloc(1, size); |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 635 | if (!fdt) { |
| 636 | fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n", |
| 637 | __func__, size); |
| 638 | ret = -ENOMEM; |
Lihua Zhao | 3fc85a7 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 639 | goto err_munmap; |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 640 | } |
| 641 | ret = fdt_open_into(old_fdt, fdt, size); |
| 642 | if (ret) { |
| 643 | debug("%s: Failed to expand FIT: %s\n", __func__, |
| 644 | fdt_strerror(errno)); |
| 645 | ret = -EINVAL; |
Lihua Zhao | 3fc85a7 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 646 | goto err_munmap; |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | images = fdt_path_offset(fdt, FIT_IMAGES_PATH); |
| 650 | if (images < 0) { |
| 651 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 652 | ret = -EINVAL; |
Lihua Zhao | 3fc85a7 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 653 | goto err_munmap; |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | for (node = fdt_first_subnode(fdt, images); |
| 657 | node >= 0; |
| 658 | node = fdt_next_subnode(fdt, node)) { |
| 659 | int buf_ptr; |
| 660 | int len; |
| 661 | |
| 662 | buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1); |
| 663 | len = fdtdec_get_int(fdt, node, "data-size", -1); |
| 664 | if (buf_ptr == -1 || len == -1) |
| 665 | continue; |
| 666 | debug("Importing data size %x\n", len); |
| 667 | |
Patrick Oppenlander | c995d85 | 2020-07-30 14:31:48 +1000 | [diff] [blame] | 668 | ret = fdt_setprop(fdt, node, "data", |
| 669 | old_fdt + data_base + buf_ptr, len); |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 670 | if (ret) { |
| 671 | debug("%s: Failed to write property: %s\n", __func__, |
| 672 | fdt_strerror(ret)); |
| 673 | ret = -EINVAL; |
Lihua Zhao | 3fc85a7 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 674 | goto err_munmap; |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Lihua Zhao | 3fc85a7 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 678 | munmap(old_fdt, sbuf.st_size); |
| 679 | |
Tom Rini | bf52fcd | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 680 | /* Close the old fd so we can re-use it. */ |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 681 | close(fd); |
| 682 | |
| 683 | /* Pack the FDT and place the data after it */ |
| 684 | fdt_pack(fdt); |
| 685 | |
| 686 | new_size = fdt_totalsize(fdt); |
| 687 | debug("Size expanded from %x to %x\n", fit_size, new_size); |
| 688 | |
| 689 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); |
| 690 | if (fd < 0) { |
| 691 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 692 | params->cmdname, fname, strerror(errno)); |
Tom Rini | bf52fcd | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 693 | ret = -EIO; |
Lihua Zhao | 3fc85a7 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 694 | goto err; |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 695 | } |
| 696 | if (write(fd, fdt, new_size) != new_size) { |
| 697 | debug("%s: Failed to write external data to file %s\n", |
| 698 | __func__, strerror(errno)); |
| 699 | ret = -EIO; |
Lihua Zhao | 3fc85a7 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 700 | goto err; |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 701 | } |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 702 | |
Simon Glass | 6e0ffce | 2016-03-16 07:45:38 -0600 | [diff] [blame] | 703 | free(fdt); |
Lihua Zhao | 3fc85a7 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 704 | close(fd); |
| 705 | return 0; |
| 706 | |
| 707 | err_munmap: |
| 708 | munmap(old_fdt, sbuf.st_size); |
| 709 | err: |
| 710 | free(fdt); |
| 711 | close(fd); |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 712 | return ret; |
| 713 | } |
| 714 | |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 715 | /** |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 716 | * fit_handle_file - main FIT file processing function |
| 717 | * |
| 718 | * fit_handle_file() runs dtc to convert .its to .itb, includes |
| 719 | * binary data, updates timestamp property and calculates hashes. |
| 720 | * |
| 721 | * datafile - .its file |
| 722 | * imagefile - .itb file |
| 723 | * |
| 724 | * returns: |
| 725 | * only on success, otherwise calls exit (EXIT_FAILURE); |
| 726 | */ |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 727 | static int fit_handle_file(struct image_tool_params *params) |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 728 | { |
| 729 | char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 730 | char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0}; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 731 | char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 732 | size_t size_inc; |
| 733 | int ret; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 734 | |
| 735 | /* Flattened Image Tree (FIT) format handling */ |
| 736 | debug ("FIT format handling\n"); |
| 737 | |
| 738 | /* call dtc to include binary properties into the tmp file */ |
| 739 | if (strlen (params->imagefile) + |
| 740 | strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) { |
| 741 | fprintf (stderr, "%s: Image file name (%s) too long, " |
Sven Roederer | 9c70237 | 2021-05-25 23:15:27 +0200 | [diff] [blame] | 742 | "can't create tmpfile.\n", |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 743 | params->imagefile, params->cmdname); |
| 744 | return (EXIT_FAILURE); |
| 745 | } |
| 746 | sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX); |
| 747 | |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 748 | /* We either compile the source file, or use the existing FIT image */ |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 749 | if (params->auto_fit) { |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 750 | if (fit_build(params, tmpfile)) { |
| 751 | fprintf(stderr, "%s: failed to build FIT\n", |
| 752 | params->cmdname); |
| 753 | return EXIT_FAILURE; |
| 754 | } |
| 755 | *cmd = '\0'; |
| 756 | } else if (params->datafile) { |
Stefan Theil | 63f881d | 2018-03-08 09:00:13 +0100 | [diff] [blame] | 757 | /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */ |
| 758 | snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"", |
| 759 | MKIMAGE_DTC, params->dtc, tmpfile, params->datafile); |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 760 | debug("Trying to execute \"%s\"\n", cmd); |
| 761 | } else { |
Mirza, Taimoor | a6e9810 | 2017-10-04 20:28:03 +0500 | [diff] [blame] | 762 | snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"", |
Simon Glass | 95d77b4 | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 763 | params->imagefile, tmpfile); |
| 764 | } |
Sven Roederer | ea5d373 | 2020-04-27 02:08:39 +0200 | [diff] [blame] | 765 | if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) { |
| 766 | fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n"); |
| 767 | } |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 768 | |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 769 | if (*cmd && system(cmd) == -1) { |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 770 | fprintf (stderr, "%s: system(%s) failed: %s\n", |
| 771 | params->cmdname, cmd, strerror(errno)); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 772 | goto err_system; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 773 | } |
| 774 | |
Simon Glass | 529fd18 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 775 | /* Move the data so it is internal to the FIT, if needed */ |
| 776 | ret = fit_import_data(params, tmpfile); |
| 777 | if (ret) |
| 778 | goto err_system; |
| 779 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 780 | /* |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 781 | * Copy the tmpfile to bakfile, then in the following loop |
| 782 | * we copy bakfile to tmpfile. So we always start from the |
| 783 | * beginning. |
| 784 | */ |
| 785 | sprintf(bakfile, "%s%s", tmpfile, ".bak"); |
| 786 | rename(tmpfile, bakfile); |
| 787 | |
| 788 | /* |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 789 | * Set hashes for images in the blob. Unfortunately we may need more |
| 790 | * space in either FDT, so keep trying until we succeed. |
| 791 | * |
| 792 | * Note: this is pretty inefficient for signing, since we must |
| 793 | * calculate the signature every time. It would be better to calculate |
| 794 | * all the data and then store it in a separate step. However, this |
| 795 | * would be considerably more complex to implement. Generally a few |
| 796 | * steps of this loop is enough to sign with several keys. |
| 797 | */ |
| 798 | for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) { |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 799 | if (copyfile(bakfile, tmpfile) < 0) { |
| 800 | printf("Can't copy %s to %s\n", bakfile, tmpfile); |
| 801 | ret = -EIO; |
| 802 | break; |
| 803 | } |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 804 | ret = fit_add_file_data(params, size_inc, tmpfile); |
| 805 | if (!ret || ret != -ENOSPC) |
| 806 | break; |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 809 | if (ret) { |
Simon Glass | 655cc69 | 2016-07-03 09:40:43 -0600 | [diff] [blame] | 810 | fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n", |
| 811 | params->cmdname, ret); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 812 | goto err_system; |
Simon Glass | e29495d | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 813 | } |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 814 | |
Simon Glass | 722ebc8 | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 815 | /* Move the data so it is external to the FIT, if requested */ |
| 816 | if (params->external_data) { |
| 817 | ret = fit_extract_data(params, tmpfile); |
| 818 | if (ret) |
| 819 | goto err_system; |
| 820 | } |
| 821 | |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 822 | if (rename (tmpfile, params->imagefile) == -1) { |
| 823 | fprintf (stderr, "%s: Can't rename %s to %s: %s\n", |
| 824 | params->cmdname, tmpfile, params->imagefile, |
| 825 | strerror (errno)); |
| 826 | unlink (tmpfile); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 827 | unlink(bakfile); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 828 | unlink (params->imagefile); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 829 | return EXIT_FAILURE; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 830 | } |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 831 | unlink(bakfile); |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 832 | return EXIT_SUCCESS; |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 833 | |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 834 | err_system: |
| 835 | unlink(tmpfile); |
Philippe Reynes | 7298e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 836 | unlink(bakfile); |
Simon Glass | aa6d6db | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 837 | return -1; |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 838 | } |
| 839 | |
Guilherme Maciel Ferreira | 39931f9 | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 840 | /** |
| 841 | * fit_image_extract - extract a FIT component image |
| 842 | * @fit: pointer to the FIT format image header |
| 843 | * @image_noffset: offset of the component image node |
| 844 | * @file_name: name of the file to store the FIT sub-image |
| 845 | * |
| 846 | * returns: |
| 847 | * zero in case of success or a negative value if fail. |
| 848 | */ |
| 849 | static int fit_image_extract( |
| 850 | const void *fit, |
| 851 | int image_noffset, |
| 852 | const char *file_name) |
| 853 | { |
| 854 | const void *file_data; |
| 855 | size_t file_size = 0; |
Andrew F. Davis | e5b5628 | 2019-09-17 17:09:34 -0400 | [diff] [blame] | 856 | int ret; |
Guilherme Maciel Ferreira | 39931f9 | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 857 | |
Andrew F. Davis | e5b5628 | 2019-09-17 17:09:34 -0400 | [diff] [blame] | 858 | /* get the data address and size of component at offset "image_noffset" */ |
| 859 | ret = fit_image_get_data_and_size(fit, image_noffset, &file_data, &file_size); |
| 860 | if (ret) { |
| 861 | fprintf(stderr, "Could not get component information\n"); |
| 862 | return ret; |
| 863 | } |
Guilherme Maciel Ferreira | 39931f9 | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 864 | |
| 865 | /* save the "file_data" into the file specified by "file_name" */ |
| 866 | return imagetool_save_subimage(file_name, (ulong) file_data, file_size); |
| 867 | } |
| 868 | |
| 869 | /** |
| 870 | * fit_extract_contents - retrieve a sub-image component from the FIT image |
| 871 | * @ptr: pointer to the FIT format image header |
| 872 | * @params: command line parameters |
| 873 | * |
| 874 | * returns: |
| 875 | * zero in case of success or a negative value if fail. |
| 876 | */ |
| 877 | static int fit_extract_contents(void *ptr, struct image_tool_params *params) |
| 878 | { |
| 879 | int images_noffset; |
| 880 | int noffset; |
| 881 | int ndepth; |
| 882 | const void *fit = ptr; |
| 883 | int count = 0; |
| 884 | const char *p; |
| 885 | |
| 886 | /* Indent string is defined in header image.h */ |
| 887 | p = IMAGE_INDENT_STRING; |
| 888 | |
Guilherme Maciel Ferreira | 39931f9 | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 889 | /* Find images parent node offset */ |
| 890 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 891 | if (images_noffset < 0) { |
| 892 | printf("Can't find images parent node '%s' (%s)\n", |
| 893 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 894 | return -1; |
| 895 | } |
| 896 | |
| 897 | /* Avoid any overrun */ |
| 898 | count = fit_get_subimage_count(fit, images_noffset); |
| 899 | if ((params->pflag < 0) || (count <= params->pflag)) { |
| 900 | printf("No such component at '%d'\n", params->pflag); |
| 901 | return -1; |
| 902 | } |
| 903 | |
| 904 | /* Process its subnodes, extract the desired component from image */ |
| 905 | for (ndepth = 0, count = 0, |
| 906 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 907 | (noffset >= 0) && (ndepth > 0); |
| 908 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 909 | if (ndepth == 1) { |
| 910 | /* |
| 911 | * Direct child node of the images parent node, |
| 912 | * i.e. component image node. |
| 913 | */ |
| 914 | if (params->pflag == count) { |
| 915 | printf("Extracted:\n%s Image %u (%s)\n", p, |
| 916 | count, fit_get_name(fit, noffset, NULL)); |
| 917 | |
| 918 | fit_image_print(fit, noffset, p); |
| 919 | |
| 920 | return fit_image_extract(fit, noffset, |
| 921 | params->outfile); |
| 922 | } |
| 923 | |
| 924 | count++; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | return 0; |
| 929 | } |
| 930 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 931 | static int fit_check_params(struct image_tool_params *params) |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 932 | { |
Massimo Pegorer | b93a652 | 2023-01-05 10:31:09 +0100 | [diff] [blame] | 933 | if (params->auto_fit) |
Simon Glass | 8e35bb0 | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 934 | return 0; |
Heinrich Schuchardt | 5819466 | 2019-12-11 13:51:38 +0100 | [diff] [blame] | 935 | return ((params->dflag && params->fflag) || |
| 936 | (params->fflag && params->lflag) || |
| 937 | (params->lflag && params->dflag)); |
Prafulla Wadaskar | 89a4d6b | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 938 | } |
| 939 | |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 940 | U_BOOT_IMAGE_TYPE( |
| 941 | fitimage, |
| 942 | "FIT Image support", |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 943 | sizeof(struct legacy_img_hdr), |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 944 | (void *)&header, |
| 945 | fit_check_params, |
| 946 | fit_verify_header, |
Pali Rohár | 2972d7d | 2023-03-29 21:25:54 +0200 | [diff] [blame] | 947 | fit_print_header, |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 948 | NULL, |
Guilherme Maciel Ferreira | 39931f9 | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 949 | fit_extract_contents, |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 950 | fit_check_image_types, |
| 951 | fit_handle_file, |
| 952 | NULL /* FIT images use DTB header */ |
| 953 | ); |