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
138 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
139 IN UINTN SectionStreamLength
,
140 IN VOID
*SectionStream
,
141 OUT UINTN
*SectionStreamHandle
148 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
149 IN UINTN SectionStreamHandle
,
150 IN EFI_SECTION_TYPE
*SectionType
,
151 IN EFI_GUID
*SectionDefinitionGuid
,
152 IN UINTN SectionInstance
,
154 IN OUT UINTN
*BufferSize
,
155 OUT UINT32
*AuthenticationStatus
162 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
163 IN UINTN StreamHandleToClose
169 IN UINTN SearchHandle
,
170 OUT CORE_SECTION_STREAM_NODE
**FoundStream
176 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
177 IN EFI_SECTION_TYPE SearchType
,
178 IN UINTN
*SectionInstance
,
179 IN EFI_GUID
*SectionDefinitionGuid
,
180 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
181 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
182 OUT UINT32
*AuthenticationStatus
188 IN CORE_SECTION_STREAM_NODE
*Stream
,
189 IN UINT32 ChildOffset
,
190 OUT CORE_SECTION_CHILD_NODE
**ChildNode
196 IN CORE_SECTION_CHILD_NODE
*ChildNode
201 OpenSectionStreamEx (
202 IN UINTN SectionStreamLength
,
203 IN VOID
*SectionStream
,
204 IN BOOLEAN AllocateBuffer
,
205 IN UINT32 AuthenticationStatus
,
206 OUT UINTN
*SectionStreamHandle
211 IsValidSectionStream (
212 IN VOID
*SectionStream
,
213 IN UINTN SectionStreamLength
219 LIST_ENTRY mStreamRoot
= INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot
);
221 EFI_HANDLE mSectionExtractionHandle
= NULL
;
223 EFI_SECTION_EXTRACTION_PROTOCOL mSectionExtraction
= {
232 InitializeSectionExtraction (
233 IN EFI_HANDLE ImageHandle
,
234 IN EFI_SYSTEM_TABLE
*SystemTable
239 Entry point of the section extraction code. Initializes an instance of the
240 section extraction interface and installs it on a new handle.
243 ImageHandle EFI_HANDLE: A handle for the image that is initializing this driver
244 SystemTable EFI_SYSTEM_TABLE: A pointer to the EFI system table
247 EFI_SUCCESS: Driver initialized successfully
248 EFI_OUT_OF_RESOURCES: Could not allocate needed resources
255 // Install SEP to a new handle
257 Status
= CoreInstallProtocolInterface (
258 &mSectionExtractionHandle
,
259 &gEfiSectionExtractionProtocolGuid
,
260 EFI_NATIVE_INTERFACE
,
263 ASSERT_EFI_ERROR (Status
);
272 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
273 IN UINTN SectionStreamLength
,
274 IN VOID
*SectionStream
,
275 OUT UINTN
*SectionStreamHandle
280 SEP member function. This function creates and returns a new section stream
281 handle to represent the new section stream.
284 This - Indicates the calling context.
285 SectionStreamLength - Size in bytes of the section stream.
286 SectionStream - Buffer containing the new section stream.
287 SectionStreamHandle - A pointer to a caller allocated UINTN that on output
288 contains the new section stream handle.
292 EFI_OUT_OF_RESOURCES - memory allocation failed.
293 EFI_INVALID_PARAMETER - section stream does not end concident with end of
299 // Check to see section stream looks good...
301 if (!IsValidSectionStream (SectionStream
, SectionStreamLength
)) {
302 return EFI_INVALID_PARAMETER
;
305 return OpenSectionStreamEx (
318 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
319 IN UINTN SectionStreamHandle
,
320 IN EFI_SECTION_TYPE
*SectionType
,
321 IN EFI_GUID
*SectionDefinitionGuid
,
322 IN UINTN SectionInstance
,
324 IN OUT UINTN
*BufferSize
,
325 OUT UINT32
*AuthenticationStatus
330 SEP member function. Retrieves requested section from section stream.
333 This: Pointer to SEP instance.
334 SectionStreamHandle: The section stream from which to extract the requested
336 SectionType: A pointer to the type of section to search for.
337 SectionDefinitionGuid: If the section type is EFI_SECTION_GUID_DEFINED, then
338 SectionDefinitionGuid indicates which of these types
339 of sections to search for.
340 SectionInstance: Indicates which instance of the requested section to
342 Buffer: Double indirection to buffer. If *Buffer is non-null on
343 input, then the buffer is caller allocated. If
344 *Buffer is NULL, then the buffer is callee allocated.
345 In either case, the requried buffer size is returned
347 BufferSize: On input, indicates the size of *Buffer if *Buffer is
348 non-null on input. On output, indicates the required
349 size (allocated size if callee allocated) of *Buffer.
350 AuthenticationStatus: Indicates the authentication status of the retrieved
354 EFI_SUCCESS: Section was retrieved successfully
355 EFI_PROTOCOL_ERROR: A GUID defined section was encountered in the section
356 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
357 bit set, but there was no corresponding GUIDed Section
358 Extraction Protocol in the handle database. *Buffer is
360 EFI_NOT_FOUND: An error was encountered when parsing the SectionStream.
361 This indicates the SectionStream is not correctly
363 EFI_NOT_FOUND: The requested section does not exist.
364 EFI_OUT_OF_RESOURCES: The system has insufficient resources to process the
366 EFI_INVALID_PARAMETER: The SectionStreamHandle does not exist.
367 EFI_WARN_TOO_SMALL: The size of the caller allocated input buffer is
368 insufficient to contain the requested section. The
369 input buffer is filled and contents are section contents
374 CORE_SECTION_STREAM_NODE
*StreamNode
;
377 CORE_SECTION_CHILD_NODE
*ChildNode
;
378 CORE_SECTION_STREAM_NODE
*ChildStreamNode
;
380 UINT32 ExtractedAuthenticationStatus
;
386 OldTpl
= CoreRaiseTpl (EFI_TPL_NOTIFY
);
387 Instance
= SectionInstance
+ 1;
390 // Locate target stream
392 Status
= FindStreamNode (SectionStreamHandle
, &StreamNode
);
393 if (EFI_ERROR (Status
)) {
394 Status
= EFI_INVALID_PARAMETER
;
395 goto GetSection_Done
;
399 // Found the stream, now locate and return the appropriate section
401 if (SectionType
== NULL
) {
403 // SectionType == NULL means return the WHOLE section stream...
405 CopySize
= StreamNode
->StreamLength
;
406 CopyBuffer
= StreamNode
->StreamBuffer
;
407 *AuthenticationStatus
= StreamNode
->AuthenticationStatus
;
410 // There's a requested section type, so go find it and return it...
412 Status
= FindChildNode (
416 SectionDefinitionGuid
,
419 &ExtractedAuthenticationStatus
421 if (EFI_ERROR (Status
)) {
422 goto GetSection_Done
;
424 CopySize
= ChildNode
->Size
- sizeof (EFI_COMMON_SECTION_HEADER
);
425 CopyBuffer
= ChildStreamNode
->StreamBuffer
+ ChildNode
->OffsetInStream
+ sizeof (EFI_COMMON_SECTION_HEADER
);
426 *AuthenticationStatus
= ExtractedAuthenticationStatus
;
429 SectionSize
= CopySize
;
430 if (*Buffer
!= NULL
) {
432 // Caller allocated buffer. Fill to size and return required size...
434 if (*BufferSize
< CopySize
) {
435 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
436 CopySize
= *BufferSize
;
440 // Callee allocated buffer. Allocate buffer and return size.
442 *Buffer
= CoreAllocateBootServicesPool (CopySize
);
443 if (*Buffer
== NULL
) {
444 Status
= EFI_OUT_OF_RESOURCES
;
445 goto GetSection_Done
;
448 CopyMem (*Buffer
, CopyBuffer
, CopySize
);
449 *BufferSize
= SectionSize
;
452 CoreRestoreTpl (OldTpl
);
461 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
462 IN UINTN StreamHandleToClose
467 SEP member function. Deletes an existing section stream
470 This - Indicates the calling context.
471 StreamHandleToClose - Indicates the stream to close
475 EFI_OUT_OF_RESOURCES - memory allocation failed.
476 EFI_INVALID_PARAMETER - section stream does not end concident with end of
481 CORE_SECTION_STREAM_NODE
*StreamNode
;
485 CORE_SECTION_CHILD_NODE
*ChildNode
;
487 OldTpl
= CoreRaiseTpl (EFI_TPL_NOTIFY
);
490 // Locate target stream
492 Status
= FindStreamNode (StreamHandleToClose
, &StreamNode
);
493 if (!EFI_ERROR (Status
)) {
495 // Found the stream, so close it
497 RemoveEntryList (&StreamNode
->Link
);
498 while (!IsListEmpty (&StreamNode
->Children
)) {
499 Link
= GetFirstNode (&StreamNode
->Children
);
500 ChildNode
= CHILD_SECTION_NODE_FROM_LINK (Link
);
501 FreeChildNode (ChildNode
);
503 CoreFreePool (StreamNode
->StreamBuffer
);
504 CoreFreePool (StreamNode
);
505 Status
= EFI_SUCCESS
;
507 Status
= EFI_INVALID_PARAMETER
;
510 CoreRestoreTpl (OldTpl
);
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
526 Worker function. Determine if the input stream:child matches the input type.
529 Stream - Indicates the section stream associated with the child
530 Child - Indicates the child to check
531 SearchType - Indicates the type of section to check against for
532 SectionDefinitionGuid - Indicates the GUID to check against if the type is
533 EFI_SECTION_GUID_DEFINED
535 TRUE - The child matches
536 FALSE - The child doesn't match
540 EFI_GUID_DEFINED_SECTION
*GuidedSection
;
542 if (SearchType
== EFI_SECTION_ALL
) {
545 if (Child
->Type
!= SearchType
) {
548 if (SearchType
!= EFI_SECTION_GUID_DEFINED
) {
551 GuidedSection
= (EFI_GUID_DEFINED_SECTION
* )(Stream
->StreamBuffer
+ Child
->OffsetInStream
);
552 return CompareGuid (&GuidedSection
->SectionDefinitionGuid
, SectionDefinitionGuid
);
559 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
560 IN EFI_SECTION_TYPE SearchType
,
561 IN OUT UINTN
*SectionInstance
,
562 IN EFI_GUID
*SectionDefinitionGuid
,
563 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
564 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
565 OUT UINT32
*AuthenticationStatus
570 Worker function Recursively searches / builds section stream database
571 looking for requested section.
574 SourceStream - Indicates the section stream in which to do the search.
575 SearchType - Indicates the type of section to search for.
576 SectionInstance - Indicates which instance of section to find. This is
577 an in/out parameter to deal with recursions.
578 SectionDefinitionGuid - Guid of section definition
579 FoundChild - Output indicating the child node that is found.
580 FoundStream - Output indicating which section stream the child was
581 found in. If this stream was generated as a result of
582 an encapsulation section, the streamhandle is visible
583 within the SEP driver only.
584 AuthenticationStatus- Indicates the authentication status of the found section.
587 EFI_SUCCESS - Child node was found and returned.
588 EFI_OUT_OF_RESOURCES- Memory allocation failed.
589 EFI_NOT_FOUND - Requested child node does not exist.
590 EFI_PROTOCOL_ERROR - a required GUIDED section extraction protocol does not
595 CORE_SECTION_CHILD_NODE
*CurrentChildNode
;
596 CORE_SECTION_CHILD_NODE
*RecursedChildNode
;
597 CORE_SECTION_STREAM_NODE
*RecursedFoundStream
;
598 UINT32 NextChildOffset
;
599 EFI_STATUS ErrorStatus
;
602 CurrentChildNode
= NULL
;
603 ErrorStatus
= EFI_NOT_FOUND
;
605 if (SourceStream
->StreamLength
== 0) {
606 return EFI_NOT_FOUND
;
609 if (IsListEmpty (&SourceStream
->Children
) &&
610 SourceStream
->StreamLength
> sizeof (EFI_COMMON_SECTION_HEADER
)) {
612 // This occurs when a section stream exists, but no child sections
613 // have been parsed out yet. Therefore, extract the first child and add it
614 // to the list of children so we can get started.
616 Status
= CreateChildNode (SourceStream
, 0, &CurrentChildNode
);
617 if (EFI_ERROR (Status
)) {
623 // At least one child has been parsed out of the section stream. So, walk
624 // through the sections that have already been parsed out looking for the
625 // requested section, if necessary, continue parsing section stream and
626 // adding children until either the requested section is found, or we run
629 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream
->Children
));
632 if (ChildIsType (SourceStream
, CurrentChildNode
, SearchType
, SectionDefinitionGuid
)) {
634 // The type matches, so check the instance count to see if it's the one we want
636 (*SectionInstance
)--;
637 if (*SectionInstance
== 0) {
641 *FoundChild
= CurrentChildNode
;
642 *FoundStream
= SourceStream
;
643 *AuthenticationStatus
= SourceStream
->AuthenticationStatus
;
648 if (CurrentChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
650 // If the current node is an encapsulating node, recurse into it...
652 Status
= FindChildNode (
653 (CORE_SECTION_STREAM_NODE
*)CurrentChildNode
->EncapsulatedStreamHandle
,
656 SectionDefinitionGuid
,
658 &RecursedFoundStream
,
662 // If the status is not EFI_SUCCESS, just save the error code and continue
663 // to find the request child node in the rest stream.
665 if (*SectionInstance
== 0) {
666 ASSERT_EFI_ERROR (Status
);
667 *FoundChild
= RecursedChildNode
;
668 *FoundStream
= RecursedFoundStream
;
671 ErrorStatus
= Status
;
675 if (!IsNodeAtEnd (&SourceStream
->Children
, &CurrentChildNode
->Link
)) {
677 // We haven't found the child node we're interested in yet, but there's
678 // still more nodes that have already been parsed so get the next one
679 // and continue searching..
681 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream
->Children
, &CurrentChildNode
->Link
));
684 // We've exhausted children that have already been parsed, so see if
685 // there's any more data and continue parsing out more children if there
688 NextChildOffset
= CurrentChildNode
->OffsetInStream
+ CurrentChildNode
->Size
;
690 // Round up to 4 byte boundary
692 NextChildOffset
+= 3;
693 NextChildOffset
&= ~(UINTN
)3;
694 if (NextChildOffset
<= SourceStream
->StreamLength
- sizeof (EFI_COMMON_SECTION_HEADER
)) {
696 // There's an unparsed child remaining in the stream, so create a new child node
698 Status
= CreateChildNode (SourceStream
, NextChildOffset
, &CurrentChildNode
);
699 if (EFI_ERROR (Status
)) {
703 ASSERT (EFI_ERROR (ErrorStatus
));
714 IN CORE_SECTION_STREAM_NODE
*Stream
,
715 IN UINT32 ChildOffset
,
716 OUT CORE_SECTION_CHILD_NODE
**ChildNode
721 Worker function. Constructor for new child nodes.
724 Stream - Indicates the section stream in which to add the child.
725 ChildOffset - Indicates the offset in Stream that is the beginning
726 of the child section.
727 ChildNode - Indicates the Callee allocated and initialized child.
730 EFI_SUCCESS - Child node was found and returned.
731 EFI_OUT_OF_RESOURCES- Memory allocation failed.
732 EFI_PROTOCOL_ERROR - Encapsulation sections produce new stream handles when
733 the child node is created. If the section type is GUID
734 defined, and the extraction GUID does not exist, and
735 producing the stream requires the GUID, then a protocol
736 error is generated and no child is produced.
737 Values returned by OpenSectionStreamEx.
742 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
743 EFI_COMPRESSION_SECTION
*CompressionHeader
;
744 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
745 EFI_TIANO_DECOMPRESS_PROTOCOL
*Decompress
;
746 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
747 VOID
*NewStreamBuffer
;
750 UINTN NewStreamBufferSize
;
751 UINT32 AuthenticationStatus
;
752 UINT32 SectionLength
;
754 CORE_SECTION_CHILD_NODE
*Node
;
756 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) (Stream
->StreamBuffer
+ ChildOffset
);
759 // Allocate a new node
761 *ChildNode
= CoreAllocateBootServicesPool (sizeof (CORE_SECTION_CHILD_NODE
));
764 return EFI_OUT_OF_RESOURCES
;
770 Node
->Signature
= CORE_SECTION_CHILD_SIGNATURE
;
771 Node
->Type
= SectionHeader
->Type
;
772 Node
->Size
= SECTION_SIZE (SectionHeader
);
773 Node
->OffsetInStream
= ChildOffset
;
774 Node
->EncapsulatedStreamHandle
= NULL_STREAM_HANDLE
;
775 Node
->EncapsulationGuid
= NULL
;
778 // If it's an encapsulating section, then create the new section stream also
780 switch (Node
->Type
) {
781 case EFI_SECTION_COMPRESSION
:
783 // Get the CompressionSectionHeader
785 ASSERT (Node
->Size
>= sizeof (EFI_COMPRESSION_SECTION
));
787 CompressionHeader
= (EFI_COMPRESSION_SECTION
*) SectionHeader
;
790 // Allocate space for the new stream
792 if (CompressionHeader
->UncompressedLength
> 0) {
793 NewStreamBufferSize
= CompressionHeader
->UncompressedLength
;
794 NewStreamBuffer
= CoreAllocateBootServicesPool (NewStreamBufferSize
);
795 if (NewStreamBuffer
== NULL
) {
797 return EFI_OUT_OF_RESOURCES
;
800 if (CompressionHeader
->CompressionType
== EFI_NOT_COMPRESSED
) {
802 // stream is not actually compressed, just encapsulated. So just copy it.
804 CopyMem (NewStreamBuffer
, CompressionHeader
+ 1, NewStreamBufferSize
);
805 } else if (CompressionHeader
->CompressionType
== EFI_STANDARD_COMPRESSION
||
806 CompressionHeader
->CompressionType
== EFI_CUSTOMIZED_COMPRESSION
) {
808 // Decompress the stream
810 if (CompressionHeader
->CompressionType
== EFI_STANDARD_COMPRESSION
) {
811 Status
= CoreLocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
813 Status
= CoreLocateProtocol (&gEfiCustomizedDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
816 ASSERT_EFI_ERROR (Status
);
818 Status
= Decompress
->GetInfo (
820 CompressionHeader
+ 1,
821 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
822 (UINT32
*)&NewStreamBufferSize
,
825 ASSERT_EFI_ERROR (Status
);
826 ASSERT (NewStreamBufferSize
== CompressionHeader
->UncompressedLength
);
828 ScratchBuffer
= CoreAllocateBootServicesPool (ScratchSize
);
829 if (ScratchBuffer
== NULL
) {
831 CoreFreePool (NewStreamBuffer
);
832 return EFI_OUT_OF_RESOURCES
;
835 Status
= Decompress
->Decompress (
837 CompressionHeader
+ 1,
838 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
840 (UINT32
)NewStreamBufferSize
,
844 ASSERT_EFI_ERROR (Status
);
845 CoreFreePool (ScratchBuffer
);
848 NewStreamBuffer
= NULL
;
849 NewStreamBufferSize
= 0;
852 Status
= OpenSectionStreamEx (
856 Stream
->AuthenticationStatus
,
857 &Node
->EncapsulatedStreamHandle
859 if (EFI_ERROR (Status
)) {
861 CoreFreePool (NewStreamBuffer
);
866 case EFI_SECTION_GUID_DEFINED
:
867 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*) SectionHeader
;
868 Node
->EncapsulationGuid
= &GuidedHeader
->SectionDefinitionGuid
;
869 Status
= CoreLocateProtocol (Node
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
870 if (!EFI_ERROR (Status
)) {
872 // NewStreamBuffer is always allocated by ExtractSection... No caller
875 Status
= GuidedExtraction
->ExtractSection (
879 &NewStreamBufferSize
,
880 &AuthenticationStatus
882 if (EFI_ERROR (Status
)) {
883 CoreFreePool (*ChildNode
);
884 return EFI_PROTOCOL_ERROR
;
888 // Make sure we initialize the new stream with the correct
889 // authentication status for both aggregate and local status fields.
891 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
893 // OR in the parent stream's aggregate status.
895 AuthenticationStatus
|= Stream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
898 // since there's no authentication data contributed by the section,
899 // just inherit the full value from our immediate parent.
901 AuthenticationStatus
= Stream
->AuthenticationStatus
;
904 Status
= OpenSectionStreamEx (
908 AuthenticationStatus
,
909 &Node
->EncapsulatedStreamHandle
911 if (EFI_ERROR (Status
)) {
912 CoreFreePool (*ChildNode
);
913 CoreFreePool (NewStreamBuffer
);
918 // There's no GUIDed section extraction protocol available.
920 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) {
922 // If the section REQUIRES an extraction protocol, then we're toast
924 CoreFreePool (*ChildNode
);
925 return EFI_PROTOCOL_ERROR
;
929 // Figure out the proper authentication status
931 AuthenticationStatus
= Stream
->AuthenticationStatus
;
932 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
934 // The local status of the new stream is contained in
935 // AuthenticaionStatus. This value needs to be ORed into the
936 // Aggregate bits also...
940 // Clear out and initialize the local status
942 AuthenticationStatus
&= ~EFI_LOCAL_AUTH_STATUS_ALL
;
943 AuthenticationStatus
|= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
;
945 // OR local status into aggregate status
947 AuthenticationStatus
|= AuthenticationStatus
>> 16;
950 SectionLength
= SECTION_SIZE (GuidedHeader
);
951 Status
= OpenSectionStreamEx (
952 SectionLength
- GuidedHeader
->DataOffset
,
953 (UINT8
*) GuidedHeader
+ GuidedHeader
->DataOffset
,
955 AuthenticationStatus
,
956 &Node
->EncapsulatedStreamHandle
958 if (EFI_ERROR (Status
)) {
964 if ((AuthenticationStatus
& EFI_LOCAL_AUTH_STATUS_ALL
) ==
965 (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
)) {
967 // Need to register for RPN for when the required GUIDed extraction
968 // protocol becomes available. This will enable us to refresh the
969 // AuthenticationStatus cached in the Stream if it's ever requested
972 CreateGuidedExtractionRpnEvent (Stream
, Node
);
980 // Nothing to do if it's a leaf
986 // Last, add the new child node to the stream
988 InsertTailList (&Stream
->Children
, &Node
->Link
);
996 CreateGuidedExtractionRpnEvent (
997 IN CORE_SECTION_STREAM_NODE
*ParentStream
,
998 IN CORE_SECTION_CHILD_NODE
*ChildNode
1002 Routine Description:
1003 Worker function. Constructor for RPN event if needed to keep AuthenticationStatus
1004 cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...
1007 ParentStream - Indicates the parent of the ecnapsulation section (child)
1008 ChildNode - Indicates the child node that is the encapsulation section.
1015 RPN_EVENT_CONTEXT
*Context
;
1018 // Allocate new event structure and context
1020 Context
= CoreAllocateBootServicesPool (sizeof (RPN_EVENT_CONTEXT
));
1021 ASSERT (Context
!= NULL
);
1023 Context
->ChildNode
= ChildNode
;
1024 Context
->ParentStream
= ParentStream
;
1026 Context
->Event
= CoreCreateProtocolNotifyEvent (
1027 Context
->ChildNode
->EncapsulationGuid
,
1029 NotifyGuidedExtraction
,
1031 &Context
->Registration
,
1040 NotifyGuidedExtraction (
1046 Routine Description:
1047 RPN callback function. Removes a stale section stream and re-initializes it
1048 with an updated AuthenticationStatus.
1051 Event - The event that fired
1052 RpnContext - A pointer to the context that allows us to identify
1053 the relevent encapsulation...
1061 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
1062 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
1063 VOID
*NewStreamBuffer
;
1064 UINTN NewStreamBufferSize
;
1065 UINT32 AuthenticationStatus
;
1066 RPN_EVENT_CONTEXT
*Context
;
1068 Context
= RpnContext
;
1070 Status
= CloseSectionStream (&mSectionExtraction
, Context
->ChildNode
->EncapsulatedStreamHandle
);
1071 if (!EFI_ERROR (Status
)) {
1073 // The stream closed successfully, so re-open the stream with correct AuthenticationStatus
1076 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*)
1077 (Context
->ParentStream
->StreamBuffer
+ Context
->ChildNode
->OffsetInStream
);
1078 ASSERT (GuidedHeader
->CommonHeader
.Type
== EFI_SECTION_GUID_DEFINED
);
1080 Status
= CoreLocateProtocol (Context
->ChildNode
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
1081 ASSERT_EFI_ERROR (Status
);
1084 Status
= GuidedExtraction
->ExtractSection (
1088 &NewStreamBufferSize
,
1089 &AuthenticationStatus
1091 ASSERT_EFI_ERROR (Status
);
1093 // OR in the parent stream's aggregagate status.
1095 AuthenticationStatus
|= Context
->ParentStream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
1096 Status
= OpenSectionStreamEx (
1097 NewStreamBufferSize
,
1100 AuthenticationStatus
,
1101 &Context
->ChildNode
->EncapsulatedStreamHandle
1103 ASSERT_EFI_ERROR (Status
);
1107 // If above, the stream did not close successfully, it indicates it's
1108 // alread been closed by someone, so just destroy the event and be done with
1112 CoreCloseEvent (Event
);
1113 CoreFreePool (Context
);
1120 IN CORE_SECTION_CHILD_NODE
*ChildNode
1124 Routine Description:
1125 Worker function. Destructor for child nodes.
1128 ChildNode - Indicates the node to destroy
1135 ASSERT (ChildNode
->Signature
== CORE_SECTION_CHILD_SIGNATURE
);
1137 // Remove the child from it's list
1139 RemoveEntryList (&ChildNode
->Link
);
1141 if (ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1143 // If it's an encapsulating section, we close the resulting section stream.
1144 // CloseSectionStream will free all memory associated with the stream.
1146 CloseSectionStream (&mSectionExtraction
, ChildNode
->EncapsulatedStreamHandle
);
1149 // Last, free the child node itself
1151 CoreFreePool (ChildNode
);
1157 OpenSectionStreamEx (
1158 IN UINTN SectionStreamLength
,
1159 IN VOID
*SectionStream
,
1160 IN BOOLEAN AllocateBuffer
,
1161 IN UINT32 AuthenticationStatus
,
1162 OUT UINTN
*SectionStreamHandle
1166 Routine Description:
1167 Worker function. Constructor for section streams.
1170 SectionStreamLength - Size in bytes of the section stream.
1171 SectionStream - Buffer containing the new section stream.
1172 AllocateBuffer - Indicates whether the stream buffer is to be copied
1173 or the input buffer is to be used in place.
1174 AuthenticationStatus- Indicates the default authentication status for the
1176 SectionStreamHandle - A pointer to a caller allocated section stream handle.
1179 EFI_SUCCESS - Stream was added to stream database.
1180 EFI_OUT_OF_RESOURCES - memory allocation failed.
1184 CORE_SECTION_STREAM_NODE
*NewStream
;
1188 // Allocate a new stream
1190 NewStream
= CoreAllocateBootServicesPool (sizeof (CORE_SECTION_STREAM_NODE
));
1191 if (NewStream
== NULL
) {
1192 return EFI_OUT_OF_RESOURCES
;
1195 if (AllocateBuffer
) {
1197 // if we're here, we're double buffering, allocate the buffer and copy the
1200 if (SectionStreamLength
> 0) {
1201 NewStream
->StreamBuffer
= CoreAllocateBootServicesPool (SectionStreamLength
);
1202 if (NewStream
->StreamBuffer
== NULL
) {
1203 CoreFreePool (NewStream
);
1204 return EFI_OUT_OF_RESOURCES
;
1207 // Copy in stream data
1209 CopyMem (NewStream
->StreamBuffer
, SectionStream
, SectionStreamLength
);
1212 // It's possible to have a zero length section stream.
1214 NewStream
->StreamBuffer
= NULL
;
1218 // If were here, the caller has supplied the buffer (it's an internal call)
1219 // so just assign the buffer. This happens when we open section streams
1220 // as a result of expanding an encapsulating section.
1222 NewStream
->StreamBuffer
= SectionStream
;
1226 // Initialize the rest of the section stream
1228 NewStream
->Signature
= CORE_SECTION_STREAM_SIGNATURE
;
1229 NewStream
->StreamHandle
= (UINTN
) NewStream
;
1230 NewStream
->StreamLength
= SectionStreamLength
;
1231 InitializeListHead (&NewStream
->Children
);
1232 NewStream
->AuthenticationStatus
= AuthenticationStatus
;
1235 // Add new stream to stream list
1237 OldTpl
= CoreRaiseTpl (EFI_TPL_NOTIFY
);
1238 InsertTailList (&mStreamRoot
, &NewStream
->Link
);
1239 CoreRestoreTpl (OldTpl
);
1241 *SectionStreamHandle
= NewStream
->StreamHandle
;
1250 IN UINTN SearchHandle
,
1251 OUT CORE_SECTION_STREAM_NODE
**FoundStream
1255 Routine Description:
1256 Worker function. Search stream database for requested stream handle.
1259 SearchHandle - Indicates which stream to look for.
1260 FoundStream - Output pointer to the found stream.
1263 EFI_SUCCESS - StreamHandle was found and *FoundStream contains
1265 EFI_NOT_FOUND - SearchHandle was not found in the stream database.
1269 CORE_SECTION_STREAM_NODE
*StreamNode
;
1271 if (!IsListEmpty (&mStreamRoot
)) {
1272 StreamNode
= STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot
));
1274 if (StreamNode
->StreamHandle
== SearchHandle
) {
1275 *FoundStream
= StreamNode
;
1277 } else if (IsNodeAtEnd (&mStreamRoot
, &StreamNode
->Link
)) {
1280 StreamNode
= STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot
, &StreamNode
->Link
));
1285 return EFI_NOT_FOUND
;
1291 IsValidSectionStream (
1292 IN VOID
*SectionStream
,
1293 IN UINTN SectionStreamLength
1297 Routine Description:
1298 Check if a stream is valid.
1301 SectionStream - The section stream to be checked
1302 SectionStreamLength - The length of section stream
1311 UINTN SectionLength
;
1312 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
1313 EFI_COMMON_SECTION_HEADER
*NextSectionHeader
;
1316 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*)SectionStream
;
1318 while (TotalLength
< SectionStreamLength
) {
1319 SectionLength
= SECTION_SIZE (SectionHeader
);
1320 TotalLength
+= SectionLength
;
1322 if (TotalLength
== SectionStreamLength
) {
1327 // Move to the next byte following the section...
1329 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) SectionHeader
+ SectionLength
);
1332 // Figure out where the next section begins
1334 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) SectionHeader
+ 3);
1335 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) NextSectionHeader
& ~(UINTN
)3);
1336 TotalLength
+= (UINTN
) NextSectionHeader
- (UINTN
) SectionHeader
;
1337 SectionHeader
= NextSectionHeader
;