-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmtkbootcmd.py
More file actions
56 lines (46 loc) · 1.55 KB
/
Copy pathmtkbootcmd.py
File metadata and controls
56 lines (46 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import sys
import time
import serial
import serial.tools.list_ports
if len(sys.argv) < 2:
print("Usage: mtkbootcmd.py <BOOTMODE>")
sys.exit(1)
PLBROM_HWID = "VID:PID=0E8D:2000"
BOOTMODE = bytes(sys.argv[1], "ascii")
READYCMD = b"READY"
def serial_port():
ports = list(serial.tools.list_ports.comports())
for port in ports:
if PLBROM_HWID in port.hwid:
print("Found {} with description: {}\nHWID: {}".format(port.device, port.description, port.hwid))
return port.device
return None
if __name__ == '__main__':
print('Listening for ports!')
abort = False
while not abort:
time.sleep(1)
port = serial_port()
if port is not None:
print('PORT:', port)
print('Initializing port:', port)
ser = serial.Serial(port=port, baudrate=115200)
try:
resp = ser.read(5) # Expect 5 bytes, since the output will be “READY” if successful
if resp == READYCMD:
ser.write(BOOTMODE)
print(f"{BOOTMODE} cmd sent")
abort = True
break
else:
print(f"Error: {resp}")
sys.exit(2)
except serial.SerialException as e:
print(f"Serial error: {e}")
sys.exit(3)
except Exception as e:
print(f"Unexpected error: {e}")
sys.exit(4)
finally:
if ser.is_open:
ser.close()