Skip to content

Commit

Permalink
Fix reinit and rpmb issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bkerler committed Aug 3, 2024
1 parent aa425b5 commit f338168
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 16 deletions.
3 changes: 2 additions & 1 deletion mtkclient/Library/DA/mtk_da_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from mtkclient.Library.utils import Progress
from mtkclient.config.brom_config import Efuse, DAmodes
from mtkclient.Library.Filesystem.mtkdafs import MtkDaFS

try:
from fuse import FUSE
except ImportError:
Expand All @@ -30,7 +31,7 @@ def __init__(self, mtk, loglevel=logging.INFO):
self.pid = mtk.config.pid
self.interface = mtk.config.interface
self.pathconfig = PathConfig()
self.__logger, self.info, self.debug, self.warning, self.error = logsetup(self, self.__logger,
self.__logger, self.info, self.debug, self.warning, self.error = logsetup(self, self.__logger,
loglevel, mtk.config.gui)
self.eh = ErrorHandler()
self.mtk = mtk
Expand Down
3 changes: 1 addition & 2 deletions mtkclient/Library/DA/mtk_daloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def reinit(self):
self.config.meid = bytes.fromhex(config["meid"])
if "socid" in config:
self.config.socid = bytes.fromhex(config["socid"])
self.config.hwparam = HwParam(self.mtk.config, self.config.meid.hex(), self.mtk.config.hwparam_path)
if config["flashmode"] == "LEGACY":
self.mtk.config.chipconfig.damode = DAmodes.LEGACY
self.flashmode = DAmodes.LEGACY
Expand All @@ -155,8 +156,6 @@ def reinit(self):
self.mtk.config.chipconfig.damode = DAmodes.XML
self.flashmode = DAmodes.XML
self.config.init_hwcode(self.config.hwcode)
if self.config.meid is not None:
self.config.hwparam = HwParam(self.mtk.config, self.config.meid.hex(), self.mtk.config.hwparam_path)
if self.flashmode == DAmodes.XML:
self.da = DAXML(self.mtk, self.daconfig, self.loglevel)
self.daconfig.flashtype = config["flashtype"]
Expand Down
15 changes: 11 additions & 4 deletions mtkclient/Library/DA/xflash/extension/xflash.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def patch(self):
ufshcd_queuecommand_ptr = daextdata.find(b"\x55\x55\x55\x55")
ufshcd_get_free_tag_ptr = daextdata.find(b"\x66\x66\x66\x66")
ptr_g_ufs_hba_ptr = daextdata.find(b"\x77\x77\x77\x77")
efuse_addr_ptr = daextdata.find(b"\x88\x88\x88\x88")

if register_ptr != -1 and mmc_get_card_ptr != -1:
if register_devctrl:
Expand Down Expand Up @@ -159,6 +160,8 @@ def patch(self):
if g_ufs_hba is None:
g_ufs_hba = 0

efuse_addr = self.config.chipconfig.efuse_addr

# Patch the addr
daextdata[register_ptr:register_ptr + 4] = pack("<I", register_devctrl)
daextdata[mmc_get_card_ptr:mmc_get_card_ptr + 4] = pack("<I", mmc_get_card)
Expand All @@ -167,6 +170,7 @@ def patch(self):
daextdata[ufshcd_get_free_tag_ptr:ufshcd_get_free_tag_ptr + 4] = pack("<I", ufshcd_get_free_tag)
daextdata[ufshcd_queuecommand_ptr:ufshcd_queuecommand_ptr + 4] = pack("<I", ufshcd_queuecommand)
daextdata[ptr_g_ufs_hba_ptr:ptr_g_ufs_hba_ptr + 4] = pack("<I", g_ufs_hba)
daextdata[efuse_addr_ptr:efuse_addr_ptr+4]=pack("<I", efuse_addr)

# print(hexlify(daextdata).decode('utf-8'))
# open("daext.bin","wb").write(daextdata)
Expand Down Expand Up @@ -315,10 +319,10 @@ def cmd(self, cmd):
def custom_read(self, addr, length):
data = bytearray()
pos = 0
while pos<length:
while pos < length:
if self.cmd(XCmd.CUSTOM_READ):
self.xsend(data=addr+pos, is64bit=True)
sz = min(length,0x10000)
self.xsend(data=addr + pos, is64bit=True)
sz = min(length, 0x10000)
self.xsend(sz)
tmp = self.xread()
data.extend(tmp)
Expand Down Expand Up @@ -443,7 +447,8 @@ def custom_rpmb_init(self):
if meid != b"\x00" * 16:
# self.config.set_meid(meid)
self.info("Generating sej rpmbkey...")
rpmbkey = hwc.aes_hwcrypt(mode="rpmb", data=meid, btype="sej", otp=otp)
#rpmbkey = hwc.aes_hwcrypt(mode="rpmb", data=meid, btype="sej", otp=otp)
rpmbkey = hwc.aes_hwcrypt(btype="dxcc", mode="rpmb")
if rpmbkey is not None:
if self.cmd(XCmd.CUSTOM_SET_RPMB_KEY):
self.xsend(rpmbkey)
Expand All @@ -461,6 +466,8 @@ def custom_rpmb_init(self):
cmd = XCmd.CUSTOM_INIT_UFS_RPMB
if self.cmd(cmd):
derivedrpmb = self.xread()
# rpmb_frame = self.xread()
# hash = self.xread()
if int.from_bytes(derivedrpmb[:4], 'little') != 0xff:
status = self.status()
if status == 0:
Expand Down
3 changes: 0 additions & 3 deletions mtkclient/Library/DA/xflash/xflash_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,9 +1245,6 @@ def upload_da(self):
if status == 0x0 and unpack("<I", ret)[0] == 0xA1A2A3A4:
self.info("DA Extensions successfully added")
self.daext = True
self.config.hwparam = HwParam(self.mtk.config, self.mtk.config.meid,
self.mtk.config.hwparam_path)
self.config.hwparam.writesetting("hwcode", hex(self.config.hwcode))
if not self.daext:
self.warning("DA Extensions failed to enable")

Expand Down
11 changes: 7 additions & 4 deletions mtkclient/Library/DA/xml/extension/v6.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import logging
import os
import sys
Expand Down Expand Up @@ -135,6 +136,7 @@ def patch(self):
ufshcd_queuecommand_ptr = daextdata.find(b"\x55\x55\x55\x55")
ufshcd_get_free_tag_ptr = daextdata.find(b"\x66\x66\x66\x66")
ptr_g_ufs_hba_ptr = daextdata.find(b"\x77\x77\x77\x77")
efuse_addr_ptr = daextdata.find(b"\x88\x88\x88\x88")

# register_xml_cmd("CMD:GET-SYS-PROPERTY", & a1, cmd_get_sys_property);

Expand Down Expand Up @@ -181,6 +183,7 @@ def patch(self):
if mmc_rpmb_send_command is None:
mmc_rpmb_send_command = 0

efuse_addr = self.config.chipconfig.efuse_addr
#########################################
if register_ptr != -1:
if register_xml_cmd:
Expand All @@ -196,6 +199,7 @@ def patch(self):
daextdata[ufshcd_get_free_tag_ptr:ufshcd_get_free_tag_ptr + 4] = pack("<I", ufshcd_get_free_tag)
daextdata[ufshcd_queuecommand_ptr:ufshcd_queuecommand_ptr + 4] = pack("<I", ufshcd_queuecommand)
daextdata[ptr_g_ufs_hba_ptr:ptr_g_ufs_hba_ptr + 4] = pack("<I", g_ufs_hba)
daextdata[efuse_addr_ptr:efuse_addr_ptr + 4] = pack("<I", efuse_addr)

# print(hexlify(daextdata).decode('utf-8'))
# open("daext.bin","wb").write(daextdata)
Expand Down Expand Up @@ -358,7 +362,8 @@ def custom_rpmb_init(self):
if meid != b"\x00" * 16:
# self.config.set_meid(meid)
self.info("Generating sej rpmbkey...")
rpmbkey = hwc.aes_hwcrypt(mode="rpmb", data=meid, btype="sej", otp=otp)
rpmbkey = hwc.aes_hwcrypt(btype="dxcc", mode="rpmb")
#rpmbkey = hwc.aes_hwcrypt(mode="rpmb", data=meid, btype="sej", otp=otp)
if rpmbkey is not None:
xmlcmd = self.xflash.Cmd.create_cmd("CUSTOMRPMBKEY")
if self.xsend(xmlcmd):
Expand All @@ -375,9 +380,7 @@ def custom_rpmb_init(self):
if rpmbkey == read_key:
self.info("Setting rpmbkey: ok")
ufs = False
if self.xflash.emmc.rpmb_size != 0:
ufs = False
elif self.xflash.ufs.block_size != 0:
if self.xflash.emmc is None:
ufs = True
xmlcmd = self.xflash.Cmd.create_cmd("CUSTOMMMCINIT")
if ufs:
Expand Down
5 changes: 5 additions & 0 deletions mtkclient/Library/mtk_preloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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
from mtkclient.Library.error import ErrorHandler
from mtkclient.config.brom_config import DAmodes
Expand Down Expand Up @@ -224,6 +225,8 @@ def init(self, maxtries=None, display=True):
self.info("\tSW Ver:\t\t\t" + hex(self.config.swver))
meid = self.get_meid()
if meid is not None:
self.config.hwparam = HwParam(self.mtk.config, self.config.meid.hex(), self.mtk.config.hwparam_path)
self.config.hwparam.writesetting("hwcode", hex(self.config.hwcode))
self.config.set_meid(meid)
if self.display:
self.info("ME_ID:\t\t\t" + hexlify(meid).decode('utf-8').upper())
Expand All @@ -234,6 +237,8 @@ def init(self, maxtries=None, display=True):
if self.display:
if socid != b"":
self.info("SOC_ID:\t\t\t" + hexlify(socid).decode('utf-8').upper())
self.config.hwparam.writesetting("socid", hexlify(socid).decode('utf-8'))

if self.config.auth is not None and self.config.is_brom and self.config.target_config["daa"]:
if os.path.exists(self.config.auth):
authdata = open(self.config.auth, "rb").read()
Expand Down
4 changes: 3 additions & 1 deletion mtkclient/Library/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def __init__(self, config, meid: str, path: str = "logs"):
self.paramsetting = {}
if os.path.exists(os.path.join(path, self.paramfile)):
try:
self.paramsetting = json.loads(open(os.path.join(path, self.paramfile), "r").read())
tmp = json.loads(open(os.path.join(path, self.paramfile), "r").read())
if tmp["meid"] == meid:
self.paramsetting = tmp
except Exception:
# json file invalid, load nothing.
pass
Expand Down
1 change: 0 additions & 1 deletion mtkclient/config/mtk_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def set_hwcode(self, hwcode):
self.hwparam.writesetting("hwcode", hex(hwcode))

def set_meid(self, meid):
self.hwparam = HwParam(self, meid, self.hwparam_path)
self.meid = meid
self.hwparam.writesetting("meid", hexlify(meid).decode('utf-8'))

Expand Down

0 comments on commit f338168

Please sign in to comment.