Skip to content

Commit

Permalink
sdcard: solve backup bug when sd is re-inserted
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
asi345 committed Oct 16, 2024
1 parent 55c77a7 commit c39697b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/rust/bitbox02-rust/src/hww/api/sdcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::workflow::sdcard;
pub async fn process(
&pb::InsertRemoveSdCardRequest { action }: &pb::InsertRemoveSdCardRequest,
) -> Result<Response, Error> {
bitbox02::sd::sdcard_init();
let inserted = bitbox02::sd::sdcard_inserted();
match SdCardAction::try_from(action) {
Ok(SdCardAction::InsertCard) => {}
Expand Down
1 change: 1 addition & 0 deletions src/rust/bitbox02-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/rust/bitbox02/src/sd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ 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() }
Expand Down
6 changes: 6 additions & 0 deletions src/sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "screen.h"
#include "sd.h"
#include "util.h"
#include <sd_mmc/sd_mmc_start.h>

#include <ff.h>

Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c39697b

Please sign in to comment.