2 Section Extraction Protocol implementation.
4 Stream database is implemented as a linked list of section streams,
5 where each stream contains a linked list of children, which may be leaves or
8 Children that are encapsulations generate new stream entries
9 when they are created. Streams can also be created by calls to
10 SEP->OpenSectionStream().
12 The database is only created far enough to return the requested data from
13 any given stream, or to determine that the requested data is not found.
15 If a GUIDed encapsulation is encountered, there are three possiblilites.
17 1) A support protocol is found, in which the stream is simply processed with
20 2) A support protocol is not found, but the data is available to be read
21 without processing. In this case, the database is built up through the
22 recursions to return the data, and a RPN event is set that will enable
23 the stream in question to be refreshed if and when the required section
24 extraction protocol is published.This insures the AuthenticationStatus
25 does not become stale in the cache.
27 3) A support protocol is not found, and the data is not available to be read
28 without it. This results in EFI_PROTOCOL_ERROR.
30 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
31 All rights reserved. This program and the accompanying materials
32 are licensed and made available under the terms and conditions of the BSD License
33 which accompanies this distribution. The full text of the license may be found at
34 http://opensource.org/licenses/bsd-license.php
36 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
37 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
41 #include "SectionExtraction.h"
46 LIST_ENTRY mStreamRoot
= INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot
);
48 EFI_HANDLE mSectionExtractionHandle
= NULL
;
50 EFI_SECTION_EXTRACTION_PROTOCOL mSectionExtraction
= {
57 Entry point of the section extraction code. Initializes an instance of the
58 section extraction interface and installs it on a new handle.
60 @param ImageHandle A handle for the image that is initializing this driver
61 @param SystemTable A pointer to the EFI system table
63 @retval EFI_SUCCESS Driver initialized successfully
64 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
69 SectionExtractionEntryPoint (
70 IN EFI_HANDLE ImageHandle
,
71 IN EFI_SYSTEM_TABLE
*SystemTable
77 // Install SEP to a new handle
79 Status
= gBS
->InstallProtocolInterface (
80 &mSectionExtractionHandle
,
81 &gEfiSectionExtractionProtocolGuid
,
85 ASSERT_EFI_ERROR (Status
);
91 SEP member function. This function creates and returns a new section stream
92 handle to represent the new section stream.
94 @param This Indicates the calling context.
95 @param SectionStreamLength Size in bytes of the section stream.
96 @param SectionStream Buffer containing the new section stream.
97 @param SectionStreamHandle A pointer to a caller allocated UINTN that on output
98 contains the new section stream handle.
100 @retval EFI_SUCCESS Section wase opened successfully.
101 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
102 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
109 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
110 IN UINTN SectionStreamLength
,
111 IN VOID
*SectionStream
,
112 OUT UINTN
*SectionStreamHandle
116 // Check to see section stream looks good...
118 if (!IsValidSectionStream (SectionStream
, SectionStreamLength
)) {
119 return EFI_INVALID_PARAMETER
;
122 return OpenSectionStreamEx (
132 SEP member function. Retrieves requested section from section stream.
134 @param This Pointer to SEP instance.
135 @param SectionStreamHandle The section stream from which to extract the requested
137 @param SectionType A pointer to the type of section to search for.
138 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED, then
139 SectionDefinitionGuid indicates which of these types
140 of sections to search for.
141 @param SectionInstance Indicates which instance of the requested section to
143 @param Buffer Double indirection to buffer. If *Buffer is non-null on
144 input, then the buffer is caller allocated. If
145 *Buffer is NULL, then the buffer is callee allocated.
146 In either case, the requried buffer size is returned
148 @param BufferSize On input, indicates the size of *Buffer if *Buffer is
149 non-null on input. On output, indicates the required
150 size (allocated size if callee allocated) of *Buffer.
151 @param AuthenticationStatus Indicates the authentication status of the retrieved
155 @retval EFI_SUCCESS Section was retrieved successfully
156 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the section
157 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
158 bit set, but there was no corresponding GUIDed Section
159 Extraction Protocol in the handle database. *Buffer is
161 @retval EFI_NOT_FOUND An error was encountered when parsing the SectionStream.
162 This indicates the SectionStream is not correctly
164 @retval EFI_NOT_FOUND The requested section does not exist.
165 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process the
167 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.
168 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is
169 insufficient to contain the requested section. The
170 input buffer is filled and contents are section contents
177 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
178 IN UINTN SectionStreamHandle
,
179 IN EFI_SECTION_TYPE
*SectionType
,
180 IN EFI_GUID
*SectionDefinitionGuid
,
181 IN UINTN SectionInstance
,
183 IN OUT UINTN
*BufferSize
,
184 OUT UINT32
*AuthenticationStatus
187 CORE_SECTION_STREAM_NODE
*StreamNode
;
190 CORE_SECTION_CHILD_NODE
*ChildNode
;
191 CORE_SECTION_STREAM_NODE
*ChildStreamNode
;
193 UINT32 ExtractedAuthenticationStatus
;
199 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
200 Instance
= SectionInstance
+ 1;
203 // Locate target stream
205 Status
= FindStreamNode (SectionStreamHandle
, &StreamNode
);
206 if (EFI_ERROR (Status
)) {
207 Status
= EFI_INVALID_PARAMETER
;
208 goto GetSection_Done
;
212 // Found the stream, now locate and return the appropriate section
214 if (SectionType
== NULL
) {
216 // SectionType == NULL means return the WHOLE section stream...
218 CopySize
= StreamNode
->StreamLength
;
219 CopyBuffer
= StreamNode
->StreamBuffer
;
220 *AuthenticationStatus
= StreamNode
->AuthenticationStatus
;
223 // There's a requested section type, so go find it and return it...
225 Status
= FindChildNode (
229 SectionDefinitionGuid
,
232 &ExtractedAuthenticationStatus
234 if (EFI_ERROR (Status
)) {
235 goto GetSection_Done
;
237 CopySize
= ChildNode
->Size
- sizeof (EFI_COMMON_SECTION_HEADER
);
238 CopyBuffer
= ChildStreamNode
->StreamBuffer
+ ChildNode
->OffsetInStream
+ sizeof (EFI_COMMON_SECTION_HEADER
);
239 *AuthenticationStatus
= ExtractedAuthenticationStatus
;
242 SectionSize
= CopySize
;
243 if (*Buffer
!= NULL
) {
245 // Caller allocated buffer. Fill to size and return required size...
247 if (*BufferSize
< CopySize
) {
248 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
249 CopySize
= *BufferSize
;
253 // Callee allocated buffer. Allocate buffer and return size.
255 *Buffer
= AllocatePool (CopySize
);
256 if (*Buffer
== NULL
) {
257 Status
= EFI_OUT_OF_RESOURCES
;
258 goto GetSection_Done
;
261 CopyMem (*Buffer
, CopyBuffer
, CopySize
);
262 *BufferSize
= SectionSize
;
265 gBS
->RestoreTPL (OldTpl
);
270 SEP member function. Deletes an existing section stream
272 @param This Indicates the calling context.
273 @param StreamHandleToClose Indicates the stream to close
275 @retval EFI_SUCCESS Section stream was closed successfully.
276 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
277 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
284 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
285 IN UINTN StreamHandleToClose
288 CORE_SECTION_STREAM_NODE
*StreamNode
;
292 CORE_SECTION_CHILD_NODE
*ChildNode
;
294 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
297 // Locate target stream
299 Status
= FindStreamNode (StreamHandleToClose
, &StreamNode
);
300 if (!EFI_ERROR (Status
)) {
302 // Found the stream, so close it
304 RemoveEntryList (&StreamNode
->Link
);
305 while (!IsListEmpty (&StreamNode
->Children
)) {
306 Link
= GetFirstNode (&StreamNode
->Children
);
307 ChildNode
= CHILD_SECTION_NODE_FROM_LINK (Link
);
308 FreeChildNode (ChildNode
);
310 gBS
->FreePool (StreamNode
->StreamBuffer
);
311 gBS
->FreePool (StreamNode
);
312 Status
= EFI_SUCCESS
;
314 Status
= EFI_INVALID_PARAMETER
;
317 gBS
->RestoreTPL (OldTpl
);
322 Worker function. Determine if the input stream:child matches the input type.
324 @param Stream Indicates the section stream associated with the child
325 @param Child Indicates the child to check
326 @param SearchType Indicates the type of section to check against for
327 @param SectionDefinitionGuid Indicates the GUID to check against if the type is
328 EFI_SECTION_GUID_DEFINED
330 @retval TRUE The child matches
331 @retval FALSE The child doesn't match
336 IN CORE_SECTION_STREAM_NODE
*Stream
,
337 IN CORE_SECTION_CHILD_NODE
*Child
,
338 IN EFI_SECTION_TYPE SearchType
,
339 IN EFI_GUID
*SectionDefinitionGuid
342 EFI_GUID_DEFINED_SECTION
*GuidedSection
;
344 if (SearchType
== EFI_SECTION_ALL
) {
347 if (Child
->Type
!= SearchType
) {
350 if (SearchType
!= EFI_SECTION_GUID_DEFINED
) {
353 GuidedSection
= (EFI_GUID_DEFINED_SECTION
* )(Stream
->StreamBuffer
+ Child
->OffsetInStream
);
354 return CompareGuid (&GuidedSection
->SectionDefinitionGuid
, SectionDefinitionGuid
);
358 Worker function Recursively searches / builds section stream database
359 looking for requested section.
362 @param SourceStream Indicates the section stream in which to do the search.
363 @param SearchType Indicates the type of section to search for.
364 @param SectionInstance Indicates which instance of section to find. This is
365 an in/out parameter to deal with recursions.
366 @param SectionDefinitionGuid Guid of section definition
367 @param FoundChild Output indicating the child node that is found.
368 @param FoundStream Output indicating which section stream the child was
369 found in. If this stream was generated as a result of
370 an encapsulation section, the streamhandle is visible
371 within the SEP driver only.
372 @param AuthenticationStatus Indicates the authentication status of the found section.
374 @retval EFI_SUCCESS Child node was found and returned.
375 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
376 @retval EFI_NOT_FOUND Requested child node does not exist.
377 @retval EFI_PROTOCOL_ERROR A required GUIDED section extraction protocol does not
383 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
384 IN EFI_SECTION_TYPE SearchType
,
385 IN OUT UINTN
*SectionInstance
,
386 IN EFI_GUID
*SectionDefinitionGuid
,
387 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
388 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
389 OUT UINT32
*AuthenticationStatus
392 CORE_SECTION_CHILD_NODE
*CurrentChildNode
;
393 CORE_SECTION_CHILD_NODE
*RecursedChildNode
;
394 CORE_SECTION_STREAM_NODE
*RecursedFoundStream
;
395 UINT32 NextChildOffset
;
396 EFI_STATUS ErrorStatus
;
399 CurrentChildNode
= NULL
;
400 ErrorStatus
= EFI_NOT_FOUND
;
402 if (SourceStream
->StreamLength
== 0) {
403 return EFI_NOT_FOUND
;
406 if (IsListEmpty (&SourceStream
->Children
) &&
407 SourceStream
->StreamLength
> sizeof (EFI_COMMON_SECTION_HEADER
)) {
409 // This occurs when a section stream exists, but no child sections
410 // have been parsed out yet. Therefore, extract the first child and add it
411 // to the list of children so we can get started.
413 Status
= CreateChildNode (SourceStream
, 0, &CurrentChildNode
);
414 if (EFI_ERROR (Status
)) {
420 // At least one child has been parsed out of the section stream. So, walk
421 // through the sections that have already been parsed out looking for the
422 // requested section, if necessary, continue parsing section stream and
423 // adding children until either the requested section is found, or we run
426 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream
->Children
));
429 if (ChildIsType (SourceStream
, CurrentChildNode
, SearchType
, SectionDefinitionGuid
)) {
431 // The type matches, so check the instance count to see if it's the one we want
433 (*SectionInstance
)--;
434 if (*SectionInstance
== 0) {
438 *FoundChild
= CurrentChildNode
;
439 *FoundStream
= SourceStream
;
440 *AuthenticationStatus
= SourceStream
->AuthenticationStatus
;
445 if (CurrentChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
447 // If the current node is an encapsulating node, recurse into it...
449 Status
= FindChildNode (
450 (CORE_SECTION_STREAM_NODE
*)CurrentChildNode
->EncapsulatedStreamHandle
,
453 SectionDefinitionGuid
,
455 &RecursedFoundStream
,
459 // If the status is not EFI_SUCCESS, just save the error code and continue
460 // to find the request child node in the rest stream.
462 if (*SectionInstance
== 0) {
463 ASSERT_EFI_ERROR (Status
);
464 *FoundChild
= RecursedChildNode
;
465 *FoundStream
= RecursedFoundStream
;
468 ErrorStatus
= Status
;
472 if (!IsNodeAtEnd (&SourceStream
->Children
, &CurrentChildNode
->Link
)) {
474 // We haven't found the child node we're interested in yet, but there's
475 // still more nodes that have already been parsed so get the next one
476 // and continue searching..
478 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream
->Children
, &CurrentChildNode
->Link
));
481 // We've exhausted children that have already been parsed, so see if
482 // there's any more data and continue parsing out more children if there
485 NextChildOffset
= CurrentChildNode
->OffsetInStream
+ CurrentChildNode
->Size
;
487 // Round up to 4 byte boundary
489 NextChildOffset
+= 3;
490 NextChildOffset
&= ~(UINTN
)3;
491 if (NextChildOffset
<= SourceStream
->StreamLength
- sizeof (EFI_COMMON_SECTION_HEADER
)) {
493 // There's an unparsed child remaining in the stream, so create a new child node
495 Status
= CreateChildNode (SourceStream
, NextChildOffset
, &CurrentChildNode
);
496 if (EFI_ERROR (Status
)) {
500 ASSERT (EFI_ERROR (ErrorStatus
));
508 Worker function. Constructor for new child nodes.
510 @param Stream Indicates the section stream in which to add the child.
511 @param ChildOffset Indicates the offset in Stream that is the beginning
512 of the child section.
513 @param ChildNode Indicates the Callee allocated and initialized child.
515 @retval EFI_SUCCESS Child node was found and returned.
516 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
517 @retval EFI_PROTOCOL_ERROR Encapsulation sections produce new stream handles when
518 the child node is created. If the section type is GUID
519 defined, and the extraction GUID does not exist, and
520 producing the stream requires the GUID, then a protocol
521 error is generated and no child is produced.
522 Values returned by OpenSectionStreamEx.
527 IN CORE_SECTION_STREAM_NODE
*Stream
,
528 IN UINT32 ChildOffset
,
529 OUT CORE_SECTION_CHILD_NODE
**ChildNode
533 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
534 EFI_COMPRESSION_SECTION
*CompressionHeader
;
535 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
536 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
537 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
538 VOID
*NewStreamBuffer
;
541 UINTN NewStreamBufferSize
;
542 UINT32 AuthenticationStatus
;
543 UINT32 SectionLength
;
545 CORE_SECTION_CHILD_NODE
*Node
;
547 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) (Stream
->StreamBuffer
+ ChildOffset
);
550 // Allocate a new node
552 *ChildNode
= AllocatePool (sizeof (CORE_SECTION_CHILD_NODE
));
555 return EFI_OUT_OF_RESOURCES
;
561 Node
->Signature
= CORE_SECTION_CHILD_SIGNATURE
;
562 Node
->Type
= SectionHeader
->Type
;
563 Node
->Size
= SECTION_SIZE (SectionHeader
);
564 Node
->OffsetInStream
= ChildOffset
;
565 Node
->EncapsulatedStreamHandle
= NULL_STREAM_HANDLE
;
566 Node
->EncapsulationGuid
= NULL
;
569 // If it's an encapsulating section, then create the new section stream also
571 switch (Node
->Type
) {
572 case EFI_SECTION_COMPRESSION
:
574 // Get the CompressionSectionHeader
576 ASSERT (Node
->Size
>= sizeof (EFI_COMPRESSION_SECTION
));
578 CompressionHeader
= (EFI_COMPRESSION_SECTION
*) SectionHeader
;
581 // Allocate space for the new stream
583 if (CompressionHeader
->UncompressedLength
> 0) {
584 NewStreamBufferSize
= CompressionHeader
->UncompressedLength
;
585 NewStreamBuffer
= AllocatePool (NewStreamBufferSize
);
586 if (NewStreamBuffer
== NULL
) {
587 gBS
->FreePool (Node
);
588 return EFI_OUT_OF_RESOURCES
;
591 if (CompressionHeader
->CompressionType
== EFI_NOT_COMPRESSED
) {
593 // stream is not actually compressed, just encapsulated. So just copy it.
595 CopyMem (NewStreamBuffer
, CompressionHeader
+ 1, NewStreamBufferSize
);
596 } else if (CompressionHeader
->CompressionType
== EFI_STANDARD_COMPRESSION
) {
598 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
602 // Decompress the stream
604 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
606 ASSERT_EFI_ERROR (Status
);
608 Status
= Decompress
->GetInfo (
610 CompressionHeader
+ 1,
611 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
612 (UINT32
*)&NewStreamBufferSize
,
615 ASSERT_EFI_ERROR (Status
);
616 ASSERT (NewStreamBufferSize
== CompressionHeader
->UncompressedLength
);
618 ScratchBuffer
= AllocatePool (ScratchSize
);
619 if (ScratchBuffer
== NULL
) {
620 gBS
->FreePool (Node
);
621 gBS
->FreePool (NewStreamBuffer
);
622 return EFI_OUT_OF_RESOURCES
;
625 Status
= Decompress
->Decompress (
627 CompressionHeader
+ 1,
628 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
630 (UINT32
)NewStreamBufferSize
,
634 ASSERT_EFI_ERROR (Status
);
635 gBS
->FreePool (ScratchBuffer
);
638 NewStreamBuffer
= NULL
;
639 NewStreamBufferSize
= 0;
642 Status
= OpenSectionStreamEx (
646 Stream
->AuthenticationStatus
,
647 &Node
->EncapsulatedStreamHandle
649 if (EFI_ERROR (Status
)) {
650 gBS
->FreePool (Node
);
651 gBS
->FreePool (NewStreamBuffer
);
656 case EFI_SECTION_GUID_DEFINED
:
657 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*) SectionHeader
;
658 Node
->EncapsulationGuid
= &GuidedHeader
->SectionDefinitionGuid
;
659 Status
= gBS
->LocateProtocol (Node
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
660 if (!EFI_ERROR (Status
)) {
662 // NewStreamBuffer is always allocated by ExtractSection... No caller
665 Status
= GuidedExtraction
->ExtractSection (
669 &NewStreamBufferSize
,
670 &AuthenticationStatus
672 if (EFI_ERROR (Status
)) {
673 gBS
->FreePool (*ChildNode
);
674 return EFI_PROTOCOL_ERROR
;
678 // Make sure we initialize the new stream with the correct
679 // authentication status for both aggregate and local status fields.
681 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
683 // OR in the parent stream's aggregate status.
685 AuthenticationStatus
|= Stream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
688 // since there's no authentication data contributed by the section,
689 // just inherit the full value from our immediate parent.
691 AuthenticationStatus
= Stream
->AuthenticationStatus
;
694 Status
= OpenSectionStreamEx (
698 AuthenticationStatus
,
699 &Node
->EncapsulatedStreamHandle
701 if (EFI_ERROR (Status
)) {
702 gBS
->FreePool (*ChildNode
);
703 gBS
->FreePool (NewStreamBuffer
);
708 // There's no GUIDed section extraction protocol available.
710 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) {
712 // If the section REQUIRES an extraction protocol, then we're toast
714 gBS
->FreePool (*ChildNode
);
715 return EFI_PROTOCOL_ERROR
;
719 // Figure out the proper authentication status
721 AuthenticationStatus
= Stream
->AuthenticationStatus
;
722 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
724 // The local status of the new stream is contained in
725 // AuthenticaionStatus. This value needs to be ORed into the
726 // Aggregate bits also...
730 // Clear out and initialize the local status
732 AuthenticationStatus
&= ~EFI_LOCAL_AUTH_STATUS_ALL
;
733 AuthenticationStatus
|= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
;
735 // OR local status into aggregate status
737 AuthenticationStatus
|= AuthenticationStatus
>> 16;
740 SectionLength
= SECTION_SIZE (GuidedHeader
);
741 Status
= OpenSectionStreamEx (
742 SectionLength
- GuidedHeader
->DataOffset
,
743 (UINT8
*) GuidedHeader
+ GuidedHeader
->DataOffset
,
745 AuthenticationStatus
,
746 &Node
->EncapsulatedStreamHandle
748 if (EFI_ERROR (Status
)) {
749 gBS
->FreePool (Node
);
754 if ((AuthenticationStatus
& EFI_LOCAL_AUTH_STATUS_ALL
) ==
755 (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
)) {
757 // Need to register for RPN for when the required GUIDed extraction
758 // protocol becomes available. This will enable us to refresh the
759 // AuthenticationStatus cached in the Stream if it's ever requested
762 CreateGuidedExtractionRpnEvent (Stream
, Node
);
770 // Nothing to do if it's a leaf
776 // Last, add the new child node to the stream
778 InsertTailList (&Stream
->Children
, &Node
->Link
);
784 Worker function. Constructor for RPN event if needed to keep AuthenticationStatus
785 cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...
787 @param ParentStream Indicates the parent of the ecnapsulation section (child)
788 @param ChildNode Indicates the child node that is the encapsulation section.
792 CreateGuidedExtractionRpnEvent (
793 IN CORE_SECTION_STREAM_NODE
*ParentStream
,
794 IN CORE_SECTION_CHILD_NODE
*ChildNode
797 RPN_EVENT_CONTEXT
*Context
;
800 // Allocate new event structure and context
802 Context
= AllocatePool (sizeof (RPN_EVENT_CONTEXT
));
803 ASSERT (Context
!= NULL
);
805 Context
->ChildNode
= ChildNode
;
806 Context
->ParentStream
= ParentStream
;
808 Context
->Event
= CoreCreateProtocolNotifyEvent (
809 Context
->ChildNode
->EncapsulationGuid
,
811 NotifyGuidedExtraction
,
813 &Context
->Registration
,
819 RPN callback function. Removes a stale section stream and re-initializes it
820 with an updated AuthenticationStatus.
822 @param Event The event that fired
823 @param RpnContext A pointer to the context that allows us to identify
824 the relevent encapsulation.
829 NotifyGuidedExtraction (
835 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
836 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
837 VOID
*NewStreamBuffer
;
838 UINTN NewStreamBufferSize
;
839 UINT32 AuthenticationStatus
;
840 RPN_EVENT_CONTEXT
*Context
;
842 Context
= RpnContext
;
844 Status
= CloseSectionStream (&mSectionExtraction
, Context
->ChildNode
->EncapsulatedStreamHandle
);
845 if (!EFI_ERROR (Status
)) {
847 // The stream closed successfully, so re-open the stream with correct AuthenticationStatus
850 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*)
851 (Context
->ParentStream
->StreamBuffer
+ Context
->ChildNode
->OffsetInStream
);
852 ASSERT (GuidedHeader
->CommonHeader
.Type
== EFI_SECTION_GUID_DEFINED
);
854 Status
= gBS
->LocateProtocol (Context
->ChildNode
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
855 ASSERT_EFI_ERROR (Status
);
858 Status
= GuidedExtraction
->ExtractSection (
862 &NewStreamBufferSize
,
863 &AuthenticationStatus
865 ASSERT_EFI_ERROR (Status
);
867 // OR in the parent stream's aggregagate status.
869 AuthenticationStatus
|= Context
->ParentStream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
870 Status
= OpenSectionStreamEx (
874 AuthenticationStatus
,
875 &Context
->ChildNode
->EncapsulatedStreamHandle
877 ASSERT_EFI_ERROR (Status
);
881 // If above, the stream did not close successfully, it indicates it's
882 // alread been closed by someone, so just destroy the event and be done with
886 gBS
->CloseEvent (Event
);
887 gBS
->FreePool (Context
);
891 Worker function. Destructor for child nodes.
893 @param ChildNode Indicates the node to destroy
898 IN CORE_SECTION_CHILD_NODE
*ChildNode
901 ASSERT (ChildNode
->Signature
== CORE_SECTION_CHILD_SIGNATURE
);
903 // Remove the child from it's list
905 RemoveEntryList (&ChildNode
->Link
);
907 if (ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
909 // If it's an encapsulating section, we close the resulting section stream.
910 // CloseSectionStream will free all memory associated with the stream.
912 CloseSectionStream (&mSectionExtraction
, ChildNode
->EncapsulatedStreamHandle
);
915 // Last, free the child node itself
917 gBS
->FreePool (ChildNode
);
921 Worker function. Constructor for section streams.
923 @param SectionStreamLength Size in bytes of the section stream.
924 @param SectionStream Buffer containing the new section stream.
925 @param AllocateBuffer Indicates whether the stream buffer is to be copied
926 or the input buffer is to be used in place.
927 @param AuthenticationStatus Indicates the default authentication status for the
929 @param SectionStreamHandle A pointer to a caller allocated section stream handle.
931 @retval EFI_SUCCESS Stream was added to stream database.
932 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
936 OpenSectionStreamEx (
937 IN UINTN SectionStreamLength
,
938 IN VOID
*SectionStream
,
939 IN BOOLEAN AllocateBuffer
,
940 IN UINT32 AuthenticationStatus
,
941 OUT UINTN
*SectionStreamHandle
944 CORE_SECTION_STREAM_NODE
*NewStream
;
948 // Allocate a new stream
950 NewStream
= AllocatePool (sizeof (CORE_SECTION_STREAM_NODE
));
951 if (NewStream
== NULL
) {
952 return EFI_OUT_OF_RESOURCES
;
955 if (AllocateBuffer
) {
957 // if we're here, we're double buffering, allocate the buffer and copy the
960 if (SectionStreamLength
> 0) {
961 NewStream
->StreamBuffer
= AllocatePool (SectionStreamLength
);
962 if (NewStream
->StreamBuffer
== NULL
) {
963 gBS
->FreePool (NewStream
);
964 return EFI_OUT_OF_RESOURCES
;
967 // Copy in stream data
969 CopyMem (NewStream
->StreamBuffer
, SectionStream
, SectionStreamLength
);
972 // It's possible to have a zero length section stream.
974 NewStream
->StreamBuffer
= NULL
;
978 // If were here, the caller has supplied the buffer (it's an internal call)
979 // so just assign the buffer. This happens when we open section streams
980 // as a result of expanding an encapsulating section.
982 NewStream
->StreamBuffer
= SectionStream
;
986 // Initialize the rest of the section stream
988 NewStream
->Signature
= CORE_SECTION_STREAM_SIGNATURE
;
989 NewStream
->StreamHandle
= (UINTN
) NewStream
;
990 NewStream
->StreamLength
= SectionStreamLength
;
991 InitializeListHead (&NewStream
->Children
);
992 NewStream
->AuthenticationStatus
= AuthenticationStatus
;
995 // Add new stream to stream list
997 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
998 InsertTailList (&mStreamRoot
, &NewStream
->Link
);
999 gBS
->RestoreTPL (OldTpl
);
1001 *SectionStreamHandle
= NewStream
->StreamHandle
;
1007 Worker function. Search stream database for requested stream handle.
1009 @param SearchHandle Indicates which stream to look for.
1010 @param FoundStream Output pointer to the found stream.
1012 @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains
1014 @retval EFI_NOT_FOUND SearchHandle was not found in the stream database.
1019 IN UINTN SearchHandle
,
1020 OUT CORE_SECTION_STREAM_NODE
**FoundStream
1023 CORE_SECTION_STREAM_NODE
*StreamNode
;
1025 if (!IsListEmpty (&mStreamRoot
)) {
1026 StreamNode
= STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot
));
1028 if (StreamNode
->StreamHandle
== SearchHandle
) {
1029 *FoundStream
= StreamNode
;
1031 } else if (IsNodeAtEnd (&mStreamRoot
, &StreamNode
->Link
)) {
1034 StreamNode
= STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot
, &StreamNode
->Link
));
1039 return EFI_NOT_FOUND
;
1044 Check if a stream is valid.
1046 @param SectionStream The section stream to be checked
1047 @param SectionStreamLength The length of section stream
1049 @return The validness of a stream.
1053 IsValidSectionStream (
1054 IN VOID
*SectionStream
,
1055 IN UINTN SectionStreamLength
1059 UINTN SectionLength
;
1060 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
1061 EFI_COMMON_SECTION_HEADER
*NextSectionHeader
;
1064 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*)SectionStream
;
1066 while (TotalLength
< SectionStreamLength
) {
1067 SectionLength
= SECTION_SIZE (SectionHeader
);
1068 TotalLength
+= SectionLength
;
1070 if (TotalLength
== SectionStreamLength
) {
1075 // Move to the next byte following the section...
1077 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) SectionHeader
+ SectionLength
);
1080 // Figure out where the next section begins
1082 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) SectionHeader
+ 3);
1083 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) NextSectionHeader
& ~(UINTN
)3);
1084 TotalLength
+= (UINTN
) NextSectionHeader
- (UINTN
) SectionHeader
;
1085 SectionHeader
= NextSectionHeader
;
1093 Create a protocol notification event and return it.
1095 @param ProtocolGuid Protocol to register notification event on.
1096 @param NotifyTpl Maximum TPL to signal the NotifyFunction.
1097 @param NotifyFunction EFI notification routine.
1098 @param NotifyContext Context passed into Event when it is created.
1099 @param Registration Registration key returned from RegisterProtocolNotify().
1100 @param SignalFlag Boolean value to decide whether kick the event after register or not.
1102 @return The EFI_EVENT that has been registered to be signaled when a ProtocolGuid
1103 is added to the system.
1107 CoreCreateProtocolNotifyEvent (
1108 IN EFI_GUID
*ProtocolGuid
,
1109 IN EFI_TPL NotifyTpl
,
1110 IN EFI_EVENT_NOTIFY NotifyFunction
,
1111 IN VOID
*NotifyContext
,
1112 OUT VOID
**Registration
,
1113 IN BOOLEAN SignalFlag
1123 Status
= gBS
->CreateEvent (
1130 ASSERT_EFI_ERROR (Status
);
1133 // Register for protocol notifactions on this event
1136 Status
= gBS
->RegisterProtocolNotify (
1141 ASSERT_EFI_ERROR (Status
);
1145 // Kick the event so we will perform an initial pass of
1146 // current installed drivers
1148 gBS
->SignalEvent (Event
);