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