]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenFds/Fv.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / Fv.py
index 3953756e0f8fb3e489fa66b650bd784707826a8e..206bd02005977d29e276d429fff532eb2aa016f7 100644 (file)
@@ -1,48 +1,50 @@
 ## @file\r
 # process FV generation\r
 #\r
-#  Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
 #\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
-#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
 \r
 ##\r
 # Import Modules\r
 #\r
+from __future__ import absolute_import\r
 import Common.LongFilePathOs as os\r
 import subprocess\r
-import StringIO\r
+from io import BytesIO\r
 from struct import *\r
-\r
-import Ffs\r
-import AprioriSection\r
-import FfsFileStatement\r
-from GenFdsGlobalVariable import GenFdsGlobalVariable\r
-from GenFds import GenFds\r
-from CommonDataClass.FdfClass import FvClassObject\r
-from Common.Misc import SaveFileOnChange\r
+from . import FfsFileStatement\r
+from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
+from Common.Misc import SaveFileOnChange, PackGUID\r
 from Common.LongFilePathSupport import CopyLongFilePath\r
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
+from Common.DataType import *\r
 \r
-T_CHAR_LF = '\n'\r
 FV_UI_EXT_ENTY_GUID = 'A67DF1FA-8DE8-4E98-AF09-4BDF2EFFBC7C'\r
 \r
 ## generate FV\r
 #\r
 #\r
-class FV (FvClassObject):\r
+class FV (object):\r
     ## The constructor\r
     #\r
     #   @param  self        The object pointer\r
     #\r
-    def __init__(self):\r
-        FvClassObject.__init__(self)\r
+    def __init__(self, Name=None):\r
+        self.UiFvName = Name\r
+        self.CreateFileName = None\r
+        self.BlockSizeList = []\r
+        self.DefineVarDict = {}\r
+        self.SetVarDict = {}\r
+        self.FvAlignment = None\r
+        self.FvAttributeDict = {}\r
+        self.FvNameGuid = None\r
+        self.FvNameString = None\r
+        self.AprioriSectionList = []\r
+        self.FfsList = []\r
+        self.BsBaseAddress = None\r
+        self.RtBaseAddress = None\r
         self.FvInfFile = None\r
         self.FvAddressFile = None\r
         self.BaseAddress = None\r
@@ -53,7 +55,9 @@ class FV (FvClassObject):
         self.FvForceRebase = None\r
         self.FvRegionInFD = None\r
         self.UsedSizeEnable = False\r
-        \r
+        self.FvExtEntryTypeValue = []\r
+        self.FvExtEntryType = []\r
+        self.FvExtEntryData = []\r
     ## AddToBuffer()\r
     #\r
     #   Generate Fv and add it to the Buffer\r
@@ -64,28 +68,25 @@ class FV (FvClassObject):
     #   @param  BlockSize   block size of FV\r
     #   @param  BlockNum    How many blocks in FV\r
     #   @param  ErasePolarity      Flash erase polarity\r
-    #   @param  VtfDict     VTF objects\r
     #   @param  MacroDict   macro value pair\r
     #   @retval string      Generated FV file path\r
     #\r
-    def AddToBuffer (self, Buffer, BaseAddress=None, BlockSize= None, BlockNum=None, ErasePloarity='1', VtfDict=None, MacroDict = {}, Flag=False) :\r
+    def AddToBuffer (self, Buffer, BaseAddress=None, BlockSize= None, BlockNum=None, ErasePloarity='1',  MacroDict = {}, Flag=False):\r
+        if BaseAddress is None and self.UiFvName.upper() + 'fv' in GenFdsGlobalVariable.ImageBinDict:\r
+            return GenFdsGlobalVariable.ImageBinDict[self.UiFvName.upper() + 'fv']\r
 \r
-        if BaseAddress == None and self.UiFvName.upper() + 'fv' in GenFds.ImageBinDict.keys():\r
-            return GenFds.ImageBinDict[self.UiFvName.upper() + 'fv']\r
-        \r
         #\r
         # Check whether FV in Capsule is in FD flash region.\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():\r
-                FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]\r
+        if self.CapsuleName is not None:\r
+            for FdObj in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
                 for RegionObj in FdObj.RegionList:\r
-                    if RegionObj.RegionType == 'FV':\r
+                    if RegionObj.RegionType == BINARY_FILE_TYPE_FV:\r
                         for RegionData in RegionObj.RegionDataList:\r
                             if RegionData.endswith(".fv"):\r
                                 continue\r
-                            elif RegionData.upper() + 'fv' in GenFds.ImageBinDict.keys():\r
+                            elif RegionData.upper() + 'fv' in GenFdsGlobalVariable.ImageBinDict:\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
@@ -93,11 +94,11 @@ class FV (FvClassObject):
             GenFdsGlobalVariable.InfLogger( "\nGenerating %s FV" %self.UiFvName)\r
         GenFdsGlobalVariable.LargeFileInFvFlags.append(False)\r
         FFSGuid = None\r
-        \r
-        if self.FvBaseAddress != None:\r
+\r
+        if self.FvBaseAddress is not None:\r
             BaseAddress = self.FvBaseAddress\r
         if not Flag:\r
-            self.__InitializeInf__(BaseAddress, BlockSize, BlockNum, ErasePloarity, VtfDict)\r
+            self._InitializeInf(BaseAddress, BlockSize, BlockNum, ErasePloarity)\r
         #\r
         # First Process the Apriori section\r
         #\r
@@ -110,37 +111,37 @@ class FV (FvClassObject):
             FfsFileList.append(FileName)\r
             # Add Apriori file name to Inf file\r
             if not Flag:\r
-                self.FvInfFile.writelines("EFI_FILE_NAME = " + \\r
+                self.FvInfFile.append("EFI_FILE_NAME = " + \\r
                                             FileName          + \\r
-                                            T_CHAR_LF)\r
+                                            TAB_LINE_BREAK)\r
 \r
         # Process Modules in FfsList\r
-        for FfsFile in self.FfsList :\r
+        for FfsFile in self.FfsList:\r
             if Flag:\r
                 if isinstance(FfsFile, FfsFileStatement.FileStatement):\r
                     continue\r
             if GenFdsGlobalVariable.EnableGenfdsMultiThread and GenFdsGlobalVariable.ModuleFile and GenFdsGlobalVariable.ModuleFile.Path.find(os.path.normpath(FfsFile.InfFileName)) == -1:\r
                 continue\r
-            FileName = FfsFile.GenFfs(MacroDict, FvParentAddr=BaseAddress, IsMakefile=Flag)\r
+            FileName = FfsFile.GenFfs(MacroDict, FvParentAddr=BaseAddress, IsMakefile=Flag, FvName=self.UiFvName)\r
             FfsFileList.append(FileName)\r
             if not Flag:\r
-                self.FvInfFile.writelines("EFI_FILE_NAME = " + \\r
+                self.FvInfFile.append("EFI_FILE_NAME = " + \\r
                                             FileName          + \\r
-                                            T_CHAR_LF)\r
+                                            TAB_LINE_BREAK)\r
         if not Flag:\r
-            SaveFileOnChange(self.InfFileName, self.FvInfFile.getvalue(), False)\r
-            self.FvInfFile.close()\r
+            FvInfFile = ''.join(self.FvInfFile)\r
+            SaveFileOnChange(self.InfFileName, FvInfFile, False)\r
         #\r
         # Call GenFv tool\r
         #\r
         FvOutputFile = os.path.join(GenFdsGlobalVariable.FvDir, self.UiFvName)\r
         FvOutputFile = FvOutputFile + '.Fv'\r
         # BUGBUG: FvOutputFile could be specified from FDF file (FV section, CreateFile statement)\r
-        if self.CreateFileName != None:\r
+        if self.CreateFileName is not None:\r
             FvOutputFile = self.CreateFileName\r
 \r
         if Flag:\r
-            GenFds.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile\r
+            GenFdsGlobalVariable.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile\r
             return FvOutputFile\r
 \r
         FvInfoFileName = os.path.join(GenFdsGlobalVariable.FfsDir, self.UiFvName + '.inf')\r
@@ -163,7 +164,7 @@ class FV (FvClassObject):
             NewFvInfo = None\r
             if os.path.exists (FvInfoFileName):\r
                 NewFvInfo = open(FvInfoFileName, 'r').read()\r
-            if NewFvInfo != None and NewFvInfo != OrigFvInfo:\r
+            if NewFvInfo is not None and NewFvInfo != OrigFvInfo:\r
                 FvChildAddr = []\r
                 AddFileObj = open(FvInfoFileName, 'r')\r
                 AddrStrings = AddFileObj.readlines()\r
@@ -178,13 +179,13 @@ class FV (FvClassObject):
 \r
                 if FvChildAddr != []:\r
                     # Update Ffs again\r
-                    for FfsFile in self.FfsList :\r
-                        FileName = FfsFile.GenFfs(MacroDict, FvChildAddr, BaseAddress, IsMakefile=Flag)\r
+                    for FfsFile in self.FfsList:\r
+                        FileName = FfsFile.GenFfs(MacroDict, FvChildAddr, BaseAddress, IsMakefile=Flag, FvName=self.UiFvName)\r
 \r
                     if GenFdsGlobalVariable.LargeFileInFvFlags[-1]:\r
                         FFSGuid = GenFdsGlobalVariable.EFI_FIRMWARE_FILE_SYSTEM3_GUID;\r
                     #Update GenFv again\r
-                        GenFdsGlobalVariable.GenerateFirmwareVolume(\r
+                    GenFdsGlobalVariable.GenerateFirmwareVolume(\r
                                                 FvOutputFile,\r
                                                 [self.InfFileName],\r
                                                 AddressFile=FvInfoFileName,\r
@@ -196,32 +197,36 @@ class FV (FvClassObject):
             #\r
             # Write the Fv contents to Buffer\r
             #\r
-            if os.path.isfile(FvOutputFile):\r
+            if os.path.isfile(FvOutputFile) and os.path.getsize(FvOutputFile) >= 0x48:\r
                 FvFileObj = open(FvOutputFile, 'rb')\r
-                GenFdsGlobalVariable.VerboseLogger("\nGenerate %s FV Successfully" % self.UiFvName)\r
-                GenFdsGlobalVariable.SharpCounter = 0\r
-\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
-                if FvAlignmentValue >= 0x400:\r
-                    if FvAlignmentValue >= 0x100000:\r
-                        if FvAlignmentValue >= 0x1000000:\r
-                        #The max alignment supported by FFS is 16M.\r
-                            self.FvAlignment = "16M"\r
+                Signature = FvHeaderBuffer[0x28:0x32]\r
+                if Signature and Signature.startswith(b'_FVH'):\r
+                    GenFdsGlobalVariable.VerboseLogger("\nGenerate %s FV Successfully" % self.UiFvName)\r
+                    GenFdsGlobalVariable.SharpCounter = 0\r
+\r
+                    FvFileObj.seek(0)\r
+                    Buffer.write(FvFileObj.read())\r
+                    # FV alignment position.\r
+                    FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E:0x2F]) & 0x1F)\r
+                    if FvAlignmentValue >= 0x400:\r
+                        if FvAlignmentValue >= 0x100000:\r
+                            if FvAlignmentValue >= 0x1000000:\r
+                            #The max alignment supported by FFS is 16M.\r
+                                self.FvAlignment = "16M"\r
+                            else:\r
+                                self.FvAlignment = str(FvAlignmentValue // 0x100000) + "M"\r
                         else:\r
-                            self.FvAlignment = str(FvAlignmentValue / 0x100000) + "M"\r
+                            self.FvAlignment = str(FvAlignmentValue // 0x400) + "K"\r
                     else:\r
-                        self.FvAlignment = str(FvAlignmentValue / 0x400) + "K"\r
+                        # FvAlignmentValue is less than 1K\r
+                        self.FvAlignment = str (FvAlignmentValue)\r
+                    FvFileObj.close()\r
+                    GenFdsGlobalVariable.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile\r
+                    GenFdsGlobalVariable.LargeFileInFvFlags.pop()\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
+                    GenFdsGlobalVariable.ErrorLogger("Invalid FV file %s." % self.UiFvName)\r
             else:\r
                 GenFdsGlobalVariable.ErrorLogger("Failed to generate %s FV file." %self.UiFvName)\r
         return FvOutputFile\r
@@ -235,10 +240,9 @@ class FV (FvClassObject):
         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 FdObj in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
             for RegionObj in FdObj.RegionList:\r
-                if RegionObj.RegionType != 'FV':\r
+                if RegionObj.RegionType != BINARY_FILE_TYPE_FV:\r
                     continue\r
                 for RegionData in RegionObj.RegionDataList:\r
                     #\r
@@ -250,100 +254,98 @@ class FV (FvClassObject):
                             return True\r
         return False\r
 \r
-    ## __InitializeInf__()\r
+    ## _InitializeInf()\r
     #\r
-    #   Initilize the inf file to create FV\r
+    #   Initialize the inf file to create FV\r
     #\r
     #   @param  self        The object pointer\r
     #   @param  BaseAddress base address of FV\r
     #   @param  BlockSize   block size of FV\r
     #   @param  BlockNum    How many blocks in FV\r
     #   @param  ErasePolarity      Flash erase polarity\r
-    #   @param  VtfDict     VTF objects\r
     #\r
-    def __InitializeInf__ (self, BaseAddress = None, BlockSize= None, BlockNum = None, ErasePloarity='1', VtfDict=None) :\r
+    def _InitializeInf (self, BaseAddress = None, BlockSize= None, BlockNum = None, ErasePloarity='1'):\r
         #\r
         # Create FV inf file\r
         #\r
         self.InfFileName = os.path.join(GenFdsGlobalVariable.FvDir,\r
                                    self.UiFvName + '.inf')\r
-        self.FvInfFile = StringIO.StringIO()\r
+        self.FvInfFile = []\r
 \r
         #\r
         # Add [Options]\r
         #\r
-        self.FvInfFile.writelines("[options]" + T_CHAR_LF)\r
-        if BaseAddress != None :\r
-            self.FvInfFile.writelines("EFI_BASE_ADDRESS = " + \\r
+        self.FvInfFile.append("[options]" + TAB_LINE_BREAK)\r
+        if BaseAddress is not None:\r
+            self.FvInfFile.append("EFI_BASE_ADDRESS = " + \\r
                                        BaseAddress          + \\r
-                                       T_CHAR_LF)\r
+                                       TAB_LINE_BREAK)\r
 \r
-        if BlockSize != None:\r
-            self.FvInfFile.writelines("EFI_BLOCK_SIZE = " + \\r
+        if BlockSize is not None:\r
+            self.FvInfFile.append("EFI_BLOCK_SIZE = " + \\r
                                       '0x%X' %BlockSize    + \\r
-                                      T_CHAR_LF)\r
-            if BlockNum != None:\r
-                self.FvInfFile.writelines("EFI_NUM_BLOCKS   = "  + \\r
+                                      TAB_LINE_BREAK)\r
+            if BlockNum is not None:\r
+                self.FvInfFile.append("EFI_NUM_BLOCKS   = "  + \\r
                                       ' 0x%X' %BlockNum    + \\r
-                                      T_CHAR_LF)\r
+                                      TAB_LINE_BREAK)\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.FvInfFile.append("EFI_BLOCK_SIZE  = 0x1" + TAB_LINE_BREAK)\r
+\r
+            for BlockSize in self.BlockSizeList:\r
+                if BlockSize[0] is not None:\r
+                    self.FvInfFile.append("EFI_BLOCK_SIZE  = "  + \\r
                                           '0x%X' %BlockSize[0]    + \\r
-                                          T_CHAR_LF)\r
+                                          TAB_LINE_BREAK)\r
 \r
-                if BlockSize[1] != None:\r
-                    self.FvInfFile.writelines("EFI_NUM_BLOCKS   = "  + \\r
+                if BlockSize[1] is not None:\r
+                    self.FvInfFile.append("EFI_NUM_BLOCKS   = "  + \\r
                                           ' 0x%X' %BlockSize[1]    + \\r
-                                          T_CHAR_LF)\r
+                                          TAB_LINE_BREAK)\r
 \r
-        if self.BsBaseAddress != None:\r
-            self.FvInfFile.writelines('EFI_BOOT_DRIVER_BASE_ADDRESS = ' + \\r
+        if self.BsBaseAddress is not None:\r
+            self.FvInfFile.append('EFI_BOOT_DRIVER_BASE_ADDRESS = ' + \\r
                                        '0x%X' %self.BsBaseAddress)\r
-        if self.RtBaseAddress != None:\r
-            self.FvInfFile.writelines('EFI_RUNTIME_DRIVER_BASE_ADDRESS = ' + \\r
+        if self.RtBaseAddress is not None:\r
+            self.FvInfFile.append('EFI_RUNTIME_DRIVER_BASE_ADDRESS = ' + \\r
                                       '0x%X' %self.RtBaseAddress)\r
         #\r
         # Add attribute\r
         #\r
-        self.FvInfFile.writelines("[attributes]" + T_CHAR_LF)\r
+        self.FvInfFile.append("[attributes]" + TAB_LINE_BREAK)\r
 \r
-        self.FvInfFile.writelines("EFI_ERASE_POLARITY   = "       + \\r
+        self.FvInfFile.append("EFI_ERASE_POLARITY   = "       + \\r
                                           ' %s' %ErasePloarity    + \\r
-                                          T_CHAR_LF)\r
-        if not (self.FvAttributeDict == None):\r
-            for FvAttribute in self.FvAttributeDict.keys() :\r
+                                          TAB_LINE_BREAK)\r
+        if not (self.FvAttributeDict is None):\r
+            for FvAttribute in self.FvAttributeDict.keys():\r
                 if FvAttribute == "FvUsedSizeEnable":\r
-                    if self.FvAttributeDict[FvAttribute].upper() in ('TRUE', '1') :\r
+                    if self.FvAttributeDict[FvAttribute].upper() in ('TRUE', '1'):\r
                         self.UsedSizeEnable = True\r
                     continue\r
-                self.FvInfFile.writelines("EFI_"            + \\r
+                self.FvInfFile.append("EFI_"            + \\r
                                           FvAttribute       + \\r
                                           ' = '             + \\r
                                           self.FvAttributeDict[FvAttribute] + \\r
-                                          T_CHAR_LF )\r
-        if self.FvAlignment != None:\r
-            self.FvInfFile.writelines("EFI_FVB2_ALIGNMENT_"     + \\r
+                                          TAB_LINE_BREAK )\r
+        if self.FvAlignment is not None:\r
+            self.FvInfFile.append("EFI_FVB2_ALIGNMENT_"     + \\r
                                        self.FvAlignment.strip() + \\r
                                        " = TRUE"                + \\r
-                                       T_CHAR_LF)\r
-                                       \r
+                                       TAB_LINE_BREAK)\r
+\r
         #\r
         # Generate FV extension header file\r
         #\r
-        if self.FvNameGuid == None or self.FvNameGuid == '':\r
+        if not self.FvNameGuid:\r
             if len(self.FvExtEntryType) > 0 or self.UsedSizeEnable:\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
+        else:\r
             TotalSize = 16 + 4\r
-            Buffer = ''\r
+            Buffer = bytearray()\r
             if self.UsedSizeEnable:\r
                 TotalSize += (4 + 4)\r
                 ## define EFI_FV_EXT_TYPE_USED_SIZE_TYPE 0x03\r
@@ -364,16 +366,13 @@ class FV (FvClassObject):
                 Guid = FV_UI_EXT_ENTY_GUID.split('-')\r
                 #\r
                 # Layout:\r
-                #   EFI_FIRMWARE_VOLUME_EXT_ENTRY : size 4\r
-                #   GUID                          : size 16\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
+                           + PackGUID(Guid)\r
+                           + self.UiFvName.encode('utf-8'))\r
 \r
             for Index in range (0, len(self.FvExtEntryType)):\r
                 if self.FvExtEntryType[Index] == 'FILE':\r
@@ -385,15 +384,15 @@ class FV (FvClassObject):
                     # 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
+                    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
+                    Buffer += FvExtFile.read()\r
                     FvExtFile.close()\r
                 if self.FvExtEntryType[Index] == 'DATA':\r
                     ByteList = self.FvExtEntryData[Index].split(',')\r
@@ -406,43 +405,25 @@ class FV (FvClassObject):
                         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
+            Buffer = PackGUID(Guid) + pack('=L', TotalSize) + 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 = BytesIO()\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
+                self.FvInfFile.append("EFI_FV_EXT_HEADER_FILE_NAME = "      + \\r
                                            FvExtHeaderFileName                  + \\r
-                                           T_CHAR_LF)\r
+                                           TAB_LINE_BREAK)\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
+        self.FvInfFile.append("[files]" + TAB_LINE_BREAK)\r