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