All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mkimage: Call verify_header after writing image to disk
@ 2022-01-13 14:42 Pali Rohár
  2022-01-13 19:30 ` Simon Glass
  0 siblings, 1 reply; 21+ messages in thread
From: Pali Rohár @ 2022-01-13 14:42 UTC (permalink / raw)
  To: Simon Glass, Alexandru Gagniuc, Yann Dirson, Stefan Roese,
	Marek Behún
  Cc: u-boot

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>
---
 tools/mkimage.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/tools/mkimage.c b/tools/mkimage.c
index fbe883ce3620..2d282630787c 100644
--- a/tools/mkimage.c
+++ b/tools/mkimage.c
@@ -698,6 +698,40 @@ int main(int argc, char **argv)
 		exit (EXIT_FAILURE);
 	}
 
+	if (tparams->verify_header) {
+		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;
+
+		map_len = sbuf.st_size;
+		ptr = mmap(0, sbuf.st_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, sbuf.st_size, &params) != 0) {
+			fprintf(stderr, "%s: Failed to verify header of %s\n",
+				params.cmdname, params.imagefile);
+			exit(EXIT_FAILURE);
+		}
+
+		(void)munmap((void *)ptr, map_len);
+		close(ifd);
+	}
+
 	exit (EXIT_SUCCESS);
 }
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2022-04-06 15:55 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-13 14:42 [PATCH] mkimage: Call verify_header after writing image to disk Pali Rohár
2022-01-13 19:30 ` Simon Glass
2022-01-14 17:34   ` [PATCH v2] tools: " Pali Rohár
2022-01-15  9:46     ` Stefan Roese
2022-01-15 18:01     ` Simon Glass
2022-01-21 21:21     ` Tom Rini
2022-01-22  1:44       ` Pali Rohár
2022-01-22  2:15         ` Tom Rini
2022-01-22 16:31           ` Pali Rohár
2022-01-22 16:34             ` Pali Rohár
2022-01-22 16:35             ` Tom Rini
2022-02-02  9:06               ` Priyanka Jain
2022-02-15 19:52                 ` Pali Rohár
2022-02-16  9:33                   ` Priyanka Jain
2022-02-16 14:17                   ` Z.Q. Hou
2022-03-06 11:50       ` Pali Rohár
2022-03-06 13:58         ` Tom Rini
2022-03-08 13:42     ` Tom Rini
2022-03-14  1:51       ` 陈健洪
2022-03-14  1:54       ` 陈健洪
2022-04-06 15:55     ` Tom Rini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.