mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-17 03:48:09 +00:00
src/main: simplify, extract USB code into usb.py
This commit is contained in:
parent
3b67cb6bea
commit
074e7c1b9b
111
src/main.py
111
src/main.py
@ -1,110 +1,41 @@
|
||||
# unlock the device
|
||||
import boot # noqa: F401
|
||||
|
||||
from trezor import io
|
||||
from trezor import log
|
||||
from trezor import loop
|
||||
from trezor import utils
|
||||
from trezor import wire
|
||||
from trezor import workflow
|
||||
# prepare the USB interfaces, but do not connect to the host yet
|
||||
import usb
|
||||
|
||||
from apps.common.storage import get_device_id
|
||||
|
||||
log.level = log.DEBUG
|
||||
|
||||
# initialize the USB stack
|
||||
|
||||
usb_wire = io.WebUSB(
|
||||
iface_num=0,
|
||||
ep_in=0x81,
|
||||
ep_out=0x01,
|
||||
)
|
||||
|
||||
if __debug__:
|
||||
usb_debug = io.WebUSB(
|
||||
iface_num=1,
|
||||
ep_in=0x82,
|
||||
ep_out=0x02,
|
||||
)
|
||||
usb_vcp = io.VCP(
|
||||
iface_num=2,
|
||||
data_iface_num=3,
|
||||
ep_in=0x83,
|
||||
ep_out=0x03,
|
||||
ep_cmd=0x84,
|
||||
)
|
||||
else:
|
||||
usb_u2f = io.HID(
|
||||
iface_num=1,
|
||||
ep_in=0x82,
|
||||
ep_out=0x02,
|
||||
report_desc=bytes([
|
||||
0x06, 0xd0, 0xf1, # USAGE_PAGE (FIDO Alliance)
|
||||
0x09, 0x01, # USAGE (U2F HID Authenticator Device)
|
||||
0xa1, 0x01, # COLLECTION (Application)
|
||||
0x09, 0x20, # USAGE (Input Report Data)
|
||||
0x15, 0x00, # LOGICAL_MINIMUM (0)
|
||||
0x26, 0xff, 0x00, # LOGICAL_MAXIMUM (255)
|
||||
0x75, 0x08, # REPORT_SIZE (8)
|
||||
0x95, 0x40, # REPORT_COUNT (64)
|
||||
0x81, 0x02, # INPUT (Data,Var,Abs)
|
||||
0x09, 0x21, # USAGE (Output Report Data)
|
||||
0x15, 0x00, # LOGICAL_MINIMUM (0)
|
||||
0x26, 0xff, 0x00, # LOGICAL_MAXIMUM (255)
|
||||
0x75, 0x08, # REPORT_SIZE (8)
|
||||
0x95, 0x40, # REPORT_COUNT (64)
|
||||
0x91, 0x02, # OUTPUT (Data,Var,Abs)
|
||||
0xc0, # END_COLLECTION
|
||||
]),
|
||||
)
|
||||
|
||||
usb = io.USB(
|
||||
vendor_id=0x1209,
|
||||
product_id=0x53C1,
|
||||
release_num=0x0200,
|
||||
manufacturer="SatoshiLabs",
|
||||
product="TREZOR",
|
||||
serial_number=get_device_id(),
|
||||
interface="TREZOR Interface",
|
||||
)
|
||||
|
||||
usb.add(usb_wire)
|
||||
if __debug__:
|
||||
usb.add(usb_debug)
|
||||
usb.add(usb_vcp)
|
||||
else:
|
||||
usb.add(usb_u2f)
|
||||
from trezor import loop, wire, workflow, utils
|
||||
|
||||
# load applications
|
||||
from apps import homescreen
|
||||
from apps import management
|
||||
from apps import wallet
|
||||
from apps import ethereum
|
||||
import apps.homescreen
|
||||
import apps.management
|
||||
import apps.wallet
|
||||
import apps.ethereum
|
||||
if __debug__:
|
||||
from apps import debug
|
||||
import apps.debug
|
||||
else:
|
||||
from apps import fido_u2f
|
||||
import apps.fido_u2f
|
||||
|
||||
# boot applications
|
||||
homescreen.boot()
|
||||
management.boot()
|
||||
wallet.boot()
|
||||
ethereum.boot()
|
||||
apps.homescreen.boot()
|
||||
apps.management.boot()
|
||||
apps.wallet.boot()
|
||||
apps.ethereum.boot()
|
||||
if __debug__:
|
||||
debug.boot()
|
||||
apps.debug.boot()
|
||||
else:
|
||||
fido_u2f.boot(usb_u2f)
|
||||
apps.fido_u2f.boot(usb.iface_u2f)
|
||||
|
||||
# initialize the wire codec and start the USB
|
||||
wire.setup(usb_wire)
|
||||
wire.setup(usb.iface_wire)
|
||||
if __debug__:
|
||||
wire.setup(usb_debug)
|
||||
usb.open()
|
||||
wire.setup(usb.iface_debug)
|
||||
usb.bus.open()
|
||||
|
||||
# switch into unprivileged mode, as we don't need the extra permissions anymore
|
||||
utils.set_mode_unprivileged()
|
||||
|
||||
# load default homescreen
|
||||
# run main event loop and specify which screen is the default
|
||||
from apps.homescreen.homescreen import homescreen
|
||||
|
||||
# run main even loop and specify which screen is default
|
||||
workflow.startdefault(homescreen)
|
||||
loop.run()
|
||||
|
@ -17,7 +17,7 @@ _leveldict = {
|
||||
CRITICAL: ('CRITICAL', '1;31'),
|
||||
}
|
||||
|
||||
level = NOTSET
|
||||
level = DEBUG
|
||||
color = True
|
||||
|
||||
|
||||
|
69
src/usb.py
Normal file
69
src/usb.py
Normal file
@ -0,0 +1,69 @@
|
||||
from trezor import io
|
||||
|
||||
from apps.common.storage import get_device_id
|
||||
|
||||
# interface used for trezor wire protocol
|
||||
iface_wire = io.WebUSB(
|
||||
iface_num=0,
|
||||
ep_in=0x81,
|
||||
ep_out=0x01,
|
||||
)
|
||||
|
||||
# as the iface_vcp inteface needs 3 endpoints, we cannot use it simultaneously
|
||||
# with the iface_u2f inteface.
|
||||
if __debug__:
|
||||
# interface used for debug messages with trezor wire protocol
|
||||
iface_debug = io.WebUSB(
|
||||
iface_num=1,
|
||||
ep_in=0x82,
|
||||
ep_out=0x02,
|
||||
)
|
||||
# interface used for cdc/vcp console emulation (debug messages)
|
||||
iface_vcp = io.VCP(
|
||||
iface_num=2,
|
||||
data_iface_num=3,
|
||||
ep_in=0x83,
|
||||
ep_out=0x03,
|
||||
ep_cmd=0x84,
|
||||
)
|
||||
else:
|
||||
# interface used for FIDO U2F HID transport
|
||||
iface_u2f = io.HID(
|
||||
iface_num=1,
|
||||
ep_in=0x82,
|
||||
ep_out=0x02,
|
||||
report_desc=bytes([
|
||||
0x06, 0xd0, 0xf1, # USAGE_PAGE (FIDO Alliance)
|
||||
0x09, 0x01, # USAGE (U2F HID Authenticator Device)
|
||||
0xa1, 0x01, # COLLECTION (Application)
|
||||
0x09, 0x20, # USAGE (Input Report Data)
|
||||
0x15, 0x00, # LOGICAL_MINIMUM (0)
|
||||
0x26, 0xff, 0x00, # LOGICAL_MAXIMUM (255)
|
||||
0x75, 0x08, # REPORT_SIZE (8)
|
||||
0x95, 0x40, # REPORT_COUNT (64)
|
||||
0x81, 0x02, # INPUT (Data,Var,Abs)
|
||||
0x09, 0x21, # USAGE (Output Report Data)
|
||||
0x15, 0x00, # LOGICAL_MINIMUM (0)
|
||||
0x26, 0xff, 0x00, # LOGICAL_MAXIMUM (255)
|
||||
0x75, 0x08, # REPORT_SIZE (8)
|
||||
0x95, 0x40, # REPORT_COUNT (64)
|
||||
0x91, 0x02, # OUTPUT (Data,Var,Abs)
|
||||
0xc0, # END_COLLECTION
|
||||
]),
|
||||
)
|
||||
|
||||
bus = io.USB(
|
||||
vendor_id=0x1209,
|
||||
product_id=0x53C1,
|
||||
release_num=0x0200,
|
||||
manufacturer="SatoshiLabs",
|
||||
product="TREZOR",
|
||||
interface="TREZOR Interface",
|
||||
serial_number=get_device_id(),
|
||||
)
|
||||
bus.add(iface_wire)
|
||||
if __debug__:
|
||||
bus.add(iface_debug)
|
||||
bus.add(iface_vcp)
|
||||
else:
|
||||
bus.add(iface_u2f)
|
Loading…
Reference in New Issue
Block a user