From nobody Sun Jul 26 00:34:10 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+111884+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+111884+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1701312385; cv=none; d=zohomail.com; s=zohoarc; b=jnHoWP+F6QBc3/WcSh+rcpKXjRg36thPQu/6tWSFd3wKOy1h97vmqVRLlfqkUvMZyakzCIqMH2hYYLiqXrwk0Ex53lAfSlvPUgHqWVQY2cHo/Z9VTncU4Qn7hAySpZG/1Ge/5nrAbdF/dkFSWFPOU08Adu3Ozsx4wTBNuu0O9M8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1701312385; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=TuLtR/SR2lxuLObMTVYRAolkLfnpiVfxEI5tDs1yJq8=; b=YVeBYLXiu/T4FlUE6edbT3Ts3lBbkYb3WEn1cvyUYxGSA0ruvLjgfJYfBA77ugFuS72NFy19nwlTjKizjp53WX8mwnDc7fyjJfKp/ThoaG93G1rQYa/shb69Biw4rqMNi5rYzOSKq5CTLrKpdtT55Iaq3uXwfqFi3iXrVuH81xI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+111884+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1701312385148527.4369527762681; Wed, 29 Nov 2023 18:46:25 -0800 (PST) Return-Path: DKIM-Signature: a=rsa-sha256; bh=meX11vWGFMmzHto/LvqAzCSzEOO+nDcJ7lkzg3+z4UA=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1701312384; v=1; b=MQ0L0VJI/2THfjyeNMFrJYxz6dTQ33MEq9OA4+J1SN1OlRtsfYaH4ORzLLjyNz092RBHTqlK C6z2LJ3NeAGTXvqG1PixpZRgidCYwFwKySbpEF2F6FYgCTP629tlVICNG3JLS0Qbr3oE8q4prVE Axi96HTLWtGo9CITu3P+J0Wc= X-Received: by 127.0.0.2 with SMTP id G0zmYY1788612xyddjHdMHoH; Wed, 29 Nov 2023 18:46:24 -0800 X-Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) by mx.groups.io with SMTP id smtpd.web10.63600.1701312383889025902 for ; Wed, 29 Nov 2023 18:46:24 -0800 X-Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-3331bb4b116so297455f8f.2 for ; Wed, 29 Nov 2023 18:46:23 -0800 (PST) X-Gm-Message-State: 35kW9TL3P1oCD6M7BlZKjRjhx1787277AA= X-Google-Smtp-Source: AGHT+IGDfWe4FajW81lfaXQh8jlOiDuJSfhXI96sfQXVyLGhraxA4hhb7Uz3IxrVDuSMvlXtiaJG2g== X-Received: by 2002:a05:6000:12c2:b0:332:d11d:527c with SMTP id l2-20020a05600012c200b00332d11d527cmr12941116wrx.8.1701312382123; Wed, 29 Nov 2023 18:46:22 -0800 (PST) X-Received: from PC-PEDRO-ARCH.lan ([2001:8a0:7280:5801:9441:3dce:686c:bfc7]) by smtp.gmail.com with ESMTPSA id b19-20020a05600c4e1300b0040648217f4fsm3986232wmq.39.2023.11.29.18.46.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 18:46:19 -0800 (PST) From: "Pedro Falcato" To: devel@edk2.groups.io Cc: Savva Mitrofanov , Pedro Falcato , Liming Gao , Michael D Kinney , Zhiguang Liu Subject: [edk2-devel] [PATCH 1/2] MdePkg/BaseLib: Fix CRC16-ANSI calculation Date: Thu, 30 Nov 2023 02:46:10 +0000 Message-ID: <20231130024611.67135-2-pedro.falcato@gmail.com> In-Reply-To: <20231130024611.67135-1-pedro.falcato@gmail.com> References: <20231130024611.67135-1-pedro.falcato@gmail.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,pedro.falcato@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1701312385721000005 Content-Type: text/plain; charset="utf-8" The current CalculateCrc16Ansi implementation does the following: 1) Invert the passed checksum 2) Calculate the new checksum by going through data and using the lookup table 3) Invert it back again This emulated my design for CalculateCrc32c, where 0 is passed as the initial checksum, and it inverts in the end. However, CRC16 does not invert the checksum on input and output. So this is incorrect. Fix the problem by not inverting input checksums nor output checksums. Callers should now pass CRC16ANSI_INIT as the initial value instead of "0". This is a breaking change. This problem was found out-of-list when older ext4 filesystems (that use crc16 checksums) failed to mount with "corruption". BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4609 Signed-off-by: Pedro Falcato Cc: Liming Gao Cc: Michael D Kinney Cc: Zhiguang Liu Reviewed-by: Michael D Kinney --- MdePkg/Include/Library/BaseLib.h | 5 +++++ MdePkg/Library/BaseLib/CheckSum.c | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/Base= Lib.h index 5d7067ee854e..728e89d2bf44 100644 --- a/MdePkg/Include/Library/BaseLib.h +++ b/MdePkg/Include/Library/BaseLib.h @@ -4599,6 +4599,11 @@ CalculateCrc16Ansi ( IN UINT16 InitialValue ); =20 +// +// Initial value for the CRC16-ANSI algorithm, when no prior checksum has = been calculated. +// +#define CRC16ANSI_INIT 0xffff + /** Calculates the CRC32c checksum of the given buffer. =20 diff --git a/MdePkg/Library/BaseLib/CheckSum.c b/MdePkg/Library/BaseLib/Che= ckSum.c index b6a076573191..57d324c82b22 100644 --- a/MdePkg/Library/BaseLib/CheckSum.c +++ b/MdePkg/Library/BaseLib/CheckSum.c @@ -678,13 +678,13 @@ CalculateCrc16Ansi ( =20 Buf =3D Buffer; =20 - Crc =3D ~InitialValue; + Crc =3D InitialValue; =20 while (Length-- !=3D 0) { Crc =3D mCrc16LookupTable[(Crc & 0xFF) ^ *(Buf++)] ^ (Crc >> 8); } =20 - return ~Crc; + return Crc; } =20 GLOBAL_REMOVE_IF_UNREFERENCED STATIC CONST UINT32 mCrc32cLookupTable[256]= =3D { --=20 2.43.0 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#111884): https://edk2.groups.io/g/devel/message/111884 Mute This Topic: https://groups.io/mt/102886793/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-