blob: 8fdde012bdac47faf63f50e4786a5cbf76e90db5 [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
Simon Glass802aa822014-06-02 22:04:53 -060027static int fit_add_file_data(struct image_tool_params *params, size_t size_inc,
28 const char *tmpfile)
29{
30 int tfd, destfd = 0;
31 void *dest_blob = NULL;
32 off_t destfd_size = 0;
33 struct stat sbuf;
34 void *ptr;
35 int ret = 0;
36
Luca Boccassibff733f2019-05-14 19:35:02 +010037 tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true,
38 false);
Simon Glass35414072022-12-21 16:08:23 -070039 if (tfd < 0) {
40 fprintf(stderr, "Cannot map FDT file '%s'\n", tmpfile);
Simon Glass802aa822014-06-02 22:04:53 -060041 return -EIO;
Simon Glass35414072022-12-21 16:08:23 -070042 }
Simon Glass802aa822014-06-02 22:04:53 -060043
44 if (params->keydest) {
45 struct stat dest_sbuf;
46
47 destfd = mmap_fdt(params->cmdname, params->keydest, size_inc,
Luca Boccassibff733f2019-05-14 19:35:02 +010048 &dest_blob, &dest_sbuf, false,
49 false);
Simon Glass802aa822014-06-02 22:04:53 -060050 if (destfd < 0) {
51 ret = -EIO;
52 goto err_keydest;
53 }
54 destfd_size = dest_sbuf.st_size;
55 }
56
57 /* for first image creation, add a timestamp at offset 0 i.e., root */
Simon Glass472ee0c2020-07-09 18:39:43 -060058 if (params->datafile || params->reset_timestamp) {
Alex Kiernanc4c7e0d2018-06-20 20:10:51 +000059 time_t time = imagetool_get_source_date(params->cmdname,
60 sbuf.st_mtime);
Vagrant Cascadianf8e066c2016-06-16 12:28:40 -070061 ret = fit_set_timestamp(ptr, 0, time);
62 }
Simon Glass802aa822014-06-02 22:04:53 -060063
Paul-Erwan Riodcfb6332023-12-21 08:26:11 +010064 if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && !ret)
Philippe Reynes3e3899c2022-03-28 22:57:02 +020065 ret = fit_pre_load_data(params->keydir, dest_blob, ptr);
66
Simon Glass802aa822014-06-02 22:04:53 -060067 if (!ret) {
Philippe Reynes3148e422019-12-18 18:25:41 +010068 ret = fit_cipher_data(params->keydir, dest_blob, ptr,
69 params->comment,
70 params->require_keys,
71 params->engine_id,
72 params->cmdname);
73 }
74
75 if (!ret) {
Alexandru Gagniuc8fcea122021-02-19 12:45:17 -060076 ret = fit_add_verification_data(params->keydir,
77 params->keyfile, dest_blob, ptr,
Simon Glass802aa822014-06-02 22:04:53 -060078 params->comment,
George McCollister23d14892017-01-06 13:14:17 -060079 params->require_keys,
Alex Kiernan697fcdc2018-06-20 20:10:52 +000080 params->engine_id,
Jan Kiszka4043f322022-01-14 10:21:19 +010081 params->cmdname,
Simon Glasse4607262021-11-12 12:28:13 -070082 params->algo_name,
83 &params->summary);
Simon Glass802aa822014-06-02 22:04:53 -060084 }
85
86 if (dest_blob) {
87 munmap(dest_blob, destfd_size);
88 close(destfd);
89 }
90
91err_keydest:
92 munmap(ptr, sbuf.st_size);
93 close(tfd);
Simon Glass802aa822014-06-02 22:04:53 -060094 return ret;
95}
96
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053097/**
Simon Glass88e31cb2016-02-22 22:55:51 -070098 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
99 */
100static int fit_calc_size(struct image_tool_params *params)
101{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700102 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700103 int size, total_size;
104
105 size = imagetool_get_filesize(params, params->datafile);
106 if (size < 0)
107 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -0700108 total_size = size;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100109
110 if (params->fit_ramdisk) {
111 size = imagetool_get_filesize(params, params->fit_ramdisk);
112 if (size < 0)
113 return -1;
114 total_size += size;
115 }
116
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700117 for (cont = params->content_head; cont; cont = cont->next) {
118 size = imagetool_get_filesize(params, cont->fname);
119 if (size < 0)
120 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -0700121
Simon Glass611fd912020-05-27 07:24:55 -0600122 /* Add space for properties and hash node */
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700123 total_size += size + 300;
124 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700125
126 /* Add plenty of space for headers, properties, nodes, etc. */
127 total_size += 4096;
128
129 return total_size;
130}
131
132static int fdt_property_file(struct image_tool_params *params,
133 void *fdt, const char *name, const char *fname)
134{
135 struct stat sbuf;
136 void *ptr;
137 int ret;
138 int fd;
139
Ahelenia Ziemiańskac668b152024-03-21 19:31:54 +0100140 fd = open(fname, O_RDONLY | O_BINARY);
Simon Glass88e31cb2016-02-22 22:55:51 -0700141 if (fd < 0) {
142 fprintf(stderr, "%s: Can't open %s: %s\n",
143 params->cmdname, fname, strerror(errno));
144 return -1;
145 }
146
147 if (fstat(fd, &sbuf) < 0) {
148 fprintf(stderr, "%s: Can't stat %s: %s\n",
149 params->cmdname, fname, strerror(errno));
150 goto err;
151 }
152
153 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
154 if (ret)
Simon Glass9ab7f052016-03-16 07:45:42 -0600155 goto err;
Simon Glass88e31cb2016-02-22 22:55:51 -0700156 ret = read(fd, ptr, sbuf.st_size);
157 if (ret != sbuf.st_size) {
158 fprintf(stderr, "%s: Can't read %s: %s\n",
159 params->cmdname, fname, strerror(errno));
160 goto err;
161 }
Simon Glass9ab7f052016-03-16 07:45:42 -0600162 close(fd);
Simon Glass88e31cb2016-02-22 22:55:51 -0700163
164 return 0;
165err:
166 close(fd);
167 return -1;
168}
169
170static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
171{
172 char str[100];
173 va_list ptr;
174
175 va_start(ptr, fmt);
176 vsnprintf(str, sizeof(str), fmt, ptr);
177 va_end(ptr);
178 return fdt_property_string(fdt, name, str);
179}
180
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700181static void get_basename(char *str, int size, const char *fname)
182{
183 const char *p, *start, *end;
184 int len;
185
186 /*
187 * Use the base name as the 'name' field. So for example:
188 *
189 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
190 * becomes "sun7i-a20-bananapro"
191 */
192 p = strrchr(fname, '/');
193 start = p ? p + 1 : fname;
194 p = strrchr(fname, '.');
195 end = p ? p : fname + strlen(fname);
196 len = end - start;
197 if (len >= size)
198 len = size - 1;
199 memcpy(str, start, len);
200 str[len] = '\0';
201}
202
Simon Glass88e31cb2016-02-22 22:55:51 -0700203/**
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100204 * fit_add_hash_or_sign() - Add a hash or signature node
Simon Glass611fd912020-05-27 07:24:55 -0600205 *
Sean Anderson5f150292022-05-16 16:11:08 -0400206 * @params: Image parameters
Simon Glass611fd912020-05-27 07:24:55 -0600207 * @fdt: Device tree to add to (in sequential-write mode)
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100208 * @is_images_subnode: true to add hash even if key name hint is provided
Sean Anderson5f150292022-05-16 16:11:08 -0400209 *
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100210 * If do_add_hash is false (default) and there is a key name hint, try to add
211 * a sign node to parent. Otherwise, just add a CRC. Rationale: if conf have
212 * to be signed, image/dt have to be hashed even if there is a key name hint.
Simon Glass611fd912020-05-27 07:24:55 -0600213 */
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100214static void fit_add_hash_or_sign(struct image_tool_params *params, void *fdt,
215 bool is_images_subnode)
Simon Glass611fd912020-05-27 07:24:55 -0600216{
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100217 const char *hash_algo = "crc32";
218 bool do_hash = false;
219 bool do_sign = false;
220
221 switch (params->auto_fit) {
222 case AF_OFF:
223 break;
224 case AF_HASHED_IMG:
225 do_hash = is_images_subnode;
226 break;
227 case AF_SIGNED_IMG:
228 do_sign = is_images_subnode;
229 break;
230 case AF_SIGNED_CONF:
231 if (is_images_subnode) {
232 do_hash = true;
233 hash_algo = "sha1";
234 } else {
235 do_sign = true;
Sean Anderson5f150292022-05-16 16:11:08 -0400236 }
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100237 break;
238 default:
239 fprintf(stderr,
240 "%s: Unsupported auto FIT mode %u\n",
241 params->cmdname, params->auto_fit);
242 break;
243 }
244
245 if (do_hash) {
246 fdt_begin_node(fdt, FIT_HASH_NODENAME);
247 fdt_property_string(fdt, FIT_ALGO_PROP, hash_algo);
248 fdt_end_node(fdt);
249 }
Sean Anderson5f150292022-05-16 16:11:08 -0400250
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100251 if (do_sign) {
252 fdt_begin_node(fdt, FIT_SIG_NODENAME);
Sean Anderson5f150292022-05-16 16:11:08 -0400253 fdt_property_string(fdt, FIT_ALGO_PROP, params->algo_name);
254 fdt_property_string(fdt, FIT_KEY_HINT, params->keyname);
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100255 fdt_end_node(fdt);
Sean Anderson5f150292022-05-16 16:11:08 -0400256 }
Simon Glass611fd912020-05-27 07:24:55 -0600257}
258
259/**
Simon Glass88e31cb2016-02-22 22:55:51 -0700260 * fit_write_images() - Write out a list of images to the FIT
261 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700262 * We always include the main image (params->datafile). If there are device
Andre Przywara9ef25e82017-12-04 02:05:12 +0000263 * tree files, we include an fdt- node for each of those too.
Simon Glass88e31cb2016-02-22 22:55:51 -0700264 */
265static int fit_write_images(struct image_tool_params *params, char *fdt)
266{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700267 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700268 const char *typename;
269 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700270 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700271 int ret;
272
273 fdt_begin_node(fdt, "images");
274
275 /* First the main image */
276 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000277 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glass88e31cb2016-02-22 22:55:51 -0700278 fdt_begin_node(fdt, str);
Michal Simeka4e678d2018-07-20 12:31:02 +0200279 fdt_property_string(fdt, FIT_DESC_PROP, params->imagename);
280 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
281 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassaeeea7f2016-06-30 10:52:12 -0600282 genimg_get_arch_short_name(params->arch));
Michal Simeka4e678d2018-07-20 12:31:02 +0200283 fdt_property_string(fdt, FIT_OS_PROP,
284 genimg_get_os_short_name(params->os));
285 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glass88e31cb2016-02-22 22:55:51 -0700286 genimg_get_comp_short_name(params->comp));
Michal Simeka4e678d2018-07-20 12:31:02 +0200287 fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr);
288 fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep);
Simon Glass88e31cb2016-02-22 22:55:51 -0700289
290 /*
291 * Put data last since it is large. SPL may only load the first part
292 * of the DT, so this way it can access all the above fields.
293 */
Michal Simeka4e678d2018-07-20 12:31:02 +0200294 ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile);
Simon Glass88e31cb2016-02-22 22:55:51 -0700295 if (ret)
296 return ret;
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100297 fit_add_hash_or_sign(params, fdt, true);
Simon Glass88e31cb2016-02-22 22:55:51 -0700298 fdt_end_node(fdt);
299
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700300 /* Now the device tree files if available */
301 upto = 0;
302 for (cont = params->content_head; cont; cont = cont->next) {
303 if (cont->type != IH_TYPE_FLATDT)
304 continue;
Michal Sojka8a90ce82019-09-13 12:43:12 +0200305 typename = genimg_get_type_short_name(cont->type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000306 snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700307 fdt_begin_node(fdt, str);
308
309 get_basename(str, sizeof(str), cont->fname);
Michal Simeka4e678d2018-07-20 12:31:02 +0200310 fdt_property_string(fdt, FIT_DESC_PROP, str);
311 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
312 cont->fname);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700313 if (ret)
314 return ret;
Michal Simeka4e678d2018-07-20 12:31:02 +0200315 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
316 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700317 genimg_get_arch_short_name(params->arch));
Michal Simeka4e678d2018-07-20 12:31:02 +0200318 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700319 genimg_get_comp_short_name(IH_COMP_NONE));
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100320 fit_add_hash_or_sign(params, fdt, true);
Sean Anderson5f150292022-05-16 16:11:08 -0400321 if (ret)
322 return ret;
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700323 fdt_end_node(fdt);
324 }
325
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100326 /* And a ramdisk file if available */
327 if (params->fit_ramdisk) {
Andre Przywara9ef25e82017-12-04 02:05:12 +0000328 fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100329
Michal Simeka4e678d2018-07-20 12:31:02 +0200330 fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP);
331 fdt_property_string(fdt, FIT_OS_PROP,
332 genimg_get_os_short_name(params->os));
Michal Sojka8a90ce82019-09-13 12:43:12 +0200333 fdt_property_string(fdt, FIT_ARCH_PROP,
334 genimg_get_arch_short_name(params->arch));
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100335
Michal Simeka4e678d2018-07-20 12:31:02 +0200336 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
337 params->fit_ramdisk);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100338 if (ret)
339 return ret;
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100340 fit_add_hash_or_sign(params, fdt, true);
Sean Anderson5f150292022-05-16 16:11:08 -0400341 if (ret)
342 return ret;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100343 fdt_end_node(fdt);
344 }
345
Simon Glass88e31cb2016-02-22 22:55:51 -0700346 fdt_end_node(fdt);
347
348 return 0;
349}
350
351/**
352 * fit_write_configs() - Write out a list of configurations to the FIT
353 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700354 * If there are device tree files, we include a configuration for each, which
355 * selects the main image (params->datafile) and its corresponding device
356 * tree file.
357 *
358 * Otherwise we just create a configuration with the main image in it.
Simon Glass88e31cb2016-02-22 22:55:51 -0700359 */
360static void fit_write_configs(struct image_tool_params *params, char *fdt)
361{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700362 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700363 const char *typename;
364 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700365 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700366
367 fdt_begin_node(fdt, "configurations");
Michal Simeka4e678d2018-07-20 12:31:02 +0200368 fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
Simon Glass88e31cb2016-02-22 22:55:51 -0700369
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700370 upto = 0;
371 for (cont = params->content_head; cont; cont = cont->next) {
372 if (cont->type != IH_TYPE_FLATDT)
373 continue;
374 typename = genimg_get_type_short_name(cont->type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000375 snprintf(str, sizeof(str), "conf-%d", ++upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700376 fdt_begin_node(fdt, str);
377
378 get_basename(str, sizeof(str), cont->fname);
Michal Simeka4e678d2018-07-20 12:31:02 +0200379 fdt_property_string(fdt, FIT_DESC_PROP, str);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700380
381 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000382 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700383 fdt_property_string(fdt, typename, str);
Abel Vesa7e0cb5e2019-03-12 08:34:32 +0000384 fdt_property_string(fdt, FIT_LOADABLE_PROP, str);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700385
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100386 if (params->fit_ramdisk)
387 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara9ef25e82017-12-04 02:05:12 +0000388 FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100389
Andre Przywara9ef25e82017-12-04 02:05:12 +0000390 snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700391 fdt_property_string(fdt, FIT_FDT_PROP, str);
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100392 fit_add_hash_or_sign(params, fdt, false);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700393 fdt_end_node(fdt);
394 }
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100395
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700396 if (!upto) {
Andre Przywara9ef25e82017-12-04 02:05:12 +0000397 fdt_begin_node(fdt, "conf-1");
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700398 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000399 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700400 fdt_property_string(fdt, typename, str);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100401
402 if (params->fit_ramdisk)
403 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara9ef25e82017-12-04 02:05:12 +0000404 FIT_RAMDISK_PROP "-1");
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100405 fit_add_hash_or_sign(params, fdt, false);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100406
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700407 fdt_end_node(fdt);
408 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700409
410 fdt_end_node(fdt);
411}
412
413static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
414{
415 int ret;
416
417 ret = fdt_create(fdt, size);
418 if (ret)
419 return ret;
420 fdt_finish_reservemap(fdt);
421 fdt_begin_node(fdt, "");
Michal Simeka4e678d2018-07-20 12:31:02 +0200422 fdt_property_strf(fdt, FIT_DESC_PROP,
Simon Glass88e31cb2016-02-22 22:55:51 -0700423 "%s image with one or more FDT blobs",
424 genimg_get_type_name(params->fit_image_type));
425 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
426 fdt_property_u32(fdt, "#address-cells", 1);
427 ret = fit_write_images(params, fdt);
428 if (ret)
429 return ret;
430 fit_write_configs(params, fdt);
431 fdt_end_node(fdt);
432 ret = fdt_finish(fdt);
433 if (ret)
434 return ret;
435
436 return fdt_totalsize(fdt);
437}
438
439static int fit_build(struct image_tool_params *params, const char *fname)
440{
441 char *buf;
442 int size;
443 int ret;
444 int fd;
445
446 size = fit_calc_size(params);
447 if (size < 0)
448 return -1;
Fabio Estevamc2248cb2020-07-27 21:03:13 -0300449 buf = calloc(1, size);
Simon Glass88e31cb2016-02-22 22:55:51 -0700450 if (!buf) {
451 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
452 params->cmdname, size);
453 return -1;
454 }
455 ret = fit_build_fdt(params, buf, size);
456 if (ret < 0) {
457 fprintf(stderr, "%s: Failed to build FIT image\n",
458 params->cmdname);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600459 goto err_buf;
Simon Glass88e31cb2016-02-22 22:55:51 -0700460 }
461 size = ret;
462 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
463 if (fd < 0) {
464 fprintf(stderr, "%s: Can't open %s: %s\n",
465 params->cmdname, fname, strerror(errno));
Tom Rinib8286652017-09-26 22:14:44 -0400466 goto err_buf;
Simon Glass88e31cb2016-02-22 22:55:51 -0700467 }
468 ret = write(fd, buf, size);
469 if (ret != size) {
470 fprintf(stderr, "%s: Can't write %s: %s\n",
471 params->cmdname, fname, strerror(errno));
Simon Glass88e31cb2016-02-22 22:55:51 -0700472 goto err;
473 }
474 close(fd);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600475 free(buf);
Simon Glass88e31cb2016-02-22 22:55:51 -0700476
477 return 0;
478err:
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600479 close(fd);
480err_buf:
Simon Glass88e31cb2016-02-22 22:55:51 -0700481 free(buf);
482 return -1;
483}
484
485/**
Simon Glassafd728c2016-02-22 22:55:53 -0700486 * fit_extract_data() - Move all data outside the FIT
487 *
488 * This takes a normal FIT file and removes all the 'data' properties from it.
489 * The data is placed in an area after the FIT so that it can be accessed
490 * using an offset into that area. The 'data' properties turn into
491 * 'data-offset' properties.
492 *
493 * This function cannot cope with FITs with 'data-offset' properties. All
494 * data must be in 'data' properties on entry.
495 */
496static int fit_extract_data(struct image_tool_params *params, const char *fname)
497{
Kever Yang8e238b52020-03-30 11:56:24 +0800498 void *buf = NULL;
Simon Glassafd728c2016-02-22 22:55:53 -0700499 int buf_ptr;
Roman Azarenko9ffe4d62023-08-25 10:10:14 +0200500 int fit_size, unpadded_size, new_size, pad_boundary;
Simon Glassafd728c2016-02-22 22:55:53 -0700501 int fd;
502 struct stat sbuf;
503 void *fdt;
504 int ret;
505 int images;
506 int node;
Kever Yang8e238b52020-03-30 11:56:24 +0800507 int image_number;
508 int align_size;
Simon Glassafd728c2016-02-22 22:55:53 -0700509
Tom Rini96642972020-05-06 11:05:17 -0400510 align_size = params->bl_len ? params->bl_len : 4;
Luca Boccassibff733f2019-05-14 19:35:02 +0100511 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false);
Simon Glassafd728c2016-02-22 22:55:53 -0700512 if (fd < 0)
513 return -EIO;
514 fit_size = fdt_totalsize(fdt);
515
Simon Glassafd728c2016-02-22 22:55:53 -0700516 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
517 if (images < 0) {
518 debug("%s: Cannot find /images node: %d\n", __func__, images);
519 ret = -EINVAL;
Simon Glass4e7505f2016-03-16 07:45:39 -0600520 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700521 }
Kever Yang8e238b52020-03-30 11:56:24 +0800522 image_number = fdtdec_get_child_count(fdt, images);
523
524 /*
525 * Allocate space to hold the image data we will extract,
526 * extral space allocate for image alignment to prevent overflow.
527 */
Fabio Estevamc2248cb2020-07-27 21:03:13 -0300528 buf = calloc(1, fit_size + (align_size * image_number));
Kever Yang8e238b52020-03-30 11:56:24 +0800529 if (!buf) {
530 ret = -ENOMEM;
531 goto err_munmap;
532 }
533 buf_ptr = 0;
Simon Glassafd728c2016-02-22 22:55:53 -0700534
535 for (node = fdt_first_subnode(fdt, images);
536 node >= 0;
537 node = fdt_next_subnode(fdt, node)) {
538 const char *data;
539 int len;
540
Michal Simeka4e678d2018-07-20 12:31:02 +0200541 data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
Simon Glassafd728c2016-02-22 22:55:53 -0700542 if (!data)
543 continue;
544 memcpy(buf + buf_ptr, data, len);
545 debug("Extracting data size %x\n", len);
546
Michal Simeka4e678d2018-07-20 12:31:02 +0200547 ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
Simon Glassafd728c2016-02-22 22:55:53 -0700548 if (ret) {
549 ret = -EPERM;
Simon Glass4e7505f2016-03-16 07:45:39 -0600550 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700551 }
Teddy Reeda8457622016-06-09 19:38:02 -0700552 if (params->external_offset > 0) {
553 /* An external offset positions the data absolutely. */
Michal Simeka4e678d2018-07-20 12:31:02 +0200554 fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
Teddy Reeda8457622016-06-09 19:38:02 -0700555 params->external_offset + buf_ptr);
556 } else {
Michal Simeka4e678d2018-07-20 12:31:02 +0200557 fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
558 buf_ptr);
Teddy Reeda8457622016-06-09 19:38:02 -0700559 }
Michal Simeka4e678d2018-07-20 12:31:02 +0200560 fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
Kever Yang8e238b52020-03-30 11:56:24 +0800561 buf_ptr += ALIGN(len, align_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700562 }
563
564 /* Pack the FDT and place the data after it */
565 fdt_pack(fdt);
566
Roman Azarenko9ffe4d62023-08-25 10:10:14 +0200567 unpadded_size = fdt_totalsize(fdt);
568 new_size = ALIGN(unpadded_size, align_size);
Kever Yang8e238b52020-03-30 11:56:24 +0800569 fdt_set_totalsize(fdt, new_size);
Roman Azarenko9ffe4d62023-08-25 10:10:14 +0200570 if (unpadded_size < fit_size) {
571 pad_boundary = new_size < fit_size ? new_size : fit_size;
572 memset(fdt + unpadded_size, 0, pad_boundary - unpadded_size);
573 }
Simon Glassafd728c2016-02-22 22:55:53 -0700574 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
575 debug("External data size %x\n", buf_ptr);
Simon Glassafd728c2016-02-22 22:55:53 -0700576 munmap(fdt, sbuf.st_size);
577
578 if (ftruncate(fd, new_size)) {
579 debug("%s: Failed to truncate file: %s\n", __func__,
580 strerror(errno));
581 ret = -EIO;
582 goto err;
583 }
Teddy Reeda8457622016-06-09 19:38:02 -0700584
585 /* Check if an offset for the external data was set. */
586 if (params->external_offset > 0) {
587 if (params->external_offset < new_size) {
Simon Glass00c06772022-01-09 20:13:38 -0700588 fprintf(stderr,
589 "External offset %x overlaps FIT length %x\n",
590 params->external_offset, new_size);
Teddy Reeda8457622016-06-09 19:38:02 -0700591 ret = -EINVAL;
592 goto err;
593 }
594 new_size = params->external_offset;
595 }
Simon Glassafd728c2016-02-22 22:55:53 -0700596 if (lseek(fd, new_size, SEEK_SET) < 0) {
597 debug("%s: Failed to seek to end of file: %s\n", __func__,
598 strerror(errno));
599 ret = -EIO;
600 goto err;
601 }
602 if (write(fd, buf, buf_ptr) != buf_ptr) {
603 debug("%s: Failed to write external data to file %s\n",
604 __func__, strerror(errno));
605 ret = -EIO;
606 goto err;
607 }
Tom Rinib8286652017-09-26 22:14:44 -0400608 free(buf);
Simon Glass4e7505f2016-03-16 07:45:39 -0600609 close(fd);
610 return 0;
Simon Glassafd728c2016-02-22 22:55:53 -0700611
Simon Glass4e7505f2016-03-16 07:45:39 -0600612err_munmap:
613 munmap(fdt, sbuf.st_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700614err:
Bin Meng6eb238742020-04-18 01:59:11 -0700615 free(buf);
Simon Glassafd728c2016-02-22 22:55:53 -0700616 close(fd);
617 return ret;
618}
619
Simon Glassd11b7002016-02-22 22:55:54 -0700620static int fit_import_data(struct image_tool_params *params, const char *fname)
621{
622 void *fdt, *old_fdt;
Lars Feyaertsadbc90f2023-10-02 10:00:14 +0200623 void *data = NULL;
624 const char *ext_data_prop = NULL;
Simon Glassd11b7002016-02-22 22:55:54 -0700625 int fit_size, new_size, size, data_base;
626 int fd;
627 struct stat sbuf;
628 int ret;
629 int images;
Aristo Chenfeb939c62025-06-10 07:41:18 +0000630 int confs;
Simon Glassd11b7002016-02-22 22:55:54 -0700631 int node;
632
Luca Boccassibff733f2019-05-14 19:35:02 +0100633 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false);
Simon Glassd11b7002016-02-22 22:55:54 -0700634 if (fd < 0)
635 return -EIO;
636 fit_size = fdt_totalsize(old_fdt);
Kever Yang8c11bc12020-03-30 11:56:22 +0800637 data_base = ALIGN(fit_size, 4);
Simon Glassd11b7002016-02-22 22:55:54 -0700638
639 /* Allocate space to hold the new FIT */
640 size = sbuf.st_size + 16384;
Fabio Estevamc2248cb2020-07-27 21:03:13 -0300641 fdt = calloc(1, size);
Simon Glassd11b7002016-02-22 22:55:54 -0700642 if (!fdt) {
643 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
644 __func__, size);
645 ret = -ENOMEM;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700646 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700647 }
648 ret = fdt_open_into(old_fdt, fdt, size);
649 if (ret) {
650 debug("%s: Failed to expand FIT: %s\n", __func__,
651 fdt_strerror(errno));
652 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700653 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700654 }
655
656 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
657 if (images < 0) {
658 debug("%s: Cannot find /images node: %d\n", __func__, images);
659 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700660 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700661 }
662
663 for (node = fdt_first_subnode(fdt, images);
664 node >= 0;
665 node = fdt_next_subnode(fdt, node)) {
666 int buf_ptr;
667 int len;
668
Lars Feyaertsadbc90f2023-10-02 10:00:14 +0200669 /*
670 * FIT_DATA_OFFSET_PROP and FIT_DATA_POSITION_PROP are never both present,
671 * but if they are, prefer FIT_DATA_OFFSET_PROP as it was there first
672 */
673 buf_ptr = fdtdec_get_int(fdt, node, FIT_DATA_POSITION_PROP, -1);
674 if (buf_ptr != -1) {
675 ext_data_prop = FIT_DATA_POSITION_PROP;
676 data = old_fdt + buf_ptr;
677 }
678 buf_ptr = fdtdec_get_int(fdt, node, FIT_DATA_OFFSET_PROP, -1);
679 if (buf_ptr != -1) {
680 ext_data_prop = FIT_DATA_OFFSET_PROP;
681 data = old_fdt + data_base + buf_ptr;
682 }
683 len = fdtdec_get_int(fdt, node, FIT_DATA_SIZE_PROP, -1);
684 if (!data || len == -1)
Simon Glassd11b7002016-02-22 22:55:54 -0700685 continue;
686 debug("Importing data size %x\n", len);
687
Lars Feyaertsadbc90f2023-10-02 10:00:14 +0200688 ret = fdt_setprop(fdt, node, FIT_DATA_PROP, data, len);
689 ret = fdt_delprop(fdt, node, ext_data_prop);
690
Simon Glassd11b7002016-02-22 22:55:54 -0700691 if (ret) {
692 debug("%s: Failed to write property: %s\n", __func__,
693 fdt_strerror(ret));
694 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700695 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700696 }
697 }
698
Aristo Chenfeb939c62025-06-10 07:41:18 +0000699 confs = fdt_path_offset(fdt, FIT_CONFS_PATH);
700 static const char * const props[] = { FIT_KERNEL_PROP,
701 FIT_RAMDISK_PROP,
702 FIT_FDT_PROP,
703 FIT_LOADABLE_PROP,
704 FIT_FPGA_PROP,
705 FIT_FIRMWARE_PROP,
706 FIT_SCRIPT_PROP};
707
708 fdt_for_each_subnode(node, fdt, confs) {
709 const char *conf_name = fdt_get_name(fdt, node, NULL);
710
711 for (int i = 0; i < ARRAY_SIZE(props); i++) {
712 int count = fdt_stringlist_count(fdt, node, props[i]);
713
714 if (count < 0)
715 continue;
716
717 for (int j = 0; j < count; j++) {
718 const char *img_name =
719 fdt_stringlist_get(fdt, node, props[i], j, NULL);
720 if (!img_name || !*img_name)
721 continue;
722
723 int img = fdt_subnode_offset(fdt, images, img_name);
724
725 if (img < 0) {
726 fprintf(stderr,
727 "Error: configuration '%s' references undefined image '%s' in property '%s'\n",
728 conf_name, img_name, props[i]);
729 ret = FDT_ERR_NOTFOUND;
730 goto err_munmap;
731 }
732 }
733 }
734 }
735
Lihua Zhao4536ef82020-04-18 01:59:10 -0700736 munmap(old_fdt, sbuf.st_size);
737
Tom Rinia29829e2017-10-07 11:27:59 -0400738 /* Close the old fd so we can re-use it. */
Simon Glassd11b7002016-02-22 22:55:54 -0700739 close(fd);
740
741 /* Pack the FDT and place the data after it */
742 fdt_pack(fdt);
743
744 new_size = fdt_totalsize(fdt);
745 debug("Size expanded from %x to %x\n", fit_size, new_size);
746
747 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
748 if (fd < 0) {
749 fprintf(stderr, "%s: Can't open %s: %s\n",
750 params->cmdname, fname, strerror(errno));
Tom Rinia29829e2017-10-07 11:27:59 -0400751 ret = -EIO;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700752 goto err;
Simon Glassd11b7002016-02-22 22:55:54 -0700753 }
754 if (write(fd, fdt, new_size) != new_size) {
755 debug("%s: Failed to write external data to file %s\n",
756 __func__, strerror(errno));
757 ret = -EIO;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700758 goto err;
Simon Glassd11b7002016-02-22 22:55:54 -0700759 }
Simon Glassd11b7002016-02-22 22:55:54 -0700760
Lihua Zhao4536ef82020-04-18 01:59:10 -0700761 free(fdt);
Tom Rinia29829e2017-10-07 11:27:59 -0400762 close(fd);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700763 return 0;
764
765err_munmap:
Tom Rinib8286652017-09-26 22:14:44 -0400766 munmap(old_fdt, sbuf.st_size);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700767err:
Simon Glass5014cc12016-03-16 07:45:38 -0600768 free(fdt);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700769 close(fd);
Simon Glassd11b7002016-02-22 22:55:54 -0700770 return ret;
771}
772
Simon Glassafd728c2016-02-22 22:55:53 -0700773/**
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530774 * fit_handle_file - main FIT file processing function
775 *
776 * fit_handle_file() runs dtc to convert .its to .itb, includes
777 * binary data, updates timestamp property and calculates hashes.
778 *
779 * datafile - .its file
780 * imagefile - .itb file
781 *
782 * returns:
783 * only on success, otherwise calls exit (EXIT_FAILURE);
784 */
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700785static int fit_handle_file(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530786{
787 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
Philippe Reynes3148e422019-12-18 18:25:41 +0100788 char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0};
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530789 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
Simon Glass802aa822014-06-02 22:04:53 -0600790 size_t size_inc;
Aristo Chen48f5c272025-06-10 07:41:17 +0000791 int ret = EXIT_FAILURE;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530792
793 /* Flattened Image Tree (FIT) format handling */
794 debug ("FIT format handling\n");
795
796 /* call dtc to include binary properties into the tmp file */
797 if (strlen (params->imagefile) +
798 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
799 fprintf (stderr, "%s: Image file name (%s) too long, "
Sven Roederer88be5d32021-05-25 23:15:27 +0200800 "can't create tmpfile.\n",
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530801 params->imagefile, params->cmdname);
802 return (EXIT_FAILURE);
803 }
804 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
805
Simon Glassce8c3ca2013-06-13 15:10:05 -0700806 /* We either compile the source file, or use the existing FIT image */
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100807 if (params->auto_fit) {
Simon Glass88e31cb2016-02-22 22:55:51 -0700808 if (fit_build(params, tmpfile)) {
809 fprintf(stderr, "%s: failed to build FIT\n",
810 params->cmdname);
811 return EXIT_FAILURE;
812 }
813 *cmd = '\0';
814 } else if (params->datafile) {
Stefan Theilff6c3b22018-03-08 09:00:13 +0100815 /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
816 snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
817 MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
Simon Glassce8c3ca2013-06-13 15:10:05 -0700818 debug("Trying to execute \"%s\"\n", cmd);
819 } else {
Mirza, Taimoor74a21dd2017-10-04 20:28:03 +0500820 snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
Simon Glassce8c3ca2013-06-13 15:10:05 -0700821 params->imagefile, tmpfile);
822 }
Sven Roederercbcb3992020-04-27 02:08:39 +0200823 if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) {
824 fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n");
825 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100826
Simon Glass88e31cb2016-02-22 22:55:51 -0700827 if (*cmd && system(cmd) == -1) {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530828 fprintf (stderr, "%s: system(%s) failed: %s\n",
829 params->cmdname, cmd, strerror(errno));
Simon Glass4161c412013-05-08 08:05:57 +0000830 goto err_system;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530831 }
832
Simon Glassd11b7002016-02-22 22:55:54 -0700833 /* Move the data so it is internal to the FIT, if needed */
834 ret = fit_import_data(params, tmpfile);
835 if (ret)
836 goto err_system;
837
Simon Glass802aa822014-06-02 22:04:53 -0600838 /*
Philippe Reynes3148e422019-12-18 18:25:41 +0100839 * Copy the tmpfile to bakfile, then in the following loop
840 * we copy bakfile to tmpfile. So we always start from the
841 * beginning.
842 */
843 sprintf(bakfile, "%s%s", tmpfile, ".bak");
844 rename(tmpfile, bakfile);
845
846 /*
Simon Glass802aa822014-06-02 22:04:53 -0600847 * Set hashes for images in the blob. Unfortunately we may need more
848 * space in either FDT, so keep trying until we succeed.
849 *
850 * Note: this is pretty inefficient for signing, since we must
851 * calculate the signature every time. It would be better to calculate
852 * all the data and then store it in a separate step. However, this
853 * would be considerably more complex to implement. Generally a few
854 * steps of this loop is enough to sign with several keys.
855 */
856 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
Philippe Reynes3148e422019-12-18 18:25:41 +0100857 if (copyfile(bakfile, tmpfile) < 0) {
858 printf("Can't copy %s to %s\n", bakfile, tmpfile);
859 ret = -EIO;
860 break;
861 }
Simon Glass802aa822014-06-02 22:04:53 -0600862 ret = fit_add_file_data(params, size_inc, tmpfile);
863 if (!ret || ret != -ENOSPC)
864 break;
Simon Glassb4d8b092013-06-13 15:10:04 -0700865 }
866
Simon Glass802aa822014-06-02 22:04:53 -0600867 if (ret) {
Simon Glass061752e2016-07-03 09:40:43 -0600868 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
869 params->cmdname, ret);
Simon Glass802aa822014-06-02 22:04:53 -0600870 goto err_system;
Simon Glassb4d8b092013-06-13 15:10:04 -0700871 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530872
Simon Glassafd728c2016-02-22 22:55:53 -0700873 /* Move the data so it is external to the FIT, if requested */
874 if (params->external_data) {
875 ret = fit_extract_data(params, tmpfile);
876 if (ret)
877 goto err_system;
878 }
879
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530880 if (rename (tmpfile, params->imagefile) == -1) {
881 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
882 params->cmdname, tmpfile, params->imagefile,
883 strerror (errno));
884 unlink (tmpfile);
Philippe Reynes3148e422019-12-18 18:25:41 +0100885 unlink(bakfile);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530886 unlink (params->imagefile);
Simon Glass802aa822014-06-02 22:04:53 -0600887 return EXIT_FAILURE;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530888 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100889 unlink(bakfile);
Simon Glass802aa822014-06-02 22:04:53 -0600890 return EXIT_SUCCESS;
Simon Glass4161c412013-05-08 08:05:57 +0000891
Simon Glass4161c412013-05-08 08:05:57 +0000892err_system:
893 unlink(tmpfile);
Philippe Reynes3148e422019-12-18 18:25:41 +0100894 unlink(bakfile);
Aristo Chen48f5c272025-06-10 07:41:17 +0000895 return ret;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530896}
897
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200898/**
899 * fit_image_extract - extract a FIT component image
900 * @fit: pointer to the FIT format image header
901 * @image_noffset: offset of the component image node
902 * @file_name: name of the file to store the FIT sub-image
903 *
904 * returns:
905 * zero in case of success or a negative value if fail.
906 */
907static int fit_image_extract(
908 const void *fit,
909 int image_noffset,
910 const char *file_name)
911{
912 const void *file_data;
913 size_t file_size = 0;
Andrew F. Davis166d5e92019-09-17 17:09:34 -0400914 int ret;
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200915
Andrew F. Davis166d5e92019-09-17 17:09:34 -0400916 /* get the data address and size of component at offset "image_noffset" */
Simon Glass833445d2025-01-10 17:00:13 -0700917 ret = fit_image_get_data(fit, image_noffset, &file_data, &file_size);
Andrew F. Davis166d5e92019-09-17 17:09:34 -0400918 if (ret) {
919 fprintf(stderr, "Could not get component information\n");
920 return ret;
921 }
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200922
923 /* save the "file_data" into the file specified by "file_name" */
924 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
925}
926
927/**
928 * fit_extract_contents - retrieve a sub-image component from the FIT image
929 * @ptr: pointer to the FIT format image header
930 * @params: command line parameters
931 *
932 * returns:
933 * zero in case of success or a negative value if fail.
934 */
935static int fit_extract_contents(void *ptr, struct image_tool_params *params)
936{
937 int images_noffset;
938 int noffset;
939 int ndepth;
940 const void *fit = ptr;
941 int count = 0;
942 const char *p;
943
944 /* Indent string is defined in header image.h */
945 p = IMAGE_INDENT_STRING;
946
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200947 /* Find images parent node offset */
948 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
949 if (images_noffset < 0) {
950 printf("Can't find images parent node '%s' (%s)\n",
951 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
952 return -1;
953 }
954
955 /* Avoid any overrun */
956 count = fit_get_subimage_count(fit, images_noffset);
957 if ((params->pflag < 0) || (count <= params->pflag)) {
958 printf("No such component at '%d'\n", params->pflag);
959 return -1;
960 }
961
962 /* Process its subnodes, extract the desired component from image */
963 for (ndepth = 0, count = 0,
964 noffset = fdt_next_node(fit, images_noffset, &ndepth);
965 (noffset >= 0) && (ndepth > 0);
966 noffset = fdt_next_node(fit, noffset, &ndepth)) {
967 if (ndepth == 1) {
968 /*
969 * Direct child node of the images parent node,
970 * i.e. component image node.
971 */
972 if (params->pflag == count) {
973 printf("Extracted:\n%s Image %u (%s)\n", p,
974 count, fit_get_name(fit, noffset, NULL));
975
976 fit_image_print(fit, noffset, p);
977
978 return fit_image_extract(fit, noffset,
979 params->outfile);
980 }
981
982 count++;
983 }
984 }
985
986 return 0;
987}
988
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700989static int fit_check_params(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530990{
Massimo Pegorer13878dd2023-01-05 10:31:09 +0100991 if (params->auto_fit)
Simon Glass88e31cb2016-02-22 22:55:51 -0700992 return 0;
Heinrich Schuchardt915f5aa2019-12-11 13:51:38 +0100993 return ((params->dflag && params->fflag) ||
994 (params->fflag && params->lflag) ||
995 (params->lflag && params->dflag));
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530996}
997
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200998U_BOOT_IMAGE_TYPE(
999 fitimage,
1000 "FIT Image support",
Simon Glassbb7d3bb2022-09-06 20:26:52 -06001001 sizeof(struct legacy_img_hdr),
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -02001002 (void *)&header,
1003 fit_check_params,
1004 fit_verify_header,
Pali Rohár0ed41e22023-03-29 21:25:54 +02001005 fit_print_header,
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -02001006 NULL,
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -02001007 fit_extract_contents,
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -02001008 fit_check_image_types,
1009 fit_handle_file,
1010 NULL /* FIT images use DTB header */
1011);