]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/Runtime/RuntimeDxe/Crc32.c
Merge R8->R9 tracker 5935 and 7080 to update runtime arch protocol to DxeCis 0.91...
[mirror_edk2.git] / EdkModulePkg / Universal / Runtime / 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 #include "Runtime.h"
30
31 UINT32 mCrcTable[256];
32
33 EFI_STATUS
34 EFIAPI
35 RuntimeDriverCalculateCrc32 (
36 IN VOID *Data,
37 IN UINTN DataSize,
38 OUT UINT32 *CrcOut
39 )
40 /*++
41
42 Routine Description:
43
44 Calculate CRC32 for target data
45
46 Arguments:
47
48 Data - The target data.
49 DataSize - The target data size.
50 CrcOut - The CRC32 for target data.
51
52 Returns:
53
54 EFI_SUCCESS - The CRC32 for target data is calculated successfully.
55 EFI_INVALID_PARAMETER - Some parameter is not valid, so the CRC32 is not
56 calculated.
57
58 --*/
59 {
60 UINT32 Crc;
61 UINTN Index;
62 UINT8 *Ptr;
63
64 if (Data == NULL || DataSize == 0 || CrcOut == NULL) {
65 return EFI_INVALID_PARAMETER;
66 }
67
68 Crc = 0xffffffff;
69 for (Index = 0, Ptr = Data; Index < DataSize; Index++, Ptr++) {
70 Crc = (Crc >> 8) ^ mCrcTable[(UINT8) Crc ^ *Ptr];
71 }
72
73 *CrcOut = Crc ^ 0xffffffff;
74 return EFI_SUCCESS;
75 }
76
77 UINT32
78 ReverseBits (
79 UINT32 Value
80 )
81 /*++
82
83 Routine Description:
84
85 Reverse bits for 32bit data.
86
87 Arguments:
88
89 Value - the data to be reversed.
90
91 Returns:
92
93 UINT32 data reversed.
94
95 --*/
96 {
97 UINTN Index;
98 UINT32 NewValue;
99
100 NewValue = 0;
101 for (Index = 0; Index < 32; Index++) {
102 if (Value & (1 << Index)) {
103 NewValue = NewValue | (1 << (31 - Index));
104 }
105 }
106
107 return NewValue;
108 }
109
110 VOID
111 RuntimeDriverInitializeCrc32Table (
112 VOID
113 )
114 /*++
115
116 Routine Description:
117
118 Initialize CRC32 table.
119
120 Arguments:
121
122 None.
123
124 Returns:
125
126 None.
127
128 --*/
129 {
130 UINTN TableEntry;
131 UINTN Index;
132 UINT32 Value;
133
134 for (TableEntry = 0; TableEntry < 256; TableEntry++) {
135 Value = ReverseBits ((UINT32) TableEntry);
136 for (Index = 0; Index < 8; Index++) {
137 if (Value & 0x80000000) {
138 Value = (Value << 1) ^ 0x04c11db7;
139 } else {
140 Value = Value << 1;
141 }
142 }
143
144 mCrcTable[TableEntry] = ReverseBits (Value);
145 }
146 }