From 2f770bc0f39091be0d7c25ee1b254af54bcbfde3 Mon Sep 17 00:00:00 2001 From: asi345 Date: Wed, 27 Nov 2024 16:35:17 +0100 Subject: [PATCH] sd: prompt user for formatting if format is not VFAT compatible 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 --- CHANGELOG.md | 1 + src/rust/bitbox02-rust/src/hww/api/backup.rs | 11 +++++++++++ src/rust/bitbox02-sys/build.rs | 1 + src/rust/bitbox02/src/sd.rs | 17 +++++++++++++++++ src/sd.c | 10 ++++++++-- src/sd.h | 3 +-- 6 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd17117b4..d04bef9d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/rust/bitbox02-rust/src/hww/api/backup.rs b/src/rust/bitbox02-rust/src/hww/api/backup.rs index c26e50d34..f960aaf8c 100644 --- a/src/rust/bitbox02-rust/src/hww/api/backup.rs +++ b/src/rust/bitbox02-rust/src/hww/api/backup.rs @@ -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 { diff --git a/src/rust/bitbox02-sys/build.rs b/src/rust/bitbox02-sys/build.rs index 7bc80b41e..3122ee4fa 100644 --- a/src/rust/bitbox02-sys/build.rs +++ b/src/rust/bitbox02-sys/build.rs @@ -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", diff --git a/src/rust/bitbox02/src/sd.rs b/src/rust/bitbox02/src/sd.rs index feda371bf..b4c9e3479 100644 --- a/src/rust/bitbox02/src/sd.rs +++ b/src/rust/bitbox02/src/sd.rs @@ -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 { diff --git a/src/sd.c b/src/sd.c index 2b281d27f..7dc5e423e 100644 --- a/src/sd.c +++ b/src/sd.c @@ -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 = { @@ -384,4 +391,3 @@ bool sd_format(void) uint8_t work[FF_MAX_SS] = {0}; return f_mkfs("SD", ¶ms, work, sizeof(work)) == FR_OK; } -#endif diff --git a/src/sd.h b/src/sd.h index f39d3aeeb..b9fe7dccf 100644 --- a/src/sd.h +++ b/src/sd.h @@ -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