Skip to content

Commit

Permalink
mtkclient: Fixes for serialport
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
AgentFabulous committed Jun 5, 2024
1 parent 54cd658 commit c3eb4b1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
27 changes: 15 additions & 12 deletions mtk
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
46 changes: 28 additions & 18 deletions mtkclient/Library/Connection/seriallib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions mtkclient/Library/Port.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c3eb4b1

Please sign in to comment.