From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web12.860.1646952112302807460 for ; Thu, 10 Mar 2022 14:41:52 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=FV8Pqz7m; spf=pass (domain: intel.com, ip: 134.134.136.31, mailfrom: isaac.w.oram@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646952112; x=1678488112; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DnU6GSLaNW8nFghAnc4bC/suUC3FhWH4yk/gDmS2rqg=; b=FV8Pqz7mVmComLMcuzoEck07NZvlxW5tb6aWgCwiYImEyCD0YqR+UWh8 awNMil7L4iTcNGFNrnKxRqB5Iiz68XhdhmYauAHkag5SLws4PC9Ak89dk rlmIQ28NjfDfTT6UFg/rLzlXQG3SW3PNU8mcalUT2NckoHFr5vWU4dftC dusH0lGGH2LovsdLUok1pyM+Y6i8rDx6fUNMcN4HWFAoqZCsWZJzQac5p YGQguF8sVAitJJKaBHJ0LasnBeS74E0SQT6zhULKUSqD7f6NDuJuu14Gf ZmTgUBk7CF6I0oWUit9IhHqR41Y53Mt8GlxeW+dEEVKElA0sOwh1/hKr0 w==; X-IronPort-AV: E=McAfee;i="6200,9189,10282"; a="316121249" X-IronPort-AV: E=Sophos;i="5.90,171,1643702400"; d="scan'208";a="316121249" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Mar 2022 14:41:34 -0800 X-IronPort-AV: E=Sophos;i="5.90,171,1643702400"; d="scan'208";a="644643363" Received: from iworam-desk.amr.corp.intel.com ([10.7.150.60]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Mar 2022 14:41:34 -0800 From: "Oram, Isaac W" To: devel@edk2.groups.io Cc: Nate DeSimone , Chasel Chiu Subject: [edk2-devel][edk2-platforms][PATCH V1 3/9] WhitleyOpenBoardPkg/BaseCrcLib: Add library for CRC16 Date: Thu, 10 Mar 2022 14:41:08 -0800 Message-Id: <406972869595ac96cd402be7843f16b9e0534df9.1646951441.git.isaac.w.oram@intel.com> X-Mailer: git-send-email 2.27.0.windows.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Core only supports CRC32, this library adds CRC16 support. Cc: Nate DeSimone Cc: Chasel Chiu Signed-off-by: Isaac Oram --- Platform/Intel/WhitleyOpenBoardPkg/Include/Library/CrcLib.h | 42 ++++++++++++ Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.c | 71 ++++++++++++++++++++ Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.inf | 23 +++++++ Platform/Intel/WhitleyOpenBoardPkg/PlatformPkg.dsc | 1 + 4 files changed, 137 insertions(+) diff --git a/Platform/Intel/WhitleyOpenBoardPkg/Include/Library/CrcLib.h b/Platform/Intel/WhitleyOpenBoardPkg/Include/Library/CrcLib.h new file mode 100644 index 0000000000..7ca3b7cabb --- /dev/null +++ b/Platform/Intel/WhitleyOpenBoardPkg/Include/Library/CrcLib.h @@ -0,0 +1,42 @@ +/** @file + Interface header file for the CRC library class. + + @copyright + Copyright 2016 - 2018 Intel Corporation.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#ifndef _CRC_LIB_H_ +#define _CRC_LIB_H_ + +#include + +/** + Calculate a 16-bit CRC. + + The algorithm used is MSB-first form of the ITU-T Recommendation V.41, which + uses an initial value of 0x0000 and a polynomial of 0x1021. It is the same + algorithm used by XMODEM. + + The output CRC location is not updated until the calculation is finished, so + it is possible to pass a structure as the data, and the CRC field of the same + structure as the output location for the calculated CRC. The CRC field should + be set to zero before calling this function. Once the CRC field is updated by + this function, running it again over the structure produces a CRC of zero. + + @param[in] Data A pointer to the target data. + @param[in] DataSize The target data size. + @param[out] CrcOut A pointer to the return location of the CRC. + + @retval EFI_SUCCESS The CRC was calculated successfully. + @retval EFI_INVALID_PARAMETER A null pointer was provided. +**/ +EFI_STATUS +CalculateCrc16 ( + IN VOID *Data, + IN UINTN DataSize, + OUT UINT16 *CrcOut + ); + +#endif // _CRC_LIB_H_ diff --git a/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.c b/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.c new file mode 100644 index 0000000000..3e8fa402ad --- /dev/null +++ b/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.c @@ -0,0 +1,71 @@ +/** @file + Base implementation of the CRC library class. + + @copyright + Copyright 2016 - 2018 Intel Corporation.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include +#include + +/** + Calculate a 16-bit CRC. + + The algorithm used is MSB-first form of the ITU-T Recommendation V.41, which + uses an initial value of 0x0000 and a polynomial of 0x1021. It is the same + algorithm used by XMODEM. + + The output CRC location is not updated until the calculation is finished, so + it is possible to pass a structure as the data, and the CRC field of the same + structure as the output location for the calculated CRC. The CRC field should + be set to zero before calling this function. Once the CRC field is updated by + this function, running it again over the structure produces a CRC of zero. + + @param[in] Data A pointer to the target data. + @param[in] DataSize The target data size. + @param[out] CrcOut A pointer to the return location of the CRC. + + @retval EFI_SUCCESS The CRC was calculated successfully. + @retval EFI_INVALID_PARAMETER A null pointer was provided. +**/ +EFI_STATUS +CalculateCrc16 ( + IN VOID *Data, + IN UINTN DataSize, + OUT UINT16 *CrcOut + ) +{ + UINT32 Crc; + UINTN Index; + UINT8 *Byte; + + if (Data == NULL || CrcOut == NULL) { + return EFI_INVALID_PARAMETER; + } + + Crc = 0x0000; + for (Byte = (UINT8 *) Data; Byte < (UINT8 *) Data + DataSize; Byte++) { + // + // XOR the next data byte into the CRC. + // + Crc ^= (UINT16) *Byte << 8; + // + // Shift out eight bits, feeding back based on the polynomial whenever a + // 1 is shifted out of bit 15. + // + for (Index = 0; Index < 8; Index++) { + Crc <<= 1; + if (Crc & BIT16) { + Crc ^= 0x1021; + } + } + } + + // + // Mask and return the 16-bit CRC. + // + *CrcOut = (UINT16) (Crc & 0xFFFF); + return EFI_SUCCESS; +} diff --git a/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.inf b/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.inf new file mode 100644 index 0000000000..6b404e1259 --- /dev/null +++ b/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.inf @@ -0,0 +1,23 @@ +## @file +# Base implementation of the CRC library class. +# +# @copyright +# Copyright 2016 Intel Corporation.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010019 + BASE_NAME = BaseCrcLib + FILE_GUID = F3BE9A28-78A2-4B02-AB26-D27EE85D9256 + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = CrcLib + +[Sources] + BaseCrcLib.c + +[Packages] + MdePkg/MdePkg.dec + WhitleyOpenBoardPkg/PlatformPkg.dec diff --git a/Platform/Intel/WhitleyOpenBoardPkg/PlatformPkg.dsc b/Platform/Intel/WhitleyOpenBoardPkg/PlatformPkg.dsc index e78a104004..9cdb5bc2f6 100644 --- a/Platform/Intel/WhitleyOpenBoardPkg/PlatformPkg.dsc +++ b/Platform/Intel/WhitleyOpenBoardPkg/PlatformPkg.dsc @@ -618,6 +618,7 @@ PciSegmentInfoLib|$(PLATFORM_PKG)/Pci/Library/PciSegmentInfoLibSimple/PciSegmentInfoLibSimple.inf PlatformOpromPolicyLib|$(RP_PKG)/Library/PlatformOpromPolicyLibNull/PlatformOpromPolicyLibNull.inf VmgExitLib|UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf + CrcLib|WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.inf [LibraryClasses.Common.SEC, LibraryClasses.Common.PEI_CORE, LibraryClasses.Common.PEIM] FspWrapperApiLib|IntelFsp2WrapperPkg/Library/BaseFspWrapperApiLib/BaseFspWrapperApiLib.inf -- 2.27.0.windows.1