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