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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 5F157C6FA86 for ; Tue, 13 Sep 2022 06:58:17 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E77E684A86; Tue, 13 Sep 2022 08:58:14 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1663052295; bh=z1EqInPjagXBNJ6f9IHgFF/x314JA55XaispCQUm13A=; h=Date:Subject:To:Cc:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=dX4DD688P5vG5NI3ShY3uQql6R+3/KqcZArx0qWHxVu7t4Dbh9Nitu1DEAxu20n72 pBs5Y+kIxjmlbKRWgUXG7BQ9AmzlO7K049cc0oUKIxxkQo48gB5eRphbW2/XP0X2LS TUobQJAXDqWYNhwE6PRqzIBJE4ApSTCIlhfgktWx306qzMWsDM7Tk/SMUE4SLbzgPN bxZHxjRTM1IEN8i/UyGA52NqX2ot4qu3WfjCDPr2sxm+Sy/5PTaJGd2tGnf0FOqbvv Kr5FzeTAYJsanCKlFCPlbUJuwbcvUc/SarYIQj0VquaSP0DETTLjvM0hRprzgvwAUa 9QngNGb5nPkrw== Received: by phobos.denx.de (Postfix, from userid 109) id 6EAF684A8A; Tue, 13 Sep 2022 08:58:13 +0200 (CEST) Received: from mout-u-107.mailbox.org (mout-u-107.mailbox.org [IPv6:2001:67c:2050:101:465::107]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 4B15684A73 for ; Tue, 13 Sep 2022 08:58:11 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sr@denx.de Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4MRZ6n2tS8z9sRw; Tue, 13 Sep 2022 08:58:09 +0200 (CEST) Message-ID: Date: Tue, 13 Sep 2022 08:58:03 +0200 MIME-Version: 1.0 Subject: Re: [PATCH v3 1/2] cmd: mvebu/bubt: Check for A38x image data checksum Content-Language: en-US To: =?UTF-8?Q?Pali_Roh=c3=a1r?= Cc: u-boot@lists.denx.de References: <20220805154819.4924-1-pali@kernel.org> <20220823125224.20638-1-pali@kernel.org> From: Stefan Roese In-Reply-To: <20220823125224.20638-1-pali@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4MRZ6n2tS8z9sRw 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: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean On 23.08.22 14:52, Pali Rohár wrote: > Currently for A38x image is checked only header checksum. > So check also for image data checksum to prevent flashing broken image. > > Signed-off-by: Pali Rohár > Reviewed-by: Stefan Roese Applied to u-boot-marvell/master Thanks, Stefan > --- > Changes in v3: > * Compile fix (move another code chunk from patch 2/2 to 1/2) > > Changes in v2: > * Compile fix (move code chunk from patch 2/2 to 1/2) > --- > cmd/mvebu/bubt.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 45 insertions(+), 1 deletion(-) > > diff --git a/cmd/mvebu/bubt.c b/cmd/mvebu/bubt.c > index 2136af64163c..a97e5ce38a5e 100644 > --- a/cmd/mvebu/bubt.c > +++ b/cmd/mvebu/bubt.c > @@ -688,9 +688,25 @@ static uint8_t image_checksum8(const void *start, size_t len) > return csum; > } > > +static uint32_t image_checksum32(const void *start, size_t len) > +{ > + u32 csum = 0; > + const u32 *p = start; > + > + while (len) { > + csum += *p; > + ++p; > + len -= sizeof(u32); > + } > + > + return csum; > +} > + > static int check_image_header(void) > { > u8 checksum; > + u32 checksum32, exp_checksum32; > + u32 offset, size; > const struct a38x_main_hdr_v1 *hdr = > (struct a38x_main_hdr_v1 *)get_load_addr(); > const size_t image_size = a38x_header_size(hdr); > @@ -701,11 +717,39 @@ static int check_image_header(void) > checksum = image_checksum8(hdr, image_size); > checksum -= hdr->checksum; > if (checksum != hdr->checksum) { > - printf("Error: Bad A38x image checksum. 0x%x != 0x%x\n", > + printf("Error: Bad A38x image header checksum. 0x%x != 0x%x\n", > checksum, hdr->checksum); > return -ENOEXEC; > } > > + offset = le32_to_cpu(hdr->srcaddr); > + size = le32_to_cpu(hdr->blocksize); > + > + if (hdr->blockid == 0x78) { /* SATA id */ > + if (offset < 1) { > + printf("Error: Bad A38x image srcaddr.\n"); > + return -ENOEXEC; > + } > + offset -= 1; > + offset *= 512; > + } > + > + if (hdr->blockid == 0xAE) /* SDIO id */ > + offset *= 512; > + > + if (offset % 4 != 0 || size < 4 || size % 4 != 0) { > + printf("Error: Bad A38x image blocksize.\n"); > + return -ENOEXEC; > + } > + > + checksum32 = image_checksum32((u8 *)hdr + offset, size - 4); > + exp_checksum32 = *(u32 *)((u8 *)hdr + offset + size - 4); > + if (checksum32 != exp_checksum32) { > + printf("Error: Bad A38x image data checksum. 0x%08x != 0x%08x\n", > + checksum32, exp_checksum32); > + return -ENOEXEC; > + } > + > printf("Image checksum...OK!\n"); > return 0; > } Viele Grüße, Stefan Roese -- DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de