]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Scripts/Ds5/firmware_volume.py
ArmPlatformPkg/Scripts/Ds5: Updated DS-5 scripts to be used by DS-5 v5.12
[mirror_edk2.git] / ArmPlatformPkg / Scripts / Ds5 / firmware_volume.py
1 #
2 # Copyright (c) 2011-2012, ARM Limited. All rights reserved.
3 #
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
8 #
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.
11 #
12
13 from arm_ds.debugger_v1 import DebugException
14
15 import struct
16 import string
17
18 class EfiFileSection(object):
19 EFI_SECTION_PE32 = 0x10
20 EFI_SECTION_PIC = 0x11
21 EFI_SECTION_TE = 0x12
22
23 EFI_IMAGE_DEBUG_TYPE_CODEVIEW = 0x2
24
25 SIZEOF_EFI_FFS_FILE_HEADER = 0x28
26
27 def __init__(self, ec, base):
28 self.base = base
29 self.ec = ec
30
31 def __str__(self):
32 return "FileSection(type:0x%X, size:0x%x)" % (self.get_type(), self.get_size())
33
34 def get_base(self):
35 return self.base
36
37 def get_type(self):
38 return struct.unpack("B", self.ec.getMemoryService().read(self.base + 0x3, 1, 8))[0]
39
40 def get_size(self):
41 return (struct.unpack("<I", self.ec.getMemoryService().read(self.base, 4, 32))[0] & 0x00ffffff)
42
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)
49 else:
50 raise Exception("EfiFileSection", "No debug section")
51 return section.get_debug_filepath()
52
53 class EfiSectionTE:
54 SIZEOF_EFI_TE_IMAGE_HEADER = 0x28
55 EFI_TE_IMAGE_SIGNATURE = ('V','Z')
56
57 def __init__(self, ec, base_te):
58 self.ec = ec
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")
63
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
67
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
72
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")
76
77 debug_rva = self.ec.getMemoryService().readMemory32(self.base_te + debug_dir_entry_rva + 0x14)
78 debug_rva -= stripped_size
79
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")
83
84 if dwarf_sig == 0x66727764:
85 filename = self.base_te + debug_rva + 0xc
86 else:
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')]
90
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
94
95 base_of_code = self.ec.getMemoryService().readMemory32(self.base_te + 0xC)
96
97 return self.base_te + base_of_code - stripped_size
98
99 class EfiSectionPE32:
100 def __init__(self, ec, base_pe32):
101 self.ec = ec
102 self.base_pe32 = base_pe32
103
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)
107
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")
112
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")
116
117
118 debug_rva = self.ec.getMemoryService().readMemory32(self.base_pe32 + debug_dir_entry_rva + 0x14)
119
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")
123
124 if dwarf_sig == 0x66727764:
125 filename = self.base_pe32 + debug_rva + 0xc
126 else:
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')]
130
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)
134
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)
137
138 if (base_of_code < base_of_data) and (base_of_code != 0):
139 return base_of_code
140 else:
141 return base_of_data
142
143 class FirmwareFile:
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
155
156 CONST_NB10_SIGNATURE = ('N','B','1','0')
157
158 def __init__(self, fv, base, ec):
159 self.fv = fv
160 self.base = base
161 self.ec = ec
162
163 def __str__(self):
164 return "FFS(state:0x%x, type:0x%X, size:0x%x)" % (self.get_state(), self.get_type(), self.get_size())
165
166 def get_base(self):
167 return self.base
168
169 def get_size(self):
170 size = (self.ec.getMemoryService().readMemory32(self.base + 0x14) & 0x00ffffff)
171
172 # Occupied size is the size considering the alignment
173 return size + ((0x8 - (size & 0x7)) & 0x7)
174
175 def get_type(self):
176 return self.ec.getMemoryService().readMemory8(self.base + 0x12)
177
178 def get_state(self):
179 state = self.ec.getMemoryService().readMemory8(self.base + 0x17)
180
181 polarity = self.fv.get_polarity()
182 if polarity:
183 state = ~state
184
185 highest_bit = 0x80;
186 while (highest_bit != 0) and ((highest_bit & state) == 0):
187 highest_bit >>= 1
188
189 return highest_bit
190
191 def get_next_section(self, section=None):
192 if section == None:
193 if self.get_type() != FirmwareFile.EFI_FV_FILETYPE_FFS_MIN:
194 section_base = self.get_base() + 0x18;
195 else:
196 return None
197 else:
198 section_base = int(section.get_base() + section.get_size())
199
200 # Align to next 4 byte boundary
201 if (section_base & 0x3) != 0:
202 section_base = section_base + 0x4 - (section_base & 0x3)
203
204 if section_base < self.get_base() + self.get_size():
205 return EfiFileSection(self.ec, section_base)
206 else:
207 return None
208
209 class FirmwareVolume:
210 CONST_FV_SIGNATURE = ('_','F','V','H')
211 EFI_FVB2_ERASE_POLARITY = 0x800
212
213 DebugInfos = []
214
215 def __init__(self, ec, fv_base, fv_size):
216 self.ec = ec
217 self.fv_base = fv_base
218 self.fv_size = fv_size
219
220 try:
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")
226
227 def get_size(self):
228 return self.ec.getMemoryService().readMemory32(self.fv_base + 0x20)
229
230 def get_attributes(self):
231 return self.ec.getMemoryService().readMemory32(self.fv_base + 0x2C)
232
233 def get_polarity(self):
234 attributes = self.get_attributes()
235 if attributes & FirmwareVolume.EFI_FVB2_ERASE_POLARITY:
236 return 1
237 else:
238 return 0
239
240 def get_next_ffs(self, ffs=None):
241 if 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)
244 else:
245 # Goto the next FFS file
246 ffs_base = int(ffs.get_base() + ffs.get_size())
247
248 # Align to next 8 byte boundary
249 if (ffs_base & 0x7) != 0:
250 ffs_base = ffs_base + 0x8 - (ffs_base & 0x7)
251
252 if ffs_base < self.fv_base + self.get_size():
253 return FirmwareFile(self, ffs_base, self.ec)
254 else:
255 return None
256
257 def get_debug_info(self):
258 self.DebugInfos = []
259
260 ffs = self.get_next_ffs()
261 while ffs != None:
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)
269
270 def load_symbols_at(self, addr):
271 if self.DebugInfos == []:
272 self.get_debug_info()
273
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)
280 else:
281 raise Exception('FirmwareVolume','Section Type not supported')
282
283 edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase())
284
285 return debug_info
286
287 def load_all_symbols(self):
288 if self.DebugInfos == []:
289 self.get_debug_info()
290
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)
296 else:
297 continue
298
299 edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase())