From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id E578F940F26 for ; Thu, 30 Nov 2023 02:46:25 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=S/HoIOcjjLBxcvD27bHv7EfE40Kc8mWiWLApFWvm1w0=; 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=S9UqRil4hQHu4Ci1MaEr9V/Ghtll29zdGgFxB448cMaodRYC9EYaISEqzmJTNYV/RnTAOnGZ 8A9qfKGFpbxuj2X5HMNfyn/NVhsLIYAiGyRl7ffbPxhJgW6mGLm6OEd5Y//fwDPDwP6J7QBmr3Y GM6TxYFLOVyCx6mEULscfJpo= X-Received: by 127.0.0.2 with SMTP id WeKpYY7687511xOZhjltU6YF; 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: sO1k11OOLGmrG4hqDjZ2rabox7686176AA= 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: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=S9UqRil4; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io 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=4609 Signed-off-by: Pedro Falcato Cc: Liming Gao Cc: Michael D Kinney Cc: Zhiguang Liu --- 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/BaseLib.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 ); +// +// 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. diff --git a/MdePkg/Library/BaseLib/CheckSum.c b/MdePkg/Library/BaseLib/CheckSum.c index b6a076573191..57d324c82b22 100644 --- a/MdePkg/Library/BaseLib/CheckSum.c +++ b/MdePkg/Library/BaseLib/CheckSum.c @@ -678,13 +678,13 @@ CalculateCrc16Ansi ( Buf = Buffer; - Crc = ~InitialValue; + Crc = InitialValue; while (Length-- != 0) { Crc = mCrc16LookupTable[(Crc & 0xFF) ^ *(Buf++)] ^ (Crc >> 8); } - return ~Crc; + return Crc; } GLOBAL_REMOVE_IF_UNREFERENCED STATIC CONST UINT32 mCrc32cLookupTable[256] = { -- 2.43.0 -=-=-=-=-=-=-=-=-=-=-=- 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/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-