1 // SPDX-License-Identifier: GPL-2.0+
4 * DENX Software Engineering
7 * (C) Copyright 2008 Semihalf
9 * (C) Copyright 2000-2004
10 * DENX Software Engineering
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 #define COPYFILE_BUFSIZE (64 * 1024)
28 void fit_print_header(const void *fit, struct image_tool_params *params)
30 fit_print_contents(fit);
33 int fit_verify_header(unsigned char *ptr, int image_size,
34 struct image_tool_params *params)
38 if (fdt_check_header(ptr) != EXIT_SUCCESS)
41 ret = fit_check_format(ptr, IMAGE_SIZE_INVAL);
43 if (ret != -EADDRNOTAVAIL)
45 fprintf(stderr, "Image contains unit addresses @, this will break signing\n");
51 int fit_check_image_types(uint8_t type)
53 if (type == IH_TYPE_FLATDT)
59 int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
60 void **blobp, struct stat *sbuf, bool delete_on_error,
66 /* Load FIT blob into memory (we need to write hashes/signatures) */
67 fd = open(fname, (read_only ? O_RDONLY : O_RDWR) | O_BINARY);
70 fprintf(stderr, "%s: Can't open %s: %s\n",
71 cmdname, fname, strerror(errno));
75 if (fstat(fd, sbuf) < 0) {
76 fprintf(stderr, "%s: Can't stat %s: %s\n",
77 cmdname, fname, strerror(errno));
82 sbuf->st_size += size_inc;
83 if (ftruncate(fd, sbuf->st_size)) {
84 fprintf(stderr, "%s: Can't expand %s: %s\n",
85 cmdname, fname, strerror(errno));
91 ptr = mmap(0, sbuf->st_size,
92 (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED,
94 if ((ptr == MAP_FAILED) || (errno != 0)) {
95 fprintf(stderr, "%s: Can't read %s: %s\n",
96 cmdname, fname, strerror(errno));
100 /* check if ptr has a valid blob */
101 if (fdt_check_header(ptr)) {
102 fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
106 /* expand if needed */
110 ret = fdt_open_into(ptr, ptr, sbuf->st_size);
112 fprintf(stderr, "%s: Cannot expand FDT: %s\n",
113 cmdname, fdt_strerror(ret));
130 int copyfile(const char *src, const char *dst)
132 int fd_src = -1, fd_dst = -1;
138 fd_src = open(src, O_RDONLY);
140 printf("Can't open file %s (%s)\n", src, strerror(errno));
144 fd_dst = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0666);
146 printf("Can't open file %s (%s)\n", dst, strerror(errno));
150 buf = calloc(1, COPYFILE_BUFSIZE);
152 printf("Can't allocate buffer to copy file\n");
157 size = read(fd_src, buf, COPYFILE_BUFSIZE);
159 printf("Can't read file %s\n", src);
166 size = write(fd_dst, buf, count);
168 printf("Can't write file %s\n", dst);
186 void summary_show(struct image_summary *summary, const char *imagefile,
189 if (summary->sig_offset) {
190 printf("Signature written to '%s', node '%s'\n", imagefile,
193 printf("Public key written to '%s', node '%s'\n",
194 keydest, summary->keydest_path);