From 24c9b76b6e563bd68b683ec2176387008b550f4e 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 | 10 ++++++++++ src/rust/bitbox02-sys/build.rs | 1 + src/rust/bitbox02/src/sd.rs | 14 ++++++++++++++ src/sd.c | 12 ++++++++++-- src/sd.h | 3 +-- 6 files changed, 37 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..5985ea2e9 100644 --- a/src/rust/bitbox02-rust/src/hww/api/backup.rs +++ b/src/rust/bitbox02-rust/src/hww/api/backup.rs @@ -89,6 +89,16 @@ 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::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..dd61e1150 100644 --- a/src/rust/bitbox02/src/sd.rs +++ b/src/rust/bitbox02/src/sd.rs @@ -30,6 +30,20 @@ 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 sd_format() -> bool { + unsafe { bitbox02_sys::sd_format() } +} + struct SdList(bitbox02_sys::sd_list_t); impl Drop for SdList { diff --git a/src/sd.c b/src/sd.c index 2b281d27f..25061f04b 100644 --- a/src/sd.c +++ b/src/sd.c @@ -370,7 +370,16 @@ bool sd_erase_file_in_subdir(const char* fn, const char* subdir) return status; } -#ifdef TESTING +bool sd_card_vfat_formatted(void) +{ + if (!_mount()) { + return -1; + } + int result = fs.fs_type == FS_FAT32; + _unmount(); + return result; +} + bool sd_format(void) { const MKFS_PARM params = { @@ -384,4 +393,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