]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Library/DxeCrc32GuidedSectionExtractLib/DxeCrc32GuidedSectionExtractLib.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Library / DxeCrc32GuidedSectionExtractLib / DxeCrc32GuidedSectionExtractLib.c
... / ...
CommitLineData
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 It uses UEFI boot service CalculateCrc32 to authenticate 32 bit CRC value.\r
6\r
7Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
8This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
12\r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <PiDxe.h>\r
19#include <Guid/Crc32GuidedSectionExtraction.h>\r
20#include <Protocol/SecurityPolicy.h>\r
21#include <Library/ExtractGuidedSectionLib.h>\r
22#include <Library/DebugLib.h>\r
23#include <Library/BaseMemoryLib.h>\r
24#include <Library/UefiBootServicesTableLib.h>\r
25\r
26///\r
27/// CRC32 Guided Section header\r
28///\r
29typedef struct {\r
30 EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header\r
31 UINT32 CRC32Checksum; ///< 32bit CRC check sum\r
32} CRC32_SECTION_HEADER;\r
33\r
34typedef struct {\r
35 EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader; ///< EFI guided section header\r
36 UINT32 CRC32Checksum; ///< 32bit CRC check sum\r
37} CRC32_SECTION2_HEADER;\r
38\r
39/**\r
40\r
41 GetInfo gets raw data size and attribute of the input guided section.\r
42 It first checks whether the input guid section is supported.\r
43 If not, EFI_INVALID_PARAMETER will return.\r
44\r
45 @param InputSection Buffer containing the input GUIDed section to be processed.\r
46 @param OutputBufferSize The size of OutputBuffer.\r
47 @param ScratchBufferSize The size of ScratchBuffer.\r
48 @param SectionAttribute The attribute of the input guided section.\r
49\r
50 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and\r
51 the attribute of the input section are successfully retrieved.\r
52 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r
53\r
54**/\r
55EFI_STATUS\r
56EFIAPI\r
57Crc32GuidedSectionGetInfo (\r
58 IN CONST VOID *InputSection,\r
59 OUT UINT32 *OutputBufferSize,\r
60 OUT UINT32 *ScratchBufferSize,\r
61 OUT UINT16 *SectionAttribute\r
62 )\r
63{\r
64 if (IS_SECTION2 (InputSection)) {\r
65 //\r
66 // Check whether the input guid section is recognized.\r
67 //\r
68 if (!CompareGuid (\r
69 &gEfiCrc32GuidedSectionExtractionGuid,\r
70 &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r
71 return EFI_INVALID_PARAMETER;\r
72 }\r
73 //\r
74 // Retrieve the size and attribute of the input section data.\r
75 //\r
76 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;\r
77 *ScratchBufferSize = 0;\r
78 *OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r
79 } else {\r
80 //\r
81 // Check whether the input guid section is recognized.\r
82 //\r
83 if (!CompareGuid (\r
84 &gEfiCrc32GuidedSectionExtractionGuid,\r
85 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
86 return EFI_INVALID_PARAMETER;\r
87 }\r
88 //\r
89 // Retrieve the size and attribute of the input section data.\r
90 //\r
91 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r
92 *ScratchBufferSize = 0;\r
93 *OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r
94 }\r
95\r
96 return EFI_SUCCESS;\r
97}\r
98\r
99/**\r
100\r
101 Extraction handler tries to extract raw data from the input guided section.\r
102 It also does authentication check for 32bit CRC value in the input guided section.\r
103 It first checks whether the input guid section is supported.\r
104 If not, EFI_INVALID_PARAMETER will return.\r
105\r
106 @param InputSection Buffer containing the input GUIDed section to be processed.\r
107 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.\r
108 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.\r
109 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the\r
110 authentication status of the output buffer.\r
111\r
112 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.\r
113 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r
114\r
115**/\r
116EFI_STATUS\r
117EFIAPI\r
118Crc32GuidedSectionHandler (\r
119 IN CONST VOID *InputSection,\r
120 OUT VOID **OutputBuffer,\r
121 IN VOID *ScratchBuffer, OPTIONAL\r
122 OUT UINT32 *AuthenticationStatus\r
123 )\r
124{\r
125 EFI_STATUS Status;\r
126 UINT32 SectionCrc32Checksum;\r
127 UINT32 Crc32Checksum;\r
128 UINT32 OutputBufferSize;\r
129 VOID *DummyInterface;\r
130\r
131 if (IS_SECTION2 (InputSection)) {\r
132 //\r
133 // Check whether the input guid section is recognized.\r
134 //\r
135 if (!CompareGuid (\r
136 &gEfiCrc32GuidedSectionExtractionGuid,\r
137 &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r
138 return EFI_INVALID_PARAMETER;\r
139 }\r
140\r
141 //\r
142 // Get section Crc32 checksum.\r
143 //\r
144 SectionCrc32Checksum = ((CRC32_SECTION2_HEADER *) InputSection)->CRC32Checksum;\r
145 *OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r
146 OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r
147\r
148 //\r
149 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set\r
150 //\r
151 ASSERT (((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r
152 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
153 } else {\r
154 //\r
155 // Check whether the input guid section is recognized.\r
156 //\r
157 if (!CompareGuid (\r
158 &gEfiCrc32GuidedSectionExtractionGuid,\r
159 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
160 return EFI_INVALID_PARAMETER;\r
161 }\r
162\r
163 //\r
164 // Get section Crc32 checksum.\r
165 //\r
166 SectionCrc32Checksum = ((CRC32_SECTION_HEADER *) InputSection)->CRC32Checksum;\r
167 *OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r
168 OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r
169\r
170 //\r
171 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set\r
172 //\r
173 ASSERT (((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r
174 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
175 }\r
176\r
177 //\r
178 // Init Checksum value to Zero.\r
179 //\r
180 Crc32Checksum = 0;\r
181\r
182 //\r
183 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.\r
184 //\r
185 Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);\r
186 if (!EFI_ERROR (Status)) {\r
187 //\r
188 // If SecurityPolicy Protocol exist, AUTH platform override bit is set.\r
189 //\r
190 *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;\r
191 } else {\r
192 //\r
193 // Calculate CRC32 Checksum of Image\r
194 //\r
195 Status = gBS->CalculateCrc32 (*OutputBuffer, OutputBufferSize, &Crc32Checksum);\r
196 if (Status == EFI_SUCCESS) {\r
197 if (Crc32Checksum != SectionCrc32Checksum) {\r
198 //\r
199 // If Crc32 checksum is not matched, AUTH tested failed bit is set.\r
200 //\r
201 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
202 }\r
203 } else {\r
204 //\r
205 // If Crc32 checksum is not calculated, AUTH not tested bit is set.\r
206 //\r
207 *AuthenticationStatus |= EFI_AUTH_STATUS_NOT_TESTED;\r
208 }\r
209 }\r
210\r
211 return EFI_SUCCESS;\r
212}\r
213\r
214/**\r
215 Register the handler to extract CRC32 guided section.\r
216\r
217 @param ImageHandle ImageHandle of the loaded driver.\r
218 @param SystemTable Pointer to the EFI System Table.\r
219\r
220 @retval EFI_SUCCESS Register successfully.\r
221 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.\r
222**/\r
223EFI_STATUS\r
224EFIAPI\r
225DxeCrc32GuidedSectionExtractLibConstructor (\r
226 IN EFI_HANDLE ImageHandle,\r
227 IN EFI_SYSTEM_TABLE *SystemTable\r
228 )\r
229{\r
230 return ExtractGuidedSectionRegisterHandlers (\r
231 &gEfiCrc32GuidedSectionExtractionGuid,\r
232 Crc32GuidedSectionGetInfo,\r
233 Crc32GuidedSectionHandler\r
234 );\r
235}\r
236\r