From 9478832620e2ef527e38fdf8a0580dbe82099b82 Mon Sep 17 00:00:00 2001 From: ColdWindScholar <3590361911@qq.com> Date: Sat, 4 May 2024 12:06:39 +0800 Subject: [PATCH] Add missing packages to requirements.txt and reformat Some Codes --- mtkclient/Library/mtk_main.py | 2 +- mtkclient/Tools/brom_to_offs | 138 +++++++++++++++++----------------- mtkclient/Tools/da_parser | 68 +++++++++-------- requirements.txt | 2 + 4 files changed, 111 insertions(+), 99 deletions(-) diff --git a/mtkclient/Library/mtk_main.py b/mtkclient/Library/mtk_main.py index 8fbc509..4ed2532 100755 --- a/mtkclient/Library/mtk_main.py +++ b/mtkclient/Library/mtk_main.py @@ -561,7 +561,7 @@ class Main(metaclass=LogBase): for pos in range(offset, offset + length, rlen): print("Reading pos %08X" % pos) res = mtk.preloader.read32(pos, rlen // 4) - if res == []: + if not res: break print(hexlify(b"".join([pack("> 16) & 0xFFFFF)<<16) + base = (((usbdl_put_data >> 16) & 0xFFFFF) << 16) usbdl_get_data = unpack(" #define PAYLOAD_2_0 char SOC_NAME[] = "{socname}"; @@ -455,7 +456,7 @@ volatile uint32_t **SEC_REG2=(volatile uint32_t **){hex(sec_sla)}; volatile uint32_t SEC_OFFSET={hex(sec_offset)}; volatile uint32_t *bladdr=(volatile uint32_t *){hex(blacklist)}; volatile uint32_t *bladdr2=(volatile uint32_t *){hex(blacklist2)}; -volatile uint32_t *uart_reg0 = (volatile uint32_t*){hex(uart_addr+0x14)}; +volatile uint32_t *uart_reg0 = (volatile uint32_t*){hex(uart_addr + 0x14)}; volatile uint32_t *uart_reg1 = (volatile uint32_t*){hex(uart_addr)}; int (*cmd_handler)() = (void*){hex(cmd_handler)}; @@ -464,9 +465,10 @@ int (*cmd_handler)() = (void*){hex(cmd_handler)}; print(header) if not os.path.exists("headers"): os.mkdir("headers") - open(os.path.join("headers",socname+".h"),"w").write(header) + open(os.path.join("headers", socname + ".h"), "w").write(header) except: print(sys.argv[1]) + if __name__ == "__main__": main() diff --git a/mtkclient/Tools/da_parser b/mtkclient/Tools/da_parser index a364eab..8ef3f0c 100755 --- a/mtkclient/Tools/da_parser +++ b/mtkclient/Tools/da_parser @@ -6,6 +6,7 @@ from struct import unpack from mtkclient.config.payloads import pathconfig from mtkclient.Library.utils import read_object from mtkclient.Library.utils import find_binary + entry_region = [ ('m_buf', 'I'), ('m_len', 'I'), @@ -27,7 +28,8 @@ DA = [ # vector LoadRegion ] -efusedb={} +efusedb = {} + def main(): da_setup = [] @@ -54,7 +56,8 @@ def main(): da.append(entry_tmp) da_setup.append(da) print(f"Loader: {os.path.basename(loader)}") - dadb[da[0]["hw_code"]]=[("da_payload_addr",da[2]["m_start_addr"]),("pl_payload_addr", da[3]["m_start_addr"])] + dadb[da[0]["hw_code"]] = [("da_payload_addr", da[2]["m_start_addr"]), + ("pl_payload_addr", da[3]["m_start_addr"])] print("hwcode: 0x%04X" % da[0]["hw_code"]) print("hw_sub_code: 0x%04X" % da[0]["hw_sub_code"]) print("hw_version: 0x%04X" % da[0]["hw_version"]) @@ -62,56 +65,60 @@ def main(): print("Reserved1: 0x%04X" % da[0]["reserved1"]) print("Reserved3: 0x%04X" % da[0]["reserved3"]) for i in range(da[0]["entry_region_count"]): - entry=da[i+1] - print(f"\t{i}: "+hex(entry['m_start_addr'])) - mbuf=da[3]["m_buf"] - m_len=da[3]["m_len"] - startaddr=da[3]["m_start_addr"] - with open(loader,"rb") as rf: + entry = da[i + 1] + print(f"\t{i}: " + hex(entry['m_start_addr'])) + mbuf = da[3]["m_buf"] + m_len = da[3]["m_len"] + startaddr = da[3]["m_start_addr"] + with open(loader, "rb") as rf: rf.seek(mbuf) - da2data=rf.read(m_len) - fname=os.path.join("loaders",hex(da[0]["hw_code"])[2:]+"_"+hex(startaddr)[2:]+os.path.basename(loader)) - open(fname,"wb").write(da2data) - mbuf=da[2]["m_buf"] - m_len=da[2]["m_len"] - startaddr=da[2]["m_start_addr"] + da2data = rf.read(m_len) + fname = os.path.join("loaders", + hex(da[0]["hw_code"])[2:] + "_" + hex(startaddr)[2:] + os.path.basename( + loader)) + open(fname, "wb").write(da2data) + mbuf = da[2]["m_buf"] + m_len = da[2]["m_len"] + startaddr = da[2]["m_start_addr"] sys.stdout.flush() - with open(loader,"rb") as rf: + with open(loader, "rb") as rf: rf.seek(mbuf) - data=rf.read(m_len) - hashidx=data.find(int.to_bytes(0xC0070004,4,'little')) - if hashidx!=-1: + data = rf.read(m_len) + hashidx = data.find(int.to_bytes(0xC0070004, 4, 'little')) + if hashidx != -1: print("Hash check found.") else: - hashidx = data.find(b"\xCC\xF2\x07\x09") # => b"\x4F\xF0\x00\x09"" + hashidx = data.find(b"\xCC\xF2\x07\x09") # => b"\x4F\xF0\x00\x09"" if hashidx != -1: print("Hash check 2 found.") else: - hashidx = find_binary(data,b"\x14\x2C\xF6.\xFE\xE7") # => b"\x14\x2C\xF6\xD1\x00\x00" + hashidx = find_binary(data, b"\x14\x2C\xF6.\xFE\xE7") # => b"\x14\x2C\xF6\xD1\x00\x00" if hashidx is not None: print("Hash check 3 found.") else: print("HASH ERROR !!!!") - fname=os.path.join("loaders",hex(da[0]["hw_code"])[2:]+"_"+hex(startaddr)[2:]+os.path.basename(loader)) - open(fname,"wb").write(data) + fname = os.path.join("loaders", + hex(da[0]["hw_code"])[2:] + "_" + hex(startaddr)[2:] + os.path.basename( + loader)) + open(fname, "wb").write(data) print(f"Offset: {hex(mbuf)}") print(f"Length: {hex(m_len)}") print(f"Addr: {hex(startaddr)}") bootldr.seek(da[2]["m_buf"]) - tt=bootldr.read(da[2]["m_len"]) + tt = bootldr.read(da[2]["m_len"]) idx = tt.find(bytes.fromhex("70BB442D27D244A7")) #idx = tt.find(bytes.fromhex("01279360D36013615361")) - if idx!=-1: + if idx != -1: print("V3 Enabled") bootldr.seek(da[3]["m_buf"]) - tt=bootldr.read(da[3]["m_len"]) + tt = bootldr.read(da[3]["m_len"]) idx2 = tt.find(bytes.fromhex("03 29 0D D9 07 4B 1B 68 03 60")) - if idx2!=-1: - efusedb[da[0]["hw_code"]]=hex(int.from_bytes(tt[idx2+0x24:idx2+0x28],'little')&0xFFFFF000) + if idx2 != -1: + efusedb[da[0]["hw_code"]] = hex(int.from_bytes(tt[idx2 + 0x24:idx2 + 0x28], 'little') & 0xFFFFF000) else: if not da[0]["hw_code"] in efusedb: - efusedb[da[0]["hw_code"]]="None" + efusedb[da[0]["hw_code"]] = "None" print() sorted_dict = dict(sorted(efusedb.items())) @@ -135,5 +142,6 @@ def main(): print(str(idx)+" "+name+": "+hex(fields[2])) """ -if __name__=="__main__": - main() \ No newline at end of file + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt index 8e4749a..442e034 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,5 @@ mock >= 4.0.3 pyserial >= 3.5 flake8 fusepy +unicorn +capstone