⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coresectionextraction.c

📁 EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是EFI BIOS源代码中的与平台无关部分的代码
💻 C
📖 第 1 页 / 共 4 页
字号:
        return EFI_SUCCESS;
      } else {
        ErrorStatus = Status;
      }
    }
    
    if (!IsNodeAtEnd (&SourceStream->Children, &CurrentChildNode->Link)) {
      //
      // We haven't found the child node we're interested in yet, but there's
      // still more nodes that have already been parsed so get the next one
      // and continue searching..
      //
      CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream->Children, &CurrentChildNode->Link));
    } else {
      //
      // We've exhausted children that have already been parsed, so see if
      // there's any more data and continue parsing out more children if there
      // is.
      //
      NextChildOffset = CurrentChildNode->OffsetInStream + CurrentChildNode->Size;
      //
      // Round up to 4 byte boundary
      //
      NextChildOffset += 3;
      NextChildOffset &= ~(UINTN)3;
      if (NextChildOffset <= SourceStream->StreamLength - sizeof (EFI_COMMON_SECTION_HEADER)) {
        //
        // There's an unparsed child remaining in the stream, so create a new child node
        //
        Status = CreateChildNode (SourceStream, NextChildOffset, &CurrentChildNode);
        if (EFI_ERROR (Status)) {
          return Status;
        }
      } else {
        ASSERT (EFI_ERROR (ErrorStatus));
        return ErrorStatus;
      }
    }
  }
}


STATIC
EFI_STATUS
CreateChildNode (
  IN     CORE_SECTION_STREAM_NODE              *Stream,
  IN     UINT32                                ChildOffset,
     OUT CORE_SECTION_CHILD_NODE               **ChildNode
  )
/*++

Routine Description:
  Worker function.  Constructor for new child nodes.

Arguments:
  Stream              - Indicates the section stream in which to add the child.
  ChildOffset         - Indicates the offset in Stream that is the beginning
                        of the child section.
  ChildNode           - Indicates the Callee allocated and initialized child.

Returns:
  EFI_SUCCESS         - Child node was found and returned.
  EFI_OUT_OF_RESOURCES- Memory allocation failed.
  EFI_PROTOCOL_ERROR  - Encapsulation sections produce new stream handles when
                        the child node is created.  If the section type is GUID
                        defined, and the extraction GUID does not exist, and
                        producing the stream requires the GUID, then a protocol
                        error is generated and no child is produced.
  Values returned by OpenSectionStreamEx.

--*/
{
  EFI_STATUS                                   Status;
  EFI_COMMON_SECTION_HEADER                    *SectionHeader;
  EFI_COMPRESSION_SECTION                      *CompressionHeader;
  EFI_GUID_DEFINED_SECTION                     *GuidedHeader;
  EFI_TIANO_DECOMPRESS_PROTOCOL                *Decompress;
  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL       *GuidedExtraction;
  VOID                                         *NewStreamBuffer;
  VOID                                         *ScratchBuffer;
  UINT32                                       ScratchSize;
  UINTN                                        NewStreamBufferSize;
  UINT32                                       AuthenticationStatus;
  UINT32                                       SectionLength;
    
  CORE_SECTION_CHILD_NODE                      *Node;

  SectionHeader = (EFI_COMMON_SECTION_HEADER *) (Stream->StreamBuffer + ChildOffset);

  //
  // Allocate a new node
  //
  *ChildNode = CoreAllocateBootServicesPool (sizeof (CORE_SECTION_CHILD_NODE));
  Node = *ChildNode;
  if (Node == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
  
  //
  // Now initialize it
  //
  Node->Signature = CORE_SECTION_CHILD_SIGNATURE;
  Node->Type = SectionHeader->Type;
  Node->Size = SECTION_SIZE (SectionHeader);
  Node->OffsetInStream = ChildOffset;
  Node->EncapsulatedStreamHandle = NULL_STREAM_HANDLE;
  Node->EncapsulationGuid = NULL;
  
  //
  // If it's an encapsulating section, then create the new section stream also
  //
  switch (Node->Type) {
    case EFI_SECTION_COMPRESSION:
      //
      // Get the CompressionSectionHeader
      //
      ASSERT (Node->Size >= sizeof (EFI_COMPRESSION_SECTION));
      
      CompressionHeader = (EFI_COMPRESSION_SECTION *) SectionHeader;
      
      //
      // Allocate space for the new stream
      //
      if (CompressionHeader->UncompressedLength > 0) {
        NewStreamBufferSize = CompressionHeader->UncompressedLength;
        NewStreamBuffer = CoreAllocateBootServicesPool (NewStreamBufferSize);
        if (NewStreamBuffer == NULL) {
          CoreFreePool (Node);
          return EFI_OUT_OF_RESOURCES;
        }
        
        if (CompressionHeader->CompressionType == EFI_NOT_COMPRESSED) {
          //
          // stream is not actually compressed, just encapsulated.  So just copy it.
          //
          EfiCommonLibCopyMem (NewStreamBuffer, CompressionHeader + 1, NewStreamBufferSize);
        } else if (CompressionHeader->CompressionType == EFI_STANDARD_COMPRESSION ||
                   CompressionHeader->CompressionType == EFI_CUSTOMIZED_COMPRESSION) {
          //
          // Decompress the stream
          //
          if (CompressionHeader->CompressionType == EFI_STANDARD_COMPRESSION) {
          Status = CoreLocateProtocol (&gEfiTianoDecompressProtocolGuid, NULL, &Decompress);
          } else {
            Status = CoreLocateProtocol (&gEfiCustomizedDecompressProtocolGuid, NULL, &Decompress);
          }
          
          ASSERT_EFI_ERROR (Status);
          
          Status = Decompress->GetInfo (
                                 Decompress,
                                 CompressionHeader + 1,
                                 Node->Size - sizeof (EFI_COMPRESSION_SECTION),
                                 (UINT32 *)&NewStreamBufferSize,
                                 &ScratchSize
                                 );
          ASSERT_EFI_ERROR (Status);
          ASSERT (NewStreamBufferSize == CompressionHeader->UncompressedLength);

          ScratchBuffer = CoreAllocateBootServicesPool (ScratchSize);
          if (ScratchBuffer == NULL) {
            CoreFreePool (Node);
            CoreFreePool (NewStreamBuffer);
            return EFI_OUT_OF_RESOURCES;
          }

          Status = Decompress->Decompress (
                                 Decompress,
                                 CompressionHeader + 1,
                                 Node->Size - sizeof (EFI_COMPRESSION_SECTION),
                                 NewStreamBuffer,
                                 (UINT32)NewStreamBufferSize,
                                 ScratchBuffer,
                                 ScratchSize
                                 );
          ASSERT_EFI_ERROR (Status);
          CoreFreePool (ScratchBuffer);                                           
        }
      } else {
        NewStreamBuffer = NULL;
        NewStreamBufferSize = 0;
      }
      
      Status = OpenSectionStreamEx (
                 NewStreamBufferSize,
                 NewStreamBuffer,
                 FALSE,
                 Stream->AuthenticationStatus,
                 &Node->EncapsulatedStreamHandle
                 );
      if (EFI_ERROR (Status)) {
        CoreFreePool (Node);
        CoreFreePool (NewStreamBuffer);
        return Status;
      }
      break;

    case EFI_SECTION_GUID_DEFINED:
      GuidedHeader = (EFI_GUID_DEFINED_SECTION *) SectionHeader;
      Node->EncapsulationGuid = &GuidedHeader->SectionDefinitionGuid;
      Status = CoreLocateProtocol (Node->EncapsulationGuid, NULL, &GuidedExtraction);
      if (!EFI_ERROR (Status)) {
        //
        // NewStreamBuffer is always allocated by ExtractSection... No caller
        // allocation here.
        //
        Status = GuidedExtraction->ExtractSection (
                                     GuidedExtraction,
                                     GuidedHeader,
                                     &NewStreamBuffer,
                                     &NewStreamBufferSize,
                                     &AuthenticationStatus
                                     );
        if (EFI_ERROR (Status)) {
          CoreFreePool (*ChildNode);
          return EFI_PROTOCOL_ERROR;
        }
        
        //
        // Make sure we initialize the new stream with the correct 
        // authentication status for both aggregate and local status fields.
        //
        if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {
          //
          // OR in the parent stream's aggregate status.
          //
          AuthenticationStatus |= Stream->AuthenticationStatus & EFI_AGGREGATE_AUTH_STATUS_ALL;
        } else {
          //
          // since there's no authentication data contributed by the section,
          // just inherit the full value from our immediate parent.
          //
          AuthenticationStatus = Stream->AuthenticationStatus;
        }
        
        Status = OpenSectionStreamEx (
                   NewStreamBufferSize,
                   NewStreamBuffer,
                   FALSE,
                   AuthenticationStatus,
                   &Node->EncapsulatedStreamHandle
                   );
        if (EFI_ERROR (Status)) {
          CoreFreePool (*ChildNode);
          CoreFreePool (NewStreamBuffer);
          return Status;
        }
      } else {
        //
        // There's no GUIDed section extraction protocol available.
        //
        if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) {
          //
          // If the section REQUIRES an extraction protocol, then we're toast
          //
          CoreFreePool (*ChildNode);
          return EFI_PROTOCOL_ERROR;
        }
        
        //
        // Figure out the proper authentication status
        //
        AuthenticationStatus = Stream->AuthenticationStatus;
        if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {
          //
          //  The local status of the new stream is contained in 
          //  AuthenticaionStatus.  This value needs to be ORed into the
          //  Aggregate bits also...
          //
          
          //
          // Clear out and initialize the local status
          //
          AuthenticationStatus &= ~EFI_LOCAL_AUTH_STATUS_ALL;
          AuthenticationStatus |= EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED | EFI_LOCAL_AUTH_STATUS_NOT_TESTED;
          //
          // OR local status into aggregate status
          //
          AuthenticationStatus |= AuthenticationStatus >> 16;
        }
        
        SectionLength = SECTION_SIZE (GuidedHeader);
        Status = OpenSectionStreamEx (
                   SectionLength - GuidedHeader->DataOffset,
                   (UINT8 *) GuidedHeader + GuidedHeader->DataOffset,
                   TRUE,
                   AuthenticationStatus,
                   &Node->EncapsulatedStreamHandle
                   );
        if (EFI_ERROR (Status)) {
          CoreFreePool (Node);
          return Status;
        }
      }
      
      if ((AuthenticationStatus & EFI_LOCAL_AUTH_STATUS_ALL) == 
            (EFI_LOCAL_AUTH_STATUS_IMAGE_SIGNED | EFI_LOCAL_AUTH_STATUS_NOT_TESTED)) {
        //
        // Need to register for RPN for when the required GUIDed extraction
        // protocol becomes available.  This will enable us to refresh the
        // AuthenticationStatus cached in the Stream if it's ever requested
        // again.
        //
        CreateGuidedExtractionRpnEvent (Stream, Node);
      }
      
      break;

    default:
      
      //
      // Nothing to do if it's a leaf
      //
      break;
  }
  
  //
  // Last, add the new child node to the stream
  //
  InsertTailList (&Stream->Children, &Node->Link);

  return EFI_SUCCESS;
}


STATIC
VOID
CreateGuidedExtractionRpnEvent (
  IN CORE_SECTION_STREAM_NODE       *ParentStream,
  IN CORE_SECTION_CHILD_NODE        *ChildNode
  )
/*++

Routine Description:
  Worker function.  Constructor for RPN event if needed to keep AuthenticationStatus
  cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...

Arguments:
  ParentStream        - Indicates the parent of the ecnapsulation section (child)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -