aboutsummaryrefslogtreecommitdiff
path: root/tools/mkimage.c
diff options
context:
space:
mode:
authorPali Rohár <pali@kernel.org>2022-01-14 18:34:43 +0100
committerTom Rini <trini@konsulko.com>2022-04-06 09:21:49 -0400
commitd3b1ca21e253547599a28002c1ea51348cb75c72 (patch)
tree57e5bbe8b481fcc8ae1f52fb33725cc54607f64a /tools/mkimage.c
parent3548903fc8eafa469f57e3dd11cf0046f2034699 (diff)
downloadu-boot-d3b1ca21e253547599a28002c1ea51348cb75c72.zip
u-boot-d3b1ca21e253547599a28002c1ea51348cb75c72.tar.gz
u-boot-d3b1ca21e253547599a28002c1ea51348cb75c72.tar.bz2
tools: mkimage: Call verify_header after writing image to disk
If image backend provides verify_header callback then call it after writing image to disk. This ensures that written image is correct. Signed-off-by: Pali Rohár <pali@kernel.org> Reviewed-by: Stefan Roese <sr@denx.de> Reviewed-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'tools/mkimage.c')
-rw-r--r--tools/mkimage.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/tools/mkimage.c b/tools/mkimage.c
index b46216f..be58e56 100644
--- a/tools/mkimage.c
+++ b/tools/mkimage.c
@@ -341,6 +341,44 @@ static void process_args(int argc, char **argv)
usage("Missing output filename");
}
+static void verify_image(const struct image_type_params *tparams)
+{
+ struct stat sbuf;
+ void *ptr;
+ int ifd;
+
+ ifd = open(params.imagefile, O_RDONLY | O_BINARY);
+ if (ifd < 0) {
+ fprintf(stderr, "%s: Can't open %s: %s\n",
+ params.cmdname, params.imagefile,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ if (fstat(ifd, &sbuf) < 0) {
+ fprintf(stderr, "%s: Can't stat %s: %s\n",
+ params.cmdname, params.imagefile, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ params.file_size = sbuf.st_size;
+
+ ptr = mmap(0, params.file_size, PROT_READ, MAP_SHARED, ifd, 0);
+ if (ptr == MAP_FAILED) {
+ fprintf(stderr, "%s: Can't map %s: %s\n",
+ params.cmdname, params.imagefile, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ if (tparams->verify_header((unsigned char *)ptr, params.file_size, &params) != 0) {
+ fprintf(stderr, "%s: Failed to verify header of %s\n",
+ params.cmdname, params.imagefile);
+ exit(EXIT_FAILURE);
+ }
+
+ (void)munmap(ptr, params.file_size);
+ (void)close(ifd);
+}
+
int main(int argc, char **argv)
{
int ifd = -1;
@@ -703,6 +741,9 @@ int main(int argc, char **argv)
exit (EXIT_FAILURE);
}
+ if (tparams->verify_header)
+ verify_image(tparams);
+
exit (EXIT_SUCCESS);
}