]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenFds/Region.py
BaseTools: Remove unused logic for IPF
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / Region.py
index 01e998e54c6fb6c5fa2ec1aa5c4d0f8e47622c54..8ca61254b01f9e2676e3f668f14fea61fcd481fb 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # process FD Region generation\r
 #\r
-#  Copyright (c) 2007 - 2014, 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
 ##\r
 # Import Modules\r
 #\r
+from __future__ import absolute_import\r
 from struct import *\r
-from GenFdsGlobalVariable import GenFdsGlobalVariable\r
-import StringIO\r
-from CommonDataClass.FdfClass import RegionClassObject\r
+from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
+from io import BytesIO\r
+import string\r
 import Common.LongFilePathOs as os\r
 from stat import *\r
 from Common import EdkLogger\r
 from Common.BuildToolError import *\r
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
 from Common.MultipleWorkspace import MultipleWorkspace as mws\r
+from Common.DataType import BINARY_FILE_TYPE_FV\r
 \r
 ## generate Region\r
 #\r
 #\r
-class Region(RegionClassObject):\r
+class Region(object):\r
 \r
     ## The constructor\r
     #\r
     #   @param  self        The object pointer\r
     #\r
     def __init__(self):\r
-        RegionClassObject.__init__(self)\r
+        self.Offset = None       # The begin position of the Region\r
+        self.Size = None         # The Size of the Region\r
+        self.PcdOffset = None\r
+        self.PcdSize = None\r
+        self.SetVarDict = {}\r
+        self.RegionType = None\r
+        self.RegionDataList = []\r
 \r
+    ## PadBuffer()\r
+    #\r
+    #   Add padding bytes to the Buffer\r
+    #\r
+    #   @param Buffer         The buffer the generated region data will be put\r
+    #                         in\r
+    #   @param ErasePolarity  Flash erase polarity\r
+    #   @param Size           Number of padding bytes requested\r
+    #\r
+\r
+    def PadBuffer(self, Buffer, ErasePolarity, Size):\r
+        if Size > 0:\r
+            if (ErasePolarity == '1') :\r
+                PadByte = pack('B', 0xFF)\r
+            else:\r
+                PadByte = pack('B', 0)\r
+            PadData = ''.join(PadByte for i in xrange(0, Size))\r
+            Buffer.write(PadData)\r
 \r
     ## AddToBuffer()\r
     #\r
@@ -49,18 +75,20 @@ class Region(RegionClassObject):
     #   @param  BlockSize   block size of region\r
     #   @param  BlockNum    How many blocks in region\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
 \r
-    def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, ImageBinDict, vtfDict=None, MacroDict={}):\r
+    def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, ImageBinDict,  MacroDict={}, Flag=False):\r
         Size = self.Size\r
-        GenFdsGlobalVariable.InfLogger('\nGenerate Region at Offset 0x%X' % self.Offset)\r
-        GenFdsGlobalVariable.InfLogger("   Region Size = 0x%X" % Size)\r
+        if not Flag:\r
+            GenFdsGlobalVariable.InfLogger('\nGenerate Region at Offset 0x%X' % self.Offset)\r
+            GenFdsGlobalVariable.InfLogger("   Region Size = 0x%X" % Size)\r
         GenFdsGlobalVariable.SharpCounter = 0\r
+        if Flag and (self.RegionType != BINARY_FILE_TYPE_FV):\r
+            return\r
 \r
-        if self.RegionType == 'FV':\r
+        if self.RegionType == BINARY_FILE_TYPE_FV:\r
             #\r
             # Get Fv from FvDict\r
             #\r
@@ -71,41 +99,48 @@ class Region(RegionClassObject):
                 FileName = None\r
                 if RegionData.endswith(".fv"):\r
                     RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)\r
-                    GenFdsGlobalVariable.InfLogger('   Region FV File Name = .fv : %s' % RegionData)\r
+                    if not Flag:\r
+                        GenFdsGlobalVariable.InfLogger('   Region FV File Name = .fv : %s' % RegionData)\r
                     if RegionData[1] != ':' :\r
-                        RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
+                        RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
                     if not os.path.exists(RegionData):\r
                         EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)\r
 \r
                     FileName = RegionData\r
-                elif RegionData.upper() + 'fv' in ImageBinDict.keys():\r
-                    GenFdsGlobalVariable.InfLogger('   Region Name = FV')\r
+                elif RegionData.upper() + 'fv' in ImageBinDict:\r
+                    if not Flag:\r
+                        GenFdsGlobalVariable.InfLogger('   Region Name = FV')\r
                     FileName = ImageBinDict[RegionData.upper() + 'fv']\r
                 else:\r
                     #\r
                     # Generate FvImage.\r
                     #\r
                     FvObj = None\r
-                    if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
-                        FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(RegionData.upper())\r
+                    if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict:\r
+                        FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[RegionData.upper()]\r
 \r
-                    if FvObj != None :\r
-                        GenFdsGlobalVariable.InfLogger('   Region Name = FV')\r
+                    if FvObj is not None :\r
+                        if not Flag:\r
+                            GenFdsGlobalVariable.InfLogger('   Region Name = FV')\r
                         #\r
                         # Call GenFv tool\r
                         #\r
                         self.BlockInfoOfRegion(BlockSizeList, FvObj)\r
                         self.FvAddress = self.FvAddress + FvOffset\r
-                        FvAlignValue = self.GetFvAlignValue(FvObj.FvAlignment)\r
+                        FvAlignValue = GenFdsGlobalVariable.GetAlignment(FvObj.FvAlignment)\r
                         if self.FvAddress % FvAlignValue != 0:\r
                             EdkLogger.error("GenFds", GENFDS_ERROR,\r
                                             "FV (%s) is NOT %s Aligned!" % (FvObj.UiFvName, FvObj.FvAlignment))\r
-                        FvBuffer = StringIO.StringIO('')\r
+                        FvBuffer = BytesIO('')\r
                         FvBaseAddress = '0x%X' % self.FvAddress\r
                         BlockSize = None\r
                         BlockNum = None\r
-                        FvObj.AddToBuffer(FvBuffer, FvBaseAddress, BlockSize, BlockNum, ErasePolarity, vtfDict)\r
-                        if FvBuffer.len > Size:\r
+                        FvObj.AddToBuffer(FvBuffer, FvBaseAddress, BlockSize, BlockNum, ErasePolarity, Flag=Flag)\r
+                        if Flag:\r
+                            continue\r
+\r
+                        FvBufferLen = len(FvBuffer.getvalue())\r
+                        if FvBufferLen > Size:\r
                             FvBuffer.close()\r
                             EdkLogger.error("GenFds", GENFDS_ERROR,\r
                                             "Size of FV (%s) is larger than Region Size 0x%X specified." % (RegionData, Size))\r
@@ -114,34 +149,30 @@ class Region(RegionClassObject):
                         #\r
                         Buffer.write(FvBuffer.getvalue())\r
                         FvBuffer.close()\r
-                        FvOffset = FvOffset + FvBuffer.len\r
-                        Size = Size - FvBuffer.len\r
+                        FvOffset = FvOffset + FvBufferLen\r
+                        Size = Size - FvBufferLen\r
                         continue\r
                     else:\r
                         EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT described in FDF file!" % (RegionData))\r
                 #\r
                 # Add the exist Fv image into FD buffer\r
                 #\r
-                if FileName != None:\r
-                    FileLength = os.stat(FileName)[ST_SIZE]\r
-                    if FileLength > Size:\r
-                        EdkLogger.error("GenFds", GENFDS_ERROR,\r
-                                        "Size of FV File (%s) is larger than Region Size 0x%X specified." \\r
-                                        % (RegionData, Size))\r
-                    BinFile = open(FileName, 'r+b')\r
-                    Buffer.write(BinFile.read())\r
-                    BinFile.close()\r
-                    Size = Size - FileLength\r
+                if not Flag:\r
+                    if FileName is not None:\r
+                        FileLength = os.stat(FileName)[ST_SIZE]\r
+                        if FileLength > Size:\r
+                            EdkLogger.error("GenFds", GENFDS_ERROR,\r
+                                            "Size of FV File (%s) is larger than Region Size 0x%X specified." \\r
+                                            % (RegionData, Size))\r
+                        BinFile = open(FileName, 'rb')\r
+                        Buffer.write(BinFile.read())\r
+                        BinFile.close()\r
+                        Size = Size - FileLength\r
             #\r
             # Pad the left buffer\r
             #\r
-            if Size > 0:\r
-                if (ErasePolarity == '1') :\r
-                    PadData = 0xFF\r
-                else :\r
-                    PadData = 0\r
-                for i in range(0, Size):\r
-                    Buffer.write(pack('B', PadData))\r
+            if not Flag:\r
+                self.PadBuffer(Buffer, ErasePolarity, Size)\r
 \r
         if self.RegionType == 'CAPSULE':\r
             #\r
@@ -152,12 +183,12 @@ class Region(RegionClassObject):
                     RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)\r
                     GenFdsGlobalVariable.InfLogger('   Region CAPSULE Image Name = .cap : %s' % RegionData)\r
                     if RegionData[1] != ':' :\r
-                        RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
+                        RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
                     if not os.path.exists(RegionData):\r
                         EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)\r
 \r
                     FileName = RegionData\r
-                elif RegionData.upper() + 'cap' in ImageBinDict.keys():\r
+                elif RegionData.upper() + 'cap' in ImageBinDict:\r
                     GenFdsGlobalVariable.InfLogger('   Region Name = CAPSULE')\r
                     FileName = ImageBinDict[RegionData.upper() + 'cap']\r
                 else:\r
@@ -165,10 +196,10 @@ class Region(RegionClassObject):
                     # Generate Capsule image and Put it into FD buffer\r
                     #\r
                     CapsuleObj = None\r
-                    if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():\r
+                    if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict:\r
                         CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[RegionData.upper()]\r
 \r
-                    if CapsuleObj != None :\r
+                    if CapsuleObj is not None :\r
                         CapsuleObj.CapsuleName = RegionData.upper()\r
                         GenFdsGlobalVariable.InfLogger('   Region Name = CAPSULE')\r
                         #\r
@@ -187,28 +218,29 @@ class Region(RegionClassObject):
                     EdkLogger.error("GenFds", GENFDS_ERROR,\r
                                     "Size 0x%X of Capsule File (%s) is larger than Region Size 0x%X specified." \\r
                                     % (FileLength, RegionData, Size))\r
-                BinFile = open(FileName, 'r+b')\r
+                BinFile = open(FileName, 'rb')\r
                 Buffer.write(BinFile.read())\r
                 BinFile.close()\r
                 Size = Size - FileLength\r
             #\r
             # Pad the left buffer\r
             #\r
-            if Size > 0:\r
-                if (ErasePolarity == '1') :\r
-                    PadData = 0xFF\r
-                else :\r
-                    PadData = 0\r
-                for i in range(0, Size):\r
-                    Buffer.write(pack('B', PadData))\r
+            self.PadBuffer(Buffer, ErasePolarity, Size)\r
 \r
-        if self.RegionType == 'FILE':\r
+        if self.RegionType in ('FILE', 'INF'):\r
             for RegionData in self.RegionDataList:\r
-                RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)\r
-                if RegionData[1] != ':' :\r
-                    RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
-                if not os.path.exists(RegionData):\r
-                    EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)\r
+                if self.RegionType == 'INF':\r
+                    RegionData.__InfParse__(None)\r
+                    if len(RegionData.BinFileList) != 1:\r
+                        EdkLogger.error('GenFds', GENFDS_ERROR, 'INF in FD region can only contain one binary: %s' % RegionData)\r
+                    File = RegionData.BinFileList[0]\r
+                    RegionData = RegionData.PatchEfiFile(File.Path, File.Type)\r
+                else:\r
+                    RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)\r
+                    if RegionData[1] != ':' :\r
+                        RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
+                    if not os.path.exists(RegionData):\r
+                        EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)\r
                 #\r
                 # Add the file image into FD buffer\r
                 #\r
@@ -225,13 +257,7 @@ class Region(RegionClassObject):
             #\r
             # Pad the left buffer\r
             #\r
-            if Size > 0:\r
-                if (ErasePolarity == '1') :\r
-                    PadData = 0xFF\r
-                else :\r
-                    PadData = 0\r
-                for i in range(0, Size):\r
-                    Buffer.write(pack('B', PadData))\r
+            self.PadBuffer(Buffer, ErasePolarity, Size)\r
 \r
         if self.RegionType == 'DATA' :\r
             GenFdsGlobalVariable.InfLogger('   Region Name = DATA')\r
@@ -248,41 +274,11 @@ class Region(RegionClassObject):
             #\r
             # Pad the left buffer\r
             #\r
-            if Size > 0:\r
-                if (ErasePolarity == '1') :\r
-                    PadData = 0xFF\r
-                else :\r
-                    PadData = 0\r
-                for i in range(0, Size):\r
-                    Buffer.write(pack('B', PadData))\r
+            self.PadBuffer(Buffer, ErasePolarity, Size)\r
 \r
-        if self.RegionType == None:\r
+        if self.RegionType is None:\r
             GenFdsGlobalVariable.InfLogger('   Region Name = None')\r
-            if (ErasePolarity == '1') :\r
-                PadData = 0xFF\r
-            else :\r
-                PadData = 0\r
-            for i in range(0, Size):\r
-                Buffer.write(pack('B', PadData))\r
-\r
-    def GetFvAlignValue(self, Str):\r
-        AlignValue = 1\r
-        Granu = 1\r
-        Str = Str.strip().upper()\r
-        if Str.endswith('K'):\r
-            Granu = 1024\r
-            Str = Str[:-1]\r
-        elif Str.endswith('M'):\r
-            Granu = 1024 * 1024\r
-            Str = Str[:-1]\r
-        elif Str.endswith('G'):\r
-            Granu = 1024 * 1024 * 1024\r
-            Str = Str[:-1]\r
-        else:\r
-            pass\r
-\r
-        AlignValue = int(Str) * Granu\r
-        return AlignValue\r
+            self.PadBuffer(Buffer, ErasePolarity, Size)\r
 \r
     ## BlockSizeOfRegion()\r
     #\r
@@ -300,7 +296,7 @@ class Region(RegionClassObject):
             if self.Offset >= End:\r
                 Start = End\r
                 continue\r
-            # region located in current blocks \r
+            # region located in current blocks\r
             else:\r
                 # region ended within current blocks\r
                 if self.Offset + self.Size <= End:\r
@@ -324,7 +320,7 @@ class Region(RegionClassObject):
             # first check whether FvObj.BlockSizeList items have only "BlockSize" or "NumBlocks",\r
             # if so, use ExpectedList\r
             for Item in FvObj.BlockSizeList:\r
-                if Item[0] == None or Item[1] == None:\r
+                if Item[0] is None or Item[1] is None:\r
                     FvObj.BlockSizeList = ExpectedList\r
                     break\r
             # make sure region size is no smaller than the summed block size in FV\r
@@ -352,5 +348,5 @@ class Region(RegionClassObject):
                 else:\r
                     Index += 1\r
 \r
-            \r
+\r
 \r