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