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