]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPlatformPkg/Scripts/Ds5/firmware_volume.py
ARM Packages: Removed trailing spaces
[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
1e57a462 97 base_of_code = self.ec.getMemoryService().readMemory32(self.base_te + 0xC)\r
3402aac7 98\r
1e57a462 99 return self.base_te + base_of_code - stripped_size\r
100\r
101class EfiSectionPE32:\r
102 def __init__(self, ec, base_pe32):\r
103 self.ec = ec\r
104 self.base_pe32 = base_pe32\r
105\r
106 def get_debug_filepath(self):\r
107 # Offset from dos hdr to PE file hdr\r
108 file_header_offset = self.ec.getMemoryService().readMemory32(self.base_pe32 + 0x3C)\r
109\r
110 # Offset to debug dir in PE hdrs\r
111 debug_dir_entry_rva = self.ec.getMemoryService().readMemory32(self.base_pe32 + file_header_offset + 0xA8)\r
112 if debug_dir_entry_rva == 0:\r
113 raise Exception("EfiFileSectionPE32","No Debug Directory")\r
114\r
115 debug_type = self.ec.getMemoryService().readMemory32(self.base_pe32 + debug_dir_entry_rva + 0xC)\r
116 if (debug_type != 0xdf) and (debug_type != EfiFileSection.EFI_IMAGE_DEBUG_TYPE_CODEVIEW):\r
117 raise Exception("EfiFileSectionPE32","Debug type is not dwarf")\r
3402aac7
RC
118\r
119\r
1e57a462 120 debug_rva = self.ec.getMemoryService().readMemory32(self.base_pe32 + debug_dir_entry_rva + 0x14)\r
3402aac7 121\r
1e57a462 122 dwarf_sig = struct.unpack("cccc", self.ec.getMemoryService().read(str(self.base_pe32 + debug_rva), 4, 32))\r
123 if (dwarf_sig != 0x66727764) and (dwarf_sig != FirmwareFile.CONST_NB10_SIGNATURE):\r
124 raise Exception("EfiFileSectionPE32","Dwarf debug signature not found")\r
3402aac7 125\r
1e57a462 126 if dwarf_sig == 0x66727764:\r
127 filename = self.base_pe32 + debug_rva + 0xc\r
128 else:\r
129 filename = self.base_pe32 + debug_rva + 0x10\r
130 filename = struct.unpack("200s", self.ec.getMemoryService().read(str(filename), 200, 32))[0]\r
131 return filename[0:string.find(filename,'\0')]\r
3402aac7 132\r
1e57a462 133 def get_debug_elfbase(self):\r
134 # Offset from dos hdr to PE file hdr\r
135 pe_file_header = self.base_pe32 + self.ec.getMemoryService().readMemory32(self.base_pe32 + 0x3C)\r
3402aac7 136\r
1e57a462 137 base_of_code = self.base_pe32 + self.ec.getMemoryService().readMemory32(pe_file_header + 0x28)\r
138 base_of_data = self.base_pe32 + self.ec.getMemoryService().readMemory32(pe_file_header + 0x2C)\r
3402aac7 139\r
1e57a462 140 if (base_of_code < base_of_data) and (base_of_code != 0):\r
141 return base_of_code\r
142 else:\r
e3d495e1
HL
143 return base_of_data\r
144\r
145class EfiSectionPE64:\r
146 def __init__(self, ec, base_pe64):\r
147 self.ec = ec\r
148 self.base_pe64 = base_pe64\r
149\r
150 def get_debug_filepath(self):\r
151 # Offset from dos hdr to PE file hdr (EFI_IMAGE_NT_HEADERS64)\r
152 #file_header_offset = self.ec.getMemoryService().readMemory32(self.base_pe64 + 0x3C)\r
153 file_header_offset = 0x0\r
154\r
155 # Offset to debug dir in PE hdrs\r
156 debug_dir_entry_rva = self.ec.getMemoryService().readMemory32(self.base_pe64 + file_header_offset + 0x138)\r
157 if debug_dir_entry_rva == 0:\r
158 raise Exception("EfiFileSectionPE64","No Debug Directory")\r
159\r
160 debug_type = self.ec.getMemoryService().readMemory32(self.base_pe64 + debug_dir_entry_rva + 0xC)\r
161 if (debug_type != 0xdf) and (debug_type != EfiFileSection.EFI_IMAGE_DEBUG_TYPE_CODEVIEW):\r
162 raise Exception("EfiFileSectionPE64","Debug type is not dwarf")\r
3402aac7
RC
163\r
164\r
e3d495e1 165 debug_rva = self.ec.getMemoryService().readMemory32(self.base_pe64 + debug_dir_entry_rva + 0x14)\r
3402aac7 166\r
e3d495e1
HL
167 dwarf_sig = struct.unpack("cccc", self.ec.getMemoryService().read(str(self.base_pe64 + debug_rva), 4, 32))\r
168 if (dwarf_sig != 0x66727764) and (dwarf_sig != FirmwareFile.CONST_NB10_SIGNATURE):\r
169 raise Exception("EfiFileSectionPE64","Dwarf debug signature not found")\r
3402aac7 170\r
e3d495e1
HL
171 if dwarf_sig == 0x66727764:\r
172 filename = self.base_pe64 + debug_rva + 0xc\r
173 else:\r
174 filename = self.base_pe64 + debug_rva + 0x10\r
175 filename = struct.unpack("200s", self.ec.getMemoryService().read(str(filename), 200, 32))[0]\r
176 return filename[0:string.find(filename,'\0')]\r
3402aac7 177\r
e3d495e1
HL
178 def get_debug_elfbase(self):\r
179 # Offset from dos hdr to PE file hdr\r
180 pe_file_header = self.base_pe64 + self.ec.getMemoryService().readMemory32(self.base_pe64 + 0x3C)\r
3402aac7 181\r
e3d495e1
HL
182 base_of_code = self.base_pe64 + self.ec.getMemoryService().readMemory32(pe_file_header + 0x28)\r
183 base_of_data = self.base_pe64 + self.ec.getMemoryService().readMemory32(pe_file_header + 0x2C)\r
3402aac7 184\r
e3d495e1
HL
185 if (base_of_code < base_of_data) and (base_of_code != 0):\r
186 return base_of_code\r
187 else:\r
188 return base_of_data\r
3402aac7 189\r
1e57a462 190class FirmwareFile:\r
191 EFI_FV_FILETYPE_RAW = 0x01\r
192 EFI_FV_FILETYPE_FREEFORM = 0x02\r
193 EFI_FV_FILETYPE_SECURITY_CORE = 0x03\r
194 EFI_FV_FILETYPE_PEI_CORE = 0x04\r
195 EFI_FV_FILETYPE_DXE_CORE = 0x05\r
196 EFI_FV_FILETYPE_PEIM = 0x06\r
197 EFI_FV_FILETYPE_DRIVER = 0x07\r
198 EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER = 0x08\r
199 EFI_FV_FILETYPE_APPLICATION = 0x09\r
200 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE = 0x0B\r
201 EFI_FV_FILETYPE_FFS_MIN = 0xF0\r
3402aac7 202\r
1e57a462 203 CONST_NB10_SIGNATURE = ('N','B','1','0')\r
3402aac7 204\r
1e57a462 205 def __init__(self, fv, base, ec):\r
206 self.fv = fv\r
207 self.base = base\r
208 self.ec = ec\r
3402aac7 209\r
1e57a462 210 def __str__(self):\r
211 return "FFS(state:0x%x, type:0x%X, size:0x%x)" % (self.get_state(), self.get_type(), self.get_size())\r
3402aac7 212\r
1e57a462 213 def get_base(self):\r
214 return self.base\r
3402aac7 215\r
1e57a462 216 def get_size(self):\r
217 size = (self.ec.getMemoryService().readMemory32(self.base + 0x14) & 0x00ffffff)\r
218\r
219 # Occupied size is the size considering the alignment\r
220 return size + ((0x8 - (size & 0x7)) & 0x7)\r
3402aac7 221\r
1e57a462 222 def get_type(self):\r
223 return self.ec.getMemoryService().readMemory8(self.base + 0x12)\r
3402aac7 224\r
1e57a462 225 def get_state(self):\r
226 state = self.ec.getMemoryService().readMemory8(self.base + 0x17)\r
3402aac7 227\r
1e57a462 228 polarity = self.fv.get_polarity()\r
229 if polarity:\r
230 state = ~state\r
3402aac7 231\r
1e57a462 232 highest_bit = 0x80;\r
233 while (highest_bit != 0) and ((highest_bit & state) == 0):\r
234 highest_bit >>= 1\r
3402aac7 235\r
1e57a462 236 return highest_bit\r
3402aac7 237\r
1e57a462 238 def get_next_section(self, section=None):\r
239 if section == None:\r
240 if self.get_type() != FirmwareFile.EFI_FV_FILETYPE_FFS_MIN:\r
241 section_base = self.get_base() + 0x18;\r
242 else:\r
243 return None\r
244 else:\r
245 section_base = int(section.get_base() + section.get_size())\r
3402aac7 246\r
1e57a462 247 # Align to next 4 byte boundary\r
248 if (section_base & 0x3) != 0:\r
249 section_base = section_base + 0x4 - (section_base & 0x3)\r
250\r
251 if section_base < self.get_base() + self.get_size():\r
252 return EfiFileSection(self.ec, section_base)\r
253 else:\r
254 return None\r
3402aac7 255\r
1e57a462 256class FirmwareVolume:\r
257 CONST_FV_SIGNATURE = ('_','F','V','H')\r
258 EFI_FVB2_ERASE_POLARITY = 0x800\r
3402aac7 259\r
1e57a462 260 DebugInfos = []\r
3402aac7 261\r
1e57a462 262 def __init__(self, ec, fv_base, fv_size):\r
263 self.ec = ec\r
264 self.fv_base = fv_base\r
265 self.fv_size = fv_size\r
3402aac7 266\r
1e57a462 267 try:\r
268 signature = struct.unpack("cccc", self.ec.getMemoryService().read(fv_base + 0x28, 4, 32))\r
269 except DebugException:\r
270 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
271 if signature != FirmwareVolume.CONST_FV_SIGNATURE:\r
272 raise Exception("FirmwareVolume", "This is not a valid firmware volume")\r
3402aac7 273\r
1e57a462 274 def get_size(self):\r
275 return self.ec.getMemoryService().readMemory32(self.fv_base + 0x20)\r
3402aac7 276\r
1e57a462 277 def get_attributes(self):\r
278 return self.ec.getMemoryService().readMemory32(self.fv_base + 0x2C)\r
3402aac7 279\r
1e57a462 280 def get_polarity(self):\r
281 attributes = self.get_attributes()\r
282 if attributes & FirmwareVolume.EFI_FVB2_ERASE_POLARITY:\r
283 return 1\r
284 else:\r
285 return 0\r
3402aac7 286\r
1e57a462 287 def get_next_ffs(self, ffs=None):\r
288 if ffs == None:\r
289 # Get the offset of the first FFS file from the FV header\r
290 ffs_base = self.fv_base + self.ec.getMemoryService().readMemory16(self.fv_base + 0x30)\r
291 else:\r
292 # Goto the next FFS file\r
293 ffs_base = int(ffs.get_base() + ffs.get_size())\r
3402aac7 294\r
1e57a462 295 # Align to next 8 byte boundary\r
296 if (ffs_base & 0x7) != 0:\r
297 ffs_base = ffs_base + 0x8 - (ffs_base & 0x7)\r
3402aac7 298\r
1e57a462 299 if ffs_base < self.fv_base + self.get_size():\r
300 return FirmwareFile(self, ffs_base, self.ec)\r
301 else:\r
302 return None\r
3402aac7
RC
303\r
304 def get_debug_info(self):\r
1e57a462 305 self.DebugInfos = []\r
3402aac7 306\r
1e57a462 307 ffs = self.get_next_ffs()\r
3402aac7 308 while ffs != None:\r
1e57a462 309 section = ffs.get_next_section()\r
310 while section != None:\r
311 type = section.get_type()\r
312 if (type == EfiFileSection.EFI_SECTION_TE) or (type == EfiFileSection.EFI_SECTION_PE32):\r
313 self.DebugInfos.append((section.get_base(), section.get_size(), section.get_type()))\r
314 section = ffs.get_next_section(section)\r
315 ffs = self.get_next_ffs(ffs)\r
316\r
72efe027 317 def load_symbols_at(self, addr, verbose = False):\r
1e57a462 318 if self.DebugInfos == []:\r
319 self.get_debug_info()\r
3402aac7 320\r
1e57a462 321 for debug_info in self.DebugInfos:\r
322 if (addr >= debug_info[0]) and (addr < debug_info[0] + debug_info[1]):\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 raise Exception('FirmwareVolume','Section Type not supported')\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
1e57a462 335\r
336 return debug_info\r
337\r
72efe027 338 def load_all_symbols(self, verbose = False):\r
1e57a462 339 if self.DebugInfos == []:\r
340 self.get_debug_info()\r
3402aac7 341\r
1e57a462 342 for debug_info in self.DebugInfos:\r
343 if debug_info[2] == EfiFileSection.EFI_SECTION_TE:\r
344 section = EfiSectionTE(self.ec, debug_info[0] + 0x4)\r
345 elif debug_info[2] == EfiFileSection.EFI_SECTION_PE32:\r
346 section = EfiSectionPE32(self.ec, debug_info[0] + 0x4)\r
347 else:\r
348 continue\r
3402aac7 349\r
72efe027 350 try:\r
351 edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase(), verbose)\r
352 except Exception, (ErrorClass, ErrorMessage):\r
353 if verbose:\r
354 print "Error while loading a symbol file (%s: %s)" % (ErrorClass, ErrorMessage)\r
72efe027 355\r