]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/PeiCrc32GuidedSectionExtractLib/PeiCrc32GuidedSectionExtractLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Library / PeiCrc32GuidedSectionExtractLib / PeiCrc32GuidedSectionExtractLib.c
CommitLineData
a402e129
MK
1/** @file\r
2\r
d1102dba 3 This library registers CRC32 guided section handler\r
a402e129
MK
4 to parse CRC32 encapsulation section and extract raw data.\r
5\r
d1102dba 6Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 7SPDX-License-Identifier: BSD-2-Clause-Patent\r
a402e129
MK
8\r
9**/\r
10\r
11#include <PiPei.h>\r
12#include <Guid/Crc32GuidedSectionExtraction.h>\r
05542f49 13#include <Library/BaseLib.h>\r
a402e129
MK
14#include <Library/ExtractGuidedSectionLib.h>\r
15#include <Library/DebugLib.h>\r
16#include <Library/BaseMemoryLib.h>\r
17\r
18///\r
19/// CRC32 Guided Section header\r
20///\r
21typedef struct {\r
1436aea4
MK
22 EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header\r
23 UINT32 CRC32Checksum; ///< 32bit CRC check sum\r
a402e129
MK
24} CRC32_SECTION_HEADER;\r
25\r
26typedef struct {\r
1436aea4
MK
27 EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader; ///< EFI guided section header\r
28 UINT32 CRC32Checksum; ///< 32bit CRC check sum\r
a402e129
MK
29} CRC32_SECTION2_HEADER;\r
30\r
a402e129
MK
31/**\r
32\r
33 GetInfo gets raw data size and attribute of the input guided section.\r
d1102dba 34 It first checks whether the input guid section is supported.\r
a402e129
MK
35 If not, EFI_INVALID_PARAMETER will return.\r
36\r
37 @param InputSection Buffer containing the input GUIDed section to be processed.\r
38 @param OutputBufferSize The size of OutputBuffer.\r
39 @param ScratchBufferSize The size of ScratchBuffer.\r
40 @param SectionAttribute The attribute of the input guided section.\r
41\r
d1102dba 42 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and\r
3b28e744 43 the attribute of the input section are successfully retrieved.\r
a402e129
MK
44 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r
45\r
46**/\r
47EFI_STATUS\r
48EFIAPI\r
49Crc32GuidedSectionGetInfo (\r
50 IN CONST VOID *InputSection,\r
51 OUT UINT32 *OutputBufferSize,\r
52 OUT UINT32 *ScratchBufferSize,\r
53 OUT UINT16 *SectionAttribute\r
54 )\r
55{\r
56 if (IS_SECTION2 (InputSection)) {\r
57 //\r
58 // Check whether the input guid section is recognized.\r
59 //\r
60 if (!CompareGuid (\r
1436aea4
MK
61 &gEfiCrc32GuidedSectionExtractionGuid,\r
62 &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid)\r
63 ))\r
64 {\r
a402e129
MK
65 return EFI_INVALID_PARAMETER;\r
66 }\r
1436aea4 67\r
a402e129
MK
68 //\r
69 // Retrieve the size and attribute of the input section data.\r
70 //\r
1436aea4 71 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->Attributes;\r
a402e129 72 *ScratchBufferSize = 0;\r
1436aea4 73 *OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset;\r
a402e129
MK
74 } else {\r
75 //\r
76 // Check whether the input guid section is recognized.\r
77 //\r
78 if (!CompareGuid (\r
1436aea4
MK
79 &gEfiCrc32GuidedSectionExtractionGuid,\r
80 &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid)\r
81 ))\r
82 {\r
a402e129
MK
83 return EFI_INVALID_PARAMETER;\r
84 }\r
1436aea4 85\r
a402e129
MK
86 //\r
87 // Retrieve the size and attribute of the input section data.\r
88 //\r
1436aea4 89 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *)InputSection)->Attributes;\r
a402e129 90 *ScratchBufferSize = 0;\r
1436aea4 91 *OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *)InputSection)->DataOffset;\r
a402e129
MK
92 }\r
93\r
94 return EFI_SUCCESS;\r
95}\r
96\r
97/**\r
98\r
99 Extraction handler tries to extract raw data from the input guided section.\r
100 It also does authentication check for 32bit CRC value in the input guided section.\r
d1102dba 101 It first checks whether the input guid section is supported.\r
a402e129
MK
102 If not, EFI_INVALID_PARAMETER will return.\r
103\r
104 @param InputSection Buffer containing the input GUIDed section to be processed.\r
105 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.\r
106 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.\r
107 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the\r
108 authentication status of the output buffer.\r
109\r
110 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.\r
111 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r
112\r
113**/\r
114EFI_STATUS\r
115EFIAPI\r
116Crc32GuidedSectionHandler (\r
117 IN CONST VOID *InputSection,\r
118 OUT VOID **OutputBuffer,\r
e3917e22 119 IN VOID *ScratchBuffer OPTIONAL,\r
a402e129
MK
120 OUT UINT32 *AuthenticationStatus\r
121 )\r
122{\r
1436aea4
MK
123 UINT32 SectionCrc32Checksum;\r
124 UINT32 Crc32Checksum;\r
125 UINT32 OutputBufferSize;\r
a402e129
MK
126\r
127 if (IS_SECTION2 (InputSection)) {\r
128 //\r
129 // Check whether the input guid section is recognized.\r
130 //\r
131 if (!CompareGuid (\r
1436aea4
MK
132 &gEfiCrc32GuidedSectionExtractionGuid,\r
133 &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid)\r
134 ))\r
135 {\r
a402e129
MK
136 return EFI_INVALID_PARAMETER;\r
137 }\r
d1102dba 138\r
a402e129
MK
139 //\r
140 // Get section Crc32 checksum.\r
141 //\r
1436aea4
MK
142 SectionCrc32Checksum = ((CRC32_SECTION2_HEADER *)InputSection)->CRC32Checksum;\r
143 *OutputBuffer = (UINT8 *)InputSection + ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset;\r
144 OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset;\r
a402e129
MK
145\r
146 //\r
147 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set\r
148 //\r
1436aea4 149 ASSERT (((EFI_GUID_DEFINED_SECTION2 *)InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r
a402e129
MK
150 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
151 } else {\r
152 //\r
153 // Check whether the input guid section is recognized.\r
154 //\r
155 if (!CompareGuid (\r
1436aea4
MK
156 &gEfiCrc32GuidedSectionExtractionGuid,\r
157 &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid)\r
158 ))\r
159 {\r
a402e129
MK
160 return EFI_INVALID_PARAMETER;\r
161 }\r
d1102dba 162\r
a402e129
MK
163 //\r
164 // Get section Crc32 checksum.\r
165 //\r
1436aea4
MK
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
a402e129
MK
169\r
170 //\r
171 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set\r
172 //\r
1436aea4 173 ASSERT (((EFI_GUID_DEFINED_SECTION *)InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r
a402e129
MK
174 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
175 }\r
176\r
a402e129
MK
177 //\r
178 // Calculate CRC32 Checksum of Image\r
179 //\r
05542f49
LG
180 Crc32Checksum = CalculateCrc32 (*OutputBuffer, OutputBufferSize);\r
181 if (Crc32Checksum != SectionCrc32Checksum) {\r
a402e129 182 //\r
05542f49 183 // If Crc32 checksum is not matched, AUTH tested failed bit is set.\r
a402e129 184 //\r
05542f49 185 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
a402e129
MK
186 }\r
187\r
188 //\r
189 // Temp solution until PeiCore checks AUTH Status.\r
190 //\r
191 if ((*AuthenticationStatus & (EFI_AUTH_STATUS_TEST_FAILED | EFI_AUTH_STATUS_NOT_TESTED)) != 0) {\r
192 return EFI_ACCESS_DENIED;\r
193 }\r
194\r
195 return EFI_SUCCESS;\r
196}\r
197\r
198/**\r
199 Register the handler to extract CRC32 guided section.\r
200\r
201 @param FileHandle The handle of FFS header the loaded driver.\r
202 @param PeiServices The pointer to the PEI services.\r
203\r
204 @retval EFI_SUCCESS Register successfully.\r
205 @retval EFI_OUT_OF_RESOURCES Not enough memory to register this handler.\r
206\r
207**/\r
208EFI_STATUS\r
209EFIAPI\r
210PeiCrc32GuidedSectionExtractLibConstructor (\r
1436aea4
MK
211 IN EFI_PEI_FILE_HANDLE FileHandle,\r
212 IN CONST EFI_PEI_SERVICES **PeiServices\r
a402e129
MK
213 )\r
214{\r
215 return ExtractGuidedSectionRegisterHandlers (\r
1436aea4
MK
216 &gEfiCrc32GuidedSectionExtractionGuid,\r
217 Crc32GuidedSectionGetInfo,\r
218 Crc32GuidedSectionHandler\r
219 );\r
a402e129 220}\r