2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2004
5 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
8 * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
9 * FIT image specific code abstracted from mkimage.c
10 * some functions added to address abstraction
12 * All rights reserved.
14 * SPDX-License-Identifier: GPL-2.0+
17 #include "imagetool.h"
18 #include "fit_common.h"
23 #include <u-boot/crc.h>
25 static image_header_t header;
27 static int fit_add_file_data(struct image_tool_params *params, size_t size_inc,
31 void *dest_blob = NULL;
32 off_t destfd_size = 0;
37 tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true);
41 if (params->keydest) {
42 struct stat dest_sbuf;
44 destfd = mmap_fdt(params->cmdname, params->keydest, size_inc,
45 &dest_blob, &dest_sbuf, false);
50 destfd_size = dest_sbuf.st_size;
53 /* for first image creation, add a timestamp at offset 0 i.e., root */
54 if (params->datafile) {
55 time_t time = imagetool_get_source_date(params, sbuf.st_mtime);
56 ret = fit_set_timestamp(ptr, 0, time);
60 ret = fit_add_verification_data(params->keydir, dest_blob, ptr,
62 params->require_keys);
66 munmap(dest_blob, destfd_size);
71 munmap(ptr, sbuf.st_size);
78 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
80 static int fit_calc_size(struct image_tool_params *params)
82 struct content_info *cont;
85 size = imagetool_get_filesize(params, params->datafile);
90 for (cont = params->content_head; cont; cont = cont->next) {
91 size = imagetool_get_filesize(params, cont->fname);
95 /* Add space for properties */
96 total_size += size + 300;
99 /* Add plenty of space for headers, properties, nodes, etc. */
105 static int fdt_property_file(struct image_tool_params *params,
106 void *fdt, const char *name, const char *fname)
113 fd = open(fname, O_RDWR | O_BINARY);
115 fprintf(stderr, "%s: Can't open %s: %s\n",
116 params->cmdname, fname, strerror(errno));
120 if (fstat(fd, &sbuf) < 0) {
121 fprintf(stderr, "%s: Can't stat %s: %s\n",
122 params->cmdname, fname, strerror(errno));
126 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
129 ret = read(fd, ptr, sbuf.st_size);
130 if (ret != sbuf.st_size) {
131 fprintf(stderr, "%s: Can't read %s: %s\n",
132 params->cmdname, fname, strerror(errno));
143 static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
149 vsnprintf(str, sizeof(str), fmt, ptr);
151 return fdt_property_string(fdt, name, str);
154 static void get_basename(char *str, int size, const char *fname)
156 const char *p, *start, *end;
160 * Use the base name as the 'name' field. So for example:
162 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
163 * becomes "sun7i-a20-bananapro"
165 p = strrchr(fname, '/');
166 start = p ? p + 1 : fname;
167 p = strrchr(fname, '.');
168 end = p ? p : fname + strlen(fname);
172 memcpy(str, start, len);
177 * fit_write_images() - Write out a list of images to the FIT
179 * We always include the main image (params->datafile). If there are device
180 * tree files, we include an fdt@ node for each of those too.
182 static int fit_write_images(struct image_tool_params *params, char *fdt)
184 struct content_info *cont;
185 const char *typename;
190 fdt_begin_node(fdt, "images");
192 /* First the main image */
193 typename = genimg_get_type_short_name(params->fit_image_type);
194 snprintf(str, sizeof(str), "%s@1", typename);
195 fdt_begin_node(fdt, str);
196 fdt_property_string(fdt, "description", params->imagename);
197 fdt_property_string(fdt, "type", typename);
198 fdt_property_string(fdt, "arch",
199 genimg_get_arch_short_name(params->arch));
200 fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os));
201 fdt_property_string(fdt, "compression",
202 genimg_get_comp_short_name(params->comp));
203 fdt_property_u32(fdt, "load", params->addr);
204 fdt_property_u32(fdt, "entry", params->ep);
207 * Put data last since it is large. SPL may only load the first part
208 * of the DT, so this way it can access all the above fields.
210 ret = fdt_property_file(params, fdt, "data", params->datafile);
215 /* Now the device tree files if available */
217 for (cont = params->content_head; cont; cont = cont->next) {
218 if (cont->type != IH_TYPE_FLATDT)
220 snprintf(str, sizeof(str), "%s@%d", FIT_FDT_PROP, ++upto);
221 fdt_begin_node(fdt, str);
223 get_basename(str, sizeof(str), cont->fname);
224 fdt_property_string(fdt, "description", str);
225 ret = fdt_property_file(params, fdt, "data", cont->fname);
228 fdt_property_string(fdt, "type", typename);
229 fdt_property_string(fdt, "arch",
230 genimg_get_arch_short_name(params->arch));
231 fdt_property_string(fdt, "compression",
232 genimg_get_comp_short_name(IH_COMP_NONE));
242 * fit_write_configs() - Write out a list of configurations to the FIT
244 * If there are device tree files, we include a configuration for each, which
245 * selects the main image (params->datafile) and its corresponding device
248 * Otherwise we just create a configuration with the main image in it.
250 static void fit_write_configs(struct image_tool_params *params, char *fdt)
252 struct content_info *cont;
253 const char *typename;
257 fdt_begin_node(fdt, "configurations");
258 fdt_property_string(fdt, "default", "conf@1");
261 for (cont = params->content_head; cont; cont = cont->next) {
262 if (cont->type != IH_TYPE_FLATDT)
264 typename = genimg_get_type_short_name(cont->type);
265 snprintf(str, sizeof(str), "conf@%d", ++upto);
266 fdt_begin_node(fdt, str);
268 get_basename(str, sizeof(str), cont->fname);
269 fdt_property_string(fdt, "description", str);
271 typename = genimg_get_type_short_name(params->fit_image_type);
272 snprintf(str, sizeof(str), "%s@1", typename);
273 fdt_property_string(fdt, typename, str);
275 snprintf(str, sizeof(str), FIT_FDT_PROP "@%d", upto);
276 fdt_property_string(fdt, FIT_FDT_PROP, str);
280 fdt_begin_node(fdt, "conf@1");
281 typename = genimg_get_type_short_name(params->fit_image_type);
282 snprintf(str, sizeof(str), "%s@1", typename);
283 fdt_property_string(fdt, typename, str);
290 static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
294 ret = fdt_create(fdt, size);
297 fdt_finish_reservemap(fdt);
298 fdt_begin_node(fdt, "");
299 fdt_property_strf(fdt, "description",
300 "%s image with one or more FDT blobs",
301 genimg_get_type_name(params->fit_image_type));
302 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
303 fdt_property_u32(fdt, "#address-cells", 1);
304 ret = fit_write_images(params, fdt);
307 fit_write_configs(params, fdt);
309 ret = fdt_finish(fdt);
313 return fdt_totalsize(fdt);
316 static int fit_build(struct image_tool_params *params, const char *fname)
323 size = fit_calc_size(params);
328 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
329 params->cmdname, size);
332 ret = fit_build_fdt(params, buf, size);
334 fprintf(stderr, "%s: Failed to build FIT image\n",
339 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
341 fprintf(stderr, "%s: Can't open %s: %s\n",
342 params->cmdname, fname, strerror(errno));
345 ret = write(fd, buf, size);
347 fprintf(stderr, "%s: Can't write %s: %s\n",
348 params->cmdname, fname, strerror(errno));
363 * fit_extract_data() - Move all data outside the FIT
365 * This takes a normal FIT file and removes all the 'data' properties from it.
366 * The data is placed in an area after the FIT so that it can be accessed
367 * using an offset into that area. The 'data' properties turn into
368 * 'data-offset' properties.
370 * This function cannot cope with FITs with 'data-offset' properties. All
371 * data must be in 'data' properties on entry.
373 static int fit_extract_data(struct image_tool_params *params, const char *fname)
377 int fit_size, new_size;
385 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false);
388 fit_size = fdt_totalsize(fdt);
390 /* Allocate space to hold the image data we will extract */
391 buf = malloc(fit_size);
398 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
400 debug("%s: Cannot find /images node: %d\n", __func__, images);
405 for (node = fdt_first_subnode(fdt, images);
407 node = fdt_next_subnode(fdt, node)) {
411 data = fdt_getprop(fdt, node, "data", &len);
414 memcpy(buf + buf_ptr, data, len);
415 debug("Extracting data size %x\n", len);
417 ret = fdt_delprop(fdt, node, "data");
422 if (params->external_offset > 0) {
423 /* An external offset positions the data absolutely. */
424 fdt_setprop_u32(fdt, node, "data-position",
425 params->external_offset + buf_ptr);
427 fdt_setprop_u32(fdt, node, "data-offset", buf_ptr);
429 fdt_setprop_u32(fdt, node, "data-size", len);
431 buf_ptr += (len + 3) & ~3;
434 /* Pack the FDT and place the data after it */
437 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
438 debug("External data size %x\n", buf_ptr);
439 new_size = fdt_totalsize(fdt);
440 new_size = (new_size + 3) & ~3;
441 munmap(fdt, sbuf.st_size);
443 if (ftruncate(fd, new_size)) {
444 debug("%s: Failed to truncate file: %s\n", __func__,
450 /* Check if an offset for the external data was set. */
451 if (params->external_offset > 0) {
452 if (params->external_offset < new_size) {
453 debug("External offset %x overlaps FIT length %x",
454 params->external_offset, new_size);
458 new_size = params->external_offset;
460 if (lseek(fd, new_size, SEEK_SET) < 0) {
461 debug("%s: Failed to seek to end of file: %s\n", __func__,
466 if (write(fd, buf, buf_ptr) != buf_ptr) {
467 debug("%s: Failed to write external data to file %s\n",
468 __func__, strerror(errno));
476 munmap(fdt, sbuf.st_size);
484 static int fit_import_data(struct image_tool_params *params, const char *fname)
487 int fit_size, new_size, size, data_base;
494 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false);
497 fit_size = fdt_totalsize(old_fdt);
498 data_base = (fit_size + 3) & ~3;
500 /* Allocate space to hold the new FIT */
501 size = sbuf.st_size + 16384;
504 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
509 ret = fdt_open_into(old_fdt, fdt, size);
511 debug("%s: Failed to expand FIT: %s\n", __func__,
512 fdt_strerror(errno));
517 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
519 debug("%s: Cannot find /images node: %d\n", __func__, images);
524 for (node = fdt_first_subnode(fdt, images);
526 node = fdt_next_subnode(fdt, node)) {
530 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
531 len = fdtdec_get_int(fdt, node, "data-size", -1);
532 if (buf_ptr == -1 || len == -1)
534 debug("Importing data size %x\n", len);
536 ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
539 debug("%s: Failed to write property: %s\n", __func__,
546 munmap(old_fdt, sbuf.st_size);
549 /* Pack the FDT and place the data after it */
552 new_size = fdt_totalsize(fdt);
553 debug("Size expanded from %x to %x\n", fit_size, new_size);
555 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
557 fprintf(stderr, "%s: Can't open %s: %s\n",
558 params->cmdname, fname, strerror(errno));
562 if (write(fd, fdt, new_size) != new_size) {
563 debug("%s: Failed to write external data to file %s\n",
564 __func__, strerror(errno));
578 * fit_handle_file - main FIT file processing function
580 * fit_handle_file() runs dtc to convert .its to .itb, includes
581 * binary data, updates timestamp property and calculates hashes.
583 * datafile - .its file
584 * imagefile - .itb file
587 * only on success, otherwise calls exit (EXIT_FAILURE);
589 static int fit_handle_file(struct image_tool_params *params)
591 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
592 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
596 /* Flattened Image Tree (FIT) format handling */
597 debug ("FIT format handling\n");
599 /* call dtc to include binary properties into the tmp file */
600 if (strlen (params->imagefile) +
601 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
602 fprintf (stderr, "%s: Image file name (%s) too long, "
603 "can't create tmpfile",
604 params->imagefile, params->cmdname);
605 return (EXIT_FAILURE);
607 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
609 /* We either compile the source file, or use the existing FIT image */
610 if (params->auto_its) {
611 if (fit_build(params, tmpfile)) {
612 fprintf(stderr, "%s: failed to build FIT\n",
617 } else if (params->datafile) {
618 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
619 snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
620 MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
621 debug("Trying to execute \"%s\"\n", cmd);
623 snprintf(cmd, sizeof(cmd), "cp %s %s",
624 params->imagefile, tmpfile);
626 if (*cmd && system(cmd) == -1) {
627 fprintf (stderr, "%s: system(%s) failed: %s\n",
628 params->cmdname, cmd, strerror(errno));
632 /* Move the data so it is internal to the FIT, if needed */
633 ret = fit_import_data(params, tmpfile);
638 * Set hashes for images in the blob. Unfortunately we may need more
639 * space in either FDT, so keep trying until we succeed.
641 * Note: this is pretty inefficient for signing, since we must
642 * calculate the signature every time. It would be better to calculate
643 * all the data and then store it in a separate step. However, this
644 * would be considerably more complex to implement. Generally a few
645 * steps of this loop is enough to sign with several keys.
647 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
648 ret = fit_add_file_data(params, size_inc, tmpfile);
649 if (!ret || ret != -ENOSPC)
654 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
655 params->cmdname, ret);
659 /* Move the data so it is external to the FIT, if requested */
660 if (params->external_data) {
661 ret = fit_extract_data(params, tmpfile);
666 if (rename (tmpfile, params->imagefile) == -1) {
667 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
668 params->cmdname, tmpfile, params->imagefile,
671 unlink (params->imagefile);
682 * fit_image_extract - extract a FIT component image
683 * @fit: pointer to the FIT format image header
684 * @image_noffset: offset of the component image node
685 * @file_name: name of the file to store the FIT sub-image
688 * zero in case of success or a negative value if fail.
690 static int fit_image_extract(
693 const char *file_name)
695 const void *file_data;
696 size_t file_size = 0;
698 /* get the "data" property of component at offset "image_noffset" */
699 fit_image_get_data(fit, image_noffset, &file_data, &file_size);
701 /* save the "file_data" into the file specified by "file_name" */
702 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
706 * fit_extract_contents - retrieve a sub-image component from the FIT image
707 * @ptr: pointer to the FIT format image header
708 * @params: command line parameters
711 * zero in case of success or a negative value if fail.
713 static int fit_extract_contents(void *ptr, struct image_tool_params *params)
718 const void *fit = ptr;
722 /* Indent string is defined in header image.h */
723 p = IMAGE_INDENT_STRING;
725 if (!fit_check_format(fit)) {
726 printf("Bad FIT image format\n");
730 /* Find images parent node offset */
731 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
732 if (images_noffset < 0) {
733 printf("Can't find images parent node '%s' (%s)\n",
734 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
738 /* Avoid any overrun */
739 count = fit_get_subimage_count(fit, images_noffset);
740 if ((params->pflag < 0) || (count <= params->pflag)) {
741 printf("No such component at '%d'\n", params->pflag);
745 /* Process its subnodes, extract the desired component from image */
746 for (ndepth = 0, count = 0,
747 noffset = fdt_next_node(fit, images_noffset, &ndepth);
748 (noffset >= 0) && (ndepth > 0);
749 noffset = fdt_next_node(fit, noffset, &ndepth)) {
752 * Direct child node of the images parent node,
753 * i.e. component image node.
755 if (params->pflag == count) {
756 printf("Extracted:\n%s Image %u (%s)\n", p,
757 count, fit_get_name(fit, noffset, NULL));
759 fit_image_print(fit, noffset, p);
761 return fit_image_extract(fit, noffset,
772 static int fit_check_params(struct image_tool_params *params)
774 if (params->auto_its)
776 return ((params->dflag && (params->fflag || params->lflag)) ||
777 (params->fflag && (params->dflag || params->lflag)) ||
778 (params->lflag && (params->dflag || params->fflag)));
784 sizeof(image_header_t),
790 fit_extract_contents,
791 fit_check_image_types,
793 NULL /* FIT images use DTB header */