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