1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 Semihalf
5 * (C) Copyright 2000-2004
6 * DENX Software Engineering
7 * Wolfgang Denk, wd@denx.de
9 * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
10 * FIT image specific code abstracted from mkimage.c
11 * some functions added to address abstraction
13 * All rights reserved.
16 #include "imagetool.h"
17 #include "fit_common.h"
22 #include <u-boot/crc.h>
24 static image_header_t header;
26 static int fit_add_file_data(struct image_tool_params *params, size_t size_inc,
30 void *dest_blob = NULL;
31 off_t destfd_size = 0;
36 tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true);
40 if (params->keydest) {
41 struct stat dest_sbuf;
43 destfd = mmap_fdt(params->cmdname, params->keydest, size_inc,
44 &dest_blob, &dest_sbuf, false);
49 destfd_size = dest_sbuf.st_size;
52 /* for first image creation, add a timestamp at offset 0 i.e., root */
53 if (params->datafile) {
54 time_t time = imagetool_get_source_date(params->cmdname,
56 ret = fit_set_timestamp(ptr, 0, time);
60 ret = fit_add_verification_data(params->keydir, dest_blob, ptr,
68 munmap(dest_blob, destfd_size);
73 munmap(ptr, sbuf.st_size);
80 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
82 static int fit_calc_size(struct image_tool_params *params)
84 struct content_info *cont;
87 size = imagetool_get_filesize(params, params->datafile);
92 if (params->fit_ramdisk) {
93 size = imagetool_get_filesize(params, params->fit_ramdisk);
99 for (cont = params->content_head; cont; cont = cont->next) {
100 size = imagetool_get_filesize(params, cont->fname);
104 /* Add space for properties */
105 total_size += size + 300;
108 /* Add plenty of space for headers, properties, nodes, etc. */
114 static int fdt_property_file(struct image_tool_params *params,
115 void *fdt, const char *name, const char *fname)
122 fd = open(fname, O_RDWR | O_BINARY);
124 fprintf(stderr, "%s: Can't open %s: %s\n",
125 params->cmdname, fname, strerror(errno));
129 if (fstat(fd, &sbuf) < 0) {
130 fprintf(stderr, "%s: Can't stat %s: %s\n",
131 params->cmdname, fname, strerror(errno));
135 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
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));
152 static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
158 vsnprintf(str, sizeof(str), fmt, ptr);
160 return fdt_property_string(fdt, name, str);
163 static void get_basename(char *str, int size, const char *fname)
165 const char *p, *start, *end;
169 * Use the base name as the 'name' field. So for example:
171 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
172 * becomes "sun7i-a20-bananapro"
174 p = strrchr(fname, '/');
175 start = p ? p + 1 : fname;
176 p = strrchr(fname, '.');
177 end = p ? p : fname + strlen(fname);
181 memcpy(str, start, len);
186 * fit_write_images() - Write out a list of images to the FIT
188 * We always include the main image (params->datafile). If there are device
189 * tree files, we include an fdt- node for each of those too.
191 static int fit_write_images(struct image_tool_params *params, char *fdt)
193 struct content_info *cont;
194 const char *typename;
199 fdt_begin_node(fdt, "images");
201 /* First the main image */
202 typename = genimg_get_type_short_name(params->fit_image_type);
203 snprintf(str, sizeof(str), "%s-1", typename);
204 fdt_begin_node(fdt, str);
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,
208 genimg_get_arch_short_name(params->arch));
209 fdt_property_string(fdt, FIT_OS_PROP,
210 genimg_get_os_short_name(params->os));
211 fdt_property_string(fdt, FIT_COMP_PROP,
212 genimg_get_comp_short_name(params->comp));
213 fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr);
214 fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep);
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.
220 ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile);
225 /* Now the device tree files if available */
227 for (cont = params->content_head; cont; cont = cont->next) {
228 if (cont->type != IH_TYPE_FLATDT)
230 snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
231 fdt_begin_node(fdt, str);
233 get_basename(str, sizeof(str), cont->fname);
234 fdt_property_string(fdt, FIT_DESC_PROP, str);
235 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
239 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
240 fdt_property_string(fdt, FIT_ARCH_PROP,
241 genimg_get_arch_short_name(params->arch));
242 fdt_property_string(fdt, FIT_COMP_PROP,
243 genimg_get_comp_short_name(IH_COMP_NONE));
247 /* And a ramdisk file if available */
248 if (params->fit_ramdisk) {
249 fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
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));
255 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
256 params->fit_ramdisk);
269 * fit_write_configs() - Write out a list of configurations to the FIT
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
275 * Otherwise we just create a configuration with the main image in it.
277 static void fit_write_configs(struct image_tool_params *params, char *fdt)
279 struct content_info *cont;
280 const char *typename;
284 fdt_begin_node(fdt, "configurations");
285 fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
288 for (cont = params->content_head; cont; cont = cont->next) {
289 if (cont->type != IH_TYPE_FLATDT)
291 typename = genimg_get_type_short_name(cont->type);
292 snprintf(str, sizeof(str), "conf-%d", ++upto);
293 fdt_begin_node(fdt, str);
295 get_basename(str, sizeof(str), cont->fname);
296 fdt_property_string(fdt, FIT_DESC_PROP, str);
298 typename = genimg_get_type_short_name(params->fit_image_type);
299 snprintf(str, sizeof(str), "%s-1", typename);
300 fdt_property_string(fdt, typename, str);
302 if (params->fit_ramdisk)
303 fdt_property_string(fdt, FIT_RAMDISK_PROP,
304 FIT_RAMDISK_PROP "-1");
306 snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
307 fdt_property_string(fdt, FIT_FDT_PROP, str);
312 fdt_begin_node(fdt, "conf-1");
313 typename = genimg_get_type_short_name(params->fit_image_type);
314 snprintf(str, sizeof(str), "%s-1", typename);
315 fdt_property_string(fdt, typename, str);
317 if (params->fit_ramdisk)
318 fdt_property_string(fdt, FIT_RAMDISK_PROP,
319 FIT_RAMDISK_PROP "-1");
327 static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
331 ret = fdt_create(fdt, size);
334 fdt_finish_reservemap(fdt);
335 fdt_begin_node(fdt, "");
336 fdt_property_strf(fdt, FIT_DESC_PROP,
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);
344 fit_write_configs(params, fdt);
346 ret = fdt_finish(fdt);
350 return fdt_totalsize(fdt);
353 static int fit_build(struct image_tool_params *params, const char *fname)
360 size = fit_calc_size(params);
365 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
366 params->cmdname, size);
369 ret = fit_build_fdt(params, buf, size);
371 fprintf(stderr, "%s: Failed to build FIT image\n",
376 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
378 fprintf(stderr, "%s: Can't open %s: %s\n",
379 params->cmdname, fname, strerror(errno));
382 ret = write(fd, buf, size);
384 fprintf(stderr, "%s: Can't write %s: %s\n",
385 params->cmdname, fname, strerror(errno));
400 * fit_extract_data() - Move all data outside the FIT
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.
407 * This function cannot cope with FITs with 'data-offset' properties. All
408 * data must be in 'data' properties on entry.
410 static int fit_extract_data(struct image_tool_params *params, const char *fname)
414 int fit_size, new_size;
422 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false);
425 fit_size = fdt_totalsize(fdt);
427 /* Allocate space to hold the image data we will extract */
428 buf = malloc(fit_size);
435 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
437 debug("%s: Cannot find /images node: %d\n", __func__, images);
442 for (node = fdt_first_subnode(fdt, images);
444 node = fdt_next_subnode(fdt, node)) {
448 data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
451 memcpy(buf + buf_ptr, data, len);
452 debug("Extracting data size %x\n", len);
454 ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
459 if (params->external_offset > 0) {
460 /* An external offset positions the data absolutely. */
461 fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
462 params->external_offset + buf_ptr);
464 fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
467 fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
469 buf_ptr += (len + 3) & ~3;
472 /* Pack the FDT and place the data after it */
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);
481 if (ftruncate(fd, new_size)) {
482 debug("%s: Failed to truncate file: %s\n", __func__,
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);
496 new_size = params->external_offset;
498 if (lseek(fd, new_size, SEEK_SET) < 0) {
499 debug("%s: Failed to seek to end of file: %s\n", __func__,
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));
515 munmap(fdt, sbuf.st_size);
523 static int fit_import_data(struct image_tool_params *params, const char *fname)
526 int fit_size, new_size, size, data_base;
533 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false);
536 fit_size = fdt_totalsize(old_fdt);
537 data_base = (fit_size + 3) & ~3;
539 /* Allocate space to hold the new FIT */
540 size = sbuf.st_size + 16384;
543 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
548 ret = fdt_open_into(old_fdt, fdt, size);
550 debug("%s: Failed to expand FIT: %s\n", __func__,
551 fdt_strerror(errno));
556 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
558 debug("%s: Cannot find /images node: %d\n", __func__, images);
563 for (node = fdt_first_subnode(fdt, images);
565 node = fdt_next_subnode(fdt, node)) {
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)
573 debug("Importing data size %x\n", len);
575 ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
578 debug("%s: Failed to write property: %s\n", __func__,
585 /* Close the old fd so we can re-use it. */
588 /* Pack the FDT and place the data after it */
591 new_size = fdt_totalsize(fdt);
592 debug("Size expanded from %x to %x\n", fit_size, new_size);
594 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
596 fprintf(stderr, "%s: Can't open %s: %s\n",
597 params->cmdname, fname, strerror(errno));
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));
613 munmap(old_fdt, sbuf.st_size);
619 * fit_handle_file - main FIT file processing function
621 * fit_handle_file() runs dtc to convert .its to .itb, includes
622 * binary data, updates timestamp property and calculates hashes.
624 * datafile - .its file
625 * imagefile - .itb file
628 * only on success, otherwise calls exit (EXIT_FAILURE);
630 static int fit_handle_file(struct image_tool_params *params)
632 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
633 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
637 /* Flattened Image Tree (FIT) format handling */
638 debug ("FIT format handling\n");
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);
648 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
650 /* We either compile the source file, or use the existing FIT image */
651 if (params->auto_its) {
652 if (fit_build(params, tmpfile)) {
653 fprintf(stderr, "%s: failed to build FIT\n",
658 } else if (params->datafile) {
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);
662 debug("Trying to execute \"%s\"\n", cmd);
664 snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
665 params->imagefile, tmpfile);
667 if (*cmd && system(cmd) == -1) {
668 fprintf (stderr, "%s: system(%s) failed: %s\n",
669 params->cmdname, cmd, strerror(errno));
673 /* Move the data so it is internal to the FIT, if needed */
674 ret = fit_import_data(params, tmpfile);
679 * Set hashes for images in the blob. Unfortunately we may need more
680 * space in either FDT, so keep trying until we succeed.
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.
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)
695 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
696 params->cmdname, ret);
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);
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,
712 unlink (params->imagefile);
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
729 * zero in case of success or a negative value if fail.
731 static int fit_image_extract(
734 const char *file_name)
736 const void *file_data;
737 size_t file_size = 0;
739 /* get the "data" property of component at offset "image_noffset" */
740 fit_image_get_data(fit, image_noffset, &file_data, &file_size);
742 /* save the "file_data" into the file specified by "file_name" */
743 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
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
752 * zero in case of success or a negative value if fail.
754 static int fit_extract_contents(void *ptr, struct image_tool_params *params)
759 const void *fit = ptr;
763 /* Indent string is defined in header image.h */
764 p = IMAGE_INDENT_STRING;
766 if (!fit_check_format(fit)) {
767 printf("Bad FIT image format\n");
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));
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);
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)) {
793 * Direct child node of the images parent node,
794 * i.e. component image node.
796 if (params->pflag == count) {
797 printf("Extracted:\n%s Image %u (%s)\n", p,
798 count, fit_get_name(fit, noffset, NULL));
800 fit_image_print(fit, noffset, p);
802 return fit_image_extract(fit, noffset,
813 static int fit_check_params(struct image_tool_params *params)
815 if (params->auto_its)
817 return ((params->dflag && (params->fflag || params->lflag)) ||
818 (params->fflag && (params->dflag || params->lflag)) ||
819 (params->lflag && (params->dflag || params->fflag)));
825 sizeof(image_header_t),
831 fit_extract_contents,
832 fit_check_image_types,
834 NULL /* FIT images use DTB header */