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