3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 CoreSectionExtraction.c
18 Section Extraction Protocol implementation.
20 Stream database is implemented as a linked list of section streams,
21 where each stream contains a linked list of children, which may be leaves or
24 Children that are encapsulations generate new stream entries
25 when they are created. Streams can also be created by calls to
26 SEP->OpenSectionStream().
28 The database is only created far enough to return the requested data from
29 any given stream, or to determine that the requested data is not found.
31 If a GUIDed encapsulation is encountered, there are three possiblilites.
33 1) A support protocol is found, in which the stream is simply processed with
36 2) A support protocol is not found, but the data is available to be read
37 without processing. In this case, the database is built up through the
38 recursions to return the data, and a RPN event is set that will enable
39 the stream in question to be refreshed if and when the required section
40 extraction protocol is published.This insures the AuthenticationStatus
41 does not become stale in the cache.
43 3) A support protocol is not found, and the data is not available to be read
44 without it. This results in EFI_PROTOCOL_ERROR.
51 // Local defines and typedefs
53 #define CORE_SECTION_CHILD_SIGNATURE EFI_SIGNATURE_32('S','X','C','S')
54 #define CHILD_SECTION_NODE_FROM_LINK(Node) \
55 CR (Node, CORE_SECTION_CHILD_NODE, Link, CORE_SECTION_CHILD_SIGNATURE)
63 // StreamBase + OffsetInStream == pointer to section header in stream. The
64 // stream base is always known when walking the sections within.
66 UINT32 OffsetInStream
;
68 // Then EncapsulatedStreamHandle below is always 0 if the section is NOT an
69 // encapsulating section. Otherwise, it contains the stream handle
70 // of the encapsulated stream. This handle is ALWAYS produced any time an
71 // encapsulating child is encountered, irrespective of whether the
72 // encapsulated stream is processed further.
74 UINTN EncapsulatedStreamHandle
;
75 EFI_GUID
*EncapsulationGuid
;
76 } CORE_SECTION_CHILD_NODE
;
78 #define CORE_SECTION_STREAM_SIGNATURE EFI_SIGNATURE_32('S','X','S','S')
79 #define STREAM_NODE_FROM_LINK(Node) \
80 CR (Node, CORE_SECTION_STREAM_NODE, Link, CORE_SECTION_STREAM_SIGNATURE)
90 // Authentication status is from GUIDed encapsulations.
92 UINT32 AuthenticationStatus
;
93 } CORE_SECTION_STREAM_NODE
;
95 #define NULL_STREAM_HANDLE 0
98 CORE_SECTION_CHILD_NODE
*ChildNode
;
99 CORE_SECTION_STREAM_NODE
*ParentStream
;
113 IN CORE_SECTION_STREAM_NODE
*Stream
,
114 IN CORE_SECTION_CHILD_NODE
*Child
,
115 IN EFI_SECTION_TYPE SearchType
,
116 IN EFI_GUID
*SectionDefinitionGuid
122 NotifyGuidedExtraction (
129 CreateGuidedExtractionRpnEvent (
130 IN CORE_SECTION_STREAM_NODE
*ParentStream
,
131 IN CORE_SECTION_CHILD_NODE
*ChildNode
137 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
138 IN UINTN SectionStreamLength
,
139 IN VOID
*SectionStream
,
140 OUT UINTN
*SectionStreamHandle
146 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
147 IN UINTN SectionStreamHandle
,
148 IN EFI_SECTION_TYPE
*SectionType
,
149 IN EFI_GUID
*SectionDefinitionGuid
,
150 IN UINTN SectionInstance
,
152 IN OUT UINTN
*BufferSize
,
153 OUT UINT32
*AuthenticationStatus
159 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
160 IN UINTN StreamHandleToClose
166 IN UINTN SearchHandle
,
167 OUT CORE_SECTION_STREAM_NODE
**FoundStream
173 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
174 IN EFI_SECTION_TYPE SearchType
,
175 IN UINTN
*SectionInstance
,
176 IN EFI_GUID
*SectionDefinitionGuid
,
177 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
178 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
179 OUT UINT32
*AuthenticationStatus
185 IN CORE_SECTION_STREAM_NODE
*Stream
,
186 IN UINT32 ChildOffset
,
187 OUT CORE_SECTION_CHILD_NODE
**ChildNode
193 IN CORE_SECTION_CHILD_NODE
*ChildNode
198 OpenSectionStreamEx (
199 IN UINTN SectionStreamLength
,
200 IN VOID
*SectionStream
,
201 IN BOOLEAN AllocateBuffer
,
202 IN UINT32 AuthenticationStatus
,
203 OUT UINTN
*SectionStreamHandle
208 IsValidSectionStream (
209 IN VOID
*SectionStream
,
210 IN UINTN SectionStreamLength
216 LIST_ENTRY mStreamRoot
= INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot
);
218 EFI_HANDLE mSectionExtractionHandle
= NULL
;
220 EFI_SECTION_EXTRACTION_PROTOCOL mSectionExtraction
= {
229 InitializeSectionExtraction (
230 IN EFI_HANDLE ImageHandle
,
231 IN EFI_SYSTEM_TABLE
*SystemTable
236 Entry point of the section extraction code. Initializes an instance of the
237 section extraction interface and installs it on a new handle.
240 ImageHandle EFI_HANDLE: A handle for the image that is initializing this driver
241 SystemTable EFI_SYSTEM_TABLE: A pointer to the EFI system table
244 EFI_SUCCESS: Driver initialized successfully
245 EFI_OUT_OF_RESOURCES: Could not allocate needed resources
252 // Install SEP to a new handle
254 Status
= CoreInstallProtocolInterface (
255 &mSectionExtractionHandle
,
256 &gEfiSectionExtractionProtocolGuid
,
257 EFI_NATIVE_INTERFACE
,
260 ASSERT_EFI_ERROR (Status
);
269 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
270 IN UINTN SectionStreamLength
,
271 IN VOID
*SectionStream
,
272 OUT UINTN
*SectionStreamHandle
277 SEP member function. This function creates and returns a new section stream
278 handle to represent the new section stream.
281 This - Indicates the calling context.
282 SectionStreamLength - Size in bytes of the section stream.
283 SectionStream - Buffer containing the new section stream.
284 SectionStreamHandle - A pointer to a caller allocated UINTN that on output
285 contains the new section stream handle.
289 EFI_OUT_OF_RESOURCES - memory allocation failed.
290 EFI_INVALID_PARAMETER - section stream does not end concident with end of
296 // Check to see section stream looks good...
298 if (!IsValidSectionStream (SectionStream
, SectionStreamLength
)) {
299 return EFI_INVALID_PARAMETER
;
302 return OpenSectionStreamEx (
315 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
316 IN UINTN SectionStreamHandle
,
317 IN EFI_SECTION_TYPE
*SectionType
,
318 IN EFI_GUID
*SectionDefinitionGuid
,
319 IN UINTN SectionInstance
,
321 IN OUT UINTN
*BufferSize
,
322 OUT UINT32
*AuthenticationStatus
327 SEP member function. Retrieves requested section from section stream.
330 This: Pointer to SEP instance.
331 SectionStreamHandle: The section stream from which to extract the requested
333 SectionType: A pointer to the type of section to search for.
334 SectionDefinitionGuid: If the section type is EFI_SECTION_GUID_DEFINED, then
335 SectionDefinitionGuid indicates which of these types
336 of sections to search for.
337 SectionInstance: Indicates which instance of the requested section to
339 Buffer: Double indirection to buffer. If *Buffer is non-null on
340 input, then the buffer is caller allocated. If
341 *Buffer is NULL, then the buffer is callee allocated.
342 In either case, the requried buffer size is returned
344 BufferSize: On input, indicates the size of *Buffer if *Buffer is
345 non-null on input. On output, indicates the required
346 size (allocated size if callee allocated) of *Buffer.
347 AuthenticationStatus: Indicates the authentication status of the retrieved
351 EFI_SUCCESS: Section was retrieved successfully
352 EFI_PROTOCOL_ERROR: A GUID defined section was encountered in the section
353 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
354 bit set, but there was no corresponding GUIDed Section
355 Extraction Protocol in the handle database. *Buffer is
357 EFI_NOT_FOUND: An error was encountered when parsing the SectionStream.
358 This indicates the SectionStream is not correctly
360 EFI_NOT_FOUND: The requested section does not exist.
361 EFI_OUT_OF_RESOURCES: The system has insufficient resources to process the
363 EFI_INVALID_PARAMETER: The SectionStreamHandle does not exist.
364 EFI_WARN_TOO_SMALL: The size of the caller allocated input buffer is
365 insufficient to contain the requested section. The
366 input buffer is filled and contents are section contents
371 CORE_SECTION_STREAM_NODE
*StreamNode
;
374 CORE_SECTION_CHILD_NODE
*ChildNode
;
375 CORE_SECTION_STREAM_NODE
*ChildStreamNode
;
377 UINT32 ExtractedAuthenticationStatus
;
383 OldTpl
= CoreRaiseTpl (EFI_TPL_NOTIFY
);
384 Instance
= SectionInstance
+ 1;
387 // Locate target stream
389 Status
= FindStreamNode (SectionStreamHandle
, &StreamNode
);
390 if (EFI_ERROR (Status
)) {
391 Status
= EFI_INVALID_PARAMETER
;
392 goto GetSection_Done
;
396 // Found the stream, now locate and return the appropriate section
398 if (SectionType
== NULL
) {
400 // SectionType == NULL means return the WHOLE section stream...
402 CopySize
= StreamNode
->StreamLength
;
403 CopyBuffer
= StreamNode
->StreamBuffer
;
404 *AuthenticationStatus
= StreamNode
->AuthenticationStatus
;
407 // There's a requested section type, so go find it and return it...
409 Status
= FindChildNode (
413 SectionDefinitionGuid
,
416 &ExtractedAuthenticationStatus
418 if (EFI_ERROR (Status
)) {
419 goto GetSection_Done
;
421 CopySize
= ChildNode
->Size
- sizeof (EFI_COMMON_SECTION_HEADER
);
422 CopyBuffer
= ChildStreamNode
->StreamBuffer
+ ChildNode
->OffsetInStream
+ sizeof (EFI_COMMON_SECTION_HEADER
);
423 *AuthenticationStatus
= ExtractedAuthenticationStatus
;
426 SectionSize
= CopySize
;
427 if (*Buffer
!= NULL
) {
429 // Caller allocated buffer. Fill to size and return required size...
431 if (*BufferSize
< CopySize
) {
432 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
433 CopySize
= *BufferSize
;
437 // Callee allocated buffer. Allocate buffer and return size.
439 *Buffer
= CoreAllocateBootServicesPool (CopySize
);
440 if (*Buffer
== NULL
) {
441 Status
= EFI_OUT_OF_RESOURCES
;
442 goto GetSection_Done
;
445 CopyMem (*Buffer
, CopyBuffer
, CopySize
);
446 *BufferSize
= SectionSize
;
449 CoreRestoreTpl (OldTpl
);
458 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
459 IN UINTN StreamHandleToClose
464 SEP member function. Deletes an existing section stream
467 This - Indicates the calling context.
468 StreamHandleToClose - Indicates the stream to close
472 EFI_OUT_OF_RESOURCES - memory allocation failed.
473 EFI_INVALID_PARAMETER - section stream does not end concident with end of
478 CORE_SECTION_STREAM_NODE
*StreamNode
;
482 CORE_SECTION_CHILD_NODE
*ChildNode
;
484 OldTpl
= CoreRaiseTpl (EFI_TPL_NOTIFY
);
487 // Locate target stream
489 Status
= FindStreamNode (StreamHandleToClose
, &StreamNode
);
490 if (!EFI_ERROR (Status
)) {
492 // Found the stream, so close it
494 RemoveEntryList (&StreamNode
->Link
);
495 while (!IsListEmpty (&StreamNode
->Children
)) {
496 Link
= GetFirstNode (&StreamNode
->Children
);
497 ChildNode
= CHILD_SECTION_NODE_FROM_LINK (Link
);
498 FreeChildNode (ChildNode
);
500 CoreFreePool (StreamNode
->StreamBuffer
);
501 CoreFreePool (StreamNode
);
502 Status
= EFI_SUCCESS
;
504 Status
= EFI_INVALID_PARAMETER
;
507 CoreRestoreTpl (OldTpl
);
515 IN CORE_SECTION_STREAM_NODE
*Stream
,
516 IN CORE_SECTION_CHILD_NODE
*Child
,
517 IN EFI_SECTION_TYPE SearchType
,
518 IN EFI_GUID
*SectionDefinitionGuid
523 Worker function. Determine if the input stream:child matches the input type.
526 Stream - Indicates the section stream associated with the child
527 Child - Indicates the child to check
528 SearchType - Indicates the type of section to check against for
529 SectionDefinitionGuid - Indicates the GUID to check against if the type is
530 EFI_SECTION_GUID_DEFINED
532 TRUE - The child matches
533 FALSE - The child doesn't match
537 EFI_GUID_DEFINED_SECTION
*GuidedSection
;
539 if (SearchType
== EFI_SECTION_ALL
) {
542 if (Child
->Type
!= SearchType
) {
545 if (SearchType
!= EFI_SECTION_GUID_DEFINED
) {
548 GuidedSection
= (EFI_GUID_DEFINED_SECTION
* )(Stream
->StreamBuffer
+ Child
->OffsetInStream
);
549 return CompareGuid (&GuidedSection
->SectionDefinitionGuid
, SectionDefinitionGuid
);
556 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
557 IN EFI_SECTION_TYPE SearchType
,
558 IN OUT UINTN
*SectionInstance
,
559 IN EFI_GUID
*SectionDefinitionGuid
,
560 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
561 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
562 OUT UINT32
*AuthenticationStatus
567 Worker function Recursively searches / builds section stream database
568 looking for requested section.
571 SourceStream - Indicates the section stream in which to do the search.
572 SearchType - Indicates the type of section to search for.
573 SectionInstance - Indicates which instance of section to find. This is
574 an in/out parameter to deal with recursions.
575 SectionDefinitionGuid - Guid of section definition
576 FoundChild - Output indicating the child node that is found.
577 FoundStream - Output indicating which section stream the child was
578 found in. If this stream was generated as a result of
579 an encapsulation section, the streamhandle is visible
580 within the SEP driver only.
581 AuthenticationStatus- Indicates the authentication status of the found section.
584 EFI_SUCCESS - Child node was found and returned.
585 EFI_OUT_OF_RESOURCES- Memory allocation failed.
586 EFI_NOT_FOUND - Requested child node does not exist.
587 EFI_PROTOCOL_ERROR - a required GUIDED section extraction protocol does not
592 CORE_SECTION_CHILD_NODE
*CurrentChildNode
;
593 CORE_SECTION_CHILD_NODE
*RecursedChildNode
;
594 CORE_SECTION_STREAM_NODE
*RecursedFoundStream
;
595 UINT32 NextChildOffset
;
596 EFI_STATUS ErrorStatus
;
599 CurrentChildNode
= NULL
;
600 ErrorStatus
= EFI_NOT_FOUND
;
602 if (SourceStream
->StreamLength
== 0) {
603 return EFI_NOT_FOUND
;
606 if (IsListEmpty (&SourceStream
->Children
) &&
607 SourceStream
->StreamLength
> sizeof (EFI_COMMON_SECTION_HEADER
)) {
609 // This occurs when a section stream exists, but no child sections
610 // have been parsed out yet. Therefore, extract the first child and add it
611 // to the list of children so we can get started.
613 Status
= CreateChildNode (SourceStream
, 0, &CurrentChildNode
);
614 if (EFI_ERROR (Status
)) {
620 // At least one child has been parsed out of the section stream. So, walk
621 // through the sections that have already been parsed out looking for the
622 // requested section, if necessary, continue parsing section stream and
623 // adding children until either the requested section is found, or we run
626 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream
->Children
));
629 if (ChildIsType (SourceStream
, CurrentChildNode
, SearchType
, SectionDefinitionGuid
)) {
631 // The type matches, so check the instance count to see if it's the one we want
633 (*SectionInstance
)--;
634 if (*SectionInstance
== 0) {
638 *FoundChild
= CurrentChildNode
;
639 *FoundStream
= SourceStream
;
640 *AuthenticationStatus
= SourceStream
->AuthenticationStatus
;
645 if (CurrentChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
647 // If the current node is an encapsulating node, recurse into it...
649 Status
= FindChildNode (
650 (CORE_SECTION_STREAM_NODE
*)CurrentChildNode
->EncapsulatedStreamHandle
,
653 SectionDefinitionGuid
,
655 &RecursedFoundStream
,
659 // If the status is not EFI_SUCCESS, just save the error code and continue
660 // to find the request child node in the rest stream.
662 if (*SectionInstance
== 0) {
663 ASSERT_EFI_ERROR (Status
);
664 *FoundChild
= RecursedChildNode
;
665 *FoundStream
= RecursedFoundStream
;
668 ErrorStatus
= Status
;
672 if (!IsNodeAtEnd (&SourceStream
->Children
, &CurrentChildNode
->Link
)) {
674 // We haven't found the child node we're interested in yet, but there's
675 // still more nodes that have already been parsed so get the next one
676 // and continue searching..
678 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream
->Children
, &CurrentChildNode
->Link
));
681 // We've exhausted children that have already been parsed, so see if
682 // there's any more data and continue parsing out more children if there
685 NextChildOffset
= CurrentChildNode
->OffsetInStream
+ CurrentChildNode
->Size
;
687 // Round up to 4 byte boundary
689 NextChildOffset
+= 3;
690 NextChildOffset
&= ~(UINTN
)3;
691 if (NextChildOffset
<= SourceStream
->StreamLength
- sizeof (EFI_COMMON_SECTION_HEADER
)) {
693 // There's an unparsed child remaining in the stream, so create a new child node
695 Status
= CreateChildNode (SourceStream
, NextChildOffset
, &CurrentChildNode
);
696 if (EFI_ERROR (Status
)) {
700 ASSERT (EFI_ERROR (ErrorStatus
));
711 IN CORE_SECTION_STREAM_NODE
*Stream
,
712 IN UINT32 ChildOffset
,
713 OUT CORE_SECTION_CHILD_NODE
**ChildNode
718 Worker function. Constructor for new child nodes.
721 Stream - Indicates the section stream in which to add the child.
722 ChildOffset - Indicates the offset in Stream that is the beginning
723 of the child section.
724 ChildNode - Indicates the Callee allocated and initialized child.
727 EFI_SUCCESS - Child node was found and returned.
728 EFI_OUT_OF_RESOURCES- Memory allocation failed.
729 EFI_PROTOCOL_ERROR - Encapsulation sections produce new stream handles when
730 the child node is created. If the section type is GUID
731 defined, and the extraction GUID does not exist, and
732 producing the stream requires the GUID, then a protocol
733 error is generated and no child is produced.
734 Values returned by OpenSectionStreamEx.
739 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
740 EFI_COMPRESSION_SECTION
*CompressionHeader
;
741 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
742 EFI_TIANO_DECOMPRESS_PROTOCOL
*Decompress
;
743 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
744 VOID
*NewStreamBuffer
;
747 UINTN NewStreamBufferSize
;
748 UINT32 AuthenticationStatus
;
749 UINT32 SectionLength
;
751 CORE_SECTION_CHILD_NODE
*Node
;
753 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) (Stream
->StreamBuffer
+ ChildOffset
);
756 // Allocate a new node
758 *ChildNode
= CoreAllocateBootServicesPool (sizeof (CORE_SECTION_CHILD_NODE
));
761 return EFI_OUT_OF_RESOURCES
;
767 Node
->Signature
= CORE_SECTION_CHILD_SIGNATURE
;
768 Node
->Type
= SectionHeader
->Type
;
769 Node
->Size
= SECTION_SIZE (SectionHeader
);
770 Node
->OffsetInStream
= ChildOffset
;
771 Node
->EncapsulatedStreamHandle
= NULL_STREAM_HANDLE
;
772 Node
->EncapsulationGuid
= NULL
;
775 // If it's an encapsulating section, then create the new section stream also
777 switch (Node
->Type
) {
778 case EFI_SECTION_COMPRESSION
:
780 // Get the CompressionSectionHeader
782 ASSERT (Node
->Size
>= sizeof (EFI_COMPRESSION_SECTION
));
784 CompressionHeader
= (EFI_COMPRESSION_SECTION
*) SectionHeader
;
787 // Allocate space for the new stream
789 if (CompressionHeader
->UncompressedLength
> 0) {
790 NewStreamBufferSize
= CompressionHeader
->UncompressedLength
;
791 NewStreamBuffer
= CoreAllocateBootServicesPool (NewStreamBufferSize
);
792 if (NewStreamBuffer
== NULL
) {
794 return EFI_OUT_OF_RESOURCES
;
797 if (CompressionHeader
->CompressionType
== EFI_NOT_COMPRESSED
) {
799 // stream is not actually compressed, just encapsulated. So just copy it.
801 CopyMem (NewStreamBuffer
, CompressionHeader
+ 1, NewStreamBufferSize
);
802 } else if (CompressionHeader
->CompressionType
== EFI_STANDARD_COMPRESSION
||
803 CompressionHeader
->CompressionType
== EFI_CUSTOMIZED_COMPRESSION
) {
805 // Decompress the stream
807 if (CompressionHeader
->CompressionType
== EFI_STANDARD_COMPRESSION
) {
808 Status
= CoreLocateProtocol (&gEfiTianoDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
810 Status
= CoreLocateProtocol (&gEfiCustomizedDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
813 ASSERT_EFI_ERROR (Status
);
815 Status
= Decompress
->GetInfo (
817 CompressionHeader
+ 1,
818 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
819 (UINT32
*)&NewStreamBufferSize
,
822 ASSERT_EFI_ERROR (Status
);
823 ASSERT (NewStreamBufferSize
== CompressionHeader
->UncompressedLength
);
825 ScratchBuffer
= CoreAllocateBootServicesPool (ScratchSize
);
826 if (ScratchBuffer
== NULL
) {
828 CoreFreePool (NewStreamBuffer
);
829 return EFI_OUT_OF_RESOURCES
;
832 Status
= Decompress
->Decompress (
834 CompressionHeader
+ 1,
835 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
837 (UINT32
)NewStreamBufferSize
,
841 ASSERT_EFI_ERROR (Status
);
842 CoreFreePool (ScratchBuffer
);
845 NewStreamBuffer
= NULL
;
846 NewStreamBufferSize
= 0;
849 Status
= OpenSectionStreamEx (
853 Stream
->AuthenticationStatus
,
854 &Node
->EncapsulatedStreamHandle
856 if (EFI_ERROR (Status
)) {
858 CoreFreePool (NewStreamBuffer
);
863 case EFI_SECTION_GUID_DEFINED
:
864 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*) SectionHeader
;
865 Node
->EncapsulationGuid
= &GuidedHeader
->SectionDefinitionGuid
;
866 Status
= CoreLocateProtocol (Node
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
867 if (!EFI_ERROR (Status
)) {
869 // NewStreamBuffer is always allocated by ExtractSection... No caller
872 Status
= GuidedExtraction
->ExtractSection (
876 &NewStreamBufferSize
,
877 &AuthenticationStatus
879 if (EFI_ERROR (Status
)) {
880 CoreFreePool (*ChildNode
);
881 return EFI_PROTOCOL_ERROR
;
885 // Make sure we initialize the new stream with the correct
886 // authentication status for both aggregate and local status fields.
888 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
890 // OR in the parent stream's aggregate status.
892 AuthenticationStatus
|= Stream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
895 // since there's no authentication data contributed by the section,
896 // just inherit the full value from our immediate parent.
898 AuthenticationStatus
= Stream
->AuthenticationStatus
;
901 Status
= OpenSectionStreamEx (
905 AuthenticationStatus
,
906 &Node
->EncapsulatedStreamHandle
908 if (EFI_ERROR (Status
)) {
909 CoreFreePool (*ChildNode
);
910 CoreFreePool (NewStreamBuffer
);
915 // There's no GUIDed section extraction protocol available.
917 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) {
919 // If the section REQUIRES an extraction protocol, then we're toast
921 CoreFreePool (*ChildNode
);
922 return EFI_PROTOCOL_ERROR
;
926 // Figure out the proper authentication status
928 AuthenticationStatus
= Stream
->AuthenticationStatus
;
929 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
931 // The local status of the new stream is contained in
932 // AuthenticaionStatus. This value needs to be ORed into the
933 // Aggregate bits also...
937 // Clear out and initialize the local status
939 AuthenticationStatus
&= ~EFI_LOCAL_AUTH_STATUS_ALL
;
940 AuthenticationStatus
|= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
;
942 // OR local status into aggregate status
944 AuthenticationStatus
|= AuthenticationStatus
>> 16;
947 SectionLength
= SECTION_SIZE (GuidedHeader
);
948 Status
= OpenSectionStreamEx (
949 SectionLength
- GuidedHeader
->DataOffset
,
950 (UINT8
*) GuidedHeader
+ GuidedHeader
->DataOffset
,
952 AuthenticationStatus
,
953 &Node
->EncapsulatedStreamHandle
955 if (EFI_ERROR (Status
)) {
961 if ((AuthenticationStatus
& EFI_LOCAL_AUTH_STATUS_ALL
) ==
962 (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
)) {
964 // Need to register for RPN for when the required GUIDed extraction
965 // protocol becomes available. This will enable us to refresh the
966 // AuthenticationStatus cached in the Stream if it's ever requested
969 CreateGuidedExtractionRpnEvent (Stream
, Node
);
977 // Nothing to do if it's a leaf
983 // Last, add the new child node to the stream
985 InsertTailList (&Stream
->Children
, &Node
->Link
);
993 CreateGuidedExtractionRpnEvent (
994 IN CORE_SECTION_STREAM_NODE
*ParentStream
,
995 IN CORE_SECTION_CHILD_NODE
*ChildNode
1000 Worker function. Constructor for RPN event if needed to keep AuthenticationStatus
1001 cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...
1004 ParentStream - Indicates the parent of the ecnapsulation section (child)
1005 ChildNode - Indicates the child node that is the encapsulation section.
1012 RPN_EVENT_CONTEXT
*Context
;
1015 // Allocate new event structure and context
1017 Context
= CoreAllocateBootServicesPool (sizeof (RPN_EVENT_CONTEXT
));
1018 ASSERT (Context
!= NULL
);
1020 Context
->ChildNode
= ChildNode
;
1021 Context
->ParentStream
= ParentStream
;
1023 Context
->Event
= CoreCreateProtocolNotifyEvent (
1024 Context
->ChildNode
->EncapsulationGuid
,
1026 NotifyGuidedExtraction
,
1028 &Context
->Registration
,
1037 NotifyGuidedExtraction (
1043 Routine Description:
1044 RPN callback function. Removes a stale section stream and re-initializes it
1045 with an updated AuthenticationStatus.
1048 Event - The event that fired
1049 RpnContext - A pointer to the context that allows us to identify
1050 the relevent encapsulation...
1058 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
1059 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
1060 VOID
*NewStreamBuffer
;
1061 UINTN NewStreamBufferSize
;
1062 UINT32 AuthenticationStatus
;
1063 RPN_EVENT_CONTEXT
*Context
;
1065 Context
= RpnContext
;
1067 Status
= CloseSectionStream (&mSectionExtraction
, Context
->ChildNode
->EncapsulatedStreamHandle
);
1068 if (!EFI_ERROR (Status
)) {
1070 // The stream closed successfully, so re-open the stream with correct AuthenticationStatus
1073 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*)
1074 (Context
->ParentStream
->StreamBuffer
+ Context
->ChildNode
->OffsetInStream
);
1075 ASSERT (GuidedHeader
->CommonHeader
.Type
== EFI_SECTION_GUID_DEFINED
);
1077 Status
= CoreLocateProtocol (Context
->ChildNode
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
1078 ASSERT_EFI_ERROR (Status
);
1081 Status
= GuidedExtraction
->ExtractSection (
1085 &NewStreamBufferSize
,
1086 &AuthenticationStatus
1088 ASSERT_EFI_ERROR (Status
);
1090 // OR in the parent stream's aggregagate status.
1092 AuthenticationStatus
|= Context
->ParentStream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
1093 Status
= OpenSectionStreamEx (
1094 NewStreamBufferSize
,
1097 AuthenticationStatus
,
1098 &Context
->ChildNode
->EncapsulatedStreamHandle
1100 ASSERT_EFI_ERROR (Status
);
1104 // If above, the stream did not close successfully, it indicates it's
1105 // alread been closed by someone, so just destroy the event and be done with
1109 CoreCloseEvent (Event
);
1110 CoreFreePool (Context
);
1117 IN CORE_SECTION_CHILD_NODE
*ChildNode
1121 Routine Description:
1122 Worker function. Destructor for child nodes.
1125 ChildNode - Indicates the node to destroy
1132 ASSERT (ChildNode
->Signature
== CORE_SECTION_CHILD_SIGNATURE
);
1134 // Remove the child from it's list
1136 RemoveEntryList (&ChildNode
->Link
);
1138 if (ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1140 // If it's an encapsulating section, we close the resulting section stream.
1141 // CloseSectionStream will free all memory associated with the stream.
1143 CloseSectionStream (&mSectionExtraction
, ChildNode
->EncapsulatedStreamHandle
);
1146 // Last, free the child node itself
1148 CoreFreePool (ChildNode
);
1154 OpenSectionStreamEx (
1155 IN UINTN SectionStreamLength
,
1156 IN VOID
*SectionStream
,
1157 IN BOOLEAN AllocateBuffer
,
1158 IN UINT32 AuthenticationStatus
,
1159 OUT UINTN
*SectionStreamHandle
1163 Routine Description:
1164 Worker function. Constructor for section streams.
1167 SectionStreamLength - Size in bytes of the section stream.
1168 SectionStream - Buffer containing the new section stream.
1169 AllocateBuffer - Indicates whether the stream buffer is to be copied
1170 or the input buffer is to be used in place.
1171 AuthenticationStatus- Indicates the default authentication status for the
1173 SectionStreamHandle - A pointer to a caller allocated section stream handle.
1176 EFI_SUCCESS - Stream was added to stream database.
1177 EFI_OUT_OF_RESOURCES - memory allocation failed.
1181 CORE_SECTION_STREAM_NODE
*NewStream
;
1185 // Allocate a new stream
1187 NewStream
= CoreAllocateBootServicesPool (sizeof (CORE_SECTION_STREAM_NODE
));
1188 if (NewStream
== NULL
) {
1189 return EFI_OUT_OF_RESOURCES
;
1192 if (AllocateBuffer
) {
1194 // if we're here, we're double buffering, allocate the buffer and copy the
1197 if (SectionStreamLength
> 0) {
1198 NewStream
->StreamBuffer
= CoreAllocateBootServicesPool (SectionStreamLength
);
1199 if (NewStream
->StreamBuffer
== NULL
) {
1200 CoreFreePool (NewStream
);
1201 return EFI_OUT_OF_RESOURCES
;
1204 // Copy in stream data
1206 CopyMem (NewStream
->StreamBuffer
, SectionStream
, SectionStreamLength
);
1209 // It's possible to have a zero length section stream.
1211 NewStream
->StreamBuffer
= NULL
;
1215 // If were here, the caller has supplied the buffer (it's an internal call)
1216 // so just assign the buffer. This happens when we open section streams
1217 // as a result of expanding an encapsulating section.
1219 NewStream
->StreamBuffer
= SectionStream
;
1223 // Initialize the rest of the section stream
1225 NewStream
->Signature
= CORE_SECTION_STREAM_SIGNATURE
;
1226 NewStream
->StreamHandle
= (UINTN
) NewStream
;
1227 NewStream
->StreamLength
= SectionStreamLength
;
1228 InitializeListHead (&NewStream
->Children
);
1229 NewStream
->AuthenticationStatus
= AuthenticationStatus
;
1232 // Add new stream to stream list
1234 OldTpl
= CoreRaiseTpl (EFI_TPL_NOTIFY
);
1235 InsertTailList (&mStreamRoot
, &NewStream
->Link
);
1236 CoreRestoreTpl (OldTpl
);
1238 *SectionStreamHandle
= NewStream
->StreamHandle
;
1247 IN UINTN SearchHandle
,
1248 OUT CORE_SECTION_STREAM_NODE
**FoundStream
1252 Routine Description:
1253 Worker function. Search stream database for requested stream handle.
1256 SearchHandle - Indicates which stream to look for.
1257 FoundStream - Output pointer to the found stream.
1260 EFI_SUCCESS - StreamHandle was found and *FoundStream contains
1262 EFI_NOT_FOUND - SearchHandle was not found in the stream database.
1266 CORE_SECTION_STREAM_NODE
*StreamNode
;
1268 if (!IsListEmpty (&mStreamRoot
)) {
1269 StreamNode
= STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot
));
1271 if (StreamNode
->StreamHandle
== SearchHandle
) {
1272 *FoundStream
= StreamNode
;
1274 } else if (IsNodeAtEnd (&mStreamRoot
, &StreamNode
->Link
)) {
1277 StreamNode
= STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot
, &StreamNode
->Link
));
1282 return EFI_NOT_FOUND
;
1288 IsValidSectionStream (
1289 IN VOID
*SectionStream
,
1290 IN UINTN SectionStreamLength
1294 Routine Description:
1295 Check if a stream is valid.
1298 SectionStream - The section stream to be checked
1299 SectionStreamLength - The length of section stream
1308 UINTN SectionLength
;
1309 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
1310 EFI_COMMON_SECTION_HEADER
*NextSectionHeader
;
1313 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*)SectionStream
;
1315 while (TotalLength
< SectionStreamLength
) {
1316 SectionLength
= SECTION_SIZE (SectionHeader
);
1317 TotalLength
+= SectionLength
;
1319 if (TotalLength
== SectionStreamLength
) {
1324 // Move to the next byte following the section...
1326 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) SectionHeader
+ SectionLength
);
1329 // Figure out where the next section begins
1331 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) SectionHeader
+ 3);
1332 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) NextSectionHeader
& ~(UINTN
)3);
1333 TotalLength
+= (UINTN
) NextSectionHeader
- (UINTN
) SectionHeader
;
1334 SectionHeader
= NextSectionHeader
;