]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Universal/SectionExtractionDxe/SectionExtraction.c
IntelFrameworkModulePkg: Fix typos in comments
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / SectionExtractionDxe / SectionExtraction.c
index 6bab4db1f091dd5adf66982bbf2a9542b000d783..cacac486bbfece03bbeb098566e4930f68da0cd3 100644 (file)
@@ -27,8 +27,8 @@
   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 - 2010, Intel Corporation. <BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2016, 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
@@ -45,6 +45,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/UefiBootServicesTableLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
+#include <Library/UefiLib.h>\r
 #include <Protocol/Decompress.h>\r
 #include <Protocol/GuidedSectionExtraction.h>\r
 #include <Protocol/SectionExtraction.h>\r
@@ -75,6 +76,11 @@ 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
 } FRAMEWORK_SECTION_CHILD_NODE;\r
 \r
 #define FRAMEWORK_SECTION_STREAM_SIGNATURE SIGNATURE_32('S','X','S','S')\r
@@ -100,7 +106,6 @@ typedef struct {
   FRAMEWORK_SECTION_CHILD_NODE     *ChildNode;\r
   FRAMEWORK_SECTION_STREAM_NODE    *ParentStream;\r
   VOID                             *Registration;\r
-  EFI_EVENT                        Event;\r
 } RPN_EVENT_CONTEXT;\r
 \r
 /**\r
@@ -144,7 +149,7 @@ OpenSectionStream (
   @param Buffer                Double indirection to buffer.  If *Buffer is non-null on\r
                                input, then the buffer is caller allocated.  If\r
                                *Buffer is NULL, then the buffer is callee allocated.\r
-                               In either case, the requried buffer size is returned\r
+                               In either case, the required buffer size is returned\r
                                in *BufferSize.\r
   @param BufferSize            On input, indicates the size of *Buffer if *Buffer is\r
                                non-null on input.  On output, indicates the required\r
@@ -278,7 +283,11 @@ IsValidSectionStream (
   SectionHeader = (EFI_COMMON_SECTION_HEADER *)SectionStream;\r
   \r
   while (TotalLength < SectionStreamLength) {\r
-    SectionLength = SECTION_SIZE (SectionHeader);\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
@@ -327,7 +336,7 @@ OpenSectionStreamEx (
   )\r
 {\r
   FRAMEWORK_SECTION_STREAM_NODE    *NewStream;\r
-  EFI_TPL                     OldTpl;\r
+  EFI_TPL                          OldTpl;\r
   \r
   //\r
   // Allocate a new stream\r
@@ -446,8 +455,8 @@ BOOLEAN
 ChildIsType (\r
   IN FRAMEWORK_SECTION_STREAM_NODE *Stream,\r
   IN FRAMEWORK_SECTION_CHILD_NODE  *Child,\r
-  IN EFI_SECTION_TYPE         SearchType,\r
-  IN EFI_GUID                 *SectionDefinitionGuid\r
+  IN EFI_SECTION_TYPE              SearchType,\r
+  IN EFI_GUID                      *SectionDefinitionGuid\r
   )\r
 {\r
   EFI_GUID_DEFINED_SECTION    *GuidedSection;\r
@@ -458,11 +467,15 @@ ChildIsType (
   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
@@ -528,8 +541,56 @@ CreateProtocolNotifyEvent (
 }\r
 \r
 /**\r
-  RPN callback function.  Removes a stale section stream and re-initializes it\r
-  with an updated AuthenticationStatus.\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
+  //\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 = gBS->LocateProtocol (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.  \r
+  1. Initialize the section stream when the GUIDED_SECTION_EXTRACTION_PROTOCOL is installed.\r
+  2. Removes a stale section stream and re-initializes it with an updated AuthenticationStatus.\r
 \r
   @param Event               The event that fired\r
   @param RpnContext          A pointer to the context that allows us to identify\r
@@ -552,21 +613,24 @@ NotifyGuidedExtraction (
   RPN_EVENT_CONTEXT                       *Context;\r
   \r
   Context = RpnContext;\r
-  \r
-  Status = CloseSectionStream (&mSectionExtraction, Context->ChildNode->EncapsulatedStreamHandle);\r
+  Status = EFI_SUCCESS;\r
+  if (Context->ChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {\r
+    Status = CloseSectionStream (&mSectionExtraction, Context->ChildNode->EncapsulatedStreamHandle);\r
+  }\r
   if (!EFI_ERROR (Status)) {\r
     //\r
-    // The stream closed successfully, so re-open the stream with correct AuthenticationStatus\r
+    // The stream is not initialized, open it. \r
+    // Or the stream closed successfully, so re-open the stream with correct AuthenticationStatus.\r
     //\r
   \r
     GuidedHeader = (EFI_GUID_DEFINED_SECTION *) \r
       (Context->ParentStream->StreamBuffer + Context->ChildNode->OffsetInStream);\r
     ASSERT (GuidedHeader->CommonHeader.Type == EFI_SECTION_GUID_DEFINED);\r
     \r
-    Status = gBS->LocateProtocol (Context->ChildNode->EncapsulationGuid, NULL, (VOID **)&GuidedExtraction);\r
-    ASSERT_EFI_ERROR (Status);\r
+    if (!VerifyGuidedSectionGuid (Context->ChildNode->EncapsulationGuid, &GuidedExtraction)) {\r
+      return;\r
+    }\r
 \r
-    \r
     Status = GuidedExtraction->ExtractSection (\r
                                  GuidedExtraction,\r
                                  GuidedHeader,\r
@@ -576,7 +640,7 @@ NotifyGuidedExtraction (
                                  );\r
     ASSERT_EFI_ERROR (Status);\r
     //\r
-    // OR in the parent stream's aggregagate status.\r
+    // OR in the parent stream's aggregate status.\r
     //\r
     AuthenticationStatus |= Context->ParentStream->AuthenticationStatus & EFI_AGGREGATE_AUTH_STATUS_ALL;\r
     Status = OpenSectionStreamEx (\r
@@ -590,12 +654,13 @@ NotifyGuidedExtraction (
   }\r
 \r
   //\r
-  //  If above, the stream  did not close successfully, it indicates it's\r
-  //  alread been closed by someone, so just destroy the event and be done with\r
+  //  If above, the stream did not close successfully, it indicates it's\r
+  //  already been closed by someone, so just destroy the event and be done with\r
   //  it.\r
   //\r
   \r
   gBS->CloseEvent (Event);\r
+  Context->ChildNode->Event = NULL;\r
   FreePool (Context);\r
 }  \r
 \r
@@ -624,14 +689,14 @@ CreateGuidedExtractionRpnEvent (
   Context->ChildNode = ChildNode;\r
   Context->ParentStream = ParentStream;\r
  \r
-  Context->Event = CreateProtocolNotifyEvent (\r
-                    Context->ChildNode->EncapsulationGuid,\r
-                    TPL_NOTIFY,\r
-                    NotifyGuidedExtraction,\r
-                    Context,\r
-                    &Context->Registration,\r
-                    FALSE\r
-                    );\r
+  Context->ChildNode->Event = CreateProtocolNotifyEvent (\r
+                                Context->ChildNode->EncapsulationGuid,\r
+                                TPL_NOTIFY,\r
+                                NotifyGuidedExtraction,\r
+                                Context,\r
+                                &Context->Registration,\r
+                                FALSE\r
+                                );\r
 }\r
 \r
 /**\r
@@ -655,8 +720,8 @@ CreateGuidedExtractionRpnEvent (
 EFI_STATUS\r
 CreateChildNode (\r
   IN     FRAMEWORK_SECTION_STREAM_NODE              *Stream,\r
-  IN     UINT32                                ChildOffset,\r
-     OUT FRAMEWORK_SECTION_CHILD_NODE               **ChildNode\r
+  IN     UINT32                                     ChildOffset,\r
+  OUT    FRAMEWORK_SECTION_CHILD_NODE               **ChildNode\r
   )\r
 {\r
   EFI_STATUS                                   Status;\r
@@ -670,7 +735,11 @@ CreateChildNode (
   UINT32                                       ScratchSize;\r
   UINTN                                        NewStreamBufferSize;\r
   UINT32                                       AuthenticationStatus;\r
-  UINT32                                       SectionLength;\r
+  VOID                                         *CompressionSource;\r
+  UINT32                                       CompressionSourceSize;\r
+  UINT32                                       UncompressedLength;\r
+  UINT8                                        CompressionType;\r
+  UINT16                                       GuidedSectionAttributes;\r
     \r
   FRAMEWORK_SECTION_CHILD_NODE                      *Node;\r
 \r
@@ -679,7 +748,7 @@ CreateChildNode (
   //\r
   // Allocate a new node\r
   //\r
-  *ChildNode = AllocatePool (sizeof (FRAMEWORK_SECTION_CHILD_NODE));\r
+  *ChildNode = AllocateZeroPool (sizeof (FRAMEWORK_SECTION_CHILD_NODE));\r
   Node = *ChildNode;\r
   if (Node == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -690,7 +759,11 @@ CreateChildNode (
   //\r
   Node->Signature = FRAMEWORK_SECTION_CHILD_SIGNATURE;\r
   Node->Type = SectionHeader->Type;\r
-  Node->Size = SECTION_SIZE (SectionHeader);\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
@@ -703,27 +776,42 @@ CreateChildNode (
       //\r
       // Get the CompressionSectionHeader\r
       //\r
-      ASSERT (Node->Size >= sizeof (EFI_COMPRESSION_SECTION));\r
-      \r
+      if (Node->Size < sizeof (EFI_COMPRESSION_SECTION)) {\r
+        FreePool (Node);\r
+        return EFI_NOT_FOUND;\r
+      }\r
+\r
       CompressionHeader = (EFI_COMPRESSION_SECTION *) SectionHeader;\r
-      \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 (CompressionHeader->UncompressedLength > 0) {\r
-        NewStreamBufferSize = CompressionHeader->UncompressedLength;\r
+      if (UncompressedLength > 0) {\r
+        NewStreamBufferSize = UncompressedLength;\r
         NewStreamBuffer = AllocatePool (NewStreamBufferSize);\r
         if (NewStreamBuffer == NULL) {\r
           FreePool (Node);\r
           return EFI_OUT_OF_RESOURCES;\r
         }\r
         \r
-        if (CompressionHeader->CompressionType == EFI_NOT_COMPRESSED) {\r
+        if (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
+          CopyMem (NewStreamBuffer, CompressionSource, NewStreamBufferSize);\r
+        } else if (CompressionType == EFI_STANDARD_COMPRESSION) {\r
           //\r
           // Only support the EFI_SATNDARD_COMPRESSION algorithm.\r
           // \r
@@ -737,13 +825,19 @@ CreateChildNode (
           \r
           Status = Decompress->GetInfo (\r
                                  Decompress,\r
-                                 CompressionHeader + 1,\r
-                                 Node->Size - sizeof (EFI_COMPRESSION_SECTION),\r
+                                 CompressionSource,\r
+                                 CompressionSourceSize,\r
                                  (UINT32 *)&NewStreamBufferSize,\r
                                  &ScratchSize\r
                                  );\r
-          ASSERT_EFI_ERROR (Status);\r
-          ASSERT (NewStreamBufferSize == CompressionHeader->UncompressedLength);\r
+          if (EFI_ERROR (Status) || (NewStreamBufferSize != UncompressedLength)) {\r
+            FreePool (Node);\r
+            FreePool (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
@@ -754,15 +848,19 @@ CreateChildNode (
 \r
           Status = Decompress->Decompress (\r
                                  Decompress,\r
-                                 CompressionHeader + 1,\r
-                                 Node->Size - sizeof (EFI_COMPRESSION_SECTION),\r
+                                 CompressionSource,\r
+                                 CompressionSourceSize,\r
                                  NewStreamBuffer,\r
                                  (UINT32)NewStreamBufferSize,\r
                                  ScratchBuffer,\r
                                  ScratchSize\r
                                  );\r
-          ASSERT_EFI_ERROR (Status);\r
-          FreePool (ScratchBuffer);                                           \r
+          FreePool (ScratchBuffer); \r
+          if (EFI_ERROR (Status)) {\r
+            FreePool (Node);\r
+            FreePool (NewStreamBuffer);\r
+            return Status;\r
+          }\r
         }\r
       } else {\r
         NewStreamBuffer = NULL;\r
@@ -785,9 +883,14 @@ CreateChildNode (
 \r
     case EFI_SECTION_GUID_DEFINED:\r
       GuidedHeader = (EFI_GUID_DEFINED_SECTION *) SectionHeader;\r
-      Node->EncapsulationGuid = &GuidedHeader->SectionDefinitionGuid;\r
-      Status = gBS->LocateProtocol (Node->EncapsulationGuid, NULL, (VOID **)&GuidedExtraction);\r
-      if (!EFI_ERROR (Status)) {\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
@@ -808,7 +911,7 @@ CreateChildNode (
         // 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) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {\r
+        if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {\r
           //\r
           // OR in the parent stream's aggregate status.\r
           //\r
@@ -837,47 +940,57 @@ CreateChildNode (
         //\r
         // There's no GUIDed section extraction protocol available.\r
         //\r
-        if ((GuidedHeader->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) == EFI_GUIDED_SECTION_PROCESSING_REQUIRED) {\r
+        if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) == EFI_GUIDED_SECTION_PROCESSING_REQUIRED) {\r
           //\r
-          // If the section REQUIRES an extraction protocol, then we're toast\r
+          // If the section REQUIRES an extraction protocol, register for RPN \r
+          // when the required GUIDed extraction protocol becomes available. \r
           //\r
-          FreePool (*ChildNode);\r
-          return EFI_PROTOCOL_ERROR;\r
-        }\r
-        \r
-        //\r
-        // Figure out the proper authentication status\r
-        //\r
-        AuthenticationStatus = Stream->AuthenticationStatus;\r
-        if ((GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {\r
+          AuthenticationStatus = 0;\r
+          CreateGuidedExtractionRpnEvent (Stream, Node);\r
+        } else {\r
           //\r
-          //  The local status of the new stream is contained in \r
-          //  AuthenticaionStatus.  This value needs to be ORed into the\r
-          //  Aggregate bits also...\r
+          // Figure out the proper authentication status\r
           //\r
+          AuthenticationStatus = Stream->AuthenticationStatus;\r
+          if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {\r
+            //\r
+            //  The local status of the new stream is contained in \r
+            //  AuthenticaionStatus.  This value needs to be ORed into the\r
+            //  Aggregate bits also...\r
+            //\r
+            \r
+            //\r
+            // Clear out and initialize the local status\r
+            //\r
+            AuthenticationStatus &= ~EFI_LOCAL_AUTH_STATUS_ALL;\r
+            AuthenticationStatus |= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED | EFI_LOCAL_AUTH_STATUS_NOT_TESTED;\r
+            //\r
+            // OR local status into aggregate status\r
+            //\r
+            AuthenticationStatus |= AuthenticationStatus >> 16;\r
+          }\r
           \r
-          //\r
-          // Clear out and initialize the local status\r
-          //\r
-          AuthenticationStatus &= ~EFI_LOCAL_AUTH_STATUS_ALL;\r
-          AuthenticationStatus |= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED | EFI_LOCAL_AUTH_STATUS_NOT_TESTED;\r
-          //\r
-          // OR local status into aggregate status\r
-          //\r
-          AuthenticationStatus |= AuthenticationStatus >> 16;\r
-        }\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
-          FreePool (Node);\r
-          return Status;\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
+            FreePool (Node);\r
+            return Status;\r
+          }\r
         }\r
       }\r
       \r
@@ -984,6 +1097,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
@@ -1000,7 +1114,6 @@ FindChildNode (
       }\r
     }\r
     \r
-    ASSERT (CurrentChildNode != NULL);\r
     if (CurrentChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {\r
       //\r
       // If the current node is an encapsulating node, recurse into it...\r
@@ -1026,6 +1139,13 @@ 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
     if (!IsNodeAtEnd (&SourceStream->Children, &CurrentChildNode->Link)) {\r
@@ -1114,7 +1234,7 @@ FindStreamNode (
   @param Buffer                Double indirection to buffer.  If *Buffer is non-null on\r
                                input, then the buffer is caller allocated.  If\r
                                *Buffer is NULL, then the buffer is callee allocated.\r
-                               In either case, the requried buffer size is returned\r
+                               In either case, the required buffer size is returned\r
                                in *BufferSize.\r
   @param BufferSize            On input, indicates the size of *Buffer if *Buffer is\r
                                non-null on input.  On output, indicates the required\r
@@ -1155,20 +1275,22 @@ GetSection (
   OUT UINT32                                            *AuthenticationStatus\r
   )\r
 {\r
-  FRAMEWORK_SECTION_STREAM_NODE                              *StreamNode;\r
+  FRAMEWORK_SECTION_STREAM_NODE                         *StreamNode;\r
   EFI_TPL                                               OldTpl;\r
   EFI_STATUS                                            Status;\r
-  FRAMEWORK_SECTION_CHILD_NODE                               *ChildNode;\r
-  FRAMEWORK_SECTION_STREAM_NODE                              *ChildStreamNode;\r
+  FRAMEWORK_SECTION_CHILD_NODE                          *ChildNode;\r
+  FRAMEWORK_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
 \r
   OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
   Instance = SectionInstance + 1;\r
+  ChildStreamNode = NULL;\r
   \r
   //\r
   // Locate target stream\r
@@ -1207,8 +1329,15 @@ GetSection (
     }\r
     ASSERT (ChildNode != NULL);\r
     ASSERT (ChildStreamNode != NULL);\r
-    CopySize = ChildNode->Size - sizeof (EFI_COMMON_SECTION_HEADER);\r
-    CopyBuffer = ChildStreamNode->StreamBuffer + ChildNode->OffsetInStream + sizeof (EFI_COMMON_SECTION_HEADER);\r
+    Section = (EFI_COMMON_SECTION_HEADER *) (ChildStreamNode->StreamBuffer + ChildNode->OffsetInStream);\r
+\r
+    if (IS_SECTION2 (Section)) {\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
@@ -1263,6 +1392,11 @@ FreeChildNode (
     //\r
     CloseSectionStream (&mSectionExtraction, ChildNode->EncapsulatedStreamHandle);\r
   }\r
+\r
+  if (ChildNode->Event != NULL) {\r
+    gBS->CloseEvent (ChildNode->Event);\r
+  }\r
+\r
   //\r
   // Last, free the child node itself\r
   //\r