mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-11 16:00:57 +00:00
fix(legacy): cleanup of undesired states where USB processing should not happen
This commit is contained in:
parent
e0754d1609
commit
28d282101e
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include "bitmaps.h"
|
#include "bitmaps.h"
|
||||||
#include "firmware/usb.h"
|
#include "firmware/usb.h"
|
||||||
#include "hmac_drbg.h"
|
#include "hmac_drbg.h"
|
||||||
@ -83,7 +84,17 @@ void __assert_func(const char *file, int line, const char *func,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void hal_delay(uint32_t ms) { usbSleep(ms); }
|
void hal_delay(uint32_t ms) {
|
||||||
|
#if EMULATOR
|
||||||
|
usleep(ms * 1000);
|
||||||
|
#else
|
||||||
|
uint32_t start = timer_ms();
|
||||||
|
|
||||||
|
while ((timer_ms() - start) < ms) {
|
||||||
|
asm("nop");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void drbg_init() {
|
void drbg_init() {
|
||||||
uint8_t entropy[48] = {0};
|
uint8_t entropy[48] = {0};
|
||||||
|
1
legacy/firmware/.changelog.d/2107.fixed
Normal file
1
legacy/firmware/.changelog.d/2107.fixed
Normal file
@ -0,0 +1 @@
|
|||||||
|
Fix legacy technical debt in USB handling (readability and FSM unwanted states).
|
@ -564,7 +564,7 @@ void config_setHomescreen(const uint8_t *data, uint32_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void get_root_node_callback(uint32_t iter, uint32_t total) {
|
static void get_root_node_callback(uint32_t iter, uint32_t total) {
|
||||||
usbSleep(1);
|
waitAndProcessUSBRequests(1);
|
||||||
layoutProgress(_("Waking up"), 1000 * iter / total);
|
layoutProgress(_("Waking up"), 1000 * iter / total);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ bool protectButton(ButtonRequestType type, bool confirm_only) {
|
|||||||
|
|
||||||
// button acked - check buttons
|
// button acked - check buttons
|
||||||
if (acked) {
|
if (acked) {
|
||||||
usbSleep(5);
|
waitAndProcessUSBRequests(5);
|
||||||
buttonUpdate();
|
buttonUpdate();
|
||||||
if (button.YesUp) {
|
if (button.YesUp) {
|
||||||
result = true;
|
result = true;
|
||||||
|
@ -421,7 +421,7 @@ static void recovery_digit(const char digit) {
|
|||||||
oledInvert(x + 1, y, x + 62, y + 9);
|
oledInvert(x + 1, y, x + 62, y + 9);
|
||||||
oledRefresh();
|
oledRefresh();
|
||||||
usbTiny(1);
|
usbTiny(1);
|
||||||
usbSleep(250);
|
waitAndProcessUSBRequests(250);
|
||||||
usbTiny(0);
|
usbTiny(0);
|
||||||
|
|
||||||
/* index of the chosen word */
|
/* index of the chosen word */
|
||||||
|
@ -77,13 +77,13 @@ void check_lock_screen(void) {
|
|||||||
// wait until NoButton is released
|
// wait until NoButton is released
|
||||||
usbTiny(1);
|
usbTiny(1);
|
||||||
do {
|
do {
|
||||||
usbSleep(5);
|
waitAndProcessUSBRequests(5);
|
||||||
buttonUpdate();
|
buttonUpdate();
|
||||||
} while (!button.NoUp);
|
} while (!button.NoUp);
|
||||||
|
|
||||||
// wait for confirmation/cancellation of the dialog
|
// wait for confirmation/cancellation of the dialog
|
||||||
do {
|
do {
|
||||||
usbSleep(5);
|
waitAndProcessUSBRequests(5);
|
||||||
buttonUpdate();
|
buttonUpdate();
|
||||||
} while (!button.YesUp && !button.NoUp);
|
} while (!button.YesUp && !button.NoUp);
|
||||||
usbTiny(0);
|
usbTiny(0);
|
||||||
@ -180,7 +180,7 @@ int main(void) {
|
|||||||
usbInit();
|
usbInit();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
#if EMULATOR
|
#if EMULATOR
|
||||||
usbSleep(10);
|
waitAndProcessUSBRequests(10);
|
||||||
#else
|
#else
|
||||||
usbPoll();
|
usbPoll();
|
||||||
#endif
|
#endif
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "usb.h"
|
#include "usb.h"
|
||||||
|
|
||||||
@ -35,7 +36,7 @@ void usbInit(void) { emulatorSocketInit(); }
|
|||||||
#define _ISDBG ('n')
|
#define _ISDBG ('n')
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void usbSleep(uint32_t millis) {
|
void waitAndProcessUSBRequests(uint32_t millis) {
|
||||||
emulatorPoll();
|
emulatorPoll();
|
||||||
|
|
||||||
static uint8_t buffer[USB_PACKET_SIZE];
|
static uint8_t buffer[USB_PACKET_SIZE];
|
||||||
@ -63,7 +64,7 @@ void usbSleep(uint32_t millis) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void usbPoll(void) { usbSleep(0); }
|
void usbPoll(void) { waitAndProcessUSBRequests(0); }
|
||||||
|
|
||||||
char usbTiny(char set) {
|
char usbTiny(char set) {
|
||||||
char old = tiny;
|
char old = tiny;
|
||||||
@ -71,4 +72,10 @@ char usbTiny(char set) {
|
|||||||
return old;
|
return old;
|
||||||
}
|
}
|
||||||
|
|
||||||
void usbFlush(uint32_t millis) { usbSleep(millis); }
|
void usbFlush(uint32_t millis) {
|
||||||
|
const uint8_t *data;
|
||||||
|
while ((data = msg_out_data()) != NULL) {
|
||||||
|
emulatorSocketWrite(0, data, USB_PACKET_SIZE);
|
||||||
|
}
|
||||||
|
usleep(millis * 1000);
|
||||||
|
}
|
||||||
|
@ -448,7 +448,7 @@ char usbTiny(char set) {
|
|||||||
return old;
|
return old;
|
||||||
}
|
}
|
||||||
|
|
||||||
void usbSleep(uint32_t millis) {
|
void waitAndProcessUSBRequests(uint32_t millis) {
|
||||||
uint32_t start = timer_ms();
|
uint32_t start = timer_ms();
|
||||||
|
|
||||||
while ((timer_ms() - start) < millis) {
|
while ((timer_ms() - start) < millis) {
|
||||||
|
@ -25,8 +25,34 @@
|
|||||||
void usbInit(void);
|
void usbInit(void);
|
||||||
void usbPoll(void);
|
void usbPoll(void);
|
||||||
void usbReconnect(void);
|
void usbReconnect(void);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Setting this value to 1 will limit the protobuf messages `usbPoll` and
|
||||||
|
* `waitAndProcessUSBRequests` can handle to a few defined in `msg_read_tiny`.
|
||||||
|
*
|
||||||
|
* Also affects U2F and DebugLink messages.
|
||||||
|
*
|
||||||
|
* Setting to 1 is meant to prevent infinite recursion when you need to read a
|
||||||
|
* message while being called from FSM.
|
||||||
|
*
|
||||||
|
* Setting to 0 allows processing all messages.
|
||||||
|
*/
|
||||||
char usbTiny(char set);
|
char usbTiny(char set);
|
||||||
void usbSleep(uint32_t millis);
|
|
||||||
|
/*
|
||||||
|
* This will wait given number of milliseconds for arrival of protobuf message.
|
||||||
|
* If it arrives, it will service it before returning.
|
||||||
|
*
|
||||||
|
* If you call this function from any function that is called from FSM,
|
||||||
|
* you must use `usbTiny(1)` before and `usbTiny(oldTinyValue)` or `usbTiny(0)`
|
||||||
|
* after, otherwise there is possibility of stack exhaustion.
|
||||||
|
*/
|
||||||
|
void waitAndProcessUSBRequests(uint32_t millis);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Flush out any messages still in USB bus FIFO while waiting given number
|
||||||
|
* of milliseconds. Any incoming USB protobuf messages are not serviced.
|
||||||
|
*/
|
||||||
void usbFlush(uint32_t millis);
|
void usbFlush(uint32_t millis);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user