From f4c67e8047ed57ebe571d8d4c6c34f048f87abd5 Mon Sep 17 00:00:00 2001 From: asi345 Date: Wed, 16 Oct 2024 15:16:38 +0200 Subject: [PATCH] sdcard: solve backup bug when sd is re-inserted 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 --- CHANGELOG.md | 1 + src/sd.c | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eda90891..cd17117b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/sd.c b/src/sd.c index 9ad53f477..201aea368 100644 --- a/src/sd.c +++ b/src/sd.c @@ -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" @@ -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 @@ -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 @@ -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"); @@ -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; }