Use DM_HASH to perform hashing operations if supported.
Thus either SW or HW-assisted hashing could be leveraged.

Signed-off-by: Chia-Wei Wang <chiawei_w...@aspeedtech.com>
---
 lib/hash-checksum.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/lib/hash-checksum.c b/lib/hash-checksum.c
index d732ecc38f..f30873db38 100644
--- a/lib/hash-checksum.c
+++ b/lib/hash-checksum.c
@@ -10,6 +10,10 @@
 #include <linux/errno.h>
 #include <asm/unaligned.h>
 #include <hash.h>
+#if defined(CONFIG_DM_HASH)
+#include <dm.h>
+#include <u-boot/hash.h>
+#endif
 #else
 #include "fdt_host.h"
 #endif
@@ -20,6 +24,38 @@ int hash_calculate(const char *name,
                    const struct image_region region[],
                    int region_count, uint8_t *checksum)
 {
+#if !defined(USE_HOSTCC) && defined(CONFIG_DM_HASH)
+       int i, rc;
+       enum HASH_ALGO hash_algo;
+       struct udevice *dev;
+       void *ctx;
+
+       rc = uclass_get_device(UCLASS_HASH, 0, &dev);
+       if (rc) {
+               debug("failed to get hash device, rc=%d\n", rc);
+               return -1;
+       }
+
+       hash_algo = hash_algo_lookup_by_name(name);
+       if (hash_algo == HASH_ALGO_INVALID) {
+               debug("Unsupported hash algorithm\n");
+               return -1;
+       };
+
+       rc = hash_init(dev, hash_algo, &ctx);
+       if (rc)
+               return rc;
+
+       for (i = 0; i < region_count; i++) {
+               rc = hash_update(dev, ctx, region[i].data, region[i].size);
+               if (rc)
+                       return rc;
+       }
+
+       rc = hash_finish(dev, ctx, checksum);
+       if (rc)
+               return rc;
+#else
        struct hash_algo *algo;
        int ret = 0;
        void *ctx;
@@ -47,6 +83,7 @@ int hash_calculate(const char *name,
        ret = algo->hash_finish(algo, ctx, checksum, algo->digest_size);
        if (ret)
                return ret;
+#endif
 
        return 0;
 }
-- 
2.17.1

Reply via email to