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>
25 #define EFI_SECITON_SIZE_MASK 0x00ffffff
28 EFI_GUID_DEFINED_SECTION GuidedSectionHeader
;
30 } CRC32_SECTION_HEADER
;
34 The implementation of Crc32 guided section GetInfo() to get
35 size and attribute of the guided section.
37 @param InputSection Buffer containing the input GUIDed section to be processed.
38 @param OutputBufferSize The size of OutputBuffer.
39 @param ScratchBufferSize The size of ScratchBuffer.
40 @param SectionAttribute The attribute of the input guided section.
42 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
43 the attribute of the input section are successull retrieved.
44 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
49 Crc32GuidedSectionGetInfo (
50 IN CONST VOID
*InputSection
,
51 OUT UINT32
*OutputBufferSize
,
52 OUT UINT32
*ScratchBufferSize
,
53 OUT UINT16
*SectionAttribute
57 // Check whether the input guid section is recognized.
60 &gEfiCrc32GuidedSectionExtractionProtocolGuid
,
61 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
62 return EFI_INVALID_PARAMETER
;
65 // Retrieve the size and attribute of the input section data.
67 *SectionAttribute
= ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->Attributes
;
68 *ScratchBufferSize
= 0;
69 *OutputBufferSize
= *(UINT32
*) (((EFI_COMMON_SECTION_HEADER
*) InputSection
)->Size
) & EFI_SECITON_SIZE_MASK
;
70 *OutputBufferSize
-= ((EFI_GUID_DEFINED_SECTION
*) InputSection
)->DataOffset
;
77 The implementation of Crc32 Guided section extraction to get the section data.
79 @param InputSection Buffer containing the input GUIDed section to be processed.
80 @param OutputBuffer to contain the output data, which is allocated by the caller.
81 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
82 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
83 authentication status of the output buffer.
85 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
86 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
91 Crc32GuidedSectionHandler (
92 IN CONST VOID
*InputSection
,
93 OUT VOID
**OutputBuffer
,
94 IN VOID
*ScratchBuffer
, OPTIONAL
95 OUT UINT32
*AuthenticationStatus
99 CRC32_SECTION_HEADER
*Crc32SectionHeader
;
100 UINT32 Crc32Checksum
;
101 UINT32 OutputBufferSize
;
102 VOID
*DummyInterface
;
105 // Check whether the input guid section is recognized.
108 &gEfiCrc32GuidedSectionExtractionProtocolGuid
,
109 &(((EFI_GUID_DEFINED_SECTION
*) InputSection
)->SectionDefinitionGuid
))) {
110 return EFI_INVALID_PARAMETER
;
114 // Init Checksum value to Zero.
118 // Points to the Crc32 section header
120 Crc32SectionHeader
= (CRC32_SECTION_HEADER
*) InputSection
;
121 *OutputBuffer
= (UINT8
*) InputSection
+ Crc32SectionHeader
->GuidedSectionHeader
.DataOffset
;
122 OutputBufferSize
= *(UINT32
*) (((EFI_COMMON_SECTION_HEADER
*) InputSection
)->Size
) & EFI_SECITON_SIZE_MASK
;
123 OutputBufferSize
-= Crc32SectionHeader
->GuidedSectionHeader
.DataOffset
;
126 // Implictly CRC32 GUIDed section should have STATUS_VALID bit set
128 ASSERT (Crc32SectionHeader
->GuidedSectionHeader
.Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
);
129 *AuthenticationStatus
= EFI_AUTH_STATUS_IMAGE_SIGNED
;
132 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
134 Status
= gBS
->LocateProtocol (&gEfiSecurityPolicyProtocolGuid
, NULL
, &DummyInterface
);
135 if (!EFI_ERROR (Status
)) {
137 // If SecurityPolicy Protocol exist, AUTH platform override bit is set.
139 *AuthenticationStatus
|= EFI_AUTH_STATUS_PLATFORM_OVERRIDE
;
142 // Calculate CRC32 Checksum of Image
144 Status
= gBS
->CalculateCrc32 (*OutputBuffer
, OutputBufferSize
, &Crc32Checksum
);
145 if (Status
== EFI_SUCCESS
) {
146 if (Crc32Checksum
!= Crc32SectionHeader
->CRC32Checksum
) {
148 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
150 *AuthenticationStatus
|= EFI_AUTH_STATUS_TEST_FAILED
;
154 // If Crc32 checksum is not calculated, AUTH not tested bit is set.
156 *AuthenticationStatus
|= EFI_AUTH_STATUS_NOT_TESTED
;
164 Register Crc32 section handler.
166 @retval RETURN_SUCCESS Register successfully.
167 @retval RETURN_OUT_OF_RESOURCES No enough memory to register this handler.
171 DxeCrc32GuidedSectionExtractLibConstructor (
172 IN EFI_HANDLE ImageHandle
,
173 IN EFI_SYSTEM_TABLE
*SystemTable
176 return ExtractGuidedSectionRegisterHandlers (
177 &gEfiCrc32GuidedSectionExtractionProtocolGuid
,
178 Crc32GuidedSectionGetInfo
,
179 Crc32GuidedSectionHandler