8000 Fix Windows USB timeouts and Sahara HELLO handling by Xblur · Pull Request #841 · bkerler/edl · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions edl.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ def run(self):
version = conninfo.get("data").version
except AttributeError:
version = 2
# sahara.connect() reports END_TRANSFER with mode="error"; check cmd
# before mode dispatch so reset still runs.
if conninfo.get("cmd") == cmd_t.SAHARA_END_TRANSFER:
print("Sahara reported END_TRANSFER (not HELLO). Reboot into EDL and retry immediately.")
try:
self.sahara.cmd_reset()
except Exception:
pass
return self.exit(1)
if mode == "sahara":
cmd = conninfo["cmd"]
if cmd == cmd_t.SAHARA_HELLO_REQ:
Expand Down
26 changes: 18 additions & 8 deletions edlclient/Library/Connection/usblib.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ def __init__(self, loglevel=logging.INFO, portconfig=None, devclass=-1, serial_n
if sys.platform.startswith('freebsd') or sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
self.backend = usb.backend.libusb1.get_backend(find_library=lambda x: "libusb-1.0.so")
elif is_windows():
# Let pyusb pick the backend for the bound Windows driver
# (WinUSB/libusb1 or libusb-win32/libusb0).
self.backend = None
if self.backend is not None:
# libusb_set_option exists only on libusb1; never clear backend on failure
# (clearing the backend can force a mismatched backend and crash).
if self.backend is not None and self.backend.__class__.__module__.endswith("libusb1"):
try:
self.backend.lib.libusb_set_option.argtypes = [c_void_p, c_int]
self.backend.lib.libusb_set_option(self.backend.ctx, 1)
except:
self.backend = None
except Exception:
pass

def load_windows_dll(self):
if os.name == 'nt':
Expand Down Expand Up @@ -388,23 +392,29 @@ def usbread(self, resplen=None, timeout=0):
buffer = self.buffer[:resplen]
epr = self.EP_IN.read
extend = res.extend
# Retry count must be independent of the timeout value in ms.
# Callers such as Sahara use timeout=1000; the old "timeout == 10"
# exit never fired and could hang forever on "Device detected".
timeout_retries = 0
max_timeout_retries = 10
while len(res) < resplen:
try:
resplen = epr(buffer, timeout)
extend(buffer[:resplen])
if resplen == self.EP_IN.wMaxPacketSize:
break
except usb.core.USBError as e:
error = str(e.strerror)
if "timed out" in error:
error = str(e.strerror).lower() if e.strerror else str(e).lower()
# libusb1: "timed out"; libusb0-win32: "timeout error"
if "timed out" in error or "timeout" in error:
if timeout is None:
return b""
self.debug("Timed out")
if timeout == 10:
timeout_retries += 1
if timeout_retries >= max_timeout_retries:
return b""
timeout += 1
pass
elif "Overflow" in error:
elif "Overflow" in error or "overflow" in error:
self.error("USB Overflow")
return b""
else:
Expand Down
25 changes: 19 additions & 6 deletions edlclient/Library/sahara.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,12 @@ def cmd_hello(self, mode, version_min=1, max_cmd_len=0, version=2): # CMD 0x1,

def connect(self):
try:
v = self.cdc.read(length=0xC * 0x4, timeout=1)
# pyusb timeouts are milliseconds; pyserial timeouts are seconds.
# USB needs ~1000 ms for HELLO; serial must stay at 1 second.
hello_timeout = 1 if getattr(self.cdc, "is_serial", False) else 1000
v = self.cdc.read(length=0xC * 0x4, timeout=hello_timeout)
if len(v) > 1:
if v[0] == 0x01:
if v[0] == 0x01 or v[0] == cmd_t.SAHARA_END_TRANSFER:
pkt = self.ch.pkt_cmd_hdr(v)
if pkt.cmd == cmd_t.SAHARA_HELLO_REQ:
rsp = self.ch.pkt_hello_req(v)
Expand All @@ -127,27 +130,37 @@ def connect(self):
return {"mode": "sahara", "cmd": cmd_t.SAHARA_HELLO_REQ, "data": rsp}
elif pkt.cmd == cmd_t.SAHARA_END_TRANSFER:
rsp = self.ch.pkt_image_end(v)
return {"mode": "sahara", "cmd": cmd_t.SAHARA_END_TRANSFER, "data": rsp}
self.error(self.get_error_desc(rsp.image_tx_status))
return {"mode": "error", "cmd": cmd_t.SAHARA_END_TRANSFER, "data": rsp}
elif b"<?xml" in v:
return {"mode": "firehose"}
elif v[0] == 0x7E:
return {"mode": "nandprg"}
else:
data = b"<?xml version=\"1.0\" ?><data><nop /></data>"
self.cdc.write(data)
res = self.cdc.read(timeout=1)
res = self.cdc.read(timeout=hello_timeout)
if b"<?xml" in res:
return {"mode": "firehose"}
elif len(res) > 0:
if res[0] == 0x7E:
return {"mode": "nandprg"}
elif res[0] == cmd_t.SAHARA_END_TRANSFER:
rsp = self.ch.pkt_image_end(res)
return {"mode": "sahara", "cmd": cmd_t.SAHARA_END_TRANSFER, "data": rsp}
self.error(self.get_error_desc(rsp.image_tx_status))
return {"mode": "error", "cmd": cmd_t.SAHARA_END_TRANSFER, "data": rsp}
elif res[0] == 0x01:
pkt = self.ch.pkt_cmd_hdr(res)
if pkt.cmd == cmd_t.SAHARA_HELLO_REQ:
rsp = self.ch.pkt_hello_req(res)
self.pktsize = rsp.cmd_packet_length
self.version = rsp.version
self.info(f"Protocol version: {rsp.version}, Version supported: {rsp.version_supported}")
return {"mode": "sahara", "cmd": cmd_t.SAHARA_HELLO_REQ, "data": rsp}
elif res == b"":
data = b"\x7E\x11\x00\x12\x00\xA0\xE3\x00\x00\xC1\xE5\x01\x40\xA0\xE3\x1E\xFF\x2F\xE1\x4B\xD9\x7E"
self.cdc.write(data)
res = self.cdc.read()
res = self.cdc.read(timeout=hello_timeout)
if len(res) > 0 and res[1] == 0x12:
return {"mode": "nandprg"}
elif len(res) == 0:
Expand Down
0