diff --git a/mtk b/mtk index 30cbae8..a20998a 100755 --- a/mtk +++ b/mtk @@ -302,7 +302,7 @@ class Main(metaclass=LogBase): logfilename = os.path.join("logs", "log.txt") if os.path.exists(logfilename): os.remove(logfilename) - fh = logging.FileHandler(logfilename) + fh = logging.FileHandler(logfilename, encoding='utf-8') self.__logger.addHandler(fh) self.debug(" ".join(sys.argv)) @@ -388,15 +388,17 @@ class Main(metaclass=LogBase): "Trying to dump preloader from ram.") plt = PLTools(mtk=mtk, loglevel=self.__logger.level) dadata, filename = plt.run_dump_preloader(self.args.ptype) - mtk.config.preloader = self.patch_preloader_security(dadata) + mtk.config.preloader = mtk.patch_preloader_security(dadata) if mtk.config.preloader is not None: self.info("Using custom preloader : "+filename) daaddr, dadata = mtk.parse_preloader(mtk.config.preloader) + mtk.config.preloader = mtk.patch_preloader_security(dadata) if mtk.preloader.send_da(daaddr, len(dadata), 0x100, dadata): self.info(f"Sent preloader to {hex(daaddr)}, length {hex(len(dadata))}") if mtk.preloader.jump_da(daaddr): self.info(f"PL Jumped to daaddr {hex(daaddr)}.") time.sleep(2) + sys.exit(1) mtk = Mtk(config=mtk.config, loglevel=self.__logger.level) res = mtk.preloader.init() if not res: @@ -412,6 +414,9 @@ class Main(metaclass=LogBase): # data=0x200*b"\x00"+data mtk.preloader.send_partition_data(partition, pldata) status = mtk.preloader.jump_to_partition(partition) # Do not remove ! + res=mtk.preloader.read32(0x10C180,10) + for val in res: + print(hex(val)) if status!=0x0: self.error("Error on jumping to partition: "+self.eh.status(status)) else: @@ -631,12 +636,6 @@ class Main(metaclass=LogBase): if mtk.port.cdc.connected and os.path.exists(".state"): info=mtk.daloader.reinit() else: - if os.path.exists("logs"): - try: - shutil.rmtree("logs", ignore_errors=True, onerror=None) - os.mkdir("logs") - except: - pass try: preloader = self.args.preloader except: @@ -1109,7 +1108,7 @@ class Main(metaclass=LogBase): self.close() -info = "MTK Flash/Exploit Client V1.50 (c) B.Kerler 2018-2021" +info = "MTK Flash/Exploit Client V1.51 (c) B.Kerler 2018-2021" cmds = { "printgpt": "Print GPT Table information", diff --git a/mtkclient/Library/Port.py b/mtkclient/Library/Port.py index f837c46..48b1eb5 100755 --- a/mtkclient/Library/Port.py +++ b/mtkclient/Library/Port.py @@ -36,7 +36,7 @@ class Port(metaclass=LogBase): if loglevel == logging.DEBUG: logfilename = os.path.join("logs", "log.txt") - fh = logging.FileHandler(logfilename) + fh = logging.FileHandler(logfilename, encoding='utf-8') self.__logger.addHandler(fh) self.__logger.setLevel(logging.DEBUG) else: diff --git a/mtkclient/Library/cqdma.py b/mtkclient/Library/cqdma.py index c3ef497..30c9cc1 100755 --- a/mtkclient/Library/cqdma.py +++ b/mtkclient/Library/cqdma.py @@ -58,7 +58,7 @@ class cqdma(metaclass=LogBase): self.reg = cqdma_reg(setup) if loglevel == logging.DEBUG: logfilename = os.path.join("logs", "log.txt") - fh = logging.FileHandler(logfilename) + fh = logging.FileHandler(logfilename, encoding='utf-8') self.__logger.addHandler(fh) self.__logger.setLevel(logging.DEBUG) else: diff --git a/mtkclient/Library/daconfig.py b/mtkclient/Library/daconfig.py index 73353d4..53eec44 100755 --- a/mtkclient/Library/daconfig.py +++ b/mtkclient/Library/daconfig.py @@ -7,6 +7,7 @@ from struct import unpack from mtkclient.Library.utils import LogBase, read_object, logsetup from mtkclient.config.payloads import pathconfig from mtkclient.config.brom_config import damodes +from mtkclient.Library.utils import structhelper class Storage: MTK_DA_HW_STORAGE_NOR = 0 @@ -73,27 +74,41 @@ class NandCellUsage: CELL_OCT = 7 -entry_region = [ - ('m_buf', 'I'), - ('m_len', 'I'), - ('m_start_addr', 'I'), - ('m_start_offset', 'I'), - ('m_sig_len', 'I')] +class entry_region: + m_buf = None + m_len = None + m_start_addr = None + m_start_offset = None + m_sig_len = None + def __init__(self, data): + sh = structhelper(data) + self.m_buf = sh.dword() + self.m_len = sh.dword() + self.m_start_addr = sh.dword() + self.m_start_offset = sh.dword() + self.m_sig_len = sh.dword() -DA = [ - ('magic', 'H'), - ('hw_code', 'H'), - ('hw_sub_code', 'H'), - ('hw_version', 'H'), - ('sw_version', 'H'), - ('reserved1', 'H'), - ('pagesize', 'H'), - ('reserved3', 'H'), - ('entry_region_index', 'H'), - ('entry_region_count', 'H') - # vector LoadRegion -] +class DA: + def __init__(self,data): + self.loader = None + sh = structhelper(data) + self.magic = sh.short() + self.hw_code = sh.short() + self.hw_sub_code = sh.short() + self.hw_version = sh.short() + self.sw_version = sh.short() + self.reserved1 = sh.short() + self.pagesize = sh.short() + self.reserved3 = sh.short() + self.entry_region_index = sh.short() + self.entry_region_count = sh.short() + self.region = [] + for _ in range(self.entry_region_count): + entry_tmp = entry_region(sh.bytes(20)) + self.region.append(entry_tmp) + def setfilename(self, loaderfilename: str): + self.loader = loaderfilename class DAconfig(metaclass=LogBase): def __init__(self, mtk, loader=None, preloader=None, loglevel=logging.INFO): @@ -118,8 +133,9 @@ class DAconfig(metaclass=LogBase): loaders = [] for root, dirs, files in os.walk(self.pathconfig.get_loader_path(), topdown=False): for file in files: - if "Preloader" not in root: + if "MTK_AllInOne_DA" in file: loaders.append(os.path.join(root, file)) + loaders = sorted(loaders)[::-1] for loader in loaders: self.parse_da_loader(loader) else: @@ -148,9 +164,9 @@ class DAconfig(metaclass=LogBase): val = unpack(">31,fdekeylen - """ - SBROM_KeyDerivation PC(00230B77) R0:10210000,R1:00000001,R2:001209D8,R3:00000008,R4:00100000,R5:00233760,R6:00000010 - R2:53514e43214c465a - R5:52504d42204b45595341534953514e43 - key="SQNC!LFZ",8 - salt="SASI",4 - requestedlen=0x10 - """ - - def SBROM_KeyDerivation(self, aeskeytype, key, salt, requestedlen, destaddr): + def SBROM_KeyDerivation(self, aeskeytype, label, salt, requestedlen, destaddr): result = bytearray() - buffer = bytearray(b"\x00" * 0x43) if aeskeytype - 1 > 4 or (1 << (aeskeytype - 1) & 0x17) == 0: return 0xF2000002 if requestedlen > 0xFF or (requestedlen << 28) & 0xFFFFFFFF: return 0xF2000003 - if 0x0 >= len(key) > 0x20: + if 0x0 >= len(label) > 0x20: return 0xF2000003 - bufferlen = len(salt) + 3 + len(key) + bufferlen = len(salt) + 3 + len(label) iterlength = (requestedlen + 0xF) >> 4 - if len(key) == 0: - keyend = 1 - else: - buffer[1:1 + len(key)] = key - keyend = len(key) + 1 - saltstart = keyend + 1 - if len(salt) > 0: - buffer[saltstart:saltstart + len(salt)] = salt - # ToDo: verify buffer structure - buffer[saltstart + len(salt):saltstart + len(salt) + 4] = pack("> 2) & 3) << 20 self.sasi_sb_adddescsequence(mdesc) # Process input data diff --git a/mtkclient/Library/hwcrypto_sej.py b/mtkclient/Library/hwcrypto_sej.py index f035f53..1120e61 100755 --- a/mtkclient/Library/hwcrypto_sej.py +++ b/mtkclient/Library/hwcrypto_sej.py @@ -153,7 +153,7 @@ class sej(metaclass=LogBase): self.info = self.__logger.info if loglevel == logging.DEBUG: logfilename = os.path.join("logs", "log.txt") - fh = logging.FileHandler(logfilename) + fh = logging.FileHandler(logfilename, encoding='utf-8') self.__logger.addHandler(fh) self.__logger.setLevel(logging.DEBUG) else: diff --git a/mtkclient/Library/mtk_dalegacy.py b/mtkclient/Library/mtk_dalegacy.py index df54f72..b864ec9 100755 --- a/mtkclient/Library/mtk_dalegacy.py +++ b/mtkclient/Library/mtk_dalegacy.py @@ -7,82 +7,155 @@ import os import time from struct import pack, unpack from binascii import hexlify -from mtkclient.Library.utils import LogBase, progress, read_object, logsetup +from mtkclient.Library.utils import LogBase, progress, read_object, logsetup, structhelper from mtkclient.Library.error import ErrorHandler from mtkclient.Library.daconfig import DaStorage, EMMC_PartitionType from mtkclient.Library.partition import Partition from mtkclient.config.payloads import pathconfig -norinfo = [ - ('m_nor_ret', '>I'), - ('m_nor_chip_select', '2B'), - ('m_nor_flash_id', '>H'), - ('m_nor_flash_size', '>I'), - ('m_nor_flash_dev_code', '>4H'), - ('m_nor_flash_otp_status', '>I'), - ('m_nor_flash_otp_size', '>I') -] -nandinfo32 = [ - ('m_nand_info', '>I'), - ('m_nand_chip_select', '>B'), - ('m_nand_flash_id', '>H'), - ('m_nand_flash_size', '>I'), - ('m_nand_flash_id_count', '>H'), -] +class norinfo: + m_nor_ret = None + m_nor_chip_select = None + m_nor_flash_id = None + m_nor_flash_size = None + m_nor_flash_dev_code = None + m_nor_flash_otp_status = None + m_nor_flash_otp_size = None + + def __init__(self, data): + sh = structhelper(data) + self.m_nor_ret = sh.dword(True) + self.m_nor_chip_select = sh.bytes(2) + self.m_nor_flash_id = sh.short(True) + self.m_nor_flash_size = sh.dword(True) + self.m_nor_flash_dev_code = sh.shorts(4, True) + self.m_nor_flash_otp_status = sh.dword(True) + self.m_nor_flash_otp_size = sh.dword(True) + + +class nandinfo32: + m_nand_info = None + m_nand_chip_select = None + m_nand_flash_id = None + m_nand_flash_size = None + m_nand_flash_id_count = None + info2 = None + + def __init__(self, data): + sh = structhelper(data) + self.m_nand_info = sh.dword(True) + self.m_nand_chip_select = sh.bytes(1) + self.m_nand_flash_id = sh.short(True) + self.m_nand_flash_size = sh.dword(True) + self.m_nand_flash_id_count = sh.short(True) + self.info2 = None + + +class nandinfo64: + m_nand_info = None + m_nand_chip_select = None + m_nand_flash_id = None + m_nand_flash_size = None + m_nand_flash_id_count = None + info2 = None + + def __init__(self, data): + sh = structhelper(data) + self.m_nand_info = sh.dword(True) + self.m_nand_chip_select = sh.bytes(1) + self.m_nand_flash_id = sh.short(True) + self.m_nand_flash_size = sh.qword(True) + self.m_nand_flash_id_count = sh.short(True) + self.info2 = None -nandinfo64 = [ - ('m_nand_info', '>I'), - ('m_nand_chip_select', '>B'), - ('m_nand_flash_id', '>H'), - ('m_nand_flash_size', '>Q'), - ('m_nand_flash_id_count', '>H'), -] # ('m_nand_flash_dev_code', '>7H'), -nandinfo2 = [ - ('m_nand_pagesize', '>H'), - ('m_nand_sparesize', '>H'), - ('m_nand_pages_per_block', '>H'), - ('m_nand_io_interface', 'B'), - ('m_nand_addr_cycle', 'B'), - ('m_nand_bmt_exist', 'B'), -] +class nandinfo2: + m_nand_pagesize = None + m_nand_sparesize = None + m_nand_pages_per_block = None + m_nand_io_interface = None + m_nand_addr_cycle = None + m_nand_bmt_exist = None -emmcinfo = [ - ('m_emmc_ret', '>I'), - ('m_emmc_boot1_size', '>Q'), - ('m_emmc_boot2_size', '>Q'), - ('m_emmc_rpmb_size', '>Q'), - ('m_emmc_gp_size', '>4Q'), - ('m_emmc_ua_size', '>Q'), - ('m_emmc_cid', '>2Q'), - ('m_emmc_fwver', '8B') -] + def __init__(self, data): + sh = structhelper(data) + self.m_nand_pagesize = sh.short(True) + self.m_nand_sparesize = sh.short(True) + self.m_nand_pages_per_block = sh.short(True) + self.m_nand_io_interface = sh.bytes(1) + self.m_nand_addr_cycle = sh.bytes(1) + self.m_nand_bmt_exist = sh.bytes(1) -sdcinfo = [ - ('m_sdmmc_info', '>I'), - ('m_sdmmc_ua_size', '>Q'), - ('m_sdmmc_cid', '>2Q') -] -configinfo = [ - ('m_int_sram_ret', '>I'), - ('m_int_sram_size', '>I'), - ('m_ext_ram_ret', '>I'), - ('m_ext_ram_type', 'B'), - ('m_ext_ram_chip_select', 'B'), - ('m_ext_ram_size', '>Q'), - ('randomid', '>2Q'), -] +class emmcinfo: + m_emmc_ret = None + m_emmc_boot1_size = None + m_emmc_boot2_size = None + m_emmc_rpmb_size = None + m_emmc_gp_size = None + m_emmc_ua_size = None + m_emmc_cid = None + m_emmc_fwver = None -passinfo = [ - ('ack', 'B'), - ('m_download_status', '>I'), - ('m_boot_style', '>I'), - ('soc_ok', 'B') -] + def __init__(self, data): + sh = structhelper(data) + self.m_emmc_ret = sh.dword(True) + self.m_emmc_boot1_size = sh.qword(True) + self.m_emmc_boot2_size = sh.qword(True) + self.m_emmc_rpmb_size = sh.qword(True) + self.m_emmc_gp_size = sh.qwords(4, True) + self.m_emmc_ua_size = sh.qword(True) + self.m_emmc_cid = sh.qwords(2, True) + self.m_emmc_fwver = sh.bytes(8) + + +class sdcinfo: + m_sdmmc_info = None + m_sdmmc_ua_size = None + m_sdmmc_cid = None + + def __init__(self, data): + sh = structhelper(data) + self.m_sdmmc_info = sh.dword(True) + self.m_sdmmc_ua_size = sh.qword(True) + self.m_sdmmc_cid = sh.qwords(2, True) + + +class configinfo: + m_int_sram_ret = None + m_int_sram_size = None + m_ext_ram_ret = None + m_ext_ram_type = None + m_ext_ram_chip_select = None + m_ext_ram_size = None + randomid = None + + def __init__(self, data): + sh = structhelper(data) + self.m_int_sram_ret = sh.dword(True) + self.m_int_sram_size = sh.dword(True) + self.m_ext_ram_ret = sh.dword(True) + self.m_ext_ram_type = sh.bytes() + self.m_ext_ram_chip_select = sh.bytes() + self.m_ext_ram_size = sh.qword(True) + self.randomid = sh.qwords(2, True) + + +class passinfo: + ack = None + m_download_status = None + m_boot_style = None + soc_ok = None + + def __init__(self, data): + sh = structhelper(data) + self.ack = sh.bytes() + self.m_download_status = sh.dword(True) + self.m_boot_style = sh.dword(True) + self.soc_ok = sh.bytes() def crc_word(data, chs=0): @@ -491,6 +564,9 @@ class DALegacy(metaclass=LogBase): def __init__(self, mtk, daconfig, loglevel=logging.INFO): self.__logger = logsetup(self, self.__logger, loglevel) + self.debug = self.debug + self.error = self.error + self.info = self.info self.emmc = None self.nand = None self.nor = None @@ -731,7 +807,7 @@ class DALegacy(metaclass=LogBase): dramlength = unpack(">I", self.usbread(0x4))[0] # 0x000000BC self.debug("Info: " + hex(dramlength)) self.usbwrite(self.Rsp.ACK) - lendram=len(self.daconfig.emi) + lendram = len(self.daconfig.emi) self.usbwrite(pack(">I", lendram)) elif val in [0x0B]: info = self.usbread(0x10) # 0x000000BC @@ -762,22 +838,19 @@ class DALegacy(metaclass=LogBase): return buffer def read_flash_info(self): - data = self.usbread(0x1C) - self.nor = read_object(data, norinfo) + self.nor = norinfo(self.usbread(0x1C)) data = self.usbread(0x11) - self.nand = read_object(data, nandinfo64) - nandcount = self.nand["m_nand_flash_id_count"] + self.nand = nandinfo64(data) + nandcount = self.nand.m_nand_flash_id_count if nandcount == 0: - self.nand = read_object(data, nandinfo32) - nandcount = self.nand["m_nand_flash_id_count"] + self.nand = nandinfo32(data) + nandcount = self.nand.m_nand_flash_id_count nc = data[-4:] + self.usbread(nandcount * 2 - 4) else: nc = self.usbread(nandcount * 2) m_nand_dev_code = unpack(">" + str(nandcount) + "H", nc) - self.nand["m_nand_flash_dev_code"] = m_nand_dev_code - ni2 = read_object(self.usbread(9), nandinfo2) - for ni in ni2: - self.nand[ni] = ni2[ni] + self.nand.m_nand_flash_dev_code = m_nand_dev_code + self.nand.info2 = nandinfo2(self.usbread(9)) self.emmc = read_object(self.usbread(0x5C), emmcinfo) self.sdc = read_object(self.usbread(0x1C), sdcinfo) self.flashconfig = read_object(self.usbread(0x26), configinfo) @@ -798,11 +871,11 @@ class DALegacy(metaclass=LogBase): self.info("Uploading stage 1...") with open(loader, 'rb') as bootldr: # stage 1 - stage = 2 - offset = self.daconfig.da[stage]["m_buf"] - size = self.daconfig.da[stage]["m_len"] - address = self.daconfig.da[stage]["m_start_addr"] - sig_len = self.daconfig.da[stage]["m_sig_len"] + stage = 1 + offset = self.daconfig.da.region[stage].m_buf + size = self.daconfig.da.region[stage].m_len + address = self.daconfig.da.region[stage].m_start_addr + sig_len = self.daconfig.da.region[stage].m_sig_len bootldr.seek(offset) dadata = bootldr.read(size) if self.mtk.preloader.send_da(address, size, sig_len, dadata): @@ -849,16 +922,16 @@ class DALegacy(metaclass=LogBase): self.set_stage2_config(self.config.hwcode) self.info("Uploading stage 2...") # stage 2 - if self.brom_send(self.daconfig, bootldr, 3): + if self.brom_send(self.daconfig, bootldr, 2): if self.read_flash_info(): if self.daconfig.flashtype == "nand": - self.daconfig.flashsize = self.nand["m_nand_flash_size"] + self.daconfig.flashsize = self.nand.m_nand_flash_size elif self.daconfig.flashtype == "emmc": - self.daconfig.flashsize = self.emmc["m_emmc_ua_size"] + self.daconfig.flashsize = self.emmc.m_emmc_ua_size if self.daconfig.flashsize == 0: - self.daconfig.flashsize = self.sdc["m_sdmmc_ua_size"] + self.daconfig.flashsize = self.sdc.m_sdmmc_ua_size elif self.daconfig.flashtype == "nor": - self.daconfig.flashsize = self.nor["m_nor_flash_size"] + self.daconfig.flashsize = self.nor.m_nor_flash_size self.info("Reconnecting to preloader") self.set_usb_cmd() self.mtk.port.close(reset=False) @@ -908,9 +981,9 @@ class DALegacy(metaclass=LogBase): self.mtk.port.close(reset=True) def brom_send(self, dasetup, da, stage, packetsize=0x1000): - offset = dasetup.da[stage]["m_buf"] - size = dasetup.da[stage]["m_len"] - address = dasetup.da[stage]["m_start_addr"] + offset = dasetup.da.region[stage].m_buf + size = dasetup.da.region[stage].m_len + address = dasetup.da.region[stage].m_start_addr da.seek(offset) dadata = da.read(size) self.usbwrite(pack(">I", address)) @@ -974,43 +1047,11 @@ class DALegacy(metaclass=LogBase): return False def sdmmc_write_data(self, addr, length, filename, offset=0, parttype=None, wdata=None, display=True): - if parttype is None or parttype == "user": - length = min(length, self.emmc["m_emmc_ua_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_USER - elif parttype == "boot1": - length = min(length, self.emmc["m_emmc_boot1_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_BOOT1 - elif parttype == "boot2": - length = min(length, self.emmc["m_emmc_boot2_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_BOOT2 - elif parttype == "gp1": - length = min(length, self.emmc["m_emmc_gp_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP1 - elif parttype == "gp2": - length = min(length, self.emmc["m_emmc_gp_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP2 - elif parttype == "gp3": - length = min(length, self.emmc["m_emmc_gp_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP3 - elif parttype == "gp4": - length = min(length, self.emmc["m_emmc_gp_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP4 - elif parttype == "rpmb": - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_RPMB - - if self.daconfig.flashtype == "nor": - storage = DaStorage.MTK_DA_STORAGE_NOR - elif self.daconfig.flashtype == "nand": - storage = DaStorage.MTK_DA_STORAGE_NAND - elif self.daconfig.flashtype == "ufs": - storage = DaStorage.MTK_DA_STORAGE_UFS - elif self.daconfig.flashtype == "sdc": - storage = DaStorage.MTK_DA_STORAGE_SDMMC - else: - storage = DaStorage.MTK_DA_STORAGE_EMMC + length, parttype = self.get_parttype(length, parttype) + storage = self.get_storage() if filename is not None: - fh=open(filename, "rb") + fh = open(filename, "rb") fh.seek(offset) self.progress.show_progress("Write", 0, 100, display) self.usbwrite(self.Cmd.SDMMC_WRITE_DATA_CMD) @@ -1029,20 +1070,33 @@ class DALegacy(metaclass=LogBase): if fh: data = bytearray(fh.read(count)) else: - data = wdata[offset:offset+count] + data = wdata[offset:offset + count] self.usbwrite(data) chksum = sum(data) & 0xFFFF self.usbwrite(pack(">H", chksum)) if self.usbread(1) != self.Rsp.CONT_CHAR: self.error("Data ack failed for sdmmc_write_data") return False - self.progress.show_progress("Write",offset,length,display) + self.progress.show_progress("Write", offset, length, display) offset += count if fh: fh.close() self.progress.show_progress("Write", 100, 100, display) return True + def get_storage(self): + if self.daconfig.flashtype == "nor": + storage = DaStorage.MTK_DA_STORAGE_NOR + elif self.daconfig.flashtype == "nand": + storage = DaStorage.MTK_DA_STORAGE_NAND + elif self.daconfig.flashtype == "ufs": + storage = DaStorage.MTK_DA_STORAGE_UFS + elif self.daconfig.flashtype == "sdc": + storage = DaStorage.MTK_DA_STORAGE_SDMMC + else: + storage = DaStorage.MTK_DA_STORAGE_EMMC + return storage + def sdmmc_write_image(self, addr, length, filename, display=True): if filename != "": with open(filename, "rb") as rf: @@ -1091,29 +1145,7 @@ class DALegacy(metaclass=LogBase): wdata=wdata, display=display) def formatflash(self, addr, length, parttype=None, display=True): - if parttype is None or parttype == "user" or parttype == "": - length = min(length, self.emmc["m_emmc_ua_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_USER - elif parttype == "boot1": - length = min(length, self.emmc["m_emmc_boot1_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_BOOT1 - elif parttype == "boot2": - length = min(length, self.emmc["m_emmc_boot2_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_BOOT2 - elif parttype == "gp1": - length = min(length, self.emmc["m_emmc_gp_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP1 - elif parttype == "gp2": - length = min(length, self.emmc["m_emmc_gp_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP2 - elif parttype == "gp3": - length = min(length, self.emmc["m_emmc_gp_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP3 - elif parttype == "gp4": - length = min(length, self.emmc["m_emmc_gp_size"]) - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP4 - elif parttype == "rpmb": - parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_RPMB + length, parttype = self.get_parttype(length, parttype) self.check_usb_cmd() if self.daconfig.flashtype == "emmc": self.sdmmc_switch_part(parttype) @@ -1149,30 +1181,34 @@ class DALegacy(metaclass=LogBase): return True return False - def readflash(self, addr, length, filename, parttype=None, display=True): + def get_parttype(self, length, parttype): if parttype is None or parttype == "user" or parttype == "": - length = min(length, self.emmc["m_emmc_ua_size"]) + length = min(length, self.emmc.m_emmc_ua_size) parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_USER elif parttype == "boot1": - length = min(length, self.emmc["m_emmc_boot1_size"]) + length = min(length, self.emmc.m_emmc_boot1_size) parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_BOOT1 elif parttype == "boot2": - length = min(length, self.emmc["m_emmc_boot2_size"]) + length = min(length, self.emmc.m_emmc_boot2_size) parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_BOOT2 elif parttype == "gp1": - length = min(length, self.emmc["m_emmc_gp_size"]) + length = min(length, self.emmc.m_emmc_gp_size) parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP1 elif parttype == "gp2": - length = min(length, self.emmc["m_emmc_gp_size"]) + length = min(length, self.emmc.m_emmc_gp_size) parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP2 elif parttype == "gp3": - length = min(length, self.emmc["m_emmc_gp_size"]) + length = min(length, self.emmc.m_emmc_gp_size) parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP3 elif parttype == "gp4": - length = min(length, self.emmc["m_emmc_gp_size"]) + length = min(length, self.emmc.m_emmc_gp_size) parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_GP4 elif parttype == "rpmb": parttype = EMMC_PartitionType.MTK_DA_EMMC_PART_RPMB + return length, parttype + + def readflash(self, addr, length, filename, parttype=None, display=True): + length, parttype = self.get_parttype(length, parttype) self.check_usb_cmd() packetsize = 0x0 if self.daconfig.flashtype == "emmc": @@ -1210,7 +1246,7 @@ class DALegacy(metaclass=LogBase): self.daconfig.readsize = self.daconfig.flashsize // self.daconfig.pagesize * ( self.daconfig.pagesize + self.daconfig.sparesize) if display: - self.progress.show_progress("Read",0,100,display) + self.progress.show_progress("Read", 0, 100, display) if filename != "": with open(filename, "wb") as wf: bytestoread = length @@ -1220,10 +1256,10 @@ class DALegacy(metaclass=LogBase): size = packetsize wf.write(self.usbread(size)) bytestoread -= size - checksum = unpack(">H", self.usbread(1)+self.usbread(1))[0] + checksum = unpack(">H", self.usbread(1) + self.usbread(1))[0] self.debug("Checksum: %04X" % checksum) self.usbwrite(self.Rsp.ACK) - self.progress.show_progress("Read", length-bytestoread, length, display) + self.progress.show_progress("Read", length - bytestoread, length, display) self.progress.show_progress("Read", 100, 100, display) return True else: @@ -1238,6 +1274,6 @@ class DALegacy(metaclass=LogBase): checksum = unpack(">H", self.usbread(2))[0] self.debug("Checksum: %04X" % checksum) self.usbwrite(self.Rsp.ACK) - self.progress.show_progress("Read", length-bytestoread, length, display) + self.progress.show_progress("Read", length - bytestoread, length, display) self.progress.show_progress("Read", 100, 100, display) return buffer diff --git a/mtkclient/Library/mtk_daxflash.py b/mtkclient/Library/mtk_daxflash.py index 9b4b066..e112259 100755 --- a/mtkclient/Library/mtk_daxflash.py +++ b/mtkclient/Library/mtk_daxflash.py @@ -885,7 +885,7 @@ class DAXFlash(metaclass=LogBase): dsize = min(write_packet_size, bytestowrite) if fh: data = bytearray(fh.read(dsize)) - if len(data)<0x200: + if len(data)