Skip to content

Commit

Permalink
Minor bugfixes, beautify
Browse files Browse the repository at this point in the history
  • Loading branch information
bkerler committed May 12, 2024
1 parent 491f930 commit 8e46df6
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
__pycache__
# Compiled python modules.
*.pyc
.idea
DA_*.bin

# Setuptools distribution folder.
/dist/
Expand Down
6 changes: 6 additions & 0 deletions mtkclient/Library/DA/mtk_da_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,11 @@ def handle_da_cmds(self, mtk, cmd: str, args):
partitionname = args.partitionname
parttype = args.parttype
filename = args.filename
self.mtk.config.hwparam_path = os.path.dirname(filename)
self.da_read(partitionname=partitionname, parttype=parttype, filename=filename)
elif cmd == "rl":
directory = args.directory
self.mtk.config.hwparam_path = directory
parttype = args.parttype
if args.skip:
skip = args.skip.split(",")
Expand All @@ -664,12 +666,14 @@ def handle_da_cmds(self, mtk, cmd: str, args):
self.da_rl(directory=directory, parttype=parttype, skip=skip)
elif cmd == "rf":
filename = args.filename
self.mtk.config.hwparam_path = os.path.dirname(filename)
parttype = args.parttype
self.da_rf(filename=filename, parttype=parttype)
elif cmd == "rs":
start = getint(args.startsector)
sectors = getint(args.sectors)
filename = args.filename
self.mtk.config.hwparam_path = os.path.dirname(filename)
parttype = args.parttype
if self.da_rs(start=start, sectors=sectors, filename=filename, parttype=parttype):
print(f"Dumped sector {str(start)} with sector count {str(sectors)} as {filename}.")
Expand All @@ -679,6 +683,7 @@ def handle_da_cmds(self, mtk, cmd: str, args):
start = getint(args.offset)
length = getint(args.length)
filename = args.filename
self.mtk.config.hwparam_path = os.path.dirname(filename)
parttype = args.parttype
if self.da_ro(start=start, length=length, filename=filename, parttype=parttype):
print(f"Dumped offset {hex(start)} with length {hex(length)} as {filename}.")
Expand Down Expand Up @@ -804,6 +809,7 @@ def handle_da_cmds(self, mtk, cmd: str, args):
data = args.data
self.da_poke(addr=addr, data=data, filename=filename)
elif subcmd == "generatekeys":
self.mtk.config.hwparam_path = "."
mtk.daloader.keys()
elif subcmd == "dumpbrom":
filename = f"brom_{hex(mtk.daloader.config.hwcode)[2:]}.bin"
Expand Down
2 changes: 1 addition & 1 deletion mtkclient/Library/DA/mtk_daloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def set_da(self):
self.flashmode = damodes.XFLASH
if self.mtk.config.chipconfig.damode == damodes.XFLASH:
self.flashmode = damodes.XFLASH
elif self.mtk.config.chipconfig.damode == damodes.XML:
elif self.mtk.config.chipconfig.damode == damodes.XML or self.daconfig.da_loader.v6:
self.flashmode = damodes.XML
if self.flashmode == damodes.XFLASH:
self.da = DAXFlash(self.mtk, self.daconfig, self.loglevel)
Expand Down
14 changes: 14 additions & 0 deletions mtkclient/Library/DA/xml/xml_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ def cmd_notify_init_hw(self):
cmd = self.create_cmd("NOTIFY-INIT-HW")
return cmd

def cmd_security_set_flash_policy(self,host_offset: int = 0x8000000,
length: int = 0x100000):
"""
<?xml version="1.0" encoding="utf-8"?><da><version>1.0</version><command>CMD:SECURITY-SET-FLASH-POLICY</command><arg>
<source_file>MEM://0x8000000:0x100000</source_file></arg></da>
"""
content = {
"arg": [
f"<source_file>MEM://{hex(host_offset)}:{hex(length)}</source_file>"
]
}
cmd = self.create_cmd("SECURITY-SET-FLASH-POLICY", content)
return cmd

def cmd_boot_to(self, at_addr: int = 0x40000000, jmp_addr: int = 0x40000000, host_offset: int = 0x7fe83c09a04c,
length: int = 0x50c78):
"""
Expand Down
2 changes: 1 addition & 1 deletion mtkclient/Library/DA/xml/xml_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def upload(self, result: dwnfile, data, display=True, raw=False):
if cmd == "CMD:START":
return True
else:
cmd, result = self.get_command_result()
cmd, startresult = self.get_command_result()
self.error(result)
return False
else:
Expand Down
6 changes: 4 additions & 2 deletions mtkclient/Library/asmtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ def disasm(code, cpu, mode, bigendian, size):
print("CPU and/or mode not supported!")
exit(0)

instr = [f"{i.mnemonic}\t{i.op_str}" for i in cs.disasm(code, size)]
# print("0x%x:\t%s\t%s" % (i.address, i.mnemonic, i.op_str))
instr = []
for i in cs.disasm(code, size):
# print("0x%x:\t%s\t%s" % (i.address, i.mnemonic, i.op_str))
instr.append("%s\t%s" % (i.mnemonic, i.op_str))
return instr


Expand Down
3 changes: 3 additions & 0 deletions mtkclient/Library/mtk_preloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,9 @@ def upload_data(self, data, gen_chksum):
time.sleep(0.035)
try:
res = self.rword(2)
if isinstance(res, list) and res == []:
self.error("No reply from da loader.")
return False
if isinstance(res, list):
checksum, status = res
if gen_chksum != checksum and checksum != 0:
Expand Down
5 changes: 5 additions & 0 deletions mtkclient/Tools/da_parser
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import os
import sys
from struct import unpack
import inspect
current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parent_dir = os.path.dirname(os.path.dirname(current_dir))
sys.path.insert(0, parent_dir)
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'),
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pyside6 >= 6.4.0.1
mock >= 4.0.3
pyserial >= 3.5
flake8
fusepy
unicorn
keystone-engine
capstone
unicorn
fusepy

0 comments on commit 8e46df6

Please sign in to comment.