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
));
140 static int fdt_property_strf(void *fdt
, const char *name
, const char *fmt
, ...)
146 vsnprintf(str
, sizeof(str
), fmt
, ptr
);
148 return fdt_property_string(fdt
, name
, str
);
151 static void get_basename(char *str
, int size
, const char *fname
)
153 const char *p
, *start
, *end
;
157 * Use the base name as the 'name' field. So for example:
159 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
160 * becomes "sun7i-a20-bananapro"
162 p
= strrchr(fname
, '/');
163 start
= p
? p
+ 1 : fname
;
164 p
= strrchr(fname
, '.');
165 end
= p
? p
: fname
+ strlen(fname
);
169 memcpy(str
, start
, len
);
174 * fit_write_images() - Write out a list of images to the FIT
176 * We always include the main image (params->datafile). If there are device
177 * tree files, we include an fdt@ node for each of those too.
179 static int fit_write_images(struct image_tool_params
*params
, char *fdt
)
181 struct content_info
*cont
;
182 const char *typename
;
187 fdt_begin_node(fdt
, "images");
189 /* First the main image */
190 typename
= genimg_get_type_short_name(params
->fit_image_type
);
191 snprintf(str
, sizeof(str
), "%s@1", typename
);
192 fdt_begin_node(fdt
, str
);
193 fdt_property_string(fdt
, "description", params
->imagename
);
194 fdt_property_string(fdt
, "type", typename
);
195 fdt_property_string(fdt
, "arch", genimg_get_arch_name(params
->arch
));
196 fdt_property_string(fdt
, "os", genimg_get_os_short_name(params
->os
));
197 fdt_property_string(fdt
, "compression",
198 genimg_get_comp_short_name(params
->comp
));
199 fdt_property_u32(fdt
, "load", params
->addr
);
200 fdt_property_u32(fdt
, "entry", params
->ep
);
203 * Put data last since it is large. SPL may only load the first part
204 * of the DT, so this way it can access all the above fields.
206 ret
= fdt_property_file(params
, fdt
, "data", params
->datafile
);
211 /* Now the device tree files if available */
213 for (cont
= params
->content_head
; cont
; cont
= cont
->next
) {
214 if (cont
->type
!= IH_TYPE_FLATDT
)
216 snprintf(str
, sizeof(str
), "%s@%d", FIT_FDT_PROP
, ++upto
);
217 fdt_begin_node(fdt
, str
);
219 get_basename(str
, sizeof(str
), cont
->fname
);
220 fdt_property_string(fdt
, "description", str
);
221 ret
= fdt_property_file(params
, fdt
, "data", cont
->fname
);
224 fdt_property_string(fdt
, "type", typename
);
225 fdt_property_string(fdt
, "arch",
226 genimg_get_arch_short_name(params
->arch
));
227 fdt_property_string(fdt
, "compression",
228 genimg_get_comp_short_name(IH_COMP_NONE
));
238 * fit_write_configs() - Write out a list of configurations to the FIT
240 * If there are device tree files, we include a configuration for each, which
241 * selects the main image (params->datafile) and its corresponding device
244 * Otherwise we just create a configuration with the main image in it.
246 static void fit_write_configs(struct image_tool_params
*params
, char *fdt
)
248 struct content_info
*cont
;
249 const char *typename
;
253 fdt_begin_node(fdt
, "configurations");
254 fdt_property_string(fdt
, "default", "conf@1");
257 for (cont
= params
->content_head
; cont
; cont
= cont
->next
) {
258 if (cont
->type
!= IH_TYPE_FLATDT
)
260 typename
= genimg_get_type_short_name(cont
->type
);
261 snprintf(str
, sizeof(str
), "conf@%d", ++upto
);
262 fdt_begin_node(fdt
, str
);
264 get_basename(str
, sizeof(str
), cont
->fname
);
265 fdt_property_string(fdt
, "description", str
);
267 typename
= genimg_get_type_short_name(params
->fit_image_type
);
268 snprintf(str
, sizeof(str
), "%s@1", typename
);
269 fdt_property_string(fdt
, typename
, str
);
271 snprintf(str
, sizeof(str
), FIT_FDT_PROP
"@%d", upto
);
272 fdt_property_string(fdt
, FIT_FDT_PROP
, str
);
276 fdt_begin_node(fdt
, "conf@1");
277 typename
= genimg_get_type_short_name(params
->fit_image_type
);
278 snprintf(str
, sizeof(str
), "%s@1", typename
);
279 fdt_property_string(fdt
, typename
, str
);
286 static int fit_build_fdt(struct image_tool_params
*params
, char *fdt
, int size
)
290 ret
= fdt_create(fdt
, size
);
293 fdt_finish_reservemap(fdt
);
294 fdt_begin_node(fdt
, "");
295 fdt_property_strf(fdt
, "description",
296 "%s image with one or more FDT blobs",
297 genimg_get_type_name(params
->fit_image_type
));
298 fdt_property_strf(fdt
, "creator", "U-Boot mkimage %s", PLAIN_VERSION
);
299 fdt_property_u32(fdt
, "#address-cells", 1);
300 ret
= fit_write_images(params
, fdt
);
303 fit_write_configs(params
, fdt
);
305 ret
= fdt_finish(fdt
);
309 return fdt_totalsize(fdt
);
312 static int fit_build(struct image_tool_params
*params
, const char *fname
)
319 size
= fit_calc_size(params
);
324 fprintf(stderr
, "%s: Out of memory (%d bytes)\n",
325 params
->cmdname
, size
);
328 ret
= fit_build_fdt(params
, buf
, size
);
330 fprintf(stderr
, "%s: Failed to build FIT image\n",
335 fd
= open(fname
, O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
, 0666);
337 fprintf(stderr
, "%s: Can't open %s: %s\n",
338 params
->cmdname
, fname
, strerror(errno
));
341 ret
= write(fd
, buf
, size
);
343 fprintf(stderr
, "%s: Can't write %s: %s\n",
344 params
->cmdname
, fname
, strerror(errno
));
357 * fit_extract_data() - Move all data outside the FIT
359 * This takes a normal FIT file and removes all the 'data' properties from it.
360 * The data is placed in an area after the FIT so that it can be accessed
361 * using an offset into that area. The 'data' properties turn into
362 * 'data-offset' properties.
364 * This function cannot cope with FITs with 'data-offset' properties. All
365 * data must be in 'data' properties on entry.
367 static int fit_extract_data(struct image_tool_params
*params
, const char *fname
)
371 int fit_size
, new_size
;
379 fd
= mmap_fdt(params
->cmdname
, fname
, 0, &fdt
, &sbuf
, false);
382 fit_size
= fdt_totalsize(fdt
);
384 /* Allocate space to hold the image data we will extract */
385 buf
= malloc(fit_size
);
392 images
= fdt_path_offset(fdt
, FIT_IMAGES_PATH
);
394 debug("%s: Cannot find /images node: %d\n", __func__
, images
);
399 for (node
= fdt_first_subnode(fdt
, images
);
401 node
= fdt_next_subnode(fdt
, node
)) {
405 data
= fdt_getprop(fdt
, node
, "data", &len
);
408 memcpy(buf
+ buf_ptr
, data
, len
);
409 debug("Extracting data size %x\n", len
);
411 ret
= fdt_delprop(fdt
, node
, "data");
416 fdt_setprop_u32(fdt
, node
, "data-offset", buf_ptr
);
417 fdt_setprop_u32(fdt
, node
, "data-size", len
);
419 buf_ptr
+= (len
+ 3) & ~3;
422 /* Pack the FDT and place the data after it */
425 debug("Size reduced from %x to %x\n", fit_size
, fdt_totalsize(fdt
));
426 debug("External data size %x\n", buf_ptr
);
427 new_size
= fdt_totalsize(fdt
);
428 new_size
= (new_size
+ 3) & ~3;
429 munmap(fdt
, sbuf
.st_size
);
431 if (ftruncate(fd
, new_size
)) {
432 debug("%s: Failed to truncate file: %s\n", __func__
,
437 if (lseek(fd
, new_size
, SEEK_SET
) < 0) {
438 debug("%s: Failed to seek to end of file: %s\n", __func__
,
443 if (write(fd
, buf
, buf_ptr
) != buf_ptr
) {
444 debug("%s: Failed to write external data to file %s\n",
445 __func__
, strerror(errno
));
459 * fit_handle_file - main FIT file processing function
461 * fit_handle_file() runs dtc to convert .its to .itb, includes
462 * binary data, updates timestamp property and calculates hashes.
464 * datafile - .its file
465 * imagefile - .itb file
468 * only on success, otherwise calls exit (EXIT_FAILURE);
470 static int fit_handle_file(struct image_tool_params
*params
)
472 char tmpfile
[MKIMAGE_MAX_TMPFILE_LEN
];
473 char cmd
[MKIMAGE_MAX_DTC_CMDLINE_LEN
];
477 /* Flattened Image Tree (FIT) format handling */
478 debug ("FIT format handling\n");
480 /* call dtc to include binary properties into the tmp file */
481 if (strlen (params
->imagefile
) +
482 strlen (MKIMAGE_TMPFILE_SUFFIX
) + 1 > sizeof (tmpfile
)) {
483 fprintf (stderr
, "%s: Image file name (%s) too long, "
484 "can't create tmpfile",
485 params
->imagefile
, params
->cmdname
);
486 return (EXIT_FAILURE
);
488 sprintf (tmpfile
, "%s%s", params
->imagefile
, MKIMAGE_TMPFILE_SUFFIX
);
490 /* We either compile the source file, or use the existing FIT image */
491 if (params
->auto_its
) {
492 if (fit_build(params
, tmpfile
)) {
493 fprintf(stderr
, "%s: failed to build FIT\n",
498 } else if (params
->datafile
) {
499 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
500 snprintf(cmd
, sizeof(cmd
), "%s %s %s > %s",
501 MKIMAGE_DTC
, params
->dtc
, params
->datafile
, tmpfile
);
502 debug("Trying to execute \"%s\"\n", cmd
);
504 snprintf(cmd
, sizeof(cmd
), "cp %s %s",
505 params
->imagefile
, tmpfile
);
507 if (*cmd
&& system(cmd
) == -1) {
508 fprintf (stderr
, "%s: system(%s) failed: %s\n",
509 params
->cmdname
, cmd
, strerror(errno
));
514 * Set hashes for images in the blob. Unfortunately we may need more
515 * space in either FDT, so keep trying until we succeed.
517 * Note: this is pretty inefficient for signing, since we must
518 * calculate the signature every time. It would be better to calculate
519 * all the data and then store it in a separate step. However, this
520 * would be considerably more complex to implement. Generally a few
521 * steps of this loop is enough to sign with several keys.
523 for (size_inc
= 0; size_inc
< 64 * 1024; size_inc
+= 1024) {
524 ret
= fit_add_file_data(params
, size_inc
, tmpfile
);
525 if (!ret
|| ret
!= -ENOSPC
)
530 fprintf(stderr
, "%s Can't add hashes to FIT blob\n",
535 /* Move the data so it is external to the FIT, if requested */
536 if (params
->external_data
) {
537 ret
= fit_extract_data(params
, tmpfile
);
542 if (rename (tmpfile
, params
->imagefile
) == -1) {
543 fprintf (stderr
, "%s: Can't rename %s to %s: %s\n",
544 params
->cmdname
, tmpfile
, params
->imagefile
,
547 unlink (params
->imagefile
);
558 * fit_image_extract - extract a FIT component image
559 * @fit: pointer to the FIT format image header
560 * @image_noffset: offset of the component image node
561 * @file_name: name of the file to store the FIT sub-image
564 * zero in case of success or a negative value if fail.
566 static int fit_image_extract(
569 const char *file_name
)
571 const void *file_data
;
572 size_t file_size
= 0;
574 /* get the "data" property of component at offset "image_noffset" */
575 fit_image_get_data(fit
, image_noffset
, &file_data
, &file_size
);
577 /* save the "file_data" into the file specified by "file_name" */
578 return imagetool_save_subimage(file_name
, (ulong
) file_data
, file_size
);
582 * fit_extract_contents - retrieve a sub-image component from the FIT image
583 * @ptr: pointer to the FIT format image header
584 * @params: command line parameters
587 * zero in case of success or a negative value if fail.
589 static int fit_extract_contents(void *ptr
, struct image_tool_params
*params
)
594 const void *fit
= ptr
;
598 /* Indent string is defined in header image.h */
599 p
= IMAGE_INDENT_STRING
;
601 if (!fit_check_format(fit
)) {
602 printf("Bad FIT image format\n");
606 /* Find images parent node offset */
607 images_noffset
= fdt_path_offset(fit
, FIT_IMAGES_PATH
);
608 if (images_noffset
< 0) {
609 printf("Can't find images parent node '%s' (%s)\n",
610 FIT_IMAGES_PATH
, fdt_strerror(images_noffset
));
614 /* Avoid any overrun */
615 count
= fit_get_subimage_count(fit
, images_noffset
);
616 if ((params
->pflag
< 0) || (count
<= params
->pflag
)) {
617 printf("No such component at '%d'\n", params
->pflag
);
621 /* Process its subnodes, extract the desired component from image */
622 for (ndepth
= 0, count
= 0,
623 noffset
= fdt_next_node(fit
, images_noffset
, &ndepth
);
624 (noffset
>= 0) && (ndepth
> 0);
625 noffset
= fdt_next_node(fit
, noffset
, &ndepth
)) {
628 * Direct child node of the images parent node,
629 * i.e. component image node.
631 if (params
->pflag
== count
) {
632 printf("Extracted:\n%s Image %u (%s)\n", p
,
633 count
, fit_get_name(fit
, noffset
, NULL
));
635 fit_image_print(fit
, noffset
, p
);
637 return fit_image_extract(fit
, noffset
,
648 static int fit_check_params(struct image_tool_params
*params
)
650 if (params
->auto_its
)
652 return ((params
->dflag
&& (params
->fflag
|| params
->lflag
)) ||
653 (params
->fflag
&& (params
->dflag
|| params
->lflag
)) ||
654 (params
->lflag
&& (params
->dflag
|| params
->fflag
)));
660 sizeof(image_header_t
),
666 fit_extract_contents
,
667 fit_check_image_types
,
669 NULL
/* FIT images use DTB header */