forked from tianocore/edk2-platforms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WhitleyOpenBoardPkg/BaseCrcLib: Add library for CRC16
Core only supports CRC32, this library adds CRC16 support. Cc: Nate DeSimone <[email protected]> Cc: Chasel Chiu <[email protected]> Signed-off-by: Isaac Oram <[email protected]> Reviewed-by: Nate DeSimone <[email protected]>
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
Platform/Intel/WhitleyOpenBoardPkg/Include/Library/CrcLib.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** @file | ||
Interface header file for the CRC library class. | ||
@copyright | ||
Copyright 2016 - 2018 Intel Corporation. <BR> | ||
SPDX-License-Identifier: BSD-2-Clause-Patent | ||
**/ | ||
|
||
#ifndef _CRC_LIB_H_ | ||
#define _CRC_LIB_H_ | ||
|
||
#include <Uefi.h> | ||
|
||
/** | ||
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_ |
71 changes: 71 additions & 0 deletions
71
Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** @file | ||
Base implementation of the CRC library class. | ||
@copyright | ||
Copyright 2016 - 2018 Intel Corporation. <BR> | ||
SPDX-License-Identifier: BSD-2-Clause-Patent | ||
**/ | ||
|
||
#include <Base.h> | ||
#include <Library/CrcLib.h> | ||
|
||
/** | ||
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; | ||
} |
23 changes: 23 additions & 0 deletions
23
Platform/Intel/WhitleyOpenBoardPkg/Library/BaseCrcLib/BaseCrcLib.inf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## @file | ||
# Base implementation of the CRC library class. | ||
# | ||
# @copyright | ||
# Copyright 2016 Intel Corporation. <BR> | ||
# | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters