]>
Commit | Line | Data |
---|---|---|
a402e129 MK |
1 | /** @file\r |
2 | \r | |
3 | This library registers CRC32 guided section handler \r | |
4 | to parse CRC32 encapsulation section and extract raw data.\r | |
5 | \r | |
6 | Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r | |
7 | This program and the accompanying materials \r | |
8 | are licensed and made available under the terms and conditions of the BSD License \r | |
9 | which accompanies this distribution. The full text of the license may be found at \r | |
10 | http://opensource.org/licenses/bsd-license.php \r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include <PiPei.h>\r | |
18 | #include <Guid/Crc32GuidedSectionExtraction.h>\r | |
19 | #include <Library/ExtractGuidedSectionLib.h>\r | |
20 | #include <Library/DebugLib.h>\r | |
21 | #include <Library/BaseMemoryLib.h>\r | |
22 | \r | |
23 | ///\r | |
24 | /// CRC32 Guided Section header\r | |
25 | ///\r | |
26 | typedef struct {\r | |
27 | EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header\r | |
28 | UINT32 CRC32Checksum; ///< 32bit CRC check sum\r | |
29 | } CRC32_SECTION_HEADER;\r | |
30 | \r | |
31 | typedef struct {\r | |
32 | EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader; ///< EFI guided section header\r | |
33 | UINT32 CRC32Checksum; ///< 32bit CRC check sum\r | |
34 | } CRC32_SECTION2_HEADER;\r | |
35 | \r | |
36 | /**\r | |
37 | This internal function reverses bits for 32bit data.\r | |
38 | \r | |
39 | @param Value The data to be reversed.\r | |
40 | \r | |
41 | @return Data reversed.\r | |
42 | \r | |
43 | **/\r | |
44 | UINT32\r | |
45 | PeiCrc32GuidedSectionExtractLibReverseBits (\r | |
46 | UINT32 Value\r | |
47 | )\r | |
48 | {\r | |
49 | UINTN Index;\r | |
50 | UINT32 NewValue;\r | |
51 | \r | |
52 | NewValue = 0;\r | |
53 | for (Index = 0; Index < 32; Index++) {\r | |
54 | if ((Value & (1 << Index)) != 0) {\r | |
55 | NewValue = NewValue | (1 << (31 - Index));\r | |
56 | }\r | |
57 | }\r | |
58 | \r | |
59 | return NewValue;\r | |
60 | }\r | |
61 | \r | |
62 | /**\r | |
63 | Calculate CRC32 for target data.\r | |
64 | \r | |
65 | @param Data The target data.\r | |
66 | @param DataSize The target data size.\r | |
67 | @param CrcOut The CRC32 for target data.\r | |
68 | \r | |
69 | @retval EFI_SUCCESS The CRC32 for target data is calculated successfully.\r | |
70 | @retval EFI_INVALID_PARAMETER Some parameter is not valid, so the CRC32 is not\r | |
71 | calculated.\r | |
72 | \r | |
73 | **/\r | |
74 | EFI_STATUS\r | |
75 | EFIAPI\r | |
76 | PeiCrc32GuidedSectionExtractLibCalculateCrc32 (\r | |
77 | IN VOID *Data,\r | |
78 | IN UINTN DataSize,\r | |
79 | OUT UINT32 *CrcOut\r | |
80 | )\r | |
81 | {\r | |
82 | UINT32 CrcTable[256];\r | |
83 | UINTN TableEntry;\r | |
84 | UINTN Index;\r | |
85 | UINT32 Value;\r | |
86 | UINT32 Crc;\r | |
87 | UINT8 *Ptr;\r | |
88 | \r | |
89 | if (Data == NULL || DataSize == 0 || CrcOut == NULL) {\r | |
90 | return EFI_INVALID_PARAMETER;\r | |
91 | }\r | |
92 | \r | |
93 | //\r | |
94 | // Initialize CRC32 table.\r | |
95 | //\r | |
96 | for (TableEntry = 0; TableEntry < 256; TableEntry++) {\r | |
97 | Value = PeiCrc32GuidedSectionExtractLibReverseBits ((UINT32) TableEntry);\r | |
98 | for (Index = 0; Index < 8; Index++) {\r | |
99 | if ((Value & 0x80000000) != 0) {\r | |
100 | Value = (Value << 1) ^ 0x04c11db7;\r | |
101 | } else {\r | |
102 | Value = Value << 1;\r | |
103 | }\r | |
104 | }\r | |
105 | CrcTable[TableEntry] = PeiCrc32GuidedSectionExtractLibReverseBits (Value);\r | |
106 | }\r | |
107 | \r | |
108 | //\r | |
109 | // Compute CRC\r | |
110 | //\r | |
111 | Crc = 0xffffffff;\r | |
112 | for (Index = 0, Ptr = Data; Index < DataSize; Index++, Ptr++) {\r | |
113 | Crc = (Crc >> 8) ^ CrcTable[(UINT8) Crc ^ *Ptr];\r | |
114 | }\r | |
115 | \r | |
116 | *CrcOut = Crc ^ 0xffffffff;\r | |
117 | return EFI_SUCCESS;\r | |
118 | }\r | |
119 | \r | |
120 | /**\r | |
121 | \r | |
122 | GetInfo gets raw data size and attribute of the input guided section.\r | |
123 | It first checks whether the input guid section is supported. \r | |
124 | If not, EFI_INVALID_PARAMETER will return.\r | |
125 | \r | |
126 | @param InputSection Buffer containing the input GUIDed section to be processed.\r | |
127 | @param OutputBufferSize The size of OutputBuffer.\r | |
128 | @param ScratchBufferSize The size of ScratchBuffer.\r | |
129 | @param SectionAttribute The attribute of the input guided section.\r | |
130 | \r | |
131 | @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and \r | |
132 | the attribute of the input section are successull retrieved.\r | |
133 | @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r | |
134 | \r | |
135 | **/\r | |
136 | EFI_STATUS\r | |
137 | EFIAPI\r | |
138 | Crc32GuidedSectionGetInfo (\r | |
139 | IN CONST VOID *InputSection,\r | |
140 | OUT UINT32 *OutputBufferSize,\r | |
141 | OUT UINT32 *ScratchBufferSize,\r | |
142 | OUT UINT16 *SectionAttribute\r | |
143 | )\r | |
144 | {\r | |
145 | if (IS_SECTION2 (InputSection)) {\r | |
146 | //\r | |
147 | // Check whether the input guid section is recognized.\r | |
148 | //\r | |
149 | if (!CompareGuid (\r | |
150 | &gEfiCrc32GuidedSectionExtractionGuid,\r | |
151 | &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r | |
152 | return EFI_INVALID_PARAMETER;\r | |
153 | }\r | |
154 | //\r | |
155 | // Retrieve the size and attribute of the input section data.\r | |
156 | //\r | |
157 | *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;\r | |
158 | *ScratchBufferSize = 0;\r | |
159 | *OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r | |
160 | } else {\r | |
161 | //\r | |
162 | // Check whether the input guid section is recognized.\r | |
163 | //\r | |
164 | if (!CompareGuid (\r | |
165 | &gEfiCrc32GuidedSectionExtractionGuid,\r | |
166 | &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r | |
167 | return EFI_INVALID_PARAMETER;\r | |
168 | }\r | |
169 | //\r | |
170 | // Retrieve the size and attribute of the input section data.\r | |
171 | //\r | |
172 | *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r | |
173 | *ScratchBufferSize = 0;\r | |
174 | *OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r | |
175 | }\r | |
176 | \r | |
177 | return EFI_SUCCESS;\r | |
178 | }\r | |
179 | \r | |
180 | /**\r | |
181 | \r | |
182 | Extraction handler tries to extract raw data from the input guided section.\r | |
183 | It also does authentication check for 32bit CRC value in the input guided section.\r | |
184 | It first checks whether the input guid section is supported. \r | |
185 | If not, EFI_INVALID_PARAMETER will return.\r | |
186 | \r | |
187 | @param InputSection Buffer containing the input GUIDed section to be processed.\r | |
188 | @param OutputBuffer Buffer to contain the output raw data allocated by the caller.\r | |
189 | @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.\r | |
190 | @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the\r | |
191 | authentication status of the output buffer.\r | |
192 | \r | |
193 | @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.\r | |
194 | @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r | |
195 | \r | |
196 | **/\r | |
197 | EFI_STATUS\r | |
198 | EFIAPI\r | |
199 | Crc32GuidedSectionHandler (\r | |
200 | IN CONST VOID *InputSection,\r | |
201 | OUT VOID **OutputBuffer,\r | |
202 | IN VOID *ScratchBuffer, OPTIONAL\r | |
203 | OUT UINT32 *AuthenticationStatus\r | |
204 | )\r | |
205 | {\r | |
206 | EFI_STATUS Status;\r | |
207 | UINT32 SectionCrc32Checksum;\r | |
208 | UINT32 Crc32Checksum;\r | |
209 | UINT32 OutputBufferSize;\r | |
210 | \r | |
211 | if (IS_SECTION2 (InputSection)) {\r | |
212 | //\r | |
213 | // Check whether the input guid section is recognized.\r | |
214 | //\r | |
215 | if (!CompareGuid (\r | |
216 | &gEfiCrc32GuidedSectionExtractionGuid,\r | |
217 | &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r | |
218 | return EFI_INVALID_PARAMETER;\r | |
219 | }\r | |
220 | \r | |
221 | //\r | |
222 | // Get section Crc32 checksum.\r | |
223 | //\r | |
224 | SectionCrc32Checksum = ((CRC32_SECTION2_HEADER *) InputSection)->CRC32Checksum;\r | |
225 | *OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r | |
226 | OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r | |
227 | \r | |
228 | //\r | |
229 | // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set\r | |
230 | //\r | |
231 | ASSERT (((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r | |
232 | *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r | |
233 | } else {\r | |
234 | //\r | |
235 | // Check whether the input guid section is recognized.\r | |
236 | //\r | |
237 | if (!CompareGuid (\r | |
238 | &gEfiCrc32GuidedSectionExtractionGuid,\r | |
239 | &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r | |
240 | return EFI_INVALID_PARAMETER;\r | |
241 | }\r | |
242 | \r | |
243 | //\r | |
244 | // Get section Crc32 checksum.\r | |
245 | //\r | |
246 | SectionCrc32Checksum = ((CRC32_SECTION_HEADER *) InputSection)->CRC32Checksum;\r | |
247 | *OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r | |
248 | OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r | |
249 | \r | |
250 | //\r | |
251 | // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set\r | |
252 | //\r | |
253 | ASSERT (((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r | |
254 | *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r | |
255 | }\r | |
256 | \r | |
257 | //\r | |
258 | // Init Checksum value to Zero.\r | |
259 | //\r | |
260 | Crc32Checksum = 0;\r | |
261 | \r | |
262 | //\r | |
263 | // Calculate CRC32 Checksum of Image\r | |
264 | //\r | |
265 | Status = PeiCrc32GuidedSectionExtractLibCalculateCrc32 (*OutputBuffer, OutputBufferSize, &Crc32Checksum);\r | |
266 | if (Status == EFI_SUCCESS) {\r | |
267 | if (Crc32Checksum != SectionCrc32Checksum) {\r | |
268 | //\r | |
269 | // If Crc32 checksum is not matched, AUTH tested failed bit is set.\r | |
270 | //\r | |
271 | *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r | |
272 | }\r | |
273 | } else {\r | |
274 | //\r | |
275 | // If Crc32 checksum is not calculated, AUTH not tested bit is set.\r | |
276 | //\r | |
277 | *AuthenticationStatus |= EFI_AUTH_STATUS_NOT_TESTED;\r | |
278 | }\r | |
279 | \r | |
280 | //\r | |
281 | // Temp solution until PeiCore checks AUTH Status.\r | |
282 | //\r | |
283 | if ((*AuthenticationStatus & (EFI_AUTH_STATUS_TEST_FAILED | EFI_AUTH_STATUS_NOT_TESTED)) != 0) {\r | |
284 | return EFI_ACCESS_DENIED;\r | |
285 | }\r | |
286 | \r | |
287 | return EFI_SUCCESS;\r | |
288 | }\r | |
289 | \r | |
290 | /**\r | |
291 | Register the handler to extract CRC32 guided section.\r | |
292 | \r | |
293 | @param FileHandle The handle of FFS header the loaded driver.\r | |
294 | @param PeiServices The pointer to the PEI services.\r | |
295 | \r | |
296 | @retval EFI_SUCCESS Register successfully.\r | |
297 | @retval EFI_OUT_OF_RESOURCES Not enough memory to register this handler.\r | |
298 | \r | |
299 | **/\r | |
300 | EFI_STATUS\r | |
301 | EFIAPI\r | |
302 | PeiCrc32GuidedSectionExtractLibConstructor (\r | |
303 | IN EFI_PEI_FILE_HANDLE FileHandle,\r | |
304 | IN CONST EFI_PEI_SERVICES **PeiServices\r | |
305 | )\r | |
306 | {\r | |
307 | return ExtractGuidedSectionRegisterHandlers (\r | |
308 | &gEfiCrc32GuidedSectionExtractionGuid,\r | |
309 | Crc32GuidedSectionGetInfo,\r | |
310 | Crc32GuidedSectionHandler\r | |
311 | );\r | |
312 | }\r |