]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c
IntelSiliconPkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / SectionExtraction / CoreSectionExtraction.c
... / ...
CommitLineData
1/** @file\r
2 Section Extraction Protocol implementation.\r
3\r
4 Stream database is implemented as a linked list of section streams,\r
5 where each stream contains a linked list of children, which may be leaves or\r
6 encapsulations.\r
7\r
8 Children that are encapsulations generate new stream entries\r
9 when they are created. Streams can also be created by calls to\r
10 SEP->OpenSectionStream().\r
11\r
12 The database is only created far enough to return the requested data from\r
13 any given stream, or to determine that the requested data is not found.\r
14\r
15 If a GUIDed encapsulation is encountered, there are three possiblilites.\r
16\r
17 1) A support protocol is found, in which the stream is simply processed with\r
18 the support protocol.\r
19\r
20 2) A support protocol is not found, but the data is available to be read\r
21 without processing. In this case, the database is built up through the\r
22 recursions to return the data, and a RPN event is set that will enable\r
23 the stream in question to be refreshed if and when the required section\r
24 extraction protocol is published.This insures the AuthenticationStatus\r
25 does not become stale in the cache.\r
26\r
27 3) A support protocol is not found, and the data is not available to be read\r
28 without it. This results in EFI_PROTOCOL_ERROR.\r
29\r
30Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
31This program and the accompanying materials\r
32are licensed and made available under the terms and conditions of the BSD License\r
33which accompanies this distribution. The full text of the license may be found at\r
34http://opensource.org/licenses/bsd-license.php\r
35\r
36THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
37WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
38\r
39**/\r
40\r
41#include "DxeMain.h"\r
42\r
43//\r
44// Local defines and typedefs\r
45//\r
46#define CORE_SECTION_CHILD_SIGNATURE SIGNATURE_32('S','X','C','S')\r
47#define CHILD_SECTION_NODE_FROM_LINK(Node) \\r
48 CR (Node, CORE_SECTION_CHILD_NODE, Link, CORE_SECTION_CHILD_SIGNATURE)\r
49\r
50typedef struct {\r
51 UINT32 Signature;\r
52 LIST_ENTRY Link;\r
53 UINT32 Type;\r
54 UINT32 Size;\r
55 //\r
56 // StreamBase + OffsetInStream == pointer to section header in stream. The\r
57 // stream base is always known when walking the sections within.\r
58 //\r
59 UINT32 OffsetInStream;\r
60 //\r
61 // Then EncapsulatedStreamHandle below is always 0 if the section is NOT an\r
62 // encapsulating section. Otherwise, it contains the stream handle\r
63 // of the encapsulated stream. This handle is ALWAYS produced any time an\r
64 // encapsulating child is encountered, irrespective of whether the\r
65 // encapsulated stream is processed further.\r
66 //\r
67 UINTN EncapsulatedStreamHandle;\r
68 EFI_GUID *EncapsulationGuid;\r
69 //\r
70 // If the section REQUIRES an extraction protocol, register for RPN \r
71 // when the required GUIDed extraction protocol becomes available.\r
72 //\r
73 EFI_EVENT Event;\r
74} CORE_SECTION_CHILD_NODE;\r
75\r
76#define CORE_SECTION_STREAM_SIGNATURE SIGNATURE_32('S','X','S','S')\r
77#define STREAM_NODE_FROM_LINK(Node) \\r
78 CR (Node, CORE_SECTION_STREAM_NODE, Link, CORE_SECTION_STREAM_SIGNATURE)\r
79\r
80typedef struct {\r
81 UINT32 Signature;\r
82 LIST_ENTRY Link;\r
83 UINTN StreamHandle;\r
84 UINT8 *StreamBuffer;\r
85 UINTN StreamLength;\r
86 LIST_ENTRY Children;\r
87 //\r
88 // Authentication status is from GUIDed encapsulations.\r
89 //\r
90 UINT32 AuthenticationStatus;\r
91} CORE_SECTION_STREAM_NODE;\r
92\r
93#define NULL_STREAM_HANDLE 0\r
94\r
95typedef struct {\r
96 CORE_SECTION_CHILD_NODE *ChildNode;\r
97 CORE_SECTION_STREAM_NODE *ParentStream;\r
98 VOID *Registration;\r
99} RPN_EVENT_CONTEXT;\r
100\r
101\r
102/**\r
103 The ExtractSection() function processes the input section and\r
104 allocates a buffer from the pool in which it returns the section\r
105 contents. If the section being extracted contains\r
106 authentication information (the section's\r
107 GuidedSectionHeader.Attributes field has the\r
108 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values\r
109 returned in AuthenticationStatus must reflect the results of\r
110 the authentication operation. Depending on the algorithm and\r
111 size of the encapsulated data, the time that is required to do\r
112 a full authentication may be prohibitively long for some\r
113 classes of systems. To indicate this, use\r
114 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by\r
115 the security policy driver (see the Platform Initialization\r
116 Driver Execution Environment Core Interface Specification for\r
117 more details and the GUID definition). If the\r
118 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle\r
119 database, then, if possible, full authentication should be\r
120 skipped and the section contents simply returned in the\r
121 OutputBuffer. In this case, the\r
122 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus\r
123 must be set on return. ExtractSection() is callable only from\r
124 TPL_NOTIFY and below. Behavior of ExtractSection() at any\r
125 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is\r
126 defined in RaiseTPL() in the UEFI 2.0 specification.\r
127\r
128\r
129 @param This Indicates the\r
130 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.\r
131 @param InputSection Buffer containing the input GUIDed section\r
132 to be processed. OutputBuffer OutputBuffer\r
133 is allocated from boot services pool\r
134 memory and contains the new section\r
135 stream. The caller is responsible for\r
136 freeing this buffer.\r
137 @param OutputBuffer *OutputBuffer is allocated from boot services\r
138 pool memory and contains the new section stream.\r
139 The caller is responsible for freeing this buffer.\r
140 @param OutputSize A pointer to a caller-allocated UINTN in\r
141 which the size of OutputBuffer allocation\r
142 is stored. If the function returns\r
143 anything other than EFI_SUCCESS, the value\r
144 of OutputSize is undefined.\r
145\r
146 @param AuthenticationStatus A pointer to a caller-allocated\r
147 UINT32 that indicates the\r
148 authentication status of the\r
149 output buffer. If the input\r
150 section's\r
151 GuidedSectionHeader.Attributes\r
152 field has the\r
153 EFI_GUIDED_SECTION_AUTH_STATUS_VAL\r
154 bit as clear, AuthenticationStatus\r
155 must return zero. Both local bits\r
156 (19:16) and aggregate bits (3:0)\r
157 in AuthenticationStatus are\r
158 returned by ExtractSection().\r
159 These bits reflect the status of\r
160 the extraction operation. The bit\r
161 pattern in both regions must be\r
162 the same, as the local and\r
163 aggregate authentication statuses\r
164 have equivalent meaning at this\r
165 level. If the function returns\r
166 anything other than EFI_SUCCESS,\r
167 the value of AuthenticationStatus\r
168 is undefined.\r
169\r
170\r
171 @retval EFI_SUCCESS The InputSection was successfully\r
172 processed and the section contents were\r
173 returned.\r
174\r
175 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
176 resources to process the\r
177 request.\r
178\r
179 @retval EFI_INVALID_PARAMETER The GUID in InputSection does\r
180 not match this instance of the\r
181 GUIDed Section Extraction\r
182 Protocol.\r
183\r
184**/\r
185EFI_STATUS\r
186EFIAPI\r
187CustomGuidedSectionExtract (\r
188 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,\r
189 IN CONST VOID *InputSection,\r
190 OUT VOID **OutputBuffer,\r
191 OUT UINTN *OutputSize,\r
192 OUT UINT32 *AuthenticationStatus\r
193 );\r
194\r
195//\r
196// Module globals\r
197//\r
198LIST_ENTRY mStreamRoot = INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot);\r
199\r
200EFI_HANDLE mSectionExtractionHandle = NULL;\r
201\r
202EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL mCustomGuidedSectionExtractionProtocol = {\r
203 CustomGuidedSectionExtract\r
204};\r
205\r
206\r
207/**\r
208 Entry point of the section extraction code. Initializes an instance of the\r
209 section extraction interface and installs it on a new handle.\r
210\r
211 @param ImageHandle A handle for the image that is initializing this driver\r
212 @param SystemTable A pointer to the EFI system table\r
213\r
214 @retval EFI_SUCCESS Driver initialized successfully\r
215 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources\r
216\r
217**/\r
218EFI_STATUS\r
219EFIAPI\r
220InitializeSectionExtraction (\r
221 IN EFI_HANDLE ImageHandle,\r
222 IN EFI_SYSTEM_TABLE *SystemTable\r
223 )\r
224{\r
225 EFI_STATUS Status;\r
226 EFI_GUID *ExtractHandlerGuidTable;\r
227 UINTN ExtractHandlerNumber;\r
228\r
229 //\r
230 // Get custom extract guided section method guid list\r
231 //\r
232 ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable);\r
233\r
234 Status = EFI_SUCCESS;\r
235 //\r
236 // Install custom guided extraction protocol\r
237 //\r
238 while (ExtractHandlerNumber-- > 0) {\r
239 Status = CoreInstallProtocolInterface (\r
240 &mSectionExtractionHandle,\r
241 &ExtractHandlerGuidTable [ExtractHandlerNumber],\r
242 EFI_NATIVE_INTERFACE,\r
243 &mCustomGuidedSectionExtractionProtocol\r
244 );\r
245 ASSERT_EFI_ERROR (Status);\r
246 }\r
247\r
248 return Status;\r
249}\r
250\r
251\r
252/**\r
253 Check if a stream is valid.\r
254\r
255 @param SectionStream The section stream to be checked\r
256 @param SectionStreamLength The length of section stream\r
257\r
258 @return A boolean value indicating the validness of the section stream.\r
259\r
260**/\r
261BOOLEAN\r
262IsValidSectionStream (\r
263 IN VOID *SectionStream,\r
264 IN UINTN SectionStreamLength\r
265 )\r
266{\r
267 UINTN TotalLength;\r
268 UINTN SectionLength;\r
269 EFI_COMMON_SECTION_HEADER *SectionHeader;\r
270 EFI_COMMON_SECTION_HEADER *NextSectionHeader;\r
271\r
272 TotalLength = 0;\r
273 SectionHeader = (EFI_COMMON_SECTION_HEADER *)SectionStream;\r
274\r
275 while (TotalLength < SectionStreamLength) {\r
276 if (IS_SECTION2 (SectionHeader)) {\r
277 SectionLength = SECTION2_SIZE (SectionHeader);\r
278 } else {\r
279 SectionLength = SECTION_SIZE (SectionHeader);\r
280 }\r
281 TotalLength += SectionLength;\r
282\r
283 if (TotalLength == SectionStreamLength) {\r
284 return TRUE;\r
285 }\r
286\r
287 //\r
288 // Move to the next byte following the section...\r
289 //\r
290 SectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) SectionHeader + SectionLength);\r
291\r
292 //\r
293 // Figure out where the next section begins\r
294 //\r
295 NextSectionHeader = ALIGN_POINTER(SectionHeader, 4);\r
296 TotalLength += (UINTN) NextSectionHeader - (UINTN) SectionHeader;\r
297 SectionHeader = NextSectionHeader;\r
298 }\r
299\r
300 ASSERT (FALSE);\r
301 return FALSE;\r
302}\r
303\r
304\r
305/**\r
306 Worker function. Constructor for section streams.\r
307\r
308 @param SectionStreamLength Size in bytes of the section stream.\r
309 @param SectionStream Buffer containing the new section stream.\r
310 @param AllocateBuffer Indicates whether the stream buffer is to be\r
311 copied or the input buffer is to be used in\r
312 place. AuthenticationStatus- Indicates the\r
313 default authentication status for the new\r
314 stream.\r
315 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that\r
316 indicates the authentication status of the\r
317 output buffer. If the input section's\r
318 GuidedSectionHeader.Attributes field\r
319 has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
320 bit as clear, AuthenticationStatus must return\r
321 zero. Both local bits (19:16) and aggregate\r
322 bits (3:0) in AuthenticationStatus are returned\r
323 by ExtractSection(). These bits reflect the\r
324 status of the extraction operation. The bit\r
325 pattern in both regions must be the same, as\r
326 the local and aggregate authentication statuses\r
327 have equivalent meaning at this level. If the\r
328 function returns anything other than\r
329 EFI_SUCCESS, the value of *AuthenticationStatus\r
330 is undefined.\r
331 @param SectionStreamHandle A pointer to a caller allocated section stream\r
332 handle.\r
333\r
334 @retval EFI_SUCCESS Stream was added to stream database.\r
335 @retval EFI_OUT_OF_RESOURCES memory allocation failed.\r
336\r
337**/\r
338EFI_STATUS\r
339OpenSectionStreamEx (\r
340 IN UINTN SectionStreamLength,\r
341 IN VOID *SectionStream,\r
342 IN BOOLEAN AllocateBuffer,\r
343 IN UINT32 AuthenticationStatus,\r
344 OUT UINTN *SectionStreamHandle\r
345 )\r
346{\r
347 CORE_SECTION_STREAM_NODE *NewStream;\r
348 EFI_TPL OldTpl;\r
349\r
350 //\r
351 // Allocate a new stream\r
352 //\r
353 NewStream = AllocatePool (sizeof (CORE_SECTION_STREAM_NODE));\r
354 if (NewStream == NULL) {\r
355 return EFI_OUT_OF_RESOURCES;\r
356 }\r
357\r
358 if (AllocateBuffer) {\r
359 //\r
360 // if we're here, we're double buffering, allocate the buffer and copy the\r
361 // data in\r
362 //\r
363 if (SectionStreamLength > 0) {\r
364 NewStream->StreamBuffer = AllocatePool (SectionStreamLength);\r
365 if (NewStream->StreamBuffer == NULL) {\r
366 CoreFreePool (NewStream);\r
367 return EFI_OUT_OF_RESOURCES;\r
368 }\r
369 //\r
370 // Copy in stream data\r
371 //\r
372 CopyMem (NewStream->StreamBuffer, SectionStream, SectionStreamLength);\r
373 } else {\r
374 //\r
375 // It's possible to have a zero length section stream.\r
376 //\r
377 NewStream->StreamBuffer = NULL;\r
378 }\r
379 } else {\r
380 //\r
381 // If were here, the caller has supplied the buffer (it's an internal call)\r
382 // so just assign the buffer. This happens when we open section streams\r
383 // as a result of expanding an encapsulating section.\r
384 //\r
385 NewStream->StreamBuffer = SectionStream;\r
386 }\r
387\r
388 //\r
389 // Initialize the rest of the section stream\r
390 //\r
391 NewStream->Signature = CORE_SECTION_STREAM_SIGNATURE;\r
392 NewStream->StreamHandle = (UINTN) NewStream;\r
393 NewStream->StreamLength = SectionStreamLength;\r
394 InitializeListHead (&NewStream->Children);\r
395 NewStream->AuthenticationStatus = AuthenticationStatus;\r
396\r
397 //\r
398 // Add new stream to stream list\r
399 //\r
400 OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
401 InsertTailList (&mStreamRoot, &NewStream->Link);\r
402 CoreRestoreTpl (OldTpl);\r
403\r
404 *SectionStreamHandle = NewStream->StreamHandle;\r
405\r
406 return EFI_SUCCESS;\r
407}\r
408\r
409\r
410/**\r
411 SEP member function. This function creates and returns a new section stream\r
412 handle to represent the new section stream.\r
413\r
414 @param SectionStreamLength Size in bytes of the section stream.\r
415 @param SectionStream Buffer containing the new section stream.\r
416 @param SectionStreamHandle A pointer to a caller allocated UINTN that on\r
417 output contains the new section stream handle.\r
418\r
419 @retval EFI_SUCCESS The section stream is created successfully.\r
420 @retval EFI_OUT_OF_RESOURCES memory allocation failed.\r
421 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end\r
422 of last section.\r
423\r
424**/\r
425EFI_STATUS\r
426EFIAPI\r
427OpenSectionStream (\r
428 IN UINTN SectionStreamLength,\r
429 IN VOID *SectionStream,\r
430 OUT UINTN *SectionStreamHandle\r
431 )\r
432{\r
433 //\r
434 // Check to see section stream looks good...\r
435 //\r
436 if (!IsValidSectionStream (SectionStream, SectionStreamLength)) {\r
437 return EFI_INVALID_PARAMETER;\r
438 }\r
439\r
440 return OpenSectionStreamEx (\r
441 SectionStreamLength,\r
442 SectionStream,\r
443 FALSE,\r
444 0,\r
445 SectionStreamHandle\r
446 );\r
447}\r
448\r
449\r
450\r
451/**\r
452 Worker function. Determine if the input stream:child matches the input type.\r
453\r
454 @param Stream Indicates the section stream associated with the\r
455 child\r
456 @param Child Indicates the child to check\r
457 @param SearchType Indicates the type of section to check against\r
458 for\r
459 @param SectionDefinitionGuid Indicates the GUID to check against if the type\r
460 is EFI_SECTION_GUID_DEFINED\r
461\r
462 @retval TRUE The child matches\r
463 @retval FALSE The child doesn't match\r
464\r
465**/\r
466BOOLEAN\r
467ChildIsType (\r
468 IN CORE_SECTION_STREAM_NODE *Stream,\r
469 IN CORE_SECTION_CHILD_NODE *Child,\r
470 IN EFI_SECTION_TYPE SearchType,\r
471 IN EFI_GUID *SectionDefinitionGuid\r
472 )\r
473{\r
474 EFI_GUID_DEFINED_SECTION *GuidedSection;\r
475\r
476 if (SearchType == EFI_SECTION_ALL) {\r
477 return TRUE;\r
478 }\r
479 if (Child->Type != SearchType) {\r
480 return FALSE;\r
481 }\r
482 if ((SearchType != EFI_SECTION_GUID_DEFINED) || (SectionDefinitionGuid == NULL)) {\r
483 return TRUE;\r
484 }\r
485 GuidedSection = (EFI_GUID_DEFINED_SECTION * )(Stream->StreamBuffer + Child->OffsetInStream);\r
486 if (IS_SECTION2 (GuidedSection)) {\r
487 return CompareGuid (&(((EFI_GUID_DEFINED_SECTION2 *) GuidedSection)->SectionDefinitionGuid), SectionDefinitionGuid);\r
488 } else {\r
489 return CompareGuid (&GuidedSection->SectionDefinitionGuid, SectionDefinitionGuid);\r
490 }\r
491}\r
492\r
493/**\r
494 Verify the Guided Section GUID by checking if there is the Guided Section GUID configuration table recorded the GUID itself.\r
495\r
496 @param GuidedSectionGuid The Guided Section GUID.\r
497 @param GuidedSectionExtraction A pointer to the pointer to the supported Guided Section Extraction Protocol\r
498 for the Guided Section.\r
499\r
500 @return TRUE The GuidedSectionGuid could be identified, and the pointer to\r
501 the Guided Section Extraction Protocol will be returned to *GuidedSectionExtraction.\r
502 @return FALSE The GuidedSectionGuid could not be identified, or \r
503 the Guided Section Extraction Protocol has not been installed yet.\r
504\r
505**/\r
506BOOLEAN\r
507VerifyGuidedSectionGuid (\r
508 IN EFI_GUID *GuidedSectionGuid,\r
509 OUT EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL **GuidedSectionExtraction\r
510 )\r
511{\r
512 EFI_GUID *GuidRecorded;\r
513 VOID *Interface;\r
514 EFI_STATUS Status;\r
515\r
516 Interface = NULL;\r
517\r
518 //\r
519 // Check if there is the Guided Section GUID configuration table recorded the GUID itself.\r
520 //\r
521 Status = EfiGetSystemConfigurationTable (GuidedSectionGuid, (VOID **) &GuidRecorded);\r
522 if (Status == EFI_SUCCESS) {\r
523 if (CompareGuid (GuidRecorded, GuidedSectionGuid)) {\r
524 //\r
525 // Found the recorded GuidedSectionGuid.\r
526 //\r
527 Status = CoreLocateProtocol (GuidedSectionGuid, NULL, (VOID **) &Interface);\r
528 if (!EFI_ERROR (Status) && Interface != NULL) {\r
529 //\r
530 // Found the supported Guided Section Extraction Porotocol for the Guided Section.\r
531 //\r
532 *GuidedSectionExtraction = (EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *) Interface;\r
533 return TRUE;\r
534 }\r
535 return FALSE;\r
536 }\r
537 }\r
538\r
539 return FALSE;\r
540}\r
541\r
542/**\r
543 RPN callback function. Initializes the section stream\r
544 when GUIDED_SECTION_EXTRACTION_PROTOCOL is installed.\r
545\r
546 @param Event The event that fired\r
547 @param RpnContext A pointer to the context that allows us to identify\r
548 the relevent encapsulation.\r
549**/\r
550VOID\r
551EFIAPI\r
552NotifyGuidedExtraction (\r
553 IN EFI_EVENT Event,\r
554 IN VOID *RpnContext\r
555 )\r
556{\r
557 EFI_STATUS Status;\r
558 EFI_GUID_DEFINED_SECTION *GuidedHeader;\r
559 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *GuidedExtraction;\r
560 VOID *NewStreamBuffer;\r
561 UINTN NewStreamBufferSize;\r
562 UINT32 AuthenticationStatus;\r
563 RPN_EVENT_CONTEXT *Context;\r
564 \r
565 Context = RpnContext;\r
566 \r
567 GuidedHeader = (EFI_GUID_DEFINED_SECTION *) (Context->ParentStream->StreamBuffer + Context->ChildNode->OffsetInStream);\r
568 ASSERT (GuidedHeader->CommonHeader.Type == EFI_SECTION_GUID_DEFINED);\r
569\r
570 if (!VerifyGuidedSectionGuid (Context->ChildNode->EncapsulationGuid, &GuidedExtraction)) {\r
571 return;\r
572 }\r
573\r
574 Status = GuidedExtraction->ExtractSection (\r
575 GuidedExtraction,\r
576 GuidedHeader,\r
577 &NewStreamBuffer,\r
578 &NewStreamBufferSize,\r
579 &AuthenticationStatus\r
580 );\r
581 ASSERT_EFI_ERROR (Status);\r
582\r
583 //\r
584 // Make sure we initialize the new stream with the correct\r
585 // authentication status for both aggregate and local status fields.\r
586 //\r
587 if ((GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0) {\r
588 //\r
589 // OR in the parent stream's aggregate status.\r
590 //\r
591 AuthenticationStatus |= Context->ParentStream->AuthenticationStatus & EFI_AUTH_STATUS_ALL;\r
592 } else {\r
593 //\r
594 // since there's no authentication data contributed by the section,\r
595 // just inherit the full value from our immediate parent.\r
596 //\r
597 AuthenticationStatus = Context->ParentStream->AuthenticationStatus;\r
598 }\r
599\r
600 Status = OpenSectionStreamEx (\r
601 NewStreamBufferSize,\r
602 NewStreamBuffer,\r
603 FALSE,\r
604 AuthenticationStatus,\r
605 &Context->ChildNode->EncapsulatedStreamHandle\r
606 );\r
607 ASSERT_EFI_ERROR (Status);\r
608\r
609 //\r
610 // Close the event when done.\r
611 //\r
612 gBS->CloseEvent (Event);\r
613 Context->ChildNode->Event = NULL;\r
614 FreePool (Context);\r
615} \r
616\r
617/**\r
618 Constructor for RPN event when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...\r
619\r
620 @param ParentStream Indicates the parent of the ecnapsulation section (child)\r
621 @param ChildNode Indicates the child node that is the encapsulation section.\r
622\r
623**/\r
624VOID\r
625CreateGuidedExtractionRpnEvent (\r
626 IN CORE_SECTION_STREAM_NODE *ParentStream,\r
627 IN CORE_SECTION_CHILD_NODE *ChildNode\r
628 )\r
629{\r
630 RPN_EVENT_CONTEXT *Context;\r
631 \r
632 //\r
633 // Allocate new event structure and context\r
634 //\r
635 Context = AllocatePool (sizeof (RPN_EVENT_CONTEXT));\r
636 ASSERT (Context != NULL);\r
637 \r
638 Context->ChildNode = ChildNode;\r
639 Context->ParentStream = ParentStream;\r
640 \r
641 Context->ChildNode->Event = EfiCreateProtocolNotifyEvent (\r
642 Context->ChildNode->EncapsulationGuid,\r
643 TPL_NOTIFY,\r
644 NotifyGuidedExtraction,\r
645 Context,\r
646 &Context->Registration\r
647 );\r
648}\r
649\r
650/**\r
651 Worker function. Constructor for new child nodes.\r
652\r
653 @param Stream Indicates the section stream in which to add the\r
654 child.\r
655 @param ChildOffset Indicates the offset in Stream that is the\r
656 beginning of the child section.\r
657 @param ChildNode Indicates the Callee allocated and initialized\r
658 child.\r
659\r
660 @retval EFI_SUCCESS Child node was found and returned.\r
661 EFI_OUT_OF_RESOURCES- Memory allocation failed.\r
662 @retval EFI_PROTOCOL_ERROR Encapsulation sections produce new stream\r
663 handles when the child node is created. If the\r
664 section type is GUID defined, and the extraction\r
665 GUID does not exist, and producing the stream\r
666 requires the GUID, then a protocol error is\r
667 generated and no child is produced. Values\r
668 returned by OpenSectionStreamEx.\r
669\r
670**/\r
671EFI_STATUS\r
672CreateChildNode (\r
673 IN CORE_SECTION_STREAM_NODE *Stream,\r
674 IN UINT32 ChildOffset,\r
675 OUT CORE_SECTION_CHILD_NODE **ChildNode\r
676 )\r
677{\r
678 EFI_STATUS Status;\r
679 EFI_COMMON_SECTION_HEADER *SectionHeader;\r
680 EFI_COMPRESSION_SECTION *CompressionHeader;\r
681 EFI_GUID_DEFINED_SECTION *GuidedHeader;\r
682 EFI_DECOMPRESS_PROTOCOL *Decompress;\r
683 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *GuidedExtraction;\r
684 VOID *NewStreamBuffer;\r
685 VOID *ScratchBuffer;\r
686 UINT32 ScratchSize;\r
687 UINTN NewStreamBufferSize;\r
688 UINT32 AuthenticationStatus;\r
689 VOID *CompressionSource;\r
690 UINT32 CompressionSourceSize;\r
691 UINT32 UncompressedLength;\r
692 UINT8 CompressionType;\r
693 UINT16 GuidedSectionAttributes;\r
694\r
695 CORE_SECTION_CHILD_NODE *Node;\r
696\r
697 SectionHeader = (EFI_COMMON_SECTION_HEADER *) (Stream->StreamBuffer + ChildOffset);\r
698\r
699 //\r
700 // Allocate a new node\r
701 //\r
702 *ChildNode = AllocateZeroPool (sizeof (CORE_SECTION_CHILD_NODE));\r
703 Node = *ChildNode;\r
704 if (Node == NULL) {\r
705 return EFI_OUT_OF_RESOURCES;\r
706 }\r
707\r
708 //\r
709 // Now initialize it\r
710 //\r
711 Node->Signature = CORE_SECTION_CHILD_SIGNATURE;\r
712 Node->Type = SectionHeader->Type;\r
713 if (IS_SECTION2 (SectionHeader)) {\r
714 Node->Size = SECTION2_SIZE (SectionHeader);\r
715 } else {\r
716 Node->Size = SECTION_SIZE (SectionHeader);\r
717 }\r
718 Node->OffsetInStream = ChildOffset;\r
719 Node->EncapsulatedStreamHandle = NULL_STREAM_HANDLE;\r
720 Node->EncapsulationGuid = NULL;\r
721\r
722 //\r
723 // If it's an encapsulating section, then create the new section stream also\r
724 //\r
725 switch (Node->Type) {\r
726 case EFI_SECTION_COMPRESSION:\r
727 //\r
728 // Get the CompressionSectionHeader\r
729 //\r
730 if (Node->Size < sizeof (EFI_COMPRESSION_SECTION)) {\r
731 CoreFreePool (Node);\r
732 return EFI_NOT_FOUND;\r
733 }\r
734\r
735 CompressionHeader = (EFI_COMPRESSION_SECTION *) SectionHeader;\r
736\r
737 if (IS_SECTION2 (CompressionHeader)) {\r
738 CompressionSource = (VOID *) ((UINT8 *) CompressionHeader + sizeof (EFI_COMPRESSION_SECTION2));\r
739 CompressionSourceSize = (UINT32) (SECTION2_SIZE (CompressionHeader) - sizeof (EFI_COMPRESSION_SECTION2));\r
740 UncompressedLength = ((EFI_COMPRESSION_SECTION2 *) CompressionHeader)->UncompressedLength;\r
741 CompressionType = ((EFI_COMPRESSION_SECTION2 *) CompressionHeader)->CompressionType;\r
742 } else {\r
743 CompressionSource = (VOID *) ((UINT8 *) CompressionHeader + sizeof (EFI_COMPRESSION_SECTION));\r
744 CompressionSourceSize = (UINT32) (SECTION_SIZE (CompressionHeader) - sizeof (EFI_COMPRESSION_SECTION));\r
745 UncompressedLength = CompressionHeader->UncompressedLength;\r
746 CompressionType = CompressionHeader->CompressionType;\r
747 }\r
748\r
749 //\r
750 // Allocate space for the new stream\r
751 //\r
752 if (UncompressedLength > 0) {\r
753 NewStreamBufferSize = UncompressedLength;\r
754 NewStreamBuffer = AllocatePool (NewStreamBufferSize);\r
755 if (NewStreamBuffer == NULL) {\r
756 CoreFreePool (Node);\r
757 return EFI_OUT_OF_RESOURCES;\r
758 }\r
759\r
760 if (CompressionType == EFI_NOT_COMPRESSED) {\r
761 //\r
762 // stream is not actually compressed, just encapsulated. So just copy it.\r
763 //\r
764 CopyMem (NewStreamBuffer, CompressionSource, NewStreamBufferSize);\r
765 } else if (CompressionType == EFI_STANDARD_COMPRESSION) {\r
766 //\r
767 // Only support the EFI_SATNDARD_COMPRESSION algorithm.\r
768 //\r
769\r
770 //\r
771 // Decompress the stream\r
772 //\r
773 Status = CoreLocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **)&Decompress);\r
774 ASSERT_EFI_ERROR (Status);\r
775 ASSERT (Decompress != NULL);\r
776\r
777 Status = Decompress->GetInfo (\r
778 Decompress,\r
779 CompressionSource,\r
780 CompressionSourceSize,\r
781 (UINT32 *)&NewStreamBufferSize,\r
782 &ScratchSize\r
783 );\r
784 if (EFI_ERROR (Status) || (NewStreamBufferSize != UncompressedLength)) {\r
785 CoreFreePool (Node);\r
786 CoreFreePool (NewStreamBuffer);\r
787 if (!EFI_ERROR (Status)) {\r
788 Status = EFI_BAD_BUFFER_SIZE;\r
789 }\r
790 return Status;\r
791 }\r
792\r
793 ScratchBuffer = AllocatePool (ScratchSize);\r
794 if (ScratchBuffer == NULL) {\r
795 CoreFreePool (Node);\r
796 CoreFreePool (NewStreamBuffer);\r
797 return EFI_OUT_OF_RESOURCES;\r
798 }\r
799\r
800 Status = Decompress->Decompress (\r
801 Decompress,\r
802 CompressionSource,\r
803 CompressionSourceSize,\r
804 NewStreamBuffer,\r
805 (UINT32)NewStreamBufferSize,\r
806 ScratchBuffer,\r
807 ScratchSize\r
808 );\r
809 CoreFreePool (ScratchBuffer);\r
810 if (EFI_ERROR (Status)) {\r
811 CoreFreePool (Node);\r
812 CoreFreePool (NewStreamBuffer);\r
813 return Status;\r
814 }\r
815 }\r
816 } else {\r
817 NewStreamBuffer = NULL;\r
818 NewStreamBufferSize = 0;\r
819 }\r
820\r
821 Status = OpenSectionStreamEx (\r
822 NewStreamBufferSize,\r
823 NewStreamBuffer,\r
824 FALSE,\r
825 Stream->AuthenticationStatus,\r
826 &Node->EncapsulatedStreamHandle\r
827 );\r
828 if (EFI_ERROR (Status)) {\r
829 CoreFreePool (Node);\r
830 CoreFreePool (NewStreamBuffer);\r
831 return Status;\r
832 }\r
833 break;\r
834\r
835 case EFI_SECTION_GUID_DEFINED:\r
836 GuidedHeader = (EFI_GUID_DEFINED_SECTION *) SectionHeader;\r
837 if (IS_SECTION2 (GuidedHeader)) {\r
838 Node->EncapsulationGuid = &(((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->SectionDefinitionGuid);\r
839 GuidedSectionAttributes = ((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->Attributes;\r
840 } else {\r
841 Node->EncapsulationGuid = &GuidedHeader->SectionDefinitionGuid;\r
842 GuidedSectionAttributes = GuidedHeader->Attributes;\r
843 }\r
844 if (VerifyGuidedSectionGuid (Node->EncapsulationGuid, &GuidedExtraction)) {\r
845 //\r
846 // NewStreamBuffer is always allocated by ExtractSection... No caller\r
847 // allocation here.\r
848 //\r
849 Status = GuidedExtraction->ExtractSection (\r
850 GuidedExtraction,\r
851 GuidedHeader,\r
852 &NewStreamBuffer,\r
853 &NewStreamBufferSize,\r
854 &AuthenticationStatus\r
855 );\r
856 if (EFI_ERROR (Status)) {\r
857 CoreFreePool (*ChildNode);\r
858 return EFI_PROTOCOL_ERROR;\r
859 }\r
860\r
861 //\r
862 // Make sure we initialize the new stream with the correct\r
863 // authentication status for both aggregate and local status fields.\r
864 //\r
865 if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0) {\r
866 //\r
867 // OR in the parent stream's aggregate status.\r
868 //\r
869 AuthenticationStatus |= Stream->AuthenticationStatus & EFI_AUTH_STATUS_ALL;\r
870 } else {\r
871 //\r
872 // since there's no authentication data contributed by the section,\r
873 // just inherit the full value from our immediate parent.\r
874 //\r
875 AuthenticationStatus = Stream->AuthenticationStatus;\r
876 }\r
877\r
878 Status = OpenSectionStreamEx (\r
879 NewStreamBufferSize,\r
880 NewStreamBuffer,\r
881 FALSE,\r
882 AuthenticationStatus,\r
883 &Node->EncapsulatedStreamHandle\r
884 );\r
885 if (EFI_ERROR (Status)) {\r
886 CoreFreePool (*ChildNode);\r
887 CoreFreePool (NewStreamBuffer);\r
888 return Status;\r
889 }\r
890 } else {\r
891 //\r
892 // There's no GUIDed section extraction protocol available.\r
893 //\r
894 if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) {\r
895 //\r
896 // If the section REQUIRES an extraction protocol, register for RPN \r
897 // when the required GUIDed extraction protocol becomes available. \r
898 //\r
899 CreateGuidedExtractionRpnEvent (Stream, Node);\r
900 } else {\r
901 //\r
902 // Figure out the proper authentication status\r
903 //\r
904 AuthenticationStatus = Stream->AuthenticationStatus;\r
905\r
906 if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {\r
907 AuthenticationStatus |= EFI_AUTH_STATUS_IMAGE_SIGNED | EFI_AUTH_STATUS_NOT_TESTED;\r
908 }\r
909\r
910 if (IS_SECTION2 (GuidedHeader)) {\r
911 Status = OpenSectionStreamEx (\r
912 SECTION2_SIZE (GuidedHeader) - ((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->DataOffset,\r
913 (UINT8 *) GuidedHeader + ((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->DataOffset,\r
914 TRUE,\r
915 AuthenticationStatus,\r
916 &Node->EncapsulatedStreamHandle\r
917 );\r
918 } else {\r
919 Status = OpenSectionStreamEx (\r
920 SECTION_SIZE (GuidedHeader) - ((EFI_GUID_DEFINED_SECTION *) GuidedHeader)->DataOffset,\r
921 (UINT8 *) GuidedHeader + ((EFI_GUID_DEFINED_SECTION *) GuidedHeader)->DataOffset,\r
922 TRUE,\r
923 AuthenticationStatus,\r
924 &Node->EncapsulatedStreamHandle\r
925 );\r
926 }\r
927 if (EFI_ERROR (Status)) {\r
928 CoreFreePool (Node);\r
929 return Status;\r
930 }\r
931 }\r
932 }\r
933\r
934 break;\r
935\r
936 default:\r
937\r
938 //\r
939 // Nothing to do if it's a leaf\r
940 //\r
941 break;\r
942 }\r
943\r
944 //\r
945 // Last, add the new child node to the stream\r
946 //\r
947 InsertTailList (&Stream->Children, &Node->Link);\r
948\r
949 return EFI_SUCCESS;\r
950}\r
951\r
952\r
953/**\r
954 Worker function Recursively searches / builds section stream database\r
955 looking for requested section.\r
956\r
957 @param SourceStream Indicates the section stream in which to do the\r
958 search.\r
959 @param SearchType Indicates the type of section to search for.\r
960 @param SectionInstance Indicates which instance of section to find.\r
961 This is an in/out parameter to deal with\r
962 recursions.\r
963 @param SectionDefinitionGuid Guid of section definition\r
964 @param FoundChild Output indicating the child node that is found.\r
965 @param FoundStream Output indicating which section stream the child\r
966 was found in. If this stream was generated as a\r
967 result of an encapsulation section, the\r
968 streamhandle is visible within the SEP driver\r
969 only.\r
970 @param AuthenticationStatus Indicates the authentication status of the found section.\r
971\r
972 @retval EFI_SUCCESS Child node was found and returned.\r
973 EFI_OUT_OF_RESOURCES- Memory allocation failed.\r
974 @retval EFI_NOT_FOUND Requested child node does not exist.\r
975 @retval EFI_PROTOCOL_ERROR a required GUIDED section extraction protocol\r
976 does not exist\r
977\r
978**/\r
979EFI_STATUS\r
980FindChildNode (\r
981 IN CORE_SECTION_STREAM_NODE *SourceStream,\r
982 IN EFI_SECTION_TYPE SearchType,\r
983 IN OUT UINTN *SectionInstance,\r
984 IN EFI_GUID *SectionDefinitionGuid,\r
985 OUT CORE_SECTION_CHILD_NODE **FoundChild,\r
986 OUT CORE_SECTION_STREAM_NODE **FoundStream,\r
987 OUT UINT32 *AuthenticationStatus\r
988 )\r
989{\r
990 CORE_SECTION_CHILD_NODE *CurrentChildNode;\r
991 CORE_SECTION_CHILD_NODE *RecursedChildNode;\r
992 CORE_SECTION_STREAM_NODE *RecursedFoundStream;\r
993 UINT32 NextChildOffset;\r
994 EFI_STATUS ErrorStatus;\r
995 EFI_STATUS Status;\r
996\r
997 CurrentChildNode = NULL;\r
998 ErrorStatus = EFI_NOT_FOUND;\r
999\r
1000 if (SourceStream->StreamLength == 0) {\r
1001 return EFI_NOT_FOUND;\r
1002 }\r
1003\r
1004 if (IsListEmpty (&SourceStream->Children) &&\r
1005 SourceStream->StreamLength >= sizeof (EFI_COMMON_SECTION_HEADER)) {\r
1006 //\r
1007 // This occurs when a section stream exists, but no child sections\r
1008 // have been parsed out yet. Therefore, extract the first child and add it\r
1009 // to the list of children so we can get started.\r
1010 // Section stream may contain an array of zero or more bytes.\r
1011 // So, its size should be >= the size of commen section header.\r
1012 //\r
1013 Status = CreateChildNode (SourceStream, 0, &CurrentChildNode);\r
1014 if (EFI_ERROR (Status)) {\r
1015 return Status;\r
1016 }\r
1017 }\r
1018\r
1019 //\r
1020 // At least one child has been parsed out of the section stream. So, walk\r
1021 // through the sections that have already been parsed out looking for the\r
1022 // requested section, if necessary, continue parsing section stream and\r
1023 // adding children until either the requested section is found, or we run\r
1024 // out of data\r
1025 //\r
1026 CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream->Children));\r
1027\r
1028 for (;;) {\r
1029 ASSERT (CurrentChildNode != NULL);\r
1030 if (ChildIsType (SourceStream, CurrentChildNode, SearchType, SectionDefinitionGuid)) {\r
1031 //\r
1032 // The type matches, so check the instance count to see if it's the one we want\r
1033 //\r
1034 (*SectionInstance)--;\r
1035 if (*SectionInstance == 0) {\r
1036 //\r
1037 // Got it!\r
1038 //\r
1039 *FoundChild = CurrentChildNode;\r
1040 *FoundStream = SourceStream;\r
1041 *AuthenticationStatus = SourceStream->AuthenticationStatus;\r
1042 return EFI_SUCCESS;\r
1043 }\r
1044 }\r
1045\r
1046 if (CurrentChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {\r
1047 //\r
1048 // If the current node is an encapsulating node, recurse into it...\r
1049 //\r
1050 Status = FindChildNode (\r
1051 (CORE_SECTION_STREAM_NODE *)CurrentChildNode->EncapsulatedStreamHandle,\r
1052 SearchType,\r
1053 SectionInstance,\r
1054 SectionDefinitionGuid,\r
1055 &RecursedChildNode,\r
1056 &RecursedFoundStream,\r
1057 AuthenticationStatus\r
1058 );\r
1059 //\r
1060 // If the status is not EFI_SUCCESS, just save the error code and continue\r
1061 // to find the request child node in the rest stream.\r
1062 //\r
1063 if (*SectionInstance == 0) {\r
1064 ASSERT_EFI_ERROR (Status);\r
1065 *FoundChild = RecursedChildNode;\r
1066 *FoundStream = RecursedFoundStream;\r
1067 return EFI_SUCCESS;\r
1068 } else {\r
1069 ErrorStatus = Status;\r
1070 }\r
1071 } else if ((CurrentChildNode->Type == EFI_SECTION_GUID_DEFINED) && (SearchType != EFI_SECTION_GUID_DEFINED)) {\r
1072 //\r
1073 // When Node Type is GUIDED section, but Node has no encapsulated data, Node data should not be parsed\r
1074 // because a required GUIDED section extraction protocol does not exist.\r
1075 // If SearchType is not GUIDED section, EFI_PROTOCOL_ERROR should return.\r
1076 //\r
1077 ErrorStatus = EFI_PROTOCOL_ERROR;\r
1078 }\r
1079\r
1080 if (!IsNodeAtEnd (&SourceStream->Children, &CurrentChildNode->Link)) {\r
1081 //\r
1082 // We haven't found the child node we're interested in yet, but there's\r
1083 // still more nodes that have already been parsed so get the next one\r
1084 // and continue searching..\r
1085 //\r
1086 CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream->Children, &CurrentChildNode->Link));\r
1087 } else {\r
1088 //\r
1089 // We've exhausted children that have already been parsed, so see if\r
1090 // there's any more data and continue parsing out more children if there\r
1091 // is.\r
1092 //\r
1093 NextChildOffset = CurrentChildNode->OffsetInStream + CurrentChildNode->Size;\r
1094 //\r
1095 // Round up to 4 byte boundary\r
1096 //\r
1097 NextChildOffset += 3;\r
1098 NextChildOffset &= ~(UINTN) 3;\r
1099 if (NextChildOffset <= SourceStream->StreamLength - sizeof (EFI_COMMON_SECTION_HEADER)) {\r
1100 //\r
1101 // There's an unparsed child remaining in the stream, so create a new child node\r
1102 //\r
1103 Status = CreateChildNode (SourceStream, NextChildOffset, &CurrentChildNode);\r
1104 if (EFI_ERROR (Status)) {\r
1105 return Status;\r
1106 }\r
1107 } else {\r
1108 ASSERT (EFI_ERROR (ErrorStatus));\r
1109 return ErrorStatus;\r
1110 }\r
1111 }\r
1112 }\r
1113}\r
1114\r
1115\r
1116/**\r
1117 Worker function. Search stream database for requested stream handle.\r
1118\r
1119 @param SearchHandle Indicates which stream to look for.\r
1120 @param FoundStream Output pointer to the found stream.\r
1121\r
1122 @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains\r
1123 the stream node.\r
1124 @retval EFI_NOT_FOUND SearchHandle was not found in the stream\r
1125 database.\r
1126\r
1127**/\r
1128EFI_STATUS\r
1129FindStreamNode (\r
1130 IN UINTN SearchHandle,\r
1131 OUT CORE_SECTION_STREAM_NODE **FoundStream\r
1132 )\r
1133{\r
1134 CORE_SECTION_STREAM_NODE *StreamNode;\r
1135\r
1136 if (!IsListEmpty (&mStreamRoot)) {\r
1137 StreamNode = STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot));\r
1138 for (;;) {\r
1139 if (StreamNode->StreamHandle == SearchHandle) {\r
1140 *FoundStream = StreamNode;\r
1141 return EFI_SUCCESS;\r
1142 } else if (IsNodeAtEnd (&mStreamRoot, &StreamNode->Link)) {\r
1143 break;\r
1144 } else {\r
1145 StreamNode = STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot, &StreamNode->Link));\r
1146 }\r
1147 }\r
1148 }\r
1149\r
1150 return EFI_NOT_FOUND;\r
1151}\r
1152\r
1153\r
1154/**\r
1155 SEP member function. Retrieves requested section from section stream.\r
1156\r
1157 @param SectionStreamHandle The section stream from which to extract the\r
1158 requested section.\r
1159 @param SectionType A pointer to the type of section to search for.\r
1160 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED,\r
1161 then SectionDefinitionGuid indicates which of\r
1162 these types of sections to search for.\r
1163 @param SectionInstance Indicates which instance of the requested\r
1164 section to return.\r
1165 @param Buffer Double indirection to buffer. If *Buffer is\r
1166 non-null on input, then the buffer is caller\r
1167 allocated. If Buffer is NULL, then the buffer\r
1168 is callee allocated. In either case, the\r
1169 required buffer size is returned in *BufferSize.\r
1170 @param BufferSize On input, indicates the size of *Buffer if\r
1171 *Buffer is non-null on input. On output,\r
1172 indicates the required size (allocated size if\r
1173 callee allocated) of *Buffer.\r
1174 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that\r
1175 indicates the authentication status of the\r
1176 output buffer. If the input section's\r
1177 GuidedSectionHeader.Attributes field\r
1178 has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
1179 bit as clear, AuthenticationStatus must return\r
1180 zero. Both local bits (19:16) and aggregate\r
1181 bits (3:0) in AuthenticationStatus are returned\r
1182 by ExtractSection(). These bits reflect the\r
1183 status of the extraction operation. The bit\r
1184 pattern in both regions must be the same, as\r
1185 the local and aggregate authentication statuses\r
1186 have equivalent meaning at this level. If the\r
1187 function returns anything other than\r
1188 EFI_SUCCESS, the value of *AuthenticationStatus\r
1189 is undefined.\r
1190 @param IsFfs3Fv Indicates the FV format.\r
1191\r
1192 @retval EFI_SUCCESS Section was retrieved successfully\r
1193 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the\r
1194 section stream with its\r
1195 EFI_GUIDED_SECTION_PROCESSING_REQUIRED bit set,\r
1196 but there was no corresponding GUIDed Section\r
1197 Extraction Protocol in the handle database.\r
1198 *Buffer is unmodified.\r
1199 @retval EFI_NOT_FOUND An error was encountered when parsing the\r
1200 SectionStream. This indicates the SectionStream\r
1201 is not correctly formatted.\r
1202 @retval EFI_NOT_FOUND The requested section does not exist.\r
1203 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process\r
1204 the request.\r
1205 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.\r
1206 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is\r
1207 insufficient to contain the requested section.\r
1208 The input buffer is filled and section contents\r
1209 are truncated.\r
1210\r
1211**/\r
1212EFI_STATUS\r
1213EFIAPI\r
1214GetSection (\r
1215 IN UINTN SectionStreamHandle,\r
1216 IN EFI_SECTION_TYPE *SectionType,\r
1217 IN EFI_GUID *SectionDefinitionGuid,\r
1218 IN UINTN SectionInstance,\r
1219 IN VOID **Buffer,\r
1220 IN OUT UINTN *BufferSize,\r
1221 OUT UINT32 *AuthenticationStatus,\r
1222 IN BOOLEAN IsFfs3Fv\r
1223 )\r
1224{\r
1225 CORE_SECTION_STREAM_NODE *StreamNode;\r
1226 EFI_TPL OldTpl;\r
1227 EFI_STATUS Status;\r
1228 CORE_SECTION_CHILD_NODE *ChildNode;\r
1229 CORE_SECTION_STREAM_NODE *ChildStreamNode;\r
1230 UINTN CopySize;\r
1231 UINT32 ExtractedAuthenticationStatus;\r
1232 UINTN Instance;\r
1233 UINT8 *CopyBuffer;\r
1234 UINTN SectionSize;\r
1235 EFI_COMMON_SECTION_HEADER *Section;\r
1236\r
1237\r
1238 ChildStreamNode = NULL;\r
1239 OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
1240 Instance = SectionInstance + 1;\r
1241\r
1242 //\r
1243 // Locate target stream\r
1244 //\r
1245 Status = FindStreamNode (SectionStreamHandle, &StreamNode);\r
1246 if (EFI_ERROR (Status)) {\r
1247 Status = EFI_INVALID_PARAMETER;\r
1248 goto GetSection_Done;\r
1249 }\r
1250\r
1251 //\r
1252 // Found the stream, now locate and return the appropriate section\r
1253 //\r
1254 if (SectionType == NULL) {\r
1255 //\r
1256 // SectionType == NULL means return the WHOLE section stream...\r
1257 //\r
1258 CopySize = StreamNode->StreamLength;\r
1259 CopyBuffer = StreamNode->StreamBuffer;\r
1260 *AuthenticationStatus = StreamNode->AuthenticationStatus;\r
1261 } else {\r
1262 //\r
1263 // There's a requested section type, so go find it and return it...\r
1264 //\r
1265 Status = FindChildNode (\r
1266 StreamNode,\r
1267 *SectionType,\r
1268 &Instance,\r
1269 SectionDefinitionGuid,\r
1270 &ChildNode,\r
1271 &ChildStreamNode,\r
1272 &ExtractedAuthenticationStatus\r
1273 );\r
1274 if (EFI_ERROR (Status)) {\r
1275 goto GetSection_Done;\r
1276 }\r
1277\r
1278 Section = (EFI_COMMON_SECTION_HEADER *) (ChildStreamNode->StreamBuffer + ChildNode->OffsetInStream);\r
1279\r
1280 if (IS_SECTION2 (Section)) {\r
1281 ASSERT (SECTION2_SIZE (Section) > 0x00FFFFFF);\r
1282 if (!IsFfs3Fv) {\r
1283 DEBUG ((DEBUG_ERROR, "It is a FFS3 formatted section in a non-FFS3 formatted FV.\n"));\r
1284 Status = EFI_NOT_FOUND;\r
1285 goto GetSection_Done;\r
1286 }\r
1287 CopySize = SECTION2_SIZE (Section) - sizeof (EFI_COMMON_SECTION_HEADER2);\r
1288 CopyBuffer = (UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER2);\r
1289 } else {\r
1290 CopySize = SECTION_SIZE (Section) - sizeof (EFI_COMMON_SECTION_HEADER);\r
1291 CopyBuffer = (UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER);\r
1292 }\r
1293 *AuthenticationStatus = ExtractedAuthenticationStatus;\r
1294 }\r
1295\r
1296 SectionSize = CopySize;\r
1297 if (*Buffer != NULL) {\r
1298 //\r
1299 // Caller allocated buffer. Fill to size and return required size...\r
1300 //\r
1301 if (*BufferSize < CopySize) {\r
1302 Status = EFI_WARN_BUFFER_TOO_SMALL;\r
1303 CopySize = *BufferSize;\r
1304 }\r
1305 } else {\r
1306 //\r
1307 // Callee allocated buffer. Allocate buffer and return size.\r
1308 //\r
1309 *Buffer = AllocatePool (CopySize);\r
1310 if (*Buffer == NULL) {\r
1311 Status = EFI_OUT_OF_RESOURCES;\r
1312 goto GetSection_Done;\r
1313 }\r
1314 }\r
1315 CopyMem (*Buffer, CopyBuffer, CopySize);\r
1316 *BufferSize = SectionSize;\r
1317\r
1318GetSection_Done:\r
1319 CoreRestoreTpl (OldTpl);\r
1320\r
1321 return Status;\r
1322}\r
1323\r
1324\r
1325/**\r
1326 Worker function. Destructor for child nodes.\r
1327\r
1328 @param ChildNode Indicates the node to destroy\r
1329\r
1330**/\r
1331VOID\r
1332FreeChildNode (\r
1333 IN CORE_SECTION_CHILD_NODE *ChildNode\r
1334 )\r
1335{\r
1336 ASSERT (ChildNode->Signature == CORE_SECTION_CHILD_SIGNATURE);\r
1337 //\r
1338 // Remove the child from it's list\r
1339 //\r
1340 RemoveEntryList (&ChildNode->Link);\r
1341\r
1342 if (ChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {\r
1343 //\r
1344 // If it's an encapsulating section, we close the resulting section stream.\r
1345 // CloseSectionStream will free all memory associated with the stream.\r
1346 //\r
1347 CloseSectionStream (ChildNode->EncapsulatedStreamHandle, TRUE);\r
1348 }\r
1349\r
1350 if (ChildNode->Event != NULL) {\r
1351 gBS->CloseEvent (ChildNode->Event);\r
1352 }\r
1353\r
1354 //\r
1355 // Last, free the child node itself\r
1356 //\r
1357 CoreFreePool (ChildNode);\r
1358}\r
1359\r
1360\r
1361/**\r
1362 SEP member function. Deletes an existing section stream\r
1363\r
1364 @param StreamHandleToClose Indicates the stream to close\r
1365 @param FreeStreamBuffer TRUE - Need to free stream buffer;\r
1366 FALSE - No need to free stream buffer.\r
1367\r
1368 @retval EFI_SUCCESS The section stream is closed sucessfully.\r
1369 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
1370 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end\r
1371 of last section.\r
1372\r
1373**/\r
1374EFI_STATUS\r
1375EFIAPI\r
1376CloseSectionStream (\r
1377 IN UINTN StreamHandleToClose,\r
1378 IN BOOLEAN FreeStreamBuffer\r
1379 )\r
1380{\r
1381 CORE_SECTION_STREAM_NODE *StreamNode;\r
1382 EFI_TPL OldTpl;\r
1383 EFI_STATUS Status;\r
1384 LIST_ENTRY *Link;\r
1385 CORE_SECTION_CHILD_NODE *ChildNode;\r
1386\r
1387 OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
1388\r
1389 //\r
1390 // Locate target stream\r
1391 //\r
1392 Status = FindStreamNode (StreamHandleToClose, &StreamNode);\r
1393 if (!EFI_ERROR (Status)) {\r
1394 //\r
1395 // Found the stream, so close it\r
1396 //\r
1397 RemoveEntryList (&StreamNode->Link);\r
1398 while (!IsListEmpty (&StreamNode->Children)) {\r
1399 Link = GetFirstNode (&StreamNode->Children);\r
1400 ChildNode = CHILD_SECTION_NODE_FROM_LINK (Link);\r
1401 FreeChildNode (ChildNode);\r
1402 }\r
1403 if (FreeStreamBuffer) {\r
1404 CoreFreePool (StreamNode->StreamBuffer);\r
1405 }\r
1406 CoreFreePool (StreamNode);\r
1407 Status = EFI_SUCCESS;\r
1408 } else {\r
1409 Status = EFI_INVALID_PARAMETER;\r
1410 }\r
1411\r
1412 CoreRestoreTpl (OldTpl);\r
1413 return Status;\r
1414}\r
1415\r
1416\r
1417/**\r
1418 The ExtractSection() function processes the input section and\r
1419 allocates a buffer from the pool in which it returns the section\r
1420 contents. If the section being extracted contains\r
1421 authentication information (the section's\r
1422 GuidedSectionHeader.Attributes field has the\r
1423 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values\r
1424 returned in AuthenticationStatus must reflect the results of\r
1425 the authentication operation. Depending on the algorithm and\r
1426 size of the encapsulated data, the time that is required to do\r
1427 a full authentication may be prohibitively long for some\r
1428 classes of systems. To indicate this, use\r
1429 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by\r
1430 the security policy driver (see the Platform Initialization\r
1431 Driver Execution Environment Core Interface Specification for\r
1432 more details and the GUID definition). If the\r
1433 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle\r
1434 database, then, if possible, full authentication should be\r
1435 skipped and the section contents simply returned in the\r
1436 OutputBuffer. In this case, the\r
1437 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus\r
1438 must be set on return. ExtractSection() is callable only from\r
1439 TPL_NOTIFY and below. Behavior of ExtractSection() at any\r
1440 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is\r
1441 defined in RaiseTPL() in the UEFI 2.0 specification.\r
1442\r
1443\r
1444 @param This Indicates the\r
1445 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.\r
1446 @param InputSection Buffer containing the input GUIDed section\r
1447 to be processed. OutputBuffer OutputBuffer\r
1448 is allocated from boot services pool\r
1449 memory and contains the new section\r
1450 stream. The caller is responsible for\r
1451 freeing this buffer.\r
1452 @param OutputBuffer *OutputBuffer is allocated from boot services\r
1453 pool memory and contains the new section stream.\r
1454 The caller is responsible for freeing this buffer.\r
1455 @param OutputSize A pointer to a caller-allocated UINTN in\r
1456 which the size of OutputBuffer allocation\r
1457 is stored. If the function returns\r
1458 anything other than EFI_SUCCESS, the value\r
1459 of OutputSize is undefined.\r
1460\r
1461 @param AuthenticationStatus A pointer to a caller-allocated\r
1462 UINT32 that indicates the\r
1463 authentication status of the\r
1464 output buffer. If the input\r
1465 section's\r
1466 GuidedSectionHeader.Attributes\r
1467 field has the\r
1468 EFI_GUIDED_SECTION_AUTH_STATUS_VAL\r
1469 bit as clear, AuthenticationStatus\r
1470 must return zero. Both local bits\r
1471 (19:16) and aggregate bits (3:0)\r
1472 in AuthenticationStatus are\r
1473 returned by ExtractSection().\r
1474 These bits reflect the status of\r
1475 the extraction operation. The bit\r
1476 pattern in both regions must be\r
1477 the same, as the local and\r
1478 aggregate authentication statuses\r
1479 have equivalent meaning at this\r
1480 level. If the function returns\r
1481 anything other than EFI_SUCCESS,\r
1482 the value of AuthenticationStatus\r
1483 is undefined.\r
1484\r
1485\r
1486 @retval EFI_SUCCESS The InputSection was successfully\r
1487 processed and the section contents were\r
1488 returned.\r
1489\r
1490 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
1491 resources to process the\r
1492 request.\r
1493\r
1494 @retval EFI_INVALID_PARAMETER The GUID in InputSection does\r
1495 not match this instance of the\r
1496 GUIDed Section Extraction\r
1497 Protocol.\r
1498\r
1499**/\r
1500EFI_STATUS\r
1501EFIAPI\r
1502CustomGuidedSectionExtract (\r
1503 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,\r
1504 IN CONST VOID *InputSection,\r
1505 OUT VOID **OutputBuffer,\r
1506 OUT UINTN *OutputSize,\r
1507 OUT UINT32 *AuthenticationStatus\r
1508 )\r
1509{\r
1510 EFI_STATUS Status;\r
1511 VOID *ScratchBuffer;\r
1512 VOID *AllocatedOutputBuffer;\r
1513 UINT32 OutputBufferSize;\r
1514 UINT32 ScratchBufferSize;\r
1515 UINT16 SectionAttribute;\r
1516\r
1517 //\r
1518 // Init local variable\r
1519 //\r
1520 ScratchBuffer = NULL;\r
1521 AllocatedOutputBuffer = NULL;\r
1522\r
1523 //\r
1524 // Call GetInfo to get the size and attribute of input guided section data.\r
1525 //\r
1526 Status = ExtractGuidedSectionGetInfo (\r
1527 InputSection,\r
1528 &OutputBufferSize,\r
1529 &ScratchBufferSize,\r
1530 &SectionAttribute\r
1531 );\r
1532\r
1533 if (EFI_ERROR (Status)) {\r
1534 DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status));\r
1535 return Status;\r
1536 }\r
1537\r
1538 if (ScratchBufferSize > 0) {\r
1539 //\r
1540 // Allocate scratch buffer\r
1541 //\r
1542 ScratchBuffer = AllocatePool (ScratchBufferSize);\r
1543 if (ScratchBuffer == NULL) {\r
1544 return EFI_OUT_OF_RESOURCES;\r
1545 }\r
1546 }\r
1547\r
1548 if (OutputBufferSize > 0) {\r
1549 //\r
1550 // Allocate output buffer\r
1551 //\r
1552 AllocatedOutputBuffer = AllocatePool (OutputBufferSize);\r
1553 if (AllocatedOutputBuffer == NULL) {\r
1554 if (ScratchBuffer != NULL) {\r
1555 FreePool (ScratchBuffer);\r
1556 }\r
1557 return EFI_OUT_OF_RESOURCES;\r
1558 }\r
1559 *OutputBuffer = AllocatedOutputBuffer;\r
1560 }\r
1561\r
1562 //\r
1563 // Call decode function to extract raw data from the guided section.\r
1564 //\r
1565 Status = ExtractGuidedSectionDecode (\r
1566 InputSection,\r
1567 OutputBuffer,\r
1568 ScratchBuffer,\r
1569 AuthenticationStatus\r
1570 );\r
1571 if (EFI_ERROR (Status)) {\r
1572 //\r
1573 // Decode failed\r
1574 //\r
1575 if (AllocatedOutputBuffer != NULL) {\r
1576 CoreFreePool (AllocatedOutputBuffer);\r
1577 }\r
1578 if (ScratchBuffer != NULL) {\r
1579 CoreFreePool (ScratchBuffer);\r
1580 }\r
1581 DEBUG ((DEBUG_ERROR, "Extract guided section Failed - %r\n", Status));\r
1582 return Status;\r
1583 }\r
1584\r
1585 if (*OutputBuffer != AllocatedOutputBuffer) {\r
1586 //\r
1587 // OutputBuffer was returned as a different value,\r
1588 // so copy section contents to the allocated memory buffer.\r
1589 //\r
1590 CopyMem (AllocatedOutputBuffer, *OutputBuffer, OutputBufferSize);\r
1591 *OutputBuffer = AllocatedOutputBuffer;\r
1592 }\r
1593\r
1594 //\r
1595 // Set real size of output buffer.\r
1596 //\r
1597 *OutputSize = (UINTN) OutputBufferSize;\r
1598\r
1599 //\r
1600 // Free unused scratch buffer.\r
1601 //\r
1602 if (ScratchBuffer != NULL) {\r
1603 CoreFreePool (ScratchBuffer);\r
1604 }\r
1605\r
1606 return EFI_SUCCESS;\r
1607}\r