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 - 2010, 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 <FrameworkDxe.h>
43 #include <Library/BaseLib.h>
44 #include <Library/DebugLib.h>
45 #include <Library/UefiBootServicesTableLib.h>
46 #include <Library/MemoryAllocationLib.h>
47 #include <Library/BaseMemoryLib.h>
48 #include <Protocol/Decompress.h>
49 #include <Protocol/GuidedSectionExtraction.h>
50 #include <Protocol/SectionExtraction.h>
53 // Local defines and typedefs
55 #define FRAMEWORK_SECTION_CHILD_SIGNATURE SIGNATURE_32('S','X','F','S')
56 #define CHILD_SECTION_NODE_FROM_LINK(Node) \
57 CR (Node, FRAMEWORK_SECTION_CHILD_NODE, Link, FRAMEWORK_SECTION_CHILD_SIGNATURE)
65 // StreamBase + OffsetInStream == pointer to section header in stream. The
66 // stream base is always known when walking the sections within.
68 UINT32 OffsetInStream
;
70 // Then EncapsulatedStreamHandle below is always 0 if the section is NOT an
71 // encapsulating section. Otherwise, it contains the stream handle
72 // of the encapsulated stream. This handle is ALWAYS produced any time an
73 // encapsulating child is encountered, irrespective of whether the
74 // encapsulated stream is processed further.
76 UINTN EncapsulatedStreamHandle
;
77 EFI_GUID
*EncapsulationGuid
;
78 } FRAMEWORK_SECTION_CHILD_NODE
;
80 #define FRAMEWORK_SECTION_STREAM_SIGNATURE SIGNATURE_32('S','X','S','S')
81 #define STREAM_NODE_FROM_LINK(Node) \
82 CR (Node, FRAMEWORK_SECTION_STREAM_NODE, Link, FRAMEWORK_SECTION_STREAM_SIGNATURE)
92 // Authentication status is from GUIDed encapsulations.
94 UINT32 AuthenticationStatus
;
95 } FRAMEWORK_SECTION_STREAM_NODE
;
97 #define NULL_STREAM_HANDLE 0
100 FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
;
101 FRAMEWORK_SECTION_STREAM_NODE
*ParentStream
;
107 SEP member function. This function creates and returns a new section stream
108 handle to represent the new section stream.
110 @param This Indicates the calling context.
111 @param SectionStreamLength Size in bytes of the section stream.
112 @param SectionStream Buffer containing the new section stream.
113 @param SectionStreamHandle A pointer to a caller allocated UINTN that on output
114 contains the new section stream handle.
116 @retval EFI_SUCCESS Section wase opened successfully.
117 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
118 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
125 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
126 IN UINTN SectionStreamLength
,
127 IN VOID
*SectionStream
,
128 OUT UINTN
*SectionStreamHandle
133 SEP member function. Retrieves requested section from section stream.
135 @param This Pointer to SEP instance.
136 @param SectionStreamHandle The section stream from which to extract the requested
138 @param SectionType A pointer to the type of section to search for.
139 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED, then
140 SectionDefinitionGuid indicates which of these types
141 of sections to search for.
142 @param SectionInstance Indicates which instance of the requested section to
144 @param Buffer Double indirection to buffer. If *Buffer is non-null on
145 input, then the buffer is caller allocated. If
146 *Buffer is NULL, then the buffer is callee allocated.
147 In either case, the requried buffer size is returned
149 @param BufferSize On input, indicates the size of *Buffer if *Buffer is
150 non-null on input. On output, indicates the required
151 size (allocated size if callee allocated) of *Buffer.
152 @param AuthenticationStatus Indicates the authentication status of the retrieved
156 @retval EFI_SUCCESS Section was retrieved successfully
157 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the section
158 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
159 bit set, but there was no corresponding GUIDed Section
160 Extraction Protocol in the handle database. *Buffer is
162 @retval EFI_NOT_FOUND An error was encountered when parsing the SectionStream.
163 This indicates the SectionStream is not correctly
165 @retval EFI_NOT_FOUND The requested section does not exist.
166 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process the
168 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.
169 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is
170 insufficient to contain the requested section. The
171 input buffer is filled and contents are section contents
178 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
179 IN UINTN SectionStreamHandle
,
180 IN EFI_SECTION_TYPE
*SectionType
,
181 IN EFI_GUID
*SectionDefinitionGuid
,
182 IN UINTN SectionInstance
,
184 IN OUT UINTN
*BufferSize
,
185 OUT UINT32
*AuthenticationStatus
190 SEP member function. Deletes an existing section stream
192 @param This Indicates the calling context.
193 @param StreamHandleToClose Indicates the stream to close
195 @retval EFI_SUCCESS Section stream was closed successfully.
196 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
197 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
204 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
205 IN UINTN StreamHandleToClose
212 LIST_ENTRY mStreamRoot
= INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot
);
214 EFI_HANDLE mSectionExtractionHandle
= NULL
;
216 EFI_SECTION_EXTRACTION_PROTOCOL mSectionExtraction
= {
223 Entry point of the section extraction code. Initializes an instance of the
224 section extraction interface and installs it on a new handle.
226 @param ImageHandle A handle for the image that is initializing this driver
227 @param SystemTable A pointer to the EFI system table
229 @retval EFI_SUCCESS Driver initialized successfully
230 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
235 SectionExtractionEntryPoint (
236 IN EFI_HANDLE ImageHandle
,
237 IN EFI_SYSTEM_TABLE
*SystemTable
243 // Install SEP to a new handle
245 Status
= gBS
->InstallProtocolInterface (
246 &mSectionExtractionHandle
,
247 &gEfiSectionExtractionProtocolGuid
,
248 EFI_NATIVE_INTERFACE
,
251 ASSERT_EFI_ERROR (Status
);
258 Check if a stream is valid.
260 @param SectionStream The section stream to be checked
261 @param SectionStreamLength The length of section stream
263 @return A boolean value indicating the validness of the section stream.
267 IsValidSectionStream (
268 IN VOID
*SectionStream
,
269 IN UINTN SectionStreamLength
274 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
275 EFI_COMMON_SECTION_HEADER
*NextSectionHeader
;
278 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*)SectionStream
;
280 while (TotalLength
< SectionStreamLength
) {
281 SectionLength
= SECTION_SIZE (SectionHeader
);
282 TotalLength
+= SectionLength
;
284 if (TotalLength
== SectionStreamLength
) {
289 // Move to the next byte following the section...
291 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) SectionHeader
+ SectionLength
);
294 // Figure out where the next section begins
296 NextSectionHeader
= ALIGN_POINTER(SectionHeader
, 4);
297 TotalLength
+= (UINTN
) NextSectionHeader
- (UINTN
) SectionHeader
;
298 SectionHeader
= NextSectionHeader
;
306 Worker function. Constructor for section streams.
308 @param SectionStreamLength Size in bytes of the section stream.
309 @param SectionStream Buffer containing the new section stream.
310 @param AllocateBuffer Indicates whether the stream buffer is to be copied
311 or the input buffer is to be used in place.
312 @param AuthenticationStatus Indicates the default authentication status for the
314 @param SectionStreamHandle A pointer to a caller allocated section stream handle.
316 @retval EFI_SUCCESS Stream was added to stream database.
317 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
321 OpenSectionStreamEx (
322 IN UINTN SectionStreamLength
,
323 IN VOID
*SectionStream
,
324 IN BOOLEAN AllocateBuffer
,
325 IN UINT32 AuthenticationStatus
,
326 OUT UINTN
*SectionStreamHandle
329 FRAMEWORK_SECTION_STREAM_NODE
*NewStream
;
333 // Allocate a new stream
335 NewStream
= AllocatePool (sizeof (FRAMEWORK_SECTION_STREAM_NODE
));
336 if (NewStream
== NULL
) {
337 return EFI_OUT_OF_RESOURCES
;
340 if (AllocateBuffer
) {
342 // if we're here, we're double buffering, allocate the buffer and copy the
345 if (SectionStreamLength
> 0) {
346 NewStream
->StreamBuffer
= AllocatePool (SectionStreamLength
);
347 if (NewStream
->StreamBuffer
== NULL
) {
348 FreePool (NewStream
);
349 return EFI_OUT_OF_RESOURCES
;
352 // Copy in stream data
354 CopyMem (NewStream
->StreamBuffer
, SectionStream
, SectionStreamLength
);
357 // It's possible to have a zero length section stream.
359 NewStream
->StreamBuffer
= NULL
;
363 // If were here, the caller has supplied the buffer (it's an internal call)
364 // so just assign the buffer. This happens when we open section streams
365 // as a result of expanding an encapsulating section.
367 NewStream
->StreamBuffer
= SectionStream
;
371 // Initialize the rest of the section stream
373 NewStream
->Signature
= FRAMEWORK_SECTION_STREAM_SIGNATURE
;
374 NewStream
->StreamHandle
= (UINTN
) NewStream
;
375 NewStream
->StreamLength
= SectionStreamLength
;
376 InitializeListHead (&NewStream
->Children
);
377 NewStream
->AuthenticationStatus
= AuthenticationStatus
;
380 // Add new stream to stream list
382 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
383 InsertTailList (&mStreamRoot
, &NewStream
->Link
);
384 gBS
->RestoreTPL (OldTpl
);
386 *SectionStreamHandle
= NewStream
->StreamHandle
;
392 SEP member function. This function creates and returns a new section stream
393 handle to represent the new section stream.
395 @param This Indicates the calling context.
396 @param SectionStreamLength Size in bytes of the section stream.
397 @param SectionStream Buffer containing the new section stream.
398 @param SectionStreamHandle A pointer to a caller allocated UINTN that on output
399 contains the new section stream handle.
401 @retval EFI_SUCCESS Section wase opened successfully.
402 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
403 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
410 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
411 IN UINTN SectionStreamLength
,
412 IN VOID
*SectionStream
,
413 OUT UINTN
*SectionStreamHandle
417 // Check to see section stream looks good...
419 if (!IsValidSectionStream (SectionStream
, SectionStreamLength
)) {
420 return EFI_INVALID_PARAMETER
;
423 return OpenSectionStreamEx (
433 Worker function. Determine if the input stream:child matches the input type.
435 @param Stream Indicates the section stream associated with the child
436 @param Child Indicates the child to check
437 @param SearchType Indicates the type of section to check against for
438 @param SectionDefinitionGuid Indicates the GUID to check against if the type is
439 EFI_SECTION_GUID_DEFINED
441 @retval TRUE The child matches
442 @retval FALSE The child doesn't match
447 IN FRAMEWORK_SECTION_STREAM_NODE
*Stream
,
448 IN FRAMEWORK_SECTION_CHILD_NODE
*Child
,
449 IN EFI_SECTION_TYPE SearchType
,
450 IN EFI_GUID
*SectionDefinitionGuid
453 EFI_GUID_DEFINED_SECTION
*GuidedSection
;
455 if (SearchType
== EFI_SECTION_ALL
) {
458 if (Child
->Type
!= SearchType
) {
461 if ((SearchType
!= EFI_SECTION_GUID_DEFINED
) || (SectionDefinitionGuid
== NULL
)) {
464 GuidedSection
= (EFI_GUID_DEFINED_SECTION
* )(Stream
->StreamBuffer
+ Child
->OffsetInStream
);
465 return CompareGuid (&GuidedSection
->SectionDefinitionGuid
, SectionDefinitionGuid
);
469 Create a protocol notification event and return it.
471 @param ProtocolGuid Protocol to register notification event on.
472 @param NotifyTpl Maximum TPL to signal the NotifyFunction.
473 @param NotifyFunction EFI notification routine.
474 @param NotifyContext Context passed into Event when it is created.
475 @param Registration Registration key returned from RegisterProtocolNotify().
476 @param SignalFlag Boolean value to decide whether kick the event after register or not.
478 @return The EFI_EVENT that has been registered to be signaled when a ProtocolGuid
479 is added to the system.
483 CreateProtocolNotifyEvent (
484 IN EFI_GUID
*ProtocolGuid
,
485 IN EFI_TPL NotifyTpl
,
486 IN EFI_EVENT_NOTIFY NotifyFunction
,
487 IN VOID
*NotifyContext
,
488 OUT VOID
**Registration
,
489 IN BOOLEAN SignalFlag
499 Status
= gBS
->CreateEvent (
506 ASSERT_EFI_ERROR (Status
);
509 // Register for protocol notifactions on this event
512 Status
= gBS
->RegisterProtocolNotify (
517 ASSERT_EFI_ERROR (Status
);
521 // Kick the event so we will perform an initial pass of
522 // current installed drivers
524 gBS
->SignalEvent (Event
);
531 RPN callback function. Removes a stale section stream and re-initializes it
532 with an updated AuthenticationStatus.
534 @param Event The event that fired
535 @param RpnContext A pointer to the context that allows us to identify
536 the relevent encapsulation.
541 NotifyGuidedExtraction (
547 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
548 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
549 VOID
*NewStreamBuffer
;
550 UINTN NewStreamBufferSize
;
551 UINT32 AuthenticationStatus
;
552 RPN_EVENT_CONTEXT
*Context
;
554 Context
= RpnContext
;
556 Status
= CloseSectionStream (&mSectionExtraction
, Context
->ChildNode
->EncapsulatedStreamHandle
);
557 if (!EFI_ERROR (Status
)) {
559 // The stream closed successfully, so re-open the stream with correct AuthenticationStatus
562 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*)
563 (Context
->ParentStream
->StreamBuffer
+ Context
->ChildNode
->OffsetInStream
);
564 ASSERT (GuidedHeader
->CommonHeader
.Type
== EFI_SECTION_GUID_DEFINED
);
566 Status
= gBS
->LocateProtocol (Context
->ChildNode
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
567 ASSERT_EFI_ERROR (Status
);
570 Status
= GuidedExtraction
->ExtractSection (
574 &NewStreamBufferSize
,
575 &AuthenticationStatus
577 ASSERT_EFI_ERROR (Status
);
579 // OR in the parent stream's aggregagate status.
581 AuthenticationStatus
|= Context
->ParentStream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
582 Status
= OpenSectionStreamEx (
586 AuthenticationStatus
,
587 &Context
->ChildNode
->EncapsulatedStreamHandle
589 ASSERT_EFI_ERROR (Status
);
593 // If above, the stream did not close successfully, it indicates it's
594 // alread been closed by someone, so just destroy the event and be done with
598 gBS
->CloseEvent (Event
);
603 Worker function. Constructor for RPN event if needed to keep AuthenticationStatus
604 cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...
606 @param ParentStream Indicates the parent of the ecnapsulation section (child)
607 @param ChildNode Indicates the child node that is the encapsulation section.
611 CreateGuidedExtractionRpnEvent (
612 IN FRAMEWORK_SECTION_STREAM_NODE
*ParentStream
,
613 IN FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
616 RPN_EVENT_CONTEXT
*Context
;
619 // Allocate new event structure and context
621 Context
= AllocatePool (sizeof (RPN_EVENT_CONTEXT
));
622 ASSERT (Context
!= NULL
);
624 Context
->ChildNode
= ChildNode
;
625 Context
->ParentStream
= ParentStream
;
627 Context
->Event
= CreateProtocolNotifyEvent (
628 Context
->ChildNode
->EncapsulationGuid
,
630 NotifyGuidedExtraction
,
632 &Context
->Registration
,
638 Worker function. Constructor for new child nodes.
640 @param Stream Indicates the section stream in which to add the child.
641 @param ChildOffset Indicates the offset in Stream that is the beginning
642 of the child section.
643 @param ChildNode Indicates the Callee allocated and initialized child.
645 @retval EFI_SUCCESS Child node was found and returned.
646 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
647 @retval EFI_PROTOCOL_ERROR Encapsulation sections produce new stream handles when
648 the child node is created. If the section type is GUID
649 defined, and the extraction GUID does not exist, and
650 producing the stream requires the GUID, then a protocol
651 error is generated and no child is produced.
652 Values returned by OpenSectionStreamEx.
657 IN FRAMEWORK_SECTION_STREAM_NODE
*Stream
,
658 IN UINT32 ChildOffset
,
659 OUT FRAMEWORK_SECTION_CHILD_NODE
**ChildNode
663 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
664 EFI_COMPRESSION_SECTION
*CompressionHeader
;
665 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
666 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
667 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
668 VOID
*NewStreamBuffer
;
671 UINTN NewStreamBufferSize
;
672 UINT32 AuthenticationStatus
;
673 UINT32 SectionLength
;
675 FRAMEWORK_SECTION_CHILD_NODE
*Node
;
677 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) (Stream
->StreamBuffer
+ ChildOffset
);
680 // Allocate a new node
682 *ChildNode
= AllocatePool (sizeof (FRAMEWORK_SECTION_CHILD_NODE
));
685 return EFI_OUT_OF_RESOURCES
;
691 Node
->Signature
= FRAMEWORK_SECTION_CHILD_SIGNATURE
;
692 Node
->Type
= SectionHeader
->Type
;
693 Node
->Size
= SECTION_SIZE (SectionHeader
);
694 Node
->OffsetInStream
= ChildOffset
;
695 Node
->EncapsulatedStreamHandle
= NULL_STREAM_HANDLE
;
696 Node
->EncapsulationGuid
= NULL
;
699 // If it's an encapsulating section, then create the new section stream also
701 switch (Node
->Type
) {
702 case EFI_SECTION_COMPRESSION
:
704 // Get the CompressionSectionHeader
706 ASSERT (Node
->Size
>= sizeof (EFI_COMPRESSION_SECTION
));
708 CompressionHeader
= (EFI_COMPRESSION_SECTION
*) SectionHeader
;
711 // Allocate space for the new stream
713 if (CompressionHeader
->UncompressedLength
> 0) {
714 NewStreamBufferSize
= CompressionHeader
->UncompressedLength
;
715 NewStreamBuffer
= AllocatePool (NewStreamBufferSize
);
716 if (NewStreamBuffer
== NULL
) {
718 return EFI_OUT_OF_RESOURCES
;
721 if (CompressionHeader
->CompressionType
== EFI_NOT_COMPRESSED
) {
723 // stream is not actually compressed, just encapsulated. So just copy it.
725 CopyMem (NewStreamBuffer
, CompressionHeader
+ 1, NewStreamBufferSize
);
726 } else if (CompressionHeader
->CompressionType
== EFI_STANDARD_COMPRESSION
) {
728 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
732 // Decompress the stream
734 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
736 ASSERT_EFI_ERROR (Status
);
738 Status
= Decompress
->GetInfo (
740 CompressionHeader
+ 1,
741 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
742 (UINT32
*)&NewStreamBufferSize
,
745 ASSERT_EFI_ERROR (Status
);
746 ASSERT (NewStreamBufferSize
== CompressionHeader
->UncompressedLength
);
748 ScratchBuffer
= AllocatePool (ScratchSize
);
749 if (ScratchBuffer
== NULL
) {
751 FreePool (NewStreamBuffer
);
752 return EFI_OUT_OF_RESOURCES
;
755 Status
= Decompress
->Decompress (
757 CompressionHeader
+ 1,
758 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
760 (UINT32
)NewStreamBufferSize
,
764 ASSERT_EFI_ERROR (Status
);
765 FreePool (ScratchBuffer
);
768 NewStreamBuffer
= NULL
;
769 NewStreamBufferSize
= 0;
772 Status
= OpenSectionStreamEx (
776 Stream
->AuthenticationStatus
,
777 &Node
->EncapsulatedStreamHandle
779 if (EFI_ERROR (Status
)) {
781 FreePool (NewStreamBuffer
);
786 case EFI_SECTION_GUID_DEFINED
:
787 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*) SectionHeader
;
788 Node
->EncapsulationGuid
= &GuidedHeader
->SectionDefinitionGuid
;
789 Status
= gBS
->LocateProtocol (Node
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
790 if (!EFI_ERROR (Status
)) {
792 // NewStreamBuffer is always allocated by ExtractSection... No caller
795 Status
= GuidedExtraction
->ExtractSection (
799 &NewStreamBufferSize
,
800 &AuthenticationStatus
802 if (EFI_ERROR (Status
)) {
803 FreePool (*ChildNode
);
804 return EFI_PROTOCOL_ERROR
;
808 // Make sure we initialize the new stream with the correct
809 // authentication status for both aggregate and local status fields.
811 if ((GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
813 // OR in the parent stream's aggregate status.
815 AuthenticationStatus
|= Stream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
818 // since there's no authentication data contributed by the section,
819 // just inherit the full value from our immediate parent.
821 AuthenticationStatus
= Stream
->AuthenticationStatus
;
824 Status
= OpenSectionStreamEx (
828 AuthenticationStatus
,
829 &Node
->EncapsulatedStreamHandle
831 if (EFI_ERROR (Status
)) {
832 FreePool (*ChildNode
);
833 FreePool (NewStreamBuffer
);
838 // There's no GUIDed section extraction protocol available.
840 if ((GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) == EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) {
842 // If the section REQUIRES an extraction protocol, then we're toast
844 FreePool (*ChildNode
);
845 return EFI_PROTOCOL_ERROR
;
849 // Figure out the proper authentication status
851 AuthenticationStatus
= Stream
->AuthenticationStatus
;
852 if ((GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
854 // The local status of the new stream is contained in
855 // AuthenticaionStatus. This value needs to be ORed into the
856 // Aggregate bits also...
860 // Clear out and initialize the local status
862 AuthenticationStatus
&= ~EFI_LOCAL_AUTH_STATUS_ALL
;
863 AuthenticationStatus
|= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
;
865 // OR local status into aggregate status
867 AuthenticationStatus
|= AuthenticationStatus
>> 16;
870 SectionLength
= SECTION_SIZE (GuidedHeader
);
871 Status
= OpenSectionStreamEx (
872 SectionLength
- GuidedHeader
->DataOffset
,
873 (UINT8
*) GuidedHeader
+ GuidedHeader
->DataOffset
,
875 AuthenticationStatus
,
876 &Node
->EncapsulatedStreamHandle
878 if (EFI_ERROR (Status
)) {
884 if ((AuthenticationStatus
& EFI_LOCAL_AUTH_STATUS_ALL
) ==
885 (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
)) {
887 // Need to register for RPN for when the required GUIDed extraction
888 // protocol becomes available. This will enable us to refresh the
889 // AuthenticationStatus cached in the Stream if it's ever requested
892 CreateGuidedExtractionRpnEvent (Stream
, Node
);
900 // Nothing to do if it's a leaf
906 // Last, add the new child node to the stream
908 InsertTailList (&Stream
->Children
, &Node
->Link
);
914 Worker function Recursively searches / builds section stream database
915 looking for requested section.
918 @param SourceStream Indicates the section stream in which to do the search.
919 @param SearchType Indicates the type of section to search for.
920 @param SectionInstance Indicates which instance of section to find. This is
921 an in/out parameter to deal with recursions.
922 @param SectionDefinitionGuid Guid of section definition
923 @param FoundChild Output indicating the child node that is found.
924 @param FoundStream Output indicating which section stream the child was
925 found in. If this stream was generated as a result of
926 an encapsulation section, the streamhandle is visible
927 within the SEP driver only.
928 @param AuthenticationStatus Indicates the authentication status of the found section.
930 @retval EFI_SUCCESS Child node was found and returned.
931 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
932 @retval EFI_NOT_FOUND Requested child node does not exist.
933 @retval EFI_PROTOCOL_ERROR A required GUIDED section extraction protocol does not
939 IN FRAMEWORK_SECTION_STREAM_NODE
*SourceStream
,
940 IN EFI_SECTION_TYPE SearchType
,
941 IN OUT UINTN
*SectionInstance
,
942 IN EFI_GUID
*SectionDefinitionGuid
,
943 OUT FRAMEWORK_SECTION_CHILD_NODE
**FoundChild
,
944 OUT FRAMEWORK_SECTION_STREAM_NODE
**FoundStream
,
945 OUT UINT32
*AuthenticationStatus
948 FRAMEWORK_SECTION_CHILD_NODE
*CurrentChildNode
;
949 FRAMEWORK_SECTION_CHILD_NODE
*RecursedChildNode
;
950 FRAMEWORK_SECTION_STREAM_NODE
*RecursedFoundStream
;
951 UINT32 NextChildOffset
;
952 EFI_STATUS ErrorStatus
;
955 CurrentChildNode
= NULL
;
956 ErrorStatus
= EFI_NOT_FOUND
;
958 if (SourceStream
->StreamLength
== 0) {
959 return EFI_NOT_FOUND
;
962 if (IsListEmpty (&SourceStream
->Children
) &&
963 SourceStream
->StreamLength
>= sizeof (EFI_COMMON_SECTION_HEADER
)) {
965 // This occurs when a section stream exists, but no child sections
966 // have been parsed out yet. Therefore, extract the first child and add it
967 // to the list of children so we can get started.
968 // Section stream may contain an array of zero or more bytes.
969 // So, its size should be >= the size of commen section header.
971 Status
= CreateChildNode (SourceStream
, 0, &CurrentChildNode
);
972 if (EFI_ERROR (Status
)) {
978 // At least one child has been parsed out of the section stream. So, walk
979 // through the sections that have already been parsed out looking for the
980 // requested section, if necessary, continue parsing section stream and
981 // adding children until either the requested section is found, or we run
984 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream
->Children
));
987 if (ChildIsType (SourceStream
, CurrentChildNode
, SearchType
, SectionDefinitionGuid
)) {
989 // The type matches, so check the instance count to see if it's the one we want
991 (*SectionInstance
)--;
992 if (*SectionInstance
== 0) {
996 *FoundChild
= CurrentChildNode
;
997 *FoundStream
= SourceStream
;
998 *AuthenticationStatus
= SourceStream
->AuthenticationStatus
;
1003 ASSERT (CurrentChildNode
!= NULL
);
1004 if (CurrentChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1006 // If the current node is an encapsulating node, recurse into it...
1008 Status
= FindChildNode (
1009 (FRAMEWORK_SECTION_STREAM_NODE
*)CurrentChildNode
->EncapsulatedStreamHandle
,
1012 SectionDefinitionGuid
,
1014 &RecursedFoundStream
,
1015 AuthenticationStatus
1018 // If the status is not EFI_SUCCESS, just save the error code and continue
1019 // to find the request child node in the rest stream.
1021 if (*SectionInstance
== 0) {
1022 ASSERT_EFI_ERROR (Status
);
1023 *FoundChild
= RecursedChildNode
;
1024 *FoundStream
= RecursedFoundStream
;
1027 ErrorStatus
= Status
;
1031 if (!IsNodeAtEnd (&SourceStream
->Children
, &CurrentChildNode
->Link
)) {
1033 // We haven't found the child node we're interested in yet, but there's
1034 // still more nodes that have already been parsed so get the next one
1035 // and continue searching..
1037 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream
->Children
, &CurrentChildNode
->Link
));
1040 // We've exhausted children that have already been parsed, so see if
1041 // there's any more data and continue parsing out more children if there
1044 NextChildOffset
= CurrentChildNode
->OffsetInStream
+ CurrentChildNode
->Size
;
1046 // Round up to 4 byte boundary
1048 NextChildOffset
+= 3;
1049 NextChildOffset
&= ~(UINTN
)3;
1050 if (NextChildOffset
<= SourceStream
->StreamLength
- sizeof (EFI_COMMON_SECTION_HEADER
)) {
1052 // There's an unparsed child remaining in the stream, so create a new child node
1054 Status
= CreateChildNode (SourceStream
, NextChildOffset
, &CurrentChildNode
);
1055 if (EFI_ERROR (Status
)) {
1059 ASSERT (EFI_ERROR (ErrorStatus
));
1067 Worker function. Search stream database for requested stream handle.
1069 @param SearchHandle Indicates which stream to look for.
1070 @param FoundStream Output pointer to the found stream.
1072 @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains
1074 @retval EFI_NOT_FOUND SearchHandle was not found in the stream database.
1079 IN UINTN SearchHandle
,
1080 OUT FRAMEWORK_SECTION_STREAM_NODE
**FoundStream
1083 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1085 if (!IsListEmpty (&mStreamRoot
)) {
1086 StreamNode
= STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot
));
1088 if (StreamNode
->StreamHandle
== SearchHandle
) {
1089 *FoundStream
= StreamNode
;
1091 } else if (IsNodeAtEnd (&mStreamRoot
, &StreamNode
->Link
)) {
1094 StreamNode
= STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot
, &StreamNode
->Link
));
1099 return EFI_NOT_FOUND
;
1103 SEP member function. Retrieves requested section from section stream.
1105 @param This Pointer to SEP instance.
1106 @param SectionStreamHandle The section stream from which to extract the requested
1108 @param SectionType A pointer to the type of section to search for.
1109 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED, then
1110 SectionDefinitionGuid indicates which of these types
1111 of sections to search for.
1112 @param SectionInstance Indicates which instance of the requested section to
1114 @param Buffer Double indirection to buffer. If *Buffer is non-null on
1115 input, then the buffer is caller allocated. If
1116 *Buffer is NULL, then the buffer is callee allocated.
1117 In either case, the requried buffer size is returned
1119 @param BufferSize On input, indicates the size of *Buffer if *Buffer is
1120 non-null on input. On output, indicates the required
1121 size (allocated size if callee allocated) of *Buffer.
1122 @param AuthenticationStatus Indicates the authentication status of the retrieved
1126 @retval EFI_SUCCESS Section was retrieved successfully
1127 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the section
1128 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
1129 bit set, but there was no corresponding GUIDed Section
1130 Extraction Protocol in the handle database. *Buffer is
1132 @retval EFI_NOT_FOUND An error was encountered when parsing the SectionStream.
1133 This indicates the SectionStream is not correctly
1135 @retval EFI_NOT_FOUND The requested section does not exist.
1136 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process the
1138 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.
1139 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is
1140 insufficient to contain the requested section. The
1141 input buffer is filled and contents are section contents
1148 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
1149 IN UINTN SectionStreamHandle
,
1150 IN EFI_SECTION_TYPE
*SectionType
,
1151 IN EFI_GUID
*SectionDefinitionGuid
,
1152 IN UINTN SectionInstance
,
1154 IN OUT UINTN
*BufferSize
,
1155 OUT UINT32
*AuthenticationStatus
1158 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1161 FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
;
1162 FRAMEWORK_SECTION_STREAM_NODE
*ChildStreamNode
;
1164 UINT32 ExtractedAuthenticationStatus
;
1170 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1171 Instance
= SectionInstance
+ 1;
1174 // Locate target stream
1176 Status
= FindStreamNode (SectionStreamHandle
, &StreamNode
);
1177 if (EFI_ERROR (Status
)) {
1178 Status
= EFI_INVALID_PARAMETER
;
1179 goto GetSection_Done
;
1183 // Found the stream, now locate and return the appropriate section
1185 if (SectionType
== NULL
) {
1187 // SectionType == NULL means return the WHOLE section stream...
1189 CopySize
= StreamNode
->StreamLength
;
1190 CopyBuffer
= StreamNode
->StreamBuffer
;
1191 *AuthenticationStatus
= StreamNode
->AuthenticationStatus
;
1194 // There's a requested section type, so go find it and return it...
1196 Status
= FindChildNode (
1200 SectionDefinitionGuid
,
1203 &ExtractedAuthenticationStatus
1205 if (EFI_ERROR (Status
)) {
1206 goto GetSection_Done
;
1208 ASSERT (ChildNode
!= NULL
);
1209 ASSERT (ChildStreamNode
!= NULL
);
1210 CopySize
= ChildNode
->Size
- sizeof (EFI_COMMON_SECTION_HEADER
);
1211 CopyBuffer
= ChildStreamNode
->StreamBuffer
+ ChildNode
->OffsetInStream
+ sizeof (EFI_COMMON_SECTION_HEADER
);
1212 *AuthenticationStatus
= ExtractedAuthenticationStatus
;
1215 SectionSize
= CopySize
;
1216 if (*Buffer
!= NULL
) {
1218 // Caller allocated buffer. Fill to size and return required size...
1220 if (*BufferSize
< CopySize
) {
1221 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
1222 CopySize
= *BufferSize
;
1226 // Callee allocated buffer. Allocate buffer and return size.
1228 *Buffer
= AllocatePool (CopySize
);
1229 if (*Buffer
== NULL
) {
1230 Status
= EFI_OUT_OF_RESOURCES
;
1231 goto GetSection_Done
;
1234 CopyMem (*Buffer
, CopyBuffer
, CopySize
);
1235 *BufferSize
= SectionSize
;
1238 gBS
->RestoreTPL (OldTpl
);
1243 Worker function. Destructor for child nodes.
1245 @param ChildNode Indicates the node to destroy
1250 IN FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
1253 ASSERT (ChildNode
->Signature
== FRAMEWORK_SECTION_CHILD_SIGNATURE
);
1255 // Remove the child from it's list
1257 RemoveEntryList (&ChildNode
->Link
);
1259 if (ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1261 // If it's an encapsulating section, we close the resulting section stream.
1262 // CloseSectionStream will free all memory associated with the stream.
1264 CloseSectionStream (&mSectionExtraction
, ChildNode
->EncapsulatedStreamHandle
);
1267 // Last, free the child node itself
1269 FreePool (ChildNode
);
1273 SEP member function. Deletes an existing section stream
1275 @param This Indicates the calling context.
1276 @param StreamHandleToClose Indicates the stream to close
1278 @retval EFI_SUCCESS Section stream was closed successfully.
1279 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
1280 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
1286 CloseSectionStream (
1287 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
1288 IN UINTN StreamHandleToClose
1291 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1295 FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
;
1297 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1300 // Locate target stream
1302 Status
= FindStreamNode (StreamHandleToClose
, &StreamNode
);
1303 if (!EFI_ERROR (Status
)) {
1305 // Found the stream, so close it
1307 RemoveEntryList (&StreamNode
->Link
);
1308 while (!IsListEmpty (&StreamNode
->Children
)) {
1309 Link
= GetFirstNode (&StreamNode
->Children
);
1310 ChildNode
= CHILD_SECTION_NODE_FROM_LINK (Link
);
1311 FreeChildNode (ChildNode
);
1313 FreePool (StreamNode
->StreamBuffer
);
1314 FreePool (StreamNode
);
1315 Status
= EFI_SUCCESS
;
1317 Status
= EFI_INVALID_PARAMETER
;
1320 gBS
->RestoreTPL (OldTpl
);