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 | |
| 25 | static image_header_t header; |
| 26 | |
Simon Glass | 802aa82 | 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 | bff733f | 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 | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 39 | if (tfd < 0) |
| 40 | return -EIO; |
| 41 | |
| 42 | if (params->keydest) { |
| 43 | struct stat dest_sbuf; |
| 44 | |
| 45 | destfd = mmap_fdt(params->cmdname, params->keydest, size_inc, |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 46 | &dest_blob, &dest_sbuf, false, |
| 47 | false); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 48 | if (destfd < 0) { |
| 49 | ret = -EIO; |
| 50 | goto err_keydest; |
| 51 | } |
| 52 | destfd_size = dest_sbuf.st_size; |
| 53 | } |
| 54 | |
| 55 | /* 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] | 56 | if (params->datafile || params->reset_timestamp) { |
Alex Kiernan | c4c7e0d | 2018-06-20 20:10:51 +0000 | [diff] [blame] | 57 | time_t time = imagetool_get_source_date(params->cmdname, |
| 58 | sbuf.st_mtime); |
Vagrant Cascadian | f8e066c | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 59 | ret = fit_set_timestamp(ptr, 0, time); |
| 60 | } |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 61 | |
| 62 | if (!ret) { |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 63 | ret = fit_cipher_data(params->keydir, dest_blob, ptr, |
| 64 | params->comment, |
| 65 | params->require_keys, |
| 66 | params->engine_id, |
| 67 | params->cmdname); |
| 68 | } |
| 69 | |
| 70 | if (!ret) { |
Alexandru Gagniuc | 8fcea12 | 2021-02-19 12:45:17 -0600 | [diff] [blame] | 71 | ret = fit_add_verification_data(params->keydir, |
| 72 | params->keyfile, dest_blob, ptr, |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 73 | params->comment, |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 74 | params->require_keys, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 75 | params->engine_id, |
Jan Kiszka | 4043f32 | 2022-01-14 10:21:19 +0100 | [diff] [blame] | 76 | params->cmdname, |
| 77 | params->algo_name); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (dest_blob) { |
| 81 | munmap(dest_blob, destfd_size); |
| 82 | close(destfd); |
| 83 | } |
| 84 | |
| 85 | err_keydest: |
| 86 | munmap(ptr, sbuf.st_size); |
| 87 | close(tfd); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 88 | return ret; |
| 89 | } |
| 90 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 91 | /** |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 92 | * fit_calc_size() - Calculate the approximate size of the FIT we will generate |
| 93 | */ |
| 94 | static int fit_calc_size(struct image_tool_params *params) |
| 95 | { |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 96 | struct content_info *cont; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 97 | int size, total_size; |
| 98 | |
| 99 | size = imagetool_get_filesize(params, params->datafile); |
| 100 | if (size < 0) |
| 101 | return -1; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 102 | total_size = size; |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 103 | |
| 104 | if (params->fit_ramdisk) { |
| 105 | size = imagetool_get_filesize(params, params->fit_ramdisk); |
| 106 | if (size < 0) |
| 107 | return -1; |
| 108 | total_size += size; |
| 109 | } |
| 110 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 111 | for (cont = params->content_head; cont; cont = cont->next) { |
| 112 | size = imagetool_get_filesize(params, cont->fname); |
| 113 | if (size < 0) |
| 114 | return -1; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 115 | |
Simon Glass | 611fd91 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 116 | /* Add space for properties and hash node */ |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 117 | total_size += size + 300; |
| 118 | } |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 119 | |
| 120 | /* Add plenty of space for headers, properties, nodes, etc. */ |
| 121 | total_size += 4096; |
| 122 | |
| 123 | return total_size; |
| 124 | } |
| 125 | |
| 126 | static int fdt_property_file(struct image_tool_params *params, |
| 127 | void *fdt, const char *name, const char *fname) |
| 128 | { |
| 129 | struct stat sbuf; |
| 130 | void *ptr; |
| 131 | int ret; |
| 132 | int fd; |
| 133 | |
| 134 | fd = open(fname, O_RDWR | O_BINARY); |
| 135 | if (fd < 0) { |
| 136 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 137 | params->cmdname, fname, strerror(errno)); |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | if (fstat(fd, &sbuf) < 0) { |
| 142 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 143 | params->cmdname, fname, strerror(errno)); |
| 144 | goto err; |
| 145 | } |
| 146 | |
| 147 | ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr); |
| 148 | if (ret) |
Simon Glass | 9ab7f05 | 2016-03-16 07:45:42 -0600 | [diff] [blame] | 149 | goto err; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 150 | ret = read(fd, ptr, sbuf.st_size); |
| 151 | if (ret != sbuf.st_size) { |
| 152 | fprintf(stderr, "%s: Can't read %s: %s\n", |
| 153 | params->cmdname, fname, strerror(errno)); |
| 154 | goto err; |
| 155 | } |
Simon Glass | 9ab7f05 | 2016-03-16 07:45:42 -0600 | [diff] [blame] | 156 | close(fd); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 157 | |
| 158 | return 0; |
| 159 | err: |
| 160 | close(fd); |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...) |
| 165 | { |
| 166 | char str[100]; |
| 167 | va_list ptr; |
| 168 | |
| 169 | va_start(ptr, fmt); |
| 170 | vsnprintf(str, sizeof(str), fmt, ptr); |
| 171 | va_end(ptr); |
| 172 | return fdt_property_string(fdt, name, str); |
| 173 | } |
| 174 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 175 | static void get_basename(char *str, int size, const char *fname) |
| 176 | { |
| 177 | const char *p, *start, *end; |
| 178 | int len; |
| 179 | |
| 180 | /* |
| 181 | * Use the base name as the 'name' field. So for example: |
| 182 | * |
| 183 | * "arch/arm/dts/sun7i-a20-bananapro.dtb" |
| 184 | * becomes "sun7i-a20-bananapro" |
| 185 | */ |
| 186 | p = strrchr(fname, '/'); |
| 187 | start = p ? p + 1 : fname; |
| 188 | p = strrchr(fname, '.'); |
| 189 | end = p ? p : fname + strlen(fname); |
| 190 | len = end - start; |
| 191 | if (len >= size) |
| 192 | len = size - 1; |
| 193 | memcpy(str, start, len); |
| 194 | str[len] = '\0'; |
| 195 | } |
| 196 | |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 197 | /** |
Simon Glass | 611fd91 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 198 | * add_crc_node() - Add a hash node to request a CRC checksum for an image |
| 199 | * |
| 200 | * @fdt: Device tree to add to (in sequential-write mode) |
| 201 | */ |
| 202 | static void add_crc_node(void *fdt) |
| 203 | { |
| 204 | fdt_begin_node(fdt, "hash-1"); |
| 205 | fdt_property_string(fdt, FIT_ALGO_PROP, "crc32"); |
| 206 | fdt_end_node(fdt); |
| 207 | } |
| 208 | |
| 209 | /** |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 210 | * fit_write_images() - Write out a list of images to the FIT |
| 211 | * |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 212 | * We always include the main image (params->datafile). If there are device |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 213 | * tree files, we include an fdt- node for each of those too. |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 214 | */ |
| 215 | static int fit_write_images(struct image_tool_params *params, char *fdt) |
| 216 | { |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 217 | struct content_info *cont; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 218 | const char *typename; |
| 219 | char str[100]; |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 220 | int upto; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 221 | int ret; |
| 222 | |
| 223 | fdt_begin_node(fdt, "images"); |
| 224 | |
| 225 | /* First the main image */ |
| 226 | typename = genimg_get_type_short_name(params->fit_image_type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 227 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 228 | fdt_begin_node(fdt, str); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 229 | fdt_property_string(fdt, FIT_DESC_PROP, params->imagename); |
| 230 | fdt_property_string(fdt, FIT_TYPE_PROP, typename); |
| 231 | fdt_property_string(fdt, FIT_ARCH_PROP, |
Simon Glass | aeeea7f | 2016-06-30 10:52:12 -0600 | [diff] [blame] | 232 | genimg_get_arch_short_name(params->arch)); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 233 | fdt_property_string(fdt, FIT_OS_PROP, |
| 234 | genimg_get_os_short_name(params->os)); |
| 235 | fdt_property_string(fdt, FIT_COMP_PROP, |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 236 | genimg_get_comp_short_name(params->comp)); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 237 | fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr); |
| 238 | fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * Put data last since it is large. SPL may only load the first part |
| 242 | * of the DT, so this way it can access all the above fields. |
| 243 | */ |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 244 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 245 | if (ret) |
| 246 | return ret; |
Simon Glass | 611fd91 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 247 | add_crc_node(fdt); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 248 | fdt_end_node(fdt); |
| 249 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 250 | /* Now the device tree files if available */ |
| 251 | upto = 0; |
| 252 | for (cont = params->content_head; cont; cont = cont->next) { |
| 253 | if (cont->type != IH_TYPE_FLATDT) |
| 254 | continue; |
Michal Sojka | 8a90ce8 | 2019-09-13 12:43:12 +0200 | [diff] [blame] | 255 | typename = genimg_get_type_short_name(cont->type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 256 | snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 257 | fdt_begin_node(fdt, str); |
| 258 | |
| 259 | get_basename(str, sizeof(str), cont->fname); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 260 | fdt_property_string(fdt, FIT_DESC_PROP, str); |
| 261 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, |
| 262 | cont->fname); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 263 | if (ret) |
| 264 | return ret; |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 265 | fdt_property_string(fdt, FIT_TYPE_PROP, typename); |
| 266 | fdt_property_string(fdt, FIT_ARCH_PROP, |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 267 | genimg_get_arch_short_name(params->arch)); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 268 | fdt_property_string(fdt, FIT_COMP_PROP, |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 269 | genimg_get_comp_short_name(IH_COMP_NONE)); |
Simon Glass | 611fd91 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 270 | add_crc_node(fdt); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 271 | fdt_end_node(fdt); |
| 272 | } |
| 273 | |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 274 | /* And a ramdisk file if available */ |
| 275 | if (params->fit_ramdisk) { |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 276 | fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1"); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 277 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 278 | fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP); |
| 279 | fdt_property_string(fdt, FIT_OS_PROP, |
| 280 | genimg_get_os_short_name(params->os)); |
Michal Sojka | 8a90ce8 | 2019-09-13 12:43:12 +0200 | [diff] [blame] | 281 | fdt_property_string(fdt, FIT_ARCH_PROP, |
| 282 | genimg_get_arch_short_name(params->arch)); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 283 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 284 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, |
| 285 | params->fit_ramdisk); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 286 | if (ret) |
| 287 | return ret; |
Simon Glass | 611fd91 | 2020-05-27 07:24:55 -0600 | [diff] [blame] | 288 | add_crc_node(fdt); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 289 | fdt_end_node(fdt); |
| 290 | } |
| 291 | |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 292 | fdt_end_node(fdt); |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * fit_write_configs() - Write out a list of configurations to the FIT |
| 299 | * |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 300 | * If there are device tree files, we include a configuration for each, which |
| 301 | * selects the main image (params->datafile) and its corresponding device |
| 302 | * tree file. |
| 303 | * |
| 304 | * Otherwise we just create a configuration with the main image in it. |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 305 | */ |
| 306 | static void fit_write_configs(struct image_tool_params *params, char *fdt) |
| 307 | { |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 308 | struct content_info *cont; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 309 | const char *typename; |
| 310 | char str[100]; |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 311 | int upto; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 312 | |
| 313 | fdt_begin_node(fdt, "configurations"); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 314 | fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1"); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 315 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 316 | upto = 0; |
| 317 | for (cont = params->content_head; cont; cont = cont->next) { |
| 318 | if (cont->type != IH_TYPE_FLATDT) |
| 319 | continue; |
| 320 | typename = genimg_get_type_short_name(cont->type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 321 | snprintf(str, sizeof(str), "conf-%d", ++upto); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 322 | fdt_begin_node(fdt, str); |
| 323 | |
| 324 | get_basename(str, sizeof(str), cont->fname); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 325 | fdt_property_string(fdt, FIT_DESC_PROP, str); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 326 | |
| 327 | typename = genimg_get_type_short_name(params->fit_image_type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 328 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 329 | fdt_property_string(fdt, typename, str); |
Abel Vesa | 7e0cb5e | 2019-03-12 08:34:32 +0000 | [diff] [blame] | 330 | fdt_property_string(fdt, FIT_LOADABLE_PROP, str); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 331 | |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 332 | if (params->fit_ramdisk) |
| 333 | fdt_property_string(fdt, FIT_RAMDISK_PROP, |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 334 | FIT_RAMDISK_PROP "-1"); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 335 | |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 336 | snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 337 | fdt_property_string(fdt, FIT_FDT_PROP, str); |
| 338 | fdt_end_node(fdt); |
| 339 | } |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 340 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 341 | if (!upto) { |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 342 | fdt_begin_node(fdt, "conf-1"); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 343 | typename = genimg_get_type_short_name(params->fit_image_type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 344 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 345 | fdt_property_string(fdt, typename, str); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 346 | |
| 347 | if (params->fit_ramdisk) |
| 348 | fdt_property_string(fdt, FIT_RAMDISK_PROP, |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 349 | FIT_RAMDISK_PROP "-1"); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 350 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 351 | fdt_end_node(fdt); |
| 352 | } |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 353 | |
| 354 | fdt_end_node(fdt); |
| 355 | } |
| 356 | |
| 357 | static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size) |
| 358 | { |
| 359 | int ret; |
| 360 | |
| 361 | ret = fdt_create(fdt, size); |
| 362 | if (ret) |
| 363 | return ret; |
| 364 | fdt_finish_reservemap(fdt); |
| 365 | fdt_begin_node(fdt, ""); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 366 | fdt_property_strf(fdt, FIT_DESC_PROP, |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 367 | "%s image with one or more FDT blobs", |
| 368 | genimg_get_type_name(params->fit_image_type)); |
| 369 | fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION); |
| 370 | fdt_property_u32(fdt, "#address-cells", 1); |
| 371 | ret = fit_write_images(params, fdt); |
| 372 | if (ret) |
| 373 | return ret; |
| 374 | fit_write_configs(params, fdt); |
| 375 | fdt_end_node(fdt); |
| 376 | ret = fdt_finish(fdt); |
| 377 | if (ret) |
| 378 | return ret; |
| 379 | |
| 380 | return fdt_totalsize(fdt); |
| 381 | } |
| 382 | |
| 383 | static int fit_build(struct image_tool_params *params, const char *fname) |
| 384 | { |
| 385 | char *buf; |
| 386 | int size; |
| 387 | int ret; |
| 388 | int fd; |
| 389 | |
| 390 | size = fit_calc_size(params); |
| 391 | if (size < 0) |
| 392 | return -1; |
Fabio Estevam | c2248cb | 2020-07-27 21:03:13 -0300 | [diff] [blame] | 393 | buf = calloc(1, size); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 394 | if (!buf) { |
| 395 | fprintf(stderr, "%s: Out of memory (%d bytes)\n", |
| 396 | params->cmdname, size); |
| 397 | return -1; |
| 398 | } |
| 399 | ret = fit_build_fdt(params, buf, size); |
| 400 | if (ret < 0) { |
| 401 | fprintf(stderr, "%s: Failed to build FIT image\n", |
| 402 | params->cmdname); |
Simon Glass | c0ee1ca | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 403 | goto err_buf; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 404 | } |
| 405 | size = ret; |
| 406 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); |
| 407 | if (fd < 0) { |
| 408 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 409 | params->cmdname, fname, strerror(errno)); |
Tom Rini | b828665 | 2017-09-26 22:14:44 -0400 | [diff] [blame] | 410 | goto err_buf; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 411 | } |
| 412 | ret = write(fd, buf, size); |
| 413 | if (ret != size) { |
| 414 | fprintf(stderr, "%s: Can't write %s: %s\n", |
| 415 | params->cmdname, fname, strerror(errno)); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 416 | goto err; |
| 417 | } |
| 418 | close(fd); |
Simon Glass | c0ee1ca | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 419 | free(buf); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 420 | |
| 421 | return 0; |
| 422 | err: |
Simon Glass | c0ee1ca | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 423 | close(fd); |
| 424 | err_buf: |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 425 | free(buf); |
| 426 | return -1; |
| 427 | } |
| 428 | |
| 429 | /** |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 430 | * fit_extract_data() - Move all data outside the FIT |
| 431 | * |
| 432 | * This takes a normal FIT file and removes all the 'data' properties from it. |
| 433 | * The data is placed in an area after the FIT so that it can be accessed |
| 434 | * using an offset into that area. The 'data' properties turn into |
| 435 | * 'data-offset' properties. |
| 436 | * |
| 437 | * This function cannot cope with FITs with 'data-offset' properties. All |
| 438 | * data must be in 'data' properties on entry. |
| 439 | */ |
| 440 | static int fit_extract_data(struct image_tool_params *params, const char *fname) |
| 441 | { |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 442 | void *buf = NULL; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 443 | int buf_ptr; |
| 444 | int fit_size, new_size; |
| 445 | int fd; |
| 446 | struct stat sbuf; |
| 447 | void *fdt; |
| 448 | int ret; |
| 449 | int images; |
| 450 | int node; |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 451 | int image_number; |
| 452 | int align_size; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 453 | |
Tom Rini | 9664297 | 2020-05-06 11:05:17 -0400 | [diff] [blame] | 454 | align_size = params->bl_len ? params->bl_len : 4; |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 455 | fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 456 | if (fd < 0) |
| 457 | return -EIO; |
| 458 | fit_size = fdt_totalsize(fdt); |
| 459 | |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 460 | images = fdt_path_offset(fdt, FIT_IMAGES_PATH); |
| 461 | if (images < 0) { |
| 462 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 463 | ret = -EINVAL; |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 464 | goto err_munmap; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 465 | } |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 466 | image_number = fdtdec_get_child_count(fdt, images); |
| 467 | |
| 468 | /* |
| 469 | * Allocate space to hold the image data we will extract, |
| 470 | * extral space allocate for image alignment to prevent overflow. |
| 471 | */ |
Fabio Estevam | c2248cb | 2020-07-27 21:03:13 -0300 | [diff] [blame] | 472 | buf = calloc(1, fit_size + (align_size * image_number)); |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 473 | if (!buf) { |
| 474 | ret = -ENOMEM; |
| 475 | goto err_munmap; |
| 476 | } |
| 477 | buf_ptr = 0; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 478 | |
| 479 | for (node = fdt_first_subnode(fdt, images); |
| 480 | node >= 0; |
| 481 | node = fdt_next_subnode(fdt, node)) { |
| 482 | const char *data; |
| 483 | int len; |
| 484 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 485 | data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 486 | if (!data) |
| 487 | continue; |
| 488 | memcpy(buf + buf_ptr, data, len); |
| 489 | debug("Extracting data size %x\n", len); |
| 490 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 491 | ret = fdt_delprop(fdt, node, FIT_DATA_PROP); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 492 | if (ret) { |
| 493 | ret = -EPERM; |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 494 | goto err_munmap; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 495 | } |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 496 | if (params->external_offset > 0) { |
| 497 | /* An external offset positions the data absolutely. */ |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 498 | fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP, |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 499 | params->external_offset + buf_ptr); |
| 500 | } else { |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 501 | fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP, |
| 502 | buf_ptr); |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 503 | } |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 504 | fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len); |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 505 | buf_ptr += ALIGN(len, align_size); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* Pack the FDT and place the data after it */ |
| 509 | fdt_pack(fdt); |
| 510 | |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 511 | new_size = fdt_totalsize(fdt); |
Tom Rini | 9664297 | 2020-05-06 11:05:17 -0400 | [diff] [blame] | 512 | new_size = ALIGN(new_size, align_size); |
Kever Yang | 8e238b5 | 2020-03-30 11:56:24 +0800 | [diff] [blame] | 513 | fdt_set_totalsize(fdt, new_size); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 514 | debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt)); |
| 515 | debug("External data size %x\n", buf_ptr); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 516 | munmap(fdt, sbuf.st_size); |
| 517 | |
| 518 | if (ftruncate(fd, new_size)) { |
| 519 | debug("%s: Failed to truncate file: %s\n", __func__, |
| 520 | strerror(errno)); |
| 521 | ret = -EIO; |
| 522 | goto err; |
| 523 | } |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 524 | |
| 525 | /* Check if an offset for the external data was set. */ |
| 526 | if (params->external_offset > 0) { |
| 527 | if (params->external_offset < new_size) { |
Simon Glass | 00c0677 | 2022-01-09 20:13:38 -0700 | [diff] [blame] | 528 | fprintf(stderr, |
| 529 | "External offset %x overlaps FIT length %x\n", |
| 530 | params->external_offset, new_size); |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 531 | ret = -EINVAL; |
| 532 | goto err; |
| 533 | } |
| 534 | new_size = params->external_offset; |
| 535 | } |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 536 | if (lseek(fd, new_size, SEEK_SET) < 0) { |
| 537 | debug("%s: Failed to seek to end of file: %s\n", __func__, |
| 538 | strerror(errno)); |
| 539 | ret = -EIO; |
| 540 | goto err; |
| 541 | } |
| 542 | if (write(fd, buf, buf_ptr) != buf_ptr) { |
| 543 | debug("%s: Failed to write external data to file %s\n", |
| 544 | __func__, strerror(errno)); |
| 545 | ret = -EIO; |
| 546 | goto err; |
| 547 | } |
Tom Rini | b828665 | 2017-09-26 22:14:44 -0400 | [diff] [blame] | 548 | free(buf); |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 549 | close(fd); |
| 550 | return 0; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 551 | |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 552 | err_munmap: |
| 553 | munmap(fdt, sbuf.st_size); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 554 | err: |
Bin Meng | 6eb23874 | 2020-04-18 01:59:11 -0700 | [diff] [blame] | 555 | free(buf); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 556 | close(fd); |
| 557 | return ret; |
| 558 | } |
| 559 | |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 560 | static int fit_import_data(struct image_tool_params *params, const char *fname) |
| 561 | { |
| 562 | void *fdt, *old_fdt; |
| 563 | int fit_size, new_size, size, data_base; |
| 564 | int fd; |
| 565 | struct stat sbuf; |
| 566 | int ret; |
| 567 | int images; |
| 568 | int node; |
| 569 | |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 570 | fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false); |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 571 | if (fd < 0) |
| 572 | return -EIO; |
| 573 | fit_size = fdt_totalsize(old_fdt); |
Kever Yang | 8c11bc1 | 2020-03-30 11:56:22 +0800 | [diff] [blame] | 574 | data_base = ALIGN(fit_size, 4); |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 575 | |
| 576 | /* Allocate space to hold the new FIT */ |
| 577 | size = sbuf.st_size + 16384; |
Fabio Estevam | c2248cb | 2020-07-27 21:03:13 -0300 | [diff] [blame] | 578 | fdt = calloc(1, size); |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 579 | if (!fdt) { |
| 580 | fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n", |
| 581 | __func__, size); |
| 582 | ret = -ENOMEM; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 583 | goto err_munmap; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 584 | } |
| 585 | ret = fdt_open_into(old_fdt, fdt, size); |
| 586 | if (ret) { |
| 587 | debug("%s: Failed to expand FIT: %s\n", __func__, |
| 588 | fdt_strerror(errno)); |
| 589 | ret = -EINVAL; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 590 | goto err_munmap; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | images = fdt_path_offset(fdt, FIT_IMAGES_PATH); |
| 594 | if (images < 0) { |
| 595 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 596 | ret = -EINVAL; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 597 | goto err_munmap; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | for (node = fdt_first_subnode(fdt, images); |
| 601 | node >= 0; |
| 602 | node = fdt_next_subnode(fdt, node)) { |
| 603 | int buf_ptr; |
| 604 | int len; |
| 605 | |
| 606 | buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1); |
| 607 | len = fdtdec_get_int(fdt, node, "data-size", -1); |
| 608 | if (buf_ptr == -1 || len == -1) |
| 609 | continue; |
| 610 | debug("Importing data size %x\n", len); |
| 611 | |
Patrick Oppenlander | 14139f5 | 2020-07-30 14:31:48 +1000 | [diff] [blame] | 612 | ret = fdt_setprop(fdt, node, "data", |
| 613 | old_fdt + data_base + buf_ptr, len); |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 614 | if (ret) { |
| 615 | debug("%s: Failed to write property: %s\n", __func__, |
| 616 | fdt_strerror(ret)); |
| 617 | ret = -EINVAL; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 618 | goto err_munmap; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 622 | munmap(old_fdt, sbuf.st_size); |
| 623 | |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 624 | /* Close the old fd so we can re-use it. */ |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 625 | close(fd); |
| 626 | |
| 627 | /* Pack the FDT and place the data after it */ |
| 628 | fdt_pack(fdt); |
| 629 | |
| 630 | new_size = fdt_totalsize(fdt); |
| 631 | debug("Size expanded from %x to %x\n", fit_size, new_size); |
| 632 | |
| 633 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); |
| 634 | if (fd < 0) { |
| 635 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 636 | params->cmdname, fname, strerror(errno)); |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 637 | ret = -EIO; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 638 | goto err; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 639 | } |
| 640 | if (write(fd, fdt, new_size) != new_size) { |
| 641 | debug("%s: Failed to write external data to file %s\n", |
| 642 | __func__, strerror(errno)); |
| 643 | ret = -EIO; |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 644 | goto err; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 645 | } |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 646 | |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 647 | free(fdt); |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 648 | close(fd); |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 649 | return 0; |
| 650 | |
| 651 | err_munmap: |
Tom Rini | b828665 | 2017-09-26 22:14:44 -0400 | [diff] [blame] | 652 | munmap(old_fdt, sbuf.st_size); |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 653 | err: |
Simon Glass | 5014cc1 | 2016-03-16 07:45:38 -0600 | [diff] [blame] | 654 | free(fdt); |
Lihua Zhao | 4536ef8 | 2020-04-18 01:59:10 -0700 | [diff] [blame] | 655 | close(fd); |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 656 | return ret; |
| 657 | } |
| 658 | |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 659 | static int copyfile(const char *src, const char *dst) |
| 660 | { |
| 661 | int fd_src = -1, fd_dst = -1; |
| 662 | void *buf = NULL; |
| 663 | ssize_t size; |
| 664 | size_t count; |
| 665 | int ret = -1; |
| 666 | |
| 667 | fd_src = open(src, O_RDONLY); |
| 668 | if (fd_src < 0) { |
| 669 | printf("Can't open file %s (%s)\n", src, strerror(errno)); |
| 670 | goto out; |
| 671 | } |
| 672 | |
Thomas Hebb | 97caa07 | 2020-03-01 10:47:53 -0800 | [diff] [blame] | 673 | fd_dst = open(dst, O_WRONLY | O_CREAT, 0666); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 674 | if (fd_dst < 0) { |
| 675 | printf("Can't open file %s (%s)\n", dst, strerror(errno)); |
| 676 | goto out; |
| 677 | } |
| 678 | |
Fabio Estevam | c2248cb | 2020-07-27 21:03:13 -0300 | [diff] [blame] | 679 | buf = calloc(1, 512); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 680 | if (!buf) { |
| 681 | printf("Can't allocate buffer to copy file\n"); |
| 682 | goto out; |
| 683 | } |
| 684 | |
| 685 | while (1) { |
| 686 | size = read(fd_src, buf, 512); |
| 687 | if (size < 0) { |
| 688 | printf("Can't read file %s\n", src); |
| 689 | goto out; |
| 690 | } |
| 691 | if (!size) |
| 692 | break; |
| 693 | |
| 694 | count = size; |
| 695 | size = write(fd_dst, buf, count); |
| 696 | if (size < 0) { |
| 697 | printf("Can't write file %s\n", dst); |
| 698 | goto out; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | ret = 0; |
| 703 | |
| 704 | out: |
| 705 | if (fd_src >= 0) |
| 706 | close(fd_src); |
| 707 | if (fd_dst >= 0) |
| 708 | close(fd_dst); |
| 709 | if (buf) |
| 710 | free(buf); |
| 711 | |
| 712 | return ret; |
| 713 | } |
| 714 | |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 715 | /** |
Prafulla Wadaskar | fabf3cf | 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 | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 727 | static int fit_handle_file(struct image_tool_params *params) |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 728 | { |
| 729 | char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 730 | char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0}; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 731 | char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 732 | size_t size_inc; |
| 733 | int ret; |
Prafulla Wadaskar | fabf3cf | 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 | 88be5d3 | 2021-05-25 23:15:27 +0200 | [diff] [blame] | 742 | "can't create tmpfile.\n", |
Prafulla Wadaskar | fabf3cf | 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 | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 748 | /* We either compile the source file, or use the existing FIT image */ |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 749 | if (params->auto_its) { |
| 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 | ff6c3b2 | 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 | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 760 | debug("Trying to execute \"%s\"\n", cmd); |
| 761 | } else { |
Mirza, Taimoor | 74a21dd | 2017-10-04 20:28:03 +0500 | [diff] [blame] | 762 | snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"", |
Simon Glass | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 763 | params->imagefile, tmpfile); |
| 764 | } |
Sven Roederer | cbcb399 | 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 | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 768 | |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 769 | if (*cmd && system(cmd) == -1) { |
Prafulla Wadaskar | fabf3cf | 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 | 4161c41 | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 772 | goto err_system; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 773 | } |
| 774 | |
Simon Glass | d11b700 | 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 | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 780 | /* |
Philippe Reynes | 3148e42 | 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 | 802aa82 | 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 | 3148e42 | 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 | 802aa82 | 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 | b4d8b09 | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 809 | if (ret) { |
Simon Glass | 061752e | 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 | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 812 | goto err_system; |
Simon Glass | b4d8b09 | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 813 | } |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 814 | |
Simon Glass | afd728c | 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 | fabf3cf | 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 | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 827 | unlink(bakfile); |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 828 | unlink (params->imagefile); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 829 | return EXIT_FAILURE; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 830 | } |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 831 | unlink(bakfile); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 832 | return EXIT_SUCCESS; |
Simon Glass | 4161c41 | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 833 | |
Simon Glass | 4161c41 | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 834 | err_system: |
| 835 | unlink(tmpfile); |
Philippe Reynes | 3148e42 | 2019-12-18 18:25:41 +0100 | [diff] [blame] | 836 | unlink(bakfile); |
Simon Glass | 4161c41 | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 837 | return -1; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 838 | } |
| 839 | |
Guilherme Maciel Ferreira | 3c46bcd | 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 | 166d5e9 | 2019-09-17 17:09:34 -0400 | [diff] [blame] | 856 | int ret; |
Guilherme Maciel Ferreira | 3c46bcd | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 857 | |
Andrew F. Davis | 166d5e9 | 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 | 3c46bcd | 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 | 3c46bcd | 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 | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 931 | static int fit_check_params(struct image_tool_params *params) |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 932 | { |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 933 | if (params->auto_its) |
| 934 | return 0; |
Heinrich Schuchardt | 915f5aa | 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 | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 938 | } |
| 939 | |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 940 | U_BOOT_IMAGE_TYPE( |
| 941 | fitimage, |
| 942 | "FIT Image support", |
| 943 | sizeof(image_header_t), |
| 944 | (void *)&header, |
| 945 | fit_check_params, |
| 946 | fit_verify_header, |
| 947 | fit_print_contents, |
| 948 | NULL, |
Guilherme Maciel Ferreira | 3c46bcd | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 949 | fit_extract_contents, |
Guilherme Maciel Ferreira | 28be1cf | 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 | ); |