Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4D90C433EF for ; Thu, 16 Sep 2021 06:39:26 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9506A6112E for ; Thu, 16 Sep 2021 06:39:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 9506A6112E Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=aspeedtech.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 87B6A82BC3; Thu, 16 Sep 2021 08:39:22 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=aspeedtech.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id D0C7B82D52; Thu, 16 Sep 2021 08:39:20 +0200 (CEST) Received: from twspam01.aspeedtech.com (twspam01.aspeedtech.com [211.20.114.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 2442B829FC for ; Thu, 16 Sep 2021 08:39:16 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=aspeedtech.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=chiawei_wang@aspeedtech.com Received: from mail.aspeedtech.com ([192.168.0.24]) by twspam01.aspeedtech.com with ESMTP id 18G6JDro094900 for ; Thu, 16 Sep 2021 14:19:13 +0800 (GMT-8) (envelope-from chiawei_wang@aspeedtech.com) Received: from ChiaWeiWang-PC.aspeed.com (192.168.2.66) by TWMBX02.aspeed.com (192.168.0.24) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 16 Sep 2021 14:39:10 +0800 From: Chia-Wei Wang To: Subject: [PATCH next] lib: hash-checksum: Use DM_HASH if supported Date: Thu, 16 Sep 2021 14:39:12 +0800 Message-ID: <20210916063912.21546-1-chiawei_wang@aspeedtech.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [192.168.2.66] X-ClientProxiedBy: TWMBX02.aspeed.com (192.168.0.24) To TWMBX02.aspeed.com (192.168.0.24) X-DNSRBL: X-MAIL: twspam01.aspeedtech.com 18G6JDro094900 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean 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 --- 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 #include #include +#if defined(CONFIG_DM_HASH) +#include +#include +#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