Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2008 Semihalf |
| 4 | * |
| 5 | * (C) Copyright 2000-2004 |
| 6 | * DENX Software Engineering |
| 7 | * Wolfgang Denk, wd@denx.de |
| 8 | * |
| 9 | * Updated-by: Prafulla Wadaskar <prafulla@marvell.com> |
| 10 | * FIT image specific code abstracted from mkimage.c |
| 11 | * some functions added to address abstraction |
| 12 | * |
| 13 | * All rights reserved. |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 14 | */ |
| 15 | |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 16 | #include "imagetool.h" |
Heiko Schocher | 3cb743c | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 17 | #include "fit_common.h" |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 18 | #include "mkimage.h" |
| 19 | #include <image.h> |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 20 | #include <stdarg.h> |
| 21 | #include <version.h> |
Prafulla Wadaskar | fabf3cf | 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 | 802aa82 | 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 | bff733f | 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 | 802aa82 | 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 | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 45 | &dest_blob, &dest_sbuf, false, |
| 46 | false); |
Simon Glass | 802aa82 | 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 | f8e066c | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 55 | if (params->datafile) { |
Alex Kiernan | c4c7e0d | 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 | f8e066c | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 58 | ret = fit_set_timestamp(ptr, 0, time); |
| 59 | } |
Simon Glass | 802aa82 | 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 | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 64 | params->require_keys, |
Alex Kiernan | 697fcdc | 2018-06-20 20:10:52 +0000 | [diff] [blame] | 65 | params->engine_id, |
| 66 | params->cmdname); |
Simon Glass | 802aa82 | 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 | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 81 | /** |
Simon Glass | 88e31cb | 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 | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 86 | struct content_info *cont; |
Simon Glass | 88e31cb | 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 | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 92 | total_size = size; |
Tomeu Vizoso | 8d83ed2 | 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 | bd8bc5d | 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 | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 105 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 106 | /* Add space for properties */ |
| 107 | total_size += size + 300; |
| 108 | } |
Simon Glass | 88e31cb | 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 | 9ab7f05 | 2016-03-16 07:45:42 -0600 | [diff] [blame] | 139 | goto err; |
Simon Glass | 88e31cb | 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 | 9ab7f05 | 2016-03-16 07:45:42 -0600 | [diff] [blame] | 146 | close(fd); |
Simon Glass | 88e31cb | 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 | bd8bc5d | 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 | 88e31cb | 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 | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 190 | * We always include the main image (params->datafile). If there are device |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 191 | * tree files, we include an fdt- node for each of those too. |
Simon Glass | 88e31cb | 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 | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 195 | struct content_info *cont; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 196 | const char *typename; |
| 197 | char str[100]; |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 198 | int upto; |
Simon Glass | 88e31cb | 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 | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 205 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 206 | fdt_begin_node(fdt, str); |
Michal Simek | a4e678d | 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 | aeeea7f | 2016-06-30 10:52:12 -0600 | [diff] [blame] | 210 | genimg_get_arch_short_name(params->arch)); |
Michal Simek | a4e678d | 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 | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 214 | genimg_get_comp_short_name(params->comp)); |
Michal Simek | a4e678d | 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 | 88e31cb | 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 | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 222 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 223 | if (ret) |
| 224 | return ret; |
| 225 | fdt_end_node(fdt); |
| 226 | |
Simon Glass | bd8bc5d | 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; |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 232 | snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 233 | fdt_begin_node(fdt, str); |
| 234 | |
| 235 | get_basename(str, sizeof(str), cont->fname); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 236 | fdt_property_string(fdt, FIT_DESC_PROP, str); |
| 237 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, |
| 238 | cont->fname); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 239 | if (ret) |
| 240 | return ret; |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 241 | fdt_property_string(fdt, FIT_TYPE_PROP, typename); |
| 242 | fdt_property_string(fdt, FIT_ARCH_PROP, |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 243 | genimg_get_arch_short_name(params->arch)); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 244 | fdt_property_string(fdt, FIT_COMP_PROP, |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 245 | genimg_get_comp_short_name(IH_COMP_NONE)); |
| 246 | fdt_end_node(fdt); |
| 247 | } |
| 248 | |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 249 | /* And a ramdisk file if available */ |
| 250 | if (params->fit_ramdisk) { |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 251 | fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1"); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 252 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 253 | fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP); |
| 254 | fdt_property_string(fdt, FIT_OS_PROP, |
| 255 | genimg_get_os_short_name(params->os)); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 256 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 257 | ret = fdt_property_file(params, fdt, FIT_DATA_PROP, |
| 258 | params->fit_ramdisk); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 259 | if (ret) |
| 260 | return ret; |
| 261 | |
| 262 | fdt_end_node(fdt); |
| 263 | } |
| 264 | |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 265 | fdt_end_node(fdt); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * fit_write_configs() - Write out a list of configurations to the FIT |
| 272 | * |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 273 | * If there are device tree files, we include a configuration for each, which |
| 274 | * selects the main image (params->datafile) and its corresponding device |
| 275 | * tree file. |
| 276 | * |
| 277 | * Otherwise we just create a configuration with the main image in it. |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 278 | */ |
| 279 | static void fit_write_configs(struct image_tool_params *params, char *fdt) |
| 280 | { |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 281 | struct content_info *cont; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 282 | const char *typename; |
| 283 | char str[100]; |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 284 | int upto; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 285 | |
| 286 | fdt_begin_node(fdt, "configurations"); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 287 | fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1"); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 288 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 289 | upto = 0; |
| 290 | for (cont = params->content_head; cont; cont = cont->next) { |
| 291 | if (cont->type != IH_TYPE_FLATDT) |
| 292 | continue; |
| 293 | typename = genimg_get_type_short_name(cont->type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 294 | snprintf(str, sizeof(str), "conf-%d", ++upto); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 295 | fdt_begin_node(fdt, str); |
| 296 | |
| 297 | get_basename(str, sizeof(str), cont->fname); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 298 | fdt_property_string(fdt, FIT_DESC_PROP, str); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 299 | |
| 300 | typename = genimg_get_type_short_name(params->fit_image_type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 301 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 302 | fdt_property_string(fdt, typename, str); |
Abel Vesa | 7e0cb5e | 2019-03-12 08:34:32 +0000 | [diff] [blame] | 303 | fdt_property_string(fdt, FIT_LOADABLE_PROP, str); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 304 | |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 305 | if (params->fit_ramdisk) |
| 306 | fdt_property_string(fdt, FIT_RAMDISK_PROP, |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 307 | FIT_RAMDISK_PROP "-1"); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 308 | |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 309 | snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 310 | fdt_property_string(fdt, FIT_FDT_PROP, str); |
| 311 | fdt_end_node(fdt); |
| 312 | } |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 313 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 314 | if (!upto) { |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 315 | fdt_begin_node(fdt, "conf-1"); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 316 | typename = genimg_get_type_short_name(params->fit_image_type); |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 317 | snprintf(str, sizeof(str), "%s-1", typename); |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 318 | fdt_property_string(fdt, typename, str); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 319 | |
| 320 | if (params->fit_ramdisk) |
| 321 | fdt_property_string(fdt, FIT_RAMDISK_PROP, |
Andre Przywara | 9ef25e8 | 2017-12-04 02:05:12 +0000 | [diff] [blame] | 322 | FIT_RAMDISK_PROP "-1"); |
Tomeu Vizoso | 8d83ed2 | 2016-11-04 14:22:15 +0100 | [diff] [blame] | 323 | |
Simon Glass | bd8bc5d | 2016-02-22 22:55:52 -0700 | [diff] [blame] | 324 | fdt_end_node(fdt); |
| 325 | } |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 326 | |
| 327 | fdt_end_node(fdt); |
| 328 | } |
| 329 | |
| 330 | static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size) |
| 331 | { |
| 332 | int ret; |
| 333 | |
| 334 | ret = fdt_create(fdt, size); |
| 335 | if (ret) |
| 336 | return ret; |
| 337 | fdt_finish_reservemap(fdt); |
| 338 | fdt_begin_node(fdt, ""); |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 339 | fdt_property_strf(fdt, FIT_DESC_PROP, |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 340 | "%s image with one or more FDT blobs", |
| 341 | genimg_get_type_name(params->fit_image_type)); |
| 342 | fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION); |
| 343 | fdt_property_u32(fdt, "#address-cells", 1); |
| 344 | ret = fit_write_images(params, fdt); |
| 345 | if (ret) |
| 346 | return ret; |
| 347 | fit_write_configs(params, fdt); |
| 348 | fdt_end_node(fdt); |
| 349 | ret = fdt_finish(fdt); |
| 350 | if (ret) |
| 351 | return ret; |
| 352 | |
| 353 | return fdt_totalsize(fdt); |
| 354 | } |
| 355 | |
| 356 | static int fit_build(struct image_tool_params *params, const char *fname) |
| 357 | { |
| 358 | char *buf; |
| 359 | int size; |
| 360 | int ret; |
| 361 | int fd; |
| 362 | |
| 363 | size = fit_calc_size(params); |
| 364 | if (size < 0) |
| 365 | return -1; |
| 366 | buf = malloc(size); |
| 367 | if (!buf) { |
| 368 | fprintf(stderr, "%s: Out of memory (%d bytes)\n", |
| 369 | params->cmdname, size); |
| 370 | return -1; |
| 371 | } |
| 372 | ret = fit_build_fdt(params, buf, size); |
| 373 | if (ret < 0) { |
| 374 | fprintf(stderr, "%s: Failed to build FIT image\n", |
| 375 | params->cmdname); |
Simon Glass | c0ee1ca | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 376 | goto err_buf; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 377 | } |
| 378 | size = ret; |
| 379 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); |
| 380 | if (fd < 0) { |
| 381 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 382 | params->cmdname, fname, strerror(errno)); |
Tom Rini | b828665 | 2017-09-26 22:14:44 -0400 | [diff] [blame] | 383 | goto err_buf; |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 384 | } |
| 385 | ret = write(fd, buf, size); |
| 386 | if (ret != size) { |
| 387 | fprintf(stderr, "%s: Can't write %s: %s\n", |
| 388 | params->cmdname, fname, strerror(errno)); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 389 | goto err; |
| 390 | } |
| 391 | close(fd); |
Simon Glass | c0ee1ca | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 392 | free(buf); |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 393 | |
| 394 | return 0; |
| 395 | err: |
Simon Glass | c0ee1ca | 2016-03-16 07:45:41 -0600 | [diff] [blame] | 396 | close(fd); |
| 397 | err_buf: |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 398 | free(buf); |
| 399 | return -1; |
| 400 | } |
| 401 | |
| 402 | /** |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 403 | * fit_extract_data() - Move all data outside the FIT |
| 404 | * |
| 405 | * This takes a normal FIT file and removes all the 'data' properties from it. |
| 406 | * The data is placed in an area after the FIT so that it can be accessed |
| 407 | * using an offset into that area. The 'data' properties turn into |
| 408 | * 'data-offset' properties. |
| 409 | * |
| 410 | * This function cannot cope with FITs with 'data-offset' properties. All |
| 411 | * data must be in 'data' properties on entry. |
| 412 | */ |
| 413 | static int fit_extract_data(struct image_tool_params *params, const char *fname) |
| 414 | { |
| 415 | void *buf; |
| 416 | int buf_ptr; |
| 417 | int fit_size, new_size; |
| 418 | int fd; |
| 419 | struct stat sbuf; |
| 420 | void *fdt; |
| 421 | int ret; |
| 422 | int images; |
| 423 | int node; |
| 424 | |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 425 | fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 426 | if (fd < 0) |
| 427 | return -EIO; |
| 428 | fit_size = fdt_totalsize(fdt); |
| 429 | |
| 430 | /* Allocate space to hold the image data we will extract */ |
| 431 | buf = malloc(fit_size); |
| 432 | if (!buf) { |
| 433 | ret = -ENOMEM; |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 434 | goto err_munmap; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 435 | } |
| 436 | buf_ptr = 0; |
| 437 | |
| 438 | images = fdt_path_offset(fdt, FIT_IMAGES_PATH); |
| 439 | if (images < 0) { |
| 440 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 441 | ret = -EINVAL; |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 442 | goto err_munmap; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | for (node = fdt_first_subnode(fdt, images); |
| 446 | node >= 0; |
| 447 | node = fdt_next_subnode(fdt, node)) { |
| 448 | const char *data; |
| 449 | int len; |
| 450 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 451 | data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 452 | if (!data) |
| 453 | continue; |
| 454 | memcpy(buf + buf_ptr, data, len); |
| 455 | debug("Extracting data size %x\n", len); |
| 456 | |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 457 | ret = fdt_delprop(fdt, node, FIT_DATA_PROP); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 458 | if (ret) { |
| 459 | ret = -EPERM; |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 460 | goto err_munmap; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 461 | } |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 462 | if (params->external_offset > 0) { |
| 463 | /* An external offset positions the data absolutely. */ |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 464 | fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP, |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 465 | params->external_offset + buf_ptr); |
| 466 | } else { |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 467 | fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP, |
| 468 | buf_ptr); |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 469 | } |
Michal Simek | a4e678d | 2018-07-20 12:31:02 +0200 | [diff] [blame] | 470 | fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 471 | |
| 472 | buf_ptr += (len + 3) & ~3; |
| 473 | } |
| 474 | |
| 475 | /* Pack the FDT and place the data after it */ |
| 476 | fdt_pack(fdt); |
| 477 | |
| 478 | debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt)); |
| 479 | debug("External data size %x\n", buf_ptr); |
| 480 | new_size = fdt_totalsize(fdt); |
| 481 | new_size = (new_size + 3) & ~3; |
| 482 | munmap(fdt, sbuf.st_size); |
| 483 | |
| 484 | if (ftruncate(fd, new_size)) { |
| 485 | debug("%s: Failed to truncate file: %s\n", __func__, |
| 486 | strerror(errno)); |
| 487 | ret = -EIO; |
| 488 | goto err; |
| 489 | } |
Teddy Reed | a845762 | 2016-06-09 19:38:02 -0700 | [diff] [blame] | 490 | |
| 491 | /* Check if an offset for the external data was set. */ |
| 492 | if (params->external_offset > 0) { |
| 493 | if (params->external_offset < new_size) { |
| 494 | debug("External offset %x overlaps FIT length %x", |
| 495 | params->external_offset, new_size); |
| 496 | ret = -EINVAL; |
| 497 | goto err; |
| 498 | } |
| 499 | new_size = params->external_offset; |
| 500 | } |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 501 | if (lseek(fd, new_size, SEEK_SET) < 0) { |
| 502 | debug("%s: Failed to seek to end of file: %s\n", __func__, |
| 503 | strerror(errno)); |
| 504 | ret = -EIO; |
| 505 | goto err; |
| 506 | } |
| 507 | if (write(fd, buf, buf_ptr) != buf_ptr) { |
| 508 | debug("%s: Failed to write external data to file %s\n", |
| 509 | __func__, strerror(errno)); |
| 510 | ret = -EIO; |
| 511 | goto err; |
| 512 | } |
Tom Rini | b828665 | 2017-09-26 22:14:44 -0400 | [diff] [blame] | 513 | free(buf); |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 514 | close(fd); |
| 515 | return 0; |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 516 | |
Simon Glass | 4e7505f | 2016-03-16 07:45:39 -0600 | [diff] [blame] | 517 | err_munmap: |
| 518 | munmap(fdt, sbuf.st_size); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 519 | err: |
Simon Glass | f3a6e8f | 2016-03-16 07:45:40 -0600 | [diff] [blame] | 520 | if (buf) |
| 521 | free(buf); |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 522 | close(fd); |
| 523 | return ret; |
| 524 | } |
| 525 | |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 526 | static int fit_import_data(struct image_tool_params *params, const char *fname) |
| 527 | { |
| 528 | void *fdt, *old_fdt; |
| 529 | int fit_size, new_size, size, data_base; |
| 530 | int fd; |
| 531 | struct stat sbuf; |
| 532 | int ret; |
| 533 | int images; |
| 534 | int node; |
| 535 | |
Luca Boccassi | bff733f | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 536 | fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false); |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 537 | if (fd < 0) |
| 538 | return -EIO; |
| 539 | fit_size = fdt_totalsize(old_fdt); |
| 540 | data_base = (fit_size + 3) & ~3; |
| 541 | |
| 542 | /* Allocate space to hold the new FIT */ |
| 543 | size = sbuf.st_size + 16384; |
| 544 | fdt = malloc(size); |
| 545 | if (!fdt) { |
| 546 | fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n", |
| 547 | __func__, size); |
| 548 | ret = -ENOMEM; |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 549 | goto err_has_fd; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 550 | } |
| 551 | ret = fdt_open_into(old_fdt, fdt, size); |
| 552 | if (ret) { |
| 553 | debug("%s: Failed to expand FIT: %s\n", __func__, |
| 554 | fdt_strerror(errno)); |
| 555 | ret = -EINVAL; |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 556 | goto err_has_fd; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | images = fdt_path_offset(fdt, FIT_IMAGES_PATH); |
| 560 | if (images < 0) { |
| 561 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 562 | ret = -EINVAL; |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 563 | goto err_has_fd; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | for (node = fdt_first_subnode(fdt, images); |
| 567 | node >= 0; |
| 568 | node = fdt_next_subnode(fdt, node)) { |
| 569 | int buf_ptr; |
| 570 | int len; |
| 571 | |
| 572 | buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1); |
| 573 | len = fdtdec_get_int(fdt, node, "data-size", -1); |
| 574 | if (buf_ptr == -1 || len == -1) |
| 575 | continue; |
| 576 | debug("Importing data size %x\n", len); |
| 577 | |
| 578 | ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr, |
| 579 | len); |
| 580 | if (ret) { |
| 581 | debug("%s: Failed to write property: %s\n", __func__, |
| 582 | fdt_strerror(ret)); |
| 583 | ret = -EINVAL; |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 584 | goto err_has_fd; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 588 | /* Close the old fd so we can re-use it. */ |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 589 | close(fd); |
| 590 | |
| 591 | /* Pack the FDT and place the data after it */ |
| 592 | fdt_pack(fdt); |
| 593 | |
| 594 | new_size = fdt_totalsize(fdt); |
| 595 | debug("Size expanded from %x to %x\n", fit_size, new_size); |
| 596 | |
| 597 | fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); |
| 598 | if (fd < 0) { |
| 599 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 600 | params->cmdname, fname, strerror(errno)); |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 601 | ret = -EIO; |
| 602 | goto err_no_fd; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 603 | } |
| 604 | if (write(fd, fdt, new_size) != new_size) { |
| 605 | debug("%s: Failed to write external data to file %s\n", |
| 606 | __func__, strerror(errno)); |
| 607 | ret = -EIO; |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 608 | goto err_has_fd; |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 609 | } |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 610 | |
| 611 | ret = 0; |
| 612 | |
Tom Rini | a29829e | 2017-10-07 11:27:59 -0400 | [diff] [blame] | 613 | err_has_fd: |
| 614 | close(fd); |
| 615 | err_no_fd: |
Tom Rini | b828665 | 2017-09-26 22:14:44 -0400 | [diff] [blame] | 616 | munmap(old_fdt, sbuf.st_size); |
Simon Glass | 5014cc1 | 2016-03-16 07:45:38 -0600 | [diff] [blame] | 617 | free(fdt); |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 618 | return ret; |
| 619 | } |
| 620 | |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 621 | /** |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 622 | * fit_handle_file - main FIT file processing function |
| 623 | * |
| 624 | * fit_handle_file() runs dtc to convert .its to .itb, includes |
| 625 | * binary data, updates timestamp property and calculates hashes. |
| 626 | * |
| 627 | * datafile - .its file |
| 628 | * imagefile - .itb file |
| 629 | * |
| 630 | * returns: |
| 631 | * only on success, otherwise calls exit (EXIT_FAILURE); |
| 632 | */ |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 633 | static int fit_handle_file(struct image_tool_params *params) |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 634 | { |
| 635 | char tmpfile[MKIMAGE_MAX_TMPFILE_LEN]; |
| 636 | char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN]; |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 637 | size_t size_inc; |
| 638 | int ret; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 639 | |
| 640 | /* Flattened Image Tree (FIT) format handling */ |
| 641 | debug ("FIT format handling\n"); |
| 642 | |
| 643 | /* call dtc to include binary properties into the tmp file */ |
| 644 | if (strlen (params->imagefile) + |
| 645 | strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) { |
| 646 | fprintf (stderr, "%s: Image file name (%s) too long, " |
| 647 | "can't create tmpfile", |
| 648 | params->imagefile, params->cmdname); |
| 649 | return (EXIT_FAILURE); |
| 650 | } |
| 651 | sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX); |
| 652 | |
Simon Glass | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 653 | /* We either compile the source file, or use the existing FIT image */ |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 654 | if (params->auto_its) { |
| 655 | if (fit_build(params, tmpfile)) { |
| 656 | fprintf(stderr, "%s: failed to build FIT\n", |
| 657 | params->cmdname); |
| 658 | return EXIT_FAILURE; |
| 659 | } |
| 660 | *cmd = '\0'; |
| 661 | } else if (params->datafile) { |
Stefan Theil | ff6c3b2 | 2018-03-08 09:00:13 +0100 | [diff] [blame] | 662 | /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */ |
| 663 | snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"", |
| 664 | MKIMAGE_DTC, params->dtc, tmpfile, params->datafile); |
Simon Glass | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 665 | debug("Trying to execute \"%s\"\n", cmd); |
| 666 | } else { |
Mirza, Taimoor | 74a21dd | 2017-10-04 20:28:03 +0500 | [diff] [blame] | 667 | snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"", |
Simon Glass | ce8c3ca | 2013-06-13 15:10:05 -0700 | [diff] [blame] | 668 | params->imagefile, tmpfile); |
| 669 | } |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 670 | if (*cmd && system(cmd) == -1) { |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 671 | fprintf (stderr, "%s: system(%s) failed: %s\n", |
| 672 | params->cmdname, cmd, strerror(errno)); |
Simon Glass | 4161c41 | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 673 | goto err_system; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 674 | } |
| 675 | |
Simon Glass | d11b700 | 2016-02-22 22:55:54 -0700 | [diff] [blame] | 676 | /* Move the data so it is internal to the FIT, if needed */ |
| 677 | ret = fit_import_data(params, tmpfile); |
| 678 | if (ret) |
| 679 | goto err_system; |
| 680 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 681 | /* |
| 682 | * Set hashes for images in the blob. Unfortunately we may need more |
| 683 | * space in either FDT, so keep trying until we succeed. |
| 684 | * |
| 685 | * Note: this is pretty inefficient for signing, since we must |
| 686 | * calculate the signature every time. It would be better to calculate |
| 687 | * all the data and then store it in a separate step. However, this |
| 688 | * would be considerably more complex to implement. Generally a few |
| 689 | * steps of this loop is enough to sign with several keys. |
| 690 | */ |
| 691 | for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) { |
| 692 | ret = fit_add_file_data(params, size_inc, tmpfile); |
| 693 | if (!ret || ret != -ENOSPC) |
| 694 | break; |
Simon Glass | b4d8b09 | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 697 | if (ret) { |
Simon Glass | 061752e | 2016-07-03 09:40:43 -0600 | [diff] [blame] | 698 | fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n", |
| 699 | params->cmdname, ret); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 700 | goto err_system; |
Simon Glass | b4d8b09 | 2013-06-13 15:10:04 -0700 | [diff] [blame] | 701 | } |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 702 | |
Simon Glass | afd728c | 2016-02-22 22:55:53 -0700 | [diff] [blame] | 703 | /* Move the data so it is external to the FIT, if requested */ |
| 704 | if (params->external_data) { |
| 705 | ret = fit_extract_data(params, tmpfile); |
| 706 | if (ret) |
| 707 | goto err_system; |
| 708 | } |
| 709 | |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 710 | if (rename (tmpfile, params->imagefile) == -1) { |
| 711 | fprintf (stderr, "%s: Can't rename %s to %s: %s\n", |
| 712 | params->cmdname, tmpfile, params->imagefile, |
| 713 | strerror (errno)); |
| 714 | unlink (tmpfile); |
| 715 | unlink (params->imagefile); |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 716 | return EXIT_FAILURE; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 717 | } |
Simon Glass | 802aa82 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 718 | return EXIT_SUCCESS; |
Simon Glass | 4161c41 | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 719 | |
Simon Glass | 4161c41 | 2013-05-08 08:05:57 +0000 | [diff] [blame] | 720 | err_system: |
| 721 | unlink(tmpfile); |
| 722 | return -1; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 723 | } |
| 724 | |
Guilherme Maciel Ferreira | 3c46bcd | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 725 | /** |
| 726 | * fit_image_extract - extract a FIT component image |
| 727 | * @fit: pointer to the FIT format image header |
| 728 | * @image_noffset: offset of the component image node |
| 729 | * @file_name: name of the file to store the FIT sub-image |
| 730 | * |
| 731 | * returns: |
| 732 | * zero in case of success or a negative value if fail. |
| 733 | */ |
| 734 | static int fit_image_extract( |
| 735 | const void *fit, |
| 736 | int image_noffset, |
| 737 | const char *file_name) |
| 738 | { |
| 739 | const void *file_data; |
| 740 | size_t file_size = 0; |
| 741 | |
| 742 | /* get the "data" property of component at offset "image_noffset" */ |
| 743 | fit_image_get_data(fit, image_noffset, &file_data, &file_size); |
| 744 | |
| 745 | /* save the "file_data" into the file specified by "file_name" */ |
| 746 | return imagetool_save_subimage(file_name, (ulong) file_data, file_size); |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * fit_extract_contents - retrieve a sub-image component from the FIT image |
| 751 | * @ptr: pointer to the FIT format image header |
| 752 | * @params: command line parameters |
| 753 | * |
| 754 | * returns: |
| 755 | * zero in case of success or a negative value if fail. |
| 756 | */ |
| 757 | static int fit_extract_contents(void *ptr, struct image_tool_params *params) |
| 758 | { |
| 759 | int images_noffset; |
| 760 | int noffset; |
| 761 | int ndepth; |
| 762 | const void *fit = ptr; |
| 763 | int count = 0; |
| 764 | const char *p; |
| 765 | |
| 766 | /* Indent string is defined in header image.h */ |
| 767 | p = IMAGE_INDENT_STRING; |
| 768 | |
| 769 | if (!fit_check_format(fit)) { |
| 770 | printf("Bad FIT image format\n"); |
| 771 | return -1; |
| 772 | } |
| 773 | |
| 774 | /* Find images parent node offset */ |
| 775 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 776 | if (images_noffset < 0) { |
| 777 | printf("Can't find images parent node '%s' (%s)\n", |
| 778 | FIT_IMAGES_PATH, fdt_strerror(images_noffset)); |
| 779 | return -1; |
| 780 | } |
| 781 | |
| 782 | /* Avoid any overrun */ |
| 783 | count = fit_get_subimage_count(fit, images_noffset); |
| 784 | if ((params->pflag < 0) || (count <= params->pflag)) { |
| 785 | printf("No such component at '%d'\n", params->pflag); |
| 786 | return -1; |
| 787 | } |
| 788 | |
| 789 | /* Process its subnodes, extract the desired component from image */ |
| 790 | for (ndepth = 0, count = 0, |
| 791 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 792 | (noffset >= 0) && (ndepth > 0); |
| 793 | noffset = fdt_next_node(fit, noffset, &ndepth)) { |
| 794 | if (ndepth == 1) { |
| 795 | /* |
| 796 | * Direct child node of the images parent node, |
| 797 | * i.e. component image node. |
| 798 | */ |
| 799 | if (params->pflag == count) { |
| 800 | printf("Extracted:\n%s Image %u (%s)\n", p, |
| 801 | count, fit_get_name(fit, noffset, NULL)); |
| 802 | |
| 803 | fit_image_print(fit, noffset, p); |
| 804 | |
| 805 | return fit_image_extract(fit, noffset, |
| 806 | params->outfile); |
| 807 | } |
| 808 | |
| 809 | count++; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 816 | static int fit_check_params(struct image_tool_params *params) |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 817 | { |
Simon Glass | 88e31cb | 2016-02-22 22:55:51 -0700 | [diff] [blame] | 818 | if (params->auto_its) |
| 819 | return 0; |
Prafulla Wadaskar | fabf3cf | 2009-08-19 17:36:46 +0530 | [diff] [blame] | 820 | return ((params->dflag && (params->fflag || params->lflag)) || |
| 821 | (params->fflag && (params->dflag || params->lflag)) || |
| 822 | (params->lflag && (params->dflag || params->fflag))); |
| 823 | } |
| 824 | |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 825 | U_BOOT_IMAGE_TYPE( |
| 826 | fitimage, |
| 827 | "FIT Image support", |
| 828 | sizeof(image_header_t), |
| 829 | (void *)&header, |
| 830 | fit_check_params, |
| 831 | fit_verify_header, |
| 832 | fit_print_contents, |
| 833 | NULL, |
Guilherme Maciel Ferreira | 3c46bcd | 2015-01-15 02:54:42 -0200 | [diff] [blame] | 834 | fit_extract_contents, |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 835 | fit_check_image_types, |
| 836 | fit_handle_file, |
| 837 | NULL /* FIT images use DTB header */ |
| 838 | ); |