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,
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,
51 destfd_size = dest_sbuf.st_size;
54 /* for first image creation, add a timestamp at offset 0 i.e., root */
55 if (params->datafile) {
56 time_t time = imagetool_get_source_date(params->cmdname,
58 ret = fit_set_timestamp(ptr, 0, time);
62 ret = fit_cipher_data(params->keydir, dest_blob, ptr,
70 ret = fit_add_verification_data(params->keydir, dest_blob, ptr,
78 munmap(dest_blob, destfd_size);
83 munmap(ptr, sbuf.st_size);
89 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
91 static int fit_calc_size(struct image_tool_params *params)
93 struct content_info *cont;
96 size = imagetool_get_filesize(params, params->datafile);
101 if (params->fit_ramdisk) {
102 size = imagetool_get_filesize(params, params->fit_ramdisk);
108 for (cont = params->content_head; cont; cont = cont->next) {
109 size = imagetool_get_filesize(params, cont->fname);
113 /* Add space for properties */
114 total_size += size + 300;
117 /* Add plenty of space for headers, properties, nodes, etc. */
123 static int fdt_property_file(struct image_tool_params *params,
124 void *fdt, const char *name, const char *fname)
131 fd = open(fname, O_RDWR | O_BINARY);
133 fprintf(stderr, "%s: Can't open %s: %s\n",
134 params->cmdname, fname, strerror(errno));
138 if (fstat(fd, &sbuf) < 0) {
139 fprintf(stderr, "%s: Can't stat %s: %s\n",
140 params->cmdname, fname, strerror(errno));
144 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
147 ret = read(fd, ptr, sbuf.st_size);
148 if (ret != sbuf.st_size) {
149 fprintf(stderr, "%s: Can't read %s: %s\n",
150 params->cmdname, fname, strerror(errno));
161 static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
167 vsnprintf(str, sizeof(str), fmt, ptr);
169 return fdt_property_string(fdt, name, str);
172 static void get_basename(char *str, int size, const char *fname)
174 const char *p, *start, *end;
178 * Use the base name as the 'name' field. So for example:
180 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
181 * becomes "sun7i-a20-bananapro"
183 p = strrchr(fname, '/');
184 start = p ? p + 1 : fname;
185 p = strrchr(fname, '.');
186 end = p ? p : fname + strlen(fname);
190 memcpy(str, start, len);
195 * fit_write_images() - Write out a list of images to the FIT
197 * We always include the main image (params->datafile). If there are device
198 * tree files, we include an fdt- node for each of those too.
200 static int fit_write_images(struct image_tool_params *params, char *fdt)
202 struct content_info *cont;
203 const char *typename;
208 fdt_begin_node(fdt, "images");
210 /* First the main image */
211 typename = genimg_get_type_short_name(params->fit_image_type);
212 snprintf(str, sizeof(str), "%s-1", typename);
213 fdt_begin_node(fdt, str);
214 fdt_property_string(fdt, FIT_DESC_PROP, params->imagename);
215 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
216 fdt_property_string(fdt, FIT_ARCH_PROP,
217 genimg_get_arch_short_name(params->arch));
218 fdt_property_string(fdt, FIT_OS_PROP,
219 genimg_get_os_short_name(params->os));
220 fdt_property_string(fdt, FIT_COMP_PROP,
221 genimg_get_comp_short_name(params->comp));
222 fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr);
223 fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep);
226 * Put data last since it is large. SPL may only load the first part
227 * of the DT, so this way it can access all the above fields.
229 ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile);
234 /* Now the device tree files if available */
236 for (cont = params->content_head; cont; cont = cont->next) {
237 if (cont->type != IH_TYPE_FLATDT)
239 typename = genimg_get_type_short_name(cont->type);
240 snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
241 fdt_begin_node(fdt, str);
243 get_basename(str, sizeof(str), cont->fname);
244 fdt_property_string(fdt, FIT_DESC_PROP, str);
245 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
249 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
250 fdt_property_string(fdt, FIT_ARCH_PROP,
251 genimg_get_arch_short_name(params->arch));
252 fdt_property_string(fdt, FIT_COMP_PROP,
253 genimg_get_comp_short_name(IH_COMP_NONE));
257 /* And a ramdisk file if available */
258 if (params->fit_ramdisk) {
259 fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
261 fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP);
262 fdt_property_string(fdt, FIT_OS_PROP,
263 genimg_get_os_short_name(params->os));
264 fdt_property_string(fdt, FIT_ARCH_PROP,
265 genimg_get_arch_short_name(params->arch));
267 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
268 params->fit_ramdisk);
281 * fit_write_configs() - Write out a list of configurations to the FIT
283 * If there are device tree files, we include a configuration for each, which
284 * selects the main image (params->datafile) and its corresponding device
287 * Otherwise we just create a configuration with the main image in it.
289 static void fit_write_configs(struct image_tool_params *params, char *fdt)
291 struct content_info *cont;
292 const char *typename;
296 fdt_begin_node(fdt, "configurations");
297 fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
300 for (cont = params->content_head; cont; cont = cont->next) {
301 if (cont->type != IH_TYPE_FLATDT)
303 typename = genimg_get_type_short_name(cont->type);
304 snprintf(str, sizeof(str), "conf-%d", ++upto);
305 fdt_begin_node(fdt, str);
307 get_basename(str, sizeof(str), cont->fname);
308 fdt_property_string(fdt, FIT_DESC_PROP, str);
310 typename = genimg_get_type_short_name(params->fit_image_type);
311 snprintf(str, sizeof(str), "%s-1", typename);
312 fdt_property_string(fdt, typename, str);
313 fdt_property_string(fdt, FIT_LOADABLE_PROP, str);
315 if (params->fit_ramdisk)
316 fdt_property_string(fdt, FIT_RAMDISK_PROP,
317 FIT_RAMDISK_PROP "-1");
319 snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
320 fdt_property_string(fdt, FIT_FDT_PROP, str);
325 fdt_begin_node(fdt, "conf-1");
326 typename = genimg_get_type_short_name(params->fit_image_type);
327 snprintf(str, sizeof(str), "%s-1", typename);
328 fdt_property_string(fdt, typename, str);
330 if (params->fit_ramdisk)
331 fdt_property_string(fdt, FIT_RAMDISK_PROP,
332 FIT_RAMDISK_PROP "-1");
340 static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
344 ret = fdt_create(fdt, size);
347 fdt_finish_reservemap(fdt);
348 fdt_begin_node(fdt, "");
349 fdt_property_strf(fdt, FIT_DESC_PROP,
350 "%s image with one or more FDT blobs",
351 genimg_get_type_name(params->fit_image_type));
352 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
353 fdt_property_u32(fdt, "#address-cells", 1);
354 ret = fit_write_images(params, fdt);
357 fit_write_configs(params, fdt);
359 ret = fdt_finish(fdt);
363 return fdt_totalsize(fdt);
366 static int fit_build(struct image_tool_params *params, const char *fname)
373 size = fit_calc_size(params);
378 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
379 params->cmdname, size);
382 ret = fit_build_fdt(params, buf, size);
384 fprintf(stderr, "%s: Failed to build FIT image\n",
389 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
391 fprintf(stderr, "%s: Can't open %s: %s\n",
392 params->cmdname, fname, strerror(errno));
395 ret = write(fd, buf, size);
397 fprintf(stderr, "%s: Can't write %s: %s\n",
398 params->cmdname, fname, strerror(errno));
413 * fit_extract_data() - Move all data outside the FIT
415 * This takes a normal FIT file and removes all the 'data' properties from it.
416 * The data is placed in an area after the FIT so that it can be accessed
417 * using an offset into that area. The 'data' properties turn into
418 * 'data-offset' properties.
420 * This function cannot cope with FITs with 'data-offset' properties. All
421 * data must be in 'data' properties on entry.
423 static int fit_extract_data(struct image_tool_params *params, const char *fname)
427 int fit_size, new_size;
437 align_size = params->bl_len ? params->bl_len : 4;
438 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false);
441 fit_size = fdt_totalsize(fdt);
443 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
445 debug("%s: Cannot find /images node: %d\n", __func__, images);
449 image_number = fdtdec_get_child_count(fdt, images);
452 * Allocate space to hold the image data we will extract,
453 * extral space allocate for image alignment to prevent overflow.
455 buf = malloc(fit_size + (align_size * image_number));
462 for (node = fdt_first_subnode(fdt, images);
464 node = fdt_next_subnode(fdt, node)) {
468 data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
471 memcpy(buf + buf_ptr, data, len);
472 debug("Extracting data size %x\n", len);
474 ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
479 if (params->external_offset > 0) {
480 /* An external offset positions the data absolutely. */
481 fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
482 params->external_offset + buf_ptr);
484 fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
487 fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
488 buf_ptr += ALIGN(len, align_size);
491 /* Pack the FDT and place the data after it */
494 new_size = fdt_totalsize(fdt);
495 new_size = ALIGN(new_size, align_size);
496 fdt_set_totalsize(fdt, new_size);
497 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
498 debug("External data size %x\n", buf_ptr);
499 munmap(fdt, sbuf.st_size);
501 if (ftruncate(fd, new_size)) {
502 debug("%s: Failed to truncate file: %s\n", __func__,
508 /* Check if an offset for the external data was set. */
509 if (params->external_offset > 0) {
510 if (params->external_offset < new_size) {
511 debug("External offset %x overlaps FIT length %x",
512 params->external_offset, new_size);
516 new_size = params->external_offset;
518 if (lseek(fd, new_size, SEEK_SET) < 0) {
519 debug("%s: Failed to seek to end of file: %s\n", __func__,
524 if (write(fd, buf, buf_ptr) != buf_ptr) {
525 debug("%s: Failed to write external data to file %s\n",
526 __func__, strerror(errno));
535 munmap(fdt, sbuf.st_size);
543 static int fit_import_data(struct image_tool_params *params, const char *fname)
546 int fit_size, new_size, size, data_base;
553 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false);
556 fit_size = fdt_totalsize(old_fdt);
557 data_base = ALIGN(fit_size, 4);
559 /* Allocate space to hold the new FIT */
560 size = sbuf.st_size + 16384;
563 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
568 ret = fdt_open_into(old_fdt, fdt, size);
570 debug("%s: Failed to expand FIT: %s\n", __func__,
571 fdt_strerror(errno));
576 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
578 debug("%s: Cannot find /images node: %d\n", __func__, images);
583 for (node = fdt_first_subnode(fdt, images);
585 node = fdt_next_subnode(fdt, node)) {
589 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
590 len = fdtdec_get_int(fdt, node, "data-size", -1);
591 if (buf_ptr == -1 || len == -1)
593 debug("Importing data size %x\n", len);
595 ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
598 debug("%s: Failed to write property: %s\n", __func__,
605 munmap(old_fdt, sbuf.st_size);
607 /* Close the old fd so we can re-use it. */
610 /* Pack the FDT and place the data after it */
613 new_size = fdt_totalsize(fdt);
614 debug("Size expanded from %x to %x\n", fit_size, new_size);
616 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
618 fprintf(stderr, "%s: Can't open %s: %s\n",
619 params->cmdname, fname, strerror(errno));
623 if (write(fd, fdt, new_size) != new_size) {
624 debug("%s: Failed to write external data to file %s\n",
625 __func__, strerror(errno));
635 munmap(old_fdt, sbuf.st_size);
642 static int copyfile(const char *src, const char *dst)
644 int fd_src = -1, fd_dst = -1;
650 fd_src = open(src, O_RDONLY);
652 printf("Can't open file %s (%s)\n", src, strerror(errno));
656 fd_dst = open(dst, O_WRONLY | O_CREAT, 0666);
658 printf("Can't open file %s (%s)\n", dst, strerror(errno));
664 printf("Can't allocate buffer to copy file\n");
669 size = read(fd_src, buf, 512);
671 printf("Can't read file %s\n", src);
678 size = write(fd_dst, buf, count);
680 printf("Can't write file %s\n", dst);
699 * fit_handle_file - main FIT file processing function
701 * fit_handle_file() runs dtc to convert .its to .itb, includes
702 * binary data, updates timestamp property and calculates hashes.
704 * datafile - .its file
705 * imagefile - .itb file
708 * only on success, otherwise calls exit (EXIT_FAILURE);
710 static int fit_handle_file(struct image_tool_params *params)
712 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
713 char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0};
714 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
718 /* Flattened Image Tree (FIT) format handling */
719 debug ("FIT format handling\n");
721 /* call dtc to include binary properties into the tmp file */
722 if (strlen (params->imagefile) +
723 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
724 fprintf (stderr, "%s: Image file name (%s) too long, "
725 "can't create tmpfile",
726 params->imagefile, params->cmdname);
727 return (EXIT_FAILURE);
729 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
731 /* We either compile the source file, or use the existing FIT image */
732 if (params->auto_its) {
733 if (fit_build(params, tmpfile)) {
734 fprintf(stderr, "%s: failed to build FIT\n",
739 } else if (params->datafile) {
740 /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
741 snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
742 MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
743 debug("Trying to execute \"%s\"\n", cmd);
745 snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
746 params->imagefile, tmpfile);
749 if (*cmd && system(cmd) == -1) {
750 fprintf (stderr, "%s: system(%s) failed: %s\n",
751 params->cmdname, cmd, strerror(errno));
755 /* Move the data so it is internal to the FIT, if needed */
756 ret = fit_import_data(params, tmpfile);
761 * Copy the tmpfile to bakfile, then in the following loop
762 * we copy bakfile to tmpfile. So we always start from the
765 sprintf(bakfile, "%s%s", tmpfile, ".bak");
766 rename(tmpfile, bakfile);
769 * Set hashes for images in the blob. Unfortunately we may need more
770 * space in either FDT, so keep trying until we succeed.
772 * Note: this is pretty inefficient for signing, since we must
773 * calculate the signature every time. It would be better to calculate
774 * all the data and then store it in a separate step. However, this
775 * would be considerably more complex to implement. Generally a few
776 * steps of this loop is enough to sign with several keys.
778 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
779 if (copyfile(bakfile, tmpfile) < 0) {
780 printf("Can't copy %s to %s\n", bakfile, tmpfile);
784 ret = fit_add_file_data(params, size_inc, tmpfile);
785 if (!ret || ret != -ENOSPC)
790 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
791 params->cmdname, ret);
795 /* Move the data so it is external to the FIT, if requested */
796 if (params->external_data) {
797 ret = fit_extract_data(params, tmpfile);
802 if (rename (tmpfile, params->imagefile) == -1) {
803 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
804 params->cmdname, tmpfile, params->imagefile,
808 unlink (params->imagefile);
821 * fit_image_extract - extract a FIT component image
822 * @fit: pointer to the FIT format image header
823 * @image_noffset: offset of the component image node
824 * @file_name: name of the file to store the FIT sub-image
827 * zero in case of success or a negative value if fail.
829 static int fit_image_extract(
832 const char *file_name)
834 const void *file_data;
835 size_t file_size = 0;
838 /* get the data address and size of component at offset "image_noffset" */
839 ret = fit_image_get_data_and_size(fit, image_noffset, &file_data, &file_size);
841 fprintf(stderr, "Could not get component information\n");
845 /* save the "file_data" into the file specified by "file_name" */
846 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
850 * fit_extract_contents - retrieve a sub-image component from the FIT image
851 * @ptr: pointer to the FIT format image header
852 * @params: command line parameters
855 * zero in case of success or a negative value if fail.
857 static int fit_extract_contents(void *ptr, struct image_tool_params *params)
862 const void *fit = ptr;
866 /* Indent string is defined in header image.h */
867 p = IMAGE_INDENT_STRING;
869 if (!fit_check_format(fit)) {
870 printf("Bad FIT image format\n");
874 /* Find images parent node offset */
875 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
876 if (images_noffset < 0) {
877 printf("Can't find images parent node '%s' (%s)\n",
878 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
882 /* Avoid any overrun */
883 count = fit_get_subimage_count(fit, images_noffset);
884 if ((params->pflag < 0) || (count <= params->pflag)) {
885 printf("No such component at '%d'\n", params->pflag);
889 /* Process its subnodes, extract the desired component from image */
890 for (ndepth = 0, count = 0,
891 noffset = fdt_next_node(fit, images_noffset, &ndepth);
892 (noffset >= 0) && (ndepth > 0);
893 noffset = fdt_next_node(fit, noffset, &ndepth)) {
896 * Direct child node of the images parent node,
897 * i.e. component image node.
899 if (params->pflag == count) {
900 printf("Extracted:\n%s Image %u (%s)\n", p,
901 count, fit_get_name(fit, noffset, NULL));
903 fit_image_print(fit, noffset, p);
905 return fit_image_extract(fit, noffset,
916 static int fit_check_params(struct image_tool_params *params)
918 if (params->auto_its)
920 return ((params->dflag && params->fflag) ||
921 (params->fflag && params->lflag) ||
922 (params->lflag && params->dflag));
928 sizeof(image_header_t),
934 fit_extract_contents,
935 fit_check_image_types,
937 NULL /* FIT images use DTB header */