2008-04-09 09:07:50 +02:00
|
|
|
/** @file
|
2007-10-17 09:52:02 +02:00
|
|
|
|
2018-06-27 15:08:52 +02:00
|
|
|
This library registers CRC32 guided section handler
|
2009-01-04 04:20:55 +01:00
|
|
|
to parse CRC32 encapsulation section and extract raw data.
|
|
|
|
It uses UEFI boot service CalculateCrc32 to authenticate 32 bit CRC value.
|
2008-04-09 09:07:50 +02:00
|
|
|
|
2018-06-27 15:08:52 +02:00
|
|
|
Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
|
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
2007-10-17 09:52:02 +02:00
|
|
|
|
2008-04-09 09:07:50 +02:00
|
|
|
**/
|
2007-10-17 09:52:02 +02:00
|
|
|
|
|
|
|
#include <PiDxe.h>
|
2009-02-24 08:53:20 +01:00
|
|
|
#include <Guid/Crc32GuidedSectionExtraction.h>
|
2007-10-17 09:52:02 +02:00
|
|
|
#include <Protocol/SecurityPolicy.h>
|
|
|
|
#include <Library/ExtractGuidedSectionLib.h>
|
|
|
|
#include <Library/DebugLib.h>
|
2007-10-18 11:12:59 +02:00
|
|
|
#include <Library/BaseMemoryLib.h>
|
2007-10-17 09:52:02 +02:00
|
|
|
#include <Library/UefiBootServicesTableLib.h>
|
|
|
|
|
2009-01-04 04:20:55 +01:00
|
|
|
///
|
|
|
|
/// CRC32 Guided Section header
|
|
|
|
///
|
2007-10-17 09:52:02 +02:00
|
|
|
typedef struct {
|
2009-01-04 04:20:55 +01:00
|
|
|
EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header
|
|
|
|
UINT32 CRC32Checksum; ///< 32bit CRC check sum
|
2007-10-17 09:52:02 +02:00
|
|
|
} CRC32_SECTION_HEADER;
|
|
|
|
|
2011-10-27 10:45:50 +02:00
|
|
|
typedef struct {
|
|
|
|
EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader; ///< EFI guided section header
|
|
|
|
UINT32 CRC32Checksum; ///< 32bit CRC check sum
|
|
|
|
} CRC32_SECTION2_HEADER;
|
|
|
|
|
2008-07-09 15:33:20 +02:00
|
|
|
/**
|
|
|
|
|
2009-01-04 04:20:55 +01:00
|
|
|
GetInfo gets raw data size and attribute of the input guided section.
|
2018-06-27 15:08:52 +02:00
|
|
|
It first checks whether the input guid section is supported.
|
2009-01-04 04:20:55 +01:00
|
|
|
If not, EFI_INVALID_PARAMETER will return.
|
2008-07-09 15:33:20 +02:00
|
|
|
|
|
|
|
@param InputSection Buffer containing the input GUIDed section to be processed.
|
|
|
|
@param OutputBufferSize The size of OutputBuffer.
|
|
|
|
@param ScratchBufferSize The size of ScratchBuffer.
|
|
|
|
@param SectionAttribute The attribute of the input guided section.
|
|
|
|
|
2018-06-27 15:08:52 +02:00
|
|
|
@retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
|
2016-10-19 09:01:25 +02:00
|
|
|
the attribute of the input section are successfully retrieved.
|
2008-07-09 15:33:20 +02:00
|
|
|
@retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
|
|
|
|
|
|
|
|
**/
|
2007-10-17 09:52:02 +02:00
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
Crc32GuidedSectionGetInfo (
|
|
|
|
IN CONST VOID *InputSection,
|
|
|
|
OUT UINT32 *OutputBufferSize,
|
|
|
|
OUT UINT32 *ScratchBufferSize,
|
|
|
|
OUT UINT16 *SectionAttribute
|
|
|
|
)
|
|
|
|
{
|
2011-10-27 10:45:50 +02:00
|
|
|
if (IS_SECTION2 (InputSection)) {
|
|
|
|
//
|
|
|
|
// Check whether the input guid section is recognized.
|
|
|
|
//
|
|
|
|
if (!CompareGuid (
|
|
|
|
&gEfiCrc32GuidedSectionExtractionGuid,
|
|
|
|
&(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Retrieve the size and attribute of the input section data.
|
|
|
|
//
|
|
|
|
*SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;
|
|
|
|
*ScratchBufferSize = 0;
|
|
|
|
*OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Check whether the input guid section is recognized.
|
|
|
|
//
|
|
|
|
if (!CompareGuid (
|
|
|
|
&gEfiCrc32GuidedSectionExtractionGuid,
|
2007-10-18 11:12:59 +02:00
|
|
|
&(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
|
2011-10-27 10:45:50 +02:00
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Retrieve the size and attribute of the input section data.
|
|
|
|
//
|
|
|
|
*SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
|
|
|
|
*ScratchBufferSize = 0;
|
|
|
|
*OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
|
2007-10-18 11:12:59 +02:00
|
|
|
}
|
2007-10-17 09:52:02 +02:00
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-07-09 15:33:20 +02:00
|
|
|
/**
|
|
|
|
|
2009-01-04 04:20:55 +01:00
|
|
|
Extraction handler tries to extract raw data from the input guided section.
|
|
|
|
It also does authentication check for 32bit CRC value in the input guided section.
|
2018-06-27 15:08:52 +02:00
|
|
|
It first checks whether the input guid section is supported.
|
2009-01-04 04:20:55 +01:00
|
|
|
If not, EFI_INVALID_PARAMETER will return.
|
2008-07-09 15:33:20 +02:00
|
|
|
|
|
|
|
@param InputSection Buffer containing the input GUIDed section to be processed.
|
2009-01-04 04:20:55 +01:00
|
|
|
@param OutputBuffer Buffer to contain the output raw data allocated by the caller.
|
2008-07-09 15:33:20 +02:00
|
|
|
@param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
|
|
|
|
@param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
|
2009-01-04 04:20:55 +01:00
|
|
|
authentication status of the output buffer.
|
2008-07-09 15:33:20 +02:00
|
|
|
|
|
|
|
@retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
|
|
|
|
@retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
|
|
|
|
|
|
|
|
**/
|
2007-10-17 09:52:02 +02:00
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
Crc32GuidedSectionHandler (
|
|
|
|
IN CONST VOID *InputSection,
|
|
|
|
OUT VOID **OutputBuffer,
|
|
|
|
IN VOID *ScratchBuffer, OPTIONAL
|
|
|
|
OUT UINT32 *AuthenticationStatus
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
2011-10-27 10:45:50 +02:00
|
|
|
UINT32 SectionCrc32Checksum;
|
2007-10-17 09:52:02 +02:00
|
|
|
UINT32 Crc32Checksum;
|
|
|
|
UINT32 OutputBufferSize;
|
|
|
|
VOID *DummyInterface;
|
|
|
|
|
2011-10-27 10:45:50 +02:00
|
|
|
if (IS_SECTION2 (InputSection)) {
|
|
|
|
//
|
|
|
|
// Check whether the input guid section is recognized.
|
|
|
|
//
|
|
|
|
if (!CompareGuid (
|
|
|
|
&gEfiCrc32GuidedSectionExtractionGuid,
|
|
|
|
&(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
2018-06-27 15:08:52 +02:00
|
|
|
|
2011-10-27 10:45:50 +02:00
|
|
|
//
|
|
|
|
// Get section Crc32 checksum.
|
|
|
|
//
|
|
|
|
SectionCrc32Checksum = ((CRC32_SECTION2_HEADER *) InputSection)->CRC32Checksum;
|
|
|
|
*OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;
|
|
|
|
OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
|
|
|
|
//
|
|
|
|
ASSERT (((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
|
|
|
|
*AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Check whether the input guid section is recognized.
|
|
|
|
//
|
|
|
|
if (!CompareGuid (
|
|
|
|
&gEfiCrc32GuidedSectionExtractionGuid,
|
2007-10-18 11:12:59 +02:00
|
|
|
&(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
|
2011-10-27 10:45:50 +02:00
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
2018-06-27 15:08:52 +02:00
|
|
|
|
2011-10-27 10:45:50 +02:00
|
|
|
//
|
|
|
|
// Get section Crc32 checksum.
|
|
|
|
//
|
|
|
|
SectionCrc32Checksum = ((CRC32_SECTION_HEADER *) InputSection)->CRC32Checksum;
|
|
|
|
*OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
|
|
|
|
OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
|
|
|
|
//
|
|
|
|
ASSERT (((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
|
|
|
|
*AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
|
|
|
|
}
|
|
|
|
|
2008-07-09 15:33:20 +02:00
|
|
|
//
|
|
|
|
// Init Checksum value to Zero.
|
|
|
|
//
|
2007-10-17 09:52:02 +02:00
|
|
|
Crc32Checksum = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
|
|
|
|
//
|
|
|
|
Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);
|
|
|
|
if (!EFI_ERROR (Status)) {
|
2008-07-09 15:33:20 +02:00
|
|
|
//
|
|
|
|
// If SecurityPolicy Protocol exist, AUTH platform override bit is set.
|
|
|
|
//
|
2007-10-17 09:52:02 +02:00
|
|
|
*AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Calculate CRC32 Checksum of Image
|
|
|
|
//
|
|
|
|
Status = gBS->CalculateCrc32 (*OutputBuffer, OutputBufferSize, &Crc32Checksum);
|
|
|
|
if (Status == EFI_SUCCESS) {
|
2011-10-27 10:45:50 +02:00
|
|
|
if (Crc32Checksum != SectionCrc32Checksum) {
|
2008-07-09 15:33:20 +02:00
|
|
|
//
|
|
|
|
// If Crc32 checksum is not matched, AUTH tested failed bit is set.
|
|
|
|
//
|
2007-10-17 09:52:02 +02:00
|
|
|
*AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
|
|
|
|
}
|
|
|
|
} else {
|
2008-07-09 15:33:20 +02:00
|
|
|
//
|
|
|
|
// If Crc32 checksum is not calculated, AUTH not tested bit is set.
|
|
|
|
//
|
2007-10-17 09:52:02 +02:00
|
|
|
*AuthenticationStatus |= EFI_AUTH_STATUS_NOT_TESTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-07-09 15:33:20 +02:00
|
|
|
/**
|
2009-01-04 04:20:55 +01:00
|
|
|
Register the handler to extract CRC32 guided section.
|
2008-07-09 15:33:20 +02:00
|
|
|
|
2008-12-11 10:30:13 +01:00
|
|
|
@param ImageHandle ImageHandle of the loaded driver.
|
|
|
|
@param SystemTable Pointer to the EFI System Table.
|
|
|
|
|
2009-01-04 04:20:55 +01:00
|
|
|
@retval EFI_SUCCESS Register successfully.
|
|
|
|
@retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
|
2007-10-17 09:52:02 +02:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
DxeCrc32GuidedSectionExtractLibConstructor (
|
2008-11-27 01:44:29 +01:00
|
|
|
IN EFI_HANDLE ImageHandle,
|
|
|
|
IN EFI_SYSTEM_TABLE *SystemTable
|
2007-10-17 09:52:02 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return ExtractGuidedSectionRegisterHandlers (
|
2009-02-24 08:53:20 +01:00
|
|
|
&gEfiCrc32GuidedSectionExtractionGuid,
|
2007-10-17 09:52:02 +02:00
|
|
|
Crc32GuidedSectionGetInfo,
|
|
|
|
Crc32GuidedSectionHandler
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|