Add support for verifying an image by use of the imd checksum.
If the checksum that is saved in the image and the one that is
calculated over the image differ, barebox_update aborts.

Signed-off-by: Steffen Trumtrar <s.trumt...@pengutronix.de>
---
 commands/barebox-update.c | 9 +++++++--
 common/bbu.c              | 6 ++++++
 include/bbu.h             | 1 +
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/commands/barebox-update.c b/commands/barebox-update.c
index 53af2a851137..10ea4dd915cc 100644
--- a/commands/barebox-update.c
+++ b/commands/barebox-update.c
@@ -23,6 +23,7 @@
 #include <errno.h>
 #include <bbu.h>
 #include <fs.h>
+#include <image-metadata.h>
 
 static void print_handlers_list(void)
 {
@@ -39,7 +40,7 @@ static int do_barebox_update(int argc, char *argv[])
        const char *name;
        const char *fmt;
 
-       while ((opt = getopt(argc, argv, "t:yf:ld:r")) > 0) {
+       while ((opt = getopt(argc, argv, "t:yf:ld:rV")) > 0) {
                switch (opt) {
                case 'd':
                        data.devicefile = optarg;
@@ -60,6 +61,9 @@ static int do_barebox_update(int argc, char *argv[])
                case 'r':
                        repair = 1;
                        break;
+               case 'V':
+                       data.flags |= BBU_FLAG_VERIFY;
+                       break;
                default:
                        return COMMAND_ERROR_USAGE;
                }
@@ -119,12 +123,13 @@ BAREBOX_CMD_HELP_OPT("-d DEVICE", "write image to DEVICE")
 BAREBOX_CMD_HELP_OPT("-r\t", "refresh or repair. Do not update, but repair an 
existing image")
 BAREBOX_CMD_HELP_OPT("-y\t", "autom. use 'yes' when asking confirmations")
 BAREBOX_CMD_HELP_OPT("-f LEVEL", "set force level")
+BAREBOX_CMD_HELP_OPT("-V\t", "Validate checksum of image")
 BAREBOX_CMD_HELP_END
 
 BAREBOX_CMD_START(barebox_update)
        .cmd            = do_barebox_update,
        BAREBOX_CMD_DESC("update barebox to persistent media")
-       BAREBOX_CMD_OPTS("[-ltdyfr] [IMAGE]")
+       BAREBOX_CMD_OPTS("[-ltdyfrV] [IMAGE]")
        BAREBOX_CMD_GROUP(CMD_GRP_MISC)
        BAREBOX_CMD_HELP(cmd_barebox_update_help)
 BAREBOX_CMD_END
diff --git a/common/bbu.c b/common/bbu.c
index 00bec32a860a..4bca4b598c4c 100644
--- a/common/bbu.c
+++ b/common/bbu.c
@@ -206,6 +206,12 @@ static int bbu_check_metadata(struct bbu_data *data)
        if (ret)
                return ret;
 
+       if (data->flags & BBU_FLAG_VERIFY) {
+               ret = imd_verify_crc32((void *)data->image, data->len);
+               if (ret)
+                       return ret;
+       }
+
        return 0;
 }
 
diff --git a/include/bbu.h b/include/bbu.h
index 9da6785d2e67..56c61093bba2 100644
--- a/include/bbu.h
+++ b/include/bbu.h
@@ -9,6 +9,7 @@
 struct bbu_data {
 #define BBU_FLAG_FORCE (1 << 0)
 #define BBU_FLAG_YES   (1 << 1)
+#define BBU_FLAG_VERIFY        (1 << 2)
        unsigned long flags;
        int force;
        const void *image;
-- 
2.24.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

Reply via email to