X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FCore%2FDxe%2FSectionExtraction%2FCoreSectionExtraction.c;h=243b6b27aeccdadb0c4384a2739307330f38f3ea;hp=07ada03d2ce72c119ea8465035c80047e3a50f6c;hb=3140ceda761b4a5ed4de72d1314f7f391691b1c0;hpb=e94a9ff7271367e649ee4f9a86da1f1bea6d112e diff --git a/MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c b/MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c index 07ada03d2c..243b6b27ae 100644 --- a/MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c +++ b/MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c @@ -1,33 +1,33 @@ /** @file Section Extraction Protocol implementation. - + Stream database is implemented as a linked list of section streams, where each stream contains a linked list of children, which may be leaves or - encapsulations. - + encapsulations. + Children that are encapsulations generate new stream entries - when they are created. Streams can also be created by calls to + when they are created. Streams can also be created by calls to SEP->OpenSectionStream(). - + The database is only created far enough to return the requested data from any given stream, or to determine that the requested data is not found. - + If a GUIDed encapsulation is encountered, there are three possiblilites. - + 1) A support protocol is found, in which the stream is simply processed with the support protocol. - + 2) A support protocol is not found, but the data is available to be read without processing. In this case, the database is built up through the recursions to return the data, and a RPN event is set that will enable the stream in question to be refreshed if and when the required section - extraction protocol is published.This insures the AuthenticationStatus + extraction protocol is published.This insures the AuthenticationStatus does not become stale in the cache. - + 3) A support protocol is not found, and the data is not available to be read without it. This results in EFI_PROTOCOL_ERROR. -Copyright (c) 2006 - 2008, Intel Corporation.
+Copyright (c) 2006 - 2010, Intel Corporation.
All rights reserved. This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -38,12 +38,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ -#include +#include "DxeMain.h" // // Local defines and typedefs // -#define CORE_SECTION_CHILD_SIGNATURE EFI_SIGNATURE_32('S','X','C','S') +#define CORE_SECTION_CHILD_SIGNATURE SIGNATURE_32('S','X','C','S') #define CHILD_SECTION_NODE_FROM_LINK(Node) \ CR (Node, CORE_SECTION_CHILD_NODE, Link, CORE_SECTION_CHILD_SIGNATURE) @@ -68,7 +68,7 @@ typedef struct { EFI_GUID *EncapsulationGuid; } CORE_SECTION_CHILD_NODE; -#define CORE_SECTION_STREAM_SIGNATURE EFI_SIGNATURE_32('S','X','S','S') +#define CORE_SECTION_STREAM_SIGNATURE SIGNATURE_32('S','X','S','S') #define STREAM_NODE_FROM_LINK(Node) \ CR (Node, CORE_SECTION_STREAM_NODE, Link, CORE_SECTION_STREAM_SIGNATURE) @@ -93,191 +93,6 @@ typedef struct { VOID *Registration; EFI_EVENT Event; } RPN_EVENT_CONTEXT; - - - -// -// Local prototypes -// -/** - Worker function. Determine if the input stream:child matches the input type. - - @param Stream Indicates the section stream associated with the - child - @param Child Indicates the child to check - @param SearchType Indicates the type of section to check against - for - @param SectionDefinitionGuid Indicates the GUID to check against if the type - is EFI_SECTION_GUID_DEFINED - - @retval TRUE The child matches - @retval FALSE The child doesn't match - -**/ -BOOLEAN -ChildIsType ( - IN CORE_SECTION_STREAM_NODE *Stream, - IN CORE_SECTION_CHILD_NODE *Child, - IN EFI_SECTION_TYPE SearchType, - IN EFI_GUID *SectionDefinitionGuid - ); - - -/** - Worker function. Search stream database for requested stream handle. - - @param SearchHandle Indicates which stream to look for. - @param FoundStream Output pointer to the found stream. - - @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains - the stream node. - @retval EFI_NOT_FOUND SearchHandle was not found in the stream - database. - -**/ -EFI_STATUS -FindStreamNode ( - IN UINTN SearchHandle, - OUT CORE_SECTION_STREAM_NODE **FoundStream - ); - - -/** - Worker function Recursively searches / builds section stream database - looking for requested section. - - @param SourceStream Indicates the section stream in which to do the - search. - @param SearchType Indicates the type of section to search for. - @param SectionInstance Indicates which instance of section to find. - This is an in/out parameter to deal with - recursions. - @param SectionDefinitionGuid Guid of section definition - @param FoundChild Output indicating the child node that is found. - @param FoundStream Output indicating which section stream the child - was found in. If this stream was generated as a - result of an encapsulation section, the - streamhandle is visible within the SEP driver - only. - @param AuthenticationStatus Indicates the authentication status of the found section. - - @retval EFI_SUCCESS Child node was found and returned. - EFI_OUT_OF_RESOURCES- Memory allocation failed. - @retval EFI_NOT_FOUND Requested child node does not exist. - @retval EFI_PROTOCOL_ERROR a required GUIDED section extraction protocol - does not exist - -**/ -EFI_STATUS -FindChildNode ( - IN CORE_SECTION_STREAM_NODE *SourceStream, - IN EFI_SECTION_TYPE SearchType, - IN OUT UINTN *SectionInstance, - IN EFI_GUID *SectionDefinitionGuid, - OUT CORE_SECTION_CHILD_NODE **FoundChild, - OUT CORE_SECTION_STREAM_NODE **FoundStream, - OUT UINT32 *AuthenticationStatus - ); - - -/** - Worker function. Constructor for new child nodes. - - @param Stream Indicates the section stream in which to add the - child. - @param ChildOffset Indicates the offset in Stream that is the - beginning of the child section. - @param ChildNode Indicates the Callee allocated and initialized - child. - - @retval EFI_SUCCESS Child node was found and returned. - EFI_OUT_OF_RESOURCES- Memory allocation failed. - @retval EFI_PROTOCOL_ERROR Encapsulation sections produce new stream - handles when the child node is created. If the - section type is GUID defined, and the extraction - GUID does not exist, and producing the stream - requires the GUID, then a protocol error is - generated and no child is produced. Values - returned by OpenSectionStreamEx. - -**/ -EFI_STATUS -CreateChildNode ( - IN CORE_SECTION_STREAM_NODE *Stream, - IN UINT32 ChildOffset, - OUT CORE_SECTION_CHILD_NODE **ChildNode - ); - - -/** - Worker function. Destructor for child nodes. - - @param ChildNode Indicates the node to destroy - -**/ -VOID -FreeChildNode ( - IN CORE_SECTION_CHILD_NODE *ChildNode - ); - - -/** - Worker function. Constructor for section streams. - - @param SectionStreamLength Size in bytes of the section stream. - @param SectionStream Buffer containing the new section stream. - @param AllocateBuffer Indicates whether the stream buffer is to be - copied or the input buffer is to be used in - place. AuthenticationStatus- Indicates the - default authentication status for the new - stream. - @param AuthenticationStatus A pointer to a caller-allocated UINT32 that - indicates the authentication status of the - output buffer. If the input section's - GuidedSectionHeader.Attributes field - has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID - bit as clear, AuthenticationStatus must return - zero. Both local bits (19:16) and aggregate - bits (3:0) in AuthenticationStatus are returned - by ExtractSection(). These bits reflect the - status of the extraction operation. The bit - pattern in both regions must be the same, as - the local and aggregate authentication statuses - have equivalent meaning at this level. If the - function returns anything other than - EFI_SUCCESS, the value of *AuthenticationStatus - is undefined. - @param SectionStreamHandle A pointer to a caller allocated section stream - handle. - - @retval EFI_SUCCESS Stream was added to stream database. - @retval EFI_OUT_OF_RESOURCES memory allocation failed. - -**/ -EFI_STATUS -OpenSectionStreamEx ( - IN UINTN SectionStreamLength, - IN VOID *SectionStream, - IN BOOLEAN AllocateBuffer, - IN UINT32 AuthenticationStatus, - OUT UINTN *SectionStreamHandle - ); - - -/** - Check if a stream is valid. - - @param SectionStream The section stream to be checked - @param SectionStreamLength The length of section stream - - @return A boolean value indicating the validness of the section stream. - -**/ -BOOLEAN -IsValidSectionStream ( - IN VOID *SectionStream, - IN UINTN SectionStreamLength - ); /** @@ -306,7 +121,7 @@ IsValidSectionStream ( EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is defined in RaiseTPL() in the UEFI 2.0 specification. - + @param This Indicates the EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance. @param InputSection Buffer containing the input GUIDed section @@ -323,6 +138,7 @@ IsValidSectionStream ( is stored. If the function returns anything other than EFI_SUCCESS, the value of OutputSize is undefined. + @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the authentication status of the @@ -370,7 +186,7 @@ CustomGuidedSectionExtract ( OUT VOID **OutputBuffer, OUT UINTN *OutputSize, OUT UINT32 *AuthenticationStatus - ); + ); // // Module globals @@ -382,7 +198,7 @@ EFI_HANDLE mSectionExtractionHandle = NULL; EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL mCustomGuidedSectionExtractionProtocol = { CustomGuidedSectionExtract }; - + /** Entry point of the section extraction code. Initializes an instance of the @@ -407,13 +223,13 @@ InitializeSectionExtraction ( UINTN ExtractHandlerNumber; // - // Get custom extract guided section method guid list + // Get custom extract guided section method guid list // ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable); - + Status = EFI_SUCCESS; // - // Install custom guided extraction protocol + // Install custom guided extraction protocol // while (ExtractHandlerNumber-- > 0) { Status = CoreInstallProtocolInterface ( @@ -430,247 +246,196 @@ InitializeSectionExtraction ( /** - SEP member function. This function creates and returns a new section stream - handle to represent the new section stream. + Check if a stream is valid. - @param SectionStreamLength Size in bytes of the section stream. - @param SectionStream Buffer containing the new section stream. - @param SectionStreamHandle A pointer to a caller allocated UINTN that on - output contains the new section stream handle. + @param SectionStream The section stream to be checked + @param SectionStreamLength The length of section stream - @retval EFI_SUCCESS The section stream is created successfully. - @retval EFI_OUT_OF_RESOURCES memory allocation failed. - @retval EFI_INVALID_PARAMETER Section stream does not end concident with end - of last section. + @return A boolean value indicating the validness of the section stream. + +**/ +BOOLEAN +IsValidSectionStream ( + IN VOID *SectionStream, + IN UINTN SectionStreamLength + ) +{ + UINTN TotalLength; + UINTN SectionLength; + EFI_COMMON_SECTION_HEADER *SectionHeader; + EFI_COMMON_SECTION_HEADER *NextSectionHeader; + + TotalLength = 0; + SectionHeader = (EFI_COMMON_SECTION_HEADER *)SectionStream; + + while (TotalLength < SectionStreamLength) { + SectionLength = SECTION_SIZE (SectionHeader); + TotalLength += SectionLength; + + if (TotalLength == SectionStreamLength) { + return TRUE; + } + + // + // Move to the next byte following the section... + // + SectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) SectionHeader + SectionLength); + + // + // Figure out where the next section begins + // + NextSectionHeader = ALIGN_POINTER(SectionHeader, 4); + TotalLength += (UINTN) NextSectionHeader - (UINTN) SectionHeader; + SectionHeader = NextSectionHeader; + } + + ASSERT (FALSE); + return FALSE; +} + + +/** + Worker function. Constructor for section streams. + + @param SectionStreamLength Size in bytes of the section stream. + @param SectionStream Buffer containing the new section stream. + @param AllocateBuffer Indicates whether the stream buffer is to be + copied or the input buffer is to be used in + place. AuthenticationStatus- Indicates the + default authentication status for the new + stream. + @param AuthenticationStatus A pointer to a caller-allocated UINT32 that + indicates the authentication status of the + output buffer. If the input section's + GuidedSectionHeader.Attributes field + has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID + bit as clear, AuthenticationStatus must return + zero. Both local bits (19:16) and aggregate + bits (3:0) in AuthenticationStatus are returned + by ExtractSection(). These bits reflect the + status of the extraction operation. The bit + pattern in both regions must be the same, as + the local and aggregate authentication statuses + have equivalent meaning at this level. If the + function returns anything other than + EFI_SUCCESS, the value of *AuthenticationStatus + is undefined. + @param SectionStreamHandle A pointer to a caller allocated section stream + handle. + + @retval EFI_SUCCESS Stream was added to stream database. + @retval EFI_OUT_OF_RESOURCES memory allocation failed. **/ EFI_STATUS -EFIAPI -OpenSectionStream ( +OpenSectionStreamEx ( IN UINTN SectionStreamLength, IN VOID *SectionStream, + IN BOOLEAN AllocateBuffer, + IN UINT32 AuthenticationStatus, OUT UINTN *SectionStreamHandle ) { + CORE_SECTION_STREAM_NODE *NewStream; + EFI_TPL OldTpl; + // - // Check to see section stream looks good... + // Allocate a new stream // - if (!IsValidSectionStream (SectionStream, SectionStreamLength)) { - return EFI_INVALID_PARAMETER; + NewStream = AllocatePool (sizeof (CORE_SECTION_STREAM_NODE)); + if (NewStream == NULL) { + return EFI_OUT_OF_RESOURCES; } - - return OpenSectionStreamEx ( - SectionStreamLength, - SectionStream, - TRUE, - 0, - SectionStreamHandle - ); -} - -/** - SEP member function. Retrieves requested section from section stream. + if (AllocateBuffer) { + // + // if we're here, we're double buffering, allocate the buffer and copy the + // data in + // + if (SectionStreamLength > 0) { + NewStream->StreamBuffer = AllocatePool (SectionStreamLength); + if (NewStream->StreamBuffer == NULL) { + CoreFreePool (NewStream); + return EFI_OUT_OF_RESOURCES; + } + // + // Copy in stream data + // + CopyMem (NewStream->StreamBuffer, SectionStream, SectionStreamLength); + } else { + // + // It's possible to have a zero length section stream. + // + NewStream->StreamBuffer = NULL; + } + } else { + // + // If were here, the caller has supplied the buffer (it's an internal call) + // so just assign the buffer. This happens when we open section streams + // as a result of expanding an encapsulating section. + // + NewStream->StreamBuffer = SectionStream; + } - @param SectionStreamHandle The section stream from which to extract the - requested section. - @param SectionType A pointer to the type of section to search for. - @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED, - then SectionDefinitionGuid indicates which of - these types of sections to search for. - @param SectionInstance Indicates which instance of the requested - section to return. - @param Buffer Double indirection to buffer. If *Buffer is - non-null on input, then the buffer is caller - allocated. If Buffer is NULL, then the buffer - is callee allocated. In either case, the - requried buffer size is returned in *BufferSize. - @param BufferSize On input, indicates the size of *Buffer if - *Buffer is non-null on input. On output, - indicates the required size (allocated size if - callee allocated) of *Buffer. - @param AuthenticationStatus A pointer to a caller-allocated UINT32 that - indicates the authentication status of the - output buffer. If the input section's - GuidedSectionHeader.Attributes field - has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID - bit as clear, AuthenticationStatus must return - zero. Both local bits (19:16) and aggregate - bits (3:0) in AuthenticationStatus are returned - by ExtractSection(). These bits reflect the - status of the extraction operation. The bit - pattern in both regions must be the same, as - the local and aggregate authentication statuses - have equivalent meaning at this level. If the - function returns anything other than - EFI_SUCCESS, the value of *AuthenticationStatus - is undefined. - - @retval EFI_SUCCESS Section was retrieved successfully - @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the - section stream with its - EFI_GUIDED_SECTION_PROCESSING_REQUIRED bit set, - but there was no corresponding GUIDed Section - Extraction Protocol in the handle database. - *Buffer is unmodified. - @retval EFI_NOT_FOUND An error was encountered when parsing the - SectionStream. This indicates the SectionStream - is not correctly formatted. - @retval EFI_NOT_FOUND The requested section does not exist. - @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process - the request. - @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist. - @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is - insufficient to contain the requested section. - The input buffer is filled and section contents - are truncated. - -**/ -EFI_STATUS -EFIAPI -GetSection ( - IN UINTN SectionStreamHandle, - IN EFI_SECTION_TYPE *SectionType, - IN EFI_GUID *SectionDefinitionGuid, - IN UINTN SectionInstance, - IN VOID **Buffer, - IN OUT UINTN *BufferSize, - OUT UINT32 *AuthenticationStatus - ) -{ - CORE_SECTION_STREAM_NODE *StreamNode; - EFI_TPL OldTpl; - EFI_STATUS Status; - CORE_SECTION_CHILD_NODE *ChildNode; - CORE_SECTION_STREAM_NODE *ChildStreamNode; - UINTN CopySize; - UINT32 ExtractedAuthenticationStatus; - UINTN Instance; - UINT8 *CopyBuffer; - UINTN SectionSize; - - - OldTpl = CoreRaiseTpl (TPL_NOTIFY); - Instance = SectionInstance + 1; - // - // Locate target stream + // Initialize the rest of the section stream // - Status = FindStreamNode (SectionStreamHandle, &StreamNode); - if (EFI_ERROR (Status)) { - Status = EFI_INVALID_PARAMETER; - goto GetSection_Done; - } - + NewStream->Signature = CORE_SECTION_STREAM_SIGNATURE; + NewStream->StreamHandle = (UINTN) NewStream; + NewStream->StreamLength = SectionStreamLength; + InitializeListHead (&NewStream->Children); + NewStream->AuthenticationStatus = AuthenticationStatus; + // - // Found the stream, now locate and return the appropriate section + // Add new stream to stream list // - if (SectionType == NULL) { - // - // SectionType == NULL means return the WHOLE section stream... - // - CopySize = StreamNode->StreamLength; - CopyBuffer = StreamNode->StreamBuffer; - *AuthenticationStatus = StreamNode->AuthenticationStatus; - } else { - // - // There's a requested section type, so go find it and return it... - // - Status = FindChildNode ( - StreamNode, - *SectionType, - &Instance, - SectionDefinitionGuid, - &ChildNode, - &ChildStreamNode, - &ExtractedAuthenticationStatus - ); - if (EFI_ERROR (Status)) { - goto GetSection_Done; - } - CopySize = ChildNode->Size - sizeof (EFI_COMMON_SECTION_HEADER); - CopyBuffer = ChildStreamNode->StreamBuffer + ChildNode->OffsetInStream + sizeof (EFI_COMMON_SECTION_HEADER); - *AuthenticationStatus = ExtractedAuthenticationStatus; - } - - SectionSize = CopySize; - if (*Buffer != NULL) { - // - // Caller allocated buffer. Fill to size and return required size... - // - if (*BufferSize < CopySize) { - Status = EFI_WARN_BUFFER_TOO_SMALL; - CopySize = *BufferSize; - } - } else { - // - // Callee allocated buffer. Allocate buffer and return size. - // - *Buffer = CoreAllocateBootServicesPool (CopySize); - if (*Buffer == NULL) { - Status = EFI_OUT_OF_RESOURCES; - goto GetSection_Done; - } - } - CopyMem (*Buffer, CopyBuffer, CopySize); - *BufferSize = SectionSize; - -GetSection_Done: + OldTpl = CoreRaiseTpl (TPL_NOTIFY); + InsertTailList (&mStreamRoot, &NewStream->Link); CoreRestoreTpl (OldTpl); - - return Status; -} + *SectionStreamHandle = NewStream->StreamHandle; + + return EFI_SUCCESS; +} /** - SEP member function. Deletes an existing section stream + SEP member function. This function creates and returns a new section stream + handle to represent the new section stream. - @param StreamHandleToClose Indicates the stream to close + @param SectionStreamLength Size in bytes of the section stream. + @param SectionStream Buffer containing the new section stream. + @param SectionStreamHandle A pointer to a caller allocated UINTN that on + output contains the new section stream handle. - @retval EFI_SUCCESS The section stream is closed sucessfully. - @retval EFI_OUT_OF_RESOURCES Memory allocation failed. - @retval EFI_INVALID_PARAMETER Section stream does not end concident with end + @retval EFI_SUCCESS The section stream is created successfully. + @retval EFI_OUT_OF_RESOURCES memory allocation failed. + @retval EFI_INVALID_PARAMETER Section stream does not end concident with end of last section. **/ EFI_STATUS EFIAPI -CloseSectionStream ( - IN UINTN StreamHandleToClose +OpenSectionStream ( + IN UINTN SectionStreamLength, + IN VOID *SectionStream, + OUT UINTN *SectionStreamHandle ) { - CORE_SECTION_STREAM_NODE *StreamNode; - EFI_TPL OldTpl; - EFI_STATUS Status; - LIST_ENTRY *Link; - CORE_SECTION_CHILD_NODE *ChildNode; - - OldTpl = CoreRaiseTpl (TPL_NOTIFY); - // - // Locate target stream + // Check to see section stream looks good... // - Status = FindStreamNode (StreamHandleToClose, &StreamNode); - if (!EFI_ERROR (Status)) { - // - // Found the stream, so close it - // - RemoveEntryList (&StreamNode->Link); - while (!IsListEmpty (&StreamNode->Children)) { - Link = GetFirstNode (&StreamNode->Children); - ChildNode = CHILD_SECTION_NODE_FROM_LINK (Link); - FreeChildNode (ChildNode); - } - CoreFreePool (StreamNode->StreamBuffer); - CoreFreePool (StreamNode); - Status = EFI_SUCCESS; - } else { - Status = EFI_INVALID_PARAMETER; + if (!IsValidSectionStream (SectionStream, SectionStreamLength)) { + return EFI_INVALID_PARAMETER; } - - CoreRestoreTpl (OldTpl); - return Status; + + return OpenSectionStreamEx ( + SectionStreamLength, + SectionStream, + TRUE, + 0, + SectionStreamHandle + ); } @@ -678,15 +443,15 @@ CloseSectionStream ( /** Worker function. Determine if the input stream:child matches the input type. - @param Stream Indicates the section stream associated with the - child - @param Child Indicates the child to check - @param SearchType Indicates the type of section to check against - for - @param SectionDefinitionGuid Indicates the GUID to check against if the type - is EFI_SECTION_GUID_DEFINED + @param Stream Indicates the section stream associated with the + child + @param Child Indicates the child to check + @param SearchType Indicates the type of section to check against + for + @param SectionDefinitionGuid Indicates the GUID to check against if the type + is EFI_SECTION_GUID_DEFINED - @retval TRUE The child matches + @retval TRUE The child matches @retval FALSE The child doesn't match **/ @@ -699,14 +464,14 @@ ChildIsType ( ) { EFI_GUID_DEFINED_SECTION *GuidedSection; - + if (SearchType == EFI_SECTION_ALL) { return TRUE; } if (Child->Type != SearchType) { return FALSE; } - if (SearchType != EFI_SECTION_GUID_DEFINED) { + if ((SearchType != EFI_SECTION_GUID_DEFINED) || (SectionDefinitionGuid == NULL)) { return TRUE; } GuidedSection = (EFI_GUID_DEFINED_SECTION * )(Stream->StreamBuffer + Child->OffsetInStream); @@ -715,214 +480,59 @@ ChildIsType ( /** - Worker function Recursively searches / builds section stream database - looking for requested section. - - @param SourceStream Indicates the section stream in which to do the - search. - @param SearchType Indicates the type of section to search for. - @param SectionInstance Indicates which instance of section to find. - This is an in/out parameter to deal with - recursions. - @param SectionDefinitionGuid Guid of section definition - @param FoundChild Output indicating the child node that is found. - @param FoundStream Output indicating which section stream the child - was found in. If this stream was generated as a - result of an encapsulation section, the - streamhandle is visible within the SEP driver - only. - @param AuthenticationStatus Indicates the authentication status of the found section. + Worker function. Constructor for new child nodes. - @retval EFI_SUCCESS Child node was found and returned. - EFI_OUT_OF_RESOURCES- Memory allocation failed. - @retval EFI_NOT_FOUND Requested child node does not exist. - @retval EFI_PROTOCOL_ERROR a required GUIDED section extraction protocol - does not exist + @param Stream Indicates the section stream in which to add the + child. + @param ChildOffset Indicates the offset in Stream that is the + beginning of the child section. + @param ChildNode Indicates the Callee allocated and initialized + child. + + @retval EFI_SUCCESS Child node was found and returned. + EFI_OUT_OF_RESOURCES- Memory allocation failed. + @retval EFI_PROTOCOL_ERROR Encapsulation sections produce new stream + handles when the child node is created. If the + section type is GUID defined, and the extraction + GUID does not exist, and producing the stream + requires the GUID, then a protocol error is + generated and no child is produced. Values + returned by OpenSectionStreamEx. **/ EFI_STATUS -FindChildNode ( - IN CORE_SECTION_STREAM_NODE *SourceStream, - IN EFI_SECTION_TYPE SearchType, - IN OUT UINTN *SectionInstance, - IN EFI_GUID *SectionDefinitionGuid, - OUT CORE_SECTION_CHILD_NODE **FoundChild, - OUT CORE_SECTION_STREAM_NODE **FoundStream, - OUT UINT32 *AuthenticationStatus +CreateChildNode ( + IN CORE_SECTION_STREAM_NODE *Stream, + IN UINT32 ChildOffset, + OUT CORE_SECTION_CHILD_NODE **ChildNode ) { - CORE_SECTION_CHILD_NODE *CurrentChildNode; - CORE_SECTION_CHILD_NODE *RecursedChildNode; - CORE_SECTION_STREAM_NODE *RecursedFoundStream; - UINT32 NextChildOffset; - EFI_STATUS ErrorStatus; - EFI_STATUS Status; - - CurrentChildNode = NULL; - ErrorStatus = EFI_NOT_FOUND; - - if (SourceStream->StreamLength == 0) { - return EFI_NOT_FOUND; - } - - if (IsListEmpty (&SourceStream->Children) && - SourceStream->StreamLength >= sizeof (EFI_COMMON_SECTION_HEADER)) { - // - // This occurs when a section stream exists, but no child sections - // have been parsed out yet. Therefore, extract the first child and add it - // to the list of children so we can get started. - // Section stream may contain an array of zero or more bytes. - // So, its size should be >= the size of commen section header. - // - Status = CreateChildNode (SourceStream, 0, &CurrentChildNode); - if (EFI_ERROR (Status)) { - return Status; - } - } - + EFI_STATUS Status; + EFI_COMMON_SECTION_HEADER *SectionHeader; + EFI_COMPRESSION_SECTION *CompressionHeader; + EFI_GUID_DEFINED_SECTION *GuidedHeader; + EFI_DECOMPRESS_PROTOCOL *Decompress; + EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *GuidedExtraction; + VOID *NewStreamBuffer; + VOID *ScratchBuffer; + UINT32 ScratchSize; + UINTN NewStreamBufferSize; + UINT32 AuthenticationStatus; + UINT32 SectionLength; + + CORE_SECTION_CHILD_NODE *Node; + + SectionHeader = (EFI_COMMON_SECTION_HEADER *) (Stream->StreamBuffer + ChildOffset); + // - // At least one child has been parsed out of the section stream. So, walk - // through the sections that have already been parsed out looking for the - // requested section, if necessary, continue parsing section stream and - // adding children until either the requested section is found, or we run - // out of data + // Allocate a new node // - CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream->Children)); + *ChildNode = AllocatePool (sizeof (CORE_SECTION_CHILD_NODE)); + Node = *ChildNode; + if (Node == NULL) { + return EFI_OUT_OF_RESOURCES; + } - for (;;) { - if (ChildIsType (SourceStream, CurrentChildNode, SearchType, SectionDefinitionGuid)) { - // - // The type matches, so check the instance count to see if it's the one we want - // - (*SectionInstance)--; - if (*SectionInstance == 0) { - // - // Got it! - // - *FoundChild = CurrentChildNode; - *FoundStream = SourceStream; - *AuthenticationStatus = SourceStream->AuthenticationStatus; - return EFI_SUCCESS; - } - } - - if (CurrentChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) { - // - // If the current node is an encapsulating node, recurse into it... - // - Status = FindChildNode ( - (CORE_SECTION_STREAM_NODE *)CurrentChildNode->EncapsulatedStreamHandle, - SearchType, - SectionInstance, - SectionDefinitionGuid, - &RecursedChildNode, - &RecursedFoundStream, - AuthenticationStatus - ); - // - // If the status is not EFI_SUCCESS, just save the error code and continue - // to find the request child node in the rest stream. - // - if (*SectionInstance == 0) { - ASSERT_EFI_ERROR (Status); - *FoundChild = RecursedChildNode; - *FoundStream = RecursedFoundStream; - return EFI_SUCCESS; - } else { - ErrorStatus = Status; - } - } - - if (!IsNodeAtEnd (&SourceStream->Children, &CurrentChildNode->Link)) { - // - // We haven't found the child node we're interested in yet, but there's - // still more nodes that have already been parsed so get the next one - // and continue searching.. - // - CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream->Children, &CurrentChildNode->Link)); - } else { - // - // We've exhausted children that have already been parsed, so see if - // there's any more data and continue parsing out more children if there - // is. - // - NextChildOffset = CurrentChildNode->OffsetInStream + CurrentChildNode->Size; - // - // Round up to 4 byte boundary - // - NextChildOffset += 3; - NextChildOffset &= ~(UINTN) 3; - if (NextChildOffset <= SourceStream->StreamLength - sizeof (EFI_COMMON_SECTION_HEADER)) { - // - // There's an unparsed child remaining in the stream, so create a new child node - // - Status = CreateChildNode (SourceStream, NextChildOffset, &CurrentChildNode); - if (EFI_ERROR (Status)) { - return Status; - } - } else { - ASSERT (EFI_ERROR (ErrorStatus)); - return ErrorStatus; - } - } - } -} - - -/** - Worker function. Constructor for new child nodes. - - @param Stream Indicates the section stream in which to add the - child. - @param ChildOffset Indicates the offset in Stream that is the - beginning of the child section. - @param ChildNode Indicates the Callee allocated and initialized - child. - - @retval EFI_SUCCESS Child node was found and returned. - EFI_OUT_OF_RESOURCES- Memory allocation failed. - @retval EFI_PROTOCOL_ERROR Encapsulation sections produce new stream - handles when the child node is created. If the - section type is GUID defined, and the extraction - GUID does not exist, and producing the stream - requires the GUID, then a protocol error is - generated and no child is produced. Values - returned by OpenSectionStreamEx. - -**/ -EFI_STATUS -CreateChildNode ( - IN CORE_SECTION_STREAM_NODE *Stream, - IN UINT32 ChildOffset, - OUT CORE_SECTION_CHILD_NODE **ChildNode - ) -{ - EFI_STATUS Status; - EFI_COMMON_SECTION_HEADER *SectionHeader; - EFI_COMPRESSION_SECTION *CompressionHeader; - EFI_GUID_DEFINED_SECTION *GuidedHeader; - EFI_DECOMPRESS_PROTOCOL *Decompress; - EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *GuidedExtraction; - VOID *NewStreamBuffer; - VOID *ScratchBuffer; - UINT32 ScratchSize; - UINTN NewStreamBufferSize; - UINT32 AuthenticationStatus; - UINT32 SectionLength; - - CORE_SECTION_CHILD_NODE *Node; - - SectionHeader = (EFI_COMMON_SECTION_HEADER *) (Stream->StreamBuffer + ChildOffset); - - // - // Allocate a new node - // - *ChildNode = CoreAllocateBootServicesPool (sizeof (CORE_SECTION_CHILD_NODE)); - Node = *ChildNode; - if (Node == NULL) { - return EFI_OUT_OF_RESOURCES; - } - // // Now initialize it // @@ -932,7 +542,7 @@ CreateChildNode ( Node->OffsetInStream = ChildOffset; Node->EncapsulatedStreamHandle = NULL_STREAM_HANDLE; Node->EncapsulationGuid = NULL; - + // // If it's an encapsulating section, then create the new section stream also // @@ -942,20 +552,20 @@ CreateChildNode ( // Get the CompressionSectionHeader // ASSERT (Node->Size >= sizeof (EFI_COMPRESSION_SECTION)); - + CompressionHeader = (EFI_COMPRESSION_SECTION *) SectionHeader; - + // // Allocate space for the new stream // if (CompressionHeader->UncompressedLength > 0) { NewStreamBufferSize = CompressionHeader->UncompressedLength; - NewStreamBuffer = CoreAllocateBootServicesPool (NewStreamBufferSize); + NewStreamBuffer = AllocatePool (NewStreamBufferSize); if (NewStreamBuffer == NULL) { CoreFreePool (Node); return EFI_OUT_OF_RESOURCES; } - + if (CompressionHeader->CompressionType == EFI_NOT_COMPRESSED) { // // stream is not actually compressed, just encapsulated. So just copy it. @@ -964,15 +574,15 @@ CreateChildNode ( } else if (CompressionHeader->CompressionType == EFI_STANDARD_COMPRESSION) { // // Only support the EFI_SATNDARD_COMPRESSION algorithm. - // + // // // Decompress the stream // Status = CoreLocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **)&Decompress); - ASSERT_EFI_ERROR (Status); - + ASSERT (Decompress != NULL); + Status = Decompress->GetInfo ( Decompress, CompressionHeader + 1, @@ -983,7 +593,7 @@ CreateChildNode ( ASSERT_EFI_ERROR (Status); ASSERT (NewStreamBufferSize == CompressionHeader->UncompressedLength); - ScratchBuffer = CoreAllocateBootServicesPool (ScratchSize); + ScratchBuffer = AllocatePool (ScratchSize); if (ScratchBuffer == NULL) { CoreFreePool (Node); CoreFreePool (NewStreamBuffer); @@ -1000,13 +610,13 @@ CreateChildNode ( ScratchSize ); ASSERT_EFI_ERROR (Status); - CoreFreePool (ScratchBuffer); + CoreFreePool (ScratchBuffer); } } else { NewStreamBuffer = NULL; NewStreamBufferSize = 0; } - + Status = OpenSectionStreamEx ( NewStreamBufferSize, NewStreamBuffer, @@ -1025,7 +635,7 @@ CreateChildNode ( GuidedHeader = (EFI_GUID_DEFINED_SECTION *) SectionHeader; Node->EncapsulationGuid = &GuidedHeader->SectionDefinitionGuid; Status = CoreLocateProtocol (Node->EncapsulationGuid, NULL, (VOID **)&GuidedExtraction); - if (!EFI_ERROR (Status)) { + if (!EFI_ERROR (Status) && GuidedExtraction != NULL) { // // NewStreamBuffer is always allocated by ExtractSection... No caller // allocation here. @@ -1041,12 +651,12 @@ CreateChildNode ( CoreFreePool (*ChildNode); return EFI_PROTOCOL_ERROR; } - + // - // Make sure we initialize the new stream with the correct + // Make sure we initialize the new stream with the correct // authentication status for both aggregate and local status fields. // - if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) { + if ((GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0) { // // OR in the parent stream's aggregate status. // @@ -1058,7 +668,7 @@ CreateChildNode ( // AuthenticationStatus = Stream->AuthenticationStatus; } - + Status = OpenSectionStreamEx ( NewStreamBufferSize, NewStreamBuffer, @@ -1075,19 +685,19 @@ CreateChildNode ( // // There's no GUIDed section extraction protocol available. // - if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) { + if ((GuidedHeader->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) { // // If the section REQUIRES an extraction protocol, then we're toast // CoreFreePool (*ChildNode); return EFI_PROTOCOL_ERROR; } - + // // Figure out the proper authentication status // AuthenticationStatus = Stream->AuthenticationStatus; - + SectionLength = SECTION_SIZE (GuidedHeader); Status = OpenSectionStreamEx ( SectionLength - GuidedHeader->DataOffset, @@ -1101,23 +711,370 @@ CreateChildNode ( return Status; } } - + + break; + + default: + + // + // Nothing to do if it's a leaf + // break; + } + + // + // Last, add the new child node to the stream + // + InsertTailList (&Stream->Children, &Node->Link); + + return EFI_SUCCESS; +} + + +/** + Worker function Recursively searches / builds section stream database + looking for requested section. + + @param SourceStream Indicates the section stream in which to do the + search. + @param SearchType Indicates the type of section to search for. + @param SectionInstance Indicates which instance of section to find. + This is an in/out parameter to deal with + recursions. + @param SectionDefinitionGuid Guid of section definition + @param FoundChild Output indicating the child node that is found. + @param FoundStream Output indicating which section stream the child + was found in. If this stream was generated as a + result of an encapsulation section, the + streamhandle is visible within the SEP driver + only. + @param AuthenticationStatus Indicates the authentication status of the found section. + + @retval EFI_SUCCESS Child node was found and returned. + EFI_OUT_OF_RESOURCES- Memory allocation failed. + @retval EFI_NOT_FOUND Requested child node does not exist. + @retval EFI_PROTOCOL_ERROR a required GUIDED section extraction protocol + does not exist + +**/ +EFI_STATUS +FindChildNode ( + IN CORE_SECTION_STREAM_NODE *SourceStream, + IN EFI_SECTION_TYPE SearchType, + IN OUT UINTN *SectionInstance, + IN EFI_GUID *SectionDefinitionGuid, + OUT CORE_SECTION_CHILD_NODE **FoundChild, + OUT CORE_SECTION_STREAM_NODE **FoundStream, + OUT UINT32 *AuthenticationStatus + ) +{ + CORE_SECTION_CHILD_NODE *CurrentChildNode; + CORE_SECTION_CHILD_NODE *RecursedChildNode; + CORE_SECTION_STREAM_NODE *RecursedFoundStream; + UINT32 NextChildOffset; + EFI_STATUS ErrorStatus; + EFI_STATUS Status; + + CurrentChildNode = NULL; + ErrorStatus = EFI_NOT_FOUND; + + if (SourceStream->StreamLength == 0) { + return EFI_NOT_FOUND; + } + + if (IsListEmpty (&SourceStream->Children) && + SourceStream->StreamLength >= sizeof (EFI_COMMON_SECTION_HEADER)) { + // + // This occurs when a section stream exists, but no child sections + // have been parsed out yet. Therefore, extract the first child and add it + // to the list of children so we can get started. + // Section stream may contain an array of zero or more bytes. + // So, its size should be >= the size of commen section header. + // + Status = CreateChildNode (SourceStream, 0, &CurrentChildNode); + if (EFI_ERROR (Status)) { + return Status; + } + } + + // + // At least one child has been parsed out of the section stream. So, walk + // through the sections that have already been parsed out looking for the + // requested section, if necessary, continue parsing section stream and + // adding children until either the requested section is found, or we run + // out of data + // + CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream->Children)); + + for (;;) { + ASSERT (CurrentChildNode != NULL); + if (ChildIsType (SourceStream, CurrentChildNode, SearchType, SectionDefinitionGuid)) { + // + // The type matches, so check the instance count to see if it's the one we want + // + (*SectionInstance)--; + if (*SectionInstance == 0) { + // + // Got it! + // + *FoundChild = CurrentChildNode; + *FoundStream = SourceStream; + *AuthenticationStatus = SourceStream->AuthenticationStatus; + return EFI_SUCCESS; + } + } + + if (CurrentChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) { + // + // If the current node is an encapsulating node, recurse into it... + // + Status = FindChildNode ( + (CORE_SECTION_STREAM_NODE *)CurrentChildNode->EncapsulatedStreamHandle, + SearchType, + SectionInstance, + SectionDefinitionGuid, + &RecursedChildNode, + &RecursedFoundStream, + AuthenticationStatus + ); + // + // If the status is not EFI_SUCCESS, just save the error code and continue + // to find the request child node in the rest stream. + // + if (*SectionInstance == 0) { + ASSERT_EFI_ERROR (Status); + *FoundChild = RecursedChildNode; + *FoundStream = RecursedFoundStream; + return EFI_SUCCESS; + } else { + ErrorStatus = Status; + } + } + + if (!IsNodeAtEnd (&SourceStream->Children, &CurrentChildNode->Link)) { + // + // We haven't found the child node we're interested in yet, but there's + // still more nodes that have already been parsed so get the next one + // and continue searching.. + // + CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream->Children, &CurrentChildNode->Link)); + } else { + // + // We've exhausted children that have already been parsed, so see if + // there's any more data and continue parsing out more children if there + // is. + // + NextChildOffset = CurrentChildNode->OffsetInStream + CurrentChildNode->Size; + // + // Round up to 4 byte boundary + // + NextChildOffset += 3; + NextChildOffset &= ~(UINTN) 3; + if (NextChildOffset <= SourceStream->StreamLength - sizeof (EFI_COMMON_SECTION_HEADER)) { + // + // There's an unparsed child remaining in the stream, so create a new child node + // + Status = CreateChildNode (SourceStream, NextChildOffset, &CurrentChildNode); + if (EFI_ERROR (Status)) { + return Status; + } + } else { + ASSERT (EFI_ERROR (ErrorStatus)); + return ErrorStatus; + } + } + } +} + + +/** + Worker function. Search stream database for requested stream handle. + + @param SearchHandle Indicates which stream to look for. + @param FoundStream Output pointer to the found stream. + + @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains + the stream node. + @retval EFI_NOT_FOUND SearchHandle was not found in the stream + database. + +**/ +EFI_STATUS +FindStreamNode ( + IN UINTN SearchHandle, + OUT CORE_SECTION_STREAM_NODE **FoundStream + ) +{ + CORE_SECTION_STREAM_NODE *StreamNode; + + if (!IsListEmpty (&mStreamRoot)) { + StreamNode = STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot)); + for (;;) { + if (StreamNode->StreamHandle == SearchHandle) { + *FoundStream = StreamNode; + return EFI_SUCCESS; + } else if (IsNodeAtEnd (&mStreamRoot, &StreamNode->Link)) { + break; + } else { + StreamNode = STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot, &StreamNode->Link)); + } + } + } + + return EFI_NOT_FOUND; +} + + +/** + SEP member function. Retrieves requested section from section stream. + + @param SectionStreamHandle The section stream from which to extract the + requested section. + @param SectionType A pointer to the type of section to search for. + @param SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED, + then SectionDefinitionGuid indicates which of + these types of sections to search for. + @param SectionInstance Indicates which instance of the requested + section to return. + @param Buffer Double indirection to buffer. If *Buffer is + non-null on input, then the buffer is caller + allocated. If Buffer is NULL, then the buffer + is callee allocated. In either case, the + requried buffer size is returned in *BufferSize. + @param BufferSize On input, indicates the size of *Buffer if + *Buffer is non-null on input. On output, + indicates the required size (allocated size if + callee allocated) of *Buffer. + @param AuthenticationStatus A pointer to a caller-allocated UINT32 that + indicates the authentication status of the + output buffer. If the input section's + GuidedSectionHeader.Attributes field + has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID + bit as clear, AuthenticationStatus must return + zero. Both local bits (19:16) and aggregate + bits (3:0) in AuthenticationStatus are returned + by ExtractSection(). These bits reflect the + status of the extraction operation. The bit + pattern in both regions must be the same, as + the local and aggregate authentication statuses + have equivalent meaning at this level. If the + function returns anything other than + EFI_SUCCESS, the value of *AuthenticationStatus + is undefined. + + @retval EFI_SUCCESS Section was retrieved successfully + @retval EFI_PROTOCOL_ERROR A GUID defined section was encountered in the + section stream with its + EFI_GUIDED_SECTION_PROCESSING_REQUIRED bit set, + but there was no corresponding GUIDed Section + Extraction Protocol in the handle database. + *Buffer is unmodified. + @retval EFI_NOT_FOUND An error was encountered when parsing the + SectionStream. This indicates the SectionStream + is not correctly formatted. + @retval EFI_NOT_FOUND The requested section does not exist. + @retval EFI_OUT_OF_RESOURCES The system has insufficient resources to process + the request. + @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist. + @retval EFI_WARN_TOO_SMALL The size of the caller allocated input buffer is + insufficient to contain the requested section. + The input buffer is filled and section contents + are truncated. + +**/ +EFI_STATUS +EFIAPI +GetSection ( + IN UINTN SectionStreamHandle, + IN EFI_SECTION_TYPE *SectionType, + IN EFI_GUID *SectionDefinitionGuid, + IN UINTN SectionInstance, + IN VOID **Buffer, + IN OUT UINTN *BufferSize, + OUT UINT32 *AuthenticationStatus + ) +{ + CORE_SECTION_STREAM_NODE *StreamNode; + EFI_TPL OldTpl; + EFI_STATUS Status; + CORE_SECTION_CHILD_NODE *ChildNode; + CORE_SECTION_STREAM_NODE *ChildStreamNode; + UINTN CopySize; + UINT32 ExtractedAuthenticationStatus; + UINTN Instance; + UINT8 *CopyBuffer; + UINTN SectionSize; - default: - - // - // Nothing to do if it's a leaf - // - break; + + OldTpl = CoreRaiseTpl (TPL_NOTIFY); + Instance = SectionInstance + 1; + + // + // Locate target stream + // + Status = FindStreamNode (SectionStreamHandle, &StreamNode); + if (EFI_ERROR (Status)) { + Status = EFI_INVALID_PARAMETER; + goto GetSection_Done; } - + // - // Last, add the new child node to the stream + // Found the stream, now locate and return the appropriate section // - InsertTailList (&Stream->Children, &Node->Link); + if (SectionType == NULL) { + // + // SectionType == NULL means return the WHOLE section stream... + // + CopySize = StreamNode->StreamLength; + CopyBuffer = StreamNode->StreamBuffer; + *AuthenticationStatus = StreamNode->AuthenticationStatus; + } else { + // + // There's a requested section type, so go find it and return it... + // + Status = FindChildNode ( + StreamNode, + *SectionType, + &Instance, + SectionDefinitionGuid, + &ChildNode, + &ChildStreamNode, + &ExtractedAuthenticationStatus + ); + if (EFI_ERROR (Status)) { + goto GetSection_Done; + } + CopySize = ChildNode->Size - sizeof (EFI_COMMON_SECTION_HEADER); + CopyBuffer = ChildStreamNode->StreamBuffer + ChildNode->OffsetInStream + sizeof (EFI_COMMON_SECTION_HEADER); + *AuthenticationStatus = ExtractedAuthenticationStatus; + } - return EFI_SUCCESS; + SectionSize = CopySize; + if (*Buffer != NULL) { + // + // Caller allocated buffer. Fill to size and return required size... + // + if (*BufferSize < CopySize) { + Status = EFI_WARN_BUFFER_TOO_SMALL; + CopySize = *BufferSize; + } + } else { + // + // Callee allocated buffer. Allocate buffer and return size. + // + *Buffer = AllocatePool (CopySize); + if (*Buffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto GetSection_Done; + } + } + CopyMem (*Buffer, CopyBuffer, CopySize); + *BufferSize = SectionSize; + +GetSection_Done: + CoreRestoreTpl (OldTpl); + + return Status; } @@ -1137,7 +1094,7 @@ FreeChildNode ( // Remove the child from it's list // RemoveEntryList (&ChildNode->Link); - + if (ChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) { // // If it's an encapsulating section, we close the resulting section stream. @@ -1149,201 +1106,57 @@ FreeChildNode ( // Last, free the child node itself // CoreFreePool (ChildNode); -} - +} /** - Worker function. Constructor for section streams. + SEP member function. Deletes an existing section stream - @param SectionStreamLength Size in bytes of the section stream. - @param SectionStream Buffer containing the new section stream. - @param AllocateBuffer Indicates whether the stream buffer is to be - copied or the input buffer is to be used in - place. AuthenticationStatus- Indicates the - default authentication status for the new - stream. - @param AuthenticationStatus A pointer to a caller-allocated UINT32 that - indicates the authentication status of the - output buffer. If the input section's - GuidedSectionHeader.Attributes field - has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID - bit as clear, AuthenticationStatus must return - zero. Both local bits (19:16) and aggregate - bits (3:0) in AuthenticationStatus are returned - by ExtractSection(). These bits reflect the - status of the extraction operation. The bit - pattern in both regions must be the same, as - the local and aggregate authentication statuses - have equivalent meaning at this level. If the - function returns anything other than - EFI_SUCCESS, the value of *AuthenticationStatus - is undefined. - @param SectionStreamHandle A pointer to a caller allocated section stream - handle. + @param StreamHandleToClose Indicates the stream to close - @retval EFI_SUCCESS Stream was added to stream database. - @retval EFI_OUT_OF_RESOURCES memory allocation failed. + @retval EFI_SUCCESS The section stream is closed sucessfully. + @retval EFI_OUT_OF_RESOURCES Memory allocation failed. + @retval EFI_INVALID_PARAMETER Section stream does not end concident with end + of last section. **/ EFI_STATUS -OpenSectionStreamEx ( - IN UINTN SectionStreamLength, - IN VOID *SectionStream, - IN BOOLEAN AllocateBuffer, - IN UINT32 AuthenticationStatus, - OUT UINTN *SectionStreamHandle +EFIAPI +CloseSectionStream ( + IN UINTN StreamHandleToClose ) { - CORE_SECTION_STREAM_NODE *NewStream; - EFI_TPL OldTpl; - + CORE_SECTION_STREAM_NODE *StreamNode; + EFI_TPL OldTpl; + EFI_STATUS Status; + LIST_ENTRY *Link; + CORE_SECTION_CHILD_NODE *ChildNode; + + OldTpl = CoreRaiseTpl (TPL_NOTIFY); + // - // Allocate a new stream + // Locate target stream // - NewStream = CoreAllocateBootServicesPool (sizeof (CORE_SECTION_STREAM_NODE)); - if (NewStream == NULL) { - return EFI_OUT_OF_RESOURCES; - } - - if (AllocateBuffer) { + Status = FindStreamNode (StreamHandleToClose, &StreamNode); + if (!EFI_ERROR (Status)) { // - // if we're here, we're double buffering, allocate the buffer and copy the - // data in + // Found the stream, so close it // - if (SectionStreamLength > 0) { - NewStream->StreamBuffer = CoreAllocateBootServicesPool (SectionStreamLength); - if (NewStream->StreamBuffer == NULL) { - CoreFreePool (NewStream); - return EFI_OUT_OF_RESOURCES; - } - // - // Copy in stream data - // - CopyMem (NewStream->StreamBuffer, SectionStream, SectionStreamLength); - } else { - // - // It's possible to have a zero length section stream. - // - NewStream->StreamBuffer = NULL; + RemoveEntryList (&StreamNode->Link); + while (!IsListEmpty (&StreamNode->Children)) { + Link = GetFirstNode (&StreamNode->Children); + ChildNode = CHILD_SECTION_NODE_FROM_LINK (Link); + FreeChildNode (ChildNode); } + CoreFreePool (StreamNode->StreamBuffer); + CoreFreePool (StreamNode); + Status = EFI_SUCCESS; } else { - // - // If were here, the caller has supplied the buffer (it's an internal call) - // so just assign the buffer. This happens when we open section streams - // as a result of expanding an encapsulating section. - // - NewStream->StreamBuffer = SectionStream; - } - - // - // Initialize the rest of the section stream - // - NewStream->Signature = CORE_SECTION_STREAM_SIGNATURE; - NewStream->StreamHandle = (UINTN) NewStream; - NewStream->StreamLength = SectionStreamLength; - InitializeListHead (&NewStream->Children); - NewStream->AuthenticationStatus = AuthenticationStatus; - - // - // Add new stream to stream list - // - OldTpl = CoreRaiseTpl (TPL_NOTIFY); - InsertTailList (&mStreamRoot, &NewStream->Link); - CoreRestoreTpl (OldTpl); - - *SectionStreamHandle = NewStream->StreamHandle; - - return EFI_SUCCESS; -} - - - -/** - Worker function. Search stream database for requested stream handle. - - @param SearchHandle Indicates which stream to look for. - @param FoundStream Output pointer to the found stream. - - @retval EFI_SUCCESS StreamHandle was found and *FoundStream contains - the stream node. - @retval EFI_NOT_FOUND SearchHandle was not found in the stream - database. - -**/ -EFI_STATUS -FindStreamNode ( - IN UINTN SearchHandle, - OUT CORE_SECTION_STREAM_NODE **FoundStream - ) -{ - CORE_SECTION_STREAM_NODE *StreamNode; - - if (!IsListEmpty (&mStreamRoot)) { - StreamNode = STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot)); - for (;;) { - if (StreamNode->StreamHandle == SearchHandle) { - *FoundStream = StreamNode; - return EFI_SUCCESS; - } else if (IsNodeAtEnd (&mStreamRoot, &StreamNode->Link)) { - break; - } else { - StreamNode = STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot, &StreamNode->Link)); - } - } - } - - return EFI_NOT_FOUND; -} - - -/** - Check if a stream is valid. - - @param SectionStream The section stream to be checked - @param SectionStreamLength The length of section stream - - @return A boolean value indicating the validness of the section stream. - -**/ -BOOLEAN -IsValidSectionStream ( - IN VOID *SectionStream, - IN UINTN SectionStreamLength - ) -{ - UINTN TotalLength; - UINTN SectionLength; - EFI_COMMON_SECTION_HEADER *SectionHeader; - EFI_COMMON_SECTION_HEADER *NextSectionHeader; - - TotalLength = 0; - SectionHeader = (EFI_COMMON_SECTION_HEADER *)SectionStream; - - while (TotalLength < SectionStreamLength) { - SectionLength = SECTION_SIZE (SectionHeader); - TotalLength += SectionLength; - - if (TotalLength == SectionStreamLength) { - return TRUE; - } - - // - // Move to the next byte following the section... - // - SectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) SectionHeader + SectionLength); - - // - // Figure out where the next section begins - // - NextSectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINTN) SectionHeader + 3); - NextSectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINTN) NextSectionHeader & ~(UINTN)3); - TotalLength += (UINTN) NextSectionHeader - (UINTN) SectionHeader; - SectionHeader = NextSectionHeader; + Status = EFI_INVALID_PARAMETER; } - ASSERT (FALSE); - return FALSE; + CoreRestoreTpl (OldTpl); + return Status; } @@ -1373,7 +1186,7 @@ IsValidSectionStream ( EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is defined in RaiseTPL() in the UEFI 2.0 specification. - + @param This Indicates the EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance. @param InputSection Buffer containing the input GUIDed section @@ -1446,7 +1259,7 @@ CustomGuidedSectionExtract ( UINT32 OutputBufferSize; UINT32 ScratchBufferSize; UINT16 SectionAttribute; - + // // Init local variable // @@ -1462,28 +1275,29 @@ CustomGuidedSectionExtract ( &ScratchBufferSize, &SectionAttribute ); - + if (EFI_ERROR (Status)) { DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status)); return Status; } - - if (ScratchBufferSize != 0) { + + if (ScratchBufferSize > 0) { // // Allocate scratch buffer // - ScratchBuffer = CoreAllocateBootServicesPool (ScratchBufferSize); + ScratchBuffer = AllocatePool (ScratchBufferSize); if (ScratchBuffer == NULL) { return EFI_OUT_OF_RESOURCES; } } - if (OutputBufferSize > 0) { + if (OutputBufferSize > 0) { // // Allocate output buffer // - AllocatedOutputBuffer = CoreAllocateBootServicesPool (OutputBufferSize); + AllocatedOutputBuffer = AllocatePool (OutputBufferSize); if (AllocatedOutputBuffer == NULL) { + FreePool (ScratchBuffer); return EFI_OUT_OF_RESOURCES; } *OutputBuffer = AllocatedOutputBuffer; @@ -1493,7 +1307,7 @@ CustomGuidedSectionExtract ( // Call decode function to extract raw data from the guided section. // Status = ExtractGuidedSectionDecode ( - InputSection, + InputSection, OutputBuffer, ScratchBuffer, AuthenticationStatus @@ -1514,9 +1328,9 @@ CustomGuidedSectionExtract ( if (*OutputBuffer != AllocatedOutputBuffer) { // - // OutputBuffer was returned as a different value, + // OutputBuffer was returned as a different value, // so copy section contents to the allocated memory buffer. - // + // CopyMem (AllocatedOutputBuffer, *OutputBuffer, OutputBufferSize); *OutputBuffer = AllocatedOutputBuffer; }