From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by mx.groups.io with SMTP id smtpd.web10.13843.1606443424310461774 for ; Thu, 26 Nov 2020 18:17:04 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.65, mailfrom: gregx.yeh@intel.com) IronPort-SDR: Z0QZ61mYVqJvcfyGKl4FRrx04NqHjgsSDhHuiBD3R3FRcBZUvKjuP///p3oiRDwsO1szFhW8Lx eVkMfEsbgTEA== X-IronPort-AV: E=McAfee;i="6000,8403,9817"; a="172439343" X-IronPort-AV: E=Sophos;i="5.78,373,1599548400"; d="scan'208";a="172439343" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Nov 2020 18:17:03 -0800 IronPort-SDR: tqo8NkSfXX3Mme/EoiqqolkROIpuhlHTb27Vb3YoiK6wwzjcxO7fd8sPciAGrwSOtcj44A5+FN 6OY8kgO3l7gg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.78,373,1599548400"; d="scan'208";a="479533270" Received: from chinghux-desk1.gar.corp.intel.com ([10.5.215.141]) by orsmga004.jf.intel.com with ESMTP; 26 Nov 2020 18:17:01 -0800 From: "GregX Yeh" To: devel@edk2.groups.io Cc: Bob Feng , Liming Gao Subject: [PATCH] [edk2-staging] BaseTools/FMMT: Replace file failure when FV level over 2 Date: Fri, 27 Nov 2020 10:16:58 +0800 Message-Id: <20201127021658.11480-1-gregx.yeh@intel.com> X-Mailer: git-send-email 2.16.2.windows.1 Fixed replace file failure when FFS in multi level FV and FV level over 2. Signed-off-by: GregX Yeh Cc: Bob Feng Cc: Liming Gao --- 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