blob: a082d9386d200d095753eae45fec826b1a718269 [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
25static image_header_t header;
26
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 Glass802aa822014-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 Boccassibff733f2019-05-14 19:35:02 +010046 &dest_blob, &dest_sbuf, false,
47 false);
Simon Glass802aa822014-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 */
Vagrant Cascadianf8e066c2016-06-16 12:28:40 -070056 if (params->datafile) {
Alex Kiernanc4c7e0d2018-06-20 20:10:51 +000057 time_t time = imagetool_get_source_date(params->cmdname,
58 sbuf.st_mtime);
Vagrant Cascadianf8e066c2016-06-16 12:28:40 -070059 ret = fit_set_timestamp(ptr, 0, time);
60 }
Simon Glass802aa822014-06-02 22:04:53 -060061
62 if (!ret) {
Philippe Reynes3148e422019-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) {
Simon Glass802aa822014-06-02 22:04:53 -060071 ret = fit_add_verification_data(params->keydir, dest_blob, ptr,
72 params->comment,
George McCollister23d14892017-01-06 13:14:17 -060073 params->require_keys,
Alex Kiernan697fcdc2018-06-20 20:10:52 +000074 params->engine_id,
75 params->cmdname);
Simon Glass802aa822014-06-02 22:04:53 -060076 }
77
78 if (dest_blob) {
79 munmap(dest_blob, destfd_size);
80 close(destfd);
81 }
82
83err_keydest:
84 munmap(ptr, sbuf.st_size);
85 close(tfd);
Simon Glass802aa822014-06-02 22:04:53 -060086 return ret;
87}
88
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053089/**
Simon Glass88e31cb2016-02-22 22:55:51 -070090 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
91 */
92static int fit_calc_size(struct image_tool_params *params)
93{
Simon Glassbd8bc5d2016-02-22 22:55:52 -070094 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -070095 int size, total_size;
96
97 size = imagetool_get_filesize(params, params->datafile);
98 if (size < 0)
99 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -0700100 total_size = size;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100101
102 if (params->fit_ramdisk) {
103 size = imagetool_get_filesize(params, params->fit_ramdisk);
104 if (size < 0)
105 return -1;
106 total_size += size;
107 }
108
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700109 for (cont = params->content_head; cont; cont = cont->next) {
110 size = imagetool_get_filesize(params, cont->fname);
111 if (size < 0)
112 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -0700113
Simon Glass611fd912020-05-27 07:24:55 -0600114 /* Add space for properties and hash node */
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700115 total_size += size + 300;
116 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700117
118 /* Add plenty of space for headers, properties, nodes, etc. */
119 total_size += 4096;
120
121 return total_size;
122}
123
124static int fdt_property_file(struct image_tool_params *params,
125 void *fdt, const char *name, const char *fname)
126{
127 struct stat sbuf;
128 void *ptr;
129 int ret;
130 int fd;
131
132 fd = open(fname, O_RDWR | O_BINARY);
133 if (fd < 0) {
134 fprintf(stderr, "%s: Can't open %s: %s\n",
135 params->cmdname, fname, strerror(errno));
136 return -1;
137 }
138
139 if (fstat(fd, &sbuf) < 0) {
140 fprintf(stderr, "%s: Can't stat %s: %s\n",
141 params->cmdname, fname, strerror(errno));
142 goto err;
143 }
144
145 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
146 if (ret)
Simon Glass9ab7f052016-03-16 07:45:42 -0600147 goto err;
Simon Glass88e31cb2016-02-22 22:55:51 -0700148 ret = read(fd, ptr, sbuf.st_size);
149 if (ret != sbuf.st_size) {
150 fprintf(stderr, "%s: Can't read %s: %s\n",
151 params->cmdname, fname, strerror(errno));
152 goto err;
153 }
Simon Glass9ab7f052016-03-16 07:45:42 -0600154 close(fd);
Simon Glass88e31cb2016-02-22 22:55:51 -0700155
156 return 0;
157err:
158 close(fd);
159 return -1;
160}
161
162static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
163{
164 char str[100];
165 va_list ptr;
166
167 va_start(ptr, fmt);
168 vsnprintf(str, sizeof(str), fmt, ptr);
169 va_end(ptr);
170 return fdt_property_string(fdt, name, str);
171}
172
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700173static void get_basename(char *str, int size, const char *fname)
174{
175 const char *p, *start, *end;
176 int len;
177
178 /*
179 * Use the base name as the 'name' field. So for example:
180 *
181 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
182 * becomes "sun7i-a20-bananapro"
183 */
184 p = strrchr(fname, '/');
185 start = p ? p + 1 : fname;
186 p = strrchr(fname, '.');
187 end = p ? p : fname + strlen(fname);
188 len = end - start;
189 if (len >= size)
190 len = size - 1;
191 memcpy(str, start, len);
192 str[len] = '\0';
193}
194
Simon Glass88e31cb2016-02-22 22:55:51 -0700195/**
Simon Glass611fd912020-05-27 07:24:55 -0600196 * add_crc_node() - Add a hash node to request a CRC checksum for an image
197 *
198 * @fdt: Device tree to add to (in sequential-write mode)
199 */
200static void add_crc_node(void *fdt)
201{
202 fdt_begin_node(fdt, "hash-1");
203 fdt_property_string(fdt, FIT_ALGO_PROP, "crc32");
204 fdt_end_node(fdt);
205}
206
207/**
Simon Glass88e31cb2016-02-22 22:55:51 -0700208 * fit_write_images() - Write out a list of images to the FIT
209 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700210 * We always include the main image (params->datafile). If there are device
Andre Przywara9ef25e82017-12-04 02:05:12 +0000211 * tree files, we include an fdt- node for each of those too.
Simon Glass88e31cb2016-02-22 22:55:51 -0700212 */
213static int fit_write_images(struct image_tool_params *params, char *fdt)
214{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700215 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700216 const char *typename;
217 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700218 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700219 int ret;
220
221 fdt_begin_node(fdt, "images");
222
223 /* First the main image */
224 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000225 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glass88e31cb2016-02-22 22:55:51 -0700226 fdt_begin_node(fdt, str);
Michal Simeka4e678d2018-07-20 12:31:02 +0200227 fdt_property_string(fdt, FIT_DESC_PROP, params->imagename);
228 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
229 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassaeeea7f2016-06-30 10:52:12 -0600230 genimg_get_arch_short_name(params->arch));
Michal Simeka4e678d2018-07-20 12:31:02 +0200231 fdt_property_string(fdt, FIT_OS_PROP,
232 genimg_get_os_short_name(params->os));
233 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glass88e31cb2016-02-22 22:55:51 -0700234 genimg_get_comp_short_name(params->comp));
Michal Simeka4e678d2018-07-20 12:31:02 +0200235 fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr);
236 fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep);
Simon Glass88e31cb2016-02-22 22:55:51 -0700237
238 /*
239 * Put data last since it is large. SPL may only load the first part
240 * of the DT, so this way it can access all the above fields.
241 */
Michal Simeka4e678d2018-07-20 12:31:02 +0200242 ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile);
Simon Glass88e31cb2016-02-22 22:55:51 -0700243 if (ret)
244 return ret;
Simon Glass611fd912020-05-27 07:24:55 -0600245 add_crc_node(fdt);
Simon Glass88e31cb2016-02-22 22:55:51 -0700246 fdt_end_node(fdt);
247
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700248 /* Now the device tree files if available */
249 upto = 0;
250 for (cont = params->content_head; cont; cont = cont->next) {
251 if (cont->type != IH_TYPE_FLATDT)
252 continue;
Michal Sojka8a90ce82019-09-13 12:43:12 +0200253 typename = genimg_get_type_short_name(cont->type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000254 snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700255 fdt_begin_node(fdt, str);
256
257 get_basename(str, sizeof(str), cont->fname);
Michal Simeka4e678d2018-07-20 12:31:02 +0200258 fdt_property_string(fdt, FIT_DESC_PROP, str);
259 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
260 cont->fname);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700261 if (ret)
262 return ret;
Michal Simeka4e678d2018-07-20 12:31:02 +0200263 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
264 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700265 genimg_get_arch_short_name(params->arch));
Michal Simeka4e678d2018-07-20 12:31:02 +0200266 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700267 genimg_get_comp_short_name(IH_COMP_NONE));
Simon Glass611fd912020-05-27 07:24:55 -0600268 add_crc_node(fdt);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700269 fdt_end_node(fdt);
270 }
271
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100272 /* And a ramdisk file if available */
273 if (params->fit_ramdisk) {
Andre Przywara9ef25e82017-12-04 02:05:12 +0000274 fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100275
Michal Simeka4e678d2018-07-20 12:31:02 +0200276 fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP);
277 fdt_property_string(fdt, FIT_OS_PROP,
278 genimg_get_os_short_name(params->os));
Michal Sojka8a90ce82019-09-13 12:43:12 +0200279 fdt_property_string(fdt, FIT_ARCH_PROP,
280 genimg_get_arch_short_name(params->arch));
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100281
Michal Simeka4e678d2018-07-20 12:31:02 +0200282 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
283 params->fit_ramdisk);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100284 if (ret)
285 return ret;
Simon Glass611fd912020-05-27 07:24:55 -0600286 add_crc_node(fdt);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100287 fdt_end_node(fdt);
288 }
289
Simon Glass88e31cb2016-02-22 22:55:51 -0700290 fdt_end_node(fdt);
291
292 return 0;
293}
294
295/**
296 * fit_write_configs() - Write out a list of configurations to the FIT
297 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700298 * If there are device tree files, we include a configuration for each, which
299 * selects the main image (params->datafile) and its corresponding device
300 * tree file.
301 *
302 * Otherwise we just create a configuration with the main image in it.
Simon Glass88e31cb2016-02-22 22:55:51 -0700303 */
304static void fit_write_configs(struct image_tool_params *params, char *fdt)
305{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700306 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700307 const char *typename;
308 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700309 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700310
311 fdt_begin_node(fdt, "configurations");
Michal Simeka4e678d2018-07-20 12:31:02 +0200312 fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
Simon Glass88e31cb2016-02-22 22:55:51 -0700313
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700314 upto = 0;
315 for (cont = params->content_head; cont; cont = cont->next) {
316 if (cont->type != IH_TYPE_FLATDT)
317 continue;
318 typename = genimg_get_type_short_name(cont->type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000319 snprintf(str, sizeof(str), "conf-%d", ++upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700320 fdt_begin_node(fdt, str);
321
322 get_basename(str, sizeof(str), cont->fname);
Michal Simeka4e678d2018-07-20 12:31:02 +0200323 fdt_property_string(fdt, FIT_DESC_PROP, str);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700324
325 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000326 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700327 fdt_property_string(fdt, typename, str);
Abel Vesa7e0cb5e2019-03-12 08:34:32 +0000328 fdt_property_string(fdt, FIT_LOADABLE_PROP, str);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700329
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100330 if (params->fit_ramdisk)
331 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara9ef25e82017-12-04 02:05:12 +0000332 FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100333
Andre Przywara9ef25e82017-12-04 02:05:12 +0000334 snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700335 fdt_property_string(fdt, FIT_FDT_PROP, str);
336 fdt_end_node(fdt);
337 }
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100338
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700339 if (!upto) {
Andre Przywara9ef25e82017-12-04 02:05:12 +0000340 fdt_begin_node(fdt, "conf-1");
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700341 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000342 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700343 fdt_property_string(fdt, typename, str);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100344
345 if (params->fit_ramdisk)
346 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara9ef25e82017-12-04 02:05:12 +0000347 FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100348
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700349 fdt_end_node(fdt);
350 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700351
352 fdt_end_node(fdt);
353}
354
355static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
356{
357 int ret;
358
359 ret = fdt_create(fdt, size);
360 if (ret)
361 return ret;
362 fdt_finish_reservemap(fdt);
363 fdt_begin_node(fdt, "");
Michal Simeka4e678d2018-07-20 12:31:02 +0200364 fdt_property_strf(fdt, FIT_DESC_PROP,
Simon Glass88e31cb2016-02-22 22:55:51 -0700365 "%s image with one or more FDT blobs",
366 genimg_get_type_name(params->fit_image_type));
367 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
368 fdt_property_u32(fdt, "#address-cells", 1);
369 ret = fit_write_images(params, fdt);
370 if (ret)
371 return ret;
372 fit_write_configs(params, fdt);
373 fdt_end_node(fdt);
374 ret = fdt_finish(fdt);
375 if (ret)
376 return ret;
377
378 return fdt_totalsize(fdt);
379}
380
381static int fit_build(struct image_tool_params *params, const char *fname)
382{
383 char *buf;
384 int size;
385 int ret;
386 int fd;
387
388 size = fit_calc_size(params);
389 if (size < 0)
390 return -1;
391 buf = malloc(size);
392 if (!buf) {
393 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
394 params->cmdname, size);
395 return -1;
396 }
397 ret = fit_build_fdt(params, buf, size);
398 if (ret < 0) {
399 fprintf(stderr, "%s: Failed to build FIT image\n",
400 params->cmdname);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600401 goto err_buf;
Simon Glass88e31cb2016-02-22 22:55:51 -0700402 }
403 size = ret;
404 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
405 if (fd < 0) {
406 fprintf(stderr, "%s: Can't open %s: %s\n",
407 params->cmdname, fname, strerror(errno));
Tom Rinib8286652017-09-26 22:14:44 -0400408 goto err_buf;
Simon Glass88e31cb2016-02-22 22:55:51 -0700409 }
410 ret = write(fd, buf, size);
411 if (ret != size) {
412 fprintf(stderr, "%s: Can't write %s: %s\n",
413 params->cmdname, fname, strerror(errno));
Simon Glass88e31cb2016-02-22 22:55:51 -0700414 goto err;
415 }
416 close(fd);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600417 free(buf);
Simon Glass88e31cb2016-02-22 22:55:51 -0700418
419 return 0;
420err:
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600421 close(fd);
422err_buf:
Simon Glass88e31cb2016-02-22 22:55:51 -0700423 free(buf);
424 return -1;
425}
426
427/**
Simon Glassafd728c2016-02-22 22:55:53 -0700428 * fit_extract_data() - Move all data outside the FIT
429 *
430 * This takes a normal FIT file and removes all the 'data' properties from it.
431 * The data is placed in an area after the FIT so that it can be accessed
432 * using an offset into that area. The 'data' properties turn into
433 * 'data-offset' properties.
434 *
435 * This function cannot cope with FITs with 'data-offset' properties. All
436 * data must be in 'data' properties on entry.
437 */
438static int fit_extract_data(struct image_tool_params *params, const char *fname)
439{
Kever Yang8e238b52020-03-30 11:56:24 +0800440 void *buf = NULL;
Simon Glassafd728c2016-02-22 22:55:53 -0700441 int buf_ptr;
442 int fit_size, new_size;
443 int fd;
444 struct stat sbuf;
445 void *fdt;
446 int ret;
447 int images;
448 int node;
Kever Yang8e238b52020-03-30 11:56:24 +0800449 int image_number;
450 int align_size;
Simon Glassafd728c2016-02-22 22:55:53 -0700451
Tom Rini96642972020-05-06 11:05:17 -0400452 align_size = params->bl_len ? params->bl_len : 4;
Luca Boccassibff733f2019-05-14 19:35:02 +0100453 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false);
Simon Glassafd728c2016-02-22 22:55:53 -0700454 if (fd < 0)
455 return -EIO;
456 fit_size = fdt_totalsize(fdt);
457
Simon Glassafd728c2016-02-22 22:55:53 -0700458 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
459 if (images < 0) {
460 debug("%s: Cannot find /images node: %d\n", __func__, images);
461 ret = -EINVAL;
Simon Glass4e7505f2016-03-16 07:45:39 -0600462 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700463 }
Kever Yang8e238b52020-03-30 11:56:24 +0800464 image_number = fdtdec_get_child_count(fdt, images);
465
466 /*
467 * Allocate space to hold the image data we will extract,
468 * extral space allocate for image alignment to prevent overflow.
469 */
470 buf = malloc(fit_size + (align_size * image_number));
471 if (!buf) {
472 ret = -ENOMEM;
473 goto err_munmap;
474 }
475 buf_ptr = 0;
Simon Glassafd728c2016-02-22 22:55:53 -0700476
477 for (node = fdt_first_subnode(fdt, images);
478 node >= 0;
479 node = fdt_next_subnode(fdt, node)) {
480 const char *data;
481 int len;
482
Michal Simeka4e678d2018-07-20 12:31:02 +0200483 data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
Simon Glassafd728c2016-02-22 22:55:53 -0700484 if (!data)
485 continue;
486 memcpy(buf + buf_ptr, data, len);
487 debug("Extracting data size %x\n", len);
488
Michal Simeka4e678d2018-07-20 12:31:02 +0200489 ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
Simon Glassafd728c2016-02-22 22:55:53 -0700490 if (ret) {
491 ret = -EPERM;
Simon Glass4e7505f2016-03-16 07:45:39 -0600492 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700493 }
Teddy Reeda8457622016-06-09 19:38:02 -0700494 if (params->external_offset > 0) {
495 /* An external offset positions the data absolutely. */
Michal Simeka4e678d2018-07-20 12:31:02 +0200496 fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
Teddy Reeda8457622016-06-09 19:38:02 -0700497 params->external_offset + buf_ptr);
498 } else {
Michal Simeka4e678d2018-07-20 12:31:02 +0200499 fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
500 buf_ptr);
Teddy Reeda8457622016-06-09 19:38:02 -0700501 }
Michal Simeka4e678d2018-07-20 12:31:02 +0200502 fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
Kever Yang8e238b52020-03-30 11:56:24 +0800503 buf_ptr += ALIGN(len, align_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700504 }
505
506 /* Pack the FDT and place the data after it */
507 fdt_pack(fdt);
508
Kever Yang8e238b52020-03-30 11:56:24 +0800509 new_size = fdt_totalsize(fdt);
Tom Rini96642972020-05-06 11:05:17 -0400510 new_size = ALIGN(new_size, align_size);
Kever Yang8e238b52020-03-30 11:56:24 +0800511 fdt_set_totalsize(fdt, new_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700512 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
513 debug("External data size %x\n", buf_ptr);
Simon Glassafd728c2016-02-22 22:55:53 -0700514 munmap(fdt, sbuf.st_size);
515
516 if (ftruncate(fd, new_size)) {
517 debug("%s: Failed to truncate file: %s\n", __func__,
518 strerror(errno));
519 ret = -EIO;
520 goto err;
521 }
Teddy Reeda8457622016-06-09 19:38:02 -0700522
523 /* Check if an offset for the external data was set. */
524 if (params->external_offset > 0) {
525 if (params->external_offset < new_size) {
526 debug("External offset %x overlaps FIT length %x",
527 params->external_offset, new_size);
528 ret = -EINVAL;
529 goto err;
530 }
531 new_size = params->external_offset;
532 }
Simon Glassafd728c2016-02-22 22:55:53 -0700533 if (lseek(fd, new_size, SEEK_SET) < 0) {
534 debug("%s: Failed to seek to end of file: %s\n", __func__,
535 strerror(errno));
536 ret = -EIO;
537 goto err;
538 }
539 if (write(fd, buf, buf_ptr) != buf_ptr) {
540 debug("%s: Failed to write external data to file %s\n",
541 __func__, strerror(errno));
542 ret = -EIO;
543 goto err;
544 }
Tom Rinib8286652017-09-26 22:14:44 -0400545 free(buf);
Simon Glass4e7505f2016-03-16 07:45:39 -0600546 close(fd);
547 return 0;
Simon Glassafd728c2016-02-22 22:55:53 -0700548
Simon Glass4e7505f2016-03-16 07:45:39 -0600549err_munmap:
550 munmap(fdt, sbuf.st_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700551err:
Bin Meng6eb238742020-04-18 01:59:11 -0700552 free(buf);
Simon Glassafd728c2016-02-22 22:55:53 -0700553 close(fd);
554 return ret;
555}
556
Simon Glassd11b7002016-02-22 22:55:54 -0700557static int fit_import_data(struct image_tool_params *params, const char *fname)
558{
559 void *fdt, *old_fdt;
560 int fit_size, new_size, size, data_base;
561 int fd;
562 struct stat sbuf;
563 int ret;
564 int images;
565 int node;
566
Luca Boccassibff733f2019-05-14 19:35:02 +0100567 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false);
Simon Glassd11b7002016-02-22 22:55:54 -0700568 if (fd < 0)
569 return -EIO;
570 fit_size = fdt_totalsize(old_fdt);
Kever Yang8c11bc12020-03-30 11:56:22 +0800571 data_base = ALIGN(fit_size, 4);
Simon Glassd11b7002016-02-22 22:55:54 -0700572
573 /* Allocate space to hold the new FIT */
574 size = sbuf.st_size + 16384;
575 fdt = malloc(size);
576 if (!fdt) {
577 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
578 __func__, size);
579 ret = -ENOMEM;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700580 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700581 }
582 ret = fdt_open_into(old_fdt, fdt, size);
583 if (ret) {
584 debug("%s: Failed to expand FIT: %s\n", __func__,
585 fdt_strerror(errno));
586 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700587 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700588 }
589
590 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
591 if (images < 0) {
592 debug("%s: Cannot find /images node: %d\n", __func__, images);
593 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700594 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700595 }
596
597 for (node = fdt_first_subnode(fdt, images);
598 node >= 0;
599 node = fdt_next_subnode(fdt, node)) {
600 int buf_ptr;
601 int len;
602
603 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
604 len = fdtdec_get_int(fdt, node, "data-size", -1);
605 if (buf_ptr == -1 || len == -1)
606 continue;
607 debug("Importing data size %x\n", len);
608
609 ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
610 len);
611 if (ret) {
612 debug("%s: Failed to write property: %s\n", __func__,
613 fdt_strerror(ret));
614 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700615 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700616 }
617 }
618
Lihua Zhao4536ef82020-04-18 01:59:10 -0700619 munmap(old_fdt, sbuf.st_size);
620
Tom Rinia29829e2017-10-07 11:27:59 -0400621 /* Close the old fd so we can re-use it. */
Simon Glassd11b7002016-02-22 22:55:54 -0700622 close(fd);
623
624 /* Pack the FDT and place the data after it */
625 fdt_pack(fdt);
626
627 new_size = fdt_totalsize(fdt);
628 debug("Size expanded from %x to %x\n", fit_size, new_size);
629
630 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
631 if (fd < 0) {
632 fprintf(stderr, "%s: Can't open %s: %s\n",
633 params->cmdname, fname, strerror(errno));
Tom Rinia29829e2017-10-07 11:27:59 -0400634 ret = -EIO;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700635 goto err;
Simon Glassd11b7002016-02-22 22:55:54 -0700636 }
637 if (write(fd, fdt, new_size) != new_size) {
638 debug("%s: Failed to write external data to file %s\n",
639 __func__, strerror(errno));
640 ret = -EIO;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700641 goto err;
Simon Glassd11b7002016-02-22 22:55:54 -0700642 }
Simon Glassd11b7002016-02-22 22:55:54 -0700643
Lihua Zhao4536ef82020-04-18 01:59:10 -0700644 free(fdt);
Tom Rinia29829e2017-10-07 11:27:59 -0400645 close(fd);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700646 return 0;
647
648err_munmap:
Tom Rinib8286652017-09-26 22:14:44 -0400649 munmap(old_fdt, sbuf.st_size);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700650err:
Simon Glass5014cc12016-03-16 07:45:38 -0600651 free(fdt);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700652 close(fd);
Simon Glassd11b7002016-02-22 22:55:54 -0700653 return ret;
654}
655
Philippe Reynes3148e422019-12-18 18:25:41 +0100656static int copyfile(const char *src, const char *dst)
657{
658 int fd_src = -1, fd_dst = -1;
659 void *buf = NULL;
660 ssize_t size;
661 size_t count;
662 int ret = -1;
663
664 fd_src = open(src, O_RDONLY);
665 if (fd_src < 0) {
666 printf("Can't open file %s (%s)\n", src, strerror(errno));
667 goto out;
668 }
669
Thomas Hebb97caa072020-03-01 10:47:53 -0800670 fd_dst = open(dst, O_WRONLY | O_CREAT, 0666);
Philippe Reynes3148e422019-12-18 18:25:41 +0100671 if (fd_dst < 0) {
672 printf("Can't open file %s (%s)\n", dst, strerror(errno));
673 goto out;
674 }
675
676 buf = malloc(512);
677 if (!buf) {
678 printf("Can't allocate buffer to copy file\n");
679 goto out;
680 }
681
682 while (1) {
683 size = read(fd_src, buf, 512);
684 if (size < 0) {
685 printf("Can't read file %s\n", src);
686 goto out;
687 }
688 if (!size)
689 break;
690
691 count = size;
692 size = write(fd_dst, buf, count);
693 if (size < 0) {
694 printf("Can't write file %s\n", dst);
695 goto out;
696 }
697 }
698
699 ret = 0;
700
701 out:
702 if (fd_src >= 0)
703 close(fd_src);
704 if (fd_dst >= 0)
705 close(fd_dst);
706 if (buf)
707 free(buf);
708
709 return ret;
710}
711
Simon Glassafd728c2016-02-22 22:55:53 -0700712/**
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530713 * fit_handle_file - main FIT file processing function
714 *
715 * fit_handle_file() runs dtc to convert .its to .itb, includes
716 * binary data, updates timestamp property and calculates hashes.
717 *
718 * datafile - .its file
719 * imagefile - .itb file
720 *
721 * returns:
722 * only on success, otherwise calls exit (EXIT_FAILURE);
723 */
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700724static int fit_handle_file(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530725{
726 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
Philippe Reynes3148e422019-12-18 18:25:41 +0100727 char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0};
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530728 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
Simon Glass802aa822014-06-02 22:04:53 -0600729 size_t size_inc;
730 int ret;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530731
732 /* Flattened Image Tree (FIT) format handling */
733 debug ("FIT format handling\n");
734
735 /* call dtc to include binary properties into the tmp file */
736 if (strlen (params->imagefile) +
737 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
738 fprintf (stderr, "%s: Image file name (%s) too long, "
739 "can't create tmpfile",
740 params->imagefile, params->cmdname);
741 return (EXIT_FAILURE);
742 }
743 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
744
Simon Glassce8c3ca2013-06-13 15:10:05 -0700745 /* We either compile the source file, or use the existing FIT image */
Simon Glass88e31cb2016-02-22 22:55:51 -0700746 if (params->auto_its) {
747 if (fit_build(params, tmpfile)) {
748 fprintf(stderr, "%s: failed to build FIT\n",
749 params->cmdname);
750 return EXIT_FAILURE;
751 }
752 *cmd = '\0';
753 } else if (params->datafile) {
Stefan Theilff6c3b22018-03-08 09:00:13 +0100754 /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
755 snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
756 MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
Simon Glassce8c3ca2013-06-13 15:10:05 -0700757 debug("Trying to execute \"%s\"\n", cmd);
758 } else {
Mirza, Taimoor74a21dd2017-10-04 20:28:03 +0500759 snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
Simon Glassce8c3ca2013-06-13 15:10:05 -0700760 params->imagefile, tmpfile);
761 }
Sven Roederercbcb3992020-04-27 02:08:39 +0200762 if (strlen(cmd) >= MKIMAGE_MAX_DTC_CMDLINE_LEN - 1) {
763 fprintf(stderr, "WARNING: command-line for FIT creation might be truncated and will probably fail.\n");
764 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100765
Simon Glass88e31cb2016-02-22 22:55:51 -0700766 if (*cmd && system(cmd) == -1) {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530767 fprintf (stderr, "%s: system(%s) failed: %s\n",
768 params->cmdname, cmd, strerror(errno));
Simon Glass4161c412013-05-08 08:05:57 +0000769 goto err_system;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530770 }
771
Simon Glassd11b7002016-02-22 22:55:54 -0700772 /* Move the data so it is internal to the FIT, if needed */
773 ret = fit_import_data(params, tmpfile);
774 if (ret)
775 goto err_system;
776
Simon Glass802aa822014-06-02 22:04:53 -0600777 /*
Philippe Reynes3148e422019-12-18 18:25:41 +0100778 * Copy the tmpfile to bakfile, then in the following loop
779 * we copy bakfile to tmpfile. So we always start from the
780 * beginning.
781 */
782 sprintf(bakfile, "%s%s", tmpfile, ".bak");
783 rename(tmpfile, bakfile);
784
785 /*
Simon Glass802aa822014-06-02 22:04:53 -0600786 * Set hashes for images in the blob. Unfortunately we may need more
787 * space in either FDT, so keep trying until we succeed.
788 *
789 * Note: this is pretty inefficient for signing, since we must
790 * calculate the signature every time. It would be better to calculate
791 * all the data and then store it in a separate step. However, this
792 * would be considerably more complex to implement. Generally a few
793 * steps of this loop is enough to sign with several keys.
794 */
795 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
Philippe Reynes3148e422019-12-18 18:25:41 +0100796 if (copyfile(bakfile, tmpfile) < 0) {
797 printf("Can't copy %s to %s\n", bakfile, tmpfile);
798 ret = -EIO;
799 break;
800 }
Simon Glass802aa822014-06-02 22:04:53 -0600801 ret = fit_add_file_data(params, size_inc, tmpfile);
802 if (!ret || ret != -ENOSPC)
803 break;
Simon Glassb4d8b092013-06-13 15:10:04 -0700804 }
805
Simon Glass802aa822014-06-02 22:04:53 -0600806 if (ret) {
Simon Glass061752e2016-07-03 09:40:43 -0600807 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
808 params->cmdname, ret);
Simon Glass802aa822014-06-02 22:04:53 -0600809 goto err_system;
Simon Glassb4d8b092013-06-13 15:10:04 -0700810 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530811
Simon Glassafd728c2016-02-22 22:55:53 -0700812 /* Move the data so it is external to the FIT, if requested */
813 if (params->external_data) {
814 ret = fit_extract_data(params, tmpfile);
815 if (ret)
816 goto err_system;
817 }
818
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530819 if (rename (tmpfile, params->imagefile) == -1) {
820 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
821 params->cmdname, tmpfile, params->imagefile,
822 strerror (errno));
823 unlink (tmpfile);
Philippe Reynes3148e422019-12-18 18:25:41 +0100824 unlink(bakfile);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530825 unlink (params->imagefile);
Simon Glass802aa822014-06-02 22:04:53 -0600826 return EXIT_FAILURE;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530827 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100828 unlink(bakfile);
Simon Glass802aa822014-06-02 22:04:53 -0600829 return EXIT_SUCCESS;
Simon Glass4161c412013-05-08 08:05:57 +0000830
Simon Glass4161c412013-05-08 08:05:57 +0000831err_system:
832 unlink(tmpfile);
Philippe Reynes3148e422019-12-18 18:25:41 +0100833 unlink(bakfile);
Simon Glass4161c412013-05-08 08:05:57 +0000834 return -1;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530835}
836
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200837/**
838 * fit_image_extract - extract a FIT component image
839 * @fit: pointer to the FIT format image header
840 * @image_noffset: offset of the component image node
841 * @file_name: name of the file to store the FIT sub-image
842 *
843 * returns:
844 * zero in case of success or a negative value if fail.
845 */
846static int fit_image_extract(
847 const void *fit,
848 int image_noffset,
849 const char *file_name)
850{
851 const void *file_data;
852 size_t file_size = 0;
Andrew F. Davis166d5e92019-09-17 17:09:34 -0400853 int ret;
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200854
Andrew F. Davis166d5e92019-09-17 17:09:34 -0400855 /* get the data address and size of component at offset "image_noffset" */
856 ret = fit_image_get_data_and_size(fit, image_noffset, &file_data, &file_size);
857 if (ret) {
858 fprintf(stderr, "Could not get component information\n");
859 return ret;
860 }
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200861
862 /* save the "file_data" into the file specified by "file_name" */
863 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
864}
865
866/**
867 * fit_extract_contents - retrieve a sub-image component from the FIT image
868 * @ptr: pointer to the FIT format image header
869 * @params: command line parameters
870 *
871 * returns:
872 * zero in case of success or a negative value if fail.
873 */
874static int fit_extract_contents(void *ptr, struct image_tool_params *params)
875{
876 int images_noffset;
877 int noffset;
878 int ndepth;
879 const void *fit = ptr;
880 int count = 0;
881 const char *p;
882
883 /* Indent string is defined in header image.h */
884 p = IMAGE_INDENT_STRING;
885
886 if (!fit_check_format(fit)) {
887 printf("Bad FIT image format\n");
888 return -1;
889 }
890
891 /* Find images parent node offset */
892 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
893 if (images_noffset < 0) {
894 printf("Can't find images parent node '%s' (%s)\n",
895 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
896 return -1;
897 }
898
899 /* Avoid any overrun */
900 count = fit_get_subimage_count(fit, images_noffset);
901 if ((params->pflag < 0) || (count <= params->pflag)) {
902 printf("No such component at '%d'\n", params->pflag);
903 return -1;
904 }
905
906 /* Process its subnodes, extract the desired component from image */
907 for (ndepth = 0, count = 0,
908 noffset = fdt_next_node(fit, images_noffset, &ndepth);
909 (noffset >= 0) && (ndepth > 0);
910 noffset = fdt_next_node(fit, noffset, &ndepth)) {
911 if (ndepth == 1) {
912 /*
913 * Direct child node of the images parent node,
914 * i.e. component image node.
915 */
916 if (params->pflag == count) {
917 printf("Extracted:\n%s Image %u (%s)\n", p,
918 count, fit_get_name(fit, noffset, NULL));
919
920 fit_image_print(fit, noffset, p);
921
922 return fit_image_extract(fit, noffset,
923 params->outfile);
924 }
925
926 count++;
927 }
928 }
929
930 return 0;
931}
932
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700933static int fit_check_params(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530934{
Simon Glass88e31cb2016-02-22 22:55:51 -0700935 if (params->auto_its)
936 return 0;
Heinrich Schuchardt915f5aa2019-12-11 13:51:38 +0100937 return ((params->dflag && params->fflag) ||
938 (params->fflag && params->lflag) ||
939 (params->lflag && params->dflag));
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530940}
941
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200942U_BOOT_IMAGE_TYPE(
943 fitimage,
944 "FIT Image support",
945 sizeof(image_header_t),
946 (void *)&header,
947 fit_check_params,
948 fit_verify_header,
949 fit_print_contents,
950 NULL,
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200951 fit_extract_contents,
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200952 fit_check_image_types,
953 fit_handle_file,
954 NULL /* FIT images use DTB header */
955);