3 This library registers CRC32 guided section handler
4 to parse CRC32 encapsulation section and extract raw data.
5 It uses UEFI boot service CalculateCrc32 to authenticate 32 bit CRC value.
7 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Guid/Crc32GuidedSectionExtraction.h>
14 #include <Protocol/SecurityPolicy.h>
15 #include <Library/ExtractGuidedSectionLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
21 /// CRC32 Guided Section header
24 EFI_GUID_DEFINED_SECTION GuidedSectionHeader
; ///< EFI guided section header
25 UINT32 CRC32Checksum
; ///< 32bit CRC check sum
26 } CRC32_SECTION_HEADER
;
29 EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader
; ///< EFI guided section header
30 UINT32 CRC32Checksum
; ///< 32bit CRC check sum
31 } CRC32_SECTION2_HEADER
;
35 GetInfo gets raw data size and attribute of the input guided section.
36 It first checks whether the input guid section is supported.
37 If not, EFI_INVALID_PARAMETER will return.
39 @param InputSection Buffer containing the input GUIDed section to be processed.
40 @param OutputBufferSize The size of OutputBuffer.
41 @param ScratchBufferSize The size of ScratchBuffer.
42 @param SectionAttribute The attribute of the input guided section.
44 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
45 the attribute of the input section are successfully retrieved.
46 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
51 Crc32GuidedSectionGetInfo (
52 IN CONST VOID
*InputSection
,
53 OUT UINT32
*OutputBufferSize
,
54 OUT UINT32
*ScratchBufferSize
,
55 OUT UINT16
*SectionAttribute
58 if (IS_SECTION2 (InputSection
)) {
60 // Check whether the input guid section is recognized.
63 &gEfiCrc32GuidedSectionExtractionGuid
,
64 &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
))) {
65 return EFI_INVALID_PARAMETER
;
68 // Retrieve the size and attribute of the input section data.
70 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->Attributes
;
71 *ScratchBufferSize
= 0;
72 *OutputBufferSize
= SECTION2_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
75 // Check whether the input guid section is recognized.
78 &gEfiCrc32GuidedSectionExtractionGuid
,
79 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
80 return EFI_INVALID_PARAMETER
;
83 // Retrieve the size and attribute of the input section data.
85 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->Attributes
;
86 *ScratchBufferSize
= 0;
87 *OutputBufferSize
= SECTION_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
95 Extraction handler tries to extract raw data from the input guided section.
96 It also does authentication check for 32bit CRC value in the input guided section.
97 It first checks whether the input guid section is supported.
98 If not, EFI_INVALID_PARAMETER will return.
100 @param InputSection Buffer containing the input GUIDed section to be processed.
101 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.
102 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
103 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
104 authentication status of the output buffer.
106 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
107 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
112 Crc32GuidedSectionHandler (
113 IN CONST VOID
*InputSection
,
114 OUT VOID
**OutputBuffer
,
115 IN VOID
*ScratchBuffer
, OPTIONAL
116 OUT UINT32
*AuthenticationStatus
120 UINT32 SectionCrc32Checksum
;
121 UINT32 Crc32Checksum
;
122 UINT32 OutputBufferSize
;
123 VOID
*DummyInterface
;
125 if (IS_SECTION2 (InputSection
)) {
127 // Check whether the input guid section is recognized.
130 &gEfiCrc32GuidedSectionExtractionGuid
,
131 &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
))) {
132 return EFI_INVALID_PARAMETER
;
136 // Get section Crc32 checksum.
138 SectionCrc32Checksum
= ((CRC32_SECTION2_HEADER
*) InputSection
)->CRC32Checksum
;
139 *OutputBuffer
= (UINT8
*) InputSection
+ ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
140 OutputBufferSize
= SECTION2_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
143 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
145 ASSERT (((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
146 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
149 // Check whether the input guid section is recognized.
152 &gEfiCrc32GuidedSectionExtractionGuid
,
153 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
154 return EFI_INVALID_PARAMETER
;
158 // Get section Crc32 checksum.
160 SectionCrc32Checksum
= ((CRC32_SECTION_HEADER
*) InputSection
)->CRC32Checksum
;
161 *OutputBuffer
= (UINT8
*) InputSection
+ ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
162 OutputBufferSize
= SECTION_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
165 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
167 ASSERT (((EFI_GUID_DEFINED_SECTION
*) InputSection
)->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
168 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
172 // Init Checksum value to Zero.
177 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
179 Status
= gBS
->LocateProtocol (&gEfiSecurityPolicyProtocolGuid
, NULL
, &DummyInterface
);
180 if (!EFI_ERROR (Status
)) {
182 // If SecurityPolicy Protocol exist, AUTH platform override bit is set.
184 *AuthenticationStatus
|= EFI_AUTH_STATUS_PLATFORM_OVERRIDE
;
187 // Calculate CRC32 Checksum of Image
189 Status
= gBS
->CalculateCrc32 (*OutputBuffer
, OutputBufferSize
, &Crc32Checksum
);
190 if (Status
== EFI_SUCCESS
) {
191 if (Crc32Checksum
!= SectionCrc32Checksum
) {
193 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
195 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
199 // If Crc32 checksum is not calculated, AUTH not tested bit is set.
201 *AuthenticationStatus
|= EFI_AUTH_STATUS_NOT_TESTED
;
209 Register the handler to extract CRC32 guided section.
211 @param ImageHandle ImageHandle of the loaded driver.
212 @param SystemTable Pointer to the EFI System Table.
214 @retval EFI_SUCCESS Register successfully.
215 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
219 DxeCrc32GuidedSectionExtractLibConstructor (
220 IN EFI_HANDLE ImageHandle
,
221 IN EFI_SYSTEM_TABLE
*SystemTable
224 return ExtractGuidedSectionRegisterHandlers (
225 &gEfiCrc32GuidedSectionExtractionGuid
,
226 Crc32GuidedSectionGetInfo
,
227 Crc32GuidedSectionHandler