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 - 2011, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Guid/Crc32GuidedSectionExtraction.h>
20 #include <Protocol/SecurityPolicy.h>
21 #include <Library/ExtractGuidedSectionLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/UefiBootServicesTableLib.h>
27 /// CRC32 Guided Section header
30 EFI_GUID_DEFINED_SECTION GuidedSectionHeader
; ///< EFI guided section header
31 UINT32 CRC32Checksum
; ///< 32bit CRC check sum
32 } CRC32_SECTION_HEADER
;
35 EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader
; ///< EFI guided section header
36 UINT32 CRC32Checksum
; ///< 32bit CRC check sum
37 } CRC32_SECTION2_HEADER
;
41 GetInfo gets raw data size and attribute of the input guided section.
42 It first checks whether the input guid section is supported.
43 If not, EFI_INVALID_PARAMETER will return.
45 @param InputSection Buffer containing the input GUIDed section to be processed.
46 @param OutputBufferSize The size of OutputBuffer.
47 @param ScratchBufferSize The size of ScratchBuffer.
48 @param SectionAttribute The attribute of the input guided section.
50 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
51 the attribute of the input section are successull retrieved.
52 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
57 Crc32GuidedSectionGetInfo (
58 IN CONST VOID
*InputSection
,
59 OUT UINT32
*OutputBufferSize
,
60 OUT UINT32
*ScratchBufferSize
,
61 OUT UINT16
*SectionAttribute
64 if (IS_SECTION2 (InputSection
)) {
66 // Check whether the input guid section is recognized.
69 &gEfiCrc32GuidedSectionExtractionGuid
,
70 &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
))) {
71 return EFI_INVALID_PARAMETER
;
74 // Retrieve the size and attribute of the input section data.
76 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->Attributes
;
77 *ScratchBufferSize
= 0;
78 *OutputBufferSize
= SECTION2_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
81 // Check whether the input guid section is recognized.
84 &gEfiCrc32GuidedSectionExtractionGuid
,
85 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
86 return EFI_INVALID_PARAMETER
;
89 // Retrieve the size and attribute of the input section data.
91 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->Attributes
;
92 *ScratchBufferSize
= 0;
93 *OutputBufferSize
= SECTION_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
101 Extraction handler tries to extract raw data from the input guided section.
102 It also does authentication check for 32bit CRC value in the input guided section.
103 It first checks whether the input guid section is supported.
104 If not, EFI_INVALID_PARAMETER will return.
106 @param InputSection Buffer containing the input GUIDed section to be processed.
107 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.
108 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
109 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
110 authentication status of the output buffer.
112 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
113 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
118 Crc32GuidedSectionHandler (
119 IN CONST VOID
*InputSection
,
120 OUT VOID
**OutputBuffer
,
121 IN VOID
*ScratchBuffer
, OPTIONAL
122 OUT UINT32
*AuthenticationStatus
126 UINT32 SectionCrc32Checksum
;
127 UINT32 Crc32Checksum
;
128 UINT32 OutputBufferSize
;
129 VOID
*DummyInterface
;
131 if (IS_SECTION2 (InputSection
)) {
133 // Check whether the input guid section is recognized.
136 &gEfiCrc32GuidedSectionExtractionGuid
,
137 &(((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->SectionDefinitionGuid
))) {
138 return EFI_INVALID_PARAMETER
;
142 // Get section Crc32 checksum.
144 SectionCrc32Checksum
= ((CRC32_SECTION2_HEADER
*) InputSection
)->CRC32Checksum
;
145 *OutputBuffer
= (UINT8
*) InputSection
+ ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
146 OutputBufferSize
= SECTION2_SIZE (InputSection
) - ((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->DataOffset
;
149 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
151 ASSERT (((EFI_GUID_DEFINED_SECTION2
*) InputSection
)->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
152 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
155 // Check whether the input guid section is recognized.
158 &gEfiCrc32GuidedSectionExtractionGuid
,
159 &(((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 // Init Checksum value to Zero.
183 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
185 Status
= gBS
->LocateProtocol (&gEfiSecurityPolicyProtocolGuid
, NULL
, &DummyInterface
);
186 if (!EFI_ERROR (Status
)) {
188 // If SecurityPolicy Protocol exist, AUTH platform override bit is set.
190 *AuthenticationStatus
|= EFI_AUTH_STATUS_PLATFORM_OVERRIDE
;
193 // Calculate CRC32 Checksum of Image
195 Status
= gBS
->CalculateCrc32 (*OutputBuffer
, OutputBufferSize
, &Crc32Checksum
);
196 if (Status
== EFI_SUCCESS
) {
197 if (Crc32Checksum
!= SectionCrc32Checksum
) {
199 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
201 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
205 // If Crc32 checksum is not calculated, AUTH not tested bit is set.
207 *AuthenticationStatus
|= EFI_AUTH_STATUS_NOT_TESTED
;
215 Register the handler to extract CRC32 guided section.
217 @param ImageHandle ImageHandle of the loaded driver.
218 @param SystemTable Pointer to the EFI System Table.
220 @retval EFI_SUCCESS Register successfully.
221 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
225 DxeCrc32GuidedSectionExtractLibConstructor (
226 IN EFI_HANDLE ImageHandle
,
227 IN EFI_SYSTEM_TABLE
*SystemTable
230 return ExtractGuidedSectionRegisterHandlers (
231 &gEfiCrc32GuidedSectionExtractionGuid
,
232 Crc32GuidedSectionGetInfo
,
233 Crc32GuidedSectionHandler