mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-23 07:58:09 +00:00
transport: consolidate USB-based transports
remove Trezor 2 support from HID transport, which never worked use ProtocolV1 explicitly everywhere, as V2 doesn't exist in practice move USB IDs and UDEV warning string to a common place fix a bug where HID would return a list instead of bytes
This commit is contained in:
parent
d3534a15c9
commit
5bb7dc39b8
@ -26,6 +26,18 @@ if False:
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
# USB vendor/product IDs for Trezors
|
||||
DEV_TREZOR1 = (0x534C, 0x0001)
|
||||
DEV_TREZOR2 = (0x1209, 0x53C1)
|
||||
DEV_TREZOR2_BL = (0x1209, 0x53C0)
|
||||
|
||||
TREZORS = {DEV_TREZOR1, DEV_TREZOR2, DEV_TREZOR2_BL}
|
||||
|
||||
UDEV_RULES_STR = """
|
||||
Do you have udev rules installed?
|
||||
https://github.com/trezor/trezor-common/blob/master/udev/51-trezor.rules
|
||||
""".strip()
|
||||
|
||||
|
||||
class TransportException(Exception):
|
||||
pass
|
||||
|
@ -20,12 +20,8 @@ from typing import Any, Dict, Iterable
|
||||
|
||||
import hid
|
||||
|
||||
from . import TransportException
|
||||
from .protocol import ProtocolBasedTransport, get_protocol
|
||||
|
||||
DEV_TREZOR1 = (0x534C, 0x0001)
|
||||
DEV_TREZOR2 = (0x1209, 0x53C1)
|
||||
DEV_TREZOR2_BL = (0x1209, 0x53C0)
|
||||
from . import DEV_TREZOR1, UDEV_RULES_STR, TransportException
|
||||
from .protocol import ProtocolBasedTransport, ProtocolV1
|
||||
|
||||
HidDevice = Dict[str, Any]
|
||||
HidDeviceHandle = Any
|
||||
@ -43,9 +39,7 @@ class HidHandle:
|
||||
self.handle.open_path(self.path)
|
||||
except (IOError, OSError) as e:
|
||||
if sys.platform.startswith("linux"):
|
||||
e.args = e.args + (
|
||||
"Do you have udev rules installed? https://github.com/trezor/trezor-common/blob/master/udev/51-trezor.rules",
|
||||
)
|
||||
e.args = e.args + (UDEV_RULES_STR)
|
||||
raise e
|
||||
self.handle.set_nonblocking(True)
|
||||
|
||||
@ -75,7 +69,7 @@ class HidHandle:
|
||||
time.sleep(0.001)
|
||||
if len(chunk) != 64:
|
||||
raise TransportException("Unexpected chunk size: %d" % len(chunk))
|
||||
return chunk
|
||||
return bytes(chunk)
|
||||
|
||||
def probe_hid_version(self) -> int:
|
||||
n = self.handle.write([0, 63] + [0xFF] * 63)
|
||||
@ -101,7 +95,7 @@ class HidTransport(ProtocolBasedTransport):
|
||||
self.device = device
|
||||
self.hid = hid_handle
|
||||
|
||||
protocol = get_protocol(hid_handle, is_trezor2(device))
|
||||
protocol = ProtocolV1(hid_handle)
|
||||
super().__init__(protocol=protocol)
|
||||
|
||||
def get_path(self) -> str:
|
||||
@ -111,7 +105,8 @@ class HidTransport(ProtocolBasedTransport):
|
||||
def enumerate(cls, debug: bool = False) -> Iterable["HidTransport"]:
|
||||
devices = []
|
||||
for dev in hid.enumerate(0, 0):
|
||||
if not (is_trezor1(dev) or is_trezor2(dev) or is_trezor2_bl(dev)):
|
||||
usb_id = (dev["vendor_id"], dev["product_id"])
|
||||
if usb_id != DEV_TREZOR1:
|
||||
continue
|
||||
if debug:
|
||||
if not is_debuglink(dev):
|
||||
@ -134,18 +129,6 @@ class HidTransport(ProtocolBasedTransport):
|
||||
raise TransportException("Debug HID device not found")
|
||||
|
||||
|
||||
def is_trezor1(dev: HidDevice) -> bool:
|
||||
return (dev["vendor_id"], dev["product_id"]) == DEV_TREZOR1
|
||||
|
||||
|
||||
def is_trezor2(dev: HidDevice) -> bool:
|
||||
return (dev["vendor_id"], dev["product_id"]) == DEV_TREZOR2
|
||||
|
||||
|
||||
def is_trezor2_bl(dev: HidDevice) -> bool:
|
||||
return (dev["vendor_id"], dev["product_id"]) == DEV_TREZOR2_BL
|
||||
|
||||
|
||||
def is_wirelink(dev: HidDevice) -> bool:
|
||||
return dev["usage_page"] == 0xFF00 or dev["interface_number"] == 0
|
||||
|
||||
|
@ -21,17 +21,13 @@ from typing import Iterable, Optional
|
||||
|
||||
import usb1
|
||||
|
||||
from . import TransportException
|
||||
from .protocol import ProtocolBasedTransport, get_protocol
|
||||
from . import TREZORS, UDEV_RULES_STR, TransportException
|
||||
from .protocol import ProtocolBasedTransport, ProtocolV1
|
||||
|
||||
if False:
|
||||
# mark Optional as used, otherwise it only exists in comments
|
||||
Optional
|
||||
|
||||
DEV_TREZOR1 = (0x534C, 0x0001)
|
||||
DEV_TREZOR2 = (0x1209, 0x53C1)
|
||||
DEV_TREZOR2_BL = (0x1209, 0x53C0)
|
||||
|
||||
INTERFACE = 0
|
||||
ENDPOINT = 1
|
||||
DEBUG_INTERFACE = 1
|
||||
@ -50,9 +46,7 @@ class WebUsbHandle:
|
||||
self.handle = self.device.open()
|
||||
if self.handle is None:
|
||||
if sys.platform.startswith("linux"):
|
||||
args = (
|
||||
"Do you have udev rules installed? https://github.com/trezor/trezor-common/blob/master/udev/51-trezor.rules",
|
||||
)
|
||||
args = (UDEV_RULES_STR,)
|
||||
else:
|
||||
args = ()
|
||||
raise IOError("Cannot open device", *args)
|
||||
@ -102,8 +96,7 @@ class WebUsbTransport(ProtocolBasedTransport):
|
||||
self.handle = handle
|
||||
self.debug = debug
|
||||
|
||||
protocol = get_protocol(handle, is_trezor2(device))
|
||||
super().__init__(protocol=protocol)
|
||||
super().__init__(protocol=ProtocolV1(handle))
|
||||
|
||||
def get_path(self) -> str:
|
||||
return "%s:%s" % (self.PATH_PREFIX, dev_to_str(self.device))
|
||||
@ -116,7 +109,8 @@ class WebUsbTransport(ProtocolBasedTransport):
|
||||
atexit.register(cls.context.close)
|
||||
devices = []
|
||||
for dev in cls.context.getDeviceIterator(skip_on_error=True):
|
||||
if not (is_trezor1(dev) or is_trezor2(dev) or is_trezor2_bl(dev)):
|
||||
usb_id = (dev.getVendorID(), dev.getProductID())
|
||||
if usb_id not in TREZORS:
|
||||
continue
|
||||
if not is_vendor_class(dev):
|
||||
continue
|
||||
@ -143,18 +137,6 @@ class WebUsbTransport(ProtocolBasedTransport):
|
||||
return WebUsbTransport(self.device, debug=True)
|
||||
|
||||
|
||||
def is_trezor1(dev: usb1.USBDevice) -> bool:
|
||||
return (dev.getVendorID(), dev.getProductID()) == DEV_TREZOR1
|
||||
|
||||
|
||||
def is_trezor2(dev: usb1.USBDevice) -> bool:
|
||||
return (dev.getVendorID(), dev.getProductID()) == DEV_TREZOR2
|
||||
|
||||
|
||||
def is_trezor2_bl(dev: usb1.USBDevice) -> bool:
|
||||
return (dev.getVendorID(), dev.getProductID()) == DEV_TREZOR2_BL
|
||||
|
||||
|
||||
def is_vendor_class(dev: usb1.USBDevice) -> bool:
|
||||
configurationId = 0
|
||||
altSettingId = 0
|
||||
|
Loading…
Reference in New Issue
Block a user