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