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, Intel Corporation
31 All rights reserved. This program and the accompanying materials
32 are licensed and made available under the terms and conditions of the BSD License
33 which accompanies this distribution. The full text of the license may be found at
34 http://opensource.org/licenses/bsd-license.php
36 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
37 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
42 #include "SectionExtraction.h"
45 // Local defines and typedefs
47 #define CORE_SECTION_CHILD_SIGNATURE EFI_SIGNATURE_32('S','X','C','S')
48 #define CHILD_SECTION_NODE_FROM_LINK(Node) \
49 CR (Node, CORE_SECTION_CHILD_NODE, Link, CORE_SECTION_CHILD_SIGNATURE)
57 // StreamBase + OffsetInStream == pointer to section header in stream. The
58 // stream base is always known when walking the sections within.
60 UINT32 OffsetInStream
;
62 // Then EncapsulatedStreamHandle below is always 0 if the section is NOT an
63 // encapsulating section. Otherwise, it contains the stream handle
64 // of the encapsulated stream. This handle is ALWAYS produced any time an
65 // encapsulating child is encountered, irrespective of whether the
66 // encapsulated stream is processed further.
68 UINTN EncapsulatedStreamHandle
;
69 EFI_GUID
*EncapsulationGuid
;
70 } CORE_SECTION_CHILD_NODE
;
72 #define CORE_SECTION_STREAM_SIGNATURE EFI_SIGNATURE_32('S','X','S','S')
73 #define STREAM_NODE_FROM_LINK(Node) \
74 CR (Node, CORE_SECTION_STREAM_NODE, Link, CORE_SECTION_STREAM_SIGNATURE)
84 // Authentication status is from GUIDed encapsulations.
86 UINT32 AuthenticationStatus
;
87 } CORE_SECTION_STREAM_NODE
;
89 #define NULL_STREAM_HANDLE 0
92 CORE_SECTION_CHILD_NODE
*ChildNode
;
93 CORE_SECTION_STREAM_NODE
*ParentStream
;
100 CoreCreateProtocolNotifyEvent (
101 IN EFI_GUID
*ProtocolGuid
,
102 IN EFI_TPL NotifyTpl
,
103 IN EFI_EVENT_NOTIFY NotifyFunction
,
104 IN VOID
*NotifyContext
,
105 OUT VOID
**Registration
,
106 IN BOOLEAN SignalFlag
116 IN CORE_SECTION_STREAM_NODE
*Stream
,
117 IN CORE_SECTION_CHILD_NODE
*Child
,
118 IN EFI_SECTION_TYPE SearchType
,
119 IN EFI_GUID
*SectionDefinitionGuid
125 NotifyGuidedExtraction (
132 CreateGuidedExtractionRpnEvent (
133 IN CORE_SECTION_STREAM_NODE
*ParentStream
,
134 IN CORE_SECTION_CHILD_NODE
*ChildNode
141 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
142 IN UINTN SectionStreamLength
,
143 IN VOID
*SectionStream
,
144 OUT UINTN
*SectionStreamHandle
151 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
152 IN UINTN SectionStreamHandle
,
153 IN EFI_SECTION_TYPE
*SectionType
,
154 IN EFI_GUID
*SectionDefinitionGuid
,
155 IN UINTN SectionInstance
,
157 IN OUT UINTN
*BufferSize
,
158 OUT UINT32
*AuthenticationStatus
165 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
166 IN UINTN StreamHandleToClose
172 IN UINTN SearchHandle
,
173 OUT CORE_SECTION_STREAM_NODE
**FoundStream
179 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
180 IN EFI_SECTION_TYPE SearchType
,
181 IN UINTN
*SectionInstance
,
182 IN EFI_GUID
*SectionDefinitionGuid
,
183 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
184 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
185 OUT UINT32
*AuthenticationStatus
191 IN CORE_SECTION_STREAM_NODE
*Stream
,
192 IN UINT32 ChildOffset
,
193 OUT CORE_SECTION_CHILD_NODE
**ChildNode
199 IN CORE_SECTION_CHILD_NODE
*ChildNode
204 OpenSectionStreamEx (
205 IN UINTN SectionStreamLength
,
206 IN VOID
*SectionStream
,
207 IN BOOLEAN AllocateBuffer
,
208 IN UINT32 AuthenticationStatus
,
209 OUT UINTN
*SectionStreamHandle
214 IsValidSectionStream (
215 IN VOID
*SectionStream
,
216 IN UINTN SectionStreamLength
222 LIST_ENTRY mStreamRoot
= INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot
);
224 EFI_HANDLE mSectionExtractionHandle
= NULL
;
226 EFI_SECTION_EXTRACTION_PROTOCOL mSectionExtraction
= {
233 Entry point of the section extraction code. Initializes an instance of the
234 section extraction interface and installs it on a new handle.
236 @param ImageHandle EFI_HANDLE: A handle for the image that is initializing this driver
237 @param SystemTable EFI_SYSTEM_TABLE: A pointer to the EFI system table
239 @retval EFI_SUCCESS Driver initialized successfully
240 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
245 SectionExtractionEntryPoint (
246 IN EFI_HANDLE ImageHandle
,
247 IN EFI_SYSTEM_TABLE
*SystemTable
253 // Install SEP to a new handle
255 Status
= gBS
->InstallProtocolInterface (
256 &mSectionExtractionHandle
,
257 &gEfiSectionExtractionProtocolGuid
,
258 EFI_NATIVE_INTERFACE
,
261 ASSERT_EFI_ERROR (Status
);
267 SEP member function. This function creates and returns a new section stream
268 handle to represent the new section stream.
270 @param This Indicates the calling context.
271 @param SectionStreamLength Size in bytes of the section stream.
272 @param SectionStream Buffer containing the new section stream.
273 @param SectionStreamHandle A pointer to a caller allocated UINTN that on output
274 contains the new section stream handle.
277 @retval EFI_OUT_OF_RESOURCES memory allocation failed.
278 @retval EFI_INVALID_PARAMETER section stream does not end concident with end of
286 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
287 IN UINTN SectionStreamLength
,
288 IN VOID
*SectionStream
,
289 OUT UINTN
*SectionStreamHandle
293 // Check to see section stream looks good...
295 if (!IsValidSectionStream (SectionStream
, SectionStreamLength
)) {
296 return EFI_INVALID_PARAMETER
;
299 return OpenSectionStreamEx (
309 SEP member function. Retrieves requested section from section stream.
311 @param This: Pointer to SEP instance.
312 @param SectionStreamHandle: The section stream from which to extract the requested
314 @param SectionType: A pointer to the type of section to search for.
315 @param SectionDefinitionGuid: If the section type is EFI_SECTION_GUID_DEFINED, then
316 SectionDefinitionGuid indicates which of these types
317 of sections to search for.
318 @param SectionInstance: Indicates which instance of the requested section to
320 @param Buffer: Double indirection to buffer. If *Buffer is non-null on
321 input, then the buffer is caller allocated. If
322 *Buffer is NULL, then the buffer is callee allocated.
323 In either case, the requried buffer size is returned
325 @param BufferSize: On input, indicates the size of *Buffer if *Buffer is
326 non-null on input. On output, indicates the required
327 size (allocated size if callee allocated) of *Buffer.
328 @param AuthenticationStatus: Indicates the authentication status of the retrieved
332 @retval EFI_SUCCESS: Section was retrieved successfully
333 @retval EFI_PROTOCOL_ERROR: A GUID defined section was encountered in the section
334 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
335 bit set, but there was no corresponding GUIDed Section
336 Extraction Protocol in the handle database. *Buffer is
338 @retval EFI_NOT_FOUND: An error was encountered when parsing the SectionStream.
339 This indicates the SectionStream is not correctly
341 @retval EFI_NOT_FOUND: The requested section does not exist.
342 @retval EFI_OUT_OF_RESOURCES: The system has insufficient resources to process the
344 @retval EFI_INVALID_PARAMETER: The SectionStreamHandle does not exist.
345 @retval EFI_WARN_TOO_SMALL: The size of the caller allocated input buffer is
346 insufficient to contain the requested section. The
347 input buffer is filled and contents are section contents
355 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
356 IN UINTN SectionStreamHandle
,
357 IN EFI_SECTION_TYPE
*SectionType
,
358 IN EFI_GUID
*SectionDefinitionGuid
,
359 IN UINTN SectionInstance
,
361 IN OUT UINTN
*BufferSize
,
362 OUT UINT32
*AuthenticationStatus
366 CORE_SECTION_STREAM_NODE
*StreamNode
;
369 CORE_SECTION_CHILD_NODE
*ChildNode
;
370 CORE_SECTION_STREAM_NODE
*ChildStreamNode
;
372 UINT32 ExtractedAuthenticationStatus
;
378 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
379 Instance
= SectionInstance
+ 1;
382 // Locate target stream
384 Status
= FindStreamNode (SectionStreamHandle
, &StreamNode
);
385 if (EFI_ERROR (Status
)) {
386 Status
= EFI_INVALID_PARAMETER
;
387 goto GetSection_Done
;
391 // Found the stream, now locate and return the appropriate section
393 if (SectionType
== NULL
) {
395 // SectionType == NULL means return the WHOLE section stream...
397 CopySize
= StreamNode
->StreamLength
;
398 CopyBuffer
= StreamNode
->StreamBuffer
;
399 *AuthenticationStatus
= StreamNode
->AuthenticationStatus
;
402 // There's a requested section type, so go find it and return it...
404 Status
= FindChildNode (
408 SectionDefinitionGuid
,
411 &ExtractedAuthenticationStatus
413 if (EFI_ERROR (Status
)) {
414 goto GetSection_Done
;
416 CopySize
= ChildNode
->Size
- sizeof (EFI_COMMON_SECTION_HEADER
);
417 CopyBuffer
= ChildStreamNode
->StreamBuffer
+ ChildNode
->OffsetInStream
+ sizeof (EFI_COMMON_SECTION_HEADER
);
418 *AuthenticationStatus
= ExtractedAuthenticationStatus
;
421 SectionSize
= CopySize
;
422 if (*Buffer
!= NULL
) {
424 // Caller allocated buffer. Fill to size and return required size...
426 if (*BufferSize
< CopySize
) {
427 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
428 CopySize
= *BufferSize
;
432 // Callee allocated buffer. Allocate buffer and return size.
434 *Buffer
= AllocatePool (CopySize
);
435 if (*Buffer
== NULL
) {
436 Status
= EFI_OUT_OF_RESOURCES
;
437 goto GetSection_Done
;
440 CopyMem (*Buffer
, CopyBuffer
, CopySize
);
441 *BufferSize
= SectionSize
;
444 gBS
->RestoreTPL (OldTpl
);
449 SEP member function. Deletes an existing section stream
451 @param This - Indicates the calling context.
452 @param StreamHandleToClose - Indicates the stream to close
455 @retval EFI_OUT_OF_RESOURCES - memory allocation failed.
456 @retval EFI_INVALID_PARAMETER - section stream does not end concident with end of
464 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
465 IN UINTN StreamHandleToClose
469 CORE_SECTION_STREAM_NODE
*StreamNode
;
473 CORE_SECTION_CHILD_NODE
*ChildNode
;
475 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
478 // Locate target stream
480 Status
= FindStreamNode (StreamHandleToClose
, &StreamNode
);
481 if (!EFI_ERROR (Status
)) {
483 // Found the stream, so close it
485 RemoveEntryList (&StreamNode
->Link
);
486 while (!IsListEmpty (&StreamNode
->Children
)) {
487 Link
= GetFirstNode (&StreamNode
->Children
);
488 ChildNode
= CHILD_SECTION_NODE_FROM_LINK (Link
);
489 FreeChildNode (ChildNode
);
491 gBS
->FreePool (StreamNode
->StreamBuffer
);
492 gBS
->FreePool (StreamNode
);
493 Status
= EFI_SUCCESS
;
495 Status
= EFI_INVALID_PARAMETER
;
498 gBS
->RestoreTPL (OldTpl
);
503 Worker function. Determine if the input stream:child matches the input type.
505 @param Stream - Indicates the section stream associated with the child
506 @param Child - Indicates the child to check
507 @param SearchType - Indicates the type of section to check against for
508 @param SectionDefinitionGuid - Indicates the GUID to check against if the type is
509 EFI_SECTION_GUID_DEFINED
511 @retval TRUE - The child matches
512 @retval FALSE - The child doesn't match
518 IN CORE_SECTION_STREAM_NODE
*Stream
,
519 IN CORE_SECTION_CHILD_NODE
*Child
,
520 IN EFI_SECTION_TYPE SearchType
,
521 IN EFI_GUID
*SectionDefinitionGuid
524 EFI_GUID_DEFINED_SECTION
*GuidedSection
;
526 if (SearchType
== EFI_SECTION_ALL
) {
529 if (Child
->Type
!= SearchType
) {
532 if (SearchType
!= EFI_SECTION_GUID_DEFINED
) {
535 GuidedSection
= (EFI_GUID_DEFINED_SECTION
* )(Stream
->StreamBuffer
+ Child
->OffsetInStream
);
536 return CompareGuid (&GuidedSection
->SectionDefinitionGuid
, SectionDefinitionGuid
);
540 Worker function Recursively searches / builds section stream database
541 looking for requested section.
544 @param SourceStream - Indicates the section stream in which to do the search.
545 @param SearchType - Indicates the type of section to search for.
546 @param SectionInstance - Indicates which instance of section to find. This is
547 an in/out parameter to deal with recursions.
548 @param SectionDefinitionGuid - Guid of section definition
549 @param FoundChild - Output indicating the child node that is found.
550 @param FoundStream - Output indicating which section stream the child was
551 found in. If this stream was generated as a result of
552 an encapsulation section, the streamhandle is visible
553 within the SEP driver only.
554 @param AuthenticationStatus- Indicates the authentication status of the found section.
556 @retval EFI_SUCCESS - Child node was found and returned.
557 @retval EFI_OUT_OF_RESOURCES- Memory allocation failed.
558 @retval EFI_NOT_FOUND - Requested child node does not exist.
559 @retval EFI_PROTOCOL_ERROR - a required GUIDED section extraction protocol does not
566 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
567 IN EFI_SECTION_TYPE SearchType
,
568 IN OUT UINTN
*SectionInstance
,
569 IN EFI_GUID
*SectionDefinitionGuid
,
570 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
571 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
572 OUT UINT32
*AuthenticationStatus
576 CORE_SECTION_CHILD_NODE
*CurrentChildNode
;
577 CORE_SECTION_CHILD_NODE
*RecursedChildNode
;
578 CORE_SECTION_STREAM_NODE
*RecursedFoundStream
;
579 UINT32 NextChildOffset
;
580 EFI_STATUS ErrorStatus
;
583 CurrentChildNode
= NULL
;
584 ErrorStatus
= EFI_NOT_FOUND
;
586 if (SourceStream
->StreamLength
== 0) {
587 return EFI_NOT_FOUND
;
590 if (IsListEmpty (&SourceStream
->Children
) &&
591 SourceStream
->StreamLength
> sizeof (EFI_COMMON_SECTION_HEADER
)) {
593 // This occurs when a section stream exists, but no child sections
594 // have been parsed out yet. Therefore, extract the first child and add it
595 // to the list of children so we can get started.
597 Status
= CreateChildNode (SourceStream
, 0, &CurrentChildNode
);
598 if (EFI_ERROR (Status
)) {
604 // At least one child has been parsed out of the section stream. So, walk
605 // through the sections that have already been parsed out looking for the
606 // requested section, if necessary, continue parsing section stream and
607 // adding children until either the requested section is found, or we run
610 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream
->Children
));
613 if (ChildIsType (SourceStream
, CurrentChildNode
, SearchType
, SectionDefinitionGuid
)) {
615 // The type matches, so check the instance count to see if it's the one we want
617 (*SectionInstance
)--;
618 if (*SectionInstance
== 0) {
622 *FoundChild
= CurrentChildNode
;
623 *FoundStream
= SourceStream
;
624 *AuthenticationStatus
= SourceStream
->AuthenticationStatus
;
629 if (CurrentChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
631 // If the current node is an encapsulating node, recurse into it...
633 Status
= FindChildNode (
634 (CORE_SECTION_STREAM_NODE
*)CurrentChildNode
->EncapsulatedStreamHandle
,
637 SectionDefinitionGuid
,
639 &RecursedFoundStream
,
643 // If the status is not EFI_SUCCESS, just save the error code and continue
644 // to find the request child node in the rest stream.
646 if (*SectionInstance
== 0) {
647 ASSERT_EFI_ERROR (Status
);
648 *FoundChild
= RecursedChildNode
;
649 *FoundStream
= RecursedFoundStream
;
652 ErrorStatus
= Status
;
656 if (!IsNodeAtEnd (&SourceStream
->Children
, &CurrentChildNode
->Link
)) {
658 // We haven't found the child node we're interested in yet, but there's
659 // still more nodes that have already been parsed so get the next one
660 // and continue searching..
662 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream
->Children
, &CurrentChildNode
->Link
));
665 // We've exhausted children that have already been parsed, so see if
666 // there's any more data and continue parsing out more children if there
669 NextChildOffset
= CurrentChildNode
->OffsetInStream
+ CurrentChildNode
->Size
;
671 // Round up to 4 byte boundary
673 NextChildOffset
+= 3;
674 NextChildOffset
&= ~(UINTN
)3;
675 if (NextChildOffset
<= SourceStream
->StreamLength
- sizeof (EFI_COMMON_SECTION_HEADER
)) {
677 // There's an unparsed child remaining in the stream, so create a new child node
679 Status
= CreateChildNode (SourceStream
, NextChildOffset
, &CurrentChildNode
);
680 if (EFI_ERROR (Status
)) {
684 ASSERT (EFI_ERROR (ErrorStatus
));
692 Worker function. Constructor for new child nodes.
694 @param Stream - Indicates the section stream in which to add the child.
695 @param ChildOffset - Indicates the offset in Stream that is the beginning
696 of the child section.
697 @param ChildNode - Indicates the Callee allocated and initialized child.
699 @retval EFI_SUCCESS - Child node was found and returned.
700 @retval EFI_OUT_OF_RESOURCES- Memory allocation failed.
701 @retval EFI_PROTOCOL_ERROR - Encapsulation sections produce new stream handles when
702 the child node is created. If the section type is GUID
703 defined, and the extraction GUID does not exist, and
704 producing the stream requires the GUID, then a protocol
705 error is generated and no child is produced.
706 Values returned by OpenSectionStreamEx.
712 IN CORE_SECTION_STREAM_NODE
*Stream
,
713 IN UINT32 ChildOffset
,
714 OUT CORE_SECTION_CHILD_NODE
**ChildNode
718 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
719 EFI_COMPRESSION_SECTION
*CompressionHeader
;
720 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
721 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
722 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
723 VOID
*NewStreamBuffer
;
726 UINTN NewStreamBufferSize
;
727 UINT32 AuthenticationStatus
;
728 UINT32 SectionLength
;
730 CORE_SECTION_CHILD_NODE
*Node
;
732 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) (Stream
->StreamBuffer
+ ChildOffset
);
735 // Allocate a new node
737 *ChildNode
= AllocatePool (sizeof (CORE_SECTION_CHILD_NODE
));
740 return EFI_OUT_OF_RESOURCES
;
746 Node
->Signature
= CORE_SECTION_CHILD_SIGNATURE
;
747 Node
->Type
= SectionHeader
->Type
;
748 Node
->Size
= SECTION_SIZE (SectionHeader
);
749 Node
->OffsetInStream
= ChildOffset
;
750 Node
->EncapsulatedStreamHandle
= NULL_STREAM_HANDLE
;
751 Node
->EncapsulationGuid
= NULL
;
754 // If it's an encapsulating section, then create the new section stream also
756 switch (Node
->Type
) {
757 case EFI_SECTION_COMPRESSION
:
759 // Get the CompressionSectionHeader
761 ASSERT (Node
->Size
>= sizeof (EFI_COMPRESSION_SECTION
));
763 CompressionHeader
= (EFI_COMPRESSION_SECTION
*) SectionHeader
;
766 // Allocate space for the new stream
768 if (CompressionHeader
->UncompressedLength
> 0) {
769 NewStreamBufferSize
= CompressionHeader
->UncompressedLength
;
770 NewStreamBuffer
= AllocatePool (NewStreamBufferSize
);
771 if (NewStreamBuffer
== NULL
) {
772 gBS
->FreePool (Node
);
773 return EFI_OUT_OF_RESOURCES
;
776 if (CompressionHeader
->CompressionType
== EFI_NOT_COMPRESSED
) {
778 // stream is not actually compressed, just encapsulated. So just copy it.
780 CopyMem (NewStreamBuffer
, CompressionHeader
+ 1, NewStreamBufferSize
);
781 } else if (CompressionHeader
->CompressionType
== EFI_STANDARD_COMPRESSION
) {
783 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
787 // Decompress the stream
789 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
791 ASSERT_EFI_ERROR (Status
);
793 Status
= Decompress
->GetInfo (
795 CompressionHeader
+ 1,
796 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
797 (UINT32
*)&NewStreamBufferSize
,
800 ASSERT_EFI_ERROR (Status
);
801 ASSERT (NewStreamBufferSize
== CompressionHeader
->UncompressedLength
);
803 ScratchBuffer
= AllocatePool (ScratchSize
);
804 if (ScratchBuffer
== NULL
) {
805 gBS
->FreePool (Node
);
806 gBS
->FreePool (NewStreamBuffer
);
807 return EFI_OUT_OF_RESOURCES
;
810 Status
= Decompress
->Decompress (
812 CompressionHeader
+ 1,
813 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
815 (UINT32
)NewStreamBufferSize
,
819 ASSERT_EFI_ERROR (Status
);
820 gBS
->FreePool (ScratchBuffer
);
823 NewStreamBuffer
= NULL
;
824 NewStreamBufferSize
= 0;
827 Status
= OpenSectionStreamEx (
831 Stream
->AuthenticationStatus
,
832 &Node
->EncapsulatedStreamHandle
834 if (EFI_ERROR (Status
)) {
835 gBS
->FreePool (Node
);
836 gBS
->FreePool (NewStreamBuffer
);
841 case EFI_SECTION_GUID_DEFINED
:
842 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*) SectionHeader
;
843 Node
->EncapsulationGuid
= &GuidedHeader
->SectionDefinitionGuid
;
844 Status
= gBS
->LocateProtocol (Node
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
845 if (!EFI_ERROR (Status
)) {
847 // NewStreamBuffer is always allocated by ExtractSection... No caller
850 Status
= GuidedExtraction
->ExtractSection (
854 &NewStreamBufferSize
,
855 &AuthenticationStatus
857 if (EFI_ERROR (Status
)) {
858 gBS
->FreePool (*ChildNode
);
859 return EFI_PROTOCOL_ERROR
;
863 // Make sure we initialize the new stream with the correct
864 // authentication status for both aggregate and local status fields.
866 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
868 // OR in the parent stream's aggregate status.
870 AuthenticationStatus
|= Stream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
873 // since there's no authentication data contributed by the section,
874 // just inherit the full value from our immediate parent.
876 AuthenticationStatus
= Stream
->AuthenticationStatus
;
879 Status
= OpenSectionStreamEx (
883 AuthenticationStatus
,
884 &Node
->EncapsulatedStreamHandle
886 if (EFI_ERROR (Status
)) {
887 gBS
->FreePool (*ChildNode
);
888 gBS
->FreePool (NewStreamBuffer
);
893 // There's no GUIDed section extraction protocol available.
895 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) {
897 // If the section REQUIRES an extraction protocol, then we're toast
899 gBS
->FreePool (*ChildNode
);
900 return EFI_PROTOCOL_ERROR
;
904 // Figure out the proper authentication status
906 AuthenticationStatus
= Stream
->AuthenticationStatus
;
907 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
909 // The local status of the new stream is contained in
910 // AuthenticaionStatus. This value needs to be ORed into the
911 // Aggregate bits also...
915 // Clear out and initialize the local status
917 AuthenticationStatus
&= ~EFI_LOCAL_AUTH_STATUS_ALL
;
918 AuthenticationStatus
|= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
;
920 // OR local status into aggregate status
922 AuthenticationStatus
|= AuthenticationStatus
>> 16;
925 SectionLength
= SECTION_SIZE (GuidedHeader
);
926 Status
= OpenSectionStreamEx (
927 SectionLength
- GuidedHeader
->DataOffset
,
928 (UINT8
*) GuidedHeader
+ GuidedHeader
->DataOffset
,
930 AuthenticationStatus
,
931 &Node
->EncapsulatedStreamHandle
933 if (EFI_ERROR (Status
)) {
934 gBS
->FreePool (Node
);
939 if ((AuthenticationStatus
& EFI_LOCAL_AUTH_STATUS_ALL
) ==
940 (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
)) {
942 // Need to register for RPN for when the required GUIDed extraction
943 // protocol becomes available. This will enable us to refresh the
944 // AuthenticationStatus cached in the Stream if it's ever requested
947 CreateGuidedExtractionRpnEvent (Stream
, Node
);
955 // Nothing to do if it's a leaf
961 // Last, add the new child node to the stream
963 InsertTailList (&Stream
->Children
, &Node
->Link
);
969 Worker function. Constructor for RPN event if needed to keep AuthenticationStatus
970 cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...
972 @param ParentStream - Indicates the parent of the ecnapsulation section (child)
973 @param ChildNode - Indicates the child node that is the encapsulation section.
978 CreateGuidedExtractionRpnEvent (
979 IN CORE_SECTION_STREAM_NODE
*ParentStream
,
980 IN CORE_SECTION_CHILD_NODE
*ChildNode
983 RPN_EVENT_CONTEXT
*Context
;
986 // Allocate new event structure and context
988 Context
= AllocatePool (sizeof (RPN_EVENT_CONTEXT
));
989 ASSERT (Context
!= NULL
);
991 Context
->ChildNode
= ChildNode
;
992 Context
->ParentStream
= ParentStream
;
994 Context
->Event
= CoreCreateProtocolNotifyEvent (
995 Context
->ChildNode
->EncapsulationGuid
,
997 NotifyGuidedExtraction
,
999 &Context
->Registration
,
1005 RPN callback function. Removes a stale section stream and re-initializes it
1006 with an updated AuthenticationStatus.
1008 @param Event - The event that fired
1009 @param RpnContext - A pointer to the context that allows us to identify
1010 the relevent encapsulation...
1016 NotifyGuidedExtraction (
1022 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
1023 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
1024 VOID
*NewStreamBuffer
;
1025 UINTN NewStreamBufferSize
;
1026 UINT32 AuthenticationStatus
;
1027 RPN_EVENT_CONTEXT
*Context
;
1029 Context
= RpnContext
;
1031 Status
= CloseSectionStream (&mSectionExtraction
, Context
->ChildNode
->EncapsulatedStreamHandle
);
1032 if (!EFI_ERROR (Status
)) {
1034 // The stream closed successfully, so re-open the stream with correct AuthenticationStatus
1037 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*)
1038 (Context
->ParentStream
->StreamBuffer
+ Context
->ChildNode
->OffsetInStream
);
1039 ASSERT (GuidedHeader
->CommonHeader
.Type
== EFI_SECTION_GUID_DEFINED
);
1041 Status
= gBS
->LocateProtocol (Context
->ChildNode
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
1042 ASSERT_EFI_ERROR (Status
);
1045 Status
= GuidedExtraction
->ExtractSection (
1049 &NewStreamBufferSize
,
1050 &AuthenticationStatus
1052 ASSERT_EFI_ERROR (Status
);
1054 // OR in the parent stream's aggregagate status.
1056 AuthenticationStatus
|= Context
->ParentStream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
1057 Status
= OpenSectionStreamEx (
1058 NewStreamBufferSize
,
1061 AuthenticationStatus
,
1062 &Context
->ChildNode
->EncapsulatedStreamHandle
1064 ASSERT_EFI_ERROR (Status
);
1068 // If above, the stream did not close successfully, it indicates it's
1069 // alread been closed by someone, so just destroy the event and be done with
1073 gBS
->CloseEvent (Event
);
1074 gBS
->FreePool (Context
);
1078 Worker function. Destructor for child nodes.
1080 @param ChildNode - Indicates the node to destroy
1086 IN CORE_SECTION_CHILD_NODE
*ChildNode
1090 ASSERT (ChildNode
->Signature
== CORE_SECTION_CHILD_SIGNATURE
);
1092 // Remove the child from it's list
1094 RemoveEntryList (&ChildNode
->Link
);
1096 if (ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1098 // If it's an encapsulating section, we close the resulting section stream.
1099 // CloseSectionStream will free all memory associated with the stream.
1101 CloseSectionStream (&mSectionExtraction
, ChildNode
->EncapsulatedStreamHandle
);
1104 // Last, free the child node itself
1106 gBS
->FreePool (ChildNode
);
1110 Worker function. Constructor for section streams.
1112 @param SectionStreamLength - Size in bytes of the section stream.
1113 @param SectionStream - Buffer containing the new section stream.
1114 @param AllocateBuffer - Indicates whether the stream buffer is to be copied
1115 or the input buffer is to be used in place.
1116 @param AuthenticationStatus- Indicates the default authentication status for the
1118 @param SectionStreamHandle - A pointer to a caller allocated section stream handle.
1121 @retval EFI_SUCCESS - Stream was added to stream database.
1122 @retval EFI_OUT_OF_RESOURCES - memory allocation failed.
1127 OpenSectionStreamEx (
1128 IN UINTN SectionStreamLength
,
1129 IN VOID
*SectionStream
,
1130 IN BOOLEAN AllocateBuffer
,
1131 IN UINT32 AuthenticationStatus
,
1132 OUT UINTN
*SectionStreamHandle
1136 CORE_SECTION_STREAM_NODE
*NewStream
;
1140 // Allocate a new stream
1142 NewStream
= AllocatePool (sizeof (CORE_SECTION_STREAM_NODE
));
1143 if (NewStream
== NULL
) {
1144 return EFI_OUT_OF_RESOURCES
;
1147 if (AllocateBuffer
) {
1149 // if we're here, we're double buffering, allocate the buffer and copy the
1152 if (SectionStreamLength
> 0) {
1153 NewStream
->StreamBuffer
= AllocatePool (SectionStreamLength
);
1154 if (NewStream
->StreamBuffer
== NULL
) {
1155 gBS
->FreePool (NewStream
);
1156 return EFI_OUT_OF_RESOURCES
;
1159 // Copy in stream data
1161 CopyMem (NewStream
->StreamBuffer
, SectionStream
, SectionStreamLength
);
1164 // It's possible to have a zero length section stream.
1166 NewStream
->StreamBuffer
= NULL
;
1170 // If were here, the caller has supplied the buffer (it's an internal call)
1171 // so just assign the buffer. This happens when we open section streams
1172 // as a result of expanding an encapsulating section.
1174 NewStream
->StreamBuffer
= SectionStream
;
1178 // Initialize the rest of the section stream
1180 NewStream
->Signature
= CORE_SECTION_STREAM_SIGNATURE
;
1181 NewStream
->StreamHandle
= (UINTN
) NewStream
;
1182 NewStream
->StreamLength
= SectionStreamLength
;
1183 InitializeListHead (&NewStream
->Children
);
1184 NewStream
->AuthenticationStatus
= AuthenticationStatus
;
1187 // Add new stream to stream list
1189 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1190 InsertTailList (&mStreamRoot
, &NewStream
->Link
);
1191 gBS
->RestoreTPL (OldTpl
);
1193 *SectionStreamHandle
= NewStream
->StreamHandle
;
1199 Worker function. Search stream database for requested stream handle.
1201 @param SearchHandle - Indicates which stream to look for.
1202 @param FoundStream - Output pointer to the found stream.
1204 @retval EFI_SUCCESS - StreamHandle was found and *FoundStream contains
1206 @retval EFI_NOT_FOUND - SearchHandle was not found in the stream database.
1212 IN UINTN SearchHandle
,
1213 OUT CORE_SECTION_STREAM_NODE
**FoundStream
1217 CORE_SECTION_STREAM_NODE
*StreamNode
;
1219 if (!IsListEmpty (&mStreamRoot
)) {
1220 StreamNode
= STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot
));
1222 if (StreamNode
->StreamHandle
== SearchHandle
) {
1223 *FoundStream
= StreamNode
;
1225 } else if (IsNodeAtEnd (&mStreamRoot
, &StreamNode
->Link
)) {
1228 StreamNode
= STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot
, &StreamNode
->Link
));
1233 return EFI_NOT_FOUND
;
1238 Check if a stream is valid.
1240 @param SectionStream - The section stream to be checked
1241 @param SectionStreamLength - The length of section stream
1243 @return if a stream is valid.
1247 IsValidSectionStream (
1248 IN VOID
*SectionStream
,
1249 IN UINTN SectionStreamLength
1254 UINTN SectionLength
;
1255 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
1256 EFI_COMMON_SECTION_HEADER
*NextSectionHeader
;
1259 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*)SectionStream
;
1261 while (TotalLength
< SectionStreamLength
) {
1262 SectionLength
= SECTION_SIZE (SectionHeader
);
1263 TotalLength
+= SectionLength
;
1265 if (TotalLength
== SectionStreamLength
) {
1270 // Move to the next byte following the section...
1272 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) SectionHeader
+ SectionLength
);
1275 // Figure out where the next section begins
1277 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) SectionHeader
+ 3);
1278 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) NextSectionHeader
& ~(UINTN
)3);
1279 TotalLength
+= (UINTN
) NextSectionHeader
- (UINTN
) SectionHeader
;
1280 SectionHeader
= NextSectionHeader
;
1288 Create a protocol notification event and return it.
1290 @param ProtocolGuid - Protocol to register notification event on.
1292 @param NotifyTpl - Maximum TPL to signal the NotifyFunction.
1294 @param NotifyFuncition - EFI notification routine.
1296 @param NotifyContext - Context passed into Event when it is created.
1298 @param Registration - Registration key returned from RegisterProtocolNotify().
1300 @param SignalFlag - Boolean value to decide whether kick the event after register or not.
1302 @return The EFI_EVENT that has been registered to be signaled when a ProtocolGuid
1303 is added to the system.
1307 CoreCreateProtocolNotifyEvent (
1308 IN EFI_GUID
*ProtocolGuid
,
1309 IN EFI_TPL NotifyTpl
,
1310 IN EFI_EVENT_NOTIFY NotifyFunction
,
1311 IN VOID
*NotifyContext
,
1312 OUT VOID
**Registration
,
1313 IN BOOLEAN SignalFlag
1323 Status
= gBS
->CreateEvent (
1330 ASSERT_EFI_ERROR (Status
);
1333 // Register for protocol notifactions on this event
1336 Status
= gBS
->RegisterProtocolNotify (
1341 ASSERT_EFI_ERROR (Status
);
1345 // Kick the event so we will perform an initial pass of
1346 // current installed drivers
1348 gBS
->SignalEvent (Event
);