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