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
;
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 if (Node
->Size
< sizeof (EFI_COMPRESSION_SECTION
)) {
728 return EFI_NOT_FOUND
;
731 CompressionHeader
= (EFI_COMPRESSION_SECTION
*) SectionHeader
;
733 if (IS_SECTION2 (CompressionHeader
)) {
734 CompressionSource
= (VOID
*) ((UINT8
*) CompressionHeader
+ sizeof (EFI_COMPRESSION_SECTION2
));
735 CompressionSourceSize
= (UINT32
) (SECTION2_SIZE (CompressionHeader
) - sizeof (EFI_COMPRESSION_SECTION2
));
736 UncompressedLength
= ((EFI_COMPRESSION_SECTION2
*) CompressionHeader
)->UncompressedLength
;
737 CompressionType
= ((EFI_COMPRESSION_SECTION2
*) CompressionHeader
)->CompressionType
;
739 CompressionSource
= (VOID
*) ((UINT8
*) CompressionHeader
+ sizeof (EFI_COMPRESSION_SECTION
));
740 CompressionSourceSize
= (UINT32
) (SECTION_SIZE (CompressionHeader
) - sizeof (EFI_COMPRESSION_SECTION
));
741 UncompressedLength
= CompressionHeader
->UncompressedLength
;
742 CompressionType
= CompressionHeader
->CompressionType
;
746 // Allocate space for the new stream
748 if (UncompressedLength
> 0) {
749 NewStreamBufferSize
= UncompressedLength
;
750 NewStreamBuffer
= AllocatePool (NewStreamBufferSize
);
751 if (NewStreamBuffer
== NULL
) {
753 return EFI_OUT_OF_RESOURCES
;
756 if (CompressionType
== EFI_NOT_COMPRESSED
) {
758 // stream is not actually compressed, just encapsulated. So just copy it.
760 CopyMem (NewStreamBuffer
, CompressionSource
, NewStreamBufferSize
);
761 } else if (CompressionType
== EFI_STANDARD_COMPRESSION
) {
763 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
767 // Decompress the stream
769 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
771 ASSERT_EFI_ERROR (Status
);
773 Status
= Decompress
->GetInfo (
776 CompressionSourceSize
,
777 (UINT32
*)&NewStreamBufferSize
,
780 if (EFI_ERROR (Status
) || (NewStreamBufferSize
!= UncompressedLength
)) {
782 CoreFreePool (NewStreamBuffer
);
783 if (!EFI_ERROR (Status
)) {
784 Status
= EFI_BAD_BUFFER_SIZE
;
789 ScratchBuffer
= AllocatePool (ScratchSize
);
790 if (ScratchBuffer
== NULL
) {
792 FreePool (NewStreamBuffer
);
793 return EFI_OUT_OF_RESOURCES
;
796 Status
= Decompress
->Decompress (
799 CompressionSourceSize
,
801 (UINT32
)NewStreamBufferSize
,
805 FreePool (ScratchBuffer
);
806 if (EFI_ERROR (Status
)) {
808 CoreFreePool (NewStreamBuffer
);
813 NewStreamBuffer
= NULL
;
814 NewStreamBufferSize
= 0;
817 Status
= OpenSectionStreamEx (
821 Stream
->AuthenticationStatus
,
822 &Node
->EncapsulatedStreamHandle
824 if (EFI_ERROR (Status
)) {
826 FreePool (NewStreamBuffer
);
831 case EFI_SECTION_GUID_DEFINED
:
832 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*) SectionHeader
;
833 if (IS_SECTION2 (GuidedHeader
)) {
834 Node
->EncapsulationGuid
= &(((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->SectionDefinitionGuid
);
835 GuidedSectionAttributes
= ((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->Attributes
;
837 Node
->EncapsulationGuid
= &GuidedHeader
->SectionDefinitionGuid
;
838 GuidedSectionAttributes
= GuidedHeader
->Attributes
;
840 Status
= gBS
->LocateProtocol (Node
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
841 if (!EFI_ERROR (Status
)) {
843 // NewStreamBuffer is always allocated by ExtractSection... No caller
846 Status
= GuidedExtraction
->ExtractSection (
850 &NewStreamBufferSize
,
851 &AuthenticationStatus
853 if (EFI_ERROR (Status
)) {
854 FreePool (*ChildNode
);
855 return EFI_PROTOCOL_ERROR
;
859 // Make sure we initialize the new stream with the correct
860 // authentication status for both aggregate and local status fields.
862 if ((GuidedSectionAttributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
864 // OR in the parent stream's aggregate status.
866 AuthenticationStatus
|= Stream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
869 // since there's no authentication data contributed by the section,
870 // just inherit the full value from our immediate parent.
872 AuthenticationStatus
= Stream
->AuthenticationStatus
;
875 Status
= OpenSectionStreamEx (
879 AuthenticationStatus
,
880 &Node
->EncapsulatedStreamHandle
882 if (EFI_ERROR (Status
)) {
883 FreePool (*ChildNode
);
884 FreePool (NewStreamBuffer
);
889 // There's no GUIDed section extraction protocol available.
891 if ((GuidedSectionAttributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) == EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) {
893 // If the section REQUIRES an extraction protocol, register for RPN
894 // when the required GUIDed extraction protocol becomes available.
896 AuthenticationStatus
= 0;
897 CreateGuidedExtractionRpnEvent (Stream
, Node
);
900 // Figure out the proper authentication status
902 AuthenticationStatus
= Stream
->AuthenticationStatus
;
903 if ((GuidedSectionAttributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
905 // The local status of the new stream is contained in
906 // AuthenticaionStatus. This value needs to be ORed into the
907 // Aggregate bits also...
911 // Clear out and initialize the local status
913 AuthenticationStatus
&= ~EFI_LOCAL_AUTH_STATUS_ALL
;
914 AuthenticationStatus
|= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
;
916 // OR local status into aggregate status
918 AuthenticationStatus
|= AuthenticationStatus
>> 16;
921 if (IS_SECTION2 (GuidedHeader
)) {
922 Status
= OpenSectionStreamEx (
923 SECTION2_SIZE (GuidedHeader
) - ((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->DataOffset
,
924 (UINT8
*) GuidedHeader
+ ((EFI_GUID_DEFINED_SECTION2
*) GuidedHeader
)->DataOffset
,
926 AuthenticationStatus
,
927 &Node
->EncapsulatedStreamHandle
930 Status
= OpenSectionStreamEx (
931 SECTION_SIZE (GuidedHeader
) - ((EFI_GUID_DEFINED_SECTION
*) GuidedHeader
)->DataOffset
,
932 (UINT8
*) GuidedHeader
+ ((EFI_GUID_DEFINED_SECTION
*) GuidedHeader
)->DataOffset
,
934 AuthenticationStatus
,
935 &Node
->EncapsulatedStreamHandle
938 if (EFI_ERROR (Status
)) {
945 if ((AuthenticationStatus
& EFI_LOCAL_AUTH_STATUS_ALL
) ==
946 (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
)) {
948 // Need to register for RPN for when the required GUIDed extraction
949 // protocol becomes available. This will enable us to refresh the
950 // AuthenticationStatus cached in the Stream if it's ever requested
953 CreateGuidedExtractionRpnEvent (Stream
, Node
);
961 // Nothing to do if it's a leaf
967 // Last, add the new child node to the stream
969 InsertTailList (&Stream
->Children
, &Node
->Link
);
975 Worker function Recursively searches / builds section stream database
976 looking for requested section.
979 @param SourceStream Indicates the section stream in which to do the search.
980 @param SearchType Indicates the type of section to search for.
981 @param SectionInstance Indicates which instance of section to find. This is
982 an in/out parameter to deal with recursions.
983 @param SectionDefinitionGuid Guid of section definition
984 @param FoundChild Output indicating the child node that is found.
985 @param FoundStream Output indicating which section stream the child was
986 found in. If this stream was generated as a result of
987 an encapsulation section, the streamhandle is visible
988 within the SEP driver only.
989 @param AuthenticationStatus Indicates the authentication status of the found section.
991 @retval EFI_SUCCESS Child node was found and returned.
992 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
993 @retval EFI_NOT_FOUND Requested child node does not exist.
994 @retval EFI_PROTOCOL_ERROR A required GUIDED section extraction protocol does not
1000 IN FRAMEWORK_SECTION_STREAM_NODE
*SourceStream
,
1001 IN EFI_SECTION_TYPE SearchType
,
1002 IN OUT UINTN
*SectionInstance
,
1003 IN EFI_GUID
*SectionDefinitionGuid
,
1004 OUT FRAMEWORK_SECTION_CHILD_NODE
**FoundChild
,
1005 OUT FRAMEWORK_SECTION_STREAM_NODE
**FoundStream
,
1006 OUT UINT32
*AuthenticationStatus
1009 FRAMEWORK_SECTION_CHILD_NODE
*CurrentChildNode
;
1010 FRAMEWORK_SECTION_CHILD_NODE
*RecursedChildNode
;
1011 FRAMEWORK_SECTION_STREAM_NODE
*RecursedFoundStream
;
1012 UINT32 NextChildOffset
;
1013 EFI_STATUS ErrorStatus
;
1016 CurrentChildNode
= NULL
;
1017 ErrorStatus
= EFI_NOT_FOUND
;
1019 if (SourceStream
->StreamLength
== 0) {
1020 return EFI_NOT_FOUND
;
1023 if (IsListEmpty (&SourceStream
->Children
) &&
1024 SourceStream
->StreamLength
>= sizeof (EFI_COMMON_SECTION_HEADER
)) {
1026 // This occurs when a section stream exists, but no child sections
1027 // have been parsed out yet. Therefore, extract the first child and add it
1028 // to the list of children so we can get started.
1029 // Section stream may contain an array of zero or more bytes.
1030 // So, its size should be >= the size of commen section header.
1032 Status
= CreateChildNode (SourceStream
, 0, &CurrentChildNode
);
1033 if (EFI_ERROR (Status
)) {
1039 // At least one child has been parsed out of the section stream. So, walk
1040 // through the sections that have already been parsed out looking for the
1041 // requested section, if necessary, continue parsing section stream and
1042 // adding children until either the requested section is found, or we run
1045 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream
->Children
));
1048 if (ChildIsType (SourceStream
, CurrentChildNode
, SearchType
, SectionDefinitionGuid
)) {
1050 // The type matches, so check the instance count to see if it's the one we want
1052 (*SectionInstance
)--;
1053 if (*SectionInstance
== 0) {
1057 *FoundChild
= CurrentChildNode
;
1058 *FoundStream
= SourceStream
;
1059 *AuthenticationStatus
= SourceStream
->AuthenticationStatus
;
1064 ASSERT (CurrentChildNode
!= NULL
);
1065 if (CurrentChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1067 // If the current node is an encapsulating node, recurse into it...
1069 Status
= FindChildNode (
1070 (FRAMEWORK_SECTION_STREAM_NODE
*)CurrentChildNode
->EncapsulatedStreamHandle
,
1073 SectionDefinitionGuid
,
1075 &RecursedFoundStream
,
1076 AuthenticationStatus
1079 // If the status is not EFI_SUCCESS, just save the error code and continue
1080 // to find the request child node in the rest stream.
1082 if (*SectionInstance
== 0) {
1083 ASSERT_EFI_ERROR (Status
);
1084 *FoundChild
= RecursedChildNode
;
1085 *FoundStream
= RecursedFoundStream
;
1088 ErrorStatus
= Status
;
1090 } else if ((CurrentChildNode
->Type
== EFI_SECTION_GUID_DEFINED
) && (SearchType
!= EFI_SECTION_GUID_DEFINED
)) {
1092 // When Node Type is GUIDED section, but Node has no encapsulated data, Node data should not be parsed
1093 // because a required GUIDED section extraction protocol does not exist.
1094 // If SearchType is not GUIDED section, EFI_PROTOCOL_ERROR should return.
1096 ErrorStatus
= EFI_PROTOCOL_ERROR
;
1099 if (!IsNodeAtEnd (&SourceStream
->Children
, &CurrentChildNode
->Link
)) {
1101 // We haven't found the child node we're interested in yet, but there's
1102 // still more nodes that have already been parsed so get the next one
1103 // and continue searching..
1105 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream
->Children
, &CurrentChildNode
->Link
));
1108 // We've exhausted children that have already been parsed, so see if
1109 // there's any more data and continue parsing out more children if there
1112 NextChildOffset
= CurrentChildNode
->OffsetInStream
+ CurrentChildNode
->Size
;
1114 // Round up to 4 byte boundary
1116 NextChildOffset
+= 3;
1117 NextChildOffset
&= ~(UINTN
)3;
1118 if (NextChildOffset
<= SourceStream
->StreamLength
- sizeof (EFI_COMMON_SECTION_HEADER
)) {
1120 // There's an unparsed child remaining in the stream, so create a new child node
1122 Status
= CreateChildNode (SourceStream
, NextChildOffset
, &CurrentChildNode
);
1123 if (EFI_ERROR (Status
)) {
1127 ASSERT (EFI_ERROR (ErrorStatus
));
1135 Worker function. Search stream database for requested stream handle.
1137 @param SearchHandle Indicates which stream to look for.
1138 @param FoundStream Output pointer to the found stream.
1140 @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains
1142 @retval EFI_NOT_FOUND SearchHandle was not found in the stream database.
1147 IN UINTN SearchHandle
,
1148 OUT FRAMEWORK_SECTION_STREAM_NODE
**FoundStream
1151 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1153 if (!IsListEmpty (&mStreamRoot
)) {
1154 StreamNode
= STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot
));
1156 if (StreamNode
->StreamHandle
== SearchHandle
) {
1157 *FoundStream
= StreamNode
;
1159 } else if (IsNodeAtEnd (&mStreamRoot
, &StreamNode
->Link
)) {
1162 StreamNode
= STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot
, &StreamNode
->Link
));
1167 return EFI_NOT_FOUND
;
1171 SEP member function. Retrieves requested section from section stream.
1173 @param This Pointer to SEP instance.
1174 @param SectionStreamHandle The section stream from which to extract the requested
1176 @param SectionType A pointer to the type of section to search for.
1177 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED, then
1178 SectionDefinitionGuid indicates which of these types
1179 of sections to search for.
1180 @param SectionInstance Indicates which instance of the requested section to
1182 @param Buffer Double indirection to buffer. If *Buffer is non-null on
1183 input, then the buffer is caller allocated. If
1184 *Buffer is NULL, then the buffer is callee allocated.
1185 In either case, the requried buffer size is returned
1187 @param BufferSize On input, indicates the size of *Buffer if *Buffer is
1188 non-null on input. On output, indicates the required
1189 size (allocated size if callee allocated) of *Buffer.
1190 @param AuthenticationStatus Indicates the authentication status of the retrieved
1194 @retval EFI_SUCCESS Section was retrieved successfully
1195 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the section
1196 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
1197 bit set, but there was no corresponding GUIDed Section
1198 Extraction Protocol in the handle database. *Buffer is
1200 @retval EFI_NOT_FOUND An error was encountered when parsing the SectionStream.
1201 This indicates the SectionStream is not correctly
1203 @retval EFI_NOT_FOUND The requested section does not exist.
1204 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process the
1206 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.
1207 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is
1208 insufficient to contain the requested section. The
1209 input buffer is filled and contents are section contents
1216 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
1217 IN UINTN SectionStreamHandle
,
1218 IN EFI_SECTION_TYPE
*SectionType
,
1219 IN EFI_GUID
*SectionDefinitionGuid
,
1220 IN UINTN SectionInstance
,
1222 IN OUT UINTN
*BufferSize
,
1223 OUT UINT32
*AuthenticationStatus
1226 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1229 FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
;
1230 FRAMEWORK_SECTION_STREAM_NODE
*ChildStreamNode
;
1232 UINT32 ExtractedAuthenticationStatus
;
1236 EFI_COMMON_SECTION_HEADER
*Section
;
1239 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1240 Instance
= SectionInstance
+ 1;
1243 // Locate target stream
1245 Status
= FindStreamNode (SectionStreamHandle
, &StreamNode
);
1246 if (EFI_ERROR (Status
)) {
1247 Status
= EFI_INVALID_PARAMETER
;
1248 goto GetSection_Done
;
1252 // Found the stream, now locate and return the appropriate section
1254 if (SectionType
== NULL
) {
1256 // SectionType == NULL means return the WHOLE section stream...
1258 CopySize
= StreamNode
->StreamLength
;
1259 CopyBuffer
= StreamNode
->StreamBuffer
;
1260 *AuthenticationStatus
= StreamNode
->AuthenticationStatus
;
1263 // There's a requested section type, so go find it and return it...
1265 Status
= FindChildNode (
1269 SectionDefinitionGuid
,
1272 &ExtractedAuthenticationStatus
1274 if (EFI_ERROR (Status
)) {
1275 goto GetSection_Done
;
1277 ASSERT (ChildNode
!= NULL
);
1278 ASSERT (ChildStreamNode
!= NULL
);
1279 Section
= (EFI_COMMON_SECTION_HEADER
*) (ChildStreamNode
->StreamBuffer
+ ChildNode
->OffsetInStream
);
1281 if (IS_SECTION2 (Section
)) {
1282 CopySize
= SECTION2_SIZE (Section
) - sizeof (EFI_COMMON_SECTION_HEADER2
);
1283 CopyBuffer
= (UINT8
*) Section
+ sizeof (EFI_COMMON_SECTION_HEADER2
);
1285 CopySize
= SECTION_SIZE (Section
) - sizeof (EFI_COMMON_SECTION_HEADER
);
1286 CopyBuffer
= (UINT8
*) Section
+ sizeof (EFI_COMMON_SECTION_HEADER
);
1288 *AuthenticationStatus
= ExtractedAuthenticationStatus
;
1291 SectionSize
= CopySize
;
1292 if (*Buffer
!= NULL
) {
1294 // Caller allocated buffer. Fill to size and return required size...
1296 if (*BufferSize
< CopySize
) {
1297 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
1298 CopySize
= *BufferSize
;
1302 // Callee allocated buffer. Allocate buffer and return size.
1304 *Buffer
= AllocatePool (CopySize
);
1305 if (*Buffer
== NULL
) {
1306 Status
= EFI_OUT_OF_RESOURCES
;
1307 goto GetSection_Done
;
1310 CopyMem (*Buffer
, CopyBuffer
, CopySize
);
1311 *BufferSize
= SectionSize
;
1314 gBS
->RestoreTPL (OldTpl
);
1319 Worker function. Destructor for child nodes.
1321 @param ChildNode Indicates the node to destroy
1326 IN FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
1329 ASSERT (ChildNode
->Signature
== FRAMEWORK_SECTION_CHILD_SIGNATURE
);
1331 // Remove the child from it's list
1333 RemoveEntryList (&ChildNode
->Link
);
1335 if (ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1337 // If it's an encapsulating section, we close the resulting section stream.
1338 // CloseSectionStream will free all memory associated with the stream.
1340 CloseSectionStream (&mSectionExtraction
, ChildNode
->EncapsulatedStreamHandle
);
1343 // Last, free the child node itself
1345 FreePool (ChildNode
);
1349 SEP member function. Deletes an existing section stream
1351 @param This Indicates the calling context.
1352 @param StreamHandleToClose Indicates the stream to close
1354 @retval EFI_SUCCESS Section stream was closed successfully.
1355 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
1356 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of
1362 CloseSectionStream (
1363 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
1364 IN UINTN StreamHandleToClose
1367 FRAMEWORK_SECTION_STREAM_NODE
*StreamNode
;
1371 FRAMEWORK_SECTION_CHILD_NODE
*ChildNode
;
1373 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1376 // Locate target stream
1378 Status
= FindStreamNode (StreamHandleToClose
, &StreamNode
);
1379 if (!EFI_ERROR (Status
)) {
1381 // Found the stream, so close it
1383 RemoveEntryList (&StreamNode
->Link
);
1384 while (!IsListEmpty (&StreamNode
->Children
)) {
1385 Link
= GetFirstNode (&StreamNode
->Children
);
1386 ChildNode
= CHILD_SECTION_NODE_FROM_LINK (Link
);
1387 FreeChildNode (ChildNode
);
1389 FreePool (StreamNode
->StreamBuffer
);
1390 FreePool (StreamNode
);
1391 Status
= EFI_SUCCESS
;
1393 Status
= EFI_INVALID_PARAMETER
;
1396 gBS
->RestoreTPL (OldTpl
);