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