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