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__)
|
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):
|
class TransportException(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -20,12 +20,8 @@ from typing import Any, Dict, Iterable
|
|||||||
|
|
||||||
import hid
|
import hid
|
||||||
|
|
||||||
from . import TransportException
|
from . import DEV_TREZOR1, UDEV_RULES_STR, TransportException
|
||||||
from .protocol import ProtocolBasedTransport, get_protocol
|
from .protocol import ProtocolBasedTransport, ProtocolV1
|
||||||
|
|
||||||
DEV_TREZOR1 = (0x534C, 0x0001)
|
|
||||||
DEV_TREZOR2 = (0x1209, 0x53C1)
|
|
||||||
DEV_TREZOR2_BL = (0x1209, 0x53C0)
|
|
||||||
|
|
||||||
HidDevice = Dict[str, Any]
|
HidDevice = Dict[str, Any]
|
||||||
HidDeviceHandle = Any
|
HidDeviceHandle = Any
|
||||||
@ -43,9 +39,7 @@ class HidHandle:
|
|||||||
self.handle.open_path(self.path)
|
self.handle.open_path(self.path)
|
||||||
except (IOError, OSError) as e:
|
except (IOError, OSError) as e:
|
||||||
if sys.platform.startswith("linux"):
|
if sys.platform.startswith("linux"):
|
||||||
e.args = e.args + (
|
e.args = e.args + (UDEV_RULES_STR)
|
||||||
"Do you have udev rules installed? https://github.com/trezor/trezor-common/blob/master/udev/51-trezor.rules",
|
|
||||||
)
|
|
||||||
raise e
|
raise e
|
||||||
self.handle.set_nonblocking(True)
|
self.handle.set_nonblocking(True)
|
||||||
|
|
||||||
@ -75,7 +69,7 @@ class HidHandle:
|
|||||||
time.sleep(0.001)
|
time.sleep(0.001)
|
||||||
if len(chunk) != 64:
|
if len(chunk) != 64:
|
||||||
raise TransportException("Unexpected chunk size: %d" % len(chunk))
|
raise TransportException("Unexpected chunk size: %d" % len(chunk))
|
||||||
return chunk
|
return bytes(chunk)
|
||||||
|
|
||||||
def probe_hid_version(self) -> int:
|
def probe_hid_version(self) -> int:
|
||||||
n = self.handle.write([0, 63] + [0xFF] * 63)
|
n = self.handle.write([0, 63] + [0xFF] * 63)
|
||||||
@ -101,7 +95,7 @@ class HidTransport(ProtocolBasedTransport):
|
|||||||
self.device = device
|
self.device = device
|
||||||
self.hid = hid_handle
|
self.hid = hid_handle
|
||||||
|
|
||||||
protocol = get_protocol(hid_handle, is_trezor2(device))
|
protocol = ProtocolV1(hid_handle)
|
||||||
super().__init__(protocol=protocol)
|
super().__init__(protocol=protocol)
|
||||||
|
|
||||||
def get_path(self) -> str:
|
def get_path(self) -> str:
|
||||||
@ -111,7 +105,8 @@ class HidTransport(ProtocolBasedTransport):
|
|||||||
def enumerate(cls, debug: bool = False) -> Iterable["HidTransport"]:
|
def enumerate(cls, debug: bool = False) -> Iterable["HidTransport"]:
|
||||||
devices = []
|
devices = []
|
||||||
for dev in hid.enumerate(0, 0):
|
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
|
continue
|
||||||
if debug:
|
if debug:
|
||||||
if not is_debuglink(dev):
|
if not is_debuglink(dev):
|
||||||
@ -134,18 +129,6 @@ class HidTransport(ProtocolBasedTransport):
|
|||||||
raise TransportException("Debug HID device not found")
|
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:
|
def is_wirelink(dev: HidDevice) -> bool:
|
||||||
return dev["usage_page"] == 0xFF00 or dev["interface_number"] == 0
|
return dev["usage_page"] == 0xFF00 or dev["interface_number"] == 0
|
||||||
|
|
||||||
|
@ -21,17 +21,13 @@ from typing import Iterable, Optional
|
|||||||
|
|
||||||
import usb1
|
import usb1
|
||||||
|
|
||||||
from . import TransportException
|
from . import TREZORS, UDEV_RULES_STR, TransportException
|
||||||
from .protocol import ProtocolBasedTransport, get_protocol
|
from .protocol import ProtocolBasedTransport, ProtocolV1
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
# mark Optional as used, otherwise it only exists in comments
|
# mark Optional as used, otherwise it only exists in comments
|
||||||
Optional
|
Optional
|
||||||
|
|
||||||
DEV_TREZOR1 = (0x534C, 0x0001)
|
|
||||||
DEV_TREZOR2 = (0x1209, 0x53C1)
|
|
||||||
DEV_TREZOR2_BL = (0x1209, 0x53C0)
|
|
||||||
|
|
||||||
INTERFACE = 0
|
INTERFACE = 0
|
||||||
ENDPOINT = 1
|
ENDPOINT = 1
|
||||||
DEBUG_INTERFACE = 1
|
DEBUG_INTERFACE = 1
|
||||||
@ -50,9 +46,7 @@ class WebUsbHandle:
|
|||||||
self.handle = self.device.open()
|
self.handle = self.device.open()
|
||||||
if self.handle is None:
|
if self.handle is None:
|
||||||
if sys.platform.startswith("linux"):
|
if sys.platform.startswith("linux"):
|
||||||
args = (
|
args = (UDEV_RULES_STR,)
|
||||||
"Do you have udev rules installed? https://github.com/trezor/trezor-common/blob/master/udev/51-trezor.rules",
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
args = ()
|
args = ()
|
||||||
raise IOError("Cannot open device", *args)
|
raise IOError("Cannot open device", *args)
|
||||||
@ -102,8 +96,7 @@ class WebUsbTransport(ProtocolBasedTransport):
|
|||||||
self.handle = handle
|
self.handle = handle
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
|
|
||||||
protocol = get_protocol(handle, is_trezor2(device))
|
super().__init__(protocol=ProtocolV1(handle))
|
||||||
super().__init__(protocol=protocol)
|
|
||||||
|
|
||||||
def get_path(self) -> str:
|
def get_path(self) -> str:
|
||||||
return "%s:%s" % (self.PATH_PREFIX, dev_to_str(self.device))
|
return "%s:%s" % (self.PATH_PREFIX, dev_to_str(self.device))
|
||||||
@ -116,7 +109,8 @@ class WebUsbTransport(ProtocolBasedTransport):
|
|||||||
atexit.register(cls.context.close)
|
atexit.register(cls.context.close)
|
||||||
devices = []
|
devices = []
|
||||||
for dev in cls.context.getDeviceIterator(skip_on_error=True):
|
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
|
continue
|
||||||
if not is_vendor_class(dev):
|
if not is_vendor_class(dev):
|
||||||
continue
|
continue
|
||||||
@ -143,18 +137,6 @@ class WebUsbTransport(ProtocolBasedTransport):
|
|||||||
return WebUsbTransport(self.device, debug=True)
|
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:
|
def is_vendor_class(dev: usb1.USBDevice) -> bool:
|
||||||
configurationId = 0
|
configurationId = 0
|
||||||
altSettingId = 0
|
altSettingId = 0
|
||||||
|
Loading…
Reference in New Issue
Block a user