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)
82 size = imagetool_get_filesize(params, params->datafile);
88 /* TODO(sjg@chromium.org): Add in the size of any other files */
90 /* Add plenty of space for headers, properties, nodes, etc. */
96 static int fdt_property_file(struct image_tool_params *params,
97 void *fdt, const char *name, const char *fname)
104 fd = open(fname, O_RDWR | O_BINARY);
106 fprintf(stderr, "%s: Can't open %s: %s\n",
107 params->cmdname, fname, strerror(errno));
111 if (fstat(fd, &sbuf) < 0) {
112 fprintf(stderr, "%s: Can't stat %s: %s\n",
113 params->cmdname, fname, strerror(errno));
117 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
120 ret = read(fd, ptr, sbuf.st_size);
121 if (ret != sbuf.st_size) {
122 fprintf(stderr, "%s: Can't read %s: %s\n",
123 params->cmdname, fname, strerror(errno));
133 static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
139 vsnprintf(str, sizeof(str), fmt, ptr);
141 return fdt_property_string(fdt, name, str);
145 * fit_write_images() - Write out a list of images to the FIT
147 * Include the main image (params->datafile).
149 static int fit_write_images(struct image_tool_params *params, char *fdt)
151 const char *typename;
155 fdt_begin_node(fdt, "images");
157 /* First the main image */
158 typename = genimg_get_type_short_name(params->fit_image_type);
159 snprintf(str, sizeof(str), "%s@1", typename);
160 fdt_begin_node(fdt, str);
161 fdt_property_string(fdt, "description", params->imagename);
162 fdt_property_string(fdt, "type", typename);
163 fdt_property_string(fdt, "arch", genimg_get_arch_name(params->arch));
164 fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os));
165 fdt_property_string(fdt, "compression",
166 genimg_get_comp_short_name(params->comp));
167 fdt_property_u32(fdt, "load", params->addr);
168 fdt_property_u32(fdt, "entry", params->ep);
171 * Put data last since it is large. SPL may only load the first part
172 * of the DT, so this way it can access all the above fields.
174 ret = fdt_property_file(params, fdt, "data", params->datafile);
185 * fit_write_configs() - Write out a list of configurations to the FIT
187 * Create a configuration with the main image in it.
189 static void fit_write_configs(struct image_tool_params *params, char *fdt)
191 const char *typename;
194 fdt_begin_node(fdt, "configurations");
195 fdt_property_string(fdt, "default", "conf@1");
197 fdt_begin_node(fdt, "conf@1");
198 typename = genimg_get_type_short_name(params->fit_image_type);
199 snprintf(str, sizeof(str), "%s@1", typename);
200 fdt_property_string(fdt, typename, str);
206 static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
210 ret = fdt_create(fdt, size);
213 fdt_finish_reservemap(fdt);
214 fdt_begin_node(fdt, "");
215 fdt_property_strf(fdt, "description",
216 "%s image with one or more FDT blobs",
217 genimg_get_type_name(params->fit_image_type));
218 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
219 fdt_property_u32(fdt, "#address-cells", 1);
220 ret = fit_write_images(params, fdt);
223 fit_write_configs(params, fdt);
225 ret = fdt_finish(fdt);
229 return fdt_totalsize(fdt);
232 static int fit_build(struct image_tool_params *params, const char *fname)
239 size = fit_calc_size(params);
244 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
245 params->cmdname, size);
248 ret = fit_build_fdt(params, buf, size);
250 fprintf(stderr, "%s: Failed to build FIT image\n",
255 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
257 fprintf(stderr, "%s: Can't open %s: %s\n",
258 params->cmdname, fname, strerror(errno));
261 ret = write(fd, buf, size);
263 fprintf(stderr, "%s: Can't write %s: %s\n",
264 params->cmdname, fname, strerror(errno));
277 * fit_handle_file - main FIT file processing function
279 * fit_handle_file() runs dtc to convert .its to .itb, includes
280 * binary data, updates timestamp property and calculates hashes.
282 * datafile - .its file
283 * imagefile - .itb file
286 * only on success, otherwise calls exit (EXIT_FAILURE);
288 static int fit_handle_file(struct image_tool_params *params)
290 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
291 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
295 /* Flattened Image Tree (FIT) format handling */
296 debug ("FIT format handling\n");
298 /* call dtc to include binary properties into the tmp file */
299 if (strlen (params->imagefile) +
300 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
301 fprintf (stderr, "%s: Image file name (%s) too long, "
302 "can't create tmpfile",
303 params->imagefile, params->cmdname);
304 return (EXIT_FAILURE);
306 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
308 /* We either compile the source file, or use the existing FIT image */
309 if (params->auto_its) {
310 if (fit_build(params, tmpfile)) {
311 fprintf(stderr, "%s: failed to build FIT\n",
316 } else if (params->datafile) {
317 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
318 snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
319 MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
320 debug("Trying to execute \"%s\"\n", cmd);
322 snprintf(cmd, sizeof(cmd), "cp %s %s",
323 params->imagefile, tmpfile);
325 if (*cmd && system(cmd) == -1) {
326 fprintf (stderr, "%s: system(%s) failed: %s\n",
327 params->cmdname, cmd, strerror(errno));
332 * Set hashes for images in the blob. Unfortunately we may need more
333 * space in either FDT, so keep trying until we succeed.
335 * Note: this is pretty inefficient for signing, since we must
336 * calculate the signature every time. It would be better to calculate
337 * all the data and then store it in a separate step. However, this
338 * would be considerably more complex to implement. Generally a few
339 * steps of this loop is enough to sign with several keys.
341 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
342 ret = fit_add_file_data(params, size_inc, tmpfile);
343 if (!ret || ret != -ENOSPC)
348 fprintf(stderr, "%s Can't add hashes to FIT blob\n",
353 if (rename (tmpfile, params->imagefile) == -1) {
354 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
355 params->cmdname, tmpfile, params->imagefile,
358 unlink (params->imagefile);
369 * fit_image_extract - extract a FIT component image
370 * @fit: pointer to the FIT format image header
371 * @image_noffset: offset of the component image node
372 * @file_name: name of the file to store the FIT sub-image
375 * zero in case of success or a negative value if fail.
377 static int fit_image_extract(
380 const char *file_name)
382 const void *file_data;
383 size_t file_size = 0;
385 /* get the "data" property of component at offset "image_noffset" */
386 fit_image_get_data(fit, image_noffset, &file_data, &file_size);
388 /* save the "file_data" into the file specified by "file_name" */
389 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
393 * fit_extract_contents - retrieve a sub-image component from the FIT image
394 * @ptr: pointer to the FIT format image header
395 * @params: command line parameters
398 * zero in case of success or a negative value if fail.
400 static int fit_extract_contents(void *ptr, struct image_tool_params *params)
405 const void *fit = ptr;
409 /* Indent string is defined in header image.h */
410 p = IMAGE_INDENT_STRING;
412 if (!fit_check_format(fit)) {
413 printf("Bad FIT image format\n");
417 /* Find images parent node offset */
418 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
419 if (images_noffset < 0) {
420 printf("Can't find images parent node '%s' (%s)\n",
421 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
425 /* Avoid any overrun */
426 count = fit_get_subimage_count(fit, images_noffset);
427 if ((params->pflag < 0) || (count <= params->pflag)) {
428 printf("No such component at '%d'\n", params->pflag);
432 /* Process its subnodes, extract the desired component from image */
433 for (ndepth = 0, count = 0,
434 noffset = fdt_next_node(fit, images_noffset, &ndepth);
435 (noffset >= 0) && (ndepth > 0);
436 noffset = fdt_next_node(fit, noffset, &ndepth)) {
439 * Direct child node of the images parent node,
440 * i.e. component image node.
442 if (params->pflag == count) {
443 printf("Extracted:\n%s Image %u (%s)\n", p,
444 count, fit_get_name(fit, noffset, NULL));
446 fit_image_print(fit, noffset, p);
448 return fit_image_extract(fit, noffset,
459 static int fit_check_params(struct image_tool_params *params)
461 if (params->auto_its)
463 return ((params->dflag && (params->fflag || params->lflag)) ||
464 (params->fflag && (params->dflag || params->lflag)) ||
465 (params->lflag && (params->dflag || params->fflag)));
471 sizeof(image_header_t),
477 fit_extract_contents,
478 fit_check_image_types,
480 NULL /* FIT images use DTB header */