blob: ae30f807834927819e50a364cf615549825cc2ba [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskar89a4d6b2009-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 Wadaskar89a4d6b2009-08-19 17:36:46 +053014 */
15
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -070016#include "imagetool.h"
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010017#include "fit_common.h"
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053018#include "mkimage.h"
19#include <image.h>
Sven Roedererea5d3732020-04-27 02:08:39 +020020#include <string.h>
Simon Glass8e35bb02016-02-22 22:55:51 -070021#include <stdarg.h>
22#include <version.h>
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053023#include <u-boot/crc.h>
24
25static image_header_t header;
26
Simon Glassa9468112014-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 Boccassi7d574852019-05-14 19:35:02 +010037 tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true,
38 false);
Simon Glassa9468112014-06-02 22:04:53 -060039 if (tfd < 0)
40 return -EIO;
41
42 if (params->keydest) {
43 struct stat dest_sbuf;
44
45 destfd = mmap_fdt(params->cmdname, params->keydest, size_inc,
Luca Boccassi7d574852019-05-14 19:35:02 +010046 &dest_blob, &dest_sbuf, false,
47 false);
Simon Glassa9468112014-06-02 22:04:53 -060048 if (destfd < 0) {
49 ret = -EIO;
50 goto err_keydest;
51 }
52 destfd_size = dest_sbuf.st_size;
53 }
54
55 /* for first image creation, add a timestamp at offset 0 i.e., root */
Simon Glass152b2462020-07-09 18:39:43 -060056 if (params->datafile || params->reset_timestamp) {
Alex Kiernan87925df2018-06-20 20:10:51 +000057 time_t time = imagetool_get_source_date(params->cmdname,
58 sbuf.st_mtime);
Vagrant Cascadian58470842016-06-16 12:28:40 -070059 ret = fit_set_timestamp(ptr, 0, time);
60 }
Simon Glassa9468112014-06-02 22:04:53 -060061
62 if (!ret) {
Philippe Reynes7298e422019-12-18 18:25:41 +010063 ret = fit_cipher_data(params->keydir, dest_blob, ptr,
64 params->comment,
65 params->require_keys,
66 params->engine_id,
67 params->cmdname);
68 }
69
70 if (!ret) {
Alexandru Gagniuc36bfcb62021-02-19 12:45:17 -060071 ret = fit_add_verification_data(params->keydir,
72 params->keyfile, dest_blob, ptr,
Simon Glassa9468112014-06-02 22:04:53 -060073 params->comment,
George McCollisterf1ca1fd2017-01-06 13:14:17 -060074 params->require_keys,
Alex Kiernan795f4522018-06-20 20:10:52 +000075 params->engine_id,
76 params->cmdname);
Simon Glassa9468112014-06-02 22:04:53 -060077 }
78
79 if (dest_blob) {
80 munmap(dest_blob, destfd_size);
81 close(destfd);
82 }
83
84err_keydest:
85 munmap(ptr, sbuf.st_size);
86 close(tfd);
Simon Glassa9468112014-06-02 22:04:53 -060087 return ret;
88}
89
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053090/**
Simon Glass8e35bb02016-02-22 22:55:51 -070091 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
92 */
93static int fit_calc_size(struct image_tool_params *params)
94{
Simon Glassfb4cce02016-02-22 22:55:52 -070095 struct content_info *cont;
Simon Glass8e35bb02016-02-22 22:55:51 -070096 int size, total_size;
97
98 size = imagetool_get_filesize(params, params->datafile);
99 if (size < 0)
100 return -1;
Simon Glass8e35bb02016-02-22 22:55:51 -0700101 total_size = size;
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100102
103 if (params->fit_ramdisk) {
104 size = imagetool_get_filesize(params, params->fit_ramdisk);
105 if (size < 0)
106 return -1;
107 total_size += size;
108 }
109
Simon Glassfb4cce02016-02-22 22:55:52 -0700110 for (cont = params->content_head; cont; cont = cont->next) {
111 size = imagetool_get_filesize(params, cont->fname);
112 if (size < 0)
113 return -1;
Simon Glass8e35bb02016-02-22 22:55:51 -0700114
Simon Glass31729112020-05-27 07:24:55 -0600115 /* Add space for properties and hash node */
Simon Glassfb4cce02016-02-22 22:55:52 -0700116 total_size += size + 300;
117 }
Simon Glass8e35bb02016-02-22 22:55:51 -0700118
119 /* Add plenty of space for headers, properties, nodes, etc. */
120 total_size += 4096;
121
122 return total_size;
123}
124
125static int fdt_property_file(struct image_tool_params *params,
126 void *fdt, const char *name, const char *fname)
127{
128 struct stat sbuf;
129 void *ptr;
130 int ret;
131 int fd;
132
133 fd = open(fname, O_RDWR | O_BINARY);
134 if (fd < 0) {
135 fprintf(stderr, "%s: Can't open %s: %s\n",
136 params->cmdname, fname, strerror(errno));
137 return -1;
138 }
139
140 if (fstat(fd, &sbuf) < 0) {
141 fprintf(stderr, "%s: Can't stat %s: %s\n",
142 params->cmdname, fname, strerror(errno));
143 goto err;
144 }
145
146 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
147 if (ret)
Simon Glass3bd3a542016-03-16 07:45:42 -0600148 goto err;
Simon Glass8e35bb02016-02-22 22:55:51 -0700149 ret = read(fd, ptr, sbuf.st_size);
150 if (ret != sbuf.st_size) {
151 fprintf(stderr, "%s: Can't read %s: %s\n",
152 params->cmdname, fname, strerror(errno));
153 goto err;
154 }
Simon Glass3bd3a542016-03-16 07:45:42 -0600155 close(fd);
Simon Glass8e35bb02016-02-22 22:55:51 -0700156
157 return 0;
158err:
159 close(fd);
160 return -1;
161}
162
163static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
164{
165 char str[100];
166 va_list ptr;
167
168 va_start(ptr, fmt);
169 vsnprintf(str, sizeof(str), fmt, ptr);
170 va_end(ptr);
171 return fdt_property_string(fdt, name, str);
172}
173
Simon Glassfb4cce02016-02-22 22:55:52 -0700174static void get_basename(char *str, int size, const char *fname)
175{
176 const char *p, *start, *end;
177 int len;
178
179 /*
180 * Use the base name as the 'name' field. So for example:
181 *
182 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
183 * becomes "sun7i-a20-bananapro"
184 */
185 p = strrchr(fname, '/');
186 start = p ? p + 1 : fname;
187 p = strrchr(fname, '.');
188 end = p ? p : fname + strlen(fname);
189 len = end - start;
190 if (len >= size)
191 len = size - 1;
192 memcpy(str, start, len);
193 str[len] = '\0';
194}
195
Simon Glass8e35bb02016-02-22 22:55:51 -0700196/**
Simon Glass31729112020-05-27 07:24:55 -0600197 * add_crc_node() - Add a hash node to request a CRC checksum for an image
198 *
199 * @fdt: Device tree to add to (in sequential-write mode)
200 */
201static void add_crc_node(void *fdt)
202{
203 fdt_begin_node(fdt, "hash-1");
204 fdt_property_string(fdt, FIT_ALGO_PROP, "crc32");
205 fdt_end_node(fdt);
206}
207
208/**
Simon Glass8e35bb02016-02-22 22:55:51 -0700209 * fit_write_images() - Write out a list of images to the FIT
210 *
Simon Glassfb4cce02016-02-22 22:55:52 -0700211 * We always include the main image (params->datafile). If there are device
Andre Przywara2eda8e92017-12-04 02:05:12 +0000212 * tree files, we include an fdt- node for each of those too.
Simon Glass8e35bb02016-02-22 22:55:51 -0700213 */
214static int fit_write_images(struct image_tool_params *params, char *fdt)
215{
Simon Glassfb4cce02016-02-22 22:55:52 -0700216 struct content_info *cont;
Simon Glass8e35bb02016-02-22 22:55:51 -0700217 const char *typename;
218 char str[100];
Simon Glassfb4cce02016-02-22 22:55:52 -0700219 int upto;
Simon Glass8e35bb02016-02-22 22:55:51 -0700220 int ret;
221
222 fdt_begin_node(fdt, "images");
223
224 /* First the main image */
225 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara2eda8e92017-12-04 02:05:12 +0000226 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glass8e35bb02016-02-22 22:55:51 -0700227 fdt_begin_node(fdt, str);
Michal Simek4a8b6e02018-07-20 12:31:02 +0200228 fdt_property_string(fdt, FIT_DESC_PROP, params->imagename);
229 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
230 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glass3a45f382016-06-30 10:52:12 -0600231 genimg_get_arch_short_name(params->arch));
Michal Simek4a8b6e02018-07-20 12:31:02 +0200232 fdt_property_string(fdt, FIT_OS_PROP,
233 genimg_get_os_short_name(params->os));
234 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glass8e35bb02016-02-22 22:55:51 -0700235 genimg_get_comp_short_name(params->comp));
Michal Simek4a8b6e02018-07-20 12:31:02 +0200236 fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr);
237 fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep);
Simon Glass8e35bb02016-02-22 22:55:51 -0700238
239 /*
240 * Put data last since it is large. SPL may only load the first part
241 * of the DT, so this way it can access all the above fields.
242 */
Michal Simek4a8b6e02018-07-20 12:31:02 +0200243 ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile);
Simon Glass8e35bb02016-02-22 22:55:51 -0700244 if (ret)
245 return ret;
Simon Glass31729112020-05-27 07:24:55 -0600246 add_crc_node(fdt);
Simon Glass8e35bb02016-02-22 22:55:51 -0700247 fdt_end_node(fdt);
248
Simon Glassfb4cce02016-02-22 22:55:52 -0700249 /* Now the device tree files if available */
250 upto = 0;
251 for (cont = params->content_head; cont; cont = cont->next) {
252 if (cont->type != IH_TYPE_FLATDT)
253 continue;
Michal Sojka12e288a2019-09-13 12:43:12 +0200254 typename = genimg_get_type_short_name(cont->type);
Andre Przywara2eda8e92017-12-04 02:05:12 +0000255 snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
Simon Glassfb4cce02016-02-22 22:55:52 -0700256 fdt_begin_node(fdt, str);
257
258 get_basename(str, sizeof(str), cont->fname);
Michal Simek4a8b6e02018-07-20 12:31:02 +0200259 fdt_property_string(fdt, FIT_DESC_PROP, str);
260 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
261 cont->fname);
Simon Glassfb4cce02016-02-22 22:55:52 -0700262 if (ret)
263 return ret;
Michal Simek4a8b6e02018-07-20 12:31:02 +0200264 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
265 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassfb4cce02016-02-22 22:55:52 -0700266 genimg_get_arch_short_name(params->arch));
Michal Simek4a8b6e02018-07-20 12:31:02 +0200267 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glassfb4cce02016-02-22 22:55:52 -0700268 genimg_get_comp_short_name(IH_COMP_NONE));
Simon Glass31729112020-05-27 07:24:55 -0600269 add_crc_node(fdt);
Simon Glassfb4cce02016-02-22 22:55:52 -0700270 fdt_end_node(fdt);
271 }
272
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100273 /* And a ramdisk file if available */
274 if (params->fit_ramdisk) {
Andre Przywara2eda8e92017-12-04 02:05:12 +0000275 fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100276
Michal Simek4a8b6e02018-07-20 12:31:02 +0200277 fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP);
278 fdt_property_string(fdt, FIT_OS_PROP,
279 genimg_get_os_short_name(params->os));
Michal Sojka12e288a2019-09-13 12:43:12 +0200280 fdt_property_string(fdt, FIT_ARCH_PROP,
281 genimg_get_arch_short_name(params->arch));
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100282
Michal Simek4a8b6e02018-07-20 12:31:02 +0200283 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
284 params->fit_ramdisk);
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100285 if (ret)
286 return ret;
Simon Glass31729112020-05-27 07:24:55 -0600287 add_crc_node(fdt);
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100288 fdt_end_node(fdt);
289 }
290
Simon Glass8e35bb02016-02-22 22:55:51 -0700291 fdt_end_node(fdt);
292
293 return 0;
294}
295
296/**
297 * fit_write_configs() - Write out a list of configurations to the FIT
298 *
Simon Glassfb4cce02016-02-22 22:55:52 -0700299 * If there are device tree files, we include a configuration for each, which
300 * selects the main image (params->datafile) and its corresponding device
301 * tree file.
302 *
303 * Otherwise we just create a configuration with the main image in it.
Simon Glass8e35bb02016-02-22 22:55:51 -0700304 */
305static void fit_write_configs(struct image_tool_params *params, char *fdt)
306{
Simon Glassfb4cce02016-02-22 22:55:52 -0700307 struct content_info *cont;
Simon Glass8e35bb02016-02-22 22:55:51 -0700308 const char *typename;
309 char str[100];
Simon Glassfb4cce02016-02-22 22:55:52 -0700310 int upto;
Simon Glass8e35bb02016-02-22 22:55:51 -0700311
312 fdt_begin_node(fdt, "configurations");
Michal Simek4a8b6e02018-07-20 12:31:02 +0200313 fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
Simon Glass8e35bb02016-02-22 22:55:51 -0700314
Simon Glassfb4cce02016-02-22 22:55:52 -0700315 upto = 0;
316 for (cont = params->content_head; cont; cont = cont->next) {
317 if (cont->type != IH_TYPE_FLATDT)
318 continue;
319 typename = genimg_get_type_short_name(cont->type);
Andre Przywara2eda8e92017-12-04 02:05:12 +0000320 snprintf(str, sizeof(str), "conf-%d", ++upto);
Simon Glassfb4cce02016-02-22 22:55:52 -0700321 fdt_begin_node(fdt, str);
322
323 get_basename(str, sizeof(str), cont->fname);
Michal Simek4a8b6e02018-07-20 12:31:02 +0200324 fdt_property_string(fdt, FIT_DESC_PROP, str);
Simon Glassfb4cce02016-02-22 22:55:52 -0700325
326 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara2eda8e92017-12-04 02:05:12 +0000327 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassfb4cce02016-02-22 22:55:52 -0700328 fdt_property_string(fdt, typename, str);
Abel Vesacabde442019-03-12 08:34:32 +0000329 fdt_property_string(fdt, FIT_LOADABLE_PROP, str);
Simon Glassfb4cce02016-02-22 22:55:52 -0700330
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100331 if (params->fit_ramdisk)
332 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara2eda8e92017-12-04 02:05:12 +0000333 FIT_RAMDISK_PROP "-1");
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100334
Andre Przywara2eda8e92017-12-04 02:05:12 +0000335 snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
Simon Glassfb4cce02016-02-22 22:55:52 -0700336 fdt_property_string(fdt, FIT_FDT_PROP, str);
337 fdt_end_node(fdt);
338 }
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100339
Simon Glassfb4cce02016-02-22 22:55:52 -0700340 if (!upto) {
Andre Przywara2eda8e92017-12-04 02:05:12 +0000341 fdt_begin_node(fdt, "conf-1");
Simon Glassfb4cce02016-02-22 22:55:52 -0700342 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara2eda8e92017-12-04 02:05:12 +0000343 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassfb4cce02016-02-22 22:55:52 -0700344 fdt_property_string(fdt, typename, str);
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100345
346 if (params->fit_ramdisk)
347 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara2eda8e92017-12-04 02:05:12 +0000348 FIT_RAMDISK_PROP "-1");
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100349
Simon Glassfb4cce02016-02-22 22:55:52 -0700350 fdt_end_node(fdt);
351 }
Simon Glass8e35bb02016-02-22 22:55:51 -0700352
353 fdt_end_node(fdt);
354}
355
356static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
357{
358 int ret;
359
360 ret = fdt_create(fdt, size);
361 if (ret)
362 return ret;
363 fdt_finish_reservemap(fdt);
364 fdt_begin_node(fdt, "");
Michal Simek4a8b6e02018-07-20 12:31:02 +0200365 fdt_property_strf(fdt, FIT_DESC_PROP,
Simon Glass8e35bb02016-02-22 22:55:51 -0700366 "%s image with one or more FDT blobs",
367 genimg_get_type_name(params->fit_image_type));
368 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
369 fdt_property_u32(fdt, "#address-cells", 1);
370 ret = fit_write_images(params, fdt);
371 if (ret)
372 return ret;
373 fit_write_configs(params, fdt);
374 fdt_end_node(fdt);
375 ret = fdt_finish(fdt);
376 if (ret)
377 return ret;
378
379 return fdt_totalsize(fdt);
380}
381
382static int fit_build(struct image_tool_params *params, const char *fname)
383{
384 char *buf;
385 int size;
386 int ret;
387 int fd;
388
389 size = fit_calc_size(params);
390 if (size < 0)
391 return -1;
Fabio Estevamaaa91a42020-07-27 21:03:13 -0300392 buf = calloc(1, size);
Simon Glass8e35bb02016-02-22 22:55:51 -0700393 if (!buf) {
394 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
395 params->cmdname, size);
396 return -1;
397 }
398 ret = fit_build_fdt(params, buf, size);
399 if (ret < 0) {
400 fprintf(stderr, "%s: Failed to build FIT image\n",
401 params->cmdname);
Simon Glass7b0bbd82016-03-16 07:45:41 -0600402 goto err_buf;
Simon Glass8e35bb02016-02-22 22:55:51 -0700403 }
404 size = ret;
405 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
406 if (fd < 0) {
407 fprintf(stderr, "%s: Can't open %s: %s\n",
408 params->cmdname, fname, strerror(errno));
Tom Rini3c2dff52017-09-26 22:14:44 -0400409 goto err_buf;
Simon Glass8e35bb02016-02-22 22:55:51 -0700410 }
411 ret = write(fd, buf, size);
412 if (ret != size) {
413 fprintf(stderr, "%s: Can't write %s: %s\n",
414 params->cmdname, fname, strerror(errno));
Simon Glass8e35bb02016-02-22 22:55:51 -0700415 goto err;
416 }
417 close(fd);
Simon Glass7b0bbd82016-03-16 07:45:41 -0600418 free(buf);
Simon Glass8e35bb02016-02-22 22:55:51 -0700419
420 return 0;
421err:
Simon Glass7b0bbd82016-03-16 07:45:41 -0600422 close(fd);
423err_buf:
Simon Glass8e35bb02016-02-22 22:55:51 -0700424 free(buf);
425 return -1;
426}
427
428/**
Simon Glass722ebc82016-02-22 22:55:53 -0700429 * fit_extract_data() - Move all data outside the FIT
430 *
431 * This takes a normal FIT file and removes all the 'data' properties from it.
432 * The data is placed in an area after the FIT so that it can be accessed
433 * using an offset into that area. The 'data' properties turn into
434 * 'data-offset' properties.
435 *
436 * This function cannot cope with FITs with 'data-offset' properties. All
437 * data must be in 'data' properties on entry.
438 */
439static int fit_extract_data(struct image_tool_params *params, const char *fname)
440{
Kever Yangebfe6112020-03-30 11:56:24 +0800441 void *buf = NULL;
Simon Glass722ebc82016-02-22 22:55:53 -0700442 int buf_ptr;
443 int fit_size, new_size;
444 int fd;
445 struct stat sbuf;
446 void *fdt;
447 int ret;
448 int images;
449 int node;
Kever Yangebfe6112020-03-30 11:56:24 +0800450 int image_number;
451 int align_size;
Simon Glass722ebc82016-02-22 22:55:53 -0700452
Tom Rini7946a812020-05-06 11:05:17 -0400453 align_size = params->bl_len ? params->bl_len : 4;
Luca Boccassi7d574852019-05-14 19:35:02 +0100454 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false);
Simon Glass722ebc82016-02-22 22:55:53 -0700455 if (fd < 0)
456 return -EIO;
457 fit_size = fdt_totalsize(fdt);
458
Simon Glass722ebc82016-02-22 22:55:53 -0700459 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
460 if (images < 0) {
461 debug("%s: Cannot find /images node: %d\n", __func__, images);
462 ret = -EINVAL;
Simon Glassb97d71e2016-03-16 07:45:39 -0600463 goto err_munmap;
Simon Glass722ebc82016-02-22 22:55:53 -0700464 }
Kever Yangebfe6112020-03-30 11:56:24 +0800465 image_number = fdtdec_get_child_count(fdt, images);
466
467 /*
468 * Allocate space to hold the image data we will extract,
469 * extral space allocate for image alignment to prevent overflow.
470 */
Fabio Estevamaaa91a42020-07-27 21:03:13 -0300471 buf = calloc(1, fit_size + (align_size * image_number));
Kever Yangebfe6112020-03-30 11:56:24 +0800472 if (!buf) {
473 ret = -ENOMEM;
474 goto err_munmap;
475 }
476 buf_ptr = 0;
Simon Glass722ebc82016-02-22 22:55:53 -0700477
478 for (node = fdt_first_subnode(fdt, images);
479 node >= 0;
480 node = fdt_next_subnode(fdt, node)) {
481 const char *data;
482 int len;
483
Michal Simek4a8b6e02018-07-20 12:31:02 +0200484 data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
Simon Glass722ebc82016-02-22 22:55:53 -0700485 if (!data)
486 continue;
487 memcpy(buf + buf_ptr, data, len);
488 debug("Extracting data size %x\n", len);
489
Michal Simek4a8b6e02018-07-20 12:31:02 +0200490 ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
Simon Glass722ebc82016-02-22 22:55:53 -0700491 if (ret) {
492 ret = -EPERM;
Simon Glassb97d71e2016-03-16 07:45:39 -0600493 goto err_munmap;
Simon Glass722ebc82016-02-22 22:55:53 -0700494 }
Teddy Reedf8f91072016-06-09 19:38:02 -0700495 if (params->external_offset > 0) {
496 /* An external offset positions the data absolutely. */
Michal Simek4a8b6e02018-07-20 12:31:02 +0200497 fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
Teddy Reedf8f91072016-06-09 19:38:02 -0700498 params->external_offset + buf_ptr);
499 } else {
Michal Simek4a8b6e02018-07-20 12:31:02 +0200500 fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
501 buf_ptr);
Teddy Reedf8f91072016-06-09 19:38:02 -0700502 }
Michal Simek4a8b6e02018-07-20 12:31:02 +0200503 fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
Kever Yangebfe6112020-03-30 11:56:24 +0800504 buf_ptr += ALIGN(len, align_size);
Simon Glass722ebc82016-02-22 22:55:53 -0700505 }
506
507 /* Pack the FDT and place the data after it */
508 fdt_pack(fdt);
509
Kever Yangebfe6112020-03-30 11:56:24 +0800510 new_size = fdt_totalsize(fdt);
Tom Rini7946a812020-05-06 11:05:17 -0400511 new_size = ALIGN(new_size, align_size);
Kever Yangebfe6112020-03-30 11:56:24 +0800512 fdt_set_totalsize(fdt, new_size);
Simon Glass722ebc82016-02-22 22:55:53 -0700513 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
514 debug("External data size %x\n", buf_ptr);
Simon Glass722ebc82016-02-22 22:55:53 -0700515 munmap(fdt, sbuf.st_size);
516
517 if (ftruncate(fd, new_size)) {
518 debug("%s: Failed to truncate file: %s\n", __func__,
519 strerror(errno));
520 ret = -EIO;
521 goto err;
522 }
Teddy Reedf8f91072016-06-09 19:38:02 -0700523
524 /* Check if an offset for the external data was set. */
525 if (params->external_offset > 0) {
526 if (params->external_offset < new_size) {
527 debug("External offset %x overlaps FIT length %x",
528 params->external_offset, new_size);
529 ret = -EINVAL;
530 goto err;
531 }
532 new_size = params->external_offset;
533 }
Simon Glass722ebc82016-02-22 22:55:53 -0700534 if (lseek(fd, new_size, SEEK_SET) < 0) {
535 debug("%s: Failed to seek to end of file: %s\n", __func__,
536 strerror(errno));
537 ret = -EIO;
538 goto err;
539 }
540 if (write(fd, buf, buf_ptr) != buf_ptr) {
541 debug("%s: Failed to write external data to file %s\n",
542 __func__, strerror(errno));
543 ret = -EIO;
544 goto err;
545 }
Tom Rini3c2dff52017-09-26 22:14:44 -0400546 free(buf);
Simon Glassb97d71e2016-03-16 07:45:39 -0600547 close(fd);
548 return 0;
Simon Glass722ebc82016-02-22 22:55:53 -0700549
Simon Glassb97d71e2016-03-16 07:45:39 -0600550err_munmap:
551 munmap(fdt, sbuf.st_size);
Simon Glass722ebc82016-02-22 22:55:53 -0700552err:
Bin Meng0dbd6e32020-04-18 01:59:11 -0700553 free(buf);
Simon Glass722ebc82016-02-22 22:55:53 -0700554 close(fd);
555 return ret;
556}
557
Simon Glass529fd182016-02-22 22:55:54 -0700558static int fit_import_data(struct image_tool_params *params, const char *fname)
559{
560 void *fdt, *old_fdt;
561 int fit_size, new_size, size, data_base;
562 int fd;
563 struct stat sbuf;
564 int ret;
565 int images;
566 int node;
567
Luca Boccassi7d574852019-05-14 19:35:02 +0100568 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false);
Simon Glass529fd182016-02-22 22:55:54 -0700569 if (fd < 0)
570 return -EIO;
571 fit_size = fdt_totalsize(old_fdt);
Kever Yang02560b12020-03-30 11:56:22 +0800572 data_base = ALIGN(fit_size, 4);
Simon Glass529fd182016-02-22 22:55:54 -0700573
574 /* Allocate space to hold the new FIT */
575 size = sbuf.st_size + 16384;
Fabio Estevamaaa91a42020-07-27 21:03:13 -0300576 fdt = calloc(1, size);
Simon Glass529fd182016-02-22 22:55:54 -0700577 if (!fdt) {
578 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
579 __func__, size);
580 ret = -ENOMEM;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700581 goto err_munmap;
Simon Glass529fd182016-02-22 22:55:54 -0700582 }
583 ret = fdt_open_into(old_fdt, fdt, size);
584 if (ret) {
585 debug("%s: Failed to expand FIT: %s\n", __func__,
586 fdt_strerror(errno));
587 ret = -EINVAL;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700588 goto err_munmap;
Simon Glass529fd182016-02-22 22:55:54 -0700589 }
590
591 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
592 if (images < 0) {
593 debug("%s: Cannot find /images node: %d\n", __func__, images);
594 ret = -EINVAL;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700595 goto err_munmap;
Simon Glass529fd182016-02-22 22:55:54 -0700596 }
597
598 for (node = fdt_first_subnode(fdt, images);
599 node >= 0;
600 node = fdt_next_subnode(fdt, node)) {
601 int buf_ptr;
602 int len;
603
604 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
605 len = fdtdec_get_int(fdt, node, "data-size", -1);
606 if (buf_ptr == -1 || len == -1)
607 continue;
608 debug("Importing data size %x\n", len);
609
Patrick Oppenlanderc995d852020-07-30 14:31:48 +1000610 ret = fdt_setprop(fdt, node, "data",
611 old_fdt + data_base + buf_ptr, len);
Simon Glass529fd182016-02-22 22:55:54 -0700612 if (ret) {
613 debug("%s: Failed to write property: %s\n", __func__,
614 fdt_strerror(ret));
615 ret = -EINVAL;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700616 goto err_munmap;
Simon Glass529fd182016-02-22 22:55:54 -0700617 }
618 }
619
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700620 munmap(old_fdt, sbuf.st_size);
621
Tom Rinibf52fcd2017-10-07 11:27:59 -0400622 /* Close the old fd so we can re-use it. */
Simon Glass529fd182016-02-22 22:55:54 -0700623 close(fd);
624
625 /* Pack the FDT and place the data after it */
626 fdt_pack(fdt);
627
628 new_size = fdt_totalsize(fdt);
629 debug("Size expanded from %x to %x\n", fit_size, new_size);
630
631 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
632 if (fd < 0) {
633 fprintf(stderr, "%s: Can't open %s: %s\n",
634 params->cmdname, fname, strerror(errno));
Tom Rinibf52fcd2017-10-07 11:27:59 -0400635 ret = -EIO;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700636 goto err;
Simon Glass529fd182016-02-22 22:55:54 -0700637 }
638 if (write(fd, fdt, new_size) != new_size) {
639 debug("%s: Failed to write external data to file %s\n",
640 __func__, strerror(errno));
641 ret = -EIO;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700642 goto err;
Simon Glass529fd182016-02-22 22:55:54 -0700643 }
Simon Glass529fd182016-02-22 22:55:54 -0700644
Simon Glass6e0ffce2016-03-16 07:45:38 -0600645 free(fdt);
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700646 close(fd);
647 return 0;
648
649err_munmap:
650 munmap(old_fdt, sbuf.st_size);
651err:
652 free(fdt);
653 close(fd);
Simon Glass529fd182016-02-22 22:55:54 -0700654 return ret;
655}
656
Philippe Reynes7298e422019-12-18 18:25:41 +0100657static int copyfile(const char *src, const char *dst)
658{
659 int fd_src = -1, fd_dst = -1;
660 void *buf = NULL;
661 ssize_t size;
662 size_t count;
663 int ret = -1;
664
665 fd_src = open(src, O_RDONLY);
666 if (fd_src < 0) {
667 printf("Can't open file %s (%s)\n", src, strerror(errno));
668 goto out;
669 }
670
Thomas Hebbab5a2b02020-03-01 10:47:53 -0800671 fd_dst = open(dst, O_WRONLY | O_CREAT, 0666);
Philippe Reynes7298e422019-12-18 18:25:41 +0100672 if (fd_dst < 0) {
673 printf("Can't open file %s (%s)\n", dst, strerror(errno));
674 goto out;
675 }
676
Fabio Estevamaaa91a42020-07-27 21:03:13 -0300677 buf = calloc(1, 512);
Philippe Reynes7298e422019-12-18 18:25:41 +0100678 if (!buf) {
679 printf("Can't allocate buffer to copy file\n");
680 goto out;
681 }
682
683 while (1) {
684 size = read(fd_src, buf, 512);
685 if (size < 0) {
686 printf("Can't read file %s\n", src);
687 goto out;
688 }
689 if (!size)
690 break;
691
692 count = size;
693 size = write(fd_dst, buf, count);
694 if (size < 0) {
695 printf("Can't write file %s\n", dst);
696 goto out;
697 }
698 }
699
700 ret = 0;
701
702 out:
703 if (fd_src >= 0)
704 close(fd_src);
705 if (fd_dst >= 0)
706 close(fd_dst);
707 if (buf)
708 free(buf);
709
710 return ret;
711}
712
Simon Glass722ebc82016-02-22 22:55:53 -0700713/**
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530714 * fit_handle_file - main FIT file processing function
715 *
716 * fit_handle_file() runs dtc to convert .its to .itb, includes
717 * binary data, updates timestamp property and calculates hashes.
718 *
719 * datafile - .its file
720 * imagefile - .itb file
721 *
722 * returns:
723 * only on success, otherwise calls exit (EXIT_FAILURE);
724 */
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700725static int fit_handle_file(struct image_tool_params *params)
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530726{
727 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
Philippe Reynes7298e422019-12-18 18:25:41 +0100728 char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0};
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530729 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
Simon Glassa9468112014-06-02 22:04:53 -0600730 size_t size_inc;
731 int ret;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530732
733 /* Flattened Image Tree (FIT) format handling */
734 debug ("FIT format handling\n");
735
736 /* call dtc to include binary properties into the tmp file */
737 if (strlen (params->imagefile) +
738 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
739 fprintf (stderr, "%s: Image file name (%s) too long, "
740 "can't create tmpfile",
741 params->imagefile, params->cmdname);
742 return (EXIT_FAILURE);
743 }
744 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
745
Simon Glass95d77b42013-06-13 15:10:05 -0700746 /* We either compile the source file, or use the existing FIT image */
Simon Glass8e35bb02016-02-22 22:55:51 -0700747 if (params->auto_its) {
748 if (fit_build(params, tmpfile)) {
749 fprintf(stderr, "%s: failed to build FIT\n",
750 params->cmdname);
751 return EXIT_FAILURE;
752 }
753 *cmd = '\0';
754 } else if (params->datafile) {
Stefan Theil63f881d2018-03-08 09:00:13 +0100755 /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
756 snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
757 MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
Simon Glass95d77b42013-06-13 15:10:05 -0700758 debug("Trying to execute \"%s\"\n", cmd);
759 } else {
Mirza, Taimoora6e98102017-10-04 20:28:03 +0500760 snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
Simon Glass95d77b42013-06-13 15:10:05 -0700761 params->imagefile, tmpfile);
762 }
Sven Roedererea5d3732020-04-27 02:08:39 +0200763 if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) {
764 fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n");
765 }
Philippe Reynes7298e422019-12-18 18:25:41 +0100766
Simon Glass8e35bb02016-02-22 22:55:51 -0700767 if (*cmd && system(cmd) == -1) {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530768 fprintf (stderr, "%s: system(%s) failed: %s\n",
769 params->cmdname, cmd, strerror(errno));
Simon Glassaa6d6db2013-05-08 08:05:57 +0000770 goto err_system;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530771 }
772
Simon Glass529fd182016-02-22 22:55:54 -0700773 /* Move the data so it is internal to the FIT, if needed */
774 ret = fit_import_data(params, tmpfile);
775 if (ret)
776 goto err_system;
777
Simon Glassa9468112014-06-02 22:04:53 -0600778 /*
Philippe Reynes7298e422019-12-18 18:25:41 +0100779 * Copy the tmpfile to bakfile, then in the following loop
780 * we copy bakfile to tmpfile. So we always start from the
781 * beginning.
782 */
783 sprintf(bakfile, "%s%s", tmpfile, ".bak");
784 rename(tmpfile, bakfile);
785
786 /*
Simon Glassa9468112014-06-02 22:04:53 -0600787 * Set hashes for images in the blob. Unfortunately we may need more
788 * space in either FDT, so keep trying until we succeed.
789 *
790 * Note: this is pretty inefficient for signing, since we must
791 * calculate the signature every time. It would be better to calculate
792 * all the data and then store it in a separate step. However, this
793 * would be considerably more complex to implement. Generally a few
794 * steps of this loop is enough to sign with several keys.
795 */
796 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
Philippe Reynes7298e422019-12-18 18:25:41 +0100797 if (copyfile(bakfile, tmpfile) < 0) {
798 printf("Can't copy %s to %s\n", bakfile, tmpfile);
799 ret = -EIO;
800 break;
801 }
Simon Glassa9468112014-06-02 22:04:53 -0600802 ret = fit_add_file_data(params, size_inc, tmpfile);
803 if (!ret || ret != -ENOSPC)
804 break;
Simon Glasse29495d2013-06-13 15:10:04 -0700805 }
806
Simon Glassa9468112014-06-02 22:04:53 -0600807 if (ret) {
Simon Glass655cc692016-07-03 09:40:43 -0600808 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
809 params->cmdname, ret);
Simon Glassa9468112014-06-02 22:04:53 -0600810 goto err_system;
Simon Glasse29495d2013-06-13 15:10:04 -0700811 }
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530812
Simon Glass722ebc82016-02-22 22:55:53 -0700813 /* Move the data so it is external to the FIT, if requested */
814 if (params->external_data) {
815 ret = fit_extract_data(params, tmpfile);
816 if (ret)
817 goto err_system;
818 }
819
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530820 if (rename (tmpfile, params->imagefile) == -1) {
821 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
822 params->cmdname, tmpfile, params->imagefile,
823 strerror (errno));
824 unlink (tmpfile);
Philippe Reynes7298e422019-12-18 18:25:41 +0100825 unlink(bakfile);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530826 unlink (params->imagefile);
Simon Glassa9468112014-06-02 22:04:53 -0600827 return EXIT_FAILURE;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530828 }
Philippe Reynes7298e422019-12-18 18:25:41 +0100829 unlink(bakfile);
Simon Glassa9468112014-06-02 22:04:53 -0600830 return EXIT_SUCCESS;
Simon Glassaa6d6db2013-05-08 08:05:57 +0000831
Simon Glassaa6d6db2013-05-08 08:05:57 +0000832err_system:
833 unlink(tmpfile);
Philippe Reynes7298e422019-12-18 18:25:41 +0100834 unlink(bakfile);
Simon Glassaa6d6db2013-05-08 08:05:57 +0000835 return -1;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530836}
837
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200838/**
839 * fit_image_extract - extract a FIT component image
840 * @fit: pointer to the FIT format image header
841 * @image_noffset: offset of the component image node
842 * @file_name: name of the file to store the FIT sub-image
843 *
844 * returns:
845 * zero in case of success or a negative value if fail.
846 */
847static int fit_image_extract(
848 const void *fit,
849 int image_noffset,
850 const char *file_name)
851{
852 const void *file_data;
853 size_t file_size = 0;
Andrew F. Davise5b56282019-09-17 17:09:34 -0400854 int ret;
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200855
Andrew F. Davise5b56282019-09-17 17:09:34 -0400856 /* get the data address and size of component at offset "image_noffset" */
857 ret = fit_image_get_data_and_size(fit, image_noffset, &file_data, &file_size);
858 if (ret) {
859 fprintf(stderr, "Could not get component information\n");
860 return ret;
861 }
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200862
863 /* save the "file_data" into the file specified by "file_name" */
864 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
865}
866
867/**
868 * fit_extract_contents - retrieve a sub-image component from the FIT image
869 * @ptr: pointer to the FIT format image header
870 * @params: command line parameters
871 *
872 * returns:
873 * zero in case of success or a negative value if fail.
874 */
875static int fit_extract_contents(void *ptr, struct image_tool_params *params)
876{
877 int images_noffset;
878 int noffset;
879 int ndepth;
880 const void *fit = ptr;
881 int count = 0;
882 const char *p;
883
884 /* Indent string is defined in header image.h */
885 p = IMAGE_INDENT_STRING;
886
Simon Glassc5819702021-02-15 17:08:09 -0700887 if (fit_check_format(fit, IMAGE_SIZE_INVAL)) {
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200888 printf("Bad FIT image format\n");
889 return -1;
890 }
891
892 /* Find images parent node offset */
893 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
894 if (images_noffset < 0) {
895 printf("Can't find images parent node '%s' (%s)\n",
896 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
897 return -1;
898 }
899
900 /* Avoid any overrun */
901 count = fit_get_subimage_count(fit, images_noffset);
902 if ((params->pflag < 0) || (count <= params->pflag)) {
903 printf("No such component at '%d'\n", params->pflag);
904 return -1;
905 }
906
907 /* Process its subnodes, extract the desired component from image */
908 for (ndepth = 0, count = 0,
909 noffset = fdt_next_node(fit, images_noffset, &ndepth);
910 (noffset >= 0) && (ndepth > 0);
911 noffset = fdt_next_node(fit, noffset, &ndepth)) {
912 if (ndepth == 1) {
913 /*
914 * Direct child node of the images parent node,
915 * i.e. component image node.
916 */
917 if (params->pflag == count) {
918 printf("Extracted:\n%s Image %u (%s)\n", p,
919 count, fit_get_name(fit, noffset, NULL));
920
921 fit_image_print(fit, noffset, p);
922
923 return fit_image_extract(fit, noffset,
924 params->outfile);
925 }
926
927 count++;
928 }
929 }
930
931 return 0;
932}
933
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700934static int fit_check_params(struct image_tool_params *params)
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530935{
Simon Glass8e35bb02016-02-22 22:55:51 -0700936 if (params->auto_its)
937 return 0;
Heinrich Schuchardt58194662019-12-11 13:51:38 +0100938 return ((params->dflag && params->fflag) ||
939 (params->fflag && params->lflag) ||
940 (params->lflag && params->dflag));
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530941}
942
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200943U_BOOT_IMAGE_TYPE(
944 fitimage,
945 "FIT Image support",
946 sizeof(image_header_t),
947 (void *)&header,
948 fit_check_params,
949 fit_verify_header,
950 fit_print_contents,
951 NULL,
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200952 fit_extract_contents,
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200953 fit_check_image_types,
954 fit_handle_file,
955 NULL /* FIT images use DTB header */
956);