<div dir="ltr"><div>Hi,</div><div><br></div><div>I've just noticed this patch adds CRC16, which I've already added to my Ext4Pkg (<a href="https://github.com/tianocore/edk2-platforms/blob/master/Features/Ext4Pkg/Ext4Dxe/Crc16.c">https://github.com/tianocore/edk2-platforms/blob/master/Features/Ext4Pkg/Ext4Dxe/Crc16.c</a>).</div><div>I suggest we add CRC16 (and possibly CRC32C, which I already have in my package as well) to MdePkg, as to de-duplicate code which might be useful in other places.</div><div>What do you think? If it sounds good to you, I'll open a bugzilla and work on that.</div><div><br></div><div>Best regards,</div><div>Pedro<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Mar 10, 2022 at 10:41 PM Oram, Isaac W <<a href="mailto:isaac.w.oram@intel.com">isaac.w.oram@intel.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Core only supports CRC32, this library adds CRC16 support.<br>
<br>
Cc: Nate DeSimone <<a href="mailto:nathaniel.l.desimone@intel.com" target="_blank">nathaniel.l.desimone@intel.com</a>><br>
Cc: Chasel Chiu <<a href="mailto:chasel.chiu@intel.com" target="_blank">chasel.chiu@intel.com</a>><br>
Signed-off-by: Isaac Oram <<a href="mailto:isaac.w.oram@intel.com" target="_blank">isaac.w.oram@intel.com</a>><br>
---<br>
 Platform/Intel/WhitleyOpenBoardPkg/Include/Library/CrcLib.h          | 42 ++++++++++++<br>
 Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.c   | 71 ++++++++++++++++++++<br>
 Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.inf | 23 +++++++<br>
 Platform/Intel/WhitleyOpenBoardPkg/PlatformPkg.dsc                   |  1 +<br>
 4 files changed, 137 insertions(+)<br>
<br>
diff --git a/Platform/Intel/WhitleyOpenBoardPkg/Include/Library/CrcLib.h b/Platform/Intel/WhitleyOpenBoardPkg/Include/Library/CrcLib.h<br>
new file mode 100644<br>
index 0000000000..7ca3b7cabb<br>
--- /dev/null<br>
+++ b/Platform/Intel/WhitleyOpenBoardPkg/Include/Library/CrcLib.h<br>
@@ -0,0 +1,42 @@<br>
+/** @file<br>
+  Interface header file for the CRC library class.<br>
+<br>
+  @copyright<br>
+  Copyright 2016 - 2018 Intel Corporation. <BR><br>
+<br>
+  SPDX-License-Identifier: BSD-2-Clause-Patent<br>
+**/<br>
+<br>
+#ifndef _CRC_LIB_H_<br>
+#define _CRC_LIB_H_<br>
+<br>
+#include <Uefi.h><br>
+<br>
+/**<br>
+  Calculate a 16-bit CRC.<br>
+<br>
+  The algorithm used is MSB-first form of the ITU-T Recommendation V.41, which<br>
+  uses an initial value of 0x0000 and a polynomial of 0x1021. It is the same<br>
+  algorithm used by XMODEM.<br>
+<br>
+  The output CRC location is not updated until the calculation is finished, so<br>
+  it is possible to pass a structure as the data, and the CRC field of the same<br>
+  structure as the output location for the calculated CRC. The CRC field should<br>
+  be set to zero before calling this function. Once the CRC field is updated by<br>
+  this function, running it again over the structure produces a CRC of zero.<br>
+<br>
+  @param[in]  Data              A pointer to the target data.<br>
+  @param[in]  DataSize          The target data size.<br>
+  @param[out] CrcOut            A pointer to the return location of the CRC.<br>
+<br>
+  @retval EFI_SUCCESS           The CRC was calculated successfully.<br>
+  @retval EFI_INVALID_PARAMETER A null pointer was provided.<br>
+**/<br>
+EFI_STATUS<br>
+CalculateCrc16 (<br>
+  IN  VOID    *Data,<br>
+  IN  UINTN   DataSize,<br>
+  OUT UINT16  *CrcOut<br>
+  );<br>
+<br>
+#endif  // _CRC_LIB_H_<br>
diff --git a/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.c b/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.c<br>
new file mode 100644<br>
index 0000000000..3e8fa402ad<br>
--- /dev/null<br>
+++ b/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.c<br>
@@ -0,0 +1,71 @@<br>
+/** @file<br>
+  Base implementation of the CRC library class.<br>
+<br>
+  @copyright<br>
+  Copyright 2016 - 2018 Intel Corporation. <BR><br>
+<br>
+  SPDX-License-Identifier: BSD-2-Clause-Patent<br>
+**/<br>
+<br>
+#include <Base.h><br>
+#include <Library/CrcLib.h><br>
+<br>
+/**<br>
+  Calculate a 16-bit CRC.<br>
+<br>
+  The algorithm used is MSB-first form of the ITU-T Recommendation V.41, which<br>
+  uses an initial value of 0x0000 and a polynomial of 0x1021. It is the same<br>
+  algorithm used by XMODEM.<br>
+<br>
+  The output CRC location is not updated until the calculation is finished, so<br>
+  it is possible to pass a structure as the data, and the CRC field of the same<br>
+  structure as the output location for the calculated CRC. The CRC field should<br>
+  be set to zero before calling this function. Once the CRC field is updated by<br>
+  this function, running it again over the structure produces a CRC of zero.<br>
+<br>
+  @param[in]  Data              A pointer to the target data.<br>
+  @param[in]  DataSize          The target data size.<br>
+  @param[out] CrcOut            A pointer to the return location of the CRC.<br>
+<br>
+  @retval EFI_SUCCESS           The CRC was calculated successfully.<br>
+  @retval EFI_INVALID_PARAMETER A null pointer was provided.<br>
+**/<br>
+EFI_STATUS<br>
+CalculateCrc16 (<br>
+  IN  VOID    *Data,<br>
+  IN  UINTN   DataSize,<br>
+  OUT UINT16  *CrcOut<br>
+  )<br>
+{<br>
+  UINT32  Crc;<br>
+  UINTN   Index;<br>
+  UINT8   *Byte;<br>
+<br>
+  if (Data == NULL || CrcOut == NULL) {<br>
+    return EFI_INVALID_PARAMETER;<br>
+  }<br>
+<br>
+  Crc = 0x0000;<br>
+  for (Byte = (UINT8 *) Data; Byte < (UINT8 *) Data + DataSize; Byte++) {<br>
+    //<br>
+    // XOR the next data byte into the CRC.<br>
+    //<br>
+    Crc ^= (UINT16) *Byte << 8;<br>
+    //<br>
+    // Shift out eight bits, feeding back based on the polynomial whenever a<br>
+    // 1 is shifted out of bit 15.<br>
+    //<br>
+    for (Index = 0; Index < 8; Index++) {<br>
+      Crc <<= 1;<br>
+      if (Crc & BIT16) {<br>
+        Crc ^= 0x1021;<br>
+      }<br>
+    }<br>
+  }<br>
+<br>
+  //<br>
+  // Mask and return the 16-bit CRC.<br>
+  //<br>
+  *CrcOut = (UINT16) (Crc & 0xFFFF);<br>
+  return EFI_SUCCESS;<br>
+}<br>
diff --git a/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.inf b/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.inf<br>
new file mode 100644<br>
index 0000000000..6b404e1259<br>
--- /dev/null<br>
+++ b/Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.inf<br>
@@ -0,0 +1,23 @@<br>
+## @file<br>
+# Base implementation of the CRC library class.<br>
+#<br>
+# @copyright<br>
+# Copyright 2016 Intel Corporation. <BR><br>
+#<br>
+# SPDX-License-Identifier: BSD-2-Clause-Patent<br>
+##<br>
+<br>
+[Defines]<br>
+  INF_VERSION                   = 0x00010019<br>
+  BASE_NAME                     = BaseCrcLib<br>
+  FILE_GUID                     = F3BE9A28-78A2-4B02-AB26-D27EE85D9256<br>
+  MODULE_TYPE                   = BASE<br>
+  VERSION_STRING                = 1.0<br>
+  LIBRARY_CLASS                 = CrcLib<br>
+<br>
+[Sources]<br>
+  BaseCrcLib.c<br>
+<br>
+[Packages]<br>
+  MdePkg/MdePkg.dec<br>
+  WhitleyOpenBoardPkg/PlatformPkg.dec<br>
diff --git a/Platform/Intel/WhitleyOpenBoardPkg/PlatformPkg.dsc b/Platform/Intel/WhitleyOpenBoardPkg/PlatformPkg.dsc<br>
index e78a104004..9cdb5bc2f6 100644<br>
--- a/Platform/Intel/WhitleyOpenBoardPkg/PlatformPkg.dsc<br>
+++ b/Platform/Intel/WhitleyOpenBoardPkg/PlatformPkg.dsc<br>
@@ -618,6 +618,7 @@<br>
   PciSegmentInfoLib|$(PLATFORM_PKG)/Pci/Library/PciSegmentInfoLibSimple/PciSegmentInfoLibSimple.inf<br>
   PlatformOpromPolicyLib|$(RP_PKG)/Library/PlatformOpromPolicyLibNull/PlatformOpromPolicyLibNull.inf<br>
   VmgExitLib|UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf<br>
+  CrcLib|WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.inf<br>
<br>
 [LibraryClasses.Common.SEC, LibraryClasses.Common.PEI_CORE, LibraryClasses.Common.PEIM]<br>
   FspWrapperApiLib|IntelFsp2WrapperPkg/Library/BaseFspWrapperApiLib/BaseFspWrapperApiLib.inf<br>
-- <br>
2.27.0.windows.1<br>
<br>
<br>
<br>
<br>
<br>
<br>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr">Pedro Falcato</div></div>


 <div width="1" style="color:white;clear:both">_._,_._,_</div> <hr>   Groups.io Links:<p>   You receive all messages sent to this group.    <p> <a target="_blank" href="https://edk2.groups.io/g/devel/message/87434">View/Reply Online (#87434)</a> |    |  <a target="_blank" href="https://groups.io/mt/89698852/1813853">Mute This Topic</a>  | <a href="https://edk2.groups.io/g/devel/post">New Topic</a><br>    <a href="https://edk2.groups.io/g/devel/editsub/1813853">Your Subscription</a> | <a href="mailto:devel+owner@edk2.groups.io">Contact Group Owner</a> |  <a href="https://edk2.groups.io/g/devel/unsub">Unsubscribe</a>  [edk2-devel-archive@redhat.com]<br> <div width="1" style="color:white;clear:both">_._,_._,_</div>