]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/SectionExtractionDxe/SectionExtractionDxe.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / SectionExtractionDxe / SectionExtractionDxe.c
CommitLineData
a402e129
MK
1/** @file\r
2 Section Extraction DXE Driver\r
3\r
d1102dba
LG
4Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
a402e129
MK
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16#include <Protocol/GuidedSectionExtraction.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/ExtractGuidedSectionLib.h>\r
19#include <Library/MemoryAllocationLib.h>\r
20#include <Library/BaseMemoryLib.h>\r
21#include <Library/UefiBootServicesTableLib.h>\r
22\r
d5eea98e
ED
23/**\r
24 The ExtractSection() function processes the input section and\r
25 allocates a buffer from the pool in which it returns the section\r
26 contents. If the section being extracted contains\r
27 authentication information (the section's\r
28 GuidedSectionHeader.Attributes field has the\r
29 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values\r
30 returned in AuthenticationStatus must reflect the results of\r
31 the authentication operation. Depending on the algorithm and\r
32 size of the encapsulated data, the time that is required to do\r
33 a full authentication may be prohibitively long for some\r
34 classes of systems. To indicate this, use\r
35 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by\r
36 the security policy driver (see the Platform Initialization\r
37 Driver Execution Environment Core Interface Specification for\r
38 more details and the GUID definition). If the\r
39 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle\r
40 database, then, if possible, full authentication should be\r
41 skipped and the section contents simply returned in the\r
42 OutputBuffer. In this case, the\r
43 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus\r
44 must be set on return. ExtractSection() is callable only from\r
45 TPL_NOTIFY and below. Behavior of ExtractSection() at any\r
46 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is\r
47 defined in RaiseTPL() in the UEFI 2.0 specification.\r
48\r
49\r
50 @param This Indicates the\r
51 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.\r
52 @param InputSection Buffer containing the input GUIDed section\r
53 to be processed. OutputBuffer OutputBuffer\r
54 is allocated from boot services pool\r
55 memory and contains the new section\r
56 stream. The caller is responsible for\r
57 freeing this buffer.\r
58 @param OutputBuffer *OutputBuffer is allocated from boot services\r
59 pool memory and contains the new section stream.\r
60 The caller is responsible for freeing this buffer.\r
61 @param OutputSize A pointer to a caller-allocated UINTN in\r
62 which the size of OutputBuffer allocation\r
63 is stored. If the function returns\r
64 anything other than EFI_SUCCESS, the value\r
65 of OutputSize is undefined.\r
66\r
67 @param AuthenticationStatus A pointer to a caller-allocated\r
68 UINT32 that indicates the\r
69 authentication status of the\r
70 output buffer. If the input\r
71 section's\r
72 GuidedSectionHeader.Attributes\r
73 field has the\r
74 EFI_GUIDED_SECTION_AUTH_STATUS_VAL\r
75 bit as clear, AuthenticationStatus\r
76 must return zero. Both local bits\r
77 (19:16) and aggregate bits (3:0)\r
78 in AuthenticationStatus are\r
79 returned by ExtractSection().\r
80 These bits reflect the status of\r
81 the extraction operation. The bit\r
82 pattern in both regions must be\r
83 the same, as the local and\r
84 aggregate authentication statuses\r
85 have equivalent meaning at this\r
86 level. If the function returns\r
87 anything other than EFI_SUCCESS,\r
88 the value of AuthenticationStatus\r
89 is undefined.\r
90\r
91\r
92 @retval EFI_SUCCESS The InputSection was successfully\r
93 processed and the section contents were\r
94 returned.\r
95\r
96 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
97 resources to process the\r
98 request.\r
99\r
100 @retval EFI_INVALID_PARAMETER The GUID in InputSection does\r
101 not match this instance of the\r
102 GUIDed Section Extraction\r
103 Protocol.\r
104\r
105**/\r
a402e129
MK
106EFI_STATUS\r
107EFIAPI\r
108CustomGuidedSectionExtract (\r
109 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,\r
110 IN CONST VOID *InputSection,\r
111 OUT VOID **OutputBuffer,\r
112 OUT UINTN *OutputSize,\r
113 OUT UINT32 *AuthenticationStatus\r
114 );\r
115\r
116//\r
117// Module global for the Section Extraction Protocol handle\r
118//\r
119EFI_HANDLE mSectionExtractionHandle = NULL;\r
120\r
121//\r
122// Module global for the Section Extraction Protocol instance\r
123//\r
124EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL mCustomGuidedSectionExtractionProtocol = {\r
125 CustomGuidedSectionExtract\r
126};\r
127\r
128/**\r
129 The ExtractSection() function processes the input section and\r
130 allocates a buffer from the pool in which it returns the section\r
131 contents. If the section being extracted contains\r
132 authentication information (the section's\r
133 GuidedSectionHeader.Attributes field has the\r
134 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values\r
135 returned in AuthenticationStatus must reflect the results of\r
136 the authentication operation. Depending on the algorithm and\r
137 size of the encapsulated data, the time that is required to do\r
138 a full authentication may be prohibitively long for some\r
139 classes of systems. To indicate this, use\r
140 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by\r
141 the security policy driver (see the Platform Initialization\r
142 Driver Execution Environment Core Interface Specification for\r
143 more details and the GUID definition). If the\r
144 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle\r
145 database, then, if possible, full authentication should be\r
146 skipped and the section contents simply returned in the\r
147 OutputBuffer. In this case, the\r
148 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus\r
149 must be set on return. ExtractSection() is callable only from\r
150 TPL_NOTIFY and below. Behavior of ExtractSection() at any\r
151 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is\r
152 defined in RaiseTPL() in the UEFI 2.0 specification.\r
153\r
154\r
155 @param This Indicates the\r
156 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.\r
157 @param InputSection Buffer containing the input GUIDed section\r
158 to be processed. OutputBuffer OutputBuffer\r
159 is allocated from boot services pool\r
160 memory and contains the new section\r
161 stream. The caller is responsible for\r
162 freeing this buffer.\r
163 @param OutputBuffer *OutputBuffer is allocated from boot services\r
164 pool memory and contains the new section stream.\r
165 The caller is responsible for freeing this buffer.\r
166 @param OutputSize A pointer to a caller-allocated UINTN in\r
167 which the size of OutputBuffer allocation\r
168 is stored. If the function returns\r
169 anything other than EFI_SUCCESS, the value\r
170 of OutputSize is undefined.\r
171\r
172 @param AuthenticationStatus A pointer to a caller-allocated\r
173 UINT32 that indicates the\r
174 authentication status of the\r
175 output buffer. If the input\r
176 section's\r
177 GuidedSectionHeader.Attributes\r
178 field has the\r
179 EFI_GUIDED_SECTION_AUTH_STATUS_VAL\r
180 bit as clear, AuthenticationStatus\r
181 must return zero. Both local bits\r
182 (19:16) and aggregate bits (3:0)\r
183 in AuthenticationStatus are\r
184 returned by ExtractSection().\r
185 These bits reflect the status of\r
186 the extraction operation. The bit\r
187 pattern in both regions must be\r
188 the same, as the local and\r
189 aggregate authentication statuses\r
190 have equivalent meaning at this\r
191 level. If the function returns\r
192 anything other than EFI_SUCCESS,\r
193 the value of AuthenticationStatus\r
194 is undefined.\r
195\r
196\r
197 @retval EFI_SUCCESS The InputSection was successfully\r
198 processed and the section contents were\r
199 returned.\r
200\r
201 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
202 resources to process the\r
203 request.\r
204\r
205 @retval EFI_INVALID_PARAMETER The GUID in InputSection does\r
206 not match this instance of the\r
207 GUIDed Section Extraction\r
208 Protocol.\r
209\r
210**/\r
211EFI_STATUS\r
212EFIAPI\r
213CustomGuidedSectionExtract (\r
214 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,\r
215 IN CONST VOID *InputSection,\r
216 OUT VOID **OutputBuffer,\r
217 OUT UINTN *OutputSize,\r
218 OUT UINT32 *AuthenticationStatus\r
219 )\r
220{\r
221 EFI_STATUS Status;\r
222 VOID *ScratchBuffer;\r
223 VOID *AllocatedOutputBuffer;\r
224 UINT32 OutputBufferSize;\r
225 UINT32 ScratchBufferSize;\r
226 UINT16 SectionAttribute;\r
227\r
228 //\r
229 // Init local variable\r
230 //\r
231 ScratchBuffer = NULL;\r
232 AllocatedOutputBuffer = NULL;\r
233\r
234 //\r
235 // Call GetInfo to get the size and attribute of input guided section data.\r
236 //\r
237 Status = ExtractGuidedSectionGetInfo (\r
238 InputSection,\r
239 &OutputBufferSize,\r
240 &ScratchBufferSize,\r
241 &SectionAttribute\r
242 );\r
243\r
244 if (EFI_ERROR (Status)) {\r
245 DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status));\r
246 return Status;\r
247 }\r
248\r
249 if (ScratchBufferSize > 0) {\r
250 //\r
251 // Allocate scratch buffer\r
252 //\r
253 ScratchBuffer = AllocatePool (ScratchBufferSize);\r
254 if (ScratchBuffer == NULL) {\r
255 return EFI_OUT_OF_RESOURCES;\r
256 }\r
257 }\r
258\r
259 if (OutputBufferSize > 0) {\r
260 //\r
261 // Allocate output buffer\r
262 //\r
263 AllocatedOutputBuffer = AllocatePool (OutputBufferSize);\r
264 if (AllocatedOutputBuffer == NULL) {\r
265 FreePool (ScratchBuffer);\r
266 return EFI_OUT_OF_RESOURCES;\r
267 }\r
268 *OutputBuffer = AllocatedOutputBuffer;\r
269 }\r
270\r
271 //\r
272 // Call decode function to extract raw data from the guided section.\r
273 //\r
274 Status = ExtractGuidedSectionDecode (\r
275 InputSection,\r
276 OutputBuffer,\r
277 ScratchBuffer,\r
278 AuthenticationStatus\r
279 );\r
280 if (EFI_ERROR (Status)) {\r
281 //\r
282 // Decode failed\r
283 //\r
284 if (AllocatedOutputBuffer != NULL) {\r
285 FreePool (AllocatedOutputBuffer);\r
286 }\r
287 if (ScratchBuffer != NULL) {\r
288 FreePool (ScratchBuffer);\r
289 }\r
290 DEBUG ((DEBUG_ERROR, "Extract guided section Failed - %r\n", Status));\r
291 return Status;\r
292 }\r
293\r
294 if (*OutputBuffer != AllocatedOutputBuffer) {\r
295 //\r
296 // OutputBuffer was returned as a different value,\r
297 // so copy section contents to the allocated memory buffer.\r
298 //\r
299 CopyMem (AllocatedOutputBuffer, *OutputBuffer, OutputBufferSize);\r
300 *OutputBuffer = AllocatedOutputBuffer;\r
301 }\r
302\r
303 //\r
304 // Set real size of output buffer.\r
305 //\r
306 *OutputSize = (UINTN) OutputBufferSize;\r
307\r
308 //\r
309 // Free unused scratch buffer.\r
310 //\r
311 if (ScratchBuffer != NULL) {\r
312 FreePool (ScratchBuffer);\r
313 }\r
314\r
315 return EFI_SUCCESS;\r
316}\r
317\r
318/**\r
319 Main entry for the Section Extraction DXE module.\r
320\r
d1102dba 321 This routine registers the Section Extraction Protocols that have been registered\r
a402e129 322 with the Section Extraction Library.\r
d1102dba 323\r
a402e129
MK
324 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
325 @param[in] SystemTable A pointer to the EFI System Table.\r
d1102dba 326\r
a402e129
MK
327 @retval EFI_SUCCESS The entry point is executed successfully.\r
328 @retval other Some error occurs when executing this entry point.\r
329\r
330**/\r
331EFI_STATUS\r
332EFIAPI\r
333SectionExtractionDxeEntry (\r
334 IN EFI_HANDLE ImageHandle,\r
335 IN EFI_SYSTEM_TABLE *SystemTable\r
336 )\r
337{\r
338 EFI_STATUS Status;\r
339 EFI_GUID *ExtractHandlerGuidTable;\r
340 UINTN ExtractHandlerNumber;\r
341\r
342 //\r
343 // Get custom extract guided section method guid list\r
344 //\r
345 ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable);\r
346\r
347 //\r
348 // Install custom guided extraction protocol\r
349 //\r
350 while (ExtractHandlerNumber-- > 0) {\r
351 Status = gBS->InstallMultipleProtocolInterfaces (\r
352 &mSectionExtractionHandle,\r
353 &ExtractHandlerGuidTable [ExtractHandlerNumber], &mCustomGuidedSectionExtractionProtocol,\r
354 NULL\r
355 );\r
356 ASSERT_EFI_ERROR (Status);\r
357 }\r
358\r
359 return EFI_SUCCESS;\r
360}\r