]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/PeiCrc32GuidedSectionExtractLib/PeiCrc32GuidedSectionExtractLib.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Library / PeiCrc32GuidedSectionExtractLib / PeiCrc32GuidedSectionExtractLib.c
1 /** @file
2
3 This library registers CRC32 guided section handler
4 to parse CRC32 encapsulation section and extract raw data.
5
6 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
7 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
11
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.
14
15 **/
16
17 #include <PiPei.h>
18 #include <Guid/Crc32GuidedSectionExtraction.h>
19 #include <Library/BaseLib.h>
20 #include <Library/ExtractGuidedSectionLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/BaseMemoryLib.h>
23
24 ///
25 /// CRC32 Guided Section header
26 ///
27 typedef struct {
28 EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header
29 UINT32 CRC32Checksum; ///< 32bit CRC check sum
30 } CRC32_SECTION_HEADER;
31
32 typedef struct {
33 EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader; ///< EFI guided section header
34 UINT32 CRC32Checksum; ///< 32bit CRC check sum
35 } CRC32_SECTION2_HEADER;
36
37 /**
38
39 GetInfo gets raw data size and attribute of the input guided section.
40 It first checks whether the input guid section is supported.
41 If not, EFI_INVALID_PARAMETER will return.
42
43 @param InputSection Buffer containing the input GUIDed section to be processed.
44 @param OutputBufferSize The size of OutputBuffer.
45 @param ScratchBufferSize The size of ScratchBuffer.
46 @param SectionAttribute The attribute of the input guided section.
47
48 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
49 the attribute of the input section are successfully retrieved.
50 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
51
52 **/
53 EFI_STATUS
54 EFIAPI
55 Crc32GuidedSectionGetInfo (
56 IN CONST VOID *InputSection,
57 OUT UINT32 *OutputBufferSize,
58 OUT UINT32 *ScratchBufferSize,
59 OUT UINT16 *SectionAttribute
60 )
61 {
62 if (IS_SECTION2 (InputSection)) {
63 //
64 // Check whether the input guid section is recognized.
65 //
66 if (!CompareGuid (
67 &gEfiCrc32GuidedSectionExtractionGuid,
68 &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
69 return EFI_INVALID_PARAMETER;
70 }
71 //
72 // Retrieve the size and attribute of the input section data.
73 //
74 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;
75 *ScratchBufferSize = 0;
76 *OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;
77 } else {
78 //
79 // Check whether the input guid section is recognized.
80 //
81 if (!CompareGuid (
82 &gEfiCrc32GuidedSectionExtractionGuid,
83 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
84 return EFI_INVALID_PARAMETER;
85 }
86 //
87 // Retrieve the size and attribute of the input section data.
88 //
89 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
90 *ScratchBufferSize = 0;
91 *OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
92 }
93
94 return EFI_SUCCESS;
95 }
96
97 /**
98
99 Extraction handler tries to extract raw data from the input guided section.
100 It also does authentication check for 32bit CRC value in the input guided section.
101 It first checks whether the input guid section is supported.
102 If not, EFI_INVALID_PARAMETER will return.
103
104 @param InputSection Buffer containing the input GUIDed section to be processed.
105 @param OutputBuffer Buffer to contain the output raw data allocated by the caller.
106 @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
107 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the
108 authentication status of the output buffer.
109
110 @retval EFI_SUCCESS Section Data and Auth Status is extracted successfully.
111 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
112
113 **/
114 EFI_STATUS
115 EFIAPI
116 Crc32GuidedSectionHandler (
117 IN CONST VOID *InputSection,
118 OUT VOID **OutputBuffer,
119 IN VOID *ScratchBuffer, OPTIONAL
120 OUT UINT32 *AuthenticationStatus
121 )
122 {
123 UINT32 SectionCrc32Checksum;
124 UINT32 Crc32Checksum;
125 UINT32 OutputBufferSize;
126
127 if (IS_SECTION2 (InputSection)) {
128 //
129 // Check whether the input guid section is recognized.
130 //
131 if (!CompareGuid (
132 &gEfiCrc32GuidedSectionExtractionGuid,
133 &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
134 return EFI_INVALID_PARAMETER;
135 }
136
137 //
138 // Get section Crc32 checksum.
139 //
140 SectionCrc32Checksum = ((CRC32_SECTION2_HEADER *) InputSection)->CRC32Checksum;
141 *OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;
142 OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;
143
144 //
145 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
146 //
147 ASSERT (((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
148 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
149 } else {
150 //
151 // Check whether the input guid section is recognized.
152 //
153 if (!CompareGuid (
154 &gEfiCrc32GuidedSectionExtractionGuid,
155 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
156 return EFI_INVALID_PARAMETER;
157 }
158
159 //
160 // Get section Crc32 checksum.
161 //
162 SectionCrc32Checksum = ((CRC32_SECTION_HEADER *) InputSection)->CRC32Checksum;
163 *OutputBuffer = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
164 OutputBufferSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
165
166 //
167 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
168 //
169 ASSERT (((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
170 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
171 }
172
173 //
174 // Calculate CRC32 Checksum of Image
175 //
176 Crc32Checksum = CalculateCrc32 (*OutputBuffer, OutputBufferSize);
177 if (Crc32Checksum != SectionCrc32Checksum) {
178 //
179 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
180 //
181 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
182 }
183
184 //
185 // Temp solution until PeiCore checks AUTH Status.
186 //
187 if ((*AuthenticationStatus & (EFI_AUTH_STATUS_TEST_FAILED | EFI_AUTH_STATUS_NOT_TESTED)) != 0) {
188 return EFI_ACCESS_DENIED;
189 }
190
191 return EFI_SUCCESS;
192 }
193
194 /**
195 Register the handler to extract CRC32 guided section.
196
197 @param FileHandle The handle of FFS header the loaded driver.
198 @param PeiServices The pointer to the PEI services.
199
200 @retval EFI_SUCCESS Register successfully.
201 @retval EFI_OUT_OF_RESOURCES Not enough memory to register this handler.
202
203 **/
204 EFI_STATUS
205 EFIAPI
206 PeiCrc32GuidedSectionExtractLibConstructor (
207 IN EFI_PEI_FILE_HANDLE FileHandle,
208 IN CONST EFI_PEI_SERVICES **PeiServices
209 )
210 {
211 return ExtractGuidedSectionRegisterHandlers (
212 &gEfiCrc32GuidedSectionExtractionGuid,
213 Crc32GuidedSectionGetInfo,
214 Crc32GuidedSectionHandler
215 );
216 }