]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenFds/FfsFileStatement.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / FfsFileStatement.py
index edb131266d98d1b6ac5e41872809a3293bb7f514..9250361d1415fa4ce0ef9388f6f404ef22235b56 100644 (file)
@@ -1,40 +1,31 @@
 ## @file\r
 # process FFS generation from FILE statement\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
-import Ffs\r
-import Rule\r
-import Common.LongFilePathOs as os\r
-import StringIO\r
-import subprocess\r
-\r
-from GenFdsGlobalVariable import GenFdsGlobalVariable\r
+from __future__ import absolute_import\r
+from io import BytesIO\r
+from struct import pack\r
 from CommonDataClass.FdfClass import FileStatementClassObject\r
 from Common import EdkLogger\r
-from Common.BuildToolError import *\r
-from Common.Misc import GuidStructureByteArrayToGuidString\r
-from GuidSection import GuidSection\r
-from FvImageSection import FvImageSection\r
-from Common.Misc import SaveFileOnChange\r
-from struct import *\r
+from Common.BuildToolError import GENFDS_ERROR\r
+from Common.Misc import GuidStructureByteArrayToGuidString, SaveFileOnChange\r
+import Common.LongFilePathOs as os\r
+from .GuidSection import GuidSection\r
+from .FvImageSection import FvImageSection\r
+from .Ffs import FdfFvFileTypeToFileType\r
+from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
 \r
 ## generate FFS from FILE\r
 #\r
 #\r
-class FileStatement (FileStatementClassObject) :\r
+class FileStatement (FileStatementClassObject):\r
     ## The constructor\r
     #\r
     #   @param  self        The object pointer\r
@@ -57,9 +48,9 @@ class FileStatement (FileStatementClassObject) :
     #   @param  FvParentAddr Parent Fv base address\r
     #   @retval string       Generated FFS file name\r
     #\r
-    def GenFfs(self, Dict = {}, FvChildAddr=[], FvParentAddr=None, IsMakefile=False):\r
-        \r
-        if self.NameGuid != None and self.NameGuid.startswith('PCD('):\r
+    def GenFfs(self, Dict = {}, FvChildAddr=[], FvParentAddr=None, IsMakefile=False, FvName=None):\r
+\r
+        if self.NameGuid and self.NameGuid.startswith('PCD('):\r
             PcdValue = GenFdsGlobalVariable.GetPcdValue(self.NameGuid)\r
             if len(PcdValue) == 0:\r
                 EdkLogger.error("GenFds", GENFDS_ERROR, '%s NOT defined.' \\r
@@ -71,32 +62,35 @@ class FileStatement (FileStatementClassObject) :
                 EdkLogger.error("GenFds", GENFDS_ERROR, 'GUID value for %s in wrong format.' \\r
                             % (self.NameGuid))\r
             self.NameGuid = RegistryGuidStr\r
-        \r
-        OutputDir = os.path.join(GenFdsGlobalVariable.FfsDir, self.NameGuid)\r
+\r
+        Str = self.NameGuid\r
+        if FvName:\r
+            Str += FvName\r
+        OutputDir = os.path.join(GenFdsGlobalVariable.FfsDir, Str)\r
         if not os.path.exists(OutputDir):\r
             os.makedirs(OutputDir)\r
 \r
         Dict.update(self.DefineVarDict)\r
         SectionAlignments = None\r
-        if self.FvName != None :\r
-            Buffer = StringIO.StringIO('')\r
-            if self.FvName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
+        if self.FvName:\r
+            Buffer = BytesIO()\r
+            if self.FvName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FvDict:\r
                 EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT described in FDF file!" % (self.FvName))\r
             Fv = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName.upper())\r
             FileName = Fv.AddToBuffer(Buffer)\r
             SectionFiles = [FileName]\r
 \r
-        elif self.FdName != None:\r
-            if self.FdName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
+        elif self.FdName:\r
+            if self.FdName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FdDict:\r
                 EdkLogger.error("GenFds", GENFDS_ERROR, "FD (%s) is NOT described in FDF file!" % (self.FdName))\r
             Fd = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(self.FdName.upper())\r
             FileName = Fd.GenFd()\r
             SectionFiles = [FileName]\r
 \r
-        elif self.FileName != None:\r
+        elif self.FileName:\r
             if hasattr(self, 'FvFileType') and self.FvFileType == 'RAW':\r
                 if isinstance(self.FileName, list) and isinstance(self.SubAlignment, list) and len(self.FileName) == len(self.SubAlignment):\r
-                    FileContent = ''\r
+                    FileContent = BytesIO()\r
                     MaxAlignIndex = 0\r
                     MaxAlignValue = 1\r
                     for Index, File in enumerate(self.FileName):\r
@@ -107,20 +101,20 @@ class FileStatement (FileStatementClassObject) :
                         Content = f.read()\r
                         f.close()\r
                         AlignValue = 1\r
-                        if self.SubAlignment[Index] != None:\r
+                        if self.SubAlignment[Index]:\r
                             AlignValue = GenFdsGlobalVariable.GetAlignment(self.SubAlignment[Index])\r
                         if AlignValue > MaxAlignValue:\r
                             MaxAlignIndex = Index\r
                             MaxAlignValue = AlignValue\r
-                        FileContent += Content\r
-                        if len(FileContent) % AlignValue != 0:\r
-                            Size = AlignValue - len(FileContent) % AlignValue\r
+                        FileContent.write(Content)\r
+                        if len(FileContent.getvalue()) % AlignValue != 0:\r
+                            Size = AlignValue - len(FileContent.getvalue()) % AlignValue\r
                             for i in range(0, Size):\r
-                                FileContent += pack('B', 0xFF)\r
+                                FileContent.write(pack('B', 0xFF))\r
 \r
-                    if FileContent:\r
+                    if FileContent.getvalue() != b'':\r
                         OutputRAWFile = os.path.join(GenFdsGlobalVariable.FfsDir, self.NameGuid, self.NameGuid + '.raw')\r
-                        SaveFileOnChange(OutputRAWFile, FileContent, True)\r
+                        SaveFileOnChange(OutputRAWFile, FileContent.getvalue(), True)\r
                         self.FileName = OutputRAWFile\r
                         self.SubAlignment = self.SubAlignment[MaxAlignIndex]\r
 \r
@@ -139,7 +133,7 @@ class FileStatement (FileStatementClassObject) :
             SectionFiles = []\r
             Index = 0\r
             SectionAlignments = []\r
-            for section in self.SectionList :\r
+            for section in self.SectionList:\r
                 Index = Index + 1\r
                 SecIndex = '%d' %Index\r
                 # process the inside FvImage from FvSection or GuidSection\r
@@ -148,7 +142,7 @@ class FileStatement (FileStatementClassObject) :
                         section.FvAddr = FvChildAddr.pop(0)\r
                     elif isinstance(section, GuidSection):\r
                         section.FvAddr = FvChildAddr\r
-                if FvParentAddr != None and isinstance(section, GuidSection):\r
+                if FvParentAddr and isinstance(section, GuidSection):\r
                     section.FvParentAddr = FvParentAddr\r
 \r
                 if self.KeepReloc == False:\r
@@ -164,7 +158,7 @@ class FileStatement (FileStatementClassObject) :
         #\r
         FfsFileOutput = os.path.join(OutputDir, self.NameGuid + '.ffs')\r
         GenFdsGlobalVariable.GenerateFfs(FfsFileOutput, SectionFiles,\r
-                                         Ffs.Ffs.FdfFvFileTypeToFileType.get(self.FvFileType),\r
+                                         FdfFvFileTypeToFileType.get(self.FvFileType),\r
                                          self.NameGuid,\r
                                          Fixed=self.Fixed,\r
                                          CheckSum=self.CheckSum,\r
@@ -173,6 +167,3 @@ class FileStatement (FileStatementClassObject) :
                                         )\r
 \r
         return FfsFileOutput\r
-\r
-\r
-\r