diff --git a/xperia-firmware-upgrade/5g-policyman/efs2.py b/xperia-firmware-upgrade/5g-policyman/efs2.py index b949df1..c43c4a2 100644 --- a/xperia-firmware-upgrade/5g-policyman/efs2.py +++ b/xperia-firmware-upgrade/5g-policyman/efs2.py @@ -4,9 +4,7 @@ Packet layouts and opcodes replicate JohnBel/EfsTools exactly. Used to back up and replace policyman RF-band config on a rooted Sony Xperia 1 VI. """ -import sys, os, time, struct, serial - -PORT = os.environ.get("DIAGPORT", "/dev/ttyUSB0") +import sys, os, time, struct, glob, serial DIAG_SUBSYS = 0x4B SUBSYS_EFS = 19 @@ -50,9 +48,57 @@ def hdlc_decode(frame): else: out.append(b) return bytes(out) +def _responds_to_diag(port, timeout=1.0): + """Return True if port answers a DIAG version request (cmd 0x00).""" + try: + s = serial.Serial(port, 115200, timeout=0.3) + except Exception: + return False + try: + s.reset_input_buffer() + s.write(hdlc_encode([0x00])) + buf, deadline = bytearray(), time.time() + timeout + while time.time() < deadline: + b = s.read(1) + if not b: + continue + if b[0] == 0x7E: + if buf: + dec = hdlc_decode(bytes(buf)) + buf = bytearray() + if len(dec) >= 3 and dec[0] == 0x00 and \ + fcs16(dec[:-2]) == struct.unpack("