2008-04-09 09:07:50 +02:00
|
|
|
/** @file
|
2007-10-17 09:52:02 +02:00
|
|
|
|
2008-04-09 09:07:50 +02:00
|
|
|
Implements CRC32 guided section handler to parse CRC32 encapsulation section,
|
|
|
|
extract data and authenticate 32 bit CRC value.
|
|
|
|
|
|
|
|
Copyright (c) 2007 - 2008, Intel Corporation
|
2007-10-17 09:52:02 +02:00
|
|
|
All rights reserved. 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.
|
|
|
|
|
2008-04-09 09:07:50 +02:00
|
|
|
**/
|
2007-10-17 09:52:02 +02:00
|
|
|
|
|
|
|
#include <PiDxe.h>
|
|
|
|
#include <Protocol/Crc32GuidedSectionExtraction.h>
|
|
|
|
#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>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
EFI_GUID_DEFINED_SECTION GuidedSectionHeader;
|
|
|
|
UINT32 CRC32Checksum;
|
|
|
|
} CRC32_SECTION_HEADER;
|
|
|
|
|
2008-07-09 15:33:20 +02:00
|
|
|
/**
|
|
|
|
|
|
|
|
The implementation of Crc32 guided section GetInfo() to get
|
|
|
|
size and attribute of the guided section.
|
|
|
|
|
|
|
|
@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.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
|
|
|
|
the attribute of the input section are successull retrieved.
|
|
|
|
@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
|
|
|
|
)
|
|
|
|
{
|
2008-07-09 15:33:20 +02:00
|
|
|
//
|
|
|
|
// Check whether the input guid section is recognized.
|
|
|
|
//
|
2007-10-18 11:12:59 +02:00
|
|
|
if (!CompareGuid (
|
|
|
|
&gEfiCrc32GuidedSectionExtractionProtocolGuid,
|
|
|
|
&(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
2007-10-17 09:52:02 +02:00
|
|
|
//
|
|
|
|
// Retrieve the size and attribute of the input section data.
|
|
|
|
//
|
|
|
|
*SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
|
|
|
|
*ScratchBufferSize = 0;
|
|
|
|
*OutputBufferSize = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & 0x00ffffff;
|
|
|
|
*OutputBufferSize -= ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-07-09 15:33:20 +02:00
|
|
|
/**
|
|
|
|
|
|
|
|
The implementation of Crc32 Guided section extraction to get the section data.
|
|
|
|
|
|
|
|
@param InputSection Buffer containing the input GUIDed section to be processed.
|
|
|
|
@param OutputBuffer to contain the output data, which is allocated by the caller.
|
|
|
|
@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
|
|
|
|
authentication status of the output buffer.
|
|
|
|
|
|
|
|
@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;
|
|
|
|
CRC32_SECTION_HEADER *Crc32SectionHeader;
|
|
|
|
UINT32 Crc32Checksum;
|
|
|
|
UINT32 OutputBufferSize;
|
|
|
|
VOID *DummyInterface;
|
|
|
|
|
2008-07-09 15:33:20 +02:00
|
|
|
//
|
|
|
|
// Check whether the input guid section is recognized.
|
|
|
|
//
|
2007-10-18 11:12:59 +02:00
|
|
|
if (!CompareGuid (
|
|
|
|
&gEfiCrc32GuidedSectionExtractionProtocolGuid,
|
|
|
|
&(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
2008-07-09 15:33:20 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Init Checksum value to Zero.
|
|
|
|
//
|
2007-10-17 09:52:02 +02:00
|
|
|
Crc32Checksum = 0;
|
|
|
|
//
|
|
|
|
// Points to the Crc32 section header
|
|
|
|
//
|
|
|
|
Crc32SectionHeader = (CRC32_SECTION_HEADER *) InputSection;
|
|
|
|
*OutputBuffer = (UINT8 *) InputSection + Crc32SectionHeader->GuidedSectionHeader.DataOffset;
|
|
|
|
OutputBufferSize = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & 0x00ffffff;
|
|
|
|
OutputBufferSize -= Crc32SectionHeader->GuidedSectionHeader.DataOffset;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Implictly CRC32 GUIDed section should have STATUS_VALID bit set
|
|
|
|
//
|
|
|
|
ASSERT (Crc32SectionHeader->GuidedSectionHeader.Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
|
|
|
|
*AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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) {
|
|
|
|
if (Crc32Checksum != Crc32SectionHeader->CRC32Checksum) {
|
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
|
|
|
/**
|
2007-10-17 09:52:02 +02:00
|
|
|
Register Crc32 section handler.
|
2008-07-09 15:33:20 +02:00
|
|
|
|
2007-10-17 09:52:02 +02:00
|
|
|
@retval RETURN_SUCCESS Register successfully.
|
2008-07-09 15:33:20 +02:00
|
|
|
@retval RETURN_OUT_OF_RESOURCES No enough memory to register this handler.
|
2007-10-17 09:52:02 +02:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
DxeCrc32GuidedSectionExtractLibConstructor (
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return ExtractGuidedSectionRegisterHandlers (
|
|
|
|
&gEfiCrc32GuidedSectionExtractionProtocolGuid,
|
|
|
|
Crc32GuidedSectionGetInfo,
|
|
|
|
Crc32GuidedSectionHandler
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|