blob: c37cae3b260c6b36738164a5c43a27e6cb604e10 [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>
Simon Glass88e31cb2016-02-22 22:55:51 -070020#include <stdarg.h>
21#include <version.h>
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053022#include <u-boot/crc.h>
23
24static image_header_t header;
25
Simon Glass802aa822014-06-02 22:04:53 -060026static int fit_add_file_data(struct image_tool_params *params, size_t size_inc,
27 const char *tmpfile)
28{
29 int tfd, destfd = 0;
30 void *dest_blob = NULL;
31 off_t destfd_size = 0;
32 struct stat sbuf;
33 void *ptr;
34 int ret = 0;
35
Luca Boccassibff733f2019-05-14 19:35:02 +010036 tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true,
37 false);
Simon Glass802aa822014-06-02 22:04:53 -060038 if (tfd < 0)
39 return -EIO;
40
41 if (params->keydest) {
42 struct stat dest_sbuf;
43
44 destfd = mmap_fdt(params->cmdname, params->keydest, size_inc,
Luca Boccassibff733f2019-05-14 19:35:02 +010045 &dest_blob, &dest_sbuf, false,
46 false);
Simon Glass802aa822014-06-02 22:04:53 -060047 if (destfd < 0) {
48 ret = -EIO;
49 goto err_keydest;
50 }
51 destfd_size = dest_sbuf.st_size;
52 }
53
54 /* for first image creation, add a timestamp at offset 0 i.e., root */
Vagrant Cascadianf8e066c2016-06-16 12:28:40 -070055 if (params->datafile) {
Alex Kiernanc4c7e0d2018-06-20 20:10:51 +000056 time_t time = imagetool_get_source_date(params->cmdname,
57 sbuf.st_mtime);
Vagrant Cascadianf8e066c2016-06-16 12:28:40 -070058 ret = fit_set_timestamp(ptr, 0, time);
59 }
Simon Glass802aa822014-06-02 22:04:53 -060060
61 if (!ret) {
Philippe Reynes3148e422019-12-18 18:25:41 +010062 ret = fit_cipher_data(params->keydir, dest_blob, ptr,
63 params->comment,
64 params->require_keys,
65 params->engine_id,
66 params->cmdname);
67 }
68
69 if (!ret) {
Simon Glass802aa822014-06-02 22:04:53 -060070 ret = fit_add_verification_data(params->keydir, dest_blob, ptr,
71 params->comment,
George McCollister23d14892017-01-06 13:14:17 -060072 params->require_keys,
Alex Kiernan697fcdc2018-06-20 20:10:52 +000073 params->engine_id,
74 params->cmdname);
Simon Glass802aa822014-06-02 22:04:53 -060075 }
76
77 if (dest_blob) {
78 munmap(dest_blob, destfd_size);
79 close(destfd);
80 }
81
82err_keydest:
83 munmap(ptr, sbuf.st_size);
84 close(tfd);
Simon Glass802aa822014-06-02 22:04:53 -060085 return ret;
86}
87
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053088/**
Simon Glass88e31cb2016-02-22 22:55:51 -070089 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
90 */
91static int fit_calc_size(struct image_tool_params *params)
92{
Simon Glassbd8bc5d2016-02-22 22:55:52 -070093 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -070094 int size, total_size;
95
96 size = imagetool_get_filesize(params, params->datafile);
97 if (size < 0)
98 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -070099 total_size = size;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100100
101 if (params->fit_ramdisk) {
102 size = imagetool_get_filesize(params, params->fit_ramdisk);
103 if (size < 0)
104 return -1;
105 total_size += size;
106 }
107
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700108 for (cont = params->content_head; cont; cont = cont->next) {
109 size = imagetool_get_filesize(params, cont->fname);
110 if (size < 0)
111 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -0700112
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700113 /* Add space for properties */
114 total_size += size + 300;
115 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700116
117 /* Add plenty of space for headers, properties, nodes, etc. */
118 total_size += 4096;
119
120 return total_size;
121}
122
123static int fdt_property_file(struct image_tool_params *params,
124 void *fdt, const char *name, const char *fname)
125{
126 struct stat sbuf;
127 void *ptr;
128 int ret;
129 int fd;
130
131 fd = open(fname, O_RDWR | O_BINARY);
132 if (fd < 0) {
133 fprintf(stderr, "%s: Can't open %s: %s\n",
134 params->cmdname, fname, strerror(errno));
135 return -1;
136 }
137
138 if (fstat(fd, &sbuf) < 0) {
139 fprintf(stderr, "%s: Can't stat %s: %s\n",
140 params->cmdname, fname, strerror(errno));
141 goto err;
142 }
143
144 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
145 if (ret)
Simon Glass9ab7f052016-03-16 07:45:42 -0600146 goto err;
Simon Glass88e31cb2016-02-22 22:55:51 -0700147 ret = read(fd, ptr, sbuf.st_size);
148 if (ret != sbuf.st_size) {
149 fprintf(stderr, "%s: Can't read %s: %s\n",
150 params->cmdname, fname, strerror(errno));
151 goto err;
152 }
Simon Glass9ab7f052016-03-16 07:45:42 -0600153 close(fd);
Simon Glass88e31cb2016-02-22 22:55:51 -0700154
155 return 0;
156err:
157 close(fd);
158 return -1;
159}
160
161static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
162{
163 char str[100];
164 va_list ptr;
165
166 va_start(ptr, fmt);
167 vsnprintf(str, sizeof(str), fmt, ptr);
168 va_end(ptr);
169 return fdt_property_string(fdt, name, str);
170}
171
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700172static void get_basename(char *str, int size, const char *fname)
173{
174 const char *p, *start, *end;
175 int len;
176
177 /*
178 * Use the base name as the 'name' field. So for example:
179 *
180 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
181 * becomes "sun7i-a20-bananapro"
182 */
183 p = strrchr(fname, '/');
184 start = p ? p + 1 : fname;
185 p = strrchr(fname, '.');
186 end = p ? p : fname + strlen(fname);
187 len = end - start;
188 if (len >= size)
189 len = size - 1;
190 memcpy(str, start, len);
191 str[len] = '\0';
192}
193
Simon Glass88e31cb2016-02-22 22:55:51 -0700194/**
195 * fit_write_images() - Write out a list of images to the FIT
196 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700197 * We always include the main image (params->datafile). If there are device
Andre Przywara9ef25e82017-12-04 02:05:12 +0000198 * tree files, we include an fdt- node for each of those too.
Simon Glass88e31cb2016-02-22 22:55:51 -0700199 */
200static int fit_write_images(struct image_tool_params *params, char *fdt)
201{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700202 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700203 const char *typename;
204 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700205 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700206 int ret;
207
208 fdt_begin_node(fdt, "images");
209
210 /* First the main image */
211 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000212 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glass88e31cb2016-02-22 22:55:51 -0700213 fdt_begin_node(fdt, str);
Michal Simeka4e678d2018-07-20 12:31:02 +0200214 fdt_property_string(fdt, FIT_DESC_PROP, params->imagename);
215 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
216 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassaeeea7f2016-06-30 10:52:12 -0600217 genimg_get_arch_short_name(params->arch));
Michal Simeka4e678d2018-07-20 12:31:02 +0200218 fdt_property_string(fdt, FIT_OS_PROP,
219 genimg_get_os_short_name(params->os));
220 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glass88e31cb2016-02-22 22:55:51 -0700221 genimg_get_comp_short_name(params->comp));
Michal Simeka4e678d2018-07-20 12:31:02 +0200222 fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr);
223 fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep);
Simon Glass88e31cb2016-02-22 22:55:51 -0700224
225 /*
226 * Put data last since it is large. SPL may only load the first part
227 * of the DT, so this way it can access all the above fields.
228 */
Michal Simeka4e678d2018-07-20 12:31:02 +0200229 ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile);
Simon Glass88e31cb2016-02-22 22:55:51 -0700230 if (ret)
231 return ret;
232 fdt_end_node(fdt);
233
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700234 /* Now the device tree files if available */
235 upto = 0;
236 for (cont = params->content_head; cont; cont = cont->next) {
237 if (cont->type != IH_TYPE_FLATDT)
238 continue;
Michal Sojka8a90ce82019-09-13 12:43:12 +0200239 typename = genimg_get_type_short_name(cont->type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000240 snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700241 fdt_begin_node(fdt, str);
242
243 get_basename(str, sizeof(str), cont->fname);
Michal Simeka4e678d2018-07-20 12:31:02 +0200244 fdt_property_string(fdt, FIT_DESC_PROP, str);
245 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
246 cont->fname);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700247 if (ret)
248 return ret;
Michal Simeka4e678d2018-07-20 12:31:02 +0200249 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
250 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700251 genimg_get_arch_short_name(params->arch));
Michal Simeka4e678d2018-07-20 12:31:02 +0200252 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700253 genimg_get_comp_short_name(IH_COMP_NONE));
254 fdt_end_node(fdt);
255 }
256
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100257 /* And a ramdisk file if available */
258 if (params->fit_ramdisk) {
Andre Przywara9ef25e82017-12-04 02:05:12 +0000259 fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100260
Michal Simeka4e678d2018-07-20 12:31:02 +0200261 fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP);
262 fdt_property_string(fdt, FIT_OS_PROP,
263 genimg_get_os_short_name(params->os));
Michal Sojka8a90ce82019-09-13 12:43:12 +0200264 fdt_property_string(fdt, FIT_ARCH_PROP,
265 genimg_get_arch_short_name(params->arch));
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100266
Michal Simeka4e678d2018-07-20 12:31:02 +0200267 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
268 params->fit_ramdisk);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100269 if (ret)
270 return ret;
271
272 fdt_end_node(fdt);
273 }
274
Simon Glass88e31cb2016-02-22 22:55:51 -0700275 fdt_end_node(fdt);
276
277 return 0;
278}
279
280/**
281 * fit_write_configs() - Write out a list of configurations to the FIT
282 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700283 * If there are device tree files, we include a configuration for each, which
284 * selects the main image (params->datafile) and its corresponding device
285 * tree file.
286 *
287 * Otherwise we just create a configuration with the main image in it.
Simon Glass88e31cb2016-02-22 22:55:51 -0700288 */
289static void fit_write_configs(struct image_tool_params *params, char *fdt)
290{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700291 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700292 const char *typename;
293 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700294 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700295
296 fdt_begin_node(fdt, "configurations");
Michal Simeka4e678d2018-07-20 12:31:02 +0200297 fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
Simon Glass88e31cb2016-02-22 22:55:51 -0700298
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700299 upto = 0;
300 for (cont = params->content_head; cont; cont = cont->next) {
301 if (cont->type != IH_TYPE_FLATDT)
302 continue;
303 typename = genimg_get_type_short_name(cont->type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000304 snprintf(str, sizeof(str), "conf-%d", ++upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700305 fdt_begin_node(fdt, str);
306
307 get_basename(str, sizeof(str), cont->fname);
Michal Simeka4e678d2018-07-20 12:31:02 +0200308 fdt_property_string(fdt, FIT_DESC_PROP, str);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700309
310 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000311 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700312 fdt_property_string(fdt, typename, str);
Abel Vesa7e0cb5e2019-03-12 08:34:32 +0000313 fdt_property_string(fdt, FIT_LOADABLE_PROP, str);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700314
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100315 if (params->fit_ramdisk)
316 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara9ef25e82017-12-04 02:05:12 +0000317 FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100318
Andre Przywara9ef25e82017-12-04 02:05:12 +0000319 snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700320 fdt_property_string(fdt, FIT_FDT_PROP, str);
321 fdt_end_node(fdt);
322 }
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100323
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700324 if (!upto) {
Andre Przywara9ef25e82017-12-04 02:05:12 +0000325 fdt_begin_node(fdt, "conf-1");
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700326 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000327 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700328 fdt_property_string(fdt, typename, str);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100329
330 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
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700334 fdt_end_node(fdt);
335 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700336
337 fdt_end_node(fdt);
338}
339
340static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
341{
342 int ret;
343
344 ret = fdt_create(fdt, size);
345 if (ret)
346 return ret;
347 fdt_finish_reservemap(fdt);
348 fdt_begin_node(fdt, "");
Michal Simeka4e678d2018-07-20 12:31:02 +0200349 fdt_property_strf(fdt, FIT_DESC_PROP,
Simon Glass88e31cb2016-02-22 22:55:51 -0700350 "%s image with one or more FDT blobs",
351 genimg_get_type_name(params->fit_image_type));
352 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
353 fdt_property_u32(fdt, "#address-cells", 1);
354 ret = fit_write_images(params, fdt);
355 if (ret)
356 return ret;
357 fit_write_configs(params, fdt);
358 fdt_end_node(fdt);
359 ret = fdt_finish(fdt);
360 if (ret)
361 return ret;
362
363 return fdt_totalsize(fdt);
364}
365
366static int fit_build(struct image_tool_params *params, const char *fname)
367{
368 char *buf;
369 int size;
370 int ret;
371 int fd;
372
373 size = fit_calc_size(params);
374 if (size < 0)
375 return -1;
376 buf = malloc(size);
377 if (!buf) {
378 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
379 params->cmdname, size);
380 return -1;
381 }
382 ret = fit_build_fdt(params, buf, size);
383 if (ret < 0) {
384 fprintf(stderr, "%s: Failed to build FIT image\n",
385 params->cmdname);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600386 goto err_buf;
Simon Glass88e31cb2016-02-22 22:55:51 -0700387 }
388 size = ret;
389 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
390 if (fd < 0) {
391 fprintf(stderr, "%s: Can't open %s: %s\n",
392 params->cmdname, fname, strerror(errno));
Tom Rinib8286652017-09-26 22:14:44 -0400393 goto err_buf;
Simon Glass88e31cb2016-02-22 22:55:51 -0700394 }
395 ret = write(fd, buf, size);
396 if (ret != size) {
397 fprintf(stderr, "%s: Can't write %s: %s\n",
398 params->cmdname, fname, strerror(errno));
Simon Glass88e31cb2016-02-22 22:55:51 -0700399 goto err;
400 }
401 close(fd);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600402 free(buf);
Simon Glass88e31cb2016-02-22 22:55:51 -0700403
404 return 0;
405err:
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600406 close(fd);
407err_buf:
Simon Glass88e31cb2016-02-22 22:55:51 -0700408 free(buf);
409 return -1;
410}
411
412/**
Simon Glassafd728c2016-02-22 22:55:53 -0700413 * fit_extract_data() - Move all data outside the FIT
414 *
415 * This takes a normal FIT file and removes all the 'data' properties from it.
416 * The data is placed in an area after the FIT so that it can be accessed
417 * using an offset into that area. The 'data' properties turn into
418 * 'data-offset' properties.
419 *
420 * This function cannot cope with FITs with 'data-offset' properties. All
421 * data must be in 'data' properties on entry.
422 */
423static int fit_extract_data(struct image_tool_params *params, const char *fname)
424{
Kever Yang8e238b52020-03-30 11:56:24 +0800425 void *buf = NULL;
Simon Glassafd728c2016-02-22 22:55:53 -0700426 int buf_ptr;
427 int fit_size, new_size;
428 int fd;
429 struct stat sbuf;
430 void *fdt;
431 int ret;
432 int images;
433 int node;
Kever Yang8e238b52020-03-30 11:56:24 +0800434 int image_number;
435 int align_size;
Simon Glassafd728c2016-02-22 22:55:53 -0700436
Kever Yang8e238b52020-03-30 11:56:24 +0800437 align_size = params->bl_len ? params->bl_len : 4;
Luca Boccassibff733f2019-05-14 19:35:02 +0100438 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false, false);
Simon Glassafd728c2016-02-22 22:55:53 -0700439 if (fd < 0)
440 return -EIO;
441 fit_size = fdt_totalsize(fdt);
442
Simon Glassafd728c2016-02-22 22:55:53 -0700443 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
444 if (images < 0) {
445 debug("%s: Cannot find /images node: %d\n", __func__, images);
446 ret = -EINVAL;
Simon Glass4e7505f2016-03-16 07:45:39 -0600447 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700448 }
Kever Yang8e238b52020-03-30 11:56:24 +0800449 image_number = fdtdec_get_child_count(fdt, images);
450
451 /*
452 * Allocate space to hold the image data we will extract,
453 * extral space allocate for image alignment to prevent overflow.
454 */
455 buf = malloc(fit_size + (align_size * image_number));
456 if (!buf) {
457 ret = -ENOMEM;
458 goto err_munmap;
459 }
460 buf_ptr = 0;
Simon Glassafd728c2016-02-22 22:55:53 -0700461
462 for (node = fdt_first_subnode(fdt, images);
463 node >= 0;
464 node = fdt_next_subnode(fdt, node)) {
465 const char *data;
466 int len;
467
Michal Simeka4e678d2018-07-20 12:31:02 +0200468 data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
Simon Glassafd728c2016-02-22 22:55:53 -0700469 if (!data)
470 continue;
471 memcpy(buf + buf_ptr, data, len);
472 debug("Extracting data size %x\n", len);
473
Michal Simeka4e678d2018-07-20 12:31:02 +0200474 ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
Simon Glassafd728c2016-02-22 22:55:53 -0700475 if (ret) {
476 ret = -EPERM;
Simon Glass4e7505f2016-03-16 07:45:39 -0600477 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700478 }
Teddy Reeda8457622016-06-09 19:38:02 -0700479 if (params->external_offset > 0) {
480 /* An external offset positions the data absolutely. */
Michal Simeka4e678d2018-07-20 12:31:02 +0200481 fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
Teddy Reeda8457622016-06-09 19:38:02 -0700482 params->external_offset + buf_ptr);
483 } else {
Michal Simeka4e678d2018-07-20 12:31:02 +0200484 fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
485 buf_ptr);
Teddy Reeda8457622016-06-09 19:38:02 -0700486 }
Michal Simeka4e678d2018-07-20 12:31:02 +0200487 fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
Kever Yang8e238b52020-03-30 11:56:24 +0800488 buf_ptr += ALIGN(len, align_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700489 }
490
491 /* Pack the FDT and place the data after it */
492 fdt_pack(fdt);
493
Kever Yang8e238b52020-03-30 11:56:24 +0800494 new_size = fdt_totalsize(fdt);
495 new_size = ALIGN(new_size, align_size);
496 fdt_set_totalsize(fdt, new_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700497 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
498 debug("External data size %x\n", buf_ptr);
Simon Glassafd728c2016-02-22 22:55:53 -0700499 munmap(fdt, sbuf.st_size);
500
501 if (ftruncate(fd, new_size)) {
502 debug("%s: Failed to truncate file: %s\n", __func__,
503 strerror(errno));
504 ret = -EIO;
505 goto err;
506 }
Teddy Reeda8457622016-06-09 19:38:02 -0700507
508 /* Check if an offset for the external data was set. */
509 if (params->external_offset > 0) {
510 if (params->external_offset < new_size) {
511 debug("External offset %x overlaps FIT length %x",
512 params->external_offset, new_size);
513 ret = -EINVAL;
514 goto err;
515 }
516 new_size = params->external_offset;
517 }
Simon Glassafd728c2016-02-22 22:55:53 -0700518 if (lseek(fd, new_size, SEEK_SET) < 0) {
519 debug("%s: Failed to seek to end of file: %s\n", __func__,
520 strerror(errno));
521 ret = -EIO;
522 goto err;
523 }
524 if (write(fd, buf, buf_ptr) != buf_ptr) {
525 debug("%s: Failed to write external data to file %s\n",
526 __func__, strerror(errno));
527 ret = -EIO;
528 goto err;
529 }
Tom Rinib8286652017-09-26 22:14:44 -0400530 free(buf);
Simon Glass4e7505f2016-03-16 07:45:39 -0600531 close(fd);
532 return 0;
Simon Glassafd728c2016-02-22 22:55:53 -0700533
Simon Glass4e7505f2016-03-16 07:45:39 -0600534err_munmap:
535 munmap(fdt, sbuf.st_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700536err:
Simon Glassf3a6e8f2016-03-16 07:45:40 -0600537 if (buf)
538 free(buf);
Simon Glassafd728c2016-02-22 22:55:53 -0700539 close(fd);
540 return ret;
541}
542
Simon Glassd11b7002016-02-22 22:55:54 -0700543static int fit_import_data(struct image_tool_params *params, const char *fname)
544{
545 void *fdt, *old_fdt;
546 int fit_size, new_size, size, data_base;
547 int fd;
548 struct stat sbuf;
549 int ret;
550 int images;
551 int node;
552
Luca Boccassibff733f2019-05-14 19:35:02 +0100553 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false, false);
Simon Glassd11b7002016-02-22 22:55:54 -0700554 if (fd < 0)
555 return -EIO;
556 fit_size = fdt_totalsize(old_fdt);
Kever Yang8c11bc12020-03-30 11:56:22 +0800557 data_base = ALIGN(fit_size, 4);
Simon Glassd11b7002016-02-22 22:55:54 -0700558
559 /* Allocate space to hold the new FIT */
560 size = sbuf.st_size + 16384;
561 fdt = malloc(size);
562 if (!fdt) {
563 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
564 __func__, size);
565 ret = -ENOMEM;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700566 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700567 }
568 ret = fdt_open_into(old_fdt, fdt, size);
569 if (ret) {
570 debug("%s: Failed to expand FIT: %s\n", __func__,
571 fdt_strerror(errno));
572 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700573 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700574 }
575
576 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
577 if (images < 0) {
578 debug("%s: Cannot find /images node: %d\n", __func__, images);
579 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700580 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700581 }
582
583 for (node = fdt_first_subnode(fdt, images);
584 node >= 0;
585 node = fdt_next_subnode(fdt, node)) {
586 int buf_ptr;
587 int len;
588
589 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
590 len = fdtdec_get_int(fdt, node, "data-size", -1);
591 if (buf_ptr == -1 || len == -1)
592 continue;
593 debug("Importing data size %x\n", len);
594
595 ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
596 len);
597 if (ret) {
598 debug("%s: Failed to write property: %s\n", __func__,
599 fdt_strerror(ret));
600 ret = -EINVAL;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700601 goto err_munmap;
Simon Glassd11b7002016-02-22 22:55:54 -0700602 }
603 }
604
Lihua Zhao4536ef82020-04-18 01:59:10 -0700605 munmap(old_fdt, sbuf.st_size);
606
Tom Rinia29829e2017-10-07 11:27:59 -0400607 /* Close the old fd so we can re-use it. */
Simon Glassd11b7002016-02-22 22:55:54 -0700608 close(fd);
609
610 /* Pack the FDT and place the data after it */
611 fdt_pack(fdt);
612
613 new_size = fdt_totalsize(fdt);
614 debug("Size expanded from %x to %x\n", fit_size, new_size);
615
616 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
617 if (fd < 0) {
618 fprintf(stderr, "%s: Can't open %s: %s\n",
619 params->cmdname, fname, strerror(errno));
Tom Rinia29829e2017-10-07 11:27:59 -0400620 ret = -EIO;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700621 goto err;
Simon Glassd11b7002016-02-22 22:55:54 -0700622 }
623 if (write(fd, fdt, new_size) != new_size) {
624 debug("%s: Failed to write external data to file %s\n",
625 __func__, strerror(errno));
626 ret = -EIO;
Lihua Zhao4536ef82020-04-18 01:59:10 -0700627 goto err;
Simon Glassd11b7002016-02-22 22:55:54 -0700628 }
Simon Glassd11b7002016-02-22 22:55:54 -0700629
Lihua Zhao4536ef82020-04-18 01:59:10 -0700630 free(fdt);
Tom Rinia29829e2017-10-07 11:27:59 -0400631 close(fd);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700632 return 0;
633
634err_munmap:
Tom Rinib8286652017-09-26 22:14:44 -0400635 munmap(old_fdt, sbuf.st_size);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700636err:
Simon Glass5014cc12016-03-16 07:45:38 -0600637 free(fdt);
Lihua Zhao4536ef82020-04-18 01:59:10 -0700638 close(fd);
Simon Glassd11b7002016-02-22 22:55:54 -0700639 return ret;
640}
641
Philippe Reynes3148e422019-12-18 18:25:41 +0100642static int copyfile(const char *src, const char *dst)
643{
644 int fd_src = -1, fd_dst = -1;
645 void *buf = NULL;
646 ssize_t size;
647 size_t count;
648 int ret = -1;
649
650 fd_src = open(src, O_RDONLY);
651 if (fd_src < 0) {
652 printf("Can't open file %s (%s)\n", src, strerror(errno));
653 goto out;
654 }
655
Thomas Hebb97caa072020-03-01 10:47:53 -0800656 fd_dst = open(dst, O_WRONLY | O_CREAT, 0666);
Philippe Reynes3148e422019-12-18 18:25:41 +0100657 if (fd_dst < 0) {
658 printf("Can't open file %s (%s)\n", dst, strerror(errno));
659 goto out;
660 }
661
662 buf = malloc(512);
663 if (!buf) {
664 printf("Can't allocate buffer to copy file\n");
665 goto out;
666 }
667
668 while (1) {
669 size = read(fd_src, buf, 512);
670 if (size < 0) {
671 printf("Can't read file %s\n", src);
672 goto out;
673 }
674 if (!size)
675 break;
676
677 count = size;
678 size = write(fd_dst, buf, count);
679 if (size < 0) {
680 printf("Can't write file %s\n", dst);
681 goto out;
682 }
683 }
684
685 ret = 0;
686
687 out:
688 if (fd_src >= 0)
689 close(fd_src);
690 if (fd_dst >= 0)
691 close(fd_dst);
692 if (buf)
693 free(buf);
694
695 return ret;
696}
697
Simon Glassafd728c2016-02-22 22:55:53 -0700698/**
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530699 * fit_handle_file - main FIT file processing function
700 *
701 * fit_handle_file() runs dtc to convert .its to .itb, includes
702 * binary data, updates timestamp property and calculates hashes.
703 *
704 * datafile - .its file
705 * imagefile - .itb file
706 *
707 * returns:
708 * only on success, otherwise calls exit (EXIT_FAILURE);
709 */
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700710static int fit_handle_file(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530711{
712 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
Philippe Reynes3148e422019-12-18 18:25:41 +0100713 char bakfile[MKIMAGE_MAX_TMPFILE_LEN + 4] = {0};
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530714 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
Simon Glass802aa822014-06-02 22:04:53 -0600715 size_t size_inc;
716 int ret;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530717
718 /* Flattened Image Tree (FIT) format handling */
719 debug ("FIT format handling\n");
720
721 /* call dtc to include binary properties into the tmp file */
722 if (strlen (params->imagefile) +
723 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
724 fprintf (stderr, "%s: Image file name (%s) too long, "
725 "can't create tmpfile",
726 params->imagefile, params->cmdname);
727 return (EXIT_FAILURE);
728 }
729 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
730
Simon Glassce8c3ca2013-06-13 15:10:05 -0700731 /* We either compile the source file, or use the existing FIT image */
Simon Glass88e31cb2016-02-22 22:55:51 -0700732 if (params->auto_its) {
733 if (fit_build(params, tmpfile)) {
734 fprintf(stderr, "%s: failed to build FIT\n",
735 params->cmdname);
736 return EXIT_FAILURE;
737 }
738 *cmd = '\0';
739 } else if (params->datafile) {
Stefan Theilff6c3b22018-03-08 09:00:13 +0100740 /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
741 snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
742 MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
Simon Glassce8c3ca2013-06-13 15:10:05 -0700743 debug("Trying to execute \"%s\"\n", cmd);
744 } else {
Mirza, Taimoor74a21dd2017-10-04 20:28:03 +0500745 snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
Simon Glassce8c3ca2013-06-13 15:10:05 -0700746 params->imagefile, tmpfile);
747 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100748
Simon Glass88e31cb2016-02-22 22:55:51 -0700749 if (*cmd && system(cmd) == -1) {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530750 fprintf (stderr, "%s: system(%s) failed: %s\n",
751 params->cmdname, cmd, strerror(errno));
Simon Glass4161c412013-05-08 08:05:57 +0000752 goto err_system;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530753 }
754
Simon Glassd11b7002016-02-22 22:55:54 -0700755 /* Move the data so it is internal to the FIT, if needed */
756 ret = fit_import_data(params, tmpfile);
757 if (ret)
758 goto err_system;
759
Simon Glass802aa822014-06-02 22:04:53 -0600760 /*
Philippe Reynes3148e422019-12-18 18:25:41 +0100761 * Copy the tmpfile to bakfile, then in the following loop
762 * we copy bakfile to tmpfile. So we always start from the
763 * beginning.
764 */
765 sprintf(bakfile, "%s%s", tmpfile, ".bak");
766 rename(tmpfile, bakfile);
767
768 /*
Simon Glass802aa822014-06-02 22:04:53 -0600769 * Set hashes for images in the blob. Unfortunately we may need more
770 * space in either FDT, so keep trying until we succeed.
771 *
772 * Note: this is pretty inefficient for signing, since we must
773 * calculate the signature every time. It would be better to calculate
774 * all the data and then store it in a separate step. However, this
775 * would be considerably more complex to implement. Generally a few
776 * steps of this loop is enough to sign with several keys.
777 */
778 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
Philippe Reynes3148e422019-12-18 18:25:41 +0100779 if (copyfile(bakfile, tmpfile) < 0) {
780 printf("Can't copy %s to %s\n", bakfile, tmpfile);
781 ret = -EIO;
782 break;
783 }
Simon Glass802aa822014-06-02 22:04:53 -0600784 ret = fit_add_file_data(params, size_inc, tmpfile);
785 if (!ret || ret != -ENOSPC)
786 break;
Simon Glassb4d8b092013-06-13 15:10:04 -0700787 }
788
Simon Glass802aa822014-06-02 22:04:53 -0600789 if (ret) {
Simon Glass061752e2016-07-03 09:40:43 -0600790 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
791 params->cmdname, ret);
Simon Glass802aa822014-06-02 22:04:53 -0600792 goto err_system;
Simon Glassb4d8b092013-06-13 15:10:04 -0700793 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530794
Simon Glassafd728c2016-02-22 22:55:53 -0700795 /* Move the data so it is external to the FIT, if requested */
796 if (params->external_data) {
797 ret = fit_extract_data(params, tmpfile);
798 if (ret)
799 goto err_system;
800 }
801
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530802 if (rename (tmpfile, params->imagefile) == -1) {
803 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
804 params->cmdname, tmpfile, params->imagefile,
805 strerror (errno));
806 unlink (tmpfile);
Philippe Reynes3148e422019-12-18 18:25:41 +0100807 unlink(bakfile);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530808 unlink (params->imagefile);
Simon Glass802aa822014-06-02 22:04:53 -0600809 return EXIT_FAILURE;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530810 }
Philippe Reynes3148e422019-12-18 18:25:41 +0100811 unlink(bakfile);
Simon Glass802aa822014-06-02 22:04:53 -0600812 return EXIT_SUCCESS;
Simon Glass4161c412013-05-08 08:05:57 +0000813
Simon Glass4161c412013-05-08 08:05:57 +0000814err_system:
815 unlink(tmpfile);
Philippe Reynes3148e422019-12-18 18:25:41 +0100816 unlink(bakfile);
Simon Glass4161c412013-05-08 08:05:57 +0000817 return -1;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530818}
819
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200820/**
821 * fit_image_extract - extract a FIT component image
822 * @fit: pointer to the FIT format image header
823 * @image_noffset: offset of the component image node
824 * @file_name: name of the file to store the FIT sub-image
825 *
826 * returns:
827 * zero in case of success or a negative value if fail.
828 */
829static int fit_image_extract(
830 const void *fit,
831 int image_noffset,
832 const char *file_name)
833{
834 const void *file_data;
835 size_t file_size = 0;
Andrew F. Davis166d5e92019-09-17 17:09:34 -0400836 int ret;
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200837
Andrew F. Davis166d5e92019-09-17 17:09:34 -0400838 /* get the data address and size of component at offset "image_noffset" */
839 ret = fit_image_get_data_and_size(fit, image_noffset, &file_data, &file_size);
840 if (ret) {
841 fprintf(stderr, "Could not get component information\n");
842 return ret;
843 }
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200844
845 /* save the "file_data" into the file specified by "file_name" */
846 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
847}
848
849/**
850 * fit_extract_contents - retrieve a sub-image component from the FIT image
851 * @ptr: pointer to the FIT format image header
852 * @params: command line parameters
853 *
854 * returns:
855 * zero in case of success or a negative value if fail.
856 */
857static int fit_extract_contents(void *ptr, struct image_tool_params *params)
858{
859 int images_noffset;
860 int noffset;
861 int ndepth;
862 const void *fit = ptr;
863 int count = 0;
864 const char *p;
865
866 /* Indent string is defined in header image.h */
867 p = IMAGE_INDENT_STRING;
868
869 if (!fit_check_format(fit)) {
870 printf("Bad FIT image format\n");
871 return -1;
872 }
873
874 /* Find images parent node offset */
875 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
876 if (images_noffset < 0) {
877 printf("Can't find images parent node '%s' (%s)\n",
878 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
879 return -1;
880 }
881
882 /* Avoid any overrun */
883 count = fit_get_subimage_count(fit, images_noffset);
884 if ((params->pflag < 0) || (count <= params->pflag)) {
885 printf("No such component at '%d'\n", params->pflag);
886 return -1;
887 }
888
889 /* Process its subnodes, extract the desired component from image */
890 for (ndepth = 0, count = 0,
891 noffset = fdt_next_node(fit, images_noffset, &ndepth);
892 (noffset >= 0) && (ndepth > 0);
893 noffset = fdt_next_node(fit, noffset, &ndepth)) {
894 if (ndepth == 1) {
895 /*
896 * Direct child node of the images parent node,
897 * i.e. component image node.
898 */
899 if (params->pflag == count) {
900 printf("Extracted:\n%s Image %u (%s)\n", p,
901 count, fit_get_name(fit, noffset, NULL));
902
903 fit_image_print(fit, noffset, p);
904
905 return fit_image_extract(fit, noffset,
906 params->outfile);
907 }
908
909 count++;
910 }
911 }
912
913 return 0;
914}
915
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700916static int fit_check_params(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530917{
Simon Glass88e31cb2016-02-22 22:55:51 -0700918 if (params->auto_its)
919 return 0;
Heinrich Schuchardt915f5aa2019-12-11 13:51:38 +0100920 return ((params->dflag && params->fflag) ||
921 (params->fflag && params->lflag) ||
922 (params->lflag && params->dflag));
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530923}
924
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200925U_BOOT_IMAGE_TYPE(
926 fitimage,
927 "FIT Image support",
928 sizeof(image_header_t),
929 (void *)&header,
930 fit_check_params,
931 fit_verify_header,
932 fit_print_contents,
933 NULL,
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200934 fit_extract_contents,
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200935 fit_check_image_types,
936 fit_handle_file,
937 NULL /* FIT images use DTB header */
938);