mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-26 09:28:13 +00:00
trezorhal: add option to programatically enable/disable WinUSB
This commit is contained in:
parent
d1b5a254ef
commit
4f44c2577b
@ -41,6 +41,7 @@ static void usb_init_all(void) {
|
|||||||
.product = "TREZOR",
|
.product = "TREZOR",
|
||||||
.serial_number = "000000000000000000000000",
|
.serial_number = "000000000000000000000000",
|
||||||
.interface = "TREZOR Interface",
|
.interface = "TREZOR Interface",
|
||||||
|
.winusb_enabled = sectrue,
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint8_t rx_buffer[USB_PACKET_SIZE];
|
static uint8_t rx_buffer[USB_PACKET_SIZE];
|
||||||
|
@ -40,7 +40,8 @@ static const char *get_0str(mp_obj_t o, size_t min_len, size_t max_len) {
|
|||||||
/// manufacturer: str='',
|
/// manufacturer: str='',
|
||||||
/// product: str='',
|
/// product: str='',
|
||||||
/// serial_number: str='',
|
/// serial_number: str='',
|
||||||
/// interface: str='') -> None:
|
/// interface: str='',
|
||||||
|
/// winusb_enabled: bool=True) -> None:
|
||||||
/// '''
|
/// '''
|
||||||
/// '''
|
/// '''
|
||||||
STATIC mp_obj_t mod_trezorio_USB_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
STATIC mp_obj_t mod_trezorio_USB_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
||||||
@ -56,6 +57,7 @@ STATIC mp_obj_t mod_trezorio_USB_make_new(const mp_obj_type_t *type, size_t n_ar
|
|||||||
{ MP_QSTR_product, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
|
{ MP_QSTR_product, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
|
||||||
{ MP_QSTR_serial_number, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
|
{ MP_QSTR_serial_number, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
|
||||||
{ MP_QSTR_interface, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
|
{ MP_QSTR_interface, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
|
||||||
|
{ MP_QSTR_winusb_enabled, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
|
||||||
};
|
};
|
||||||
mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)];
|
mp_arg_val_t vals[MP_ARRAY_SIZE(allowed_args)];
|
||||||
mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals);
|
mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, vals);
|
||||||
@ -70,6 +72,7 @@ STATIC mp_obj_t mod_trezorio_USB_make_new(const mp_obj_type_t *type, size_t n_ar
|
|||||||
const char *product = get_0str(vals[7].u_obj, 0, 32);
|
const char *product = get_0str(vals[7].u_obj, 0, 32);
|
||||||
const char *serial_number = get_0str(vals[8].u_obj, 0, 32);
|
const char *serial_number = get_0str(vals[8].u_obj, 0, 32);
|
||||||
const char *interface = get_0str(vals[9].u_obj, 0, 32);
|
const char *interface = get_0str(vals[9].u_obj, 0, 32);
|
||||||
|
const secbool winusb_enabled = vals[10].u_bool ? sectrue : secfalse;
|
||||||
|
|
||||||
CHECK_PARAM_RANGE(device_class, 0, 255)
|
CHECK_PARAM_RANGE(device_class, 0, 255)
|
||||||
CHECK_PARAM_RANGE(device_subclass, 0, 255)
|
CHECK_PARAM_RANGE(device_subclass, 0, 255)
|
||||||
@ -105,6 +108,8 @@ STATIC mp_obj_t mod_trezorio_USB_make_new(const mp_obj_type_t *type, size_t n_ar
|
|||||||
o->info.product = product;
|
o->info.product = product;
|
||||||
o->info.serial_number = serial_number;
|
o->info.serial_number = serial_number;
|
||||||
o->info.interface = interface;
|
o->info.interface = interface;
|
||||||
|
o->info.winusb_enabled = winusb_enabled;
|
||||||
|
|
||||||
mp_obj_list_init(&o->ifaces, 0);
|
mp_obj_list_init(&o->ifaces, 0);
|
||||||
|
|
||||||
return MP_OBJ_FROM_PTR(o);
|
return MP_OBJ_FROM_PTR(o);
|
||||||
|
@ -96,6 +96,7 @@ static void usb_init_all(void)
|
|||||||
.product = "TREZOR",
|
.product = "TREZOR",
|
||||||
.serial_number = "000000000000",
|
.serial_number = "000000000000",
|
||||||
.interface = "TREZOR Interface",
|
.interface = "TREZOR Interface",
|
||||||
|
.winusb_enabled = secfalse,
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint8_t tx_packet[VCP_PACKET_LEN];
|
static uint8_t tx_packet[VCP_PACKET_LEN];
|
||||||
|
@ -11,8 +11,6 @@
|
|||||||
#include "usb.h"
|
#include "usb.h"
|
||||||
#include "usbd_core.h"
|
#include "usbd_core.h"
|
||||||
|
|
||||||
#define USE_WINUSB 1
|
|
||||||
|
|
||||||
#define USB_MAX_CONFIG_DESC_SIZE 256
|
#define USB_MAX_CONFIG_DESC_SIZE 256
|
||||||
#define USB_MAX_STR_SIZE 62
|
#define USB_MAX_STR_SIZE 62
|
||||||
#define USB_MAX_STR_DESC_SIZE (USB_MAX_STR_SIZE * 2 + 2)
|
#define USB_MAX_STR_DESC_SIZE (USB_MAX_STR_SIZE * 2 + 2)
|
||||||
@ -25,13 +23,11 @@
|
|||||||
#error Unable to determine proper USB_PHY_ID to use
|
#error Unable to determine proper USB_PHY_ID to use
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if USE_WINUSB
|
|
||||||
#define USB_WINUSB_VENDOR_CODE '!' // arbitrary, but must be equivalent to the last character in extra string
|
#define USB_WINUSB_VENDOR_CODE '!' // arbitrary, but must be equivalent to the last character in extra string
|
||||||
#define USB_WINUSB_EXTRA_STRING 'M', 0x00, 'S', 0x00, 'F', 0x00, 'T', 0x00, '1', 0x00, '0', 0x00, '0', 0x00, USB_WINUSB_VENDOR_CODE , 0x00 // MSFT100!
|
#define USB_WINUSB_EXTRA_STRING 'M', 0x00, 'S', 0x00, 'F', 0x00, 'T', 0x00, '1', 0x00, '0', 0x00, '0', 0x00, USB_WINUSB_VENDOR_CODE , 0x00 // MSFT100!
|
||||||
#define USB_WINUSB_EXTRA_STRING_INDEX 0xEE
|
#define USB_WINUSB_EXTRA_STRING_INDEX 0xEE
|
||||||
#define USB_WINUSB_REQ_GET_COMPATIBLE_ID_FEATURE_DESCRIPTOR 0x04
|
#define USB_WINUSB_REQ_GET_COMPATIBLE_ID_FEATURE_DESCRIPTOR 0x04
|
||||||
#define USB_WINUSB_REQ_GET_EXTENDED_PROPERTIES_OS_FEATURE_DESCRIPTOR 0x05
|
#define USB_WINUSB_REQ_GET_EXTENDED_PROPERTIES_OS_FEATURE_DESCRIPTOR 0x05
|
||||||
#endif
|
|
||||||
|
|
||||||
#define UNCONST(X) ((uint8_t *)(X))
|
#define UNCONST(X) ((uint8_t *)(X))
|
||||||
|
|
||||||
@ -52,6 +48,8 @@ static USBD_HandleTypeDef usb_dev_handle;
|
|||||||
static const USBD_DescriptorsTypeDef usb_descriptors;
|
static const USBD_DescriptorsTypeDef usb_descriptors;
|
||||||
static const USBD_ClassTypeDef usb_class;
|
static const USBD_ClassTypeDef usb_class;
|
||||||
|
|
||||||
|
static secbool usb_winusb_enabled = secfalse;
|
||||||
|
|
||||||
static secbool __wur check_desc_str(const char *s) {
|
static secbool __wur check_desc_str(const char *s) {
|
||||||
if (NULL == s) return secfalse;
|
if (NULL == s) return secfalse;
|
||||||
if (strlen(s) > USB_MAX_STR_SIZE) return secfalse;
|
if (strlen(s) > USB_MAX_STR_SIZE) return secfalse;
|
||||||
@ -87,6 +85,9 @@ void usb_init(const usb_dev_info_t *dev_info) {
|
|||||||
usb_str_table.serial_number = dev_info->serial_number;
|
usb_str_table.serial_number = dev_info->serial_number;
|
||||||
usb_str_table.interface = dev_info->interface;
|
usb_str_table.interface = dev_info->interface;
|
||||||
|
|
||||||
|
// enable/disable WinUSB
|
||||||
|
usb_winusb_enabled = dev_info->winusb_enabled;
|
||||||
|
|
||||||
// Configuration descriptor
|
// Configuration descriptor
|
||||||
usb_config_desc->bLength = sizeof(usb_config_descriptor_t);
|
usb_config_desc->bLength = sizeof(usb_config_descriptor_t);
|
||||||
usb_config_desc->bDescriptorType = USB_DESC_TYPE_CONFIGURATION;
|
usb_config_desc->bDescriptorType = USB_DESC_TYPE_CONFIGURATION;
|
||||||
@ -316,9 +317,8 @@ static uint8_t usb_class_setup(USBD_HandleTypeDef *dev, USBD_SetupReqTypedef *re
|
|||||||
return USBD_FAIL;
|
return USBD_FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#if USE_WINUSB
|
|
||||||
else
|
else
|
||||||
if (req->bRequest == USB_WINUSB_VENDOR_CODE) {
|
if (sectrue == usb_winusb_enabled && req->bRequest == USB_WINUSB_VENDOR_CODE) {
|
||||||
if (req->wIndex == USB_WINUSB_REQ_GET_COMPATIBLE_ID_FEATURE_DESCRIPTOR) {
|
if (req->wIndex == USB_WINUSB_REQ_GET_COMPATIBLE_ID_FEATURE_DESCRIPTOR) {
|
||||||
static const uint8_t winusb_wcid[] = {
|
static const uint8_t winusb_wcid[] = {
|
||||||
// header
|
// header
|
||||||
@ -341,11 +341,9 @@ static uint8_t usb_class_setup(USBD_HandleTypeDef *dev, USBD_SetupReqTypedef *re
|
|||||||
return USBD_FAIL;
|
return USBD_FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
#if USE_WINUSB
|
|
||||||
if ((req->bmRequest & USB_REQ_RECIPIENT_MASK) == USB_REQ_RECIPIENT_INTERFACE) {
|
if ((req->bmRequest & USB_REQ_RECIPIENT_MASK) == USB_REQ_RECIPIENT_INTERFACE) {
|
||||||
if (req->bRequest == USB_WINUSB_VENDOR_CODE) {
|
if (sectrue == usb_winusb_enabled && req->bRequest == USB_WINUSB_VENDOR_CODE) {
|
||||||
if (req->wIndex == USB_WINUSB_REQ_GET_EXTENDED_PROPERTIES_OS_FEATURE_DESCRIPTOR &&
|
if (req->wIndex == USB_WINUSB_REQ_GET_EXTENDED_PROPERTIES_OS_FEATURE_DESCRIPTOR &&
|
||||||
(req->wValue & 0xFF) == 0) { // reply only if interface is 0
|
(req->wValue & 0xFF) == 0) { // reply only if interface is 0
|
||||||
static const uint8_t winusb_guid[] = {
|
static const uint8_t winusb_guid[] = {
|
||||||
@ -370,7 +368,6 @@ static uint8_t usb_class_setup(USBD_HandleTypeDef *dev, USBD_SetupReqTypedef *re
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
} else if (req->wIndex >= USBD_MAX_NUM_INTERFACES) {
|
} else if (req->wIndex >= USBD_MAX_NUM_INTERFACES) {
|
||||||
USBD_CtlError(dev, req);
|
USBD_CtlError(dev, req);
|
||||||
return USBD_FAIL;
|
return USBD_FAIL;
|
||||||
@ -445,19 +442,18 @@ static uint8_t *usb_class_get_cfg_desc(uint16_t *length) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t *usb_class_get_usrstr_desc(USBD_HandleTypeDef *dev, uint8_t index, uint16_t *length) {
|
static uint8_t *usb_class_get_usrstr_desc(USBD_HandleTypeDef *dev, uint8_t index, uint16_t *length) {
|
||||||
#if USE_WINUSB
|
if (sectrue == usb_winusb_enabled && index == USB_WINUSB_EXTRA_STRING_INDEX) {
|
||||||
static const uint8_t winusb_string_descriptor[] = {
|
static const uint8_t winusb_string_descriptor[] = {
|
||||||
0x12, // bLength
|
0x12, // bLength
|
||||||
USB_DESC_TYPE_STRING, // bDescriptorType
|
USB_DESC_TYPE_STRING, // bDescriptorType
|
||||||
USB_WINUSB_EXTRA_STRING // wData
|
USB_WINUSB_EXTRA_STRING // wData
|
||||||
};
|
};
|
||||||
if (index == USB_WINUSB_EXTRA_STRING_INDEX) {
|
|
||||||
*length = sizeof(winusb_string_descriptor);
|
*length = sizeof(winusb_string_descriptor);
|
||||||
return UNCONST(winusb_string_descriptor);
|
return UNCONST(winusb_string_descriptor);
|
||||||
|
} else {
|
||||||
|
*length = 0;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
*length = 0;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const USBD_ClassTypeDef usb_class = {
|
static const USBD_ClassTypeDef usb_class = {
|
||||||
|
@ -103,6 +103,7 @@ typedef struct {
|
|||||||
const char *product;
|
const char *product;
|
||||||
const char *serial_number;
|
const char *serial_number;
|
||||||
const char *interface;
|
const char *interface;
|
||||||
|
secbool winusb_enabled;
|
||||||
} usb_dev_info_t;
|
} usb_dev_info_t;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
Loading…
Reference in New Issue
Block a user