From 2e65679cc8502df42eb03e4b09c978313b6a6368 Mon Sep 17 00:00:00 2001 From: hopez13 <140780838+hopez13@users.noreply.github.com> Date: Sun, 15 Sep 2024 01:30:13 +0530 Subject: [PATCH 1/3] Fix preloader handshake --- mtkclient/Library/Port.py | 77 +++++++++++++----------------- mtkclient/Library/mtk_preloader.py | 3 -- 2 files changed, 32 insertions(+), 48 deletions(-) diff --git a/mtkclient/Library/Port.py b/mtkclient/Library/Port.py index 216ad8f..7391e06 100755 --- a/mtkclient/Library/Port.py +++ b/mtkclient/Library/Port.py @@ -125,58 +125,45 @@ class Port(metaclass=LogBase): pass return False - def run_handshake(self): - ep_out = self.cdc.EP_OUT.write - ep_in = self.cdc.EP_IN.read - maxinsize = self.cdc.EP_IN.wMaxPacketSize - - i = 0 - startcmd = b"\xa0\x0a\x50\x05" - length = len(startcmd) - # On preloader, send 0xa0 first - if self.cdc.pid!=0x3: - ep_out(startcmd[0:1]) - try: - while i < length: - if ep_out(startcmd[i:i+1]): - if ep_in(maxinsize)[-1] == ~(startcmd[i]) & 0xFF: - i += 1 - else: - i = 0 - self.info("Device detected :)") - return True - except Exception as serr: - self.debug(str(serr)) - time.sleep(0.005) - return False + # def run_handshake(self): + + + def handshake(self, maxtries=None, loop=0): counter = 0 - while not self.cdc.connected: try: if maxtries is not None and counter == maxtries: break - counter += 1 - if self.cdc.connect() and self.run_handshake(): - return True - else: - if loop == 5: - sys.stdout.write('\n') - self.info("Hint:\n\nPower off the phone before connecting.\n" + - "For brom mode, press and hold vol up, vol dwn, or all hw buttons and " + - "connect usb.\n" + - "For preloader mode, don't press any hw button and connect usb.\n" - "If it is already connected and on, hold power for 10 seconds to reset.\n") - sys.stdout.write('\n') - if loop >= 10: - sys.stdout.write('.') - if loop >= 20: - sys.stdout.write('\n') - loop = 0 - loop += 1 - time.sleep(0.3) - sys.stdout.flush() + self.cdc.connect() + ep_out = self.cdc.EP_OUT.write + ep_in = self.cdc.EP_IN.read + maxinsize = self.cdc.EP_IN.wMaxPacketSize + + i = 0 + startcmd = b"\xa0\x0a\x50\x05" + length = len(startcmd) + # On preloader, send 0xa0 first + if self.cdc.pid!=0x3: + ep_out(startcmd[0:1]) + try: + while i < length: + if ep_out(startcmd[i:i+1]): + if ep_in(maxinsize)[-1] == ~(startcmd[i]) & 0xFF: + i += 1 + else: + i = 0 + + + self.info("Device detected :)") + return True + + except Exception as serr: + self.debug(str(serr)) + time.sleep(0.005) + return False + except Exception as serr: if "access denied" in str(serr): diff --git a/mtkclient/Library/mtk_preloader.py b/mtkclient/Library/mtk_preloader.py index 03c5472..1b234c3 100755 --- a/mtkclient/Library/mtk_preloader.py +++ b/mtkclient/Library/mtk_preloader.py @@ -160,9 +160,6 @@ class Preloader(metaclass=LogBase): else: res = self.mtk.port.handshake(maxtries=maxtries) if not res: - if display: - self.error("Status: Handshake failed, retrying...") - self.config.set_gui_status(self.config.tr("Status: Handshake failed, retrying...")) self.mtk.port.close() tries += 1 if tries == 1000: From 35c5803ae0bb3916801abdc60a0c6272cd415ba7 Mon Sep 17 00:00:00 2001 From: hopez13 <140780838+hopez13@users.noreply.github.com> Date: Sun, 15 Sep 2024 01:52:34 +0530 Subject: [PATCH 2/3] Update Port.py --- mtkclient/Library/Port.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mtkclient/Library/Port.py b/mtkclient/Library/Port.py index 7391e06..9bd7450 100755 --- a/mtkclient/Library/Port.py +++ b/mtkclient/Library/Port.py @@ -136,6 +136,7 @@ class Port(metaclass=LogBase): try: if maxtries is not None and counter == maxtries: break + counter += 1 self.cdc.connect() ep_out = self.cdc.EP_OUT.write ep_in = self.cdc.EP_IN.read From e3cd58a837e1df49a3f64bac29451f7239fa0079 Mon Sep 17 00:00:00 2001 From: hopez13 <140780838+hopez13@users.noreply.github.com> Date: Mon, 16 Sep 2024 21:06:29 +0530 Subject: [PATCH 3/3] Fix Preloader Handshake In Debug Mode --- mtkclient/Library/Connection/usblib.py | 2 +- mtkclient/Library/Port.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mtkclient/Library/Connection/usblib.py b/mtkclient/Library/Connection/usblib.py index 0ba7887..4136b12 100755 --- a/mtkclient/Library/Connection/usblib.py +++ b/mtkclient/Library/Connection/usblib.py @@ -309,7 +309,7 @@ class UsbClass(DeviceClass): break if self.device is None: - self.debug("Couldn't detect the device. Is it connected ?") + # self.debug("Couldn't detect the device. Is it connected ?") return False try: diff --git a/mtkclient/Library/Port.py b/mtkclient/Library/Port.py index 9bd7450..ffc2148 100755 --- a/mtkclient/Library/Port.py +++ b/mtkclient/Library/Port.py @@ -169,7 +169,7 @@ class Port(metaclass=LogBase): except Exception as serr: if "access denied" in str(serr): self.warning(str(serr)) - self.debug(str(serr)) + # self.debug(str(serr)) pass return False