3 Implements CRC32 guided section handler to parse CRC32 encapsulation section,
4 extract data and authenticate 32 bit CRC value.
6 Copyright (c) 2007 - 2008, Intel Corporation
7 All rights reserved. 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 <Protocol/Crc32GuidedSectionExtraction.h>
19 #include <Protocol/SecurityPolicy.h>
20 #include <Library/ExtractGuidedSectionLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
26 EFI_GUID_DEFINED_SECTION GuidedSectionHeader
;
28 } CRC32_SECTION_HEADER
;
32 The implementation of Crc32 guided section GetInfo() to get
33 size and attribute of the guided section.
35 @param InputSection Buffer containing the input GUIDed section to be processed.
36 @param OutputBufferSize The size of OutputBuffer.
37 @param ScratchBufferSize The size of ScratchBuffer.
38 @param SectionAttribute The attribute of the input guided section.
40 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
41 the attribute of the input section are successull retrieved.
42 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
47 Crc32GuidedSectionGetInfo (
48 IN CONST VOID
*InputSection
,
49 OUT UINT32
*OutputBufferSize
,
50 OUT UINT32
*ScratchBufferSize
,
51 OUT UINT16
*SectionAttribute
55 // Check whether the input guid section is recognized.
58 &gEfiCrc32GuidedSectionExtractionProtocolGuid
,
59 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
60 return EFI_INVALID_PARAMETER
;
63 // Retrieve the size and attribute of the input section data.
65 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->Attributes
;
66 *ScratchBufferSize
= 0;
67 *OutputBufferSize
= *(UINT32
*) (((EFI_COMMON_SECTION_HEADER
*) InputSection
)->Size
) & 0x00ffffff;
68 *OutputBufferSize
-= ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
75 The implementation of Crc32 Guided section extraction to get the section data.
77 @param InputSection Buffer containing the input GUIDed section to be processed.
78 @param OutputBuffer to contain the output data, which is allocated by the caller.
79 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
80 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
81 authentication status of the output buffer.
83 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
84 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
89 Crc32GuidedSectionHandler (
90 IN CONST VOID
*InputSection
,
91 OUT VOID
**OutputBuffer
,
92 IN VOID
*ScratchBuffer
, OPTIONAL
93 OUT UINT32
*AuthenticationStatus
97 CRC32_SECTION_HEADER
*Crc32SectionHeader
;
99 UINT32 OutputBufferSize
;
100 VOID
*DummyInterface
;
103 // Check whether the input guid section is recognized.
106 &gEfiCrc32GuidedSectionExtractionProtocolGuid
,
107 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
108 return EFI_INVALID_PARAMETER
;
112 // Init Checksum value to Zero.
116 // Points to the Crc32 section header
118 Crc32SectionHeader
= (CRC32_SECTION_HEADER
*) InputSection
;
119 *OutputBuffer
= (UINT8
*) InputSection
+ Crc32SectionHeader
->GuidedSectionHeader
.DataOffset
;
120 OutputBufferSize
= *(UINT32
*) (((EFI_COMMON_SECTION_HEADER
*) InputSection
)->Size
) & 0x00ffffff;
121 OutputBufferSize
-= Crc32SectionHeader
->GuidedSectionHeader
.DataOffset
;
124 // Implictly CRC32 GUIDed section should have STATUS_VALID bit set
126 ASSERT (Crc32SectionHeader
->GuidedSectionHeader
.Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
127 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
130 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
132 Status
= gBS
->LocateProtocol (&gEfiSecurityPolicyProtocolGuid
, NULL
, &DummyInterface
);
133 if (!EFI_ERROR (Status
)) {
135 // If SecurityPolicy Protocol exist, AUTH platform override bit is set.
137 *AuthenticationStatus
|= EFI_AUTH_STATUS_PLATFORM_OVERRIDE
;
140 // Calculate CRC32 Checksum of Image
142 Status
= gBS
->CalculateCrc32 (*OutputBuffer
, OutputBufferSize
, &Crc32Checksum
);
143 if (Status
== EFI_SUCCESS
) {
144 if (Crc32Checksum
!= Crc32SectionHeader
->CRC32Checksum
) {
146 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
148 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
152 // If Crc32 checksum is not calculated, AUTH not tested bit is set.
154 *AuthenticationStatus
|= EFI_AUTH_STATUS_NOT_TESTED
;
162 Register Crc32 section handler.
164 @retval RETURN_SUCCESS Register successfully.
165 @retval RETURN_OUT_OF_RESOURCES No enough memory to register this handler.
169 DxeCrc32GuidedSectionExtractLibConstructor (
172 return ExtractGuidedSectionRegisterHandlers (
173 &gEfiCrc32GuidedSectionExtractionProtocolGuid
,
174 Crc32GuidedSectionGetInfo
,
175 Crc32GuidedSectionHandler