Skip to content

Commit

Permalink
sd: prompt user for formatting if format is not VFAT compatible
Browse files Browse the repository at this point in the history
This commit inserts a check to validate whether the sdcard where the
backup will be put is VFAT compatible or not. VFAT compatilibity is
crucial for using the card and lack of it could result in errors in the
firmware. Therefore, when setting up the device, this commit validates
it and if it is not compatible, it asks user whether he/she is okay with
formatting or not. When the user confirms, SD card is formatted
accordingly and the fresh SD card is ready to be used by BitBox02.

Signed-off-by: asi345 <inanata15@gmail.com>
  • Loading branch information
asi345 committed Dec 18, 2024
1 parent 5ce4a18 commit 2f770bc
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ customers cannot upgrade their bootloader, its changes are recorded separately.
- Update manufacturer HID descriptor to bitbox.swiss
- Ethereum: remove deprecated Goerli network
- SD card: solve backup bug when sd card is re-inserted
- SD card: prompt user for formatting if sd is not VFAT compatible

### 9.21.0
- Bitcoin: add support for sending to silent payment (BIP-352) addresses
Expand Down
11 changes: 11 additions & 0 deletions src/rust/bitbox02-rust/src/hww/api/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ pub async fn create(
})
.await?;

let is_vfat_formatted = bitbox02::sd::sdcard_vfat_formatted();
if !is_vfat_formatted {
confirm::confirm(&confirm::Params {
title: "SD card\nformatting needed",
body: "This will erase all\ndata on the SD card.",
..Default::default()
})
.await?;
bitbox02::sd::format()?;
}

let is_initialized = bitbox02::memory::is_initialized();

if is_initialized {
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 @@ -117,6 +117,7 @@ const ALLOWLIST_FNS: &[&str] = &[
"screen_saver_disable",
"screen_saver_enable",
"sd_card_inserted",
"sd_card_vfat_formatted",
"sd_erase_file_in_subdir",
"sd_format",
"sd_free_list",
Expand Down
17 changes: 17 additions & 0 deletions src/rust/bitbox02/src/sd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ pub fn sdcard_inserted() -> bool {
data.sdcard_inserted.unwrap()
}

#[cfg(not(feature = "testing"))]
pub fn sdcard_vfat_formatted() -> bool {
unsafe { bitbox02_sys::sd_card_vfat_formatted() }
}

#[cfg(feature = "testing")]
pub fn sdcard_vfat_formatted() -> bool {
true
}

pub fn format() -> Result<(), ()> {
match unsafe { bitbox02_sys::sd_format() } {
true => Ok(()),
false => Err(()),
}
}

struct SdList(bitbox02_sys::sd_list_t);

impl Drop for SdList {
Expand Down
10 changes: 8 additions & 2 deletions src/sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,14 @@ bool sd_erase_file_in_subdir(const char* fn, const char* subdir)
return status;
}

#ifdef TESTING
bool sd_card_vfat_formatted(void)
{
memset(&fs, 0, sizeof(FATFS));
FRESULT res = f_mount(&fs, "", 1);
f_unmount("");
return res == FR_OK;
}

bool sd_format(void)
{
const MKFS_PARM params = {
Expand All @@ -384,4 +391,3 @@ bool sd_format(void)
uint8_t work[FF_MAX_SS] = {0};
return f_mkfs("SD", &params, work, sizeof(work)) == FR_OK;
}
#endif
3 changes: 1 addition & 2 deletions src/sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ USE_RESULT bool sd_write_bin(
uint16_t length,
bool replace);

#ifdef TESTING
USE_RESULT bool sd_card_vfat_formatted(void);
USE_RESULT bool sd_format(void);
#endif

#endif

0 comments on commit 2f770bc

Please sign in to comment.