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,
67 munmap(dest_blob, destfd_size);
72 munmap(ptr, sbuf.st_size);
79 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
81 static int fit_calc_size(struct image_tool_params *params)
83 struct content_info *cont;
86 size = imagetool_get_filesize(params, params->datafile);
91 if (params->fit_ramdisk) {
92 size = imagetool_get_filesize(params, params->fit_ramdisk);
98 for (cont = params->content_head; cont; cont = cont->next) {
99 size = imagetool_get_filesize(params, cont->fname);
103 /* Add space for properties */
104 total_size += size + 300;
107 /* Add plenty of space for headers, properties, nodes, etc. */
113 static int fdt_property_file(struct image_tool_params *params,
114 void *fdt, const char *name, const char *fname)
121 fd = open(fname, O_RDWR | O_BINARY);
123 fprintf(stderr, "%s: Can't open %s: %s\n",
124 params->cmdname, fname, strerror(errno));
128 if (fstat(fd, &sbuf) < 0) {
129 fprintf(stderr, "%s: Can't stat %s: %s\n",
130 params->cmdname, fname, strerror(errno));
134 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
137 ret = read(fd, ptr, sbuf.st_size);
138 if (ret != sbuf.st_size) {
139 fprintf(stderr, "%s: Can't read %s: %s\n",
140 params->cmdname, fname, strerror(errno));
151 static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
157 vsnprintf(str, sizeof(str), fmt, ptr);
159 return fdt_property_string(fdt, name, str);
162 static void get_basename(char *str, int size, const char *fname)
164 const char *p, *start, *end;
168 * Use the base name as the 'name' field. So for example:
170 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
171 * becomes "sun7i-a20-bananapro"
173 p = strrchr(fname, '/');
174 start = p ? p + 1 : fname;
175 p = strrchr(fname, '.');
176 end = p ? p : fname + strlen(fname);
180 memcpy(str, start, len);
185 * fit_write_images() - Write out a list of images to the FIT
187 * We always include the main image (params->datafile). If there are device
188 * tree files, we include an fdt- node for each of those too.
190 static int fit_write_images(struct image_tool_params *params, char *fdt)
192 struct content_info *cont;
193 const char *typename;
198 fdt_begin_node(fdt, "images");
200 /* First the main image */
201 typename = genimg_get_type_short_name(params->fit_image_type);
202 snprintf(str, sizeof(str), "%s-1", typename);
203 fdt_begin_node(fdt, str);
204 fdt_property_string(fdt, "description", params->imagename);
205 fdt_property_string(fdt, "type", typename);
206 fdt_property_string(fdt, "arch",
207 genimg_get_arch_short_name(params->arch));
208 fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os));
209 fdt_property_string(fdt, "compression",
210 genimg_get_comp_short_name(params->comp));
211 fdt_property_u32(fdt, "load", params->addr);
212 fdt_property_u32(fdt, "entry", params->ep);
215 * Put data last since it is large. SPL may only load the first part
216 * of the DT, so this way it can access all the above fields.
218 ret = fdt_property_file(params, fdt, "data", params->datafile);
223 /* Now the device tree files if available */
225 for (cont = params->content_head; cont; cont = cont->next) {
226 if (cont->type != IH_TYPE_FLATDT)
228 snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
229 fdt_begin_node(fdt, str);
231 get_basename(str, sizeof(str), cont->fname);
232 fdt_property_string(fdt, "description", str);
233 ret = fdt_property_file(params, fdt, "data", cont->fname);
236 fdt_property_string(fdt, "type", typename);
237 fdt_property_string(fdt, "arch",
238 genimg_get_arch_short_name(params->arch));
239 fdt_property_string(fdt, "compression",
240 genimg_get_comp_short_name(IH_COMP_NONE));
244 /* And a ramdisk file if available */
245 if (params->fit_ramdisk) {
246 fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
248 fdt_property_string(fdt, "type", FIT_RAMDISK_PROP);
249 fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os));
251 ret = fdt_property_file(params, fdt, "data", params->fit_ramdisk);
264 * fit_write_configs() - Write out a list of configurations to the FIT
266 * If there are device tree files, we include a configuration for each, which
267 * selects the main image (params->datafile) and its corresponding device
270 * Otherwise we just create a configuration with the main image in it.
272 static void fit_write_configs(struct image_tool_params *params, char *fdt)
274 struct content_info *cont;
275 const char *typename;
279 fdt_begin_node(fdt, "configurations");
280 fdt_property_string(fdt, "default", "conf-1");
283 for (cont = params->content_head; cont; cont = cont->next) {
284 if (cont->type != IH_TYPE_FLATDT)
286 typename = genimg_get_type_short_name(cont->type);
287 snprintf(str, sizeof(str), "conf-%d", ++upto);
288 fdt_begin_node(fdt, str);
290 get_basename(str, sizeof(str), cont->fname);
291 fdt_property_string(fdt, "description", str);
293 typename = genimg_get_type_short_name(params->fit_image_type);
294 snprintf(str, sizeof(str), "%s-1", typename);
295 fdt_property_string(fdt, typename, str);
297 if (params->fit_ramdisk)
298 fdt_property_string(fdt, FIT_RAMDISK_PROP,
299 FIT_RAMDISK_PROP "-1");
301 snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
302 fdt_property_string(fdt, FIT_FDT_PROP, str);
307 fdt_begin_node(fdt, "conf-1");
308 typename = genimg_get_type_short_name(params->fit_image_type);
309 snprintf(str, sizeof(str), "%s-1", typename);
310 fdt_property_string(fdt, typename, str);
312 if (params->fit_ramdisk)
313 fdt_property_string(fdt, FIT_RAMDISK_PROP,
314 FIT_RAMDISK_PROP "-1");
322 static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
326 ret = fdt_create(fdt, size);
329 fdt_finish_reservemap(fdt);
330 fdt_begin_node(fdt, "");
331 fdt_property_strf(fdt, "description",
332 "%s image with one or more FDT blobs",
333 genimg_get_type_name(params->fit_image_type));
334 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
335 fdt_property_u32(fdt, "#address-cells", 1);
336 ret = fit_write_images(params, fdt);
339 fit_write_configs(params, fdt);
341 ret = fdt_finish(fdt);
345 return fdt_totalsize(fdt);
348 static int fit_build(struct image_tool_params *params, const char *fname)
355 size = fit_calc_size(params);
360 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
361 params->cmdname, size);
364 ret = fit_build_fdt(params, buf, size);
366 fprintf(stderr, "%s: Failed to build FIT image\n",
371 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
373 fprintf(stderr, "%s: Can't open %s: %s\n",
374 params->cmdname, fname, strerror(errno));
377 ret = write(fd, buf, size);
379 fprintf(stderr, "%s: Can't write %s: %s\n",
380 params->cmdname, fname, strerror(errno));
395 * fit_extract_data() - Move all data outside the FIT
397 * This takes a normal FIT file and removes all the 'data' properties from it.
398 * The data is placed in an area after the FIT so that it can be accessed
399 * using an offset into that area. The 'data' properties turn into
400 * 'data-offset' properties.
402 * This function cannot cope with FITs with 'data-offset' properties. All
403 * data must be in 'data' properties on entry.
405 static int fit_extract_data(struct image_tool_params *params, const char *fname)
409 int fit_size, new_size;
417 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false);
420 fit_size = fdt_totalsize(fdt);
422 /* Allocate space to hold the image data we will extract */
423 buf = malloc(fit_size);
430 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
432 debug("%s: Cannot find /images node: %d\n", __func__, images);
437 for (node = fdt_first_subnode(fdt, images);
439 node = fdt_next_subnode(fdt, node)) {
443 data = fdt_getprop(fdt, node, "data", &len);
446 memcpy(buf + buf_ptr, data, len);
447 debug("Extracting data size %x\n", len);
449 ret = fdt_delprop(fdt, node, "data");
454 if (params->external_offset > 0) {
455 /* An external offset positions the data absolutely. */
456 fdt_setprop_u32(fdt, node, "data-position",
457 params->external_offset + buf_ptr);
459 fdt_setprop_u32(fdt, node, "data-offset", buf_ptr);
461 fdt_setprop_u32(fdt, node, "data-size", len);
463 buf_ptr += (len + 3) & ~3;
466 /* Pack the FDT and place the data after it */
469 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
470 debug("External data size %x\n", buf_ptr);
471 new_size = fdt_totalsize(fdt);
472 new_size = (new_size + 3) & ~3;
473 munmap(fdt, sbuf.st_size);
475 if (ftruncate(fd, new_size)) {
476 debug("%s: Failed to truncate file: %s\n", __func__,
482 /* Check if an offset for the external data was set. */
483 if (params->external_offset > 0) {
484 if (params->external_offset < new_size) {
485 debug("External offset %x overlaps FIT length %x",
486 params->external_offset, new_size);
490 new_size = params->external_offset;
492 if (lseek(fd, new_size, SEEK_SET) < 0) {
493 debug("%s: Failed to seek to end of file: %s\n", __func__,
498 if (write(fd, buf, buf_ptr) != buf_ptr) {
499 debug("%s: Failed to write external data to file %s\n",
500 __func__, strerror(errno));
509 munmap(fdt, sbuf.st_size);
517 static int fit_import_data(struct image_tool_params *params, const char *fname)
520 int fit_size, new_size, size, data_base;
527 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false);
530 fit_size = fdt_totalsize(old_fdt);
531 data_base = (fit_size + 3) & ~3;
533 /* Allocate space to hold the new FIT */
534 size = sbuf.st_size + 16384;
537 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
542 ret = fdt_open_into(old_fdt, fdt, size);
544 debug("%s: Failed to expand FIT: %s\n", __func__,
545 fdt_strerror(errno));
550 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
552 debug("%s: Cannot find /images node: %d\n", __func__, images);
557 for (node = fdt_first_subnode(fdt, images);
559 node = fdt_next_subnode(fdt, node)) {
563 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
564 len = fdtdec_get_int(fdt, node, "data-size", -1);
565 if (buf_ptr == -1 || len == -1)
567 debug("Importing data size %x\n", len);
569 ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
572 debug("%s: Failed to write property: %s\n", __func__,
579 /* Close the old fd so we can re-use it. */
582 /* Pack the FDT and place the data after it */
585 new_size = fdt_totalsize(fdt);
586 debug("Size expanded from %x to %x\n", fit_size, new_size);
588 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
590 fprintf(stderr, "%s: Can't open %s: %s\n",
591 params->cmdname, fname, strerror(errno));
595 if (write(fd, fdt, new_size) != new_size) {
596 debug("%s: Failed to write external data to file %s\n",
597 __func__, strerror(errno));
607 munmap(old_fdt, sbuf.st_size);
613 * fit_handle_file - main FIT file processing function
615 * fit_handle_file() runs dtc to convert .its to .itb, includes
616 * binary data, updates timestamp property and calculates hashes.
618 * datafile - .its file
619 * imagefile - .itb file
622 * only on success, otherwise calls exit (EXIT_FAILURE);
624 static int fit_handle_file(struct image_tool_params *params)
626 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
627 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
631 /* Flattened Image Tree (FIT) format handling */
632 debug ("FIT format handling\n");
634 /* call dtc to include binary properties into the tmp file */
635 if (strlen (params->imagefile) +
636 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
637 fprintf (stderr, "%s: Image file name (%s) too long, "
638 "can't create tmpfile",
639 params->imagefile, params->cmdname);
640 return (EXIT_FAILURE);
642 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
644 /* We either compile the source file, or use the existing FIT image */
645 if (params->auto_its) {
646 if (fit_build(params, tmpfile)) {
647 fprintf(stderr, "%s: failed to build FIT\n",
652 } else if (params->datafile) {
653 /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
654 snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
655 MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
656 debug("Trying to execute \"%s\"\n", cmd);
658 snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
659 params->imagefile, tmpfile);
661 if (*cmd && system(cmd) == -1) {
662 fprintf (stderr, "%s: system(%s) failed: %s\n",
663 params->cmdname, cmd, strerror(errno));
667 /* Move the data so it is internal to the FIT, if needed */
668 ret = fit_import_data(params, tmpfile);
673 * Set hashes for images in the blob. Unfortunately we may need more
674 * space in either FDT, so keep trying until we succeed.
676 * Note: this is pretty inefficient for signing, since we must
677 * calculate the signature every time. It would be better to calculate
678 * all the data and then store it in a separate step. However, this
679 * would be considerably more complex to implement. Generally a few
680 * steps of this loop is enough to sign with several keys.
682 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
683 ret = fit_add_file_data(params, size_inc, tmpfile);
684 if (!ret || ret != -ENOSPC)
689 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
690 params->cmdname, ret);
694 /* Move the data so it is external to the FIT, if requested */
695 if (params->external_data) {
696 ret = fit_extract_data(params, tmpfile);
701 if (rename (tmpfile, params->imagefile) == -1) {
702 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
703 params->cmdname, tmpfile, params->imagefile,
706 unlink (params->imagefile);
717 * fit_image_extract - extract a FIT component image
718 * @fit: pointer to the FIT format image header
719 * @image_noffset: offset of the component image node
720 * @file_name: name of the file to store the FIT sub-image
723 * zero in case of success or a negative value if fail.
725 static int fit_image_extract(
728 const char *file_name)
730 const void *file_data;
731 size_t file_size = 0;
733 /* get the "data" property of component at offset "image_noffset" */
734 fit_image_get_data(fit, image_noffset, &file_data, &file_size);
736 /* save the "file_data" into the file specified by "file_name" */
737 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
741 * fit_extract_contents - retrieve a sub-image component from the FIT image
742 * @ptr: pointer to the FIT format image header
743 * @params: command line parameters
746 * zero in case of success or a negative value if fail.
748 static int fit_extract_contents(void *ptr, struct image_tool_params *params)
753 const void *fit = ptr;
757 /* Indent string is defined in header image.h */
758 p = IMAGE_INDENT_STRING;
760 if (!fit_check_format(fit)) {
761 printf("Bad FIT image format\n");
765 /* Find images parent node offset */
766 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
767 if (images_noffset < 0) {
768 printf("Can't find images parent node '%s' (%s)\n",
769 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
773 /* Avoid any overrun */
774 count = fit_get_subimage_count(fit, images_noffset);
775 if ((params->pflag < 0) || (count <= params->pflag)) {
776 printf("No such component at '%d'\n", params->pflag);
780 /* Process its subnodes, extract the desired component from image */
781 for (ndepth = 0, count = 0,
782 noffset = fdt_next_node(fit, images_noffset, &ndepth);
783 (noffset >= 0) && (ndepth > 0);
784 noffset = fdt_next_node(fit, noffset, &ndepth)) {
787 * Direct child node of the images parent node,
788 * i.e. component image node.
790 if (params->pflag == count) {
791 printf("Extracted:\n%s Image %u (%s)\n", p,
792 count, fit_get_name(fit, noffset, NULL));
794 fit_image_print(fit, noffset, p);
796 return fit_image_extract(fit, noffset,
807 static int fit_check_params(struct image_tool_params *params)
809 if (params->auto_its)
811 return ((params->dflag && (params->fflag || params->lflag)) ||
812 (params->fflag && (params->dflag || params->lflag)) ||
813 (params->lflag && (params->dflag || params->fflag)));
819 sizeof(image_header_t),
825 fit_extract_contents,
826 fit_check_image_types,
828 NULL /* FIT images use DTB header */