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

GregX Yeh gregx.yeh at intel.com
Fri Nov 27 02:16:58 UTC 2020


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

Signed-off-by: GregX Yeh <gregx.yeh at intel.com>
Cc: Bob Feng <bob.c.feng at intel.com>
Cc: Liming Gao <gaoliming at 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



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#68036): https://edk2.groups.io/g/devel/message/68036
Mute This Topic: https://groups.io/mt/78525275/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-





More information about the edk2-devel-archive mailing list