BaseLib 提供了一些计算CheckSum 的函数,用这些可以让我们方便的计算一些协议要求的校验码。
下面编写一个简单的例子:
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/UefiLib.h>
#include <Library/ShellCEntryLib.h>
extern EFI_SYSTEM_TABLE *gST;
extern EFI_BOOT_SERVICES *gBS;
UINT8 Buffer[]={1,2,3,4,5};
/***
Print a welcoming message.
Establishes the main structure of the application.
@retval 0 The application exited normally.
@retval Other An error occurred.
***/
INTN
EFIAPI
ShellAppMain (
IN UINTN Argc,
IN CHAR16 **Argv
)
{
Print(L"[CalculateSum8] = %x\n",CalculateSum8((UINT8 *)Buffer,sizeof(Buffer)));
Print(L"[CalculateCheckSum8]= %x\n",CalculateCheckSum8((UINT8 *)Buffer,sizeof(Buffer)));
Print(L"[CalculateCrc32] = %x\n",CalculateCrc32((UINT8 *)Buffer,sizeof(Buffer)));
return(0);
}
运行结果:
使用在线 CRC32 计算工具【参考1】来验证,结果一致。
上述函数源代码可以在 \MdePkg\Library\BaseLib\CheckSum.c 看到。
参考: