1# 2# Copyright (c) 2011-2013, 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 13from arm_ds.debugger_v1 import DebugException 14 15import struct 16import string 17 18import edk2_debugger 19 20class 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 55class 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 return self.base_te - stripped_size 98 99class 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 return self.base_pe32 133 134class EfiSectionPE64: 135 def __init__(self, ec, base_pe64): 136 self.ec = ec 137 self.base_pe64 = base_pe64 138 139 def get_debug_filepath(self): 140 # Offset from dos hdr to PE file hdr (EFI_IMAGE_NT_HEADERS64) 141 file_header_offset = self.ec.getMemoryService().readMemory32(self.base_pe64 + 0x3C) 142 143 # Offset to debug dir in PE hdrs 144 debug_dir_entry_rva = self.ec.getMemoryService().readMemory32(self.base_pe64 + file_header_offset + 0xB8) 145 if debug_dir_entry_rva == 0: 146 raise Exception("EfiFileSectionPE64","No Debug Directory") 147 148 debug_type = self.ec.getMemoryService().readMemory32(self.base_pe64 + debug_dir_entry_rva + 0xC) 149 if (debug_type != 0xdf) and (debug_type != EfiFileSection.EFI_IMAGE_DEBUG_TYPE_CODEVIEW): 150 raise Exception("EfiFileSectionPE64","Debug type is not dwarf") 151 152 153 debug_rva = self.ec.getMemoryService().readMemory32(self.base_pe64 + debug_dir_entry_rva + 0x14) 154 155 dwarf_sig = struct.unpack("cccc", self.ec.getMemoryService().read(str(self.base_pe64 + debug_rva), 4, 32)) 156 if (dwarf_sig != 0x66727764) and (dwarf_sig != FirmwareFile.CONST_NB10_SIGNATURE): 157 raise Exception("EfiFileSectionPE64","Dwarf debug signature not found") 158 159 if dwarf_sig == 0x66727764: 160 filename = self.base_pe64 + debug_rva + 0xc 161 else: 162 filename = self.base_pe64 + debug_rva + 0x10 163 filename = struct.unpack("200s", self.ec.getMemoryService().read(str(filename), 200, 32))[0] 164 return filename[0:string.find(filename,'\0')] 165 166 def get_debug_elfbase(self): 167 return self.base_pe64 168 169class FirmwareFile: 170 EFI_FV_FILETYPE_RAW = 0x01 171 EFI_FV_FILETYPE_FREEFORM = 0x02 172 EFI_FV_FILETYPE_SECURITY_CORE = 0x03 173 EFI_FV_FILETYPE_PEI_CORE = 0x04 174 EFI_FV_FILETYPE_DXE_CORE = 0x05 175 EFI_FV_FILETYPE_PEIM = 0x06 176 EFI_FV_FILETYPE_DRIVER = 0x07 177 EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER = 0x08 178 EFI_FV_FILETYPE_APPLICATION = 0x09 179 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE = 0x0B 180 EFI_FV_FILETYPE_FFS_MIN = 0xF0 181 182 CONST_NB10_SIGNATURE = ('N','B','1','0') 183 184 def __init__(self, fv, base, ec): 185 self.fv = fv 186 self.base = base 187 self.ec = ec 188 189 def __str__(self): 190 return "FFS(state:0x%x, type:0x%X, size:0x%x)" % (self.get_state(), self.get_type(), self.get_size()) 191 192 def get_base(self): 193 return self.base 194 195 def get_size(self): 196 size = (self.ec.getMemoryService().readMemory32(self.base + 0x14) & 0x00ffffff) 197 198 # Occupied size is the size considering the alignment 199 return size + ((0x8 - (size & 0x7)) & 0x7) 200 201 def get_type(self): 202 return self.ec.getMemoryService().readMemory8(self.base + 0x12) 203 204 def get_state(self): 205 state = self.ec.getMemoryService().readMemory8(self.base + 0x17) 206 207 polarity = self.fv.get_polarity() 208 if polarity: 209 state = ~state 210 211 highest_bit = 0x80; 212 while (highest_bit != 0) and ((highest_bit & state) == 0): 213 highest_bit >>= 1 214 215 return highest_bit 216 217 def get_next_section(self, section=None): 218 if section == None: 219 if self.get_type() != FirmwareFile.EFI_FV_FILETYPE_FFS_MIN: 220 section_base = self.get_base() + 0x18; 221 else: 222 return None 223 else: 224 section_base = int(section.get_base() + section.get_size()) 225 226 # Align to next 4 byte boundary 227 if (section_base & 0x3) != 0: 228 section_base = section_base + 0x4 - (section_base & 0x3) 229 230 if section_base < self.get_base() + self.get_size(): 231 return EfiFileSection(self.ec, section_base) 232 else: 233 return None 234 235class FirmwareVolume: 236 CONST_FV_SIGNATURE = ('_','F','V','H') 237 EFI_FVB2_ERASE_POLARITY = 0x800 238 239 DebugInfos = [] 240 241 def __init__(self, ec, fv_base, fv_size): 242 self.ec = ec 243 self.fv_base = fv_base 244 self.fv_size = fv_size 245 246 try: 247 signature = struct.unpack("cccc", self.ec.getMemoryService().read(fv_base + 0x28, 4, 32)) 248 except DebugException: 249 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))) 250 if signature != FirmwareVolume.CONST_FV_SIGNATURE: 251 raise Exception("FirmwareVolume", "This is not a valid firmware volume") 252 253 def get_size(self): 254 return self.ec.getMemoryService().readMemory32(self.fv_base + 0x20) 255 256 def get_attributes(self): 257 return self.ec.getMemoryService().readMemory32(self.fv_base + 0x2C) 258 259 def get_polarity(self): 260 attributes = self.get_attributes() 261 if attributes & FirmwareVolume.EFI_FVB2_ERASE_POLARITY: 262 return 1 263 else: 264 return 0 265 266 def get_next_ffs(self, ffs=None): 267 if ffs == None: 268 # Get the offset of the first FFS file from the FV header 269 ffs_base = self.fv_base + self.ec.getMemoryService().readMemory16(self.fv_base + 0x30) 270 else: 271 # Goto the next FFS file 272 ffs_base = int(ffs.get_base() + ffs.get_size()) 273 274 # Align to next 8 byte boundary 275 if (ffs_base & 0x7) != 0: 276 ffs_base = ffs_base + 0x8 - (ffs_base & 0x7) 277 278 if ffs_base < self.fv_base + self.get_size(): 279 return FirmwareFile(self, ffs_base, self.ec) 280 else: 281 return None 282 283 def get_debug_info(self): 284 self.DebugInfos = [] 285 286 ffs = self.get_next_ffs() 287 while ffs != None: 288 section = ffs.get_next_section() 289 while section != None: 290 type = section.get_type() 291 if (type == EfiFileSection.EFI_SECTION_TE) or (type == EfiFileSection.EFI_SECTION_PE32): 292 self.DebugInfos.append((section.get_base(), section.get_size(), section.get_type())) 293 section = ffs.get_next_section(section) 294 ffs = self.get_next_ffs(ffs) 295 296 def load_symbols_at(self, addr, verbose = False): 297 if self.DebugInfos == []: 298 self.get_debug_info() 299 300 for debug_info in self.DebugInfos: 301 if (addr >= debug_info[0]) and (addr < debug_info[0] + debug_info[1]): 302 if debug_info[2] == EfiFileSection.EFI_SECTION_TE: 303 section = EfiSectionTE(self.ec, debug_info[0] + 0x4) 304 elif debug_info[2] == EfiFileSection.EFI_SECTION_PE32: 305 section = EfiSectionPE32(self.ec, debug_info[0] + 0x4) 306 else: 307 raise Exception('FirmwareVolume','Section Type not supported') 308 309 try: 310 edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase(), verbose) 311 except Exception, (ErrorClass, ErrorMessage): 312 if verbose: 313 print "Error while loading a symbol file (%s: %s)" % (ErrorClass, ErrorMessage) 314 315 return debug_info 316 317 def load_all_symbols(self, verbose = False): 318 if self.DebugInfos == []: 319 self.get_debug_info() 320 321 for debug_info in self.DebugInfos: 322 if debug_info[2] == EfiFileSection.EFI_SECTION_TE: 323 section = EfiSectionTE(self.ec, debug_info[0] + 0x4) 324 elif debug_info[2] == EfiFileSection.EFI_SECTION_PE32: 325 section = EfiSectionPE32(self.ec, debug_info[0] + 0x4) 326 else: 327 continue 328 329 try: 330 edk2_debugger.load_symbol_from_file(self.ec, section.get_debug_filepath(), section.get_debug_elfbase(), verbose) 331 except Exception, (ErrorClass, ErrorMessage): 332 if verbose: 333 print "Error while loading a symbol file (%s: %s)" % (ErrorClass, ErrorMessage) 334 335