blob: 8df95c40d20280bf8595ddff48ecc6d45c582cf1 [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,
Jan Kiszka5902a392022-01-14 10:21:19 +010076 params->cmdname,
77 params->algo_name);
Simon Glassa9468112014-06-02 22:04:53 -060078 }
79
80 if (dest_blob) {
81 munmap(dest_blob, destfd_size);
82 close(destfd);
83 }
84
85err_keydest:
86 munmap(ptr, sbuf.st_size);
87 close(tfd);
Simon Glassa9468112014-06-02 22:04:53 -060088 return ret;
89}
90
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +053091/**
Simon Glass8e35bb02016-02-22 22:55:51 -070092 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
93 */
94static int fit_calc_size(struct image_tool_params *params)
95{
Simon Glassfb4cce02016-02-22 22:55:52 -070096 struct content_info *cont;
Simon Glass8e35bb02016-02-22 22:55:51 -070097 int size, total_size;
98
99 size = imagetool_get_filesize(params, params->datafile);
100 if (size < 0)
101 return -1;
Simon Glass8e35bb02016-02-22 22:55:51 -0700102 total_size = size;
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100103
104 if (params->fit_ramdisk) {
105 size = imagetool_get_filesize(params, params->fit_ramdisk);
106 if (size < 0)
107 return -1;
108 total_size += size;
109 }
110
Simon Glassfb4cce02016-02-22 22:55:52 -0700111 for (cont = params->content_head; cont; cont = cont->next) {
112 size = imagetool_get_filesize(params, cont->fname);
113 if (size < 0)
114 return -1;
Simon Glass8e35bb02016-02-22 22:55:51 -0700115
Simon Glass31729112020-05-27 07:24:55 -0600116 /* Add space for properties and hash node */
Simon Glassfb4cce02016-02-22 22:55:52 -0700117 total_size += size + 300;
118 }
Simon Glass8e35bb02016-02-22 22:55:51 -0700119
120 /* Add plenty of space for headers, properties, nodes, etc. */
121 total_size += 4096;
122
123 return total_size;
124}
125
126static int fdt_property_file(struct image_tool_params *params,
127 void *fdt, const char *name, const char *fname)
128{
129 struct stat sbuf;
130 void *ptr;
131 int ret;
132 int fd;
133
134 fd = open(fname, O_RDWR | O_BINARY);
135 if (fd < 0) {
136 fprintf(stderr, "%s: Can't open %s: %s\n",
137 params->cmdname, fname, strerror(errno));
138 return -1;
139 }
140
141 if (fstat(fd, &sbuf) < 0) {
142 fprintf(stderr, "%s: Can't stat %s: %s\n",
143 params->cmdname, fname, strerror(errno));
144 goto err;
145 }
146
147 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
148 if (ret)
Simon Glass3bd3a542016-03-16 07:45:42 -0600149 goto err;
Simon Glass8e35bb02016-02-22 22:55:51 -0700150 ret = read(fd, ptr, sbuf.st_size);
151 if (ret != sbuf.st_size) {
152 fprintf(stderr, "%s: Can't read %s: %s\n",
153 params->cmdname, fname, strerror(errno));
154 goto err;
155 }
Simon Glass3bd3a542016-03-16 07:45:42 -0600156 close(fd);
Simon Glass8e35bb02016-02-22 22:55:51 -0700157
158 return 0;
159err:
160 close(fd);
161 return -1;
162}
163
164static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
165{
166 char str[100];
167 va_list ptr;
168
169 va_start(ptr, fmt);
170 vsnprintf(str, sizeof(str), fmt, ptr);
171 va_end(ptr);
172 return fdt_property_string(fdt, name, str);
173}
174
Simon Glassfb4cce02016-02-22 22:55:52 -0700175static void get_basename(char *str, int size, const char *fname)
176{
177 const char *p, *start, *end;
178 int len;
179
180 /*
181 * Use the base name as the 'name' field. So for example:
182 *
183 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
184 * becomes "sun7i-a20-bananapro"
185 */
186 p = strrchr(fname, '/');
187 start = p ? p + 1 : fname;
188 p = strrchr(fname, '.');
189 end = p ? p : fname + strlen(fname);
190 len = end - start;
191 if (len >= size)
192 len = size - 1;
193 memcpy(str, start, len);
194 str[len] = '\0';
195}
196
Simon Glass8e35bb02016-02-22 22:55:51 -0700197/**
Simon Glass31729112020-05-27 07:24:55 -0600198 * add_crc_node() - Add a hash node to request a CRC checksum for an image
199 *
200 * @fdt: Device tree to add to (in sequential-write mode)
201 */
202static void add_crc_node(void *fdt)
203{
204 fdt_begin_node(fdt, "hash-1");
205 fdt_property_string(fdt, FIT_ALGO_PROP, "crc32");
206 fdt_end_node(fdt);
207}
208
209/**
Simon Glass8e35bb02016-02-22 22:55:51 -0700210 * fit_write_images() - Write out a list of images to the FIT
211 *
Simon Glassfb4cce02016-02-22 22:55:52 -0700212 * We always include the main image (params->datafile). If there are device
Andre Przywara2eda8e92017-12-04 02:05:12 +0000213 * tree files, we include an fdt- node for each of those too.
Simon Glass8e35bb02016-02-22 22:55:51 -0700214 */
215static int fit_write_images(struct image_tool_params *params, char *fdt)
216{
Simon Glassfb4cce02016-02-22 22:55:52 -0700217 struct content_info *cont;
Simon Glass8e35bb02016-02-22 22:55:51 -0700218 const char *typename;
219 char str[100];
Simon Glassfb4cce02016-02-22 22:55:52 -0700220 int upto;
Simon Glass8e35bb02016-02-22 22:55:51 -0700221 int ret;
222
223 fdt_begin_node(fdt, "images");
224
225 /* First the main image */
226 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara2eda8e92017-12-04 02:05:12 +0000227 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glass8e35bb02016-02-22 22:55:51 -0700228 fdt_begin_node(fdt, str);
Michal Simek4a8b6e02018-07-20 12:31:02 +0200229 fdt_property_string(fdt, FIT_DESC_PROP, params->imagename);
230 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
231 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glass3a45f382016-06-30 10:52:12 -0600232 genimg_get_arch_short_name(params->arch));
Michal Simek4a8b6e02018-07-20 12:31:02 +0200233 fdt_property_string(fdt, FIT_OS_PROP,
234 genimg_get_os_short_name(params->os));
235 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glass8e35bb02016-02-22 22:55:51 -0700236 genimg_get_comp_short_name(params->comp));
Michal Simek4a8b6e02018-07-20 12:31:02 +0200237 fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr);
238 fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep);
Simon Glass8e35bb02016-02-22 22:55:51 -0700239
240 /*
241 * Put data last since it is large. SPL may only load the first part
242 * of the DT, so this way it can access all the above fields.
243 */
Michal Simek4a8b6e02018-07-20 12:31:02 +0200244 ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile);
Simon Glass8e35bb02016-02-22 22:55:51 -0700245 if (ret)
246 return ret;
Simon Glass31729112020-05-27 07:24:55 -0600247 add_crc_node(fdt);
Simon Glass8e35bb02016-02-22 22:55:51 -0700248 fdt_end_node(fdt);
249
Simon Glassfb4cce02016-02-22 22:55:52 -0700250 /* Now the device tree files if available */
251 upto = 0;
252 for (cont = params->content_head; cont; cont = cont->next) {
253 if (cont->type != IH_TYPE_FLATDT)
254 continue;
Michal Sojka12e288a2019-09-13 12:43:12 +0200255 typename = genimg_get_type_short_name(cont->type);
Andre Przywara2eda8e92017-12-04 02:05:12 +0000256 snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
Simon Glassfb4cce02016-02-22 22:55:52 -0700257 fdt_begin_node(fdt, str);
258
259 get_basename(str, sizeof(str), cont->fname);
Michal Simek4a8b6e02018-07-20 12:31:02 +0200260 fdt_property_string(fdt, FIT_DESC_PROP, str);
261 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
262 cont->fname);
Simon Glassfb4cce02016-02-22 22:55:52 -0700263 if (ret)
264 return ret;
Michal Simek4a8b6e02018-07-20 12:31:02 +0200265 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
266 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassfb4cce02016-02-22 22:55:52 -0700267 genimg_get_arch_short_name(params->arch));
Michal Simek4a8b6e02018-07-20 12:31:02 +0200268 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glassfb4cce02016-02-22 22:55:52 -0700269 genimg_get_comp_short_name(IH_COMP_NONE));
Simon Glass31729112020-05-27 07:24:55 -0600270 add_crc_node(fdt);
Simon Glassfb4cce02016-02-22 22:55:52 -0700271 fdt_end_node(fdt);
272 }
273
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100274 /* And a ramdisk file if available */
275 if (params->fit_ramdisk) {
Andre Przywara2eda8e92017-12-04 02:05:12 +0000276 fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100277
Michal Simek4a8b6e02018-07-20 12:31:02 +0200278 fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP);
279 fdt_property_string(fdt, FIT_OS_PROP,
280 genimg_get_os_short_name(params->os));
Michal Sojka12e288a2019-09-13 12:43:12 +0200281 fdt_property_string(fdt, FIT_ARCH_PROP,
282 genimg_get_arch_short_name(params->arch));
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100283
Michal Simek4a8b6e02018-07-20 12:31:02 +0200284 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
285 params->fit_ramdisk);
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100286 if (ret)
287 return ret;
Simon Glass31729112020-05-27 07:24:55 -0600288 add_crc_node(fdt);
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100289 fdt_end_node(fdt);
290 }
291
Simon Glass8e35bb02016-02-22 22:55:51 -0700292 fdt_end_node(fdt);
293
294 return 0;
295}
296
297/**
298 * fit_write_configs() - Write out a list of configurations to the FIT
299 *
Simon Glassfb4cce02016-02-22 22:55:52 -0700300 * If there are device tree files, we include a configuration for each, which
301 * selects the main image (params->datafile) and its corresponding device
302 * tree file.
303 *
304 * Otherwise we just create a configuration with the main image in it.
Simon Glass8e35bb02016-02-22 22:55:51 -0700305 */
306static void fit_write_configs(struct image_tool_params *params, char *fdt)
307{
Simon Glassfb4cce02016-02-22 22:55:52 -0700308 struct content_info *cont;
Simon Glass8e35bb02016-02-22 22:55:51 -0700309 const char *typename;
310 char str[100];
Simon Glassfb4cce02016-02-22 22:55:52 -0700311 int upto;
Simon Glass8e35bb02016-02-22 22:55:51 -0700312
313 fdt_begin_node(fdt, "configurations");
Michal Simek4a8b6e02018-07-20 12:31:02 +0200314 fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
Simon Glass8e35bb02016-02-22 22:55:51 -0700315
Simon Glassfb4cce02016-02-22 22:55:52 -0700316 upto = 0;
317 for (cont = params->content_head; cont; cont = cont->next) {
318 if (cont->type != IH_TYPE_FLATDT)
319 continue;
320 typename = genimg_get_type_short_name(cont->type);
Andre Przywara2eda8e92017-12-04 02:05:12 +0000321 snprintf(str, sizeof(str), "conf-%d", ++upto);
Simon Glassfb4cce02016-02-22 22:55:52 -0700322 fdt_begin_node(fdt, str);
323
324 get_basename(str, sizeof(str), cont->fname);
Michal Simek4a8b6e02018-07-20 12:31:02 +0200325 fdt_property_string(fdt, FIT_DESC_PROP, str);
Simon Glassfb4cce02016-02-22 22:55:52 -0700326
327 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara2eda8e92017-12-04 02:05:12 +0000328 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassfb4cce02016-02-22 22:55:52 -0700329 fdt_property_string(fdt, typename, str);
Abel Vesacabde442019-03-12 08:34:32 +0000330 fdt_property_string(fdt, FIT_LOADABLE_PROP, str);
Simon Glassfb4cce02016-02-22 22:55:52 -0700331
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100332 if (params->fit_ramdisk)
333 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara2eda8e92017-12-04 02:05:12 +0000334 FIT_RAMDISK_PROP "-1");
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100335
Andre Przywara2eda8e92017-12-04 02:05:12 +0000336 snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
Simon Glassfb4cce02016-02-22 22:55:52 -0700337 fdt_property_string(fdt, FIT_FDT_PROP, str);
338 fdt_end_node(fdt);
339 }
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100340
Simon Glassfb4cce02016-02-22 22:55:52 -0700341 if (!upto) {
Andre Przywara2eda8e92017-12-04 02:05:12 +0000342 fdt_begin_node(fdt, "conf-1");
Simon Glassfb4cce02016-02-22 22:55:52 -0700343 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara2eda8e92017-12-04 02:05:12 +0000344 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassfb4cce02016-02-22 22:55:52 -0700345 fdt_property_string(fdt, typename, str);
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100346
347 if (params->fit_ramdisk)
348 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara2eda8e92017-12-04 02:05:12 +0000349 FIT_RAMDISK_PROP "-1");
Tomeu Vizoso0f7c6cd2016-11-04 14:22:15 +0100350
Simon Glassfb4cce02016-02-22 22:55:52 -0700351 fdt_end_node(fdt);
352 }
Simon Glass8e35bb02016-02-22 22:55:51 -0700353
354 fdt_end_node(fdt);
355}
356
357static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
358{
359 int ret;
360
361 ret = fdt_create(fdt, size);
362 if (ret)
363 return ret;
364 fdt_finish_reservemap(fdt);
365 fdt_begin_node(fdt, "");
Michal Simek4a8b6e02018-07-20 12:31:02 +0200366 fdt_property_strf(fdt, FIT_DESC_PROP,
Simon Glass8e35bb02016-02-22 22:55:51 -0700367 "%s image with one or more FDT blobs",
368 genimg_get_type_name(params->fit_image_type));
369 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
370 fdt_property_u32(fdt, "#address-cells", 1);
371 ret = fit_write_images(params, fdt);
372 if (ret)
373 return ret;
374 fit_write_configs(params, fdt);
375 fdt_end_node(fdt);
376 ret = fdt_finish(fdt);
377 if (ret)
378 return ret;
379
380 return fdt_totalsize(fdt);
381}
382
383static int fit_build(struct image_tool_params *params, const char *fname)
384{
385 char *buf;
386 int size;
387 int ret;
388 int fd;
389
390 size = fit_calc_size(params);
391 if (size < 0)
392 return -1;
Fabio Estevamaaa91a42020-07-27 21:03:13 -0300393 buf = calloc(1, size);
Simon Glass8e35bb02016-02-22 22:55:51 -0700394 if (!buf) {
395 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
396 params->cmdname, size);
397 return -1;
398 }
399 ret = fit_build_fdt(params, buf, size);
400 if (ret < 0) {
401 fprintf(stderr, "%s: Failed to build FIT image\n",
402 params->cmdname);
Simon Glass7b0bbd82016-03-16 07:45:41 -0600403 goto err_buf;
Simon Glass8e35bb02016-02-22 22:55:51 -0700404 }
405 size = ret;
406 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
407 if (fd < 0) {
408 fprintf(stderr, "%s: Can't open %s: %s\n",
409 params->cmdname, fname, strerror(errno));
Tom Rini3c2dff52017-09-26 22:14:44 -0400410 goto err_buf;
Simon Glass8e35bb02016-02-22 22:55:51 -0700411 }
412 ret = write(fd, buf, size);
413 if (ret != size) {
414 fprintf(stderr, "%s: Can't write %s: %s\n",
415 params->cmdname, fname, strerror(errno));
Simon Glass8e35bb02016-02-22 22:55:51 -0700416 goto err;
417 }
418 close(fd);
Simon Glass7b0bbd82016-03-16 07:45:41 -0600419 free(buf);
Simon Glass8e35bb02016-02-22 22:55:51 -0700420
421 return 0;
422err:
Simon Glass7b0bbd82016-03-16 07:45:41 -0600423 close(fd);
424err_buf:
Simon Glass8e35bb02016-02-22 22:55:51 -0700425 free(buf);
426 return -1;
427}
428
429/**
Simon Glass722ebc82016-02-22 22:55:53 -0700430 * fit_extract_data() - Move all data outside the FIT
431 *
432 * This takes a normal FIT file and removes all the 'data' properties from it.
433 * The data is placed in an area after the FIT so that it can be accessed
434 * using an offset into that area. The 'data' properties turn into
435 * 'data-offset' properties.
436 *
437 * This function cannot cope with FITs with 'data-offset' properties. All
438 * data must be in 'data' properties on entry.
439 */
440static int fit_extract_data(struct image_tool_params *params, const char *fname)
441{
Kever Yangebfe6112020-03-30 11:56:24 +0800442 void *buf = NULL;
Simon Glass722ebc82016-02-22 22:55:53 -0700443 int buf_ptr;
444 int fit_size, new_size;
445 int fd;
446 struct stat sbuf;
447 void *fdt;
448 int ret;
449 int images;
450 int node;
Kever Yangebfe6112020-03-30 11:56:24 +0800451 int image_number;
452 int align_size;
Simon Glass722ebc82016-02-22 22:55:53 -0700453
Tom Rini7946a812020-05-06 11:05:17 -0400454 align_size = params->bl_len ? params->bl_len : 4;
Luca Boccassi7d574852019-05-14 19:35:02 +0100455 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false);
Simon Glass722ebc82016-02-22 22:55:53 -0700456 if (fd < 0)
457 return -EIO;
458 fit_size = fdt_totalsize(fdt);
459
Simon Glass722ebc82016-02-22 22:55:53 -0700460 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
461 if (images < 0) {
462 debug("%s: Cannot find /images node: %d\n", __func__, images);
463 ret = -EINVAL;
Simon Glassb97d71e2016-03-16 07:45:39 -0600464 goto err_munmap;
Simon Glass722ebc82016-02-22 22:55:53 -0700465 }
Kever Yangebfe6112020-03-30 11:56:24 +0800466 image_number = fdtdec_get_child_count(fdt, images);
467
468 /*
469 * Allocate space to hold the image data we will extract,
470 * extral space allocate for image alignment to prevent overflow.
471 */
Fabio Estevamaaa91a42020-07-27 21:03:13 -0300472 buf = calloc(1, fit_size + (align_size * image_number));
Kever Yangebfe6112020-03-30 11:56:24 +0800473 if (!buf) {
474 ret = -ENOMEM;
475 goto err_munmap;
476 }
477 buf_ptr = 0;
Simon Glass722ebc82016-02-22 22:55:53 -0700478
479 for (node = fdt_first_subnode(fdt, images);
480 node >= 0;
481 node = fdt_next_subnode(fdt, node)) {
482 const char *data;
483 int len;
484
Michal Simek4a8b6e02018-07-20 12:31:02 +0200485 data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
Simon Glass722ebc82016-02-22 22:55:53 -0700486 if (!data)
487 continue;
488 memcpy(buf + buf_ptr, data, len);
489 debug("Extracting data size %x\n", len);
490
Michal Simek4a8b6e02018-07-20 12:31:02 +0200491 ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
Simon Glass722ebc82016-02-22 22:55:53 -0700492 if (ret) {
493 ret = -EPERM;
Simon Glassb97d71e2016-03-16 07:45:39 -0600494 goto err_munmap;
Simon Glass722ebc82016-02-22 22:55:53 -0700495 }
Teddy Reedf8f91072016-06-09 19:38:02 -0700496 if (params->external_offset > 0) {
497 /* An external offset positions the data absolutely. */
Michal Simek4a8b6e02018-07-20 12:31:02 +0200498 fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
Teddy Reedf8f91072016-06-09 19:38:02 -0700499 params->external_offset + buf_ptr);
500 } else {
Michal Simek4a8b6e02018-07-20 12:31:02 +0200501 fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
502 buf_ptr);
Teddy Reedf8f91072016-06-09 19:38:02 -0700503 }
Michal Simek4a8b6e02018-07-20 12:31:02 +0200504 fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
Kever Yangebfe6112020-03-30 11:56:24 +0800505 buf_ptr += ALIGN(len, align_size);
Simon Glass722ebc82016-02-22 22:55:53 -0700506 }
507
508 /* Pack the FDT and place the data after it */
509 fdt_pack(fdt);
510
Kever Yangebfe6112020-03-30 11:56:24 +0800511 new_size = fdt_totalsize(fdt);
Tom Rini7946a812020-05-06 11:05:17 -0400512 new_size = ALIGN(new_size, align_size);
Kever Yangebfe6112020-03-30 11:56:24 +0800513 fdt_set_totalsize(fdt, new_size);
Simon Glass722ebc82016-02-22 22:55:53 -0700514 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
515 debug("External data size %x\n", buf_ptr);
Simon Glass722ebc82016-02-22 22:55:53 -0700516 munmap(fdt, sbuf.st_size);
517
518 if (ftruncate(fd, new_size)) {
519 debug("%s: Failed to truncate file: %s\n", __func__,
520 strerror(errno));
521 ret = -EIO;
522 goto err;
523 }
Teddy Reedf8f91072016-06-09 19:38:02 -0700524
525 /* Check if an offset for the external data was set. */
526 if (params->external_offset > 0) {
527 if (params->external_offset < new_size) {
Sven Roederer9c702372021-05-25 23:15:27 +0200528 debug("External offset %x overlaps FIT length %x\n",
Teddy Reedf8f91072016-06-09 19:38:02 -0700529 params->external_offset, new_size);
530 ret = -EINVAL;
531 goto err;
532 }
533 new_size = params->external_offset;
534 }
Simon Glass722ebc82016-02-22 22:55:53 -0700535 if (lseek(fd, new_size, SEEK_SET) < 0) {
536 debug("%s: Failed to seek to end of file: %s\n", __func__,
537 strerror(errno));
538 ret = -EIO;
539 goto err;
540 }
541 if (write(fd, buf, buf_ptr) != buf_ptr) {
542 debug("%s: Failed to write external data to file %s\n",
543 __func__, strerror(errno));
544 ret = -EIO;
545 goto err;
546 }
Tom Rini3c2dff52017-09-26 22:14:44 -0400547 free(buf);
Simon Glassb97d71e2016-03-16 07:45:39 -0600548 close(fd);
549 return 0;
Simon Glass722ebc82016-02-22 22:55:53 -0700550
Simon Glassb97d71e2016-03-16 07:45:39 -0600551err_munmap:
552 munmap(fdt, sbuf.st_size);
Simon Glass722ebc82016-02-22 22:55:53 -0700553err:
Bin Meng0dbd6e32020-04-18 01:59:11 -0700554 free(buf);
Simon Glass722ebc82016-02-22 22:55:53 -0700555 close(fd);
556 return ret;
557}
558
Simon Glass529fd182016-02-22 22:55:54 -0700559static int fit_import_data(struct image_tool_params *params, const char *fname)
560{
561 void *fdt, *old_fdt;
562 int fit_size, new_size, size, data_base;
563 int fd;
564 struct stat sbuf;
565 int ret;
566 int images;
567 int node;
568
Luca Boccassi7d574852019-05-14 19:35:02 +0100569 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false);
Simon Glass529fd182016-02-22 22:55:54 -0700570 if (fd < 0)
571 return -EIO;
572 fit_size = fdt_totalsize(old_fdt);
Kever Yang02560b12020-03-30 11:56:22 +0800573 data_base = ALIGN(fit_size, 4);
Simon Glass529fd182016-02-22 22:55:54 -0700574
575 /* Allocate space to hold the new FIT */
576 size = sbuf.st_size + 16384;
Fabio Estevamaaa91a42020-07-27 21:03:13 -0300577 fdt = calloc(1, size);
Simon Glass529fd182016-02-22 22:55:54 -0700578 if (!fdt) {
579 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
580 __func__, size);
581 ret = -ENOMEM;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700582 goto err_munmap;
Simon Glass529fd182016-02-22 22:55:54 -0700583 }
584 ret = fdt_open_into(old_fdt, fdt, size);
585 if (ret) {
586 debug("%s: Failed to expand FIT: %s\n", __func__,
587 fdt_strerror(errno));
588 ret = -EINVAL;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700589 goto err_munmap;
Simon Glass529fd182016-02-22 22:55:54 -0700590 }
591
592 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
593 if (images < 0) {
594 debug("%s: Cannot find /images node: %d\n", __func__, images);
595 ret = -EINVAL;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700596 goto err_munmap;
Simon Glass529fd182016-02-22 22:55:54 -0700597 }
598
599 for (node = fdt_first_subnode(fdt, images);
600 node >= 0;
601 node = fdt_next_subnode(fdt, node)) {
602 int buf_ptr;
603 int len;
604
605 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
606 len = fdtdec_get_int(fdt, node, "data-size", -1);
607 if (buf_ptr == -1 || len == -1)
608 continue;
609 debug("Importing data size %x\n", len);
610
Patrick Oppenlanderc995d852020-07-30 14:31:48 +1000611 ret = fdt_setprop(fdt, node, "data",
612 old_fdt + data_base + buf_ptr, len);
Simon Glass529fd182016-02-22 22:55:54 -0700613 if (ret) {
614 debug("%s: Failed to write property: %s\n", __func__,
615 fdt_strerror(ret));
616 ret = -EINVAL;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700617 goto err_munmap;
Simon Glass529fd182016-02-22 22:55:54 -0700618 }
619 }
620
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700621 munmap(old_fdt, sbuf.st_size);
622
Tom Rinibf52fcd2017-10-07 11:27:59 -0400623 /* Close the old fd so we can re-use it. */
Simon Glass529fd182016-02-22 22:55:54 -0700624 close(fd);
625
626 /* Pack the FDT and place the data after it */
627 fdt_pack(fdt);
628
629 new_size = fdt_totalsize(fdt);
630 debug("Size expanded from %x to %x\n", fit_size, new_size);
631
632 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
633 if (fd < 0) {
634 fprintf(stderr, "%s: Can't open %s: %s\n",
635 params->cmdname, fname, strerror(errno));
Tom Rinibf52fcd2017-10-07 11:27:59 -0400636 ret = -EIO;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700637 goto err;
Simon Glass529fd182016-02-22 22:55:54 -0700638 }
639 if (write(fd, fdt, new_size) != new_size) {
640 debug("%s: Failed to write external data to file %s\n",
641 __func__, strerror(errno));
642 ret = -EIO;
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700643 goto err;
Simon Glass529fd182016-02-22 22:55:54 -0700644 }
Simon Glass529fd182016-02-22 22:55:54 -0700645
Simon Glass6e0ffce2016-03-16 07:45:38 -0600646 free(fdt);
Lihua Zhao3fc85a72020-04-18 01:59:10 -0700647 close(fd);
648 return 0;
649
650err_munmap:
651 munmap(old_fdt, sbuf.st_size);
652err:
653 free(fdt);
654 close(fd);
Simon Glass529fd182016-02-22 22:55:54 -0700655 return ret;
656}
657
Philippe Reynes7298e422019-12-18 18:25:41 +0100658static int copyfile(const char *src, const char *dst)
659{
660 int fd_src = -1, fd_dst = -1;
661 void *buf = NULL;
662 ssize_t size;
663 size_t count;
664 int ret = -1;
665
666 fd_src = open(src, O_RDONLY);
667 if (fd_src < 0) {
668 printf("Can't open file %s (%s)\n", src, strerror(errno));
669 goto out;
670 }
671
Thomas Hebbab5a2b02020-03-01 10:47:53 -0800672 fd_dst = open(dst, O_WRONLY | O_CREAT, 0666);
Philippe Reynes7298e422019-12-18 18:25:41 +0100673 if (fd_dst < 0) {
674 printf("Can't open file %s (%s)\n", dst, strerror(errno));
675 goto out;
676 }
677
Fabio Estevamaaa91a42020-07-27 21:03:13 -0300678 buf = calloc(1, 512);
Philippe Reynes7298e422019-12-18 18:25:41 +0100679 if (!buf) {
680 printf("Can't allocate buffer to copy file\n");
681 goto out;
682 }
683
684 while (1) {
685 size = read(fd_src, buf, 512);
686 if (size < 0) {
687 printf("Can't read file %s\n", src);
688 goto out;
689 }
690 if (!size)
691 break;
692
693 count = size;
694 size = write(fd_dst, buf, count);
695 if (size < 0) {
696 printf("Can't write file %s\n", dst);
697 goto out;
698 }
699 }
700
701 ret = 0;
702
703 out:
704 if (fd_src >= 0)
705 close(fd_src);
706 if (fd_dst >= 0)
707 close(fd_dst);
708 if (buf)
709 free(buf);
710
711 return ret;
712}
713
Simon Glass722ebc82016-02-22 22:55:53 -0700714/**
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530715 * fit_handle_file - main FIT file processing function
716 *
717 * fit_handle_file() runs dtc to convert .its to .itb, includes
718 * binary data, updates timestamp property and calculates hashes.
719 *
720 * datafile - .its file
721 * imagefile - .itb file
722 *
723 * returns:
724 * only on success, otherwise calls exit (EXIT_FAILURE);
725 */
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700726static int fit_handle_file(struct image_tool_params *params)
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530727{
728 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
Philippe Reynes7298e422019-12-18 18:25:41 +0100729 char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0};
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530730 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
Simon Glassa9468112014-06-02 22:04:53 -0600731 size_t size_inc;
732 int ret;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530733
734 /* Flattened Image Tree (FIT) format handling */
735 debug ("FIT format handling\n");
736
737 /* call dtc to include binary properties into the tmp file */
738 if (strlen (params->imagefile) +
739 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
740 fprintf (stderr, "%s: Image file name (%s) too long, "
Sven Roederer9c702372021-05-25 23:15:27 +0200741 "can't create tmpfile.\n",
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530742 params->imagefile, params->cmdname);
743 return (EXIT_FAILURE);
744 }
745 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
746
Simon Glass95d77b42013-06-13 15:10:05 -0700747 /* We either compile the source file, or use the existing FIT image */
Simon Glass8e35bb02016-02-22 22:55:51 -0700748 if (params->auto_its) {
749 if (fit_build(params, tmpfile)) {
750 fprintf(stderr, "%s: failed to build FIT\n",
751 params->cmdname);
752 return EXIT_FAILURE;
753 }
754 *cmd = '\0';
755 } else if (params->datafile) {
Stefan Theil63f881d2018-03-08 09:00:13 +0100756 /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
757 snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
758 MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
Simon Glass95d77b42013-06-13 15:10:05 -0700759 debug("Trying to execute \"%s\"\n", cmd);
760 } else {
Mirza, Taimoora6e98102017-10-04 20:28:03 +0500761 snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
Simon Glass95d77b42013-06-13 15:10:05 -0700762 params->imagefile, tmpfile);
763 }
Sven Roedererea5d3732020-04-27 02:08:39 +0200764 if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) {
765 fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n");
766 }
Philippe Reynes7298e422019-12-18 18:25:41 +0100767
Simon Glass8e35bb02016-02-22 22:55:51 -0700768 if (*cmd && system(cmd) == -1) {
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530769 fprintf (stderr, "%s: system(%s) failed: %s\n",
770 params->cmdname, cmd, strerror(errno));
Simon Glassaa6d6db2013-05-08 08:05:57 +0000771 goto err_system;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530772 }
773
Simon Glass529fd182016-02-22 22:55:54 -0700774 /* Move the data so it is internal to the FIT, if needed */
775 ret = fit_import_data(params, tmpfile);
776 if (ret)
777 goto err_system;
778
Simon Glassa9468112014-06-02 22:04:53 -0600779 /*
Philippe Reynes7298e422019-12-18 18:25:41 +0100780 * Copy the tmpfile to bakfile, then in the following loop
781 * we copy bakfile to tmpfile. So we always start from the
782 * beginning.
783 */
784 sprintf(bakfile, "%s%s", tmpfile, ".bak");
785 rename(tmpfile, bakfile);
786
787 /*
Simon Glassa9468112014-06-02 22:04:53 -0600788 * Set hashes for images in the blob. Unfortunately we may need more
789 * space in either FDT, so keep trying until we succeed.
790 *
791 * Note: this is pretty inefficient for signing, since we must
792 * calculate the signature every time. It would be better to calculate
793 * all the data and then store it in a separate step. However, this
794 * would be considerably more complex to implement. Generally a few
795 * steps of this loop is enough to sign with several keys.
796 */
797 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
Philippe Reynes7298e422019-12-18 18:25:41 +0100798 if (copyfile(bakfile, tmpfile) < 0) {
799 printf("Can't copy %s to %s\n", bakfile, tmpfile);
800 ret = -EIO;
801 break;
802 }
Simon Glassa9468112014-06-02 22:04:53 -0600803 ret = fit_add_file_data(params, size_inc, tmpfile);
804 if (!ret || ret != -ENOSPC)
805 break;
Simon Glasse29495d2013-06-13 15:10:04 -0700806 }
807
Simon Glassa9468112014-06-02 22:04:53 -0600808 if (ret) {
Simon Glass655cc692016-07-03 09:40:43 -0600809 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
810 params->cmdname, ret);
Simon Glassa9468112014-06-02 22:04:53 -0600811 goto err_system;
Simon Glasse29495d2013-06-13 15:10:04 -0700812 }
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530813
Simon Glass722ebc82016-02-22 22:55:53 -0700814 /* Move the data so it is external to the FIT, if requested */
815 if (params->external_data) {
816 ret = fit_extract_data(params, tmpfile);
817 if (ret)
818 goto err_system;
819 }
820
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530821 if (rename (tmpfile, params->imagefile) == -1) {
822 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
823 params->cmdname, tmpfile, params->imagefile,
824 strerror (errno));
825 unlink (tmpfile);
Philippe Reynes7298e422019-12-18 18:25:41 +0100826 unlink(bakfile);
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530827 unlink (params->imagefile);
Simon Glassa9468112014-06-02 22:04:53 -0600828 return EXIT_FAILURE;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530829 }
Philippe Reynes7298e422019-12-18 18:25:41 +0100830 unlink(bakfile);
Simon Glassa9468112014-06-02 22:04:53 -0600831 return EXIT_SUCCESS;
Simon Glassaa6d6db2013-05-08 08:05:57 +0000832
Simon Glassaa6d6db2013-05-08 08:05:57 +0000833err_system:
834 unlink(tmpfile);
Philippe Reynes7298e422019-12-18 18:25:41 +0100835 unlink(bakfile);
Simon Glassaa6d6db2013-05-08 08:05:57 +0000836 return -1;
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530837}
838
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200839/**
840 * fit_image_extract - extract a FIT component image
841 * @fit: pointer to the FIT format image header
842 * @image_noffset: offset of the component image node
843 * @file_name: name of the file to store the FIT sub-image
844 *
845 * returns:
846 * zero in case of success or a negative value if fail.
847 */
848static int fit_image_extract(
849 const void *fit,
850 int image_noffset,
851 const char *file_name)
852{
853 const void *file_data;
854 size_t file_size = 0;
Andrew F. Davise5b56282019-09-17 17:09:34 -0400855 int ret;
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200856
Andrew F. Davise5b56282019-09-17 17:09:34 -0400857 /* get the data address and size of component at offset "image_noffset" */
858 ret = fit_image_get_data_and_size(fit, image_noffset, &file_data, &file_size);
859 if (ret) {
860 fprintf(stderr, "Could not get component information\n");
861 return ret;
862 }
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200863
864 /* save the "file_data" into the file specified by "file_name" */
865 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
866}
867
868/**
869 * fit_extract_contents - retrieve a sub-image component from the FIT image
870 * @ptr: pointer to the FIT format image header
871 * @params: command line parameters
872 *
873 * returns:
874 * zero in case of success or a negative value if fail.
875 */
876static int fit_extract_contents(void *ptr, struct image_tool_params *params)
877{
878 int images_noffset;
879 int noffset;
880 int ndepth;
881 const void *fit = ptr;
882 int count = 0;
883 const char *p;
884
885 /* Indent string is defined in header image.h */
886 p = IMAGE_INDENT_STRING;
887
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200888 /* Find images parent node offset */
889 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
890 if (images_noffset < 0) {
891 printf("Can't find images parent node '%s' (%s)\n",
892 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
893 return -1;
894 }
895
896 /* Avoid any overrun */
897 count = fit_get_subimage_count(fit, images_noffset);
898 if ((params->pflag < 0) || (count <= params->pflag)) {
899 printf("No such component at '%d'\n", params->pflag);
900 return -1;
901 }
902
903 /* Process its subnodes, extract the desired component from image */
904 for (ndepth = 0, count = 0,
905 noffset = fdt_next_node(fit, images_noffset, &ndepth);
906 (noffset >= 0) && (ndepth > 0);
907 noffset = fdt_next_node(fit, noffset, &ndepth)) {
908 if (ndepth == 1) {
909 /*
910 * Direct child node of the images parent node,
911 * i.e. component image node.
912 */
913 if (params->pflag == count) {
914 printf("Extracted:\n%s Image %u (%s)\n", p,
915 count, fit_get_name(fit, noffset, NULL));
916
917 fit_image_print(fit, noffset, p);
918
919 return fit_image_extract(fit, noffset,
920 params->outfile);
921 }
922
923 count++;
924 }
925 }
926
927 return 0;
928}
929
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700930static int fit_check_params(struct image_tool_params *params)
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530931{
Simon Glass8e35bb02016-02-22 22:55:51 -0700932 if (params->auto_its)
933 return 0;
Heinrich Schuchardt58194662019-12-11 13:51:38 +0100934 return ((params->dflag && params->fflag) ||
935 (params->fflag && params->lflag) ||
936 (params->lflag && params->dflag));
Prafulla Wadaskar89a4d6b2009-08-19 17:36:46 +0530937}
938
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200939U_BOOT_IMAGE_TYPE(
940 fitimage,
941 "FIT Image support",
942 sizeof(image_header_t),
943 (void *)&header,
944 fit_check_params,
945 fit_verify_header,
946 fit_print_contents,
947 NULL,
Guilherme Maciel Ferreira39931f92015-01-15 02:54:42 -0200948 fit_extract_contents,
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200949 fit_check_image_types,
950 fit_handle_file,
951 NULL /* FIT images use DTB header */
952);