1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00

embed: fix wur for usb functions

This commit is contained in:
Pavol Rusnak 2018-01-01 21:48:39 +01:00
parent f1a75b36f7
commit 424115967f
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
8 changed files with 24 additions and 22 deletions

View File

@ -182,9 +182,6 @@ static secbool bootloader_loop(secbool firmware_present)
for (;;) {
int r = usb_hid_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE, 100);
if (r != USB_PACKET_SIZE) {
continue;
}
ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
uint16_t msg_id;
uint32_t msg_size;

View File

@ -58,7 +58,8 @@ 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
ensure(usb_hid_write_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, 100), NULL);
int r = usb_hid_write_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, 100);
ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
// prepare new packet
state->packet_index++;
memset(state->buf, 0, USB_PACKET_SIZE);
@ -78,7 +79,8 @@ static void _usb_write_flush(usb_write_state *state)
memset(state->buf + state->packet_pos, 0, USB_PACKET_SIZE - state->packet_pos);
}
// send packet
ensure(usb_hid_write_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, 100), NULL);
int r = usb_hid_write_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, 100);
ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
}
static secbool _send_msg(uint8_t iface_num, uint16_t msg_id, const pb_field_t fields[], const void *msg)
@ -157,7 +159,8 @@ 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
ensure(usb_hid_read_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, 100), NULL);
int r = usb_hid_read_blocking(state->iface_num, state->buf, USB_PACKET_SIZE, 100);
ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
// prepare next packet
state->packet_index++;
state->packet_pos = MSG_HEADER2_LEN;
@ -485,9 +488,7 @@ void process_msg_unknown(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
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 (r != USB_PACKET_SIZE) {
break;
}
ensure(sectrue * (r == USB_PACKET_SIZE), NULL);
}
MSG_SEND_INIT(Failure);

View File

@ -61,7 +61,7 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, mp_obj_t t
}
} else
if (mode == POLL_READ) {
if (usb_hid_can_read(iface)) {
if (sectrue == usb_hid_can_read(iface)) {
uint8_t buf[64];
int l = usb_hid_read(iface, buf, sizeof(buf));
if (l > 0) {
@ -72,7 +72,7 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, mp_obj_t t
}
} else
if (mode == POLL_WRITE) {
if (usb_hid_can_write(iface)) {
if (sectrue == usb_hid_can_write(iface)) {
ret->items[0] = MP_OBJ_NEW_SMALL_INT(i);
ret->items[1] = mp_const_none;
return mp_const_true;

View File

@ -7,13 +7,15 @@ static int vcp_iface_num = -1;
int mp_hal_stdin_rx_chr(void) {
ensure(sectrue * (vcp_iface_num >= 0), "vcp stdio is not configured");
uint8_t c = 0;
ensure(usb_vcp_read_blocking(vcp_iface_num, &c, 1, -1), NULL);
int r = usb_vcp_read_blocking(vcp_iface_num, &c, 1, -1);
(void)r;
return c;
}
void mp_hal_stdout_tx_strn(const char *str, size_t len) {
if (vcp_iface_num >= 0) {
ensure(usb_vcp_write_blocking(vcp_iface_num, (const uint8_t *)str, len, 0), NULL);
int r = usb_vcp_write_blocking(vcp_iface_num, (const uint8_t *)str, len, 0);
(void)r;
}
}

View File

@ -32,14 +32,16 @@ static void vcp_intr(void)
static void vcp_puts(const char *s, size_t len)
{
ensure(usb_vcp_write_blocking(VCP_IFACE, (const uint8_t *) s, len, -1), NULL);
int r = usb_vcp_write_blocking(VCP_IFACE, (const uint8_t *) s, len, -1);
(void)r;
}
static char vcp_getchar(void)
{
uint8_t c = 0;
ensure(usb_vcp_read_blocking(VCP_IFACE, &c, 1, -1), NULL);
return (char) c;
int r = usb_vcp_read_blocking(VCP_IFACE, &c, 1, -1);
(void)r;
return (char)c;
}
static void vcp_readline(char *buf, size_t len)

View File

@ -182,7 +182,7 @@ int usb_hid_read_select(uint32_t timeout) {
const uint32_t start = HAL_GetTick();
for (;;) {
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
if (usb_hid_can_read(i)) {
if (sectrue == usb_hid_can_read(i)) {
return i;
}
}
@ -196,7 +196,7 @@ int usb_hid_read_select(uint32_t timeout) {
int usb_hid_read_blocking(uint8_t iface_num, uint8_t *buf, uint32_t len, int timeout) {
const uint32_t start = HAL_GetTick();
while (!usb_hid_can_read(iface_num)) {
while (sectrue != usb_hid_can_read(iface_num)) {
if (timeout >= 0 && HAL_GetTick() - start >= timeout) {
return 0; // Timeout
}
@ -207,7 +207,7 @@ int usb_hid_read_blocking(uint8_t iface_num, uint8_t *buf, uint32_t len, int tim
int usb_hid_write_blocking(uint8_t iface_num, const uint8_t *buf, uint32_t len, int timeout) {
const uint32_t start = HAL_GetTick();
while (!usb_hid_can_write(iface_num)) {
while (sectrue != usb_hid_can_write(iface_num)) {
if (timeout >= 0 && HAL_GetTick() - start >= timeout) {
return 0; // Timeout
}

View File

@ -293,7 +293,7 @@ int usb_vcp_write(uint8_t iface_num, const uint8_t *buf, uint32_t len) {
int usb_vcp_read_blocking(uint8_t iface_num, uint8_t *buf, uint32_t len, int timeout) {
uint32_t start = HAL_GetTick();
while (!usb_vcp_can_read(iface_num)) {
while (sectrue != usb_vcp_can_read(iface_num)) {
if (timeout >= 0 && HAL_GetTick() - start >= timeout) {
return 0; // Timeout
}
@ -304,7 +304,7 @@ int usb_vcp_read_blocking(uint8_t iface_num, uint8_t *buf, uint32_t len, int tim
int usb_vcp_write_blocking(uint8_t iface_num, const uint8_t *buf, uint32_t len, int timeout) {
uint32_t start = HAL_GetTick();
while (!usb_vcp_can_write(iface_num)) {
while (sectrue != usb_vcp_can_write(iface_num)) {
if (timeout >= 0 && HAL_GetTick() - start >= timeout) {
return 0; // Timeout
}

View File

@ -106,7 +106,7 @@ int usb_hid_read(uint8_t iface_num, uint8_t *buf, uint32_t len) {
static const char *ping_req = "PINGPING";
static const char *ping_resp = "PONGPONG";
if (r == strlen(ping_req) && memcmp(ping_req, buf, strlen(ping_req)) == 0) {
ensure(usb_hid_write(0, (const uint8_t *)ping_resp, strlen(ping_resp)), NULL);
usb_hid_write(0, (const uint8_t *)ping_resp, strlen(ping_resp));
return 0;
}
return r;