|
|
|
@ -25,16 +25,12 @@
|
|
|
|
|
#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
|
|
|
|
@ -72,17 +68,22 @@ void MessageProcessFunc(char type, char dir, uint16_t msg_id, void *ptr) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Buffer for outgoing USB packets.
|
|
|
|
|
static uint32_t msg_out_start = 0;
|
|
|
|
|
static uint32_t msg_out_end = 0;
|
|
|
|
|
static uint32_t msg_out_cur = 0;
|
|
|
|
|
static uint8_t msg_out[MSG_OUT_SIZE];
|
|
|
|
|
static uint8_t msg_out[MSG_OUT_BUFFER_SIZE];
|
|
|
|
|
_Static_assert(MSG_OUT_BUFFER_SIZE % USB_PACKET_SIZE == 0,
|
|
|
|
|
"MSG_OUT_BUFFER_SIZE");
|
|
|
|
|
|
|
|
|
|
#if DEBUG_LINK
|
|
|
|
|
|
|
|
|
|
static uint32_t msg_debug_out_start = 0;
|
|
|
|
|
static uint32_t msg_debug_out_end = 0;
|
|
|
|
|
static uint32_t msg_debug_out_cur = 0;
|
|
|
|
|
static uint8_t msg_debug_out[MSG_DEBUG_OUT_SIZE];
|
|
|
|
|
static uint8_t msg_debug_out[MSG_DEBUG_OUT_BUFFER_SIZE];
|
|
|
|
|
_Static_assert(MSG_DEBUG_OUT_BUFFER_SIZE % USB_PACKET_SIZE == 0,
|
|
|
|
|
"MSG_DEBUG_OUT_BUFFER_SIZE");
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
@ -95,7 +96,7 @@ static inline void msg_out_append(uint8_t c) {
|
|
|
|
|
msg_out_cur++;
|
|
|
|
|
if (msg_out_cur == USB_PACKET_SIZE) {
|
|
|
|
|
msg_out_cur = 0;
|
|
|
|
|
msg_out_end = (msg_out_end + 1) % (MSG_OUT_SIZE / USB_PACKET_SIZE);
|
|
|
|
|
msg_out_end = (msg_out_end + 1) % (MSG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -111,7 +112,7 @@ static inline void msg_debug_out_append(uint8_t c) {
|
|
|
|
|
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 / USB_PACKET_SIZE);
|
|
|
|
|
(msg_debug_out_end + 1) % (MSG_DEBUG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -124,7 +125,7 @@ static inline void msg_out_pad(void) {
|
|
|
|
|
msg_out_cur++;
|
|
|
|
|
}
|
|
|
|
|
msg_out_cur = 0;
|
|
|
|
|
msg_out_end = (msg_out_end + 1) % (MSG_OUT_SIZE / USB_PACKET_SIZE);
|
|
|
|
|
msg_out_end = (msg_out_end + 1) % (MSG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if DEBUG_LINK
|
|
|
|
@ -137,7 +138,7 @@ static inline void msg_debug_out_pad(void) {
|
|
|
|
|
}
|
|
|
|
|
msg_debug_out_cur = 0;
|
|
|
|
|
msg_debug_out_end =
|
|
|
|
|
(msg_debug_out_end + 1) % (MSG_DEBUG_OUT_SIZE / USB_PACKET_SIZE);
|
|
|
|
|
(msg_debug_out_end + 1) % (MSG_DEBUG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
@ -219,13 +220,13 @@ enum {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void msg_process(char type, uint16_t msg_id, const pb_msgdesc_t *fields,
|
|
|
|
|
uint8_t *msg_raw, uint32_t msg_size) {
|
|
|
|
|
static uint8_t msg_data[MSG_IN_SIZE];
|
|
|
|
|
memzero(msg_data, sizeof(msg_data));
|
|
|
|
|
pb_istream_t stream = pb_istream_from_buffer(msg_raw, msg_size);
|
|
|
|
|
bool status = pb_decode(&stream, fields, msg_data);
|
|
|
|
|
uint8_t *msg_encoded, uint32_t msg_encoded_size) {
|
|
|
|
|
static uint8_t msg_decoded[MSG_IN_DECODED_SIZE];
|
|
|
|
|
memzero(msg_decoded, sizeof(msg_decoded));
|
|
|
|
|
pb_istream_t stream = pb_istream_from_buffer(msg_encoded, msg_encoded_size);
|
|
|
|
|
bool status = pb_decode(&stream, fields, msg_decoded);
|
|
|
|
|
if (status) {
|
|
|
|
|
MessageProcessFunc(type, 'i', msg_id, msg_data);
|
|
|
|
|
MessageProcessFunc(type, 'i', msg_id, msg_decoded);
|
|
|
|
|
} else {
|
|
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, stream.errmsg);
|
|
|
|
|
}
|
|
|
|
@ -233,9 +234,9 @@ void msg_process(char type, uint16_t msg_id, const pb_msgdesc_t *fields,
|
|
|
|
|
|
|
|
|
|
void msg_read_common(char type, const uint8_t *buf, uint32_t len) {
|
|
|
|
|
static char read_state = READSTATE_IDLE;
|
|
|
|
|
static uint8_t msg_in[MSG_IN_SIZE];
|
|
|
|
|
static uint8_t msg_encoded[MSG_IN_ENCODED_SIZE];
|
|
|
|
|
static uint16_t msg_id = 0xFFFF;
|
|
|
|
|
static uint32_t msg_size = 0;
|
|
|
|
|
static uint32_t msg_encoded_size = 0;
|
|
|
|
|
static uint32_t msg_pos = 0;
|
|
|
|
|
static const pb_msgdesc_t *fields = 0;
|
|
|
|
|
|
|
|
|
@ -247,7 +248,7 @@ void msg_read_common(char type, const uint8_t *buf, uint32_t len) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
msg_id = (buf[3] << 8) + buf[4];
|
|
|
|
|
msg_size =
|
|
|
|
|
msg_encoded_size =
|
|
|
|
|
((uint32_t)buf[5] << 24) + (buf[6] << 16) + (buf[7] << 8) + buf[8];
|
|
|
|
|
|
|
|
|
|
fields = MessageFields(type, 'i', msg_id);
|
|
|
|
@ -256,14 +257,14 @@ void msg_read_common(char type, const uint8_t *buf, uint32_t len) {
|
|
|
|
|
_("Unknown message"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (msg_size > MSG_IN_SIZE) { // message is too big :(
|
|
|
|
|
if (msg_encoded_size > MSG_IN_ENCODED_SIZE) { // message is too big :(
|
|
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Message too big"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
read_state = READSTATE_READING;
|
|
|
|
|
|
|
|
|
|
memcpy(msg_in, buf + MSG_HEADER_SIZE, len - MSG_HEADER_SIZE);
|
|
|
|
|
memcpy(msg_encoded, 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
|
|
|
|
@ -272,14 +273,14 @@ void msg_read_common(char type, const uint8_t *buf, uint32_t len) {
|
|
|
|
|
}
|
|
|
|
|
/* raw data starts at buf + 1 with len - 1 bytes */
|
|
|
|
|
buf++;
|
|
|
|
|
len = MIN(len - 1, MSG_IN_SIZE - msg_pos);
|
|
|
|
|
len = MIN(len - 1, MSG_IN_ENCODED_SIZE - msg_pos);
|
|
|
|
|
|
|
|
|
|
memcpy(msg_in + msg_pos, buf, len);
|
|
|
|
|
memcpy(msg_encoded + msg_pos, buf, len);
|
|
|
|
|
msg_pos += len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg_pos >= msg_size) {
|
|
|
|
|
msg_process(type, msg_id, fields, msg_in, msg_size);
|
|
|
|
|
if (msg_pos >= msg_encoded_size) {
|
|
|
|
|
msg_process(type, msg_id, fields, msg_encoded, msg_encoded_size);
|
|
|
|
|
msg_pos = 0;
|
|
|
|
|
read_state = READSTATE_IDLE;
|
|
|
|
|
}
|
|
|
|
@ -288,7 +289,7 @@ 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 * USB_PACKET_SIZE);
|
|
|
|
|
msg_out_start = (msg_out_start + 1) % (MSG_OUT_SIZE / USB_PACKET_SIZE);
|
|
|
|
|
msg_out_start = (msg_out_start + 1) % (MSG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
|
|
|
|
debugLog(0, "", "msg_out_data");
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
@ -299,7 +300,7 @@ 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 * USB_PACKET_SIZE);
|
|
|
|
|
msg_debug_out_start =
|
|
|
|
|
(msg_debug_out_start + 1) % (MSG_DEBUG_OUT_SIZE / USB_PACKET_SIZE);
|
|
|
|
|
(msg_debug_out_start + 1) % (MSG_DEBUG_OUT_BUFFER_SIZE / USB_PACKET_SIZE);
|
|
|
|
|
debugLog(0, "", "msg_debug_out_data");
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|