]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/PeiCrc32GuidedSectionExtractLib/PeiCrc32GuidedSectionExtractLib.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[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
22 EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header\r
23 UINT32 CRC32Checksum; ///< 32bit CRC check sum\r
24} CRC32_SECTION_HEADER;\r
25\r
26typedef struct {\r
27 EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader; ///< EFI guided section header\r
28 UINT32 CRC32Checksum; ///< 32bit CRC check sum\r
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
61 &gEfiCrc32GuidedSectionExtractionGuid,\r
62 &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r
63 return EFI_INVALID_PARAMETER;\r
64 }\r
65 //\r
66 // Retrieve the size and attribute of the input section data.\r
67 //\r
68 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;\r
69 *ScratchBufferSize = 0;\r
70 *OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r
71 } else {\r
72 //\r
73 // Check whether the input guid section is recognized.\r
74 //\r
75 if (!CompareGuid (\r
76 &gEfiCrc32GuidedSectionExtractionGuid,\r
77 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
78 return EFI_INVALID_PARAMETER;\r
79 }\r
80 //\r
81 // Retrieve the size and attribute of the input section data.\r
82 //\r
83 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r
84 *ScratchBufferSize = 0;\r
85 *OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r
86 }\r
87\r
88 return EFI_SUCCESS;\r
89}\r
90\r
91/**\r
92\r
93 Extraction handler tries to extract raw data from the input guided section.\r
94 It also does authentication check for 32bit CRC value in the input guided section.\r
d1102dba 95 It first checks whether the input guid section is supported.\r
a402e129
MK
96 If not, EFI_INVALID_PARAMETER will return.\r
97\r
98 @param InputSection Buffer containing the input GUIDed section to be processed.\r
99 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.\r
100 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.\r
101 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the\r
102 authentication status of the output buffer.\r
103\r
104 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.\r
105 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.\r
106\r
107**/\r
108EFI_STATUS\r
109EFIAPI\r
110Crc32GuidedSectionHandler (\r
111 IN CONST VOID *InputSection,\r
112 OUT VOID **OutputBuffer,\r
113 IN VOID *ScratchBuffer, OPTIONAL\r
114 OUT UINT32 *AuthenticationStatus\r
115 )\r
116{\r
a402e129
MK
117 UINT32 SectionCrc32Checksum;\r
118 UINT32 Crc32Checksum;\r
119 UINT32 OutputBufferSize;\r
120\r
121 if (IS_SECTION2 (InputSection)) {\r
122 //\r
123 // Check whether the input guid section is recognized.\r
124 //\r
125 if (!CompareGuid (\r
126 &gEfiCrc32GuidedSectionExtractionGuid,\r
127 &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r
128 return EFI_INVALID_PARAMETER;\r
129 }\r
d1102dba 130\r
a402e129
MK
131 //\r
132 // Get section Crc32 checksum.\r
133 //\r
134 SectionCrc32Checksum = ((CRC32_SECTION2_HEADER *) InputSection)->CRC32Checksum;\r
135 *OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r
136 OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;\r
137\r
138 //\r
139 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set\r
140 //\r
141 ASSERT (((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r
142 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
143 } else {\r
144 //\r
145 // Check whether the input guid section is recognized.\r
146 //\r
147 if (!CompareGuid (\r
148 &gEfiCrc32GuidedSectionExtractionGuid,\r
149 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
150 return EFI_INVALID_PARAMETER;\r
151 }\r
d1102dba 152\r
a402e129
MK
153 //\r
154 // Get section Crc32 checksum.\r
155 //\r
156 SectionCrc32Checksum = ((CRC32_SECTION_HEADER *) InputSection)->CRC32Checksum;\r
157 *OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r
158 OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;\r
159\r
160 //\r
161 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set\r
162 //\r
163 ASSERT (((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r
164 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
165 }\r
166\r
a402e129
MK
167 //\r
168 // Calculate CRC32 Checksum of Image\r
169 //\r
05542f49
LG
170 Crc32Checksum = CalculateCrc32 (*OutputBuffer, OutputBufferSize);\r
171 if (Crc32Checksum != SectionCrc32Checksum) {\r
a402e129 172 //\r
05542f49 173 // If Crc32 checksum is not matched, AUTH tested failed bit is set.\r
a402e129 174 //\r
05542f49 175 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
a402e129
MK
176 }\r
177\r
178 //\r
179 // Temp solution until PeiCore checks AUTH Status.\r
180 //\r
181 if ((*AuthenticationStatus & (EFI_AUTH_STATUS_TEST_FAILED | EFI_AUTH_STATUS_NOT_TESTED)) != 0) {\r
182 return EFI_ACCESS_DENIED;\r
183 }\r
184\r
185 return EFI_SUCCESS;\r
186}\r
187\r
188/**\r
189 Register the handler to extract CRC32 guided section.\r
190\r
191 @param FileHandle The handle of FFS header the loaded driver.\r
192 @param PeiServices The pointer to the PEI services.\r
193\r
194 @retval EFI_SUCCESS Register successfully.\r
195 @retval EFI_OUT_OF_RESOURCES Not enough memory to register this handler.\r
196\r
197**/\r
198EFI_STATUS\r
199EFIAPI\r
200PeiCrc32GuidedSectionExtractLibConstructor (\r
201 IN EFI_PEI_FILE_HANDLE FileHandle,\r
202 IN CONST EFI_PEI_SERVICES **PeiServices\r
203 )\r
204{\r
205 return ExtractGuidedSectionRegisterHandlers (\r
206 &gEfiCrc32GuidedSectionExtractionGuid,\r
207 Crc32GuidedSectionGetInfo,\r
208 Crc32GuidedSectionHandler\r
209 );\r
210}\r