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