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.
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.
48 #include "SectionExtraction.h"
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
;
106 CoreCreateProtocolNotifyEvent (
107 IN EFI_GUID
*ProtocolGuid
,
108 IN EFI_TPL NotifyTpl
,
109 IN EFI_EVENT_NOTIFY NotifyFunction
,
110 IN VOID
*NotifyContext
,
111 OUT VOID
**Registration
,
112 IN BOOLEAN SignalFlag
122 IN CORE_SECTION_STREAM_NODE
*Stream
,
123 IN CORE_SECTION_CHILD_NODE
*Child
,
124 IN EFI_SECTION_TYPE SearchType
,
125 IN EFI_GUID
*SectionDefinitionGuid
131 NotifyGuidedExtraction (
138 CreateGuidedExtractionRpnEvent (
139 IN CORE_SECTION_STREAM_NODE
*ParentStream
,
140 IN CORE_SECTION_CHILD_NODE
*ChildNode
147 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
148 IN UINTN SectionStreamLength
,
149 IN VOID
*SectionStream
,
150 OUT UINTN
*SectionStreamHandle
157 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
158 IN UINTN SectionStreamHandle
,
159 IN EFI_SECTION_TYPE
*SectionType
,
160 IN EFI_GUID
*SectionDefinitionGuid
,
161 IN UINTN SectionInstance
,
163 IN OUT UINTN
*BufferSize
,
164 OUT UINT32
*AuthenticationStatus
171 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
172 IN UINTN StreamHandleToClose
178 IN UINTN SearchHandle
,
179 OUT CORE_SECTION_STREAM_NODE
**FoundStream
185 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
186 IN EFI_SECTION_TYPE SearchType
,
187 IN UINTN
*SectionInstance
,
188 IN EFI_GUID
*SectionDefinitionGuid
,
189 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
190 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
191 OUT UINT32
*AuthenticationStatus
197 IN CORE_SECTION_STREAM_NODE
*Stream
,
198 IN UINT32 ChildOffset
,
199 OUT CORE_SECTION_CHILD_NODE
**ChildNode
205 IN CORE_SECTION_CHILD_NODE
*ChildNode
210 OpenSectionStreamEx (
211 IN UINTN SectionStreamLength
,
212 IN VOID
*SectionStream
,
213 IN BOOLEAN AllocateBuffer
,
214 IN UINT32 AuthenticationStatus
,
215 OUT UINTN
*SectionStreamHandle
220 IsValidSectionStream (
221 IN VOID
*SectionStream
,
222 IN UINTN SectionStreamLength
228 LIST_ENTRY mStreamRoot
= INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot
);
230 EFI_HANDLE mSectionExtractionHandle
= NULL
;
232 EFI_SECTION_EXTRACTION_PROTOCOL mSectionExtraction
= {
240 SectionExtractionEntryPoint (
241 IN EFI_HANDLE ImageHandle
,
242 IN EFI_SYSTEM_TABLE
*SystemTable
247 Entry point of the section extraction code. Initializes an instance of the
248 section extraction interface and installs it on a new handle.
251 ImageHandle EFI_HANDLE: A handle for the image that is initializing this driver
252 SystemTable EFI_SYSTEM_TABLE: A pointer to the EFI system table
255 EFI_SUCCESS: Driver initialized successfully
256 EFI_OUT_OF_RESOURCES: Could not allocate needed resources
263 // Install SEP to a new handle
265 Status
= gBS
->InstallProtocolInterface (
266 &mSectionExtractionHandle
,
267 &gEfiSectionExtractionProtocolGuid
,
268 EFI_NATIVE_INTERFACE
,
271 ASSERT_EFI_ERROR (Status
);
280 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
281 IN UINTN SectionStreamLength
,
282 IN VOID
*SectionStream
,
283 OUT UINTN
*SectionStreamHandle
288 SEP member function. This function creates and returns a new section stream
289 handle to represent the new section stream.
292 This - Indicates the calling context.
293 SectionStreamLength - Size in bytes of the section stream.
294 SectionStream - Buffer containing the new section stream.
295 SectionStreamHandle - A pointer to a caller allocated UINTN that on output
296 contains the new section stream handle.
300 EFI_OUT_OF_RESOURCES - memory allocation failed.
301 EFI_INVALID_PARAMETER - section stream does not end concident with end of
307 // Check to see section stream looks good...
309 if (!IsValidSectionStream (SectionStream
, SectionStreamLength
)) {
310 return EFI_INVALID_PARAMETER
;
313 return OpenSectionStreamEx (
326 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
327 IN UINTN SectionStreamHandle
,
328 IN EFI_SECTION_TYPE
*SectionType
,
329 IN EFI_GUID
*SectionDefinitionGuid
,
330 IN UINTN SectionInstance
,
332 IN OUT UINTN
*BufferSize
,
333 OUT UINT32
*AuthenticationStatus
338 SEP member function. Retrieves requested section from section stream.
341 This: Pointer to SEP instance.
342 SectionStreamHandle: The section stream from which to extract the requested
344 SectionType: A pointer to the type of section to search for.
345 SectionDefinitionGuid: If the section type is EFI_SECTION_GUID_DEFINED, then
346 SectionDefinitionGuid indicates which of these types
347 of sections to search for.
348 SectionInstance: Indicates which instance of the requested section to
350 Buffer: Double indirection to buffer. If *Buffer is non-null on
351 input, then the buffer is caller allocated. If
352 *Buffer is NULL, then the buffer is callee allocated.
353 In either case, the requried buffer size is returned
355 BufferSize: On input, indicates the size of *Buffer if *Buffer is
356 non-null on input. On output, indicates the required
357 size (allocated size if callee allocated) of *Buffer.
358 AuthenticationStatus: Indicates the authentication status of the retrieved
362 EFI_SUCCESS: Section was retrieved successfully
363 EFI_PROTOCOL_ERROR: A GUID defined section was encountered in the section
364 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
365 bit set, but there was no corresponding GUIDed Section
366 Extraction Protocol in the handle database. *Buffer is
368 EFI_NOT_FOUND: An error was encountered when parsing the SectionStream.
369 This indicates the SectionStream is not correctly
371 EFI_NOT_FOUND: The requested section does not exist.
372 EFI_OUT_OF_RESOURCES: The system has insufficient resources to process the
374 EFI_INVALID_PARAMETER: The SectionStreamHandle does not exist.
375 EFI_WARN_TOO_SMALL: The size of the caller allocated input buffer is
376 insufficient to contain the requested section. The
377 input buffer is filled and contents are section contents
382 CORE_SECTION_STREAM_NODE
*StreamNode
;
385 CORE_SECTION_CHILD_NODE
*ChildNode
;
386 CORE_SECTION_STREAM_NODE
*ChildStreamNode
;
388 UINT32 ExtractedAuthenticationStatus
;
394 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
395 Instance
= SectionInstance
+ 1;
398 // Locate target stream
400 Status
= FindStreamNode (SectionStreamHandle
, &StreamNode
);
401 if (EFI_ERROR (Status
)) {
402 Status
= EFI_INVALID_PARAMETER
;
403 goto GetSection_Done
;
407 // Found the stream, now locate and return the appropriate section
409 if (SectionType
== NULL
) {
411 // SectionType == NULL means return the WHOLE section stream...
413 CopySize
= StreamNode
->StreamLength
;
414 CopyBuffer
= StreamNode
->StreamBuffer
;
415 *AuthenticationStatus
= StreamNode
->AuthenticationStatus
;
418 // There's a requested section type, so go find it and return it...
420 Status
= FindChildNode (
424 SectionDefinitionGuid
,
427 &ExtractedAuthenticationStatus
429 if (EFI_ERROR (Status
)) {
430 goto GetSection_Done
;
432 CopySize
= ChildNode
->Size
- sizeof (EFI_COMMON_SECTION_HEADER
);
433 CopyBuffer
= ChildStreamNode
->StreamBuffer
+ ChildNode
->OffsetInStream
+ sizeof (EFI_COMMON_SECTION_HEADER
);
434 *AuthenticationStatus
= ExtractedAuthenticationStatus
;
437 SectionSize
= CopySize
;
438 if (*Buffer
!= NULL
) {
440 // Caller allocated buffer. Fill to size and return required size...
442 if (*BufferSize
< CopySize
) {
443 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
444 CopySize
= *BufferSize
;
448 // Callee allocated buffer. Allocate buffer and return size.
450 *Buffer
= AllocatePool (CopySize
);
451 if (*Buffer
== NULL
) {
452 Status
= EFI_OUT_OF_RESOURCES
;
453 goto GetSection_Done
;
456 CopyMem (*Buffer
, CopyBuffer
, CopySize
);
457 *BufferSize
= SectionSize
;
460 gBS
->RestoreTPL (OldTpl
);
469 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
470 IN UINTN StreamHandleToClose
475 SEP member function. Deletes an existing section stream
478 This - Indicates the calling context.
479 StreamHandleToClose - Indicates the stream to close
483 EFI_OUT_OF_RESOURCES - memory allocation failed.
484 EFI_INVALID_PARAMETER - section stream does not end concident with end of
489 CORE_SECTION_STREAM_NODE
*StreamNode
;
493 CORE_SECTION_CHILD_NODE
*ChildNode
;
495 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
498 // Locate target stream
500 Status
= FindStreamNode (StreamHandleToClose
, &StreamNode
);
501 if (!EFI_ERROR (Status
)) {
503 // Found the stream, so close it
505 RemoveEntryList (&StreamNode
->Link
);
506 while (!IsListEmpty (&StreamNode
->Children
)) {
507 Link
= GetFirstNode (&StreamNode
->Children
);
508 ChildNode
= CHILD_SECTION_NODE_FROM_LINK (Link
);
509 FreeChildNode (ChildNode
);
511 gBS
->FreePool (StreamNode
->StreamBuffer
);
512 gBS
->FreePool (StreamNode
);
513 Status
= EFI_SUCCESS
;
515 Status
= EFI_INVALID_PARAMETER
;
518 gBS
->RestoreTPL (OldTpl
);
526 IN CORE_SECTION_STREAM_NODE
*Stream
,
527 IN CORE_SECTION_CHILD_NODE
*Child
,
528 IN EFI_SECTION_TYPE SearchType
,
529 IN EFI_GUID
*SectionDefinitionGuid
534 Worker function. Determine if the input stream:child matches the input type.
537 Stream - Indicates the section stream associated with the child
538 Child - Indicates the child to check
539 SearchType - Indicates the type of section to check against for
540 SectionDefinitionGuid - Indicates the GUID to check against if the type is
541 EFI_SECTION_GUID_DEFINED
543 TRUE - The child matches
544 FALSE - The child doesn't match
548 EFI_GUID_DEFINED_SECTION
*GuidedSection
;
550 if (SearchType
== EFI_SECTION_ALL
) {
553 if (Child
->Type
!= SearchType
) {
556 if (SearchType
!= EFI_SECTION_GUID_DEFINED
) {
559 GuidedSection
= (EFI_GUID_DEFINED_SECTION
* )(Stream
->StreamBuffer
+ Child
->OffsetInStream
);
560 return CompareGuid (&GuidedSection
->SectionDefinitionGuid
, SectionDefinitionGuid
);
567 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
568 IN EFI_SECTION_TYPE SearchType
,
569 IN OUT UINTN
*SectionInstance
,
570 IN EFI_GUID
*SectionDefinitionGuid
,
571 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
572 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
573 OUT UINT32
*AuthenticationStatus
578 Worker function Recursively searches / builds section stream database
579 looking for requested section.
582 SourceStream - Indicates the section stream in which to do the search.
583 SearchType - Indicates the type of section to search for.
584 SectionInstance - Indicates which instance of section to find. This is
585 an in/out parameter to deal with recursions.
586 SectionDefinitionGuid - Guid of section definition
587 FoundChild - Output indicating the child node that is found.
588 FoundStream - Output indicating which section stream the child was
589 found in. If this stream was generated as a result of
590 an encapsulation section, the streamhandle is visible
591 within the SEP driver only.
592 AuthenticationStatus- Indicates the authentication status of the found section.
595 EFI_SUCCESS - Child node was found and returned.
596 EFI_OUT_OF_RESOURCES- Memory allocation failed.
597 EFI_NOT_FOUND - Requested child node does not exist.
598 EFI_PROTOCOL_ERROR - a required GUIDED section extraction protocol does not
603 CORE_SECTION_CHILD_NODE
*CurrentChildNode
;
604 CORE_SECTION_CHILD_NODE
*RecursedChildNode
;
605 CORE_SECTION_STREAM_NODE
*RecursedFoundStream
;
606 UINT32 NextChildOffset
;
607 EFI_STATUS ErrorStatus
;
610 CurrentChildNode
= NULL
;
611 ErrorStatus
= EFI_NOT_FOUND
;
613 if (SourceStream
->StreamLength
== 0) {
614 return EFI_NOT_FOUND
;
617 if (IsListEmpty (&SourceStream
->Children
) &&
618 SourceStream
->StreamLength
> sizeof (EFI_COMMON_SECTION_HEADER
)) {
620 // This occurs when a section stream exists, but no child sections
621 // have been parsed out yet. Therefore, extract the first child and add it
622 // to the list of children so we can get started.
624 Status
= CreateChildNode (SourceStream
, 0, &CurrentChildNode
);
625 if (EFI_ERROR (Status
)) {
631 // At least one child has been parsed out of the section stream. So, walk
632 // through the sections that have already been parsed out looking for the
633 // requested section, if necessary, continue parsing section stream and
634 // adding children until either the requested section is found, or we run
637 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream
->Children
));
640 if (ChildIsType (SourceStream
, CurrentChildNode
, SearchType
, SectionDefinitionGuid
)) {
642 // The type matches, so check the instance count to see if it's the one we want
644 (*SectionInstance
)--;
645 if (*SectionInstance
== 0) {
649 *FoundChild
= CurrentChildNode
;
650 *FoundStream
= SourceStream
;
651 *AuthenticationStatus
= SourceStream
->AuthenticationStatus
;
656 if (CurrentChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
658 // If the current node is an encapsulating node, recurse into it...
660 Status
= FindChildNode (
661 (CORE_SECTION_STREAM_NODE
*)CurrentChildNode
->EncapsulatedStreamHandle
,
664 SectionDefinitionGuid
,
666 &RecursedFoundStream
,
670 // If the status is not EFI_SUCCESS, just save the error code and continue
671 // to find the request child node in the rest stream.
673 if (*SectionInstance
== 0) {
674 ASSERT_EFI_ERROR (Status
);
675 *FoundChild
= RecursedChildNode
;
676 *FoundStream
= RecursedFoundStream
;
679 ErrorStatus
= Status
;
683 if (!IsNodeAtEnd (&SourceStream
->Children
, &CurrentChildNode
->Link
)) {
685 // We haven't found the child node we're interested in yet, but there's
686 // still more nodes that have already been parsed so get the next one
687 // and continue searching..
689 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream
->Children
, &CurrentChildNode
->Link
));
692 // We've exhausted children that have already been parsed, so see if
693 // there's any more data and continue parsing out more children if there
696 NextChildOffset
= CurrentChildNode
->OffsetInStream
+ CurrentChildNode
->Size
;
698 // Round up to 4 byte boundary
700 NextChildOffset
+= 3;
701 NextChildOffset
&= ~(UINTN
)3;
702 if (NextChildOffset
<= SourceStream
->StreamLength
- sizeof (EFI_COMMON_SECTION_HEADER
)) {
704 // There's an unparsed child remaining in the stream, so create a new child node
706 Status
= CreateChildNode (SourceStream
, NextChildOffset
, &CurrentChildNode
);
707 if (EFI_ERROR (Status
)) {
711 ASSERT (EFI_ERROR (ErrorStatus
));
722 IN CORE_SECTION_STREAM_NODE
*Stream
,
723 IN UINT32 ChildOffset
,
724 OUT CORE_SECTION_CHILD_NODE
**ChildNode
729 Worker function. Constructor for new child nodes.
732 Stream - Indicates the section stream in which to add the child.
733 ChildOffset - Indicates the offset in Stream that is the beginning
734 of the child section.
735 ChildNode - Indicates the Callee allocated and initialized child.
738 EFI_SUCCESS - Child node was found and returned.
739 EFI_OUT_OF_RESOURCES- Memory allocation failed.
740 EFI_PROTOCOL_ERROR - Encapsulation sections produce new stream handles when
741 the child node is created. If the section type is GUID
742 defined, and the extraction GUID does not exist, and
743 producing the stream requires the GUID, then a protocol
744 error is generated and no child is produced.
745 Values returned by OpenSectionStreamEx.
750 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
751 EFI_COMPRESSION_SECTION
*CompressionHeader
;
752 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
753 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
754 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
755 VOID
*NewStreamBuffer
;
758 UINTN NewStreamBufferSize
;
759 UINT32 AuthenticationStatus
;
760 UINT32 SectionLength
;
762 CORE_SECTION_CHILD_NODE
*Node
;
764 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) (Stream
->StreamBuffer
+ ChildOffset
);
767 // Allocate a new node
769 *ChildNode
= AllocatePool (sizeof (CORE_SECTION_CHILD_NODE
));
772 return EFI_OUT_OF_RESOURCES
;
778 Node
->Signature
= CORE_SECTION_CHILD_SIGNATURE
;
779 Node
->Type
= SectionHeader
->Type
;
780 Node
->Size
= SECTION_SIZE (SectionHeader
);
781 Node
->OffsetInStream
= ChildOffset
;
782 Node
->EncapsulatedStreamHandle
= NULL_STREAM_HANDLE
;
783 Node
->EncapsulationGuid
= NULL
;
786 // If it's an encapsulating section, then create the new section stream also
788 switch (Node
->Type
) {
789 case EFI_SECTION_COMPRESSION
:
791 // Get the CompressionSectionHeader
793 ASSERT (Node
->Size
>= sizeof (EFI_COMPRESSION_SECTION
));
795 CompressionHeader
= (EFI_COMPRESSION_SECTION
*) SectionHeader
;
798 // Allocate space for the new stream
800 if (CompressionHeader
->UncompressedLength
> 0) {
801 NewStreamBufferSize
= CompressionHeader
->UncompressedLength
;
802 NewStreamBuffer
= AllocatePool (NewStreamBufferSize
);
803 if (NewStreamBuffer
== NULL
) {
804 gBS
->FreePool (Node
);
805 return EFI_OUT_OF_RESOURCES
;
808 if (CompressionHeader
->CompressionType
== EFI_NOT_COMPRESSED
) {
810 // stream is not actually compressed, just encapsulated. So just copy it.
812 CopyMem (NewStreamBuffer
, CompressionHeader
+ 1, NewStreamBufferSize
);
813 } else if (CompressionHeader
->CompressionType
== EFI_STANDARD_COMPRESSION
) {
815 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
819 // Decompress the stream
821 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
823 ASSERT_EFI_ERROR (Status
);
825 Status
= Decompress
->GetInfo (
827 CompressionHeader
+ 1,
828 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
829 (UINT32
*)&NewStreamBufferSize
,
832 ASSERT_EFI_ERROR (Status
);
833 ASSERT (NewStreamBufferSize
== CompressionHeader
->UncompressedLength
);
835 ScratchBuffer
= AllocatePool (ScratchSize
);
836 if (ScratchBuffer
== NULL
) {
837 gBS
->FreePool (Node
);
838 gBS
->FreePool (NewStreamBuffer
);
839 return EFI_OUT_OF_RESOURCES
;
842 Status
= Decompress
->Decompress (
844 CompressionHeader
+ 1,
845 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
847 (UINT32
)NewStreamBufferSize
,
851 ASSERT_EFI_ERROR (Status
);
852 gBS
->FreePool (ScratchBuffer
);
855 NewStreamBuffer
= NULL
;
856 NewStreamBufferSize
= 0;
859 Status
= OpenSectionStreamEx (
863 Stream
->AuthenticationStatus
,
864 &Node
->EncapsulatedStreamHandle
866 if (EFI_ERROR (Status
)) {
867 gBS
->FreePool (Node
);
868 gBS
->FreePool (NewStreamBuffer
);
873 case EFI_SECTION_GUID_DEFINED
:
874 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*) SectionHeader
;
875 Node
->EncapsulationGuid
= &GuidedHeader
->SectionDefinitionGuid
;
876 Status
= gBS
->LocateProtocol (Node
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
877 if (!EFI_ERROR (Status
)) {
879 // NewStreamBuffer is always allocated by ExtractSection... No caller
882 Status
= GuidedExtraction
->ExtractSection (
886 &NewStreamBufferSize
,
887 &AuthenticationStatus
889 if (EFI_ERROR (Status
)) {
890 gBS
->FreePool (*ChildNode
);
891 return EFI_PROTOCOL_ERROR
;
895 // Make sure we initialize the new stream with the correct
896 // authentication status for both aggregate and local status fields.
898 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
900 // OR in the parent stream's aggregate status.
902 AuthenticationStatus
|= Stream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
905 // since there's no authentication data contributed by the section,
906 // just inherit the full value from our immediate parent.
908 AuthenticationStatus
= Stream
->AuthenticationStatus
;
911 Status
= OpenSectionStreamEx (
915 AuthenticationStatus
,
916 &Node
->EncapsulatedStreamHandle
918 if (EFI_ERROR (Status
)) {
919 gBS
->FreePool (*ChildNode
);
920 gBS
->FreePool (NewStreamBuffer
);
925 // There's no GUIDed section extraction protocol available.
927 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) {
929 // If the section REQUIRES an extraction protocol, then we're toast
931 gBS
->FreePool (*ChildNode
);
932 return EFI_PROTOCOL_ERROR
;
936 // Figure out the proper authentication status
938 AuthenticationStatus
= Stream
->AuthenticationStatus
;
939 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
941 // The local status of the new stream is contained in
942 // AuthenticaionStatus. This value needs to be ORed into the
943 // Aggregate bits also...
947 // Clear out and initialize the local status
949 AuthenticationStatus
&= ~EFI_LOCAL_AUTH_STATUS_ALL
;
950 AuthenticationStatus
|= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
;
952 // OR local status into aggregate status
954 AuthenticationStatus
|= AuthenticationStatus
>> 16;
957 SectionLength
= SECTION_SIZE (GuidedHeader
);
958 Status
= OpenSectionStreamEx (
959 SectionLength
- GuidedHeader
->DataOffset
,
960 (UINT8
*) GuidedHeader
+ GuidedHeader
->DataOffset
,
962 AuthenticationStatus
,
963 &Node
->EncapsulatedStreamHandle
965 if (EFI_ERROR (Status
)) {
966 gBS
->FreePool (Node
);
971 if ((AuthenticationStatus
& EFI_LOCAL_AUTH_STATUS_ALL
) ==
972 (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
)) {
974 // Need to register for RPN for when the required GUIDed extraction
975 // protocol becomes available. This will enable us to refresh the
976 // AuthenticationStatus cached in the Stream if it's ever requested
979 CreateGuidedExtractionRpnEvent (Stream
, Node
);
987 // Nothing to do if it's a leaf
993 // Last, add the new child node to the stream
995 InsertTailList (&Stream
->Children
, &Node
->Link
);
1003 CreateGuidedExtractionRpnEvent (
1004 IN CORE_SECTION_STREAM_NODE
*ParentStream
,
1005 IN CORE_SECTION_CHILD_NODE
*ChildNode
1009 Routine Description:
1010 Worker function. Constructor for RPN event if needed to keep AuthenticationStatus
1011 cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...
1014 ParentStream - Indicates the parent of the ecnapsulation section (child)
1015 ChildNode - Indicates the child node that is the encapsulation section.
1022 RPN_EVENT_CONTEXT
*Context
;
1025 // Allocate new event structure and context
1027 Context
= AllocatePool (sizeof (RPN_EVENT_CONTEXT
));
1028 ASSERT (Context
!= NULL
);
1030 Context
->ChildNode
= ChildNode
;
1031 Context
->ParentStream
= ParentStream
;
1033 Context
->Event
= CoreCreateProtocolNotifyEvent (
1034 Context
->ChildNode
->EncapsulationGuid
,
1036 NotifyGuidedExtraction
,
1038 &Context
->Registration
,
1047 NotifyGuidedExtraction (
1053 Routine Description:
1054 RPN callback function. Removes a stale section stream and re-initializes it
1055 with an updated AuthenticationStatus.
1058 Event - The event that fired
1059 RpnContext - A pointer to the context that allows us to identify
1060 the relevent encapsulation...
1068 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
1069 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
1070 VOID
*NewStreamBuffer
;
1071 UINTN NewStreamBufferSize
;
1072 UINT32 AuthenticationStatus
;
1073 RPN_EVENT_CONTEXT
*Context
;
1075 Context
= RpnContext
;
1077 Status
= CloseSectionStream (&mSectionExtraction
, Context
->ChildNode
->EncapsulatedStreamHandle
);
1078 if (!EFI_ERROR (Status
)) {
1080 // The stream closed successfully, so re-open the stream with correct AuthenticationStatus
1083 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*)
1084 (Context
->ParentStream
->StreamBuffer
+ Context
->ChildNode
->OffsetInStream
);
1085 ASSERT (GuidedHeader
->CommonHeader
.Type
== EFI_SECTION_GUID_DEFINED
);
1087 Status
= gBS
->LocateProtocol (Context
->ChildNode
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
1088 ASSERT_EFI_ERROR (Status
);
1091 Status
= GuidedExtraction
->ExtractSection (
1095 &NewStreamBufferSize
,
1096 &AuthenticationStatus
1098 ASSERT_EFI_ERROR (Status
);
1100 // OR in the parent stream's aggregagate status.
1102 AuthenticationStatus
|= Context
->ParentStream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
1103 Status
= OpenSectionStreamEx (
1104 NewStreamBufferSize
,
1107 AuthenticationStatus
,
1108 &Context
->ChildNode
->EncapsulatedStreamHandle
1110 ASSERT_EFI_ERROR (Status
);
1114 // If above, the stream did not close successfully, it indicates it's
1115 // alread been closed by someone, so just destroy the event and be done with
1119 gBS
->CloseEvent (Event
);
1120 gBS
->FreePool (Context
);
1127 IN CORE_SECTION_CHILD_NODE
*ChildNode
1131 Routine Description:
1132 Worker function. Destructor for child nodes.
1135 ChildNode - Indicates the node to destroy
1142 ASSERT (ChildNode
->Signature
== CORE_SECTION_CHILD_SIGNATURE
);
1144 // Remove the child from it's list
1146 RemoveEntryList (&ChildNode
->Link
);
1148 if (ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1150 // If it's an encapsulating section, we close the resulting section stream.
1151 // CloseSectionStream will free all memory associated with the stream.
1153 CloseSectionStream (&mSectionExtraction
, ChildNode
->EncapsulatedStreamHandle
);
1156 // Last, free the child node itself
1158 gBS
->FreePool (ChildNode
);
1164 OpenSectionStreamEx (
1165 IN UINTN SectionStreamLength
,
1166 IN VOID
*SectionStream
,
1167 IN BOOLEAN AllocateBuffer
,
1168 IN UINT32 AuthenticationStatus
,
1169 OUT UINTN
*SectionStreamHandle
1173 Routine Description:
1174 Worker function. Constructor for section streams.
1177 SectionStreamLength - Size in bytes of the section stream.
1178 SectionStream - Buffer containing the new section stream.
1179 AllocateBuffer - Indicates whether the stream buffer is to be copied
1180 or the input buffer is to be used in place.
1181 AuthenticationStatus- Indicates the default authentication status for the
1183 SectionStreamHandle - A pointer to a caller allocated section stream handle.
1186 EFI_SUCCESS - Stream was added to stream database.
1187 EFI_OUT_OF_RESOURCES - memory allocation failed.
1191 CORE_SECTION_STREAM_NODE
*NewStream
;
1195 // Allocate a new stream
1197 NewStream
= AllocatePool (sizeof (CORE_SECTION_STREAM_NODE
));
1198 if (NewStream
== NULL
) {
1199 return EFI_OUT_OF_RESOURCES
;
1202 if (AllocateBuffer
) {
1204 // if we're here, we're double buffering, allocate the buffer and copy the
1207 if (SectionStreamLength
> 0) {
1208 NewStream
->StreamBuffer
= AllocatePool (SectionStreamLength
);
1209 if (NewStream
->StreamBuffer
== NULL
) {
1210 gBS
->FreePool (NewStream
);
1211 return EFI_OUT_OF_RESOURCES
;
1214 // Copy in stream data
1216 CopyMem (NewStream
->StreamBuffer
, SectionStream
, SectionStreamLength
);
1219 // It's possible to have a zero length section stream.
1221 NewStream
->StreamBuffer
= NULL
;
1225 // If were here, the caller has supplied the buffer (it's an internal call)
1226 // so just assign the buffer. This happens when we open section streams
1227 // as a result of expanding an encapsulating section.
1229 NewStream
->StreamBuffer
= SectionStream
;
1233 // Initialize the rest of the section stream
1235 NewStream
->Signature
= CORE_SECTION_STREAM_SIGNATURE
;
1236 NewStream
->StreamHandle
= (UINTN
) NewStream
;
1237 NewStream
->StreamLength
= SectionStreamLength
;
1238 InitializeListHead (&NewStream
->Children
);
1239 NewStream
->AuthenticationStatus
= AuthenticationStatus
;
1242 // Add new stream to stream list
1244 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1245 InsertTailList (&mStreamRoot
, &NewStream
->Link
);
1246 gBS
->RestoreTPL (OldTpl
);
1248 *SectionStreamHandle
= NewStream
->StreamHandle
;
1257 IN UINTN SearchHandle
,
1258 OUT CORE_SECTION_STREAM_NODE
**FoundStream
1262 Routine Description:
1263 Worker function. Search stream database for requested stream handle.
1266 SearchHandle - Indicates which stream to look for.
1267 FoundStream - Output pointer to the found stream.
1270 EFI_SUCCESS - StreamHandle was found and *FoundStream contains
1272 EFI_NOT_FOUND - SearchHandle was not found in the stream database.
1276 CORE_SECTION_STREAM_NODE
*StreamNode
;
1278 if (!IsListEmpty (&mStreamRoot
)) {
1279 StreamNode
= STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot
));
1281 if (StreamNode
->StreamHandle
== SearchHandle
) {
1282 *FoundStream
= StreamNode
;
1284 } else if (IsNodeAtEnd (&mStreamRoot
, &StreamNode
->Link
)) {
1287 StreamNode
= STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot
, &StreamNode
->Link
));
1292 return EFI_NOT_FOUND
;
1298 IsValidSectionStream (
1299 IN VOID
*SectionStream
,
1300 IN UINTN SectionStreamLength
1304 Routine Description:
1305 Check if a stream is valid.
1308 SectionStream - The section stream to be checked
1309 SectionStreamLength - The length of section stream
1318 UINTN SectionLength
;
1319 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
1320 EFI_COMMON_SECTION_HEADER
*NextSectionHeader
;
1323 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*)SectionStream
;
1325 while (TotalLength
< SectionStreamLength
) {
1326 SectionLength
= SECTION_SIZE (SectionHeader
);
1327 TotalLength
+= SectionLength
;
1329 if (TotalLength
== SectionStreamLength
) {
1334 // Move to the next byte following the section...
1336 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) SectionHeader
+ SectionLength
);
1339 // Figure out where the next section begins
1341 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) SectionHeader
+ 3);
1342 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) NextSectionHeader
& ~(UINTN
)3);
1343 TotalLength
+= (UINTN
) NextSectionHeader
- (UINTN
) SectionHeader
;
1344 SectionHeader
= NextSectionHeader
;
1352 CoreCreateProtocolNotifyEvent (
1353 IN EFI_GUID
*ProtocolGuid
,
1354 IN EFI_TPL NotifyTpl
,
1355 IN EFI_EVENT_NOTIFY NotifyFunction
,
1356 IN VOID
*NotifyContext
,
1357 OUT VOID
**Registration
,
1358 IN BOOLEAN SignalFlag
1362 Routine Description:
1364 Create a protocol notification event and return it.
1368 ProtocolGuid - Protocol to register notification event on.
1370 NotifyTpl - Maximum TPL to signal the NotifyFunction.
1372 NotifyFuncition - EFI notification routine.
1374 NotifyContext - Context passed into Event when it is created.
1376 Registration - Registration key returned from RegisterProtocolNotify().
1378 SignalFlag - Boolean value to decide whether kick the event after register or not.
1382 The EFI_EVENT that has been registered to be signaled when a ProtocolGuid
1383 is added to the system.
1394 Status
= gBS
->CreateEvent (
1401 ASSERT_EFI_ERROR (Status
);
1404 // Register for protocol notifactions on this event
1407 Status
= gBS
->RegisterProtocolNotify (
1412 ASSERT_EFI_ERROR (Status
);
1416 // Kick the event so we will perform an initial pass of
1417 // current installed drivers
1419 gBS
->SignalEvent (Event
);