Skip to content

Commit

Permalink
Merge branch 'handle-panics'
Browse files Browse the repository at this point in the history
  • Loading branch information
Beerosagos committed Dec 16, 2024
2 parents fd7f793 + 7265e60 commit 402201e
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/rust/bitbox02-rust-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod workflow;
fn panic(info: &core::panic::PanicInfo) -> ! {
::util::log::log!("{}", info);
#[cfg(feature = "firmware")]
bitbox02_rust::print_debug!(0, "Error: {}", info);
bitbox02_rust::print_screen!(0, "Error: {}", info);
loop {}
}

Expand Down
8 changes: 8 additions & 0 deletions src/rust/bitbox02-rust/src/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@

#[macro_use]
pub mod screen;

/// displays the input error message on the screen and enters
/// an infinite loop.
#[allow(clippy::empty_loop)]
pub fn abort(err: &str) -> ! {
print_screen!(0, "Error: {}", err);
loop {}
}
4 changes: 2 additions & 2 deletions src/rust/bitbox02-rust/src/general/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ pub fn print_debug_internal(duration: Duration, msg: &str) {
/// ```no_run
/// # #[macro_use] extern crate bitbox02_rust; fn main() {
/// let my_str = "abc";
/// print_debug!(1000, "{}", &my_str);
/// print_screen!(1000, "{}", &my_str);
/// # }
/// ```
#[macro_export]
macro_rules! print_debug {
macro_rules! print_screen {
($duration:expr, $($arg:tt)*) => ({
extern crate alloc;
let duration = core::time::Duration::from_millis($duration);
Expand Down
11 changes: 8 additions & 3 deletions src/rust/bitbox02-rust/src/hww/api/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::pb;

use pb::response::Response;

use crate::general::abort;
use crate::workflow::{confirm, mnemonic, password, status, unlock};

pub async fn from_file(request: &pb::RestoreBackupRequest) -> Result<Response, Error> {
Expand Down Expand Up @@ -75,7 +76,9 @@ pub async fn from_file(request: &pb::RestoreBackupRequest) -> Result<Response, E
}

bitbox02::memory::set_initialized().or(Err(Error::Memory))?;
bitbox02::keystore::unlock(&password).expect("restore_from_file: unlock failed");
if bitbox02::keystore::unlock(&password).is_err() {
abort("restore_from_file: unlock failed");
};

// Ignore non-critical error.
let _ = bitbox02::memory::set_device_name(&metadata.name);
Expand Down Expand Up @@ -144,9 +147,11 @@ pub async fn from_mnemonic(
}

bitbox02::memory::set_initialized().or(Err(Error::Memory))?;

// This should never fail.
bitbox02::keystore::unlock(&password).expect("restore_from_mnemonic: unlock failed");
if bitbox02::keystore::unlock(&password).is_err() {
abort("restore_from_mnemonic: unlock failed");
};

unlock::unlock_bip39().await;
Ok(Response::Success(pb::Success {}))
}
8 changes: 5 additions & 3 deletions src/rust/bitbox02-rust/src/workflow/unlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::general::abort;
use crate::workflow::confirm;
use crate::workflow::password;
use crate::workflow::status::status;
Expand Down Expand Up @@ -116,9 +117,10 @@ pub async fn unlock_bip39() {
}
}

bitbox02::ui::with_lock_animation(|| {
keystore::unlock_bip39(&mnemonic_passphrase).expect("bip39 unlock failed");
});
let result = bitbox02::ui::with_lock_animation(|| keystore::unlock_bip39(&mnemonic_passphrase));
if result.is_err() {
abort("bip39 unlock failed");
}
}

/// Invokes the unlock workflow. This function does not finish until the keystore is unlocked, or
Expand Down
5 changes: 3 additions & 2 deletions src/rust/bitbox02/src/ui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,11 @@ pub fn trinary_input_string_set_input(component: &mut Component, word: &str) {
}
}

pub fn with_lock_animation<F: Fn()>(f: F) {
pub fn with_lock_animation<F: Fn() -> R, R>(f: F) -> R {
unsafe { bitbox02_sys::lock_animation_start() };
f();
let result = f();
unsafe { bitbox02_sys::lock_animation_stop() };
result
}

pub fn screen_stack_pop_all() {
Expand Down
2 changes: 1 addition & 1 deletion src/rust/bitbox02/src/ui/ui_stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub fn trinary_input_string_set_input(_component: &mut Component, _word: &str) {
panic!("not implemented")
}

pub fn with_lock_animation<F: Fn()>(f: F) {
pub fn with_lock_animation<F: Fn() -> R, R>(f: F) -> R {
f()
}

Expand Down
2 changes: 1 addition & 1 deletion src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub fn trinary_input_string_set_input(_component: &mut Component, _word: &str) {
panic!("not implemented")
}

pub fn with_lock_animation<F: Fn()>(f: F) {
pub fn with_lock_animation<F: Fn() -> R, R>(f: F) -> R {
f()
}

Expand Down

0 comments on commit 402201e

Please sign in to comment.