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 - 2008, Intel Corporation
8 All rights reserved. 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>
26 #define EFI_SECITON_SIZE_MASK 0x00ffffff
29 /// CRC32 Guided Section header
32 EFI_GUID_DEFINED_SECTION GuidedSectionHeader
; ///< EFI guided section header
33 UINT32 CRC32Checksum
; ///< 32bit CRC check sum
34 } CRC32_SECTION_HEADER
;
38 GetInfo gets raw data size and attribute of the input guided section.
39 It first checks whether the input guid section is supported.
40 If not, EFI_INVALID_PARAMETER will return.
42 @param InputSection Buffer containing the input GUIDed section to be processed.
43 @param OutputBufferSize The size of OutputBuffer.
44 @param ScratchBufferSize The size of ScratchBuffer.
45 @param SectionAttribute The attribute of the input guided section.
47 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
48 the attribute of the input section are successull retrieved.
49 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
54 Crc32GuidedSectionGetInfo (
55 IN CONST VOID
*InputSection
,
56 OUT UINT32
*OutputBufferSize
,
57 OUT UINT32
*ScratchBufferSize
,
58 OUT UINT16
*SectionAttribute
62 // Check whether the input guid section is recognized.
65 &gEfiCrc32GuidedSectionExtractionGuid
,
66 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
67 return EFI_INVALID_PARAMETER
;
70 // Retrieve the size and attribute of the input section data.
72 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->Attributes
;
73 *ScratchBufferSize
= 0;
74 *OutputBufferSize
= *(UINT32
*) (((EFI_COMMON_SECTION_HEADER
*) InputSection
)->Size
) & EFI_SECITON_SIZE_MASK
;
75 *OutputBufferSize
-= ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
82 Extraction handler tries to extract raw data from the input guided section.
83 It also does authentication check for 32bit CRC value in the input guided section.
84 It first checks whether the input guid section is supported.
85 If not, EFI_INVALID_PARAMETER will return.
87 @param InputSection Buffer containing the input GUIDed section to be processed.
88 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.
89 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
90 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
91 authentication status of the output buffer.
93 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
94 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
99 Crc32GuidedSectionHandler (
100 IN CONST VOID
*InputSection
,
101 OUT VOID
**OutputBuffer
,
102 IN VOID
*ScratchBuffer
, OPTIONAL
103 OUT UINT32
*AuthenticationStatus
107 CRC32_SECTION_HEADER
*Crc32SectionHeader
;
108 UINT32 Crc32Checksum
;
109 UINT32 OutputBufferSize
;
110 VOID
*DummyInterface
;
113 // Check whether the input guid section is recognized.
116 &gEfiCrc32GuidedSectionExtractionGuid
,
117 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
118 return EFI_INVALID_PARAMETER
;
122 // Init Checksum value to Zero.
126 // Points to the Crc32 section header
128 Crc32SectionHeader
= (CRC32_SECTION_HEADER
*) InputSection
;
129 *OutputBuffer
= (UINT8
*) InputSection
+ Crc32SectionHeader
->GuidedSectionHeader
.DataOffset
;
130 OutputBufferSize
= *(UINT32
*) (((EFI_COMMON_SECTION_HEADER
*) InputSection
)->Size
) & EFI_SECITON_SIZE_MASK
;
131 OutputBufferSize
-= Crc32SectionHeader
->GuidedSectionHeader
.DataOffset
;
134 // Implictly CRC32 GUIDed section should have STATUS_VALID bit set
136 ASSERT (Crc32SectionHeader
->GuidedSectionHeader
.Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
137 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
140 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
142 Status
= gBS
->LocateProtocol (&gEfiSecurityPolicyProtocolGuid
, NULL
, &DummyInterface
);
143 if (!EFI_ERROR (Status
)) {
145 // If SecurityPolicy Protocol exist, AUTH platform override bit is set.
147 *AuthenticationStatus
|= EFI_AUTH_STATUS_PLATFORM_OVERRIDE
;
150 // Calculate CRC32 Checksum of Image
152 Status
= gBS
->CalculateCrc32 (*OutputBuffer
, OutputBufferSize
, &Crc32Checksum
);
153 if (Status
== EFI_SUCCESS
) {
154 if (Crc32Checksum
!= Crc32SectionHeader
->CRC32Checksum
) {
156 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
158 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
162 // If Crc32 checksum is not calculated, AUTH not tested bit is set.
164 *AuthenticationStatus
|= EFI_AUTH_STATUS_NOT_TESTED
;
172 Register the handler to extract CRC32 guided section.
174 @param ImageHandle ImageHandle of the loaded driver.
175 @param SystemTable Pointer to the EFI System Table.
177 @retval EFI_SUCCESS Register successfully.
178 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
182 DxeCrc32GuidedSectionExtractLibConstructor (
183 IN EFI_HANDLE ImageHandle
,
184 IN EFI_SYSTEM_TABLE
*SystemTable
187 return ExtractGuidedSectionRegisterHandlers (
188 &gEfiCrc32GuidedSectionExtractionGuid
,
189 Crc32GuidedSectionGetInfo
,
190 Crc32GuidedSectionHandler