2 Section Extraction Protocol implementation.
4 Stream database is implemented as a linked list of section streams,
5 where each stream contains a linked list of children, which may be leaves or
8 Children that are encapsulations generate new stream entries
9 when they are created. Streams can also be created by calls to
10 SEP->OpenSectionStream().
12 The database is only created far enough to return the requested data from
13 any given stream, or to determine that the requested data is not found.
15 If a GUIDed encapsulation is encountered, there are three possiblilites.
17 1) A support protocol is found, in which the stream is simply processed with
20 2) A support protocol is not found, but the data is available to be read
21 without processing. In this case, the database is built up through the
22 recursions to return the data, and a RPN event is set that will enable
23 the stream in question to be refreshed if and when the required section
24 extraction protocol is published.This insures the AuthenticationStatus
25 does not become stale in the cache.
27 3) A support protocol is not found, and the data is not available to be read
28 without it. This results in EFI_PROTOCOL_ERROR.
30 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
31 All rights reserved. This program and the accompanying materials
32 are licensed and made available under the terms and conditions of the BSD License
33 which accompanies this distribution. The full text of the license may be found at
34 http://opensource.org/licenses/bsd-license.php
36 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
37 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
44 // Local defines and typedefs
46 #define CORE_SECTION_CHILD_SIGNATURE EFI_SIGNATURE_32('S','X','C','S')
47 #define CHILD_SECTION_NODE_FROM_LINK(Node) \
48 CR (Node, CORE_SECTION_CHILD_NODE, Link, CORE_SECTION_CHILD_SIGNATURE)
56 // StreamBase + OffsetInStream == pointer to section header in stream. The
57 // stream base is always known when walking the sections within.
59 UINT32 OffsetInStream
;
61 // Then EncapsulatedStreamHandle below is always 0 if the section is NOT an
62 // encapsulating section. Otherwise, it contains the stream handle
63 // of the encapsulated stream. This handle is ALWAYS produced any time an
64 // encapsulating child is encountered, irrespective of whether the
65 // encapsulated stream is processed further.
67 UINTN EncapsulatedStreamHandle
;
68 EFI_GUID
*EncapsulationGuid
;
69 } CORE_SECTION_CHILD_NODE
;
71 #define CORE_SECTION_STREAM_SIGNATURE EFI_SIGNATURE_32('S','X','S','S')
72 #define STREAM_NODE_FROM_LINK(Node) \
73 CR (Node, CORE_SECTION_STREAM_NODE, Link, CORE_SECTION_STREAM_SIGNATURE)
83 // Authentication status is from GUIDed encapsulations.
85 UINT32 AuthenticationStatus
;
86 } CORE_SECTION_STREAM_NODE
;
88 #define NULL_STREAM_HANDLE 0
91 CORE_SECTION_CHILD_NODE
*ChildNode
;
92 CORE_SECTION_STREAM_NODE
*ParentStream
;
103 Worker function. Determine if the input stream:child matches the input type.
105 @param Stream Indicates the section stream associated with the
107 @param Child Indicates the child to check
108 @param SearchType Indicates the type of section to check against
110 @param SectionDefinitionGuid Indicates the GUID to check against if the type
111 is EFI_SECTION_GUID_DEFINED
113 @retval TRUE The child matches
114 @retval FALSE The child doesn't match
119 IN CORE_SECTION_STREAM_NODE
*Stream
,
120 IN CORE_SECTION_CHILD_NODE
*Child
,
121 IN EFI_SECTION_TYPE SearchType
,
122 IN EFI_GUID
*SectionDefinitionGuid
127 Worker function. Search stream database for requested stream handle.
129 @param SearchHandle Indicates which stream to look for.
130 @param FoundStream Output pointer to the found stream.
132 @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains
134 @retval EFI_NOT_FOUND SearchHandle was not found in the stream
140 IN UINTN SearchHandle
,
141 OUT CORE_SECTION_STREAM_NODE
**FoundStream
146 Worker function Recursively searches / builds section stream database
147 looking for requested section.
149 @param SourceStream Indicates the section stream in which to do the
151 @param SearchType Indicates the type of section to search for.
152 @param SectionInstance Indicates which instance of section to find.
153 This is an in/out parameter to deal with
155 @param SectionDefinitionGuid Guid of section definition
156 @param FoundChild Output indicating the child node that is found.
157 @param FoundStream Output indicating which section stream the child
158 was found in. If this stream was generated as a
159 result of an encapsulation section, the
160 streamhandle is visible within the SEP driver
162 @param AuthenticationStatus Indicates the authentication status of the found section.
164 @retval EFI_SUCCESS Child node was found and returned.
165 EFI_OUT_OF_RESOURCES- Memory allocation failed.
166 @retval EFI_NOT_FOUND Requested child node does not exist.
167 @retval EFI_PROTOCOL_ERROR a required GUIDED section extraction protocol
173 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
174 IN EFI_SECTION_TYPE SearchType
,
175 IN OUT UINTN
*SectionInstance
,
176 IN EFI_GUID
*SectionDefinitionGuid
,
177 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
178 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
179 OUT UINT32
*AuthenticationStatus
184 Worker function. Constructor for new child nodes.
186 @param Stream Indicates the section stream in which to add the
188 @param ChildOffset Indicates the offset in Stream that is the
189 beginning of the child section.
190 @param ChildNode Indicates the Callee allocated and initialized
193 @retval EFI_SUCCESS Child node was found and returned.
194 EFI_OUT_OF_RESOURCES- Memory allocation failed.
195 @retval EFI_PROTOCOL_ERROR Encapsulation sections produce new stream
196 handles when the child node is created. If the
197 section type is GUID defined, and the extraction
198 GUID does not exist, and producing the stream
199 requires the GUID, then a protocol error is
200 generated and no child is produced. Values
201 returned by OpenSectionStreamEx.
206 IN CORE_SECTION_STREAM_NODE
*Stream
,
207 IN UINT32 ChildOffset
,
208 OUT CORE_SECTION_CHILD_NODE
**ChildNode
213 Worker function. Destructor for child nodes.
215 @param ChildNode Indicates the node to destroy
220 IN CORE_SECTION_CHILD_NODE
*ChildNode
225 Worker function. Constructor for section streams.
227 @param SectionStreamLength Size in bytes of the section stream.
228 @param SectionStream Buffer containing the new section stream.
229 @param AllocateBuffer Indicates whether the stream buffer is to be
230 copied or the input buffer is to be used in
231 place. AuthenticationStatus- Indicates the
232 default authentication status for the new
234 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that
235 indicates the authentication status of the
236 output buffer. If the input section's
237 GuidedSectionHeader.Attributes field
238 has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID
239 bit as clear, AuthenticationStatus must return
240 zero. Both local bits (19:16) and aggregate
241 bits (3:0) in AuthenticationStatus are returned
242 by ExtractSection(). These bits reflect the
243 status of the extraction operation. The bit
244 pattern in both regions must be the same, as
245 the local and aggregate authentication statuses
246 have equivalent meaning at this level. If the
247 function returns anything other than
248 EFI_SUCCESS, the value of *AuthenticationStatus
250 @param SectionStreamHandle A pointer to a caller allocated section stream
253 @retval EFI_SUCCESS Stream was added to stream database.
254 @retval EFI_OUT_OF_RESOURCES memory allocation failed.
258 OpenSectionStreamEx (
259 IN UINTN SectionStreamLength
,
260 IN VOID
*SectionStream
,
261 IN BOOLEAN AllocateBuffer
,
262 IN UINT32 AuthenticationStatus
,
263 OUT UINTN
*SectionStreamHandle
268 Check if a stream is valid.
270 @param SectionStream The section stream to be checked
271 @param SectionStreamLength The length of section stream
273 @return A boolean value indicating the validness of the section stream.
277 IsValidSectionStream (
278 IN VOID
*SectionStream
,
279 IN UINTN SectionStreamLength
284 The ExtractSection() function processes the input section and
285 allocates a buffer from the pool in which it returns the section
286 contents. If the section being extracted contains
287 authentication information (the section's
288 GuidedSectionHeader.Attributes field has the
289 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values
290 returned in AuthenticationStatus must reflect the results of
291 the authentication operation. Depending on the algorithm and
292 size of the encapsulated data, the time that is required to do
293 a full authentication may be prohibitively long for some
294 classes of systems. To indicate this, use
295 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by
296 the security policy driver (see the Platform Initialization
297 Driver Execution Environment Core Interface Specification for
298 more details and the GUID definition). If the
299 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle
300 database, then, if possible, full authentication should be
301 skipped and the section contents simply returned in the
302 OutputBuffer. In this case, the
303 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus
304 must be set on return. ExtractSection() is callable only from
305 TPL_NOTIFY and below. Behavior of ExtractSection() at any
306 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is
307 defined in RaiseTPL() in the UEFI 2.0 specification.
310 @param This Indicates the
311 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.
312 @param InputSection Buffer containing the input GUIDed section
313 to be processed. OutputBuffer OutputBuffer
314 is allocated from boot services pool
315 memory and contains the new section
316 stream. The caller is responsible for
318 @param OutputBuffer *OutputBuffer is allocated from boot services
319 pool memory and contains the new section stream.
320 The caller is responsible for freeing this buffer.
321 @param OutputSize A pointer to a caller-allocated UINTN in
322 which the size of OutputBuffer allocation
323 is stored. If the function returns
324 anything other than EFI_SUCCESS, the value
325 of OutputSize is undefined.
326 @param AuthenticationStatus A pointer to a caller-allocated
327 UINT32 that indicates the
328 authentication status of the
329 output buffer. If the input
331 GuidedSectionHeader.Attributes
333 EFI_GUIDED_SECTION_AUTH_STATUS_VAL
334 bit as clear, AuthenticationStatus
335 must return zero. Both local bits
336 (19:16) and aggregate bits (3:0)
337 in AuthenticationStatus are
338 returned by ExtractSection().
339 These bits reflect the status of
340 the extraction operation. The bit
341 pattern in both regions must be
342 the same, as the local and
343 aggregate authentication statuses
344 have equivalent meaning at this
345 level. If the function returns
346 anything other than EFI_SUCCESS,
347 the value of AuthenticationStatus
351 @retval EFI_SUCCESS The InputSection was successfully
352 processed and the section contents were
355 @retval EFI_OUT_OF_RESOURCES The system has insufficient
356 resources to process the
359 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
360 not match this instance of the
361 GUIDed Section Extraction
367 CustomGuidedSectionExtract (
368 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*This
,
369 IN CONST VOID
*InputSection
,
370 OUT VOID
**OutputBuffer
,
371 OUT UINTN
*OutputSize
,
372 OUT UINT32
*AuthenticationStatus
378 LIST_ENTRY mStreamRoot
= INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot
);
380 EFI_HANDLE mSectionExtractionHandle
= NULL
;
382 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL mCustomGuidedSectionExtractionProtocol
= {
383 CustomGuidedSectionExtract
388 Entry point of the section extraction code. Initializes an instance of the
389 section extraction interface and installs it on a new handle.
391 @param ImageHandle A handle for the image that is initializing this driver
392 @param SystemTable A pointer to the EFI system table
394 @retval EFI_SUCCESS Driver initialized successfully
395 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
400 InitializeSectionExtraction (
401 IN EFI_HANDLE ImageHandle
,
402 IN EFI_SYSTEM_TABLE
*SystemTable
406 EFI_GUID
*ExtractHandlerGuidTable
;
407 UINTN ExtractHandlerNumber
;
410 // Get custom extract guided section method guid list
412 ExtractHandlerNumber
= ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable
);
414 Status
= EFI_SUCCESS
;
416 // Install custom guided extraction protocol
418 while (ExtractHandlerNumber
-- > 0) {
419 Status
= CoreInstallProtocolInterface (
420 &mSectionExtractionHandle
,
421 &ExtractHandlerGuidTable
[ExtractHandlerNumber
],
422 EFI_NATIVE_INTERFACE
,
423 &mCustomGuidedSectionExtractionProtocol
425 ASSERT_EFI_ERROR (Status
);
433 SEP member function. This function creates and returns a new section stream
434 handle to represent the new section stream.
436 @param SectionStreamLength Size in bytes of the section stream.
437 @param SectionStream Buffer containing the new section stream.
438 @param SectionStreamHandle A pointer to a caller allocated UINTN that on
439 output contains the new section stream handle.
441 @retval EFI_SUCCESS The section stream is created successfully.
442 @retval EFI_OUT_OF_RESOURCES memory allocation failed.
443 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end
450 IN UINTN SectionStreamLength
,
451 IN VOID
*SectionStream
,
452 OUT UINTN
*SectionStreamHandle
456 // Check to see section stream looks good...
458 if (!IsValidSectionStream (SectionStream
, SectionStreamLength
)) {
459 return EFI_INVALID_PARAMETER
;
462 return OpenSectionStreamEx (
473 SEP member function. Retrieves requested section from section stream.
475 @param SectionStreamHandle The section stream from which to extract the
477 @param SectionType A pointer to the type of section to search for.
478 @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED,
479 then SectionDefinitionGuid indicates which of
480 these types of sections to search for.
481 @param SectionInstance Indicates which instance of the requested
483 @param Buffer Double indirection to buffer. If *Buffer is
484 non-null on input, then the buffer is caller
485 allocated. If Buffer is NULL, then the buffer
486 is callee allocated. In either case, the
487 requried buffer size is returned in *BufferSize.
488 @param BufferSize On input, indicates the size of *Buffer if
489 *Buffer is non-null on input. On output,
490 indicates the required size (allocated size if
491 callee allocated) of *Buffer.
492 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that
493 indicates the authentication status of the
494 output buffer. If the input section's
495 GuidedSectionHeader.Attributes field
496 has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID
497 bit as clear, AuthenticationStatus must return
498 zero. Both local bits (19:16) and aggregate
499 bits (3:0) in AuthenticationStatus are returned
500 by ExtractSection(). These bits reflect the
501 status of the extraction operation. The bit
502 pattern in both regions must be the same, as
503 the local and aggregate authentication statuses
504 have equivalent meaning at this level. If the
505 function returns anything other than
506 EFI_SUCCESS, the value of *AuthenticationStatus
509 @retval EFI_SUCCESS Section was retrieved successfully
510 @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the
511 section stream with its
512 EFI_GUIDED_SECTION_PROCESSING_REQUIRED bit set,
513 but there was no corresponding GUIDed Section
514 Extraction Protocol in the handle database.
515 *Buffer is unmodified.
516 @retval EFI_NOT_FOUND An error was encountered when parsing the
517 SectionStream. This indicates the SectionStream
518 is not correctly formatted.
519 @retval EFI_NOT_FOUND The requested section does not exist.
520 @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process
522 @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.
523 @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is
524 insufficient to contain the requested section.
525 The input buffer is filled and section contents
532 IN UINTN SectionStreamHandle
,
533 IN EFI_SECTION_TYPE
*SectionType
,
534 IN EFI_GUID
*SectionDefinitionGuid
,
535 IN UINTN SectionInstance
,
537 IN OUT UINTN
*BufferSize
,
538 OUT UINT32
*AuthenticationStatus
541 CORE_SECTION_STREAM_NODE
*StreamNode
;
544 CORE_SECTION_CHILD_NODE
*ChildNode
;
545 CORE_SECTION_STREAM_NODE
*ChildStreamNode
;
547 UINT32 ExtractedAuthenticationStatus
;
553 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
554 Instance
= SectionInstance
+ 1;
557 // Locate target stream
559 Status
= FindStreamNode (SectionStreamHandle
, &StreamNode
);
560 if (EFI_ERROR (Status
)) {
561 Status
= EFI_INVALID_PARAMETER
;
562 goto GetSection_Done
;
566 // Found the stream, now locate and return the appropriate section
568 if (SectionType
== NULL
) {
570 // SectionType == NULL means return the WHOLE section stream...
572 CopySize
= StreamNode
->StreamLength
;
573 CopyBuffer
= StreamNode
->StreamBuffer
;
574 *AuthenticationStatus
= StreamNode
->AuthenticationStatus
;
577 // There's a requested section type, so go find it and return it...
579 Status
= FindChildNode (
583 SectionDefinitionGuid
,
586 &ExtractedAuthenticationStatus
588 if (EFI_ERROR (Status
)) {
589 goto GetSection_Done
;
591 CopySize
= ChildNode
->Size
- sizeof (EFI_COMMON_SECTION_HEADER
);
592 CopyBuffer
= ChildStreamNode
->StreamBuffer
+ ChildNode
->OffsetInStream
+ sizeof (EFI_COMMON_SECTION_HEADER
);
593 *AuthenticationStatus
= ExtractedAuthenticationStatus
;
596 SectionSize
= CopySize
;
597 if (*Buffer
!= NULL
) {
599 // Caller allocated buffer. Fill to size and return required size...
601 if (*BufferSize
< CopySize
) {
602 Status
= EFI_WARN_BUFFER_TOO_SMALL
;
603 CopySize
= *BufferSize
;
607 // Callee allocated buffer. Allocate buffer and return size.
609 *Buffer
= CoreAllocateBootServicesPool (CopySize
);
610 if (*Buffer
== NULL
) {
611 Status
= EFI_OUT_OF_RESOURCES
;
612 goto GetSection_Done
;
615 CopyMem (*Buffer
, CopyBuffer
, CopySize
);
616 *BufferSize
= SectionSize
;
619 CoreRestoreTpl (OldTpl
);
627 SEP member function. Deletes an existing section stream
629 @param StreamHandleToClose Indicates the stream to close
631 @retval EFI_SUCCESS The section stream is closed sucessfully.
632 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
633 @retval EFI_INVALID_PARAMETER Section stream does not end concident with end
640 IN UINTN StreamHandleToClose
643 CORE_SECTION_STREAM_NODE
*StreamNode
;
647 CORE_SECTION_CHILD_NODE
*ChildNode
;
649 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
652 // Locate target stream
654 Status
= FindStreamNode (StreamHandleToClose
, &StreamNode
);
655 if (!EFI_ERROR (Status
)) {
657 // Found the stream, so close it
659 RemoveEntryList (&StreamNode
->Link
);
660 while (!IsListEmpty (&StreamNode
->Children
)) {
661 Link
= GetFirstNode (&StreamNode
->Children
);
662 ChildNode
= CHILD_SECTION_NODE_FROM_LINK (Link
);
663 FreeChildNode (ChildNode
);
665 CoreFreePool (StreamNode
->StreamBuffer
);
666 CoreFreePool (StreamNode
);
667 Status
= EFI_SUCCESS
;
669 Status
= EFI_INVALID_PARAMETER
;
672 CoreRestoreTpl (OldTpl
);
679 Worker function. Determine if the input stream:child matches the input type.
681 @param Stream Indicates the section stream associated with the
683 @param Child Indicates the child to check
684 @param SearchType Indicates the type of section to check against
686 @param SectionDefinitionGuid Indicates the GUID to check against if the type
687 is EFI_SECTION_GUID_DEFINED
689 @retval TRUE The child matches
690 @retval FALSE The child doesn't match
695 IN CORE_SECTION_STREAM_NODE
*Stream
,
696 IN CORE_SECTION_CHILD_NODE
*Child
,
697 IN EFI_SECTION_TYPE SearchType
,
698 IN EFI_GUID
*SectionDefinitionGuid
701 EFI_GUID_DEFINED_SECTION
*GuidedSection
;
703 if (SearchType
== EFI_SECTION_ALL
) {
706 if (Child
->Type
!= SearchType
) {
709 if (SearchType
!= EFI_SECTION_GUID_DEFINED
) {
712 GuidedSection
= (EFI_GUID_DEFINED_SECTION
* )(Stream
->StreamBuffer
+ Child
->OffsetInStream
);
713 return CompareGuid (&GuidedSection
->SectionDefinitionGuid
, SectionDefinitionGuid
);
718 Worker function Recursively searches / builds section stream database
719 looking for requested section.
721 @param SourceStream Indicates the section stream in which to do the
723 @param SearchType Indicates the type of section to search for.
724 @param SectionInstance Indicates which instance of section to find.
725 This is an in/out parameter to deal with
727 @param SectionDefinitionGuid Guid of section definition
728 @param FoundChild Output indicating the child node that is found.
729 @param FoundStream Output indicating which section stream the child
730 was found in. If this stream was generated as a
731 result of an encapsulation section, the
732 streamhandle is visible within the SEP driver
734 @param AuthenticationStatus Indicates the authentication status of the found section.
736 @retval EFI_SUCCESS Child node was found and returned.
737 EFI_OUT_OF_RESOURCES- Memory allocation failed.
738 @retval EFI_NOT_FOUND Requested child node does not exist.
739 @retval EFI_PROTOCOL_ERROR a required GUIDED section extraction protocol
745 IN CORE_SECTION_STREAM_NODE
*SourceStream
,
746 IN EFI_SECTION_TYPE SearchType
,
747 IN OUT UINTN
*SectionInstance
,
748 IN EFI_GUID
*SectionDefinitionGuid
,
749 OUT CORE_SECTION_CHILD_NODE
**FoundChild
,
750 OUT CORE_SECTION_STREAM_NODE
**FoundStream
,
751 OUT UINT32
*AuthenticationStatus
754 CORE_SECTION_CHILD_NODE
*CurrentChildNode
;
755 CORE_SECTION_CHILD_NODE
*RecursedChildNode
;
756 CORE_SECTION_STREAM_NODE
*RecursedFoundStream
;
757 UINT32 NextChildOffset
;
758 EFI_STATUS ErrorStatus
;
761 CurrentChildNode
= NULL
;
762 ErrorStatus
= EFI_NOT_FOUND
;
764 if (SourceStream
->StreamLength
== 0) {
765 return EFI_NOT_FOUND
;
768 if (IsListEmpty (&SourceStream
->Children
) &&
769 SourceStream
->StreamLength
>= sizeof (EFI_COMMON_SECTION_HEADER
)) {
771 // This occurs when a section stream exists, but no child sections
772 // have been parsed out yet. Therefore, extract the first child and add it
773 // to the list of children so we can get started.
774 // Section stream may contain an array of zero or more bytes.
775 // So, its size should be >= the size of commen section header.
777 Status
= CreateChildNode (SourceStream
, 0, &CurrentChildNode
);
778 if (EFI_ERROR (Status
)) {
784 // At least one child has been parsed out of the section stream. So, walk
785 // through the sections that have already been parsed out looking for the
786 // requested section, if necessary, continue parsing section stream and
787 // adding children until either the requested section is found, or we run
790 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream
->Children
));
793 if (ChildIsType (SourceStream
, CurrentChildNode
, SearchType
, SectionDefinitionGuid
)) {
795 // The type matches, so check the instance count to see if it's the one we want
797 (*SectionInstance
)--;
798 if (*SectionInstance
== 0) {
802 *FoundChild
= CurrentChildNode
;
803 *FoundStream
= SourceStream
;
804 *AuthenticationStatus
= SourceStream
->AuthenticationStatus
;
809 if (CurrentChildNode
->EncapsulatedStreamHandle
!= NULL_STREAM_HANDLE
) {
811 // If the current node is an encapsulating node, recurse into it...
813 Status
= FindChildNode (
814 (CORE_SECTION_STREAM_NODE
*)CurrentChildNode
->EncapsulatedStreamHandle
,
817 SectionDefinitionGuid
,
819 &RecursedFoundStream
,
823 // If the status is not EFI_SUCCESS, just save the error code and continue
824 // to find the request child node in the rest stream.
826 if (*SectionInstance
== 0) {
827 ASSERT_EFI_ERROR (Status
);
828 *FoundChild
= RecursedChildNode
;
829 *FoundStream
= RecursedFoundStream
;
832 ErrorStatus
= Status
;
836 if (!IsNodeAtEnd (&SourceStream
->Children
, &CurrentChildNode
->Link
)) {
838 // We haven't found the child node we're interested in yet, but there's
839 // still more nodes that have already been parsed so get the next one
840 // and continue searching..
842 CurrentChildNode
= CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream
->Children
, &CurrentChildNode
->Link
));
845 // We've exhausted children that have already been parsed, so see if
846 // there's any more data and continue parsing out more children if there
849 NextChildOffset
= CurrentChildNode
->OffsetInStream
+ CurrentChildNode
->Size
;
851 // Round up to 4 byte boundary
853 NextChildOffset
+= 3;
854 NextChildOffset
&= ~(UINTN
) 3;
855 if (NextChildOffset
<= SourceStream
->StreamLength
- sizeof (EFI_COMMON_SECTION_HEADER
)) {
857 // There's an unparsed child remaining in the stream, so create a new child node
859 Status
= CreateChildNode (SourceStream
, NextChildOffset
, &CurrentChildNode
);
860 if (EFI_ERROR (Status
)) {
864 ASSERT (EFI_ERROR (ErrorStatus
));
873 Worker function. Constructor for new child nodes.
875 @param Stream Indicates the section stream in which to add the
877 @param ChildOffset Indicates the offset in Stream that is the
878 beginning of the child section.
879 @param ChildNode Indicates the Callee allocated and initialized
882 @retval EFI_SUCCESS Child node was found and returned.
883 EFI_OUT_OF_RESOURCES- Memory allocation failed.
884 @retval EFI_PROTOCOL_ERROR Encapsulation sections produce new stream
885 handles when the child node is created. If the
886 section type is GUID defined, and the extraction
887 GUID does not exist, and producing the stream
888 requires the GUID, then a protocol error is
889 generated and no child is produced. Values
890 returned by OpenSectionStreamEx.
895 IN CORE_SECTION_STREAM_NODE
*Stream
,
896 IN UINT32 ChildOffset
,
897 OUT CORE_SECTION_CHILD_NODE
**ChildNode
901 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
902 EFI_COMPRESSION_SECTION
*CompressionHeader
;
903 EFI_GUID_DEFINED_SECTION
*GuidedHeader
;
904 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
905 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*GuidedExtraction
;
906 VOID
*NewStreamBuffer
;
909 UINTN NewStreamBufferSize
;
910 UINT32 AuthenticationStatus
;
911 UINT32 SectionLength
;
913 CORE_SECTION_CHILD_NODE
*Node
;
915 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) (Stream
->StreamBuffer
+ ChildOffset
);
918 // Allocate a new node
920 *ChildNode
= CoreAllocateBootServicesPool (sizeof (CORE_SECTION_CHILD_NODE
));
923 return EFI_OUT_OF_RESOURCES
;
929 Node
->Signature
= CORE_SECTION_CHILD_SIGNATURE
;
930 Node
->Type
= SectionHeader
->Type
;
931 Node
->Size
= SECTION_SIZE (SectionHeader
);
932 Node
->OffsetInStream
= ChildOffset
;
933 Node
->EncapsulatedStreamHandle
= NULL_STREAM_HANDLE
;
934 Node
->EncapsulationGuid
= NULL
;
937 // If it's an encapsulating section, then create the new section stream also
939 switch (Node
->Type
) {
940 case EFI_SECTION_COMPRESSION
:
942 // Get the CompressionSectionHeader
944 ASSERT (Node
->Size
>= sizeof (EFI_COMPRESSION_SECTION
));
946 CompressionHeader
= (EFI_COMPRESSION_SECTION
*) SectionHeader
;
949 // Allocate space for the new stream
951 if (CompressionHeader
->UncompressedLength
> 0) {
952 NewStreamBufferSize
= CompressionHeader
->UncompressedLength
;
953 NewStreamBuffer
= CoreAllocateBootServicesPool (NewStreamBufferSize
);
954 if (NewStreamBuffer
== NULL
) {
956 return EFI_OUT_OF_RESOURCES
;
959 if (CompressionHeader
->CompressionType
== EFI_NOT_COMPRESSED
) {
961 // stream is not actually compressed, just encapsulated. So just copy it.
963 CopyMem (NewStreamBuffer
, CompressionHeader
+ 1, NewStreamBufferSize
);
964 } else if (CompressionHeader
->CompressionType
== EFI_STANDARD_COMPRESSION
) {
966 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
970 // Decompress the stream
972 Status
= CoreLocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
974 ASSERT_EFI_ERROR (Status
);
976 Status
= Decompress
->GetInfo (
978 CompressionHeader
+ 1,
979 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
980 (UINT32
*)&NewStreamBufferSize
,
983 ASSERT_EFI_ERROR (Status
);
984 ASSERT (NewStreamBufferSize
== CompressionHeader
->UncompressedLength
);
986 ScratchBuffer
= CoreAllocateBootServicesPool (ScratchSize
);
987 if (ScratchBuffer
== NULL
) {
989 CoreFreePool (NewStreamBuffer
);
990 return EFI_OUT_OF_RESOURCES
;
993 Status
= Decompress
->Decompress (
995 CompressionHeader
+ 1,
996 Node
->Size
- sizeof (EFI_COMPRESSION_SECTION
),
998 (UINT32
)NewStreamBufferSize
,
1002 ASSERT_EFI_ERROR (Status
);
1003 CoreFreePool (ScratchBuffer
);
1006 NewStreamBuffer
= NULL
;
1007 NewStreamBufferSize
= 0;
1010 Status
= OpenSectionStreamEx (
1011 NewStreamBufferSize
,
1014 Stream
->AuthenticationStatus
,
1015 &Node
->EncapsulatedStreamHandle
1017 if (EFI_ERROR (Status
)) {
1018 CoreFreePool (Node
);
1019 CoreFreePool (NewStreamBuffer
);
1024 case EFI_SECTION_GUID_DEFINED
:
1025 GuidedHeader
= (EFI_GUID_DEFINED_SECTION
*) SectionHeader
;
1026 Node
->EncapsulationGuid
= &GuidedHeader
->SectionDefinitionGuid
;
1027 Status
= CoreLocateProtocol (Node
->EncapsulationGuid
, NULL
, (VOID
**)&GuidedExtraction
);
1028 if (!EFI_ERROR (Status
)) {
1030 // NewStreamBuffer is always allocated by ExtractSection... No caller
1033 Status
= GuidedExtraction
->ExtractSection (
1037 &NewStreamBufferSize
,
1038 &AuthenticationStatus
1040 if (EFI_ERROR (Status
)) {
1041 CoreFreePool (*ChildNode
);
1042 return EFI_PROTOCOL_ERROR
;
1046 // Make sure we initialize the new stream with the correct
1047 // authentication status for both aggregate and local status fields.
1049 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_AUTH_STATUS_VALID
) {
1051 // OR in the parent stream's aggregate status.
1053 AuthenticationStatus
|= Stream
->AuthenticationStatus
& EFI_AUTH_STATUS_ALL
;
1056 // since there's no authentication data contributed by the section,
1057 // just inherit the full value from our immediate parent.
1059 AuthenticationStatus
= Stream
->AuthenticationStatus
;
1062 Status
= OpenSectionStreamEx (
1063 NewStreamBufferSize
,
1066 AuthenticationStatus
,
1067 &Node
->EncapsulatedStreamHandle
1069 if (EFI_ERROR (Status
)) {
1070 CoreFreePool (*ChildNode
);
1071 CoreFreePool (NewStreamBuffer
);
1076 // There's no GUIDed section extraction protocol available.
1078 if (GuidedHeader
->Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) {
1080 // If the section REQUIRES an extraction protocol, then we're toast
1082 CoreFreePool (*ChildNode
);
1083 return EFI_PROTOCOL_ERROR
;
1087 // Figure out the proper authentication status
1089 AuthenticationStatus
= Stream
->AuthenticationStatus
;
1091 SectionLength
= SECTION_SIZE (GuidedHeader
);
1092 Status
= OpenSectionStreamEx (
1093 SectionLength
- GuidedHeader
->DataOffset
,
1094 (UINT8
*) GuidedHeader
+ GuidedHeader
->DataOffset
,
1096 AuthenticationStatus
,
1097 &Node
->EncapsulatedStreamHandle
1099 if (EFI_ERROR (Status
)) {
1100 CoreFreePool (Node
);
1110 // Nothing to do if it's a leaf
1116 // Last, add the new child node to the stream
1118 InsertTailList (&Stream
->Children
, &Node
->Link
);
1125 Worker function. Destructor for child nodes.
1127 @param ChildNode Indicates the node to destroy
1132 IN CORE_SECTION_CHILD_NODE
*ChildNode
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 (ChildNode
->EncapsulatedStreamHandle
);
1149 // Last, free the child node itself
1151 CoreFreePool (ChildNode
);
1157 Worker function. Constructor for section streams.
1159 @param SectionStreamLength Size in bytes of the section stream.
1160 @param SectionStream Buffer containing the new section stream.
1161 @param AllocateBuffer Indicates whether the stream buffer is to be
1162 copied or the input buffer is to be used in
1163 place. AuthenticationStatus- Indicates the
1164 default authentication status for the new
1166 @param AuthenticationStatus A pointer to a caller-allocated UINT32 that
1167 indicates the authentication status of the
1168 output buffer. If the input section's
1169 GuidedSectionHeader.Attributes field
1170 has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID
1171 bit as clear, AuthenticationStatus must return
1172 zero. Both local bits (19:16) and aggregate
1173 bits (3:0) in AuthenticationStatus are returned
1174 by ExtractSection(). These bits reflect the
1175 status of the extraction operation. The bit
1176 pattern in both regions must be the same, as
1177 the local and aggregate authentication statuses
1178 have equivalent meaning at this level. If the
1179 function returns anything other than
1180 EFI_SUCCESS, the value of *AuthenticationStatus
1182 @param SectionStreamHandle A pointer to a caller allocated section stream
1185 @retval EFI_SUCCESS Stream was added to stream database.
1186 @retval EFI_OUT_OF_RESOURCES memory allocation failed.
1190 OpenSectionStreamEx (
1191 IN UINTN SectionStreamLength
,
1192 IN VOID
*SectionStream
,
1193 IN BOOLEAN AllocateBuffer
,
1194 IN UINT32 AuthenticationStatus
,
1195 OUT UINTN
*SectionStreamHandle
1198 CORE_SECTION_STREAM_NODE
*NewStream
;
1202 // Allocate a new stream
1204 NewStream
= CoreAllocateBootServicesPool (sizeof (CORE_SECTION_STREAM_NODE
));
1205 if (NewStream
== NULL
) {
1206 return EFI_OUT_OF_RESOURCES
;
1209 if (AllocateBuffer
) {
1211 // if we're here, we're double buffering, allocate the buffer and copy the
1214 if (SectionStreamLength
> 0) {
1215 NewStream
->StreamBuffer
= CoreAllocateBootServicesPool (SectionStreamLength
);
1216 if (NewStream
->StreamBuffer
== NULL
) {
1217 CoreFreePool (NewStream
);
1218 return EFI_OUT_OF_RESOURCES
;
1221 // Copy in stream data
1223 CopyMem (NewStream
->StreamBuffer
, SectionStream
, SectionStreamLength
);
1226 // It's possible to have a zero length section stream.
1228 NewStream
->StreamBuffer
= NULL
;
1232 // If were here, the caller has supplied the buffer (it's an internal call)
1233 // so just assign the buffer. This happens when we open section streams
1234 // as a result of expanding an encapsulating section.
1236 NewStream
->StreamBuffer
= SectionStream
;
1240 // Initialize the rest of the section stream
1242 NewStream
->Signature
= CORE_SECTION_STREAM_SIGNATURE
;
1243 NewStream
->StreamHandle
= (UINTN
) NewStream
;
1244 NewStream
->StreamLength
= SectionStreamLength
;
1245 InitializeListHead (&NewStream
->Children
);
1246 NewStream
->AuthenticationStatus
= AuthenticationStatus
;
1249 // Add new stream to stream list
1251 OldTpl
= CoreRaiseTpl (TPL_NOTIFY
);
1252 InsertTailList (&mStreamRoot
, &NewStream
->Link
);
1253 CoreRestoreTpl (OldTpl
);
1255 *SectionStreamHandle
= NewStream
->StreamHandle
;
1263 Worker function. Search stream database for requested stream handle.
1265 @param SearchHandle Indicates which stream to look for.
1266 @param FoundStream Output pointer to the found stream.
1268 @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains
1270 @retval EFI_NOT_FOUND SearchHandle was not found in the stream
1276 IN UINTN SearchHandle
,
1277 OUT CORE_SECTION_STREAM_NODE
**FoundStream
1280 CORE_SECTION_STREAM_NODE
*StreamNode
;
1282 if (!IsListEmpty (&mStreamRoot
)) {
1283 StreamNode
= STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot
));
1285 if (StreamNode
->StreamHandle
== SearchHandle
) {
1286 *FoundStream
= StreamNode
;
1288 } else if (IsNodeAtEnd (&mStreamRoot
, &StreamNode
->Link
)) {
1291 StreamNode
= STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot
, &StreamNode
->Link
));
1296 return EFI_NOT_FOUND
;
1301 Check if a stream is valid.
1303 @param SectionStream The section stream to be checked
1304 @param SectionStreamLength The length of section stream
1306 @return A boolean value indicating the validness of the section stream.
1310 IsValidSectionStream (
1311 IN VOID
*SectionStream
,
1312 IN UINTN SectionStreamLength
1316 UINTN SectionLength
;
1317 EFI_COMMON_SECTION_HEADER
*SectionHeader
;
1318 EFI_COMMON_SECTION_HEADER
*NextSectionHeader
;
1321 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*)SectionStream
;
1323 while (TotalLength
< SectionStreamLength
) {
1324 SectionLength
= SECTION_SIZE (SectionHeader
);
1325 TotalLength
+= SectionLength
;
1327 if (TotalLength
== SectionStreamLength
) {
1332 // Move to the next byte following the section...
1334 SectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) SectionHeader
+ SectionLength
);
1337 // Figure out where the next section begins
1339 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) SectionHeader
+ 3);
1340 NextSectionHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) NextSectionHeader
& ~(UINTN
)3);
1341 TotalLength
+= (UINTN
) NextSectionHeader
- (UINTN
) SectionHeader
;
1342 SectionHeader
= NextSectionHeader
;
1351 The ExtractSection() function processes the input section and
1352 allocates a buffer from the pool in which it returns the section
1353 contents. If the section being extracted contains
1354 authentication information (the section's
1355 GuidedSectionHeader.Attributes field has the
1356 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values
1357 returned in AuthenticationStatus must reflect the results of
1358 the authentication operation. Depending on the algorithm and
1359 size of the encapsulated data, the time that is required to do
1360 a full authentication may be prohibitively long for some
1361 classes of systems. To indicate this, use
1362 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by
1363 the security policy driver (see the Platform Initialization
1364 Driver Execution Environment Core Interface Specification for
1365 more details and the GUID definition). If the
1366 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle
1367 database, then, if possible, full authentication should be
1368 skipped and the section contents simply returned in the
1369 OutputBuffer. In this case, the
1370 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus
1371 must be set on return. ExtractSection() is callable only from
1372 TPL_NOTIFY and below. Behavior of ExtractSection() at any
1373 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is
1374 defined in RaiseTPL() in the UEFI 2.0 specification.
1377 @param This Indicates the
1378 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.
1379 @param InputSection Buffer containing the input GUIDed section
1380 to be processed. OutputBuffer OutputBuffer
1381 is allocated from boot services pool
1382 memory and contains the new section
1383 stream. The caller is responsible for
1384 freeing this buffer.
1385 @param OutputBuffer *OutputBuffer is allocated from boot services
1386 pool memory and contains the new section stream.
1387 The caller is responsible for freeing this buffer.
1388 @param OutputSize A pointer to a caller-allocated UINTN in
1389 which the size of OutputBuffer allocation
1390 is stored. If the function returns
1391 anything other than EFI_SUCCESS, the value
1392 of OutputSize is undefined.
1394 @param AuthenticationStatus A pointer to a caller-allocated
1395 UINT32 that indicates the
1396 authentication status of the
1397 output buffer. If the input
1399 GuidedSectionHeader.Attributes
1401 EFI_GUIDED_SECTION_AUTH_STATUS_VAL
1402 bit as clear, AuthenticationStatus
1403 must return zero. Both local bits
1404 (19:16) and aggregate bits (3:0)
1405 in AuthenticationStatus are
1406 returned by ExtractSection().
1407 These bits reflect the status of
1408 the extraction operation. The bit
1409 pattern in both regions must be
1410 the same, as the local and
1411 aggregate authentication statuses
1412 have equivalent meaning at this
1413 level. If the function returns
1414 anything other than EFI_SUCCESS,
1415 the value of AuthenticationStatus
1419 @retval EFI_SUCCESS The InputSection was successfully
1420 processed and the section contents were
1423 @retval EFI_OUT_OF_RESOURCES The system has insufficient
1424 resources to process the
1427 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
1428 not match this instance of the
1429 GUIDed Section Extraction
1435 CustomGuidedSectionExtract (
1436 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL
*This
,
1437 IN CONST VOID
*InputSection
,
1438 OUT VOID
**OutputBuffer
,
1439 OUT UINTN
*OutputSize
,
1440 OUT UINT32
*AuthenticationStatus
1444 VOID
*ScratchBuffer
;
1445 VOID
*AllocatedOutputBuffer
;
1446 UINT32 OutputBufferSize
;
1447 UINT32 ScratchBufferSize
;
1448 UINT16 SectionAttribute
;
1451 // Init local variable
1453 ScratchBuffer
= NULL
;
1454 AllocatedOutputBuffer
= NULL
;
1457 // Call GetInfo to get the size and attribute of input guided section data.
1459 Status
= ExtractGuidedSectionGetInfo (
1466 if (EFI_ERROR (Status
)) {
1467 DEBUG ((DEBUG_ERROR
, "GetInfo from guided section Failed - %r\n", Status
));
1471 if (ScratchBufferSize
!= 0) {
1473 // Allocate scratch buffer
1475 ScratchBuffer
= CoreAllocateBootServicesPool (ScratchBufferSize
);
1476 if (ScratchBuffer
== NULL
) {
1477 return EFI_OUT_OF_RESOURCES
;
1481 if (OutputBufferSize
> 0) {
1483 // Allocate output buffer
1485 AllocatedOutputBuffer
= CoreAllocateBootServicesPool (OutputBufferSize
);
1486 if (AllocatedOutputBuffer
== NULL
) {
1487 return EFI_OUT_OF_RESOURCES
;
1489 *OutputBuffer
= AllocatedOutputBuffer
;
1493 // Call decode function to extract raw data from the guided section.
1495 Status
= ExtractGuidedSectionDecode (
1499 AuthenticationStatus
1501 if (EFI_ERROR (Status
)) {
1505 if (AllocatedOutputBuffer
!= NULL
) {
1506 CoreFreePool (AllocatedOutputBuffer
);
1508 if (ScratchBuffer
!= NULL
) {
1509 CoreFreePool (ScratchBuffer
);
1511 DEBUG ((DEBUG_ERROR
, "Extract guided section Failed - %r\n", Status
));
1515 if (*OutputBuffer
!= AllocatedOutputBuffer
) {
1517 // OutputBuffer was returned as a different value,
1518 // so copy section contents to the allocated memory buffer.
1520 CopyMem (AllocatedOutputBuffer
, *OutputBuffer
, OutputBufferSize
);
1521 *OutputBuffer
= AllocatedOutputBuffer
;
1525 // Set real size of output buffer.
1527 *OutputSize
= (UINTN
) OutputBufferSize
;
1530 // Free unused scratch buffer.
1532 if (ScratchBuffer
!= NULL
) {
1533 CoreFreePool (ScratchBuffer
);