]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPlatformPkg/Scripts/Ds5/firmware_volume.py
ArmPlatformPkg: bring DS-5 scripts in line with linker script changes
[mirror_edk2.git] / ArmPlatformPkg / Scripts / Ds5 / firmware_volume.py
index 651c748a10a18b9df8935f6adfa0dead38702dd2..c434e3de19da766053c32e9077999fcf3dc011ea 100644 (file)
@@ -1,13 +1,13 @@
 #\r
 #  Copyright (c) 2011-2013, ARM Limited. All rights reserved.\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
+#  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
 #\r
 \r
 from arm_ds.debugger_v1 import DebugException\r
@@ -16,29 +16,29 @@ import struct
 import string\r
 \r
 import edk2_debugger\r
-    \r
+\r
 class EfiFileSection(object):\r
     EFI_SECTION_PE32                  = 0x10\r
     EFI_SECTION_PIC                   = 0x11\r
     EFI_SECTION_TE                    = 0x12\r
-    \r
+\r
     EFI_IMAGE_DEBUG_TYPE_CODEVIEW     = 0x2\r
-    \r
+\r
     SIZEOF_EFI_FFS_FILE_HEADER        = 0x28\r
 \r
     def __init__(self, ec, base):\r
         self.base = base\r
         self.ec = ec\r
-        \r
+\r
     def __str__(self):\r
         return "FileSection(type:0x%X, size:0x%x)" % (self.get_type(), self.get_size())\r
-        \r
+\r
     def get_base(self):\r
         return self.base\r
\r
+\r
     def get_type(self):\r
         return struct.unpack("B", self.ec.getMemoryService().read(self.base + 0x3, 1, 8))[0]\r
-       \r
+\r
     def get_size(self):\r
         return (struct.unpack("<I", self.ec.getMemoryService().read(self.base, 4, 32))[0] & 0x00ffffff)\r
 \r
@@ -55,23 +55,23 @@ class EfiFileSection(object):
 class EfiSectionTE:\r
     SIZEOF_EFI_TE_IMAGE_HEADER        = 0x28\r
     EFI_TE_IMAGE_SIGNATURE            = ('V','Z')\r
-    \r
+\r
     def __init__(self, ec, base_te):\r
         self.ec = ec\r
         self.base_te = int(base_te)\r
         te_sig = struct.unpack("cc", self.ec.getMemoryService().read(self.base_te, 2, 32))\r
         if te_sig != EfiSectionTE.EFI_TE_IMAGE_SIGNATURE:\r
             raise Exception("EfiFileSectionTE","TE Signature incorrect")\r
-        \r
+\r
     def get_debug_filepath(self):\r
         stripped_size = struct.unpack("<H", self.ec.getMemoryService().read(self.base_te + 0x6, 2, 32))[0]\r
         stripped_size -= EfiSectionTE.SIZEOF_EFI_TE_IMAGE_HEADER\r
-        \r
+\r
         debug_dir_entry_rva = self.ec.getMemoryService().readMemory32(self.base_te + 0x20)\r
         if debug_dir_entry_rva == 0:\r
             raise Exception("EfiFileSectionTE","No debug directory for image")\r
         debug_dir_entry_rva -= stripped_size\r
-        \r
+\r
         debug_type = self.ec.getMemoryService().readMemory32(self.base_te + debug_dir_entry_rva + 0xC)\r
         if (debug_type != 0xdf) and (debug_type != EfiFileSection.EFI_IMAGE_DEBUG_TYPE_CODEVIEW):\r
             raise Exception("EfiFileSectionTE","Debug type is not dwarf")\r
@@ -82,21 +82,19 @@ class EfiSectionTE:
         dwarf_sig = struct.unpack("cccc", self.ec.getMemoryService().read(self.base_te + debug_rva, 4, 32))\r
         if (dwarf_sig != 0x66727764) and (dwarf_sig != FirmwareFile.CONST_NB10_SIGNATURE):\r
             raise Exception("EfiFileSectionTE","Dwarf debug signature not found")\r
-        \r
+\r
         if dwarf_sig == 0x66727764:\r
             filename = self.base_te + debug_rva + 0xc\r
         else:\r
             filename = self.base_te + debug_rva + 0x10\r
         filename = struct.unpack("200s", self.ec.getMemoryService().read(filename, 200, 32))[0]\r
         return filename[0:string.find(filename,'\0')]\r
-        \r
+\r
     def get_debug_elfbase(self):\r
         stripped_size = struct.unpack("<H", self.ec.getMemoryService().read(self.base_te + 0x6, 2, 32))[0]\r
         stripped_size -= EfiSectionTE.SIZEOF_EFI_TE_IMAGE_HEADER\r
-        \r
-        base_of_code = self.ec.getMemoryService().readMemory32(self.base_te + 0xC)\r
-        \r
-        return self.base_te + base_of_code - stripped_size\r
+\r
+        return self.base_te - stripped_size\r
 \r
 class EfiSectionPE32:\r
     def __init__(self, ec, base_pe32):\r
@@ -115,33 +113,60 @@ class EfiSectionPE32:
         debug_type = self.ec.getMemoryService().readMemory32(self.base_pe32 + debug_dir_entry_rva + 0xC)\r
         if (debug_type != 0xdf) and (debug_type != EfiFileSection.EFI_IMAGE_DEBUG_TYPE_CODEVIEW):\r
             raise Exception("EfiFileSectionPE32","Debug type is not dwarf")\r
-        \r
-        \r
+\r
+\r
         debug_rva = self.ec.getMemoryService().readMemory32(self.base_pe32 + debug_dir_entry_rva + 0x14)\r
-        \r
+\r
         dwarf_sig = struct.unpack("cccc", self.ec.getMemoryService().read(str(self.base_pe32 + debug_rva), 4, 32))\r
         if (dwarf_sig != 0x66727764) and (dwarf_sig != FirmwareFile.CONST_NB10_SIGNATURE):\r
             raise Exception("EfiFileSectionPE32","Dwarf debug signature not found")\r
-    \r
+\r
         if dwarf_sig == 0x66727764:\r
             filename = self.base_pe32 + debug_rva + 0xc\r
         else:\r
             filename = self.base_pe32 + debug_rva + 0x10\r
         filename = struct.unpack("200s", self.ec.getMemoryService().read(str(filename), 200, 32))[0]\r
         return filename[0:string.find(filename,'\0')]\r
-    \r
+\r
     def get_debug_elfbase(self):\r
-        # Offset from dos hdr to PE file hdr\r
-        pe_file_header = self.base_pe32 + self.ec.getMemoryService().readMemory32(self.base_pe32 + 0x3C)\r
-        \r
-        base_of_code = self.base_pe32 + self.ec.getMemoryService().readMemory32(pe_file_header + 0x28)\r
-        base_of_data = self.base_pe32 + self.ec.getMemoryService().readMemory32(pe_file_header + 0x2C)\r
-        \r
-        if (base_of_code < base_of_data) and (base_of_code != 0):\r
-            return base_of_code\r
+        return self.base_pe32\r
+\r
+class EfiSectionPE64:\r
+    def __init__(self, ec, base_pe64):\r
+        self.ec = ec\r
+        self.base_pe64 = base_pe64\r
+\r
+    def get_debug_filepath(self):\r
+        # Offset from dos hdr to PE file hdr (EFI_IMAGE_NT_HEADERS64)\r
+        #file_header_offset = self.ec.getMemoryService().readMemory32(self.base_pe64 + 0x3C)\r
+        file_header_offset = 0x0\r
+\r
+        # Offset to debug dir in PE hdrs\r
+        debug_dir_entry_rva = self.ec.getMemoryService().readMemory32(self.base_pe64 + file_header_offset + 0x138)\r
+        if debug_dir_entry_rva == 0:\r
+            raise Exception("EfiFileSectionPE64","No Debug Directory")\r
+\r
+        debug_type = self.ec.getMemoryService().readMemory32(self.base_pe64 + debug_dir_entry_rva + 0xC)\r
+        if (debug_type != 0xdf) and (debug_type != EfiFileSection.EFI_IMAGE_DEBUG_TYPE_CODEVIEW):\r
+            raise Exception("EfiFileSectionPE64","Debug type is not dwarf")\r
+\r
+\r
+        debug_rva = self.ec.getMemoryService().readMemory32(self.base_pe64 + debug_dir_entry_rva + 0x14)\r
+\r
+        dwarf_sig = struct.unpack("cccc", self.ec.getMemoryService().read(str(self.base_pe64 + debug_rva), 4, 32))\r
+        if (dwarf_sig != 0x66727764) and (dwarf_sig != FirmwareFile.CONST_NB10_SIGNATURE):\r
+            raise Exception("EfiFileSectionPE64","Dwarf debug signature not found")\r
+\r
+        if dwarf_sig == 0x66727764:\r
+            filename = self.base_pe64 + debug_rva + 0xc\r
         else:\r
-            return base_of_data       \r
-    \r
+            filename = self.base_pe64 + debug_rva + 0x10\r
+        filename = struct.unpack("200s", self.ec.getMemoryService().read(str(filename), 200, 32))[0]\r
+        return filename[0:string.find(filename,'\0')]\r
+\r
+    def get_debug_elfbase(self):\r
+        return self.base_pe64\r
+\r
 class FirmwareFile:\r
     EFI_FV_FILETYPE_RAW                   = 0x01\r
     EFI_FV_FILETYPE_FREEFORM              = 0x02\r
@@ -154,42 +179,42 @@ class FirmwareFile:
     EFI_FV_FILETYPE_APPLICATION           = 0x09\r
     EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE = 0x0B\r
     EFI_FV_FILETYPE_FFS_MIN               = 0xF0\r
-    \r
+\r
     CONST_NB10_SIGNATURE = ('N','B','1','0')\r
-    \r
+\r
     def __init__(self, fv, base, ec):\r
         self.fv = fv\r
         self.base = base\r
         self.ec = ec\r
-        \r
+\r
     def __str__(self):\r
         return "FFS(state:0x%x, type:0x%X, size:0x%x)" % (self.get_state(), self.get_type(), self.get_size())\r
-    \r
+\r
     def get_base(self):\r
         return self.base\r
-    \r
+\r
     def get_size(self):\r
         size = (self.ec.getMemoryService().readMemory32(self.base + 0x14) & 0x00ffffff)\r
 \r
         # Occupied size is the size considering the alignment\r
         return size + ((0x8 - (size & 0x7)) & 0x7)\r
-    \r
+\r
     def get_type(self):\r
         return self.ec.getMemoryService().readMemory8(self.base + 0x12)\r
-     \r
+\r
     def get_state(self):\r
         state = self.ec.getMemoryService().readMemory8(self.base + 0x17)\r
-        \r
+\r
         polarity = self.fv.get_polarity()\r
         if polarity:\r
             state = ~state\r
-            \r
+\r
         highest_bit = 0x80;\r
         while (highest_bit != 0) and ((highest_bit & state) == 0):\r
             highest_bit >>= 1\r
-            \r
+\r
         return highest_bit\r
-    \r
+\r
     def get_next_section(self, section=None):\r
         if section == None:\r
             if self.get_type() != FirmwareFile.EFI_FV_FILETYPE_FFS_MIN:\r
@@ -198,7 +223,7 @@ class FirmwareFile:
                 return None\r
         else:\r
             section_base = int(section.get_base() + section.get_size())\r
-            \r
+\r
             # Align to next 4 byte boundary\r
             if (section_base & 0x3) != 0:\r
                 section_base = section_base + 0x4 - (section_base & 0x3)\r
@@ -207,38 +232,38 @@ class FirmwareFile:
             return EfiFileSection(self.ec, section_base)\r
         else:\r
             return None\r
-    \r
+\r
 class FirmwareVolume:\r
     CONST_FV_SIGNATURE = ('_','F','V','H')\r
     EFI_FVB2_ERASE_POLARITY = 0x800\r
-    \r
+\r
     DebugInfos = []\r
-    \r
+\r
     def __init__(self, ec, fv_base, fv_size):\r
         self.ec = ec\r
         self.fv_base = fv_base\r
         self.fv_size = fv_size\r
-        \r
+\r
         try:\r
             signature = struct.unpack("cccc", self.ec.getMemoryService().read(fv_base + 0x28, 4, 32))\r
         except DebugException:\r
             raise Exception("FirmwareVolume", "Not possible to access the defined firmware volume at [0x%X,0x%X]. Could be the used build report does not correspond to your current debugging context." % (int(fv_base),int(fv_base+fv_size)))\r
         if signature != FirmwareVolume.CONST_FV_SIGNATURE:\r
             raise Exception("FirmwareVolume", "This is not a valid firmware volume")\r
-      \r
+\r
     def get_size(self):\r
         return self.ec.getMemoryService().readMemory32(self.fv_base + 0x20)\r
-    \r
+\r
     def get_attributes(self):\r
         return self.ec.getMemoryService().readMemory32(self.fv_base + 0x2C)\r
-    \r
+\r
     def get_polarity(self):\r
         attributes = self.get_attributes()\r
         if attributes & FirmwareVolume.EFI_FVB2_ERASE_POLARITY:\r
             return 1\r
         else:\r
             return 0\r
-    \r
+\r
     def get_next_ffs(self, ffs=None):\r
         if ffs == None:\r
             # Get the offset of the first FFS file from the FV header\r
@@ -246,21 +271,21 @@ class FirmwareVolume:
         else:\r
             # Goto the next FFS file\r
             ffs_base = int(ffs.get_base() + ffs.get_size())\r
-            \r
+\r
             # Align to next 8 byte boundary\r
             if (ffs_base & 0x7) != 0:\r
                 ffs_base = ffs_base + 0x8 - (ffs_base & 0x7)\r
-                \r
+\r
         if ffs_base < self.fv_base + self.get_size():\r
             return FirmwareFile(self, ffs_base, self.ec)\r
         else:\r
             return None\r
-        \r
-    def get_debug_info(self):        \r
+\r
+    def get_debug_info(self):\r
         self.DebugInfos = []\r
-        \r
+\r
         ffs = self.get_next_ffs()\r
-        while ffs != None:            \r
+        while ffs != None:\r
             section = ffs.get_next_section()\r
             while section != None:\r
                 type = section.get_type()\r
@@ -272,7 +297,7 @@ class FirmwareVolume:
     def load_symbols_at(self, addr, verbose = False):\r
         if self.DebugInfos == []:\r
             self.get_debug_info()\r
-        \r
+\r
         for debug_info in self.DebugInfos:\r
             if (addr >= debug_info[0]) and (addr < debug_info[0] + debug_info[1]):\r
                 if debug_info[2] == EfiFileSection.EFI_SECTION_TE:\r
@@ -281,20 +306,19 @@ class FirmwareVolume:
                     section = EfiSectionPE32(self.ec, debug_info[0] + 0x4)\r
                 else:\r
                     raise Exception('FirmwareVolume','Section Type not supported')\r
-                \r
+\r
                 try:\r
                     edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase(), verbose)\r
                 except Exception, (ErrorClass, ErrorMessage):\r
                     if verbose:\r
                         print "Error while loading a symbol file (%s: %s)" % (ErrorClass, ErrorMessage)\r
-                    pass\r
 \r
                 return debug_info\r
 \r
     def load_all_symbols(self, verbose = False):\r
         if self.DebugInfos == []:\r
             self.get_debug_info()\r
-        \r
+\r
         for debug_info in self.DebugInfos:\r
             if debug_info[2] == EfiFileSection.EFI_SECTION_TE:\r
                 section = EfiSectionTE(self.ec, debug_info[0] + 0x4)\r
@@ -302,11 +326,10 @@ class FirmwareVolume:
                 section = EfiSectionPE32(self.ec, debug_info[0] + 0x4)\r
             else:\r
                 continue\r
-            \r
+\r
             try:\r
                 edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase(), verbose)\r
             except Exception, (ErrorClass, ErrorMessage):\r
                 if verbose:\r
                     print "Error while loading a symbol file (%s: %s)" % (ErrorClass, ErrorMessage)\r
-                pass\r
 \r