]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/RuntimeDxe/Crc32.c
Clean up Runtime for Doxygen comments requirement.
[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
9fc78752 34 @param Data The target data.\r
fb0b259e 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
9fc78752 71 This is a internal function.\r
f2abdc91 72\r
fb0b259e 73 @param Value The data to be reversed.\r
f2abdc91 74\r
9fc78752 75 @return Data reversed.\r
f2abdc91 76\r
fb0b259e 77**/\r
fb0b259e 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
9fc78752 88 if ((Value & (1 << Index)) != 0) {\r
f2abdc91 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
fb0b259e 98**/\r
99VOID\r
100RuntimeDriverInitializeCrc32Table (\r
101 VOID\r
102 )\r
f2abdc91 103{\r
104 UINTN TableEntry;\r
105 UINTN Index;\r
106 UINT32 Value;\r
107\r
108 for (TableEntry = 0; TableEntry < 256; TableEntry++) {\r
109 Value = ReverseBits ((UINT32) TableEntry);\r
110 for (Index = 0; Index < 8; Index++) {\r
9fc78752 111 if ((Value & 0x80000000) != 0) {\r
f2abdc91 112 Value = (Value << 1) ^ 0x04c11db7;\r
113 } else {\r
114 Value = Value << 1;\r
115 }\r
116 }\r
117\r
118 mCrcTable[TableEntry] = ReverseBits (Value);\r
119 }\r
120}\r