blob: 8717dc9a3b1c7b4f8b46b09ecbc3991eebfa78e9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +05302/*
3 * (C) Copyright 2008 Semihalf
4 *
5 * (C) Copyright 2000-2004
6 * DENX Software Engineering
7 * Wolfgang Denk, wd@denx.de
8 *
9 * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
10 * FIT image specific code abstracted from mkimage.c
11 * some functions added to address abstraction
12 *
13 * All rights reserved.
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053014 */
15
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070016#include "imagetool.h"
Heiko Schocher3cb743c2014-03-03 12:19:29 +010017#include "fit_common.h"
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053018#include "mkimage.h"
19#include <image.h>
Sven Roederercbcb3992020-04-27 02:08:39 +020020#include <string.h>
Simon Glass88e31cb2016-02-22 22:55:51 -070021#include <stdarg.h>
22#include <version.h>
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053023#include <u-boot/crc.h>
24
Simon Glassbb7d3bb2022-09-06 20:26:52 -060025static struct legacy_img_hdr header;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053026
Rasmus Villemoes9cc19702025-06-10 14:27:48 +020027static int fit_estimate_hash_sig_size(struct image_tool_params *params, const char *fname)
28{
29 bool signing = IMAGE_ENABLE_SIGN && (params->keydir || params->keyfile);
30 struct stat sbuf;
31 void *fdt;
32 int fd;
33 int estimate = 0;
34 int depth, noffset;
35 const char *name;
36
37 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, true);
38 if (fd < 0)
39 return -EIO;
40
41 /*
42 * Walk the FIT image, looking for nodes named hash* and
43 * signature*. Since the interesting nodes are subnodes of an
44 * image or configuration node, we are only interested in
45 * those at depth exactly 3.
46 *
47 * The estimate for a hash node is based on a sha512 digest
48 * being 64 bytes, with another 64 bytes added to account for
49 * fdt structure overhead (the tags and the name of the
50 * "value" property).
51 *
52 * The estimate for a signature node is based on an rsa4096
53 * signature being 512 bytes, with another 512 bytes to
54 * account for fdt overhead and the various other properties
55 * (hashed-nodes etc.) that will also be filled in.
56 *
57 * One could try to be more precise in the estimates by
58 * looking at the "algo" property and, in the case of
59 * configuration signatures, the sign-images property. Also,
60 * when signing an already created FIT image, the hash nodes
61 * already have properly sized value properties, so one could
62 * also take pre-existence of "value" properties in hash nodes
63 * into account. But this rather simple approach should work
64 * well enough in practice.
65 */
66 for (depth = 0, noffset = fdt_next_node(fdt, 0, &depth);
67 noffset >= 0 && depth > 0;
68 noffset = fdt_next_node(fdt, noffset, &depth)) {
69 if (depth != 3)
70 continue;
71
72 name = fdt_get_name(fdt, noffset, NULL);
73 if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME)))
74 estimate += 128;
75
76 if (signing && !strncmp(name, FIT_SIG_NODENAME, strlen(FIT_SIG_NODENAME)))
77 estimate += 1024;
78 }
79
80 munmap(fdt, sbuf.st_size);
81 close(fd);
82
83 return estimate;
84}
85
Simon Glass802aa822014-06-02 22:04:53 -060086static int fit_add_file_data(struct image_tool_params *params, size_t size_inc,
87 const char *tmpfile)
88{
89 int tfd, destfd = 0;
90 void *dest_blob = NULL;
91 off_t destfd_size = 0;
92 struct stat sbuf;
93 void *ptr;
94 int ret = 0;
95
Luca Boccassibff733f2019-05-14 19:35:02 +010096 tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true,
97 false);
Simon Glass35414072022-12-21 16:08:23 -070098 if (tfd < 0) {
99 fprintf(stderr, "Cannot map FDT file '%s'\n", tmpfile);
Simon Glass802aa822014-06-02 22:04:53 -0600100 return -EIO;
Simon Glass35414072022-12-21 16:08:23 -0700101 }
Simon Glass802aa822014-06-02 22:04:53 -0600102
103 if (params->keydest) {
104 struct stat dest_sbuf;
105
106 destfd = mmap_fdt(params->cmdname, params->keydest, size_inc,
Luca Boccassibff733f2019-05-14 19:35:02 +0100107 &dest_blob, &dest_sbuf, false,
108 false);
Simon Glass802aa822014-06-02 22:04:53 -0600109 if (destfd < 0) {
110 ret = -EIO;
111 goto err_keydest;
112 }
113 destfd_size = dest_sbuf.st_size;
114 }
115
116 /* for first image creation, add a timestamp at offset 0 i.e., root */
Simon Glass472ee0c2020-07-09 18:39:43 -0600117 if (params->datafile || params->reset_timestamp) {
Alex Kiernanc4c7e0d2018-06-20 20:10:51 +0000118 time_t time = imagetool_get_source_date(params->cmdname,
119 sbuf.st_mtime);
Vagrant Cascadianf8e066c2016-06-16 12:28:40 -0700120 ret = fit_set_timestamp(ptr, 0, time);
121 }
Simon Glass802aa822014-06-02 22:04:53 -0600122
Paul-Erwan Riodcfb6332023-12-21 08:26:11 +0100123 if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && !ret)
Philippe Reynes3e3899c2022-03-28 22:57:02 +0200124 ret = fit_pre_load_data(params->keydir, dest_blob, ptr);
125
Simon Glass802aa822014-06-02 22:04:53 -0600126 if (!ret) {
Philippe Reynes3148e422019-12-18 18:25:41 +0100127 ret = fit_cipher_data(params->keydir, dest_blob, ptr,
128 params->comment,
129 params->require_keys,
130 params->engine_id,
131 params->cmdname);
132 }
133
134 if (!ret) {
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -0600135 ret = fit_add_verification_data(params->keydir,
136 params->keyfile, dest_blob, ptr,
Simon Glass802aa822014-06-02 22:04:53 -0600137 params->comment,
George McCollister23d14892017-01-06 13:14:17 -0600138 params->require_keys,
Alex Kiernan697fcdc2018-06-20 20:10:52 +0000139 params->engine_id,
Jan Kiszka4043f322022-01-14 10:21:19 +0100140 params->cmdname,
Simon Glasse4607262021-11-12 12:28:13 -0700141 params->algo_name,
142 &params->summary);
Simon Glass802aa822014-06-02 22:04:53 -0600143 }
144
145 if (dest_blob) {
146 munmap(dest_blob, destfd_size);
147 close(destfd);
148 }
149
150err_keydest:
151 munmap(ptr, sbuf.st_size);
152 close(tfd);
Simon Glass802aa822014-06-02 22:04:53 -0600153 return ret;
154}
155
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530156/**
Simon Glass88e31cb2016-02-22 22:55:51 -0700157 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
158 */
159static int fit_calc_size(struct image_tool_params *params)
160{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700161 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700162 int size, total_size;
163
164 size = imagetool_get_filesize(params, params->datafile);
165 if (size < 0)
166 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -0700167 total_size = size;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100168
169 if (params->fit_ramdisk) {
170 size = imagetool_get_filesize(params, params->fit_ramdisk);
171 if (size < 0)
172 return -1;
173 total_size += size;
174 }
175
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700176 for (cont = params->content_head; cont; cont = cont->next) {
177 size = imagetool_get_filesize(params, cont->fname);
178 if (size < 0)
179 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -0700180
Simon Glass611fd912020-05-27 07:24:55 -0600181 /* Add space for properties and hash node */
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700182 total_size += size + 300;
183 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700184
185 /* Add plenty of space for headers, properties, nodes, etc. */
186 total_size += 4096;
187
188 return total_size;
189}
190
191static int fdt_property_file(struct image_tool_params *params,
192 void *fdt, const char *name, const char *fname)
193{
194 struct stat sbuf;
195 void *ptr;
196 int ret;
197 int fd;
198
Ahelenia Ziemiańskac668b152024-03-21 19:31:54 +0100199 fd = open(fname, O_RDONLY | O_BINARY);
Simon Glass88e31cb2016-02-22 22:55:51 -0700200 if (fd < 0) {
201 fprintf(stderr, "%s: Can't open %s: %s\n",
202 params->cmdname, fname, strerror(errno));
203 return -1;
204 }
205
206 if (fstat(fd, &sbuf) < 0) {
207 fprintf(stderr, "%s: Can't stat %s: %s\n",
208 params->cmdname, fname, strerror(errno));
209 goto err;
210 }
211
212 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
213 if (ret)
Simon Glass9ab7f052016-03-16 07:45:42 -0600214 goto err;
Simon Glass88e31cb2016-02-22 22:55:51 -0700215 ret = read(fd, ptr, sbuf.st_size);
216 if (ret != sbuf.st_size) {
217 fprintf(stderr, "%s: Can't read %s: %s\n",
218 params->cmdname, fname, strerror(errno));
219 goto err;
220 }
Simon Glass9ab7f052016-03-16 07:45:42 -0600221 close(fd);
Simon Glass88e31cb2016-02-22 22:55:51 -0700222
223 return 0;
224err:
225 close(fd);
226 return -1;
227}
228
229static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
230{
231 char str[100];
232 va_list ptr;
233
234 va_start(ptr, fmt);
235 vsnprintf(str, sizeof(str), fmt, ptr);
236 va_end(ptr);
237 return fdt_property_string(fdt, name, str);
238}
239
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700240static void get_basename(char *str, int size, const char *fname)
241{
242 const char *p, *start, *end;
243 int len;
244
245 /*
246 * Use the base name as the 'name' field. So for example:
247 *
248 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
249 * becomes "sun7i-a20-bananapro"
250 */
251 p = strrchr(fname, '/');
252 start = p ? p + 1 : fname;
253 p = strrchr(fname, '.');
254 end = p ? p : fname + strlen(fname);
255 len = end - start;
256 if (len >= size)
257 len = size - 1;
258 memcpy(str, start, len);
259 str[len] = '\0';
260}
261
Simon Glass88e31cb2016-02-22 22:55:51 -0700262/**
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100263 * fit_add_hash_or_sign() - Add a hash or signature node
Simon Glass611fd912020-05-27 07:24:55 -0600264 *
Sean Anderson5f150292022-05-16 16:11:08 -0400265 * @params: Image parameters
Simon Glass611fd912020-05-27 07:24:55 -0600266 * @fdt: Device tree to add to (in sequential-write mode)
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100267 * @is_images_subnode: true to add hash even if key name hint is provided
Sean Anderson5f150292022-05-16 16:11:08 -0400268 *
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100269 * If do_add_hash is false (default) and there is a key name hint, try to add
270 * a sign node to parent. Otherwise, just add a CRC. Rationale: if conf have
271 * to be signed, image/dt have to be hashed even if there is a key name hint.
Simon Glass611fd912020-05-27 07:24:55 -0600272 */
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100273static void fit_add_hash_or_sign(struct image_tool_params *params, void *fdt,
274 bool is_images_subnode)
Simon Glass611fd912020-05-27 07:24:55 -0600275{
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100276 const char *hash_algo = "crc32";
277 bool do_hash = false;
278 bool do_sign = false;
279
280 switch (params->auto_fit) {
281 case AF_OFF:
282 break;
283 case AF_HASHED_IMG:
284 do_hash = is_images_subnode;
285 break;
286 case AF_SIGNED_IMG:
287 do_sign = is_images_subnode;
288 break;
289 case AF_SIGNED_CONF:
290 if (is_images_subnode) {
291 do_hash = true;
292 hash_algo = "sha1";
293 } else {
294 do_sign = true;
Sean Anderson5f150292022-05-16 16:11:08 -0400295 }
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100296 break;
297 default:
298 fprintf(stderr,
299 "%s: Unsupported auto FIT mode %u\n",
300 params->cmdname, params->auto_fit);
301 break;
302 }
303
304 if (do_hash) {
305 fdt_begin_node(fdt, FIT_HASH_NODENAME);
306 fdt_property_string(fdt, FIT_ALGO_PROP, hash_algo);
307 fdt_end_node(fdt);
308 }
Sean Anderson5f150292022-05-16 16:11:08 -0400309
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100310 if (do_sign) {
311 fdt_begin_node(fdt, FIT_SIG_NODENAME);
Sean Anderson5f150292022-05-16 16:11:08 -0400312 fdt_property_string(fdt, FIT_ALGO_PROP, params->algo_name);
313 fdt_property_string(fdt, FIT_KEY_HINT, params->keyname);
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100314 fdt_end_node(fdt);
Sean Anderson5f150292022-05-16 16:11:08 -0400315 }
Simon Glass611fd912020-05-27 07:24:55 -0600316}
317
318/**
Simon Glass88e31cb2016-02-22 22:55:51 -0700319 * fit_write_images() - Write out a list of images to the FIT
320 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700321 * We always include the main image (params->datafile). If there are device
Andre Przywara9ef25e82017-12-04 02:05:12 +0000322 * tree files, we include an fdt- node for each of those too.
Simon Glass88e31cb2016-02-22 22:55:51 -0700323 */
324static int fit_write_images(struct image_tool_params *params, char *fdt)
325{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700326 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700327 const char *typename;
328 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700329 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700330 int ret;
331
332 fdt_begin_node(fdt, "images");
333
334 /* First the main image */
335 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000336 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glass88e31cb2016-02-22 22:55:51 -0700337 fdt_begin_node(fdt, str);
Michal Simeka4e678d2018-07-20 12:31:02 +0200338 fdt_property_string(fdt, FIT_DESC_PROP, params->imagename);
339 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
340 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassaeeea7f2016-06-30 10:52:12 -0600341 genimg_get_arch_short_name(params->arch));
Michal Simeka4e678d2018-07-20 12:31:02 +0200342 fdt_property_string(fdt, FIT_OS_PROP,
343 genimg_get_os_short_name(params->os));
344 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glass88e31cb2016-02-22 22:55:51 -0700345 genimg_get_comp_short_name(params->comp));
Michal Simeka4e678d2018-07-20 12:31:02 +0200346 fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr);
347 fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep);
Simon Glass88e31cb2016-02-22 22:55:51 -0700348
349 /*
350 * Put data last since it is large. SPL may only load the first part
351 * of the DT, so this way it can access all the above fields.
352 */
Michal Simeka4e678d2018-07-20 12:31:02 +0200353 ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile);
Simon Glass88e31cb2016-02-22 22:55:51 -0700354 if (ret)
355 return ret;
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100356 fit_add_hash_or_sign(params, fdt, true);
Simon Glass88e31cb2016-02-22 22:55:51 -0700357 fdt_end_node(fdt);
358
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700359 /* Now the device tree files if available */
360 upto = 0;
361 for (cont = params->content_head; cont; cont = cont->next) {
362 if (cont->type != IH_TYPE_FLATDT)
363 continue;
Michal Sojka8a90ce82019-09-13 12:43:12 +0200364 typename = genimg_get_type_short_name(cont->type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000365 snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700366 fdt_begin_node(fdt, str);
367
368 get_basename(str, sizeof(str), cont->fname);
Michal Simeka4e678d2018-07-20 12:31:02 +0200369 fdt_property_string(fdt, FIT_DESC_PROP, str);
370 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
371 cont->fname);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700372 if (ret)
373 return ret;
Michal Simeka4e678d2018-07-20 12:31:02 +0200374 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
375 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700376 genimg_get_arch_short_name(params->arch));
Michal Simeka4e678d2018-07-20 12:31:02 +0200377 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700378 genimg_get_comp_short_name(IH_COMP_NONE));
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100379 fit_add_hash_or_sign(params, fdt, true);
Sean Anderson5f150292022-05-16 16:11:08 -0400380 if (ret)
381 return ret;
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700382 fdt_end_node(fdt);
383 }
384
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100385 /* And a ramdisk file if available */
386 if (params->fit_ramdisk) {
Andre Przywara9ef25e82017-12-04 02:05:12 +0000387 fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100388
Michal Simeka4e678d2018-07-20 12:31:02 +0200389 fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP);
390 fdt_property_string(fdt, FIT_OS_PROP,
391 genimg_get_os_short_name(params->os));
Michal Sojka8a90ce82019-09-13 12:43:12 +0200392 fdt_property_string(fdt, FIT_ARCH_PROP,
393 genimg_get_arch_short_name(params->arch));
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100394
Michal Simeka4e678d2018-07-20 12:31:02 +0200395 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
396 params->fit_ramdisk);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100397 if (ret)
398 return ret;
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100399 fit_add_hash_or_sign(params, fdt, true);
Sean Anderson5f150292022-05-16 16:11:08 -0400400 if (ret)
401 return ret;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100402 fdt_end_node(fdt);
403 }
404
Simon Glass88e31cb2016-02-22 22:55:51 -0700405 fdt_end_node(fdt);
406
407 return 0;
408}
409
410/**
411 * fit_write_configs() - Write out a list of configurations to the FIT
412 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700413 * If there are device tree files, we include a configuration for each, which
414 * selects the main image (params->datafile) and its corresponding device
415 * tree file.
416 *
417 * Otherwise we just create a configuration with the main image in it.
Simon Glass88e31cb2016-02-22 22:55:51 -0700418 */
419static void fit_write_configs(struct image_tool_params *params, char *fdt)
420{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700421 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700422 const char *typename;
423 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700424 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700425
426 fdt_begin_node(fdt, "configurations");
Michal Simeka4e678d2018-07-20 12:31:02 +0200427 fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
Simon Glass88e31cb2016-02-22 22:55:51 -0700428
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700429 upto = 0;
430 for (cont = params->content_head; cont; cont = cont->next) {
431 if (cont->type != IH_TYPE_FLATDT)
432 continue;
433 typename = genimg_get_type_short_name(cont->type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000434 snprintf(str, sizeof(str), "conf-%d", ++upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700435 fdt_begin_node(fdt, str);
436
437 get_basename(str, sizeof(str), cont->fname);
Michal Simeka4e678d2018-07-20 12:31:02 +0200438 fdt_property_string(fdt, FIT_DESC_PROP, str);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700439
440 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000441 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700442 fdt_property_string(fdt, typename, str);
Abel Vesa7e0cb5e2019-03-12 08:34:32 +0000443 fdt_property_string(fdt, FIT_LOADABLE_PROP, str);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700444
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100445 if (params->fit_ramdisk)
446 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara9ef25e82017-12-04 02:05:12 +0000447 FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100448
Andre Przywara9ef25e82017-12-04 02:05:12 +0000449 snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700450 fdt_property_string(fdt, FIT_FDT_PROP, str);
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100451 fit_add_hash_or_sign(params, fdt, false);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700452 fdt_end_node(fdt);
453 }
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100454
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700455 if (!upto) {
Andre Przywara9ef25e82017-12-04 02:05:12 +0000456 fdt_begin_node(fdt, "conf-1");
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700457 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000458 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700459 fdt_property_string(fdt, typename, str);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100460
461 if (params->fit_ramdisk)
462 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara9ef25e82017-12-04 02:05:12 +0000463 FIT_RAMDISK_PROP "-1");
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100464 fit_add_hash_or_sign(params, fdt, false);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100465
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700466 fdt_end_node(fdt);
467 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700468
469 fdt_end_node(fdt);
470}
471
472static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
473{
474 int ret;
475
476 ret = fdt_create(fdt, size);
477 if (ret)
478 return ret;
479 fdt_finish_reservemap(fdt);
480 fdt_begin_node(fdt, "");
Michal Simeka4e678d2018-07-20 12:31:02 +0200481 fdt_property_strf(fdt, FIT_DESC_PROP,
Simon Glass88e31cb2016-02-22 22:55:51 -0700482 "%s image with one or more FDT blobs",
483 genimg_get_type_name(params->fit_image_type));
484 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
485 fdt_property_u32(fdt, "#address-cells", 1);
486 ret = fit_write_images(params, fdt);
487 if (ret)
488 return ret;
489 fit_write_configs(params, fdt);
490 fdt_end_node(fdt);
491 ret = fdt_finish(fdt);
492 if (ret)
493 return ret;
494
495 return fdt_totalsize(fdt);
496}
497
498static int fit_build(struct image_tool_params *params, const char *fname)
499{
500 char *buf;
501 int size;
502 int ret;
503 int fd;
504
505 size = fit_calc_size(params);
506 if (size < 0)
507 return -1;
Fabio Estevamc2248cb2020-07-27 21:03:13 -0300508 buf = calloc(1, size);
Simon Glass88e31cb2016-02-22 22:55:51 -0700509 if (!buf) {
510 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
511 params->cmdname, size);
512 return -1;
513 }
514 ret = fit_build_fdt(params, buf, size);
515 if (ret < 0) {
516 fprintf(stderr, "%s: Failed to build FIT image\n",
517 params->cmdname);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600518 goto err_buf;
Simon Glass88e31cb2016-02-22 22:55:51 -0700519 }
520 size = ret;
521 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
522 if (fd < 0) {
523 fprintf(stderr, "%s: Can't open %s: %s\n",
524 params->cmdname, fname, strerror(errno));
Tom Rinib8286652017-09-26 22:14:44 -0400525 goto err_buf;
Simon Glass88e31cb2016-02-22 22:55:51 -0700526 }
527 ret = write(fd, buf, size);
528 if (ret != size) {
529 fprintf(stderr, "%s: Can't write %s: %s\n",
530 params->cmdname, fname, strerror(errno));
Simon Glass88e31cb2016-02-22 22:55:51 -0700531 goto err;
532 }
533 close(fd);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600534 free(buf);
Simon Glass88e31cb2016-02-22 22:55:51 -0700535
536 return 0;
537err:
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600538 close(fd);
539err_buf:
Simon Glass88e31cb2016-02-22 22:55:51 -0700540 free(buf);
541 return -1;
542}
543
544/**
Simon Glassafd728c2016-02-22 22:55:53 -0700545 * fit_extract_data() - Move all data outside the FIT
546 *
547 * This takes a normal FIT file and removes all the 'data' properties from it.
548 * The data is placed in an area after the FIT so that it can be accessed
549 * using an offset into that area. The 'data' properties turn into
550 * 'data-offset' properties.
551 *
552 * This function cannot cope with FITs with 'data-offset' properties. All
553 * data must be in 'data' properties on entry.
554 */
555static int fit_extract_data(struct image_tool_params *params, const char *fname)
556{
Kever Yang8e238b52020-03-30 11:56:24 +0800557 void *buf = NULL;
Simon Glassafd728c2016-02-22 22:55:53 -0700558 int buf_ptr;
Roman Azarenko9ffe4d62023-08-25 10:10:14 +0200559 int fit_size, unpadded_size, new_size, pad_boundary;
Simon Glassafd728c2016-02-22 22:55:53 -0700560 int fd;
561 struct stat sbuf;
562 void *fdt;
563 int ret;
564 int images;
565 int node;
Kever Yang8e238b52020-03-30 11:56:24 +0800566 int image_number;
567 int align_size;
Simon Glassafd728c2016-02-22 22:55:53 -0700568
Tom Rini96642972020-05-06 11:05:17 -0400569 align_size = params->bl_len ? params->bl_len : 4;
Luca Boccassibff733f2019-05-14 19:35:02 +0100570 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false);
Simon Glassafd728c2016-02-22 22:55:53 -0700571 if (fd < 0)
572 return -EIO;
573 fit_size = fdt_totalsize(fdt);
574
Simon Glassafd728c2016-02-22 22:55:53 -0700575 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
576 if (images < 0) {
577 debug("%s: Cannot find /images node: %d\n", __func__, images);
578 ret = -EINVAL;
Simon Glass4e7505f2016-03-16 07:45:39 -0600579 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700580 }
Kever Yang8e238b52020-03-30 11:56:24 +0800581 image_number = fdtdec_get_child_count(fdt, images);
582
583 /*
584 * Allocate space to hold the image data we will extract,
585 * extral space allocate for image alignment to prevent overflow.
586 */
Fabio Estevamc2248cb2020-07-27 21:03:13 -0300587 buf = calloc(1, fit_size + (align_size * image_number));
Kever Yang8e238b52020-03-30 11:56:24 +0800588 if (!buf) {
589 ret = -ENOMEM;
590 goto err_munmap;
591 }
592 buf_ptr = 0;
Simon Glassafd728c2016-02-22 22:55:53 -0700593
594 for (node = fdt_first_subnode(fdt, images);
595 node >= 0;
596 node = fdt_next_subnode(fdt, node)) {
597 const char *data;
598 int len;
599
Michal Simeka4e678d2018-07-20 12:31:02 +0200600 data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
Simon Glassafd728c2016-02-22 22:55:53 -0700601 if (!data)
602 continue;
603 memcpy(buf + buf_ptr, data, len);
604 debug("Extracting data size %x\n", len);
605
Michal Simeka4e678d2018-07-20 12:31:02 +0200606 ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
Simon Glassafd728c2016-02-22 22:55:53 -0700607 if (ret) {
608 ret = -EPERM;
Simon Glass4e7505f2016-03-16 07:45:39 -0600609 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700610 }
Teddy Reeda8457622016-06-09 19:38:02 -0700611 if (params->external_offset > 0) {
612 /* An external offset positions the data absolutely. */
Michal Simeka4e678d2018-07-20 12:31:02 +0200613 fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
Teddy Reeda8457622016-06-09 19:38:02 -0700614 params->external_offset + buf_ptr);
615 } else {
Michal Simeka4e678d2018-07-20 12:31:02 +0200616 fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
617 buf_ptr);
Teddy Reeda8457622016-06-09 19:38:02 -0700618 }
Michal Simeka4e678d2018-07-20 12:31:02 +0200619 fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
Kever Yang8e238b52020-03-30 11:56:24 +0800620 buf_ptr += ALIGN(len, align_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700621 }
622
623 /* Pack the FDT and place the data after it */
624 fdt_pack(fdt);
625
Roman Azarenko9ffe4d62023-08-25 10:10:14 +0200626 unpadded_size = fdt_totalsize(fdt);
627 new_size = ALIGN(unpadded_size, align_size);
Kever Yang8e238b52020-03-30 11:56:24 +0800628 fdt_set_totalsize(fdt, new_size);
Roman Azarenko9ffe4d62023-08-25 10:10:14 +0200629 if (unpadded_size < fit_size) {
630 pad_boundary = new_size < fit_size ? new_size : fit_size;
631 memset(fdt + unpadded_size, 0, pad_boundary - unpadded_size);
632 }
Simon Glassafd728c2016-02-22 22:55:53 -0700633 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
634 debug("External data size %x\n", buf_ptr);
Simon Glassafd728c2016-02-22 22:55:53 -0700635 munmap(fdt, sbuf.st_size);
636
637 if (ftruncate(fd, new_size)) {
638 debug("%s: Failed to truncate file: %s\n", __func__,
639 strerror(errno));
640 ret = -EIO;
641 goto err;
642 }
Teddy Reeda8457622016-06-09 19:38:02 -0700643
644 /* Check if an offset for the external data was set. */
645 if (params->external_offset > 0) {
646 if (params->external_offset < new_size) {
Simon Glass00c06772022-01-09 20:13:38 -0700647 fprintf(stderr,
648 "External offset %x overlaps FIT length %x\n",
649 params->external_offset, new_size);
Teddy Reeda8457622016-06-09 19:38:02 -0700650 ret = -EINVAL;
651 goto err;
652 }
653 new_size = params->external_offset;
654 }
Simon Glassafd728c2016-02-22 22:55:53 -0700655 if (lseek(fd, new_size, SEEK_SET) < 0) {
656 debug("%s: Failed to seek to end of file: %s\n", __func__,
657 strerror(errno));
658 ret = -EIO;
659 goto err;
660 }
661 if (write(fd, buf, buf_ptr) != buf_ptr) {
662 debug("%s: Failed to write external data to file %s\n",
663 __func__, strerror(errno));
664 ret = -EIO;
665 goto err;
666 }
Tom Rinib8286652017-09-26 22:14:44 -0400667 free(buf);
Simon Glass4e7505f2016-03-16 07:45:39 -0600668 close(fd);
669 return 0;
Simon Glassafd728c2016-02-22 22:55:53 -0700670
Simon Glass4e7505f2016-03-16 07:45:39 -0600671err_munmap:
672 munmap(fdt, sbuf.st_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700673err:
Bin Meng6eb238742020-04-18 01:59:11 -0700674 free(buf);
Simon Glassafd728c2016-02-22 22:55:53 -0700675 close(fd);
676 return ret;
677}
678
Simon Glassd11b7002016-02-22 22:55:54 -0700679static int fit_import_data(struct image_tool_params *params, const char *fname)
680{
681 void *fdt, *old_fdt;
Lars Feyaertsadbc90f2023-10-02 10:00:14 +0200682 void *data = NULL;
683 const char *ext_data_prop = NULL;
Simon Glassd11b7002016-02-22 22:55:54 -0700684 int fit_size, new_size, size, data_base;
685 int fd;
686 struct stat sbuf;
687 int ret;
688 int images;
Aristo Chenfeb939c62025-06-10 07:41:18 +0000689 int confs;
Simon Glassd11b7002016-02-22 22:55:54 -0700690 int node;
691
Luca Boccassibff733f2019-05-14 19:35:02 +0100692 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false);
Simon Glassd11b7002016-02-22 22:55:54 -0700693 if (fd < 0)
694 return -EIO;
695 fit_size = fdt_totalsize(old_fdt);
Kever Yang8c11bc12020-03-30 11:56:22 +0800696 data_base = ALIGN(fit_size, 4);
Simon Glassd11b7002016-02-22 22:55:54 -0700697
698 /* Allocate space to hold the new FIT */
699 size = sbuf.st_size + 16384;
Fabio Estevamc2248cb2020-07-27 21:03:13 -0300700 fdt = calloc(1, size);
Simon Glassd11b7002016-02-22 22:55:54 -0700701 if (!fdt) {
702 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
703 __func__, size);
704 ret = -ENOMEM;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700705 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700706 }
707 ret = fdt_open_into(old_fdt, fdt, size);
708 if (ret) {
709 debug("%s: Failed to expand FIT: %s\n", __func__,
710 fdt_strerror(errno));
711 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700712 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700713 }
714
715 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
716 if (images < 0) {
717 debug("%s: Cannot find /images node: %d\n", __func__, images);
718 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700719 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700720 }
721
722 for (node = fdt_first_subnode(fdt, images);
723 node >= 0;
724 node = fdt_next_subnode(fdt, node)) {
725 int buf_ptr;
726 int len;
727
Lars Feyaertsadbc90f2023-10-02 10:00:14 +0200728 /*
729 * FIT_DATA_OFFSET_PROP and FIT_DATA_POSITION_PROP are never both present,
730 * but if they are, prefer FIT_DATA_OFFSET_PROP as it was there first
731 */
732 buf_ptr = fdtdec_get_int(fdt, node, FIT_DATA_POSITION_PROP, -1);
733 if (buf_ptr != -1) {
734 ext_data_prop = FIT_DATA_POSITION_PROP;
735 data = old_fdt + buf_ptr;
736 }
737 buf_ptr = fdtdec_get_int(fdt, node, FIT_DATA_OFFSET_PROP, -1);
738 if (buf_ptr != -1) {
739 ext_data_prop = FIT_DATA_OFFSET_PROP;
740 data = old_fdt + data_base + buf_ptr;
741 }
742 len = fdtdec_get_int(fdt, node, FIT_DATA_SIZE_PROP, -1);
743 if (!data || len == -1)
Simon Glassd11b7002016-02-22 22:55:54 -0700744 continue;
745 debug("Importing data size %x\n", len);
746
Lars Feyaertsadbc90f2023-10-02 10:00:14 +0200747 ret = fdt_setprop(fdt, node, FIT_DATA_PROP, data, len);
748 ret = fdt_delprop(fdt, node, ext_data_prop);
749
Simon Glassd11b7002016-02-22 22:55:54 -0700750 if (ret) {
751 debug("%s: Failed to write property: %s\n", __func__,
752 fdt_strerror(ret));
753 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700754 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700755 }
756 }
757
Aristo Chenfeb939c62025-06-10 07:41:18 +0000758 confs = fdt_path_offset(fdt, FIT_CONFS_PATH);
759 static const char * const props[] = { FIT_KERNEL_PROP,
760 FIT_RAMDISK_PROP,
761 FIT_FDT_PROP,
762 FIT_LOADABLE_PROP,
763 FIT_FPGA_PROP,
764 FIT_FIRMWARE_PROP,
765 FIT_SCRIPT_PROP};
766
767 fdt_for_each_subnode(node, fdt, confs) {
768 const char *conf_name = fdt_get_name(fdt, node, NULL);
769
770 for (int i = 0; i < ARRAY_SIZE(props); i++) {
771 int count = fdt_stringlist_count(fdt, node, props[i]);
772
773 if (count < 0)
774 continue;
775
776 for (int j = 0; j < count; j++) {
777 const char *img_name =
778 fdt_stringlist_get(fdt, node, props[i], j, NULL);
779 if (!img_name || !*img_name)
780 continue;
781
782 int img = fdt_subnode_offset(fdt, images, img_name);
783
784 if (img < 0) {
785 fprintf(stderr,
786 "Error: configuration '%s' references undefined image '%s' in property '%s'\n",
787 conf_name, img_name, props[i]);
788 ret = FDT_ERR_NOTFOUND;
789 goto err_munmap;
790 }
791 }
792 }
793 }
794
Lihua Zhao4536ef82020-04-18 01:59:10 -0700795 munmap(old_fdt, sbuf.st_size);
796
Tom Rinia29829e2017-10-07 11:27:59 -0400797 /* Close the old fd so we can re-use it. */
Simon Glassd11b7002016-02-22 22:55:54 -0700798 close(fd);
799
800 /* Pack the FDT and place the data after it */
801 fdt_pack(fdt);
802
803 new_size = fdt_totalsize(fdt);
804 debug("Size expanded from %x to %x\n", fit_size, new_size);
805
806 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
807 if (fd < 0) {
808 fprintf(stderr, "%s: Can't open %s: %s\n",
809 params->cmdname, fname, strerror(errno));
Tom Rinia29829e2017-10-07 11:27:59 -0400810 ret = -EIO;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700811 goto err;
Simon Glassd11b7002016-02-22 22:55:54 -0700812 }
813 if (write(fd, fdt, new_size) != new_size) {
814 debug("%s: Failed to write external data to file %s\n",
815 __func__, strerror(errno));
816 ret = -EIO;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700817 goto err;
Simon Glassd11b7002016-02-22 22:55:54 -0700818 }
Simon Glassd11b7002016-02-22 22:55:54 -0700819
Lihua Zhao4536ef82020-04-18 01:59:10 -0700820 free(fdt);
Tom Rinia29829e2017-10-07 11:27:59 -0400821 close(fd);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700822 return 0;
823
824err_munmap:
Tom Rinib8286652017-09-26 22:14:44 -0400825 munmap(old_fdt, sbuf.st_size);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700826err:
Simon Glass5014cc12016-03-16 07:45:38 -0600827 free(fdt);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700828 close(fd);
Simon Glassd11b7002016-02-22 22:55:54 -0700829 return ret;
830}
831
Simon Glassafd728c2016-02-22 22:55:53 -0700832/**
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530833 * fit_handle_file - main FIT file processing function
834 *
835 * fit_handle_file() runs dtc to convert .its to .itb, includes
836 * binary data, updates timestamp property and calculates hashes.
837 *
838 * datafile - .its file
839 * imagefile - .itb file
840 *
841 * returns:
842 * only on success, otherwise calls exit (EXIT_FAILURE);
843 */
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700844static int fit_handle_file(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530845{
846 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
Philippe Reynes3148e422019-12-18 18:25:41 +0100847 char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0};
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530848 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
Simon Glass802aa822014-06-02 22:04:53 -0600849 size_t size_inc;
Aristo Chen48f5c272025-06-10 07:41:17 +0000850 int ret = EXIT_FAILURE;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530851
852 /* Flattened Image Tree (FIT) format handling */
853 debug ("FIT format handling\n");
854
855 /* call dtc to include binary properties into the tmp file */
856 if (strlen (params->imagefile) +
857 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
858 fprintf (stderr, "%s: Image file name (%s) too long, "
Sven Roederer88be5d32021-05-25 23:15:27 +0200859 "can't create tmpfile.\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530860 params->imagefile, params->cmdname);
861 return (EXIT_FAILURE);
862 }
863 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
864
Simon Glassce8c3ca2013-06-13 15:10:05 -0700865 /* We either compile the source file, or use the existing FIT image */
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100866 if (params->auto_fit) {
Simon Glass88e31cb2016-02-22 22:55:51 -0700867 if (fit_build(params, tmpfile)) {
868 fprintf(stderr, "%s: failed to build FIT\n",
869 params->cmdname);
870 return EXIT_FAILURE;
871 }
872 *cmd = '\0';
873 } else if (params->datafile) {
Stefan Theilff6c3b22018-03-08 09:00:13 +0100874 /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
875 snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
876 MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
Simon Glassce8c3ca2013-06-13 15:10:05 -0700877 debug("Trying to execute \"%s\"\n", cmd);
878 } else {
Mirza, Taimoor74a21dd2017-10-04 20:28:03 +0500879 snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
Simon Glassce8c3ca2013-06-13 15:10:05 -0700880 params->imagefile, tmpfile);
881 }
Sven Roederercbcb3992020-04-27 02:08:39 +0200882 if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) {
883 fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n");
884 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100885
Simon Glass88e31cb2016-02-22 22:55:51 -0700886 if (*cmd && system(cmd) == -1) {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530887 fprintf (stderr, "%s: system(%s) failed: %s\n",
888 params->cmdname, cmd, strerror(errno));
Simon Glass4161c412013-05-08 08:05:57 +0000889 goto err_system;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530890 }
891
Simon Glassd11b7002016-02-22 22:55:54 -0700892 /* Move the data so it is internal to the FIT, if needed */
893 ret = fit_import_data(params, tmpfile);
894 if (ret)
895 goto err_system;
896
Simon Glass802aa822014-06-02 22:04:53 -0600897 /*
Philippe Reynes3148e422019-12-18 18:25:41 +0100898 * Copy the tmpfile to bakfile, then in the following loop
899 * we copy bakfile to tmpfile. So we always start from the
900 * beginning.
901 */
902 sprintf(bakfile, "%s%s", tmpfile, ".bak");
903 rename(tmpfile, bakfile);
904
905 /*
Rasmus Villemoes9cc19702025-06-10 14:27:48 +0200906 * Set hashes for images in the blob and compute
907 * signatures. We do an attempt at estimating the expected
908 * extra size, but just in case that is not sufficient, keep
909 * trying adding 1K, with a reasonable upper bound of 64K
910 * total, until we succeed.
Simon Glass802aa822014-06-02 22:04:53 -0600911 */
Rasmus Villemoes9cc19702025-06-10 14:27:48 +0200912 size_inc = fit_estimate_hash_sig_size(params, bakfile);
913 if (size_inc < 0)
914 goto err_system;
915 do {
Philippe Reynes3148e422019-12-18 18:25:41 +0100916 if (copyfile(bakfile, tmpfile) < 0) {
917 printf("Can't copy %s to %s\n", bakfile, tmpfile);
918 ret = -EIO;
919 break;
920 }
Simon Glass802aa822014-06-02 22:04:53 -0600921 ret = fit_add_file_data(params, size_inc, tmpfile);
922 if (!ret || ret != -ENOSPC)
923 break;
Rasmus Villemoes9cc19702025-06-10 14:27:48 +0200924 size_inc += 1024;
925 } while (size_inc < 64 * 1024);
Simon Glassb4d8b092013-06-13 15:10:04 -0700926
Simon Glass802aa822014-06-02 22:04:53 -0600927 if (ret) {
Simon Glass061752e2016-07-03 09:40:43 -0600928 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
929 params->cmdname, ret);
Simon Glass802aa822014-06-02 22:04:53 -0600930 goto err_system;
Simon Glassb4d8b092013-06-13 15:10:04 -0700931 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530932
Simon Glassafd728c2016-02-22 22:55:53 -0700933 /* Move the data so it is external to the FIT, if requested */
934 if (params->external_data) {
935 ret = fit_extract_data(params, tmpfile);
936 if (ret)
937 goto err_system;
938 }
939
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530940 if (rename (tmpfile, params->imagefile) == -1) {
941 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
942 params->cmdname, tmpfile, params->imagefile,
943 strerror (errno));
944 unlink (tmpfile);
Philippe Reynes3148e422019-12-18 18:25:41 +0100945 unlink(bakfile);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530946 unlink (params->imagefile);
Simon Glass802aa822014-06-02 22:04:53 -0600947 return EXIT_FAILURE;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530948 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100949 unlink(bakfile);
Simon Glass802aa822014-06-02 22:04:53 -0600950 return EXIT_SUCCESS;
Simon Glass4161c412013-05-08 08:05:57 +0000951
Simon Glass4161c412013-05-08 08:05:57 +0000952err_system:
953 unlink(tmpfile);
Philippe Reynes3148e422019-12-18 18:25:41 +0100954 unlink(bakfile);
Aristo Chen48f5c272025-06-10 07:41:17 +0000955 return ret;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530956}
957
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200958/**
959 * fit_image_extract - extract a FIT component image
960 * @fit: pointer to the FIT format image header
961 * @image_noffset: offset of the component image node
962 * @file_name: name of the file to store the FIT sub-image
963 *
964 * returns:
965 * zero in case of success or a negative value if fail.
966 */
967static int fit_image_extract(
968 const void *fit,
969 int image_noffset,
970 const char *file_name)
971{
972 const void *file_data;
973 size_t file_size = 0;
Andrew F. Davis166d5e92019-09-17 17:09:34 -0400974 int ret;
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200975
Andrew F. Davis166d5e92019-09-17 17:09:34 -0400976 /* get the data address and size of component at offset "image_noffset" */
Simon Glass833445d2025-01-10 17:00:13 -0700977 ret = fit_image_get_data(fit, image_noffset, &file_data, &file_size);
Andrew F. Davis166d5e92019-09-17 17:09:34 -0400978 if (ret) {
979 fprintf(stderr, "Could not get component information\n");
980 return ret;
981 }
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200982
983 /* save the "file_data" into the file specified by "file_name" */
984 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
985}
986
987/**
988 * fit_extract_contents - retrieve a sub-image component from the FIT image
989 * @ptr: pointer to the FIT format image header
990 * @params: command line parameters
991 *
992 * returns:
993 * zero in case of success or a negative value if fail.
994 */
995static int fit_extract_contents(void *ptr, struct image_tool_params *params)
996{
997 int images_noffset;
998 int noffset;
999 int ndepth;
1000 const void *fit = ptr;
1001 int count = 0;
1002 const char *p;
1003
1004 /* Indent string is defined in header image.h */
1005 p = IMAGE_INDENT_STRING;
1006
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -02001007 /* Find images parent node offset */
1008 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
1009 if (images_noffset < 0) {
1010 printf("Can't find images parent node '%s' (%s)\n",
1011 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
1012 return -1;
1013 }
1014
1015 /* Avoid any overrun */
1016 count = fit_get_subimage_count(fit, images_noffset);
1017 if ((params->pflag < 0) || (count <= params->pflag)) {
1018 printf("No such component at '%d'\n", params->pflag);
1019 return -1;
1020 }
1021
1022 /* Process its subnodes, extract the desired component from image */
1023 for (ndepth = 0, count = 0,
1024 noffset = fdt_next_node(fit, images_noffset, &ndepth);
1025 (noffset >= 0) && (ndepth > 0);
1026 noffset = fdt_next_node(fit, noffset, &ndepth)) {
1027 if (ndepth == 1) {
1028 /*
1029 * Direct child node of the images parent node,
1030 * i.e. component image node.
1031 */
1032 if (params->pflag == count) {
1033 printf("Extracted:\n%s Image %u (%s)\n", p,
1034 count, fit_get_name(fit, noffset, NULL));
1035
1036 fit_image_print(fit, noffset, p);
1037
1038 return fit_image_extract(fit, noffset,
1039 params->outfile);
1040 }
1041
1042 count++;
1043 }
1044 }
1045
1046 return 0;
1047}
1048
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -07001049static int fit_check_params(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +05301050{
Massimo Pegorer13878dd2023-01-05 10:31:09 +01001051 if (params->auto_fit)
Simon Glass88e31cb2016-02-22 22:55:51 -07001052 return 0;
Heinrich Schuchardt915f5aa2019-12-11 13:51:38 +01001053 return ((params->dflag && params->fflag) ||
1054 (params->fflag && params->lflag) ||
1055 (params->lflag && params->dflag));
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +05301056}
1057
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -02001058U_BOOT_IMAGE_TYPE(
1059 fitimage,
1060 "FIT Image support",
Simon Glassbb7d3bb2022-09-06 20:26:52 -06001061 sizeof(struct legacy_img_hdr),
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -02001062 (void *)&header,
1063 fit_check_params,
1064 fit_verify_header,
Pali Rohár0ed41e22023-03-29 21:25:54 +02001065 fit_print_header,
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -02001066 NULL,
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -02001067 fit_extract_contents,
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -02001068 fit_check_image_types,
1069 fit_handle_file,
1070 NULL /* FIT images use DTB header */
1071);