]> git.proxmox.com Git - mirror_edk2.git/blame - 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
CommitLineData
1e57a462 1#\r
72efe027 2# Copyright (c) 2011-2013, ARM Limited. All rights reserved.\r
1e57a462 3#\r
3402aac7
RC
4# This program and the accompanying materials\r
5# are licensed and made available under the terms and conditions of the BSD License\r
6# which accompanies this distribution. The full text of the license may be found at\r
7# http://opensource.org/licenses/bsd-license.php\r
8#\r
9# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
1e57a462 11#\r
12\r
13from arm_ds.debugger_v1 import DebugException\r
14\r
15import struct\r
16import string\r
17\r
18import edk2_debugger\r
3402aac7 19\r
1e57a462 20class EfiFileSection(object):\r
21 EFI_SECTION_PE32 = 0x10\r
22 EFI_SECTION_PIC = 0x11\r
23 EFI_SECTION_TE = 0x12\r
3402aac7 24\r
1e57a462 25 EFI_IMAGE_DEBUG_TYPE_CODEVIEW = 0x2\r
3402aac7 26\r
1e57a462 27 SIZEOF_EFI_FFS_FILE_HEADER = 0x28\r
28\r
29 def __init__(self, ec, base):\r
30 self.base = base\r
31 self.ec = ec\r
3402aac7 32\r
1e57a462 33 def __str__(self):\r
34 return "FileSection(type:0x%X, size:0x%x)" % (self.get_type(), self.get_size())\r
3402aac7 35\r
1e57a462 36 def get_base(self):\r
37 return self.base\r
3402aac7 38\r
1e57a462 39 def get_type(self):\r
40 return struct.unpack("B", self.ec.getMemoryService().read(self.base + 0x3, 1, 8))[0]\r
3402aac7 41\r
1e57a462 42 def get_size(self):\r
43 return (struct.unpack("<I", self.ec.getMemoryService().read(self.base, 4, 32))[0] & 0x00ffffff)\r
44\r
45 def get_debug_filepath(self):\r
46 type = self.get_type()\r
47 if type == EfiFileSection.EFI_SECTION_TE:\r
48 section = EfiSectionTE(self, ec, self.base + 0x4)\r
49 elif type == EfiFileSection.EFI_SECTION_PE32:\r
50 section = EfiSectionPE32(self, ec, self.base + 0x4)\r
51 else:\r
52 raise Exception("EfiFileSection", "No debug section")\r
53 return section.get_debug_filepath()\r
54\r
55class EfiSectionTE:\r
56 SIZEOF_EFI_TE_IMAGE_HEADER = 0x28\r
57 EFI_TE_IMAGE_SIGNATURE = ('V','Z')\r
3402aac7 58\r
1e57a462 59 def __init__(self, ec, base_te):\r
60 self.ec = ec\r
61 self.base_te = int(base_te)\r
62 te_sig = struct.unpack("cc", self.ec.getMemoryService().read(self.base_te, 2, 32))\r
63 if te_sig != EfiSectionTE.EFI_TE_IMAGE_SIGNATURE:\r
64 raise Exception("EfiFileSectionTE","TE Signature incorrect")\r
3402aac7 65\r
1e57a462 66 def get_debug_filepath(self):\r
67 stripped_size = struct.unpack("<H", self.ec.getMemoryService().read(self.base_te + 0x6, 2, 32))[0]\r
68 stripped_size -= EfiSectionTE.SIZEOF_EFI_TE_IMAGE_HEADER\r
3402aac7 69\r
1e57a462 70 debug_dir_entry_rva = self.ec.getMemoryService().readMemory32(self.base_te + 0x20)\r
71 if debug_dir_entry_rva == 0:\r
72 raise Exception("EfiFileSectionTE","No debug directory for image")\r
73 debug_dir_entry_rva -= stripped_size\r
3402aac7 74\r
1e57a462 75 debug_type = self.ec.getMemoryService().readMemory32(self.base_te + debug_dir_entry_rva + 0xC)\r
76 if (debug_type != 0xdf) and (debug_type != EfiFileSection.EFI_IMAGE_DEBUG_TYPE_CODEVIEW):\r
77 raise Exception("EfiFileSectionTE","Debug type is not dwarf")\r
78\r
79 debug_rva = self.ec.getMemoryService().readMemory32(self.base_te + debug_dir_entry_rva + 0x14)\r
80 debug_rva -= stripped_size\r
81\r
82 dwarf_sig = struct.unpack("cccc", self.ec.getMemoryService().read(self.base_te + debug_rva, 4, 32))\r
83 if (dwarf_sig != 0x66727764) and (dwarf_sig != FirmwareFile.CONST_NB10_SIGNATURE):\r
84 raise Exception("EfiFileSectionTE","Dwarf debug signature not found")\r
3402aac7 85\r
1e57a462 86 if dwarf_sig == 0x66727764:\r
87 filename = self.base_te + debug_rva + 0xc\r
88 else:\r
89 filename = self.base_te + debug_rva + 0x10\r
90 filename = struct.unpack("200s", self.ec.getMemoryService().read(filename, 200, 32))[0]\r
91 return filename[0:string.find(filename,'\0')]\r
3402aac7 92\r
1e57a462 93 def get_debug_elfbase(self):\r
94 stripped_size = struct.unpack("<H", self.ec.getMemoryService().read(self.base_te + 0x6, 2, 32))[0]\r
95 stripped_size -= EfiSectionTE.SIZEOF_EFI_TE_IMAGE_HEADER\r
3402aac7 96\r
0ce8410e 97 return self.base_te - stripped_size\r
1e57a462 98\r
99class EfiSectionPE32:\r
100 def __init__(self, ec, base_pe32):\r
101 self.ec = ec\r
102 self.base_pe32 = base_pe32\r
103\r
104 def get_debug_filepath(self):\r
105 # Offset from dos hdr to PE file hdr\r
106 file_header_offset = self.ec.getMemoryService().readMemory32(self.base_pe32 + 0x3C)\r
107\r
108 # Offset to debug dir in PE hdrs\r
109 debug_dir_entry_rva = self.ec.getMemoryService().readMemory32(self.base_pe32 + file_header_offset + 0xA8)\r
110 if debug_dir_entry_rva == 0:\r
111 raise Exception("EfiFileSectionPE32","No Debug Directory")\r
112\r
113 debug_type = self.ec.getMemoryService().readMemory32(self.base_pe32 + debug_dir_entry_rva + 0xC)\r
114 if (debug_type != 0xdf) and (debug_type != EfiFileSection.EFI_IMAGE_DEBUG_TYPE_CODEVIEW):\r
115 raise Exception("EfiFileSectionPE32","Debug type is not dwarf")\r
3402aac7
RC
116\r
117\r
1e57a462 118 debug_rva = self.ec.getMemoryService().readMemory32(self.base_pe32 + debug_dir_entry_rva + 0x14)\r
3402aac7 119\r
1e57a462 120 dwarf_sig = struct.unpack("cccc", self.ec.getMemoryService().read(str(self.base_pe32 + debug_rva), 4, 32))\r
121 if (dwarf_sig != 0x66727764) and (dwarf_sig != FirmwareFile.CONST_NB10_SIGNATURE):\r
122 raise Exception("EfiFileSectionPE32","Dwarf debug signature not found")\r
3402aac7 123\r
1e57a462 124 if dwarf_sig == 0x66727764:\r
125 filename = self.base_pe32 + debug_rva + 0xc\r
126 else:\r
127 filename = self.base_pe32 + debug_rva + 0x10\r
128 filename = struct.unpack("200s", self.ec.getMemoryService().read(str(filename), 200, 32))[0]\r
129 return filename[0:string.find(filename,'\0')]\r
3402aac7 130\r
1e57a462 131 def get_debug_elfbase(self):\r
0ce8410e 132 return self.base_pe32\r
e3d495e1
HL
133\r
134class EfiSectionPE64:\r
135 def __init__(self, ec, base_pe64):\r
136 self.ec = ec\r
137 self.base_pe64 = base_pe64\r
138\r
139 def get_debug_filepath(self):\r
140 # Offset from dos hdr to PE file hdr (EFI_IMAGE_NT_HEADERS64)\r
141 #file_header_offset = self.ec.getMemoryService().readMemory32(self.base_pe64 + 0x3C)\r
142 file_header_offset = 0x0\r
143\r
144 # Offset to debug dir in PE hdrs\r
145 debug_dir_entry_rva = self.ec.getMemoryService().readMemory32(self.base_pe64 + file_header_offset + 0x138)\r
146 if debug_dir_entry_rva == 0:\r
147 raise Exception("EfiFileSectionPE64","No Debug Directory")\r
148\r
149 debug_type = self.ec.getMemoryService().readMemory32(self.base_pe64 + debug_dir_entry_rva + 0xC)\r
150 if (debug_type != 0xdf) and (debug_type != EfiFileSection.EFI_IMAGE_DEBUG_TYPE_CODEVIEW):\r
151 raise Exception("EfiFileSectionPE64","Debug type is not dwarf")\r
3402aac7
RC
152\r
153\r
e3d495e1 154 debug_rva = self.ec.getMemoryService().readMemory32(self.base_pe64 + debug_dir_entry_rva + 0x14)\r
3402aac7 155\r
e3d495e1
HL
156 dwarf_sig = struct.unpack("cccc", self.ec.getMemoryService().read(str(self.base_pe64 + debug_rva), 4, 32))\r
157 if (dwarf_sig != 0x66727764) and (dwarf_sig != FirmwareFile.CONST_NB10_SIGNATURE):\r
158 raise Exception("EfiFileSectionPE64","Dwarf debug signature not found")\r
3402aac7 159\r
e3d495e1
HL
160 if dwarf_sig == 0x66727764:\r
161 filename = self.base_pe64 + debug_rva + 0xc\r
162 else:\r
163 filename = self.base_pe64 + debug_rva + 0x10\r
164 filename = struct.unpack("200s", self.ec.getMemoryService().read(str(filename), 200, 32))[0]\r
165 return filename[0:string.find(filename,'\0')]\r
3402aac7 166\r
e3d495e1 167 def get_debug_elfbase(self):\r
0ce8410e 168 return self.base_pe64\r
3402aac7 169\r
1e57a462 170class FirmwareFile:\r
171 EFI_FV_FILETYPE_RAW = 0x01\r
172 EFI_FV_FILETYPE_FREEFORM = 0x02\r
173 EFI_FV_FILETYPE_SECURITY_CORE = 0x03\r
174 EFI_FV_FILETYPE_PEI_CORE = 0x04\r
175 EFI_FV_FILETYPE_DXE_CORE = 0x05\r
176 EFI_FV_FILETYPE_PEIM = 0x06\r
177 EFI_FV_FILETYPE_DRIVER = 0x07\r
178 EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER = 0x08\r
179 EFI_FV_FILETYPE_APPLICATION = 0x09\r
180 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE = 0x0B\r
181 EFI_FV_FILETYPE_FFS_MIN = 0xF0\r
3402aac7 182\r
1e57a462 183 CONST_NB10_SIGNATURE = ('N','B','1','0')\r
3402aac7 184\r
1e57a462 185 def __init__(self, fv, base, ec):\r
186 self.fv = fv\r
187 self.base = base\r
188 self.ec = ec\r
3402aac7 189\r
1e57a462 190 def __str__(self):\r
191 return "FFS(state:0x%x, type:0x%X, size:0x%x)" % (self.get_state(), self.get_type(), self.get_size())\r
3402aac7 192\r
1e57a462 193 def get_base(self):\r
194 return self.base\r
3402aac7 195\r
1e57a462 196 def get_size(self):\r
197 size = (self.ec.getMemoryService().readMemory32(self.base + 0x14) & 0x00ffffff)\r
198\r
199 # Occupied size is the size considering the alignment\r
200 return size + ((0x8 - (size & 0x7)) & 0x7)\r
3402aac7 201\r
1e57a462 202 def get_type(self):\r
203 return self.ec.getMemoryService().readMemory8(self.base + 0x12)\r
3402aac7 204\r
1e57a462 205 def get_state(self):\r
206 state = self.ec.getMemoryService().readMemory8(self.base + 0x17)\r
3402aac7 207\r
1e57a462 208 polarity = self.fv.get_polarity()\r
209 if polarity:\r
210 state = ~state\r
3402aac7 211\r
1e57a462 212 highest_bit = 0x80;\r
213 while (highest_bit != 0) and ((highest_bit & state) == 0):\r
214 highest_bit >>= 1\r
3402aac7 215\r
1e57a462 216 return highest_bit\r
3402aac7 217\r
1e57a462 218 def get_next_section(self, section=None):\r
219 if section == None:\r
220 if self.get_type() != FirmwareFile.EFI_FV_FILETYPE_FFS_MIN:\r
221 section_base = self.get_base() + 0x18;\r
222 else:\r
223 return None\r
224 else:\r
225 section_base = int(section.get_base() + section.get_size())\r
3402aac7 226\r
1e57a462 227 # Align to next 4 byte boundary\r
228 if (section_base & 0x3) != 0:\r
229 section_base = section_base + 0x4 - (section_base & 0x3)\r
230\r
231 if section_base < self.get_base() + self.get_size():\r
232 return EfiFileSection(self.ec, section_base)\r
233 else:\r
234 return None\r
3402aac7 235\r
1e57a462 236class FirmwareVolume:\r
237 CONST_FV_SIGNATURE = ('_','F','V','H')\r
238 EFI_FVB2_ERASE_POLARITY = 0x800\r
3402aac7 239\r
1e57a462 240 DebugInfos = []\r
3402aac7 241\r
1e57a462 242 def __init__(self, ec, fv_base, fv_size):\r
243 self.ec = ec\r
244 self.fv_base = fv_base\r
245 self.fv_size = fv_size\r
3402aac7 246\r
1e57a462 247 try:\r
248 signature = struct.unpack("cccc", self.ec.getMemoryService().read(fv_base + 0x28, 4, 32))\r
249 except DebugException:\r
250 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
251 if signature != FirmwareVolume.CONST_FV_SIGNATURE:\r
252 raise Exception("FirmwareVolume", "This is not a valid firmware volume")\r
3402aac7 253\r
1e57a462 254 def get_size(self):\r
255 return self.ec.getMemoryService().readMemory32(self.fv_base + 0x20)\r
3402aac7 256\r
1e57a462 257 def get_attributes(self):\r
258 return self.ec.getMemoryService().readMemory32(self.fv_base + 0x2C)\r
3402aac7 259\r
1e57a462 260 def get_polarity(self):\r
261 attributes = self.get_attributes()\r
262 if attributes & FirmwareVolume.EFI_FVB2_ERASE_POLARITY:\r
263 return 1\r
264 else:\r
265 return 0\r
3402aac7 266\r
1e57a462 267 def get_next_ffs(self, ffs=None):\r
268 if ffs == None:\r
269 # Get the offset of the first FFS file from the FV header\r
270 ffs_base = self.fv_base + self.ec.getMemoryService().readMemory16(self.fv_base + 0x30)\r
271 else:\r
272 # Goto the next FFS file\r
273 ffs_base = int(ffs.get_base() + ffs.get_size())\r
3402aac7 274\r
1e57a462 275 # Align to next 8 byte boundary\r
276 if (ffs_base & 0x7) != 0:\r
277 ffs_base = ffs_base + 0x8 - (ffs_base & 0x7)\r
3402aac7 278\r
1e57a462 279 if ffs_base < self.fv_base + self.get_size():\r
280 return FirmwareFile(self, ffs_base, self.ec)\r
281 else:\r
282 return None\r
3402aac7
RC
283\r
284 def get_debug_info(self):\r
1e57a462 285 self.DebugInfos = []\r
3402aac7 286\r
1e57a462 287 ffs = self.get_next_ffs()\r
3402aac7 288 while ffs != None:\r
1e57a462 289 section = ffs.get_next_section()\r
290 while section != None:\r
291 type = section.get_type()\r
292 if (type == EfiFileSection.EFI_SECTION_TE) or (type == EfiFileSection.EFI_SECTION_PE32):\r
293 self.DebugInfos.append((section.get_base(), section.get_size(), section.get_type()))\r
294 section = ffs.get_next_section(section)\r
295 ffs = self.get_next_ffs(ffs)\r
296\r
72efe027 297 def load_symbols_at(self, addr, verbose = False):\r
1e57a462 298 if self.DebugInfos == []:\r
299 self.get_debug_info()\r
3402aac7 300\r
1e57a462 301 for debug_info in self.DebugInfos:\r
302 if (addr >= debug_info[0]) and (addr < debug_info[0] + debug_info[1]):\r
303 if debug_info[2] == EfiFileSection.EFI_SECTION_TE:\r
304 section = EfiSectionTE(self.ec, debug_info[0] + 0x4)\r
305 elif debug_info[2] == EfiFileSection.EFI_SECTION_PE32:\r
306 section = EfiSectionPE32(self.ec, debug_info[0] + 0x4)\r
307 else:\r
308 raise Exception('FirmwareVolume','Section Type not supported')\r
3402aac7 309\r
72efe027 310 try:\r
311 edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase(), verbose)\r
312 except Exception, (ErrorClass, ErrorMessage):\r
313 if verbose:\r
314 print "Error while loading a symbol file (%s: %s)" % (ErrorClass, ErrorMessage)\r
1e57a462 315\r
316 return debug_info\r
317\r
72efe027 318 def load_all_symbols(self, verbose = False):\r
1e57a462 319 if self.DebugInfos == []:\r
320 self.get_debug_info()\r
3402aac7 321\r
1e57a462 322 for debug_info in self.DebugInfos:\r
323 if debug_info[2] == EfiFileSection.EFI_SECTION_TE:\r
324 section = EfiSectionTE(self.ec, debug_info[0] + 0x4)\r
325 elif debug_info[2] == EfiFileSection.EFI_SECTION_PE32:\r
326 section = EfiSectionPE32(self.ec, debug_info[0] + 0x4)\r
327 else:\r
328 continue\r
3402aac7 329\r
72efe027 330 try:\r
331 edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase(), verbose)\r
332 except Exception, (ErrorClass, ErrorMessage):\r
333 if verbose:\r
334 print "Error while loading a symbol file (%s: %s)" % (ErrorClass, ErrorMessage)\r
72efe027 335\r