]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenFds/Capsule.py
BaseTools: Move FindExtendTool to GenFdsGlobalVariable.py
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / Capsule.py
index f8af12a7c27afbeabbc27f38bc01ab7797320d74..8471cd5d0f7a813d30caec719996561eb949986b 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # generate capsule\r
 #\r
-#  Copyright (c) 2007 - 2016, 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 GenFdsGlobalVariable import GenFdsGlobalVariable\r
+from __future__ import absolute_import\r
+from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
+from .GenFdsGlobalVariable import FindExtendTool\r
 from CommonDataClass.FdfClass import CapsuleClassObject\r
 import Common.LongFilePathOs as os\r
 import subprocess\r
-import StringIO\r
+from io import BytesIO\r
 from Common.Misc import SaveFileOnChange\r
-from GenFds import GenFds\r
 from Common.Misc import PackRegistryFormatGuid\r
 import uuid\r
 from struct import pack\r
-from GenFds import FindExtendTool\r
 from Common import EdkLogger\r
 from Common.BuildToolError import *\r
 \r
@@ -66,7 +66,7 @@ class Capsule (CapsuleClassObject) :
         #     UINT32            CapsuleImageSize;\r
         # } EFI_CAPSULE_HEADER;\r
         #\r
-        Header = StringIO.StringIO()\r
+        Header = BytesIO()\r
         #\r
         # Use FMP capsule GUID: 6DCBD5ED-E82D-4C44-BDA1-7194199AD92A\r
         #\r
@@ -97,7 +97,7 @@ class Capsule (CapsuleClassObject) :
         #     // UINT64 ItemOffsetList[];\r
         # } EFI_FIRMWARE_MANAGEMENT_CAPSULE_HEADER;\r
         #\r
-        FwMgrHdr = StringIO.StringIO()\r
+        FwMgrHdr = BytesIO()\r
         if 'CAPSULE_HEADER_INIT_VERSION' in self.TokensDict:\r
             FwMgrHdr.write(pack('=I', int(self.TokensDict['CAPSULE_HEADER_INIT_VERSION'], 16)))\r
         else:\r
@@ -132,7 +132,7 @@ class Capsule (CapsuleClassObject) :
         #\r
 \r
         PreSize = FwMgrHdrSize\r
-        Content = StringIO.StringIO()\r
+        Content = BytesIO()\r
         for driver in self.CapsuleDataList:\r
             FileName = driver.GenCapsuleSubItem()\r
             FwMgrHdr.write(pack('=Q', PreSize))\r
@@ -141,6 +141,17 @@ class Capsule (CapsuleClassObject) :
             Content.write(File.read())\r
             File.close()\r
         for fmp in self.FmpPayloadList:\r
+            if fmp.Existed:\r
+                FwMgrHdr.write(pack('=Q', PreSize))\r
+                PreSize += len(fmp.Buffer)\r
+                Content.write(fmp.Buffer)\r
+                continue\r
+            if fmp.ImageFile:\r
+                for Obj in fmp.ImageFile:\r
+                    fmp.ImageFile = Obj.GenCapsuleSubItem()\r
+            if fmp.VendorCodeFile:\r
+                for Obj in fmp.VendorCodeFile:\r
+                    fmp.VendorCodeFile = Obj.GenCapsuleSubItem()\r
             if fmp.Certificate_Guid:\r
                 ExternalTool, ExternalOption = FindExtendTool([], GenFdsGlobalVariable.ArchList, fmp.Certificate_Guid)\r
                 CmdOption = ''\r
@@ -148,7 +159,7 @@ class Capsule (CapsuleClassObject) :
                 if not os.path.isabs(fmp.ImageFile):\r
                     CapInputFile = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, fmp.ImageFile)\r
                 CapOutputTmp = os.path.join(GenFdsGlobalVariable.FvDir, self.UiCapsuleName) + '.tmp'\r
-                if ExternalTool == None:\r
+                if ExternalTool is None:\r
                     EdkLogger.error("GenFds", GENFDS_ERROR, "No tool found with GUID %s" % fmp.Certificate_Guid)\r
                 else:\r
                     CmdOption += ExternalTool\r
@@ -161,27 +172,14 @@ class Capsule (CapsuleClassObject) :
                     dwLength = 4 + 2 + 2 + 16 + os.path.getsize(CapOutputTmp) - os.path.getsize(CapInputFile)\r
                 else:\r
                     dwLength = 4 + 2 + 2 + 16 + 16 + 256 + 256\r
-                Buffer  = pack('Q', fmp.MonotonicCount)\r
-                Buffer += pack('I', dwLength)\r
-                Buffer += pack('H', WIN_CERT_REVISION)\r
-                Buffer += pack('H', WIN_CERT_TYPE_EFI_GUID)\r
-                Buffer += uuid.UUID(fmp.Certificate_Guid).get_bytes_le()\r
-                if os.path.exists(CapOutputTmp):\r
-                    TmpFile = open(CapOutputTmp, 'rb')\r
-                    Buffer += TmpFile.read()\r
-                    TmpFile.close()\r
-                    if fmp.VendorCodeFile:\r
-                        VendorFile = open(fmp.VendorCodeFile, 'rb')\r
-                        Buffer += VendorFile.read()\r
-                        VendorFile.close()\r
-                    FwMgrHdr.write(pack('=Q', PreSize))\r
-                    PreSize += len(Buffer)\r
-                    Content.write(Buffer)\r
+                fmp.ImageFile = CapOutputTmp\r
+                AuthData = [fmp.MonotonicCount, dwLength, WIN_CERT_REVISION, WIN_CERT_TYPE_EFI_GUID, fmp.Certificate_Guid]\r
+                fmp.Buffer = fmp.GenCapsuleSubItem(AuthData)\r
             else:\r
-                payload = fmp.GenCapsuleSubItem()\r
-                FwMgrHdr.write(pack('=Q', PreSize))\r
-                PreSize += len(payload)\r
-                Content.write(payload)\r
+                fmp.Buffer = fmp.GenCapsuleSubItem()\r
+            FwMgrHdr.write(pack('=Q', PreSize))\r
+            PreSize += len(fmp.Buffer)\r
+            Content.write(fmp.Buffer)\r
         BodySize = len(FwMgrHdr.getvalue()) + len(Content.getvalue())\r
         Header.write(pack('=I', HdrSize + BodySize))\r
         #\r
@@ -203,11 +201,12 @@ class Capsule (CapsuleClassObject) :
     #   @retval string      Generated Capsule file path\r
     #\r
     def GenCapsule(self):\r
-        if self.UiCapsuleName.upper() + 'cap' in GenFds.ImageBinDict.keys():\r
+        from .GenFds import GenFds\r
+        if self.UiCapsuleName.upper() + 'cap' in GenFds.ImageBinDict:\r
             return GenFds.ImageBinDict[self.UiCapsuleName.upper() + 'cap']\r
 \r
         GenFdsGlobalVariable.InfLogger( "\nGenerate %s Capsule" %self.UiCapsuleName)\r
-        if ('CAPSULE_GUID' in self.TokensDict and \r
+        if ('CAPSULE_GUID' in self.TokensDict and\r
             uuid.UUID(self.TokensDict['CAPSULE_GUID']) == uuid.UUID('6DCBD5ED-E82D-4C44-BDA1-7194199AD92A')):\r
             return self.GenFmpCapsule()\r
 \r
@@ -249,15 +248,15 @@ class Capsule (CapsuleClassObject) :
     def GenCapInf(self):\r
         self.CapInfFileName = os.path.join(GenFdsGlobalVariable.FvDir,\r
                                    self.UiCapsuleName +  "_Cap" + '.inf')\r
-        CapInfFile = StringIO.StringIO() #open (self.CapInfFileName , 'w+')\r
+        CapInfFile = BytesIO() #open (self.CapInfFileName , 'w+')\r
 \r
         CapInfFile.writelines("[options]" + T_CHAR_LF)\r
 \r
-        for Item in self.TokensDict.keys():\r
+        for Item in self.TokensDict:\r
             CapInfFile.writelines("EFI_"                    + \\r
                                   Item                      + \\r
                                   ' = '                     + \\r
-                                  self.TokensDict.get(Item) + \\r
+                                  self.TokensDict[Item]     + \\r
                                   T_CHAR_LF)\r
 \r
         return CapInfFile\r