From mboxrd@z Thu Jan 1 00:00:00 1970 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=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 C82DEC432BE for ; Tue, 24 Aug 2021 00:54:40 +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 4F255613A7 for ; Tue, 24 Aug 2021 00:54:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 4F255613A7 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.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 1885F82EDA; Tue, 24 Aug 2021 02:54:21 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="NtycZs5O"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7511F82D95; Tue, 24 Aug 2021 02:53:45 +0200 (CEST) Received: from mail-ot1-x32a.google.com (mail-ot1-x32a.google.com [IPv6:2607:f8b0:4864:20::32a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 90E7B82DB2 for ; Tue, 24 Aug 2021 02:53:29 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=mr.nuke.me@gmail.com Received: by mail-ot1-x32a.google.com with SMTP id i8-20020a056830402800b0051afc3e373aso30186836ots.5 for ; Mon, 23 Aug 2021 17:53:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZRvVTCFnJ/qqCMReYZtiuFNH5mi31MYXXN53T3WgOE4=; b=NtycZs5OgSV+ZWwNeaBd4w5EniW4RKXY11jnniXYKrAPvj3gHShyp/lAVcQ1y4POoG OhTtgvCEBqHK4ZUupHM9yoQ4DGS7dcPJDrjI8Vv1rYpj1EWB7ADyyDNqrge66HShL3sM m7ii5YyRndq4H72N8s4yvaJQWAC7gFF213eofuiZbiEVkdxx5orqKxHMIfXmqERmTUuR bZG6V4yF+WQkjrMcwupEOMolxF2Y7QNvtohXMonFKnIrfBKWaMYOGL8J8OKqEdFDz8+F 6WHJZQgbeIUiOzvEyJr1BMaQyN01rBQ++WvZRIX+Fpzhhp8+Gr8Aud+xUn5NNPcG4Nm7 726g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZRvVTCFnJ/qqCMReYZtiuFNH5mi31MYXXN53T3WgOE4=; b=TH8tBEgPh2UOGJ2QLLAOWJMWmku062f0VR9T6LKxEO4eHJfpWkFezlqu8NP6BOXqar 9Rn4STYzWJIcTyJb/wfbJfd6uO9R6UdfnH44BxCMiCwLxBEmuTpaStu+Aet41cS0fNfN M4ebCGCCYRViZi/fs3NEVRdb5IvzMqfon1jHLDI9JLS6qPp1k6lnyrPZBRAWa0RQPTXV DPy1fV1/C6a39DMqPLDguCh4KXhk/cebTBFcCcrzcVEN04AMpi9DlIib2oZtwFuDHkJQ zUXHGHfGV3CmL2TZH5KpRXnXWlfU23/rzM3Dfp4ah1z5gjc8H8J7YA8yoIuwpJGzFk5e uSTw== X-Gm-Message-State: AOAM533ku9NMhfF/8fZ9Jd/KkUE//7GPXlf1BX6QOlE6ZwTTxTXeMOka iwFajDNpIi83fJaQfpdai74= X-Google-Smtp-Source: ABdhPJwiNaFNQ0QaxEAAK0bcs62qnha7MAcc5cyZn0knvesLuA6PgspixlajpoIZcbjO9xBLTCD+pQ== X-Received: by 2002:a05:6830:b8e:: with SMTP id a14mr7005846otv.162.1629766408290; Mon, 23 Aug 2021 17:53:28 -0700 (PDT) Received: from nuclearis3.lan (c-98-195-139-126.hsd1.tx.comcast.net. [98.195.139.126]) by smtp.gmail.com with ESMTPSA id h14sm4229142otm.5.2021.08.23.17.53.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 17:53:27 -0700 (PDT) From: Alexandru Gagniuc To: trini@konsulko.com, u-boot@lists.denx.de Cc: sjg@chromium.org, Alexandru Gagniuc Subject: [PATCH 4/5] image: Drop if/elseif hash selection in calculate_hash() Date: Mon, 23 Aug 2021 19:53:19 -0500 Message-Id: <20210824005320.3647668-5-mr.nuke.me@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210824005320.3647668-1-mr.nuke.me@gmail.com> References: <20210824005320.3647668-1-mr.nuke.me@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 calculate_hash() would try to select the appropriate hashing function by a if/elseif contruct. But that is exactly why hash_lookup_algo() exists, so use it instead. This does mean that we now have to 'select HASH' to make sure we get the hash_lookup_algo() symbol. However, the change makes sense because even basic FITs will have to deal with "hash" nodes. My only concern is that the 'select SPL_HASH' might cause some platform to grow above its SPL size allowance Signed-off-by: Alexandru Gagniuc --- common/Kconfig.boot | 2 ++ common/image-fit.c | 45 +++++++++++++++++++-------------------------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/common/Kconfig.boot b/common/Kconfig.boot index 314f1e50a0..c2d6c89372 100644 --- a/common/Kconfig.boot +++ b/common/Kconfig.boot @@ -13,6 +13,7 @@ config FIT bool "Support Flattened Image Tree" select MD5 select SHA1 + select HASH help This option allows you to boot the new uImage structure, Flattened Image Tree. FIT is formally a FDT, which can include @@ -133,6 +134,7 @@ if SPL config SPL_FIT bool "Support Flattened Image Tree within SPL" depends on SPL + select SPL_HASH select SPL_OF_LIBFDT config SPL_FIT_PRINT diff --git a/common/image-fit.c b/common/image-fit.c index aff4670be3..92d9141bcd 100644 --- a/common/image-fit.c +++ b/common/image-fit.c @@ -1193,6 +1193,12 @@ int fit_set_timestamp(void *fit, int noffset, time_t timestamp) return 0; } +static void crc32_uimage_fixup(void *value) +{ + /* TODO: In C, this type punning is undefined behavior: */ + *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); +} + /** * calculate_hash - calculate and return hash for provided input data * @data: pointer to the input data @@ -1211,37 +1217,24 @@ int fit_set_timestamp(void *fit, int noffset, time_t timestamp) * 0, on success * -1, when algo is unsupported */ -int calculate_hash(const void *data, int data_len, const char *algo, +int calculate_hash(const void *data, int data_len, const char *name, uint8_t *value, int *value_len) { - if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) { - *((uint32_t *)value) = crc32_wd(0, data, data_len, - CHUNKSZ_CRC32); - *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); - *value_len = 4; - } else if (CONFIG_IS_ENABLED(SHA1) && strcmp(algo, "sha1") == 0) { - sha1_csum_wd((unsigned char *)data, data_len, - (unsigned char *)value, CHUNKSZ_SHA1); - *value_len = 20; - } else if (CONFIG_IS_ENABLED(SHA256) && strcmp(algo, "sha256") == 0) { - sha256_csum_wd((unsigned char *)data, data_len, - (unsigned char *)value, CHUNKSZ_SHA256); - *value_len = SHA256_SUM_LEN; - } else if (CONFIG_IS_ENABLED(SHA384) && strcmp(algo, "sha384") == 0) { - sha384_csum_wd((unsigned char *)data, data_len, - (unsigned char *)value, CHUNKSZ_SHA384); - *value_len = SHA384_SUM_LEN; - } else if (CONFIG_IS_ENABLED(SHA512) && strcmp(algo, "sha512") == 0) { - sha512_csum_wd((unsigned char *)data, data_len, - (unsigned char *)value, CHUNKSZ_SHA512); - *value_len = SHA512_SUM_LEN; - } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { - md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5); - *value_len = 16; - } else { + struct hash_algo *algo; + int ret; + + ret = hash_lookup_algo(name, &algo); + if (ret < 0) { debug("Unsupported hash alogrithm\n"); return -1; } + + algo->hash_func_ws(data, data_len, value, algo->chunk_size); + *value_len = algo->digest_size; + + if (!strcmp(name, "crc32")) + crc32_uimage_fixup(value); + return 0; } -- 2.31.1