3 This library registers CRC32 guided section handler
4 to parse CRC32 encapsulation section and extract raw data.
6 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Guid/Crc32GuidedSectionExtraction.h>
13 #include <Library/BaseLib.h>
14 #include <Library/ExtractGuidedSectionLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/BaseMemoryLib.h>
19 /// CRC32 Guided Section header
22 EFI_GUID_DEFINED_SECTION GuidedSectionHeader
; ///< EFI guided section header
23 UINT32 CRC32Checksum
; ///< 32bit CRC check sum
24 } CRC32_SECTION_HEADER
;
27 EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader
; ///< EFI guided section header
28 UINT32 CRC32Checksum
; ///< 32bit CRC check sum
29 } CRC32_SECTION2_HEADER
;
33 GetInfo gets raw data size and attribute of the input guided section.
34 It first checks whether the input guid section is supported.
35 If not, EFI_INVALID_PARAMETER will return.
37 @param InputSection Buffer containing the input GUIDed section to be processed.
38 @param OutputBufferSize The size of OutputBuffer.
39 @param ScratchBufferSize The size of ScratchBuffer.
40 @param SectionAttribute The attribute of the input guided section.
42 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
43 the attribute of the input section are successfully retrieved.
44 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
49 Crc32GuidedSectionGetInfo (
50 IN CONST VOID
*InputSection
,
51 OUT UINT32
*OutputBufferSize
,
52 OUT UINT32
*ScratchBufferSize
,
53 OUT UINT16
*SectionAttribute
56 if (IS_SECTION2 (InputSection
)) {
58 // Check whether the input guid section is recognized.
61 &gEfiCrc32GuidedSectionExtractionGuid
,
62 &(((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->SectionDefinitionGuid
)
65 return EFI_INVALID_PARAMETER
;
69 // Retrieve the size and attribute of the input section data.
71 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->Attributes
;
72 *ScratchBufferSize
= 0;
73 *OutputBufferSize
= SECTION2_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->DataOffset
;
76 // Check whether the input guid section is recognized.
79 &gEfiCrc32GuidedSectionExtractionGuid
,
80 &(((EFI_GUID_DEFINED_SECTION
*)InputSection
)->SectionDefinitionGuid
)
83 return EFI_INVALID_PARAMETER
;
87 // Retrieve the size and attribute of the input section data.
89 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION
*)InputSection
)->Attributes
;
90 *ScratchBufferSize
= 0;
91 *OutputBufferSize
= SECTION_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION
*)InputSection
)->DataOffset
;
99 Extraction handler tries to extract raw data from the input guided section.
100 It also does authentication check for 32bit CRC value in the input guided section.
101 It first checks whether the input guid section is supported.
102 If not, EFI_INVALID_PARAMETER will return.
104 @param InputSection Buffer containing the input GUIDed section to be processed.
105 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.
106 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
107 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
108 authentication status of the output buffer.
110 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
111 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
116 Crc32GuidedSectionHandler (
117 IN CONST VOID
*InputSection
,
118 OUT VOID
**OutputBuffer
,
119 IN VOID
*ScratchBuffer OPTIONAL
,
120 OUT UINT32
*AuthenticationStatus
123 UINT32 SectionCrc32Checksum
;
124 UINT32 Crc32Checksum
;
125 UINT32 OutputBufferSize
;
127 if (IS_SECTION2 (InputSection
)) {
129 // Check whether the input guid section is recognized.
132 &gEfiCrc32GuidedSectionExtractionGuid
,
133 &(((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->SectionDefinitionGuid
)
136 return EFI_INVALID_PARAMETER
;
140 // Get section Crc32 checksum.
142 SectionCrc32Checksum
= ((CRC32_SECTION2_HEADER
*)InputSection
)->CRC32Checksum
;
143 *OutputBuffer
= (UINT8
*)InputSection
+ ((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->DataOffset
;
144 OutputBufferSize
= SECTION2_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->DataOffset
;
147 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
149 ASSERT (((EFI_GUID_DEFINED_SECTION2
*)InputSection
)->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
150 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
153 // Check whether the input guid section is recognized.
156 &gEfiCrc32GuidedSectionExtractionGuid
,
157 &(((EFI_GUID_DEFINED_SECTION
*)InputSection
)->SectionDefinitionGuid
)
160 return EFI_INVALID_PARAMETER
;
164 // Get section Crc32 checksum.
166 SectionCrc32Checksum
= ((CRC32_SECTION_HEADER
*)InputSection
)->CRC32Checksum
;
167 *OutputBuffer
= (UINT8
*)InputSection
+ ((EFI_GUID_DEFINED_SECTION
*)InputSection
)->DataOffset
;
168 OutputBufferSize
= SECTION_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION
*)InputSection
)->DataOffset
;
171 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
173 ASSERT (((EFI_GUID_DEFINED_SECTION
*)InputSection
)->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
174 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
178 // Calculate CRC32 Checksum of Image
180 Crc32Checksum
= CalculateCrc32 (*OutputBuffer
, OutputBufferSize
);
181 if (Crc32Checksum
!= SectionCrc32Checksum
) {
183 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
185 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
189 // Temp solution until PeiCore checks AUTH Status.
191 if ((*AuthenticationStatus
& (EFI_AUTH_STATUS_TEST_FAILED
| EFI_AUTH_STATUS_NOT_TESTED
)) != 0) {
192 return EFI_ACCESS_DENIED
;
199 Register the handler to extract CRC32 guided section.
201 @param FileHandle The handle of FFS header the loaded driver.
202 @param PeiServices The pointer to the PEI services.
204 @retval EFI_SUCCESS Register successfully.
205 @retval EFI_OUT_OF_RESOURCES Not enough memory to register this handler.
210 PeiCrc32GuidedSectionExtractLibConstructor (
211 IN EFI_PEI_FILE_HANDLE FileHandle
,
212 IN CONST EFI_PEI_SERVICES
**PeiServices
215 return ExtractGuidedSectionRegisterHandlers (
216 &gEfiCrc32GuidedSectionExtractionGuid
,
217 Crc32GuidedSectionGetInfo
,
218 Crc32GuidedSectionHandler