]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Universal/Runtime/RuntimeDxe/Crc32.c
Initial import.
[mirror_edk2.git] / EdkModulePkg / Universal / Runtime / RuntimeDxe / Crc32.c
CommitLineData
878ddf1f 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#include "Runtime.h"\r
30\r
31UINT32 mCrcTable[256];\r
32\r
33EFI_STATUS\r
34EFIAPI\r
35RuntimeDriverCalculateCrc32 (\r
36 IN VOID *Data,\r
37 IN UINTN DataSize,\r
38 OUT UINT32 *CrcOut\r
39 )\r
40/*++\r
41\r
42Routine Description:\r
43\r
44Arguments:\r
45\r
46Returns:\r
47\r
48--*/\r
49{\r
50 UINT32 Crc;\r
51 UINTN Index;\r
52 UINT8 *Ptr;\r
53\r
54 if (Data == NULL || DataSize == 0 || CrcOut == NULL) {\r
55 return EFI_INVALID_PARAMETER;\r
56 }\r
57\r
58 Crc = 0xffffffff;\r
59 for (Index = 0, Ptr = Data; Index < DataSize; Index++, Ptr++) {\r
60 Crc = (Crc >> 8) ^ mCrcTable[(UINT8) Crc ^ *Ptr];\r
61 }\r
62\r
63 *CrcOut = Crc ^ 0xffffffff;\r
64 return EFI_SUCCESS;\r
65}\r
66\r
67UINT32\r
68ReverseBits (\r
69 UINT32 Value\r
70 )\r
71/*++\r
72\r
73Routine Description:\r
74\r
75Arguments:\r
76\r
77Returns:\r
78\r
79--*/\r
80{\r
81 UINTN Index;\r
82 UINT32 NewValue;\r
83\r
84 NewValue = 0;\r
85 for (Index = 0; Index < 32; Index++) {\r
86 if (Value & (1 << Index)) {\r
87 NewValue = NewValue | (1 << (31 - Index));\r
88 }\r
89 }\r
90\r
91 return NewValue;\r
92}\r
93\r
94VOID\r
95RuntimeDriverInitializeCrc32Table (\r
96 VOID\r
97 )\r
98/*++\r
99\r
100Routine Description:\r
101\r
102Arguments:\r
103\r
104Returns:\r
105\r
106--*/\r
107{\r
108 UINTN TableEntry;\r
109 UINTN Index;\r
110 UINT32 Value;\r
111\r
112 for (TableEntry = 0; TableEntry < 256; TableEntry++) {\r
113 Value = ReverseBits ((UINT32) TableEntry);\r
114 for (Index = 0; Index < 8; Index++) {\r
115 if (Value & 0x80000000) {\r
116 Value = (Value << 1) ^ 0x04c11db7;\r
117 } else {\r
118 Value = Value << 1;\r
119 }\r
120 }\r
121\r
122 mCrcTable[TableEntry] = ReverseBits (Value);\r
123 }\r
124}\r