]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenFds/Fd.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / Fd.py
index 26de500d9b6c59d30efed35fda1e9f8fab9946af..973936b6f273a2a36774581bf35625810e97e174 100644 (file)
@@ -1,32 +1,27 @@
 ## @file\r
 # process FD generation\r
 #\r
-#  Copyright (c) 2007, Intel Corporation\r
+#  Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
 #\r
-#  All rights reserved. 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
-import Region\r
-import Fv\r
-import os\r
-import StringIO\r
+from __future__ import absolute_import\r
+from . import Region\r
+from . import Fv\r
+import Common.LongFilePathOs as os\r
+from io import BytesIO\r
 import sys\r
 from struct import *\r
-from GenFdsGlobalVariable import GenFdsGlobalVariable\r
+from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
 from CommonDataClass.FdfClass import FDClassObject\r
 from Common import EdkLogger\r
 from Common.BuildToolError import *\r
 from Common.Misc import SaveFileOnChange\r
-from GenFds import GenFds\r
+from Common.DataType import BINARY_FILE_TYPE_FV\r
 \r
 ## generate FD\r
 #\r
@@ -45,14 +40,17 @@ class FD(FDClassObject):
     #\r
     #   @retval string      Generated FD file name\r
     #\r
-    def GenFd (self):\r
-        if self.FdUiName.upper() + 'fd' in GenFds.ImageBinDict.keys():\r
-            return GenFds.ImageBinDict[self.FdUiName.upper() + 'fd']\r
+    def GenFd (self, Flag = False):\r
+        if self.FdUiName.upper() + 'fd' in GenFdsGlobalVariable.ImageBinDict:\r
+            return GenFdsGlobalVariable.ImageBinDict[self.FdUiName.upper() + 'fd']\r
 \r
         #\r
         # Print Information\r
         #\r
-        GenFdsGlobalVariable.InfLogger("Fd File Name:%s" %self.FdUiName)\r
+        FdFileName = os.path.join(GenFdsGlobalVariable.FvDir, self.FdUiName + '.fd')\r
+        if not Flag:\r
+            GenFdsGlobalVariable.InfLogger("\nFd File Name:%s (%s)" %(self.FdUiName, FdFileName))\r
+\r
         Offset = 0x00\r
         for item in self.BlockSizeList:\r
             Offset = Offset + item[0]  * item[1]\r
@@ -62,37 +60,42 @@ class FD(FDClassObject):
         for FvObj in GenFdsGlobalVariable.FdfParser.Profile.FvDict:\r
             GenFdsGlobalVariable.VerboseLogger(FvObj)\r
 \r
-        GenFdsGlobalVariable.VerboseLogger('################### Gen VTF ####################')\r
-        self.GenVtfFile()\r
-\r
-        TempFdBuffer = StringIO.StringIO('')\r
-        PreviousRegionStart = -1\r
-        PreviousRegionSize = 1\r
-        \r
-        for RegionObj in self.RegionList :\r
+        HasCapsuleRegion = False\r
+        for RegionObj in self.RegionList:\r
             if RegionObj.RegionType == 'CAPSULE':\r
-              continue\r
-            if RegionObj.Offset + RegionObj.Size <= PreviousRegionStart:\r
-              pass\r
-            elif RegionObj.Offset <= PreviousRegionStart or (RegionObj.Offset >=PreviousRegionStart and RegionObj.Offset < PreviousRegionStart + PreviousRegionSize):\r
-              pass\r
-            elif RegionObj.Offset > PreviousRegionStart + PreviousRegionSize:\r
-                GenFdsGlobalVariable.InfLogger('Padding region starting from offset 0x%X, with size 0x%X' %(PreviousRegionStart + PreviousRegionSize, RegionObj.Offset - (PreviousRegionStart + PreviousRegionSize)))\r
-                PadRegion = Region.Region()\r
-                PadRegion.Offset = PreviousRegionStart + PreviousRegionSize\r
-                PadRegion.Size = RegionObj.Offset - PadRegion.Offset\r
-                PadRegion.AddToBuffer(TempFdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)\r
-            PreviousRegionStart = RegionObj.Offset\r
-            PreviousRegionSize = RegionObj.Size\r
-            #\r
-            # Call each region's AddToBuffer function\r
-            #\r
-            if PreviousRegionSize > self.Size:\r
-              pass\r
-            GenFdsGlobalVariable.VerboseLogger('Call each region\'s AddToBuffer function')\r
-            RegionObj.AddToBuffer (TempFdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)\r
-        \r
-        FdBuffer = StringIO.StringIO('')\r
+                HasCapsuleRegion = True\r
+                break\r
+        if HasCapsuleRegion:\r
+            TempFdBuffer = BytesIO()\r
+            PreviousRegionStart = -1\r
+            PreviousRegionSize = 1\r
+\r
+            for RegionObj in self.RegionList :\r
+                if RegionObj.RegionType == 'CAPSULE':\r
+                    continue\r
+                if RegionObj.Offset + RegionObj.Size <= PreviousRegionStart:\r
+                    pass\r
+                elif RegionObj.Offset <= PreviousRegionStart or (RegionObj.Offset >=PreviousRegionStart and RegionObj.Offset < PreviousRegionStart + PreviousRegionSize):\r
+                    pass\r
+                elif RegionObj.Offset > PreviousRegionStart + PreviousRegionSize:\r
+                    if not Flag:\r
+                        GenFdsGlobalVariable.InfLogger('Padding region starting from offset 0x%X, with size 0x%X' %(PreviousRegionStart + PreviousRegionSize, RegionObj.Offset - (PreviousRegionStart + PreviousRegionSize)))\r
+                    PadRegion = Region.Region()\r
+                    PadRegion.Offset = PreviousRegionStart + PreviousRegionSize\r
+                    PadRegion.Size = RegionObj.Offset - PadRegion.Offset\r
+                    if not Flag:\r
+                        PadRegion.AddToBuffer(TempFdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFdsGlobalVariable.ImageBinDict, self.DefineVarDict)\r
+                PreviousRegionStart = RegionObj.Offset\r
+                PreviousRegionSize = RegionObj.Size\r
+                #\r
+                # Call each region's AddToBuffer function\r
+                #\r
+                if PreviousRegionSize > self.Size:\r
+                    pass\r
+                GenFdsGlobalVariable.VerboseLogger('Call each region\'s AddToBuffer function')\r
+                RegionObj.AddToBuffer (TempFdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFdsGlobalVariable.ImageBinDict, self.DefineVarDict)\r
+\r
+        FdBuffer = BytesIO()\r
         PreviousRegionStart = -1\r
         PreviousRegionSize = 1\r
         for RegionObj in self.RegionList :\r
@@ -105,79 +108,37 @@ class FD(FDClassObject):
                                 'Region offset 0x%X overlaps with Region starting from 0x%X, size 0x%X' \\r
                                 % (RegionObj.Offset, PreviousRegionStart, PreviousRegionSize))\r
             elif RegionObj.Offset > PreviousRegionStart + PreviousRegionSize:\r
-                GenFdsGlobalVariable.InfLogger('Padding region starting from offset 0x%X, with size 0x%X' %(PreviousRegionStart + PreviousRegionSize, RegionObj.Offset - (PreviousRegionStart + PreviousRegionSize)))\r
+                if not Flag:\r
+                    GenFdsGlobalVariable.InfLogger('Padding region starting from offset 0x%X, with size 0x%X' %(PreviousRegionStart + PreviousRegionSize, RegionObj.Offset - (PreviousRegionStart + PreviousRegionSize)))\r
                 PadRegion = Region.Region()\r
                 PadRegion.Offset = PreviousRegionStart + PreviousRegionSize\r
                 PadRegion.Size = RegionObj.Offset - PadRegion.Offset\r
-                PadRegion.AddToBuffer(FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)\r
+                if not Flag:\r
+                    PadRegion.AddToBuffer(FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFdsGlobalVariable.ImageBinDict, self.DefineVarDict)\r
             PreviousRegionStart = RegionObj.Offset\r
             PreviousRegionSize = RegionObj.Size\r
             #\r
+            # Verify current region fits within allocated FD section Size\r
+            #\r
+            if PreviousRegionStart + PreviousRegionSize > self.Size:\r
+                EdkLogger.error("GenFds", GENFDS_ERROR,\r
+                                'FD %s size too small to fit region with offset 0x%X and size 0x%X'\r
+                                % (self.FdUiName, PreviousRegionStart, PreviousRegionSize))\r
+            #\r
             # Call each region's AddToBuffer function\r
             #\r
-            if PreviousRegionSize > self.Size:\r
-                EdkLogger.error("GenFds", GENFDS_ERROR, 'FD %s size too small' % self.FdUiName)\r
             GenFdsGlobalVariable.VerboseLogger('Call each region\'s AddToBuffer function')\r
-            RegionObj.AddToBuffer (FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)\r
-        #\r
-        # Create a empty Fd file\r
-        #\r
-        GenFdsGlobalVariable.VerboseLogger ('Create an empty Fd file')\r
-        FdFileName = os.path.join(GenFdsGlobalVariable.FvDir,self.FdUiName + '.fd')\r
+            RegionObj.AddToBuffer (FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFdsGlobalVariable.ImageBinDict, self.DefineVarDict, Flag=Flag)\r
         #\r
         # Write the buffer contents to Fd file\r
         #\r
         GenFdsGlobalVariable.VerboseLogger('Write the buffer contents to Fd file')\r
-        SaveFileOnChange(FdFileName, FdBuffer.getvalue())\r
-        FdBuffer.close();\r
-        GenFds.ImageBinDict[self.FdUiName.upper() + 'fd'] = FdFileName\r
+        if not Flag:\r
+            SaveFileOnChange(FdFileName, FdBuffer.getvalue())\r
+        FdBuffer.close()\r
+        GenFdsGlobalVariable.ImageBinDict[self.FdUiName.upper() + 'fd'] = FdFileName\r
         return FdFileName\r
 \r
-    ## generate VTF\r
-    #\r
-    #   @param  self        The object pointer\r
-    #\r
-    def GenVtfFile (self) :\r
-        #\r
-        # Get this Fd's all Fv name\r
-        #\r
-        FvAddDict ={}\r
-        FvList = []\r
-        for RegionObj in self.RegionList:\r
-            if RegionObj.RegionType == 'FV':\r
-                if len(RegionObj.RegionDataList) == 1:\r
-                    RegionData = RegionObj.RegionDataList[0]\r
-                    FvList.append(RegionData.upper())\r
-                    FvAddDict[RegionData.upper()] = (int(self.BaseAddress,16) + \\r
-                                                RegionObj.Offset, RegionObj.Size)\r
-                else:\r
-                    Offset = RegionObj.Offset\r
-                    for RegionData in RegionObj.RegionDataList:\r
-                        FvList.append(RegionData.upper())\r
-                        FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(RegionData.upper())\r
-                        if len(FvObj.BlockSizeList) < 1:\r
-                            EdkLogger.error("GenFds", GENFDS_ERROR,\r
-                                            'FV.%s must point out FVs blocksize and Fv BlockNum' \\r
-                                            % FvObj.UiFvName)\r
-                        else:\r
-                            Size = 0\r
-                            for blockStatement in FvObj.BlockSizeList:\r
-                                Size = Size + blockStatement[0] * blockStatement[1]\r
-                            FvAddDict[RegionData.upper()] = (int(self.BaseAddress,16) + \\r
-                                                             Offset, Size)\r
-                            Offset = Offset + Size\r
-        #\r
-        # Check whether this Fd need VTF\r
-        #\r
-        Flag = False\r
-        for VtfObj in GenFdsGlobalVariable.FdfParser.Profile.VtfList:\r
-            compLocList = VtfObj.GetFvList()\r
-            if set(compLocList).issubset(FvList):\r
-                Flag = True\r
-                break\r
-        if Flag == True:\r
-            self.vtfRawDict = VtfObj.GenVtf(FvAddDict)\r
-\r
     ## generate flash map file\r
     #\r
     #   @param  self        The object pointer\r