## @file\r
# process FV generation\r
#\r
-# Copyright (c) 2007, Intel Corporation\r
+# Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>\r
#\r
-# All rights reserved. This program and the accompanying materials\r
+# This program and the accompanying materials\r
# are licensed and made available under the terms and conditions of the BSD License\r
# which accompanies this distribution. The full text of the license may be found at\r
# http://opensource.org/licenses/bsd-license.php\r
##\r
# Import Modules\r
#\r
-import os\r
-import shutil\r
+import Common.LongFilePathOs as os\r
import subprocess\r
import StringIO\r
+from struct import *\r
\r
import Ffs\r
import AprioriSection\r
from GenFds import GenFds\r
from CommonDataClass.FdfClass import FvClassObject\r
from Common.Misc import SaveFileOnChange\r
+from Common.LongFilePathSupport import CopyLongFilePath\r
+from Common.LongFilePathSupport import OpenLongFilePath as open\r
\r
T_CHAR_LF = '\n'\r
+FV_UI_EXT_ENTY_GUID = 'A67DF1FA-8DE8-4E98-AF09-4BDF2EFFBC7C'\r
\r
## generate FV\r
#\r
self.InfFileName = None\r
self.FvAddressFileName = None\r
self.CapsuleName = None\r
-\r
+ self.FvBaseAddress = None\r
+ self.FvForceRebase = None\r
+ \r
## AddToBuffer()\r
#\r
# Generate Fv and add it to the Buffer\r
#\r
def AddToBuffer (self, Buffer, BaseAddress=None, BlockSize= None, BlockNum=None, ErasePloarity='1', VtfDict=None, MacroDict = {}) :\r
\r
- if self.UiFvName.upper() + 'fv' in GenFds.ImageBinDict.keys():\r
+ if BaseAddress == None and self.UiFvName.upper() + 'fv' in GenFds.ImageBinDict.keys():\r
return GenFds.ImageBinDict[self.UiFvName.upper() + 'fv']\r
\r
#\r
# If yes, return error. Doesn't support FV in Capsule image is also in FD flash region.\r
#\r
if self.CapsuleName != None:\r
- for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
- FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]
+ for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
+ FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]\r
for RegionObj in FdObj.RegionList:\r
if RegionObj.RegionType == 'FV':\r
for RegionData in RegionObj.RegionDataList:\r
elif RegionData.upper() + 'fv' in GenFds.ImageBinDict.keys():\r
continue\r
elif self.UiFvName.upper() == RegionData.upper():\r
- GenFdsGlobalVariable.ErrorLogger("Capsule %s in FD region can't contain a FV %s in FD region." % (self.CapsuleName, self.UiFvName.upper()))\r
+ GenFdsGlobalVariable.ErrorLogger("Capsule %s in FD region can't contain a FV %s in FD region." % (self.CapsuleName, self.UiFvName.upper()))\r
\r
GenFdsGlobalVariable.InfLogger( "\nGenerating %s FV" %self.UiFvName)\r
+ GenFdsGlobalVariable.LargeFileInFvFlags.append(False)\r
+ FFSGuid = None\r
+ \r
+ if self.FvBaseAddress != None:\r
+ BaseAddress = self.FvBaseAddress\r
\r
self.__InitializeInf__(BaseAddress, BlockSize, BlockNum, ErasePloarity, VtfDict)\r
#\r
\r
# Process Modules in FfsList\r
for FfsFile in self.FfsList :\r
- FileName = FfsFile.GenFfs(MacroDict)\r
+ if hasattr(FfsFile, 'FvFileType') and FfsFile.FvFileType == 'RAW':\r
+ if isinstance(FfsFile.FileName, list) and isinstance(FfsFile.Alignment, list) and len(FfsFile.FileName) == len(FfsFile.Alignment):\r
+ FileContent = ''\r
+ for Index, File in enumerate(FfsFile.FileName):\r
+ try:\r
+ f = open(File, 'r+b')\r
+ except:\r
+ GenFdsGlobalVariable.ErrorLogger("Error opening RAW file %s." % (File))\r
+ Content = f.read()\r
+ f.close()\r
+ AlignValue = FfsFile.Alignment[Index]\r
+ if AlignValue == None:\r
+ AlignValue = 1\r
+ FileContent += Content\r
+ if len(FileContent) % AlignValue != 0:\r
+ Size = AlignValue - len(FileContent) % AlignValue\r
+ for i in range(0, Size):\r
+ FileContent += pack('B', 0xFF)\r
+\r
+ if FileContent:\r
+ OutputRAWFile = os.path.join(GenFdsGlobalVariable.FfsDir, FfsFile.NameGuid, FfsFile.NameGuid + '.raw')\r
+ SaveFileOnChange(OutputRAWFile, FileContent, True)\r
+ FfsFile.FileName = OutputRAWFile\r
+ if max(FfsFile.Alignment):\r
+ FfsFile.Alignment = str(max(FfsFile.Alignment))\r
+ else:\r
+ FfsFile.Alignment = None\r
+\r
+ FileName = FfsFile.GenFfs(MacroDict, FvParentAddr=BaseAddress)\r
FfsFileList.append(FileName)\r
self.FvInfFile.writelines("EFI_FILE_NAME = " + \\r
FileName + \\r
FvOutputFile = self.CreateFileName\r
\r
FvInfoFileName = os.path.join(GenFdsGlobalVariable.FfsDir, self.UiFvName + '.inf')\r
- shutil.copy(GenFdsGlobalVariable.FvAddressFileName, FvInfoFileName)\r
+ CopyLongFilePath(GenFdsGlobalVariable.FvAddressFileName, FvInfoFileName)\r
+ OrigFvInfo = None\r
+ if os.path.exists (FvInfoFileName):\r
+ OrigFvInfo = open(FvInfoFileName, 'r').read()\r
+ if GenFdsGlobalVariable.LargeFileInFvFlags[-1]:\r
+ FFSGuid = GenFdsGlobalVariable.EFI_FIRMWARE_FILE_SYSTEM3_GUID;\r
GenFdsGlobalVariable.GenerateFirmwareVolume(\r
FvOutputFile,\r
[self.InfFileName],\r
AddressFile=FvInfoFileName,\r
- FfsList=FfsFileList\r
+ FfsList=FfsFileList,\r
+ ForceRebase=self.FvForceRebase,\r
+ FileSystemGuid=FFSGuid\r
)\r
\r
+ NewFvInfo = None\r
+ if os.path.exists (FvInfoFileName):\r
+ NewFvInfo = open(FvInfoFileName, 'r').read()\r
+ if NewFvInfo != None and NewFvInfo != OrigFvInfo:\r
+ FvChildAddr = []\r
+ AddFileObj = open(FvInfoFileName, 'r')\r
+ AddrStrings = AddFileObj.readlines()\r
+ AddrKeyFound = False\r
+ for AddrString in AddrStrings:\r
+ if AddrKeyFound:\r
+ #get base address for the inside FvImage\r
+ FvChildAddr.append (AddrString)\r
+ elif AddrString.find ("[FV_BASE_ADDRESS]") != -1:\r
+ AddrKeyFound = True\r
+ AddFileObj.close()\r
+\r
+ if FvChildAddr != []:\r
+ # Update Ffs again\r
+ for FfsFile in self.FfsList :\r
+ FileName = FfsFile.GenFfs(MacroDict, FvChildAddr, BaseAddress)\r
+ \r
+ if GenFdsGlobalVariable.LargeFileInFvFlags[-1]:\r
+ FFSGuid = GenFdsGlobalVariable.EFI_FIRMWARE_FILE_SYSTEM3_GUID;\r
+ #Update GenFv again\r
+ GenFdsGlobalVariable.GenerateFirmwareVolume(\r
+ FvOutputFile,\r
+ [self.InfFileName],\r
+ AddressFile=FvInfoFileName,\r
+ FfsList=FfsFileList,\r
+ ForceRebase=self.FvForceRebase,\r
+ FileSystemGuid=FFSGuid\r
+ )\r
+\r
#\r
# Write the Fv contents to Buffer\r
#\r
- FvFileObj = open ( FvOutputFile,'r+b')\r
+ if os.path.isfile(FvOutputFile):\r
+ FvFileObj = open ( FvOutputFile,'r+b')\r
\r
- GenFdsGlobalVariable.VerboseLogger( "\nGenerate %s FV Successfully" %self.UiFvName)\r
- GenFdsGlobalVariable.SharpCounter = 0\r
+ GenFdsGlobalVariable.VerboseLogger( "\nGenerate %s FV Successfully" %self.UiFvName)\r
+ GenFdsGlobalVariable.SharpCounter = 0\r
\r
- Buffer.write(FvFileObj.read())\r
- FvFileObj.close()\r
- GenFds.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile\r
+ Buffer.write(FvFileObj.read())\r
+ FvFileObj.seek(0)\r
+ # PI FvHeader is 0x48 byte\r
+ FvHeaderBuffer = FvFileObj.read(0x48)\r
+ # FV alignment position.\r
+ FvAlignmentValue = 1 << (ord (FvHeaderBuffer[0x2E]) & 0x1F)\r
+ # FvAlignmentValue is larger than or equal to 1K\r
+ if FvAlignmentValue >= 0x400:\r
+ if FvAlignmentValue >= 0x10000:\r
+ #The max alignment supported by FFS is 64K.\r
+ self.FvAlignment = "64K"\r
+ else:\r
+ self.FvAlignment = str (FvAlignmentValue / 0x400) + "K"\r
+ else:\r
+ # FvAlignmentValue is less than 1K\r
+ self.FvAlignment = str (FvAlignmentValue)\r
+ FvFileObj.close()\r
+ GenFds.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile\r
+ GenFdsGlobalVariable.LargeFileInFvFlags.pop()\r
+ else:\r
+ GenFdsGlobalVariable.ErrorLogger("Failed to generate %s FV file." %self.UiFvName)\r
return FvOutputFile\r
\r
+ ## _GetBlockSize()\r
+ #\r
+ # Calculate FV's block size\r
+ # Inherit block size from FD if no block size specified in FV\r
+ #\r
+ def _GetBlockSize(self):\r
+ if self.BlockSizeList:\r
+ return True\r
+\r
+ for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
+ FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]\r
+ for RegionObj in FdObj.RegionList:\r
+ if RegionObj.RegionType != 'FV':\r
+ continue\r
+ for RegionData in RegionObj.RegionDataList:\r
+ #\r
+ # Found the FD and region that contain this FV\r
+ #\r
+ if self.UiFvName.upper() == RegionData.upper():\r
+ RegionObj.BlockInfoOfRegion(FdObj.BlockSizeList, self)\r
+ if self.BlockSizeList:\r
+ return True\r
+ return False\r
+\r
## __InitializeInf__()\r
#\r
# Initilize the inf file to create FV\r
' 0x%X' %BlockNum + \\r
T_CHAR_LF)\r
else:\r
+ if self.BlockSizeList == []:\r
+ if not self._GetBlockSize():\r
+ #set default block size is 1\r
+ self.FvInfFile.writelines("EFI_BLOCK_SIZE = 0x1" + T_CHAR_LF)\r
+ \r
for BlockSize in self.BlockSizeList :\r
if BlockSize[0] != None:\r
self.FvInfFile.writelines("EFI_BLOCK_SIZE = " + \\r
self.FvAlignment.strip() + \\r
" = TRUE" + \\r
T_CHAR_LF)\r
- \r
- if self.FvNameGuid != None:\r
- self.FvInfFile.writelines("EFI_FVNAME_GUID" + \\r
- " = %s" % self.FvNameGuid + \\r
- T_CHAR_LF)\r
+ \r
#\r
- # Add [Files]\r
+ # Generate FV extension header file\r
#\r
+ if self.FvNameGuid == None or self.FvNameGuid == '':\r
+ if len(self.FvExtEntryType) > 0:\r
+ GenFdsGlobalVariable.ErrorLogger("FV Extension Header Entries declared for %s with no FvNameGuid declaration." % (self.UiFvName))\r
+ \r
+ if self.FvNameGuid <> None and self.FvNameGuid <> '':\r
+ TotalSize = 16 + 4\r
+ Buffer = ''\r
+ if self.FvNameString == 'TRUE':\r
+ #\r
+ # Create EXT entry for FV UI name\r
+ # This GUID is used: A67DF1FA-8DE8-4E98-AF09-4BDF2EFFBC7C\r
+ #\r
+ FvUiLen = len(self.UiFvName)\r
+ TotalSize += (FvUiLen + 16 + 4)\r
+ Guid = FV_UI_EXT_ENTY_GUID.split('-')\r
+ #\r
+ # Layout:\r
+ # EFI_FIRMWARE_VOLUME_EXT_ENTRY : size 4\r
+ # GUID : size 16\r
+ # FV UI name\r
+ #\r
+ Buffer += (pack('HH', (FvUiLen + 16 + 4), 0x0002)\r
+ + pack('=LHHBBBBBBBB', int(Guid[0], 16), int(Guid[1], 16), int(Guid[2], 16),\r
+ int(Guid[3][-4:-2], 16), int(Guid[3][-2:], 16), int(Guid[4][-12:-10], 16),\r
+ int(Guid[4][-10:-8], 16), int(Guid[4][-8:-6], 16), int(Guid[4][-6:-4], 16),\r
+ int(Guid[4][-4:-2], 16), int(Guid[4][-2:], 16))\r
+ + self.UiFvName)\r
\r
+ for Index in range (0, len(self.FvExtEntryType)):\r
+ if self.FvExtEntryType[Index] == 'FILE':\r
+ # check if the path is absolute or relative\r
+ if os.path.isabs(self.FvExtEntryData[Index]):\r
+ FileFullPath = os.path.normpath(self.FvExtEntryData[Index])\r
+ else:\r
+ FileFullPath = os.path.normpath(os.path.join(GenFdsGlobalVariable.WorkSpaceDir, self.FvExtEntryData[Index]))\r
+ # check if the file path exists or not\r
+ if not os.path.isfile(FileFullPath):\r
+ GenFdsGlobalVariable.ErrorLogger("Error opening FV Extension Header Entry file %s." % (self.FvExtEntryData[Index]))\r
+ FvExtFile = open (FileFullPath,'rb')\r
+ FvExtFile.seek(0,2)\r
+ Size = FvExtFile.tell()\r
+ if Size >= 0x10000:\r
+ GenFdsGlobalVariable.ErrorLogger("The size of FV Extension Header Entry file %s exceeds 0x10000." % (self.FvExtEntryData[Index]))\r
+ TotalSize += (Size + 4)\r
+ FvExtFile.seek(0)\r
+ Buffer += pack('HH', (Size + 4), int(self.FvExtEntryTypeValue[Index], 16))\r
+ Buffer += FvExtFile.read() \r
+ FvExtFile.close()\r
+ if self.FvExtEntryType[Index] == 'DATA':\r
+ ByteList = self.FvExtEntryData[Index].split(',')\r
+ Size = len (ByteList)\r
+ if Size >= 0x10000:\r
+ GenFdsGlobalVariable.ErrorLogger("The size of FV Extension Header Entry data %s exceeds 0x10000." % (self.FvExtEntryData[Index]))\r
+ TotalSize += (Size + 4)\r
+ Buffer += pack('HH', (Size + 4), int(self.FvExtEntryTypeValue[Index], 16))\r
+ for Index1 in range (0, Size):\r
+ Buffer += pack('B', int(ByteList[Index1], 16))\r
+\r
+ Guid = self.FvNameGuid.split('-')\r
+ Buffer = pack('=LHHBBBBBBBBL', \r
+ int(Guid[0], 16), \r
+ int(Guid[1], 16), \r
+ int(Guid[2], 16), \r
+ int(Guid[3][-4:-2], 16), \r
+ int(Guid[3][-2:], 16), \r
+ int(Guid[4][-12:-10], 16),\r
+ int(Guid[4][-10:-8], 16),\r
+ int(Guid[4][-8:-6], 16),\r
+ int(Guid[4][-6:-4], 16),\r
+ int(Guid[4][-4:-2], 16),\r
+ int(Guid[4][-2:], 16),\r
+ TotalSize\r
+ ) + Buffer\r
+\r
+ #\r
+ # Generate FV extension header file if the total size is not zero\r
+ #\r
+ if TotalSize > 0:\r
+ FvExtHeaderFileName = os.path.join(GenFdsGlobalVariable.FvDir, self.UiFvName + '.ext')\r
+ FvExtHeaderFile = StringIO.StringIO()\r
+ FvExtHeaderFile.write(Buffer)\r
+ Changed = SaveFileOnChange(FvExtHeaderFileName, FvExtHeaderFile.getvalue(), True)\r
+ FvExtHeaderFile.close()\r
+ if Changed:\r
+ if os.path.exists (self.InfFileName):\r
+ os.remove (self.InfFileName)\r
+ self.FvInfFile.writelines("EFI_FV_EXT_HEADER_FILE_NAME = " + \\r
+ FvExtHeaderFileName + \\r
+ T_CHAR_LF)\r
+\r
+ \r
+ #\r
+ # Add [Files]\r
+ #\r
self.FvInfFile.writelines("[files]" + T_CHAR_LF)\r
if VtfDict != None and self.UiFvName in VtfDict.keys():\r
self.FvInfFile.writelines("EFI_FILE_NAME = " + \\r
VtfDict.get(self.UiFvName) + \\r
T_CHAR_LF)\r
-\r
-\r