mirror of
https://github.com/bkerler/mtkclient.git
synced 2024-11-14 19:25:05 -05:00
Improve read handling and fix write on mtk da legacy
This commit is contained in:
parent
5714a30b43
commit
7c940e8443
2 changed files with 117 additions and 47 deletions
|
@ -14,6 +14,36 @@ from mtkclient.Library.partition import Partition
|
||||||
from mtkclient.config.payloads import pathconfig
|
from mtkclient.config.payloads import pathconfig
|
||||||
from mtkclient.Library.legacy_ext import legacyext
|
from mtkclient.Library.legacy_ext import legacyext
|
||||||
from mtkclient.config.mtk_config import Mtk_Config
|
from mtkclient.config.mtk_config import Mtk_Config
|
||||||
|
from queue import Queue
|
||||||
|
from threading import Thread, Event
|
||||||
|
|
||||||
|
rq = Queue()
|
||||||
|
wq = Queue()
|
||||||
|
|
||||||
|
def writedata(filename,rq,event):
|
||||||
|
pos=0
|
||||||
|
with open(filename, "wb") as wf:
|
||||||
|
while True:
|
||||||
|
if event.is_set():
|
||||||
|
break
|
||||||
|
data = rq.get()
|
||||||
|
pos+=len(data)
|
||||||
|
wf.write(data)
|
||||||
|
rq.task_done()
|
||||||
|
|
||||||
|
def readdata(filename,wq,pagesize,event):
|
||||||
|
pos=0
|
||||||
|
with open(filename, "rb") as rf:
|
||||||
|
bytestoread=os.stat(filename).st_size
|
||||||
|
while bytestoread>0:
|
||||||
|
if event.is_set():
|
||||||
|
break
|
||||||
|
data = rf.read(pagesize)
|
||||||
|
size = len(data)
|
||||||
|
wq.put(data)
|
||||||
|
pos += size
|
||||||
|
bytestoread-=size
|
||||||
|
wq.task_done()
|
||||||
|
|
||||||
|
|
||||||
class norinfo:
|
class norinfo:
|
||||||
|
@ -1097,8 +1127,8 @@ class DALegacy(metaclass=LogBase):
|
||||||
if speed[0] == 0: # 1 = USB High Speed, 2= USB Ultra high speed
|
if speed[0] == 0: # 1 = USB High Speed, 2= USB Ultra high speed
|
||||||
self.info("Reconnecting to preloader")
|
self.info("Reconnecting to preloader")
|
||||||
self.set_usb_cmd()
|
self.set_usb_cmd()
|
||||||
self.mtk.port.close(reset=False)
|
self.mtk.port.close(reset=True)
|
||||||
time.sleep(1)
|
time.sleep(2)
|
||||||
while not self.mtk.port.cdc.connect():
|
while not self.mtk.port.cdc.connect():
|
||||||
self.info("Waiting for reconnection")
|
self.info("Waiting for reconnection")
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
@ -1173,7 +1203,7 @@ class DALegacy(metaclass=LogBase):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def set_usb_cmd(self):
|
def set_usb_cmd(self):
|
||||||
if self.usbwrite(self.Cmd.USB_SETUP_PORT): # 72
|
if self.usbwrite(self.Cmd.USB_SETUP_PORT): # 70
|
||||||
if self.usbwrite(b"\x01"): # USB_HIGH_SPEED
|
if self.usbwrite(b"\x01"): # USB_HIGH_SPEED
|
||||||
res = self.usbread(1)
|
res = self.usbread(1)
|
||||||
if len(res) > 0:
|
if len(res) > 0:
|
||||||
|
@ -1262,7 +1292,7 @@ class DALegacy(metaclass=LogBase):
|
||||||
self.usbwrite(b"\x08") # EMMC_PART_USER
|
self.usbwrite(b"\x08") # EMMC_PART_USER
|
||||||
self.usbwrite(pack(">Q", addr))
|
self.usbwrite(pack(">Q", addr))
|
||||||
self.usbwrite(pack(">Q", length))
|
self.usbwrite(pack(">Q", length))
|
||||||
self.usbwrite(b"\x08") # index 8
|
self.usbwrite(b"\x06") # index 8
|
||||||
self.usbwrite(b"\x03")
|
self.usbwrite(b"\x03")
|
||||||
packetsize = unpack(">I", self.usbread(4))[0]
|
packetsize = unpack(">I", self.usbread(4))[0]
|
||||||
ack = unpack(">B", self.usbread(1))[0]
|
ack = unpack(">B", self.usbread(1))[0]
|
||||||
|
@ -1366,6 +1396,7 @@ class DALegacy(metaclass=LogBase):
|
||||||
return length, parttype
|
return length, parttype
|
||||||
|
|
||||||
def readflash(self, addr, length, filename, parttype=None, display=True):
|
def readflash(self, addr, length, filename, parttype=None, display=True):
|
||||||
|
global rq
|
||||||
self.mtk.daloader.progress.clear()
|
self.mtk.daloader.progress.clear()
|
||||||
length, parttype = self.get_parttype(length, parttype)
|
length, parttype = self.get_parttype(length, parttype)
|
||||||
self.check_usb_cmd()
|
self.check_usb_cmd()
|
||||||
|
@ -1409,24 +1440,28 @@ class DALegacy(metaclass=LogBase):
|
||||||
if display:
|
if display:
|
||||||
self.mtk.daloader.progress.show_progress("Read", 0, length, display)
|
self.mtk.daloader.progress.show_progress("Read", 0, length, display)
|
||||||
if filename != "":
|
if filename != "":
|
||||||
with open(filename, "wb") as wf:
|
event = Event()
|
||||||
bytestoread = length
|
worker = Thread(target=writedata, args=(filename, rq, event), daemon=True)
|
||||||
while bytestoread > 0:
|
worker.start()
|
||||||
size = bytestoread
|
bytestoread = length
|
||||||
if bytestoread > packetsize:
|
while bytestoread > 0:
|
||||||
size = packetsize
|
size = bytestoread
|
||||||
wf.write(self.usbread(size))
|
if bytestoread > packetsize:
|
||||||
bytestoread -= size
|
size = packetsize
|
||||||
checksum = unpack(">H", self.usbread(1) + self.usbread(1))[0]
|
data=self.usbread(size)
|
||||||
self.debug("Checksum: %04X" % checksum)
|
bytestoread -= len(data)
|
||||||
self.usbwrite(self.Rsp.ACK)
|
rq.put(data)
|
||||||
if length > bytestoread:
|
checksum = unpack(">H", self.usbread(1) + self.usbread(1))[0]
|
||||||
rpos = length - bytestoread
|
self.debug("Checksum: %04X" % checksum)
|
||||||
else:
|
self.usbwrite(self.Rsp.ACK)
|
||||||
rpos = 0
|
if length > bytestoread:
|
||||||
self.mtk.daloader.progress.show_progress("Read", rpos, length, display)
|
rpos = length - bytestoread
|
||||||
self.mtk.daloader.progress.show_progress("Read", length, length, display)
|
else:
|
||||||
return True
|
rpos = 0
|
||||||
|
self.mtk.daloader.progress.show_progress("Read", rpos, length, display)
|
||||||
|
self.mtk.daloader.progress.show_progress("Read", length, length, display)
|
||||||
|
event.set()
|
||||||
|
return True
|
||||||
else:
|
else:
|
||||||
buffer = bytearray()
|
buffer = bytearray()
|
||||||
bytestoread = length
|
bytestoread = length
|
||||||
|
|
|
@ -14,7 +14,36 @@ from mtkclient.Library.partition import Partition
|
||||||
from mtkclient.config.payloads import pathconfig
|
from mtkclient.config.payloads import pathconfig
|
||||||
from mtkclient.Library.xflash_ext import xflashext, XCmd
|
from mtkclient.Library.xflash_ext import xflashext, XCmd
|
||||||
from mtkclient.Library.settings import hwparam
|
from mtkclient.Library.settings import hwparam
|
||||||
|
from queue import Queue
|
||||||
|
from threading import Thread, Event
|
||||||
|
|
||||||
|
rq = Queue()
|
||||||
|
wq = Queue()
|
||||||
|
|
||||||
|
def writedata(filename,rq,event):
|
||||||
|
pos=0
|
||||||
|
with open(filename, "wb") as wf:
|
||||||
|
while True:
|
||||||
|
if event.is_set():
|
||||||
|
break
|
||||||
|
data = rq.get()
|
||||||
|
pos+=len(data)
|
||||||
|
wf.write(data)
|
||||||
|
rq.task_done()
|
||||||
|
|
||||||
|
def readdata(filename,wq,pagesize,event):
|
||||||
|
pos=0
|
||||||
|
with open(filename, "rb") as rf:
|
||||||
|
bytestoread=os.stat(filename).st_size
|
||||||
|
while bytestoread>0:
|
||||||
|
if event.is_set():
|
||||||
|
break
|
||||||
|
data = rf.read(pagesize)
|
||||||
|
size = len(data)
|
||||||
|
wq.put(data)
|
||||||
|
pos += size
|
||||||
|
bytestoread-=size
|
||||||
|
wq.task_done()
|
||||||
|
|
||||||
class NandExtension:
|
class NandExtension:
|
||||||
# uni=0, multi=1
|
# uni=0, multi=1
|
||||||
|
@ -848,6 +877,7 @@ class DAXFlash(metaclass=LogBase):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def readflash(self, addr, length, filename, parttype=None, display=True):
|
def readflash(self, addr, length, filename, parttype=None, display=True):
|
||||||
|
global rq
|
||||||
partinfo = self.getstorage(parttype, length)
|
partinfo = self.getstorage(parttype, length)
|
||||||
if not partinfo:
|
if not partinfo:
|
||||||
return None
|
return None
|
||||||
|
@ -859,34 +889,39 @@ class DAXFlash(metaclass=LogBase):
|
||||||
bytestoread = length
|
bytestoread = length
|
||||||
total = length
|
total = length
|
||||||
if filename != "":
|
if filename != "":
|
||||||
with open(filename, "wb") as wf:
|
event = Event()
|
||||||
while bytestoread > 0:
|
worker = Thread(target=writedata, args=(filename, rq, event), daemon=True)
|
||||||
status = self.usbread(4 + 4 + 4)
|
worker.start()
|
||||||
magic, datatype, slength = unpack("<III", status)
|
resdata = bytearray()
|
||||||
if magic == 0xFEEEEEEF:
|
while bytestoread > 0:
|
||||||
resdata = self.usbread(slength)
|
|
||||||
if slength > 4:
|
|
||||||
wf.write(resdata)
|
|
||||||
stmp = pack("<III", self.Cmd.MAGIC, self.DataType.DT_PROTOCOL_FLOW, 4)
|
|
||||||
data = pack("<I", 0)
|
|
||||||
self.usbwrite(stmp)
|
|
||||||
self.usbwrite(data)
|
|
||||||
bytestoread -= len(resdata)
|
|
||||||
bytesread += len(resdata)
|
|
||||||
if display:
|
|
||||||
self.mtk.daloader.progress.show_progress("Read", bytesread, total, display)
|
|
||||||
elif slength == 4:
|
|
||||||
if unpack("<I", resdata)[0] != 0:
|
|
||||||
break
|
|
||||||
status = self.usbread(4 + 4 + 4)
|
status = self.usbread(4 + 4 + 4)
|
||||||
magic, datatype, slength = unpack("<III", status)
|
magic, datatype, slength = unpack("<III", status)
|
||||||
if magic == 0xFEEEEEEF:
|
if magic == 0xFEEEEEEF:
|
||||||
resdata = self.usbread(slength)
|
resdata = self.usbread(slength)
|
||||||
if slength == 4:
|
if len(resdata) > 4:
|
||||||
if unpack("<I", resdata)[0] == 0:
|
rq.put(resdata)
|
||||||
if display:
|
stmp = pack("<III", self.Cmd.MAGIC, self.DataType.DT_PROTOCOL_FLOW, 4)
|
||||||
self.mtk.daloader.progress.show_progress("Read", total, total, display)
|
data = pack("<I", 0)
|
||||||
return True
|
self.usbwrite(stmp)
|
||||||
|
self.usbwrite(data)
|
||||||
|
bytestoread -= len(resdata)
|
||||||
|
bytesread += len(resdata)
|
||||||
|
if display:
|
||||||
|
self.mtk.daloader.progress.show_progress("Read", bytesread, total, display)
|
||||||
|
elif slength == 4:
|
||||||
|
if unpack("<I", resdata)[0] != 0:
|
||||||
|
break
|
||||||
|
status = self.usbread(4 + 4 + 4)
|
||||||
|
magic, datatype, slength = unpack("<III", status)
|
||||||
|
if magic == 0xFEEEEEEF:
|
||||||
|
resdata = self.usbread(slength)
|
||||||
|
if slength == 4:
|
||||||
|
if unpack("<I", resdata)[0] == 0:
|
||||||
|
if display:
|
||||||
|
self.mtk.daloader.progress.show_progress("Read", total, total, display)
|
||||||
|
event.set()
|
||||||
|
return True
|
||||||
|
event.set()
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
buffer = bytearray()
|
buffer = bytearray()
|
||||||
|
@ -1135,7 +1170,7 @@ class DAXFlash(metaclass=LogBase):
|
||||||
self.info("Reconnecting to preloader")
|
self.info("Reconnecting to preloader")
|
||||||
self.config.set_gui_status(self.config.tr("Reconnecting to preloader"))
|
self.config.set_gui_status(self.config.tr("Reconnecting to preloader"))
|
||||||
self.set_usb_speed()
|
self.set_usb_speed()
|
||||||
self.mtk.port.close(reset=False)
|
self.mtk.port.close(reset=True)
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
while not self.mtk.port.cdc.connect():
|
while not self.mtk.port.cdc.connect():
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
|
Loading…
Reference in a new issue