Home | History | Annotate | Download | only in DxeCrc32GuidedSectionExtractLib
      1 /** @file
      2 
      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.
      6 
      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
     12 
     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.
     15 
     16 **/
     17 
     18 #include <PiDxe.h>
     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>
     25 
     26 ///
     27 /// CRC32 Guided Section header
     28 ///
     29 typedef struct {
     30   EFI_GUID_DEFINED_SECTION  GuidedSectionHeader; ///< EFI guided section header
     31   UINT32                    CRC32Checksum;       ///< 32bit CRC check sum
     32 } CRC32_SECTION_HEADER;
     33 
     34 typedef struct {
     35   EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader; ///< EFI guided section header
     36   UINT32                    CRC32Checksum;       ///< 32bit CRC check sum
     37 } CRC32_SECTION2_HEADER;
     38 
     39 /**
     40 
     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.
     44 
     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.
     49 
     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.
     53 
     54 **/
     55 EFI_STATUS
     56 EFIAPI
     57 Crc32GuidedSectionGetInfo (
     58   IN  CONST VOID  *InputSection,
     59   OUT UINT32      *OutputBufferSize,
     60   OUT UINT32      *ScratchBufferSize,
     61   OUT UINT16      *SectionAttribute
     62   )
     63 {
     64   if (IS_SECTION2 (InputSection)) {
     65     //
     66     // Check whether the input guid section is recognized.
     67     //
     68     if (!CompareGuid (
     69         &gEfiCrc32GuidedSectionExtractionGuid,
     70         &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
     71       return EFI_INVALID_PARAMETER;
     72     }
     73     //
     74     // Retrieve the size and attribute of the input section data.
     75     //
     76     *SectionAttribute  = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;
     77     *ScratchBufferSize = 0;
     78     *OutputBufferSize  = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;
     79   } else {
     80     //
     81     // Check whether the input guid section is recognized.
     82     //
     83     if (!CompareGuid (
     84         &gEfiCrc32GuidedSectionExtractionGuid,
     85         &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
     86       return EFI_INVALID_PARAMETER;
     87     }
     88     //
     89     // Retrieve the size and attribute of the input section data.
     90     //
     91     *SectionAttribute  = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
     92     *ScratchBufferSize = 0;
     93     *OutputBufferSize  = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
     94   }
     95 
     96   return EFI_SUCCESS;
     97 }
     98 
     99 /**
    100 
    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.
    105 
    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.
    111 
    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.
    114 
    115 **/
    116 EFI_STATUS
    117 EFIAPI
    118 Crc32GuidedSectionHandler (
    119   IN CONST  VOID    *InputSection,
    120   OUT       VOID    **OutputBuffer,
    121   IN        VOID    *ScratchBuffer,        OPTIONAL
    122   OUT       UINT32  *AuthenticationStatus
    123   )
    124 {
    125   EFI_STATUS                Status;
    126   UINT32                    SectionCrc32Checksum;
    127   UINT32                    Crc32Checksum;
    128   UINT32                    OutputBufferSize;
    129   VOID                      *DummyInterface;
    130 
    131   if (IS_SECTION2 (InputSection)) {
    132     //
    133     // Check whether the input guid section is recognized.
    134     //
    135     if (!CompareGuid (
    136         &gEfiCrc32GuidedSectionExtractionGuid,
    137         &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
    138       return EFI_INVALID_PARAMETER;
    139     }
    140 
    141     //
    142     // Get section Crc32 checksum.
    143     //
    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;
    147 
    148     //
    149     // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
    150     //
    151     ASSERT (((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
    152     *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
    153   } else {
    154     //
    155     // Check whether the input guid section is recognized.
    156     //
    157     if (!CompareGuid (
    158         &gEfiCrc32GuidedSectionExtractionGuid,
    159         &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
    160       return EFI_INVALID_PARAMETER;
    161     }
    162 
    163     //
    164     // Get section Crc32 checksum.
    165     //
    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;
    169 
    170     //
    171     // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
    172     //
    173     ASSERT (((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
    174     *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
    175   }
    176 
    177   //
    178   // Init Checksum value to Zero.
    179   //
    180   Crc32Checksum = 0;
    181 
    182   //
    183   // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
    184   //
    185   Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);
    186   if (!EFI_ERROR (Status)) {
    187     //
    188     // If SecurityPolicy Protocol exist, AUTH platform override bit is set.
    189     //
    190     *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;
    191   } else {
    192     //
    193     // Calculate CRC32 Checksum of Image
    194     //
    195     Status = gBS->CalculateCrc32 (*OutputBuffer, OutputBufferSize, &Crc32Checksum);
    196     if (Status == EFI_SUCCESS) {
    197       if (Crc32Checksum != SectionCrc32Checksum) {
    198         //
    199         // If Crc32 checksum is not matched, AUTH tested failed bit is set.
    200         //
    201         *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
    202       }
    203     } else {
    204       //
    205       // If Crc32 checksum is not calculated, AUTH not tested bit is set.
    206       //
    207       *AuthenticationStatus |= EFI_AUTH_STATUS_NOT_TESTED;
    208     }
    209   }
    210 
    211   return EFI_SUCCESS;
    212 }
    213 
    214 /**
    215   Register the handler to extract CRC32 guided section.
    216 
    217   @param  ImageHandle  ImageHandle of the loaded driver.
    218   @param  SystemTable  Pointer to the EFI System Table.
    219 
    220   @retval  EFI_SUCCESS            Register successfully.
    221   @retval  EFI_OUT_OF_RESOURCES   No enough memory to register this handler.
    222 **/
    223 EFI_STATUS
    224 EFIAPI
    225 DxeCrc32GuidedSectionExtractLibConstructor (
    226   IN EFI_HANDLE        ImageHandle,
    227   IN EFI_SYSTEM_TABLE  *SystemTable
    228   )
    229 {
    230   return ExtractGuidedSectionRegisterHandlers (
    231           &gEfiCrc32GuidedSectionExtractionGuid,
    232           Crc32GuidedSectionGetInfo,
    233           Crc32GuidedSectionHandler
    234           );
    235 }
    236 
    237