From 4642b94a244b12182ba230d418b2dfa97bcc1c29 Mon Sep 17 00:00:00 2001 From: asi345 Date: Wed, 16 Oct 2024 15:16:38 +0200 Subject: [PATCH] sdcard: solve backup bug when sd is re-inserted This issue was existent for a long time. While using the BitBox02, when the sdcard is plugged into the device and the user unplugs and replugs it, backup operations (e.g., list backups, restore from backup) failed and throwed error from the firmware side. Sdcard backup operations first check if the sdcard is inserted before calling sdcard interface funtions. This commit fixes the re-insertion problem by reinitializing the sdcard whenever it is checked if it is inserted. The problem earlier most probably stems from sdcard being in an unexpected state when it is re-inserted. The forced initialization step fixes the broken states. Signed-off-by: asi345 --- CHANGELOG.md | 1 + src/rust/bitbox02-rust/src/hww/api/sdcard.rs | 1 + src/rust/bitbox02-sys/build.rs | 1 + src/rust/bitbox02/src/sd.rs | 9 +++++++++ src/sd.c | 6 ++++++ src/sd.h | 1 + 6 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf05a41c8..627920597 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ customers cannot upgrade their bootloader, its changes are recorded separately. ### [Unreleased] - Update manufacturer HID descriptor to bitbox.swiss +- SD card: solve backup bug when sd card is re-inserted ### 9.21.0 - Bitcoin: add support for sending to silent payment (BIP-352) addresses diff --git a/src/rust/bitbox02-rust/src/hww/api/sdcard.rs b/src/rust/bitbox02-rust/src/hww/api/sdcard.rs index a93d7ae46..0942c991f 100644 --- a/src/rust/bitbox02-rust/src/hww/api/sdcard.rs +++ b/src/rust/bitbox02-rust/src/hww/api/sdcard.rs @@ -23,6 +23,7 @@ use crate::workflow::sdcard; pub async fn process( &pb::InsertRemoveSdCardRequest { action }: &pb::InsertRemoveSdCardRequest, ) -> Result { + bitbox02::sd::sdcard_init(); let inserted = bitbox02::sd::sdcard_inserted(); match SdCardAction::try_from(action) { Ok(SdCardAction::InsertCard) => {} diff --git a/src/rust/bitbox02-sys/build.rs b/src/rust/bitbox02-sys/build.rs index 7bc80b41e..a1003c69d 100644 --- a/src/rust/bitbox02-sys/build.rs +++ b/src/rust/bitbox02-sys/build.rs @@ -116,6 +116,7 @@ const ALLOWLIST_FNS: &[&str] = &[ "screen_process", "screen_saver_disable", "screen_saver_enable", + "sd_card_init", "sd_card_inserted", "sd_erase_file_in_subdir", "sd_format", diff --git a/src/rust/bitbox02/src/sd.rs b/src/rust/bitbox02/src/sd.rs index feda371bf..04b931e04 100644 --- a/src/rust/bitbox02/src/sd.rs +++ b/src/rust/bitbox02/src/sd.rs @@ -19,6 +19,15 @@ use alloc::vec::Vec; use crate::util::str_to_cstr_vec; use bitbox02_sys::SD_MAX_FILE_SIZE; +#[cfg(not(feature = "testing"))] +pub fn sdcard_init() -> () { + unsafe { bitbox02_sys::sd_card_init() } +} + +#[cfg(feature = "testing")] +pub fn sdcard_init() -> () { +} + #[cfg(not(feature = "testing"))] pub fn sdcard_inserted() -> bool { unsafe { bitbox02_sys::sd_card_inserted() } diff --git a/src/sd.c b/src/sd.c index 9ad53f477..1c6810c41 100644 --- a/src/sd.c +++ b/src/sd.c @@ -27,6 +27,7 @@ #include "screen.h" #include "sd.h" #include "util.h" +#include #include @@ -92,6 +93,11 @@ static bool _get_absolute_path( return true; } +void sd_card_init(void) +{ + sd_mmc_start(); +} + /** * Checks if an SD card is inserted and, if so, mounts it. * Resumes the bus clock. If mounting fails, pauses the bus clock. diff --git a/src/sd.h b/src/sd.h index f39d3aeeb..01fc1536d 100644 --- a/src/sd.h +++ b/src/sd.h @@ -28,6 +28,7 @@ typedef struct { char** files; } sd_list_t; +void sd_card_init(void); // the caller must free list_out using sd_free_list(). list_out itself is not // freed, as it could be passed as a stack var. USE_RESULT bool sd_list(sd_list_t* list_out);