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
))) {
63 return EFI_INVALID_PARAMETER
;
66 // Retrieve the size and attribute of the input section data.
68 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->Attributes
;
69 *ScratchBufferSize
= 0;
70 *OutputBufferSize
= SECTION2_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
73 // Check whether the input guid section is recognized.
76 &gEfiCrc32GuidedSectionExtractionGuid
,
77 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
78 return EFI_INVALID_PARAMETER
;
81 // Retrieve the size and attribute of the input section data.
83 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->Attributes
;
84 *ScratchBufferSize
= 0;
85 *OutputBufferSize
= SECTION_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
93 Extraction handler tries to extract raw data from the input guided section.
94 It also does authentication check for 32bit CRC value in the input guided section.
95 It first checks whether the input guid section is supported.
96 If not, EFI_INVALID_PARAMETER will return.
98 @param InputSection Buffer containing the input GUIDed section to be processed.
99 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.
100 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
101 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
102 authentication status of the output buffer.
104 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
105 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
110 Crc32GuidedSectionHandler (
111 IN CONST VOID
*InputSection
,
112 OUT VOID
**OutputBuffer
,
113 IN VOID
*ScratchBuffer
, OPTIONAL
114 OUT UINT32
*AuthenticationStatus
117 UINT32 SectionCrc32Checksum
;
118 UINT32 Crc32Checksum
;
119 UINT32 OutputBufferSize
;
121 if (IS_SECTION2 (InputSection
)) {
123 // Check whether the input guid section is recognized.
126 &gEfiCrc32GuidedSectionExtractionGuid
,
127 &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
))) {
128 return EFI_INVALID_PARAMETER
;
132 // Get section Crc32 checksum.
134 SectionCrc32Checksum
= ((CRC32_SECTION2_HEADER
*) InputSection
)->CRC32Checksum
;
135 *OutputBuffer
= (UINT8
*) InputSection
+ ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
136 OutputBufferSize
= SECTION2_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
139 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
141 ASSERT (((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
142 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
145 // Check whether the input guid section is recognized.
148 &gEfiCrc32GuidedSectionExtractionGuid
,
149 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
150 return EFI_INVALID_PARAMETER
;
154 // Get section Crc32 checksum.
156 SectionCrc32Checksum
= ((CRC32_SECTION_HEADER
*) InputSection
)->CRC32Checksum
;
157 *OutputBuffer
= (UINT8
*) InputSection
+ ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
158 OutputBufferSize
= SECTION_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
161 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
163 ASSERT (((EFI_GUID_DEFINED_SECTION
*) InputSection
)->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
164 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
168 // Calculate CRC32 Checksum of Image
170 Crc32Checksum
= CalculateCrc32 (*OutputBuffer
, OutputBufferSize
);
171 if (Crc32Checksum
!= SectionCrc32Checksum
) {
173 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
175 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
179 // Temp solution until PeiCore checks AUTH Status.
181 if ((*AuthenticationStatus
& (EFI_AUTH_STATUS_TEST_FAILED
| EFI_AUTH_STATUS_NOT_TESTED
)) != 0) {
182 return EFI_ACCESS_DENIED
;
189 Register the handler to extract CRC32 guided section.
191 @param FileHandle The handle of FFS header the loaded driver.
192 @param PeiServices The pointer to the PEI services.
194 @retval EFI_SUCCESS Register successfully.
195 @retval EFI_OUT_OF_RESOURCES Not enough memory to register this handler.
200 PeiCrc32GuidedSectionExtractLibConstructor (
201 IN EFI_PEI_FILE_HANDLE FileHandle
,
202 IN CONST EFI_PEI_SERVICES
**PeiServices
205 return ExtractGuidedSectionRegisterHandlers (
206 &gEfiCrc32GuidedSectionExtractionGuid
,
207 Crc32GuidedSectionGetInfo
,
208 Crc32GuidedSectionHandler