Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced t-echo file system integrity check #3818

Merged
merged 11 commits into from
May 11, 2024
86 changes: 78 additions & 8 deletions src/FSCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,62 @@ void rmDir(const char *dirname)
#endif
}

bool fsCheck()
{
#if defined(ARCH_NRF52)
size_t write_size = 0;
size_t read_size = 0;
char buf[32] = {0};

Adafruit_LittleFS_Namespace::File file(FSCom);
const char *text = "meshtastic fs test";
size_t text_length = strlen(text);
const char *filename = "/meshtastic.txt";

LOG_DEBUG("Try create file .\n");
if (file.open(filename, FILE_O_WRITE)) {
write_size = file.write(text);
} else {
LOG_DEBUG("Open file failed .\n");
goto FORMAT_FS;
}

if (write_size != text_length) {
LOG_DEBUG("Text bytes do not match .\n");
file.close();
goto FORMAT_FS;
}

file.close();

if (!file.open(filename, FILE_O_READ)) {
LOG_DEBUG("Open file failed .\n");
goto FORMAT_FS;
}

read_size = file.readBytes(buf, text_length);
if (read_size != text_length) {
LOG_DEBUG("Text bytes do not match .\n");
file.close();
goto FORMAT_FS;
}

if (memcmp(buf, text, text_length) != 0) {
LOG_DEBUG("The written bytes do not match the read bytes .\n");
file.close();
goto FORMAT_FS;
}
return true;
FORMAT_FS:
LOG_DEBUG("Format FS ....\n");
FSCom.format();
FSCom.begin();
return false;
#else
return true;
#endif
}

void fsInit()
{
#ifdef FSCom
Expand All @@ -219,15 +275,29 @@ void fsInit()
* nRF52840 has a certain chance of automatic formatting failure.
* Try to create a file after initializing the file system. If the creation fails,
* it means that the file system is not working properly. Please format it manually again.
* To check the normality of the file system, you need to disable the LFS_NO_ASSERT assertion.
* Otherwise, the assertion will be entered at the moment of reading or opening, and the FS will not be formatted.
* */
Adafruit_LittleFS_Namespace::File file(FSCom);
const char *filename = "/meshtastic.txt";
if (!file.open(filename, FILE_O_WRITE)) {
LOG_DEBUG("Format ....");
FSCom.format();
FSCom.begin();
} else {
file.close();
bool ret = false;
uint8_t retry = 3;

while (retry--) {
ret = fsCheck();
if (ret) {
LOG_DEBUG("File system check is OK.\n");
break;
}
delay(10);
}

// It may not be possible to reach this step.
// Add a loop here to prevent unpredictable situations from happening.
// Can add a screen to display error status later.
if (!ret) {
while (1) {
Serial.println("The file system is damaged and cannot proceed to the next step.\n");
delay(1000);
lewisxhe marked this conversation as resolved.
Show resolved Hide resolved
}
}
#else
LOG_DEBUG("Filesystem files:\n");
Expand Down
1 change: 1 addition & 0 deletions variants/t-echo/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ build_flags = ${nrf52840_base.build_flags} -Ivariants/t-echo
-DEINK_LIMIT_RATE_RESPONSIVE_SEC=1 ; Minimum interval between RESPONSIVE updates
-DEINK_LIMIT_GHOSTING_PX=2000 ; (Optional) How much image ghosting is tolerated
-DEINK_BACKGROUND_USES_FAST ; (Optional) Use FAST refresh for both BACKGROUND and RESPONSIVE, until a limit is reached.
-DLFS_NO_ASSERT ; Disable LFS assertions
lewisxhe marked this conversation as resolved.
Show resolved Hide resolved
build_src_filter = ${nrf52_base.build_src_filter} +<../variants/t-echo>
lib_deps =
${nrf52840_base.lib_deps}
Expand Down
Loading