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 */
55 ret = fit_set_timestamp(ptr, 0, sbuf.st_mtime);
58 ret = fit_add_verification_data(params->keydir, dest_blob, ptr,
60 params->require_keys);
64 munmap(dest_blob, destfd_size);
69 munmap(ptr, sbuf.st_size);
76 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
78 static int fit_calc_size(struct image_tool_params *params)
80 struct content_info *cont;
83 size = imagetool_get_filesize(params, params->datafile);
88 for (cont = params->content_head; cont; cont = cont->next) {
89 size = imagetool_get_filesize(params, cont->fname);
93 /* Add space for properties */
94 total_size += size + 300;
97 /* Add plenty of space for headers, properties, nodes, etc. */
103 static int fdt_property_file(struct image_tool_params *params,
104 void *fdt, const char *name, const char *fname)
111 fd = open(fname, O_RDWR | O_BINARY);
113 fprintf(stderr, "%s: Can't open %s: %s\n",
114 params->cmdname, fname, strerror(errno));
118 if (fstat(fd, &sbuf) < 0) {
119 fprintf(stderr, "%s: Can't stat %s: %s\n",
120 params->cmdname, fname, strerror(errno));
124 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
127 ret = read(fd, ptr, sbuf.st_size);
128 if (ret != sbuf.st_size) {
129 fprintf(stderr, "%s: Can't read %s: %s\n",
130 params->cmdname, fname, strerror(errno));
141 static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
147 vsnprintf(str, sizeof(str), fmt, ptr);
149 return fdt_property_string(fdt, name, str);
152 static void get_basename(char *str, int size, const char *fname)
154 const char *p, *start, *end;
158 * Use the base name as the 'name' field. So for example:
160 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
161 * becomes "sun7i-a20-bananapro"
163 p = strrchr(fname, '/');
164 start = p ? p + 1 : fname;
165 p = strrchr(fname, '.');
166 end = p ? p : fname + strlen(fname);
170 memcpy(str, start, len);
175 * fit_write_images() - Write out a list of images to the FIT
177 * We always include the main image (params->datafile). If there are device
178 * tree files, we include an fdt@ node for each of those too.
180 static int fit_write_images(struct image_tool_params *params, char *fdt)
182 struct content_info *cont;
183 const char *typename;
188 fdt_begin_node(fdt, "images");
190 /* First the main image */
191 typename = genimg_get_type_short_name(params->fit_image_type);
192 snprintf(str, sizeof(str), "%s@1", typename);
193 fdt_begin_node(fdt, str);
194 fdt_property_string(fdt, "description", params->imagename);
195 fdt_property_string(fdt, "type", typename);
196 fdt_property_string(fdt, "arch", genimg_get_arch_name(params->arch));
197 fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os));
198 fdt_property_string(fdt, "compression",
199 genimg_get_comp_short_name(params->comp));
200 fdt_property_u32(fdt, "load", params->addr);
201 fdt_property_u32(fdt, "entry", params->ep);
204 * Put data last since it is large. SPL may only load the first part
205 * of the DT, so this way it can access all the above fields.
207 ret = fdt_property_file(params, fdt, "data", params->datafile);
212 /* Now the device tree files if available */
214 for (cont = params->content_head; cont; cont = cont->next) {
215 if (cont->type != IH_TYPE_FLATDT)
217 snprintf(str, sizeof(str), "%s@%d", FIT_FDT_PROP, ++upto);
218 fdt_begin_node(fdt, str);
220 get_basename(str, sizeof(str), cont->fname);
221 fdt_property_string(fdt, "description", str);
222 ret = fdt_property_file(params, fdt, "data", cont->fname);
225 fdt_property_string(fdt, "type", typename);
226 fdt_property_string(fdt, "arch",
227 genimg_get_arch_short_name(params->arch));
228 fdt_property_string(fdt, "compression",
229 genimg_get_comp_short_name(IH_COMP_NONE));
239 * fit_write_configs() - Write out a list of configurations to the FIT
241 * If there are device tree files, we include a configuration for each, which
242 * selects the main image (params->datafile) and its corresponding device
245 * Otherwise we just create a configuration with the main image in it.
247 static void fit_write_configs(struct image_tool_params *params, char *fdt)
249 struct content_info *cont;
250 const char *typename;
254 fdt_begin_node(fdt, "configurations");
255 fdt_property_string(fdt, "default", "conf@1");
258 for (cont = params->content_head; cont; cont = cont->next) {
259 if (cont->type != IH_TYPE_FLATDT)
261 typename = genimg_get_type_short_name(cont->type);
262 snprintf(str, sizeof(str), "conf@%d", ++upto);
263 fdt_begin_node(fdt, str);
265 get_basename(str, sizeof(str), cont->fname);
266 fdt_property_string(fdt, "description", str);
268 typename = genimg_get_type_short_name(params->fit_image_type);
269 snprintf(str, sizeof(str), "%s@1", typename);
270 fdt_property_string(fdt, typename, str);
272 snprintf(str, sizeof(str), FIT_FDT_PROP "@%d", upto);
273 fdt_property_string(fdt, FIT_FDT_PROP, str);
277 fdt_begin_node(fdt, "conf@1");
278 typename = genimg_get_type_short_name(params->fit_image_type);
279 snprintf(str, sizeof(str), "%s@1", typename);
280 fdt_property_string(fdt, typename, str);
287 static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
291 ret = fdt_create(fdt, size);
294 fdt_finish_reservemap(fdt);
295 fdt_begin_node(fdt, "");
296 fdt_property_strf(fdt, "description",
297 "%s image with one or more FDT blobs",
298 genimg_get_type_name(params->fit_image_type));
299 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
300 fdt_property_u32(fdt, "#address-cells", 1);
301 ret = fit_write_images(params, fdt);
304 fit_write_configs(params, fdt);
306 ret = fdt_finish(fdt);
310 return fdt_totalsize(fdt);
313 static int fit_build(struct image_tool_params *params, const char *fname)
320 size = fit_calc_size(params);
325 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
326 params->cmdname, size);
329 ret = fit_build_fdt(params, buf, size);
331 fprintf(stderr, "%s: Failed to build FIT image\n",
336 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
338 fprintf(stderr, "%s: Can't open %s: %s\n",
339 params->cmdname, fname, strerror(errno));
342 ret = write(fd, buf, size);
344 fprintf(stderr, "%s: Can't write %s: %s\n",
345 params->cmdname, fname, strerror(errno));
360 * fit_extract_data() - Move all data outside the FIT
362 * This takes a normal FIT file and removes all the 'data' properties from it.
363 * The data is placed in an area after the FIT so that it can be accessed
364 * using an offset into that area. The 'data' properties turn into
365 * 'data-offset' properties.
367 * This function cannot cope with FITs with 'data-offset' properties. All
368 * data must be in 'data' properties on entry.
370 static int fit_extract_data(struct image_tool_params *params, const char *fname)
374 int fit_size, new_size;
382 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false);
385 fit_size = fdt_totalsize(fdt);
387 /* Allocate space to hold the image data we will extract */
388 buf = malloc(fit_size);
395 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
397 debug("%s: Cannot find /images node: %d\n", __func__, images);
402 for (node = fdt_first_subnode(fdt, images);
404 node = fdt_next_subnode(fdt, node)) {
408 data = fdt_getprop(fdt, node, "data", &len);
411 memcpy(buf + buf_ptr, data, len);
412 debug("Extracting data size %x\n", len);
414 ret = fdt_delprop(fdt, node, "data");
419 fdt_setprop_u32(fdt, node, "data-offset", buf_ptr);
420 fdt_setprop_u32(fdt, node, "data-size", len);
422 buf_ptr += (len + 3) & ~3;
425 /* Pack the FDT and place the data after it */
428 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
429 debug("External data size %x\n", buf_ptr);
430 new_size = fdt_totalsize(fdt);
431 new_size = (new_size + 3) & ~3;
432 munmap(fdt, sbuf.st_size);
434 if (ftruncate(fd, new_size)) {
435 debug("%s: Failed to truncate file: %s\n", __func__,
440 if (lseek(fd, new_size, SEEK_SET) < 0) {
441 debug("%s: Failed to seek to end of file: %s\n", __func__,
446 if (write(fd, buf, buf_ptr) != buf_ptr) {
447 debug("%s: Failed to write external data to file %s\n",
448 __func__, strerror(errno));
456 munmap(fdt, sbuf.st_size);
464 static int fit_import_data(struct image_tool_params *params, const char *fname)
467 int fit_size, new_size, size, data_base;
474 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false);
477 fit_size = fdt_totalsize(old_fdt);
478 data_base = (fit_size + 3) & ~3;
480 /* Allocate space to hold the new FIT */
481 size = sbuf.st_size + 16384;
484 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
489 ret = fdt_open_into(old_fdt, fdt, size);
491 debug("%s: Failed to expand FIT: %s\n", __func__,
492 fdt_strerror(errno));
497 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
499 debug("%s: Cannot find /images node: %d\n", __func__, images);
504 for (node = fdt_first_subnode(fdt, images);
506 node = fdt_next_subnode(fdt, node)) {
510 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
511 len = fdtdec_get_int(fdt, node, "data-size", -1);
512 if (buf_ptr == -1 || len == -1)
514 debug("Importing data size %x\n", len);
516 ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
519 debug("%s: Failed to write property: %s\n", __func__,
526 munmap(old_fdt, sbuf.st_size);
529 /* Pack the FDT and place the data after it */
532 new_size = fdt_totalsize(fdt);
533 debug("Size expanded from %x to %x\n", fit_size, new_size);
535 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
537 fprintf(stderr, "%s: Can't open %s: %s\n",
538 params->cmdname, fname, strerror(errno));
542 if (write(fd, fdt, new_size) != new_size) {
543 debug("%s: Failed to write external data to file %s\n",
544 __func__, strerror(errno));
558 * fit_handle_file - main FIT file processing function
560 * fit_handle_file() runs dtc to convert .its to .itb, includes
561 * binary data, updates timestamp property and calculates hashes.
563 * datafile - .its file
564 * imagefile - .itb file
567 * only on success, otherwise calls exit (EXIT_FAILURE);
569 static int fit_handle_file(struct image_tool_params *params)
571 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
572 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
576 /* Flattened Image Tree (FIT) format handling */
577 debug ("FIT format handling\n");
579 /* call dtc to include binary properties into the tmp file */
580 if (strlen (params->imagefile) +
581 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
582 fprintf (stderr, "%s: Image file name (%s) too long, "
583 "can't create tmpfile",
584 params->imagefile, params->cmdname);
585 return (EXIT_FAILURE);
587 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
589 /* We either compile the source file, or use the existing FIT image */
590 if (params->auto_its) {
591 if (fit_build(params, tmpfile)) {
592 fprintf(stderr, "%s: failed to build FIT\n",
597 } else if (params->datafile) {
598 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
599 snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
600 MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
601 debug("Trying to execute \"%s\"\n", cmd);
603 snprintf(cmd, sizeof(cmd), "cp %s %s",
604 params->imagefile, tmpfile);
606 if (*cmd && system(cmd) == -1) {
607 fprintf (stderr, "%s: system(%s) failed: %s\n",
608 params->cmdname, cmd, strerror(errno));
612 /* Move the data so it is internal to the FIT, if needed */
613 ret = fit_import_data(params, tmpfile);
618 * Set hashes for images in the blob. Unfortunately we may need more
619 * space in either FDT, so keep trying until we succeed.
621 * Note: this is pretty inefficient for signing, since we must
622 * calculate the signature every time. It would be better to calculate
623 * all the data and then store it in a separate step. However, this
624 * would be considerably more complex to implement. Generally a few
625 * steps of this loop is enough to sign with several keys.
627 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
628 ret = fit_add_file_data(params, size_inc, tmpfile);
629 if (!ret || ret != -ENOSPC)
634 fprintf(stderr, "%s Can't add hashes to FIT blob\n",
639 /* Move the data so it is external to the FIT, if requested */
640 if (params->external_data) {
641 ret = fit_extract_data(params, tmpfile);
646 if (rename (tmpfile, params->imagefile) == -1) {
647 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
648 params->cmdname, tmpfile, params->imagefile,
651 unlink (params->imagefile);
662 * fit_image_extract - extract a FIT component image
663 * @fit: pointer to the FIT format image header
664 * @image_noffset: offset of the component image node
665 * @file_name: name of the file to store the FIT sub-image
668 * zero in case of success or a negative value if fail.
670 static int fit_image_extract(
673 const char *file_name)
675 const void *file_data;
676 size_t file_size = 0;
678 /* get the "data" property of component at offset "image_noffset" */
679 fit_image_get_data(fit, image_noffset, &file_data, &file_size);
681 /* save the "file_data" into the file specified by "file_name" */
682 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
686 * fit_extract_contents - retrieve a sub-image component from the FIT image
687 * @ptr: pointer to the FIT format image header
688 * @params: command line parameters
691 * zero in case of success or a negative value if fail.
693 static int fit_extract_contents(void *ptr, struct image_tool_params *params)
698 const void *fit = ptr;
702 /* Indent string is defined in header image.h */
703 p = IMAGE_INDENT_STRING;
705 if (!fit_check_format(fit)) {
706 printf("Bad FIT image format\n");
710 /* Find images parent node offset */
711 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
712 if (images_noffset < 0) {
713 printf("Can't find images parent node '%s' (%s)\n",
714 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
718 /* Avoid any overrun */
719 count = fit_get_subimage_count(fit, images_noffset);
720 if ((params->pflag < 0) || (count <= params->pflag)) {
721 printf("No such component at '%d'\n", params->pflag);
725 /* Process its subnodes, extract the desired component from image */
726 for (ndepth = 0, count = 0,
727 noffset = fdt_next_node(fit, images_noffset, &ndepth);
728 (noffset >= 0) && (ndepth > 0);
729 noffset = fdt_next_node(fit, noffset, &ndepth)) {
732 * Direct child node of the images parent node,
733 * i.e. component image node.
735 if (params->pflag == count) {
736 printf("Extracted:\n%s Image %u (%s)\n", p,
737 count, fit_get_name(fit, noffset, NULL));
739 fit_image_print(fit, noffset, p);
741 return fit_image_extract(fit, noffset,
752 static int fit_check_params(struct image_tool_params *params)
754 if (params->auto_its)
756 return ((params->dflag && (params->fflag || params->lflag)) ||
757 (params->fflag && (params->dflag || params->lflag)) ||
758 (params->lflag && (params->dflag || params->fflag)));
764 sizeof(image_header_t),
770 fit_extract_contents,
771 fit_check_image_types,
773 NULL /* FIT images use DTB header */