def load_ELF(self, fname): try: fd = open(fname, "rb") self.elf = elffile.ELFFile(fd) except: traceback.print_exc() log.warn("Could not parse ELF file...") return False if "_fini" in symbols: fini = symbols["_fini"] if yesno("Found _fini of already installed patch at 0x%x. Execute?" % fini): self.launchRam(fini-1) #load sections for i in range(self.elf.num_sections()): section = self.elf.get_section(i) if section.header["sh_flags"] & SH_FLAGS.SHF_ALLOC != 0: addr = section.header["sh_addr"] name = section.name #NOBITS sections contains no data in file #Will be initialized with zero if section.header["sh_type"] == "SHT_NOBITS": data = b"\x00" * section.header["sh_size"] else: data = section.data() log.info("Loading %s @ 0x%x - 0x%x (%d bytes)" % (name, addr, addr+len(data), len(data))) #write section data to device self.writeMem(addr, data) #load symbols n = 0 for i in range(self.elf.num_sections()): section = self.elf.get_section(i) if section.header.sh_type == 'SHT_SYMTAB': for symbol in section.iter_symbols(): if symbol.name != "" and "$" not in symbol.name: symbols[symbol.name] = symbol.entry["st_value"] n += 1 log.info("Loaded %d symbols" % n) return self.elf.header.e_entry
def debug_hci_callback(self, record): hcipkt = record[0] if not issubclass(hcipkt.__class__, hci.HCI_Event): return #We use event code 0xfe for info text messages #Buffer messages until a newline is found if hcipkt.event_code == 0xfe: self.msg += hcipkt.data while "\n" in self.msg: msg_split = self.msg.split("\n") log.info("Firmware says: %s" % msg_split[0]) self.msg = "\n".join(msg_split[1:]) #We use event code 0xfd for data if hcipkt.event_code == 0xfd: log.info("Firmware says: %s" % hexlify(hcipkt.data)) return
def xmit_state_hci_callback(self, record): hcipkt = record[0] if not issubclass(hcipkt.__class__, hci.HCI_Event): return #State report if hcipkt.event_code == 0xfc: saved_regs, cont = struct.unpack("II", hcipkt.data[:8]) if cont != 0: log.info("Receiving firmware state: regs@0x%x cont@0x%x" % (saved_regs, cont)) self.segment_data = [] self.segments = {} self.succsess = True self.saved_regs = saved_regs self.cont = cont else: if not self.succsess: return log.info("Received fuill firmware state") groupName = datetime.now().strftime("internalBlue_%m.%d.%Y_%H.%M.%S") self.project = Project("projects/"+self.internalblue.fw.FW_NAME) self.project.add_group(groupName) self.project.deactivate_all_groups() self.project.set_active_group(groupName, True) self.project.save() self.project.add_symbol(groupName, "cont", self.cont|1) self.project.add_symbol(groupName, "get_int", symbols["get_int"]) self.project.add_symbol(groupName, "set_int", symbols["set_int"]) self.project.add_symbol(groupName, "saved_regs", self.saved_regs) for segment_addr in self.segments: self.project.add_segment(groupName, "", segment_addr, "".join(self.segments[segment_addr])) self.project.save() if hcipkt.event_code == 0xfb: segment_addr,size,current = struct.unpack("III", hcipkt.data[:12]) self.segment_data += [hcipkt.data[12:]] #Check if we have missed an HCI event if segment_addr + len(self.segment_data)*128 != current + 128: if self.succsess: print( hex(segment_addr), hex(len(self.segment_data)*128), hex( current + 128)) log.info("Failed to receive state") self.succsess = False #Fully received memory dumo if len(self.segment_data)*128 == size: log.info("Received segment 0x%x - 0x%x" % (segment_addr, segment_addr+size)) self.segments[segment_addr] = self.segment_data self.segment_data = []