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