[edk2-devel] [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2

GregX Yeh posted 1 patch 3 years, 4 months ago
Failed in applying to current master (apply log)
There is a newer version of this series
...MMT-Replace-file-failure-when-FV-level-ov.patch | 456 ++++++++++++++
BaseTools/Source/C/FMMT/FirmwareModuleManagement.h |   4 +-
BaseTools/Source/C/FMMT/FmmtLib.c                  | 654 ++++++++++-----------
3 files changed, 782 insertions(+), 332 deletions(-)
create mode 100644 0001-BaseTools-FMMT-Replace-file-failure-when-FV-level-ov.patch
[edk2-devel] [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
Posted by GregX Yeh 3 years, 4 months ago
Fixed replace file failure when FFS in multi level FV and FV level over 2.

Signed-off-by: GregX Yeh <gregx.yeh@intel.com>
Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
---
 ...MMT-Replace-file-failure-when-FV-level-ov.patch | 456 ++++++++++++++
 BaseTools/Source/C/FMMT/FirmwareModuleManagement.h |   4 +-
 BaseTools/Source/C/FMMT/FmmtLib.c                  | 654 ++++++++++-----------
 3 files changed, 782 insertions(+), 332 deletions(-)
 create mode 100644 0001-BaseTools-FMMT-Replace-file-failure-when-FV-level-ov.patch

diff --git a/0001-BaseTools-FMMT-Replace-file-failure-when-FV-level-ov.patch b/0001-BaseTools-FMMT-Replace-file-failure-when-FV-level-ov.patch
new file mode 100644
index 0000000000..e42770c9e4
--- /dev/null
+++ b/0001-BaseTools-FMMT-Replace-file-failure-when-FV-level-ov.patch
@@ -0,0 +1,456 @@
+From eba4dd0899b011b0ac89723876be66a9d3f39251 Mon Sep 17 00:00:00 2001
+From: GregX Yeh <gregx.yeh@intel.com>
+Date: Tue, 3 Nov 2020 10:57:00 +0800
+Subject: [PATCH] BaseTools/FMMT: Replace file failure when FV level over 2
+
+Fixed replace file failure when FFS in multi level FV and FV level over 2.
+
+Signed-off-by: GregX Yeh <gregx.yeh@intel.com>
+Cc: Bob Feng <bob.c.feng@intel.com>
+Cc: Liming Gao <gaoliming@byosoft.com.cn>
+---
+ BaseTools/Source/C/FMMT/FirmwareModuleManagement.c |   2 +-
+ BaseTools/Source/C/FMMT/FirmwareModuleManagement.h |   3 +-
+ BaseTools/Source/C/FMMT/FmmtLib.c                  | 171 ++++++++++++++-------
+ 3 files changed, 118 insertions(+), 58 deletions(-)
+
+diff --git a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.c b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.c
+index 38056153fb..dfad02838b 100644
+--- a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.c
++++ b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.c
+@@ -615,7 +615,7 @@ static UINT32 FindFile(FV_INFORMATION *FvInFd, UINT8 FvLevel, CHAR8 *File, UINT3
+     return 0;
+   }
+   LibAscii2Unicode(File, UIName);
+-  for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {
++  for (Index = 0; Index < FvInFd->FfsNumbers; Index++) {
+     //
+     // Compare the New File Name with UI Name of FFS
+     // NOTE: The UI Name is Unicode, but the New File Name is Ascii.
+diff --git a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
+index 9d09c9160a..da9eb1a4b9 100644
+--- a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
++++ b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
+@@ -2,7 +2,7 @@
+ 
+  Structures and functions declaration.
+ 
+- Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
++ Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+ 
+ **/
+@@ -284,6 +284,7 @@ typedef struct _FFS_INFOMATION{
+   UINT8                      *Depex;
+   UINT32                     DepexLen;
+   BOOLEAN                    FfsFoundFlag;
++  UINT8                      FvLevel;
+   struct _FFS_INFOMATION     *Next;
+ } FFS_INFORMATION;
+ 
+diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
+index b945e9b63d..9bccd634bb 100644
+--- a/BaseTools/Source/C/FMMT/FmmtLib.c
++++ b/BaseTools/Source/C/FMMT/FmmtLib.c
+@@ -523,7 +523,7 @@ LibReadFvHeader (
+            FvExtHeader->FvName.Data4[6],
+            FvExtHeader->FvName.Data4[7]);
+     LibExtractFvUiName(FvExtHeader, &FvUiName);
+-    if (FvUiName != NULL && FvLevel == 1) {
++    if (FvUiName != NULL && (FvLevel - 1) != 0) {
+       printf("%sFV UI Name:            %s\n\n", BlankSpace, FvUiName);
+     }
+     free(FvUiName);
+@@ -709,10 +709,10 @@ LibGenFfsFile (
+   free(FfsFileName);
+   FfsFileName = NULL;
+ 
+-  CurrentFv->FfsNumbers  = *FfsCount;
+-
+   *FfsCount += 1;
+ 
++  CurrentFv->FfsNumbers  = *FfsCount;
++
+   if (ErasePolarity) {
+     CurrentFile->State = (UINT8)~(CurrentFile->State);
+   }
+@@ -789,7 +789,8 @@ LibParseSection (
+   UINT8                  *FvCount,
+   BOOLEAN                ViewFlag,
+   BOOLEAN                ErasePolarity,
+-  BOOLEAN                *IsFfsGenerated
++  BOOLEAN                *IsFfsGenerated,
++  UINT32                 *FfsIndex
+   )
+ {
+   UINT32              ParsedLength;
+@@ -1147,19 +1148,21 @@ LibParseSection (
+         return EFI_SECTION_ERROR;
+       }
+ 
+-      Status = LibParseSection (  UncompressedBuffer,
+-                                  UncompressedLength,
+-                                  CurrentFv,
+-                                  FvName,
+-                                  CurrentFile,
+-                                  Level,
+-                                  &LocalEncapData,
+-                                  FfsLevel,
+-                                  FfsCount,
+-                                  FvCount,
+-                                  ViewFlag,
+-                                  ErasePolarity,
+-                                  IsFfsGenerated);
++      Status = LibParseSection (UncompressedBuffer,
++                                UncompressedLength,
++                                CurrentFv,
++                                FvName,
++                                CurrentFile,
++                                Level,
++                                &LocalEncapData,
++                                FfsLevel,
++                                FfsCount,
++                                FvCount,
++                                ViewFlag,
++                                ErasePolarity,
++                                IsFfsGenerated,
++                                FfsIndex
++                                );
+ 
+       if (CompressionType == EFI_STANDARD_COMPRESSION) {
+         //
+@@ -1216,7 +1219,8 @@ LibParseSection (
+                 FvCount,
+                 ViewFlag,
+                 ErasePolarity,
+-                IsFfsGenerated
++                IsFfsGenerated,
++                FfsIndex
+                 );
+         if (EFI_ERROR(Status)) {
+           Error(NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL);
+@@ -1471,7 +1475,8 @@ LibParseSection (
+                   FvCount,
+                   ViewFlag,
+                   ErasePolarity,
+-                  IsFfsGenerated
++                  IsFfsGenerated,
++                  FfsIndex
+                   );
+         if (EFI_ERROR (Status)) {
+           Error (NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL);
+@@ -1491,7 +1496,8 @@ LibParseSection (
+             FvCount,
+             ViewFlag,
+             ErasePolarity,
+-            IsFfsGenerated
++            IsFfsGenerated,
++            FfsIndex
+             );
+           if (ExtractionTool != NULL) {
+             free (ExtractionTool);
+@@ -1527,6 +1533,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+ 
+@@ -1538,6 +1545,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+ 
+@@ -1549,6 +1557,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+ 
+@@ -1560,6 +1569,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+       break;
+@@ -1571,6 +1581,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+       break;
+@@ -1582,6 +1593,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+       break;
+@@ -1665,11 +1677,11 @@ LibParseSection (
+       // If Ffs file has been generated, then the FfsCount should decrease 1.
+       //
+       if (*IsFfsGenerated) {
+-        memcpy (CurrentFv->FfsAttuibutes[*FfsCount -1].UiName, UIName, UINameSize);
+-        CurrentFv->FfsAttuibutes[*FfsCount -1].UiNameSize = UINameSize;
++        memcpy (CurrentFv->FfsAttuibutes[*FfsIndex - 1].UiName, UIName, UINameSize);
++        CurrentFv->FfsAttuibutes[*FfsIndex - 1].UiNameSize = UINameSize;
+       } else {
+-        memcpy (CurrentFv->FfsAttuibutes[*FfsCount].UiName, UIName, UINameSize);
+-        CurrentFv->FfsAttuibutes[*FfsCount].UiNameSize = UINameSize;
++        memcpy (CurrentFv->FfsAttuibutes[*FfsIndex].UiName, UIName, UINameSize);
++        CurrentFv->FfsAttuibutes[*FfsIndex].UiNameSize = UINameSize;
+       }
+ 
+       HasDepexSection = FALSE;
+@@ -1682,7 +1694,9 @@ LibParseSection (
+     }
+ 
+     ParsedLength += SectionLength;
+-    FirstInFlag  = FALSE;
++    if (Type != EFI_SECTION_PEI_DEPEX && Type != EFI_SECTION_DXE_DEPEX) {
++      FirstInFlag  = FALSE;
++    }
+     //
+     // We make then next section begin on a 4-byte boundary
+     //
+@@ -1872,12 +1886,14 @@ LibGetFileInfo (
+   BOOLEAN             IsGeneratedFfs;
+   UINT32              FfsFileHeaderSize;
+   CHAR8               *BlankChar;
++  UINT32              FfsIndex;
+ 
+   Status = EFI_SUCCESS;
+ 
+   LocalEncapData  = NULL;
+   EncapDataNeedUpdateFlag = TRUE;
+   IsGeneratedFfs   = FALSE;
++  FfsIndex        = 0xFFFFFFFF;
+   BlankChar        = NULL;
+ 
+   FfsFileHeaderSize = GetFfsHeaderLength  ((EFI_FFS_FILE_HEADER *) CurrentFile);
+@@ -2083,24 +2099,25 @@ LibGetFileInfo (
+     } else if( CurrentFile->Type == EFI_FV_FILETYPE_FFS_PAD){
+       //EFI_FV_FILETYPE_FFS_PAD
+     } else {
+-    //
+-    // All other files have sections
+-    //
+-    Status = LibParseSection (
+-      (UINT8 *) ((UINTN) CurrentFile + FfsFileHeaderSize),
+-      FileLength - FfsFileHeaderSize,
+-      CurrentFv,
+-      FvName,
+-      CurrentFile,
+-      Level,
+-      &LocalEncapData,
+-      Level,
+-      FfsCount,
+-      FvCount,
+-      ViewFlag,
+-      ErasePolarity,
+-      &IsGeneratedFfs
+-      );
++      //
++      // All other files have sections
++      //
++      Status = LibParseSection (
++                 (UINT8 *) ((UINTN) CurrentFile + FfsFileHeaderSize),
++                 FileLength - FfsFileHeaderSize,
++                 CurrentFv,
++                 FvName,
++                 CurrentFile,
++                 Level,
++                 &LocalEncapData,
++                 Level,
++                 FfsCount,
++                 FvCount,
++                 ViewFlag,
++                 ErasePolarity,
++                 &IsGeneratedFfs,
++                 &FfsIndex
++                 );
+     }
+     if (EFI_ERROR (Status)) {
+       printf ("ERROR: Parsing the FFS file.\n");
+@@ -2144,6 +2161,7 @@ LibGetFvInfo (
+   EFI_FIRMWARE_VOLUME_EXT_HEADER *ExtHdrPtr;
+   EFI_FIRMWARE_VOLUME_HEADER *FvHdr;
+   UINT8                      PreFvId;
++  UINT8                       GuidBuffer[PRINTED_GUID_BUFFER_SIZE];
+ 
+   NumberOfFiles  = 0;
+   Key            = 0;
+@@ -2344,9 +2362,10 @@ LibGetFvInfo (
+ 
+     CurrentFv->FfsAttuibutes[*FfsCount].FvLevel = CurrentFv->FvLevel;
+     CurrentFv->FfsAttuibutes[*FfsCount].FvId    = PreFvId;
+-     if (CurrentFv->FvLevel > CurrentFv->MulFvLevel) {
++    if (CurrentFv->FvLevel > CurrentFv->MulFvLevel) {
+       CurrentFv->MulFvLevel = CurrentFv->FvLevel;
+-   }
++    }
++    PrintGuidToBuffer (&(CurrentFile->Name), GuidBuffer, PRINTED_GUID_BUFFER_SIZE, FALSE);
+     //
+     // Display info about this file
+     //
+@@ -4198,10 +4217,13 @@ LibEncapNewFvFile(
+   UINT32                      header;
+   UINT8                       AlignN;
+   UINT8                       AlignV[1] = {0xFF};
++  UINT32                      EntryFvId;
++
+   AlignN                      = 0;
+   Id                          = 0;
+   InputFileSize               = 0;
+   TmpFileSize                 = 0;
++  AlignmentFileSize           = 0;
+   EncapFvIndex                = 0;
+   Index                       = 0;
+   OuterIndex                  = 0;
+@@ -4224,7 +4246,7 @@ LibEncapNewFvFile(
+   IsLargeFile                 = FALSE;
+   OutputFileSize              = 0;
+   LargeFileSize               = 0x1000000;
+-
++  EntryFvId                   = 0;
+ 
+   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
+   if (OutputFileNameList == NULL) {
+@@ -4240,6 +4262,7 @@ LibEncapNewFvFile(
+   OutputFileNameList->Depex = NULL;
+   OutputFileNameList->DepexLen = 0;
+   OutputFileNameList->FfsFoundFlag = FALSE;
++  OutputFileNameList->FvLevel = 0;
+ 
+   ChildFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
+   if (ChildFileNameList == NULL) {
+@@ -4258,12 +4281,16 @@ LibEncapNewFvFile(
+   //
+   // Encapsulate from the lowest FFS file level.
+   //
+-    LocalEncapData = CurrentEncapData;
+-    if (LocalEncapData == NULL) {
+-        LocalEncapData = FvInFd->EncapData;
+-    }
+-    Level = LocalEncapData->Level;
+-    Type = LocalEncapData->Type;
++  LocalEncapData = CurrentEncapData;
++  if (LocalEncapData == NULL) {
++    LocalEncapData = FvInFd->EncapData;
++    EntryFvId = 0xFFFFFFFF;
++  } else {
++    EntryFvId = LocalEncapData->FvId;
++  }
++
++  Level = LocalEncapData->Level;
++  Type  = LocalEncapData->Type;
+ 
+   if (CurrentEncapData == NULL) {
+     LocalEncapData = FvInFd->EncapData;
+@@ -4278,6 +4305,7 @@ LibEncapNewFvFile(
+                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
+                 if (FvInFd->ChildFvFFS == NULL) {
+                     FvInFd->ChildFvFFS = ChildFileNameList;
++                    NewFileNameList = FvInFd->ChildFvFFS;
+                 } else {
+                     NewFileNameList = FvInFd->ChildFvFFS;
+                     while (NewFileNameList->Next != NULL) {
+@@ -4325,6 +4353,12 @@ LibEncapNewFvFile(
+         Type        = LocalEncapData->Type;
+       }
+       LocalEncapData = LocalEncapData->NextNode;
++      if (LocalEncapData == NULL) {
++        if (Type == FMMT_ENCAP_TREE_FV) {
++          ParentLevel = Level;
++          ParentType  = Type;
++        }
++      }
+     }
+   } else {
+     LocalEncapData = CurrentEncapData;
+@@ -4349,6 +4383,28 @@ LibEncapNewFvFile(
+             LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
+         }
+       }
++
++      if (LocalEncapData->FvId > EntryFvId && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
++        LocalEncapDataTemp = LocalEncapData->RightNode;
++
++        while (LocalEncapDataTemp != NULL) {
++          LocalEncapDataNext = LocalEncapDataTemp->NextNode;
++          if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
++            LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, LocalEncapDataTemp->Level - 1, &ChildFileNameList);
++            if (FvInFd->ChildFvFFS == NULL) {
++              FvInFd->ChildFvFFS = ChildFileNameList;
++            } else {
++              NewFileNameList = FvInFd->ChildFvFFS;
++              while (NewFileNameList->Next != NULL) {
++                NewFileNameList = NewFileNameList->Next;
++              }
++              NewFileNameList->Next = ChildFileNameList;
++            }
++          }
++          LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
++        }
++      }
++
+       if (LocalEncapData->Level > Level) {
+         if (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+           ParentLevel = Level;
+@@ -4485,12 +4541,12 @@ LibEncapNewFvFile(
+         //
+         FfsFoundFlag = FALSE;
+         IsRootFv = FALSE;
+-        for (Index=0; Index <= FvInFd->FfsNumbers; Index++) {
++        for (Index=0; Index < FvInFd->FfsNumbers; Index++) {
+             if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == FALSE){
+                 break;
+             }
+             if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == TRUE){
+-                if (Index == EncapFvIndex) {
++                if (Index == EncapFvStart) {
+                     if (FirstInFlag) {
+                             Status = LibAddFfsFileToFvInf (OutputFileNameList->FFSName, InfFile, TRUE);
+                             FirstInFlag = FALSE;
+@@ -4601,7 +4657,7 @@ LibEncapNewFvFile(
+             }
+           }
+        // The Fv may has multiple level (> 2), when it is in the FvLevel == 2, we set the IsLastLevelFfs Flag
+-       if (Index <=FvInFd->FfsNumbers && FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) {
++       if (Index < FvInFd->FfsNumbers && FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) {
+            if (FvInFd->FfsAttuibutes[Index].FvLevel == 2) {
+                IsLastLevelFfs = FALSE;
+            }
+@@ -4777,6 +4833,7 @@ LibEncapNewFvFile(
+          for (Id = FvInFd->FfsNumbers; Id <= FvInFd->FfsNumbers; Id--) {
+              if ((memcmp(&FvInFd->FfsAttuibutes[Id].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)){
+                  if (access(FvInFd->FfsAttuibutes[Id].FfsName, 0) != -1) {
++                     OutputFileNameList->FvLevel = FvInFd->FfsAttuibutes[Id].FvLevel;
+                      Status = LibFmmtDeleteFile(FvInFd->FfsAttuibutes[Id].FfsName);
+                      if (EFI_ERROR(Status)) {
+                          Error("FMMT", 0, 0004, "error while encapsulate FD Image", "Delete the specified file failed!");
+@@ -5079,7 +5136,9 @@ LibEncapNewFvFile(
+     } else {
+         if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == TRUE) {
+           *OutputFile = OutputFileNameList;
+-          return EFI_SUCCESS;
++          if (OutputFileNameList->FvLevel < 2) {
++            return EFI_SUCCESS;
++          }
+         }
+         LocalEncapData = CurrentEncapData;
+     }
+-- 
+2.16.2.windows.1
+
diff --git a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
index 9d09c9160a..ccc8ba00be 100644
--- a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
+++ b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
@@ -195,7 +195,7 @@ typedef struct {
   // UI Name for this FFS file, if has.
   //
   CHAR16           UiName[_MAX_PATH];
-   UINT32           UiNameSize;
+  UINT32           UiNameSize;
   //
   // Total section number in this FFS.
   //
@@ -208,7 +208,7 @@ typedef struct {
   //
   //Describe the belong to FV
   //
-  UINT8           FvId;
+  UINT8            FvId;
   //
   // If this FFS has no encapsulate section, this flag will set to True.
   //
diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..d6d43030fa 100644
--- a/BaseTools/Source/C/FMMT/FmmtLib.c
+++ b/BaseTools/Source/C/FMMT/FmmtLib.c
@@ -709,10 +709,10 @@ LibGenFfsFile (
   free(FfsFileName);
   FfsFileName = NULL;
 
-  CurrentFv->FfsNumbers  = *FfsCount;
-
   *FfsCount += 1;
 
+  CurrentFv->FfsNumbers  = *FfsCount;
+
   if (ErasePolarity) {
     CurrentFile->State = (UINT8)~(CurrentFile->State);
   }
@@ -833,7 +833,6 @@ LibParseSection (
   CHAR8               *ToolInputFileName;
   CHAR8               *ToolOutputFileName;
   BOOLEAN              HasUiSection;
-  BOOLEAN              FirstInFlag;
 
   DataOffset                 = 0;
   GuidAttr                   = 0;
@@ -869,7 +868,6 @@ LibParseSection (
   EncapDataNeedUpdata        = TRUE;
   LargeHeaderOffset          = 0;
   HasUiSection               = FALSE;
-  FirstInFlag                = TRUE;
 
 
   while (ParsedLength < BufferLength) {
@@ -997,9 +995,7 @@ LibParseSection (
       break;
 
     case EFI_SECTION_COMPRESSION:
-      if (FirstInFlag) {
-        Level ++;
-      }
+      Level ++;
       NumberOfSections ++;
 
       EncapDataNeedUpdata = TRUE;
@@ -1181,9 +1177,7 @@ LibParseSection (
       // looks up the appropriate tool to use for extracting
       // a GUID defined FV section.
       //
-      if (FirstInFlag) {
-        Level ++;
-      }
+      Level ++;
       NumberOfSections++;
       EncapDataNeedUpdata = TRUE;
       HasUiSection = TRUE;
@@ -1682,7 +1676,6 @@ LibParseSection (
     }
 
     ParsedLength += SectionLength;
-    FirstInFlag  = FALSE;
     //
     // We make then next section begin on a 4-byte boundary
     //
@@ -2016,7 +2009,7 @@ LibGetFileInfo (
 
       LocalEncapData->Level = Level;
       LocalEncapData->Type  = FMMT_ENCAP_TREE_FFS;
-    LocalEncapData->FvExtHeader = NULL;
+      LocalEncapData->FvExtHeader = NULL;
       LocalEncapData->Depex = NULL;
       LocalEncapData->DepexLen = 0;
       LocalEncapData->UiNameSize = 0;
@@ -4198,6 +4191,7 @@ LibEncapNewFvFile(
   UINT32                      header;
   UINT8                       AlignN;
   UINT8                       AlignV[1] = {0xFF};
+  
   AlignN                      = 0;
   Id                          = 0;
   InputFileSize               = 0;
@@ -4225,7 +4219,6 @@ LibEncapNewFvFile(
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
 
-
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
     Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
@@ -4258,12 +4251,12 @@ LibEncapNewFvFile(
   //
   // Encapsulate from the lowest FFS file level.
   //
-    LocalEncapData = CurrentEncapData;
-    if (LocalEncapData == NULL) {
-        LocalEncapData = FvInFd->EncapData;
-    }
-    Level = LocalEncapData->Level;
-    Type = LocalEncapData->Type;
+  LocalEncapData = CurrentEncapData;
+  if (LocalEncapData == NULL) {
+      LocalEncapData = FvInFd->EncapData;
+  }
+  Level = LocalEncapData->Level;
+  Type = LocalEncapData->Type;
 
   if (CurrentEncapData == NULL) {
     LocalEncapData = FvInFd->EncapData;
@@ -4271,22 +4264,21 @@ LibEncapNewFvFile(
       if (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
-            LocalEncapDataNext = LocalEncapDataTemp->NextNode;
-            if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
-
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
-                ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
-                if (FvInFd->ChildFvFFS == NULL) {
-                    FvInFd->ChildFvFFS = ChildFileNameList;
-                } else {
-                    NewFileNameList = FvInFd->ChildFvFFS;
-                    while (NewFileNameList->Next != NULL) {
-                        NewFileNameList = NewFileNameList->Next;
-                    }
-                    NewFileNameList->Next = ChildFileNameList;
+          LocalEncapDataNext = LocalEncapDataTemp->NextNode;
+          if(LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
+            LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, LocalEncapDataTemp->Level-1, &ChildFileNameList);
+            //ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
+            if (FvInFd->ChildFvFFS == NULL) {
+                FvInFd->ChildFvFFS = ChildFileNameList;
+            } else {
+                NewFileNameList = FvInFd->ChildFvFFS;
+                while (NewFileNameList->Next != NULL) {
+                    NewFileNameList = NewFileNameList->Next;
                 }
+                NewFileNameList->Next = ChildFileNameList;
             }
-            LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
+          }
+          LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
         }
       }
 
@@ -4329,24 +4321,24 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
-            LocalEncapDataNext = LocalEncapDataTemp->NextNode;
-            if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, LocalEncapDataTemp->Level-1, &ChildFileNameList);
-                //ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
-                if (FvInFd->ChildFvFFS == NULL) {
-                    FvInFd->ChildFvFFS = ChildFileNameList;
-                } else {
-                    NewFileNameList = FvInFd->ChildFvFFS;
-                    while (NewFileNameList->Next != NULL) {
-                        NewFileNameList = NewFileNameList->Next;
-                    }
-                    NewFileNameList->Next = ChildFileNameList;
+          LocalEncapDataNext = LocalEncapDataTemp->NextNode;
+          if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
+            LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, LocalEncapDataTemp->Level-1, &ChildFileNameList);
+            //ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
+            if (FvInFd->ChildFvFFS == NULL) {
+                FvInFd->ChildFvFFS = ChildFileNameList;
+            } else {
+                NewFileNameList = FvInFd->ChildFvFFS;
+                while (NewFileNameList->Next != NULL) {
+                    NewFileNameList = NewFileNameList->Next;
                 }
+                NewFileNameList->Next = ChildFileNameList;
             }
-            LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
+          }
+          LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
         }
       }
       if (LocalEncapData->Level > Level) {
@@ -4366,12 +4358,12 @@ LibEncapNewFvFile(
       case FMMT_ENCAP_TREE_FV:
         OutputFileNameListFlag = TRUE;
         EncapFvStart = 0;
-    for(OuterIndex=0;OutputFileNameListFlag;OuterIndex++){
+        for(OuterIndex=0;OutputFileNameListFlag;OuterIndex++){
         //
         // Generate FV.inf attributes.
         //
         InfFileName = LibFilenameStrExtended (strrchr(GenTempFile (),OS_SEP), TemDir, "inf");
-    FirstInFlag = TRUE;
+        FirstInFlag = TRUE;
 
         InfFile = fopen (InfFileName, "wt+");
 
@@ -4383,9 +4375,9 @@ LibEncapNewFvFile(
         }
 
         if (CurrentEncapData == NULL) {
-           LocalEncapData = FvInFd->EncapData;
+          LocalEncapData = FvInFd->EncapData;
         } else {
-            LocalEncapData = CurrentEncapData;
+          LocalEncapData = CurrentEncapData;
         }
 
         while (LocalEncapData->NextNode != NULL) {
@@ -4456,6 +4448,7 @@ LibEncapNewFvFile(
           free (ChildFileNameList);
           return Status;
         }
+
         if (LocalEncapData->FvExtHeader != NULL) {
           Status = LibGenExtFile(LocalEncapData->FvExtHeader, InfFile);
           if (EFI_ERROR(Status)) {
@@ -4473,93 +4466,93 @@ LibEncapNewFvFile(
           while (LocalEncapData->Level != ParentLevel) {
             LocalEncapData = LocalEncapData->NextNode;
           }
-            for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {
-                if ((memcmp(&FvInFd->FfsAttuibutes[Index].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)) {
-                    SubFvId = Index;
-                    break;
-                }
+          for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {
+            if ((memcmp(&FvInFd->FfsAttuibutes[Index].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)) {
+              SubFvId = Index;
+              break;
             }
+          }
         }
         //
         // Found FFSs from Fv structure.
         //
         FfsFoundFlag = FALSE;
         IsRootFv = FALSE;
-        for (Index=0; Index <= FvInFd->FfsNumbers; Index++) {
-            if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == FALSE){
-                break;
-            }
-            if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == TRUE){
-                if (Index == EncapFvIndex) {
-                    if (FirstInFlag) {
-                            Status = LibAddFfsFileToFvInf (OutputFileNameList->FFSName, InfFile, TRUE);
-                            FirstInFlag = FALSE;
-                        } else {
-                            Status = LibAddFfsFileToFvInf (OutputFileNameList->FFSName, InfFile, FALSE);
-                        }
-                        if (EFI_ERROR (Status)) {
-                            Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
-                            fclose (InfFile);
-                            free (OutputFileNameList);
-                            free (ChildFileNameList);
-                            return Status;
-                        }
-                }
+          for (Index = 0; Index < FvInFd->FfsNumbers; Index++) {
+          if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == FALSE){
+              break;
+          }
+          if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == TRUE){
+            if (Index == EncapFvIndex) {
+              if (FirstInFlag) {   
+                Status = LibAddFfsFileToFvInf (OutputFileNameList->FFSName, InfFile, TRUE);
+                FirstInFlag = FALSE;
+              } else {
+                Status = LibAddFfsFileToFvInf (OutputFileNameList->FFSName, InfFile, FALSE);
+              }
+              if (EFI_ERROR (Status)) {
+                Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
+                fclose (InfFile);
+                free (OutputFileNameList);
+                free (ChildFileNameList);
+                return Status;
+              }
             }
+          }
 
-            NewFileNameList = FvInFd->ChildFvFFS;
-            while (NewFileNameList != NULL && NewFileNameList -> FFSName != NULL) {
-                if (NewFileNameList -> ParentLevel == ParentLevel && Index == NewFileNameList->InFvId && NewFileNameList->FfsFoundFlag==TRUE) {
-                    if (FirstInFlag) {
-                        Status = LibAddFfsFileToFvInf (NewFileNameList->FFSName, InfFile, TRUE);
-                        FirstInFlag = FALSE;
-                    } else {
-                        Status = LibAddFfsFileToFvInf (NewFileNameList->FFSName, InfFile, FALSE);
-                    }
-                    if (EFI_ERROR (Status)) {
-                        Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
-                        fclose (InfFile);
-                        free (OutputFileNameList);
-                        free (ChildFileNameList);
-                        return Status;
-                    }
-                  NewFileNameList->FfsFoundFlag = FALSE;
-                }
-                NewFileNameList = NewFileNameList->Next;
+          NewFileNameList = FvInFd->ChildFvFFS;
+          while (NewFileNameList != NULL && NewFileNameList -> FFSName != NULL) {
+            if (NewFileNameList -> ParentLevel == ParentLevel && Index == NewFileNameList->InFvId && NewFileNameList->FfsFoundFlag==TRUE) {
+              if (FirstInFlag) {
+                Status = LibAddFfsFileToFvInf (NewFileNameList->FFSName, InfFile, TRUE);
+                FirstInFlag = FALSE;
+              } else {
+                Status = LibAddFfsFileToFvInf (NewFileNameList->FFSName, InfFile, FALSE);
+              }
+              if (EFI_ERROR (Status)) {
+                Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
+                fclose (InfFile);
+                free (OutputFileNameList);
+                free (ChildFileNameList);
+                return Status;
+              }
+              NewFileNameList->FfsFoundFlag = FALSE;
             }
+            NewFileNameList = NewFileNameList->Next;
+          }
 
-            if (FvInFd->FfsAttuibutes[Index].IsHandle==TRUE) {
-                continue;
-            }
-            if (SubFvId > 0 && Index < SubFvId) {
-                continue;
-            }
+          if (FvInFd->FfsAttuibutes[Index].IsHandle==TRUE) {
+            continue;
+          }
+          if (SubFvId > 0 && Index < SubFvId) {
+            continue;
+          }
 
           //
           // For the last level FFS, the level below FFSs we should not care the IsLeaf Flag.
           //
           if (IsLastLevelFfs) {
             IsLeafFlagIgnore = TRUE;
-            } else {
-              IsLeafFlagIgnore = FvInFd->FfsAttuibutes[Index].IsLeaf;
-            }
+          } else {
+            IsLeafFlagIgnore = FvInFd->FfsAttuibutes[Index].IsLeaf;
+          }
 
           if (FvInFd->FfsAttuibutes[Index].FvId == LocalEncapData->FvId  && FvInFd->FfsAttuibutes[Index].Level >= ParentLevel + 1 && IsLeafFlagIgnore) {
             if (FirstInFlag) {
-        if (FvInFd->FfsAttuibutes[Index].Level < 0xFF) {
-          FfsFoundFlag = TRUE;
-          Status = LibAddFfsFileToFvInf (FvInFd->FfsAttuibutes[Index].FfsName, InfFile, TRUE);
-          FirstInFlag = FALSE;
-          FvInFd->FfsAttuibutes[Index].IsHandle=TRUE;
-          EncapFvStart = Index;
-        }
+              if (FvInFd->FfsAttuibutes[Index].Level < 0xFF) {
+                FfsFoundFlag = TRUE;
+                Status = LibAddFfsFileToFvInf (FvInFd->FfsAttuibutes[Index].FfsName, InfFile, TRUE);
+                FirstInFlag = FALSE;
+                FvInFd->FfsAttuibutes[Index].IsHandle=TRUE;
+                EncapFvStart = Index;
+              }
 
               if (EFI_ERROR (Status)) {
-                  Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
-                  fclose (InfFile);
-                  free (OutputFileNameList);
-                  free (ChildFileNameList);
-                  return Status;
+                Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
+                fclose (InfFile);
+                free (OutputFileNameList);
+                free (ChildFileNameList);
+                return Status;
               }
               if (Index == 0) {
                 // Root FV need to include all FFS files.
@@ -4586,33 +4579,32 @@ LibEncapNewFvFile(
             }
 
 
-      //avoid a FV contain too many ffs files
-           if ((!IsRootFv) && (FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) && (FvInFd->FfsAttuibutes[Index+1].FvLevel <= FvInFd->MulFvLevel) &&
-               (FvInFd->FfsAttuibutes[Index].FvLevel != FvInFd->FfsAttuibutes[Index+1].FvLevel) && (ParentLevel != 1) && (FvInFd->FfsAttuibutes[Index].Level != FvInFd->FfsAttuibutes[Index+1].Level) &&
-               FvInFd->FfsAttuibutes[Index].Level != 0xFF && FvInFd->FfsAttuibutes[Index+1].Level != 0xFF && FvInFd->FfsAttuibutes[Index+1].Level != 0x0){
-        FvInFd->FfsAttuibutes[Index].Level = 0;
-        break;
-      }else{
-        if (FvInFd->FfsAttuibutes[Index].Level != 0xFF){
-          FvInFd->FfsAttuibutes[Index].Level = 0;
-        }
-      }
-
+            //avoid a FV contain too many ffs files
+            if ((!IsRootFv) && (FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) && (FvInFd->FfsAttuibutes[Index+1].FvLevel <= FvInFd->MulFvLevel) &&
+              (FvInFd->FfsAttuibutes[Index].FvLevel != FvInFd->FfsAttuibutes[Index+1].FvLevel) && (ParentLevel != 1) && (FvInFd->FfsAttuibutes[Index].Level != FvInFd->FfsAttuibutes[Index+1].Level) &&
+              FvInFd->FfsAttuibutes[Index].Level != 0xFF && FvInFd->FfsAttuibutes[Index+1].Level != 0xFF && FvInFd->FfsAttuibutes[Index+1].Level != 0x0){
+              FvInFd->FfsAttuibutes[Index].Level = 0;
+              break;
+            } else {
+              if (FvInFd->FfsAttuibutes[Index].Level != 0xFF){
+                FvInFd->FfsAttuibutes[Index].Level = 0;
+              }
             }
           }
-       // The Fv may has multiple level (> 2), when it is in the FvLevel == 2, we set the IsLastLevelFfs Flag
-       if (Index <=FvInFd->FfsNumbers && FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) {
-           if (FvInFd->FfsAttuibutes[Index].FvLevel == 2) {
-               IsLastLevelFfs = FALSE;
-           }
-       }
-    if (!FfsFoundFlag){
-      OutputFileNameListFlag = FALSE;
-      if (OuterIndex > 0){
-        fclose (InfFile);
-        break;
-      }
-    }
+        }
+        // The Fv may has multiple level (> 2), when it is in the FvLevel == 2, we set the IsLastLevelFfs Flag
+        if (Index <=FvInFd->FfsNumbers && FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) {
+          if (FvInFd->FfsAttuibutes[Index].FvLevel == 2) {
+            IsLastLevelFfs = FALSE;
+          }
+        }
+        if (!FfsFoundFlag){
+          OutputFileNameListFlag = FALSE;
+          if (OuterIndex > 0){
+            fclose (InfFile);
+            break;
+          }
+        }
         //
         // Create FV
         //
@@ -4631,15 +4623,15 @@ LibEncapNewFvFile(
           return Status;
         }
 
-    OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
-    if (OutputFileNameList->FFSName == NULL) {
-      Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
-      return EFI_OUT_OF_RESOURCES;
-    }
-    memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
-      if (CurrentEncapData != NULL) {
-        OutputFileNameList->InFvId = EncapFvIndex;
-        if (EncapFvIndex > 0) {
+        OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
+        if (OutputFileNameList->FFSName == NULL) {
+          Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
+          return EFI_OUT_OF_RESOURCES;
+        }
+        memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
+        if (CurrentEncapData != NULL) {
+          OutputFileNameList->InFvId = EncapFvIndex;
+          if (EncapFvIndex > 0) {
             memcpy(OutputFileNameList->UiName,FvInFd->FfsAttuibutes[EncapFvIndex - 1].UiName, FvInFd->FfsAttuibutes[EncapFvIndex - 1].UiNameSize);
             OutputFileNameList->UiNameSize = FvInFd->FfsAttuibutes[EncapFvIndex - 1].UiNameSize;
             OutputFileNameList->Depex = FvInFd->FfsAttuibutes[EncapFvIndex - 1].Depex;
@@ -4647,9 +4639,9 @@ LibEncapNewFvFile(
             OutputFileNameList->FfsFoundFlag = FfsFoundFlag;
             OutputFileNameList->FvId = FvInFd->FfsAttuibutes[EncapFvIndex - 1].FvId;
             OutputFileNameList->ParentLevel = ParentLevel - 1;
+          }
+        }
         }
-      }
-    }
         break;
       case FMMT_ENCAP_TREE_FFS:
 
@@ -4662,13 +4654,13 @@ LibEncapNewFvFile(
           }
           while (LocalEncapData->NextNode != NULL) {
             if (LocalEncapData->Level == ParentLevel) {
-        for(;LocalEncapData->NextNode != NULL;) {
-          if(LocalEncapData->FvExtHeader != NULL) {
-            break;
-          }
-          LocalEncapData = LocalEncapData->NextNode;
-        }
-                break;
+              for(;LocalEncapData->NextNode != NULL;) {
+                if(LocalEncapData->FvExtHeader != NULL) {
+                  break;
+                }
+                LocalEncapData = LocalEncapData->NextNode;
+              }
+              break;
             }
             LocalEncapData = LocalEncapData->NextNode;
           }
@@ -4728,101 +4720,106 @@ LibEncapNewFvFile(
              InputFileName = TmpFileName;
           }
           if (OutputFileNameList->DepexLen > 0) {
-              TmpFileName = LibFilenameStrExtended(strrchr(GenTempFile (), OS_SEP), TemDir, "tmp");
-              TmpFile = fopen(TmpFileName, "wb+");
-              if (TmpFile == NULL) {
-                  Error("FMMT", 0, 0004, "Could not open tmp file %s to store Depex section information! \n", "");
-                  free (OutputFileNameList);
-                  free (ChildFileNameList);
-                  return EFI_ABORTED;
-              }
-              InputFile = fopen(InputFileName, "rb+");
-              if (InputFile == NULL) {
-                Error("FMMT", 0, 0004, "Could not open input file %s! \n", "");
+            TmpFileName = LibFilenameStrExtended(strrchr(GenTempFile (), OS_SEP), TemDir, "tmp");
+            TmpFile = fopen(TmpFileName, "wb+");
+            if (TmpFile == NULL) {
+                Error("FMMT", 0, 0004, "Could not open tmp file %s to store Depex section information! \n", "");
+                free (OutputFileNameList);
+                free (ChildFileNameList);
+                return EFI_ABORTED;
+            }
+            InputFile = fopen(InputFileName, "rb+");
+            if (InputFile == NULL) {
+              Error("FMMT", 0, 0004, "Could not open input file %s! \n", "");
+              fclose(TmpFile);
+              free (OutputFileNameList);
+              free (ChildFileNameList);
+              return EFI_ABORTED;
+            }
+            fseek(InputFile, 0, SEEK_SET);
+            fseek(InputFile, 0, SEEK_END);
+            InputFileSize = ftell(InputFile);
+            fseek(InputFile, 0, SEEK_SET);
+            // make sure the section is 4 byte align
+            if (OutputFileNameList->DepexLen % 4 != 0) {
+                AlignN = 4 - OutputFileNameList->DepexLen % 4;
+            }
+            Buffer = malloc(InputFileSize + OutputFileNameList->DepexLen + AlignN);
+            memcpy(Buffer, OutputFileNameList->Depex, OutputFileNameList->DepexLen);
+            if (AlignN != 0) {
+                for (Index = 0; Index < AlignN; Index ++) {
+                    memcpy(Buffer + OutputFileNameList->DepexLen + Index, AlignV, 1);
+                }
+            }
+            if (fread(Buffer + OutputFileNameList->DepexLen + AlignN, 1, InputFileSize, InputFile) != InputFileSize) {
+                Error("FMMT", 0, 0004, "Could not open sec file %s to add Depex section information! \n", "");
                 fclose(TmpFile);
+                fclose(InputFile);
+                free(Buffer);
                 free (OutputFileNameList);
                 free (ChildFileNameList);
                 return EFI_ABORTED;
+            }
+            fwrite(Buffer, 1, InputFileSize + OutputFileNameList->DepexLen + AlignN, TmpFile);
+            free(Buffer);
+            fclose(TmpFile);
+            fclose(InputFile);
+            InputFileName = TmpFileName;
+          }
+          //
+          // Delete origin FFS
+          //
+          for (Id = FvInFd->FfsNumbers; Id <= FvInFd->FfsNumbers; Id--) {
+            if ((memcmp(&FvInFd->FfsAttuibutes[Id].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)){
+              if (access(FvInFd->FfsAttuibutes[Id].FfsName, 0) != -1) {
+                Status = LibFmmtDeleteFile(FvInFd->FfsAttuibutes[Id].FfsName);
+                if (EFI_ERROR(Status)) {
+                    Error("FMMT", 0, 0004, "error while encapsulate FD Image", "Delete the specified file failed!");
+                    free (OutputFileNameList);
+                    free (ChildFileNameList);
+                    return Status;
+                }
+                memset(FvInFd->FfsAttuibutes[Id].FfsName, '\0', _MAX_PATH);
+                FvInFd->FfsAttuibutes[Id].Level = 0xFF;
+                break;
               }
-              fseek(InputFile, 0, SEEK_SET);
-              fseek(InputFile, 0, SEEK_END);
-              InputFileSize = ftell(InputFile);
-              fseek(InputFile, 0, SEEK_SET);
-              // make sure the section is 4 byte align
-              if (OutputFileNameList->DepexLen % 4 != 0) {
-                  AlignN = 4 - OutputFileNameList->DepexLen % 4;
-              }
-              Buffer = malloc(InputFileSize + OutputFileNameList->DepexLen + AlignN);
-              memcpy(Buffer, OutputFileNameList->Depex, OutputFileNameList->DepexLen);
-              if (AlignN != 0) {
-                  for (Index = 0; Index < AlignN; Index ++) {
-                      memcpy(Buffer + OutputFileNameList->DepexLen + Index, AlignV, 1);
-                  }
-              }
-              if (fread(Buffer + OutputFileNameList->DepexLen + AlignN, 1, InputFileSize, InputFile) != InputFileSize) {
-                  Error("FMMT", 0, 0004, "Could not open sec file %s to add Depex section information! \n", "");
-                  fclose(TmpFile);
-                  fclose(InputFile);
-                  free(Buffer);
-                  free (OutputFileNameList);
-                  free (ChildFileNameList);
-                  return EFI_ABORTED;
-              }
-              fwrite(Buffer, 1, InputFileSize + OutputFileNameList->DepexLen + AlignN, TmpFile);
-              free(Buffer);
-              fclose(TmpFile);
-              fclose(InputFile);
-              InputFileName = TmpFileName;
-         }
-         for (Id = FvInFd->FfsNumbers; Id <= FvInFd->FfsNumbers; Id--) {
-             if ((memcmp(&FvInFd->FfsAttuibutes[Id].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)){
-                 if (access(FvInFd->FfsAttuibutes[Id].FfsName, 0) != -1) {
-                     Status = LibFmmtDeleteFile(FvInFd->FfsAttuibutes[Id].FfsName);
-                     if (EFI_ERROR(Status)) {
-                         Error("FMMT", 0, 0004, "error while encapsulate FD Image", "Delete the specified file failed!");
-                         free (OutputFileNameList);
-                         free (ChildFileNameList);
-                         return Status;
-                     }
-                     memset(FvInFd->FfsAttuibutes[Id].FfsName, '\0', _MAX_PATH);
-                     FvInFd->FfsAttuibutes[Id].Level = 0xFF;
-                     break;
-                 }
-             }
-         }
-         if (LocalEncapData->NextNode != NULL) {
-             LocalEncapDataTemp = LocalEncapData->NextNode;
-             if ((LocalEncapDataTemp->Type == FMMT_ENCAP_TREE_GUIDED_SECTION) || (LocalEncapDataTemp->Type == FMMT_ENCAP_TREE_COMPRESS_SECTION)) {
-                 Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, "1");
-             }
-             else{
-                 Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, FvInFd->AlignmentStr);
-             }
-         }
-         else{
-             Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, FvInFd->AlignmentStr);
-         }
-      if (EFI_ERROR (Status)) {
-        Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FFS file failed!");
-        free (OutputFileNameList);
-        free (ChildFileNameList);
-        return Status;
-      }
-      free(LocalEncapData->FvExtHeader);
-      LocalEncapData->FvExtHeader = NULL;
-      OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
-      if (OutputFileNameList->FFSName == NULL) {
-          Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
-          return EFI_OUT_OF_RESOURCES;
-      }
-      memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
-      OutputFileNameList->IsFFS = TRUE;
-      OutputFileNameList->ParentLevel = ParentLevel - 1;
-      if (OutputFileNameList->Next == NULL){
-          break;
-      }
-      OutputFileNameList = OutputFileNameList->Next;
-    }
+            }
+          }
+          if (LocalEncapData->NextNode != NULL) {
+            LocalEncapDataTemp = LocalEncapData->NextNode;
+            if ((LocalEncapDataTemp->Type == FMMT_ENCAP_TREE_GUIDED_SECTION) || (LocalEncapDataTemp->Type == FMMT_ENCAP_TREE_COMPRESS_SECTION)) {
+              Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, "1");
+            } else{
+              Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, FvInFd->AlignmentStr);
+            }
+          } else{
+            Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, FvInFd->AlignmentStr);
+          }
+
+          if (EFI_ERROR (Status)) {
+            Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FFS file failed!");
+            free (OutputFileNameList);
+            free (ChildFileNameList);
+            return Status;
+          }
+          
+          free(LocalEncapData->FvExtHeader);
+          LocalEncapData->FvExtHeader = NULL;
+          
+          OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
+          if (OutputFileNameList->FFSName == NULL) {
+            Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
+            return EFI_OUT_OF_RESOURCES;
+          }
+          memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
+          OutputFileNameList->IsFFS = TRUE;
+          OutputFileNameList->ParentLevel = ParentLevel - 1;
+          OutputFileNameList->InFvId = Id;
+          if (OutputFileNameList->Next == NULL){
+            break;
+          }
+          OutputFileNameList = OutputFileNameList->Next;
+        }
         break;
       case FMMT_ENCAP_TREE_GUIDED_SECTION:
       while(OutputFileNameList!= NULL && OutputFileNameList->FFSName != NULL){
@@ -4941,25 +4938,25 @@ LibEncapNewFvFile(
         if (OutputFileSize > LargeFileSize) {
             IsLargeFile = TRUE;
         }
-    OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
-    if (OutputFileNameList->FFSName == NULL) {
-      Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
-      return EFI_OUT_OF_RESOURCES;
-    }
-    memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
-
-        if (EFI_ERROR (Status)) {
-          Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate guided section failed!");
-          free (OutputFileNameList);
-          free (ChildFileNameList);
-          return Status;
+        OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
+        if (OutputFileNameList->FFSName == NULL) {
+          Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
+          return EFI_OUT_OF_RESOURCES;
+        }
+        memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
+        
+         if (EFI_ERROR (Status)) {
+           Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate guided section failed!");
+           free (OutputFileNameList);
+           free (ChildFileNameList);
+           return Status;
+         }
+         OutputFileNameList->ParentLevel = ParentLevel - 1;
+         if (OutputFileNameList->Next == NULL) {
+           break;
+         }
+         OutputFileNameList = OutputFileNameList->Next;
         }
-        OutputFileNameList->ParentLevel = ParentLevel - 1;
-        if (OutputFileNameList->Next == NULL){
-          break;
-       }
-       OutputFileNameList = OutputFileNameList->Next;
-    }
         break;
       case FMMT_ENCAP_TREE_COMPRESS_SECTION:
         while(OutputFileNameList!= NULL && OutputFileNameList->FFSName != NULL){
@@ -5000,89 +4997,86 @@ LibEncapNewFvFile(
         break;
       case FMMT_ENCAP_TREE_FV_SECTION:
         while(OutputFileNameList!= NULL && OutputFileNameList->FFSName != NULL){
-        InputFileName  = OutputFileNameList->FFSName;
-        OutputFileName= LibFilenameStrExtended (strrchr(GenTempFile (), OS_SEP), TemDir, "sec");
-
-        Status = LibCreateFfsSection(NULL, InputFileName, NULL, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, OutputFileName, NULL, NULL, NULL, 0, 0, NULL);
+          InputFileName  = OutputFileNameList->FFSName;
+          OutputFileName= LibFilenameStrExtended (strrchr(GenTempFile (), OS_SEP), TemDir, "sec");
 
-        if (EFI_ERROR (Status)) {
-          Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV section failed!");
-          free (OutputFileNameList);
-          free (ChildFileNameList);
-          return Status;
-        }
+          Status = LibCreateFfsSection(NULL, InputFileName, NULL, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, OutputFileName, NULL, NULL, NULL, 0, 0, NULL);
 
-        InputFileName  = OutputFileName;
-        OutputFileName= LibFilenameStrExtended (strrchr(GenTempFile (), OS_SEP), TemDir, "sec");
+          if (EFI_ERROR (Status)) {
+            Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV section failed!");
+            free (OutputFileNameList);
+            free (ChildFileNameList);
+            return Status;
+          }
 
-        //
-        // Make it alignment.
-        //
-        Status = LibCreateFfsSection(FvInFd, InputFileName, NULL, 0, OutputFileName, NULL, NULL, NULL, 0, 0, NULL);
-        OutFile = fopen(OutputFileName, "rb+");
-        if (OutFile == NULL) {
+          InputFileName  = OutputFileName;
+          OutputFileName= LibFilenameStrExtended (strrchr(GenTempFile (), OS_SEP), TemDir, "sec");
+          
+          //
+          // Make it alignment.
+          //
+          Status = LibCreateFfsSection(FvInFd, InputFileName, NULL, 0, OutputFileName, NULL, NULL, NULL, 0, 0, NULL);
+          OutFile = fopen(OutputFileName, "rb+");
+          if (OutFile == NULL) {
             Error("FMMT", 0, 0004, "Could not open the file %s! \n", "");
             free (OutputFileNameList);
             free (ChildFileNameList);
             return EFI_ABORTED;
-        }
-        fseek(OutFile, 0, SEEK_SET);
-        fseek(OutFile, 0, SEEK_END);
-        OutputFileSize = ftell(OutFile);
-        fclose(OutFile);
-        if (OutputFileSize > LargeFileSize) {
+          }
+          fseek(OutFile, 0, SEEK_SET);
+          fseek(OutFile, 0, SEEK_END);
+          OutputFileSize = ftell(OutFile);
+          fclose(OutFile);
+          if (OutputFileSize > LargeFileSize) {
             IsLargeFile = TRUE;
+          }
+          
+          OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
+          if (OutputFileNameList->FFSName == NULL) {
+            Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
+            return EFI_OUT_OF_RESOURCES;
+          }
+          memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
+          
+          if (EFI_ERROR (Status)) {
+            Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV section failed!");
+            free (OutputFileNameList);
+            free (ChildFileNameList);
+            return Status;
+          }
+          OutputFileNameList->ParentLevel = ParentLevel - 1;
+          if (OutputFileNameList->Next == NULL){
+             break;
+          }
+          OutputFileNameList = OutputFileNameList->Next;
         }
-
-    OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
-    if (OutputFileNameList->FFSName == NULL) {
-      Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
-      return EFI_OUT_OF_RESOURCES;
-    }
-    memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
-
-        if (EFI_ERROR (Status)) {
-          Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV section failed!");
-          free (OutputFileNameList);
-          free (ChildFileNameList);
-          return Status;
-        }
-        OutputFileNameList->ParentLevel = ParentLevel - 1;
-        if (OutputFileNameList->Next == NULL){
-           break;
-        }
-        OutputFileNameList = OutputFileNameList->Next;
-    }
         break;
       default:
         for (Id = FvInFd->FfsNumbers; Id <= FvInFd->FfsNumbers; Id--) {
-            if ((memcmp(&FvInFd->FfsAttuibutes[Id].GuidName, &(CurrentEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)){
-                FvInFd->FfsAttuibutes[Id].IsHandle = TRUE;
-                memcpy(OutputFileNameList->UiName, FvInFd->FfsAttuibutes[Id].UiName, FvInFd->FfsAttuibutes[Id].UiNameSize);
-                OutputFileNameList->UiNameSize = FvInFd->FfsAttuibutes[Id].UiNameSize;
-                OutputFileNameList->FFSName = FvInFd->FfsAttuibutes[Id].FfsName;
-                OutputFileNameList->Depex = FvInFd->FfsAttuibutes[Id].Depex;
-                OutputFileNameList->DepexLen = FvInFd->FfsAttuibutes[Id].DepexLen;
-                OutputFileNameList->FfsFoundFlag = TRUE;
-                OutputFileNameList->IsFFS = TRUE;
-                OutputFileNameList->InFvId = Id;
-                OutputFileNameList->FvId = FvInFd->FfsAttuibutes[Id].FvId;
-                OutputFileNameList->ParentLevel = ParentLevel - 1;
-                *OutputFile = OutputFileNameList;
-                return EFI_SUCCESS;
-            }
+          if ((memcmp(&FvInFd->FfsAttuibutes[Id].GuidName, &(CurrentEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)){
+            FvInFd->FfsAttuibutes[Id].IsHandle = TRUE;
+            memcpy(OutputFileNameList->UiName, FvInFd->FfsAttuibutes[Id].UiName, FvInFd->FfsAttuibutes[Id].UiNameSize);
+            OutputFileNameList->UiNameSize = FvInFd->FfsAttuibutes[Id].UiNameSize;
+            OutputFileNameList->FFSName = FvInFd->FfsAttuibutes[Id].FfsName;
+            OutputFileNameList->Depex = FvInFd->FfsAttuibutes[Id].Depex;
+            OutputFileNameList->DepexLen = FvInFd->FfsAttuibutes[Id].DepexLen;
+            OutputFileNameList->FfsFoundFlag = TRUE;
+            OutputFileNameList->IsFFS = TRUE;
+            OutputFileNameList->InFvId = Id;
+            OutputFileNameList->FvId = FvInFd->FfsAttuibutes[Id].FvId;
+            OutputFileNameList->ParentLevel = ParentLevel - 1;
+            *OutputFile = OutputFileNameList;
+            return EFI_SUCCESS;
+          }
         }
     }
 
     if (CurrentEncapData == NULL) {
        LocalEncapData = FvInFd->EncapData;
     } else {
-        if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == TRUE) {
-          *OutputFile = OutputFileNameList;
-          return EFI_SUCCESS;
-        }
         LocalEncapData = CurrentEncapData;
     }
+    
     ParentLevel -= 1;
 
     while (LocalEncapData->NextNode != NULL) {
-- 
2.16.2.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#68034): https://edk2.groups.io/g/devel/message/68034
Mute This Topic: https://groups.io/mt/78525275/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-


Re: [edk2-devel] [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
Posted by Bob Feng 3 years, 4 months ago
Still have other patch...

    ...MMT-Replace-file-failure-when-FV-level-ov.patch | 456 ++++++++++++++

Thanks,
Bob

-----Original Message-----
From: Yeh, GregX <gregx.yeh@intel.com> 
Sent: Friday, November 27, 2020 9:38 AM
To: devel@edk2.groups.io
Cc: Feng, Bob C <bob.c.feng@intel.com>; Liming Gao <gaoliming@byosoft.com.cn>
Subject: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2

Fixed replace file failure when FFS in multi level FV and FV level over 2.

Signed-off-by: GregX Yeh <gregx.yeh@intel.com>
Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
---
 ...MMT-Replace-file-failure-when-FV-level-ov.patch | 456 ++++++++++++++
 BaseTools/Source/C/FMMT/FirmwareModuleManagement.h |   4 +-
 BaseTools/Source/C/FMMT/FmmtLib.c                  | 654 ++++++++++-----------
 3 files changed, 782 insertions(+), 332 deletions(-)
 create mode 100644 0001-BaseTools-FMMT-Replace-file-failure-when-FV-level-ov.patch

diff --git a/0001-BaseTools-FMMT-Replace-file-failure-when-FV-level-ov.patch b/0001-BaseTools-FMMT-Replace-file-failure-when-FV-level-ov.patch
new file mode 100644
index 0000000000..e42770c9e4
--- /dev/null
+++ b/0001-BaseTools-FMMT-Replace-file-failure-when-FV-level-ov.patch
@@ -0,0 +1,456 @@
+From eba4dd0899b011b0ac89723876be66a9d3f39251 Mon Sep 17 00:00:00 2001
+From: GregX Yeh <gregx.yeh@intel.com>
+Date: Tue, 3 Nov 2020 10:57:00 +0800
+Subject: [PATCH] BaseTools/FMMT: Replace file failure when FV level over 2
+
+Fixed replace file failure when FFS in multi level FV and FV level over 2.
+
+Signed-off-by: GregX Yeh <gregx.yeh@intel.com>
+Cc: Bob Feng <bob.c.feng@intel.com>
+Cc: Liming Gao <gaoliming@byosoft.com.cn>
+---
+ BaseTools/Source/C/FMMT/FirmwareModuleManagement.c |   2 +-
+ BaseTools/Source/C/FMMT/FirmwareModuleManagement.h |   3 +-
+ BaseTools/Source/C/FMMT/FmmtLib.c                  | 171 ++++++++++++++-------
+ 3 files changed, 118 insertions(+), 58 deletions(-)
+
+diff --git a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.c b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.c
+index 38056153fb..dfad02838b 100644
+--- a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.c
++++ b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.c
+@@ -615,7 +615,7 @@ static UINT32 FindFile(FV_INFORMATION *FvInFd, UINT8 FvLevel, CHAR8 *File, UINT3
+     return 0;
+   }
+   LibAscii2Unicode(File, UIName);
+-  for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {
++  for (Index = 0; Index < FvInFd->FfsNumbers; Index++) {
+     //
+     // Compare the New File Name with UI Name of FFS
+     // NOTE: The UI Name is Unicode, but the New File Name is Ascii.
+diff --git a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
+index 9d09c9160a..da9eb1a4b9 100644
+--- a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
++++ b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
+@@ -2,7 +2,7 @@
+ 
+  Structures and functions declaration.
+ 
+- Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
++ Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+ 
+ **/
+@@ -284,6 +284,7 @@ typedef struct _FFS_INFOMATION{
+   UINT8                      *Depex;
+   UINT32                     DepexLen;
+   BOOLEAN                    FfsFoundFlag;
++  UINT8                      FvLevel;
+   struct _FFS_INFOMATION     *Next;
+ } FFS_INFORMATION;
+ 
+diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
+index b945e9b63d..9bccd634bb 100644
+--- a/BaseTools/Source/C/FMMT/FmmtLib.c
++++ b/BaseTools/Source/C/FMMT/FmmtLib.c
+@@ -523,7 +523,7 @@ LibReadFvHeader (
+            FvExtHeader->FvName.Data4[6],
+            FvExtHeader->FvName.Data4[7]);
+     LibExtractFvUiName(FvExtHeader, &FvUiName);
+-    if (FvUiName != NULL && FvLevel == 1) {
++    if (FvUiName != NULL && (FvLevel - 1) != 0) {
+       printf("%sFV UI Name:            %s\n\n", BlankSpace, FvUiName);
+     }
+     free(FvUiName);
+@@ -709,10 +709,10 @@ LibGenFfsFile (
+   free(FfsFileName);
+   FfsFileName = NULL;
+ 
+-  CurrentFv->FfsNumbers  = *FfsCount;
+-
+   *FfsCount += 1;
+ 
++  CurrentFv->FfsNumbers  = *FfsCount;
++
+   if (ErasePolarity) {
+     CurrentFile->State = (UINT8)~(CurrentFile->State);
+   }
+@@ -789,7 +789,8 @@ LibParseSection (
+   UINT8                  *FvCount,
+   BOOLEAN                ViewFlag,
+   BOOLEAN                ErasePolarity,
+-  BOOLEAN                *IsFfsGenerated
++  BOOLEAN                *IsFfsGenerated,
++  UINT32                 *FfsIndex
+   )
+ {
+   UINT32              ParsedLength;
+@@ -1147,19 +1148,21 @@ LibParseSection (
+         return EFI_SECTION_ERROR;
+       }
+ 
+-      Status = LibParseSection (  UncompressedBuffer,
+-                                  UncompressedLength,
+-                                  CurrentFv,
+-                                  FvName,
+-                                  CurrentFile,
+-                                  Level,
+-                                  &LocalEncapData,
+-                                  FfsLevel,
+-                                  FfsCount,
+-                                  FvCount,
+-                                  ViewFlag,
+-                                  ErasePolarity,
+-                                  IsFfsGenerated);
++      Status = LibParseSection (UncompressedBuffer,
++                                UncompressedLength,
++                                CurrentFv,
++                                FvName,
++                                CurrentFile,
++                                Level,
++                                &LocalEncapData,
++                                FfsLevel,
++                                FfsCount,
++                                FvCount,
++                                ViewFlag,
++                                ErasePolarity,
++                                IsFfsGenerated,
++                                FfsIndex
++                                );
+ 
+       if (CompressionType == EFI_STANDARD_COMPRESSION) {
+         //
+@@ -1216,7 +1219,8 @@ LibParseSection (
+                 FvCount,
+                 ViewFlag,
+                 ErasePolarity,
+-                IsFfsGenerated
++                IsFfsGenerated,
++                FfsIndex
+                 );
+         if (EFI_ERROR(Status)) {
+           Error(NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL);
+@@ -1471,7 +1475,8 @@ LibParseSection (
+                   FvCount,
+                   ViewFlag,
+                   ErasePolarity,
+-                  IsFfsGenerated
++                  IsFfsGenerated,
++                  FfsIndex
+                   );
+         if (EFI_ERROR (Status)) {
+           Error (NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL);
+@@ -1491,7 +1496,8 @@ LibParseSection (
+             FvCount,
+             ViewFlag,
+             ErasePolarity,
+-            IsFfsGenerated
++            IsFfsGenerated,
++            FfsIndex
+             );
+           if (ExtractionTool != NULL) {
+             free (ExtractionTool);
+@@ -1527,6 +1533,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+ 
+@@ -1538,6 +1545,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+ 
+@@ -1549,6 +1557,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+ 
+@@ -1560,6 +1569,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+       break;
+@@ -1571,6 +1581,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+       break;
+@@ -1582,6 +1593,7 @@ LibParseSection (
+         if (!*IsFfsGenerated) {
+           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
+           *IsFfsGenerated = TRUE;
++          *FfsIndex = *FfsCount;
+         }
+       }
+       break;
+@@ -1665,11 +1677,11 @@ LibParseSection (
+       // If Ffs file has been generated, then the FfsCount should decrease 1.
+       //
+       if (*IsFfsGenerated) {
+-        memcpy (CurrentFv->FfsAttuibutes[*FfsCount -1].UiName, UIName, UINameSize);
+-        CurrentFv->FfsAttuibutes[*FfsCount -1].UiNameSize = UINameSize;
++        memcpy (CurrentFv->FfsAttuibutes[*FfsIndex - 1].UiName, UIName, UINameSize);
++        CurrentFv->FfsAttuibutes[*FfsIndex - 1].UiNameSize = UINameSize;
+       } else {
+-        memcpy (CurrentFv->FfsAttuibutes[*FfsCount].UiName, UIName, UINameSize);
+-        CurrentFv->FfsAttuibutes[*FfsCount].UiNameSize = UINameSize;
++        memcpy (CurrentFv->FfsAttuibutes[*FfsIndex].UiName, UIName, UINameSize);
++        CurrentFv->FfsAttuibutes[*FfsIndex].UiNameSize = UINameSize;
+       }
+ 
+       HasDepexSection = FALSE;
+@@ -1682,7 +1694,9 @@ LibParseSection (
+     }
+ 
+     ParsedLength += SectionLength;
+-    FirstInFlag  = FALSE;
++    if (Type != EFI_SECTION_PEI_DEPEX && Type != EFI_SECTION_DXE_DEPEX) {
++      FirstInFlag  = FALSE;
++    }
+     //
+     // We make then next section begin on a 4-byte boundary
+     //
+@@ -1872,12 +1886,14 @@ LibGetFileInfo (
+   BOOLEAN             IsGeneratedFfs;
+   UINT32              FfsFileHeaderSize;
+   CHAR8               *BlankChar;
++  UINT32              FfsIndex;
+ 
+   Status = EFI_SUCCESS;
+ 
+   LocalEncapData  = NULL;
+   EncapDataNeedUpdateFlag = TRUE;
+   IsGeneratedFfs   = FALSE;
++  FfsIndex        = 0xFFFFFFFF;
+   BlankChar        = NULL;
+ 
+   FfsFileHeaderSize = GetFfsHeaderLength  ((EFI_FFS_FILE_HEADER *) CurrentFile);
+@@ -2083,24 +2099,25 @@ LibGetFileInfo (
+     } else if( CurrentFile->Type == EFI_FV_FILETYPE_FFS_PAD){
+       //EFI_FV_FILETYPE_FFS_PAD
+     } else {
+-    //
+-    // All other files have sections
+-    //
+-    Status = LibParseSection (
+-      (UINT8 *) ((UINTN) CurrentFile + FfsFileHeaderSize),
+-      FileLength - FfsFileHeaderSize,
+-      CurrentFv,
+-      FvName,
+-      CurrentFile,
+-      Level,
+-      &LocalEncapData,
+-      Level,
+-      FfsCount,
+-      FvCount,
+-      ViewFlag,
+-      ErasePolarity,
+-      &IsGeneratedFfs
+-      );
++      //
++      // All other files have sections
++      //
++      Status = LibParseSection (
++                 (UINT8 *) ((UINTN) CurrentFile + FfsFileHeaderSize),
++                 FileLength - FfsFileHeaderSize,
++                 CurrentFv,
++                 FvName,
++                 CurrentFile,
++                 Level,
++                 &LocalEncapData,
++                 Level,
++                 FfsCount,
++                 FvCount,
++                 ViewFlag,
++                 ErasePolarity,
++                 &IsGeneratedFfs,
++                 &FfsIndex
++                 );
+     }
+     if (EFI_ERROR (Status)) {
+       printf ("ERROR: Parsing the FFS file.\n");
+@@ -2144,6 +2161,7 @@ LibGetFvInfo (
+   EFI_FIRMWARE_VOLUME_EXT_HEADER *ExtHdrPtr;
+   EFI_FIRMWARE_VOLUME_HEADER *FvHdr;
+   UINT8                      PreFvId;
++  UINT8                       GuidBuffer[PRINTED_GUID_BUFFER_SIZE];
+ 
+   NumberOfFiles  = 0;
+   Key            = 0;
+@@ -2344,9 +2362,10 @@ LibGetFvInfo (
+ 
+     CurrentFv->FfsAttuibutes[*FfsCount].FvLevel = CurrentFv->FvLevel;
+     CurrentFv->FfsAttuibutes[*FfsCount].FvId    = PreFvId;
+-     if (CurrentFv->FvLevel > CurrentFv->MulFvLevel) {
++    if (CurrentFv->FvLevel > CurrentFv->MulFvLevel) {
+       CurrentFv->MulFvLevel = CurrentFv->FvLevel;
+-   }
++    }
++    PrintGuidToBuffer (&(CurrentFile->Name), GuidBuffer, PRINTED_GUID_BUFFER_SIZE, FALSE);
+     //
+     // Display info about this file
+     //
+@@ -4198,10 +4217,13 @@ LibEncapNewFvFile(
+   UINT32                      header;
+   UINT8                       AlignN;
+   UINT8                       AlignV[1] = {0xFF};
++  UINT32                      EntryFvId;
++
+   AlignN                      = 0;
+   Id                          = 0;
+   InputFileSize               = 0;
+   TmpFileSize                 = 0;
++  AlignmentFileSize           = 0;
+   EncapFvIndex                = 0;
+   Index                       = 0;
+   OuterIndex                  = 0;
+@@ -4224,7 +4246,7 @@ LibEncapNewFvFile(
+   IsLargeFile                 = FALSE;
+   OutputFileSize              = 0;
+   LargeFileSize               = 0x1000000;
+-
++  EntryFvId                   = 0;
+ 
+   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
+   if (OutputFileNameList == NULL) {
+@@ -4240,6 +4262,7 @@ LibEncapNewFvFile(
+   OutputFileNameList->Depex = NULL;
+   OutputFileNameList->DepexLen = 0;
+   OutputFileNameList->FfsFoundFlag = FALSE;
++  OutputFileNameList->FvLevel = 0;
+ 
+   ChildFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
+   if (ChildFileNameList == NULL) {
+@@ -4258,12 +4281,16 @@ LibEncapNewFvFile(
+   //
+   // Encapsulate from the lowest FFS file level.
+   //
+-    LocalEncapData = CurrentEncapData;
+-    if (LocalEncapData == NULL) {
+-        LocalEncapData = FvInFd->EncapData;
+-    }
+-    Level = LocalEncapData->Level;
+-    Type = LocalEncapData->Type;
++  LocalEncapData = CurrentEncapData;
++  if (LocalEncapData == NULL) {
++    LocalEncapData = FvInFd->EncapData;
++    EntryFvId = 0xFFFFFFFF;
++  } else {
++    EntryFvId = LocalEncapData->FvId;
++  }
++
++  Level = LocalEncapData->Level;
++  Type  = LocalEncapData->Type;
+ 
+   if (CurrentEncapData == NULL) {
+     LocalEncapData = FvInFd->EncapData;
+@@ -4278,6 +4305,7 @@ LibEncapNewFvFile(
+                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
+                 if (FvInFd->ChildFvFFS == NULL) {
+                     FvInFd->ChildFvFFS = ChildFileNameList;
++                    NewFileNameList = FvInFd->ChildFvFFS;
+                 } else {
+                     NewFileNameList = FvInFd->ChildFvFFS;
+                     while (NewFileNameList->Next != NULL) {
+@@ -4325,6 +4353,12 @@ LibEncapNewFvFile(
+         Type        = LocalEncapData->Type;
+       }
+       LocalEncapData = LocalEncapData->NextNode;
++      if (LocalEncapData == NULL) {
++        if (Type == FMMT_ENCAP_TREE_FV) {
++          ParentLevel = Level;
++          ParentType  = Type;
++        }
++      }
+     }
+   } else {
+     LocalEncapData = CurrentEncapData;
+@@ -4349,6 +4383,28 @@ LibEncapNewFvFile(
+             LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
+         }
+       }
++
++      if (LocalEncapData->FvId > EntryFvId && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
++        LocalEncapDataTemp = LocalEncapData->RightNode;
++
++        while (LocalEncapDataTemp != NULL) {
++          LocalEncapDataNext = LocalEncapDataTemp->NextNode;
++          if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
++            LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, LocalEncapDataTemp->Level - 1, &ChildFileNameList);
++            if (FvInFd->ChildFvFFS == NULL) {
++              FvInFd->ChildFvFFS = ChildFileNameList;
++            } else {
++              NewFileNameList = FvInFd->ChildFvFFS;
++              while (NewFileNameList->Next != NULL) {
++                NewFileNameList = NewFileNameList->Next;
++              }
++              NewFileNameList->Next = ChildFileNameList;
++            }
++          }
++          LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
++        }
++      }
++
+       if (LocalEncapData->Level > Level) {
+         if (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+           ParentLevel = Level;
+@@ -4485,12 +4541,12 @@ LibEncapNewFvFile(
+         //
+         FfsFoundFlag = FALSE;
+         IsRootFv = FALSE;
+-        for (Index=0; Index <= FvInFd->FfsNumbers; Index++) {
++        for (Index=0; Index < FvInFd->FfsNumbers; Index++) {
+             if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == FALSE){
+                 break;
+             }
+             if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == TRUE){
+-                if (Index == EncapFvIndex) {
++                if (Index == EncapFvStart) {
+                     if (FirstInFlag) {
+                             Status = LibAddFfsFileToFvInf (OutputFileNameList->FFSName, InfFile, TRUE);
+                             FirstInFlag = FALSE;
+@@ -4601,7 +4657,7 @@ LibEncapNewFvFile(
+             }
+           }
+        // The Fv may has multiple level (> 2), when it is in the FvLevel == 2, we set the IsLastLevelFfs Flag
+-       if (Index <=FvInFd->FfsNumbers && FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) {
++       if (Index < FvInFd->FfsNumbers && FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) {
+            if (FvInFd->FfsAttuibutes[Index].FvLevel == 2) {
+                IsLastLevelFfs = FALSE;
+            }
+@@ -4777,6 +4833,7 @@ LibEncapNewFvFile(
+          for (Id = FvInFd->FfsNumbers; Id <= FvInFd->FfsNumbers; Id--) {
+              if ((memcmp(&FvInFd->FfsAttuibutes[Id].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)){
+                  if (access(FvInFd->FfsAttuibutes[Id].FfsName, 0) != -1) {
++                     OutputFileNameList->FvLevel = FvInFd->FfsAttuibutes[Id].FvLevel;
+                      Status = LibFmmtDeleteFile(FvInFd->FfsAttuibutes[Id].FfsName);
+                      if (EFI_ERROR(Status)) {
+                          Error("FMMT", 0, 0004, "error while encapsulate FD Image", "Delete the specified file failed!");
+@@ -5079,7 +5136,9 @@ LibEncapNewFvFile(
+     } else {
+         if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == TRUE) {
+           *OutputFile = OutputFileNameList;
+-          return EFI_SUCCESS;
++          if (OutputFileNameList->FvLevel < 2) {
++            return EFI_SUCCESS;
++          }
+         }
+         LocalEncapData = CurrentEncapData;
+     }
+-- 
+2.16.2.windows.1
+
diff --git a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
index 9d09c9160a..ccc8ba00be 100644
--- a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
+++ b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
@@ -195,7 +195,7 @@ typedef struct {
   // UI Name for this FFS file, if has.
   //
   CHAR16           UiName[_MAX_PATH];
-   UINT32           UiNameSize;
+  UINT32           UiNameSize;
   //
   // Total section number in this FFS.
   //
@@ -208,7 +208,7 @@ typedef struct {
   //
   //Describe the belong to FV
   //
-  UINT8           FvId;
+  UINT8            FvId;
   //
   // If this FFS has no encapsulate section, this flag will set to True.
   //
diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..d6d43030fa 100644
--- a/BaseTools/Source/C/FMMT/FmmtLib.c
+++ b/BaseTools/Source/C/FMMT/FmmtLib.c
@@ -709,10 +709,10 @@ LibGenFfsFile (
   free(FfsFileName);
   FfsFileName = NULL;
 
-  CurrentFv->FfsNumbers  = *FfsCount;
-
   *FfsCount += 1;
 
+  CurrentFv->FfsNumbers  = *FfsCount;
+
   if (ErasePolarity) {
     CurrentFile->State = (UINT8)~(CurrentFile->State);
   }
@@ -833,7 +833,6 @@ LibParseSection (
   CHAR8               *ToolInputFileName;
   CHAR8               *ToolOutputFileName;
   BOOLEAN              HasUiSection;
-  BOOLEAN              FirstInFlag;
 
   DataOffset                 = 0;
   GuidAttr                   = 0;
@@ -869,7 +868,6 @@ LibParseSection (
   EncapDataNeedUpdata        = TRUE;
   LargeHeaderOffset          = 0;
   HasUiSection               = FALSE;
-  FirstInFlag                = TRUE;
 
 
   while (ParsedLength < BufferLength) {
@@ -997,9 +995,7 @@ LibParseSection (
       break;
 
     case EFI_SECTION_COMPRESSION:
-      if (FirstInFlag) {
-        Level ++;
-      }
+      Level ++;
       NumberOfSections ++;
 
       EncapDataNeedUpdata = TRUE;
@@ -1181,9 +1177,7 @@ LibParseSection (
       // looks up the appropriate tool to use for extracting
       // a GUID defined FV section.
       //
-      if (FirstInFlag) {
-        Level ++;
-      }
+      Level ++;
       NumberOfSections++;
       EncapDataNeedUpdata = TRUE;
       HasUiSection = TRUE;
@@ -1682,7 +1676,6 @@ LibParseSection (
     }
 
     ParsedLength += SectionLength;
-    FirstInFlag  = FALSE;
     //
     // We make then next section begin on a 4-byte boundary
     //
@@ -2016,7 +2009,7 @@ LibGetFileInfo (
 
       LocalEncapData->Level = Level;
       LocalEncapData->Type  = FMMT_ENCAP_TREE_FFS;
-    LocalEncapData->FvExtHeader = NULL;
+      LocalEncapData->FvExtHeader = NULL;
       LocalEncapData->Depex = NULL;
       LocalEncapData->DepexLen = 0;
       LocalEncapData->UiNameSize = 0;
@@ -4198,6 +4191,7 @@ LibEncapNewFvFile(
   UINT32                      header;
   UINT8                       AlignN;
   UINT8                       AlignV[1] = {0xFF};
+  
   AlignN                      = 0;
   Id                          = 0;
   InputFileSize               = 0;
@@ -4225,7 +4219,6 @@ LibEncapNewFvFile(
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
 
-
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
     Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
@@ -4258,12 +4251,12 @@ LibEncapNewFvFile(
   //
   // Encapsulate from the lowest FFS file level.
   //
-    LocalEncapData = CurrentEncapData;
-    if (LocalEncapData == NULL) {
-        LocalEncapData = FvInFd->EncapData;
-    }
-    Level = LocalEncapData->Level;
-    Type = LocalEncapData->Type;
+  LocalEncapData = CurrentEncapData;
+  if (LocalEncapData == NULL) {
+      LocalEncapData = FvInFd->EncapData;
+  }
+  Level = LocalEncapData->Level;
+  Type = LocalEncapData->Type;
 
   if (CurrentEncapData == NULL) {
     LocalEncapData = FvInFd->EncapData;
@@ -4271,22 +4264,21 @@ LibEncapNewFvFile(
       if (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
-            LocalEncapDataNext = LocalEncapDataTemp->NextNode;
-            if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
-
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
-                ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
-                if (FvInFd->ChildFvFFS == NULL) {
-                    FvInFd->ChildFvFFS = ChildFileNameList;
-                } else {
-                    NewFileNameList = FvInFd->ChildFvFFS;
-                    while (NewFileNameList->Next != NULL) {
-                        NewFileNameList = NewFileNameList->Next;
-                    }
-                    NewFileNameList->Next = ChildFileNameList;
+          LocalEncapDataNext = LocalEncapDataTemp->NextNode;
+          if(LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
+            LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, LocalEncapDataTemp->Level-1, &ChildFileNameList);
+            //ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
+            if (FvInFd->ChildFvFFS == NULL) {
+                FvInFd->ChildFvFFS = ChildFileNameList;
+            } else {
+                NewFileNameList = FvInFd->ChildFvFFS;
+                while (NewFileNameList->Next != NULL) {
+                    NewFileNameList = NewFileNameList->Next;
                 }
+                NewFileNameList->Next = ChildFileNameList;
             }
-            LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
+          }
+          LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
         }
       }
 
@@ -4329,24 +4321,24 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
-            LocalEncapDataNext = LocalEncapDataTemp->NextNode;
-            if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, LocalEncapDataTemp->Level-1, &ChildFileNameList);
-                //ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
-                if (FvInFd->ChildFvFFS == NULL) {
-                    FvInFd->ChildFvFFS = ChildFileNameList;
-                } else {
-                    NewFileNameList = FvInFd->ChildFvFFS;
-                    while (NewFileNameList->Next != NULL) {
-                        NewFileNameList = NewFileNameList->Next;
-                    }
-                    NewFileNameList->Next = ChildFileNameList;
+          LocalEncapDataNext = LocalEncapDataTemp->NextNode;
+          if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
+            LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, LocalEncapDataTemp->Level-1, &ChildFileNameList);
+            //ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
+            if (FvInFd->ChildFvFFS == NULL) {
+                FvInFd->ChildFvFFS = ChildFileNameList;
+            } else {
+                NewFileNameList = FvInFd->ChildFvFFS;
+                while (NewFileNameList->Next != NULL) {
+                    NewFileNameList = NewFileNameList->Next;
                 }
+                NewFileNameList->Next = ChildFileNameList;
             }
-            LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
+          }
+          LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
         }
       }
       if (LocalEncapData->Level > Level) {
@@ -4366,12 +4358,12 @@ LibEncapNewFvFile(
       case FMMT_ENCAP_TREE_FV:
         OutputFileNameListFlag = TRUE;
         EncapFvStart = 0;
-    for(OuterIndex=0;OutputFileNameListFlag;OuterIndex++){
+        for(OuterIndex=0;OutputFileNameListFlag;OuterIndex++){
         //
         // Generate FV.inf attributes.
         //
         InfFileName = LibFilenameStrExtended (strrchr(GenTempFile (),OS_SEP), TemDir, "inf");
-    FirstInFlag = TRUE;
+        FirstInFlag = TRUE;
 
         InfFile = fopen (InfFileName, "wt+");
 
@@ -4383,9 +4375,9 @@ LibEncapNewFvFile(
         }
 
         if (CurrentEncapData == NULL) {
-           LocalEncapData = FvInFd->EncapData;
+          LocalEncapData = FvInFd->EncapData;
         } else {
-            LocalEncapData = CurrentEncapData;
+          LocalEncapData = CurrentEncapData;
         }
 
         while (LocalEncapData->NextNode != NULL) {
@@ -4456,6 +4448,7 @@ LibEncapNewFvFile(
           free (ChildFileNameList);
           return Status;
         }
+
         if (LocalEncapData->FvExtHeader != NULL) {
           Status = LibGenExtFile(LocalEncapData->FvExtHeader, InfFile);
           if (EFI_ERROR(Status)) {
@@ -4473,93 +4466,93 @@ LibEncapNewFvFile(
           while (LocalEncapData->Level != ParentLevel) {
             LocalEncapData = LocalEncapData->NextNode;
           }
-            for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {
-                if ((memcmp(&FvInFd->FfsAttuibutes[Index].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)) {
-                    SubFvId = Index;
-                    break;
-                }
+          for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {
+            if ((memcmp(&FvInFd->FfsAttuibutes[Index].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)) {
+              SubFvId = Index;
+              break;
             }
+          }
         }
         //
         // Found FFSs from Fv structure.
         //
         FfsFoundFlag = FALSE;
         IsRootFv = FALSE;
-        for (Index=0; Index <= FvInFd->FfsNumbers; Index++) {
-            if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == FALSE){
-                break;
-            }
-            if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == TRUE){
-                if (Index == EncapFvIndex) {
-                    if (FirstInFlag) {
-                            Status = LibAddFfsFileToFvInf (OutputFileNameList->FFSName, InfFile, TRUE);
-                            FirstInFlag = FALSE;
-                        } else {
-                            Status = LibAddFfsFileToFvInf (OutputFileNameList->FFSName, InfFile, FALSE);
-                        }
-                        if (EFI_ERROR (Status)) {
-                            Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
-                            fclose (InfFile);
-                            free (OutputFileNameList);
-                            free (ChildFileNameList);
-                            return Status;
-                        }
-                }
+          for (Index = 0; Index < FvInFd->FfsNumbers; Index++) {
+          if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == FALSE){
+              break;
+          }
+          if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == TRUE){
+            if (Index == EncapFvIndex) {
+              if (FirstInFlag) {   
+                Status = LibAddFfsFileToFvInf (OutputFileNameList->FFSName, InfFile, TRUE);
+                FirstInFlag = FALSE;
+              } else {
+                Status = LibAddFfsFileToFvInf (OutputFileNameList->FFSName, InfFile, FALSE);
+              }
+              if (EFI_ERROR (Status)) {
+                Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
+                fclose (InfFile);
+                free (OutputFileNameList);
+                free (ChildFileNameList);
+                return Status;
+              }
             }
+          }
 
-            NewFileNameList = FvInFd->ChildFvFFS;
-            while (NewFileNameList != NULL && NewFileNameList -> FFSName != NULL) {
-                if (NewFileNameList -> ParentLevel == ParentLevel && Index == NewFileNameList->InFvId && NewFileNameList->FfsFoundFlag==TRUE) {
-                    if (FirstInFlag) {
-                        Status = LibAddFfsFileToFvInf (NewFileNameList->FFSName, InfFile, TRUE);
-                        FirstInFlag = FALSE;
-                    } else {
-                        Status = LibAddFfsFileToFvInf (NewFileNameList->FFSName, InfFile, FALSE);
-                    }
-                    if (EFI_ERROR (Status)) {
-                        Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
-                        fclose (InfFile);
-                        free (OutputFileNameList);
-                        free (ChildFileNameList);
-                        return Status;
-                    }
-                  NewFileNameList->FfsFoundFlag = FALSE;
-                }
-                NewFileNameList = NewFileNameList->Next;
+          NewFileNameList = FvInFd->ChildFvFFS;
+          while (NewFileNameList != NULL && NewFileNameList -> FFSName != NULL) {
+            if (NewFileNameList -> ParentLevel == ParentLevel && Index == NewFileNameList->InFvId && NewFileNameList->FfsFoundFlag==TRUE) {
+              if (FirstInFlag) {
+                Status = LibAddFfsFileToFvInf (NewFileNameList->FFSName, InfFile, TRUE);
+                FirstInFlag = FALSE;
+              } else {
+                Status = LibAddFfsFileToFvInf (NewFileNameList->FFSName, InfFile, FALSE);
+              }
+              if (EFI_ERROR (Status)) {
+                Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
+                fclose (InfFile);
+                free (OutputFileNameList);
+                free (ChildFileNameList);
+                return Status;
+              }
+              NewFileNameList->FfsFoundFlag = FALSE;
             }
+            NewFileNameList = NewFileNameList->Next;
+          }
 
-            if (FvInFd->FfsAttuibutes[Index].IsHandle==TRUE) {
-                continue;
-            }
-            if (SubFvId > 0 && Index < SubFvId) {
-                continue;
-            }
+          if (FvInFd->FfsAttuibutes[Index].IsHandle==TRUE) {
+            continue;
+          }
+          if (SubFvId > 0 && Index < SubFvId) {
+            continue;
+          }
 
           //
           // For the last level FFS, the level below FFSs we should not care the IsLeaf Flag.
           //
           if (IsLastLevelFfs) {
             IsLeafFlagIgnore = TRUE;
-            } else {
-              IsLeafFlagIgnore = FvInFd->FfsAttuibutes[Index].IsLeaf;
-            }
+          } else {
+            IsLeafFlagIgnore = FvInFd->FfsAttuibutes[Index].IsLeaf;
+          }
 
           if (FvInFd->FfsAttuibutes[Index].FvId == LocalEncapData->FvId  && FvInFd->FfsAttuibutes[Index].Level >= ParentLevel + 1 && IsLeafFlagIgnore) {
             if (FirstInFlag) {
-        if (FvInFd->FfsAttuibutes[Index].Level < 0xFF) {
-          FfsFoundFlag = TRUE;
-          Status = LibAddFfsFileToFvInf (FvInFd->FfsAttuibutes[Index].FfsName, InfFile, TRUE);
-          FirstInFlag = FALSE;
-          FvInFd->FfsAttuibutes[Index].IsHandle=TRUE;
-          EncapFvStart = Index;
-        }
+              if (FvInFd->FfsAttuibutes[Index].Level < 0xFF) {
+                FfsFoundFlag = TRUE;
+                Status = LibAddFfsFileToFvInf (FvInFd->FfsAttuibutes[Index].FfsName, InfFile, TRUE);
+                FirstInFlag = FALSE;
+                FvInFd->FfsAttuibutes[Index].IsHandle=TRUE;
+                EncapFvStart = Index;
+              }
 
               if (EFI_ERROR (Status)) {
-                  Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
-                  fclose (InfFile);
-                  free (OutputFileNameList);
-                  free (ChildFileNameList);
-                  return Status;
+                Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV inf file [files] section failed!");
+                fclose (InfFile);
+                free (OutputFileNameList);
+                free (ChildFileNameList);
+                return Status;
               }
               if (Index == 0) {
                 // Root FV need to include all FFS files.
@@ -4586,33 +4579,32 @@ LibEncapNewFvFile(
             }
 
 
-      //avoid a FV contain too many ffs files
-           if ((!IsRootFv) && (FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) && (FvInFd->FfsAttuibutes[Index+1].FvLevel <= FvInFd->MulFvLevel) &&
-               (FvInFd->FfsAttuibutes[Index].FvLevel != FvInFd->FfsAttuibutes[Index+1].FvLevel) && (ParentLevel != 1) && (FvInFd->FfsAttuibutes[Index].Level != FvInFd->FfsAttuibutes[Index+1].Level) &&
-               FvInFd->FfsAttuibutes[Index].Level != 0xFF && FvInFd->FfsAttuibutes[Index+1].Level != 0xFF && FvInFd->FfsAttuibutes[Index+1].Level != 0x0){
-        FvInFd->FfsAttuibutes[Index].Level = 0;
-        break;
-      }else{
-        if (FvInFd->FfsAttuibutes[Index].Level != 0xFF){
-          FvInFd->FfsAttuibutes[Index].Level = 0;
-        }
-      }
-
+            //avoid a FV contain too many ffs files
+            if ((!IsRootFv) && (FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) && (FvInFd->FfsAttuibutes[Index+1].FvLevel <= FvInFd->MulFvLevel) &&
+              (FvInFd->FfsAttuibutes[Index].FvLevel != FvInFd->FfsAttuibutes[Index+1].FvLevel) && (ParentLevel != 1) && (FvInFd->FfsAttuibutes[Index].Level != FvInFd->FfsAttuibutes[Index+1].Level) &&
+              FvInFd->FfsAttuibutes[Index].Level != 0xFF && FvInFd->FfsAttuibutes[Index+1].Level != 0xFF && FvInFd->FfsAttuibutes[Index+1].Level != 0x0){
+              FvInFd->FfsAttuibutes[Index].Level = 0;
+              break;
+            } else {
+              if (FvInFd->FfsAttuibutes[Index].Level != 0xFF){
+                FvInFd->FfsAttuibutes[Index].Level = 0;
+              }
             }
           }
-       // The Fv may has multiple level (> 2), when it is in the FvLevel == 2, we set the IsLastLevelFfs Flag
-       if (Index <=FvInFd->FfsNumbers && FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) {
-           if (FvInFd->FfsAttuibutes[Index].FvLevel == 2) {
-               IsLastLevelFfs = FALSE;
-           }
-       }
-    if (!FfsFoundFlag){
-      OutputFileNameListFlag = FALSE;
-      if (OuterIndex > 0){
-        fclose (InfFile);
-        break;
-      }
-    }
+        }
+        // The Fv may has multiple level (> 2), when it is in the FvLevel == 2, we set the IsLastLevelFfs Flag
+        if (Index <=FvInFd->FfsNumbers && FvInFd->FfsAttuibutes[Index].FvLevel <= FvInFd->MulFvLevel) {
+          if (FvInFd->FfsAttuibutes[Index].FvLevel == 2) {
+            IsLastLevelFfs = FALSE;
+          }
+        }
+        if (!FfsFoundFlag){
+          OutputFileNameListFlag = FALSE;
+          if (OuterIndex > 0){
+            fclose (InfFile);
+            break;
+          }
+        }
         //
         // Create FV
         //
@@ -4631,15 +4623,15 @@ LibEncapNewFvFile(
           return Status;
         }
 
-    OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
-    if (OutputFileNameList->FFSName == NULL) {
-      Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
-      return EFI_OUT_OF_RESOURCES;
-    }
-    memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
-      if (CurrentEncapData != NULL) {
-        OutputFileNameList->InFvId = EncapFvIndex;
-        if (EncapFvIndex > 0) {
+        OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
+        if (OutputFileNameList->FFSName == NULL) {
+          Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
+          return EFI_OUT_OF_RESOURCES;
+        }
+        memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
+        if (CurrentEncapData != NULL) {
+          OutputFileNameList->InFvId = EncapFvIndex;
+          if (EncapFvIndex > 0) {
             memcpy(OutputFileNameList->UiName,FvInFd->FfsAttuibutes[EncapFvIndex - 1].UiName, FvInFd->FfsAttuibutes[EncapFvIndex - 1].UiNameSize);
             OutputFileNameList->UiNameSize = FvInFd->FfsAttuibutes[EncapFvIndex - 1].UiNameSize;
             OutputFileNameList->Depex = FvInFd->FfsAttuibutes[EncapFvIndex - 1].Depex;
@@ -4647,9 +4639,9 @@ LibEncapNewFvFile(
             OutputFileNameList->FfsFoundFlag = FfsFoundFlag;
             OutputFileNameList->FvId = FvInFd->FfsAttuibutes[EncapFvIndex - 1].FvId;
             OutputFileNameList->ParentLevel = ParentLevel - 1;
+          }
+        }
         }
-      }
-    }
         break;
       case FMMT_ENCAP_TREE_FFS:
 
@@ -4662,13 +4654,13 @@ LibEncapNewFvFile(
           }
           while (LocalEncapData->NextNode != NULL) {
             if (LocalEncapData->Level == ParentLevel) {
-        for(;LocalEncapData->NextNode != NULL;) {
-          if(LocalEncapData->FvExtHeader != NULL) {
-            break;
-          }
-          LocalEncapData = LocalEncapData->NextNode;
-        }
-                break;
+              for(;LocalEncapData->NextNode != NULL;) {
+                if(LocalEncapData->FvExtHeader != NULL) {
+                  break;
+                }
+                LocalEncapData = LocalEncapData->NextNode;
+              }
+              break;
             }
             LocalEncapData = LocalEncapData->NextNode;
           }
@@ -4728,101 +4720,106 @@ LibEncapNewFvFile(
              InputFileName = TmpFileName;
           }
           if (OutputFileNameList->DepexLen > 0) {
-              TmpFileName = LibFilenameStrExtended(strrchr(GenTempFile (), OS_SEP), TemDir, "tmp");
-              TmpFile = fopen(TmpFileName, "wb+");
-              if (TmpFile == NULL) {
-                  Error("FMMT", 0, 0004, "Could not open tmp file %s to store Depex section information! \n", "");
-                  free (OutputFileNameList);
-                  free (ChildFileNameList);
-                  return EFI_ABORTED;
-              }
-              InputFile = fopen(InputFileName, "rb+");
-              if (InputFile == NULL) {
-                Error("FMMT", 0, 0004, "Could not open input file %s! \n", "");
+            TmpFileName = LibFilenameStrExtended(strrchr(GenTempFile (), OS_SEP), TemDir, "tmp");
+            TmpFile = fopen(TmpFileName, "wb+");
+            if (TmpFile == NULL) {
+                Error("FMMT", 0, 0004, "Could not open tmp file %s to store Depex section information! \n", "");
+                free (OutputFileNameList);
+                free (ChildFileNameList);
+                return EFI_ABORTED;
+            }
+            InputFile = fopen(InputFileName, "rb+");
+            if (InputFile == NULL) {
+              Error("FMMT", 0, 0004, "Could not open input file %s! \n", "");
+              fclose(TmpFile);
+              free (OutputFileNameList);
+              free (ChildFileNameList);
+              return EFI_ABORTED;
+            }
+            fseek(InputFile, 0, SEEK_SET);
+            fseek(InputFile, 0, SEEK_END);
+            InputFileSize = ftell(InputFile);
+            fseek(InputFile, 0, SEEK_SET);
+            // make sure the section is 4 byte align
+            if (OutputFileNameList->DepexLen % 4 != 0) {
+                AlignN = 4 - OutputFileNameList->DepexLen % 4;
+            }
+            Buffer = malloc(InputFileSize + OutputFileNameList->DepexLen + AlignN);
+            memcpy(Buffer, OutputFileNameList->Depex, OutputFileNameList->DepexLen);
+            if (AlignN != 0) {
+                for (Index = 0; Index < AlignN; Index ++) {
+                    memcpy(Buffer + OutputFileNameList->DepexLen + Index, AlignV, 1);
+                }
+            }
+            if (fread(Buffer + OutputFileNameList->DepexLen + AlignN, 1, InputFileSize, InputFile) != InputFileSize) {
+                Error("FMMT", 0, 0004, "Could not open sec file %s to add Depex section information! \n", "");
                 fclose(TmpFile);
+                fclose(InputFile);
+                free(Buffer);
                 free (OutputFileNameList);
                 free (ChildFileNameList);
                 return EFI_ABORTED;
+            }
+            fwrite(Buffer, 1, InputFileSize + OutputFileNameList->DepexLen + AlignN, TmpFile);
+            free(Buffer);
+            fclose(TmpFile);
+            fclose(InputFile);
+            InputFileName = TmpFileName;
+          }
+          //
+          // Delete origin FFS
+          //
+          for (Id = FvInFd->FfsNumbers; Id <= FvInFd->FfsNumbers; Id--) {
+            if ((memcmp(&FvInFd->FfsAttuibutes[Id].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)){
+              if (access(FvInFd->FfsAttuibutes[Id].FfsName, 0) != -1) {
+                Status = LibFmmtDeleteFile(FvInFd->FfsAttuibutes[Id].FfsName);
+                if (EFI_ERROR(Status)) {
+                    Error("FMMT", 0, 0004, "error while encapsulate FD Image", "Delete the specified file failed!");
+                    free (OutputFileNameList);
+                    free (ChildFileNameList);
+                    return Status;
+                }
+                memset(FvInFd->FfsAttuibutes[Id].FfsName, '\0', _MAX_PATH);
+                FvInFd->FfsAttuibutes[Id].Level = 0xFF;
+                break;
               }
-              fseek(InputFile, 0, SEEK_SET);
-              fseek(InputFile, 0, SEEK_END);
-              InputFileSize = ftell(InputFile);
-              fseek(InputFile, 0, SEEK_SET);
-              // make sure the section is 4 byte align
-              if (OutputFileNameList->DepexLen % 4 != 0) {
-                  AlignN = 4 - OutputFileNameList->DepexLen % 4;
-              }
-              Buffer = malloc(InputFileSize + OutputFileNameList->DepexLen + AlignN);
-              memcpy(Buffer, OutputFileNameList->Depex, OutputFileNameList->DepexLen);
-              if (AlignN != 0) {
-                  for (Index = 0; Index < AlignN; Index ++) {
-                      memcpy(Buffer + OutputFileNameList->DepexLen + Index, AlignV, 1);
-                  }
-              }
-              if (fread(Buffer + OutputFileNameList->DepexLen + AlignN, 1, InputFileSize, InputFile) != InputFileSize) {
-                  Error("FMMT", 0, 0004, "Could not open sec file %s to add Depex section information! \n", "");
-                  fclose(TmpFile);
-                  fclose(InputFile);
-                  free(Buffer);
-                  free (OutputFileNameList);
-                  free (ChildFileNameList);
-                  return EFI_ABORTED;
-              }
-              fwrite(Buffer, 1, InputFileSize + OutputFileNameList->DepexLen + AlignN, TmpFile);
-              free(Buffer);
-              fclose(TmpFile);
-              fclose(InputFile);
-              InputFileName = TmpFileName;
-         }
-         for (Id = FvInFd->FfsNumbers; Id <= FvInFd->FfsNumbers; Id--) {
-             if ((memcmp(&FvInFd->FfsAttuibutes[Id].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)){
-                 if (access(FvInFd->FfsAttuibutes[Id].FfsName, 0) != -1) {
-                     Status = LibFmmtDeleteFile(FvInFd->FfsAttuibutes[Id].FfsName);
-                     if (EFI_ERROR(Status)) {
-                         Error("FMMT", 0, 0004, "error while encapsulate FD Image", "Delete the specified file failed!");
-                         free (OutputFileNameList);
-                         free (ChildFileNameList);
-                         return Status;
-                     }
-                     memset(FvInFd->FfsAttuibutes[Id].FfsName, '\0', _MAX_PATH);
-                     FvInFd->FfsAttuibutes[Id].Level = 0xFF;
-                     break;
-                 }
-             }
-         }
-         if (LocalEncapData->NextNode != NULL) {
-             LocalEncapDataTemp = LocalEncapData->NextNode;
-             if ((LocalEncapDataTemp->Type == FMMT_ENCAP_TREE_GUIDED_SECTION) || (LocalEncapDataTemp->Type == FMMT_ENCAP_TREE_COMPRESS_SECTION)) {
-                 Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, "1");
-             }
-             else{
-                 Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, FvInFd->AlignmentStr);
-             }
-         }
-         else{
-             Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, FvInFd->AlignmentStr);
-         }
-      if (EFI_ERROR (Status)) {
-        Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FFS file failed!");
-        free (OutputFileNameList);
-        free (ChildFileNameList);
-        return Status;
-      }
-      free(LocalEncapData->FvExtHeader);
-      LocalEncapData->FvExtHeader = NULL;
-      OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
-      if (OutputFileNameList->FFSName == NULL) {
-          Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
-          return EFI_OUT_OF_RESOURCES;
-      }
-      memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
-      OutputFileNameList->IsFFS = TRUE;
-      OutputFileNameList->ParentLevel = ParentLevel - 1;
-      if (OutputFileNameList->Next == NULL){
-          break;
-      }
-      OutputFileNameList = OutputFileNameList->Next;
-    }
+            }
+          }
+          if (LocalEncapData->NextNode != NULL) {
+            LocalEncapDataTemp = LocalEncapData->NextNode;
+            if ((LocalEncapDataTemp->Type == FMMT_ENCAP_TREE_GUIDED_SECTION) || (LocalEncapDataTemp->Type == FMMT_ENCAP_TREE_COMPRESS_SECTION)) {
+              Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, "1");
+            } else{
+              Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, FvInFd->AlignmentStr);
+            }
+          } else{
+            Status = LibEncapSectionFileToFFS(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, InputFileName, OutputFileName, LocalEncapData->FvExtHeader->FvName, FALSE, FvInFd->AlignmentStr);
+          }
+
+          if (EFI_ERROR (Status)) {
+            Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FFS file failed!");
+            free (OutputFileNameList);
+            free (ChildFileNameList);
+            return Status;
+          }
+          
+          free(LocalEncapData->FvExtHeader);
+          LocalEncapData->FvExtHeader = NULL;
+          
+          OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
+          if (OutputFileNameList->FFSName == NULL) {
+            Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
+            return EFI_OUT_OF_RESOURCES;
+          }
+          memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
+          OutputFileNameList->IsFFS = TRUE;
+          OutputFileNameList->ParentLevel = ParentLevel - 1;
+          OutputFileNameList->InFvId = Id;
+          if (OutputFileNameList->Next == NULL){
+            break;
+          }
+          OutputFileNameList = OutputFileNameList->Next;
+        }
         break;
       case FMMT_ENCAP_TREE_GUIDED_SECTION:
       while(OutputFileNameList!= NULL && OutputFileNameList->FFSName != NULL){
@@ -4941,25 +4938,25 @@ LibEncapNewFvFile(
         if (OutputFileSize > LargeFileSize) {
             IsLargeFile = TRUE;
         }
-    OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
-    if (OutputFileNameList->FFSName == NULL) {
-      Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
-      return EFI_OUT_OF_RESOURCES;
-    }
-    memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
-
-        if (EFI_ERROR (Status)) {
-          Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate guided section failed!");
-          free (OutputFileNameList);
-          free (ChildFileNameList);
-          return Status;
+        OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
+        if (OutputFileNameList->FFSName == NULL) {
+          Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
+          return EFI_OUT_OF_RESOURCES;
+        }
+        memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
+        
+         if (EFI_ERROR (Status)) {
+           Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate guided section failed!");
+           free (OutputFileNameList);
+           free (ChildFileNameList);
+           return Status;
+         }
+         OutputFileNameList->ParentLevel = ParentLevel - 1;
+         if (OutputFileNameList->Next == NULL) {
+           break;
+         }
+         OutputFileNameList = OutputFileNameList->Next;
         }
-        OutputFileNameList->ParentLevel = ParentLevel - 1;
-        if (OutputFileNameList->Next == NULL){
-          break;
-       }
-       OutputFileNameList = OutputFileNameList->Next;
-    }
         break;
       case FMMT_ENCAP_TREE_COMPRESS_SECTION:
         while(OutputFileNameList!= NULL && OutputFileNameList->FFSName != NULL){
@@ -5000,89 +4997,86 @@ LibEncapNewFvFile(
         break;
       case FMMT_ENCAP_TREE_FV_SECTION:
         while(OutputFileNameList!= NULL && OutputFileNameList->FFSName != NULL){
-        InputFileName  = OutputFileNameList->FFSName;
-        OutputFileName= LibFilenameStrExtended (strrchr(GenTempFile (), OS_SEP), TemDir, "sec");
-
-        Status = LibCreateFfsSection(NULL, InputFileName, NULL, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, OutputFileName, NULL, NULL, NULL, 0, 0, NULL);
+          InputFileName  = OutputFileNameList->FFSName;
+          OutputFileName= LibFilenameStrExtended (strrchr(GenTempFile (), OS_SEP), TemDir, "sec");
 
-        if (EFI_ERROR (Status)) {
-          Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV section failed!");
-          free (OutputFileNameList);
-          free (ChildFileNameList);
-          return Status;
-        }
+          Status = LibCreateFfsSection(NULL, InputFileName, NULL, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, OutputFileName, NULL, NULL, NULL, 0, 0, NULL);
 
-        InputFileName  = OutputFileName;
-        OutputFileName= LibFilenameStrExtended (strrchr(GenTempFile (), OS_SEP), TemDir, "sec");
+          if (EFI_ERROR (Status)) {
+            Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV section failed!");
+            free (OutputFileNameList);
+            free (ChildFileNameList);
+            return Status;
+          }
 
-        //
-        // Make it alignment.
-        //
-        Status = LibCreateFfsSection(FvInFd, InputFileName, NULL, 0, OutputFileName, NULL, NULL, NULL, 0, 0, NULL);
-        OutFile = fopen(OutputFileName, "rb+");
-        if (OutFile == NULL) {
+          InputFileName  = OutputFileName;
+          OutputFileName= LibFilenameStrExtended (strrchr(GenTempFile (), OS_SEP), TemDir, "sec");
+          
+          //
+          // Make it alignment.
+          //
+          Status = LibCreateFfsSection(FvInFd, InputFileName, NULL, 0, OutputFileName, NULL, NULL, NULL, 0, 0, NULL);
+          OutFile = fopen(OutputFileName, "rb+");
+          if (OutFile == NULL) {
             Error("FMMT", 0, 0004, "Could not open the file %s! \n", "");
             free (OutputFileNameList);
             free (ChildFileNameList);
             return EFI_ABORTED;
-        }
-        fseek(OutFile, 0, SEEK_SET);
-        fseek(OutFile, 0, SEEK_END);
-        OutputFileSize = ftell(OutFile);
-        fclose(OutFile);
-        if (OutputFileSize > LargeFileSize) {
+          }
+          fseek(OutFile, 0, SEEK_SET);
+          fseek(OutFile, 0, SEEK_END);
+          OutputFileSize = ftell(OutFile);
+          fclose(OutFile);
+          if (OutputFileSize > LargeFileSize) {
             IsLargeFile = TRUE;
+          }
+          
+          OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
+          if (OutputFileNameList->FFSName == NULL) {
+            Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
+            return EFI_OUT_OF_RESOURCES;
+          }
+          memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
+          
+          if (EFI_ERROR (Status)) {
+            Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV section failed!");
+            free (OutputFileNameList);
+            free (ChildFileNameList);
+            return Status;
+          }
+          OutputFileNameList->ParentLevel = ParentLevel - 1;
+          if (OutputFileNameList->Next == NULL){
+             break;
+          }
+          OutputFileNameList = OutputFileNameList->Next;
         }
-
-    OutputFileNameList->FFSName = (char *)malloc(strlen(OutputFileName)+1);
-    if (OutputFileNameList->FFSName == NULL) {
-      Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
-      return EFI_OUT_OF_RESOURCES;
-    }
-    memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
-
-        if (EFI_ERROR (Status)) {
-          Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate FV section failed!");
-          free (OutputFileNameList);
-          free (ChildFileNameList);
-          return Status;
-        }
-        OutputFileNameList->ParentLevel = ParentLevel - 1;
-        if (OutputFileNameList->Next == NULL){
-           break;
-        }
-        OutputFileNameList = OutputFileNameList->Next;
-    }
         break;
       default:
         for (Id = FvInFd->FfsNumbers; Id <= FvInFd->FfsNumbers; Id--) {
-            if ((memcmp(&FvInFd->FfsAttuibutes[Id].GuidName, &(CurrentEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)){
-                FvInFd->FfsAttuibutes[Id].IsHandle = TRUE;
-                memcpy(OutputFileNameList->UiName, FvInFd->FfsAttuibutes[Id].UiName, FvInFd->FfsAttuibutes[Id].UiNameSize);
-                OutputFileNameList->UiNameSize = FvInFd->FfsAttuibutes[Id].UiNameSize;
-                OutputFileNameList->FFSName = FvInFd->FfsAttuibutes[Id].FfsName;
-                OutputFileNameList->Depex = FvInFd->FfsAttuibutes[Id].Depex;
-                OutputFileNameList->DepexLen = FvInFd->FfsAttuibutes[Id].DepexLen;
-                OutputFileNameList->FfsFoundFlag = TRUE;
-                OutputFileNameList->IsFFS = TRUE;
-                OutputFileNameList->InFvId = Id;
-                OutputFileNameList->FvId = FvInFd->FfsAttuibutes[Id].FvId;
-                OutputFileNameList->ParentLevel = ParentLevel - 1;
-                *OutputFile = OutputFileNameList;
-                return EFI_SUCCESS;
-            }
+          if ((memcmp(&FvInFd->FfsAttuibutes[Id].GuidName, &(CurrentEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)){
+            FvInFd->FfsAttuibutes[Id].IsHandle = TRUE;
+            memcpy(OutputFileNameList->UiName, FvInFd->FfsAttuibutes[Id].UiName, FvInFd->FfsAttuibutes[Id].UiNameSize);
+            OutputFileNameList->UiNameSize = FvInFd->FfsAttuibutes[Id].UiNameSize;
+            OutputFileNameList->FFSName = FvInFd->FfsAttuibutes[Id].FfsName;
+            OutputFileNameList->Depex = FvInFd->FfsAttuibutes[Id].Depex;
+            OutputFileNameList->DepexLen = FvInFd->FfsAttuibutes[Id].DepexLen;
+            OutputFileNameList->FfsFoundFlag = TRUE;
+            OutputFileNameList->IsFFS = TRUE;
+            OutputFileNameList->InFvId = Id;
+            OutputFileNameList->FvId = FvInFd->FfsAttuibutes[Id].FvId;
+            OutputFileNameList->ParentLevel = ParentLevel - 1;
+            *OutputFile = OutputFileNameList;
+            return EFI_SUCCESS;
+          }
         }
     }
 
     if (CurrentEncapData == NULL) {
        LocalEncapData = FvInFd->EncapData;
     } else {
-        if (OutputFileNameList != NULL && OutputFileNameList->FFSName != NULL && OutputFileNameList->IsFFS == TRUE) {
-          *OutputFile = OutputFileNameList;
-          return EFI_SUCCESS;
-        }
         LocalEncapData = CurrentEncapData;
     }
+    
     ParentLevel -= 1;
 
     while (LocalEncapData->NextNode != NULL) {
-- 
2.16.2.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#68035): https://edk2.groups.io/g/devel/message/68035
Mute This Topic: https://groups.io/mt/78525275/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-