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