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