[edk2-devel] [PATCH] [edk2-staging]BaseTools/Fmmt: Replace FFS in FV include multiple GUID section

Feng, YunhuaX posted 1 patch 3 years, 9 months ago
Failed in applying to current master (apply log)
BaseTools/Source/C/FMMT/FirmwareModuleManagement.h |   6 +
BaseTools/Source/C/FMMT/FmmtLib.c                  | 206 +++++++++++++++++----
2 files changed, 181 insertions(+), 31 deletions(-)
[edk2-devel] [PATCH] [edk2-staging]BaseTools/Fmmt: Replace FFS in FV include multiple GUID section
Posted by Feng, YunhuaX 3 years, 9 months ago
Fmmt replace case like as below:
One Fv include one GUID section, and the GUID section include multiple sub
GUID section.

This patch will support the replace case.

Change-Id: I78f015e00f2b73e4e0e9ee36d03d24c697783a9c
Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
---
 BaseTools/Source/C/FMMT/FirmwareModuleManagement.h |   6 +
 BaseTools/Source/C/FMMT/FmmtLib.c                  | 206 +++++++++++++++++----
 2 files changed, 181 insertions(+), 31 deletions(-)

diff --git a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
index 601682c2ad..9d09c9160a 100644
--- a/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
+++ b/BaseTools/Source/C/FMMT/FirmwareModuleManagement.h
@@ -204,10 +204,14 @@ typedef struct {
   //
   // Describe the position of the FFS file.
   //
   UINT8            Level;
   //
+  //Describe the belong to FV
+  //
+  UINT8           FvId;
+  //
   // If this FFS has no encapsulate section, this flag will set to True.
   //
   BOOLEAN          IsLeaf;
   //
   // Section type for each section in FFS.
@@ -270,10 +274,11 @@ typedef struct __ENCAP_INFO_DATA{
 } ENCAP_INFO_DATA;

 typedef struct _FFS_INFOMATION{
   CHAR8                      *FFSName;
   UINT32                     InFvId;
+  UINT8                      FvId;
   UINT8                      ParentLevel;
   BOOLEAN                    IsFFS;
   CHAR16                     UiName[_MAX_PATH];
   UINT32                     UiNameSize;
   UINT8                      *Depex;
@@ -299,10 +304,11 @@ typedef struct _FV_INFOMATION{
   UINT8                      FvLevel;
   CHAR8                      *FvUiName;
   UINT8                      MulFvLevel;
   CHAR8                      AlignmentStr[16];
   FFS_INFORMATION            *ChildFvFFS;
+  FFS_INFORMATION            *ChildGuid;
 } FV_INFORMATION;

 typedef struct _FIRMWARE_DEVICE {
   ///
   /// Size of FD file
diff --git a/BaseTools/Source/C/FMMT/FmmtLib.c b/BaseTools/Source/C/FMMT/FmmtLib.c
index 401db5a48e..2c3529680e 100644
--- a/BaseTools/Source/C/FMMT/FmmtLib.c
+++ b/BaseTools/Source/C/FMMT/FmmtLib.c
@@ -167,10 +167,11 @@ LibInitializeFvStruct (
     Fv->FfsAttuibutes[Index].Depex                = NULL;
     Fv->FfsAttuibutes[Index].DepexLen             = 0;
     Fv->FfsAttuibutes[Index].IsHandle             = FALSE;
     Fv->FfsAttuibutes[Index].IsFvStart            = FALSE;
     Fv->FfsAttuibutes[Index].IsFvEnd              = FALSE;
+    Fv->FfsAttuibutes[Index].FvId                 = 0;
   }

   Fv->EncapData = NULL;
   Fv->FvNext = NULL;
   Fv->ChildFvFFS = NULL;
@@ -830,10 +831,11 @@ LibParseSection (
   UINT16              DataOffset;
   CHAR8               *UIFileName;
   CHAR8               *ToolInputFileName;
   CHAR8               *ToolOutputFileName;
   BOOLEAN              HasUiSection;
+  BOOLEAN              FirstInFlag;

   DataOffset                 = 0;
   GuidAttr                   = 0;
   ParsedLength               = 0;
   ToolOutputLength           = 0;
@@ -865,10 +867,11 @@ LibParseSection (
   ToolOutputFileFullName     = NULL;
   HasDepexSection            = FALSE;
   EncapDataNeedUpdata        = TRUE;
   LargeHeaderOffset          = 0;
   HasUiSection               = FALSE;
+  FirstInFlag                = TRUE;


   while (ParsedLength < BufferLength) {
     Ptr           = SectionBuffer + ParsedLength;

@@ -897,11 +900,13 @@ LibParseSection (
     case EFI_SECTION_FIRMWARE_VOLUME_IMAGE:

       EncapDataNeedUpdata = TRUE;
       HasUiSection = TRUE;

-      Level ++;
+      if (FirstInFlag) {
+        Level ++;
+      }
       NumberOfSections ++;

       CurrentFv->FfsAttuibutes[*FfsCount].IsLeaf = FALSE;
       CurrentFv->FfsAttuibutes[*FfsCount].IsFvStart = TRUE;
       //
@@ -992,11 +997,13 @@ LibParseSection (
         CurrentFv->FfsAttuibutes[*FfsCount -1].IsFvEnd = TRUE;
       }
       break;

     case EFI_SECTION_COMPRESSION:
-      Level ++;
+      if (FirstInFlag) {
+        Level ++;
+      }
       NumberOfSections ++;

       EncapDataNeedUpdata = TRUE;
       HasUiSection = TRUE;
       //
@@ -1027,10 +1034,11 @@ LibParseSection (

         LocalEncapData        = LocalEncapData->NextNode;

         LocalEncapData->Level = Level;
         LocalEncapData->Type  = FMMT_ENCAP_TREE_COMPRESS_SECTION;
+        LocalEncapData->FvId = *FvCount;

         //
         // Store the compress type
         //
         LocalEncapData->Data     = malloc (sizeof (UINT8));
@@ -1052,10 +1060,11 @@ LibParseSection (
         }
         LocalEncapData        = LocalEncapData->RightNode;

         LocalEncapData->Level = Level;
         LocalEncapData->Type  = FMMT_ENCAP_TREE_COMPRESS_SECTION;
+        LocalEncapData->FvId  = *FvCount;

         //
         // Store the compress type
         //
         LocalEncapData->Data     = malloc (sizeof (UINT8));
@@ -1172,20 +1181,25 @@ LibParseSection (
       //
       // Process GUID defined
       // looks up the appropriate tool to use for extracting
       // a GUID defined FV section.
       //
-      Level ++;
+      if (FirstInFlag) {
+        Level ++;
+      }
       NumberOfSections++;
       EncapDataNeedUpdata = TRUE;
       HasUiSection = TRUE;
       //
       // Put in encapsulate data information.
       //
       LocalEncapData = *CurrentFvEncapData;
       if (LocalEncapData->NextNode != NULL) {
         EncapDataNeedUpdata = FALSE;
+        while (Level != LocalEncapData->Level) {
+          LocalEncapData = LocalEncapData->NextNode;
+        }
         while (LocalEncapData->RightNode != NULL) {
           LocalEncapData = LocalEncapData->RightNode;
         }
       }
       GuidAttr = ((EFI_GUID_DEFINED_SECTION *)(Ptr + LargeHeaderOffset))->Attributes;
@@ -1237,10 +1251,11 @@ LibParseSection (
         LocalEncapData->Level = Level;
         LocalEncapData->Type  = FMMT_ENCAP_TREE_GUIDED_SECTION;
         LocalEncapData->Depex = NULL;
         LocalEncapData->DepexLen = 0;
         LocalEncapData->UiNameSize = 0;
+        LocalEncapData->FvId  = *FvCount;
         //
         // We don't need additional data for encapsulate this FFS but type.
         // include DataOffset + Attributes
         //

@@ -1269,10 +1284,11 @@ LibParseSection (
         LocalEncapData->Level = Level;
         LocalEncapData->Type  = FMMT_ENCAP_TREE_GUIDED_SECTION;
         LocalEncapData->Depex = NULL;
         LocalEncapData->DepexLen = 0;
         LocalEncapData->UiNameSize = 0;
+        LocalEncapData->FvId = *FvCount;
         //
         // We don't need additional data for encapsulate this FFS but type.
         // include DataOffset + Attributes
         //

@@ -1508,10 +1524,13 @@ LibParseSection (

     //
     //Leaf sections
     //
     case EFI_SECTION_RAW:
+      if (FirstInFlag) {
+        Level++;
+      }
       NumberOfSections ++;
       CurrentFv->FfsAttuibutes[*FfsCount].Level = Level;
       if (!ViewFlag) {
         if (!*IsFfsGenerated) {
           LibGenFfsFile(CurrentFile, CurrentFv, FvName, Level, FfsCount, ErasePolarity);
@@ -1669,10 +1688,11 @@ LibParseSection (
     default:
       break;
     }

     ParsedLength += SectionLength;
+    FirstInFlag  = FALSE;
     //
     // We make then next section begin on a 4-byte boundary
     //
     ParsedLength = GetOccupiedSize (ParsedLength, 4);
   }
@@ -1958,10 +1978,11 @@ LibGetFileInfo (
       LocalEncapData->Type  = FMMT_ENCAP_TREE_FFS;
       LocalEncapData->FvExtHeader = NULL;
       LocalEncapData->Depex = NULL;
       LocalEncapData->DepexLen = 0;
       LocalEncapData->UiNameSize = 0;
+      LocalEncapData->FvId = *FvCount;
       //
       // Store the header of FFS file.
       //
       LocalEncapData->Data     = malloc (FfsFileHeaderSize);
       if (LocalEncapData->Data == NULL) {
@@ -2005,10 +2026,11 @@ LibGetFileInfo (
       LocalEncapData->Type  = FMMT_ENCAP_TREE_FFS;
     LocalEncapData->FvExtHeader = NULL;
       LocalEncapData->Depex = NULL;
       LocalEncapData->DepexLen = 0;
       LocalEncapData->UiNameSize = 0;
+      LocalEncapData->FvId = *FvCount;
       //
       // Store the header of FFS file.
       //
       LocalEncapData->Data     = malloc (FfsFileHeaderSize);
       if (LocalEncapData->Data == NULL) {
@@ -2325,10 +2347,11 @@ LibGetFvInfo (
     CurrentFv->FfsHeader[*FfsCount].Type             = CurrentFile->Type;
     CurrentFv->FfsHeader[*FfsCount].ExtendedSize     = CurrentFile->ExtendedSize;
     CurrentFv->FfsAttuibutes[*FfsCount].Offset = Key - GetFfsFileLength ((EFI_FFS_FILE_HEADER *) CurrentFile);

     CurrentFv->FfsAttuibutes[*FfsCount].FvLevel = CurrentFv->FvLevel;
+    CurrentFv->FfsAttuibutes[*FfsCount].FvId    = *FvCount;
      if (CurrentFv->FvLevel > CurrentFv->MulFvLevel) {
       CurrentFv->MulFvLevel = CurrentFv->FvLevel;
    }
     //
     // Display info about this file
@@ -4170,20 +4193,23 @@ LibEncapNewFvFile(
   FILE                        *InputFile;
   FILE                        *OutFile;
   UINT32                      InputFileSize;
   UINT32                      OutputFileSize;
   UINT32                      LargeFileSize;
+  UINT32                      TmpFileSize;
+  UINT32                      AlignmentFileSize;
   UINT8                       *Buffer = NULL;
   UINT8                       SectionHeader[4] = { 0x00, 0x00, 0x00, 0x00 };
   UINT32                      Id;
   UINT32                      SubFvId;
   UINT32                      header;
   UINT8                       AlignN;
   UINT8                       AlignV[1] = {0xFF};
   AlignN                      = 0;
   Id                          = 0;
   InputFileSize               = 0;
+  TmpFileSize                 = 0;
   EncapFvIndex                = 0;
   Index                       = 0;
   OuterIndex                  = 0;
   ParentType                  = 0;
   ParentLevel                 = 0;
@@ -4204,11 +4230,11 @@ LibEncapNewFvFile(
   IsLargeFile                 = FALSE;
   OutputFileSize              = 0;
   LargeFileSize               = 0x1000000;


-  OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FV_INFORMATION));
+  OutputFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (OutputFileNameList == NULL) {
     Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
     return EFI_OUT_OF_RESOURCES;
   }
   OutputFileNameList->FFSName = NULL;
@@ -4219,11 +4245,11 @@ LibEncapNewFvFile(
   OutputFileNameList->UiNameSize = 0;
   OutputFileNameList->Depex = NULL;
   OutputFileNameList->DepexLen = 0;
   OutputFileNameList->FfsFoundFlag = FALSE;

-  ChildFileNameList = (FFS_INFORMATION *)malloc(sizeof(FV_INFORMATION));
+  ChildFileNameList = (FFS_INFORMATION *)malloc(sizeof(FFS_INFORMATION));
   if (ChildFileNameList == NULL) {
     Error ("FMMT", 0, 0004, "Out of resource, memory allocation failed! \n", "");
     return EFI_OUT_OF_RESOURCES;
   }
   ChildFileNameList->FFSName = NULL;
@@ -4268,10 +4294,36 @@ LibEncapNewFvFile(
             }
             LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
         }
       }

+      //
+      //One Fv include multiple GUIDED SECTION
+      //
+      if (LocalEncapData->Type == FMMT_ENCAP_TREE_GUIDED_SECTION) {
+        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;
+            ChildFileNameList->FfsFoundFlag = FALSE;
+            ChildFileNameList->InFvId = 0;
+            if (FvInFd->ChildGuid == NULL) {
+              FvInFd->ChildGuid = ChildFileNameList;
+            } else {
+              NewFileNameList = FvInFd->ChildGuid;
+              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;
             ParentType  = Type;
         }
@@ -4281,19 +4333,39 @@ LibEncapNewFvFile(
       LocalEncapData = LocalEncapData->NextNode;
     }
   } else {
     LocalEncapData = CurrentEncapData;
     while (LocalEncapData != NULL) {
-        if (LocalEncapData->Level > Level) {
-            if (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
-                ParentLevel = Level;
-                ParentType  = Type;
+      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;
+                }
             }
-            Level       = LocalEncapData->Level;
-            Type        = LocalEncapData->Type;
+            LocalEncapDataTemp = LocalEncapDataTemp->RightNode;
         }
-        LocalEncapData = LocalEncapData->NextNode;
+      }
+      if (LocalEncapData->Level > Level) {
+        if (LocalEncapData->Type == FMMT_ENCAP_TREE_FFS) {
+          ParentLevel = Level;
+          ParentType  = Type;
+        }
+        Level       = LocalEncapData->Level;
+        Type        = LocalEncapData->Type;
+      }
+      LocalEncapData = LocalEncapData->NextNode;
     }
   }

   do {
     switch (ParentType) {
@@ -4401,12 +4473,16 @@ LibEncapNewFvFile(
           }
           FvGuidName = NULL;
         }

         if (CurrentEncapData != NULL) {
+          LocalEncapData = CurrentEncapData;
+          while (LocalEncapData->Level != ParentLevel) {
+            LocalEncapData = LocalEncapData->NextNode;
+          }
             for (Index = 0; Index <= FvInFd->FfsNumbers; Index++) {
-                if ((memcmp(&FvInFd->FfsAttuibutes[Index].GuidName, &(CurrentEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)) {
+                if ((memcmp(&FvInFd->FfsAttuibutes[Index].GuidName, &(LocalEncapData->FvExtHeader->FvName), sizeof(EFI_GUID)) == 0)) {
                     SubFvId = Index;
                     break;
                 }
             }
         }
@@ -4437,11 +4513,11 @@ LibEncapNewFvFile(
                 }
             }

             NewFileNameList = FvInFd->ChildFvFFS;
             while (NewFileNameList != NULL && NewFileNameList -> FFSName != NULL) {
-                if (NewFileNameList -> ParentLevel == ParentLevel && Index == NewFileNameList->InFvId && NewFileNameList->FfsFoundFlag==TRUE) {
+                if (NewFileNameList->FvId == LocalEncapData->FvId && 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);
@@ -4451,10 +4527,11 @@ LibEncapNewFvFile(
                         fclose (InfFile);
                         free (OutputFileNameList);
                         free (ChildFileNameList);
                         return Status;
                     }
+                  NewFileNameList->FfsFoundFlag = FALSE;
                 }
                 NewFileNameList = NewFileNameList->Next;
             }

             if (FvInFd->FfsAttuibutes[Index].IsHandle==TRUE) {
@@ -4471,11 +4548,11 @@ LibEncapNewFvFile(
             IsLeafFlagIgnore = TRUE;
             } else {
               IsLeafFlagIgnore = FvInFd->FfsAttuibutes[Index].IsLeaf;
             }

-          if (FvInFd->FfsAttuibutes[Index].Level >= ParentLevel + 1 && IsLeafFlagIgnore) {
+          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;
@@ -4493,28 +4570,28 @@ LibEncapNewFvFile(
               if (Index == 0) {
                 // Root FV need to include all FFS files.
                 IsRootFv = TRUE;
               }
             } else {
-          if (FvInFd->FfsAttuibutes[Index].Level < 0xFF) {
-          FfsFoundFlag = TRUE;
-          Status = LibAddFfsFileToFvInf (FvInFd->FfsAttuibutes[Index].FfsName, InfFile, FALSE);
-          FvInFd->FfsAttuibutes[Index].IsHandle=TRUE;
-        }
+              if (FvInFd->FfsAttuibutes[Index].Level < 0xFF) {
+                FfsFoundFlag = TRUE;
+                Status = LibAddFfsFileToFvInf (FvInFd->FfsAttuibutes[Index].FfsName, InfFile, FALSE);
+                FvInFd->FfsAttuibutes[Index].IsHandle=TRUE;
+              }

-                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;
-                }
-                if (Index == 0) {
-                  // Root FV need to include all FFS files.
-                  IsRootFv = TRUE;
-                }
+              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;
               }
+              if (Index == 0) {
+                // Root FV need to include all FFS files.
+                IsRootFv = TRUE;
+              }
+            }


       //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) &&
@@ -4572,10 +4649,12 @@ LibEncapNewFvFile(
             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;
             OutputFileNameList->DepexLen = FvInFd->FfsAttuibutes[EncapFvIndex - 1].DepexLen;
             OutputFileNameList->FfsFoundFlag = FfsFoundFlag;
+            OutputFileNameList->FvId = FvInFd->FfsAttuibutes[EncapFvIndex - 1].FvId;
+            OutputFileNameList->ParentLevel = ParentLevel - 1;
         }
       }
     }
         break;
       case FMMT_ENCAP_TREE_FFS:
@@ -4742,10 +4821,11 @@ LibEncapNewFvFile(
           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;
     }
@@ -4754,10 +4834,69 @@ LibEncapNewFvFile(
       while(OutputFileNameList!= NULL && OutputFileNameList->FFSName != NULL){
         //
         // Create the guided section original data, do compress operation.
         //
         InputFileName  = OutputFileNameList->FFSName;
+        //
+        //Fv include multiple Guided section
+        //
+        if (CurrentEncapData == NULL && FvInFd->ChildGuid != NULL) {
+          NewFileNameList = FvInFd->ChildGuid;
+          while (NewFileNameList != NULL) {
+            if (NewFileNameList->FFSName != NULL && NewFileNameList->ParentLevel == ParentLevel) {
+              InputFile = fopen(InputFileName, "rb+");
+              if (InputFile == NULL) {
+                Error("FMMT", 0, 0004, "Could not open input file %s! \n", InputFileName);
+                return EFI_ABORTED;
+              }
+              fseek(InputFile, 0, SEEK_SET);
+              fseek(InputFile, 0, SEEK_END);
+              InputFileSize = ftell(InputFile);
+              fseek(InputFile, 0, SEEK_SET);
+              TmpFile = fopen(NewFileNameList->FFSName, "rb+");
+              if (TmpFile == NULL) {
+                Error("FMMT", 0, 0004, "Could not open input file %s! \n", NewFileNameList->FFSName);
+                return EFI_ABORTED;
+              }
+              fseek(TmpFile, 0, SEEK_SET);
+              fseek(TmpFile, 0, SEEK_END);
+              TmpFileSize = ftell(TmpFile);
+              fseek(TmpFile, 0, SEEK_SET);
+              //
+              //Guided section need 4 bytes alignment
+              //
+              AlignmentFileSize = (InputFileSize + 3) & (UINT32)~3;
+              Buffer = malloc(AlignmentFileSize + TmpFileSize);
+              //
+              //Alignment filled 00
+              //
+              memset(Buffer, 0, AlignmentFileSize+ TmpFileSize);
+              if (fread (Buffer, 1, InputFileSize, InputFile) != InputFileSize) {
+                Error("FMMT", 0, 0004, "Could not open guided file %s to add GUIDED section information! \n", InputFileName);
+                fclose(InputFile);
+                fclose(TmpFile);
+                free (Buffer);
+                return EFI_ABORTED;
+              }
+              fseek(InputFile, 0, SEEK_SET);
+              if (fread (Buffer + AlignmentFileSize, 1, TmpFileSize, TmpFile) != TmpFileSize) {
+                Error("FMMT", 0, 0004, "Could not open guided file %s to add GUIDED section information! \n", NewFileNameList->FFSName);
+                fclose(InputFile);
+                fclose(TmpFile);
+                free (Buffer);
+                return EFI_ABORTED;
+              }
+              fwrite(Buffer, 1, AlignmentFileSize + TmpFileSize, InputFile);
+              free(NewFileNameList->FFSName);
+              NewFileNameList->FFSName = NULL;
+              free(Buffer);
+              fclose(TmpFile);
+              fclose(InputFile);
+            }
+            NewFileNameList = NewFileNameList->Next;
+          }
+        }
         OutputFileName= LibFilenameStrExtended (strrchr(GenTempFile (), OS_SEP), TemDir, "compressed");

         //
         // Use the guided section header guid to find out compress application name.
         //
@@ -4819,10 +4958,11 @@ LibEncapNewFvFile(
           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;
     }
@@ -4855,10 +4995,11 @@ LibEncapNewFvFile(
             Error ("FMMT", 0, 0004, "error while encapsulate FD Image", "Generate compressed section failed!");
             free (OutputFileNameList);
             free (ChildFileNameList);
             return Status;
           }
+          OutputFileNameList->ParentLevel = ParentLevel - 1;
           if (OutputFileNameList->Next == NULL){
             break;
           }
           OutputFileNameList = OutputFileNameList->Next;
         }
@@ -4910,10 +5051,11 @@ LibEncapNewFvFile(
           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;
     }
@@ -4928,10 +5070,12 @@ LibEncapNewFvFile(
                 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;
             }
         }
     }
--
2.12.2.windows.2


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#60716): https://edk2.groups.io/g/devel/message/60716
Mute This Topic: https://groups.io/mt/74667780/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-