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