blob: 10fd6d4929374728732efd63e327cab36275766e [file] [log] [blame]
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +05301/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2004
5 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
7 *
8 * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
9 * FIT image specific code abstracted from mkimage.c
10 * some functions added to address abstraction
11 *
12 * All rights reserved.
13 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020014 * SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053015 */
16
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070017#include "imagetool.h"
Heiko Schocher3cb743c2014-03-03 12:19:29 +010018#include "fit_common.h"
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053019#include "mkimage.h"
20#include <image.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
37 tfd = mmap_fdt(params->cmdname, tmpfile, size_inc, &ptr, &sbuf, true);
38 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,
45 &dest_blob, &dest_sbuf, false);
46 if (destfd < 0) {
47 ret = -EIO;
48 goto err_keydest;
49 }
50 destfd_size = dest_sbuf.st_size;
51 }
52
53 /* for first image creation, add a timestamp at offset 0 i.e., root */
Vagrant Cascadianf8e066c2016-06-16 12:28:40 -070054 if (params->datafile) {
55 time_t time = imagetool_get_source_date(params, sbuf.st_mtime);
56 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,
62 params->require_keys);
63 }
64
65 if (dest_blob) {
66 munmap(dest_blob, destfd_size);
67 close(destfd);
68 }
69
70err_keydest:
71 munmap(ptr, sbuf.st_size);
72 close(tfd);
73
74 return ret;
75}
76
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053077/**
Simon Glass88e31cb2016-02-22 22:55:51 -070078 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
79 */
80static int fit_calc_size(struct image_tool_params *params)
81{
Simon Glassbd8bc5d2016-02-22 22:55:52 -070082 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -070083 int size, total_size;
84
85 size = imagetool_get_filesize(params, params->datafile);
86 if (size < 0)
87 return -1;
88
89 total_size = size;
Simon Glassbd8bc5d2016-02-22 22:55:52 -070090 for (cont = params->content_head; cont; cont = cont->next) {
91 size = imagetool_get_filesize(params, cont->fname);
92 if (size < 0)
93 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -070094
Simon Glassbd8bc5d2016-02-22 22:55:52 -070095 /* Add space for properties */
96 total_size += size + 300;
97 }
Simon Glass88e31cb2016-02-22 22:55:51 -070098
99 /* Add plenty of space for headers, properties, nodes, etc. */
100 total_size += 4096;
101
102 return total_size;
103}
104
105static int fdt_property_file(struct image_tool_params *params,
106 void *fdt, const char *name, const char *fname)
107{
108 struct stat sbuf;
109 void *ptr;
110 int ret;
111 int fd;
112
113 fd = open(fname, O_RDWR | O_BINARY);
114 if (fd < 0) {
115 fprintf(stderr, "%s: Can't open %s: %s\n",
116 params->cmdname, fname, strerror(errno));
117 return -1;
118 }
119
120 if (fstat(fd, &sbuf) < 0) {
121 fprintf(stderr, "%s: Can't stat %s: %s\n",
122 params->cmdname, fname, strerror(errno));
123 goto err;
124 }
125
126 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
127 if (ret)
Simon Glass9ab7f052016-03-16 07:45:42 -0600128 goto err;
Simon Glass88e31cb2016-02-22 22:55:51 -0700129 ret = read(fd, ptr, sbuf.st_size);
130 if (ret != sbuf.st_size) {
131 fprintf(stderr, "%s: Can't read %s: %s\n",
132 params->cmdname, fname, strerror(errno));
133 goto err;
134 }
Simon Glass9ab7f052016-03-16 07:45:42 -0600135 close(fd);
Simon Glass88e31cb2016-02-22 22:55:51 -0700136
137 return 0;
138err:
139 close(fd);
140 return -1;
141}
142
143static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
144{
145 char str[100];
146 va_list ptr;
147
148 va_start(ptr, fmt);
149 vsnprintf(str, sizeof(str), fmt, ptr);
150 va_end(ptr);
151 return fdt_property_string(fdt, name, str);
152}
153
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700154static void get_basename(char *str, int size, const char *fname)
155{
156 const char *p, *start, *end;
157 int len;
158
159 /*
160 * Use the base name as the 'name' field. So for example:
161 *
162 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
163 * becomes "sun7i-a20-bananapro"
164 */
165 p = strrchr(fname, '/');
166 start = p ? p + 1 : fname;
167 p = strrchr(fname, '.');
168 end = p ? p : fname + strlen(fname);
169 len = end - start;
170 if (len >= size)
171 len = size - 1;
172 memcpy(str, start, len);
173 str[len] = '\0';
174}
175
Simon Glass88e31cb2016-02-22 22:55:51 -0700176/**
177 * fit_write_images() - Write out a list of images to the FIT
178 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700179 * We always include the main image (params->datafile). If there are device
180 * tree files, we include an fdt@ node for each of those too.
Simon Glass88e31cb2016-02-22 22:55:51 -0700181 */
182static int fit_write_images(struct image_tool_params *params, char *fdt)
183{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700184 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700185 const char *typename;
186 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700187 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700188 int ret;
189
190 fdt_begin_node(fdt, "images");
191
192 /* First the main image */
193 typename = genimg_get_type_short_name(params->fit_image_type);
194 snprintf(str, sizeof(str), "%s@1", typename);
195 fdt_begin_node(fdt, str);
196 fdt_property_string(fdt, "description", params->imagename);
197 fdt_property_string(fdt, "type", typename);
Simon Glassaeeea7f2016-06-30 10:52:12 -0600198 fdt_property_string(fdt, "arch",
199 genimg_get_arch_short_name(params->arch));
Simon Glass88e31cb2016-02-22 22:55:51 -0700200 fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os));
201 fdt_property_string(fdt, "compression",
202 genimg_get_comp_short_name(params->comp));
203 fdt_property_u32(fdt, "load", params->addr);
204 fdt_property_u32(fdt, "entry", params->ep);
205
206 /*
207 * Put data last since it is large. SPL may only load the first part
208 * of the DT, so this way it can access all the above fields.
209 */
210 ret = fdt_property_file(params, fdt, "data", params->datafile);
211 if (ret)
212 return ret;
213 fdt_end_node(fdt);
214
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700215 /* Now the device tree files if available */
216 upto = 0;
217 for (cont = params->content_head; cont; cont = cont->next) {
218 if (cont->type != IH_TYPE_FLATDT)
219 continue;
220 snprintf(str, sizeof(str), "%s@%d", FIT_FDT_PROP, ++upto);
221 fdt_begin_node(fdt, str);
222
223 get_basename(str, sizeof(str), cont->fname);
224 fdt_property_string(fdt, "description", str);
225 ret = fdt_property_file(params, fdt, "data", cont->fname);
226 if (ret)
227 return ret;
228 fdt_property_string(fdt, "type", typename);
229 fdt_property_string(fdt, "arch",
230 genimg_get_arch_short_name(params->arch));
231 fdt_property_string(fdt, "compression",
232 genimg_get_comp_short_name(IH_COMP_NONE));
233 fdt_end_node(fdt);
234 }
235
Simon Glass88e31cb2016-02-22 22:55:51 -0700236 fdt_end_node(fdt);
237
238 return 0;
239}
240
241/**
242 * fit_write_configs() - Write out a list of configurations to the FIT
243 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700244 * If there are device tree files, we include a configuration for each, which
245 * selects the main image (params->datafile) and its corresponding device
246 * tree file.
247 *
248 * Otherwise we just create a configuration with the main image in it.
Simon Glass88e31cb2016-02-22 22:55:51 -0700249 */
250static void fit_write_configs(struct image_tool_params *params, char *fdt)
251{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700252 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700253 const char *typename;
254 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700255 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700256
257 fdt_begin_node(fdt, "configurations");
258 fdt_property_string(fdt, "default", "conf@1");
259
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700260 upto = 0;
261 for (cont = params->content_head; cont; cont = cont->next) {
262 if (cont->type != IH_TYPE_FLATDT)
263 continue;
264 typename = genimg_get_type_short_name(cont->type);
265 snprintf(str, sizeof(str), "conf@%d", ++upto);
266 fdt_begin_node(fdt, str);
267
268 get_basename(str, sizeof(str), cont->fname);
269 fdt_property_string(fdt, "description", str);
270
271 typename = genimg_get_type_short_name(params->fit_image_type);
272 snprintf(str, sizeof(str), "%s@1", typename);
273 fdt_property_string(fdt, typename, str);
274
275 snprintf(str, sizeof(str), FIT_FDT_PROP "@%d", upto);
276 fdt_property_string(fdt, FIT_FDT_PROP, str);
277 fdt_end_node(fdt);
278 }
279 if (!upto) {
280 fdt_begin_node(fdt, "conf@1");
281 typename = genimg_get_type_short_name(params->fit_image_type);
282 snprintf(str, sizeof(str), "%s@1", typename);
283 fdt_property_string(fdt, typename, str);
284 fdt_end_node(fdt);
285 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700286
287 fdt_end_node(fdt);
288}
289
290static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
291{
292 int ret;
293
294 ret = fdt_create(fdt, size);
295 if (ret)
296 return ret;
297 fdt_finish_reservemap(fdt);
298 fdt_begin_node(fdt, "");
299 fdt_property_strf(fdt, "description",
300 "%s image with one or more FDT blobs",
301 genimg_get_type_name(params->fit_image_type));
302 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
303 fdt_property_u32(fdt, "#address-cells", 1);
304 ret = fit_write_images(params, fdt);
305 if (ret)
306 return ret;
307 fit_write_configs(params, fdt);
308 fdt_end_node(fdt);
309 ret = fdt_finish(fdt);
310 if (ret)
311 return ret;
312
313 return fdt_totalsize(fdt);
314}
315
316static int fit_build(struct image_tool_params *params, const char *fname)
317{
318 char *buf;
319 int size;
320 int ret;
321 int fd;
322
323 size = fit_calc_size(params);
324 if (size < 0)
325 return -1;
326 buf = malloc(size);
327 if (!buf) {
328 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
329 params->cmdname, size);
330 return -1;
331 }
332 ret = fit_build_fdt(params, buf, size);
333 if (ret < 0) {
334 fprintf(stderr, "%s: Failed to build FIT image\n",
335 params->cmdname);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600336 goto err_buf;
Simon Glass88e31cb2016-02-22 22:55:51 -0700337 }
338 size = ret;
339 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
340 if (fd < 0) {
341 fprintf(stderr, "%s: Can't open %s: %s\n",
342 params->cmdname, fname, strerror(errno));
343 goto err;
344 }
345 ret = write(fd, buf, size);
346 if (ret != size) {
347 fprintf(stderr, "%s: Can't write %s: %s\n",
348 params->cmdname, fname, strerror(errno));
Simon Glass88e31cb2016-02-22 22:55:51 -0700349 goto err;
350 }
351 close(fd);
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600352 free(buf);
Simon Glass88e31cb2016-02-22 22:55:51 -0700353
354 return 0;
355err:
Simon Glassc0ee1ca2016-03-16 07:45:41 -0600356 close(fd);
357err_buf:
Simon Glass88e31cb2016-02-22 22:55:51 -0700358 free(buf);
359 return -1;
360}
361
362/**
Simon Glassafd728c2016-02-22 22:55:53 -0700363 * fit_extract_data() - Move all data outside the FIT
364 *
365 * This takes a normal FIT file and removes all the 'data' properties from it.
366 * The data is placed in an area after the FIT so that it can be accessed
367 * using an offset into that area. The 'data' properties turn into
368 * 'data-offset' properties.
369 *
370 * This function cannot cope with FITs with 'data-offset' properties. All
371 * data must be in 'data' properties on entry.
372 */
373static int fit_extract_data(struct image_tool_params *params, const char *fname)
374{
375 void *buf;
376 int buf_ptr;
377 int fit_size, new_size;
378 int fd;
379 struct stat sbuf;
380 void *fdt;
381 int ret;
382 int images;
383 int node;
384
385 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false);
386 if (fd < 0)
387 return -EIO;
388 fit_size = fdt_totalsize(fdt);
389
390 /* Allocate space to hold the image data we will extract */
391 buf = malloc(fit_size);
392 if (!buf) {
393 ret = -ENOMEM;
Simon Glass4e7505f2016-03-16 07:45:39 -0600394 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700395 }
396 buf_ptr = 0;
397
398 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
399 if (images < 0) {
400 debug("%s: Cannot find /images node: %d\n", __func__, images);
401 ret = -EINVAL;
Simon Glass4e7505f2016-03-16 07:45:39 -0600402 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700403 }
404
405 for (node = fdt_first_subnode(fdt, images);
406 node >= 0;
407 node = fdt_next_subnode(fdt, node)) {
408 const char *data;
409 int len;
410
411 data = fdt_getprop(fdt, node, "data", &len);
412 if (!data)
413 continue;
414 memcpy(buf + buf_ptr, data, len);
415 debug("Extracting data size %x\n", len);
416
417 ret = fdt_delprop(fdt, node, "data");
418 if (ret) {
419 ret = -EPERM;
Simon Glass4e7505f2016-03-16 07:45:39 -0600420 goto err_munmap;
Simon Glassafd728c2016-02-22 22:55:53 -0700421 }
Teddy Reeda8457622016-06-09 19:38:02 -0700422 if (params->external_offset > 0) {
423 /* An external offset positions the data absolutely. */
424 fdt_setprop_u32(fdt, node, "data-position",
425 params->external_offset + buf_ptr);
426 } else {
427 fdt_setprop_u32(fdt, node, "data-offset", buf_ptr);
428 }
Simon Glassafd728c2016-02-22 22:55:53 -0700429 fdt_setprop_u32(fdt, node, "data-size", len);
430
431 buf_ptr += (len + 3) & ~3;
432 }
433
434 /* Pack the FDT and place the data after it */
435 fdt_pack(fdt);
436
437 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
438 debug("External data size %x\n", buf_ptr);
439 new_size = fdt_totalsize(fdt);
440 new_size = (new_size + 3) & ~3;
441 munmap(fdt, sbuf.st_size);
442
443 if (ftruncate(fd, new_size)) {
444 debug("%s: Failed to truncate file: %s\n", __func__,
445 strerror(errno));
446 ret = -EIO;
447 goto err;
448 }
Teddy Reeda8457622016-06-09 19:38:02 -0700449
450 /* Check if an offset for the external data was set. */
451 if (params->external_offset > 0) {
452 if (params->external_offset < new_size) {
453 debug("External offset %x overlaps FIT length %x",
454 params->external_offset, new_size);
455 ret = -EINVAL;
456 goto err;
457 }
458 new_size = params->external_offset;
459 }
Simon Glassafd728c2016-02-22 22:55:53 -0700460 if (lseek(fd, new_size, SEEK_SET) < 0) {
461 debug("%s: Failed to seek to end of file: %s\n", __func__,
462 strerror(errno));
463 ret = -EIO;
464 goto err;
465 }
466 if (write(fd, buf, buf_ptr) != buf_ptr) {
467 debug("%s: Failed to write external data to file %s\n",
468 __func__, strerror(errno));
469 ret = -EIO;
470 goto err;
471 }
Simon Glass4e7505f2016-03-16 07:45:39 -0600472 close(fd);
473 return 0;
Simon Glassafd728c2016-02-22 22:55:53 -0700474
Simon Glass4e7505f2016-03-16 07:45:39 -0600475err_munmap:
476 munmap(fdt, sbuf.st_size);
Simon Glassafd728c2016-02-22 22:55:53 -0700477err:
Simon Glassf3a6e8f2016-03-16 07:45:40 -0600478 if (buf)
479 free(buf);
Simon Glassafd728c2016-02-22 22:55:53 -0700480 close(fd);
481 return ret;
482}
483
Simon Glassd11b7002016-02-22 22:55:54 -0700484static int fit_import_data(struct image_tool_params *params, const char *fname)
485{
486 void *fdt, *old_fdt;
487 int fit_size, new_size, size, data_base;
488 int fd;
489 struct stat sbuf;
490 int ret;
491 int images;
492 int node;
493
494 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false);
495 if (fd < 0)
496 return -EIO;
497 fit_size = fdt_totalsize(old_fdt);
498 data_base = (fit_size + 3) & ~3;
499
500 /* Allocate space to hold the new FIT */
501 size = sbuf.st_size + 16384;
502 fdt = malloc(size);
503 if (!fdt) {
504 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
505 __func__, size);
506 ret = -ENOMEM;
507 goto err;
508 }
509 ret = fdt_open_into(old_fdt, fdt, size);
510 if (ret) {
511 debug("%s: Failed to expand FIT: %s\n", __func__,
512 fdt_strerror(errno));
513 ret = -EINVAL;
514 goto err;
515 }
516
517 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
518 if (images < 0) {
519 debug("%s: Cannot find /images node: %d\n", __func__, images);
520 ret = -EINVAL;
521 goto err;
522 }
523
524 for (node = fdt_first_subnode(fdt, images);
525 node >= 0;
526 node = fdt_next_subnode(fdt, node)) {
527 int buf_ptr;
528 int len;
529
530 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
531 len = fdtdec_get_int(fdt, node, "data-size", -1);
532 if (buf_ptr == -1 || len == -1)
533 continue;
534 debug("Importing data size %x\n", len);
535
536 ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
537 len);
538 if (ret) {
539 debug("%s: Failed to write property: %s\n", __func__,
540 fdt_strerror(ret));
541 ret = -EINVAL;
542 goto err;
543 }
544 }
545
Simon Glass84abf152016-03-16 07:45:31 -0600546 munmap(old_fdt, sbuf.st_size);
Simon Glassd11b7002016-02-22 22:55:54 -0700547 close(fd);
548
549 /* Pack the FDT and place the data after it */
550 fdt_pack(fdt);
551
552 new_size = fdt_totalsize(fdt);
553 debug("Size expanded from %x to %x\n", fit_size, new_size);
554
555 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
556 if (fd < 0) {
557 fprintf(stderr, "%s: Can't open %s: %s\n",
558 params->cmdname, fname, strerror(errno));
Simon Glass6da4a652016-03-16 07:45:43 -0600559 free(fdt);
560 return -EIO;
Simon Glassd11b7002016-02-22 22:55:54 -0700561 }
562 if (write(fd, fdt, new_size) != new_size) {
563 debug("%s: Failed to write external data to file %s\n",
564 __func__, strerror(errno));
565 ret = -EIO;
566 goto err;
567 }
Simon Glassd11b7002016-02-22 22:55:54 -0700568
569 ret = 0;
570
571err:
Simon Glass5014cc12016-03-16 07:45:38 -0600572 free(fdt);
Simon Glassd11b7002016-02-22 22:55:54 -0700573 close(fd);
574 return ret;
575}
576
Simon Glassafd728c2016-02-22 22:55:53 -0700577/**
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530578 * fit_handle_file - main FIT file processing function
579 *
580 * fit_handle_file() runs dtc to convert .its to .itb, includes
581 * binary data, updates timestamp property and calculates hashes.
582 *
583 * datafile - .its file
584 * imagefile - .itb file
585 *
586 * returns:
587 * only on success, otherwise calls exit (EXIT_FAILURE);
588 */
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700589static int fit_handle_file(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530590{
591 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
592 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
Simon Glass802aa822014-06-02 22:04:53 -0600593 size_t size_inc;
594 int ret;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530595
596 /* Flattened Image Tree (FIT) format handling */
597 debug ("FIT format handling\n");
598
599 /* call dtc to include binary properties into the tmp file */
600 if (strlen (params->imagefile) +
601 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
602 fprintf (stderr, "%s: Image file name (%s) too long, "
603 "can't create tmpfile",
604 params->imagefile, params->cmdname);
605 return (EXIT_FAILURE);
606 }
607 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
608
Simon Glassce8c3ca2013-06-13 15:10:05 -0700609 /* We either compile the source file, or use the existing FIT image */
Simon Glass88e31cb2016-02-22 22:55:51 -0700610 if (params->auto_its) {
611 if (fit_build(params, tmpfile)) {
612 fprintf(stderr, "%s: failed to build FIT\n",
613 params->cmdname);
614 return EXIT_FAILURE;
615 }
616 *cmd = '\0';
617 } else if (params->datafile) {
Simon Glassce8c3ca2013-06-13 15:10:05 -0700618 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
619 snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
620 MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
621 debug("Trying to execute \"%s\"\n", cmd);
622 } else {
623 snprintf(cmd, sizeof(cmd), "cp %s %s",
624 params->imagefile, tmpfile);
625 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700626 if (*cmd && system(cmd) == -1) {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530627 fprintf (stderr, "%s: system(%s) failed: %s\n",
628 params->cmdname, cmd, strerror(errno));
Simon Glass4161c412013-05-08 08:05:57 +0000629 goto err_system;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530630 }
631
Simon Glassd11b7002016-02-22 22:55:54 -0700632 /* Move the data so it is internal to the FIT, if needed */
633 ret = fit_import_data(params, tmpfile);
634 if (ret)
635 goto err_system;
636
Simon Glass802aa822014-06-02 22:04:53 -0600637 /*
638 * Set hashes for images in the blob. Unfortunately we may need more
639 * space in either FDT, so keep trying until we succeed.
640 *
641 * Note: this is pretty inefficient for signing, since we must
642 * calculate the signature every time. It would be better to calculate
643 * all the data and then store it in a separate step. However, this
644 * would be considerably more complex to implement. Generally a few
645 * steps of this loop is enough to sign with several keys.
646 */
647 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
648 ret = fit_add_file_data(params, size_inc, tmpfile);
649 if (!ret || ret != -ENOSPC)
650 break;
Simon Glassb4d8b092013-06-13 15:10:04 -0700651 }
652
Simon Glass802aa822014-06-02 22:04:53 -0600653 if (ret) {
Simon Glass061752e2016-07-03 09:40:43 -0600654 fprintf(stderr, "%s Can't add hashes to FIT blob: %d\n",
655 params->cmdname, ret);
Simon Glass802aa822014-06-02 22:04:53 -0600656 goto err_system;
Simon Glassb4d8b092013-06-13 15:10:04 -0700657 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530658
Simon Glassafd728c2016-02-22 22:55:53 -0700659 /* Move the data so it is external to the FIT, if requested */
660 if (params->external_data) {
661 ret = fit_extract_data(params, tmpfile);
662 if (ret)
663 goto err_system;
664 }
665
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530666 if (rename (tmpfile, params->imagefile) == -1) {
667 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
668 params->cmdname, tmpfile, params->imagefile,
669 strerror (errno));
670 unlink (tmpfile);
671 unlink (params->imagefile);
Simon Glass802aa822014-06-02 22:04:53 -0600672 return EXIT_FAILURE;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530673 }
Simon Glass802aa822014-06-02 22:04:53 -0600674 return EXIT_SUCCESS;
Simon Glass4161c412013-05-08 08:05:57 +0000675
Simon Glass4161c412013-05-08 08:05:57 +0000676err_system:
677 unlink(tmpfile);
678 return -1;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530679}
680
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200681/**
682 * fit_image_extract - extract a FIT component image
683 * @fit: pointer to the FIT format image header
684 * @image_noffset: offset of the component image node
685 * @file_name: name of the file to store the FIT sub-image
686 *
687 * returns:
688 * zero in case of success or a negative value if fail.
689 */
690static int fit_image_extract(
691 const void *fit,
692 int image_noffset,
693 const char *file_name)
694{
695 const void *file_data;
696 size_t file_size = 0;
697
698 /* get the "data" property of component at offset "image_noffset" */
699 fit_image_get_data(fit, image_noffset, &file_data, &file_size);
700
701 /* save the "file_data" into the file specified by "file_name" */
702 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
703}
704
705/**
706 * fit_extract_contents - retrieve a sub-image component from the FIT image
707 * @ptr: pointer to the FIT format image header
708 * @params: command line parameters
709 *
710 * returns:
711 * zero in case of success or a negative value if fail.
712 */
713static int fit_extract_contents(void *ptr, struct image_tool_params *params)
714{
715 int images_noffset;
716 int noffset;
717 int ndepth;
718 const void *fit = ptr;
719 int count = 0;
720 const char *p;
721
722 /* Indent string is defined in header image.h */
723 p = IMAGE_INDENT_STRING;
724
725 if (!fit_check_format(fit)) {
726 printf("Bad FIT image format\n");
727 return -1;
728 }
729
730 /* Find images parent node offset */
731 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
732 if (images_noffset < 0) {
733 printf("Can't find images parent node '%s' (%s)\n",
734 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
735 return -1;
736 }
737
738 /* Avoid any overrun */
739 count = fit_get_subimage_count(fit, images_noffset);
740 if ((params->pflag < 0) || (count <= params->pflag)) {
741 printf("No such component at '%d'\n", params->pflag);
742 return -1;
743 }
744
745 /* Process its subnodes, extract the desired component from image */
746 for (ndepth = 0, count = 0,
747 noffset = fdt_next_node(fit, images_noffset, &ndepth);
748 (noffset >= 0) && (ndepth > 0);
749 noffset = fdt_next_node(fit, noffset, &ndepth)) {
750 if (ndepth == 1) {
751 /*
752 * Direct child node of the images parent node,
753 * i.e. component image node.
754 */
755 if (params->pflag == count) {
756 printf("Extracted:\n%s Image %u (%s)\n", p,
757 count, fit_get_name(fit, noffset, NULL));
758
759 fit_image_print(fit, noffset, p);
760
761 return fit_image_extract(fit, noffset,
762 params->outfile);
763 }
764
765 count++;
766 }
767 }
768
769 return 0;
770}
771
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700772static int fit_check_params(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530773{
Simon Glass88e31cb2016-02-22 22:55:51 -0700774 if (params->auto_its)
775 return 0;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530776 return ((params->dflag && (params->fflag || params->lflag)) ||
777 (params->fflag && (params->dflag || params->lflag)) ||
778 (params->lflag && (params->dflag || params->fflag)));
779}
780
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200781U_BOOT_IMAGE_TYPE(
782 fitimage,
783 "FIT Image support",
784 sizeof(image_header_t),
785 (void *)&header,
786 fit_check_params,
787 fit_verify_header,
788 fit_print_contents,
789 NULL,
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200790 fit_extract_contents,
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200791 fit_check_image_types,
792 fit_handle_file,
793 NULL /* FIT images use DTB header */
794);