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