]>
Commit | Line | Data |
---|---|---|
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 | |
13 | from arm_ds.debugger_v1 import DebugException\r | |
14 | \r | |
15 | import struct\r | |
16 | import string\r | |
17 | \r | |
18 | import edk2_debugger\r | |
3402aac7 | 19 | \r |
1e57a462 | 20 | class 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 | |
55 | class 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 |
99 | class 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 |
134 | class 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 | |
a8c39ba2 | 141 | file_header_offset = self.ec.getMemoryService().readMemory32(self.base_pe64 + 0x3C)\r |
e3d495e1 HL |
142 | \r |
143 | # Offset to debug dir in PE hdrs\r | |
a8c39ba2 | 144 | debug_dir_entry_rva = self.ec.getMemoryService().readMemory32(self.base_pe64 + file_header_offset + 0xB8)\r |
e3d495e1 HL |
145 | if debug_dir_entry_rva == 0:\r |
146 | raise Exception("EfiFileSectionPE64","No Debug Directory")\r | |
147 | \r | |
148 | debug_type = self.ec.getMemoryService().readMemory32(self.base_pe64 + debug_dir_entry_rva + 0xC)\r | |
149 | if (debug_type != 0xdf) and (debug_type != EfiFileSection.EFI_IMAGE_DEBUG_TYPE_CODEVIEW):\r | |
150 | raise Exception("EfiFileSectionPE64","Debug type is not dwarf")\r | |
3402aac7 RC |
151 | \r |
152 | \r | |
e3d495e1 | 153 | debug_rva = self.ec.getMemoryService().readMemory32(self.base_pe64 + debug_dir_entry_rva + 0x14)\r |
3402aac7 | 154 | \r |
e3d495e1 HL |
155 | dwarf_sig = struct.unpack("cccc", self.ec.getMemoryService().read(str(self.base_pe64 + debug_rva), 4, 32))\r |
156 | if (dwarf_sig != 0x66727764) and (dwarf_sig != FirmwareFile.CONST_NB10_SIGNATURE):\r | |
157 | raise Exception("EfiFileSectionPE64","Dwarf debug signature not found")\r | |
3402aac7 | 158 | \r |
e3d495e1 HL |
159 | if dwarf_sig == 0x66727764:\r |
160 | filename = self.base_pe64 + debug_rva + 0xc\r | |
161 | else:\r | |
162 | filename = self.base_pe64 + debug_rva + 0x10\r | |
163 | filename = struct.unpack("200s", self.ec.getMemoryService().read(str(filename), 200, 32))[0]\r | |
164 | return filename[0:string.find(filename,'\0')]\r | |
3402aac7 | 165 | \r |
e3d495e1 | 166 | def get_debug_elfbase(self):\r |
0ce8410e | 167 | return self.base_pe64\r |
3402aac7 | 168 | \r |
1e57a462 | 169 | class FirmwareFile:\r |
170 | EFI_FV_FILETYPE_RAW = 0x01\r | |
171 | EFI_FV_FILETYPE_FREEFORM = 0x02\r | |
172 | EFI_FV_FILETYPE_SECURITY_CORE = 0x03\r | |
173 | EFI_FV_FILETYPE_PEI_CORE = 0x04\r | |
174 | EFI_FV_FILETYPE_DXE_CORE = 0x05\r | |
175 | EFI_FV_FILETYPE_PEIM = 0x06\r | |
176 | EFI_FV_FILETYPE_DRIVER = 0x07\r | |
177 | EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER = 0x08\r | |
178 | EFI_FV_FILETYPE_APPLICATION = 0x09\r | |
179 | EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE = 0x0B\r | |
180 | EFI_FV_FILETYPE_FFS_MIN = 0xF0\r | |
3402aac7 | 181 | \r |
1e57a462 | 182 | CONST_NB10_SIGNATURE = ('N','B','1','0')\r |
3402aac7 | 183 | \r |
1e57a462 | 184 | def __init__(self, fv, base, ec):\r |
185 | self.fv = fv\r | |
186 | self.base = base\r | |
187 | self.ec = ec\r | |
3402aac7 | 188 | \r |
1e57a462 | 189 | def __str__(self):\r |
190 | return "FFS(state:0x%x, type:0x%X, size:0x%x)" % (self.get_state(), self.get_type(), self.get_size())\r | |
3402aac7 | 191 | \r |
1e57a462 | 192 | def get_base(self):\r |
193 | return self.base\r | |
3402aac7 | 194 | \r |
1e57a462 | 195 | def get_size(self):\r |
196 | size = (self.ec.getMemoryService().readMemory32(self.base + 0x14) & 0x00ffffff)\r | |
197 | \r | |
198 | # Occupied size is the size considering the alignment\r | |
199 | return size + ((0x8 - (size & 0x7)) & 0x7)\r | |
3402aac7 | 200 | \r |
1e57a462 | 201 | def get_type(self):\r |
202 | return self.ec.getMemoryService().readMemory8(self.base + 0x12)\r | |
3402aac7 | 203 | \r |
1e57a462 | 204 | def get_state(self):\r |
205 | state = self.ec.getMemoryService().readMemory8(self.base + 0x17)\r | |
3402aac7 | 206 | \r |
1e57a462 | 207 | polarity = self.fv.get_polarity()\r |
208 | if polarity:\r | |
209 | state = ~state\r | |
3402aac7 | 210 | \r |
1e57a462 | 211 | highest_bit = 0x80;\r |
212 | while (highest_bit != 0) and ((highest_bit & state) == 0):\r | |
213 | highest_bit >>= 1\r | |
3402aac7 | 214 | \r |
1e57a462 | 215 | return highest_bit\r |
3402aac7 | 216 | \r |
1e57a462 | 217 | def get_next_section(self, section=None):\r |
218 | if section == None:\r | |
219 | if self.get_type() != FirmwareFile.EFI_FV_FILETYPE_FFS_MIN:\r | |
220 | section_base = self.get_base() + 0x18;\r | |
221 | else:\r | |
222 | return None\r | |
223 | else:\r | |
224 | section_base = int(section.get_base() + section.get_size())\r | |
3402aac7 | 225 | \r |
1e57a462 | 226 | # Align to next 4 byte boundary\r |
227 | if (section_base & 0x3) != 0:\r | |
228 | section_base = section_base + 0x4 - (section_base & 0x3)\r | |
229 | \r | |
230 | if section_base < self.get_base() + self.get_size():\r | |
231 | return EfiFileSection(self.ec, section_base)\r | |
232 | else:\r | |
233 | return None\r | |
3402aac7 | 234 | \r |
1e57a462 | 235 | class FirmwareVolume:\r |
236 | CONST_FV_SIGNATURE = ('_','F','V','H')\r | |
237 | EFI_FVB2_ERASE_POLARITY = 0x800\r | |
3402aac7 | 238 | \r |
1e57a462 | 239 | DebugInfos = []\r |
3402aac7 | 240 | \r |
1e57a462 | 241 | def __init__(self, ec, fv_base, fv_size):\r |
242 | self.ec = ec\r | |
243 | self.fv_base = fv_base\r | |
244 | self.fv_size = fv_size\r | |
3402aac7 | 245 | \r |
1e57a462 | 246 | try:\r |
247 | signature = struct.unpack("cccc", self.ec.getMemoryService().read(fv_base + 0x28, 4, 32))\r | |
248 | except DebugException:\r | |
249 | 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 | |
250 | if signature != FirmwareVolume.CONST_FV_SIGNATURE:\r | |
251 | raise Exception("FirmwareVolume", "This is not a valid firmware volume")\r | |
3402aac7 | 252 | \r |
1e57a462 | 253 | def get_size(self):\r |
254 | return self.ec.getMemoryService().readMemory32(self.fv_base + 0x20)\r | |
3402aac7 | 255 | \r |
1e57a462 | 256 | def get_attributes(self):\r |
257 | return self.ec.getMemoryService().readMemory32(self.fv_base + 0x2C)\r | |
3402aac7 | 258 | \r |
1e57a462 | 259 | def get_polarity(self):\r |
260 | attributes = self.get_attributes()\r | |
261 | if attributes & FirmwareVolume.EFI_FVB2_ERASE_POLARITY:\r | |
262 | return 1\r | |
263 | else:\r | |
264 | return 0\r | |
3402aac7 | 265 | \r |
1e57a462 | 266 | def get_next_ffs(self, ffs=None):\r |
267 | if ffs == None:\r | |
268 | # Get the offset of the first FFS file from the FV header\r | |
269 | ffs_base = self.fv_base + self.ec.getMemoryService().readMemory16(self.fv_base + 0x30)\r | |
270 | else:\r | |
271 | # Goto the next FFS file\r | |
272 | ffs_base = int(ffs.get_base() + ffs.get_size())\r | |
3402aac7 | 273 | \r |
1e57a462 | 274 | # Align to next 8 byte boundary\r |
275 | if (ffs_base & 0x7) != 0:\r | |
276 | ffs_base = ffs_base + 0x8 - (ffs_base & 0x7)\r | |
3402aac7 | 277 | \r |
1e57a462 | 278 | if ffs_base < self.fv_base + self.get_size():\r |
279 | return FirmwareFile(self, ffs_base, self.ec)\r | |
280 | else:\r | |
281 | return None\r | |
3402aac7 RC |
282 | \r |
283 | def get_debug_info(self):\r | |
1e57a462 | 284 | self.DebugInfos = []\r |
3402aac7 | 285 | \r |
1e57a462 | 286 | ffs = self.get_next_ffs()\r |
3402aac7 | 287 | while ffs != None:\r |
1e57a462 | 288 | section = ffs.get_next_section()\r |
289 | while section != None:\r | |
290 | type = section.get_type()\r | |
291 | if (type == EfiFileSection.EFI_SECTION_TE) or (type == EfiFileSection.EFI_SECTION_PE32):\r | |
292 | self.DebugInfos.append((section.get_base(), section.get_size(), section.get_type()))\r | |
293 | section = ffs.get_next_section(section)\r | |
294 | ffs = self.get_next_ffs(ffs)\r | |
295 | \r | |
72efe027 | 296 | def load_symbols_at(self, addr, verbose = False):\r |
1e57a462 | 297 | if self.DebugInfos == []:\r |
298 | self.get_debug_info()\r | |
3402aac7 | 299 | \r |
1e57a462 | 300 | for debug_info in self.DebugInfos:\r |
301 | if (addr >= debug_info[0]) and (addr < debug_info[0] + debug_info[1]):\r | |
302 | if debug_info[2] == EfiFileSection.EFI_SECTION_TE:\r | |
303 | section = EfiSectionTE(self.ec, debug_info[0] + 0x4)\r | |
304 | elif debug_info[2] == EfiFileSection.EFI_SECTION_PE32:\r | |
305 | section = EfiSectionPE32(self.ec, debug_info[0] + 0x4)\r | |
306 | else:\r | |
307 | raise Exception('FirmwareVolume','Section Type not supported')\r | |
3402aac7 | 308 | \r |
72efe027 | 309 | try:\r |
310 | edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase(), verbose)\r | |
311 | except Exception, (ErrorClass, ErrorMessage):\r | |
312 | if verbose:\r | |
313 | print "Error while loading a symbol file (%s: %s)" % (ErrorClass, ErrorMessage)\r | |
1e57a462 | 314 | \r |
315 | return debug_info\r | |
316 | \r | |
72efe027 | 317 | def load_all_symbols(self, 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 debug_info[2] == EfiFileSection.EFI_SECTION_TE:\r | |
323 | section = EfiSectionTE(self.ec, debug_info[0] + 0x4)\r | |
324 | elif debug_info[2] == EfiFileSection.EFI_SECTION_PE32:\r | |
325 | section = EfiSectionPE32(self.ec, debug_info[0] + 0x4)\r | |
326 | else:\r | |
327 | continue\r | |
3402aac7 | 328 | \r |
72efe027 | 329 | try:\r |
330 | edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase(), verbose)\r | |
331 | except Exception, (ErrorClass, ErrorMessage):\r | |
332 | if verbose:\r | |
333 | print "Error while loading a symbol file (%s: %s)" % (ErrorClass, ErrorMessage)\r | |
72efe027 | 334 | \r |