]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Scripts/Ds5/firmware_volume.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ArmPlatformPkg / Scripts / Ds5 / firmware_volume.py
1 #
2 # Copyright (c) 2011-2013, ARM Limited. All rights reserved.
3 #
4 # SPDX-License-Identifier: BSD-2-Clause-Patent
5 #
6
7 from arm_ds.debugger_v1 import DebugException
8
9 import struct
10 import string
11
12 import edk2_debugger
13
14 class EfiFileSection(object):
15 EFI_SECTION_PE32 = 0x10
16 EFI_SECTION_PIC = 0x11
17 EFI_SECTION_TE = 0x12
18
19 EFI_IMAGE_DEBUG_TYPE_CODEVIEW = 0x2
20
21 SIZEOF_EFI_FFS_FILE_HEADER = 0x28
22
23 def __init__(self, ec, base):
24 self.base = base
25 self.ec = ec
26
27 def __str__(self):
28 return "FileSection(type:0x%X, size:0x%x)" % (self.get_type(), self.get_size())
29
30 def get_base(self):
31 return self.base
32
33 def get_type(self):
34 return struct.unpack("B", self.ec.getMemoryService().read(self.base + 0x3, 1, 8))[0]
35
36 def get_size(self):
37 return (struct.unpack("<I", self.ec.getMemoryService().read(self.base, 4, 32))[0] & 0x00ffffff)
38
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)
45 else:
46 raise Exception("EfiFileSection", "No debug section")
47 return section.get_debug_filepath()
48
49 class EfiSectionTE:
50 SIZEOF_EFI_TE_IMAGE_HEADER = 0x28
51 EFI_TE_IMAGE_SIGNATURE = ('V','Z')
52
53 def __init__(self, ec, base_te):
54 self.ec = ec
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")
59
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
63
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
68
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")
72
73 debug_rva = self.ec.getMemoryService().readMemory32(self.base_te + debug_dir_entry_rva + 0x14)
74 debug_rva -= stripped_size
75
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")
79
80 if dwarf_sig == 0x66727764:
81 filename = self.base_te + debug_rva + 0xc
82 else:
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')]
86
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
90
91 return self.base_te - stripped_size
92
93 class EfiSectionPE32:
94 def __init__(self, ec, base_pe32):
95 self.ec = ec
96 self.base_pe32 = base_pe32
97
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)
101
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")
106
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")
110
111
112 debug_rva = self.ec.getMemoryService().readMemory32(self.base_pe32 + debug_dir_entry_rva + 0x14)
113
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")
117
118 if dwarf_sig == 0x66727764:
119 filename = self.base_pe32 + debug_rva + 0xc
120 else:
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')]
124
125 def get_debug_elfbase(self):
126 return self.base_pe32
127
128 class EfiSectionPE64:
129 def __init__(self, ec, base_pe64):
130 self.ec = ec
131 self.base_pe64 = base_pe64
132
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)
136
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")
141
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")
145
146
147 debug_rva = self.ec.getMemoryService().readMemory32(self.base_pe64 + debug_dir_entry_rva + 0x14)
148
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")
152
153 if dwarf_sig == 0x66727764:
154 filename = self.base_pe64 + debug_rva + 0xc
155 else:
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')]
159
160 def get_debug_elfbase(self):
161 return self.base_pe64
162
163 class FirmwareFile:
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
175
176 CONST_NB10_SIGNATURE = ('N','B','1','0')
177
178 def __init__(self, fv, base, ec):
179 self.fv = fv
180 self.base = base
181 self.ec = ec
182
183 def __str__(self):
184 return "FFS(state:0x%x, type:0x%X, size:0x%x)" % (self.get_state(), self.get_type(), self.get_size())
185
186 def get_base(self):
187 return self.base
188
189 def get_size(self):
190 size = (self.ec.getMemoryService().readMemory32(self.base + 0x14) & 0x00ffffff)
191
192 # Occupied size is the size considering the alignment
193 return size + ((0x8 - (size & 0x7)) & 0x7)
194
195 def get_type(self):
196 return self.ec.getMemoryService().readMemory8(self.base + 0x12)
197
198 def get_state(self):
199 state = self.ec.getMemoryService().readMemory8(self.base + 0x17)
200
201 polarity = self.fv.get_polarity()
202 if polarity:
203 state = ~state
204
205 highest_bit = 0x80;
206 while (highest_bit != 0) and ((highest_bit & state) == 0):
207 highest_bit >>= 1
208
209 return highest_bit
210
211 def get_next_section(self, section=None):
212 if section == None:
213 if self.get_type() != FirmwareFile.EFI_FV_FILETYPE_FFS_MIN:
214 section_base = self.get_base() + 0x18;
215 else:
216 return None
217 else:
218 section_base = int(section.get_base() + section.get_size())
219
220 # Align to next 4 byte boundary
221 if (section_base & 0x3) != 0:
222 section_base = section_base + 0x4 - (section_base & 0x3)
223
224 if section_base < self.get_base() + self.get_size():
225 return EfiFileSection(self.ec, section_base)
226 else:
227 return None
228
229 class FirmwareVolume:
230 CONST_FV_SIGNATURE = ('_','F','V','H')
231 EFI_FVB2_ERASE_POLARITY = 0x800
232
233 DebugInfos = []
234
235 def __init__(self, ec, fv_base, fv_size):
236 self.ec = ec
237 self.fv_base = fv_base
238 self.fv_size = fv_size
239
240 try:
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")
246
247 def get_size(self):
248 return self.ec.getMemoryService().readMemory32(self.fv_base + 0x20)
249
250 def get_attributes(self):
251 return self.ec.getMemoryService().readMemory32(self.fv_base + 0x2C)
252
253 def get_polarity(self):
254 attributes = self.get_attributes()
255 if attributes & FirmwareVolume.EFI_FVB2_ERASE_POLARITY:
256 return 1
257 else:
258 return 0
259
260 def get_next_ffs(self, ffs=None):
261 if 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)
264 else:
265 # Goto the next FFS file
266 ffs_base = int(ffs.get_base() + ffs.get_size())
267
268 # Align to next 8 byte boundary
269 if (ffs_base & 0x7) != 0:
270 ffs_base = ffs_base + 0x8 - (ffs_base & 0x7)
271
272 if ffs_base < self.fv_base + self.get_size():
273 return FirmwareFile(self, ffs_base, self.ec)
274 else:
275 return None
276
277 def get_debug_info(self):
278 self.DebugInfos = []
279
280 ffs = self.get_next_ffs()
281 while ffs != None:
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)
289
290 def load_symbols_at(self, addr, verbose = False):
291 if self.DebugInfos == []:
292 self.get_debug_info()
293
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)
300 else:
301 raise Exception('FirmwareVolume','Section Type not supported')
302
303 try:
304 edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase(), verbose)
305 except Exception, (ErrorClass, ErrorMessage):
306 if verbose:
307 print "Error while loading a symbol file (%s: %s)" % (ErrorClass, ErrorMessage)
308
309 return debug_info
310
311 def load_all_symbols(self, verbose = False):
312 if self.DebugInfos == []:
313 self.get_debug_info()
314
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)
320 else:
321 continue
322
323 try:
324 edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase(), verbose)
325 except Exception, (ErrorClass, ErrorMessage):
326 if verbose:
327 print "Error while loading a symbol file (%s: %s)" % (ErrorClass, ErrorMessage)
328