]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/FMMT/core/FMMTParser.py
BaseTools: Add FMMT Python Tool
[mirror_edk2.git] / BaseTools / Source / Python / FMMT / core / FMMTParser.py
diff --git a/BaseTools/Source/Python/FMMT/core/FMMTParser.py b/BaseTools/Source/Python/FMMT/core/FMMTParser.py
new file mode 100644 (file)
index 0000000..e76ac51
--- /dev/null
@@ -0,0 +1,87 @@
+## @file\r
+# This file is used to define the interface of Bios Parser.\r
+#\r
+# Copyright (c) 2021-, Intel Corporation. All rights reserved.<BR>\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+from FirmwareStorageFormat.Common import *\r
+from core.BinaryFactoryProduct import ParserEntry\r
+from core.BiosTreeNode import *\r
+from core.BiosTree import *\r
+from core.GuidTools import *\r
+from utils.FmmtLogger import FmmtLogger as logger\r
+\r
+class FMMTParser:\r
+    def __init__(self, name: str, TYPE: str) -> None:\r
+        self.WholeFvTree = BIOSTREE(name)\r
+        self.WholeFvTree.type = TYPE\r
+        self.FinalData = b''\r
+        self.BinaryInfo = []\r
+\r
+    ## Parser the nodes in WholeTree.\r
+    def ParserFromRoot(self, WholeFvTree=None, whole_data: bytes=b'', Reloffset: int=0) -> None:\r
+        if WholeFvTree.type == ROOT_TREE or WholeFvTree.type == ROOT_FV_TREE:\r
+            ParserEntry().DataParser(self.WholeFvTree, whole_data, Reloffset)\r
+        else:\r
+            ParserEntry().DataParser(WholeFvTree, whole_data, Reloffset)\r
+        for Child in WholeFvTree.Child:\r
+            self.ParserFromRoot(Child, "")\r
+\r
+    ## Encapuslation all the data in tree into self.FinalData\r
+    def Encapsulation(self, rootTree, CompressStatus: bool) -> None:\r
+        # If current node is Root node, skip it.\r
+        if rootTree.type == ROOT_TREE or rootTree.type == ROOT_FV_TREE or rootTree.type == ROOT_FFS_TREE or rootTree.type == ROOT_SECTION_TREE:\r
+            logger.debug('Encapsulated successfully!')\r
+        # If current node do not have Header, just add Data.\r
+        elif rootTree.type == BINARY_DATA or rootTree.type == FFS_FREE_SPACE:\r
+            self.FinalData += rootTree.Data.Data\r
+            rootTree.Child = []\r
+        # If current node do not have Child and ExtHeader, just add its Header and Data.\r
+        elif rootTree.type == DATA_FV_TREE or rootTree.type == FFS_PAD:\r
+            self.FinalData += struct2stream(rootTree.Data.Header) + rootTree.Data.Data + rootTree.Data.PadData\r
+            if rootTree.isFinalChild():\r
+                ParTree = rootTree.Parent\r
+                if ParTree.type != 'ROOT':\r
+                    self.FinalData += ParTree.Data.PadData\r
+            rootTree.Child = []\r
+        # If current node is not Section node and may have Child and ExtHeader, add its Header,ExtHeader. If do not have Child, add its Data.\r
+        elif rootTree.type == FV_TREE or rootTree.type == FFS_TREE or rootTree.type == SEC_FV_TREE:\r
+            if rootTree.HasChild():\r
+                self.FinalData += struct2stream(rootTree.Data.Header)\r
+            else:\r
+                self.FinalData += struct2stream(rootTree.Data.Header) + rootTree.Data.Data + rootTree.Data.PadData\r
+                if rootTree.isFinalChild():\r
+                    ParTree = rootTree.Parent\r
+                    if ParTree.type != 'ROOT':\r
+                        self.FinalData += ParTree.Data.PadData\r
+        # If current node is Section, need to consider its ExtHeader, Child and Compressed Status.\r
+        elif rootTree.type == SECTION_TREE:\r
+            # Not compressed section\r
+            if rootTree.Data.OriData == b'' or (rootTree.Data.OriData != b'' and CompressStatus):\r
+                if rootTree.HasChild():\r
+                    if rootTree.Data.ExtHeader:\r
+                        self.FinalData += struct2stream(rootTree.Data.Header) + struct2stream(rootTree.Data.ExtHeader)\r
+                    else:\r
+                        self.FinalData += struct2stream(rootTree.Data.Header)\r
+                else:\r
+                    Data = rootTree.Data.Data\r
+                    if rootTree.Data.ExtHeader:\r
+                        self.FinalData += struct2stream(rootTree.Data.Header) + struct2stream(rootTree.Data.ExtHeader) + Data + rootTree.Data.PadData\r
+                    else:\r
+                        self.FinalData += struct2stream(rootTree.Data.Header) + Data + rootTree.Data.PadData\r
+                    if rootTree.isFinalChild():\r
+                        ParTree = rootTree.Parent\r
+                        self.FinalData += ParTree.Data.PadData\r
+            # If compressed section\r
+            else:\r
+                Data = rootTree.Data.OriData\r
+                rootTree.Child = []\r
+                if rootTree.Data.ExtHeader:\r
+                    self.FinalData += struct2stream(rootTree.Data.Header) + struct2stream(rootTree.Data.ExtHeader) + Data + rootTree.Data.PadData\r
+                else:\r
+                    self.FinalData += struct2stream(rootTree.Data.Header) + Data + rootTree.Data.PadData\r
+                if rootTree.isFinalChild():\r
+                    ParTree = rootTree.Parent\r
+                    self.FinalData += ParTree.Data.PadData\r
+        for Child in rootTree.Child:\r
+            self.Encapsulation(Child, CompressStatus)\r