diff --git a/embed/bootloader/main.c b/embed/bootloader/main.c index a4a89de29..c19e2cacd 100644 --- a/embed/bootloader/main.c +++ b/embed/bootloader/main.c @@ -93,14 +93,12 @@ void check_and_jump(void) int usb_init_all(void) { static const usb_dev_info_t dev_info = { - .vendor_id = 0x1209, - .product_id = 0x53C0, - .release_num = 0x0002, - .manufacturer_str = (const uint8_t *)"SatoshiLabs", - .product_str = (const uint8_t *)"TREZOR Bootloader", - .serial_number_str = (const uint8_t *)"", - .configuration_str = (const uint8_t *)"", - .interface_str = (const uint8_t *)"", + .vendor_id = 0x1209, + .product_id = 0x53C0, + .release_num = 0x0002, + .manufacturer = (const uint8_t *)"SatoshiLabs", + .product = (const uint8_t *)"TREZOR Bootloader", + .serial_number = (const uint8_t *)"", }; static uint8_t hid_rx_buffer[USB_PACKET_SIZE]; static const uint8_t hid_report_desc[] = { diff --git a/embed/extmod/modtrezorio/modtrezorio-msg.h b/embed/extmod/modtrezorio/modtrezorio-msg.h index 4c017763b..73cfc5372 100644 --- a/embed/extmod/modtrezorio/modtrezorio-msg.h +++ b/embed/extmod/modtrezorio/modtrezorio-msg.h @@ -175,7 +175,7 @@ STATIC mp_obj_t mod_trezorio_HID_make_new(const mp_obj_type_t *type, size_t n_ar /// Returns the configured number of this interface. /// ''' STATIC mp_obj_t mod_trezorio_HID_iface_num(mp_obj_t self) { - mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); + mp_obj_HID_t *o = MP_OBJ_TO_PTR(self); return MP_OBJ_NEW_SMALL_INT(o->info.iface_num); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_HID_iface_num_obj, mod_trezorio_HID_iface_num); @@ -273,7 +273,7 @@ STATIC mp_obj_t mod_trezorio_VCP_make_new(const mp_obj_type_t *type, size_t n_ar /// Returns the configured number of this interface. /// ''' STATIC mp_obj_t mod_trezorio_VCP_iface_num(mp_obj_t self) { - mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); + mp_obj_VCP_t *o = MP_OBJ_TO_PTR(self); return MP_OBJ_NEW_SMALL_INT(o->info.iface_num); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_VCP_iface_num_obj, mod_trezorio_VCP_iface_num); @@ -391,11 +391,11 @@ STATIC mp_obj_t mod_trezorio_USB_add(mp_obj_t self, mp_obj_t iface) { if (o->state != USB_CLOSED) { mp_raise_msg(&mp_type_RuntimeError, "already initialized"); } - mp_obj_list_append(o->ifaces, iface); + mp_obj_list_append(MP_OBJ_FROM_PTR(&o->ifaces), iface); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_USB_add_obj, mod_trezorio_USB_add); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_USB_add_obj, mod_trezorio_USB_add); /// def start(self) -> None: /// ''' @@ -410,7 +410,7 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) { size_t iface_cnt; mp_obj_t *iface_objs; - mp_obj_get_array(o->ifaces, &iface_cnt, &iface_objs); + mp_obj_get_array(MP_OBJ_FROM_PTR(&o->ifaces), &iface_cnt, &iface_objs); // Initialize the USB stack if (usb_init(&o->info) != 0) { @@ -443,7 +443,7 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) { } // Start the USB stack - if (USB_open() != 0) { + if (usb_start() != 0) { usb_deinit(); mp_raise_msg(&mp_type_RuntimeError, "failed to start USB"); } @@ -455,21 +455,21 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) { return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_USB_open_obj, mod_trezorio_USB_open); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_USB_open_obj, mod_trezorio_USB_open); /// def stop(self) -> None: /// ''' /// Cleans up the USB stack. /// ''' -STATIC mp_obj_t mod_trezorio_USB_close_usb(mp_obj_t self) { +STATIC mp_obj_t mod_trezorio_USB_close(mp_obj_t self) { mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); if (o->state != USB_OPENED) { mp_raise_msg(&mp_type_RuntimeError, "not initialized"); } - USB_close(); + usb_stop(); usb_deinit(); - mp_obj_list_set_len(o->ifaces, 0); + mp_obj_list_set_len(MP_OBJ_FROM_PTR(&o->ifaces), 0); mp_seq_clear(o->ifaces.items, 0, o->ifaces.alloc, sizeof(*o->ifaces.items)); o->info.vendor_id = 0; o->info.product_id = 0; @@ -481,7 +481,7 @@ STATIC mp_obj_t mod_trezorio_USB_close_usb(mp_obj_t self) { return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_USB_close_usb_obj, mod_trezorio_USB_close_usb); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_USB_close_obj, mod_trezorio_USB_close); /// def write(self, iface: int, msg: bytes) -> int: /// ''' @@ -503,7 +503,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorio_USB_write_obj, mod_trezorio_USB_wr STATIC mp_obj_t mod_trezorio_USB___del__(mp_obj_t self) { mp_obj_USB_t *o = MP_OBJ_TO_PTR(self); if (o->state != USB_CLOSED) { - usb_close(); + usb_stop(); usb_deinit(); o->state = USB_CLOSED; } diff --git a/embed/trezorhal/usb.c b/embed/trezorhal/usb.c index 60795f100..df5c3b51d 100644 --- a/embed/trezorhal/usb.c +++ b/embed/trezorhal/usb.c @@ -68,14 +68,14 @@ int usb_init(const usb_dev_info_t *dev_info) { usb_dev_desc.bNumConfigurations = 1; // String table - if ((0 != check_desc_str(dev_info->manufacturer_str)) || - (0 != check_desc_str(dev_info->product_str)) || - (0 != check_desc_str(dev_info->serial_number_str))) { + if ((0 != check_desc_str(dev_info->manufacturer)) || + (0 != check_desc_str(dev_info->product)) || + (0 != check_desc_str(dev_info->serial_number))) { return 1; // Invalid descriptor string } - usb_str_table.manufacturer_str = dev_info->manufacturer_str; - usb_str_table.product_str = dev_info->product_str; - usb_str_table.serial_str = dev_info->serial_number_str; + usb_str_table.manufacturer = dev_info->manufacturer; + usb_str_table.product = dev_info->product; + usb_str_table.serial_number = dev_info->serial_number; // Configuration descriptor usb_config_desc->bLength = sizeof(usb_config_descriptor_t); @@ -185,17 +185,17 @@ static uint8_t *usb_get_langid_str_descriptor(USBD_SpeedTypeDef speed, uint16_t } static uint8_t *usb_get_manufacturer_str_descriptor(USBD_SpeedTypeDef speed, uint16_t *length) { - USBD_GetString(UNCONST(usb_str_table.manufacturer_str), usb_str_buf, length); + USBD_GetString(UNCONST(usb_str_table.manufacturer), usb_str_buf, length); return usb_str_buf; } static uint8_t *usb_get_product_str_descriptor(USBD_SpeedTypeDef speed, uint16_t *length) { - USBD_GetString(UNCONST(usb_str_table.product_str), usb_str_buf, length); + USBD_GetString(UNCONST(usb_str_table.product), usb_str_buf, length); return usb_str_buf; } static uint8_t *usb_get_serial_str_descriptor(USBD_SpeedTypeDef speed, uint16_t *length) { - USBD_GetString(UNCONST(usb_str_table.serial_str), usb_str_buf, length); + USBD_GetString(UNCONST(usb_str_table.serial_number), usb_str_buf, length); return usb_str_buf; } diff --git a/embed/trezorhal/usb.h b/embed/trezorhal/usb.h index a4b3e944a..602a6bd3c 100644 --- a/embed/trezorhal/usb.h +++ b/embed/trezorhal/usb.h @@ -85,18 +85,18 @@ typedef enum { } usb_language_id_t; typedef struct { - const uint8_t *manufacturer_str; - const uint8_t *product_str; - const uint8_t *serial_str; + const uint8_t *manufacturer; + const uint8_t *product; + const uint8_t *serial_number; } usb_dev_string_table_t; typedef struct { uint16_t vendor_id; uint16_t product_id; uint16_t release_num; - const uint8_t *manufacturer_str; - const uint8_t *product_str; - const uint8_t *serial_number_str; + const uint8_t *manufacturer; + const uint8_t *product; + const uint8_t *serial_number; } usb_dev_info_t; typedef enum { diff --git a/src/apps/fido_u2f/__init__.py b/src/apps/fido_u2f/__init__.py index c1d9ef454..4b9594a7d 100644 --- a/src/apps/fido_u2f/__init__.py +++ b/src/apps/fido_u2f/__init__.py @@ -6,7 +6,7 @@ import utime from trezor import log from trezor import loop -from trezor import msg +from trezor import io from trezor import ui from trezor import utils from trezor import workflow @@ -295,7 +295,7 @@ def send_cmd(cmd: Cmd, iface: int) -> None: frm.bcnt = datalen offset += utils.memcpy(frm.data, 0, cmd.data, offset, datalen) - msg.send(iface, buf) + io.send(iface, buf) # log.debug(__name__, 'send init %s', buf) if offset < datalen: @@ -305,7 +305,7 @@ def send_cmd(cmd: Cmd, iface: int) -> None: frm.seq = seq offset += utils.memcpy(frm.data, 0, cmd.data, offset, datalen) utime.sleep_ms(1) # FIXME: do async send - msg.send(iface, buf) + io.send(iface, buf) # log.debug(__name__, 'send cont %s', buf) seq += 1 diff --git a/src/main.py b/src/main.py index f0c432145..36b3bb121 100644 --- a/src/main.py +++ b/src/main.py @@ -1,16 +1,11 @@ from micropython import const -import trezor.main -from trezor import config -from trezor import msg -from trezor import ui +from trezor import io from trezor import wire -from trezor import loop -from trezor.wire import codec_v2 +from trezor import main -config.init() - -# Load all applications +# Load applications +from apps.common import storage if __debug__: from apps import debug from apps import homescreen @@ -19,7 +14,7 @@ from apps import wallet from apps import ethereum from apps import fido_u2f -# Initialize all applications +# Boot applications if __debug__: debug.boot() homescreen.boot() @@ -28,21 +23,9 @@ wallet.boot() ethereum.boot() fido_u2f.boot() -# HACK: keep storage loaded at all times -from apps.common import storage - -# Change backlight to white for better visibility -ui.display.backlight(ui.BACKLIGHT_NORMAL) - -# Register USB ifaces - -_IFACE_WIRE = const(0x00) -_IFACE_VCP = const(0x01) -_IFACE_VCP_DATA = const(0x02) -_IFACE_U2F = const(0x03) - -hid_wire = msg.HID( - iface_num=_IFACE_WIRE, +# Intialize the USB stack +usb_wire = io.HID( + iface_num=0x00, ep_in=0x81, ep_out=0x01, report_desc=bytes([ @@ -64,17 +47,15 @@ hid_wire = msg.HID( 0xc0, # END_COLLECTION ]), ) - -vcp = msg.VCP( - iface_num=_IFACE_VCP, - data_iface_num=_IFACE_VCP_DATA, +usb_vcp = io.VCP( + iface_num=0x01, + data_iface_num=0x02, ep_in=0x82, ep_out=0x02, ep_cmd=0x83, ) - -hid_u2f = msg.HID( - iface_num=_IFACE_U2F, +usb_u2f = io.HID( + iface_num=0x03, ep_in=0x84, ep_out=0x03, report_desc=bytes([ @@ -96,21 +77,24 @@ hid_u2f = msg.HID( 0xc0, # END_COLLECTION ]), ) - -msg.init_usb(msg.USB( +usb = io.USB( vendor_id=0x1209, product_id=0x53C1, release_num=0x0002, - manufacturer_str="SatoshiLabs", - product_str="TREZOR", - serial_number_str="000000000000000000000000" -), (hid_wire, vcp, hid_u2f)) + manufacturer="SatoshiLabs", + product="TREZOR", + serial_number="000000000000000000000000", +) +usb.add(usb_wire) +usb.add(usb_vcp) +usb.add(usb_u2f) +usb.open() # Initialize the wire codec pipeline -wire.setup(_IFACE_WIRE) +wire.setup(usb_wire.iface_num()) # Load default homescreen from apps.homescreen.homescreen import layout_homescreen # Run main even loop and specify which screen is default -trezor.main.run(default_workflow=layout_homescreen) +main.run(default_workflow=layout_homescreen) diff --git a/src/trezor/loop.py b/src/trezor/loop.py index d634159a7..b9ebd79b5 100644 --- a/src/trezor/loop.py +++ b/src/trezor/loop.py @@ -11,18 +11,16 @@ and `Wait`. import utime import utimeq from micropython import const -from trezor import msg from trezor import log +from trezor import io -import trezormsg +TOUCH = io.TOUCH +TOUCH_START = io.TOUCH_START +TOUCH_MOVE = io.TOUCH_MOVE +TOUCH_END = io.TOUCH_END -TOUCH = trezormsg.TOUCH -TOUCH_START = trezormsg.TOUCH_START -TOUCH_MOVE = trezormsg.TOUCH_MOVE -TOUCH_END = trezormsg.TOUCH_END - -READ = trezormsg.POLL_READ -WRITE = trezormsg.POLL_WRITE +READ = io.POLL_READ +WRITE = io.POLL_WRITE after_step_hook = None # function, called after each task step diff --git a/src/trezor/msg.py b/src/trezor/msg.py deleted file mode 100644 index ed0718681..000000000 --- a/src/trezor/msg.py +++ /dev/null @@ -1,15 +0,0 @@ -from trezormsg import Msg, USB, HID, VCP - -_msg = Msg() - - -def init_usb(usb, ifaces): - return _msg.init_usb(usb, ifaces) - - -def select(timeout_us): - return _msg.select(timeout_us) - - -def send(iface, msg): - return _msg.send(iface, msg) diff --git a/src/trezor/wire/__init__.py b/src/trezor/wire/__init__.py index 93a660f47..07435e987 100644 --- a/src/trezor/wire/__init__.py +++ b/src/trezor/wire/__init__.py @@ -121,7 +121,6 @@ async def handle_unexp_msg(ctx, reader): await ctx.write( Failure(code=UnexpectedMessage, message='Unexpected message')) - def make_failure_msg(exc): from trezor.messages.Failure import Failure from trezor.messages.FailureType import FirmwareError diff --git a/src/trezor/wire/codec_v1.py b/src/trezor/wire/codec_v1.py index 9a3dfc87b..c296a1b80 100644 --- a/src/trezor/wire/codec_v1.py +++ b/src/trezor/wire/codec_v1.py @@ -127,7 +127,7 @@ class Writer: if self.ofs == _REP_LEN: # we are at the end of the report, flush it await write - io.send(self.iface, self.data) + io.write(self.iface, self.data) self.ofs = _REP_CONT_DATA return nwritten diff --git a/src/trezor/wire/codec_v2.py b/src/trezor/wire/codec_v2.py index 808b4fa43..bdd79415f 100644 --- a/src/trezor/wire/codec_v2.py +++ b/src/trezor/wire/codec_v2.py @@ -200,12 +200,12 @@ class SesssionSupervisor: self.open(newsid) yield await write - self.sendopen(newsid) + self.writeopen(newsid) elif repmarker == _REP_MARKER_CLOSE: self.close(repsid) yield await write - self.sendclose(repsid) + self.writeclose(repsid) def open(self, sid): if sid not in self.handling_tasks: @@ -223,10 +223,10 @@ class SesssionSupervisor: if sid not in self.handling_tasks: return sid - def sendopen(self, sid): + def writeopen(self, sid): ustruct.pack_into(_REP, self.session_report, 0, _REP_MARKER_OPEN, sid) - io.send(self.iface, self.session_report) + io.write(self.iface, self.session_report) - def sendclose(self, sid): + def writeclose(self, sid): ustruct.pack_into(_REP, self.session_report, 0, _REP_MARKER_CLOSE, sid) - io.send(self.iface, self.session_report) + io.write(self.iface, self.session_report)