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 - 2011, Intel Corporation. All rights reserved.<BR>
31 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 if (IS_SECTION2 (SectionHeader
)) {
282 SectionLength
= SECTION2_SIZE (SectionHeader
);
284 SectionLength
= SECTION_SIZE (SectionHeader
);
286 TotalLength
+= SectionLength
;
288 if (TotalLength
== SectionStreamLength
) {
293 // Move to the next byte following the section...
295 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) SectionHeader
+ SectionLength
);
298 // Figure out where the next section begins
300 NextSectionHeader
= ALIGN_POINTER(SectionHeader
, 4);
301 TotalLength
+= (UINTN
) NextSectionHeader
- (UINTN
) SectionHeader
;
302 SectionHeader
= NextSectionHeader
;
310 Worker function. Constructor for section streams.
312 @param SectionStreamLength Size in bytes of the section stream.
313 @param SectionStream Buffer containing the new section stream.
314 @param AllocateBuffer Indicates whether the stream buffer is to be copied
315 or the input buffer is to be used in place.
316 @param AuthenticationStatus Indicates the default authentication status for the
318 @param SectionStreamHandle A pointer to a caller allocated section stream handle.
320 @retval EFI_SUCCESS Stream was added to stream database.
321 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
325 OpenSectionStreamEx (
326 IN UINTN SectionStreamLength
,
327 IN VOID
*SectionStream
,
328 IN BOOLEAN AllocateBuffer
,
329 IN UINT32 AuthenticationStatus
,
330 OUT UINTN
*SectionStreamHandle
333 FRAMEWORK_SECTION_STREAM_NODE
*NewStream
;
337 // Allocate a new stream
339 NewStream
= AllocatePool (sizeof (FRAMEWORK_SECTION_STREAM_NODE
));
340 if (NewStream
== NULL
) {
341 return EFI_OUT_OF_RESOURCES
;
344 if (AllocateBuffer
) {
346 // if we're here, we're double buffering, allocate the buffer and copy the
349 if (SectionStreamLength
> 0) {
350 NewStream
->StreamBuffer
= AllocatePool (SectionStreamLength
);
351 if (NewStream
->StreamBuffer
== NULL
) {
352 FreePool (NewStream
);
353 return EFI_OUT_OF_RESOURCES
;
356 // Copy in stream data
358 CopyMem (NewStream
->StreamBuffer
, SectionStream
, SectionStreamLength
);
361 // It's possible to have a zero length section stream.
363 NewStream
->StreamBuffer
= NULL
;
367 // If were here, the caller has supplied the buffer (it's an internal call)
368 // so just assign the buffer. This happens when we open section streams
369 // as a result of expanding an encapsulating section.
371 NewStream
->StreamBuffer
= SectionStream
;
375 // Initialize the rest of the section stream
377 NewStream
->Signature
= FRAMEWORK_SECTION_STREAM_SIGNATURE
;
378 NewStream
->StreamHandle
= (UINTN
) NewStream
;
379 NewStream
->StreamLength
= SectionStreamLength
;
380 InitializeListHead (&NewStream
->Children
);
381 NewStream
->AuthenticationStatus
= AuthenticationStatus
;
384 // Add new stream to stream list
386 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
387 InsertTailList (&mStreamRoot
, &NewStream
->Link
);
388 gBS
->RestoreTPL (OldTpl
);
390 *SectionStreamHandle
= NewStream
->StreamHandle
;
396 SEP member function. This function creates and returns a new section stream
397 handle to represent the new section stream.
399 @param This Indicates the calling context.
400 @param SectionStreamLength Size in bytes of the section stream.
401 @param SectionStream Buffer containing the new section stream.
402 @param SectionStreamHandle A pointer to a caller allocated UINTN that on output
403 contains the new section stream handle.
405 @retval EFI_SUCCESS Section wase opened successfully.
406 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
407 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
414 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
415 IN UINTN SectionStreamLength
,
416 IN VOID
*SectionStream
,
417 OUT UINTN
*SectionStreamHandle
421 // Check to see section stream looks good...
423 if (!IsValidSectionStream (SectionStream
, SectionStreamLength
)) {
424 return EFI_INVALID_PARAMETER
;
427 return OpenSectionStreamEx (
437 Worker function. Determine if the input stream:child matches the input type.
439 @param Stream Indicates the section stream associated with the child
440 @param Child Indicates the child to check
441 @param SearchType Indicates the type of section to check against for
442 @param SectionDefinitionGuid Indicates the GUID to check against if the type is
443 EFI_SECTION_GUID_DEFINED
445 @retval TRUE The child matches
446 @retval FALSE The child doesn't match
451 IN FRAMEWORK_SECTION_STREAM_NODE
*Stream
,
452 IN FRAMEWORK_SECTION_CHILD_NODE
*Child
,
453 IN EFI_SECTION_TYPE SearchType
,
454 IN EFI_GUID
*SectionDefinitionGuid
457 EFI_GUID_DEFINED_SECTION
*GuidedSection
;
459 if (SearchType
== EFI_SECTION_ALL
) {
462 if (Child
->Type
!= SearchType
) {
465 if ((SearchType
!= EFI_SECTION_GUID_DEFINED
) || (SectionDefinitionGuid
== NULL
)) {
468 GuidedSection
= (EFI_GUID_DEFINED_SECTION
* )(Stream
->StreamBuffer
+ Child
->OffsetInStream
);
469 if (IS_SECTION2 (GuidedSection
)) {
470 return CompareGuid (&(((EFI_GUID_DEFINED_SECTION2
*) GuidedSection
)->SectionDefinitionGuid
), SectionDefinitionGuid
);
472 return CompareGuid (&GuidedSection
->SectionDefinitionGuid
, SectionDefinitionGuid
);
477 Create a protocol notification event and return it.
479 @param ProtocolGuid Protocol to register notification event on.
480 @param NotifyTpl Maximum TPL to signal the NotifyFunction.
481 @param NotifyFunction EFI notification routine.
482 @param NotifyContext Context passed into Event when it is created.
483 @param Registration Registration key returned from RegisterProtocolNotify().
484 @param SignalFlag Boolean value to decide whether kick the event after register or not.
486 @return The EFI_EVENT that has been registered to be signaled when a ProtocolGuid
487 is added to the system.
491 CreateProtocolNotifyEvent (
492 IN EFI_GUID
*ProtocolGuid
,
493 IN EFI_TPL NotifyTpl
,
494 IN EFI_EVENT_NOTIFY NotifyFunction
,
495 IN VOID
*NotifyContext
,
496 OUT VOID
**Registration
,
497 IN BOOLEAN SignalFlag
507 Status
= gBS
->CreateEvent (
514 ASSERT_EFI_ERROR (Status
);
517 // Register for protocol notifactions on this event
520 Status
= gBS
->RegisterProtocolNotify (
525 ASSERT_EFI_ERROR (Status
);
529 // Kick the event so we will perform an initial pass of
530 // current installed drivers
532 gBS
->SignalEvent (Event
);
539 RPN callback function.
540 1. Initialize the section stream when the GUIDED_SECTION_EXTRACTION_PROTOCOL is installed.
541 2. Removes a stale section stream and re-initializes it with an updated AuthenticationStatus.
543 @param Event The event that fired
544 @param RpnContext A pointer to the context that allows us to identify
545 the relevent encapsulation.
550 NotifyGuidedExtraction (
556 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
557 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
558 VOID
*NewStreamBuffer
;
559 UINTN NewStreamBufferSize
;
560 UINT32 AuthenticationStatus
;
561 RPN_EVENT_CONTEXT
*Context
;
563 Context
= RpnContext
;
564 Status
= EFI_SUCCESS
;
565 if (Context
->ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
566 Status
= CloseSectionStream (&mSectionExtraction
, Context
->ChildNode
->EncapsulatedStreamHandle
);
568 if (!EFI_ERROR (Status
)) {
570 // The stream is not initialized, open it.
571 // Or the stream closed successfully, so re-open the stream with correct AuthenticationStatus.
574 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*)
575 (Context
->ParentStream
->StreamBuffer
+ Context
->ChildNode
->OffsetInStream
);
576 ASSERT (GuidedHeader
->CommonHeader
.Type
== EFI_SECTION_GUID_DEFINED
);
578 Status
= gBS
->LocateProtocol (Context
->ChildNode
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
579 ASSERT_EFI_ERROR (Status
);
582 Status
= GuidedExtraction
->ExtractSection (
586 &NewStreamBufferSize
,
587 &AuthenticationStatus
589 ASSERT_EFI_ERROR (Status
);
591 // OR in the parent stream's aggregate status.
593 AuthenticationStatus
|= Context
->ParentStream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
594 Status
= OpenSectionStreamEx (
598 AuthenticationStatus
,
599 &Context
->ChildNode
->EncapsulatedStreamHandle
601 ASSERT_EFI_ERROR (Status
);
605 // If above, the stream did not close successfully, it indicates it's
606 // alread been closed by someone, so just destroy the event and be done with
610 gBS
->CloseEvent (Event
);
615 Worker function. Constructor for RPN event if needed to keep AuthenticationStatus
616 cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...
618 @param ParentStream Indicates the parent of the ecnapsulation section (child)
619 @param ChildNode Indicates the child node that is the encapsulation section.
623 CreateGuidedExtractionRpnEvent (
624 IN FRAMEWORK_SECTION_STREAM_NODE
*ParentStream
,
625 IN FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
628 RPN_EVENT_CONTEXT
*Context
;
631 // Allocate new event structure and context
633 Context
= AllocatePool (sizeof (RPN_EVENT_CONTEXT
));
634 ASSERT (Context
!= NULL
);
636 Context
->ChildNode
= ChildNode
;
637 Context
->ParentStream
= ParentStream
;
639 Context
->Event
= CreateProtocolNotifyEvent (
640 Context
->ChildNode
->EncapsulationGuid
,
642 NotifyGuidedExtraction
,
644 &Context
->Registration
,
650 Worker function. Constructor for new child nodes.
652 @param Stream Indicates the section stream in which to add the child.
653 @param ChildOffset Indicates the offset in Stream that is the beginning
654 of the child section.
655 @param ChildNode Indicates the Callee allocated and initialized child.
657 @retval EFI_SUCCESS Child node was found and returned.
658 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
659 @retval EFI_PROTOCOL_ERROR Encapsulation sections produce new stream handles when
660 the child node is created. If the section type is GUID
661 defined, and the extraction GUID does not exist, and
662 producing the stream requires the GUID, then a protocol
663 error is generated and no child is produced.
664 Values returned by OpenSectionStreamEx.
669 IN FRAMEWORK_SECTION_STREAM_NODE
*Stream
,
670 IN UINT32 ChildOffset
,
671 OUT FRAMEWORK_SECTION_CHILD_NODE
**ChildNode
675 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
676 EFI_COMPRESSION_SECTION
*CompressionHeader
;
677 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
678 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
679 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
680 VOID
*NewStreamBuffer
;
683 UINTN NewStreamBufferSize
;
684 UINT32 AuthenticationStatus
;
685 VOID
*CompressionSource
;
686 UINT32 CompressionSourceSize
;
687 UINT32 UncompressedLength
;
688 UINT8 CompressionType
;
689 UINT16 GuidedSectionAttributes
;
691 FRAMEWORK_SECTION_CHILD_NODE
*Node
;
693 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) (Stream
->StreamBuffer
+ ChildOffset
);
696 // Allocate a new node
698 *ChildNode
= AllocatePool (sizeof (FRAMEWORK_SECTION_CHILD_NODE
));
701 return EFI_OUT_OF_RESOURCES
;
707 Node
->Signature
= FRAMEWORK_SECTION_CHILD_SIGNATURE
;
708 Node
->Type
= SectionHeader
->Type
;
709 if (IS_SECTION2 (SectionHeader
)) {
710 Node
->Size
= SECTION2_SIZE (SectionHeader
);
712 Node
->Size
= SECTION_SIZE (SectionHeader
);
714 Node
->OffsetInStream
= ChildOffset
;
715 Node
->EncapsulatedStreamHandle
= NULL_STREAM_HANDLE
;
716 Node
->EncapsulationGuid
= NULL
;
719 // If it's an encapsulating section, then create the new section stream also
721 switch (Node
->Type
) {
722 case EFI_SECTION_COMPRESSION
:
724 // Get the CompressionSectionHeader
726 ASSERT (Node
->Size
>= sizeof (EFI_COMPRESSION_SECTION
));
728 CompressionHeader
= (EFI_COMPRESSION_SECTION
*) SectionHeader
;
730 if (IS_SECTION2 (CompressionHeader
)) {
731 CompressionSource
= (VOID
*) ((UINT8
*) CompressionHeader
+ sizeof (EFI_COMPRESSION_SECTION2
));
732 CompressionSourceSize
= (UINT32
) (SECTION2_SIZE (CompressionHeader
) - sizeof (EFI_COMPRESSION_SECTION2
));
733 UncompressedLength
= ((EFI_COMPRESSION_SECTION2
*) CompressionHeader
)->UncompressedLength
;
734 CompressionType
= ((EFI_COMPRESSION_SECTION2
*) CompressionHeader
)->CompressionType
;
736 CompressionSource
= (VOID
*) ((UINT8
*) CompressionHeader
+ sizeof (EFI_COMPRESSION_SECTION
));
737 CompressionSourceSize
= (UINT32
) (SECTION_SIZE (CompressionHeader
) - sizeof (EFI_COMPRESSION_SECTION
));
738 UncompressedLength
= CompressionHeader
->UncompressedLength
;
739 CompressionType
= CompressionHeader
->CompressionType
;
743 // Allocate space for the new stream
745 if (UncompressedLength
> 0) {
746 NewStreamBufferSize
= UncompressedLength
;
747 NewStreamBuffer
= AllocatePool (NewStreamBufferSize
);
748 if (NewStreamBuffer
== NULL
) {
750 return EFI_OUT_OF_RESOURCES
;
753 if (CompressionType
== EFI_NOT_COMPRESSED
) {
755 // stream is not actually compressed, just encapsulated. So just copy it.
757 CopyMem (NewStreamBuffer
, CompressionSource
, NewStreamBufferSize
);
758 } else if (CompressionType
== EFI_STANDARD_COMPRESSION
) {
760 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
764 // Decompress the stream
766 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
768 ASSERT_EFI_ERROR (Status
);
770 Status
= Decompress
->GetInfo (
773 CompressionSourceSize
,
774 (UINT32
*)&NewStreamBufferSize
,
777 ASSERT_EFI_ERROR (Status
);
778 ASSERT (NewStreamBufferSize
== UncompressedLength
);
780 ScratchBuffer
= AllocatePool (ScratchSize
);
781 if (ScratchBuffer
== NULL
) {
783 FreePool (NewStreamBuffer
);
784 return EFI_OUT_OF_RESOURCES
;
787 Status
= Decompress
->Decompress (
790 CompressionSourceSize
,
792 (UINT32
)NewStreamBufferSize
,
796 ASSERT_EFI_ERROR (Status
);
797 FreePool (ScratchBuffer
);
800 NewStreamBuffer
= NULL
;
801 NewStreamBufferSize
= 0;
804 Status
= OpenSectionStreamEx (
808 Stream
->AuthenticationStatus
,
809 &Node
->EncapsulatedStreamHandle
811 if (EFI_ERROR (Status
)) {
813 FreePool (NewStreamBuffer
);
818 case EFI_SECTION_GUID_DEFINED
:
819 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*) SectionHeader
;
820 if (IS_SECTION2 (GuidedHeader
)) {
821 Node
->EncapsulationGuid
= &(((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->SectionDefinitionGuid
);
822 GuidedSectionAttributes
= ((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->Attributes
;
824 Node
->EncapsulationGuid
= &GuidedHeader
->SectionDefinitionGuid
;
825 GuidedSectionAttributes
= GuidedHeader
->Attributes
;
827 Status
= gBS
->LocateProtocol (Node
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
828 if (!EFI_ERROR (Status
)) {
830 // NewStreamBuffer is always allocated by ExtractSection... No caller
833 Status
= GuidedExtraction
->ExtractSection (
837 &NewStreamBufferSize
,
838 &AuthenticationStatus
840 if (EFI_ERROR (Status
)) {
841 FreePool (*ChildNode
);
842 return EFI_PROTOCOL_ERROR
;
846 // Make sure we initialize the new stream with the correct
847 // authentication status for both aggregate and local status fields.
849 if ((GuidedSectionAttributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
851 // OR in the parent stream's aggregate status.
853 AuthenticationStatus
|= Stream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
856 // since there's no authentication data contributed by the section,
857 // just inherit the full value from our immediate parent.
859 AuthenticationStatus
= Stream
->AuthenticationStatus
;
862 Status
= OpenSectionStreamEx (
866 AuthenticationStatus
,
867 &Node
->EncapsulatedStreamHandle
869 if (EFI_ERROR (Status
)) {
870 FreePool (*ChildNode
);
871 FreePool (NewStreamBuffer
);
876 // There's no GUIDed section extraction protocol available.
878 if ((GuidedSectionAttributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) == EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) {
880 // If the section REQUIRES an extraction protocol, register for RPN
881 // when the required GUIDed extraction protocol becomes available.
883 AuthenticationStatus
= 0;
884 CreateGuidedExtractionRpnEvent (Stream
, Node
);
887 // Figure out the proper authentication status
889 AuthenticationStatus
= Stream
->AuthenticationStatus
;
890 if ((GuidedSectionAttributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
892 // The local status of the new stream is contained in
893 // AuthenticaionStatus. This value needs to be ORed into the
894 // Aggregate bits also...
898 // Clear out and initialize the local status
900 AuthenticationStatus
&= ~EFI_LOCAL_AUTH_STATUS_ALL
;
901 AuthenticationStatus
|= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
;
903 // OR local status into aggregate status
905 AuthenticationStatus
|= AuthenticationStatus
>> 16;
908 if (IS_SECTION2 (GuidedHeader
)) {
909 Status
= OpenSectionStreamEx (
910 SECTION2_SIZE (GuidedHeader
) - ((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->DataOffset
,
911 (UINT8
*) GuidedHeader
+ ((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->DataOffset
,
913 AuthenticationStatus
,
914 &Node
->EncapsulatedStreamHandle
917 Status
= OpenSectionStreamEx (
918 SECTION_SIZE (GuidedHeader
) - ((EFI_GUID_DEFINED_SECTION
*) GuidedHeader
)->DataOffset
,
919 (UINT8
*) GuidedHeader
+ ((EFI_GUID_DEFINED_SECTION
*) GuidedHeader
)->DataOffset
,
921 AuthenticationStatus
,
922 &Node
->EncapsulatedStreamHandle
925 if (EFI_ERROR (Status
)) {
932 if ((AuthenticationStatus
& EFI_LOCAL_AUTH_STATUS_ALL
) ==
933 (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
)) {
935 // Need to register for RPN for when the required GUIDed extraction
936 // protocol becomes available. This will enable us to refresh the
937 // AuthenticationStatus cached in the Stream if it's ever requested
940 CreateGuidedExtractionRpnEvent (Stream
, Node
);
948 // Nothing to do if it's a leaf
954 // Last, add the new child node to the stream
956 InsertTailList (&Stream
->Children
, &Node
->Link
);
962 Worker function Recursively searches / builds section stream database
963 looking for requested section.
966 @param SourceStream Indicates the section stream in which to do the search.
967 @param SearchType Indicates the type of section to search for.
968 @param SectionInstance Indicates which instance of section to find. This is
969 an in/out parameter to deal with recursions.
970 @param SectionDefinitionGuid Guid of section definition
971 @param FoundChild Output indicating the child node that is found.
972 @param FoundStream Output indicating which section stream the child was
973 found in. If this stream was generated as a result of
974 an encapsulation section, the streamhandle is visible
975 within the SEP driver only.
976 @param AuthenticationStatus Indicates the authentication status of the found section.
978 @retval EFI_SUCCESS Child node was found and returned.
979 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
980 @retval EFI_NOT_FOUND Requested child node does not exist.
981 @retval EFI_PROTOCOL_ERROR A required GUIDED section extraction protocol does not
987 IN FRAMEWORK_SECTION_STREAM_NODE
*SourceStream
,
988 IN EFI_SECTION_TYPE SearchType
,
989 IN OUT UINTN
*SectionInstance
,
990 IN EFI_GUID
*SectionDefinitionGuid
,
991 OUT FRAMEWORK_SECTION_CHILD_NODE
**FoundChild
,
992 OUT FRAMEWORK_SECTION_STREAM_NODE
**FoundStream
,
993 OUT UINT32
*AuthenticationStatus
996 FRAMEWORK_SECTION_CHILD_NODE
*CurrentChildNode
;
997 FRAMEWORK_SECTION_CHILD_NODE
*RecursedChildNode
;
998 FRAMEWORK_SECTION_STREAM_NODE
*RecursedFoundStream
;
999 UINT32 NextChildOffset
;
1000 EFI_STATUS ErrorStatus
;
1003 CurrentChildNode
= NULL
;
1004 ErrorStatus
= EFI_NOT_FOUND
;
1006 if (SourceStream
->StreamLength
== 0) {
1007 return EFI_NOT_FOUND
;
1010 if (IsListEmpty (&SourceStream
->Children
) &&
1011 SourceStream
->StreamLength
>= sizeof (EFI_COMMON_SECTION_HEADER
)) {
1013 // This occurs when a section stream exists, but no child sections
1014 // have been parsed out yet. Therefore, extract the first child and add it
1015 // to the list of children so we can get started.
1016 // Section stream may contain an array of zero or more bytes.
1017 // So, its size should be >= the size of commen section header.
1019 Status
= CreateChildNode (SourceStream
, 0, &CurrentChildNode
);
1020 if (EFI_ERROR (Status
)) {
1026 // At least one child has been parsed out of the section stream. So, walk
1027 // through the sections that have already been parsed out looking for the
1028 // requested section, if necessary, continue parsing section stream and
1029 // adding children until either the requested section is found, or we run
1032 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream
->Children
));
1035 if (ChildIsType (SourceStream
, CurrentChildNode
, SearchType
, SectionDefinitionGuid
)) {
1037 // The type matches, so check the instance count to see if it's the one we want
1039 (*SectionInstance
)--;
1040 if (*SectionInstance
== 0) {
1044 *FoundChild
= CurrentChildNode
;
1045 *FoundStream
= SourceStream
;
1046 *AuthenticationStatus
= SourceStream
->AuthenticationStatus
;
1051 ASSERT (CurrentChildNode
!= NULL
);
1052 if (CurrentChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1054 // If the current node is an encapsulating node, recurse into it...
1056 Status
= FindChildNode (
1057 (FRAMEWORK_SECTION_STREAM_NODE
*)CurrentChildNode
->EncapsulatedStreamHandle
,
1060 SectionDefinitionGuid
,
1062 &RecursedFoundStream
,
1063 AuthenticationStatus
1066 // If the status is not EFI_SUCCESS, just save the error code and continue
1067 // to find the request child node in the rest stream.
1069 if (*SectionInstance
== 0) {
1070 ASSERT_EFI_ERROR (Status
);
1071 *FoundChild
= RecursedChildNode
;
1072 *FoundStream
= RecursedFoundStream
;
1075 ErrorStatus
= Status
;
1077 } else if ((CurrentChildNode
->Type
== EFI_SECTION_GUID_DEFINED
) && (SearchType
!= EFI_SECTION_GUID_DEFINED
)) {
1079 // When Node Type is GUIDED section, but Node has no encapsulated data, Node data should not be parsed
1080 // because a required GUIDED section extraction protocol does not exist.
1081 // If SearchType is not GUIDED section, EFI_PROTOCOL_ERROR should return.
1083 ErrorStatus
= EFI_PROTOCOL_ERROR
;
1086 if (!IsNodeAtEnd (&SourceStream
->Children
, &CurrentChildNode
->Link
)) {
1088 // We haven't found the child node we're interested in yet, but there's
1089 // still more nodes that have already been parsed so get the next one
1090 // and continue searching..
1092 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream
->Children
, &CurrentChildNode
->Link
));
1095 // We've exhausted children that have already been parsed, so see if
1096 // there's any more data and continue parsing out more children if there
1099 NextChildOffset
= CurrentChildNode
->OffsetInStream
+ CurrentChildNode
->Size
;
1101 // Round up to 4 byte boundary
1103 NextChildOffset
+= 3;
1104 NextChildOffset
&= ~(UINTN
)3;
1105 if (NextChildOffset
<= SourceStream
->StreamLength
- sizeof (EFI_COMMON_SECTION_HEADER
)) {
1107 // There's an unparsed child remaining in the stream, so create a new child node
1109 Status
= CreateChildNode (SourceStream
, NextChildOffset
, &CurrentChildNode
);
1110 if (EFI_ERROR (Status
)) {
1114 ASSERT (EFI_ERROR (ErrorStatus
));
1122 Worker function. Search stream database for requested stream handle.
1124 @param SearchHandle Indicates which stream to look for.
1125 @param FoundStream Output pointer to the found stream.
1127 @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains
1129 @retval EFI_NOT_FOUND SearchHandle was not found in the stream database.
1134 IN UINTN SearchHandle
,
1135 OUT FRAMEWORK_SECTION_STREAM_NODE
**FoundStream
1138 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1140 if (!IsListEmpty (&mStreamRoot
)) {
1141 StreamNode
= STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot
));
1143 if (StreamNode
->StreamHandle
== SearchHandle
) {
1144 *FoundStream
= StreamNode
;
1146 } else if (IsNodeAtEnd (&mStreamRoot
, &StreamNode
->Link
)) {
1149 StreamNode
= STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot
, &StreamNode
->Link
));
1154 return EFI_NOT_FOUND
;
1158 SEP member function. Retrieves requested section from section stream.
1160 @param This Pointer to SEP instance.
1161 @param SectionStreamHandle The section stream from which to extract the requested
1163 @param SectionType A pointer to the type of section to search for.
1164 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED, then
1165 SectionDefinitionGuid indicates which of these types
1166 of sections to search for.
1167 @param SectionInstance Indicates which instance of the requested section to
1169 @param Buffer Double indirection to buffer. If *Buffer is non-null on
1170 input, then the buffer is caller allocated. If
1171 *Buffer is NULL, then the buffer is callee allocated.
1172 In either case, the requried buffer size is returned
1174 @param BufferSize On input, indicates the size of *Buffer if *Buffer is
1175 non-null on input. On output, indicates the required
1176 size (allocated size if callee allocated) of *Buffer.
1177 @param AuthenticationStatus Indicates the authentication status of the retrieved
1181 @retval EFI_SUCCESS Section was retrieved successfully
1182 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the section
1183 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
1184 bit set, but there was no corresponding GUIDed Section
1185 Extraction Protocol in the handle database. *Buffer is
1187 @retval EFI_NOT_FOUND An error was encountered when parsing the SectionStream.
1188 This indicates the SectionStream is not correctly
1190 @retval EFI_NOT_FOUND The requested section does not exist.
1191 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process the
1193 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.
1194 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is
1195 insufficient to contain the requested section. The
1196 input buffer is filled and contents are section contents
1203 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
1204 IN UINTN SectionStreamHandle
,
1205 IN EFI_SECTION_TYPE
*SectionType
,
1206 IN EFI_GUID
*SectionDefinitionGuid
,
1207 IN UINTN SectionInstance
,
1209 IN OUT UINTN
*BufferSize
,
1210 OUT UINT32
*AuthenticationStatus
1213 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1216 FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
;
1217 FRAMEWORK_SECTION_STREAM_NODE
*ChildStreamNode
;
1219 UINT32 ExtractedAuthenticationStatus
;
1223 EFI_COMMON_SECTION_HEADER
*Section
;
1226 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1227 Instance
= SectionInstance
+ 1;
1230 // Locate target stream
1232 Status
= FindStreamNode (SectionStreamHandle
, &StreamNode
);
1233 if (EFI_ERROR (Status
)) {
1234 Status
= EFI_INVALID_PARAMETER
;
1235 goto GetSection_Done
;
1239 // Found the stream, now locate and return the appropriate section
1241 if (SectionType
== NULL
) {
1243 // SectionType == NULL means return the WHOLE section stream...
1245 CopySize
= StreamNode
->StreamLength
;
1246 CopyBuffer
= StreamNode
->StreamBuffer
;
1247 *AuthenticationStatus
= StreamNode
->AuthenticationStatus
;
1250 // There's a requested section type, so go find it and return it...
1252 Status
= FindChildNode (
1256 SectionDefinitionGuid
,
1259 &ExtractedAuthenticationStatus
1261 if (EFI_ERROR (Status
)) {
1262 goto GetSection_Done
;
1264 ASSERT (ChildNode
!= NULL
);
1265 ASSERT (ChildStreamNode
!= NULL
);
1266 Section
= (EFI_COMMON_SECTION_HEADER
*) (ChildStreamNode
->StreamBuffer
+ ChildNode
->OffsetInStream
);
1268 if (IS_SECTION2 (Section
)) {
1269 CopySize
= SECTION2_SIZE (Section
) - sizeof (EFI_COMMON_SECTION_HEADER2
);
1270 CopyBuffer
= (UINT8
*) Section
+ sizeof (EFI_COMMON_SECTION_HEADER2
);
1272 CopySize
= SECTION_SIZE (Section
) - sizeof (EFI_COMMON_SECTION_HEADER
);
1273 CopyBuffer
= (UINT8
*) Section
+ sizeof (EFI_COMMON_SECTION_HEADER
);
1275 *AuthenticationStatus
= ExtractedAuthenticationStatus
;
1278 SectionSize
= CopySize
;
1279 if (*Buffer
!= NULL
) {
1281 // Caller allocated buffer. Fill to size and return required size...
1283 if (*BufferSize
< CopySize
) {
1284 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
1285 CopySize
= *BufferSize
;
1289 // Callee allocated buffer. Allocate buffer and return size.
1291 *Buffer
= AllocatePool (CopySize
);
1292 if (*Buffer
== NULL
) {
1293 Status
= EFI_OUT_OF_RESOURCES
;
1294 goto GetSection_Done
;
1297 CopyMem (*Buffer
, CopyBuffer
, CopySize
);
1298 *BufferSize
= SectionSize
;
1301 gBS
->RestoreTPL (OldTpl
);
1306 Worker function. Destructor for child nodes.
1308 @param ChildNode Indicates the node to destroy
1313 IN FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
1316 ASSERT (ChildNode
->Signature
== FRAMEWORK_SECTION_CHILD_SIGNATURE
);
1318 // Remove the child from it's list
1320 RemoveEntryList (&ChildNode
->Link
);
1322 if (ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1324 // If it's an encapsulating section, we close the resulting section stream.
1325 // CloseSectionStream will free all memory associated with the stream.
1327 CloseSectionStream (&mSectionExtraction
, ChildNode
->EncapsulatedStreamHandle
);
1330 // Last, free the child node itself
1332 FreePool (ChildNode
);
1336 SEP member function. Deletes an existing section stream
1338 @param This Indicates the calling context.
1339 @param StreamHandleToClose Indicates the stream to close
1341 @retval EFI_SUCCESS Section stream was closed successfully.
1342 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
1343 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
1349 CloseSectionStream (
1350 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
1351 IN UINTN StreamHandleToClose
1354 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1358 FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
;
1360 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1363 // Locate target stream
1365 Status
= FindStreamNode (StreamHandleToClose
, &StreamNode
);
1366 if (!EFI_ERROR (Status
)) {
1368 // Found the stream, so close it
1370 RemoveEntryList (&StreamNode
->Link
);
1371 while (!IsListEmpty (&StreamNode
->Children
)) {
1372 Link
= GetFirstNode (&StreamNode
->Children
);
1373 ChildNode
= CHILD_SECTION_NODE_FROM_LINK (Link
);
1374 FreeChildNode (ChildNode
);
1376 FreePool (StreamNode
->StreamBuffer
);
1377 FreePool (StreamNode
);
1378 Status
= EFI_SUCCESS
;
1380 Status
= EFI_INVALID_PARAMETER
;
1383 gBS
->RestoreTPL (OldTpl
);