]> git.ipfire.org Git - people/ms/u-boot.git/blame - tools/fit_image.c
Use C locale when setting CC_VERSION_STRING and LD_VERSION_STRING.
[people/ms/u-boot.git] / tools / fit_image.c
CommitLineData
89a4d6b1
PW
1/*
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 *
1a459660 14 * SPDX-License-Identifier: GPL-2.0+
89a4d6b1
PW
15 */
16
f86ed6a8 17#include "imagetool.h"
6bf4ca07 18#include "fit_common.h"
89a4d6b1
PW
19#include "mkimage.h"
20#include <image.h>
8e35bb07
SG
21#include <stdarg.h>
22#include <version.h>
89a4d6b1
PW
23#include <u-boot/crc.h>
24
25static image_header_t header;
26
a9468115
SG
27static 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
8e35bb07
SG
75/**
76 * 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{
fb4cce0f 80 struct content_info *cont;
8e35bb07
SG
81 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;
fb4cce0f
SG
88 for (cont = params->content_head; cont; cont = cont->next) {
89 size = imagetool_get_filesize(params, cont->fname);
90 if (size < 0)
91 return -1;
8e35bb07 92
fb4cce0f
SG
93 /* Add space for properties */
94 total_size += size + 300;
95 }
8e35bb07
SG
96
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)
3bd3a54a 126 goto err;
8e35bb07
SG
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 }
3bd3a54a 133 close(fd);
8e35bb07
SG
134
135 return 0;
136err:
137 close(fd);
138 return -1;
139}
140
141static int fdt_property_strf(void *fdt, const char *name, const char *fmt, ...)
142{
143 char str[100];
144 va_list ptr;
145
146 va_start(ptr, fmt);
147 vsnprintf(str, sizeof(str), fmt, ptr);
148 va_end(ptr);
149 return fdt_property_string(fdt, name, str);
150}
151
fb4cce0f
SG
152static void get_basename(char *str, int size, const char *fname)
153{
154 const char *p, *start, *end;
155 int len;
156
157 /*
158 * Use the base name as the 'name' field. So for example:
159 *
160 * "arch/arm/dts/sun7i-a20-bananapro.dtb"
161 * becomes "sun7i-a20-bananapro"
162 */
163 p = strrchr(fname, '/');
164 start = p ? p + 1 : fname;
165 p = strrchr(fname, '.');
166 end = p ? p : fname + strlen(fname);
167 len = end - start;
168 if (len >= size)
169 len = size - 1;
170 memcpy(str, start, len);
171 str[len] = '\0';
172}
173
8e35bb07
SG
174/**
175 * fit_write_images() - Write out a list of images to the FIT
176 *
fb4cce0f
SG
177 * We always include the main image (params->datafile). If there are device
178 * tree files, we include an fdt@ node for each of those too.
8e35bb07
SG
179 */
180static int fit_write_images(struct image_tool_params *params, char *fdt)
181{
fb4cce0f 182 struct content_info *cont;
8e35bb07
SG
183 const char *typename;
184 char str[100];
fb4cce0f 185 int upto;
8e35bb07
SG
186 int ret;
187
188 fdt_begin_node(fdt, "images");
189
190 /* First the main image */
191 typename = genimg_get_type_short_name(params->fit_image_type);
192 snprintf(str, sizeof(str), "%s@1", typename);
193 fdt_begin_node(fdt, str);
194 fdt_property_string(fdt, "description", params->imagename);
195 fdt_property_string(fdt, "type", typename);
196 fdt_property_string(fdt, "arch", genimg_get_arch_name(params->arch));
197 fdt_property_string(fdt, "os", genimg_get_os_short_name(params->os));
198 fdt_property_string(fdt, "compression",
199 genimg_get_comp_short_name(params->comp));
200 fdt_property_u32(fdt, "load", params->addr);
201 fdt_property_u32(fdt, "entry", params->ep);
202
203 /*
204 * Put data last since it is large. SPL may only load the first part
205 * of the DT, so this way it can access all the above fields.
206 */
207 ret = fdt_property_file(params, fdt, "data", params->datafile);
208 if (ret)
209 return ret;
210 fdt_end_node(fdt);
211
fb4cce0f
SG
212 /* Now the device tree files if available */
213 upto = 0;
214 for (cont = params->content_head; cont; cont = cont->next) {
215 if (cont->type != IH_TYPE_FLATDT)
216 continue;
217 snprintf(str, sizeof(str), "%s@%d", FIT_FDT_PROP, ++upto);
218 fdt_begin_node(fdt, str);
219
220 get_basename(str, sizeof(str), cont->fname);
221 fdt_property_string(fdt, "description", str);
222 ret = fdt_property_file(params, fdt, "data", cont->fname);
223 if (ret)
224 return ret;
225 fdt_property_string(fdt, "type", typename);
226 fdt_property_string(fdt, "arch",
227 genimg_get_arch_short_name(params->arch));
228 fdt_property_string(fdt, "compression",
229 genimg_get_comp_short_name(IH_COMP_NONE));
230 fdt_end_node(fdt);
231 }
232
8e35bb07
SG
233 fdt_end_node(fdt);
234
235 return 0;
236}
237
238/**
239 * fit_write_configs() - Write out a list of configurations to the FIT
240 *
fb4cce0f
SG
241 * If there are device tree files, we include a configuration for each, which
242 * selects the main image (params->datafile) and its corresponding device
243 * tree file.
244 *
245 * Otherwise we just create a configuration with the main image in it.
8e35bb07
SG
246 */
247static void fit_write_configs(struct image_tool_params *params, char *fdt)
248{
fb4cce0f 249 struct content_info *cont;
8e35bb07
SG
250 const char *typename;
251 char str[100];
fb4cce0f 252 int upto;
8e35bb07
SG
253
254 fdt_begin_node(fdt, "configurations");
255 fdt_property_string(fdt, "default", "conf@1");
256
fb4cce0f
SG
257 upto = 0;
258 for (cont = params->content_head; cont; cont = cont->next) {
259 if (cont->type != IH_TYPE_FLATDT)
260 continue;
261 typename = genimg_get_type_short_name(cont->type);
262 snprintf(str, sizeof(str), "conf@%d", ++upto);
263 fdt_begin_node(fdt, str);
264
265 get_basename(str, sizeof(str), cont->fname);
266 fdt_property_string(fdt, "description", str);
267
268 typename = genimg_get_type_short_name(params->fit_image_type);
269 snprintf(str, sizeof(str), "%s@1", typename);
270 fdt_property_string(fdt, typename, str);
271
272 snprintf(str, sizeof(str), FIT_FDT_PROP "@%d", upto);
273 fdt_property_string(fdt, FIT_FDT_PROP, str);
274 fdt_end_node(fdt);
275 }
276 if (!upto) {
277 fdt_begin_node(fdt, "conf@1");
278 typename = genimg_get_type_short_name(params->fit_image_type);
279 snprintf(str, sizeof(str), "%s@1", typename);
280 fdt_property_string(fdt, typename, str);
281 fdt_end_node(fdt);
282 }
8e35bb07
SG
283
284 fdt_end_node(fdt);
285}
286
287static int fit_build_fdt(struct image_tool_params *params, char *fdt, int size)
288{
289 int ret;
290
291 ret = fdt_create(fdt, size);
292 if (ret)
293 return ret;
294 fdt_finish_reservemap(fdt);
295 fdt_begin_node(fdt, "");
296 fdt_property_strf(fdt, "description",
297 "%s image with one or more FDT blobs",
298 genimg_get_type_name(params->fit_image_type));
299 fdt_property_strf(fdt, "creator", "U-Boot mkimage %s", PLAIN_VERSION);
300 fdt_property_u32(fdt, "#address-cells", 1);
301 ret = fit_write_images(params, fdt);
302 if (ret)
303 return ret;
304 fit_write_configs(params, fdt);
305 fdt_end_node(fdt);
306 ret = fdt_finish(fdt);
307 if (ret)
308 return ret;
309
310 return fdt_totalsize(fdt);
311}
312
313static int fit_build(struct image_tool_params *params, const char *fname)
314{
315 char *buf;
316 int size;
317 int ret;
318 int fd;
319
320 size = fit_calc_size(params);
321 if (size < 0)
322 return -1;
323 buf = malloc(size);
324 if (!buf) {
325 fprintf(stderr, "%s: Out of memory (%d bytes)\n",
326 params->cmdname, size);
327 return -1;
328 }
329 ret = fit_build_fdt(params, buf, size);
330 if (ret < 0) {
331 fprintf(stderr, "%s: Failed to build FIT image\n",
332 params->cmdname);
7b0bbd88 333 goto err_buf;
8e35bb07
SG
334 }
335 size = ret;
336 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
337 if (fd < 0) {
338 fprintf(stderr, "%s: Can't open %s: %s\n",
339 params->cmdname, fname, strerror(errno));
340 goto err;
341 }
342 ret = write(fd, buf, size);
343 if (ret != size) {
344 fprintf(stderr, "%s: Can't write %s: %s\n",
345 params->cmdname, fname, strerror(errno));
8e35bb07
SG
346 goto err;
347 }
348 close(fd);
7b0bbd88 349 free(buf);
8e35bb07
SG
350
351 return 0;
352err:
7b0bbd88
SG
353 close(fd);
354err_buf:
8e35bb07
SG
355 free(buf);
356 return -1;
357}
358
722ebc8f
SG
359/**
360 * fit_extract_data() - Move all data outside the FIT
361 *
362 * This takes a normal FIT file and removes all the 'data' properties from it.
363 * The data is placed in an area after the FIT so that it can be accessed
364 * using an offset into that area. The 'data' properties turn into
365 * 'data-offset' properties.
366 *
367 * This function cannot cope with FITs with 'data-offset' properties. All
368 * data must be in 'data' properties on entry.
369 */
370static int fit_extract_data(struct image_tool_params *params, const char *fname)
371{
372 void *buf;
373 int buf_ptr;
374 int fit_size, new_size;
375 int fd;
376 struct stat sbuf;
377 void *fdt;
378 int ret;
379 int images;
380 int node;
381
382 fd = mmap_fdt(params->cmdname, fname, 0, &fdt, &sbuf, false);
383 if (fd < 0)
384 return -EIO;
385 fit_size = fdt_totalsize(fdt);
386
387 /* Allocate space to hold the image data we will extract */
388 buf = malloc(fit_size);
389 if (!buf) {
390 ret = -ENOMEM;
b97d71e2 391 goto err_munmap;
722ebc8f
SG
392 }
393 buf_ptr = 0;
394
395 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
396 if (images < 0) {
397 debug("%s: Cannot find /images node: %d\n", __func__, images);
398 ret = -EINVAL;
b97d71e2 399 goto err_munmap;
722ebc8f
SG
400 }
401
402 for (node = fdt_first_subnode(fdt, images);
403 node >= 0;
404 node = fdt_next_subnode(fdt, node)) {
405 const char *data;
406 int len;
407
408 data = fdt_getprop(fdt, node, "data", &len);
409 if (!data)
410 continue;
411 memcpy(buf + buf_ptr, data, len);
412 debug("Extracting data size %x\n", len);
413
414 ret = fdt_delprop(fdt, node, "data");
415 if (ret) {
416 ret = -EPERM;
b97d71e2 417 goto err_munmap;
722ebc8f 418 }
f8f9107d
TR
419 if (params->external_offset > 0) {
420 /* An external offset positions the data absolutely. */
421 fdt_setprop_u32(fdt, node, "data-position",
422 params->external_offset + buf_ptr);
423 } else {
424 fdt_setprop_u32(fdt, node, "data-offset", buf_ptr);
425 }
722ebc8f
SG
426 fdt_setprop_u32(fdt, node, "data-size", len);
427
428 buf_ptr += (len + 3) & ~3;
429 }
430
431 /* Pack the FDT and place the data after it */
432 fdt_pack(fdt);
433
434 debug("Size reduced from %x to %x\n", fit_size, fdt_totalsize(fdt));
435 debug("External data size %x\n", buf_ptr);
436 new_size = fdt_totalsize(fdt);
437 new_size = (new_size + 3) & ~3;
438 munmap(fdt, sbuf.st_size);
439
440 if (ftruncate(fd, new_size)) {
441 debug("%s: Failed to truncate file: %s\n", __func__,
442 strerror(errno));
443 ret = -EIO;
444 goto err;
445 }
f8f9107d
TR
446
447 /* Check if an offset for the external data was set. */
448 if (params->external_offset > 0) {
449 if (params->external_offset < new_size) {
450 debug("External offset %x overlaps FIT length %x",
451 params->external_offset, new_size);
452 ret = -EINVAL;
453 goto err;
454 }
455 new_size = params->external_offset;
456 }
722ebc8f
SG
457 if (lseek(fd, new_size, SEEK_SET) < 0) {
458 debug("%s: Failed to seek to end of file: %s\n", __func__,
459 strerror(errno));
460 ret = -EIO;
461 goto err;
462 }
463 if (write(fd, buf, buf_ptr) != buf_ptr) {
464 debug("%s: Failed to write external data to file %s\n",
465 __func__, strerror(errno));
466 ret = -EIO;
467 goto err;
468 }
b97d71e2
SG
469 close(fd);
470 return 0;
722ebc8f 471
b97d71e2
SG
472err_munmap:
473 munmap(fdt, sbuf.st_size);
722ebc8f 474err:
21c2975a
SG
475 if (buf)
476 free(buf);
722ebc8f
SG
477 close(fd);
478 return ret;
479}
480
529fd188
SG
481static int fit_import_data(struct image_tool_params *params, const char *fname)
482{
483 void *fdt, *old_fdt;
484 int fit_size, new_size, size, data_base;
485 int fd;
486 struct stat sbuf;
487 int ret;
488 int images;
489 int node;
490
491 fd = mmap_fdt(params->cmdname, fname, 0, &old_fdt, &sbuf, false);
492 if (fd < 0)
493 return -EIO;
494 fit_size = fdt_totalsize(old_fdt);
495 data_base = (fit_size + 3) & ~3;
496
497 /* Allocate space to hold the new FIT */
498 size = sbuf.st_size + 16384;
499 fdt = malloc(size);
500 if (!fdt) {
501 fprintf(stderr, "%s: Failed to allocate memory (%d bytes)\n",
502 __func__, size);
503 ret = -ENOMEM;
504 goto err;
505 }
506 ret = fdt_open_into(old_fdt, fdt, size);
507 if (ret) {
508 debug("%s: Failed to expand FIT: %s\n", __func__,
509 fdt_strerror(errno));
510 ret = -EINVAL;
511 goto err;
512 }
513
514 images = fdt_path_offset(fdt, FIT_IMAGES_PATH);
515 if (images < 0) {
516 debug("%s: Cannot find /images node: %d\n", __func__, images);
517 ret = -EINVAL;
518 goto err;
519 }
520
521 for (node = fdt_first_subnode(fdt, images);
522 node >= 0;
523 node = fdt_next_subnode(fdt, node)) {
524 int buf_ptr;
525 int len;
526
527 buf_ptr = fdtdec_get_int(fdt, node, "data-offset", -1);
528 len = fdtdec_get_int(fdt, node, "data-size", -1);
529 if (buf_ptr == -1 || len == -1)
530 continue;
531 debug("Importing data size %x\n", len);
532
533 ret = fdt_setprop(fdt, node, "data", fdt + data_base + buf_ptr,
534 len);
535 if (ret) {
536 debug("%s: Failed to write property: %s\n", __func__,
537 fdt_strerror(ret));
538 ret = -EINVAL;
539 goto err;
540 }
541 }
542
9cff2d1e 543 munmap(old_fdt, sbuf.st_size);
529fd188
SG
544 close(fd);
545
546 /* Pack the FDT and place the data after it */
547 fdt_pack(fdt);
548
549 new_size = fdt_totalsize(fdt);
550 debug("Size expanded from %x to %x\n", fit_size, new_size);
551
552 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666);
553 if (fd < 0) {
554 fprintf(stderr, "%s: Can't open %s: %s\n",
555 params->cmdname, fname, strerror(errno));
5b15a259
SG
556 free(fdt);
557 return -EIO;
529fd188
SG
558 }
559 if (write(fd, fdt, new_size) != new_size) {
560 debug("%s: Failed to write external data to file %s\n",
561 __func__, strerror(errno));
562 ret = -EIO;
563 goto err;
564 }
529fd188
SG
565
566 ret = 0;
567
568err:
6e0ffce6 569 free(fdt);
529fd188
SG
570 close(fd);
571 return ret;
572}
573
89a4d6b1
PW
574/**
575 * fit_handle_file - main FIT file processing function
576 *
577 * fit_handle_file() runs dtc to convert .its to .itb, includes
578 * binary data, updates timestamp property and calculates hashes.
579 *
580 * datafile - .its file
581 * imagefile - .itb file
582 *
583 * returns:
584 * only on success, otherwise calls exit (EXIT_FAILURE);
585 */
f86ed6a8 586static int fit_handle_file(struct image_tool_params *params)
89a4d6b1
PW
587{
588 char tmpfile[MKIMAGE_MAX_TMPFILE_LEN];
589 char cmd[MKIMAGE_MAX_DTC_CMDLINE_LEN];
a9468115
SG
590 size_t size_inc;
591 int ret;
89a4d6b1
PW
592
593 /* Flattened Image Tree (FIT) format handling */
594 debug ("FIT format handling\n");
595
596 /* call dtc to include binary properties into the tmp file */
597 if (strlen (params->imagefile) +
598 strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
599 fprintf (stderr, "%s: Image file name (%s) too long, "
600 "can't create tmpfile",
601 params->imagefile, params->cmdname);
602 return (EXIT_FAILURE);
603 }
604 sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
605
95d77b44 606 /* We either compile the source file, or use the existing FIT image */
8e35bb07
SG
607 if (params->auto_its) {
608 if (fit_build(params, tmpfile)) {
609 fprintf(stderr, "%s: failed to build FIT\n",
610 params->cmdname);
611 return EXIT_FAILURE;
612 }
613 *cmd = '\0';
614 } else if (params->datafile) {
95d77b44
SG
615 /* dtc -I dts -O dtb -p 500 datafile > tmpfile */
616 snprintf(cmd, sizeof(cmd), "%s %s %s > %s",
617 MKIMAGE_DTC, params->dtc, params->datafile, tmpfile);
618 debug("Trying to execute \"%s\"\n", cmd);
619 } else {
620 snprintf(cmd, sizeof(cmd), "cp %s %s",
621 params->imagefile, tmpfile);
622 }
8e35bb07 623 if (*cmd && system(cmd) == -1) {
89a4d6b1
PW
624 fprintf (stderr, "%s: system(%s) failed: %s\n",
625 params->cmdname, cmd, strerror(errno));
aa6d6db4 626 goto err_system;
89a4d6b1
PW
627 }
628
529fd188
SG
629 /* Move the data so it is internal to the FIT, if needed */
630 ret = fit_import_data(params, tmpfile);
631 if (ret)
632 goto err_system;
633
a9468115
SG
634 /*
635 * Set hashes for images in the blob. Unfortunately we may need more
636 * space in either FDT, so keep trying until we succeed.
637 *
638 * Note: this is pretty inefficient for signing, since we must
639 * calculate the signature every time. It would be better to calculate
640 * all the data and then store it in a separate step. However, this
641 * would be considerably more complex to implement. Generally a few
642 * steps of this loop is enough to sign with several keys.
643 */
644 for (size_inc = 0; size_inc < 64 * 1024; size_inc += 1024) {
645 ret = fit_add_file_data(params, size_inc, tmpfile);
646 if (!ret || ret != -ENOSPC)
647 break;
e29495d3
SG
648 }
649
a9468115 650 if (ret) {
399c744b
SG
651 fprintf(stderr, "%s Can't add hashes to FIT blob\n",
652 params->cmdname);
a9468115 653 goto err_system;
e29495d3 654 }
89a4d6b1 655
722ebc8f
SG
656 /* Move the data so it is external to the FIT, if requested */
657 if (params->external_data) {
658 ret = fit_extract_data(params, tmpfile);
659 if (ret)
660 goto err_system;
661 }
662
89a4d6b1
PW
663 if (rename (tmpfile, params->imagefile) == -1) {
664 fprintf (stderr, "%s: Can't rename %s to %s: %s\n",
665 params->cmdname, tmpfile, params->imagefile,
666 strerror (errno));
667 unlink (tmpfile);
668 unlink (params->imagefile);
a9468115 669 return EXIT_FAILURE;
89a4d6b1 670 }
a9468115 671 return EXIT_SUCCESS;
aa6d6db4 672
aa6d6db4
SG
673err_system:
674 unlink(tmpfile);
675 return -1;
89a4d6b1
PW
676}
677
39931f96
GMF
678/**
679 * fit_image_extract - extract a FIT component image
680 * @fit: pointer to the FIT format image header
681 * @image_noffset: offset of the component image node
682 * @file_name: name of the file to store the FIT sub-image
683 *
684 * returns:
685 * zero in case of success or a negative value if fail.
686 */
687static int fit_image_extract(
688 const void *fit,
689 int image_noffset,
690 const char *file_name)
691{
692 const void *file_data;
693 size_t file_size = 0;
694
695 /* get the "data" property of component at offset "image_noffset" */
696 fit_image_get_data(fit, image_noffset, &file_data, &file_size);
697
698 /* save the "file_data" into the file specified by "file_name" */
699 return imagetool_save_subimage(file_name, (ulong) file_data, file_size);
700}
701
702/**
703 * fit_extract_contents - retrieve a sub-image component from the FIT image
704 * @ptr: pointer to the FIT format image header
705 * @params: command line parameters
706 *
707 * returns:
708 * zero in case of success or a negative value if fail.
709 */
710static int fit_extract_contents(void *ptr, struct image_tool_params *params)
711{
712 int images_noffset;
713 int noffset;
714 int ndepth;
715 const void *fit = ptr;
716 int count = 0;
717 const char *p;
718
719 /* Indent string is defined in header image.h */
720 p = IMAGE_INDENT_STRING;
721
722 if (!fit_check_format(fit)) {
723 printf("Bad FIT image format\n");
724 return -1;
725 }
726
727 /* Find images parent node offset */
728 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
729 if (images_noffset < 0) {
730 printf("Can't find images parent node '%s' (%s)\n",
731 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
732 return -1;
733 }
734
735 /* Avoid any overrun */
736 count = fit_get_subimage_count(fit, images_noffset);
737 if ((params->pflag < 0) || (count <= params->pflag)) {
738 printf("No such component at '%d'\n", params->pflag);
739 return -1;
740 }
741
742 /* Process its subnodes, extract the desired component from image */
743 for (ndepth = 0, count = 0,
744 noffset = fdt_next_node(fit, images_noffset, &ndepth);
745 (noffset >= 0) && (ndepth > 0);
746 noffset = fdt_next_node(fit, noffset, &ndepth)) {
747 if (ndepth == 1) {
748 /*
749 * Direct child node of the images parent node,
750 * i.e. component image node.
751 */
752 if (params->pflag == count) {
753 printf("Extracted:\n%s Image %u (%s)\n", p,
754 count, fit_get_name(fit, noffset, NULL));
755
756 fit_image_print(fit, noffset, p);
757
758 return fit_image_extract(fit, noffset,
759 params->outfile);
760 }
761
762 count++;
763 }
764 }
765
766 return 0;
767}
768
f86ed6a8 769static int fit_check_params(struct image_tool_params *params)
89a4d6b1 770{
8e35bb07
SG
771 if (params->auto_its)
772 return 0;
89a4d6b1
PW
773 return ((params->dflag && (params->fflag || params->lflag)) ||
774 (params->fflag && (params->dflag || params->lflag)) ||
775 (params->lflag && (params->dflag || params->fflag)));
776}
777
a93648d1
GMF
778U_BOOT_IMAGE_TYPE(
779 fitimage,
780 "FIT Image support",
781 sizeof(image_header_t),
782 (void *)&header,
783 fit_check_params,
784 fit_verify_header,
785 fit_print_contents,
786 NULL,
39931f96 787 fit_extract_contents,
a93648d1
GMF
788 fit_check_image_types,
789 fit_handle_file,
790 NULL /* FIT images use DTB header */
791);