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