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