zck: add option to select chunk hash
authorStefano Babic <sbabic@denx.de>
Fri, 10 Sep 2021 10:08:47 +0000 (12:08 +0200)
committerStefano Babic <sbabic@denx.de>
Tue, 12 Oct 2021 08:58:57 +0000 (10:58 +0200)
Hash is set to sha512_128, add an option to choose one of the supported
hashes for chunks.

Signed-off-by: Stefano Babic <sbabic@denx.de>
src/zck.c

index ac33a306a35d4f42c88b4ffaa459766950ec583d..b99de29a9a807b41f92affb6b77adef3bfd7c6af 100644 (file)
--- a/src/zck.c
+++ b/src/zck.c
@@ -55,6 +55,8 @@ static struct argp_option options[] = {
      "Set zstd compression dictionary to FILE"},
     {"manual-chunk",       'm', 0,           0,
      "Don't do any automatic chunking (implies -s)"},
+    {"chunk-hash-type",     'h', "HASH",     0,
+     "Set hash type to one of sha256, sha512, sha512_128"},
     {"uncompressed",       'u', 0,           0,
      "Add extension in header for uncompressed data"},
     {"version",            'V', 0,           0, "Show program version"},
@@ -75,6 +77,7 @@ struct arguments {
   char *compression_format;
   bool exit;
   bool uncompressed;
+  zck_hash chunk_hashtype;
 };
 
 static error_t parse_opt (int key, char *arg, struct argp_state *state) {
@@ -94,6 +97,20 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) {
             break;
         case 'm':
             arguments->manual_chunk = true;
+            break;
+        case 'h':
+            if (!strcmp(arg, "sha256"))
+                arguments->chunk_hashtype = ZCK_HASH_SHA256;
+            else if (!strcmp(arg, "sha512"))
+                arguments->chunk_hashtype = ZCK_HASH_SHA512;
+            else if (!strcmp(arg, "sha512_128"))
+                arguments->chunk_hashtype = ZCK_HASH_SHA512_128;
+            else {
+                dprintf(STDERR_FILENO, "Wrong value for chunk hashtype. \n "
+                        "It should be one of sha1|sha256|sha512|sha512_128 instead of %s\n", arg);
+                return -EINVAL;
+            }
+
             break;
         case 'o':
             arguments->output = arg;
@@ -141,6 +158,7 @@ int main (int argc, char *argv[]) {
 
     /* Defaults */
     arguments.log_level = ZCK_LOG_ERROR;
+    arguments.chunk_hashtype = ZCK_HASH_UNKNOWN;
     arguments.compression_format = "zstd";
 
     int retval = argp_parse(&argp, argc, argv, 0, 0, &arguments);
@@ -257,6 +275,12 @@ int main (int argc, char *argv[]) {
             exit(1);
         }
     }
+    if (arguments.chunk_hashtype != ZCK_HASH_UNKNOWN) {
+        if(!zck_set_ioption(zck, ZCK_HASH_CHUNK_TYPE, arguments.chunk_hashtype)) {
+            dprintf(STDERR_FILENO, "Unable to set hash type %s\n", zck_get_error(zck));
+            exit(1);
+        }
+    }
     char *data;
     int in_fd = open(arguments.args[0], O_RDONLY);
     off_t in_size = 0;