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 30, 2024
1 parent 6494a12 commit f4c67e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ customers cannot upgrade their bootloader, its changes are recorded separately.
### [Unreleased]
- Update manufacturer HID descriptor to bitbox.swiss
- Ethereum: remove deprecated Goerli network
- 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
19 changes: 14 additions & 5 deletions src/sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef TESTING
#include "driver_init.h"
#include "sd_mmc.h"
#include "sd_mmc/sd_mmc_start.h"
#endif

#include "flags.h"
Expand Down Expand Up @@ -107,7 +108,14 @@ static bool _mount(void)
sd_mmc_resume_clock();
#endif
memset(&fs, 0, sizeof(FATFS));
if (f_mount(&fs, "SD", 1) == FR_INVALID_DRIVE) {
int res = f_mount(&fs, "", 1);
if (res == FR_DISK_ERR) {
#ifndef TESTING
sd_mmc_start();
#endif
res = f_mount(&fs, "", 1);
}
if (res == FR_INVALID_DRIVE) {
#ifndef TESTING
sd_mmc_pause_clock();
#endif
Expand All @@ -117,11 +125,11 @@ static bool _mount(void)
}

/**
* Unmunts an SD card and pauses the bus clock.
* Unmounts an SD card and pauses the bus clock.
*/
static void _unmount(void)
{
f_mount(NULL, "SD", 1);
f_unmount("");
#ifndef TESTING
sd_mmc_pause_clock();
#endif
Expand Down Expand Up @@ -268,7 +276,8 @@ bool sd_list_subdir(sd_list_t* list_out, const char* subdir)
if (list_out->num_files == allocated_files) {
char** new_list_out_files;
allocated_files *= 2;
new_list_out_files = (char**)realloc(list_out->files, sizeof(char*) * allocated_files);
new_list_out_files =
(char**)realloc((void*)list_out->files, sizeof(char*) * allocated_files);
if (new_list_out_files == NULL) {
sd_free_list(list_out);
Abort("Error: realloc sd_list_subdir");
Expand All @@ -295,7 +304,7 @@ void sd_free_list(sd_list_t* list)
util_zero(list->files[i], strlen(list->files[i]));
free(list->files[i]);
}
free(list->files);
free((void*)list->files);
list->files = NULL;
}

Expand Down

0 comments on commit f4c67e8

Please sign in to comment.