1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-27 01:48:17 +00:00

bootloader: add (optional) WebUSB support

This commit is contained in:
Pavol Rusnak 2018-01-21 04:04:33 +01:00
parent 5ef0967857
commit f50afd2d2a
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
3 changed files with 53 additions and 8 deletions

View File

@ -127,6 +127,7 @@ static const uint8_t * const BOOTLOADER_KEYS[] = {
}; };
static void usb_init_all(void) { static void usb_init_all(void) {
static const usb_dev_info_t dev_info = { static const usb_dev_info_t dev_info = {
.vendor_id = 0x1209, .vendor_id = 0x1209,
.product_id = 0x53C0, .product_id = 0x53C0,
@ -137,7 +138,21 @@ static void usb_init_all(void) {
.configuration = (const uint8_t *)"", .configuration = (const uint8_t *)"",
.interface = (const uint8_t *)"TREZOR Interface", .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[] = { static const uint8_t hid_report_desc[] = {
0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined) 0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined)
0x09, 0x01, // USAGE (1) 0x09, 0x01, // USAGE (1)
@ -162,15 +177,22 @@ static void usb_init_all(void) {
.ep_out = USB_EP_DIR_OUT | 0x01, .ep_out = USB_EP_DIR_OUT | 0x01,
.subclass = 0, .subclass = 0,
.protocol = 0, .protocol = 0,
.max_packet_len = sizeof(hid_rx_buffer), .max_packet_len = sizeof(rx_buffer),
.rx_buffer = hid_rx_buffer, .rx_buffer = rx_buffer,
.polling_interval = 1, .polling_interval = 1,
.report_desc_len = sizeof(hid_report_desc), .report_desc_len = sizeof(hid_report_desc),
.report_desc = hid_report_desc, .report_desc = hid_report_desc,
}; };
#endif
usb_init(&dev_info); usb_init(&dev_info);
#if USE_WEBUSB
ensure(usb_webusb_add(&webusb_info), NULL);
#else
ensure(usb_hid_add(&hid_info), NULL); ensure(usb_hid_add(&hid_info), NULL);
#endif
usb_start(); usb_start();
} }
@ -183,7 +205,11 @@ static secbool bootloader_loop(secbool firmware_present)
uint8_t buf[USB_PACKET_SIZE]; uint8_t buf[USB_PACKET_SIZE];
for (;;) { 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); ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
uint16_t msg_id; uint16_t msg_id;
uint32_t msg_size; uint32_t msg_size;

View File

@ -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); memcpy(state->buf + state->packet_pos, buf + written, USB_PACKET_SIZE - state->packet_pos);
written += USB_PACKET_SIZE - state->packet_pos; written += USB_PACKET_SIZE - state->packet_pos;
// send packet // 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); ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
// prepare new packet // prepare new packet
state->packet_index++; 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); memset(state->buf + state->packet_pos, 0, USB_PACKET_SIZE - state->packet_pos);
} }
// send packet // 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); 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); memcpy(buf + read, state->buf + state->packet_pos, USB_PACKET_SIZE - state->packet_pos);
read += USB_PACKET_SIZE - state->packet_pos; read += USB_PACKET_SIZE - state->packet_pos;
// read next packet // 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); ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
// prepare next packet // prepare next packet
state->packet_index++; 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 // consume remaining message
int remaining_chunks = (msg_size - (USB_PACKET_SIZE - MSG_HEADER1_LEN)) / (USB_PACKET_SIZE - MSG_HEADER2_LEN); 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++) { 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); ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
} }

View File

@ -4,6 +4,9 @@
#include <stdint.h> #include <stdint.h>
#include "secbool.h" #include "secbool.h"
#define USE_WEBUSB 0
#define USB_TIMEOUT 100
#define USB_PACKET_SIZE 64 #define USB_PACKET_SIZE 64
#define USB_IFACE_NUM 0 #define USB_IFACE_NUM 0