From patchwork Mon Mar 20 07:49:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Fritz X-Patchwork-Id: 1758863 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlemail.com header.i=@googlemail.com header.a=rsa-sha256 header.s=20210112 header.b=bsp5+csN; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Pg6M721pZz246f for ; Mon, 20 Mar 2023 18:49:23 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6696885A5F; Mon, 20 Mar 2023 08:49:15 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=googlemail.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=googlemail.com header.i=@googlemail.com header.b="bsp5+csN"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AF25F85A62; Mon, 20 Mar 2023 08:49:13 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) (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 2B65C85A48 for ; Mon, 20 Mar 2023 08:49:11 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=chf.fritz@googlemail.com Received: by mail-wm1-x336.google.com with SMTP id m35so6796816wms.4 for ; Mon, 20 Mar 2023 00:49:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; t=1679298550; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:reply-to:from:subject:message-id:from:to:cc :subject:date:message-id:reply-to; bh=Ou6iPUu/IThFH8dUoZj9e+IvXUogyc34tI4ennsxON4=; b=bsp5+csNAQGXyMyyXOaSCGfEnVq0ZCZLVhVfINtfbz3TImDhdFZBSqlxJxA2gmqd2A RkNZTPM7tpHNoLXVOulWwnED66EGxqvs85kE1Q9UjD+yJvqVt2unV8cCPDMT1/C+H5MD lSS7ZNjBgPRwNsLRoF5GEj1ApPZ0El1vS/ql6CsDcJ57WoCzX/z+7B3sAeufgvIgC/Xt Aktu3FUbVG4sLznUQyO9f669U+KcObNakzSW58OnKTZXXaWfjqeFe2X+w4KEczxEyKBX Z0cxG5hOdMtP9m7I3nmyCaEcO1cxEuTd9+P5TPARkiRFsLunvePoVv9DyD9elQ8e60gS qscw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679298550; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:reply-to:from:subject:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Ou6iPUu/IThFH8dUoZj9e+IvXUogyc34tI4ennsxON4=; b=0uvsJnILqGq7XQsGhN1bwA5wiKgx7l+uFfTPRBodYUNPrNjkoBiS7AP3vXKGfDyFtg Ph1BmYUfxydX8moD6o6G3xnjNBSJE140YAr6TbkIQjn571tPSDYKSYs5lYco3I4Rm69M WmBVvY+jC9KjWKJo94NaYOHOmkeC+BW7ebYEg0jEsKMiptcM3BpG+2YUlBmkRW1USpNx OA6gk2VkVy2YWcTZy1Xe1qu+8cZEigLMLNrL7ouvsVkwH8r/iS+fVE0sF6Tby5+ffu+n Jtt5j0jizWYK6Dmzy+BEKIpUNsOdX867LfgSCBhr8gnZyfgV0f8gfS4+RbKnazMO3VW9 Elpw== X-Gm-Message-State: AO0yUKUqIZiK6fz7vYMvdax79RBARJ/nDkC5EKJZBS/lLzCLrCW67ykr /jEmmSqL3UGH/vHUEMg3K9Te+/uZSws= X-Google-Smtp-Source: AK7set99YWK0vx7V0RRFpNK8iaWMA9MLsZXiozPJSSzilFE1VLXt9qT9L/w35UwaE00U9tbR452xFg== X-Received: by 2002:a05:600c:46c7:b0:3d3:49db:9b25 with SMTP id q7-20020a05600c46c700b003d349db9b25mr31166964wmo.26.1679298550545; Mon, 20 Mar 2023 00:49:10 -0700 (PDT) Received: from mars.fritz.box ([2a02:8070:b80:7760:b352:6d7:c693:9659]) by smtp.gmail.com with ESMTPSA id w16-20020a05600c475000b003edc9a5f98asm5166539wmo.44.2023.03.20.00.49.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Mar 2023 00:49:09 -0700 (PDT) Message-ID: <8945e17ea15cf0bf9fedd4fb7f9820f30981f3bf.camel@googlemail.com> Subject: [PATCH] image-fit: aligned output of hash calculation From: Christoph Fritz To: u-boot Cc: Gaurav Jain , Meenakshi Aggarwal Date: Mon, 20 Mar 2023 08:49:09 +0100 In-Reply-To: <18c0ee6aef75159bf02480ca6ed6c5ec78ef04db.camel@googlemail.com> References: <33c971e1425bf49d5dac558259dac3024e72329b.camel@googlemail.com> <4c4dee3280973870639ea414a137bc8cde375fe0.camel@googlemail.com> <9f5a096eb94faac0e7fc6ffad2b556df883702dc.camel@googlemail.com> <18c0ee6aef75159bf02480ca6ed6c5ec78ef04db.camel@googlemail.com> User-Agent: Evolution 3.38.3-1+deb11u1 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: chf.fritz@googlemail.com Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean When calculating the hash of a FIT image, the result is being stored in an unaligned memory location. This causes problems (wrong hash) with the fsl_hash CAAM engine on i.mx7ulp. To fix the issue, this patch introduces a use of memalign() to allocate memory for the hash value so that it is aligned correctly, similar to what is done in file common/hash.c function hash_command() already. Signed-off-by: Christoph Fritz --- boot/image-fit.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/boot/image-fit.c b/boot/image-fit.c index 0de9180d2bb..c2283431925 100644 --- a/boot/image-fit.c +++ b/boot/image-fit.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #ifdef CONFIG_DM_HASH #include @@ -1263,12 +1264,17 @@ int calculate_hash(const void *data, int data_len, const char *name, static int fit_image_check_hash(const void *fit, int noffset, const void *data, size_t size, char **err_msgp) { +#ifdef USE_HOSTCC uint8_t value[FIT_MAX_HASH_LEN]; +#else + u8 *value; +#endif int value_len; const char *algo; uint8_t *fit_value; int fit_value_len; int ignore; + int ret = 0; *err_msgp = NULL; @@ -1292,20 +1298,32 @@ static int fit_image_check_hash(const void *fit, int noffset, const void *data, return -1; } +#ifndef USE_HOSTCC + value = memalign(ARCH_DMA_MINALIGN, + sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE); +#endif + if (calculate_hash(data, size, algo, value, &value_len)) { *err_msgp = "Unsupported hash algorithm"; - return -1; + ret = -1; + goto hash_exit; } if (value_len != fit_value_len) { *err_msgp = "Bad hash value len"; - return -1; + ret = -1; + goto hash_exit; } else if (memcmp(value, fit_value, value_len) != 0) { *err_msgp = "Bad hash value"; - return -1; + ret = -1; + goto hash_exit; } - return 0; +hash_exit: +#ifndef USE_HOSTCC + unmap_sysmem(value); +#endif + return ret; } int fit_image_verify_with_data(const void *fit, int image_noffset,