]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenFds/CapsuleData.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / CapsuleData.py
index 2d532bea04c1186c313e0549e93ac65fb5b5a77d..ebbde7f8708c556b3d350d7149da2f2226b605e0 100644 (file)
@@ -1,23 +1,21 @@
 ## @file\r
 # generate capsule\r
 #\r
-#  Copyright (c) 2007-2013, 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
-from GenFdsGlobalVariable import GenFdsGlobalVariable\r
-import StringIO\r
+from __future__ import absolute_import\r
+from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
+from io import BytesIO\r
+from struct import pack\r
+import os\r
+from Common.Misc import SaveFileOnChange\r
+import uuid\r
 \r
 ## base class for capsule data\r
 #\r
@@ -28,13 +26,13 @@ class CapsuleData:
     #   @param  self        The object pointer\r
     def __init__(self):\r
         pass\r
-    \r
+\r
     ## generate capsule data\r
     #\r
     #   @param  self        The object pointer\r
     def GenCapsuleSubItem(self):\r
         pass\r
-        \r
+\r
 ## FFS class for capsule data\r
 #\r
 #\r
@@ -76,9 +74,9 @@ class CapsuleFv (CapsuleData):
     #\r
     def GenCapsuleSubItem(self):\r
         if self.FvName.find('.fv') == -1:\r
-            if self.FvName.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
-                FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName.upper())\r
-                FdBuffer = StringIO.StringIO('')\r
+            if self.FvName.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict:\r
+                FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[self.FvName.upper()]\r
+                FdBuffer = BytesIO()\r
                 FvObj.CapsuleName = self.CapsuleName\r
                 FvFile = FvObj.AddToBuffer(FdBuffer)\r
                 FvObj.CapsuleName = None\r
@@ -108,14 +106,14 @@ class CapsuleFd (CapsuleData):
     #\r
     def GenCapsuleSubItem(self):\r
         if self.FdName.find('.fd') == -1:\r
-            if self.FdName.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
-                FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(self.FdName.upper())\r
+            if self.FdName.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict:\r
+                FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[self.FdName.upper()]\r
                 FdFile = FdObj.GenFd()\r
                 return FdFile\r
         else:\r
             FdFile = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FdName)\r
             return FdFile\r
-        \r
+\r
 ## AnyFile class for capsule data\r
 #\r
 #\r
@@ -135,7 +133,7 @@ class CapsuleAnyFile (CapsuleData):
     #\r
     def GenCapsuleSubItem(self):\r
         return self.FileName\r
-    \r
+\r
 ## Afile class for capsule data\r
 #\r
 #\r
@@ -154,4 +152,88 @@ class CapsuleAfile (CapsuleData):
     #   @retval string      Generated file name\r
     #\r
     def GenCapsuleSubItem(self):\r
-        return self.FileName
\ No newline at end of file
+        return self.FileName\r
+\r
+class CapsulePayload(CapsuleData):\r
+    '''Generate payload file, the header is defined below:\r
+    #pragma pack(1)\r
+    typedef struct {\r
+        UINT32 Version;\r
+        EFI_GUID UpdateImageTypeId;\r
+        UINT8 UpdateImageIndex;\r
+        UINT8 reserved_bytes[3];\r
+        UINT32 UpdateImageSize;\r
+        UINT32 UpdateVendorCodeSize;\r
+        UINT64 UpdateHardwareInstance; //Introduced in v2\r
+    } EFI_FIRMWARE_MANAGEMENT_CAPSULE_IMAGE_HEADER;\r
+    '''\r
+    def __init__(self):\r
+        self.UiName = None\r
+        self.Version = None\r
+        self.ImageTypeId = None\r
+        self.ImageIndex = None\r
+        self.HardwareInstance = None\r
+        self.ImageFile = []\r
+        self.VendorCodeFile = []\r
+        self.Certificate_Guid = None\r
+        self.MonotonicCount = None\r
+        self.Existed = False\r
+        self.Buffer = None\r
+\r
+    def GenCapsuleSubItem(self, AuthData=[]):\r
+        if not self.Version:\r
+            self.Version = '0x00000002'\r
+        if not self.ImageIndex:\r
+            self.ImageIndex = '0x1'\r
+        if not self.HardwareInstance:\r
+            self.HardwareInstance = '0x0'\r
+        ImageFileSize = os.path.getsize(self.ImageFile)\r
+        if AuthData:\r
+            # the ImageFileSize need include the full authenticated info size. From first bytes of MonotonicCount to last bytes of certificate.\r
+            # the 32 bit is the MonotonicCount, dwLength, wRevision, wCertificateType and CertType\r
+            ImageFileSize += 32\r
+        VendorFileSize = 0\r
+        if self.VendorCodeFile:\r
+            VendorFileSize = os.path.getsize(self.VendorCodeFile)\r
+\r
+        #\r
+        # Fill structure\r
+        #\r
+        Guid = self.ImageTypeId.split('-')\r
+        Buffer = pack('=ILHHBBBBBBBBBBBBIIQ',\r
+                       int(self.Version, 16),\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
+                       int(self.ImageIndex, 16),\r
+                       0,\r
+                       0,\r
+                       0,\r
+                       ImageFileSize,\r
+                       VendorFileSize,\r
+                       int(self.HardwareInstance, 16)\r
+                       )\r
+        if AuthData:\r
+            Buffer += pack('QIHH', AuthData[0], AuthData[1], AuthData[2], AuthData[3])\r
+            Buffer += uuid.UUID(AuthData[4]).bytes_le\r
+\r
+        #\r
+        # Append file content to the structure\r
+        #\r
+        ImageFile = open(self.ImageFile, 'rb')\r
+        Buffer += ImageFile.read()\r
+        ImageFile.close()\r
+        if self.VendorCodeFile:\r
+            VendorFile = open(self.VendorCodeFile, 'rb')\r
+            Buffer += VendorFile.read()\r
+            VendorFile.close()\r
+        self.Existed = True\r
+        return Buffer\r