From 9287dd7e04d3347403ea536a20de891e005e17a9 Mon Sep 17 00:00:00 2001 From: Jochen Hoenicke Date: Thu, 7 Jul 2016 18:37:37 +0200 Subject: [PATCH] Poll USB during BIP39 derivation. This patch adds calls to usbPoll in the progress callback. This should address #98. We call usbDelay instead of Poll, to call usbd_poll several times. Otherwise it would only handle one event instead of handling all events that were pending so far. The ugly magic number 5 is a guess. Note that we also need to set usbTiny, so that we don't recursively process messages. Since we don't know whether usbTiny is set, we need to store the old value (especially true for u2f). This fix also relies on another fix in libopencm3. --- firmware/storage.c | 4 ++++ firmware/usb.c | 4 +++- firmware/usb.h | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/firmware/storage.c b/firmware/storage.c index 8ef55fce77..7a13f41314 100644 --- a/firmware/storage.c +++ b/firmware/storage.c @@ -39,6 +39,7 @@ #include "debug.h" #include "protect.h" #include "layout2.h" +#include "usb.h" Storage storage; @@ -337,6 +338,7 @@ void storage_setHomescreen(const uint8_t *data, uint32_t size) void get_root_node_callback(uint32_t iter, uint32_t total) { + usbDelay(10); // handle up to ten usb interrupts. layoutProgress("Waking up", 1000 * iter / total); } @@ -361,7 +363,9 @@ const uint8_t *storage_getSeed(bool usePassphrase) storage_show_error(); } } + char oldTiny = usbTiny(1); mnemonic_to_seed(storage.mnemonic, usePassphrase ? sessionPassphrase : "", sessionSeed, get_root_node_callback); // BIP-0039 + usbTiny(oldTiny); sessionSeedCached = true; sessionSeedUsesPassphrase = usePassphrase; return sessionSeed; diff --git a/firmware/usb.c b/firmware/usb.c index 56504deee8..241d05616a 100644 --- a/firmware/usb.c +++ b/firmware/usb.c @@ -419,9 +419,11 @@ void usbReconnect(void) usbd_disconnect(usbd_dev, 0); } -void usbTiny(char set) +char usbTiny(char set) { + char old = tiny; tiny = set; + return old; } void usbDelay(int cycles) diff --git a/firmware/usb.h b/firmware/usb.h index d1dc250187..286e3a4a4d 100644 --- a/firmware/usb.h +++ b/firmware/usb.h @@ -23,7 +23,7 @@ void usbInit(void); void usbPoll(void); void usbReconnect(void); -void usbTiny(char set); +char usbTiny(char set); void usbDelay(int cycles); #endif