]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeCrc32GuidedSectionExtractLib/DxeCrc32GuidedSectionExtractLib.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Library / DxeCrc32GuidedSectionExtractLib / DxeCrc32GuidedSectionExtractLib.c
CommitLineData
504214c4 1/** @file\r
d51ffc1f 2\r
d1102dba 3 This library registers CRC32 guided section handler\r
109e9a61
LG
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
504214c4 6\r
d1102dba
LG
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
d51ffc1f 15\r
504214c4 16**/\r
d51ffc1f
LG
17\r
18#include <PiDxe.h>\r
76f1dde2 19#include <Guid/Crc32GuidedSectionExtraction.h>\r
d51ffc1f
LG
20#include <Protocol/SecurityPolicy.h>\r
21#include <Library/ExtractGuidedSectionLib.h>\r
22#include <Library/DebugLib.h>\r
e6c560aa 23#include <Library/BaseMemoryLib.h>\r
d51ffc1f
LG
24#include <Library/UefiBootServicesTableLib.h>\r
25\r
109e9a61
LG
26///\r
27/// CRC32 Guided Section header\r
28///\r
d51ffc1f 29typedef struct {\r
109e9a61
LG
30 EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header\r
31 UINT32 CRC32Checksum; ///< 32bit CRC check sum\r
d51ffc1f
LG
32} CRC32_SECTION_HEADER;\r
33\r
30f001ca
SZ
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
5d69642d
LG
39/**\r
40\r
109e9a61 41 GetInfo gets raw data size and attribute of the input guided section.\r
d1102dba 42 It first checks whether the input guid section is supported.\r
109e9a61 43 If not, EFI_INVALID_PARAMETER will return.\r
5d69642d
LG
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
d1102dba 50 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and\r
3b28e744 51 the attribute of the input section are successfully retrieved.\r
5d69642d
LG
52 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r
53\r
54**/\r
d51ffc1f
LG
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
d51ffc1f 63{\r
30f001ca
SZ
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
e6c560aa 85 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
30f001ca
SZ
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
e6c560aa 94 }\r
d51ffc1f
LG
95\r
96 return EFI_SUCCESS;\r
97}\r
98\r
5d69642d
LG
99/**\r
100\r
109e9a61
LG
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
d1102dba 103 It first checks whether the input guid section is supported.\r
109e9a61 104 If not, EFI_INVALID_PARAMETER will return.\r
5d69642d
LG
105\r
106 @param InputSection Buffer containing the input GUIDed section to be processed.\r
109e9a61 107 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.\r
5d69642d
LG
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
109e9a61 110 authentication status of the output buffer.\r
5d69642d
LG
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
d51ffc1f
LG
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
d51ffc1f
LG
124{\r
125 EFI_STATUS Status;\r
30f001ca 126 UINT32 SectionCrc32Checksum;\r
d51ffc1f
LG
127 UINT32 Crc32Checksum;\r
128 UINT32 OutputBufferSize;\r
129 VOID *DummyInterface;\r
130\r
30f001ca
SZ
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
d1102dba 140\r
30f001ca
SZ
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
e6c560aa 159 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
30f001ca
SZ
160 return EFI_INVALID_PARAMETER;\r
161 }\r
d1102dba 162\r
30f001ca
SZ
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
5d69642d
LG
177 //\r
178 // Init Checksum value to Zero.\r
179 //\r
d51ffc1f 180 Crc32Checksum = 0;\r
d51ffc1f
LG
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
5d69642d
LG
187 //\r
188 // If SecurityPolicy Protocol exist, AUTH platform override bit is set.\r
189 //\r
d51ffc1f
LG
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
30f001ca 197 if (Crc32Checksum != SectionCrc32Checksum) {\r
5d69642d
LG
198 //\r
199 // If Crc32 checksum is not matched, AUTH tested failed bit is set.\r
200 //\r
d51ffc1f
LG
201 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
202 }\r
203 } else {\r
5d69642d
LG
204 //\r
205 // If Crc32 checksum is not calculated, AUTH not tested bit is set.\r
206 //\r
d51ffc1f
LG
207 *AuthenticationStatus |= EFI_AUTH_STATUS_NOT_TESTED;\r
208 }\r
209 }\r
210\r
211 return EFI_SUCCESS;\r
212}\r
213\r
5d69642d 214/**\r
109e9a61 215 Register the handler to extract CRC32 guided section.\r
5d69642d 216\r
cebc8d48
LG
217 @param ImageHandle ImageHandle of the loaded driver.\r
218 @param SystemTable Pointer to the EFI System Table.\r
219\r
109e9a61
LG
220 @retval EFI_SUCCESS Register successfully.\r
221 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.\r
d51ffc1f
LG
222**/\r
223EFI_STATUS\r
224EFIAPI\r
225DxeCrc32GuidedSectionExtractLibConstructor (\r
a5ff00a6 226 IN EFI_HANDLE ImageHandle,\r
227 IN EFI_SYSTEM_TABLE *SystemTable\r
d51ffc1f
LG
228 )\r
229{\r
230 return ExtractGuidedSectionRegisterHandlers (\r
76f1dde2 231 &gEfiCrc32GuidedSectionExtractionGuid,\r
d51ffc1f
LG
232 Crc32GuidedSectionGetInfo,\r
233 Crc32GuidedSectionHandler\r
234 );\r
235}\r
236\r