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