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