2 # Copyright (c) 2011-2013, ARM Limited. All rights reserved.
4 # SPDX-License-Identifier: BSD-2-Clause-Patent
7 from arm_ds
.debugger_v1
import DebugException
14 class EfiFileSection(object):
15 EFI_SECTION_PE32
= 0x10
16 EFI_SECTION_PIC
= 0x11
19 EFI_IMAGE_DEBUG_TYPE_CODEVIEW
= 0x2
21 SIZEOF_EFI_FFS_FILE_HEADER
= 0x28
23 def __init__(self
, ec
, base
):
28 return "FileSection(type:0x%X, size:0x%x)" % (self
.get_type(), self
.get_size())
34 return struct
.unpack("B", self
.ec
.getMemoryService().read(self
.base
+ 0x3, 1, 8))[0]
37 return (struct
.unpack("<I", self
.ec
.getMemoryService().read(self
.base
, 4, 32))[0] & 0x00ffffff)
39 def get_debug_filepath(self
):
40 type = self
.get_type()
41 if type == EfiFileSection
.EFI_SECTION_TE
:
42 section
= EfiSectionTE(self
, ec
, self
.base
+ 0x4)
43 elif type == EfiFileSection
.EFI_SECTION_PE32
:
44 section
= EfiSectionPE32(self
, ec
, self
.base
+ 0x4)
46 raise Exception("EfiFileSection", "No debug section")
47 return section
.get_debug_filepath()
50 SIZEOF_EFI_TE_IMAGE_HEADER
= 0x28
51 EFI_TE_IMAGE_SIGNATURE
= ('V','Z')
53 def __init__(self
, ec
, base_te
):
55 self
.base_te
= int(base_te
)
56 te_sig
= struct
.unpack("cc", self
.ec
.getMemoryService().read(self
.base_te
, 2, 32))
57 if te_sig
!= EfiSectionTE
.EFI_TE_IMAGE_SIGNATURE
:
58 raise Exception("EfiFileSectionTE","TE Signature incorrect")
60 def get_debug_filepath(self
):
61 stripped_size
= struct
.unpack("<H", self
.ec
.getMemoryService().read(self
.base_te
+ 0x6, 2, 32))[0]
62 stripped_size
-= EfiSectionTE
.SIZEOF_EFI_TE_IMAGE_HEADER
64 debug_dir_entry_rva
= self
.ec
.getMemoryService().readMemory32(self
.base_te
+ 0x20)
65 if debug_dir_entry_rva
== 0:
66 raise Exception("EfiFileSectionTE","No debug directory for image")
67 debug_dir_entry_rva
-= stripped_size
69 debug_type
= self
.ec
.getMemoryService().readMemory32(self
.base_te
+ debug_dir_entry_rva
+ 0xC)
70 if (debug_type
!= 0xdf) and (debug_type
!= EfiFileSection
.EFI_IMAGE_DEBUG_TYPE_CODEVIEW
):
71 raise Exception("EfiFileSectionTE","Debug type is not dwarf")
73 debug_rva
= self
.ec
.getMemoryService().readMemory32(self
.base_te
+ debug_dir_entry_rva
+ 0x14)
74 debug_rva
-= stripped_size
76 dwarf_sig
= struct
.unpack("cccc", self
.ec
.getMemoryService().read(self
.base_te
+ debug_rva
, 4, 32))
77 if (dwarf_sig
!= 0x66727764) and (dwarf_sig
!= FirmwareFile
.CONST_NB10_SIGNATURE
):
78 raise Exception("EfiFileSectionTE","Dwarf debug signature not found")
80 if dwarf_sig
== 0x66727764:
81 filename
= self
.base_te
+ debug_rva
+ 0xc
83 filename
= self
.base_te
+ debug_rva
+ 0x10
84 filename
= struct
.unpack("400s", self
.ec
.getMemoryService().read(filename
, 400, 32))[0]
85 return filename
[0:string
.find(filename
,'\0')]
87 def get_debug_elfbase(self
):
88 stripped_size
= struct
.unpack("<H", self
.ec
.getMemoryService().read(self
.base_te
+ 0x6, 2, 32))[0]
89 stripped_size
-= EfiSectionTE
.SIZEOF_EFI_TE_IMAGE_HEADER
91 return self
.base_te
- stripped_size
94 def __init__(self
, ec
, base_pe32
):
96 self
.base_pe32
= base_pe32
98 def get_debug_filepath(self
):
99 # Offset from dos hdr to PE file hdr
100 file_header_offset
= self
.ec
.getMemoryService().readMemory32(self
.base_pe32
+ 0x3C)
102 # Offset to debug dir in PE hdrs
103 debug_dir_entry_rva
= self
.ec
.getMemoryService().readMemory32(self
.base_pe32
+ file_header_offset
+ 0xA8)
104 if debug_dir_entry_rva
== 0:
105 raise Exception("EfiFileSectionPE32","No Debug Directory")
107 debug_type
= self
.ec
.getMemoryService().readMemory32(self
.base_pe32
+ debug_dir_entry_rva
+ 0xC)
108 if (debug_type
!= 0xdf) and (debug_type
!= EfiFileSection
.EFI_IMAGE_DEBUG_TYPE_CODEVIEW
):
109 raise Exception("EfiFileSectionPE32","Debug type is not dwarf")
112 debug_rva
= self
.ec
.getMemoryService().readMemory32(self
.base_pe32
+ debug_dir_entry_rva
+ 0x14)
114 dwarf_sig
= struct
.unpack("cccc", self
.ec
.getMemoryService().read(str(self
.base_pe32
+ debug_rva
), 4, 32))
115 if (dwarf_sig
!= 0x66727764) and (dwarf_sig
!= FirmwareFile
.CONST_NB10_SIGNATURE
):
116 raise Exception("EfiFileSectionPE32","Dwarf debug signature not found")
118 if dwarf_sig
== 0x66727764:
119 filename
= self
.base_pe32
+ debug_rva
+ 0xc
121 filename
= self
.base_pe32
+ debug_rva
+ 0x10
122 filename
= struct
.unpack("400s", self
.ec
.getMemoryService().read(str(filename
), 400, 32))[0]
123 return filename
[0:string
.find(filename
,'\0')]
125 def get_debug_elfbase(self
):
126 return self
.base_pe32
128 class EfiSectionPE64
:
129 def __init__(self
, ec
, base_pe64
):
131 self
.base_pe64
= base_pe64
133 def get_debug_filepath(self
):
134 # Offset from dos hdr to PE file hdr (EFI_IMAGE_NT_HEADERS64)
135 file_header_offset
= self
.ec
.getMemoryService().readMemory32(self
.base_pe64
+ 0x3C)
137 # Offset to debug dir in PE hdrs
138 debug_dir_entry_rva
= self
.ec
.getMemoryService().readMemory32(self
.base_pe64
+ file_header_offset
+ 0xB8)
139 if debug_dir_entry_rva
== 0:
140 raise Exception("EfiFileSectionPE64","No Debug Directory")
142 debug_type
= self
.ec
.getMemoryService().readMemory32(self
.base_pe64
+ debug_dir_entry_rva
+ 0xC)
143 if (debug_type
!= 0xdf) and (debug_type
!= EfiFileSection
.EFI_IMAGE_DEBUG_TYPE_CODEVIEW
):
144 raise Exception("EfiFileSectionPE64","Debug type is not dwarf")
147 debug_rva
= self
.ec
.getMemoryService().readMemory32(self
.base_pe64
+ debug_dir_entry_rva
+ 0x14)
149 dwarf_sig
= struct
.unpack("cccc", self
.ec
.getMemoryService().read(str(self
.base_pe64
+ debug_rva
), 4, 32))
150 if (dwarf_sig
!= 0x66727764) and (dwarf_sig
!= FirmwareFile
.CONST_NB10_SIGNATURE
):
151 raise Exception("EfiFileSectionPE64","Dwarf debug signature not found")
153 if dwarf_sig
== 0x66727764:
154 filename
= self
.base_pe64
+ debug_rva
+ 0xc
156 filename
= self
.base_pe64
+ debug_rva
+ 0x10
157 filename
= struct
.unpack("400s", self
.ec
.getMemoryService().read(str(filename
), 400, 32))[0]
158 return filename
[0:string
.find(filename
,'\0')]
160 def get_debug_elfbase(self
):
161 return self
.base_pe64
164 EFI_FV_FILETYPE_RAW
= 0x01
165 EFI_FV_FILETYPE_FREEFORM
= 0x02
166 EFI_FV_FILETYPE_SECURITY_CORE
= 0x03
167 EFI_FV_FILETYPE_PEI_CORE
= 0x04
168 EFI_FV_FILETYPE_DXE_CORE
= 0x05
169 EFI_FV_FILETYPE_PEIM
= 0x06
170 EFI_FV_FILETYPE_DRIVER
= 0x07
171 EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
= 0x08
172 EFI_FV_FILETYPE_APPLICATION
= 0x09
173 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
= 0x0B
174 EFI_FV_FILETYPE_FFS_MIN
= 0xF0
176 CONST_NB10_SIGNATURE
= ('N','B','1','0')
178 def __init__(self
, fv
, base
, ec
):
184 return "FFS(state:0x%x, type:0x%X, size:0x%x)" % (self
.get_state(), self
.get_type(), self
.get_size())
190 size
= (self
.ec
.getMemoryService().readMemory32(self
.base
+ 0x14) & 0x00ffffff)
192 # Occupied size is the size considering the alignment
193 return size
+ ((0x8 - (size
& 0x7)) & 0x7)
196 return self
.ec
.getMemoryService().readMemory8(self
.base
+ 0x12)
199 state
= self
.ec
.getMemoryService().readMemory8(self
.base
+ 0x17)
201 polarity
= self
.fv
.get_polarity()
206 while (highest_bit
!= 0) and ((highest_bit
& state
) == 0):
211 def get_next_section(self
, section
=None):
213 if self
.get_type() != FirmwareFile
.EFI_FV_FILETYPE_FFS_MIN
:
214 section_base
= self
.get_base() + 0x18;
218 section_base
= int(section
.get_base() + section
.get_size())
220 # Align to next 4 byte boundary
221 if (section_base
& 0x3) != 0:
222 section_base
= section_base
+ 0x4 - (section_base
& 0x3)
224 if section_base
< self
.get_base() + self
.get_size():
225 return EfiFileSection(self
.ec
, section_base
)
229 class FirmwareVolume
:
230 CONST_FV_SIGNATURE
= ('_','F','V','H')
231 EFI_FVB2_ERASE_POLARITY
= 0x800
235 def __init__(self
, ec
, fv_base
, fv_size
):
237 self
.fv_base
= fv_base
238 self
.fv_size
= fv_size
241 signature
= struct
.unpack("cccc", self
.ec
.getMemoryService().read(fv_base
+ 0x28, 4, 32))
242 except DebugException
:
243 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
)))
244 if signature
!= FirmwareVolume
.CONST_FV_SIGNATURE
:
245 raise Exception("FirmwareVolume", "This is not a valid firmware volume")
248 return self
.ec
.getMemoryService().readMemory32(self
.fv_base
+ 0x20)
250 def get_attributes(self
):
251 return self
.ec
.getMemoryService().readMemory32(self
.fv_base
+ 0x2C)
253 def get_polarity(self
):
254 attributes
= self
.get_attributes()
255 if attributes
& FirmwareVolume
.EFI_FVB2_ERASE_POLARITY
:
260 def get_next_ffs(self
, ffs
=None):
262 # Get the offset of the first FFS file from the FV header
263 ffs_base
= self
.fv_base
+ self
.ec
.getMemoryService().readMemory16(self
.fv_base
+ 0x30)
265 # Goto the next FFS file
266 ffs_base
= int(ffs
.get_base() + ffs
.get_size())
268 # Align to next 8 byte boundary
269 if (ffs_base
& 0x7) != 0:
270 ffs_base
= ffs_base
+ 0x8 - (ffs_base
& 0x7)
272 if ffs_base
< self
.fv_base
+ self
.get_size():
273 return FirmwareFile(self
, ffs_base
, self
.ec
)
277 def get_debug_info(self
):
280 ffs
= self
.get_next_ffs()
282 section
= ffs
.get_next_section()
283 while section
!= None:
284 type = section
.get_type()
285 if (type == EfiFileSection
.EFI_SECTION_TE
) or (type == EfiFileSection
.EFI_SECTION_PE32
):
286 self
.DebugInfos
.append((section
.get_base(), section
.get_size(), section
.get_type()))
287 section
= ffs
.get_next_section(section
)
288 ffs
= self
.get_next_ffs(ffs
)
290 def load_symbols_at(self
, addr
, verbose
= False):
291 if self
.DebugInfos
== []:
292 self
.get_debug_info()
294 for debug_info
in self
.DebugInfos
:
295 if (addr
>= debug_info
[0]) and (addr
< debug_info
[0] + debug_info
[1]):
296 if debug_info
[2] == EfiFileSection
.EFI_SECTION_TE
:
297 section
= EfiSectionTE(self
.ec
, debug_info
[0] + 0x4)
298 elif debug_info
[2] == EfiFileSection
.EFI_SECTION_PE32
:
299 section
= EfiSectionPE32(self
.ec
, debug_info
[0] + 0x4)
301 raise Exception('FirmwareVolume','Section Type not supported')
304 edk2_debugger
.load_symbol_from_file(self
.ec
, section
.get_debug_filepath(), section
.get_debug_elfbase(), verbose
)
305 except Exception, (ErrorClass
, ErrorMessage
):
307 print "Error while loading a symbol file (%s: %s)" % (ErrorClass
, ErrorMessage
)
311 def load_all_symbols(self
, verbose
= False):
312 if self
.DebugInfos
== []:
313 self
.get_debug_info()
315 for debug_info
in self
.DebugInfos
:
316 if debug_info
[2] == EfiFileSection
.EFI_SECTION_TE
:
317 section
= EfiSectionTE(self
.ec
, debug_info
[0] + 0x4)
318 elif debug_info
[2] == EfiFileSection
.EFI_SECTION_PE32
:
319 section
= EfiSectionPE32(self
.ec
, debug_info
[0] + 0x4)
324 edk2_debugger
.load_symbol_from_file(self
.ec
, section
.get_debug_filepath(), section
.get_debug_elfbase(), verbose
)
325 except Exception, (ErrorClass
, ErrorMessage
):
327 print "Error while loading a symbol file (%s: %s)" % (ErrorClass
, ErrorMessage
)