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 - 2012, 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
;
79 // If the section REQUIRES an extraction protocol, register for RPN
80 // when the required GUIDed extraction protocol becomes available.
83 } FRAMEWORK_SECTION_CHILD_NODE
;
85 #define FRAMEWORK_SECTION_STREAM_SIGNATURE SIGNATURE_32('S','X','S','S')
86 #define STREAM_NODE_FROM_LINK(Node) \
87 CR (Node, FRAMEWORK_SECTION_STREAM_NODE, Link, FRAMEWORK_SECTION_STREAM_SIGNATURE)
97 // Authentication status is from GUIDed encapsulations.
99 UINT32 AuthenticationStatus
;
100 } FRAMEWORK_SECTION_STREAM_NODE
;
102 #define NULL_STREAM_HANDLE 0
105 FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
;
106 FRAMEWORK_SECTION_STREAM_NODE
*ParentStream
;
111 SEP member function. This function creates and returns a new section stream
112 handle to represent the new section stream.
114 @param This Indicates the calling context.
115 @param SectionStreamLength Size in bytes of the section stream.
116 @param SectionStream Buffer containing the new section stream.
117 @param SectionStreamHandle A pointer to a caller allocated UINTN that on output
118 contains the new section stream handle.
120 @retval EFI_SUCCESS Section wase opened successfully.
121 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
122 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
129 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
130 IN UINTN SectionStreamLength
,
131 IN VOID
*SectionStream
,
132 OUT UINTN
*SectionStreamHandle
137 SEP member function. Retrieves requested section from section stream.
139 @param This Pointer to SEP instance.
140 @param SectionStreamHandle The section stream from which to extract the requested
142 @param SectionType A pointer to the type of section to search for.
143 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED, then
144 SectionDefinitionGuid indicates which of these types
145 of sections to search for.
146 @param SectionInstance Indicates which instance of the requested section to
148 @param Buffer Double indirection to buffer. If *Buffer is non-null on
149 input, then the buffer is caller allocated. If
150 *Buffer is NULL, then the buffer is callee allocated.
151 In either case, the requried buffer size is returned
153 @param BufferSize On input, indicates the size of *Buffer if *Buffer is
154 non-null on input. On output, indicates the required
155 size (allocated size if callee allocated) of *Buffer.
156 @param AuthenticationStatus Indicates the authentication status of the retrieved
160 @retval EFI_SUCCESS Section was retrieved successfully
161 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the section
162 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
163 bit set, but there was no corresponding GUIDed Section
164 Extraction Protocol in the handle database. *Buffer is
166 @retval EFI_NOT_FOUND An error was encountered when parsing the SectionStream.
167 This indicates the SectionStream is not correctly
169 @retval EFI_NOT_FOUND The requested section does not exist.
170 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process the
172 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.
173 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is
174 insufficient to contain the requested section. The
175 input buffer is filled and contents are section contents
182 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
183 IN UINTN SectionStreamHandle
,
184 IN EFI_SECTION_TYPE
*SectionType
,
185 IN EFI_GUID
*SectionDefinitionGuid
,
186 IN UINTN SectionInstance
,
188 IN OUT UINTN
*BufferSize
,
189 OUT UINT32
*AuthenticationStatus
194 SEP member function. Deletes an existing section stream
196 @param This Indicates the calling context.
197 @param StreamHandleToClose Indicates the stream to close
199 @retval EFI_SUCCESS Section stream was closed successfully.
200 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
201 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
208 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
209 IN UINTN StreamHandleToClose
216 LIST_ENTRY mStreamRoot
= INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot
);
218 EFI_HANDLE mSectionExtractionHandle
= NULL
;
220 EFI_SECTION_EXTRACTION_PROTOCOL mSectionExtraction
= {
227 Entry point of the section extraction code. Initializes an instance of the
228 section extraction interface and installs it on a new handle.
230 @param ImageHandle A handle for the image that is initializing this driver
231 @param SystemTable A pointer to the EFI system table
233 @retval EFI_SUCCESS Driver initialized successfully
234 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
239 SectionExtractionEntryPoint (
240 IN EFI_HANDLE ImageHandle
,
241 IN EFI_SYSTEM_TABLE
*SystemTable
247 // Install SEP to a new handle
249 Status
= gBS
->InstallProtocolInterface (
250 &mSectionExtractionHandle
,
251 &gEfiSectionExtractionProtocolGuid
,
252 EFI_NATIVE_INTERFACE
,
255 ASSERT_EFI_ERROR (Status
);
262 Check if a stream is valid.
264 @param SectionStream The section stream to be checked
265 @param SectionStreamLength The length of section stream
267 @return A boolean value indicating the validness of the section stream.
271 IsValidSectionStream (
272 IN VOID
*SectionStream
,
273 IN UINTN SectionStreamLength
278 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
279 EFI_COMMON_SECTION_HEADER
*NextSectionHeader
;
282 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*)SectionStream
;
284 while (TotalLength
< SectionStreamLength
) {
285 if (IS_SECTION2 (SectionHeader
)) {
286 SectionLength
= SECTION2_SIZE (SectionHeader
);
288 SectionLength
= SECTION_SIZE (SectionHeader
);
290 TotalLength
+= SectionLength
;
292 if (TotalLength
== SectionStreamLength
) {
297 // Move to the next byte following the section...
299 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) SectionHeader
+ SectionLength
);
302 // Figure out where the next section begins
304 NextSectionHeader
= ALIGN_POINTER(SectionHeader
, 4);
305 TotalLength
+= (UINTN
) NextSectionHeader
- (UINTN
) SectionHeader
;
306 SectionHeader
= NextSectionHeader
;
314 Worker function. Constructor for section streams.
316 @param SectionStreamLength Size in bytes of the section stream.
317 @param SectionStream Buffer containing the new section stream.
318 @param AllocateBuffer Indicates whether the stream buffer is to be copied
319 or the input buffer is to be used in place.
320 @param AuthenticationStatus Indicates the default authentication status for the
322 @param SectionStreamHandle A pointer to a caller allocated section stream handle.
324 @retval EFI_SUCCESS Stream was added to stream database.
325 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
329 OpenSectionStreamEx (
330 IN UINTN SectionStreamLength
,
331 IN VOID
*SectionStream
,
332 IN BOOLEAN AllocateBuffer
,
333 IN UINT32 AuthenticationStatus
,
334 OUT UINTN
*SectionStreamHandle
337 FRAMEWORK_SECTION_STREAM_NODE
*NewStream
;
341 // Allocate a new stream
343 NewStream
= AllocatePool (sizeof (FRAMEWORK_SECTION_STREAM_NODE
));
344 if (NewStream
== NULL
) {
345 return EFI_OUT_OF_RESOURCES
;
348 if (AllocateBuffer
) {
350 // if we're here, we're double buffering, allocate the buffer and copy the
353 if (SectionStreamLength
> 0) {
354 NewStream
->StreamBuffer
= AllocatePool (SectionStreamLength
);
355 if (NewStream
->StreamBuffer
== NULL
) {
356 FreePool (NewStream
);
357 return EFI_OUT_OF_RESOURCES
;
360 // Copy in stream data
362 CopyMem (NewStream
->StreamBuffer
, SectionStream
, SectionStreamLength
);
365 // It's possible to have a zero length section stream.
367 NewStream
->StreamBuffer
= NULL
;
371 // If were here, the caller has supplied the buffer (it's an internal call)
372 // so just assign the buffer. This happens when we open section streams
373 // as a result of expanding an encapsulating section.
375 NewStream
->StreamBuffer
= SectionStream
;
379 // Initialize the rest of the section stream
381 NewStream
->Signature
= FRAMEWORK_SECTION_STREAM_SIGNATURE
;
382 NewStream
->StreamHandle
= (UINTN
) NewStream
;
383 NewStream
->StreamLength
= SectionStreamLength
;
384 InitializeListHead (&NewStream
->Children
);
385 NewStream
->AuthenticationStatus
= AuthenticationStatus
;
388 // Add new stream to stream list
390 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
391 InsertTailList (&mStreamRoot
, &NewStream
->Link
);
392 gBS
->RestoreTPL (OldTpl
);
394 *SectionStreamHandle
= NewStream
->StreamHandle
;
400 SEP member function. This function creates and returns a new section stream
401 handle to represent the new section stream.
403 @param This Indicates the calling context.
404 @param SectionStreamLength Size in bytes of the section stream.
405 @param SectionStream Buffer containing the new section stream.
406 @param SectionStreamHandle A pointer to a caller allocated UINTN that on output
407 contains the new section stream handle.
409 @retval EFI_SUCCESS Section wase opened successfully.
410 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
411 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
418 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
419 IN UINTN SectionStreamLength
,
420 IN VOID
*SectionStream
,
421 OUT UINTN
*SectionStreamHandle
425 // Check to see section stream looks good...
427 if (!IsValidSectionStream (SectionStream
, SectionStreamLength
)) {
428 return EFI_INVALID_PARAMETER
;
431 return OpenSectionStreamEx (
441 Worker function. Determine if the input stream:child matches the input type.
443 @param Stream Indicates the section stream associated with the child
444 @param Child Indicates the child to check
445 @param SearchType Indicates the type of section to check against for
446 @param SectionDefinitionGuid Indicates the GUID to check against if the type is
447 EFI_SECTION_GUID_DEFINED
449 @retval TRUE The child matches
450 @retval FALSE The child doesn't match
455 IN FRAMEWORK_SECTION_STREAM_NODE
*Stream
,
456 IN FRAMEWORK_SECTION_CHILD_NODE
*Child
,
457 IN EFI_SECTION_TYPE SearchType
,
458 IN EFI_GUID
*SectionDefinitionGuid
461 EFI_GUID_DEFINED_SECTION
*GuidedSection
;
463 if (SearchType
== EFI_SECTION_ALL
) {
466 if (Child
->Type
!= SearchType
) {
469 if ((SearchType
!= EFI_SECTION_GUID_DEFINED
) || (SectionDefinitionGuid
== NULL
)) {
472 GuidedSection
= (EFI_GUID_DEFINED_SECTION
* )(Stream
->StreamBuffer
+ Child
->OffsetInStream
);
473 if (IS_SECTION2 (GuidedSection
)) {
474 return CompareGuid (&(((EFI_GUID_DEFINED_SECTION2
*) GuidedSection
)->SectionDefinitionGuid
), SectionDefinitionGuid
);
476 return CompareGuid (&GuidedSection
->SectionDefinitionGuid
, SectionDefinitionGuid
);
481 Create a protocol notification event and return it.
483 @param ProtocolGuid Protocol to register notification event on.
484 @param NotifyTpl Maximum TPL to signal the NotifyFunction.
485 @param NotifyFunction EFI notification routine.
486 @param NotifyContext Context passed into Event when it is created.
487 @param Registration Registration key returned from RegisterProtocolNotify().
488 @param SignalFlag Boolean value to decide whether kick the event after register or not.
490 @return The EFI_EVENT that has been registered to be signaled when a ProtocolGuid
491 is added to the system.
495 CreateProtocolNotifyEvent (
496 IN EFI_GUID
*ProtocolGuid
,
497 IN EFI_TPL NotifyTpl
,
498 IN EFI_EVENT_NOTIFY NotifyFunction
,
499 IN VOID
*NotifyContext
,
500 OUT VOID
**Registration
,
501 IN BOOLEAN SignalFlag
511 Status
= gBS
->CreateEvent (
518 ASSERT_EFI_ERROR (Status
);
521 // Register for protocol notifactions on this event
524 Status
= gBS
->RegisterProtocolNotify (
529 ASSERT_EFI_ERROR (Status
);
533 // Kick the event so we will perform an initial pass of
534 // current installed drivers
536 gBS
->SignalEvent (Event
);
543 RPN callback function.
544 1. Initialize the section stream when the GUIDED_SECTION_EXTRACTION_PROTOCOL is installed.
545 2. Removes a stale section stream and re-initializes it with an updated AuthenticationStatus.
547 @param Event The event that fired
548 @param RpnContext A pointer to the context that allows us to identify
549 the relevent encapsulation.
554 NotifyGuidedExtraction (
560 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
561 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
562 VOID
*NewStreamBuffer
;
563 UINTN NewStreamBufferSize
;
564 UINT32 AuthenticationStatus
;
565 RPN_EVENT_CONTEXT
*Context
;
567 Context
= RpnContext
;
568 Status
= EFI_SUCCESS
;
569 if (Context
->ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
570 Status
= CloseSectionStream (&mSectionExtraction
, Context
->ChildNode
->EncapsulatedStreamHandle
);
572 if (!EFI_ERROR (Status
)) {
574 // The stream is not initialized, open it.
575 // Or the stream closed successfully, so re-open the stream with correct AuthenticationStatus.
578 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*)
579 (Context
->ParentStream
->StreamBuffer
+ Context
->ChildNode
->OffsetInStream
);
580 ASSERT (GuidedHeader
->CommonHeader
.Type
== EFI_SECTION_GUID_DEFINED
);
582 Status
= gBS
->LocateProtocol (Context
->ChildNode
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
583 ASSERT_EFI_ERROR (Status
);
586 Status
= GuidedExtraction
->ExtractSection (
590 &NewStreamBufferSize
,
591 &AuthenticationStatus
593 ASSERT_EFI_ERROR (Status
);
595 // OR in the parent stream's aggregate status.
597 AuthenticationStatus
|= Context
->ParentStream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
598 Status
= OpenSectionStreamEx (
602 AuthenticationStatus
,
603 &Context
->ChildNode
->EncapsulatedStreamHandle
605 ASSERT_EFI_ERROR (Status
);
609 // If above, the stream did not close successfully, it indicates it's
610 // already been closed by someone, so just destroy the event and be done with
614 gBS
->CloseEvent (Event
);
615 Context
->ChildNode
->Event
= NULL
;
620 Worker function. Constructor for RPN event if needed to keep AuthenticationStatus
621 cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...
623 @param ParentStream Indicates the parent of the ecnapsulation section (child)
624 @param ChildNode Indicates the child node that is the encapsulation section.
628 CreateGuidedExtractionRpnEvent (
629 IN FRAMEWORK_SECTION_STREAM_NODE
*ParentStream
,
630 IN FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
633 RPN_EVENT_CONTEXT
*Context
;
636 // Allocate new event structure and context
638 Context
= AllocatePool (sizeof (RPN_EVENT_CONTEXT
));
639 ASSERT (Context
!= NULL
);
641 Context
->ChildNode
= ChildNode
;
642 Context
->ParentStream
= ParentStream
;
644 Context
->ChildNode
->Event
= CreateProtocolNotifyEvent (
645 Context
->ChildNode
->EncapsulationGuid
,
647 NotifyGuidedExtraction
,
649 &Context
->Registration
,
655 Worker function. Constructor for new child nodes.
657 @param Stream Indicates the section stream in which to add the child.
658 @param ChildOffset Indicates the offset in Stream that is the beginning
659 of the child section.
660 @param ChildNode Indicates the Callee allocated and initialized child.
662 @retval EFI_SUCCESS Child node was found and returned.
663 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
664 @retval EFI_PROTOCOL_ERROR Encapsulation sections produce new stream handles when
665 the child node is created. If the section type is GUID
666 defined, and the extraction GUID does not exist, and
667 producing the stream requires the GUID, then a protocol
668 error is generated and no child is produced.
669 Values returned by OpenSectionStreamEx.
674 IN FRAMEWORK_SECTION_STREAM_NODE
*Stream
,
675 IN UINT32 ChildOffset
,
676 OUT FRAMEWORK_SECTION_CHILD_NODE
**ChildNode
680 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
681 EFI_COMPRESSION_SECTION
*CompressionHeader
;
682 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
683 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
684 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
685 VOID
*NewStreamBuffer
;
688 UINTN NewStreamBufferSize
;
689 UINT32 AuthenticationStatus
;
690 VOID
*CompressionSource
;
691 UINT32 CompressionSourceSize
;
692 UINT32 UncompressedLength
;
693 UINT8 CompressionType
;
694 UINT16 GuidedSectionAttributes
;
696 FRAMEWORK_SECTION_CHILD_NODE
*Node
;
698 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) (Stream
->StreamBuffer
+ ChildOffset
);
701 // Allocate a new node
703 *ChildNode
= AllocateZeroPool (sizeof (FRAMEWORK_SECTION_CHILD_NODE
));
706 return EFI_OUT_OF_RESOURCES
;
712 Node
->Signature
= FRAMEWORK_SECTION_CHILD_SIGNATURE
;
713 Node
->Type
= SectionHeader
->Type
;
714 if (IS_SECTION2 (SectionHeader
)) {
715 Node
->Size
= SECTION2_SIZE (SectionHeader
);
717 Node
->Size
= SECTION_SIZE (SectionHeader
);
719 Node
->OffsetInStream
= ChildOffset
;
720 Node
->EncapsulatedStreamHandle
= NULL_STREAM_HANDLE
;
721 Node
->EncapsulationGuid
= NULL
;
724 // If it's an encapsulating section, then create the new section stream also
726 switch (Node
->Type
) {
727 case EFI_SECTION_COMPRESSION
:
729 // Get the CompressionSectionHeader
731 if (Node
->Size
< sizeof (EFI_COMPRESSION_SECTION
)) {
733 return EFI_NOT_FOUND
;
736 CompressionHeader
= (EFI_COMPRESSION_SECTION
*) SectionHeader
;
738 if (IS_SECTION2 (CompressionHeader
)) {
739 CompressionSource
= (VOID
*) ((UINT8
*) CompressionHeader
+ sizeof (EFI_COMPRESSION_SECTION2
));
740 CompressionSourceSize
= (UINT32
) (SECTION2_SIZE (CompressionHeader
) - sizeof (EFI_COMPRESSION_SECTION2
));
741 UncompressedLength
= ((EFI_COMPRESSION_SECTION2
*) CompressionHeader
)->UncompressedLength
;
742 CompressionType
= ((EFI_COMPRESSION_SECTION2
*) CompressionHeader
)->CompressionType
;
744 CompressionSource
= (VOID
*) ((UINT8
*) CompressionHeader
+ sizeof (EFI_COMPRESSION_SECTION
));
745 CompressionSourceSize
= (UINT32
) (SECTION_SIZE (CompressionHeader
) - sizeof (EFI_COMPRESSION_SECTION
));
746 UncompressedLength
= CompressionHeader
->UncompressedLength
;
747 CompressionType
= CompressionHeader
->CompressionType
;
751 // Allocate space for the new stream
753 if (UncompressedLength
> 0) {
754 NewStreamBufferSize
= UncompressedLength
;
755 NewStreamBuffer
= AllocatePool (NewStreamBufferSize
);
756 if (NewStreamBuffer
== NULL
) {
758 return EFI_OUT_OF_RESOURCES
;
761 if (CompressionType
== EFI_NOT_COMPRESSED
) {
763 // stream is not actually compressed, just encapsulated. So just copy it.
765 CopyMem (NewStreamBuffer
, CompressionSource
, NewStreamBufferSize
);
766 } else if (CompressionType
== EFI_STANDARD_COMPRESSION
) {
768 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
772 // Decompress the stream
774 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
776 ASSERT_EFI_ERROR (Status
);
778 Status
= Decompress
->GetInfo (
781 CompressionSourceSize
,
782 (UINT32
*)&NewStreamBufferSize
,
785 if (EFI_ERROR (Status
) || (NewStreamBufferSize
!= UncompressedLength
)) {
787 FreePool (NewStreamBuffer
);
788 if (!EFI_ERROR (Status
)) {
789 Status
= EFI_BAD_BUFFER_SIZE
;
794 ScratchBuffer
= AllocatePool (ScratchSize
);
795 if (ScratchBuffer
== NULL
) {
797 FreePool (NewStreamBuffer
);
798 return EFI_OUT_OF_RESOURCES
;
801 Status
= Decompress
->Decompress (
804 CompressionSourceSize
,
806 (UINT32
)NewStreamBufferSize
,
810 FreePool (ScratchBuffer
);
811 if (EFI_ERROR (Status
)) {
813 FreePool (NewStreamBuffer
);
818 NewStreamBuffer
= NULL
;
819 NewStreamBufferSize
= 0;
822 Status
= OpenSectionStreamEx (
826 Stream
->AuthenticationStatus
,
827 &Node
->EncapsulatedStreamHandle
829 if (EFI_ERROR (Status
)) {
831 FreePool (NewStreamBuffer
);
836 case EFI_SECTION_GUID_DEFINED
:
837 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*) SectionHeader
;
838 if (IS_SECTION2 (GuidedHeader
)) {
839 Node
->EncapsulationGuid
= &(((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->SectionDefinitionGuid
);
840 GuidedSectionAttributes
= ((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->Attributes
;
842 Node
->EncapsulationGuid
= &GuidedHeader
->SectionDefinitionGuid
;
843 GuidedSectionAttributes
= GuidedHeader
->Attributes
;
845 Status
= gBS
->LocateProtocol (Node
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
846 if (!EFI_ERROR (Status
)) {
848 // NewStreamBuffer is always allocated by ExtractSection... No caller
851 Status
= GuidedExtraction
->ExtractSection (
855 &NewStreamBufferSize
,
856 &AuthenticationStatus
858 if (EFI_ERROR (Status
)) {
859 FreePool (*ChildNode
);
860 return EFI_PROTOCOL_ERROR
;
864 // Make sure we initialize the new stream with the correct
865 // authentication status for both aggregate and local status fields.
867 if ((GuidedSectionAttributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
869 // OR in the parent stream's aggregate status.
871 AuthenticationStatus
|= Stream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
874 // since there's no authentication data contributed by the section,
875 // just inherit the full value from our immediate parent.
877 AuthenticationStatus
= Stream
->AuthenticationStatus
;
880 Status
= OpenSectionStreamEx (
884 AuthenticationStatus
,
885 &Node
->EncapsulatedStreamHandle
887 if (EFI_ERROR (Status
)) {
888 FreePool (*ChildNode
);
889 FreePool (NewStreamBuffer
);
894 // There's no GUIDed section extraction protocol available.
896 if ((GuidedSectionAttributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) == EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) {
898 // If the section REQUIRES an extraction protocol, register for RPN
899 // when the required GUIDed extraction protocol becomes available.
901 AuthenticationStatus
= 0;
902 CreateGuidedExtractionRpnEvent (Stream
, Node
);
905 // Figure out the proper authentication status
907 AuthenticationStatus
= Stream
->AuthenticationStatus
;
908 if ((GuidedSectionAttributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
910 // The local status of the new stream is contained in
911 // AuthenticaionStatus. This value needs to be ORed into the
912 // Aggregate bits also...
916 // Clear out and initialize the local status
918 AuthenticationStatus
&= ~EFI_LOCAL_AUTH_STATUS_ALL
;
919 AuthenticationStatus
|= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
;
921 // OR local status into aggregate status
923 AuthenticationStatus
|= AuthenticationStatus
>> 16;
926 if (IS_SECTION2 (GuidedHeader
)) {
927 Status
= OpenSectionStreamEx (
928 SECTION2_SIZE (GuidedHeader
) - ((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->DataOffset
,
929 (UINT8
*) GuidedHeader
+ ((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->DataOffset
,
931 AuthenticationStatus
,
932 &Node
->EncapsulatedStreamHandle
935 Status
= OpenSectionStreamEx (
936 SECTION_SIZE (GuidedHeader
) - ((EFI_GUID_DEFINED_SECTION
*) GuidedHeader
)->DataOffset
,
937 (UINT8
*) GuidedHeader
+ ((EFI_GUID_DEFINED_SECTION
*) GuidedHeader
)->DataOffset
,
939 AuthenticationStatus
,
940 &Node
->EncapsulatedStreamHandle
943 if (EFI_ERROR (Status
)) {
950 if ((AuthenticationStatus
& EFI_LOCAL_AUTH_STATUS_ALL
) ==
951 (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
)) {
953 // Need to register for RPN for when the required GUIDed extraction
954 // protocol becomes available. This will enable us to refresh the
955 // AuthenticationStatus cached in the Stream if it's ever requested
958 CreateGuidedExtractionRpnEvent (Stream
, Node
);
966 // Nothing to do if it's a leaf
972 // Last, add the new child node to the stream
974 InsertTailList (&Stream
->Children
, &Node
->Link
);
980 Worker function Recursively searches / builds section stream database
981 looking for requested section.
984 @param SourceStream Indicates the section stream in which to do the search.
985 @param SearchType Indicates the type of section to search for.
986 @param SectionInstance Indicates which instance of section to find. This is
987 an in/out parameter to deal with recursions.
988 @param SectionDefinitionGuid Guid of section definition
989 @param FoundChild Output indicating the child node that is found.
990 @param FoundStream Output indicating which section stream the child was
991 found in. If this stream was generated as a result of
992 an encapsulation section, the streamhandle is visible
993 within the SEP driver only.
994 @param AuthenticationStatus Indicates the authentication status of the found section.
996 @retval EFI_SUCCESS Child node was found and returned.
997 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
998 @retval EFI_NOT_FOUND Requested child node does not exist.
999 @retval EFI_PROTOCOL_ERROR A required GUIDED section extraction protocol does not
1005 IN FRAMEWORK_SECTION_STREAM_NODE
*SourceStream
,
1006 IN EFI_SECTION_TYPE SearchType
,
1007 IN OUT UINTN
*SectionInstance
,
1008 IN EFI_GUID
*SectionDefinitionGuid
,
1009 OUT FRAMEWORK_SECTION_CHILD_NODE
**FoundChild
,
1010 OUT FRAMEWORK_SECTION_STREAM_NODE
**FoundStream
,
1011 OUT UINT32
*AuthenticationStatus
1014 FRAMEWORK_SECTION_CHILD_NODE
*CurrentChildNode
;
1015 FRAMEWORK_SECTION_CHILD_NODE
*RecursedChildNode
;
1016 FRAMEWORK_SECTION_STREAM_NODE
*RecursedFoundStream
;
1017 UINT32 NextChildOffset
;
1018 EFI_STATUS ErrorStatus
;
1021 CurrentChildNode
= NULL
;
1022 ErrorStatus
= EFI_NOT_FOUND
;
1024 if (SourceStream
->StreamLength
== 0) {
1025 return EFI_NOT_FOUND
;
1028 if (IsListEmpty (&SourceStream
->Children
) &&
1029 SourceStream
->StreamLength
>= sizeof (EFI_COMMON_SECTION_HEADER
)) {
1031 // This occurs when a section stream exists, but no child sections
1032 // have been parsed out yet. Therefore, extract the first child and add it
1033 // to the list of children so we can get started.
1034 // Section stream may contain an array of zero or more bytes.
1035 // So, its size should be >= the size of commen section header.
1037 Status
= CreateChildNode (SourceStream
, 0, &CurrentChildNode
);
1038 if (EFI_ERROR (Status
)) {
1044 // At least one child has been parsed out of the section stream. So, walk
1045 // through the sections that have already been parsed out looking for the
1046 // requested section, if necessary, continue parsing section stream and
1047 // adding children until either the requested section is found, or we run
1050 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream
->Children
));
1053 ASSERT (CurrentChildNode
!= NULL
);
1054 if (ChildIsType (SourceStream
, CurrentChildNode
, SearchType
, SectionDefinitionGuid
)) {
1056 // The type matches, so check the instance count to see if it's the one we want
1058 (*SectionInstance
)--;
1059 if (*SectionInstance
== 0) {
1063 *FoundChild
= CurrentChildNode
;
1064 *FoundStream
= SourceStream
;
1065 *AuthenticationStatus
= SourceStream
->AuthenticationStatus
;
1070 if (CurrentChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1072 // If the current node is an encapsulating node, recurse into it...
1074 Status
= FindChildNode (
1075 (FRAMEWORK_SECTION_STREAM_NODE
*)CurrentChildNode
->EncapsulatedStreamHandle
,
1078 SectionDefinitionGuid
,
1080 &RecursedFoundStream
,
1081 AuthenticationStatus
1084 // If the status is not EFI_SUCCESS, just save the error code and continue
1085 // to find the request child node in the rest stream.
1087 if (*SectionInstance
== 0) {
1088 ASSERT_EFI_ERROR (Status
);
1089 *FoundChild
= RecursedChildNode
;
1090 *FoundStream
= RecursedFoundStream
;
1093 ErrorStatus
= Status
;
1095 } else if ((CurrentChildNode
->Type
== EFI_SECTION_GUID_DEFINED
) && (SearchType
!= EFI_SECTION_GUID_DEFINED
)) {
1097 // When Node Type is GUIDED section, but Node has no encapsulated data, Node data should not be parsed
1098 // because a required GUIDED section extraction protocol does not exist.
1099 // If SearchType is not GUIDED section, EFI_PROTOCOL_ERROR should return.
1101 ErrorStatus
= EFI_PROTOCOL_ERROR
;
1104 if (!IsNodeAtEnd (&SourceStream
->Children
, &CurrentChildNode
->Link
)) {
1106 // We haven't found the child node we're interested in yet, but there's
1107 // still more nodes that have already been parsed so get the next one
1108 // and continue searching..
1110 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream
->Children
, &CurrentChildNode
->Link
));
1113 // We've exhausted children that have already been parsed, so see if
1114 // there's any more data and continue parsing out more children if there
1117 NextChildOffset
= CurrentChildNode
->OffsetInStream
+ CurrentChildNode
->Size
;
1119 // Round up to 4 byte boundary
1121 NextChildOffset
+= 3;
1122 NextChildOffset
&= ~(UINTN
)3;
1123 if (NextChildOffset
<= SourceStream
->StreamLength
- sizeof (EFI_COMMON_SECTION_HEADER
)) {
1125 // There's an unparsed child remaining in the stream, so create a new child node
1127 Status
= CreateChildNode (SourceStream
, NextChildOffset
, &CurrentChildNode
);
1128 if (EFI_ERROR (Status
)) {
1132 ASSERT (EFI_ERROR (ErrorStatus
));
1140 Worker function. Search stream database for requested stream handle.
1142 @param SearchHandle Indicates which stream to look for.
1143 @param FoundStream Output pointer to the found stream.
1145 @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains
1147 @retval EFI_NOT_FOUND SearchHandle was not found in the stream database.
1152 IN UINTN SearchHandle
,
1153 OUT FRAMEWORK_SECTION_STREAM_NODE
**FoundStream
1156 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1158 if (!IsListEmpty (&mStreamRoot
)) {
1159 StreamNode
= STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot
));
1161 if (StreamNode
->StreamHandle
== SearchHandle
) {
1162 *FoundStream
= StreamNode
;
1164 } else if (IsNodeAtEnd (&mStreamRoot
, &StreamNode
->Link
)) {
1167 StreamNode
= STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot
, &StreamNode
->Link
));
1172 return EFI_NOT_FOUND
;
1176 SEP member function. Retrieves requested section from section stream.
1178 @param This Pointer to SEP instance.
1179 @param SectionStreamHandle The section stream from which to extract the requested
1181 @param SectionType A pointer to the type of section to search for.
1182 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED, then
1183 SectionDefinitionGuid indicates which of these types
1184 of sections to search for.
1185 @param SectionInstance Indicates which instance of the requested section to
1187 @param Buffer Double indirection to buffer. If *Buffer is non-null on
1188 input, then the buffer is caller allocated. If
1189 *Buffer is NULL, then the buffer is callee allocated.
1190 In either case, the requried buffer size is returned
1192 @param BufferSize On input, indicates the size of *Buffer if *Buffer is
1193 non-null on input. On output, indicates the required
1194 size (allocated size if callee allocated) of *Buffer.
1195 @param AuthenticationStatus Indicates the authentication status of the retrieved
1199 @retval EFI_SUCCESS Section was retrieved successfully
1200 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the section
1201 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
1202 bit set, but there was no corresponding GUIDed Section
1203 Extraction Protocol in the handle database. *Buffer is
1205 @retval EFI_NOT_FOUND An error was encountered when parsing the SectionStream.
1206 This indicates the SectionStream is not correctly
1208 @retval EFI_NOT_FOUND The requested section does not exist.
1209 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process the
1211 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.
1212 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is
1213 insufficient to contain the requested section. The
1214 input buffer is filled and contents are section contents
1221 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
1222 IN UINTN SectionStreamHandle
,
1223 IN EFI_SECTION_TYPE
*SectionType
,
1224 IN EFI_GUID
*SectionDefinitionGuid
,
1225 IN UINTN SectionInstance
,
1227 IN OUT UINTN
*BufferSize
,
1228 OUT UINT32
*AuthenticationStatus
1231 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1234 FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
;
1235 FRAMEWORK_SECTION_STREAM_NODE
*ChildStreamNode
;
1237 UINT32 ExtractedAuthenticationStatus
;
1241 EFI_COMMON_SECTION_HEADER
*Section
;
1244 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1245 Instance
= SectionInstance
+ 1;
1248 // Locate target stream
1250 Status
= FindStreamNode (SectionStreamHandle
, &StreamNode
);
1251 if (EFI_ERROR (Status
)) {
1252 Status
= EFI_INVALID_PARAMETER
;
1253 goto GetSection_Done
;
1257 // Found the stream, now locate and return the appropriate section
1259 if (SectionType
== NULL
) {
1261 // SectionType == NULL means return the WHOLE section stream...
1263 CopySize
= StreamNode
->StreamLength
;
1264 CopyBuffer
= StreamNode
->StreamBuffer
;
1265 *AuthenticationStatus
= StreamNode
->AuthenticationStatus
;
1268 // There's a requested section type, so go find it and return it...
1270 Status
= FindChildNode (
1274 SectionDefinitionGuid
,
1277 &ExtractedAuthenticationStatus
1279 if (EFI_ERROR (Status
)) {
1280 goto GetSection_Done
;
1282 ASSERT (ChildNode
!= NULL
);
1283 ASSERT (ChildStreamNode
!= NULL
);
1284 Section
= (EFI_COMMON_SECTION_HEADER
*) (ChildStreamNode
->StreamBuffer
+ ChildNode
->OffsetInStream
);
1286 if (IS_SECTION2 (Section
)) {
1287 CopySize
= SECTION2_SIZE (Section
) - sizeof (EFI_COMMON_SECTION_HEADER2
);
1288 CopyBuffer
= (UINT8
*) Section
+ sizeof (EFI_COMMON_SECTION_HEADER2
);
1290 CopySize
= SECTION_SIZE (Section
) - sizeof (EFI_COMMON_SECTION_HEADER
);
1291 CopyBuffer
= (UINT8
*) Section
+ sizeof (EFI_COMMON_SECTION_HEADER
);
1293 *AuthenticationStatus
= ExtractedAuthenticationStatus
;
1296 SectionSize
= CopySize
;
1297 if (*Buffer
!= NULL
) {
1299 // Caller allocated buffer. Fill to size and return required size...
1301 if (*BufferSize
< CopySize
) {
1302 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
1303 CopySize
= *BufferSize
;
1307 // Callee allocated buffer. Allocate buffer and return size.
1309 *Buffer
= AllocatePool (CopySize
);
1310 if (*Buffer
== NULL
) {
1311 Status
= EFI_OUT_OF_RESOURCES
;
1312 goto GetSection_Done
;
1315 CopyMem (*Buffer
, CopyBuffer
, CopySize
);
1316 *BufferSize
= SectionSize
;
1319 gBS
->RestoreTPL (OldTpl
);
1324 Worker function. Destructor for child nodes.
1326 @param ChildNode Indicates the node to destroy
1331 IN FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
1334 ASSERT (ChildNode
->Signature
== FRAMEWORK_SECTION_CHILD_SIGNATURE
);
1336 // Remove the child from it's list
1338 RemoveEntryList (&ChildNode
->Link
);
1340 if (ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1342 // If it's an encapsulating section, we close the resulting section stream.
1343 // CloseSectionStream will free all memory associated with the stream.
1345 CloseSectionStream (&mSectionExtraction
, ChildNode
->EncapsulatedStreamHandle
);
1348 if (ChildNode
->Event
!= NULL
) {
1349 gBS
->CloseEvent (ChildNode
->Event
);
1353 // Last, free the child node itself
1355 FreePool (ChildNode
);
1359 SEP member function. Deletes an existing section stream
1361 @param This Indicates the calling context.
1362 @param StreamHandleToClose Indicates the stream to close
1364 @retval EFI_SUCCESS Section stream was closed successfully.
1365 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
1366 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
1372 CloseSectionStream (
1373 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
1374 IN UINTN StreamHandleToClose
1377 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1381 FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
;
1383 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1386 // Locate target stream
1388 Status
= FindStreamNode (StreamHandleToClose
, &StreamNode
);
1389 if (!EFI_ERROR (Status
)) {
1391 // Found the stream, so close it
1393 RemoveEntryList (&StreamNode
->Link
);
1394 while (!IsListEmpty (&StreamNode
->Children
)) {
1395 Link
= GetFirstNode (&StreamNode
->Children
);
1396 ChildNode
= CHILD_SECTION_NODE_FROM_LINK (Link
);
1397 FreeChildNode (ChildNode
);
1399 FreePool (StreamNode
->StreamBuffer
);
1400 FreePool (StreamNode
);
1401 Status
= EFI_SUCCESS
;
1403 Status
= EFI_INVALID_PARAMETER
;
1406 gBS
->RestoreTPL (OldTpl
);