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"
23 #include <u-boot/crc.h>
25 static struct legacy_img_hdr 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,
42 if (params->keydest) {
43 struct stat dest_sbuf;
45 destfd = mmap_fdt(params->cmdname, params->keydest, size_inc,
46 &dest_blob, &dest_sbuf, false,
52 destfd_size = dest_sbuf.st_size;
55 /* for first image creation, add a timestamp at offset 0 i.e., root */
56 if (params->datafile || params->reset_timestamp) {
57 time_t time = imagetool_get_source_date(params->cmdname,
59 ret = fit_set_timestamp(ptr, 0, time);
63 ret = fit_pre_load_data(params->keydir, dest_blob, ptr);
66 ret = fit_cipher_data(params->keydir, dest_blob, ptr,
74 ret = fit_add_verification_data(params->keydir,
75 params->keyfile, dest_blob, ptr,
85 munmap(dest_blob, destfd_size);
90 munmap(ptr, sbuf.st_size);
96 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
98 static int fit_calc_size(struct image_tool_params *params)
100 struct content_info *cont;
101 int size, total_size;
103 size = imagetool_get_filesize(params, params->datafile);
108 if (params->fit_ramdisk) {
109 size = imagetool_get_filesize(params, params->fit_ramdisk);
115 for (cont = params->content_head; cont; cont = cont->next) {
116 size = imagetool_get_filesize(params, cont->fname);
120 /* Add space for properties and hash node */
121 total_size += size + 300;
124 /* Add plenty of space for headers, properties, nodes, etc. */
130 static int fdt_property_file(struct image_tool_params *params,
131 void *fdt, const char *name, const char *fname)
138 fd = open(fname, O_RDWR | O_BINARY);
140 fprintf(stderr, "%s: Can't open %s: %s\n",
141 params->cmdname, fname, strerror(errno));
145 if (fstat(fd, &sbuf) < 0) {
146 fprintf(stderr, "%s: Can't stat %s: %s\n",
147 params->cmdname, fname, strerror(errno));
151 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
154 ret = read(fd, ptr, sbuf.st_size);
155 if (ret != sbuf.st_size) {
156 fprintf(stderr, "%s: Can't read %s: %s\n",
157 params->cmdname, fname, strerror(errno));
168 static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
174 vsnprintf(str, sizeof(str), fmt, ptr);
176 return fdt_property_string(fdt, name, str);
179 static void get_basename(char *str, int size, const char *fname)
181 const char *p, *start, *end;
185 * Use the base name as the 'name' field. So for example:
187 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
188 * becomes "sun7i-a20-bananapro"
190 p = strrchr(fname, '/');
191 start = p ? p + 1 : fname;
192 p = strrchr(fname, '.');
193 end = p ? p : fname + strlen(fname);
197 memcpy(str, start, len);
202 * add_hash_node() - Add a hash or signature node
204 * @params: Image parameters
205 * @fdt: Device tree to add to (in sequential-write mode)
207 * If there is a key name hint, try to sign the images. Otherwise, just add a
210 * Return: 0 on success, or -1 on failure
212 static int add_hash_node(struct image_tool_params *params, void *fdt)
214 if (params->keyname) {
215 if (!params->algo_name) {
217 "%s: Algorithm name must be specified\n",
222 fdt_begin_node(fdt, "signature-1");
223 fdt_property_string(fdt, FIT_ALGO_PROP, params->algo_name);
224 fdt_property_string(fdt, FIT_KEY_HINT, params->keyname);
226 fdt_begin_node(fdt, "hash-1");
227 fdt_property_string(fdt, FIT_ALGO_PROP, "crc32");
235 * fit_write_images() - Write out a list of images to the FIT
237 * We always include the main image (params->datafile). If there are device
238 * tree files, we include an fdt- node for each of those too.
240 static int fit_write_images(struct image_tool_params *params, char *fdt)
242 struct content_info *cont;
243 const char *typename;
248 fdt_begin_node(fdt, "images");
250 /* First the main image */
251 typename = genimg_get_type_short_name(params->fit_image_type);
252 snprintf(str, sizeof(str), "%s-1", typename);
253 fdt_begin_node(fdt, str);
254 fdt_property_string(fdt, FIT_DESC_PROP, params->imagename);
255 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
256 fdt_property_string(fdt, FIT_ARCH_PROP,
257 genimg_get_arch_short_name(params->arch));
258 fdt_property_string(fdt, FIT_OS_PROP,
259 genimg_get_os_short_name(params->os));
260 fdt_property_string(fdt, FIT_COMP_PROP,
261 genimg_get_comp_short_name(params->comp));
262 fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr);
263 fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep);
266 * Put data last since it is large. SPL may only load the first part
267 * of the DT, so this way it can access all the above fields.
269 ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile);
272 ret = add_hash_node(params, fdt);
277 /* Now the device tree files if available */
279 for (cont = params->content_head; cont; cont = cont->next) {
280 if (cont->type != IH_TYPE_FLATDT)
282 typename = genimg_get_type_short_name(cont->type);
283 snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
284 fdt_begin_node(fdt, str);
286 get_basename(str, sizeof(str), cont->fname);
287 fdt_property_string(fdt, FIT_DESC_PROP, str);
288 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
292 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
293 fdt_property_string(fdt, FIT_ARCH_PROP,
294 genimg_get_arch_short_name(params->arch));
295 fdt_property_string(fdt, FIT_COMP_PROP,
296 genimg_get_comp_short_name(IH_COMP_NONE));
297 ret = add_hash_node(params, fdt);
303 /* And a ramdisk file if available */
304 if (params->fit_ramdisk) {
305 fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
307 fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP);
308 fdt_property_string(fdt, FIT_OS_PROP,
309 genimg_get_os_short_name(params->os));
310 fdt_property_string(fdt, FIT_ARCH_PROP,
311 genimg_get_arch_short_name(params->arch));
313 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
314 params->fit_ramdisk);
317 ret = add_hash_node(params, fdt);
329 * fit_write_configs() - Write out a list of configurations to the FIT
331 * If there are device tree files, we include a configuration for each, which
332 * selects the main image (params->datafile) and its corresponding device
335 * Otherwise we just create a configuration with the main image in it.
337 static void fit_write_configs(struct image_tool_params *params, char *fdt)
339 struct content_info *cont;
340 const char *typename;
344 fdt_begin_node(fdt, "configurations");
345 fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
348 for (cont = params->content_head; cont; cont = cont->next) {
349 if (cont->type != IH_TYPE_FLATDT)
351 typename = genimg_get_type_short_name(cont->type);
352 snprintf(str, sizeof(str), "conf-%d", ++upto);
353 fdt_begin_node(fdt, str);
355 get_basename(str, sizeof(str), cont->fname);
356 fdt_property_string(fdt, FIT_DESC_PROP, str);
358 typename = genimg_get_type_short_name(params->fit_image_type);
359 snprintf(str, sizeof(str), "%s-1", typename);
360 fdt_property_string(fdt, typename, str);
361 fdt_property_string(fdt, FIT_LOADABLE_PROP, str);
363 if (params->fit_ramdisk)
364 fdt_property_string(fdt, FIT_RAMDISK_PROP,
365 FIT_RAMDISK_PROP "-1");
367 snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
368 fdt_property_string(fdt, FIT_FDT_PROP, str);
373 fdt_begin_node(fdt, "conf-1");
374 typename = genimg_get_type_short_name(params->fit_image_type);
375 snprintf(str, sizeof(str), "%s-1", typename);
376 fdt_property_string(fdt, typename, str);
378 if (params->fit_ramdisk)
379 fdt_property_string(fdt, FIT_RAMDISK_PROP,
380 FIT_RAMDISK_PROP "-1");
388 static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
392 ret = fdt_create(fdt, size);
395 fdt_finish_reservemap(fdt);
396 fdt_begin_node(fdt, "");
397 fdt_property_strf(fdt, FIT_DESC_PROP,
398 "%s image with one or more FDT blobs",
399 genimg_get_type_name(params->fit_image_type));
400 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
401 fdt_property_u32(fdt, "#address-cells", 1);
402 ret = fit_write_images(params, fdt);
405 fit_write_configs(params, fdt);
407 ret = fdt_finish(fdt);
411 return fdt_totalsize(fdt);
414 static int fit_build(struct image_tool_params *params, const char *fname)
421 size = fit_calc_size(params);
424 buf = calloc(1, size);
426 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
427 params->cmdname, size);
430 ret = fit_build_fdt(params, buf, size);
432 fprintf(stderr, "%s: Failed to build FIT image\n",
437 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
439 fprintf(stderr, "%s: Can't open %s: %s\n",
440 params->cmdname, fname, strerror(errno));
443 ret = write(fd, buf, size);
445 fprintf(stderr, "%s: Can't write %s: %s\n",
446 params->cmdname, fname, strerror(errno));
461 * fit_extract_data() - Move all data outside the FIT
463 * This takes a normal FIT file and removes all the 'data' properties from it.
464 * The data is placed in an area after the FIT so that it can be accessed
465 * using an offset into that area. The 'data' properties turn into
466 * 'data-offset' properties.
468 * This function cannot cope with FITs with 'data-offset' properties. All
469 * data must be in 'data' properties on entry.
471 static int fit_extract_data(struct image_tool_params *params, const char *fname)
475 int fit_size, new_size;
485 align_size = params->bl_len ? params->bl_len : 4;
486 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false);
489 fit_size = fdt_totalsize(fdt);
491 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
493 debug("%s: Cannot find /images node: %d\n", __func__, images);
497 image_number = fdtdec_get_child_count(fdt, images);
500 * Allocate space to hold the image data we will extract,
501 * extral space allocate for image alignment to prevent overflow.
503 buf = calloc(1, fit_size + (align_size * image_number));
510 for (node = fdt_first_subnode(fdt, images);
512 node = fdt_next_subnode(fdt, node)) {
516 data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
519 memcpy(buf + buf_ptr, data, len);
520 debug("Extracting data size %x\n", len);
522 ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
527 if (params->external_offset > 0) {
528 /* An external offset positions the data absolutely. */
529 fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
530 params->external_offset + buf_ptr);
532 fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
535 fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
536 buf_ptr += ALIGN(len, align_size);
539 /* Pack the FDT and place the data after it */
542 new_size = fdt_totalsize(fdt);
543 new_size = ALIGN(new_size, align_size);
544 fdt_set_totalsize(fdt, new_size);
545 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
546 debug("External data size %x\n", buf_ptr);
547 munmap(fdt, sbuf.st_size);
549 if (ftruncate(fd, new_size)) {
550 debug("%s: Failed to truncate file: %s\n", __func__,
556 /* Check if an offset for the external data was set. */
557 if (params->external_offset > 0) {
558 if (params->external_offset < new_size) {
560 "External offset %x overlaps FIT length %x\n",
561 params->external_offset, new_size);
565 new_size = params->external_offset;
567 if (lseek(fd, new_size, SEEK_SET) < 0) {
568 debug("%s: Failed to seek to end of file: %s\n", __func__,
573 if (write(fd, buf, buf_ptr) != buf_ptr) {
574 debug("%s: Failed to write external data to file %s\n",
575 __func__, strerror(errno));
584 munmap(fdt, sbuf.st_size);
591 static int fit_import_data(struct image_tool_params *params, const char *fname)
594 int fit_size, new_size, size, data_base;
601 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false);
604 fit_size = fdt_totalsize(old_fdt);
605 data_base = ALIGN(fit_size, 4);
607 /* Allocate space to hold the new FIT */
608 size = sbuf.st_size + 16384;
609 fdt = calloc(1, size);
611 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
616 ret = fdt_open_into(old_fdt, fdt, size);
618 debug("%s: Failed to expand FIT: %s\n", __func__,
619 fdt_strerror(errno));
624 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
626 debug("%s: Cannot find /images node: %d\n", __func__, images);
631 for (node = fdt_first_subnode(fdt, images);
633 node = fdt_next_subnode(fdt, node)) {
637 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
638 len = fdtdec_get_int(fdt, node, "data-size", -1);
639 if (buf_ptr == -1 || len == -1)
641 debug("Importing data size %x\n", len);
643 ret = fdt_setprop(fdt, node, "data",
644 old_fdt + data_base + buf_ptr, len);
646 debug("%s: Failed to write property: %s\n", __func__,
653 munmap(old_fdt, sbuf.st_size);
655 /* Close the old fd so we can re-use it. */
658 /* Pack the FDT and place the data after it */
661 new_size = fdt_totalsize(fdt);
662 debug("Size expanded from %x to %x\n", fit_size, new_size);
664 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
666 fprintf(stderr, "%s: Can't open %s: %s\n",
667 params->cmdname, fname, strerror(errno));
671 if (write(fd, fdt, new_size) != new_size) {
672 debug("%s: Failed to write external data to file %s\n",
673 __func__, strerror(errno));
683 munmap(old_fdt, sbuf.st_size);
691 * fit_handle_file - main FIT file processing function
693 * fit_handle_file() runs dtc to convert .its to .itb, includes
694 * binary data, updates timestamp property and calculates hashes.
696 * datafile - .its file
697 * imagefile - .itb file
700 * only on success, otherwise calls exit (EXIT_FAILURE);
702 static int fit_handle_file(struct image_tool_params *params)
704 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
705 char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0};
706 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
710 /* Flattened Image Tree (FIT) format handling */
711 debug ("FIT format handling\n");
713 /* call dtc to include binary properties into the tmp file */
714 if (strlen (params->imagefile) +
715 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
716 fprintf (stderr, "%s: Image file name (%s) too long, "
717 "can't create tmpfile.\n",
718 params->imagefile, params->cmdname);
719 return (EXIT_FAILURE);
721 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
723 /* We either compile the source file, or use the existing FIT image */
724 if (params->auto_its) {
725 if (fit_build(params, tmpfile)) {
726 fprintf(stderr, "%s: failed to build FIT\n",
731 } else if (params->datafile) {
732 /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
733 snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
734 MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
735 debug("Trying to execute \"%s\"\n", cmd);
737 snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
738 params->imagefile, tmpfile);
740 if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) {
741 fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n");
744 if (*cmd && system(cmd) == -1) {
745 fprintf (stderr, "%s: system(%s) failed: %s\n",
746 params->cmdname, cmd, strerror(errno));
750 /* Move the data so it is internal to the FIT, if needed */
751 ret = fit_import_data(params, tmpfile);
756 * Copy the tmpfile to bakfile, then in the following loop
757 * we copy bakfile to tmpfile. So we always start from the
760 sprintf(bakfile, "%s%s", tmpfile, ".bak");
761 rename(tmpfile, bakfile);
764 * Set hashes for images in the blob. Unfortunately we may need more
765 * space in either FDT, so keep trying until we succeed.
767 * Note: this is pretty inefficient for signing, since we must
768 * calculate the signature every time. It would be better to calculate
769 * all the data and then store it in a separate step. However, this
770 * would be considerably more complex to implement. Generally a few
771 * steps of this loop is enough to sign with several keys.
773 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
774 if (copyfile(bakfile, tmpfile) < 0) {
775 printf("Can't copy %s to %s\n", bakfile, tmpfile);
779 ret = fit_add_file_data(params, size_inc, tmpfile);
780 if (!ret || ret != -ENOSPC)
785 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
786 params->cmdname, ret);
790 /* Move the data so it is external to the FIT, if requested */
791 if (params->external_data) {
792 ret = fit_extract_data(params, tmpfile);
797 if (rename (tmpfile, params->imagefile) == -1) {
798 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
799 params->cmdname, tmpfile, params->imagefile,
803 unlink (params->imagefile);
816 * fit_image_extract - extract a FIT component image
817 * @fit: pointer to the FIT format image header
818 * @image_noffset: offset of the component image node
819 * @file_name: name of the file to store the FIT sub-image
822 * zero in case of success or a negative value if fail.
824 static int fit_image_extract(
827 const char *file_name)
829 const void *file_data;
830 size_t file_size = 0;
833 /* get the data address and size of component at offset "image_noffset" */
834 ret = fit_image_get_data_and_size(fit, image_noffset, &file_data, &file_size);
836 fprintf(stderr, "Could not get component information\n");
840 /* save the "file_data" into the file specified by "file_name" */
841 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
845 * fit_extract_contents - retrieve a sub-image component from the FIT image
846 * @ptr: pointer to the FIT format image header
847 * @params: command line parameters
850 * zero in case of success or a negative value if fail.
852 static int fit_extract_contents(void *ptr, struct image_tool_params *params)
857 const void *fit = ptr;
861 /* Indent string is defined in header image.h */
862 p = IMAGE_INDENT_STRING;
864 /* Find images parent node offset */
865 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
866 if (images_noffset < 0) {
867 printf("Can't find images parent node '%s' (%s)\n",
868 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
872 /* Avoid any overrun */
873 count = fit_get_subimage_count(fit, images_noffset);
874 if ((params->pflag < 0) || (count <= params->pflag)) {
875 printf("No such component at '%d'\n", params->pflag);
879 /* Process its subnodes, extract the desired component from image */
880 for (ndepth = 0, count = 0,
881 noffset = fdt_next_node(fit, images_noffset, &ndepth);
882 (noffset >= 0) && (ndepth > 0);
883 noffset = fdt_next_node(fit, noffset, &ndepth)) {
886 * Direct child node of the images parent node,
887 * i.e. component image node.
889 if (params->pflag == count) {
890 printf("Extracted:\n%s Image %u (%s)\n", p,
891 count, fit_get_name(fit, noffset, NULL));
893 fit_image_print(fit, noffset, p);
895 return fit_image_extract(fit, noffset,
906 static int fit_check_params(struct image_tool_params *params)
908 if (params->auto_its)
910 return ((params->dflag && params->fflag) ||
911 (params->fflag && params->lflag) ||
912 (params->lflag && params->dflag));
918 sizeof(struct legacy_img_hdr),
924 fit_extract_contents,
925 fit_check_image_types,
927 NULL /* FIT images use DTB header */