public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
@ 2020-11-26  7:07 GregX Yeh
  0 siblings, 0 replies; 18+ messages in thread
From: GregX Yeh @ 2020-11-26  7:07 UTC (permalink / raw)
  To: devel; +Cc: Bob Feng, Liming Gao

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


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
@ 2020-11-27  1:38 GregX Yeh
  2020-11-27  2:08 ` Bob Feng
  0 siblings, 1 reply; 18+ messages in thread
From: GregX Yeh @ 2020-11-27  1:38 UTC (permalink / raw)
  To: devel; +Cc: Bob Feng, Liming Gao

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


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
  2020-11-27  1:38 GregX Yeh
@ 2020-11-27  2:08 ` Bob Feng
  0 siblings, 0 replies; 18+ messages in thread
From: Bob Feng @ 2020-11-27  2:08 UTC (permalink / raw)
  To: Yeh, GregX, devel@edk2.groups.io; +Cc: Liming Gao

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


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
@ 2020-11-27  2:16 GregX Yeh
  0 siblings, 0 replies; 18+ messages in thread
From: GregX Yeh @ 2020-11-27  2:16 UTC (permalink / raw)
  To: devel; +Cc: Bob Feng, Liming Gao

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.h |   4 +-
 BaseTools/Source/C/FMMT/FmmtLib.c                  | 654 ++++++++++-----------
 2 files changed, 326 insertions(+), 332 deletions(-)

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


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
@ 2020-12-01  7:43 GregX Yeh
  0 siblings, 0 replies; 18+ messages in thread
From: GregX Yeh @ 2020-12-01  7:43 UTC (permalink / raw)
  To: devel; +Cc: Bob Feng, Liming Gao

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/FmmtLib.c | 25 +++++++------------------
 1 file changed, 7 insertions(+), 18 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..952d64fc35 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
     //
@@ -4274,7 +4267,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList;
@@ -4329,7 +4322,7 @@ 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;
@@ -4485,7 +4478,7 @@ 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;
             }
@@ -5077,10 +5070,6 @@ LibEncapNewFvFile(
     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;
-- 
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
@ 2020-12-04  9:04 GregX Yeh
  0 siblings, 0 replies; 18+ messages in thread
From: GregX Yeh @ 2020-12-04  9:04 UTC (permalink / raw)
  To: devel; +Cc: Bob Feng, Liming Gao

Fixed replace file failure when FFS in multiple 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/FmmtLib.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..bfb3b57d4a 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
     //
@@ -4198,10 +4191,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 +4220,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4257,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4273,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList;
@@ -4329,7 +4328,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
@@ -4485,7 +4484,7 @@ 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;
             }
@@ -4818,6 +4817,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5077,6 @@ LibEncapNewFvFile(
     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;
-- 
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
@ 2020-12-08  7:32 GregX Yeh
  2020-12-08  8:30 ` Bob Feng
  2020-12-18  9:33 ` Bob Feng
  0 siblings, 2 replies; 18+ messages in thread
From: GregX Yeh @ 2020-12-08  7:32 UTC (permalink / raw)
  To: devel; +Cc: Bob Feng, Liming Gao

Fixed replace file failure when FFS in multiple 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/FmmtLib.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..bfb3b57d4a 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
     //
@@ -4198,10 +4191,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 +4220,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4257,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4273,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList;
@@ -4329,7 +4328,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
@@ -4485,7 +4484,7 @@ 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;
             }
@@ -4818,6 +4817,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5077,6 @@ LibEncapNewFvFile(
     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;
-- 
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
  2020-12-08  7:32 GregX Yeh
@ 2020-12-08  8:30 ` Bob Feng
  2020-12-08  8:33   ` GregX Yeh
  2020-12-18  9:33 ` Bob Feng
  1 sibling, 1 reply; 18+ messages in thread
From: Bob Feng @ 2020-12-08  8:30 UTC (permalink / raw)
  To: Yeh, GregX, devel@edk2.groups.io; +Cc: Liming Gao

Hi Greg,

Is it the version 2 of the patch that you sent in 12/4?


Thanks,
Bob

-----Original Message-----
From: Yeh, GregX <gregx.yeh@intel.com> 
Sent: Tuesday, December 8, 2020 3:32 PM
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 multiple 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/FmmtLib.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..bfb3b57d4a 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
     //
@@ -4198,10 +4191,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 +4220,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4257,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4273,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, 
+ LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList; @@ -4329,7 +4328,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type 
+ == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode; @@ -4485,7 +4484,7 @@ 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;
             }
@@ -4818,6 +4817,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5077,6 @@ LibEncapNewFvFile(
     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;
--
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
  2020-12-08  8:30 ` Bob Feng
@ 2020-12-08  8:33   ` GregX Yeh
  0 siblings, 0 replies; 18+ messages in thread
From: GregX Yeh @ 2020-12-08  8:33 UTC (permalink / raw)
  To: Feng, Bob C, devel@edk2.groups.io; +Cc: Liming Gao

Hi Bob,

This is latest patch for FMMT.

Thanks,
Greg 

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

Hi Greg,

Is it the version 2 of the patch that you sent in 12/4?


Thanks,
Bob

-----Original Message-----
From: Yeh, GregX <gregx.yeh@intel.com>
Sent: Tuesday, December 8, 2020 3:32 PM
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 multiple 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/FmmtLib.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..bfb3b57d4a 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
     //
@@ -4198,10 +4191,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 +4220,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4257,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4273,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, 
+ LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList; @@ -4329,7 +4328,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type 
+ == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode; @@ -4485,7 +4484,7 @@ 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;
             }
@@ -4818,6 +4817,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5077,6 @@ LibEncapNewFvFile(
     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;
--
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
  2020-12-08  7:32 GregX Yeh
  2020-12-08  8:30 ` Bob Feng
@ 2020-12-18  9:33 ` Bob Feng
  2020-12-23  8:00   ` GregX Yeh
  1 sibling, 1 reply; 18+ messages in thread
From: Bob Feng @ 2020-12-18  9:33 UTC (permalink / raw)
  To: Yeh, GregX, devel@edk2.groups.io; +Cc: Liming Gao, 'Yunhua Feng'

Hi Greg

For the change of the following block,

1. 
-  CurrentFv->FfsNumbers  = *FfsCount;
-
   *FfsCount += 1;
 
+  CurrentFv->FfsNumbers  = *FfsCount;
+

2.
-        for (Index=0; Index <= FvInFd->FfsNumbers; Index++) {
+        for (Index=0; Index < FvInFd->FfsNumbers; Index++) {


I think it's correct but I see there are many places still have such statement: 
"for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) { "

I think all others may also need to be changed from "for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {" need to change to "for (Index = 0; Index < FvInFd->FfsNumbers; Index++) {"

Thanks,
Bob

-----Original Message-----
From: Yeh, GregX <gregx.yeh@intel.com> 
Sent: Tuesday, December 8, 2020 3:32 PM
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 multiple 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/FmmtLib.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..bfb3b57d4a 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
     //
@@ -4198,10 +4191,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 +4220,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4257,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4273,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, 
+ LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList; @@ -4329,7 +4328,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type 
+ == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode; @@ -4485,7 +4484,7 @@ 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;
             }
@@ -4818,6 +4817,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5077,6 @@ LibEncapNewFvFile(
     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;
--
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
  2020-12-18  9:33 ` Bob Feng
@ 2020-12-23  8:00   ` GregX Yeh
  2021-01-04  2:54     ` Bob Feng
  0 siblings, 1 reply; 18+ messages in thread
From: GregX Yeh @ 2020-12-23  8:00 UTC (permalink / raw)
  To: Feng, Bob C, devel@edk2.groups.io; +Cc: Liming Gao, 'Yunhua Feng'

[-- Attachment #1: Type: text/plain, Size: 7024 bytes --]

Hi Bob,

I have fixed for loop that use FfsNumbers.
I attach new patch file.


Thanks,
Greg


-----Original Message-----
From: Feng, Bob C <bob.c.feng@intel.com> 
Sent: Friday, December 18, 2020 5:33 PM
To: Yeh, GregX <gregx.yeh@intel.com>; devel@edk2.groups.io
Cc: Liming Gao <gaoliming@byosoft.com.cn>; 'Yunhua Feng' <fengyunhua@byosoft.com.cn>
Subject: RE: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2

Hi Greg

For the change of the following block,

1. 
-  CurrentFv->FfsNumbers  = *FfsCount;
-
   *FfsCount += 1;
 
+  CurrentFv->FfsNumbers  = *FfsCount;
+

2.
-        for (Index=0; Index <= FvInFd->FfsNumbers; Index++) {
+        for (Index=0; Index < FvInFd->FfsNumbers; Index++) {


I think it's correct but I see there are many places still have such statement: 
"for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) { "

I think all others may also need to be changed from "for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {" need to change to "for (Index = 0; Index < FvInFd->FfsNumbers; Index++) {"

Thanks,
Bob

-----Original Message-----
From: Yeh, GregX <gregx.yeh@intel.com>
Sent: Tuesday, December 8, 2020 3:32 PM
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 multiple 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/FmmtLib.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..bfb3b57d4a 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
     //
@@ -4198,10 +4191,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 +4220,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4257,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4273,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, 
+ LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList; @@ -4329,7 +4328,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type 
+ == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode; @@ -4485,7 +4484,7 @@ 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;
             }
@@ -4818,6 +4817,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5077,6 @@ LibEncapNewFvFile(
     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;
--
2.16.2.windows.1


[-- Attachment #2: 0001-edk2-staging-BaseTools-FMMT-Replace-file-failure-whe.patch --]
[-- Type: application/octet-stream, Size: 6327 bytes --]

From 54edfad456fdef488c953404f71b2aef646bc143 Mon Sep 17 00:00:00 2001
From: GregX Yeh <gregx.yeh@intel.com>
Date: Tue, 1 Dec 2020 15:37:23 +0800
Subject: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV
 level over 2

Fixed replace file failure when FFS in multiple 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/FmmtLib.c | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..60bd47e5bb 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
     //
@@ -4198,10 +4191,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 +4220,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4257,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4273,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList;
@@ -4329,7 +4328,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
@@ -4473,7 +4472,7 @@ LibEncapNewFvFile(
           while (LocalEncapData->Level != ParentLevel) {
             LocalEncapData = LocalEncapData->NextNode;
           }
-            for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {
+            for (Index = 0; Index < FvInFd->FfsNumbers; Index++) {
                 if ((memcmp(&FvInFd->FfsAttuibutes[Index].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)) {
                     SubFvId = Index;
                     break;
@@ -4485,7 +4484,7 @@ 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;
             }
@@ -4818,6 +4817,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5077,6 @@ LibEncapNewFvFile(
     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;
-- 
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
  2020-12-23  8:00   ` GregX Yeh
@ 2021-01-04  2:54     ` Bob Feng
  0 siblings, 0 replies; 18+ messages in thread
From: Bob Feng @ 2021-01-04  2:54 UTC (permalink / raw)
  To: Yeh, GregX, devel@edk2.groups.io; +Cc: Liming Gao, 'Yunhua Feng'

Hi Greg,

Could you send a code review mail with this patch embedded?

Thanks,
Bob 

-----Original Message-----
From: Yeh, GregX <gregx.yeh@intel.com> 
Sent: Wednesday, December 23, 2020 4:01 PM
To: Feng, Bob C <bob.c.feng@intel.com>; devel@edk2.groups.io
Cc: Liming Gao <gaoliming@byosoft.com.cn>; 'Yunhua Feng' <fengyunhua@byosoft.com.cn>
Subject: RE: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2

Hi Bob,

I have fixed for loop that use FfsNumbers.
I attach new patch file.


Thanks,
Greg


-----Original Message-----
From: Feng, Bob C <bob.c.feng@intel.com>
Sent: Friday, December 18, 2020 5:33 PM
To: Yeh, GregX <gregx.yeh@intel.com>; devel@edk2.groups.io
Cc: Liming Gao <gaoliming@byosoft.com.cn>; 'Yunhua Feng' <fengyunhua@byosoft.com.cn>
Subject: RE: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2

Hi Greg

For the change of the following block,

1. 
-  CurrentFv->FfsNumbers  = *FfsCount;
-
   *FfsCount += 1;
 
+  CurrentFv->FfsNumbers  = *FfsCount;
+

2.
-        for (Index=0; Index <= FvInFd->FfsNumbers; Index++) {
+        for (Index=0; Index < FvInFd->FfsNumbers; Index++) {


I think it's correct but I see there are many places still have such statement: 
"for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) { "

I think all others may also need to be changed from "for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {" need to change to "for (Index = 0; Index < FvInFd->FfsNumbers; Index++) {"

Thanks,
Bob

-----Original Message-----
From: Yeh, GregX <gregx.yeh@intel.com>
Sent: Tuesday, December 8, 2020 3:32 PM
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 multiple 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/FmmtLib.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..bfb3b57d4a 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
     //
@@ -4198,10 +4191,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 +4220,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4257,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4273,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, 
+ LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList; @@ -4329,7 +4328,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type 
+ == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode; @@ -4485,7 +4484,7 @@ 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;
             }
@@ -4818,6 +4817,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5077,6 @@ LibEncapNewFvFile(
     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;
--
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
@ 2021-01-04  3:11 GregX Yeh
  0 siblings, 0 replies; 18+ messages in thread
From: GregX Yeh @ 2021-01-04  3:11 UTC (permalink / raw)
  To: devel; +Cc: Bob Feng, Liming Gao

Fixed replace file failure when FFS in multiple 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/FmmtLib.c | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..60bd47e5bb 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
     //
@@ -4198,10 +4191,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 +4220,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4257,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4273,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList;
@@ -4329,7 +4328,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
@@ -4473,7 +4472,7 @@ LibEncapNewFvFile(
           while (LocalEncapData->Level != ParentLevel) {
             LocalEncapData = LocalEncapData->NextNode;
           }
-            for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {
+            for (Index = 0; Index < FvInFd->FfsNumbers; Index++) {
                 if ((memcmp(&FvInFd->FfsAttuibutes[Index].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)) {
                     SubFvId = Index;
                     break;
@@ -4485,7 +4484,7 @@ 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;
             }
@@ -4818,6 +4817,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5077,6 @@ LibEncapNewFvFile(
     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;
-- 
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
@ 2021-03-10  2:05 GregX Yeh
  2021-03-15  4:34 ` Bob Feng
  0 siblings, 1 reply; 18+ messages in thread
From: GregX Yeh @ 2021-03-10  2:05 UTC (permalink / raw)
  To: devel; +Cc: Bob Feng, Liming Gao

Fixed replace file failure when FFS in multiple 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/FmmtLib.c | 56 ++++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..26df0181c7 100644
--- a/BaseTools/Source/C/FMMT/FmmtLib.c
+++ b/BaseTools/Source/C/FMMT/FmmtLib.c
@@ -494,7 +494,7 @@ LibReadFvHeader (
     if ((FvLevel -1) == 0) {
       printf ("\n%s :\n", FvName);
     } else {
-      printf ("%sChild FV named FV%d of %s\n", BlankSpace, FvCount, FvName);
+      printf ("\n%sChild FV named FV%d of %s\n", BlankSpace, FvCount, FvName);
     }
   }
 
@@ -502,7 +502,7 @@ LibReadFvHeader (
   // Print FV header information
   //
   if (ViewFlag) {
-    printf ("\n%sAttributes:            %X\n", BlankSpace, (unsigned) VolumeHeader->Attributes);
+    printf ("%sAttributes:            %X\n", BlankSpace, (unsigned) VolumeHeader->Attributes);
     printf ("%sTotal Volume Size:     0x%08X\n", BlankSpace, (unsigned) VolumeHeader->FvLength);
     printf ("%sFree Volume Size:      0x%08X\n", BlankSpace, (unsigned) (VolumeHeader->FvLength - GetFreeOffset(InputFv)));
   }
@@ -789,7 +789,8 @@ LibParseSection (
   UINT8                  *FvCount,
   BOOLEAN                ViewFlag,
   BOOLEAN                ErasePolarity,
-  BOOLEAN                *IsFfsGenerated
+  BOOLEAN                *IsFfsGenerated,
+  BOOLEAN                IsFfs
   )
 {
   UINT32              ParsedLength;
@@ -997,8 +998,12 @@ LibParseSection (
       break;
 
     case EFI_SECTION_COMPRESSION:
-      if (FirstInFlag) {
-        Level ++;
+    	if (IsFfs){
+    	  Level ++;
+    	} else {
+        if (FirstInFlag) {
+          Level ++;
+        }
       }
       NumberOfSections ++;
 
@@ -1159,7 +1164,9 @@ LibParseSection (
                                   FvCount,
                                   ViewFlag,
                                   ErasePolarity,
-                                  IsFfsGenerated);
+                                  IsFfsGenerated,
+                                  FALSE
+                                  );
 
       if (CompressionType == EFI_STANDARD_COMPRESSION) {
         //
@@ -1181,8 +1188,12 @@ LibParseSection (
       // looks up the appropriate tool to use for extracting
       // a GUID defined FV section.
       //
-      if (FirstInFlag) {
+      if (IsFfs) {
         Level ++;
+      } else {
+        if (FirstInFlag) {
+          Level ++;
+        }
       }
       NumberOfSections++;
       EncapDataNeedUpdata = TRUE;
@@ -1216,7 +1227,8 @@ LibParseSection (
                 FvCount,
                 ViewFlag,
                 ErasePolarity,
-                IsFfsGenerated
+                IsFfsGenerated,
+                FALSE
                 );
         if (EFI_ERROR(Status)) {
           Error(NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL);
@@ -1471,7 +1483,8 @@ LibParseSection (
                   FvCount,
                   ViewFlag,
                   ErasePolarity,
-                  IsFfsGenerated
+                  IsFfsGenerated,
+                  FALSE
                   );
         if (EFI_ERROR (Status)) {
           Error (NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL);
@@ -1491,7 +1504,8 @@ LibParseSection (
             FvCount,
             ViewFlag,
             ErasePolarity,
-            IsFfsGenerated
+            IsFfsGenerated,
+            FALSE
             );
           if (ExtractionTool != NULL) {
             free (ExtractionTool);
@@ -2016,7 +2030,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;
@@ -2099,7 +2113,8 @@ LibGetFileInfo (
       FvCount,
       ViewFlag,
       ErasePolarity,
-      &IsGeneratedFfs
+      &IsGeneratedFfs,
+      TRUE
       );
     }
     if (EFI_ERROR (Status)) {
@@ -4198,10 +4213,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 +4242,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4279,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4295,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList;
@@ -4329,7 +4350,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
@@ -4818,6 +4839,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5099,6 @@ LibEncapNewFvFile(
     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;
-- 
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
  2021-03-10  2:05 [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2 GregX Yeh
@ 2021-03-15  4:34 ` Bob Feng
  2021-03-16  1:13   ` 回复: [edk2-devel] " fengyunhua
  2021-03-16  1:54   ` GregX Yeh
  0 siblings, 2 replies; 18+ messages in thread
From: Bob Feng @ 2021-03-15  4:34 UTC (permalink / raw)
  To: Yeh, GregX, devel@edk2.groups.io, Yunhua Feng; +Cc: Liming Gao

Hi Greg,

Please use BaseTools\Scripts\PatchCheck.py to check this patch. There are some lines code format invalid.
The logic looks good to me.

Yunhua, could you please double check, I remember you have concerns about the previous version of this patch.

Thanks,
Bob

-----Original Message-----
From: Yeh, GregX <gregx.yeh@intel.com> 
Sent: Wednesday, March 10, 2021 10:05 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 multiple 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/FmmtLib.c | 56 ++++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..26df0181c7 100644
--- a/BaseTools/Source/C/FMMT/FmmtLib.c
+++ b/BaseTools/Source/C/FMMT/FmmtLib.c
@@ -494,7 +494,7 @@ LibReadFvHeader (
     if ((FvLevel -1) == 0) {
       printf ("\n%s :\n", FvName);
     } else {
-      printf ("%sChild FV named FV%d of %s\n", BlankSpace, FvCount, FvName);
+      printf ("\n%sChild FV named FV%d of %s\n", BlankSpace, FvCount, 
+ FvName);
     }
   }
 
@@ -502,7 +502,7 @@ LibReadFvHeader (
   // Print FV header information
   //
   if (ViewFlag) {
-    printf ("\n%sAttributes:            %X\n", BlankSpace, (unsigned) VolumeHeader->Attributes);
+    printf ("%sAttributes:            %X\n", BlankSpace, (unsigned) VolumeHeader->Attributes);
     printf ("%sTotal Volume Size:     0x%08X\n", BlankSpace, (unsigned) VolumeHeader->FvLength);
     printf ("%sFree Volume Size:      0x%08X\n", BlankSpace, (unsigned) (VolumeHeader->FvLength - GetFreeOffset(InputFv)));
   }
@@ -789,7 +789,8 @@ LibParseSection (
   UINT8                  *FvCount,
   BOOLEAN                ViewFlag,
   BOOLEAN                ErasePolarity,
-  BOOLEAN                *IsFfsGenerated
+  BOOLEAN                *IsFfsGenerated,
+  BOOLEAN                IsFfs
   )
 {
   UINT32              ParsedLength;
@@ -997,8 +998,12 @@ LibParseSection (
       break;
 
     case EFI_SECTION_COMPRESSION:
-      if (FirstInFlag) {
-        Level ++;
+    	if (IsFfs){
+    	  Level ++;
+    	} else {
+        if (FirstInFlag) {
+          Level ++;
+        }
       }
       NumberOfSections ++;
 
@@ -1159,7 +1164,9 @@ LibParseSection (
                                   FvCount,
                                   ViewFlag,
                                   ErasePolarity,
-                                  IsFfsGenerated);
+                                  IsFfsGenerated,
+                                  FALSE
+                                  );
 
       if (CompressionType == EFI_STANDARD_COMPRESSION) {
         //
@@ -1181,8 +1188,12 @@ LibParseSection (
       // looks up the appropriate tool to use for extracting
       // a GUID defined FV section.
       //
-      if (FirstInFlag) {
+      if (IsFfs) {
         Level ++;
+      } else {
+        if (FirstInFlag) {
+          Level ++;
+        }
       }
       NumberOfSections++;
       EncapDataNeedUpdata = TRUE;
@@ -1216,7 +1227,8 @@ LibParseSection (
                 FvCount,
                 ViewFlag,
                 ErasePolarity,
-                IsFfsGenerated
+                IsFfsGenerated,
+                FALSE
                 );
         if (EFI_ERROR(Status)) {
           Error(NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL); @@ -1471,7 +1483,8 @@ LibParseSection (
                   FvCount,
                   ViewFlag,
                   ErasePolarity,
-                  IsFfsGenerated
+                  IsFfsGenerated,
+                  FALSE
                   );
         if (EFI_ERROR (Status)) {
           Error (NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL); @@ -1491,7 +1504,8 @@ LibParseSection (
             FvCount,
             ViewFlag,
             ErasePolarity,
-            IsFfsGenerated
+            IsFfsGenerated,
+            FALSE
             );
           if (ExtractionTool != NULL) {
             free (ExtractionTool);
@@ -2016,7 +2030,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;
@@ -2099,7 +2113,8 @@ LibGetFileInfo (
       FvCount,
       ViewFlag,
       ErasePolarity,
-      &IsGeneratedFfs
+      &IsGeneratedFfs,
+      TRUE
       );
     }
     if (EFI_ERROR (Status)) {
@@ -4198,10 +4213,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 +4242,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4279,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4295,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, 
+ LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList; @@ -4329,7 +4350,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type 
+ == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode; @@ -4818,6 +4839,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5099,6 @@ LibEncapNewFvFile(
     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;
--
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* 回复: [edk2-devel] [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
  2021-03-15  4:34 ` Bob Feng
@ 2021-03-16  1:13   ` fengyunhua
  2021-03-16  1:54   ` GregX Yeh
  1 sibling, 0 replies; 18+ messages in thread
From: fengyunhua @ 2021-03-16  1:13 UTC (permalink / raw)
  To: devel, bob.c.feng, 'Yeh, GregX'; +Cc: 'Liming Gao'

Hi Bob,
  I have no concern about this patch.

Thanks,
Yunhua

-----邮件原件-----
发件人: devel@edk2.groups.io <devel@edk2.groups.io> 代表 Bob Feng
发送时间: 2021年3月15日 12:35
收件人: Yeh, GregX <gregx.yeh@intel.com>; devel@edk2.groups.io; Yunhua Feng
<fengyunhua@byosoft.com.cn>
抄送: Liming Gao <gaoliming@byosoft.com.cn>
主题: Re: [edk2-devel] [PATCH] [edk2-staging] BaseTools/FMMT: Replace file
failure when FV level over 2

Hi Greg,

Please use BaseTools\Scripts\PatchCheck.py to check this patch. There are
some lines code format invalid.
The logic looks good to me.

Yunhua, could you please double check, I remember you have concerns about
the previous version of this patch.

Thanks,
Bob

-----Original Message-----
From: Yeh, GregX <gregx.yeh@intel.com> 
Sent: Wednesday, March 10, 2021 10:05 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 multiple 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/FmmtLib.c | 56
++++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c
b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..26df0181c7 100644
--- a/BaseTools/Source/C/FMMT/FmmtLib.c
+++ b/BaseTools/Source/C/FMMT/FmmtLib.c
@@ -494,7 +494,7 @@ LibReadFvHeader (
     if ((FvLevel -1) == 0) {
       printf ("\n%s :\n", FvName);
     } else {
-      printf ("%sChild FV named FV%d of %s\n", BlankSpace, FvCount,
FvName);
+      printf ("\n%sChild FV named FV%d of %s\n", BlankSpace, FvCount, 
+ FvName);
     }
   }
 
@@ -502,7 +502,7 @@ LibReadFvHeader (
   // Print FV header information
   //
   if (ViewFlag) {
-    printf ("\n%sAttributes:            %X\n", BlankSpace, (unsigned)
VolumeHeader->Attributes);
+    printf ("%sAttributes:            %X\n", BlankSpace, (unsigned)
VolumeHeader->Attributes);
     printf ("%sTotal Volume Size:     0x%08X\n", BlankSpace, (unsigned)
VolumeHeader->FvLength);
     printf ("%sFree Volume Size:      0x%08X\n", BlankSpace, (unsigned)
(VolumeHeader->FvLength - GetFreeOffset(InputFv)));
   }
@@ -789,7 +789,8 @@ LibParseSection (
   UINT8                  *FvCount,
   BOOLEAN                ViewFlag,
   BOOLEAN                ErasePolarity,
-  BOOLEAN                *IsFfsGenerated
+  BOOLEAN                *IsFfsGenerated,
+  BOOLEAN                IsFfs
   )
 {
   UINT32              ParsedLength;
@@ -997,8 +998,12 @@ LibParseSection (
       break;
 
     case EFI_SECTION_COMPRESSION:
-      if (FirstInFlag) {
-        Level ++;
+    	if (IsFfs){
+    	  Level ++;
+    	} else {
+        if (FirstInFlag) {
+          Level ++;
+        }
       }
       NumberOfSections ++;
 
@@ -1159,7 +1164,9 @@ LibParseSection (
                                   FvCount,
                                   ViewFlag,
                                   ErasePolarity,
-                                  IsFfsGenerated);
+                                  IsFfsGenerated,
+                                  FALSE
+                                  );
 
       if (CompressionType == EFI_STANDARD_COMPRESSION) {
         //
@@ -1181,8 +1188,12 @@ LibParseSection (
       // looks up the appropriate tool to use for extracting
       // a GUID defined FV section.
       //
-      if (FirstInFlag) {
+      if (IsFfs) {
         Level ++;
+      } else {
+        if (FirstInFlag) {
+          Level ++;
+        }
       }
       NumberOfSections++;
       EncapDataNeedUpdata = TRUE;
@@ -1216,7 +1227,8 @@ LibParseSection (
                 FvCount,
                 ViewFlag,
                 ErasePolarity,
-                IsFfsGenerated
+                IsFfsGenerated,
+                FALSE
                 );
         if (EFI_ERROR(Status)) {
           Error(NULL, 0, 0003, "parse of decoded GUIDED section failed",
NULL); @@ -1471,7 +1483,8 @@ LibParseSection (
                   FvCount,
                   ViewFlag,
                   ErasePolarity,
-                  IsFfsGenerated
+                  IsFfsGenerated,
+                  FALSE
                   );
         if (EFI_ERROR (Status)) {
           Error (NULL, 0, 0003, "parse of decoded GUIDED section failed",
NULL); @@ -1491,7 +1504,8 @@ LibParseSection (
             FvCount,
             ViewFlag,
             ErasePolarity,
-            IsFfsGenerated
+            IsFfsGenerated,
+            FALSE
             );
           if (ExtractionTool != NULL) {
             free (ExtractionTool);
@@ -2016,7 +2030,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;
@@ -2099,7 +2113,8 @@ LibGetFileInfo (
       FvCount,
       ViewFlag,
       ErasePolarity,
-      &IsGeneratedFfs
+      &IsGeneratedFfs,
+      TRUE
       );
     }
     if (EFI_ERROR (Status)) {
@@ -4198,10 +4213,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 +4242,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4279,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4295,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode
!= NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1,
&ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, 
+ LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level
-1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList; @@ -4329,7
+4350,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type 
+ == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode; @@ -4818,6
+4839,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName,
strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5099,6 @@ LibEncapNewFvFile(
     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;
--
2.16.2.windows.1









^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
  2021-03-15  4:34 ` Bob Feng
  2021-03-16  1:13   ` 回复: [edk2-devel] " fengyunhua
@ 2021-03-16  1:54   ` GregX Yeh
  2021-03-17  6:57     ` Bob Feng
  1 sibling, 1 reply; 18+ messages in thread
From: GregX Yeh @ 2021-03-16  1:54 UTC (permalink / raw)
  To: Feng, Bob C, devel@edk2.groups.io, Yunhua Feng; +Cc: Liming Gao

Hi Bob,

I have created new patch file. PatchCheck is pass.
Using Git send-email  send to edk2.group.io 

Thanks,
Greg

-----Original Message-----
From: Feng, Bob C <bob.c.feng@intel.com> 
Sent: Monday, March 15, 2021 12:35 PM
To: Yeh, GregX <gregx.yeh@intel.com>; devel@edk2.groups.io; Yunhua Feng <fengyunhua@byosoft.com.cn>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Subject: RE: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2

Hi Greg,

Please use BaseTools\Scripts\PatchCheck.py to check this patch. There are some lines code format invalid.
The logic looks good to me.

Yunhua, could you please double check, I remember you have concerns about the previous version of this patch.

Thanks,
Bob

-----Original Message-----
From: Yeh, GregX <gregx.yeh@intel.com>
Sent: Wednesday, March 10, 2021 10:05 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 multiple 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/FmmtLib.c | 56 ++++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..26df0181c7 100644
--- a/BaseTools/Source/C/FMMT/FmmtLib.c
+++ b/BaseTools/Source/C/FMMT/FmmtLib.c
@@ -494,7 +494,7 @@ LibReadFvHeader (
     if ((FvLevel -1) == 0) {
       printf ("\n%s :\n", FvName);
     } else {
-      printf ("%sChild FV named FV%d of %s\n", BlankSpace, FvCount, FvName);
+      printf ("\n%sChild FV named FV%d of %s\n", BlankSpace, FvCount, 
+ FvName);
     }
   }
 
@@ -502,7 +502,7 @@ LibReadFvHeader (
   // Print FV header information
   //
   if (ViewFlag) {
-    printf ("\n%sAttributes:            %X\n", BlankSpace, (unsigned) VolumeHeader->Attributes);
+    printf ("%sAttributes:            %X\n", BlankSpace, (unsigned) VolumeHeader->Attributes);
     printf ("%sTotal Volume Size:     0x%08X\n", BlankSpace, (unsigned) VolumeHeader->FvLength);
     printf ("%sFree Volume Size:      0x%08X\n", BlankSpace, (unsigned) (VolumeHeader->FvLength - GetFreeOffset(InputFv)));
   }
@@ -789,7 +789,8 @@ LibParseSection (
   UINT8                  *FvCount,
   BOOLEAN                ViewFlag,
   BOOLEAN                ErasePolarity,
-  BOOLEAN                *IsFfsGenerated
+  BOOLEAN                *IsFfsGenerated,
+  BOOLEAN                IsFfs
   )
 {
   UINT32              ParsedLength;
@@ -997,8 +998,12 @@ LibParseSection (
       break;
 
     case EFI_SECTION_COMPRESSION:
-      if (FirstInFlag) {
-        Level ++;
+    	if (IsFfs){
+    	  Level ++;
+    	} else {
+        if (FirstInFlag) {
+          Level ++;
+        }
       }
       NumberOfSections ++;
 
@@ -1159,7 +1164,9 @@ LibParseSection (
                                   FvCount,
                                   ViewFlag,
                                   ErasePolarity,
-                                  IsFfsGenerated);
+                                  IsFfsGenerated,
+                                  FALSE
+                                  );
 
       if (CompressionType == EFI_STANDARD_COMPRESSION) {
         //
@@ -1181,8 +1188,12 @@ LibParseSection (
       // looks up the appropriate tool to use for extracting
       // a GUID defined FV section.
       //
-      if (FirstInFlag) {
+      if (IsFfs) {
         Level ++;
+      } else {
+        if (FirstInFlag) {
+          Level ++;
+        }
       }
       NumberOfSections++;
       EncapDataNeedUpdata = TRUE;
@@ -1216,7 +1227,8 @@ LibParseSection (
                 FvCount,
                 ViewFlag,
                 ErasePolarity,
-                IsFfsGenerated
+                IsFfsGenerated,
+                FALSE
                 );
         if (EFI_ERROR(Status)) {
           Error(NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL); @@ -1471,7 +1483,8 @@ LibParseSection (
                   FvCount,
                   ViewFlag,
                   ErasePolarity,
-                  IsFfsGenerated
+                  IsFfsGenerated,
+                  FALSE
                   );
         if (EFI_ERROR (Status)) {
           Error (NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL); @@ -1491,7 +1504,8 @@ LibParseSection (
             FvCount,
             ViewFlag,
             ErasePolarity,
-            IsFfsGenerated
+            IsFfsGenerated,
+            FALSE
             );
           if (ExtractionTool != NULL) {
             free (ExtractionTool);
@@ -2016,7 +2030,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;
@@ -2099,7 +2113,8 @@ LibGetFileInfo (
       FvCount,
       ViewFlag,
       ErasePolarity,
-      &IsGeneratedFfs
+      &IsGeneratedFfs,
+      TRUE
       );
     }
     if (EFI_ERROR (Status)) {
@@ -4198,10 +4213,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 +4242,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4279,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4295,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, 
+ LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList; @@ -4329,7 +4350,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type 
+ == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode; @@ -4818,6 +4839,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5099,6 @@ LibEncapNewFvFile(
     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;
--
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2
  2021-03-16  1:54   ` GregX Yeh
@ 2021-03-17  6:57     ` Bob Feng
  0 siblings, 0 replies; 18+ messages in thread
From: Bob Feng @ 2021-03-17  6:57 UTC (permalink / raw)
  To: Yeh, GregX, devel@edk2.groups.io, Yunhua Feng; +Cc: Liming Gao

Pushed. 4bfc77a87b820cd585ba10ca28aa957ae315c3dc

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

Hi Bob,

I have created new patch file. PatchCheck is pass.
Using Git send-email  send to edk2.group.io 

Thanks,
Greg

-----Original Message-----
From: Feng, Bob C <bob.c.feng@intel.com>
Sent: Monday, March 15, 2021 12:35 PM
To: Yeh, GregX <gregx.yeh@intel.com>; devel@edk2.groups.io; Yunhua Feng <fengyunhua@byosoft.com.cn>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Subject: RE: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2

Hi Greg,

Please use BaseTools\Scripts\PatchCheck.py to check this patch. There are some lines code format invalid.
The logic looks good to me.

Yunhua, could you please double check, I remember you have concerns about the previous version of this patch.

Thanks,
Bob

-----Original Message-----
From: Yeh, GregX <gregx.yeh@intel.com>
Sent: Wednesday, March 10, 2021 10:05 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 multiple 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/FmmtLib.c | 56 ++++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index b945e9b63d..26df0181c7 100644
--- a/BaseTools/Source/C/FMMT/FmmtLib.c
+++ b/BaseTools/Source/C/FMMT/FmmtLib.c
@@ -494,7 +494,7 @@ LibReadFvHeader (
     if ((FvLevel -1) == 0) {
       printf ("\n%s :\n", FvName);
     } else {
-      printf ("%sChild FV named FV%d of %s\n", BlankSpace, FvCount, FvName);
+      printf ("\n%sChild FV named FV%d of %s\n", BlankSpace, FvCount, 
+ FvName);
     }
   }
 
@@ -502,7 +502,7 @@ LibReadFvHeader (
   // Print FV header information
   //
   if (ViewFlag) {
-    printf ("\n%sAttributes:            %X\n", BlankSpace, (unsigned) VolumeHeader->Attributes);
+    printf ("%sAttributes:            %X\n", BlankSpace, (unsigned) VolumeHeader->Attributes);
     printf ("%sTotal Volume Size:     0x%08X\n", BlankSpace, (unsigned) VolumeHeader->FvLength);
     printf ("%sFree Volume Size:      0x%08X\n", BlankSpace, (unsigned) (VolumeHeader->FvLength - GetFreeOffset(InputFv)));
   }
@@ -789,7 +789,8 @@ LibParseSection (
   UINT8                  *FvCount,
   BOOLEAN                ViewFlag,
   BOOLEAN                ErasePolarity,
-  BOOLEAN                *IsFfsGenerated
+  BOOLEAN                *IsFfsGenerated,
+  BOOLEAN                IsFfs
   )
 {
   UINT32              ParsedLength;
@@ -997,8 +998,12 @@ LibParseSection (
       break;
 
     case EFI_SECTION_COMPRESSION:
-      if (FirstInFlag) {
-        Level ++;
+    	if (IsFfs){
+    	  Level ++;
+    	} else {
+        if (FirstInFlag) {
+          Level ++;
+        }
       }
       NumberOfSections ++;
 
@@ -1159,7 +1164,9 @@ LibParseSection (
                                   FvCount,
                                   ViewFlag,
                                   ErasePolarity,
-                                  IsFfsGenerated);
+                                  IsFfsGenerated,
+                                  FALSE
+                                  );
 
       if (CompressionType == EFI_STANDARD_COMPRESSION) {
         //
@@ -1181,8 +1188,12 @@ LibParseSection (
       // looks up the appropriate tool to use for extracting
       // a GUID defined FV section.
       //
-      if (FirstInFlag) {
+      if (IsFfs) {
         Level ++;
+      } else {
+        if (FirstInFlag) {
+          Level ++;
+        }
       }
       NumberOfSections++;
       EncapDataNeedUpdata = TRUE;
@@ -1216,7 +1227,8 @@ LibParseSection (
                 FvCount,
                 ViewFlag,
                 ErasePolarity,
-                IsFfsGenerated
+                IsFfsGenerated,
+                FALSE
                 );
         if (EFI_ERROR(Status)) {
           Error(NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL); @@ -1471,7 +1483,8 @@ LibParseSection (
                   FvCount,
                   ViewFlag,
                   ErasePolarity,
-                  IsFfsGenerated
+                  IsFfsGenerated,
+                  FALSE
                   );
         if (EFI_ERROR (Status)) {
           Error (NULL, 0, 0003, "parse of decoded GUIDED section failed", NULL); @@ -1491,7 +1504,8 @@ LibParseSection (
             FvCount,
             ViewFlag,
             ErasePolarity,
-            IsFfsGenerated
+            IsFfsGenerated,
+            FALSE
             );
           if (ExtractionTool != NULL) {
             free (ExtractionTool);
@@ -2016,7 +2030,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;
@@ -2099,7 +2113,8 @@ LibGetFileInfo (
       FvCount,
       ViewFlag,
       ErasePolarity,
-      &IsGeneratedFfs
+      &IsGeneratedFfs,
+      TRUE
       );
     }
     if (EFI_ERROR (Status)) {
@@ -4198,10 +4213,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 +4242,7 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;
-
+  EntryFvId                   = 0;
 
   OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
@@ -4261,6 +4279,9 @@ LibEncapNewFvFile(
     LocalEncapData = CurrentEncapData;
     if (LocalEncapData == NULL) {
         LocalEncapData = FvInFd->EncapData;
+        EntryFvId = 0xFFFFFFFF;
+    } else {
+      EntryFvId = LocalEncapData->FvId;
     }
     Level = LocalEncapData->Level;
     Type = LocalEncapData->Type;
@@ -4274,7 +4295,7 @@ LibEncapNewFvFile(
             LocalEncapDataNext = LocalEncapDataTemp->NextNode;
             if (LocalEncapDataNext != NULL && LocalEncapDataNext->NextNode != NULL) {
 
-                LibEncapNewFvFile(FvInFd, TemDir, LocalEncapDataTemp, 1, &ChildFileNameList);
+                LibEncapNewFvFile(FvInFd, TemDir, 
+ LocalEncapDataTemp,LocalEncapDataTemp->Level - 1, &ChildFileNameList);
                 ChildFileNameList->ParentLevel = LocalEncapDataTemp->Level -1;
                 if (FvInFd->ChildFvFFS == NULL) {
                     FvInFd->ChildFvFFS = ChildFileNameList; @@ -4329,7 +4350,7 @@ LibEncapNewFvFile(
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-      if (Level_Break > 1 && LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+      if ((LocalEncapData->FvId > EntryFvId) && (LocalEncapData->Type 
+ == FMMT_ENCAP_TREE_FFS)) {
         LocalEncapDataTemp = LocalEncapData->RightNode;
         while (LocalEncapDataTemp != NULL) {
             LocalEncapDataNext = LocalEncapDataTemp->NextNode; @@ -4818,6 +4839,7 @@ LibEncapNewFvFile(
       memcpy((char *)OutputFileNameList->FFSName, (char *)OutputFileName, strlen(OutputFileName)+1);
       OutputFileNameList->IsFFS = TRUE;
       OutputFileNameList->ParentLevel = ParentLevel - 1;
+      OutputFileNameList->InFvId = Id;
       if (OutputFileNameList->Next == NULL){
           break;
       }
@@ -5077,10 +5099,6 @@ LibEncapNewFvFile(
     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;
--
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2021-03-17  6:57 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-03-10  2:05 [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2 GregX Yeh
2021-03-15  4:34 ` Bob Feng
2021-03-16  1:13   ` 回复: [edk2-devel] " fengyunhua
2021-03-16  1:54   ` GregX Yeh
2021-03-17  6:57     ` Bob Feng
  -- strict thread matches above, loose matches on Subject: below --
2021-01-04  3:11 GregX Yeh
2020-12-08  7:32 GregX Yeh
2020-12-08  8:30 ` Bob Feng
2020-12-08  8:33   ` GregX Yeh
2020-12-18  9:33 ` Bob Feng
2020-12-23  8:00   ` GregX Yeh
2021-01-04  2:54     ` Bob Feng
2020-12-04  9:04 GregX Yeh
2020-12-01  7:43 GregX Yeh
2020-11-27  2:16 GregX Yeh
2020-11-27  1:38 GregX Yeh
2020-11-27  2:08 ` Bob Feng
2020-11-26  7:07 GregX Yeh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox