2 # Copyright (c) 2011-2012, ARM Limited. All rights reserved.
4 # This program and the accompanying materials
5 # are licensed and made available under the terms and conditions of the BSD License
6 # which accompanies this distribution. The full text of the license may be found at
7 # http://opensource.org/licenses/bsd-license.php
9 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 from arm_ds
.debugger_v1
import DebugException
18 class EfiFileSection(object):
19 EFI_SECTION_PE32
= 0x10
20 EFI_SECTION_PIC
= 0x11
23 EFI_IMAGE_DEBUG_TYPE_CODEVIEW
= 0x2
25 SIZEOF_EFI_FFS_FILE_HEADER
= 0x28
27 def __init__(self
, ec
, base
):
32 return "FileSection(type:0x%X, size:0x%x)" % (self
.get_type(), self
.get_size())
38 return struct
.unpack("B", self
.ec
.getMemoryService().read(self
.base
+ 0x3, 1, 8))[0]
41 return (struct
.unpack("<I", self
.ec
.getMemoryService().read(self
.base
, 4, 32))[0] & 0x00ffffff)
43 def get_debug_filepath(self
):
44 type = self
.get_type()
45 if type == EfiFileSection
.EFI_SECTION_TE
:
46 section
= EfiSectionTE(self
, ec
, self
.base
+ 0x4)
47 elif type == EfiFileSection
.EFI_SECTION_PE32
:
48 section
= EfiSectionPE32(self
, ec
, self
.base
+ 0x4)
50 raise Exception("EfiFileSection", "No debug section")
51 return section
.get_debug_filepath()
54 SIZEOF_EFI_TE_IMAGE_HEADER
= 0x28
55 EFI_TE_IMAGE_SIGNATURE
= ('V','Z')
57 def __init__(self
, ec
, base_te
):
59 self
.base_te
= int(base_te
)
60 te_sig
= struct
.unpack("cc", self
.ec
.getMemoryService().read(self
.base_te
, 2, 32))
61 if te_sig
!= EfiSectionTE
.EFI_TE_IMAGE_SIGNATURE
:
62 raise Exception("EfiFileSectionTE","TE Signature incorrect")
64 def get_debug_filepath(self
):
65 stripped_size
= struct
.unpack("<H", self
.ec
.getMemoryService().read(self
.base_te
+ 0x6, 2, 32))[0]
66 stripped_size
-= EfiSectionTE
.SIZEOF_EFI_TE_IMAGE_HEADER
68 debug_dir_entry_rva
= self
.ec
.getMemoryService().readMemory32(self
.base_te
+ 0x20)
69 if debug_dir_entry_rva
== 0:
70 raise Exception("EfiFileSectionTE","No debug directory for image")
71 debug_dir_entry_rva
-= stripped_size
73 debug_type
= self
.ec
.getMemoryService().readMemory32(self
.base_te
+ debug_dir_entry_rva
+ 0xC)
74 if (debug_type
!= 0xdf) and (debug_type
!= EfiFileSection
.EFI_IMAGE_DEBUG_TYPE_CODEVIEW
):
75 raise Exception("EfiFileSectionTE","Debug type is not dwarf")
77 debug_rva
= self
.ec
.getMemoryService().readMemory32(self
.base_te
+ debug_dir_entry_rva
+ 0x14)
78 debug_rva
-= stripped_size
80 dwarf_sig
= struct
.unpack("cccc", self
.ec
.getMemoryService().read(self
.base_te
+ debug_rva
, 4, 32))
81 if (dwarf_sig
!= 0x66727764) and (dwarf_sig
!= FirmwareFile
.CONST_NB10_SIGNATURE
):
82 raise Exception("EfiFileSectionTE","Dwarf debug signature not found")
84 if dwarf_sig
== 0x66727764:
85 filename
= self
.base_te
+ debug_rva
+ 0xc
87 filename
= self
.base_te
+ debug_rva
+ 0x10
88 filename
= struct
.unpack("200s", self
.ec
.getMemoryService().read(filename
, 200, 32))[0]
89 return filename
[0:string
.find(filename
,'\0')]
91 def get_debug_elfbase(self
):
92 stripped_size
= struct
.unpack("<H", self
.ec
.getMemoryService().read(self
.base_te
+ 0x6, 2, 32))[0]
93 stripped_size
-= EfiSectionTE
.SIZEOF_EFI_TE_IMAGE_HEADER
95 base_of_code
= self
.ec
.getMemoryService().readMemory32(self
.base_te
+ 0xC)
97 return self
.base_te
+ base_of_code
- stripped_size
100 def __init__(self
, ec
, base_pe32
):
102 self
.base_pe32
= base_pe32
104 def get_debug_filepath(self
):
105 # Offset from dos hdr to PE file hdr
106 file_header_offset
= self
.ec
.getMemoryService().readMemory32(self
.base_pe32
+ 0x3C)
108 # Offset to debug dir in PE hdrs
109 debug_dir_entry_rva
= self
.ec
.getMemoryService().readMemory32(self
.base_pe32
+ file_header_offset
+ 0xA8)
110 if debug_dir_entry_rva
== 0:
111 raise Exception("EfiFileSectionPE32","No Debug Directory")
113 debug_type
= self
.ec
.getMemoryService().readMemory32(self
.base_pe32
+ debug_dir_entry_rva
+ 0xC)
114 if (debug_type
!= 0xdf) and (debug_type
!= EfiFileSection
.EFI_IMAGE_DEBUG_TYPE_CODEVIEW
):
115 raise Exception("EfiFileSectionPE32","Debug type is not dwarf")
118 debug_rva
= self
.ec
.getMemoryService().readMemory32(self
.base_pe32
+ debug_dir_entry_rva
+ 0x14)
120 dwarf_sig
= struct
.unpack("cccc", self
.ec
.getMemoryService().read(str(self
.base_pe32
+ debug_rva
), 4, 32))
121 if (dwarf_sig
!= 0x66727764) and (dwarf_sig
!= FirmwareFile
.CONST_NB10_SIGNATURE
):
122 raise Exception("EfiFileSectionPE32","Dwarf debug signature not found")
124 if dwarf_sig
== 0x66727764:
125 filename
= self
.base_pe32
+ debug_rva
+ 0xc
127 filename
= self
.base_pe32
+ debug_rva
+ 0x10
128 filename
= struct
.unpack("200s", self
.ec
.getMemoryService().read(str(filename
), 200, 32))[0]
129 return filename
[0:string
.find(filename
,'\0')]
131 def get_debug_elfbase(self
):
132 # Offset from dos hdr to PE file hdr
133 pe_file_header
= self
.base_pe32
+ self
.ec
.getMemoryService().readMemory32(self
.base_pe32
+ 0x3C)
135 base_of_code
= self
.base_pe32
+ self
.ec
.getMemoryService().readMemory32(pe_file_header
+ 0x28)
136 base_of_data
= self
.base_pe32
+ self
.ec
.getMemoryService().readMemory32(pe_file_header
+ 0x2C)
138 if (base_of_code
< base_of_data
) and (base_of_code
!= 0):
144 EFI_FV_FILETYPE_RAW
= 0x01
145 EFI_FV_FILETYPE_FREEFORM
= 0x02
146 EFI_FV_FILETYPE_SECURITY_CORE
= 0x03
147 EFI_FV_FILETYPE_PEI_CORE
= 0x04
148 EFI_FV_FILETYPE_DXE_CORE
= 0x05
149 EFI_FV_FILETYPE_PEIM
= 0x06
150 EFI_FV_FILETYPE_DRIVER
= 0x07
151 EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
= 0x08
152 EFI_FV_FILETYPE_APPLICATION
= 0x09
153 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
= 0x0B
154 EFI_FV_FILETYPE_FFS_MIN
= 0xF0
156 CONST_NB10_SIGNATURE
= ('N','B','1','0')
158 def __init__(self
, fv
, base
, ec
):
164 return "FFS(state:0x%x, type:0x%X, size:0x%x)" % (self
.get_state(), self
.get_type(), self
.get_size())
170 size
= (self
.ec
.getMemoryService().readMemory32(self
.base
+ 0x14) & 0x00ffffff)
172 # Occupied size is the size considering the alignment
173 return size
+ ((0x8 - (size
& 0x7)) & 0x7)
176 return self
.ec
.getMemoryService().readMemory8(self
.base
+ 0x12)
179 state
= self
.ec
.getMemoryService().readMemory8(self
.base
+ 0x17)
181 polarity
= self
.fv
.get_polarity()
186 while (highest_bit
!= 0) and ((highest_bit
& state
) == 0):
191 def get_next_section(self
, section
=None):
193 if self
.get_type() != FirmwareFile
.EFI_FV_FILETYPE_FFS_MIN
:
194 section_base
= self
.get_base() + 0x18;
198 section_base
= int(section
.get_base() + section
.get_size())
200 # Align to next 4 byte boundary
201 if (section_base
& 0x3) != 0:
202 section_base
= section_base
+ 0x4 - (section_base
& 0x3)
204 if section_base
< self
.get_base() + self
.get_size():
205 return EfiFileSection(self
.ec
, section_base
)
209 class FirmwareVolume
:
210 CONST_FV_SIGNATURE
= ('_','F','V','H')
211 EFI_FVB2_ERASE_POLARITY
= 0x800
215 def __init__(self
, ec
, fv_base
, fv_size
):
217 self
.fv_base
= fv_base
218 self
.fv_size
= fv_size
221 signature
= struct
.unpack("cccc", self
.ec
.getMemoryService().read(fv_base
+ 0x28, 4, 32))
222 except DebugException
:
223 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
)))
224 if signature
!= FirmwareVolume
.CONST_FV_SIGNATURE
:
225 raise Exception("FirmwareVolume", "This is not a valid firmware volume")
228 return self
.ec
.getMemoryService().readMemory32(self
.fv_base
+ 0x20)
230 def get_attributes(self
):
231 return self
.ec
.getMemoryService().readMemory32(self
.fv_base
+ 0x2C)
233 def get_polarity(self
):
234 attributes
= self
.get_attributes()
235 if attributes
& FirmwareVolume
.EFI_FVB2_ERASE_POLARITY
:
240 def get_next_ffs(self
, ffs
=None):
242 # Get the offset of the first FFS file from the FV header
243 ffs_base
= self
.fv_base
+ self
.ec
.getMemoryService().readMemory16(self
.fv_base
+ 0x30)
245 # Goto the next FFS file
246 ffs_base
= int(ffs
.get_base() + ffs
.get_size())
248 # Align to next 8 byte boundary
249 if (ffs_base
& 0x7) != 0:
250 ffs_base
= ffs_base
+ 0x8 - (ffs_base
& 0x7)
252 if ffs_base
< self
.fv_base
+ self
.get_size():
253 return FirmwareFile(self
, ffs_base
, self
.ec
)
257 def get_debug_info(self
):
260 ffs
= self
.get_next_ffs()
262 section
= ffs
.get_next_section()
263 while section
!= None:
264 type = section
.get_type()
265 if (type == EfiFileSection
.EFI_SECTION_TE
) or (type == EfiFileSection
.EFI_SECTION_PE32
):
266 self
.DebugInfos
.append((section
.get_base(), section
.get_size(), section
.get_type()))
267 section
= ffs
.get_next_section(section
)
268 ffs
= self
.get_next_ffs(ffs
)
270 def load_symbols_at(self
, addr
):
271 if self
.DebugInfos
== []:
272 self
.get_debug_info()
274 for debug_info
in self
.DebugInfos
:
275 if (addr
>= debug_info
[0]) and (addr
< debug_info
[0] + debug_info
[1]):
276 if debug_info
[2] == EfiFileSection
.EFI_SECTION_TE
:
277 section
= EfiSectionTE(self
.ec
, debug_info
[0] + 0x4)
278 elif debug_info
[2] == EfiFileSection
.EFI_SECTION_PE32
:
279 section
= EfiSectionPE32(self
.ec
, debug_info
[0] + 0x4)
281 raise Exception('FirmwareVolume','Section Type not supported')
283 edk2_debugger
.load_symbol_from_file(self
.ec
, section
.get_debug_filepath(), section
.get_debug_elfbase())
287 def load_all_symbols(self
):
288 if self
.DebugInfos
== []:
289 self
.get_debug_info()
291 for debug_info
in self
.DebugInfos
:
292 if debug_info
[2] == EfiFileSection
.EFI_SECTION_TE
:
293 section
= EfiSectionTE(self
.ec
, debug_info
[0] + 0x4)
294 elif debug_info
[2] == EfiFileSection
.EFI_SECTION_PE32
:
295 section
= EfiSectionPE32(self
.ec
, debug_info
[0] + 0x4)
299 edk2_debugger
.load_symbol_from_file(self
.ec
, section
.get_debug_filepath(), section
.get_debug_elfbase())