]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / SectionExtraction / CoreSectionExtraction.c
index 07ada03d2ce72c119ea8465035c80047e3a50f6c..a1d5316c657db18908e310d845d7dee3997140d8 100644 (file)
@@ -1,34 +1,34 @@
 /** @file\r
   Section Extraction Protocol implementation.\r
-  \r
+\r
   Stream database is implemented as a linked list of section streams,\r
   where each stream contains a linked list of children, which may be leaves or\r
-  encapsulations.  \r
-  \r
+  encapsulations.\r
+\r
   Children that are encapsulations generate new stream entries\r
-  when they are created.  Streams can also be created by calls to \r
+  when they are created.  Streams can also be created by calls to\r
   SEP->OpenSectionStream().\r
-  \r
+\r
   The database is only created far enough to return the requested data from\r
   any given stream, or to determine that the requested data is not found.\r
-  \r
+\r
   If a GUIDed encapsulation is encountered, there are three possiblilites.\r
-  \r
+\r
   1) A support protocol is found, in which the stream is simply processed with\r
      the support protocol.\r
-     \r
+\r
   2) A support protocol is not found, but the data is available to be read\r
      without processing.  In this case, the database is built up through the\r
      recursions to return the data, and a RPN event is set that will enable\r
      the stream in question to be refreshed if and when the required section\r
-     extraction protocol is published.This insures the AuthenticationStatus \r
+     extraction protocol is published.This insures the AuthenticationStatus\r
      does not become stale in the cache.\r
-     \r
+\r
   3) A support protocol is not found, and the data is not available to be read\r
      without it.  This results in EFI_PROTOCOL_ERROR.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
@@ -38,12 +38,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <DxeMain.h>\r
+#include "DxeMain.h"\r
 \r
 //\r
 // Local defines and typedefs\r
 //\r
-#define CORE_SECTION_CHILD_SIGNATURE  EFI_SIGNATURE_32('S','X','C','S')\r
+#define CORE_SECTION_CHILD_SIGNATURE  SIGNATURE_32('S','X','C','S')\r
 #define CHILD_SECTION_NODE_FROM_LINK(Node) \\r
   CR (Node, CORE_SECTION_CHILD_NODE, Link, CORE_SECTION_CHILD_SIGNATURE)\r
 \r
@@ -66,9 +66,14 @@ typedef struct {
   //\r
   UINTN                       EncapsulatedStreamHandle;\r
   EFI_GUID                    *EncapsulationGuid;\r
+  //\r
+  // If the section REQUIRES an extraction protocol, register for RPN\r
+  // when the required GUIDed extraction protocol becomes available.\r
+  //\r
+  EFI_EVENT                   Event;\r
 } CORE_SECTION_CHILD_NODE;\r
 \r
-#define CORE_SECTION_STREAM_SIGNATURE EFI_SIGNATURE_32('S','X','S','S')\r
+#define CORE_SECTION_STREAM_SIGNATURE SIGNATURE_32('S','X','S','S')\r
 #define STREAM_NODE_FROM_LINK(Node) \\r
   CR (Node, CORE_SECTION_STREAM_NODE, Link, CORE_SECTION_STREAM_SIGNATURE)\r
 \r
@@ -91,193 +96,7 @@ typedef struct {
   CORE_SECTION_CHILD_NODE     *ChildNode;\r
   CORE_SECTION_STREAM_NODE    *ParentStream;\r
   VOID                        *Registration;\r
-  EFI_EVENT                   Event;\r
 } RPN_EVENT_CONTEXT;\r
-  \r
-  \r
-\r
-//\r
-// Local prototypes\r
-//\r
-/**\r
-  Worker function.  Determine if the input stream:child matches the input type.\r
-\r
-  @param  Stream                 Indicates the section stream associated with the \r
-                                 child \r
-  @param  Child                  Indicates the child to check \r
-  @param  SearchType             Indicates the type of section to check against \r
-                                 for \r
-  @param  SectionDefinitionGuid  Indicates the GUID to check against if the type \r
-                                 is EFI_SECTION_GUID_DEFINED \r
-\r
-  @retval TRUE                   The child matches \r
-  @retval FALSE                  The child doesn't match\r
-\r
-**/\r
-BOOLEAN\r
-ChildIsType (\r
-  IN CORE_SECTION_STREAM_NODE *Stream,\r
-  IN CORE_SECTION_CHILD_NODE  *Child,\r
-  IN EFI_SECTION_TYPE         SearchType,\r
-  IN EFI_GUID                 *SectionDefinitionGuid\r
-  );\r
-\r
-\r
-/**\r
-  Worker function.  Search stream database for requested stream handle.\r
-\r
-  @param  SearchHandle           Indicates which stream to look for. \r
-  @param  FoundStream            Output pointer to the found stream. \r
-\r
-  @retval EFI_SUCCESS            StreamHandle was found and *FoundStream contains \r
-                                 the stream node. \r
-  @retval EFI_NOT_FOUND          SearchHandle was not found in the stream \r
-                                 database.\r
-\r
-**/\r
-EFI_STATUS\r
-FindStreamNode (\r
-  IN  UINTN                                     SearchHandle,\r
-  OUT CORE_SECTION_STREAM_NODE                  **FoundStream\r
-  );\r
-  \r
-\r
-/**\r
-  Worker function  Recursively searches / builds section stream database\r
-  looking for requested section.\r
-\r
-  @param  SourceStream           Indicates the section stream in which to do the \r
-                                 search. \r
-  @param  SearchType             Indicates the type of section to search for. \r
-  @param  SectionInstance        Indicates which instance of section to find.  \r
-                                 This is an in/out parameter to deal with \r
-                                 recursions. \r
-  @param  SectionDefinitionGuid  Guid of section definition \r
-  @param  FoundChild             Output indicating the child node that is found. \r
-  @param  FoundStream            Output indicating which section stream the child \r
-                                 was found in.  If this stream was generated as a \r
-                                 result of an encapsulation section, the \r
-                                 streamhandle is visible within the SEP driver \r
-                                 only.\r
-  @param  AuthenticationStatus   Indicates the authentication status of the found section. \r
-\r
-  @retval EFI_SUCCESS            Child node was found and returned. \r
-                                 EFI_OUT_OF_RESOURCES- Memory allocation failed. \r
-  @retval EFI_NOT_FOUND          Requested child node does not exist. \r
-  @retval EFI_PROTOCOL_ERROR     a required GUIDED section extraction protocol \r
-                                 does not exist\r
-\r
-**/\r
-EFI_STATUS\r
-FindChildNode (\r
-  IN     CORE_SECTION_STREAM_NODE                   *SourceStream,\r
-  IN     EFI_SECTION_TYPE                           SearchType,\r
-  IN OUT UINTN                                      *SectionInstance,\r
-  IN     EFI_GUID                                   *SectionDefinitionGuid,\r
-  OUT    CORE_SECTION_CHILD_NODE                    **FoundChild,\r
-  OUT    CORE_SECTION_STREAM_NODE                   **FoundStream,\r
-  OUT    UINT32                                     *AuthenticationStatus\r
-  );\r
-  \r
-\r
-/**\r
-  Worker function.  Constructor for new child nodes.\r
-\r
-  @param  Stream                 Indicates the section stream in which to add the \r
-                                 child. \r
-  @param  ChildOffset            Indicates the offset in Stream that is the \r
-                                 beginning of the child section. \r
-  @param  ChildNode              Indicates the Callee allocated and initialized \r
-                                 child. \r
-\r
-  @retval EFI_SUCCESS            Child node was found and returned. \r
-                                 EFI_OUT_OF_RESOURCES- Memory allocation failed. \r
-  @retval EFI_PROTOCOL_ERROR     Encapsulation sections produce new stream \r
-                                 handles when the child node is created.  If the \r
-                                 section type is GUID defined, and the extraction \r
-                                 GUID does not exist, and producing the stream \r
-                                 requires the GUID, then a protocol error is \r
-                                 generated and no child is produced. Values \r
-                                 returned by OpenSectionStreamEx.\r
-\r
-**/\r
-EFI_STATUS\r
-CreateChildNode (\r
-  IN     CORE_SECTION_STREAM_NODE              *Stream,\r
-  IN     UINT32                                ChildOffset,\r
-     OUT CORE_SECTION_CHILD_NODE               **ChildNode\r
-  );\r
-  \r
-\r
-/**\r
-  Worker function.  Destructor for child nodes.\r
-\r
-  @param  ChildNode              Indicates the node to destroy\r
-\r
-**/\r
-VOID\r
-FreeChildNode (\r
-  IN  CORE_SECTION_CHILD_NODE                   *ChildNode\r
-  );\r
-\r
-\r
-/**\r
-  Worker function.  Constructor for section streams.\r
-\r
-  @param  SectionStreamLength    Size in bytes of the section stream. \r
-  @param  SectionStream          Buffer containing the new section stream. \r
-  @param  AllocateBuffer         Indicates whether the stream buffer is to be \r
-                                 copied or the input buffer is to be used in \r
-                                 place. AuthenticationStatus- Indicates the \r
-                                 default authentication status for the new \r
-                                 stream. \r
-  @param  AuthenticationStatus   A pointer to a caller-allocated UINT32 that\r
-                                 indicates the authentication status of the\r
-                                 output buffer. If the input section's\r
-                                 GuidedSectionHeader.Attributes field\r
-                                 has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
-                                 bit as clear, AuthenticationStatus must return\r
-                                 zero. Both local bits (19:16) and aggregate\r
-                                 bits (3:0) in AuthenticationStatus are returned\r
-                                 by ExtractSection(). These bits reflect the\r
-                                 status of the extraction operation. The bit\r
-                                 pattern in both regions must be the same, as\r
-                                 the local and aggregate authentication statuses\r
-                                 have equivalent meaning at this level. If the\r
-                                 function returns anything other than\r
-                                 EFI_SUCCESS, the value of *AuthenticationStatus\r
-                                 is undefined.\r
-  @param  SectionStreamHandle    A pointer to a caller allocated section stream \r
-                                 handle. \r
-\r
-  @retval EFI_SUCCESS            Stream was added to stream database. \r
-  @retval EFI_OUT_OF_RESOURCES   memory allocation failed.\r
-\r
-**/\r
-EFI_STATUS\r
-OpenSectionStreamEx (\r
-  IN     UINTN                                     SectionStreamLength,\r
-  IN     VOID                                      *SectionStream,\r
-  IN     BOOLEAN                                   AllocateBuffer,\r
-  IN     UINT32                                    AuthenticationStatus,   \r
-     OUT UINTN                                     *SectionStreamHandle\r
-  );\r
-\r
-\r
-/**\r
-  Check if a stream is valid.\r
-\r
-  @param  SectionStream          The section stream to be checked \r
-  @param  SectionStreamLength    The length of section stream \r
-\r
-  @return A boolean value indicating the validness of the section stream.\r
-\r
-**/\r
-BOOLEAN\r
-IsValidSectionStream (\r
-  IN  VOID              *SectionStream,\r
-  IN  UINTN             SectionStreamLength\r
-  );\r
 \r
 \r
 /**\r
@@ -306,7 +125,7 @@ IsValidSectionStream (
   EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is\r
   defined in RaiseTPL() in the UEFI 2.0 specification.\r
 \r
-  \r
+\r
   @param This         Indicates the\r
                       EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.\r
   @param InputSection Buffer containing the input GUIDed section\r
@@ -323,6 +142,7 @@ IsValidSectionStream (
                       is stored. If the function returns\r
                       anything other than EFI_SUCCESS, the value\r
                       of OutputSize is undefined.\r
+\r
   @param AuthenticationStatus A pointer to a caller-allocated\r
                               UINT32 that indicates the\r
                               authentication status of the\r
@@ -370,7 +190,7 @@ CustomGuidedSectionExtract (
   OUT       VOID                                   **OutputBuffer,\r
   OUT       UINTN                                  *OutputSize,\r
   OUT       UINT32                                 *AuthenticationStatus\r
-  );  \r
+  );\r
 \r
 //\r
 // Module globals\r
@@ -382,7 +202,7 @@ EFI_HANDLE mSectionExtractionHandle = NULL;
 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL mCustomGuidedSectionExtractionProtocol = {\r
   CustomGuidedSectionExtract\r
 };\r
-                                             \r
+\r
 \r
 /**\r
   Entry point of the section extraction code. Initializes an instance of the\r
@@ -407,13 +227,13 @@ InitializeSectionExtraction (
   UINTN                              ExtractHandlerNumber;\r
 \r
   //\r
-  // Get custom extract guided section method guid list \r
+  // Get custom extract guided section method guid list\r
   //\r
   ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable);\r
-  \r
+\r
   Status = EFI_SUCCESS;\r
   //\r
-  // Install custom guided extraction protocol \r
+  // Install custom guided extraction protocol\r
   //\r
   while (ExtractHandlerNumber-- > 0) {\r
     Status = CoreInstallProtocolInterface (\r
@@ -430,247 +250,200 @@ InitializeSectionExtraction (
 \r
 \r
 /**\r
-  SEP member function.  This function creates and returns a new section stream\r
-  handle to represent the new section stream.\r
+  Check if a stream is valid.\r
 \r
-  @param  SectionStreamLength    Size in bytes of the section stream. \r
-  @param  SectionStream          Buffer containing the new section stream. \r
-  @param  SectionStreamHandle    A pointer to a caller allocated UINTN that on \r
-                                 output contains the new section stream handle. \r
+  @param  SectionStream          The section stream to be checked\r
+  @param  SectionStreamLength    The length of section stream\r
 \r
-  @retval EFI_SUCCESS            The section stream is created successfully.\r
-  @retval EFI_OUT_OF_RESOURCES   memory allocation failed. \r
-  @retval EFI_INVALID_PARAMETER  Section stream does not end concident with end \r
-                                 of last section.\r
+  @return A boolean value indicating the validness of the section stream.\r
+\r
+**/\r
+BOOLEAN\r
+IsValidSectionStream (\r
+  IN  VOID              *SectionStream,\r
+  IN  UINTN             SectionStreamLength\r
+  )\r
+{\r
+  UINTN                       TotalLength;\r
+  UINTN                       SectionLength;\r
+  EFI_COMMON_SECTION_HEADER   *SectionHeader;\r
+  EFI_COMMON_SECTION_HEADER   *NextSectionHeader;\r
+\r
+  TotalLength = 0;\r
+  SectionHeader = (EFI_COMMON_SECTION_HEADER *)SectionStream;\r
+\r
+  while (TotalLength < SectionStreamLength) {\r
+    if (IS_SECTION2 (SectionHeader)) {\r
+      SectionLength = SECTION2_SIZE (SectionHeader);\r
+    } else {\r
+      SectionLength = SECTION_SIZE (SectionHeader);\r
+    }\r
+    TotalLength += SectionLength;\r
+\r
+    if (TotalLength == SectionStreamLength) {\r
+      return TRUE;\r
+    }\r
+\r
+    //\r
+    // Move to the next byte following the section...\r
+    //\r
+    SectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) SectionHeader + SectionLength);\r
+\r
+    //\r
+    // Figure out where the next section begins\r
+    //\r
+    NextSectionHeader = ALIGN_POINTER(SectionHeader, 4);\r
+    TotalLength += (UINTN) NextSectionHeader - (UINTN) SectionHeader;\r
+    SectionHeader = NextSectionHeader;\r
+  }\r
+\r
+  ASSERT (FALSE);\r
+  return FALSE;\r
+}\r
+\r
+\r
+/**\r
+  Worker function.  Constructor for section streams.\r
+\r
+  @param  SectionStreamLength    Size in bytes of the section stream.\r
+  @param  SectionStream          Buffer containing the new section stream.\r
+  @param  AllocateBuffer         Indicates whether the stream buffer is to be\r
+                                 copied or the input buffer is to be used in\r
+                                 place. AuthenticationStatus- Indicates the\r
+                                 default authentication status for the new\r
+                                 stream.\r
+  @param  AuthenticationStatus   A pointer to a caller-allocated UINT32 that\r
+                                 indicates the authentication status of the\r
+                                 output buffer. If the input section's\r
+                                 GuidedSectionHeader.Attributes field\r
+                                 has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
+                                 bit as clear, AuthenticationStatus must return\r
+                                 zero. Both local bits (19:16) and aggregate\r
+                                 bits (3:0) in AuthenticationStatus are returned\r
+                                 by ExtractSection(). These bits reflect the\r
+                                 status of the extraction operation. The bit\r
+                                 pattern in both regions must be the same, as\r
+                                 the local and aggregate authentication statuses\r
+                                 have equivalent meaning at this level. If the\r
+                                 function returns anything other than\r
+                                 EFI_SUCCESS, the value of *AuthenticationStatus\r
+                                 is undefined.\r
+  @param  SectionStreamHandle    A pointer to a caller allocated section stream\r
+                                 handle.\r
+\r
+  @retval EFI_SUCCESS            Stream was added to stream database.\r
+  @retval EFI_OUT_OF_RESOURCES   memory allocation failed.\r
 \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
-OpenSectionStream (\r
+OpenSectionStreamEx (\r
   IN     UINTN                                     SectionStreamLength,\r
   IN     VOID                                      *SectionStream,\r
+  IN     BOOLEAN                                   AllocateBuffer,\r
+  IN     UINT32                                    AuthenticationStatus,\r
      OUT UINTN                                     *SectionStreamHandle\r
   )\r
 {\r
+  CORE_SECTION_STREAM_NODE    *NewStream;\r
+  EFI_TPL                     OldTpl;\r
+\r
   //\r
-  // Check to see section stream looks good...\r
+  // Allocate a new stream\r
   //\r
-  if (!IsValidSectionStream (SectionStream, SectionStreamLength)) {\r
-    return EFI_INVALID_PARAMETER;\r
+  NewStream = AllocatePool (sizeof (CORE_SECTION_STREAM_NODE));\r
+  if (NewStream == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
   }\r
-  \r
-  return OpenSectionStreamEx ( \r
-           SectionStreamLength, \r
-           SectionStream,\r
-           TRUE,\r
-           0,\r
-           SectionStreamHandle\r
-           );\r
-}\r
-  \r
 \r
-/**\r
-  SEP member function.  Retrieves requested section from section stream.\r
+  if (AllocateBuffer) {\r
+    //\r
+    // if we're here, we're double buffering, allocate the buffer and copy the\r
+    // data in\r
+    //\r
+    if (SectionStreamLength > 0) {\r
+      NewStream->StreamBuffer = AllocatePool (SectionStreamLength);\r
+      if (NewStream->StreamBuffer == NULL) {\r
+        CoreFreePool (NewStream);\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+      //\r
+      // Copy in stream data\r
+      //\r
+      CopyMem (NewStream->StreamBuffer, SectionStream, SectionStreamLength);\r
+    } else {\r
+      //\r
+      // It's possible to have a zero length section stream.\r
+      //\r
+      NewStream->StreamBuffer = NULL;\r
+    }\r
+  } else {\r
+    //\r
+    // If were here, the caller has supplied the buffer (it's an internal call)\r
+    // so just assign the buffer.  This happens when we open section streams\r
+    // as a result of expanding an encapsulating section.\r
+    //\r
+    NewStream->StreamBuffer = SectionStream;\r
+  }\r
 \r
-  @param  SectionStreamHandle   The section stream from which to extract the\r
-                                requested section.\r
-  @param  SectionType           A pointer to the type of section to search for.\r
-  @param  SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED,\r
-                                then SectionDefinitionGuid indicates which of\r
-                                these types of sections to search for.\r
-  @param  SectionInstance       Indicates which instance of the requested\r
-                                section to return.\r
-  @param  Buffer                Double indirection to buffer.  If *Buffer is\r
-                                non-null on input, then the buffer is caller\r
-                                allocated.  If Buffer is NULL, then the buffer\r
-                                is callee allocated.  In either case, the\r
-                                requried buffer size is returned in *BufferSize.\r
-  @param  BufferSize            On input, indicates the size of *Buffer if\r
-                                *Buffer is non-null on input.  On output,\r
-                                indicates the required size (allocated size if\r
-                                callee allocated) of *Buffer.\r
-  @param  AuthenticationStatus  A pointer to a caller-allocated UINT32 that\r
-                                indicates the authentication status of the\r
-                                output buffer. If the input section's\r
-                                GuidedSectionHeader.Attributes field\r
-                                has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
-                                bit as clear, AuthenticationStatus must return\r
-                                zero. Both local bits (19:16) and aggregate\r
-                                bits (3:0) in AuthenticationStatus are returned\r
-                                by ExtractSection(). These bits reflect the\r
-                                status of the extraction operation. The bit\r
-                                pattern in both regions must be the same, as\r
-                                the local and aggregate authentication statuses\r
-                                have equivalent meaning at this level. If the\r
-                                function returns anything other than\r
-                                EFI_SUCCESS, the value of *AuthenticationStatus\r
-                                is undefined.\r
-\r
-  @retval EFI_SUCCESS           Section was retrieved successfully\r
-  @retval EFI_PROTOCOL_ERROR    A GUID defined section was encountered in the\r
-                                section stream with its\r
-                                EFI_GUIDED_SECTION_PROCESSING_REQUIRED bit set,\r
-                                but there was no corresponding GUIDed Section\r
-                                Extraction Protocol in the handle database.\r
-                                *Buffer is unmodified.\r
-  @retval EFI_NOT_FOUND         An error was encountered when parsing the\r
-                                SectionStream.  This indicates the SectionStream\r
-                                is not correctly formatted.\r
-  @retval EFI_NOT_FOUND         The requested section does not exist.\r
-  @retval EFI_OUT_OF_RESOURCES  The system has insufficient resources to process\r
-                                the request.\r
-  @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.\r
-  @retval EFI_WARN_TOO_SMALL    The size of the caller allocated input buffer is\r
-                                insufficient to contain the requested section.\r
-                                The input buffer is filled and section contents\r
-                                are truncated.\r
-\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-GetSection (\r
-  IN UINTN                                              SectionStreamHandle,\r
-  IN EFI_SECTION_TYPE                                   *SectionType,\r
-  IN EFI_GUID                                           *SectionDefinitionGuid,\r
-  IN UINTN                                              SectionInstance,\r
-  IN VOID                                               **Buffer,\r
-  IN OUT UINTN                                          *BufferSize,\r
-  OUT UINT32                                            *AuthenticationStatus\r
-  )\r
-{\r
-  CORE_SECTION_STREAM_NODE                              *StreamNode;\r
-  EFI_TPL                                               OldTpl;\r
-  EFI_STATUS                                            Status;\r
-  CORE_SECTION_CHILD_NODE                               *ChildNode;\r
-  CORE_SECTION_STREAM_NODE                              *ChildStreamNode;\r
-  UINTN                                                 CopySize;\r
-  UINT32                                                ExtractedAuthenticationStatus;\r
-  UINTN                                                 Instance;\r
-  UINT8                                                 *CopyBuffer;\r
-  UINTN                                                 SectionSize;\r
-  \r
-\r
-  OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
-  Instance = SectionInstance + 1;\r
-  \r
   //\r
-  // Locate target stream\r
+  // Initialize the rest of the section stream\r
   //\r
-  Status = FindStreamNode (SectionStreamHandle, &StreamNode);\r
-  if (EFI_ERROR (Status)) {\r
-    Status = EFI_INVALID_PARAMETER;\r
-    goto GetSection_Done;\r
-  }\r
-  \r
+  NewStream->Signature = CORE_SECTION_STREAM_SIGNATURE;\r
+  NewStream->StreamHandle = (UINTN) NewStream;\r
+  NewStream->StreamLength = SectionStreamLength;\r
+  InitializeListHead (&NewStream->Children);\r
+  NewStream->AuthenticationStatus = AuthenticationStatus;\r
+\r
   //\r
-  // Found the stream, now locate and return the appropriate section\r
+  // Add new stream to stream list\r
   //\r
-  if (SectionType == NULL) {\r
-    //\r
-    // SectionType == NULL means return the WHOLE section stream...\r
-    //\r
-    CopySize = StreamNode->StreamLength;\r
-    CopyBuffer = StreamNode->StreamBuffer;\r
-    *AuthenticationStatus = StreamNode->AuthenticationStatus;\r
-  } else {\r
-    //\r
-    // There's a requested section type, so go find it and return it...\r
-    //\r
-    Status = FindChildNode (\r
-               StreamNode, \r
-               *SectionType, \r
-               &Instance, \r
-               SectionDefinitionGuid,\r
-               &ChildNode,\r
-               &ChildStreamNode, \r
-               &ExtractedAuthenticationStatus\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      goto GetSection_Done;\r
-    }\r
-    CopySize = ChildNode->Size - sizeof (EFI_COMMON_SECTION_HEADER);\r
-    CopyBuffer = ChildStreamNode->StreamBuffer + ChildNode->OffsetInStream + sizeof (EFI_COMMON_SECTION_HEADER);\r
-    *AuthenticationStatus = ExtractedAuthenticationStatus;\r
-  }   \r
-    \r
-  SectionSize = CopySize;  \r
-  if (*Buffer != NULL) {\r
-    //\r
-    // Caller allocated buffer.  Fill to size and return required size...\r
-    //\r
-    if (*BufferSize < CopySize) {\r
-      Status = EFI_WARN_BUFFER_TOO_SMALL;\r
-      CopySize = *BufferSize;\r
-    }\r
-  } else {\r
-    //\r
-    // Callee allocated buffer.  Allocate buffer and return size.\r
-    //\r
-    *Buffer = CoreAllocateBootServicesPool (CopySize);\r
-    if (*Buffer == NULL) {\r
-      Status = EFI_OUT_OF_RESOURCES;\r
-      goto GetSection_Done;\r
-    }\r
-  }\r
-  CopyMem (*Buffer, CopyBuffer, CopySize);\r
-  *BufferSize = SectionSize;\r
-  \r
-GetSection_Done:\r
+  OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
+  InsertTailList (&mStreamRoot, &NewStream->Link);\r
   CoreRestoreTpl (OldTpl);\r
-  \r
-  return Status;\r
-}\r
 \r
+  *SectionStreamHandle = NewStream->StreamHandle;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
 \r
 \r
 /**\r
-  SEP member function.  Deletes an existing section stream\r
+  SEP member function.  This function creates and returns a new section stream\r
+  handle to represent the new section stream.\r
 \r
-  @param  StreamHandleToClose    Indicates the stream to close \r
+  @param  SectionStreamLength    Size in bytes of the section stream.\r
+  @param  SectionStream          Buffer containing the new section stream.\r
+  @param  SectionStreamHandle    A pointer to a caller allocated UINTN that on\r
+                                 output contains the new section stream handle.\r
 \r
-  @retval EFI_SUCCESS            The section stream is closed sucessfully.\r
-  @retval EFI_OUT_OF_RESOURCES   Memory allocation failed. \r
-  @retval EFI_INVALID_PARAMETER  Section stream does not end concident with end \r
+  @retval EFI_SUCCESS            The section stream is created successfully.\r
+  @retval EFI_OUT_OF_RESOURCES   memory allocation failed.\r
+  @retval EFI_INVALID_PARAMETER  Section stream does not end concident with end\r
                                  of last section.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-CloseSectionStream (\r
-  IN  UINTN                                     StreamHandleToClose\r
+OpenSectionStream (\r
+  IN     UINTN                                     SectionStreamLength,\r
+  IN     VOID                                      *SectionStream,\r
+     OUT UINTN                                     *SectionStreamHandle\r
   )\r
 {\r
-  CORE_SECTION_STREAM_NODE                      *StreamNode;\r
-  EFI_TPL                                       OldTpl;\r
-  EFI_STATUS                                    Status;\r
-  LIST_ENTRY                                    *Link;\r
-  CORE_SECTION_CHILD_NODE                       *ChildNode;\r
-  \r
-  OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
-  \r
   //\r
-  // Locate target stream\r
+  // Check to see section stream looks good...\r
   //\r
-  Status = FindStreamNode (StreamHandleToClose, &StreamNode);\r
-  if (!EFI_ERROR (Status)) {\r
-    //\r
-    // Found the stream, so close it\r
-    //\r
-    RemoveEntryList (&StreamNode->Link);\r
-    while (!IsListEmpty (&StreamNode->Children)) {\r
-      Link = GetFirstNode (&StreamNode->Children);\r
-      ChildNode = CHILD_SECTION_NODE_FROM_LINK (Link);\r
-      FreeChildNode (ChildNode);\r
-    }\r
-    CoreFreePool (StreamNode->StreamBuffer);\r
-    CoreFreePool (StreamNode);\r
-    Status = EFI_SUCCESS;\r
-  } else {\r
-    Status = EFI_INVALID_PARAMETER;\r
+  if (!IsValidSectionStream (SectionStream, SectionStreamLength)) {\r
+    return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
-  CoreRestoreTpl (OldTpl);\r
-  return Status;\r
+\r
+  return OpenSectionStreamEx (\r
+           SectionStreamLength,\r
+           SectionStream,\r
+           FALSE,\r
+           0,\r
+           SectionStreamHandle\r
+           );\r
 }\r
 \r
 \r
@@ -678,15 +451,15 @@ CloseSectionStream (
 /**\r
   Worker function.  Determine if the input stream:child matches the input type.\r
 \r
-  @param  Stream                 Indicates the section stream associated with the \r
-                                 child \r
-  @param  Child                  Indicates the child to check \r
-  @param  SearchType             Indicates the type of section to check against \r
-                                 for \r
-  @param  SectionDefinitionGuid  Indicates the GUID to check against if the type \r
-                                 is EFI_SECTION_GUID_DEFINED \r
+  @param  Stream                 Indicates the section stream associated with the\r
+                                 child\r
+  @param  Child                  Indicates the child to check\r
+  @param  SearchType             Indicates the type of section to check against\r
+                                 for\r
+  @param  SectionDefinitionGuid  Indicates the GUID to check against if the type\r
+                                 is EFI_SECTION_GUID_DEFINED\r
 \r
-  @retval TRUE                   The child matches \r
+  @retval TRUE                   The child matches\r
   @retval FALSE                  The child doesn't match\r
 \r
 **/\r
@@ -699,18 +472,481 @@ ChildIsType (
   )\r
 {\r
   EFI_GUID_DEFINED_SECTION    *GuidedSection;\r
-  \r
+\r
   if (SearchType == EFI_SECTION_ALL) {\r
     return TRUE;\r
   }\r
   if (Child->Type != SearchType) {\r
     return FALSE;\r
   }\r
-  if (SearchType != EFI_SECTION_GUID_DEFINED) {\r
+  if ((SearchType != EFI_SECTION_GUID_DEFINED) || (SectionDefinitionGuid == NULL)) {\r
     return TRUE;\r
   }\r
   GuidedSection = (EFI_GUID_DEFINED_SECTION * )(Stream->StreamBuffer + Child->OffsetInStream);\r
-  return CompareGuid (&GuidedSection->SectionDefinitionGuid, SectionDefinitionGuid);\r
+  if (IS_SECTION2 (GuidedSection)) {\r
+    return CompareGuid (&(((EFI_GUID_DEFINED_SECTION2 *) GuidedSection)->SectionDefinitionGuid), SectionDefinitionGuid);\r
+  } else {\r
+    return CompareGuid (&GuidedSection->SectionDefinitionGuid, SectionDefinitionGuid);\r
+  }\r
+}\r
+\r
+/**\r
+  Verify the Guided Section GUID by checking if there is the Guided Section GUID configuration table recorded the GUID itself.\r
+\r
+  @param GuidedSectionGuid          The Guided Section GUID.\r
+  @param GuidedSectionExtraction    A pointer to the pointer to the supported Guided Section Extraction Protocol\r
+                                    for the Guided Section.\r
+\r
+  @return TRUE      The GuidedSectionGuid could be identified, and the pointer to\r
+                    the Guided Section Extraction Protocol will be returned to *GuidedSectionExtraction.\r
+  @return FALSE     The GuidedSectionGuid could not be identified, or\r
+                    the Guided Section Extraction Protocol has not been installed yet.\r
+\r
+**/\r
+BOOLEAN\r
+VerifyGuidedSectionGuid (\r
+  IN  EFI_GUID                                  *GuidedSectionGuid,\r
+  OUT EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL    **GuidedSectionExtraction\r
+  )\r
+{\r
+  EFI_GUID              *GuidRecorded;\r
+  VOID                  *Interface;\r
+  EFI_STATUS            Status;\r
+\r
+  Interface = NULL;\r
+\r
+  //\r
+  // Check if there is the Guided Section GUID configuration table recorded the GUID itself.\r
+  //\r
+  Status = EfiGetSystemConfigurationTable (GuidedSectionGuid, (VOID **) &GuidRecorded);\r
+  if (Status == EFI_SUCCESS) {\r
+    if (CompareGuid (GuidRecorded, GuidedSectionGuid)) {\r
+      //\r
+      // Found the recorded GuidedSectionGuid.\r
+      //\r
+      Status = CoreLocateProtocol (GuidedSectionGuid, NULL, (VOID **) &Interface);\r
+      if (!EFI_ERROR (Status) && Interface != NULL) {\r
+        //\r
+        // Found the supported Guided Section Extraction Porotocol for the Guided Section.\r
+        //\r
+        *GuidedSectionExtraction = (EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *) Interface;\r
+        return TRUE;\r
+      }\r
+      return FALSE;\r
+    }\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
+/**\r
+  RPN callback function. Initializes the section stream\r
+  when GUIDED_SECTION_EXTRACTION_PROTOCOL is installed.\r
+\r
+  @param Event               The event that fired\r
+  @param RpnContext          A pointer to the context that allows us to identify\r
+                             the relevent encapsulation.\r
+**/\r
+VOID\r
+EFIAPI\r
+NotifyGuidedExtraction (\r
+  IN   EFI_EVENT   Event,\r
+  IN   VOID        *RpnContext\r
+  )\r
+{\r
+  EFI_STATUS                              Status;\r
+  EFI_GUID_DEFINED_SECTION                *GuidedHeader;\r
+  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL  *GuidedExtraction;\r
+  VOID                                    *NewStreamBuffer;\r
+  UINTN                                   NewStreamBufferSize;\r
+  UINT32                                  AuthenticationStatus;\r
+  RPN_EVENT_CONTEXT                       *Context;\r
+\r
+  Context = RpnContext;\r
+\r
+  GuidedHeader = (EFI_GUID_DEFINED_SECTION *) (Context->ParentStream->StreamBuffer + Context->ChildNode->OffsetInStream);\r
+  ASSERT (GuidedHeader->CommonHeader.Type == EFI_SECTION_GUID_DEFINED);\r
+\r
+  if (!VerifyGuidedSectionGuid (Context->ChildNode->EncapsulationGuid, &GuidedExtraction)) {\r
+    return;\r
+  }\r
+\r
+  Status = GuidedExtraction->ExtractSection (\r
+                               GuidedExtraction,\r
+                               GuidedHeader,\r
+                               &NewStreamBuffer,\r
+                               &NewStreamBufferSize,\r
+                               &AuthenticationStatus\r
+                               );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Make sure we initialize the new stream with the correct\r
+  // authentication status for both aggregate and local status fields.\r
+  //\r
+  if ((GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0) {\r
+    //\r
+    // OR in the parent stream's aggregate status.\r
+    //\r
+    AuthenticationStatus |= Context->ParentStream->AuthenticationStatus & EFI_AUTH_STATUS_ALL;\r
+  } else {\r
+    //\r
+    // since there's no authentication data contributed by the section,\r
+    // just inherit the full value from our immediate parent.\r
+    //\r
+    AuthenticationStatus = Context->ParentStream->AuthenticationStatus;\r
+  }\r
+\r
+  Status = OpenSectionStreamEx (\r
+             NewStreamBufferSize,\r
+             NewStreamBuffer,\r
+             FALSE,\r
+             AuthenticationStatus,\r
+             &Context->ChildNode->EncapsulatedStreamHandle\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  //  Close the event when done.\r
+  //\r
+  gBS->CloseEvent (Event);\r
+  Context->ChildNode->Event = NULL;\r
+  FreePool (Context);\r
+}\r
+\r
+/**\r
+  Constructor for RPN event when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...\r
+\r
+  @param ParentStream        Indicates the parent of the ecnapsulation section (child)\r
+  @param ChildNode           Indicates the child node that is the encapsulation section.\r
+\r
+**/\r
+VOID\r
+CreateGuidedExtractionRpnEvent (\r
+  IN CORE_SECTION_STREAM_NODE      *ParentStream,\r
+  IN CORE_SECTION_CHILD_NODE       *ChildNode\r
+  )\r
+{\r
+  RPN_EVENT_CONTEXT *Context;\r
+\r
+  //\r
+  // Allocate new event structure and context\r
+  //\r
+  Context = AllocatePool (sizeof (RPN_EVENT_CONTEXT));\r
+  ASSERT (Context != NULL);\r
+\r
+  Context->ChildNode = ChildNode;\r
+  Context->ParentStream = ParentStream;\r
+\r
+  Context->ChildNode->Event = EfiCreateProtocolNotifyEvent (\r
+                                Context->ChildNode->EncapsulationGuid,\r
+                                TPL_NOTIFY,\r
+                                NotifyGuidedExtraction,\r
+                                Context,\r
+                                &Context->Registration\r
+                                );\r
+}\r
+\r
+/**\r
+  Worker function.  Constructor for new child nodes.\r
+\r
+  @param  Stream                 Indicates the section stream in which to add the\r
+                                 child.\r
+  @param  ChildOffset            Indicates the offset in Stream that is the\r
+                                 beginning of the child section.\r
+  @param  ChildNode              Indicates the Callee allocated and initialized\r
+                                 child.\r
+\r
+  @retval EFI_SUCCESS            Child node was found and returned.\r
+                                 EFI_OUT_OF_RESOURCES- Memory allocation failed.\r
+  @retval EFI_PROTOCOL_ERROR     Encapsulation sections produce new stream\r
+                                 handles when the child node is created.  If the\r
+                                 section type is GUID defined, and the extraction\r
+                                 GUID does not exist, and producing the stream\r
+                                 requires the GUID, then a protocol error is\r
+                                 generated and no child is produced. Values\r
+                                 returned by OpenSectionStreamEx.\r
+\r
+**/\r
+EFI_STATUS\r
+CreateChildNode (\r
+  IN     CORE_SECTION_STREAM_NODE              *Stream,\r
+  IN     UINT32                                ChildOffset,\r
+  OUT    CORE_SECTION_CHILD_NODE               **ChildNode\r
+  )\r
+{\r
+  EFI_STATUS                                   Status;\r
+  EFI_COMMON_SECTION_HEADER                    *SectionHeader;\r
+  EFI_COMPRESSION_SECTION                      *CompressionHeader;\r
+  EFI_GUID_DEFINED_SECTION                     *GuidedHeader;\r
+  EFI_DECOMPRESS_PROTOCOL                      *Decompress;\r
+  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL       *GuidedExtraction;\r
+  VOID                                         *NewStreamBuffer;\r
+  VOID                                         *ScratchBuffer;\r
+  UINT32                                       ScratchSize;\r
+  UINTN                                        NewStreamBufferSize;\r
+  UINT32                                       AuthenticationStatus;\r
+  VOID                                         *CompressionSource;\r
+  UINT32                                       CompressionSourceSize;\r
+  UINT32                                       UncompressedLength;\r
+  UINT8                                        CompressionType;\r
+  UINT16                                       GuidedSectionAttributes;\r
+\r
+  CORE_SECTION_CHILD_NODE                      *Node;\r
+\r
+  SectionHeader = (EFI_COMMON_SECTION_HEADER *) (Stream->StreamBuffer + ChildOffset);\r
+\r
+  //\r
+  // Allocate a new node\r
+  //\r
+  *ChildNode = AllocateZeroPool (sizeof (CORE_SECTION_CHILD_NODE));\r
+  Node = *ChildNode;\r
+  if (Node == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // Now initialize it\r
+  //\r
+  Node->Signature = CORE_SECTION_CHILD_SIGNATURE;\r
+  Node->Type = SectionHeader->Type;\r
+  if (IS_SECTION2 (SectionHeader)) {\r
+    Node->Size = SECTION2_SIZE (SectionHeader);\r
+  } else {\r
+    Node->Size = SECTION_SIZE (SectionHeader);\r
+  }\r
+  Node->OffsetInStream = ChildOffset;\r
+  Node->EncapsulatedStreamHandle = NULL_STREAM_HANDLE;\r
+  Node->EncapsulationGuid = NULL;\r
+\r
+  //\r
+  // If it's an encapsulating section, then create the new section stream also\r
+  //\r
+  switch (Node->Type) {\r
+    case EFI_SECTION_COMPRESSION:\r
+      //\r
+      // Get the CompressionSectionHeader\r
+      //\r
+      if (Node->Size < sizeof (EFI_COMPRESSION_SECTION)) {\r
+        CoreFreePool (Node);\r
+        return EFI_NOT_FOUND;\r
+      }\r
+\r
+      CompressionHeader = (EFI_COMPRESSION_SECTION *) SectionHeader;\r
+\r
+      if (IS_SECTION2 (CompressionHeader)) {\r
+        CompressionSource = (VOID *) ((UINT8 *) CompressionHeader + sizeof (EFI_COMPRESSION_SECTION2));\r
+        CompressionSourceSize = (UINT32) (SECTION2_SIZE (CompressionHeader) - sizeof (EFI_COMPRESSION_SECTION2));\r
+        UncompressedLength = ((EFI_COMPRESSION_SECTION2 *) CompressionHeader)->UncompressedLength;\r
+        CompressionType = ((EFI_COMPRESSION_SECTION2 *) CompressionHeader)->CompressionType;\r
+      } else {\r
+        CompressionSource = (VOID *) ((UINT8 *) CompressionHeader + sizeof (EFI_COMPRESSION_SECTION));\r
+        CompressionSourceSize = (UINT32) (SECTION_SIZE (CompressionHeader) - sizeof (EFI_COMPRESSION_SECTION));\r
+        UncompressedLength = CompressionHeader->UncompressedLength;\r
+        CompressionType = CompressionHeader->CompressionType;\r
+      }\r
+\r
+      //\r
+      // Allocate space for the new stream\r
+      //\r
+      if (UncompressedLength > 0) {\r
+        NewStreamBufferSize = UncompressedLength;\r
+        NewStreamBuffer = AllocatePool (NewStreamBufferSize);\r
+        if (NewStreamBuffer == NULL) {\r
+          CoreFreePool (Node);\r
+          return EFI_OUT_OF_RESOURCES;\r
+        }\r
+\r
+        if (CompressionType == EFI_NOT_COMPRESSED) {\r
+          //\r
+          // stream is not actually compressed, just encapsulated.  So just copy it.\r
+          //\r
+          CopyMem (NewStreamBuffer, CompressionSource, NewStreamBufferSize);\r
+        } else if (CompressionType == EFI_STANDARD_COMPRESSION) {\r
+          //\r
+          // Only support the EFI_SATNDARD_COMPRESSION algorithm.\r
+          //\r
+\r
+          //\r
+          // Decompress the stream\r
+          //\r
+          Status = CoreLocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **)&Decompress);\r
+          ASSERT_EFI_ERROR (Status);\r
+          ASSERT (Decompress != NULL);\r
+\r
+          Status = Decompress->GetInfo (\r
+                                 Decompress,\r
+                                 CompressionSource,\r
+                                 CompressionSourceSize,\r
+                                 (UINT32 *)&NewStreamBufferSize,\r
+                                 &ScratchSize\r
+                                 );\r
+          if (EFI_ERROR (Status) || (NewStreamBufferSize != UncompressedLength)) {\r
+            CoreFreePool (Node);\r
+            CoreFreePool (NewStreamBuffer);\r
+            if (!EFI_ERROR (Status)) {\r
+              Status = EFI_BAD_BUFFER_SIZE;\r
+            }\r
+            return Status;\r
+          }\r
+\r
+          ScratchBuffer = AllocatePool (ScratchSize);\r
+          if (ScratchBuffer == NULL) {\r
+            CoreFreePool (Node);\r
+            CoreFreePool (NewStreamBuffer);\r
+            return EFI_OUT_OF_RESOURCES;\r
+          }\r
+\r
+          Status = Decompress->Decompress (\r
+                                 Decompress,\r
+                                 CompressionSource,\r
+                                 CompressionSourceSize,\r
+                                 NewStreamBuffer,\r
+                                 (UINT32)NewStreamBufferSize,\r
+                                 ScratchBuffer,\r
+                                 ScratchSize\r
+                                 );\r
+          CoreFreePool (ScratchBuffer);\r
+          if (EFI_ERROR (Status)) {\r
+            CoreFreePool (Node);\r
+            CoreFreePool (NewStreamBuffer);\r
+            return Status;\r
+          }\r
+        }\r
+      } else {\r
+        NewStreamBuffer = NULL;\r
+        NewStreamBufferSize = 0;\r
+      }\r
+\r
+      Status = OpenSectionStreamEx (\r
+                 NewStreamBufferSize,\r
+                 NewStreamBuffer,\r
+                 FALSE,\r
+                 Stream->AuthenticationStatus,\r
+                 &Node->EncapsulatedStreamHandle\r
+                 );\r
+      if (EFI_ERROR (Status)) {\r
+        CoreFreePool (Node);\r
+        CoreFreePool (NewStreamBuffer);\r
+        return Status;\r
+      }\r
+      break;\r
+\r
+    case EFI_SECTION_GUID_DEFINED:\r
+      GuidedHeader = (EFI_GUID_DEFINED_SECTION *) SectionHeader;\r
+      if (IS_SECTION2 (GuidedHeader)) {\r
+        Node->EncapsulationGuid = &(((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->SectionDefinitionGuid);\r
+        GuidedSectionAttributes = ((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->Attributes;\r
+      } else {\r
+        Node->EncapsulationGuid = &GuidedHeader->SectionDefinitionGuid;\r
+        GuidedSectionAttributes = GuidedHeader->Attributes;\r
+      }\r
+      if (VerifyGuidedSectionGuid (Node->EncapsulationGuid, &GuidedExtraction)) {\r
+        //\r
+        // NewStreamBuffer is always allocated by ExtractSection... No caller\r
+        // allocation here.\r
+        //\r
+        Status = GuidedExtraction->ExtractSection (\r
+                                     GuidedExtraction,\r
+                                     GuidedHeader,\r
+                                     &NewStreamBuffer,\r
+                                     &NewStreamBufferSize,\r
+                                     &AuthenticationStatus\r
+                                     );\r
+        if (EFI_ERROR (Status)) {\r
+          CoreFreePool (*ChildNode);\r
+          return EFI_PROTOCOL_ERROR;\r
+        }\r
+\r
+        //\r
+        // Make sure we initialize the new stream with the correct\r
+        // authentication status for both aggregate and local status fields.\r
+        //\r
+        if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) != 0) {\r
+          //\r
+          // OR in the parent stream's aggregate status.\r
+          //\r
+          AuthenticationStatus |= Stream->AuthenticationStatus & EFI_AUTH_STATUS_ALL;\r
+        } else {\r
+          //\r
+          // since there's no authentication data contributed by the section,\r
+          // just inherit the full value from our immediate parent.\r
+          //\r
+          AuthenticationStatus = Stream->AuthenticationStatus;\r
+        }\r
+\r
+        Status = OpenSectionStreamEx (\r
+                   NewStreamBufferSize,\r
+                   NewStreamBuffer,\r
+                   FALSE,\r
+                   AuthenticationStatus,\r
+                   &Node->EncapsulatedStreamHandle\r
+                   );\r
+        if (EFI_ERROR (Status)) {\r
+          CoreFreePool (*ChildNode);\r
+          CoreFreePool (NewStreamBuffer);\r
+          return Status;\r
+        }\r
+      } else {\r
+        //\r
+        // There's no GUIDed section extraction protocol available.\r
+        //\r
+        if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) {\r
+          //\r
+          // If the section REQUIRES an extraction protocol, register for RPN\r
+          // when the required GUIDed extraction protocol becomes available.\r
+          //\r
+          CreateGuidedExtractionRpnEvent (Stream, Node);\r
+        } else {\r
+          //\r
+          // Figure out the proper authentication status\r
+          //\r
+          AuthenticationStatus = Stream->AuthenticationStatus;\r
+\r
+          if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {\r
+            AuthenticationStatus |= EFI_AUTH_STATUS_IMAGE_SIGNED | EFI_AUTH_STATUS_NOT_TESTED;\r
+          }\r
+\r
+          if (IS_SECTION2 (GuidedHeader)) {\r
+            Status = OpenSectionStreamEx (\r
+                       SECTION2_SIZE (GuidedHeader) - ((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->DataOffset,\r
+                       (UINT8 *) GuidedHeader + ((EFI_GUID_DEFINED_SECTION2 *) GuidedHeader)->DataOffset,\r
+                       TRUE,\r
+                       AuthenticationStatus,\r
+                       &Node->EncapsulatedStreamHandle\r
+                       );\r
+          } else {\r
+            Status = OpenSectionStreamEx (\r
+                       SECTION_SIZE (GuidedHeader) - ((EFI_GUID_DEFINED_SECTION *) GuidedHeader)->DataOffset,\r
+                       (UINT8 *) GuidedHeader + ((EFI_GUID_DEFINED_SECTION *) GuidedHeader)->DataOffset,\r
+                       TRUE,\r
+                       AuthenticationStatus,\r
+                       &Node->EncapsulatedStreamHandle\r
+                       );\r
+          }\r
+          if (EFI_ERROR (Status)) {\r
+            CoreFreePool (Node);\r
+            return Status;\r
+          }\r
+        }\r
+      }\r
+\r
+      break;\r
+\r
+    default:\r
+\r
+      //\r
+      // Nothing to do if it's a leaf\r
+      //\r
+      break;\r
+  }\r
+\r
+  //\r
+  // Last, add the new child node to the stream\r
+  //\r
+  InsertTailList (&Stream->Children, &Node->Link);\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 \r
@@ -718,25 +954,25 @@ ChildIsType (
   Worker function  Recursively searches / builds section stream database\r
   looking for requested section.\r
 \r
-  @param  SourceStream           Indicates the section stream in which to do the \r
-                                 search. \r
-  @param  SearchType             Indicates the type of section to search for. \r
-  @param  SectionInstance        Indicates which instance of section to find.  \r
-                                 This is an in/out parameter to deal with \r
-                                 recursions. \r
-  @param  SectionDefinitionGuid  Guid of section definition \r
-  @param  FoundChild             Output indicating the child node that is found. \r
-  @param  FoundStream            Output indicating which section stream the child \r
-                                 was found in.  If this stream was generated as a \r
-                                 result of an encapsulation section, the \r
-                                 streamhandle is visible within the SEP driver \r
+  @param  SourceStream           Indicates the section stream in which to do the\r
+                                 search.\r
+  @param  SearchType             Indicates the type of section to search for.\r
+  @param  SectionInstance        Indicates which instance of section to find.\r
+                                 This is an in/out parameter to deal with\r
+                                 recursions.\r
+  @param  SectionDefinitionGuid  Guid of section definition\r
+  @param  FoundChild             Output indicating the child node that is found.\r
+  @param  FoundStream            Output indicating which section stream the child\r
+                                 was found in.  If this stream was generated as a\r
+                                 result of an encapsulation section, the\r
+                                 streamhandle is visible within the SEP driver\r
                                  only.\r
-  @param  AuthenticationStatus   Indicates the authentication status of the found section. \r
+  @param  AuthenticationStatus   Indicates the authentication status of the found section.\r
 \r
-  @retval EFI_SUCCESS            Child node was found and returned. \r
-                                 EFI_OUT_OF_RESOURCES- Memory allocation failed. \r
-  @retval EFI_NOT_FOUND          Requested child node does not exist. \r
-  @retval EFI_PROTOCOL_ERROR     a required GUIDED section extraction protocol \r
+  @retval EFI_SUCCESS            Child node was found and returned.\r
+                                 EFI_OUT_OF_RESOURCES- Memory allocation failed.\r
+  @retval EFI_NOT_FOUND          Requested child node does not exist.\r
+  @retval EFI_PROTOCOL_ERROR     a required GUIDED section extraction protocol\r
                                  does not exist\r
 \r
 **/\r
@@ -757,21 +993,21 @@ FindChildNode (
   UINT32                                        NextChildOffset;\r
   EFI_STATUS                                    ErrorStatus;\r
   EFI_STATUS                                    Status;\r
-  \r
+\r
   CurrentChildNode = NULL;\r
   ErrorStatus = EFI_NOT_FOUND;\r
-  \r
+\r
   if (SourceStream->StreamLength == 0) {\r
     return EFI_NOT_FOUND;\r
   }\r
-  \r
-  if (IsListEmpty (&SourceStream->Children) && \r
+\r
+  if (IsListEmpty (&SourceStream->Children) &&\r
       SourceStream->StreamLength >= sizeof (EFI_COMMON_SECTION_HEADER)) {\r
     //\r
     // This occurs when a section stream exists, but no child sections\r
     // have been parsed out yet.  Therefore, extract the first child and add it\r
     // to the list of children so we can get started.\r
-    // Section stream may contain an array of zero or more bytes. \r
+    // Section stream may contain an array of zero or more bytes.\r
     // So, its size should be >= the size of commen section header.\r
     //\r
     Status = CreateChildNode (SourceStream, 0, &CurrentChildNode);\r
@@ -779,7 +1015,7 @@ FindChildNode (
       return Status;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // At least one child has been parsed out of the section stream.  So, walk\r
   // through the sections that have already been parsed out looking for the\r
@@ -790,6 +1026,7 @@ FindChildNode (
   CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream->Children));\r
 \r
   for (;;) {\r
+    ASSERT (CurrentChildNode != NULL);\r
     if (ChildIsType (SourceStream, CurrentChildNode, SearchType, SectionDefinitionGuid)) {\r
       //\r
       // The type matches, so check the instance count to see if it's the one we want\r
@@ -805,7 +1042,7 @@ FindChildNode (
         return EFI_SUCCESS;\r
       }\r
     }\r
-    \r
+\r
     if (CurrentChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {\r
       //\r
       // If the current node is an encapsulating node, recurse into it...\r
@@ -831,8 +1068,15 @@ FindChildNode (
       } else {\r
         ErrorStatus = Status;\r
       }\r
+    } else if ((CurrentChildNode->Type == EFI_SECTION_GUID_DEFINED) && (SearchType != EFI_SECTION_GUID_DEFINED)) {\r
+      //\r
+      // When Node Type is GUIDED section, but Node has no encapsulated data, Node data should not be parsed\r
+      // because a required GUIDED section extraction protocol does not exist.\r
+      // If SearchType is not GUIDED section, EFI_PROTOCOL_ERROR should return.\r
+      //\r
+      ErrorStatus = EFI_PROTOCOL_ERROR;\r
     }\r
-    \r
+\r
     if (!IsNodeAtEnd (&SourceStream->Children, &CurrentChildNode->Link)) {\r
       //\r
       // We haven't found the child node we're interested in yet, but there's\r
@@ -870,254 +1114,211 @@ FindChildNode (
 \r
 \r
 /**\r
-  Worker function.  Constructor for new child nodes.\r
+  Worker function.  Search stream database for requested stream handle.\r
 \r
-  @param  Stream                 Indicates the section stream in which to add the \r
-                                 child. \r
-  @param  ChildOffset            Indicates the offset in Stream that is the \r
-                                 beginning of the child section. \r
-  @param  ChildNode              Indicates the Callee allocated and initialized \r
-                                 child. \r
-\r
-  @retval EFI_SUCCESS            Child node was found and returned. \r
-                                 EFI_OUT_OF_RESOURCES- Memory allocation failed. \r
-  @retval EFI_PROTOCOL_ERROR     Encapsulation sections produce new stream \r
-                                 handles when the child node is created.  If the \r
-                                 section type is GUID defined, and the extraction \r
-                                 GUID does not exist, and producing the stream \r
-                                 requires the GUID, then a protocol error is \r
-                                 generated and no child is produced. Values \r
-                                 returned by OpenSectionStreamEx.\r
+  @param  SearchHandle           Indicates which stream to look for.\r
+  @param  FoundStream            Output pointer to the found stream.\r
+\r
+  @retval EFI_SUCCESS            StreamHandle was found and *FoundStream contains\r
+                                 the stream node.\r
+  @retval EFI_NOT_FOUND          SearchHandle was not found in the stream\r
+                                 database.\r
 \r
 **/\r
 EFI_STATUS\r
-CreateChildNode (\r
-  IN     CORE_SECTION_STREAM_NODE              *Stream,\r
-  IN     UINT32                                ChildOffset,\r
-     OUT CORE_SECTION_CHILD_NODE               **ChildNode\r
+FindStreamNode (\r
+  IN  UINTN                                     SearchHandle,\r
+  OUT CORE_SECTION_STREAM_NODE                  **FoundStream\r
   )\r
 {\r
-  EFI_STATUS                                   Status;\r
-  EFI_COMMON_SECTION_HEADER                    *SectionHeader;\r
-  EFI_COMPRESSION_SECTION                      *CompressionHeader;\r
-  EFI_GUID_DEFINED_SECTION                     *GuidedHeader;\r
-  EFI_DECOMPRESS_PROTOCOL                      *Decompress;\r
-  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL       *GuidedExtraction;\r
-  VOID                                         *NewStreamBuffer;\r
-  VOID                                         *ScratchBuffer;\r
-  UINT32                                       ScratchSize;\r
-  UINTN                                        NewStreamBufferSize;\r
-  UINT32                                       AuthenticationStatus;\r
-  UINT32                                       SectionLength;\r
-    \r
-  CORE_SECTION_CHILD_NODE                      *Node;\r
-\r
-  SectionHeader = (EFI_COMMON_SECTION_HEADER *) (Stream->StreamBuffer + ChildOffset);\r
+  CORE_SECTION_STREAM_NODE                      *StreamNode;\r
 \r
-  //\r
-  // Allocate a new node\r
-  //\r
-  *ChildNode = CoreAllocateBootServicesPool (sizeof (CORE_SECTION_CHILD_NODE));\r
-  Node = *ChildNode;\r
-  if (Node == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
+  if (!IsListEmpty (&mStreamRoot)) {\r
+    StreamNode = STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot));\r
+    for (;;) {\r
+      if (StreamNode->StreamHandle == SearchHandle) {\r
+        *FoundStream = StreamNode;\r
+        return EFI_SUCCESS;\r
+      } else if (IsNodeAtEnd (&mStreamRoot, &StreamNode->Link)) {\r
+        break;\r
+      } else {\r
+        StreamNode = STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot, &StreamNode->Link));\r
+      }\r
+    }\r
   }\r
-  \r
-  //\r
-  // Now initialize it\r
-  //\r
-  Node->Signature = CORE_SECTION_CHILD_SIGNATURE;\r
-  Node->Type = SectionHeader->Type;\r
-  Node->Size = SECTION_SIZE (SectionHeader);\r
-  Node->OffsetInStream = ChildOffset;\r
-  Node->EncapsulatedStreamHandle = NULL_STREAM_HANDLE;\r
-  Node->EncapsulationGuid = NULL;\r
-  \r
-  //\r
-  // If it's an encapsulating section, then create the new section stream also\r
-  //\r
-  switch (Node->Type) {\r
-    case EFI_SECTION_COMPRESSION:\r
-      //\r
-      // Get the CompressionSectionHeader\r
-      //\r
-      ASSERT (Node->Size >= sizeof (EFI_COMPRESSION_SECTION));\r
-      \r
-      CompressionHeader = (EFI_COMPRESSION_SECTION *) SectionHeader;\r
-      \r
-      //\r
-      // Allocate space for the new stream\r
-      //\r
-      if (CompressionHeader->UncompressedLength > 0) {\r
-        NewStreamBufferSize = CompressionHeader->UncompressedLength;\r
-        NewStreamBuffer = CoreAllocateBootServicesPool (NewStreamBufferSize);\r
-        if (NewStreamBuffer == NULL) {\r
-          CoreFreePool (Node);\r
-          return EFI_OUT_OF_RESOURCES;\r
-        }\r
-        \r
-        if (CompressionHeader->CompressionType == EFI_NOT_COMPRESSED) {\r
-          //\r
-          // stream is not actually compressed, just encapsulated.  So just copy it.\r
-          //\r
-          CopyMem (NewStreamBuffer, CompressionHeader + 1, NewStreamBufferSize);\r
-        } else if (CompressionHeader->CompressionType == EFI_STANDARD_COMPRESSION) {\r
-          //\r
-          // Only support the EFI_SATNDARD_COMPRESSION algorithm.\r
-          // \r
 \r
-          //\r
-          // Decompress the stream\r
-          //\r
-          Status = CoreLocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **)&Decompress);\r
-          \r
-          ASSERT_EFI_ERROR (Status);\r
-          \r
-          Status = Decompress->GetInfo (\r
-                                 Decompress,\r
-                                 CompressionHeader + 1,\r
-                                 Node->Size - sizeof (EFI_COMPRESSION_SECTION),\r
-                                 (UINT32 *)&NewStreamBufferSize,\r
-                                 &ScratchSize\r
-                                 );\r
-          ASSERT_EFI_ERROR (Status);\r
-          ASSERT (NewStreamBufferSize == CompressionHeader->UncompressedLength);\r
+  return EFI_NOT_FOUND;\r
+}\r
 \r
-          ScratchBuffer = CoreAllocateBootServicesPool (ScratchSize);\r
-          if (ScratchBuffer == NULL) {\r
-            CoreFreePool (Node);\r
-            CoreFreePool (NewStreamBuffer);\r
-            return EFI_OUT_OF_RESOURCES;\r
-          }\r
 \r
-          Status = Decompress->Decompress (\r
-                                 Decompress,\r
-                                 CompressionHeader + 1,\r
-                                 Node->Size - sizeof (EFI_COMPRESSION_SECTION),\r
-                                 NewStreamBuffer,\r
-                                 (UINT32)NewStreamBufferSize,\r
-                                 ScratchBuffer,\r
-                                 ScratchSize\r
-                                 );\r
-          ASSERT_EFI_ERROR (Status);\r
-          CoreFreePool (ScratchBuffer);                                           \r
-        }\r
-      } else {\r
-        NewStreamBuffer = NULL;\r
-        NewStreamBufferSize = 0;\r
-      }\r
-      \r
-      Status = OpenSectionStreamEx (\r
-                 NewStreamBufferSize,\r
-                 NewStreamBuffer,\r
-                 FALSE,\r
-                 Stream->AuthenticationStatus,\r
-                 &Node->EncapsulatedStreamHandle\r
-                 );\r
-      if (EFI_ERROR (Status)) {\r
-        CoreFreePool (Node);\r
-        CoreFreePool (NewStreamBuffer);\r
-        return Status;\r
-      }\r
-      break;\r
+/**\r
+  SEP member function.  Retrieves requested section from section stream.\r
 \r
-    case EFI_SECTION_GUID_DEFINED:\r
-      GuidedHeader = (EFI_GUID_DEFINED_SECTION *) SectionHeader;\r
-      Node->EncapsulationGuid = &GuidedHeader->SectionDefinitionGuid;\r
-      Status = CoreLocateProtocol (Node->EncapsulationGuid, NULL, (VOID **)&GuidedExtraction);\r
-      if (!EFI_ERROR (Status)) {\r
-        //\r
-        // NewStreamBuffer is always allocated by ExtractSection... No caller\r
-        // allocation here.\r
-        //\r
-        Status = GuidedExtraction->ExtractSection (\r
-                                     GuidedExtraction,\r
-                                     GuidedHeader,\r
-                                     &NewStreamBuffer,\r
-                                     &NewStreamBufferSize,\r
-                                     &AuthenticationStatus\r
-                                     );\r
-        if (EFI_ERROR (Status)) {\r
-          CoreFreePool (*ChildNode);\r
-          return EFI_PROTOCOL_ERROR;\r
-        }\r
-        \r
-        //\r
-        // Make sure we initialize the new stream with the correct \r
-        // authentication status for both aggregate and local status fields.\r
-        //\r
-        if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {\r
-          //\r
-          // OR in the parent stream's aggregate status.\r
-          //\r
-          AuthenticationStatus |= Stream->AuthenticationStatus & EFI_AUTH_STATUS_ALL;\r
-        } else {\r
-          //\r
-          // since there's no authentication data contributed by the section,\r
-          // just inherit the full value from our immediate parent.\r
-          //\r
-          AuthenticationStatus = Stream->AuthenticationStatus;\r
-        }\r
-        \r
-        Status = OpenSectionStreamEx (\r
-                   NewStreamBufferSize,\r
-                   NewStreamBuffer,\r
-                   FALSE,\r
-                   AuthenticationStatus,\r
-                   &Node->EncapsulatedStreamHandle\r
-                   );\r
-        if (EFI_ERROR (Status)) {\r
-          CoreFreePool (*ChildNode);\r
-          CoreFreePool (NewStreamBuffer);\r
-          return Status;\r
-        }\r
-      } else {\r
-        //\r
-        // There's no GUIDed section extraction protocol available.\r
-        //\r
-        if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) {\r
-          //\r
-          // If the section REQUIRES an extraction protocol, then we're toast\r
-          //\r
-          CoreFreePool (*ChildNode);\r
-          return EFI_PROTOCOL_ERROR;\r
-        }\r
-        \r
-        //\r
-        // Figure out the proper authentication status\r
-        //\r
-        AuthenticationStatus = Stream->AuthenticationStatus;\r
-        \r
-        SectionLength = SECTION_SIZE (GuidedHeader);\r
-        Status = OpenSectionStreamEx (\r
-                   SectionLength - GuidedHeader->DataOffset,\r
-                   (UINT8 *) GuidedHeader + GuidedHeader->DataOffset,\r
-                   TRUE,\r
-                   AuthenticationStatus,\r
-                   &Node->EncapsulatedStreamHandle\r
-                   );\r
-        if (EFI_ERROR (Status)) {\r
-          CoreFreePool (Node);\r
-          return Status;\r
-        }\r
-      }\r
-      \r
-      break;\r
+  @param  SectionStreamHandle   The section stream from which to extract the\r
+                                requested section.\r
+  @param  SectionType           A pointer to the type of section to search for.\r
+  @param  SectionDefinitionGuid If the section type is EFI_SECTION_GUID_DEFINED,\r
+                                then SectionDefinitionGuid indicates which of\r
+                                these types of sections to search for.\r
+  @param  SectionInstance       Indicates which instance of the requested\r
+                                section to return.\r
+  @param  Buffer                Double indirection to buffer.  If *Buffer is\r
+                                non-null on input, then the buffer is caller\r
+                                allocated.  If Buffer is NULL, then the buffer\r
+                                is callee allocated.  In either case, the\r
+                                required buffer size is returned in *BufferSize.\r
+  @param  BufferSize            On input, indicates the size of *Buffer if\r
+                                *Buffer is non-null on input.  On output,\r
+                                indicates the required size (allocated size if\r
+                                callee allocated) of *Buffer.\r
+  @param  AuthenticationStatus  A pointer to a caller-allocated UINT32 that\r
+                                indicates the authentication status of the\r
+                                output buffer. If the input section's\r
+                                GuidedSectionHeader.Attributes field\r
+                                has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
+                                bit as clear, AuthenticationStatus must return\r
+                                zero. Both local bits (19:16) and aggregate\r
+                                bits (3:0) in AuthenticationStatus are returned\r
+                                by ExtractSection(). These bits reflect the\r
+                                status of the extraction operation. The bit\r
+                                pattern in both regions must be the same, as\r
+                                the local and aggregate authentication statuses\r
+                                have equivalent meaning at this level. If the\r
+                                function returns anything other than\r
+                                EFI_SUCCESS, the value of *AuthenticationStatus\r
+                                is undefined.\r
+  @param  IsFfs3Fv              Indicates the FV format.\r
+\r
+  @retval EFI_SUCCESS           Section was retrieved successfully\r
+  @retval EFI_PROTOCOL_ERROR    A GUID defined section was encountered in the\r
+                                section stream with its\r
+                                EFI_GUIDED_SECTION_PROCESSING_REQUIRED bit set,\r
+                                but there was no corresponding GUIDed Section\r
+                                Extraction Protocol in the handle database.\r
+                                *Buffer is unmodified.\r
+  @retval EFI_NOT_FOUND         An error was encountered when parsing the\r
+                                SectionStream.  This indicates the SectionStream\r
+                                is not correctly formatted.\r
+  @retval EFI_NOT_FOUND         The requested section does not exist.\r
+  @retval EFI_OUT_OF_RESOURCES  The system has insufficient resources to process\r
+                                the request.\r
+  @retval EFI_INVALID_PARAMETER The SectionStreamHandle does not exist.\r
+  @retval EFI_WARN_TOO_SMALL    The size of the caller allocated input buffer is\r
+                                insufficient to contain the requested section.\r
+                                The input buffer is filled and section contents\r
+                                are truncated.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetSection (\r
+  IN UINTN                                              SectionStreamHandle,\r
+  IN EFI_SECTION_TYPE                                   *SectionType,\r
+  IN EFI_GUID                                           *SectionDefinitionGuid,\r
+  IN UINTN                                              SectionInstance,\r
+  IN VOID                                               **Buffer,\r
+  IN OUT UINTN                                          *BufferSize,\r
+  OUT UINT32                                            *AuthenticationStatus,\r
+  IN BOOLEAN                                            IsFfs3Fv\r
+  )\r
+{\r
+  CORE_SECTION_STREAM_NODE                              *StreamNode;\r
+  EFI_TPL                                               OldTpl;\r
+  EFI_STATUS                                            Status;\r
+  CORE_SECTION_CHILD_NODE                               *ChildNode;\r
+  CORE_SECTION_STREAM_NODE                              *ChildStreamNode;\r
+  UINTN                                                 CopySize;\r
+  UINT32                                                ExtractedAuthenticationStatus;\r
+  UINTN                                                 Instance;\r
+  UINT8                                                 *CopyBuffer;\r
+  UINTN                                                 SectionSize;\r
+  EFI_COMMON_SECTION_HEADER                             *Section;\r
 \r
-    default:\r
-      \r
-      //\r
-      // Nothing to do if it's a leaf\r
-      //\r
-      break;\r
+\r
+  ChildStreamNode = NULL;\r
+  OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
+  Instance = SectionInstance + 1;\r
+\r
+  //\r
+  // Locate target stream\r
+  //\r
+  Status = FindStreamNode (SectionStreamHandle, &StreamNode);\r
+  if (EFI_ERROR (Status)) {\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto GetSection_Done;\r
   }\r
-  \r
+\r
   //\r
-  // Last, add the new child node to the stream\r
+  // Found the stream, now locate and return the appropriate section\r
   //\r
-  InsertTailList (&Stream->Children, &Node->Link);\r
+  if (SectionType == NULL) {\r
+    //\r
+    // SectionType == NULL means return the WHOLE section stream...\r
+    //\r
+    CopySize = StreamNode->StreamLength;\r
+    CopyBuffer = StreamNode->StreamBuffer;\r
+    *AuthenticationStatus = StreamNode->AuthenticationStatus;\r
+  } else {\r
+    //\r
+    // There's a requested section type, so go find it and return it...\r
+    //\r
+    Status = FindChildNode (\r
+               StreamNode,\r
+               *SectionType,\r
+               &Instance,\r
+               SectionDefinitionGuid,\r
+               &ChildNode,\r
+               &ChildStreamNode,\r
+               &ExtractedAuthenticationStatus\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      goto GetSection_Done;\r
+    }\r
 \r
-  return EFI_SUCCESS;\r
+    Section = (EFI_COMMON_SECTION_HEADER *) (ChildStreamNode->StreamBuffer + ChildNode->OffsetInStream);\r
+\r
+    if (IS_SECTION2 (Section)) {\r
+      ASSERT (SECTION2_SIZE (Section) > 0x00FFFFFF);\r
+      if (!IsFfs3Fv) {\r
+        DEBUG ((DEBUG_ERROR, "It is a FFS3 formatted section in a non-FFS3 formatted FV.\n"));\r
+        Status = EFI_NOT_FOUND;\r
+        goto GetSection_Done;\r
+      }\r
+      CopySize = SECTION2_SIZE (Section) - sizeof (EFI_COMMON_SECTION_HEADER2);\r
+      CopyBuffer = (UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER2);\r
+    } else {\r
+      CopySize = SECTION_SIZE (Section) - sizeof (EFI_COMMON_SECTION_HEADER);\r
+      CopyBuffer = (UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER);\r
+    }\r
+    *AuthenticationStatus = ExtractedAuthenticationStatus;\r
+  }\r
+\r
+  SectionSize = CopySize;\r
+  if (*Buffer != NULL) {\r
+    //\r
+    // Caller allocated buffer.  Fill to size and return required size...\r
+    //\r
+    if (*BufferSize < CopySize) {\r
+      Status = EFI_WARN_BUFFER_TOO_SMALL;\r
+      CopySize = *BufferSize;\r
+    }\r
+  } else {\r
+    //\r
+    // Callee allocated buffer.  Allocate buffer and return size.\r
+    //\r
+    *Buffer = AllocatePool (CopySize);\r
+    if (*Buffer == NULL) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto GetSection_Done;\r
+    }\r
+  }\r
+  CopyMem (*Buffer, CopyBuffer, CopySize);\r
+  *BufferSize = SectionSize;\r
+\r
+GetSection_Done:\r
+  CoreRestoreTpl (OldTpl);\r
+\r
+  return Status;\r
 }\r
 \r
 \r
@@ -1137,213 +1338,79 @@ FreeChildNode (
   // Remove the child from it's list\r
   //\r
   RemoveEntryList (&ChildNode->Link);\r
-  \r
+\r
   if (ChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {\r
     //\r
     // If it's an encapsulating section, we close the resulting section stream.\r
     // CloseSectionStream will free all memory associated with the stream.\r
     //\r
-    CloseSectionStream (ChildNode->EncapsulatedStreamHandle);\r
+    CloseSectionStream (ChildNode->EncapsulatedStreamHandle, TRUE);\r
   }\r
+\r
+  if (ChildNode->Event != NULL) {\r
+    gBS->CloseEvent (ChildNode->Event);\r
+  }\r
+\r
   //\r
   // Last, free the child node itself\r
   //\r
   CoreFreePool (ChildNode);\r
-}  \r
-\r
+}\r
 \r
 \r
 /**\r
-  Worker function.  Constructor for section streams.\r
+  SEP member function.  Deletes an existing section stream\r
 \r
-  @param  SectionStreamLength    Size in bytes of the section stream. \r
-  @param  SectionStream          Buffer containing the new section stream. \r
-  @param  AllocateBuffer         Indicates whether the stream buffer is to be \r
-                                 copied or the input buffer is to be used in \r
-                                 place. AuthenticationStatus- Indicates the \r
-                                 default authentication status for the new \r
-                                 stream. \r
-  @param  AuthenticationStatus   A pointer to a caller-allocated UINT32 that\r
-                                 indicates the authentication status of the\r
-                                 output buffer. If the input section's\r
-                                 GuidedSectionHeader.Attributes field\r
-                                 has the EFI_GUIDED_SECTION_AUTH_STATUS_VALID\r
-                                 bit as clear, AuthenticationStatus must return\r
-                                 zero. Both local bits (19:16) and aggregate\r
-                                 bits (3:0) in AuthenticationStatus are returned\r
-                                 by ExtractSection(). These bits reflect the\r
-                                 status of the extraction operation. The bit\r
-                                 pattern in both regions must be the same, as\r
-                                 the local and aggregate authentication statuses\r
-                                 have equivalent meaning at this level. If the\r
-                                 function returns anything other than\r
-                                 EFI_SUCCESS, the value of *AuthenticationStatus\r
-                                 is undefined.\r
-  @param  SectionStreamHandle    A pointer to a caller allocated section stream \r
-                                 handle. \r
+  @param  StreamHandleToClose    Indicates the stream to close\r
+  @param  FreeStreamBuffer       TRUE - Need to free stream buffer;\r
+                                 FALSE - No need to free stream buffer.\r
 \r
-  @retval EFI_SUCCESS            Stream was added to stream database. \r
-  @retval EFI_OUT_OF_RESOURCES   memory allocation failed.\r
+  @retval EFI_SUCCESS            The section stream is closed sucessfully.\r
+  @retval EFI_OUT_OF_RESOURCES   Memory allocation failed.\r
+  @retval EFI_INVALID_PARAMETER  Section stream does not end concident with end\r
+                                 of last section.\r
 \r
 **/\r
 EFI_STATUS\r
-OpenSectionStreamEx (\r
-  IN     UINTN                                     SectionStreamLength,\r
-  IN     VOID                                      *SectionStream,\r
-  IN     BOOLEAN                                   AllocateBuffer,\r
-  IN     UINT32                                    AuthenticationStatus,   \r
-     OUT UINTN                                     *SectionStreamHandle\r
+EFIAPI\r
+CloseSectionStream (\r
+  IN  UINTN                                     StreamHandleToClose,\r
+  IN  BOOLEAN                                   FreeStreamBuffer\r
   )\r
 {\r
-  CORE_SECTION_STREAM_NODE    *NewStream;\r
-  EFI_TPL                     OldTpl;\r
-  \r
+  CORE_SECTION_STREAM_NODE                      *StreamNode;\r
+  EFI_TPL                                       OldTpl;\r
+  EFI_STATUS                                    Status;\r
+  LIST_ENTRY                                    *Link;\r
+  CORE_SECTION_CHILD_NODE                       *ChildNode;\r
+\r
+  OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
+\r
   //\r
-  // Allocate a new stream\r
+  // Locate target stream\r
   //\r
-  NewStream = CoreAllocateBootServicesPool (sizeof (CORE_SECTION_STREAM_NODE));\r
-  if (NewStream == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  \r
-  if (AllocateBuffer) { \r
-    //\r
-    // if we're here, we're double buffering, allocate the buffer and copy the\r
-    // data in\r
-    //\r
-    if (SectionStreamLength > 0) {\r
-      NewStream->StreamBuffer = CoreAllocateBootServicesPool (SectionStreamLength); \r
-      if (NewStream->StreamBuffer == NULL) {\r
-        CoreFreePool (NewStream);\r
-        return EFI_OUT_OF_RESOURCES;\r
-      }\r
-      //\r
-      // Copy in stream data\r
-      //\r
-      CopyMem (NewStream->StreamBuffer, SectionStream, SectionStreamLength);\r
-    } else {\r
-      //\r
-      // It's possible to have a zero length section stream.\r
-      //\r
-      NewStream->StreamBuffer = NULL;\r
-    }\r
-  } else {\r
+  Status = FindStreamNode (StreamHandleToClose, &StreamNode);\r
+  if (!EFI_ERROR (Status)) {\r
     //\r
-    // If were here, the caller has supplied the buffer (it's an internal call)\r
-    // so just assign the buffer.  This happens when we open section streams\r
-    // as a result of expanding an encapsulating section.\r
+    // Found the stream, so close it\r
     //\r
-    NewStream->StreamBuffer = SectionStream;\r
-  }\r
-  \r
-  //\r
-  // Initialize the rest of the section stream\r
-  //\r
-  NewStream->Signature = CORE_SECTION_STREAM_SIGNATURE;\r
-  NewStream->StreamHandle = (UINTN) NewStream;\r
-  NewStream->StreamLength = SectionStreamLength;\r
-  InitializeListHead (&NewStream->Children);\r
-  NewStream->AuthenticationStatus = AuthenticationStatus;\r
-  \r
-  //\r
-  // Add new stream to stream list\r
-  //\r
-  OldTpl = CoreRaiseTpl (TPL_NOTIFY);\r
-  InsertTailList (&mStreamRoot, &NewStream->Link);\r
-  CoreRestoreTpl (OldTpl);\r
-\r
-  *SectionStreamHandle = NewStream->StreamHandle;\r
-  \r
-  return EFI_SUCCESS;\r
-}\r
-\r
-\r
-\r
-/**\r
-  Worker function.  Search stream database for requested stream handle.\r
-\r
-  @param  SearchHandle           Indicates which stream to look for. \r
-  @param  FoundStream            Output pointer to the found stream. \r
-\r
-  @retval EFI_SUCCESS            StreamHandle was found and *FoundStream contains \r
-                                 the stream node. \r
-  @retval EFI_NOT_FOUND          SearchHandle was not found in the stream \r
-                                 database.\r
-\r
-**/\r
-EFI_STATUS\r
-FindStreamNode (\r
-  IN  UINTN                                     SearchHandle,\r
-  OUT CORE_SECTION_STREAM_NODE                  **FoundStream\r
-  )\r
-{  \r
-  CORE_SECTION_STREAM_NODE                      *StreamNode;\r
-  \r
-  if (!IsListEmpty (&mStreamRoot)) {\r
-    StreamNode = STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot));\r
-    for (;;) {\r
-      if (StreamNode->StreamHandle == SearchHandle) {\r
-        *FoundStream = StreamNode;\r
-        return EFI_SUCCESS;\r
-      } else if (IsNodeAtEnd (&mStreamRoot, &StreamNode->Link)) {\r
-        break;\r
-      } else {\r
-        StreamNode = STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot, &StreamNode->Link));\r
-      }\r
+    RemoveEntryList (&StreamNode->Link);\r
+    while (!IsListEmpty (&StreamNode->Children)) {\r
+      Link = GetFirstNode (&StreamNode->Children);\r
+      ChildNode = CHILD_SECTION_NODE_FROM_LINK (Link);\r
+      FreeChildNode (ChildNode);\r
     }\r
-  }\r
-  \r
-  return EFI_NOT_FOUND;\r
-}\r
-\r
-\r
-/**\r
-  Check if a stream is valid.\r
-\r
-  @param  SectionStream          The section stream to be checked \r
-  @param  SectionStreamLength    The length of section stream \r
-\r
-  @return A boolean value indicating the validness of the section stream.\r
-\r
-**/\r
-BOOLEAN\r
-IsValidSectionStream (\r
-  IN  VOID              *SectionStream,\r
-  IN  UINTN             SectionStreamLength\r
-  )\r
-{\r
-  UINTN                       TotalLength;\r
-  UINTN                       SectionLength;\r
-  EFI_COMMON_SECTION_HEADER   *SectionHeader;\r
-  EFI_COMMON_SECTION_HEADER   *NextSectionHeader;\r
-\r
-  TotalLength = 0;\r
-  SectionHeader = (EFI_COMMON_SECTION_HEADER *)SectionStream;\r
-  \r
-  while (TotalLength < SectionStreamLength) {\r
-    SectionLength = SECTION_SIZE (SectionHeader);\r
-    TotalLength += SectionLength;\r
-\r
-    if (TotalLength == SectionStreamLength) {\r
-      return TRUE;    \r
+    if (FreeStreamBuffer) {\r
+      CoreFreePool (StreamNode->StreamBuffer);\r
     }\r
-\r
-    //\r
-    // Move to the next byte following the section...\r
-    //\r
-    SectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) SectionHeader + SectionLength);\r
-    \r
-    //\r
-    // Figure out where the next section begins\r
-    //\r
-    NextSectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINTN) SectionHeader + 3);\r
-    NextSectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINTN) NextSectionHeader & ~(UINTN)3);\r
-    TotalLength += (UINTN) NextSectionHeader - (UINTN) SectionHeader;\r
-    SectionHeader = NextSectionHeader;\r
+    CoreFreePool (StreamNode);\r
+    Status = EFI_SUCCESS;\r
+  } else {\r
+    Status = EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  ASSERT (FALSE);\r
-  return FALSE;\r
+  CoreRestoreTpl (OldTpl);\r
+  return Status;\r
 }\r
 \r
 \r
@@ -1373,7 +1440,7 @@ IsValidSectionStream (
   EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is\r
   defined in RaiseTPL() in the UEFI 2.0 specification.\r
 \r
-  \r
+\r
   @param This         Indicates the\r
                       EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.\r
   @param InputSection Buffer containing the input GUIDed section\r
@@ -1446,7 +1513,7 @@ CustomGuidedSectionExtract (
   UINT32          OutputBufferSize;\r
   UINT32          ScratchBufferSize;\r
   UINT16          SectionAttribute;\r
-  \r
+\r
   //\r
   // Init local variable\r
   //\r
@@ -1462,28 +1529,31 @@ CustomGuidedSectionExtract (
              &ScratchBufferSize,\r
              &SectionAttribute\r
              );\r
-  \r
+\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status));\r
     return Status;\r
   }\r
-  \r
-  if (ScratchBufferSize != 0) {\r
+\r
+  if (ScratchBufferSize > 0) {\r
     //\r
     // Allocate scratch buffer\r
     //\r
-    ScratchBuffer = CoreAllocateBootServicesPool (ScratchBufferSize);\r
+    ScratchBuffer = AllocatePool (ScratchBufferSize);\r
     if (ScratchBuffer == NULL) {\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
   }\r
 \r
-  if (OutputBufferSize > 0) {  \r
+  if (OutputBufferSize > 0) {\r
     //\r
     // Allocate output buffer\r
     //\r
-    AllocatedOutputBuffer = CoreAllocateBootServicesPool (OutputBufferSize);\r
+    AllocatedOutputBuffer = AllocatePool (OutputBufferSize);\r
     if (AllocatedOutputBuffer == NULL) {\r
+      if (ScratchBuffer != NULL) {\r
+        FreePool (ScratchBuffer);\r
+      }\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
     *OutputBuffer = AllocatedOutputBuffer;\r
@@ -1493,7 +1563,7 @@ CustomGuidedSectionExtract (
   // Call decode function to extract raw data from the guided section.\r
   //\r
   Status = ExtractGuidedSectionDecode (\r
-             InputSection, \r
+             InputSection,\r
              OutputBuffer,\r
              ScratchBuffer,\r
              AuthenticationStatus\r
@@ -1514,9 +1584,9 @@ CustomGuidedSectionExtract (
 \r
   if (*OutputBuffer != AllocatedOutputBuffer) {\r
     //\r
-    // OutputBuffer was returned as a different value, \r
+    // OutputBuffer was returned as a different value,\r
     // so copy section contents to the allocated memory buffer.\r
-    // \r
+    //\r
     CopyMem (AllocatedOutputBuffer, *OutputBuffer, OutputBufferSize);\r
     *OutputBuffer = AllocatedOutputBuffer;\r
   }\r