1 // SPDX-License-Identifier: GPL-2.0+
4 * DENX Software Engineering
5 * Heiko Schocher <hs@denx.de>
7 * (C) Copyright 2008 Semihalf
9 * (C) Copyright 2000-2004
10 * DENX Software Engineering
11 * Wolfgang Denk, wd@denx.de
13 * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
14 * FIT image specific code abstracted from mkimage.c
15 * some functions added to address abstraction
17 * All rights reserved.
20 #include "imagetool.h"
22 #include "fit_common.h"
24 #include <u-boot/crc.h>
26 void fit_print_header(const void *fit, struct image_tool_params *params)
28 fit_print_contents(fit);
31 int fit_verify_header(unsigned char *ptr, int image_size,
32 struct image_tool_params *params)
36 if (fdt_check_header(ptr) != EXIT_SUCCESS)
39 ret = fit_check_format(ptr, IMAGE_SIZE_INVAL);
41 if (ret != -EADDRNOTAVAIL)
43 fprintf(stderr, "Image contains unit addresses @, this will break signing\n");
49 int fit_check_image_types(uint8_t type)
51 if (type == IH_TYPE_FLATDT)
57 int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
58 void **blobp, struct stat *sbuf, bool delete_on_error,
64 /* Load FIT blob into memory (we need to write hashes/signatures) */
65 fd = open(fname, (read_only ? O_RDONLY : O_RDWR) | O_BINARY);
68 fprintf(stderr, "%s: Can't open %s: %s\n",
69 cmdname, fname, strerror(errno));
73 if (fstat(fd, sbuf) < 0) {
74 fprintf(stderr, "%s: Can't stat %s: %s\n",
75 cmdname, fname, strerror(errno));
80 sbuf->st_size += size_inc;
81 if (ftruncate(fd, sbuf->st_size)) {
82 fprintf(stderr, "%s: Can't expand %s: %s\n",
83 cmdname, fname, strerror(errno));
89 ptr = mmap(0, sbuf->st_size,
90 (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED,
92 if ((ptr == MAP_FAILED) || (errno != 0)) {
93 fprintf(stderr, "%s: Can't read %s: %s\n",
94 cmdname, fname, strerror(errno));
98 /* check if ptr has a valid blob */
99 if (fdt_check_header(ptr)) {
100 fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
104 /* expand if needed */
108 ret = fdt_open_into(ptr, ptr, sbuf->st_size);
110 fprintf(stderr, "%s: Cannot expand FDT: %s\n",
111 cmdname, fdt_strerror(ret));
128 int copyfile(const char *src, const char *dst)
130 int fd_src = -1, fd_dst = -1;
136 fd_src = open(src, O_RDONLY);
138 printf("Can't open file %s (%s)\n", src, strerror(errno));
142 fd_dst = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0666);
144 printf("Can't open file %s (%s)\n", dst, strerror(errno));
148 buf = calloc(1, 512);
150 printf("Can't allocate buffer to copy file\n");
155 size = read(fd_src, buf, 512);
157 printf("Can't read file %s\n", src);
164 size = write(fd_dst, buf, count);
166 printf("Can't write file %s\n", dst);
184 void summary_show(struct image_summary *summary, const char *imagefile,
187 if (summary->sig_offset) {
188 printf("Signature written to '%s', node '%s'\n", imagefile,
191 printf("Public key written to '%s', node '%s'\n",
192 keydest, summary->keydest_path);