blob: 3b867e06564e34e2e80f4997b3ce84967fd35f76 [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
36 tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true);
37 if (tfd < 0)
38 return -EIO;
39
40 if (params->keydest) {
41 struct stat dest_sbuf;
42
43 destfd = mmap_fdt(params->cmdname, params->keydest, size_inc,
44 &dest_blob, &dest_sbuf, false);
45 if (destfd < 0) {
46 ret = -EIO;
47 goto err_keydest;
48 }
49 destfd_size = dest_sbuf.st_size;
50 }
51
52 /* for first image creation, add a timestamp at offset 0 i.e., root */
Vagrant Cascadianf8e066c2016-06-16 12:28:40 -070053 if (params->datafile) {
Alex Kiernanc4c7e0d2018-06-20 20:10:51 +000054 time_t time = imagetool_get_source_date(params->cmdname,
55 sbuf.st_mtime);
Vagrant Cascadianf8e066c2016-06-16 12:28:40 -070056 ret = fit_set_timestamp(ptr, 0, time);
57 }
Simon Glass802aa822014-06-02 22:04:53 -060058
59 if (!ret) {
60 ret = fit_add_verification_data(params->keydir, dest_blob, ptr,
61 params->comment,
George McCollister23d14892017-01-06 13:14:17 -060062 params->require_keys,
Alex Kiernan697fcdc2018-06-20 20:10:52 +000063 params->engine_id,
64 params->cmdname);
Simon Glass802aa822014-06-02 22:04:53 -060065 }
66
67 if (dest_blob) {
68 munmap(dest_blob, destfd_size);
69 close(destfd);
70 }
71
72err_keydest:
73 munmap(ptr, sbuf.st_size);
74 close(tfd);
75
76 return ret;
77}
78
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053079/**
Simon Glass88e31cb2016-02-22 22:55:51 -070080 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
81 */
82static int fit_calc_size(struct image_tool_params *params)
83{
Simon Glassbd8bc5d2016-02-22 22:55:52 -070084 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -070085 int size, total_size;
86
87 size = imagetool_get_filesize(params, params->datafile);
88 if (size < 0)
89 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -070090 total_size = size;
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +010091
92 if (params->fit_ramdisk) {
93 size = imagetool_get_filesize(params, params->fit_ramdisk);
94 if (size < 0)
95 return -1;
96 total_size += size;
97 }
98
Simon Glassbd8bc5d2016-02-22 22:55:52 -070099 for (cont = params->content_head; cont; cont = cont->next) {
100 size = imagetool_get_filesize(params, cont->fname);
101 if (size < 0)
102 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -0700103
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700104 /* Add space for properties */
105 total_size += size + 300;
106 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700107
108 /* Add plenty of space for headers, properties, nodes, etc. */
109 total_size += 4096;
110
111 return total_size;
112}
113
114static int fdt_property_file(struct image_tool_params *params,
115 void *fdt, const char *name, const char *fname)
116{
117 struct stat sbuf;
118 void *ptr;
119 int ret;
120 int fd;
121
122 fd = open(fname, O_RDWR | O_BINARY);
123 if (fd < 0) {
124 fprintf(stderr, "%s: Can't open %s: %s\n",
125 params->cmdname, fname, strerror(errno));
126 return -1;
127 }
128
129 if (fstat(fd, &sbuf) < 0) {
130 fprintf(stderr, "%s: Can't stat %s: %s\n",
131 params->cmdname, fname, strerror(errno));
132 goto err;
133 }
134
135 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
136 if (ret)
Simon Glass9ab7f052016-03-16 07:45:42 -0600137 goto err;
Simon Glass88e31cb2016-02-22 22:55:51 -0700138 ret = read(fd, ptr, sbuf.st_size);
139 if (ret != sbuf.st_size) {
140 fprintf(stderr, "%s: Can't read %s: %s\n",
141 params->cmdname, fname, strerror(errno));
142 goto err;
143 }
Simon Glass9ab7f052016-03-16 07:45:42 -0600144 close(fd);
Simon Glass88e31cb2016-02-22 22:55:51 -0700145
146 return 0;
147err:
148 close(fd);
149 return -1;
150}
151
152static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
153{
154 char str[100];
155 va_list ptr;
156
157 va_start(ptr, fmt);
158 vsnprintf(str, sizeof(str), fmt, ptr);
159 va_end(ptr);
160 return fdt_property_string(fdt, name, str);
161}
162
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700163static void get_basename(char *str, int size, const char *fname)
164{
165 const char *p, *start, *end;
166 int len;
167
168 /*
169 * Use the base name as the 'name' field. So for example:
170 *
171 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
172 * becomes "sun7i-a20-bananapro"
173 */
174 p = strrchr(fname, '/');
175 start = p ? p + 1 : fname;
176 p = strrchr(fname, '.');
177 end = p ? p : fname + strlen(fname);
178 len = end - start;
179 if (len >= size)
180 len = size - 1;
181 memcpy(str, start, len);
182 str[len] = '\0';
183}
184
Simon Glass88e31cb2016-02-22 22:55:51 -0700185/**
186 * fit_write_images() - Write out a list of images to the FIT
187 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700188 * We always include the main image (params->datafile). If there are device
Andre Przywara9ef25e82017-12-04 02:05:12 +0000189 * tree files, we include an fdt- node for each of those too.
Simon Glass88e31cb2016-02-22 22:55:51 -0700190 */
191static int fit_write_images(struct image_tool_params *params, char *fdt)
192{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700193 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700194 const char *typename;
195 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700196 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700197 int ret;
198
199 fdt_begin_node(fdt, "images");
200
201 /* First the main image */
202 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000203 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glass88e31cb2016-02-22 22:55:51 -0700204 fdt_begin_node(fdt, str);
Michal Simeka4e678d2018-07-20 12:31:02 +0200205 fdt_property_string(fdt, FIT_DESC_PROP, params->imagename);
206 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
207 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassaeeea7f2016-06-30 10:52:12 -0600208 genimg_get_arch_short_name(params->arch));
Michal Simeka4e678d2018-07-20 12:31:02 +0200209 fdt_property_string(fdt, FIT_OS_PROP,
210 genimg_get_os_short_name(params->os));
211 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glass88e31cb2016-02-22 22:55:51 -0700212 genimg_get_comp_short_name(params->comp));
Michal Simeka4e678d2018-07-20 12:31:02 +0200213 fdt_property_u32(fdt, FIT_LOAD_PROP, params->addr);
214 fdt_property_u32(fdt, FIT_ENTRY_PROP, params->ep);
Simon Glass88e31cb2016-02-22 22:55:51 -0700215
216 /*
217 * Put data last since it is large. SPL may only load the first part
218 * of the DT, so this way it can access all the above fields.
219 */
Michal Simeka4e678d2018-07-20 12:31:02 +0200220 ret = fdt_property_file(params, fdt, FIT_DATA_PROP, params->datafile);
Simon Glass88e31cb2016-02-22 22:55:51 -0700221 if (ret)
222 return ret;
223 fdt_end_node(fdt);
224
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700225 /* Now the device tree files if available */
226 upto = 0;
227 for (cont = params->content_head; cont; cont = cont->next) {
228 if (cont->type != IH_TYPE_FLATDT)
229 continue;
Andre Przywara9ef25e82017-12-04 02:05:12 +0000230 snprintf(str, sizeof(str), "%s-%d", FIT_FDT_PROP, ++upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700231 fdt_begin_node(fdt, str);
232
233 get_basename(str, sizeof(str), cont->fname);
Michal Simeka4e678d2018-07-20 12:31:02 +0200234 fdt_property_string(fdt, FIT_DESC_PROP, str);
235 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
236 cont->fname);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700237 if (ret)
238 return ret;
Michal Simeka4e678d2018-07-20 12:31:02 +0200239 fdt_property_string(fdt, FIT_TYPE_PROP, typename);
240 fdt_property_string(fdt, FIT_ARCH_PROP,
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700241 genimg_get_arch_short_name(params->arch));
Michal Simeka4e678d2018-07-20 12:31:02 +0200242 fdt_property_string(fdt, FIT_COMP_PROP,
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700243 genimg_get_comp_short_name(IH_COMP_NONE));
244 fdt_end_node(fdt);
245 }
246
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100247 /* And a ramdisk file if available */
248 if (params->fit_ramdisk) {
Andre Przywara9ef25e82017-12-04 02:05:12 +0000249 fdt_begin_node(fdt, FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100250
Michal Simeka4e678d2018-07-20 12:31:02 +0200251 fdt_property_string(fdt, FIT_TYPE_PROP, FIT_RAMDISK_PROP);
252 fdt_property_string(fdt, FIT_OS_PROP,
253 genimg_get_os_short_name(params->os));
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100254
Michal Simeka4e678d2018-07-20 12:31:02 +0200255 ret = fdt_property_file(params, fdt, FIT_DATA_PROP,
256 params->fit_ramdisk);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100257 if (ret)
258 return ret;
259
260 fdt_end_node(fdt);
261 }
262
Simon Glass88e31cb2016-02-22 22:55:51 -0700263 fdt_end_node(fdt);
264
265 return 0;
266}
267
268/**
269 * fit_write_configs() - Write out a list of configurations to the FIT
270 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700271 * If there are device tree files, we include a configuration for each, which
272 * selects the main image (params->datafile) and its corresponding device
273 * tree file.
274 *
275 * Otherwise we just create a configuration with the main image in it.
Simon Glass88e31cb2016-02-22 22:55:51 -0700276 */
277static void fit_write_configs(struct image_tool_params *params, char *fdt)
278{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700279 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700280 const char *typename;
281 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700282 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700283
284 fdt_begin_node(fdt, "configurations");
Michal Simeka4e678d2018-07-20 12:31:02 +0200285 fdt_property_string(fdt, FIT_DEFAULT_PROP, "conf-1");
Simon Glass88e31cb2016-02-22 22:55:51 -0700286
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700287 upto = 0;
288 for (cont = params->content_head; cont; cont = cont->next) {
289 if (cont->type != IH_TYPE_FLATDT)
290 continue;
291 typename = genimg_get_type_short_name(cont->type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000292 snprintf(str, sizeof(str), "conf-%d", ++upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700293 fdt_begin_node(fdt, str);
294
295 get_basename(str, sizeof(str), cont->fname);
Michal Simeka4e678d2018-07-20 12:31:02 +0200296 fdt_property_string(fdt, FIT_DESC_PROP, str);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700297
298 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000299 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700300 fdt_property_string(fdt, typename, str);
Abel Vesa7e0cb5e2019-03-12 08:34:32 +0000301 fdt_property_string(fdt, FIT_LOADABLE_PROP, str);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700302
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100303 if (params->fit_ramdisk)
304 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara9ef25e82017-12-04 02:05:12 +0000305 FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100306
Andre Przywara9ef25e82017-12-04 02:05:12 +0000307 snprintf(str, sizeof(str), FIT_FDT_PROP "-%d", upto);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700308 fdt_property_string(fdt, FIT_FDT_PROP, str);
309 fdt_end_node(fdt);
310 }
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100311
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700312 if (!upto) {
Andre Przywara9ef25e82017-12-04 02:05:12 +0000313 fdt_begin_node(fdt, "conf-1");
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700314 typename = genimg_get_type_short_name(params->fit_image_type);
Andre Przywara9ef25e82017-12-04 02:05:12 +0000315 snprintf(str, sizeof(str), "%s-1", typename);
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700316 fdt_property_string(fdt, typename, str);
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100317
318 if (params->fit_ramdisk)
319 fdt_property_string(fdt, FIT_RAMDISK_PROP,
Andre Przywara9ef25e82017-12-04 02:05:12 +0000320 FIT_RAMDISK_PROP "-1");
Tomeu Vizoso8d83ed22016-11-04 14:22:15 +0100321
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700322 fdt_end_node(fdt);
323 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700324
325 fdt_end_node(fdt);
326}
327
328static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
329{
330 int ret;
331
332 ret = fdt_create(fdt, size);
333 if (ret)
334 return ret;
335 fdt_finish_reservemap(fdt);
336 fdt_begin_node(fdt, "");
Michal Simeka4e678d2018-07-20 12:31:02 +0200337 fdt_property_strf(fdt, FIT_DESC_PROP,
Simon Glass88e31cb2016-02-22 22:55:51 -0700338 "%s image with one or more FDT blobs",
339 genimg_get_type_name(params->fit_image_type));
340 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
341 fdt_property_u32(fdt, "#address-cells", 1);
342 ret = fit_write_images(params, fdt);
343 if (ret)
344 return ret;
345 fit_write_configs(params, fdt);
346 fdt_end_node(fdt);
347 ret = fdt_finish(fdt);
348 if (ret)
349 return ret;
350
351 return fdt_totalsize(fdt);
352}
353
354static int fit_build(struct image_tool_params *params, const char *fname)
355{
356 char *buf;
357 int size;
358 int ret;
359 int fd;
360
361 size = fit_calc_size(params);
362 if (size < 0)
363 return -1;
364 buf = malloc(size);
365 if (!buf) {
366 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
367 params->cmdname, size);
368 return -1;
369 }
370 ret = fit_build_fdt(params, buf, size);
371 if (ret < 0) {
372 fprintf(stderr, "%s: Failed to build FIT image\n",
373 params->cmdname);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600374 goto err_buf;
Simon Glass88e31cb2016-02-22 22:55:51 -0700375 }
376 size = ret;
377 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
378 if (fd < 0) {
379 fprintf(stderr, "%s: Can't open %s: %s\n",
380 params->cmdname, fname, strerror(errno));
Tom Rinib8286652017-09-26 22:14:44 -0400381 goto err_buf;
Simon Glass88e31cb2016-02-22 22:55:51 -0700382 }
383 ret = write(fd, buf, size);
384 if (ret != size) {
385 fprintf(stderr, "%s: Can't write %s: %s\n",
386 params->cmdname, fname, strerror(errno));
Simon Glass88e31cb2016-02-22 22:55:51 -0700387 goto err;
388 }
389 close(fd);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600390 free(buf);
Simon Glass88e31cb2016-02-22 22:55:51 -0700391
392 return 0;
393err:
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600394 close(fd);
395err_buf:
Simon Glass88e31cb2016-02-22 22:55:51 -0700396 free(buf);
397 return -1;
398}
399
400/**
Simon Glassafd728c2016-02-22 22:55:53 -0700401 * fit_extract_data() - Move all data outside the FIT
402 *
403 * This takes a normal FIT file and removes all the 'data' properties from it.
404 * The data is placed in an area after the FIT so that it can be accessed
405 * using an offset into that area. The 'data' properties turn into
406 * 'data-offset' properties.
407 *
408 * This function cannot cope with FITs with 'data-offset' properties. All
409 * data must be in 'data' properties on entry.
410 */
411static int fit_extract_data(struct image_tool_params *params, const char *fname)
412{
413 void *buf;
414 int buf_ptr;
415 int fit_size, new_size;
416 int fd;
417 struct stat sbuf;
418 void *fdt;
419 int ret;
420 int images;
421 int node;
422
423 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false);
424 if (fd < 0)
425 return -EIO;
426 fit_size = fdt_totalsize(fdt);
427
428 /* Allocate space to hold the image data we will extract */
429 buf = malloc(fit_size);
430 if (!buf) {
431 ret = -ENOMEM;
Simon Glass4e7505f2016-03-16 07:45:39 -0600432 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700433 }
434 buf_ptr = 0;
435
436 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
437 if (images < 0) {
438 debug("%s: Cannot find /images node: %d\n", __func__, images);
439 ret = -EINVAL;
Simon Glass4e7505f2016-03-16 07:45:39 -0600440 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700441 }
442
443 for (node = fdt_first_subnode(fdt, images);
444 node >= 0;
445 node = fdt_next_subnode(fdt, node)) {
446 const char *data;
447 int len;
448
Michal Simeka4e678d2018-07-20 12:31:02 +0200449 data = fdt_getprop(fdt, node, FIT_DATA_PROP, &len);
Simon Glassafd728c2016-02-22 22:55:53 -0700450 if (!data)
451 continue;
452 memcpy(buf + buf_ptr, data, len);
453 debug("Extracting data size %x\n", len);
454
Michal Simeka4e678d2018-07-20 12:31:02 +0200455 ret = fdt_delprop(fdt, node, FIT_DATA_PROP);
Simon Glassafd728c2016-02-22 22:55:53 -0700456 if (ret) {
457 ret = -EPERM;
Simon Glass4e7505f2016-03-16 07:45:39 -0600458 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700459 }
Teddy Reeda8457622016-06-09 19:38:02 -0700460 if (params->external_offset > 0) {
461 /* An external offset positions the data absolutely. */
Michal Simeka4e678d2018-07-20 12:31:02 +0200462 fdt_setprop_u32(fdt, node, FIT_DATA_POSITION_PROP,
Teddy Reeda8457622016-06-09 19:38:02 -0700463 params->external_offset + buf_ptr);
464 } else {
Michal Simeka4e678d2018-07-20 12:31:02 +0200465 fdt_setprop_u32(fdt, node, FIT_DATA_OFFSET_PROP,
466 buf_ptr);
Teddy Reeda8457622016-06-09 19:38:02 -0700467 }
Michal Simeka4e678d2018-07-20 12:31:02 +0200468 fdt_setprop_u32(fdt, node, FIT_DATA_SIZE_PROP, len);
Simon Glassafd728c2016-02-22 22:55:53 -0700469
470 buf_ptr += (len + 3) & ~3;
471 }
472
473 /* Pack the FDT and place the data after it */
474 fdt_pack(fdt);
475
476 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
477 debug("External data size %x\n", buf_ptr);
478 new_size = fdt_totalsize(fdt);
479 new_size = (new_size + 3) & ~3;
480 munmap(fdt, sbuf.st_size);
481
482 if (ftruncate(fd, new_size)) {
483 debug("%s: Failed to truncate file: %s\n", __func__,
484 strerror(errno));
485 ret = -EIO;
486 goto err;
487 }
Teddy Reeda8457622016-06-09 19:38:02 -0700488
489 /* Check if an offset for the external data was set. */
490 if (params->external_offset > 0) {
491 if (params->external_offset < new_size) {
492 debug("External offset %x overlaps FIT length %x",
493 params->external_offset, new_size);
494 ret = -EINVAL;
495 goto err;
496 }
497 new_size = params->external_offset;
498 }
Simon Glassafd728c2016-02-22 22:55:53 -0700499 if (lseek(fd, new_size, SEEK_SET) < 0) {
500 debug("%s: Failed to seek to end of file: %s\n", __func__,
501 strerror(errno));
502 ret = -EIO;
503 goto err;
504 }
505 if (write(fd, buf, buf_ptr) != buf_ptr) {
506 debug("%s: Failed to write external data to file %s\n",
507 __func__, strerror(errno));
508 ret = -EIO;
509 goto err;
510 }
Tom Rinib8286652017-09-26 22:14:44 -0400511 free(buf);
Simon Glass4e7505f2016-03-16 07:45:39 -0600512 close(fd);
513 return 0;
Simon Glassafd728c2016-02-22 22:55:53 -0700514
Simon Glass4e7505f2016-03-16 07:45:39 -0600515err_munmap:
516 munmap(fdt, sbuf.st_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700517err:
Simon Glassf3a6e8f2016-03-16 07:45:40 -0600518 if (buf)
519 free(buf);
Simon Glassafd728c2016-02-22 22:55:53 -0700520 close(fd);
521 return ret;
522}
523
Simon Glassd11b7002016-02-22 22:55:54 -0700524static int fit_import_data(struct image_tool_params *params, const char *fname)
525{
526 void *fdt, *old_fdt;
527 int fit_size, new_size, size, data_base;
528 int fd;
529 struct stat sbuf;
530 int ret;
531 int images;
532 int node;
533
534 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false);
535 if (fd < 0)
536 return -EIO;
537 fit_size = fdt_totalsize(old_fdt);
538 data_base = (fit_size + 3) & ~3;
539
540 /* Allocate space to hold the new FIT */
541 size = sbuf.st_size + 16384;
542 fdt = malloc(size);
543 if (!fdt) {
544 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
545 __func__, size);
546 ret = -ENOMEM;
Tom Rinia29829e2017-10-07 11:27:59 -0400547 goto err_has_fd;
Simon Glassd11b7002016-02-22 22:55:54 -0700548 }
549 ret = fdt_open_into(old_fdt, fdt, size);
550 if (ret) {
551 debug("%s: Failed to expand FIT: %s\n", __func__,
552 fdt_strerror(errno));
553 ret = -EINVAL;
Tom Rinia29829e2017-10-07 11:27:59 -0400554 goto err_has_fd;
Simon Glassd11b7002016-02-22 22:55:54 -0700555 }
556
557 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
558 if (images < 0) {
559 debug("%s: Cannot find /images node: %d\n", __func__, images);
560 ret = -EINVAL;
Tom Rinia29829e2017-10-07 11:27:59 -0400561 goto err_has_fd;
Simon Glassd11b7002016-02-22 22:55:54 -0700562 }
563
564 for (node = fdt_first_subnode(fdt, images);
565 node >= 0;
566 node = fdt_next_subnode(fdt, node)) {
567 int buf_ptr;
568 int len;
569
570 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
571 len = fdtdec_get_int(fdt, node, "data-size", -1);
572 if (buf_ptr == -1 || len == -1)
573 continue;
574 debug("Importing data size %x\n", len);
575
576 ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
577 len);
578 if (ret) {
579 debug("%s: Failed to write property: %s\n", __func__,
580 fdt_strerror(ret));
581 ret = -EINVAL;
Tom Rinia29829e2017-10-07 11:27:59 -0400582 goto err_has_fd;
Simon Glassd11b7002016-02-22 22:55:54 -0700583 }
584 }
585
Tom Rinia29829e2017-10-07 11:27:59 -0400586 /* Close the old fd so we can re-use it. */
Simon Glassd11b7002016-02-22 22:55:54 -0700587 close(fd);
588
589 /* Pack the FDT and place the data after it */
590 fdt_pack(fdt);
591
592 new_size = fdt_totalsize(fdt);
593 debug("Size expanded from %x to %x\n", fit_size, new_size);
594
595 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
596 if (fd < 0) {
597 fprintf(stderr, "%s: Can't open %s: %s\n",
598 params->cmdname, fname, strerror(errno));
Tom Rinia29829e2017-10-07 11:27:59 -0400599 ret = -EIO;
600 goto err_no_fd;
Simon Glassd11b7002016-02-22 22:55:54 -0700601 }
602 if (write(fd, fdt, new_size) != new_size) {
603 debug("%s: Failed to write external data to file %s\n",
604 __func__, strerror(errno));
605 ret = -EIO;
Tom Rinia29829e2017-10-07 11:27:59 -0400606 goto err_has_fd;
Simon Glassd11b7002016-02-22 22:55:54 -0700607 }
Simon Glassd11b7002016-02-22 22:55:54 -0700608
609 ret = 0;
610
Tom Rinia29829e2017-10-07 11:27:59 -0400611err_has_fd:
612 close(fd);
613err_no_fd:
Tom Rinib8286652017-09-26 22:14:44 -0400614 munmap(old_fdt, sbuf.st_size);
Simon Glass5014cc12016-03-16 07:45:38 -0600615 free(fdt);
Simon Glassd11b7002016-02-22 22:55:54 -0700616 return ret;
617}
618
Simon Glassafd728c2016-02-22 22:55:53 -0700619/**
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530620 * fit_handle_file - main FIT file processing function
621 *
622 * fit_handle_file() runs dtc to convert .its to .itb, includes
623 * binary data, updates timestamp property and calculates hashes.
624 *
625 * datafile - .its file
626 * imagefile - .itb file
627 *
628 * returns:
629 * only on success, otherwise calls exit (EXIT_FAILURE);
630 */
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700631static int fit_handle_file(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530632{
633 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
634 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
Simon Glass802aa822014-06-02 22:04:53 -0600635 size_t size_inc;
636 int ret;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530637
638 /* Flattened Image Tree (FIT) format handling */
639 debug ("FIT format handling\n");
640
641 /* call dtc to include binary properties into the tmp file */
642 if (strlen (params->imagefile) +
643 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
644 fprintf (stderr, "%s: Image file name (%s) too long, "
645 "can't create tmpfile",
646 params->imagefile, params->cmdname);
647 return (EXIT_FAILURE);
648 }
649 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
650
Simon Glassce8c3ca2013-06-13 15:10:05 -0700651 /* We either compile the source file, or use the existing FIT image */
Simon Glass88e31cb2016-02-22 22:55:51 -0700652 if (params->auto_its) {
653 if (fit_build(params, tmpfile)) {
654 fprintf(stderr, "%s: failed to build FIT\n",
655 params->cmdname);
656 return EXIT_FAILURE;
657 }
658 *cmd = '\0';
659 } else if (params->datafile) {
Stefan Theilff6c3b22018-03-08 09:00:13 +0100660 /* dtc -I dts -O dtb -p 500 -o tmpfile datafile */
661 snprintf(cmd, sizeof(cmd), "%s %s -o \"%s\" \"%s\"",
662 MKIMAGE_DTC, params->dtc, tmpfile, params->datafile);
Simon Glassce8c3ca2013-06-13 15:10:05 -0700663 debug("Trying to execute \"%s\"\n", cmd);
664 } else {
Mirza, Taimoor74a21dd2017-10-04 20:28:03 +0500665 snprintf(cmd, sizeof(cmd), "cp \"%s\" \"%s\"",
Simon Glassce8c3ca2013-06-13 15:10:05 -0700666 params->imagefile, tmpfile);
667 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700668 if (*cmd && system(cmd) == -1) {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530669 fprintf (stderr, "%s: system(%s) failed: %s\n",
670 params->cmdname, cmd, strerror(errno));
Simon Glass4161c412013-05-08 08:05:57 +0000671 goto err_system;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530672 }
673
Simon Glassd11b7002016-02-22 22:55:54 -0700674 /* Move the data so it is internal to the FIT, if needed */
675 ret = fit_import_data(params, tmpfile);
676 if (ret)
677 goto err_system;
678
Simon Glass802aa822014-06-02 22:04:53 -0600679 /*
680 * Set hashes for images in the blob. Unfortunately we may need more
681 * space in either FDT, so keep trying until we succeed.
682 *
683 * Note: this is pretty inefficient for signing, since we must
684 * calculate the signature every time. It would be better to calculate
685 * all the data and then store it in a separate step. However, this
686 * would be considerably more complex to implement. Generally a few
687 * steps of this loop is enough to sign with several keys.
688 */
689 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
690 ret = fit_add_file_data(params, size_inc, tmpfile);
691 if (!ret || ret != -ENOSPC)
692 break;
Simon Glassb4d8b092013-06-13 15:10:04 -0700693 }
694
Simon Glass802aa822014-06-02 22:04:53 -0600695 if (ret) {
Simon Glass061752e2016-07-03 09:40:43 -0600696 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
697 params->cmdname, ret);
Simon Glass802aa822014-06-02 22:04:53 -0600698 goto err_system;
Simon Glassb4d8b092013-06-13 15:10:04 -0700699 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530700
Simon Glassafd728c2016-02-22 22:55:53 -0700701 /* Move the data so it is external to the FIT, if requested */
702 if (params->external_data) {
703 ret = fit_extract_data(params, tmpfile);
704 if (ret)
705 goto err_system;
706 }
707
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530708 if (rename (tmpfile, params->imagefile) == -1) {
709 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
710 params->cmdname, tmpfile, params->imagefile,
711 strerror (errno));
712 unlink (tmpfile);
713 unlink (params->imagefile);
Simon Glass802aa822014-06-02 22:04:53 -0600714 return EXIT_FAILURE;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530715 }
Simon Glass802aa822014-06-02 22:04:53 -0600716 return EXIT_SUCCESS;
Simon Glass4161c412013-05-08 08:05:57 +0000717
Simon Glass4161c412013-05-08 08:05:57 +0000718err_system:
719 unlink(tmpfile);
720 return -1;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530721}
722
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200723/**
724 * fit_image_extract - extract a FIT component image
725 * @fit: pointer to the FIT format image header
726 * @image_noffset: offset of the component image node
727 * @file_name: name of the file to store the FIT sub-image
728 *
729 * returns:
730 * zero in case of success or a negative value if fail.
731 */
732static int fit_image_extract(
733 const void *fit,
734 int image_noffset,
735 const char *file_name)
736{
737 const void *file_data;
738 size_t file_size = 0;
739
740 /* get the "data" property of component at offset "image_noffset" */
741 fit_image_get_data(fit, image_noffset, &file_data, &file_size);
742
743 /* save the "file_data" into the file specified by "file_name" */
744 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
745}
746
747/**
748 * fit_extract_contents - retrieve a sub-image component from the FIT image
749 * @ptr: pointer to the FIT format image header
750 * @params: command line parameters
751 *
752 * returns:
753 * zero in case of success or a negative value if fail.
754 */
755static int fit_extract_contents(void *ptr, struct image_tool_params *params)
756{
757 int images_noffset;
758 int noffset;
759 int ndepth;
760 const void *fit = ptr;
761 int count = 0;
762 const char *p;
763
764 /* Indent string is defined in header image.h */
765 p = IMAGE_INDENT_STRING;
766
767 if (!fit_check_format(fit)) {
768 printf("Bad FIT image format\n");
769 return -1;
770 }
771
772 /* Find images parent node offset */
773 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
774 if (images_noffset < 0) {
775 printf("Can't find images parent node '%s' (%s)\n",
776 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
777 return -1;
778 }
779
780 /* Avoid any overrun */
781 count = fit_get_subimage_count(fit, images_noffset);
782 if ((params->pflag < 0) || (count <= params->pflag)) {
783 printf("No such component at '%d'\n", params->pflag);
784 return -1;
785 }
786
787 /* Process its subnodes, extract the desired component from image */
788 for (ndepth = 0, count = 0,
789 noffset = fdt_next_node(fit, images_noffset, &ndepth);
790 (noffset >= 0) && (ndepth > 0);
791 noffset = fdt_next_node(fit, noffset, &ndepth)) {
792 if (ndepth == 1) {
793 /*
794 * Direct child node of the images parent node,
795 * i.e. component image node.
796 */
797 if (params->pflag == count) {
798 printf("Extracted:\n%s Image %u (%s)\n", p,
799 count, fit_get_name(fit, noffset, NULL));
800
801 fit_image_print(fit, noffset, p);
802
803 return fit_image_extract(fit, noffset,
804 params->outfile);
805 }
806
807 count++;
808 }
809 }
810
811 return 0;
812}
813
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700814static int fit_check_params(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530815{
Simon Glass88e31cb2016-02-22 22:55:51 -0700816 if (params->auto_its)
817 return 0;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530818 return ((params->dflag && (params->fflag || params->lflag)) ||
819 (params->fflag && (params->dflag || params->lflag)) ||
820 (params->lflag && (params->dflag || params->fflag)));
821}
822
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200823U_BOOT_IMAGE_TYPE(
824 fitimage,
825 "FIT Image support",
826 sizeof(image_header_t),
827 (void *)&header,
828 fit_check_params,
829 fit_verify_header,
830 fit_print_contents,
831 NULL,
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200832 fit_extract_contents,
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200833 fit_check_image_types,
834 fit_handle_file,
835 NULL /* FIT images use DTB header */
836);