]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/RuntimeDxe/Crc32.c
Clean up ExtendedHiiLib, HiiLib, IfrSupportLib, ExtendedIfrSupportLib for Doxygen...
[mirror_edk2.git] / MdeModulePkg / Core / RuntimeDxe / Crc32.c
CommitLineData
fb0b259e 1/** @file\r
2 CalculateCrc32 Boot Services as defined in DXE CIS.\r
3\r
4Copyright (c) 2006, Intel Corporation. <BR>\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
f2abdc91 9\r
fb0b259e 10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
f2abdc91 12\r
13Module Name:\r
14\r
15 Crc32.c\r
16\r
17Abstract:\r
18\r
f2abdc91 19 This Boot Services is in the Runtime Driver because this service is\r
20 also required by SetVirtualAddressMap() when the EFI System Table and\r
fb0b259e 21 EFI Runtime Services Table are converted from physical address to\r
f2abdc91 22 virtual addresses. This requires that the 32-bit CRC be recomputed.\r
23\r
fb0b259e 24**/\r
f2abdc91 25\r
ed7748fe 26\r
f2abdc91 27#include <PiDxe.h>\r
28\r
29UINT32 mCrcTable[256];\r
30\r
fb0b259e 31/**\r
32 Calculate CRC32 for target data.\r
33\r
34 @param Len The target data.\r
35 @param DataSize The target data size.\r
36 @param CrcOut The CRC32 for target data.\r
37\r
38 @retval EFI_SUCCESS The CRC32 for target data is calculated successfully.\r
39 @retval EFI_INVALID_PARAMETER Some parameter is not valid, so the CRC32 is not\r
40 calculated.\r
41\r
42**/\r
f2abdc91 43EFI_STATUS\r
44EFIAPI\r
45RuntimeDriverCalculateCrc32 (\r
46 IN VOID *Data,\r
47 IN UINTN DataSize,\r
48 OUT UINT32 *CrcOut\r
49 )\r
f2abdc91 50{\r
51 UINT32 Crc;\r
52 UINTN Index;\r
53 UINT8 *Ptr;\r
54\r
55 if (Data == NULL || DataSize == 0 || CrcOut == NULL) {\r
56 return EFI_INVALID_PARAMETER;\r
57 }\r
58\r
59 Crc = 0xffffffff;\r
60 for (Index = 0, Ptr = Data; Index < DataSize; Index++, Ptr++) {\r
61 Crc = (Crc >> 8) ^ mCrcTable[(UINT8) Crc ^ *Ptr];\r
62 }\r
63\r
64 *CrcOut = Crc ^ 0xffffffff;\r
65 return EFI_SUCCESS;\r
66}\r
67\r
f2abdc91 68\r
fb0b259e 69/**\r
f2abdc91 70 Reverse bits for 32bit data.\r
71\r
fb0b259e 72 @param Value The data to be reversed.\r
f2abdc91 73\r
fb0b259e 74 @retrun Data reversed.\r
f2abdc91 75\r
fb0b259e 76**/\r
77STATIC\r
78UINT32\r
79ReverseBits (\r
80 UINT32 Value\r
81 )\r
f2abdc91 82{\r
83 UINTN Index;\r
84 UINT32 NewValue;\r
85\r
86 NewValue = 0;\r
87 for (Index = 0; Index < 32; Index++) {\r
88 if (Value & (1 << Index)) {\r
89 NewValue = NewValue | (1 << (31 - Index));\r
90 }\r
91 }\r
92\r
93 return NewValue;\r
94}\r
95\r
fb0b259e 96/**\r
f2abdc91 97 Initialize CRC32 table.\r
98\r
fb0b259e 99 @param None\r
f2abdc91 100\r
fb0b259e 101 @retrun None\r
f2abdc91 102\r
fb0b259e 103**/\r
104VOID\r
105RuntimeDriverInitializeCrc32Table (\r
106 VOID\r
107 )\r
f2abdc91 108{\r
109 UINTN TableEntry;\r
110 UINTN Index;\r
111 UINT32 Value;\r
112\r
113 for (TableEntry = 0; TableEntry < 256; TableEntry++) {\r
114 Value = ReverseBits ((UINT32) TableEntry);\r
115 for (Index = 0; Index < 8; Index++) {\r
116 if (Value & 0x80000000) {\r
117 Value = (Value << 1) ^ 0x04c11db7;\r
118 } else {\r
119 Value = Value << 1;\r
120 }\r
121 }\r
122\r
123 mCrcTable[TableEntry] = ReverseBits (Value);\r
124 }\r
125}\r