From c3eb4b1183c9ca3698994f5fb3b27b4cb9705ab7 Mon Sep 17 00:00:00 2001 From: Kshitij Gupta Date: Wed, 5 Jun 2024 17:16:03 +0530 Subject: [PATCH] mtkclient: Fixes for serialport - Allow arg to be empty: autodetect serialport - If arg is specified, autodetect and then filter - Preloader/BROM handshake frame is very short, attempt cdc.connect() on each 0.1s iteration - Add serialport arg to more commands --- mtk | 27 +++++++------ mtkclient/Library/Connection/seriallib.py | 46 ++++++++++++++--------- mtkclient/Library/Port.py | 2 + 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/mtk b/mtk index e754d93c..7b2662c2 100755 --- a/mtk +++ b/mtk @@ -872,18 +872,21 @@ if __name__ == '__main__': parser_gpt.add_argument('--generatekeys', action="store_true", help='Option for deriving hw keys') parser_r.add_argument('--generatekeys', action="store_true", help='Option for deriving hw keys') - parser_printgpt.add_argument('--serialport', help='Use serial port') - parser_footer.add_argument('--serialport', help='Use serial port') - parser_e.add_argument('--serialport', help='Use serial port') - parser_es.add_argument('--serialport', help='Use serial port') - parser_wl.add_argument('--serialport', help='Use serial port') - parser_wf.add_argument('--serialport', help='Use serial port') - parser_w.add_argument('--serialport', help='Use serial port') - parser_rs.add_argument('--serialport', help='Use serial port') - parser_rf.add_argument('--serialport', help='Use serial port') - parser_rl.add_argument('--serialport', help='Use serial port') - parser_gpt.add_argument('--serialport', help='Use serial port') - parser_r.add_argument('--serialport', help='Use serial port') + parser_printgpt.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_footer.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_e.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_es.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_wl.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_wf.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_w.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_rs.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_rf.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_rl.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_gpt.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_r.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_reset.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_payload.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') + parser_script.add_argument('--serialport', help='Use serial port', default=None, const='DETECT', action='store', type=str, nargs='?') parser_script.add_argument('--noreconnect', action="store_true", help='Disable reconnect') parser_printgpt.add_argument('--noreconnect', action="store_true", help='Disable reconnect') diff --git a/mtkclient/Library/Connection/seriallib.py b/mtkclient/Library/Connection/seriallib.py index a7bbd358..f3763c73 100755 --- a/mtkclient/Library/Connection/seriallib.py +++ b/mtkclient/Library/Connection/seriallib.py @@ -33,25 +33,36 @@ def connect(self, EP_IN=-1, EP_OUT=-1): if self.connected: self.close() self.connected = False - if self.portname is None: - devices = self.detectdevices() - if len(devices) > 0: - self.portname = devices[0] - elif self.portname is not None: - self.device = serial.Serial(baudrate=115200, bytesize=serial.EIGHTBITS, + + ports = self.detectdevices() + if ports: + port = None + if self.portname is not "DETECT": + if self.portname not in ports: + self.debug("{} not in detected ports: {}".format(self.portname, ports)) + return False + else: + port = ports[ports.index(self.portname)] + else: + port = ports[0] + self.debug("Got port: {}, initializing".format(port)) + self.device = serial.Serial(port=port, baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=500, xonxoff=False, dsrdtr=False, rtscts=False) - self.device._reset_input_buffer = _reset_input_buffer - self.device.setPort(port=self.portname) - try: - self.device.open() - except Exception: - pass - self.device._reset_input_buffer = _reset_input_buffer_org - self.connected = self.device.is_open - if self.connected: - return True + self.portname = port + else: + return False + self.device._reset_input_buffer = _reset_input_buffer + try: + self.device.open() + except Exception as e: + self.debug(e) + pass + self.device._reset_input_buffer = _reset_input_buffer_org + self.connected = self.device.is_open + if self.connected: + return True return False def setportname(self, portname: str): @@ -87,8 +98,7 @@ def detectdevices(self): for port in serial.tools.list_ports.comports(): for usbid in self.portconfig: if port.pid == usbid[1] and port.vid == usbid[0]: - # portid = port.location[-1:] - print(f"Detected {hex(port.vid)}:{hex(port.pid)} device at: {port.device}") + self.debug(f"Detected {hex(port.vid)}:{hex(port.pid)} device at: {port.device}") ids.append(port.device) return sorted(ids) diff --git a/mtkclient/Library/Port.py b/mtkclient/Library/Port.py index fa3cae13..d4dd742d 100755 --- a/mtkclient/Library/Port.py +++ b/mtkclient/Library/Port.py @@ -89,6 +89,8 @@ def serial_handshake(self, maxtries=None, loop=0): self.cdc.connected = self.cdc.connect() while 1: # Workaround for serial port try: + if not self.cdc.connected: + self.cdc.connected = self.cdc.connect() if maxtries is not None and counter == maxtries: break counter += 1