From 838b2c2c770fb8b148c933c92c909da0cc72212c Mon Sep 17 00:00:00 2001 From: Andrew Kozlik Date: Tue, 9 Feb 2021 19:07:48 +0100 Subject: [PATCH] fix(legacy): Replace magic numbers with USB_PACKET_SIZE and MSG_HEADER_SIZE. --- legacy/firmware/messages.c | 55 +++++++++++++++++------------- legacy/firmware/udp.c | 6 ++-- legacy/firmware/usb.c | 70 ++++++++++++++++++++------------------ legacy/firmware/usb.h | 2 ++ 4 files changed, 73 insertions(+), 60 deletions(-) diff --git a/legacy/firmware/messages.c b/legacy/firmware/messages.c index 5a8ba385a..9ff77f1f7 100644 --- a/legacy/firmware/messages.c +++ b/legacy/firmware/messages.c @@ -25,12 +25,16 @@ #include "memzero.h" #include "messages.h" #include "trezor.h" +#include "usb.h" #include "util.h" #include "messages.pb.h" #include "pb_decode.h" #include "pb_encode.h" +// The size of the message header "?##<2 bytes msg_id><4 bytes msg_size>". +#define MSG_HEADER_SIZE 9 + struct MessagesMap_t { char type; // n = normal, d = debug char dir; // i = in, o = out @@ -84,14 +88,14 @@ static uint8_t msg_debug_out[MSG_DEBUG_OUT_SIZE]; static inline void msg_out_append(uint8_t c) { if (msg_out_cur == 0) { - msg_out[msg_out_end * 64] = '?'; + msg_out[msg_out_end * USB_PACKET_SIZE] = '?'; msg_out_cur = 1; } - msg_out[msg_out_end * 64 + msg_out_cur] = c; + msg_out[msg_out_end * USB_PACKET_SIZE + msg_out_cur] = c; msg_out_cur++; - if (msg_out_cur == 64) { + if (msg_out_cur == USB_PACKET_SIZE) { msg_out_cur = 0; - msg_out_end = (msg_out_end + 1) % (MSG_OUT_SIZE / 64); + msg_out_end = (msg_out_end + 1) % (MSG_OUT_SIZE / USB_PACKET_SIZE); } } @@ -99,14 +103,15 @@ static inline void msg_out_append(uint8_t c) { static inline void msg_debug_out_append(uint8_t c) { if (msg_debug_out_cur == 0) { - msg_debug_out[msg_debug_out_end * 64] = '?'; + msg_debug_out[msg_debug_out_end * USB_PACKET_SIZE] = '?'; msg_debug_out_cur = 1; } - msg_debug_out[msg_debug_out_end * 64 + msg_debug_out_cur] = c; + msg_debug_out[msg_debug_out_end * USB_PACKET_SIZE + msg_debug_out_cur] = c; msg_debug_out_cur++; - if (msg_debug_out_cur == 64) { + if (msg_debug_out_cur == USB_PACKET_SIZE) { msg_debug_out_cur = 0; - msg_debug_out_end = (msg_debug_out_end + 1) % (MSG_DEBUG_OUT_SIZE / 64); + msg_debug_out_end = + (msg_debug_out_end + 1) % (MSG_DEBUG_OUT_SIZE / USB_PACKET_SIZE); } } @@ -114,24 +119,25 @@ static inline void msg_debug_out_append(uint8_t c) { static inline void msg_out_pad(void) { if (msg_out_cur == 0) return; - while (msg_out_cur < 64) { - msg_out[msg_out_end * 64 + msg_out_cur] = 0; + while (msg_out_cur < USB_PACKET_SIZE) { + msg_out[msg_out_end * USB_PACKET_SIZE + msg_out_cur] = 0; msg_out_cur++; } msg_out_cur = 0; - msg_out_end = (msg_out_end + 1) % (MSG_OUT_SIZE / 64); + msg_out_end = (msg_out_end + 1) % (MSG_OUT_SIZE / USB_PACKET_SIZE); } #if DEBUG_LINK static inline void msg_debug_out_pad(void) { if (msg_debug_out_cur == 0) return; - while (msg_debug_out_cur < 64) { - msg_debug_out[msg_debug_out_end * 64 + msg_debug_out_cur] = 0; + while (msg_debug_out_cur < USB_PACKET_SIZE) { + msg_debug_out[msg_debug_out_end * USB_PACKET_SIZE + msg_debug_out_cur] = 0; msg_debug_out_cur++; } msg_debug_out_cur = 0; - msg_debug_out_end = (msg_debug_out_end + 1) % (MSG_DEBUG_OUT_SIZE / 64); + msg_debug_out_end = + (msg_debug_out_end + 1) % (MSG_DEBUG_OUT_SIZE / USB_PACKET_SIZE); } #endif @@ -233,7 +239,7 @@ void msg_read_common(char type, const uint8_t *buf, uint32_t len) { static uint32_t msg_pos = 0; static const pb_msgdesc_t *fields = 0; - if (len != 64) return; + if (len != USB_PACKET_SIZE) return; if (read_state == READSTATE_IDLE) { if (buf[0] != '?' || buf[1] != '#' || @@ -257,8 +263,8 @@ void msg_read_common(char type, const uint8_t *buf, uint32_t len) { read_state = READSTATE_READING; - memcpy(msg_in, buf + 9, len - 9); - msg_pos = len - 9; + memcpy(msg_in, buf + MSG_HEADER_SIZE, len - MSG_HEADER_SIZE); + msg_pos = len - MSG_HEADER_SIZE; } else if (read_state == READSTATE_READING) { if (buf[0] != '?') { // invalid contents read_state = READSTATE_IDLE; @@ -281,8 +287,8 @@ void msg_read_common(char type, const uint8_t *buf, uint32_t len) { const uint8_t *msg_out_data(void) { if (msg_out_start == msg_out_end) return 0; - uint8_t *data = msg_out + (msg_out_start * 64); - msg_out_start = (msg_out_start + 1) % (MSG_OUT_SIZE / 64); + uint8_t *data = msg_out + (msg_out_start * USB_PACKET_SIZE); + msg_out_start = (msg_out_start + 1) % (MSG_OUT_SIZE / USB_PACKET_SIZE); debugLog(0, "", "msg_out_data"); return data; } @@ -291,8 +297,9 @@ const uint8_t *msg_out_data(void) { const uint8_t *msg_debug_out_data(void) { if (msg_debug_out_start == msg_debug_out_end) return 0; - uint8_t *data = msg_debug_out + (msg_debug_out_start * 64); - msg_debug_out_start = (msg_debug_out_start + 1) % (MSG_DEBUG_OUT_SIZE / 64); + uint8_t *data = msg_debug_out + (msg_debug_out_start * USB_PACKET_SIZE); + msg_debug_out_start = + (msg_debug_out_start + 1) % (MSG_DEBUG_OUT_SIZE / USB_PACKET_SIZE); debugLog(0, "", "msg_debug_out_data"); return data; } @@ -314,19 +321,19 @@ _Static_assert(sizeof(msg_tiny) >= sizeof(DebugLinkGetState), uint16_t msg_tiny_id = 0xFFFF; void msg_read_tiny(const uint8_t *buf, int len) { - if (len != 64) return; + if (len != USB_PACKET_SIZE) return; if (buf[0] != '?' || buf[1] != '#' || buf[2] != '#') { return; } uint16_t msg_id = (buf[3] << 8) + buf[4]; uint32_t msg_size = ((uint32_t)buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8]; - if (msg_size > 64 || len - msg_size < 9) { + if (msg_size > USB_PACKET_SIZE || len - msg_size < MSG_HEADER_SIZE) { return; } const pb_msgdesc_t *fields = 0; - pb_istream_t stream = pb_istream_from_buffer(buf + 9, msg_size); + pb_istream_t stream = pb_istream_from_buffer(buf + MSG_HEADER_SIZE, msg_size); switch (msg_id) { case MessageType_MessageType_PinMatrixAck: diff --git a/legacy/firmware/udp.c b/legacy/firmware/udp.c index 52538713a..7d0a71d2e 100644 --- a/legacy/firmware/udp.c +++ b/legacy/firmware/udp.c @@ -38,7 +38,7 @@ void usbInit(void) { emulatorSocketInit(); } void usbPoll(void) { emulatorPoll(); - static uint8_t buffer[64]; + static uint8_t buffer[USB_PACKET_SIZE]; int iface = 0; if (emulatorSocketRead(&iface, buffer, sizeof(buffer)) > 0) { @@ -51,13 +51,13 @@ void usbPoll(void) { const uint8_t *data = msg_out_data(); if (data != NULL) { - emulatorSocketWrite(0, data, 64); + emulatorSocketWrite(0, data, USB_PACKET_SIZE); } #if DEBUG_LINK data = msg_debug_out_data(); if (data != NULL) { - emulatorSocketWrite(1, data, 64); + emulatorSocketWrite(1, data, USB_PACKET_SIZE); } #endif } diff --git a/legacy/firmware/usb.c b/legacy/firmware/usb.c index e0ec5b6a8..0f4398b63 100644 --- a/legacy/firmware/usb.c +++ b/legacy/firmware/usb.c @@ -91,7 +91,7 @@ static const struct usb_device_descriptor dev_descr = { .bDeviceClass = 0, .bDeviceSubClass = 0, .bDeviceProtocol = 0, - .bMaxPacketSize0 = 64, + .bMaxPacketSize0 = USB_PACKET_SIZE, .idVendor = 0x1209, .idProduct = 0x53c1, .bcdDevice = 0x0100, @@ -148,7 +148,7 @@ static const struct usb_endpoint_descriptor hid_endpoints_u2f[2] = { .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = ENDPOINT_ADDRESS_U2F_IN, .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 64, + .wMaxPacketSize = USB_PACKET_SIZE, .bInterval = 1, }, { @@ -156,7 +156,7 @@ static const struct usb_endpoint_descriptor hid_endpoints_u2f[2] = { .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = ENDPOINT_ADDRESS_U2F_OUT, .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 64, + .wMaxPacketSize = USB_PACKET_SIZE, .bInterval = 1, }}; @@ -185,7 +185,7 @@ static const struct usb_endpoint_descriptor webusb_endpoints_debug[2] = { .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = ENDPOINT_ADDRESS_DEBUG_IN, .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 64, + .wMaxPacketSize = USB_PACKET_SIZE, .bInterval = 1, }, { @@ -193,7 +193,7 @@ static const struct usb_endpoint_descriptor webusb_endpoints_debug[2] = { .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = ENDPOINT_ADDRESS_DEBUG_OUT, .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 64, + .wMaxPacketSize = USB_PACKET_SIZE, .bInterval = 1, }}; @@ -220,7 +220,7 @@ static const struct usb_endpoint_descriptor webusb_endpoints_main[2] = { .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = ENDPOINT_ADDRESS_MAIN_IN, .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 64, + .wMaxPacketSize = USB_PACKET_SIZE, .bInterval = 1, }, { @@ -228,7 +228,7 @@ static const struct usb_endpoint_descriptor webusb_endpoints_main[2] = { .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = ENDPOINT_ADDRESS_MAIN_OUT, .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 64, + .wMaxPacketSize = USB_PACKET_SIZE, .bInterval = 1, }}; @@ -303,10 +303,12 @@ static enum usbd_request_return_codes hid_control_request( static void u2f_rx_callback(usbd_device *dev, uint8_t ep) { (void)ep; - static CONFIDENTIAL uint8_t buf[64] __attribute__((aligned(4))); + static CONFIDENTIAL uint8_t buf[USB_PACKET_SIZE] __attribute__((aligned(4))); debugLog(0, "", "u2f_rx_callback"); - if (usbd_ep_read_packet(dev, ENDPOINT_ADDRESS_U2F_OUT, buf, 64) != 64) return; + if (usbd_ep_read_packet(dev, ENDPOINT_ADDRESS_U2F_OUT, buf, sizeof(buf)) != + USB_PACKET_SIZE) + return; u2fhid_read(tiny, (const U2FHID_FRAME *)(void *)buf); } @@ -314,14 +316,15 @@ static void u2f_rx_callback(usbd_device *dev, uint8_t ep) { static void main_rx_callback(usbd_device *dev, uint8_t ep) { (void)ep; - static CONFIDENTIAL uint8_t buf[64] __attribute__((aligned(4))); - if (usbd_ep_read_packet(dev, ENDPOINT_ADDRESS_MAIN_OUT, buf, 64) != 64) + static CONFIDENTIAL uint8_t buf[USB_PACKET_SIZE] __attribute__((aligned(4))); + if (usbd_ep_read_packet(dev, ENDPOINT_ADDRESS_MAIN_OUT, buf, sizeof(buf)) != + USB_PACKET_SIZE) return; debugLog(0, "", "main_rx_callback"); if (!tiny) { - msg_read(buf, 64); + msg_read(buf, sizeof(buf)); } else { - msg_read_tiny(buf, 64); + msg_read_tiny(buf, sizeof(buf)); } } @@ -329,14 +332,15 @@ static void main_rx_callback(usbd_device *dev, uint8_t ep) { static void debug_rx_callback(usbd_device *dev, uint8_t ep) { (void)ep; - static uint8_t buf[64] __attribute__((aligned(4))); - if (usbd_ep_read_packet(dev, ENDPOINT_ADDRESS_DEBUG_OUT, buf, 64) != 64) + static uint8_t buf[USB_PACKET_SIZE] __attribute__((aligned(4))); + if (usbd_ep_read_packet(dev, ENDPOINT_ADDRESS_DEBUG_OUT, buf, sizeof(buf)) != + USB_PACKET_SIZE) return; debugLog(0, "", "debug_rx_callback"); if (!tiny) { - msg_debug_read(buf, 64); + msg_debug_read(buf, sizeof(buf)); } else { - msg_read_tiny(buf, 64); + msg_read_tiny(buf, sizeof(buf)); } } @@ -345,21 +349,21 @@ static void debug_rx_callback(usbd_device *dev, uint8_t ep) { static void set_config(usbd_device *dev, uint16_t wValue) { (void)wValue; - usbd_ep_setup(dev, ENDPOINT_ADDRESS_MAIN_IN, USB_ENDPOINT_ATTR_INTERRUPT, 64, - 0); - usbd_ep_setup(dev, ENDPOINT_ADDRESS_MAIN_OUT, USB_ENDPOINT_ATTR_INTERRUPT, 64, - main_rx_callback); + usbd_ep_setup(dev, ENDPOINT_ADDRESS_MAIN_IN, USB_ENDPOINT_ATTR_INTERRUPT, + USB_PACKET_SIZE, 0); + usbd_ep_setup(dev, ENDPOINT_ADDRESS_MAIN_OUT, USB_ENDPOINT_ATTR_INTERRUPT, + USB_PACKET_SIZE, main_rx_callback); #if U2F_ENABLED - usbd_ep_setup(dev, ENDPOINT_ADDRESS_U2F_IN, USB_ENDPOINT_ATTR_INTERRUPT, 64, - 0); - usbd_ep_setup(dev, ENDPOINT_ADDRESS_U2F_OUT, USB_ENDPOINT_ATTR_INTERRUPT, 64, - u2f_rx_callback); + usbd_ep_setup(dev, ENDPOINT_ADDRESS_U2F_IN, USB_ENDPOINT_ATTR_INTERRUPT, + USB_PACKET_SIZE, 0); + usbd_ep_setup(dev, ENDPOINT_ADDRESS_U2F_OUT, USB_ENDPOINT_ATTR_INTERRUPT, + USB_PACKET_SIZE, u2f_rx_callback); #endif #if DEBUG_LINK - usbd_ep_setup(dev, ENDPOINT_ADDRESS_DEBUG_IN, USB_ENDPOINT_ATTR_INTERRUPT, 64, - 0); + usbd_ep_setup(dev, ENDPOINT_ADDRESS_DEBUG_IN, USB_ENDPOINT_ATTR_INTERRUPT, + USB_PACKET_SIZE, 0); usbd_ep_setup(dev, ENDPOINT_ADDRESS_DEBUG_OUT, USB_ENDPOINT_ATTR_INTERRUPT, - 64, debug_rx_callback); + USB_PACKET_SIZE, debug_rx_callback); #endif #if U2F_ENABLED usbd_register_control_callback( @@ -406,15 +410,15 @@ void usbPoll(void) { // write pending data data = msg_out_data(); if (data) { - while (usbd_ep_write_packet(usbd_dev, ENDPOINT_ADDRESS_MAIN_IN, data, 64) != - 64) { + while (usbd_ep_write_packet(usbd_dev, ENDPOINT_ADDRESS_MAIN_IN, data, + USB_PACKET_SIZE) != USB_PACKET_SIZE) { } } #if U2F_ENABLED data = u2f_out_data(); if (data) { - while (usbd_ep_write_packet(usbd_dev, ENDPOINT_ADDRESS_U2F_IN, data, 64) != - 64) { + while (usbd_ep_write_packet(usbd_dev, ENDPOINT_ADDRESS_U2F_IN, data, + USB_PACKET_SIZE) != USB_PACKET_SIZE) { } } #endif @@ -423,7 +427,7 @@ void usbPoll(void) { data = msg_debug_out_data(); if (data) { while (usbd_ep_write_packet(usbd_dev, ENDPOINT_ADDRESS_DEBUG_IN, data, - 64) != 64) { + USB_PACKET_SIZE) != USB_PACKET_SIZE) { } } #endif diff --git a/legacy/firmware/usb.h b/legacy/firmware/usb.h index 8b6007f5b..f1fc55837 100644 --- a/legacy/firmware/usb.h +++ b/legacy/firmware/usb.h @@ -20,6 +20,8 @@ #ifndef __USB_H__ #define __USB_H__ +#define USB_PACKET_SIZE 64 + void usbInit(void); void usbPoll(void); void usbReconnect(void);