]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/PeiCrc32GuidedSectionExtractLib/PeiCrc32GuidedSectionExtractLib.c
MdeModulePkg: Apply uncrustify changes
[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 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <PiPei.h>
12 #include <Guid/Crc32GuidedSectionExtraction.h>
13 #include <Library/BaseLib.h>
14 #include <Library/ExtractGuidedSectionLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/BaseMemoryLib.h>
17
18 ///
19 /// CRC32 Guided Section header
20 ///
21 typedef struct {
22 EFI_GUID_DEFINED_SECTION GuidedSectionHeader; ///< EFI guided section header
23 UINT32 CRC32Checksum; ///< 32bit CRC check sum
24 } CRC32_SECTION_HEADER;
25
26 typedef struct {
27 EFI_GUID_DEFINED_SECTION2 GuidedSectionHeader; ///< EFI guided section header
28 UINT32 CRC32Checksum; ///< 32bit CRC check sum
29 } CRC32_SECTION2_HEADER;
30
31 /**
32
33 GetInfo gets raw data size and attribute of the input guided section.
34 It first checks whether the input guid section is supported.
35 If not, EFI_INVALID_PARAMETER will return.
36
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.
41
42 @retval EFI_SUCCESS The size of destination buffer, the size of scratch buffer and
43 the attribute of the input section are successfully retrieved.
44 @retval EFI_INVALID_PARAMETER The GUID in InputSection does not match this instance guid.
45
46 **/
47 EFI_STATUS
48 EFIAPI
49 Crc32GuidedSectionGetInfo (
50 IN CONST VOID *InputSection,
51 OUT UINT32 *OutputBufferSize,
52 OUT UINT32 *ScratchBufferSize,
53 OUT UINT16 *SectionAttribute
54 )
55 {
56 if (IS_SECTION2 (InputSection)) {
57 //
58 // Check whether the input guid section is recognized.
59 //
60 if (!CompareGuid (
61 &gEfiCrc32GuidedSectionExtractionGuid,
62 &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid)
63 ))
64 {
65 return EFI_INVALID_PARAMETER;
66 }
67
68 //
69 // Retrieve the size and attribute of the input section data.
70 //
71 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->Attributes;
72 *ScratchBufferSize = 0;
73 *OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset;
74 } else {
75 //
76 // Check whether the input guid section is recognized.
77 //
78 if (!CompareGuid (
79 &gEfiCrc32GuidedSectionExtractionGuid,
80 &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid)
81 ))
82 {
83 return EFI_INVALID_PARAMETER;
84 }
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 ))
135 {
136 return EFI_INVALID_PARAMETER;
137 }
138
139 //
140 // Get section Crc32 checksum.
141 //
142 SectionCrc32Checksum = ((CRC32_SECTION2_HEADER *)InputSection)->CRC32Checksum;
143 *OutputBuffer = (UINT8 *)InputSection + ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset;
144 OutputBufferSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *)InputSection)->DataOffset;
145
146 //
147 // Implicitly CRC32 GUIDed section should have STATUS_VALID bit set
148 //
149 ASSERT (((EFI_GUID_DEFINED_SECTION2 *)InputSection)->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);
150 *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;
151 } else {
152 //
153 // Check whether the input guid section is recognized.
154 //
155 if (!CompareGuid (
156 &gEfiCrc32GuidedSectionExtractionGuid,
157 &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid)
158 ))
159 {
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 // Calculate CRC32 Checksum of Image
179 //
180 Crc32Checksum = CalculateCrc32 (*OutputBuffer, OutputBufferSize);
181 if (Crc32Checksum != SectionCrc32Checksum) {
182 //
183 // If Crc32 checksum is not matched, AUTH tested failed bit is set.
184 //
185 *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;
186 }
187
188 //
189 // Temp solution until PeiCore checks AUTH Status.
190 //
191 if ((*AuthenticationStatus & (EFI_AUTH_STATUS_TEST_FAILED | EFI_AUTH_STATUS_NOT_TESTED)) != 0) {
192 return EFI_ACCESS_DENIED;
193 }
194
195 return EFI_SUCCESS;
196 }
197
198 /**
199 Register the handler to extract CRC32 guided section.
200
201 @param FileHandle The handle of FFS header the loaded driver.
202 @param PeiServices The pointer to the PEI services.
203
204 @retval EFI_SUCCESS Register successfully.
205 @retval EFI_OUT_OF_RESOURCES Not enough memory to register this handler.
206
207 **/
208 EFI_STATUS
209 EFIAPI
210 PeiCrc32GuidedSectionExtractLibConstructor (
211 IN EFI_PEI_FILE_HANDLE FileHandle,
212 IN CONST EFI_PEI_SERVICES **PeiServices
213 )
214 {
215 return ExtractGuidedSectionRegisterHandlers (
216 &gEfiCrc32GuidedSectionExtractionGuid,
217 Crc32GuidedSectionGetInfo,
218 Crc32GuidedSectionHandler
219 );
220 }