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
217 CustomGuidedSectionExtract (
218 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*This
,
219 IN CONST VOID
*InputSection
,
220 OUT VOID
**OutputBuffer
,
221 OUT UINTN
*OutputSize
,
222 OUT UINT32
*AuthenticationStatus
227 LIST_ENTRY mStreamRoot
= INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot
);
229 EFI_HANDLE mSectionExtractionHandle
= NULL
;
231 EFI_SECTION_EXTRACTION_PROTOCOL mSectionExtraction
= {
237 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL mCustomGuidedSectionExtractionProtocol
= {
238 CustomGuidedSectionExtract
243 InitializeSectionExtraction (
244 IN EFI_HANDLE ImageHandle
,
245 IN EFI_SYSTEM_TABLE
*SystemTable
250 Entry point of the section extraction code. Initializes an instance of the
251 section extraction interface and installs it on a new handle.
254 ImageHandle EFI_HANDLE: A handle for the image that is initializing this driver
255 SystemTable EFI_SYSTEM_TABLE: A pointer to the EFI system table
258 EFI_SUCCESS: Driver initialized successfully
259 EFI_OUT_OF_RESOURCES: Could not allocate needed resources
264 EFI_GUID
*ExtractHandlerGuidTable
;
265 UINTN ExtractHandlerNumber
;
268 // Install SEP to a new handle
270 Status
= CoreInstallProtocolInterface (
271 &mSectionExtractionHandle
,
272 &gEfiSectionExtractionProtocolGuid
,
273 EFI_NATIVE_INTERFACE
,
276 ASSERT_EFI_ERROR (Status
);
279 // Get custom extract guided section method guid list
281 ExtractHandlerNumber
= ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable
);
284 // Install custom guided extraction protocol
286 while (ExtractHandlerNumber
-- > 0) {
287 Status
= CoreInstallProtocolInterface (
288 &mSectionExtractionHandle
,
289 &ExtractHandlerGuidTable
[ExtractHandlerNumber
],
290 EFI_NATIVE_INTERFACE
,
291 &mCustomGuidedSectionExtractionProtocol
293 ASSERT_EFI_ERROR (Status
);
303 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
304 IN UINTN SectionStreamLength
,
305 IN VOID
*SectionStream
,
306 OUT UINTN
*SectionStreamHandle
311 SEP member function. This function creates and returns a new section stream
312 handle to represent the new section stream.
315 This - Indicates the calling context.
316 SectionStreamLength - Size in bytes of the section stream.
317 SectionStream - Buffer containing the new section stream.
318 SectionStreamHandle - A pointer to a caller allocated UINTN that on output
319 contains the new section stream handle.
323 EFI_OUT_OF_RESOURCES - memory allocation failed.
324 EFI_INVALID_PARAMETER - section stream does not end concident with end of
330 // Check to see section stream looks good...
332 if (!IsValidSectionStream (SectionStream
, SectionStreamLength
)) {
333 return EFI_INVALID_PARAMETER
;
336 return OpenSectionStreamEx (
349 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
350 IN UINTN SectionStreamHandle
,
351 IN EFI_SECTION_TYPE
*SectionType
,
352 IN EFI_GUID
*SectionDefinitionGuid
,
353 IN UINTN SectionInstance
,
355 IN OUT UINTN
*BufferSize
,
356 OUT UINT32
*AuthenticationStatus
361 SEP member function. Retrieves requested section from section stream.
364 This: Pointer to SEP instance.
365 SectionStreamHandle: The section stream from which to extract the requested
367 SectionType: A pointer to the type of section to search for.
368 SectionDefinitionGuid: If the section type is EFI_SECTION_GUID_DEFINED, then
369 SectionDefinitionGuid indicates which of these types
370 of sections to search for.
371 SectionInstance: Indicates which instance of the requested section to
373 Buffer: Double indirection to buffer. If *Buffer is non-null on
374 input, then the buffer is caller allocated. If
375 *Buffer is NULL, then the buffer is callee allocated.
376 In either case, the requried buffer size is returned
378 BufferSize: On input, indicates the size of *Buffer if *Buffer is
379 non-null on input. On output, indicates the required
380 size (allocated size if callee allocated) of *Buffer.
381 AuthenticationStatus: Indicates the authentication status of the retrieved
385 EFI_SUCCESS: Section was retrieved successfully
386 EFI_PROTOCOL_ERROR: A GUID defined section was encountered in the section
387 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
388 bit set, but there was no corresponding GUIDed Section
389 Extraction Protocol in the handle database. *Buffer is
391 EFI_NOT_FOUND: An error was encountered when parsing the SectionStream.
392 This indicates the SectionStream is not correctly
394 EFI_NOT_FOUND: The requested section does not exist.
395 EFI_OUT_OF_RESOURCES: The system has insufficient resources to process the
397 EFI_INVALID_PARAMETER: The SectionStreamHandle does not exist.
398 EFI_WARN_TOO_SMALL: The size of the caller allocated input buffer is
399 insufficient to contain the requested section. The
400 input buffer is filled and contents are section contents
405 CORE_SECTION_STREAM_NODE
*StreamNode
;
408 CORE_SECTION_CHILD_NODE
*ChildNode
;
409 CORE_SECTION_STREAM_NODE
*ChildStreamNode
;
411 UINT32 ExtractedAuthenticationStatus
;
415 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
419 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
420 Instance
= SectionInstance
+ 1;
424 // Locate target stream
426 Status
= FindStreamNode (SectionStreamHandle
, &StreamNode
);
427 if (EFI_ERROR (Status
)) {
428 Status
= EFI_INVALID_PARAMETER
;
429 goto GetSection_Done
;
433 // Found the stream, now locate and return the appropriate section
435 if (SectionType
== NULL
) {
437 // SectionType == NULL means return the WHOLE section stream...
439 CopySize
= StreamNode
->StreamLength
;
440 CopyBuffer
= StreamNode
->StreamBuffer
;
441 *AuthenticationStatus
= StreamNode
->AuthenticationStatus
;
444 // There's a requested section type, so go find it and return it...
446 Status
= FindChildNode (
450 SectionDefinitionGuid
,
453 &ExtractedAuthenticationStatus
455 if (EFI_ERROR (Status
)) {
456 goto GetSection_Done
;
458 CopySize
= ChildNode
->Size
- sizeof (EFI_COMMON_SECTION_HEADER
);
459 CopyBuffer
= ChildStreamNode
->StreamBuffer
+ ChildNode
->OffsetInStream
+ sizeof (EFI_COMMON_SECTION_HEADER
);
460 *AuthenticationStatus
= ExtractedAuthenticationStatus
;
463 SectionSize
= CopySize
;
464 if (*Buffer
!= NULL
) {
466 // Caller allocated buffer. Fill to size and return required size...
468 if (*BufferSize
< CopySize
) {
469 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
470 CopySize
= *BufferSize
;
474 // Callee allocated buffer. Allocate buffer and return size.
475 // For FvImage, the buffer is allocated at its required alignment.
477 if (*SectionType
== EFI_SECTION_FIRMWARE_VOLUME_IMAGE
) {
478 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) CopyBuffer
;
479 FvAlignment
= 1 << ((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16);
481 // FvAlignment must be more than 8 bytes required by FvHeader structure.
483 if (FvAlignment
< 8) {
487 *Buffer
= AllocateAlignedPool ((UINTN
) CopySize
, (UINTN
) FvAlignment
);
488 if (*Buffer
== NULL
) {
489 Status
= EFI_OUT_OF_RESOURCES
;
490 goto GetSection_Done
;
493 CopyMem (*Buffer
, CopyBuffer
, CopySize
);
494 *BufferSize
= SectionSize
;
497 CoreRestoreTpl (OldTpl
);
506 IN EFI_SECTION_EXTRACTION_PROTOCOL
*This
,
507 IN UINTN StreamHandleToClose
512 SEP member function. Deletes an existing section stream
515 This - Indicates the calling context.
516 StreamHandleToClose - Indicates the stream to close
520 EFI_OUT_OF_RESOURCES - memory allocation failed.
521 EFI_INVALID_PARAMETER - section stream does not end concident with end of
526 CORE_SECTION_STREAM_NODE
*StreamNode
;
530 CORE_SECTION_CHILD_NODE
*ChildNode
;
532 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
535 // Locate target stream
537 Status
= FindStreamNode (StreamHandleToClose
, &StreamNode
);
538 if (!EFI_ERROR (Status
)) {
540 // Found the stream, so close it
542 RemoveEntryList (&StreamNode
->Link
);
543 while (!IsListEmpty (&StreamNode
->Children
)) {
544 Link
= GetFirstNode (&StreamNode
->Children
);
545 ChildNode
= CHILD_SECTION_NODE_FROM_LINK (Link
);
546 FreeChildNode (ChildNode
);
548 CoreFreePool (StreamNode
->StreamBuffer
);
549 CoreFreePool (StreamNode
);
550 Status
= EFI_SUCCESS
;
552 Status
= EFI_INVALID_PARAMETER
;
555 CoreRestoreTpl (OldTpl
);
563 IN CORE_SECTION_STREAM_NODE
*Stream
,
564 IN CORE_SECTION_CHILD_NODE
*Child
,
565 IN EFI_SECTION_TYPE SearchType
,
566 IN EFI_GUID
*SectionDefinitionGuid
571 Worker function. Determine if the input stream:child matches the input type.
574 Stream - Indicates the section stream associated with the child
575 Child - Indicates the child to check
576 SearchType - Indicates the type of section to check against for
577 SectionDefinitionGuid - Indicates the GUID to check against if the type is
578 EFI_SECTION_GUID_DEFINED
580 TRUE - The child matches
581 FALSE - The child doesn't match
585 EFI_GUID_DEFINED_SECTION
*GuidedSection
;
587 if (SearchType
== EFI_SECTION_ALL
) {
590 if (Child
->Type
!= SearchType
) {
593 if (SearchType
!= EFI_SECTION_GUID_DEFINED
) {
596 GuidedSection
= (EFI_GUID_DEFINED_SECTION
* )(Stream
->StreamBuffer
+ Child
->OffsetInStream
);
597 return CompareGuid (&GuidedSection
->SectionDefinitionGuid
, SectionDefinitionGuid
);
604 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
605 IN EFI_SECTION_TYPE SearchType
,
606 IN OUT UINTN
*SectionInstance
,
607 IN EFI_GUID
*SectionDefinitionGuid
,
608 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
609 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
610 OUT UINT32
*AuthenticationStatus
615 Worker function Recursively searches / builds section stream database
616 looking for requested section.
619 SourceStream - Indicates the section stream in which to do the search.
620 SearchType - Indicates the type of section to search for.
621 SectionInstance - Indicates which instance of section to find. This is
622 an in/out parameter to deal with recursions.
623 SectionDefinitionGuid - Guid of section definition
624 FoundChild - Output indicating the child node that is found.
625 FoundStream - Output indicating which section stream the child was
626 found in. If this stream was generated as a result of
627 an encapsulation section, the streamhandle is visible
628 within the SEP driver only.
629 AuthenticationStatus- Indicates the authentication status of the found section.
632 EFI_SUCCESS - Child node was found and returned.
633 EFI_OUT_OF_RESOURCES- Memory allocation failed.
634 EFI_NOT_FOUND - Requested child node does not exist.
635 EFI_PROTOCOL_ERROR - a required GUIDED section extraction protocol does not
640 CORE_SECTION_CHILD_NODE
*CurrentChildNode
;
641 CORE_SECTION_CHILD_NODE
*RecursedChildNode
;
642 CORE_SECTION_STREAM_NODE
*RecursedFoundStream
;
643 UINT32 NextChildOffset
;
644 EFI_STATUS ErrorStatus
;
647 CurrentChildNode
= NULL
;
648 ErrorStatus
= EFI_NOT_FOUND
;
650 if (SourceStream
->StreamLength
== 0) {
651 return EFI_NOT_FOUND
;
654 if (IsListEmpty (&SourceStream
->Children
) &&
655 SourceStream
->StreamLength
> sizeof (EFI_COMMON_SECTION_HEADER
)) {
657 // This occurs when a section stream exists, but no child sections
658 // have been parsed out yet. Therefore, extract the first child and add it
659 // to the list of children so we can get started.
661 Status
= CreateChildNode (SourceStream
, 0, &CurrentChildNode
);
662 if (EFI_ERROR (Status
)) {
668 // At least one child has been parsed out of the section stream. So, walk
669 // through the sections that have already been parsed out looking for the
670 // requested section, if necessary, continue parsing section stream and
671 // adding children until either the requested section is found, or we run
674 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream
->Children
));
677 if (ChildIsType (SourceStream
, CurrentChildNode
, SearchType
, SectionDefinitionGuid
)) {
679 // The type matches, so check the instance count to see if it's the one we want
681 (*SectionInstance
)--;
682 if (*SectionInstance
== 0) {
686 *FoundChild
= CurrentChildNode
;
687 *FoundStream
= SourceStream
;
688 *AuthenticationStatus
= SourceStream
->AuthenticationStatus
;
693 if (CurrentChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
695 // If the current node is an encapsulating node, recurse into it...
697 Status
= FindChildNode (
698 (CORE_SECTION_STREAM_NODE
*)CurrentChildNode
->EncapsulatedStreamHandle
,
701 SectionDefinitionGuid
,
703 &RecursedFoundStream
,
707 // If the status is not EFI_SUCCESS, just save the error code and continue
708 // to find the request child node in the rest stream.
710 if (*SectionInstance
== 0) {
711 ASSERT_EFI_ERROR (Status
);
712 *FoundChild
= RecursedChildNode
;
713 *FoundStream
= RecursedFoundStream
;
716 ErrorStatus
= Status
;
720 if (!IsNodeAtEnd (&SourceStream
->Children
, &CurrentChildNode
->Link
)) {
722 // We haven't found the child node we're interested in yet, but there's
723 // still more nodes that have already been parsed so get the next one
724 // and continue searching..
726 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream
->Children
, &CurrentChildNode
->Link
));
729 // We've exhausted children that have already been parsed, so see if
730 // there's any more data and continue parsing out more children if there
733 NextChildOffset
= CurrentChildNode
->OffsetInStream
+ CurrentChildNode
->Size
;
735 // Round up to 4 byte boundary
737 NextChildOffset
+= 3;
738 NextChildOffset
&= ~(UINTN
)3;
739 if (NextChildOffset
<= SourceStream
->StreamLength
- sizeof (EFI_COMMON_SECTION_HEADER
)) {
741 // There's an unparsed child remaining in the stream, so create a new child node
743 Status
= CreateChildNode (SourceStream
, NextChildOffset
, &CurrentChildNode
);
744 if (EFI_ERROR (Status
)) {
748 ASSERT (EFI_ERROR (ErrorStatus
));
759 IN CORE_SECTION_STREAM_NODE
*Stream
,
760 IN UINT32 ChildOffset
,
761 OUT CORE_SECTION_CHILD_NODE
**ChildNode
766 Worker function. Constructor for new child nodes.
769 Stream - Indicates the section stream in which to add the child.
770 ChildOffset - Indicates the offset in Stream that is the beginning
771 of the child section.
772 ChildNode - Indicates the Callee allocated and initialized child.
775 EFI_SUCCESS - Child node was found and returned.
776 EFI_OUT_OF_RESOURCES- Memory allocation failed.
777 EFI_PROTOCOL_ERROR - Encapsulation sections produce new stream handles when
778 the child node is created. If the section type is GUID
779 defined, and the extraction GUID does not exist, and
780 producing the stream requires the GUID, then a protocol
781 error is generated and no child is produced.
782 Values returned by OpenSectionStreamEx.
787 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
788 EFI_COMPRESSION_SECTION
*CompressionHeader
;
789 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
790 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
791 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
792 VOID
*NewStreamBuffer
;
795 UINTN NewStreamBufferSize
;
796 UINT32 AuthenticationStatus
;
797 UINT32 SectionLength
;
799 CORE_SECTION_CHILD_NODE
*Node
;
801 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) (Stream
->StreamBuffer
+ ChildOffset
);
804 // Allocate a new node
806 *ChildNode
= CoreAllocateBootServicesPool (sizeof (CORE_SECTION_CHILD_NODE
));
809 return EFI_OUT_OF_RESOURCES
;
815 Node
->Signature
= CORE_SECTION_CHILD_SIGNATURE
;
816 Node
->Type
= SectionHeader
->Type
;
817 Node
->Size
= SECTION_SIZE (SectionHeader
);
818 Node
->OffsetInStream
= ChildOffset
;
819 Node
->EncapsulatedStreamHandle
= NULL_STREAM_HANDLE
;
820 Node
->EncapsulationGuid
= NULL
;
823 // If it's an encapsulating section, then create the new section stream also
825 switch (Node
->Type
) {
826 case EFI_SECTION_COMPRESSION
:
828 // Get the CompressionSectionHeader
830 ASSERT (Node
->Size
>= sizeof (EFI_COMPRESSION_SECTION
));
832 CompressionHeader
= (EFI_COMPRESSION_SECTION
*) SectionHeader
;
835 // Allocate space for the new stream
837 if (CompressionHeader
->UncompressedLength
> 0) {
838 NewStreamBufferSize
= CompressionHeader
->UncompressedLength
;
839 NewStreamBuffer
= CoreAllocateBootServicesPool (NewStreamBufferSize
);
840 if (NewStreamBuffer
== NULL
) {
842 return EFI_OUT_OF_RESOURCES
;
845 if (CompressionHeader
->CompressionType
== EFI_NOT_COMPRESSED
) {
847 // stream is not actually compressed, just encapsulated. So just copy it.
849 CopyMem (NewStreamBuffer
, CompressionHeader
+ 1, NewStreamBufferSize
);
850 } else if (CompressionHeader
->CompressionType
== EFI_STANDARD_COMPRESSION
) {
852 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
856 // Decompress the stream
858 Status
= CoreLocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
860 ASSERT_EFI_ERROR (Status
);
862 Status
= Decompress
->GetInfo (
864 CompressionHeader
+ 1,
865 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
866 (UINT32
*)&NewStreamBufferSize
,
869 ASSERT_EFI_ERROR (Status
);
870 ASSERT (NewStreamBufferSize
== CompressionHeader
->UncompressedLength
);
872 ScratchBuffer
= CoreAllocateBootServicesPool (ScratchSize
);
873 if (ScratchBuffer
== NULL
) {
875 CoreFreePool (NewStreamBuffer
);
876 return EFI_OUT_OF_RESOURCES
;
879 Status
= Decompress
->Decompress (
881 CompressionHeader
+ 1,
882 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
884 (UINT32
)NewStreamBufferSize
,
888 ASSERT_EFI_ERROR (Status
);
889 CoreFreePool (ScratchBuffer
);
892 NewStreamBuffer
= NULL
;
893 NewStreamBufferSize
= 0;
896 Status
= OpenSectionStreamEx (
900 Stream
->AuthenticationStatus
,
901 &Node
->EncapsulatedStreamHandle
903 if (EFI_ERROR (Status
)) {
905 CoreFreePool (NewStreamBuffer
);
910 case EFI_SECTION_GUID_DEFINED
:
911 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*) SectionHeader
;
912 Node
->EncapsulationGuid
= &GuidedHeader
->SectionDefinitionGuid
;
913 Status
= CoreLocateProtocol (Node
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
914 if (!EFI_ERROR (Status
)) {
916 // NewStreamBuffer is always allocated by ExtractSection... No caller
919 Status
= GuidedExtraction
->ExtractSection (
923 &NewStreamBufferSize
,
924 &AuthenticationStatus
926 if (EFI_ERROR (Status
)) {
927 CoreFreePool (*ChildNode
);
928 return EFI_PROTOCOL_ERROR
;
932 // Make sure we initialize the new stream with the correct
933 // authentication status for both aggregate and local status fields.
935 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
937 // OR in the parent stream's aggregate status.
939 AuthenticationStatus
|= Stream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
942 // since there's no authentication data contributed by the section,
943 // just inherit the full value from our immediate parent.
945 AuthenticationStatus
= Stream
->AuthenticationStatus
;
948 Status
= OpenSectionStreamEx (
952 AuthenticationStatus
,
953 &Node
->EncapsulatedStreamHandle
955 if (EFI_ERROR (Status
)) {
956 CoreFreePool (*ChildNode
);
957 CoreFreePool (NewStreamBuffer
);
962 // There's no GUIDed section extraction protocol available.
964 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) {
966 // If the section REQUIRES an extraction protocol, then we're toast
968 CoreFreePool (*ChildNode
);
969 return EFI_PROTOCOL_ERROR
;
973 // Figure out the proper authentication status
975 AuthenticationStatus
= Stream
->AuthenticationStatus
;
976 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
978 // The local status of the new stream is contained in
979 // AuthenticaionStatus. This value needs to be ORed into the
980 // Aggregate bits also...
984 // Clear out and initialize the local status
986 AuthenticationStatus
&= ~EFI_LOCAL_AUTH_STATUS_ALL
;
987 AuthenticationStatus
|= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
;
989 // OR local status into aggregate status
991 AuthenticationStatus
|= AuthenticationStatus
>> 16;
994 SectionLength
= SECTION_SIZE (GuidedHeader
);
995 Status
= OpenSectionStreamEx (
996 SectionLength
- GuidedHeader
->DataOffset
,
997 (UINT8
*) GuidedHeader
+ GuidedHeader
->DataOffset
,
999 AuthenticationStatus
,
1000 &Node
->EncapsulatedStreamHandle
1002 if (EFI_ERROR (Status
)) {
1003 CoreFreePool (Node
);
1008 if ((AuthenticationStatus
& EFI_LOCAL_AUTH_STATUS_ALL
) ==
1009 (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED
| EFI_LOCAL_AUTH_STATUS_NOT_TESTED
)) {
1011 // Need to register for RPN for when the required GUIDed extraction
1012 // protocol becomes available. This will enable us to refresh the
1013 // AuthenticationStatus cached in the Stream if it's ever requested
1016 CreateGuidedExtractionRpnEvent (Stream
, Node
);
1024 // Nothing to do if it's a leaf
1030 // Last, add the new child node to the stream
1032 InsertTailList (&Stream
->Children
, &Node
->Link
);
1040 CreateGuidedExtractionRpnEvent (
1041 IN CORE_SECTION_STREAM_NODE
*ParentStream
,
1042 IN CORE_SECTION_CHILD_NODE
*ChildNode
1046 Routine Description:
1047 Worker function. Constructor for RPN event if needed to keep AuthenticationStatus
1048 cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...
1051 ParentStream - Indicates the parent of the ecnapsulation section (child)
1052 ChildNode - Indicates the child node that is the encapsulation section.
1059 RPN_EVENT_CONTEXT
*Context
;
1062 // Allocate new event structure and context
1064 Context
= CoreAllocateBootServicesPool (sizeof (RPN_EVENT_CONTEXT
));
1065 ASSERT (Context
!= NULL
);
1067 Context
->ChildNode
= ChildNode
;
1068 Context
->ParentStream
= ParentStream
;
1070 Context
->Event
= CoreCreateProtocolNotifyEvent (
1071 Context
->ChildNode
->EncapsulationGuid
,
1073 NotifyGuidedExtraction
,
1075 &Context
->Registration
,
1084 NotifyGuidedExtraction (
1090 Routine Description:
1091 RPN callback function. Removes a stale section stream and re-initializes it
1092 with an updated AuthenticationStatus.
1095 Event - The event that fired
1096 RpnContext - A pointer to the context that allows us to identify
1097 the relevent encapsulation...
1105 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
1106 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
1107 VOID
*NewStreamBuffer
;
1108 UINTN NewStreamBufferSize
;
1109 UINT32 AuthenticationStatus
;
1110 RPN_EVENT_CONTEXT
*Context
;
1112 Context
= RpnContext
;
1114 Status
= CloseSectionStream (&mSectionExtraction
, Context
->ChildNode
->EncapsulatedStreamHandle
);
1115 if (!EFI_ERROR (Status
)) {
1117 // The stream closed successfully, so re-open the stream with correct AuthenticationStatus
1120 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*)
1121 (Context
->ParentStream
->StreamBuffer
+ Context
->ChildNode
->OffsetInStream
);
1122 ASSERT (GuidedHeader
->CommonHeader
.Type
== EFI_SECTION_GUID_DEFINED
);
1124 Status
= CoreLocateProtocol (Context
->ChildNode
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
1125 ASSERT_EFI_ERROR (Status
);
1128 Status
= GuidedExtraction
->ExtractSection (
1132 &NewStreamBufferSize
,
1133 &AuthenticationStatus
1135 ASSERT_EFI_ERROR (Status
);
1137 // OR in the parent stream's aggregagate status.
1139 AuthenticationStatus
|= Context
->ParentStream
->AuthenticationStatus
& EFI_AGGREGATE_AUTH_STATUS_ALL
;
1140 Status
= OpenSectionStreamEx (
1141 NewStreamBufferSize
,
1144 AuthenticationStatus
,
1145 &Context
->ChildNode
->EncapsulatedStreamHandle
1147 ASSERT_EFI_ERROR (Status
);
1151 // If above, the stream did not close successfully, it indicates it's
1152 // alread been closed by someone, so just destroy the event and be done with
1156 CoreCloseEvent (Event
);
1157 CoreFreePool (Context
);
1164 IN CORE_SECTION_CHILD_NODE
*ChildNode
1168 Routine Description:
1169 Worker function. Destructor for child nodes.
1172 ChildNode - Indicates the node to destroy
1179 ASSERT (ChildNode
->Signature
== CORE_SECTION_CHILD_SIGNATURE
);
1181 // Remove the child from it's list
1183 RemoveEntryList (&ChildNode
->Link
);
1185 if (ChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
1187 // If it's an encapsulating section, we close the resulting section stream.
1188 // CloseSectionStream will free all memory associated with the stream.
1190 CloseSectionStream (&mSectionExtraction
, ChildNode
->EncapsulatedStreamHandle
);
1193 // Last, free the child node itself
1195 CoreFreePool (ChildNode
);
1201 OpenSectionStreamEx (
1202 IN UINTN SectionStreamLength
,
1203 IN VOID
*SectionStream
,
1204 IN BOOLEAN AllocateBuffer
,
1205 IN UINT32 AuthenticationStatus
,
1206 OUT UINTN
*SectionStreamHandle
1210 Routine Description:
1211 Worker function. Constructor for section streams.
1214 SectionStreamLength - Size in bytes of the section stream.
1215 SectionStream - Buffer containing the new section stream.
1216 AllocateBuffer - Indicates whether the stream buffer is to be copied
1217 or the input buffer is to be used in place.
1218 AuthenticationStatus- Indicates the default authentication status for the
1220 SectionStreamHandle - A pointer to a caller allocated section stream handle.
1223 EFI_SUCCESS - Stream was added to stream database.
1224 EFI_OUT_OF_RESOURCES - memory allocation failed.
1228 CORE_SECTION_STREAM_NODE
*NewStream
;
1232 // Allocate a new stream
1234 NewStream
= CoreAllocateBootServicesPool (sizeof (CORE_SECTION_STREAM_NODE
));
1235 if (NewStream
== NULL
) {
1236 return EFI_OUT_OF_RESOURCES
;
1239 if (AllocateBuffer
) {
1241 // if we're here, we're double buffering, allocate the buffer and copy the
1244 if (SectionStreamLength
> 0) {
1245 NewStream
->StreamBuffer
= CoreAllocateBootServicesPool (SectionStreamLength
);
1246 if (NewStream
->StreamBuffer
== NULL
) {
1247 CoreFreePool (NewStream
);
1248 return EFI_OUT_OF_RESOURCES
;
1251 // Copy in stream data
1253 CopyMem (NewStream
->StreamBuffer
, SectionStream
, SectionStreamLength
);
1256 // It's possible to have a zero length section stream.
1258 NewStream
->StreamBuffer
= NULL
;
1262 // If were here, the caller has supplied the buffer (it's an internal call)
1263 // so just assign the buffer. This happens when we open section streams
1264 // as a result of expanding an encapsulating section.
1266 NewStream
->StreamBuffer
= SectionStream
;
1270 // Initialize the rest of the section stream
1272 NewStream
->Signature
= CORE_SECTION_STREAM_SIGNATURE
;
1273 NewStream
->StreamHandle
= (UINTN
) NewStream
;
1274 NewStream
->StreamLength
= SectionStreamLength
;
1275 InitializeListHead (&NewStream
->Children
);
1276 NewStream
->AuthenticationStatus
= AuthenticationStatus
;
1279 // Add new stream to stream list
1281 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1282 InsertTailList (&mStreamRoot
, &NewStream
->Link
);
1283 CoreRestoreTpl (OldTpl
);
1285 *SectionStreamHandle
= NewStream
->StreamHandle
;
1294 IN UINTN SearchHandle
,
1295 OUT CORE_SECTION_STREAM_NODE
**FoundStream
1299 Routine Description:
1300 Worker function. Search stream database for requested stream handle.
1303 SearchHandle - Indicates which stream to look for.
1304 FoundStream - Output pointer to the found stream.
1307 EFI_SUCCESS - StreamHandle was found and *FoundStream contains
1309 EFI_NOT_FOUND - SearchHandle was not found in the stream database.
1313 CORE_SECTION_STREAM_NODE
*StreamNode
;
1315 if (!IsListEmpty (&mStreamRoot
)) {
1316 StreamNode
= STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot
));
1318 if (StreamNode
->StreamHandle
== SearchHandle
) {
1319 *FoundStream
= StreamNode
;
1321 } else if (IsNodeAtEnd (&mStreamRoot
, &StreamNode
->Link
)) {
1324 StreamNode
= STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot
, &StreamNode
->Link
));
1329 return EFI_NOT_FOUND
;
1335 IsValidSectionStream (
1336 IN VOID
*SectionStream
,
1337 IN UINTN SectionStreamLength
1341 Routine Description:
1342 Check if a stream is valid.
1345 SectionStream - The section stream to be checked
1346 SectionStreamLength - The length of section stream
1355 UINTN SectionLength
;
1356 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
1357 EFI_COMMON_SECTION_HEADER
*NextSectionHeader
;
1360 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*)SectionStream
;
1362 while (TotalLength
< SectionStreamLength
) {
1363 SectionLength
= SECTION_SIZE (SectionHeader
);
1364 TotalLength
+= SectionLength
;
1366 if (TotalLength
== SectionStreamLength
) {
1371 // Move to the next byte following the section...
1373 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) SectionHeader
+ SectionLength
);
1376 // Figure out where the next section begins
1378 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) SectionHeader
+ 3);
1379 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) NextSectionHeader
& ~(UINTN
)3);
1380 TotalLength
+= (UINTN
) NextSectionHeader
- (UINTN
) SectionHeader
;
1381 SectionHeader
= NextSectionHeader
;
1389 The ExtractSection() function processes the input section and
1390 allocates a buffer from the pool in which it returns the section
1391 contents. If the section being extracted contains
1392 authentication information (the section's
1393 GuidedSectionHeader.Attributes field has the
1394 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values
1395 returned in AuthenticationStatus must reflect the results of
1396 the authentication operation. Depending on the algorithm and
1397 size of the encapsulated data, the time that is required to do
1398 a full authentication may be prohibitively long for some
1399 classes of systems. To indicate this, use
1400 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by
1401 the security policy driver (see the Platform Initialization
1402 Driver Execution Environment Core Interface Specification for
1403 more details and the GUID definition). If the
1404 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle
1405 database, then, if possible, full authentication should be
1406 skipped and the section contents simply returned in the
1407 OutputBuffer. In this case, the
1408 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus
1409 must be set on return. ExtractSection() is callable only from
1410 TPL_NOTIFY and below. Behavior of ExtractSection() at any
1411 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is
1412 defined in RaiseTPL() in the UEFI 2.0 specification.
1415 @param This Indicates the
1416 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.
1418 @param InputSection Buffer containing the input GUIDed section
1419 to be processed. OutputBuffer OutputBuffer
1420 is allocated from boot services pool
1421 memory and contains the new section
1422 stream. The caller is responsible for
1423 freeing this buffer.
1425 @param OutputSize A pointer to a caller-allocated UINTN in
1426 which the size of OutputBuffer allocation
1427 is stored. If the function returns
1428 anything other than EFI_SUCCESS, the value
1429 of OutputSize is undefined.
1431 @param AuthenticationStatus A pointer to a caller-allocated
1432 UINT32 that indicates the
1433 authentication status of the
1434 output buffer. If the input
1436 GuidedSectionHeader.Attributes
1438 EFI_GUIDED_SECTION_AUTH_STATUS_VAL
1439 bit as clear, AuthenticationStatus
1440 must return zero. Both local bits
1441 (19:16) and aggregate bits (3:0)
1442 in AuthenticationStatus are
1443 returned by ExtractSection().
1444 These bits reflect the status of
1445 the extraction operation. The bit
1446 pattern in both regions must be
1447 the same, as the local and
1448 aggregate authentication statuses
1449 have equivalent meaning at this
1450 level. If the function returns
1451 anything other than EFI_SUCCESS,
1452 the value of AuthenticationStatus
1456 @retval EFI_SUCCESS The InputSection was successfully
1457 processed and the section contents were
1460 @retval EFI_OUT_OF_RESOURCES The system has insufficient
1461 resources to process the
1464 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
1465 not match this instance of the
1466 GUIDed Section Extraction
1471 CustomGuidedSectionExtract (
1472 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*This
,
1473 IN CONST VOID
*InputSection
,
1474 OUT VOID
**OutputBuffer
,
1475 OUT UINTN
*OutputSize
,
1476 OUT UINT32
*AuthenticationStatus
1480 VOID
*ScratchBuffer
;
1481 VOID
*AllocatedOutputBuffer
;
1482 UINT32 OutputBufferSize
;
1483 UINT32 ScratchBufferSize
;
1484 UINT16 SectionAttribute
;
1487 // Init local variable
1489 ScratchBuffer
= NULL
;
1490 AllocatedOutputBuffer
= NULL
;
1493 // Call GetInfo to get the size and attribute of input guided section data.
1495 Status
= ExtractGuidedSectionGetInfo (
1502 if (EFI_ERROR (Status
)) {
1503 DEBUG ((EFI_D_ERROR
, "GetInfo from guided section Failed - %r\n", Status
));
1507 if (ScratchBufferSize
!= 0) {
1509 // Allocate scratch buffer
1511 ScratchBuffer
= CoreAllocateBootServicesPool (ScratchBufferSize
);
1512 if (ScratchBuffer
== NULL
) {
1513 return EFI_OUT_OF_RESOURCES
;
1517 if (OutputBufferSize
> 0) {
1519 // Allocate output buffer
1521 AllocatedOutputBuffer
= CoreAllocateBootServicesPool (OutputBufferSize
);
1522 if (AllocatedOutputBuffer
== NULL
) {
1523 return EFI_OUT_OF_RESOURCES
;
1525 *OutputBuffer
= AllocatedOutputBuffer
;
1529 // Call decode function to extract raw data from the guided section.
1531 Status
= ExtractGuidedSectionDecode (
1535 AuthenticationStatus
1537 if (EFI_ERROR (Status
)) {
1541 if (AllocatedOutputBuffer
!= NULL
) {
1542 CoreFreePool (AllocatedOutputBuffer
);
1544 if (ScratchBuffer
!= NULL
) {
1545 CoreFreePool (ScratchBuffer
);
1547 DEBUG ((EFI_D_ERROR
, "Extract guided section Failed - %r\n", Status
));
1551 if (*OutputBuffer
!= AllocatedOutputBuffer
) {
1553 // OutputBuffer was returned as a different value,
1554 // so copy section contents to the allocated memory buffer.
1556 CopyMem (AllocatedOutputBuffer
, *OutputBuffer
, OutputBufferSize
);
1557 *OutputBuffer
= AllocatedOutputBuffer
;
1561 // Set real size of output buffer.
1563 *OutputSize
= (UINTN
) OutputBufferSize
;
1566 // Free unused scratch buffer.
1568 if (ScratchBuffer
!= NULL
) {
1569 CoreFreePool (ScratchBuffer
);