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