3 This library registers CRC32 guided section handler
4 to parse CRC32 encapsulation section and extract raw data.
6 Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Guid/Crc32GuidedSectionExtraction.h>
19 #include <Library/BaseLib.h>
20 #include <Library/ExtractGuidedSectionLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/BaseMemoryLib.h>
25 /// CRC32 Guided Section header
28 EFI_GUID_DEFINED_SECTION GuidedSectionHeader
; ///< EFI guided section header
29 UINT32 CRC32Checksum
; ///< 32bit CRC check sum
30 } CRC32_SECTION_HEADER
;
33 EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader
; ///< EFI guided section header
34 UINT32 CRC32Checksum
; ///< 32bit CRC check sum
35 } CRC32_SECTION2_HEADER
;
39 GetInfo gets raw data size and attribute of the input guided section.
40 It first checks whether the input guid section is supported.
41 If not, EFI_INVALID_PARAMETER will return.
43 @param InputSection Buffer containing the input GUIDed section to be processed.
44 @param OutputBufferSize The size of OutputBuffer.
45 @param ScratchBufferSize The size of ScratchBuffer.
46 @param SectionAttribute The attribute of the input guided section.
48 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
49 the attribute of the input section are successfully retrieved.
50 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
55 Crc32GuidedSectionGetInfo (
56 IN CONST VOID
*InputSection
,
57 OUT UINT32
*OutputBufferSize
,
58 OUT UINT32
*ScratchBufferSize
,
59 OUT UINT16
*SectionAttribute
62 if (IS_SECTION2 (InputSection
)) {
64 // Check whether the input guid section is recognized.
67 &gEfiCrc32GuidedSectionExtractionGuid
,
68 &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
))) {
69 return EFI_INVALID_PARAMETER
;
72 // Retrieve the size and attribute of the input section data.
74 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->Attributes
;
75 *ScratchBufferSize
= 0;
76 *OutputBufferSize
= SECTION2_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
79 // Check whether the input guid section is recognized.
82 &gEfiCrc32GuidedSectionExtractionGuid
,
83 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
84 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
))) {
134 return EFI_INVALID_PARAMETER
;
138 // Get section Crc32 checksum.
140 SectionCrc32Checksum
= ((CRC32_SECTION2_HEADER
*) InputSection
)->CRC32Checksum
;
141 *OutputBuffer
= (UINT8
*) InputSection
+ ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
142 OutputBufferSize
= SECTION2_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
145 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
147 ASSERT (((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
148 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
151 // Check whether the input guid section is recognized.
154 &gEfiCrc32GuidedSectionExtractionGuid
,
155 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
156 return EFI_INVALID_PARAMETER
;
160 // Get section Crc32 checksum.
162 SectionCrc32Checksum
= ((CRC32_SECTION_HEADER
*) InputSection
)->CRC32Checksum
;
163 *OutputBuffer
= (UINT8
*) InputSection
+ ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
164 OutputBufferSize
= SECTION_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
167 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
169 ASSERT (((EFI_GUID_DEFINED_SECTION
*) InputSection
)->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
170 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
174 // Calculate CRC32 Checksum of Image
176 Crc32Checksum
= CalculateCrc32 (*OutputBuffer
, OutputBufferSize
);
177 if (Crc32Checksum
!= SectionCrc32Checksum
) {
179 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
181 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
185 // Temp solution until PeiCore checks AUTH Status.
187 if ((*AuthenticationStatus
& (EFI_AUTH_STATUS_TEST_FAILED
| EFI_AUTH_STATUS_NOT_TESTED
)) != 0) {
188 return EFI_ACCESS_DENIED
;
195 Register the handler to extract CRC32 guided section.
197 @param FileHandle The handle of FFS header the loaded driver.
198 @param PeiServices The pointer to the PEI services.
200 @retval EFI_SUCCESS Register successfully.
201 @retval EFI_OUT_OF_RESOURCES Not enough memory to register this handler.
206 PeiCrc32GuidedSectionExtractLibConstructor (
207 IN EFI_PEI_FILE_HANDLE FileHandle
,
208 IN CONST EFI_PEI_SERVICES
**PeiServices
211 return ExtractGuidedSectionRegisterHandlers (
212 &gEfiCrc32GuidedSectionExtractionGuid
,
213 Crc32GuidedSectionGetInfo
,
214 Crc32GuidedSectionHandler