From b42dc4c3f19fae2ff8bf64a88739d3fdabefc9ec Mon Sep 17 00:00:00 2001 From: James Coleman Date: Thu, 27 Aug 2026 16:09:10 -0500 Subject: [PATCH] Auto-detect the diag port in efs2.py Probe each candidate serial port with a DIAG version request and keep the one that answers, interface 0 (-if00-) first, instead of hardcoding ttyUSB0. Use /dev/serial/by-id for stable naming; DIAGPORT overrides. Claude-Session: https://claude.ai/code/session_01Ctisr9uXe4H8XsscZ2exGE --- xperia-firmware-upgrade/5g-policyman/efs2.py | 57 ++++++++++++++++++-- xperia-firmware-upgrade/SKILL.md | 10 ++-- 2 files changed, 58 insertions(+), 9 deletions(-) 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("