From f50afd2d2a939f766121026864dd02c7bc66a8da Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Sun, 21 Jan 2018 04:04:33 +0100 Subject: [PATCH] bootloader: add (optional) WebUSB support --- embed/bootloader/main.c | 34 ++++++++++++++++++++++++++++++---- embed/bootloader/messages.c | 24 ++++++++++++++++++++---- embed/bootloader/messages.h | 3 +++ 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/embed/bootloader/main.c b/embed/bootloader/main.c index 3d98a55a2..d04b97f92 100644 --- a/embed/bootloader/main.c +++ b/embed/bootloader/main.c @@ -127,6 +127,7 @@ static const uint8_t * const BOOTLOADER_KEYS[] = { }; static void usb_init_all(void) { + static const usb_dev_info_t dev_info = { .vendor_id = 0x1209, .product_id = 0x53C0, @@ -137,7 +138,21 @@ static void usb_init_all(void) { .configuration = (const uint8_t *)"", .interface = (const uint8_t *)"TREZOR Interface", }; - static uint8_t hid_rx_buffer[USB_PACKET_SIZE]; + + static uint8_t rx_buffer[USB_PACKET_SIZE]; + +#if USE_WEBUSB + static const usb_webusb_info_t webusb_info = { + .iface_num = USB_IFACE_NUM, + .ep_in = USB_EP_DIR_IN | 0x01, + .ep_out = USB_EP_DIR_OUT | 0x01, + .subclass = 0, + .protocol = 0, + .max_packet_len = sizeof(rx_buffer), + .rx_buffer = rx_buffer, + .polling_interval = 1, + }; +#else static const uint8_t hid_report_desc[] = { 0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined) 0x09, 0x01, // USAGE (1) @@ -162,15 +177,22 @@ static void usb_init_all(void) { .ep_out = USB_EP_DIR_OUT | 0x01, .subclass = 0, .protocol = 0, - .max_packet_len = sizeof(hid_rx_buffer), - .rx_buffer = hid_rx_buffer, + .max_packet_len = sizeof(rx_buffer), + .rx_buffer = rx_buffer, .polling_interval = 1, .report_desc_len = sizeof(hid_report_desc), .report_desc = hid_report_desc, }; +#endif usb_init(&dev_info); + +#if USE_WEBUSB + ensure(usb_webusb_add(&webusb_info), NULL); +#else ensure(usb_hid_add(&hid_info), NULL); +#endif + usb_start(); } @@ -183,7 +205,11 @@ static secbool bootloader_loop(secbool firmware_present) uint8_t buf[USB_PACKET_SIZE]; for (;;) { - int r = usb_hid_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE, 100); +#if USE_WEBUSB + int r = usb_webusb_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE, USB_TIMEOUT); +#else + int r = usb_hid_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE, USB_TIMEOUT); +#endif ensure(sectrue * (r == USB_PACKET_SIZE), NULL); uint16_t msg_id; uint32_t msg_size; diff --git a/embed/bootloader/messages.c b/embed/bootloader/messages.c index 7d1acf502..debd6d7c2 100644 --- a/embed/bootloader/messages.c +++ b/embed/bootloader/messages.c @@ -58,7 +58,11 @@ static bool _usb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count) memcpy(state->buf + state->packet_pos, buf + written, USB_PACKET_SIZE - state->packet_pos); written += USB_PACKET_SIZE - state->packet_pos; // send packet - int r = usb_hid_write_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, 100); +#if USE_WEBUSB + int r = usb_webusb_write_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, USB_TIMEOUT); +#else + int r = usb_hid_write_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, USB_TIMEOUT); +#endif ensure(sectrue * (r == USB_PACKET_SIZE), NULL); // prepare new packet state->packet_index++; @@ -79,7 +83,11 @@ static void _usb_write_flush(usb_write_state *state) memset(state->buf + state->packet_pos, 0, USB_PACKET_SIZE - state->packet_pos); } // send packet - int r = usb_hid_write_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, 100); +#if USE_WEBUSB + int r = usb_webusb_write_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, USB_TIMEOUT); +#else + int r = usb_hid_write_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, USB_TIMEOUT); +#endif ensure(sectrue * (r == USB_PACKET_SIZE), NULL); } @@ -159,7 +167,11 @@ static bool _usb_read(pb_istream_t *stream, uint8_t *buf, size_t count) memcpy(buf + read, state->buf + state->packet_pos, USB_PACKET_SIZE - state->packet_pos); read += USB_PACKET_SIZE - state->packet_pos; // read next packet - int r = usb_hid_read_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, 100); +#if USE_WEBUSB + int r = usb_webusb_read_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, USB_TIMEOUT); +#else + int r = usb_hid_read_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, USB_TIMEOUT); +#endif ensure(sectrue * (r == USB_PACKET_SIZE), NULL); // prepare next packet state->packet_index++; @@ -487,7 +499,11 @@ void process_msg_unknown(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) // consume remaining message int remaining_chunks = (msg_size - (USB_PACKET_SIZE - MSG_HEADER1_LEN)) / (USB_PACKET_SIZE - MSG_HEADER2_LEN); for (int i = 0; i < remaining_chunks; i++) { - int r = usb_hid_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE, 100); +#if USE_WEBUSB + int r = usb_webusb_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE, USB_TIMEOUT); +#else + int r = usb_hid_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE, USB_TIMEOUT); +#endif ensure(sectrue * (r == USB_PACKET_SIZE), NULL); } diff --git a/embed/bootloader/messages.h b/embed/bootloader/messages.h index fbd23067f..16534db91 100644 --- a/embed/bootloader/messages.h +++ b/embed/bootloader/messages.h @@ -4,6 +4,9 @@ #include #include "secbool.h" +#define USE_WEBUSB 0 + +#define USB_TIMEOUT 100 #define USB_PACKET_SIZE 64 #define USB_IFACE_NUM 0