Skip to content

Commit

Permalink
Fix return value of rword, improve v6 handling, fix handling of check…
Browse files Browse the repository at this point in the history
…sum, minor bug fixes.
  • Loading branch information
bkerler committed Sep 14, 2024
1 parent dad27fc commit a53a9ef
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 112 deletions.
250 changes: 168 additions & 82 deletions mtkclient/Library/Auth/sla_keys.py

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions mtkclient/Library/Connection/devicehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,8 @@ def rdword(self, count=1, little=False):

def rword(self, count=1, little=False):
rev = "<" if little else ">"
data = []
for _ in range(count):
v = self.usbread(2)
if len(v) == 0:
return data
data.append(unpack(rev + "H", v)[0])
value = self.usbread(2*count)
data = unpack(rev + "H" * count, value)
if count == 1:
return data[0]
return data
Expand Down
4 changes: 2 additions & 2 deletions mtkclient/Library/Connection/seriallib.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def usbread(self, resplen=None, maxtimeout=0, timeout=0, w_max_packet_size=None)
return b""

if loglevel == logging.DEBUG:
self.debug(inspect.currentframe().f_back.f_code.co_name + ":" + hex(resplen))
self.debug("SERIAL "+inspect.currentframe().f_back.f_code.co_name + ": length(" + hex(resplen)+")")
if self.loglevel == logging.DEBUG:
self.verify_data(res[:resplen], "RX:")
return res[:resplen]
Expand Down Expand Up @@ -290,7 +290,7 @@ def usbxmlread(self, timeout=0):
return b""

if loglevel == logging.DEBUG:
self.debug(inspect.currentframe().f_back.f_code.co_name + ":" + hex(resplen))
self.debug("SERIAL "+inspect.currentframe().f_back.f_code.co_name + ": length(" + hex(resplen)+")")
if self.loglevel == logging.DEBUG:
self.verify_data(res[:resplen], "RX:")
return res[:resplen]
Expand Down
4 changes: 2 additions & 2 deletions mtkclient/Library/Connection/usblib.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def usbread(self, resplen=None, maxtimeout=100, w_max_packet_size=None):
return b""

if loglevel == logging.DEBUG:
self.debug(inspect.currentframe().f_back.f_code.co_name + ":" + hex(resplen))
self.debug("USB "+inspect.currentframe().f_back.f_code.co_name + ": length(" + hex(resplen)+")")
if self.loglevel == logging.DEBUG:
self.verify_data(res[:resplen], "RX:")
return res[:resplen]
Expand Down Expand Up @@ -558,7 +558,7 @@ def usbxmlread(self, maxtimeout=100):
break

if loglevel == logging.DEBUG:
self.debug(inspect.currentframe().f_back.f_code.co_name + ":" + hex(len(res)))
self.debug("USB "+inspect.currentframe().f_back.f_code.co_name + ": length(" + hex(len(res))+")")
if self.loglevel == logging.DEBUG:
self.verify_data(res, "RX:")
return res
Expand Down
2 changes: 2 additions & 0 deletions mtkclient/Library/DA/xflash/extension/xflash.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ class XCmd:
CUSTOM_WRITEREGISTER = 0x0F0004
CUSTOM_SET_STORAGE = 0x0F0005
CUSTOM_RPMB_SET_KEY = 0x0F0006
CUSTOM_RPMB_PROG_KEY = 0x0F0007
CUSTOM_RPMB_INIT = 0x0F0008
CUSTOM_RPMB_READ = 0x0F0009
CUSTOM_RPMB_WRITE = 0x0F000A
CUSTOM_SEJ_HW = 0x0F000B


rpmb_error = [
Expand Down
2 changes: 1 addition & 1 deletion mtkclient/Library/DA/xflash/xflash_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from struct import pack, unpack

from mtkclient.Library.Auth.sla import generate_da_sla_signature
from mtkclient.Library.Auth.sla_keys import da_sla_keys
from mtkclient.Library.DA.xflash.xflash_flash_param import NandExtension
from mtkclient.Library.DA.xflash.xflash_param import Cmd, ChecksumAlgorithm, FtSystemOSE, DataType
from mtkclient.Library.utils import LogBase, logsetup
Expand Down Expand Up @@ -1141,6 +1140,7 @@ def set_remote_sec_policy(self, data):

def handle_sla(self, da2):
rsakey = None
from mtkclient.Library.Auth.sla_keys import da_sla_keys
for key in da_sla_keys:
if da2.find(bytes.fromhex(key.n)) != -1:
rsakey = key
Expand Down
42 changes: 27 additions & 15 deletions mtkclient/Library/DA/xml/xml_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from mtkclient.Library.DA.xml.xml_cmd import XMLCmd, BootModes
from mtkclient.Library.DA.xml.extension.v6 import XmlFlashExt
from mtkclient.Library.Auth.sla import generate_da_sla_signature
from mtkclient.Library.Auth.sla_keys import da_sla_keys, SlaKey


rq = Queue()

Expand Down Expand Up @@ -189,6 +189,9 @@ def send_command(self, xmldata, noack: bool = False):
cmd, result = self.get_command_result()
if cmd == "CMD:END":
self.ack()
if result == '2nd DA address is invalid. reset.\r\n':
self.error(result)
exit(1)
scmd, sresult = self.get_command_result()
if scmd == "CMD:START":
if result == "OK":
Expand All @@ -203,7 +206,7 @@ def send_command(self, xmldata, noack: bool = False):
self.ack()
tcmd, tresult = self.get_command_result()
if tcmd == "CMD:START":
return sresult
return False
elif "ERR!" in result:
return result
return False
Expand Down Expand Up @@ -618,6 +621,7 @@ def upload_da(self):
else:
self.info("SLA is enabled")
rsakey = None
from mtkclient.Library.Auth.sla_keys import da_sla_keys, SlaKey
for key in da_sla_keys:
if isinstance(key, SlaKey):
if da2.find(bytes.fromhex(key.n)) != -1:
Expand Down Expand Up @@ -666,6 +670,8 @@ def upload_da(self):
# parttbl = self.read_partition_table()
self.config.hwparam.writesetting("hwcode", hex(self.config.hwcode))
return True
else:
return True
return False

def get_dev_info(self):
Expand Down Expand Up @@ -909,20 +915,23 @@ def readflash(self, addr, length, filename, parttype=None, display=True) -> (byt
self.mtk.daloader.progress.clear()
storage, parttype, length = partinfo

self.send_command(self.Cmd.cmd_read_flash(parttype, addr, length), noack=True)
cmd, result = self.get_command_result()
if type(result) is not UpFile:
return b""
data = self.download_raw(result=result, filename=filename, display=display)
scmd, sresult = self.get_command_result()
if sresult == "START":
if self.send_command(self.Cmd.cmd_read_flash(parttype, addr, length), noack=True):
cmd, result = self.get_command_result()
if type(result) is not UpFile:
return b""
data = self.download_raw(result=result, filename=filename, display=display)
scmd, sresult = self.get_command_result()
if sresult == "START":
if not filename:
return data
else:
return True
if not filename:
return data
else:
return True
if not filename:
return b""
return False
return b""
return False
else:
self.error("Read flash isn't supported")
return False

def writeflash(self, addr, length, filename, offset=0, parttype=None, wdata=None, display=True):
self.mtk.daloader.progress.clear()
Expand Down Expand Up @@ -973,6 +982,9 @@ def check_lifecycle(self):
self.send_command(self.Cmd.cmd_emmc_control(function="LIFE-CYCLE-STATUS"), noack=True)
cmd, result = self.get_command_result()
if not isinstance(result, UpFile):
if cmd == 'CMD:END':
self.ack()
scmd, sresult = self.get_command_result()
return False
data = self.download(result)
scmd, sresult = self.get_command_result()
Expand Down
4 changes: 3 additions & 1 deletion mtkclient/Library/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@
0x5002: "IMG_LOCK_NO_SPACE_ADD_LOCK_INFO",
0x5003: "IMG_LOCK_THIS_IMG_INFO_NOT_EXIST",
0x5004: "IMG_LOCK_MAGIC_ERROR",
0x5A5B: "DA_IN_BLACKLIST",
0x6000: "SBC_KEY_NOT_FOUND",
0x6001: "BR_SEC_CFG_NOT_FOUND",
0x6002: "ERR_PUBK_NOT_INITIALIZED",
Expand Down Expand Up @@ -977,7 +978,8 @@
3232: "S_DA_UPDATE_BOOTLOADER_NOT_CONTAIN_CRITICAL_DATA",
3233: "S_DA_DUMP_FLASH_LAYOUT_FAIL",
3234: "S_DA_BMT_NO_INIT",
3235: "S_DA_NOR_PROGRAM_REGION_IS_OVERLAP"
3235: "S_DA_NOR_PROGRAM_REGION_IS_OVERLAP",
0x5A5B: "S_DA_IN_BLACKLIST"
}


Expand Down
6 changes: 3 additions & 3 deletions mtkclient/Library/mtk_preloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from binascii import hexlify

from Cryptodome.Util.number import size
from mtkclient.Library.Auth.sla_keys import brom_sla_keys
from mtkclient.Library.Auth.sla import generate_brom_sla_challenge
from mtkclient.Library.settings import HwParam
from mtkclient.Library.utils import LogBase, logsetup
Expand Down Expand Up @@ -660,6 +659,7 @@ def send_auth(self, auth):
def handle_sla(self, func=None, isbrom: bool = True):
if isbrom:
# e, n, d
from mtkclient.Library.Auth.sla_keys import brom_sla_keys
for key in brom_sla_keys:
if self.echo(self.Cmd.SLA.value):
status = self.rword()
Expand Down Expand Up @@ -844,10 +844,10 @@ def upload_data(self, data, gen_chksum):
time.sleep(0.035)
try:
res = self.rword(2)
if isinstance(res, list) and res == []:
if isinstance(res, tuple) and res == []:
self.error("No reply from da loader.")
return False
if isinstance(res, list):
if isinstance(res, tuple):
checksum, status = res
if gen_chksum != checksum and checksum != 0:
self.warning("Checksum of upload doesn't match !")
Expand Down
2 changes: 2 additions & 0 deletions mtkclient/Library/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class Partf:

def get_gpt(self, gpt_settings, parttype: str = "user") -> tuple:
data = self.readflash(addr=0, length=2 * self.config.pagesize, filename="", parttype=parttype, display=False)
if not data:
return None, None
if data[:4] == b"BPI\x00":
guid_gpt = GPT(
num_part_entries=gpt_settings.gpt_num_part_entries,
Expand Down

0 comments on commit a53a9ef

Please sign in to comment.