Optimize code for Tools

This commit is contained in:
ColdWindScholar 2024-05-04 13:24:44 +08:00
parent 15af7e8324
commit 4a48dec4b1
6 changed files with 20 additions and 15 deletions

View file

@ -203,7 +203,7 @@ class DAconfig(metaclass=LogBase):
with open(preloader, "rb") as rf: with open(preloader, "rb") as rf:
data = rf.read() data = rf.read()
else: else:
self.error("Preloader : " + preloader + " doesn't exist. Aborting.") self.error(f"Preloader : {preloader} doesn't exist. Aborting.")
exit(1) exit(1)
try: try:
self.emiver, self.emi = self.m_extract_emi(data) self.emiver, self.emi = self.m_extract_emi(data)
@ -211,7 +211,7 @@ class DAconfig(metaclass=LogBase):
self.emiver = 0 self.emiver = 0
self.emi = None self.emi = None
def parse_da_loader(self, loader:str, dasetup:dict): def parse_da_loader(self, loader: str, dasetup: dict):
try: try:
with open(loader, 'rb') as bootldr: with open(loader, 'rb') as bootldr:
# data = bootldr.read() # data = bootldr.read()
@ -245,7 +245,7 @@ class DAconfig(metaclass=LogBase):
dasetup[da.hw_code].append(da) dasetup[da.hw_code].append(da)
return True return True
except Exception as e: except Exception as e:
self.error("Couldn't open loader: " + loader + ". Reason: " + str(e)) self.error(f"Couldn't open loader: {loader}. Reason: {str(e)}")
return False return False
def setup(self): def setup(self):

View file

@ -80,7 +80,8 @@ class legacyext(metaclass=LogBase):
self.warning("Legacy DA2 CMD F0 not patched.") self.warning("Legacy DA2 CMD F0 not patched.")
return da2patched return da2patched
def fix_hash(self, da1, da2, da2sig_len, hashpos, hashmode): @staticmethod
def fix_hash(da1, da2, da2sig_len, hashpos, hashmode):
da1 = bytearray(da1) da1 = bytearray(da1)
dahash = None dahash = None
if hashmode == 1: if hashmode == 1:
@ -245,8 +246,7 @@ class legacyext(metaclass=LogBase):
except Exception: except Exception:
return return
hwc = self.cryptosetup() hwc = self.cryptosetup()
retval = {} retval = {"hwcode": hex(self.config.hwcode)}
retval["hwcode"] = hex(self.config.hwcode)
meid = self.config.get_meid() meid = self.config.get_meid()
socid = self.config.get_socid() socid = self.config.get_socid()
hwcode = self.config.get_hwcode() hwcode = self.config.get_hwcode()

View file

@ -91,7 +91,7 @@ class DA_handler(metaclass=LogBase):
if mtk.config.target_config["sbc"] and not mtk.config.is_brom and mtk.config.loader is None: if mtk.config.target_config["sbc"] and not mtk.config.is_brom and mtk.config.loader is None:
mtk = mtk.bypass_security() mtk = mtk.bypass_security()
self.mtk = mtk self.mtk = mtk
if self.mtk.daloader.patch : if self.mtk.daloader.patch:
self.info("Device was protected. Successfully bypassed security.") self.info("Device was protected. Successfully bypassed security.")
else: else:
self.info("Device is still protected, trying to boot to brom") self.info("Device is still protected, trying to boot to brom")
@ -685,7 +685,8 @@ class DA_handler(metaclass=LogBase):
print(f"Failed to dump offset {hex(start)} with length {hex(length)} as {filename}.") print(f"Failed to dump offset {hex(start)} with length {hex(length)} as {filename}.")
elif cmd == "fs": elif cmd == "fs":
print(f'Mounting FUSE fs at: {args.mountpoint}...') print(f'Mounting FUSE fs at: {args.mountpoint}...')
fs = FUSE(MtkDaFS(self, rw=args.rw), mountpoint=args.mountpoint, foreground=True, allow_other=True, nothreads=True) fs = FUSE(MtkDaFS(self, rw=args.rw), mountpoint=args.mountpoint, foreground=True, allow_other=True,
nothreads=True)
elif cmd == "footer": elif cmd == "footer":
filename = args.filename filename = args.filename
self.da_footer(filename=filename) self.da_footer(filename=filename)

View file

@ -89,7 +89,8 @@ class DAloader(metaclass=LogBase):
return idx, hashmode, hashlen return idx, hashmode, hashlen
return idx, hashmode, hashlen return idx, hashmode, hashlen
def find_da_hash_V6(self, da1, siglen): @staticmethod
def find_da_hash_V6(da1, siglen):
pos = len(da1) - siglen - 0x30 pos = len(da1) - siglen - 0x30
hash = da1[pos:pos + 0x30] hash = da1[pos:pos + 0x30]
if hash[-4:] == b"\x00\x00\x00\x00": if hash[-4:] == b"\x00\x00\x00\x00":
@ -110,7 +111,8 @@ class DAloader(metaclass=LogBase):
self.debug("Error: No hash found") self.debug("Error: No hash found")
return -1, -1 return -1, -1
def calc_da_hash(self, da1, da2): @staticmethod
def calc_da_hash(da1, da2):
hashdigest = hashlib.sha1(da2).digest() hashdigest = hashlib.sha1(da2).digest()
hashdigest256 = hashlib.sha256(da2).digest() hashdigest256 = hashlib.sha256(da2).digest()
idx = da1.find(hashdigest) idx = da1.find(hashdigest)
@ -120,7 +122,8 @@ class DAloader(metaclass=LogBase):
hashmode = 2 hashmode = 2
return hashmode, idx return hashmode, idx
def fix_hash(self, da1, da2, hashpos, hashmode, hashlen): @staticmethod
def fix_hash(da1, da2, hashpos, hashmode, hashlen):
da1 = bytearray(da1) da1 = bytearray(da1)
dahash = None dahash = None
if hashmode == 1: if hashmode == 1:

View file

@ -14,7 +14,8 @@ class XMLCmd(metaclass=LogBase):
self.mtk = mtk self.mtk = mtk
self.MAGIC = 0xFEEEEEEF self.MAGIC = 0xFEEEEEEF
def create_cmd(self, cmd: str, content: dict = None, version="1.0"): @staticmethod
def create_cmd(cmd: str, content: dict = None, version="1.0"):
cmd = f"<?xml version=\"1.0\" encoding=\"utf-8\"?><da><version>{version}</version><command>CMD:{cmd}</command>" cmd = f"<?xml version=\"1.0\" encoding=\"utf-8\"?><da><version>{version}</version><command>CMD:{cmd}</command>"
if content is not None: if content is not None:
for item in content: for item in content:

View file

@ -104,16 +104,16 @@ def main():
pos -= 1 pos -= 1
else: else:
pos2 = find_binary(data, "46FFF7", pos + 8) pos2 = find_binary(data, "46FFF7", pos + 8)
if pos2 != None: if pos2 is not None:
if pos2 - pos < 0x20: if pos2 - pos < 0x20:
pos = pos pos = pos
else: else:
pos = pos2 - 1 pos = pos2 - 1
posr = -1 posr = -1
startpos = 0 startpos = 0
while posr != None: while posr is not None:
posr = find_binary(data, "2DE9F047", startpos) posr = find_binary(data, "2DE9F047", startpos)
if posr == None: if posr is None:
break break
if data[posr + 7] == 0x46 and data[posr + 8] == 0x92: if data[posr + 7] == 0x46 and data[posr + 8] == 0x92:
break break