blob: 3ecc88fb56589e6e666c0e782059096f980c2e6b [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 */
54 if (params->datafile)
55 ret = fit_set_timestamp(ptr, 0, sbuf.st_mtime);
56
57 if (!ret) {
58 ret = fit_add_verification_data(params->keydir, dest_blob, ptr,
59 params->comment,
60 params->require_keys);
61 }
62
63 if (dest_blob) {
64 munmap(dest_blob, destfd_size);
65 close(destfd);
66 }
67
68err_keydest:
69 munmap(ptr, sbuf.st_size);
70 close(tfd);
71
72 return ret;
73}
74
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053075/**
Simon Glass88e31cb2016-02-22 22:55:51 -070076 * fit_calc_size() - Calculate the approximate size of the FIT we will generate
77 */
78static int fit_calc_size(struct image_tool_params *params)
79{
Simon Glassbd8bc5d2016-02-22 22:55:52 -070080 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -070081 int size, total_size;
82
83 size = imagetool_get_filesize(params, params->datafile);
84 if (size < 0)
85 return -1;
86
87 total_size = size;
Simon Glassbd8bc5d2016-02-22 22:55:52 -070088 for (cont = params->content_head; cont; cont = cont->next) {
89 size = imagetool_get_filesize(params, cont->fname);
90 if (size < 0)
91 return -1;
Simon Glass88e31cb2016-02-22 22:55:51 -070092
Simon Glassbd8bc5d2016-02-22 22:55:52 -070093 /* Add space for properties */
94 total_size += size + 300;
95 }
Simon Glass88e31cb2016-02-22 22:55:51 -070096
97 /* Add plenty of space for headers, properties, nodes, etc. */
98 total_size += 4096;
99
100 return total_size;
101}
102
103static int fdt_property_file(struct image_tool_params *params,
104 void *fdt, const char *name, const char *fname)
105{
106 struct stat sbuf;
107 void *ptr;
108 int ret;
109 int fd;
110
111 fd = open(fname, O_RDWR | O_BINARY);
112 if (fd < 0) {
113 fprintf(stderr, "%s: Can't open %s: %s\n",
114 params->cmdname, fname, strerror(errno));
115 return -1;
116 }
117
118 if (fstat(fd, &sbuf) < 0) {
119 fprintf(stderr, "%s: Can't stat %s: %s\n",
120 params->cmdname, fname, strerror(errno));
121 goto err;
122 }
123
124 ret = fdt_property_placeholder(fdt, "data", sbuf.st_size, &ptr);
125 if (ret)
126 return ret;
127 ret = read(fd, ptr, sbuf.st_size);
128 if (ret != sbuf.st_size) {
129 fprintf(stderr, "%s: Can't read %s: %s\n",
130 params->cmdname, fname, strerror(errno));
131 goto err;
132 }
133
134 return 0;
135err:
136 close(fd);
137 return -1;
138}
139
140static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
141{
142 char str[100];
143 va_list ptr;
144
145 va_start(ptr, fmt);
146 vsnprintf(str, sizeof(str), fmt, ptr);
147 va_end(ptr);
148 return fdt_property_string(fdt, name, str);
149}
150
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700151static void get_basename(char *str, int size, const char *fname)
152{
153 const char *p, *start, *end;
154 int len;
155
156 /*
157 * Use the base name as the 'name' field. So for example:
158 *
159 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
160 * becomes "sun7i-a20-bananapro"
161 */
162 p = strrchr(fname, '/');
163 start = p ? p + 1 : fname;
164 p = strrchr(fname, '.');
165 end = p ? p : fname + strlen(fname);
166 len = end - start;
167 if (len >= size)
168 len = size - 1;
169 memcpy(str, start, len);
170 str[len] = '\0';
171}
172
Simon Glass88e31cb2016-02-22 22:55:51 -0700173/**
174 * fit_write_images() - Write out a list of images to the FIT
175 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700176 * We always include the main image (params->datafile). If there are device
177 * tree files, we include an fdt@ node for each of those too.
Simon Glass88e31cb2016-02-22 22:55:51 -0700178 */
179static int fit_write_images(struct image_tool_params *params, char *fdt)
180{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700181 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700182 const char *typename;
183 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700184 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700185 int ret;
186
187 fdt_begin_node(fdt, "images");
188
189 /* First the main image */
190 typename = genimg_get_type_short_name(params->fit_image_type);
191 snprintf(str, sizeof(str), "%s@1", typename);
192 fdt_begin_node(fdt, str);
193 fdt_property_string(fdt, "description", params->imagename);
194 fdt_property_string(fdt, "type", typename);
195 fdt_property_string(fdt, "arch", genimg_get_arch_name(params->arch));
196 fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os));
197 fdt_property_string(fdt, "compression",
198 genimg_get_comp_short_name(params->comp));
199 fdt_property_u32(fdt, "load", params->addr);
200 fdt_property_u32(fdt, "entry", params->ep);
201
202 /*
203 * Put data last since it is large. SPL may only load the first part
204 * of the DT, so this way it can access all the above fields.
205 */
206 ret = fdt_property_file(params, fdt, "data", params->datafile);
207 if (ret)
208 return ret;
209 fdt_end_node(fdt);
210
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700211 /* Now the device tree files if available */
212 upto = 0;
213 for (cont = params->content_head; cont; cont = cont->next) {
214 if (cont->type != IH_TYPE_FLATDT)
215 continue;
216 snprintf(str, sizeof(str), "%s@%d", FIT_FDT_PROP, ++upto);
217 fdt_begin_node(fdt, str);
218
219 get_basename(str, sizeof(str), cont->fname);
220 fdt_property_string(fdt, "description", str);
221 ret = fdt_property_file(params, fdt, "data", cont->fname);
222 if (ret)
223 return ret;
224 fdt_property_string(fdt, "type", typename);
225 fdt_property_string(fdt, "arch",
226 genimg_get_arch_short_name(params->arch));
227 fdt_property_string(fdt, "compression",
228 genimg_get_comp_short_name(IH_COMP_NONE));
229 fdt_end_node(fdt);
230 }
231
Simon Glass88e31cb2016-02-22 22:55:51 -0700232 fdt_end_node(fdt);
233
234 return 0;
235}
236
237/**
238 * fit_write_configs() - Write out a list of configurations to the FIT
239 *
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700240 * If there are device tree files, we include a configuration for each, which
241 * selects the main image (params->datafile) and its corresponding device
242 * tree file.
243 *
244 * Otherwise we just create a configuration with the main image in it.
Simon Glass88e31cb2016-02-22 22:55:51 -0700245 */
246static void fit_write_configs(struct image_tool_params *params, char *fdt)
247{
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700248 struct content_info *cont;
Simon Glass88e31cb2016-02-22 22:55:51 -0700249 const char *typename;
250 char str[100];
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700251 int upto;
Simon Glass88e31cb2016-02-22 22:55:51 -0700252
253 fdt_begin_node(fdt, "configurations");
254 fdt_property_string(fdt, "default", "conf@1");
255
Simon Glassbd8bc5d2016-02-22 22:55:52 -0700256 upto = 0;
257 for (cont = params->content_head; cont; cont = cont->next) {
258 if (cont->type != IH_TYPE_FLATDT)
259 continue;
260 typename = genimg_get_type_short_name(cont->type);
261 snprintf(str, sizeof(str), "conf@%d", ++upto);
262 fdt_begin_node(fdt, str);
263
264 get_basename(str, sizeof(str), cont->fname);
265 fdt_property_string(fdt, "description", str);
266
267 typename = genimg_get_type_short_name(params->fit_image_type);
268 snprintf(str, sizeof(str), "%s@1", typename);
269 fdt_property_string(fdt, typename, str);
270
271 snprintf(str, sizeof(str), FIT_FDT_PROP "@%d", upto);
272 fdt_property_string(fdt, FIT_FDT_PROP, str);
273 fdt_end_node(fdt);
274 }
275 if (!upto) {
276 fdt_begin_node(fdt, "conf@1");
277 typename = genimg_get_type_short_name(params->fit_image_type);
278 snprintf(str, sizeof(str), "%s@1", typename);
279 fdt_property_string(fdt, typename, str);
280 fdt_end_node(fdt);
281 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700282
283 fdt_end_node(fdt);
284}
285
286static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
287{
288 int ret;
289
290 ret = fdt_create(fdt, size);
291 if (ret)
292 return ret;
293 fdt_finish_reservemap(fdt);
294 fdt_begin_node(fdt, "");
295 fdt_property_strf(fdt, "description",
296 "%s image with one or more FDT blobs",
297 genimg_get_type_name(params->fit_image_type));
298 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
299 fdt_property_u32(fdt, "#address-cells", 1);
300 ret = fit_write_images(params, fdt);
301 if (ret)
302 return ret;
303 fit_write_configs(params, fdt);
304 fdt_end_node(fdt);
305 ret = fdt_finish(fdt);
306 if (ret)
307 return ret;
308
309 return fdt_totalsize(fdt);
310}
311
312static int fit_build(struct image_tool_params *params, const char *fname)
313{
314 char *buf;
315 int size;
316 int ret;
317 int fd;
318
319 size = fit_calc_size(params);
320 if (size < 0)
321 return -1;
322 buf = malloc(size);
323 if (!buf) {
324 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
325 params->cmdname, size);
326 return -1;
327 }
328 ret = fit_build_fdt(params, buf, size);
329 if (ret < 0) {
330 fprintf(stderr, "%s: Failed to build FIT image\n",
331 params->cmdname);
332 goto err;
333 }
334 size = ret;
335 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
336 if (fd < 0) {
337 fprintf(stderr, "%s: Can't open %s: %s\n",
338 params->cmdname, fname, strerror(errno));
339 goto err;
340 }
341 ret = write(fd, buf, size);
342 if (ret != size) {
343 fprintf(stderr, "%s: Can't write %s: %s\n",
344 params->cmdname, fname, strerror(errno));
345 close(fd);
346 goto err;
347 }
348 close(fd);
349
350 return 0;
351err:
352 free(buf);
353 return -1;
354}
355
356/**
Simon Glassafd728c2016-02-22 22:55:53 -0700357 * fit_extract_data() - Move all data outside the FIT
358 *
359 * This takes a normal FIT file and removes all the 'data' properties from it.
360 * The data is placed in an area after the FIT so that it can be accessed
361 * using an offset into that area. The 'data' properties turn into
362 * 'data-offset' properties.
363 *
364 * This function cannot cope with FITs with 'data-offset' properties. All
365 * data must be in 'data' properties on entry.
366 */
367static int fit_extract_data(struct image_tool_params *params, const char *fname)
368{
369 void *buf;
370 int buf_ptr;
371 int fit_size, new_size;
372 int fd;
373 struct stat sbuf;
374 void *fdt;
375 int ret;
376 int images;
377 int node;
378
379 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false);
380 if (fd < 0)
381 return -EIO;
382 fit_size = fdt_totalsize(fdt);
383
384 /* Allocate space to hold the image data we will extract */
385 buf = malloc(fit_size);
386 if (!buf) {
387 ret = -ENOMEM;
388 goto err;
389 }
390 buf_ptr = 0;
391
392 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
393 if (images < 0) {
394 debug("%s: Cannot find /images node: %d\n", __func__, images);
395 ret = -EINVAL;
396 goto err;
397 }
398
399 for (node = fdt_first_subnode(fdt, images);
400 node >= 0;
401 node = fdt_next_subnode(fdt, node)) {
402 const char *data;
403 int len;
404
405 data = fdt_getprop(fdt, node, "data", &len);
406 if (!data)
407 continue;
408 memcpy(buf + buf_ptr, data, len);
409 debug("Extracting data size %x\n", len);
410
411 ret = fdt_delprop(fdt, node, "data");
412 if (ret) {
413 ret = -EPERM;
414 goto err;
415 }
416 fdt_setprop_u32(fdt, node, "data-offset", buf_ptr);
417 fdt_setprop_u32(fdt, node, "data-size", len);
418
419 buf_ptr += (len + 3) & ~3;
420 }
421
422 /* Pack the FDT and place the data after it */
423 fdt_pack(fdt);
424
425 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
426 debug("External data size %x\n", buf_ptr);
427 new_size = fdt_totalsize(fdt);
428 new_size = (new_size + 3) & ~3;
429 munmap(fdt, sbuf.st_size);
430
431 if (ftruncate(fd, new_size)) {
432 debug("%s: Failed to truncate file: %s\n", __func__,
433 strerror(errno));
434 ret = -EIO;
435 goto err;
436 }
437 if (lseek(fd, new_size, SEEK_SET) < 0) {
438 debug("%s: Failed to seek to end of file: %s\n", __func__,
439 strerror(errno));
440 ret = -EIO;
441 goto err;
442 }
443 if (write(fd, buf, buf_ptr) != buf_ptr) {
444 debug("%s: Failed to write external data to file %s\n",
445 __func__, strerror(errno));
446 ret = -EIO;
447 goto err;
448 }
449 close(fd);
450
451 ret = 0;
452
453err:
454 close(fd);
455 return ret;
456}
457
Simon Glassd11b7002016-02-22 22:55:54 -0700458static int fit_import_data(struct image_tool_params *params, const char *fname)
459{
460 void *fdt, *old_fdt;
461 int fit_size, new_size, size, data_base;
462 int fd;
463 struct stat sbuf;
464 int ret;
465 int images;
466 int node;
467
468 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false);
469 if (fd < 0)
470 return -EIO;
471 fit_size = fdt_totalsize(old_fdt);
472 data_base = (fit_size + 3) & ~3;
473
474 /* Allocate space to hold the new FIT */
475 size = sbuf.st_size + 16384;
476 fdt = malloc(size);
477 if (!fdt) {
478 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
479 __func__, size);
480 ret = -ENOMEM;
481 goto err;
482 }
483 ret = fdt_open_into(old_fdt, fdt, size);
484 if (ret) {
485 debug("%s: Failed to expand FIT: %s\n", __func__,
486 fdt_strerror(errno));
487 ret = -EINVAL;
488 goto err;
489 }
490
491 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
492 if (images < 0) {
493 debug("%s: Cannot find /images node: %d\n", __func__, images);
494 ret = -EINVAL;
495 goto err;
496 }
497
498 for (node = fdt_first_subnode(fdt, images);
499 node >= 0;
500 node = fdt_next_subnode(fdt, node)) {
501 int buf_ptr;
502 int len;
503
504 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
505 len = fdtdec_get_int(fdt, node, "data-size", -1);
506 if (buf_ptr == -1 || len == -1)
507 continue;
508 debug("Importing data size %x\n", len);
509
510 ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
511 len);
512 if (ret) {
513 debug("%s: Failed to write property: %s\n", __func__,
514 fdt_strerror(ret));
515 ret = -EINVAL;
516 goto err;
517 }
518 }
519
520 munmap(fdt, sbuf.st_size);
521 close(fd);
522
523 /* Pack the FDT and place the data after it */
524 fdt_pack(fdt);
525
526 new_size = fdt_totalsize(fdt);
527 debug("Size expanded from %x to %x\n", fit_size, new_size);
528
529 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
530 if (fd < 0) {
531 fprintf(stderr, "%s: Can't open %s: %s\n",
532 params->cmdname, fname, strerror(errno));
533 goto err;
534 }
535 if (write(fd, fdt, new_size) != new_size) {
536 debug("%s: Failed to write external data to file %s\n",
537 __func__, strerror(errno));
538 ret = -EIO;
539 goto err;
540 }
541 close(fd);
542
543 ret = 0;
544
545err:
546 close(fd);
547 return ret;
548}
549
Simon Glassafd728c2016-02-22 22:55:53 -0700550/**
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530551 * fit_handle_file - main FIT file processing function
552 *
553 * fit_handle_file() runs dtc to convert .its to .itb, includes
554 * binary data, updates timestamp property and calculates hashes.
555 *
556 * datafile - .its file
557 * imagefile - .itb file
558 *
559 * returns:
560 * only on success, otherwise calls exit (EXIT_FAILURE);
561 */
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700562static int fit_handle_file(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530563{
564 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
565 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
Simon Glass802aa822014-06-02 22:04:53 -0600566 size_t size_inc;
567 int ret;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530568
569 /* Flattened Image Tree (FIT) format handling */
570 debug ("FIT format handling\n");
571
572 /* call dtc to include binary properties into the tmp file */
573 if (strlen (params->imagefile) +
574 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
575 fprintf (stderr, "%s: Image file name (%s) too long, "
576 "can't create tmpfile",
577 params->imagefile, params->cmdname);
578 return (EXIT_FAILURE);
579 }
580 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
581
Simon Glassce8c3ca2013-06-13 15:10:05 -0700582 /* We either compile the source file, or use the existing FIT image */
Simon Glass88e31cb2016-02-22 22:55:51 -0700583 if (params->auto_its) {
584 if (fit_build(params, tmpfile)) {
585 fprintf(stderr, "%s: failed to build FIT\n",
586 params->cmdname);
587 return EXIT_FAILURE;
588 }
589 *cmd = '\0';
590 } else if (params->datafile) {
Simon Glassce8c3ca2013-06-13 15:10:05 -0700591 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
592 snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
593 MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
594 debug("Trying to execute \"%s\"\n", cmd);
595 } else {
596 snprintf(cmd, sizeof(cmd), "cp %s %s",
597 params->imagefile, tmpfile);
598 }
Simon Glass88e31cb2016-02-22 22:55:51 -0700599 if (*cmd && system(cmd) == -1) {
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530600 fprintf (stderr, "%s: system(%s) failed: %s\n",
601 params->cmdname, cmd, strerror(errno));
Simon Glass4161c412013-05-08 08:05:57 +0000602 goto err_system;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530603 }
604
Simon Glassd11b7002016-02-22 22:55:54 -0700605 /* Move the data so it is internal to the FIT, if needed */
606 ret = fit_import_data(params, tmpfile);
607 if (ret)
608 goto err_system;
609
Simon Glass802aa822014-06-02 22:04:53 -0600610 /*
611 * Set hashes for images in the blob. Unfortunately we may need more
612 * space in either FDT, so keep trying until we succeed.
613 *
614 * Note: this is pretty inefficient for signing, since we must
615 * calculate the signature every time. It would be better to calculate
616 * all the data and then store it in a separate step. However, this
617 * would be considerably more complex to implement. Generally a few
618 * steps of this loop is enough to sign with several keys.
619 */
620 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
621 ret = fit_add_file_data(params, size_inc, tmpfile);
622 if (!ret || ret != -ENOSPC)
623 break;
Simon Glassb4d8b092013-06-13 15:10:04 -0700624 }
625
Simon Glass802aa822014-06-02 22:04:53 -0600626 if (ret) {
Simon Glass817278a2013-06-13 15:10:07 -0700627 fprintf(stderr, "%s Can't add hashes to FIT blob\n",
628 params->cmdname);
Simon Glass802aa822014-06-02 22:04:53 -0600629 goto err_system;
Simon Glassb4d8b092013-06-13 15:10:04 -0700630 }
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530631
Simon Glassafd728c2016-02-22 22:55:53 -0700632 /* Move the data so it is external to the FIT, if requested */
633 if (params->external_data) {
634 ret = fit_extract_data(params, tmpfile);
635 if (ret)
636 goto err_system;
637 }
638
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530639 if (rename (tmpfile, params->imagefile) == -1) {
640 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
641 params->cmdname, tmpfile, params->imagefile,
642 strerror (errno));
643 unlink (tmpfile);
644 unlink (params->imagefile);
Simon Glass802aa822014-06-02 22:04:53 -0600645 return EXIT_FAILURE;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530646 }
Simon Glass802aa822014-06-02 22:04:53 -0600647 return EXIT_SUCCESS;
Simon Glass4161c412013-05-08 08:05:57 +0000648
Simon Glass4161c412013-05-08 08:05:57 +0000649err_system:
650 unlink(tmpfile);
651 return -1;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530652}
653
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200654/**
655 * fit_image_extract - extract a FIT component image
656 * @fit: pointer to the FIT format image header
657 * @image_noffset: offset of the component image node
658 * @file_name: name of the file to store the FIT sub-image
659 *
660 * returns:
661 * zero in case of success or a negative value if fail.
662 */
663static int fit_image_extract(
664 const void *fit,
665 int image_noffset,
666 const char *file_name)
667{
668 const void *file_data;
669 size_t file_size = 0;
670
671 /* get the "data" property of component at offset "image_noffset" */
672 fit_image_get_data(fit, image_noffset, &file_data, &file_size);
673
674 /* save the "file_data" into the file specified by "file_name" */
675 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
676}
677
678/**
679 * fit_extract_contents - retrieve a sub-image component from the FIT image
680 * @ptr: pointer to the FIT format image header
681 * @params: command line parameters
682 *
683 * returns:
684 * zero in case of success or a negative value if fail.
685 */
686static int fit_extract_contents(void *ptr, struct image_tool_params *params)
687{
688 int images_noffset;
689 int noffset;
690 int ndepth;
691 const void *fit = ptr;
692 int count = 0;
693 const char *p;
694
695 /* Indent string is defined in header image.h */
696 p = IMAGE_INDENT_STRING;
697
698 if (!fit_check_format(fit)) {
699 printf("Bad FIT image format\n");
700 return -1;
701 }
702
703 /* Find images parent node offset */
704 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
705 if (images_noffset < 0) {
706 printf("Can't find images parent node '%s' (%s)\n",
707 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
708 return -1;
709 }
710
711 /* Avoid any overrun */
712 count = fit_get_subimage_count(fit, images_noffset);
713 if ((params->pflag < 0) || (count <= params->pflag)) {
714 printf("No such component at '%d'\n", params->pflag);
715 return -1;
716 }
717
718 /* Process its subnodes, extract the desired component from image */
719 for (ndepth = 0, count = 0,
720 noffset = fdt_next_node(fit, images_noffset, &ndepth);
721 (noffset >= 0) && (ndepth > 0);
722 noffset = fdt_next_node(fit, noffset, &ndepth)) {
723 if (ndepth == 1) {
724 /*
725 * Direct child node of the images parent node,
726 * i.e. component image node.
727 */
728 if (params->pflag == count) {
729 printf("Extracted:\n%s Image %u (%s)\n", p,
730 count, fit_get_name(fit, noffset, NULL));
731
732 fit_image_print(fit, noffset, p);
733
734 return fit_image_extract(fit, noffset,
735 params->outfile);
736 }
737
738 count++;
739 }
740 }
741
742 return 0;
743}
744
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700745static int fit_check_params(struct image_tool_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530746{
Simon Glass88e31cb2016-02-22 22:55:51 -0700747 if (params->auto_its)
748 return 0;
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530749 return ((params->dflag && (params->fflag || params->lflag)) ||
750 (params->fflag && (params->dflag || params->lflag)) ||
751 (params->lflag && (params->dflag || params->fflag)));
752}
753
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200754U_BOOT_IMAGE_TYPE(
755 fitimage,
756 "FIT Image support",
757 sizeof(image_header_t),
758 (void *)&header,
759 fit_check_params,
760 fit_verify_header,
761 fit_print_contents,
762 NULL,
Guilherme Maciel Ferreira3c46bcd2015-01-15 02:54:42 -0200763 fit_extract_contents,
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200764 fit_check_image_types,
765 fit_handle_file,
766 NULL /* FIT images use DTB header */
767);