]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxeCrc32GuidedSectionExtractLib/DxeCrc32GuidedSectionExtractLib.c
Move Crc32GuidedSectionExtraction from Protocol to Guid directory
[mirror_edk2.git] / MdeModulePkg / Library / DxeCrc32GuidedSectionExtractLib / DxeCrc32GuidedSectionExtractLib.c
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 - 2008, Intel Corporation
8 All rights reserved. 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 #define EFI_SECITON_SIZE_MASK 0x00ffffff
27
28 ///
29 /// CRC32 Guided Section header
30 ///
31 typedef struct {
32 EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header
33 UINT32 CRC32Checksum; ///< 32bit CRC check sum
34 } CRC32_SECTION_HEADER;
35
36 /**
37
38 GetInfo gets raw data size and attribute of the input guided section.
39 It first checks whether the input guid section is supported.
40 If not, EFI_INVALID_PARAMETER will return.
41
42 @param InputSection Buffer containing the input GUIDed section to be processed.
43 @param OutputBufferSize The size of OutputBuffer.
44 @param ScratchBufferSize The size of ScratchBuffer.
45 @param SectionAttribute The attribute of the input guided section.
46
47 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
48 the attribute of the input section are successull retrieved.
49 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
50
51 **/
52 EFI_STATUS
53 EFIAPI
54 Crc32GuidedSectionGetInfo (
55 IN CONST VOID *InputSection,
56 OUT UINT32 *OutputBufferSize,
57 OUT UINT32 *ScratchBufferSize,
58 OUT UINT16 *SectionAttribute
59 )
60 {
61 //
62 // Check whether the input guid section is recognized.
63 //
64 if (!CompareGuid (
65 &gEfiCrc32GuidedSectionExtractionGuid,
66 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
67 return EFI_INVALID_PARAMETER;
68 }
69 //
70 // Retrieve the size and attribute of the input section data.
71 //
72 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
73 *ScratchBufferSize = 0;
74 *OutputBufferSize = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & EFI_SECITON_SIZE_MASK;
75 *OutputBufferSize -= ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
76
77 return EFI_SUCCESS;
78 }
79
80 /**
81
82 Extraction handler tries to extract raw data from the input guided section.
83 It also does authentication check for 32bit CRC value in the input guided section.
84 It first checks whether the input guid section is supported.
85 If not, EFI_INVALID_PARAMETER will return.
86
87 @param InputSection Buffer containing the input GUIDed section to be processed.
88 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.
89 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
90 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
91 authentication status of the output buffer.
92
93 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
94 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
95
96 **/
97 EFI_STATUS
98 EFIAPI
99 Crc32GuidedSectionHandler (
100 IN CONST VOID *InputSection,
101 OUT VOID **OutputBuffer,
102 IN VOID *ScratchBuffer, OPTIONAL
103 OUT UINT32 *AuthenticationStatus
104 )
105 {
106 EFI_STATUS Status;
107 CRC32_SECTION_HEADER *Crc32SectionHeader;
108 UINT32 Crc32Checksum;
109 UINT32 OutputBufferSize;
110 VOID *DummyInterface;
111
112 //
113 // Check whether the input guid section is recognized.
114 //
115 if (!CompareGuid (
116 &gEfiCrc32GuidedSectionExtractionGuid,
117 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
118 return EFI_INVALID_PARAMETER;
119 }
120
121 //
122 // Init Checksum value to Zero.
123 //
124 Crc32Checksum = 0;
125 //
126 // Points to the Crc32 section header
127 //
128 Crc32SectionHeader = (CRC32_SECTION_HEADER *) InputSection;
129 *OutputBuffer = (UINT8 *) InputSection + Crc32SectionHeader->GuidedSectionHeader.DataOffset;
130 OutputBufferSize = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & EFI_SECITON_SIZE_MASK;
131 OutputBufferSize -= Crc32SectionHeader->GuidedSectionHeader.DataOffset;
132
133 //
134 // Implictly CRC32 GUIDed section should have STATUS_VALID bit set
135 //
136 ASSERT (Crc32SectionHeader->GuidedSectionHeader.Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
137 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
138
139 //
140 // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.
141 //
142 Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);
143 if (!EFI_ERROR (Status)) {
144 //
145 // If SecurityPolicy Protocol exist, AUTH platform override bit is set.
146 //
147 *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;
148 } else {
149 //
150 // Calculate CRC32 Checksum of Image
151 //
152 Status = gBS->CalculateCrc32 (*OutputBuffer, OutputBufferSize, &Crc32Checksum);
153 if (Status == EFI_SUCCESS) {
154 if (Crc32Checksum != Crc32SectionHeader->CRC32Checksum) {
155 //
156 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
157 //
158 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
159 }
160 } else {
161 //
162 // If Crc32 checksum is not calculated, AUTH not tested bit is set.
163 //
164 *AuthenticationStatus |= EFI_AUTH_STATUS_NOT_TESTED;
165 }
166 }
167
168 return EFI_SUCCESS;
169 }
170
171 /**
172 Register the handler to extract CRC32 guided section.
173
174 @param ImageHandle ImageHandle of the loaded driver.
175 @param SystemTable Pointer to the EFI System Table.
176
177 @retval EFI_SUCCESS Register successfully.
178 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
179 **/
180 EFI_STATUS
181 EFIAPI
182 DxeCrc32GuidedSectionExtractLibConstructor (
183 IN EFI_HANDLE ImageHandle,
184 IN EFI_SYSTEM_TABLE *SystemTable
185 )
186 {
187 return ExtractGuidedSectionRegisterHandlers (
188 &gEfiCrc32GuidedSectionExtractionGuid,
189 Crc32GuidedSectionGetInfo,
190 Crc32GuidedSectionHandler
191 );
192 }
193