diff --git a/examples/companion_radio/DataStore.cpp b/examples/companion_radio/DataStore.cpp index 06c56a7a44..60f719b9c7 100644 --- a/examples/companion_radio/DataStore.cpp +++ b/examples/companion_radio/DataStore.cpp @@ -80,7 +80,7 @@ void DataStore::begin() { #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) int _countLfsBlock(void *p, lfs_block_t block){ - if (block > _ContactsChannelsTotalBlocks) { + if (block >= _ContactsChannelsTotalBlocks) { MESH_DEBUG_PRINTLN("ERROR: Block %d exceeds filesystem bounds - CORRUPTION DETECTED!", block); return LFS_ERR_CORRUPT; // return error to abort lfs_traverse() gracefully } @@ -91,7 +91,9 @@ int _countLfsBlock(void *p, lfs_block_t block){ lfs_ssize_t _getLfsUsedBlockCount(FILESYSTEM* fs) { lfs_size_t size = 0; + fs->_lockFS(); int err = lfs_traverse(fs->_getFS(), _countLfsBlock, &size); + fs->_unlockFS(); if (err) { MESH_DEBUG_PRINTLN("ERROR: lfs_traverse() error: %d", err); return 0; diff --git a/examples/companion_radio/LockedLFS.h b/examples/companion_radio/LockedLFS.h new file mode 100644 index 0000000000..56eb0a2090 --- /dev/null +++ b/examples/companion_radio/LockedLFS.h @@ -0,0 +1,52 @@ +#pragma once + +#include +#include + +// InternalFS and a CustomLFS on internal flash use the same flash cache, which +// has no lock of its own. InternalFS uses it under its own lock, so take that +// lock around all operations here too. +class LockedLFS : public CustomLFS { + lfs_config orig; + + static const lfs_config& base(const lfs_config* c) { + return static_cast(static_cast(c->context))->orig; + } + + static int read(const lfs_config* c, lfs_block_t b, lfs_off_t o, void* buf, lfs_size_t n) { + InternalFS._lockFS(); + int r = base(c).read(c, b, o, buf, n); + InternalFS._unlockFS(); + return r; + } + + static int prog(const lfs_config* c, lfs_block_t b, lfs_off_t o, const void* buf, lfs_size_t n) { + InternalFS._lockFS(); + int r = base(c).prog(c, b, o, buf, n); + InternalFS._unlockFS(); + return r; + } + + static int erase(const lfs_config* c, lfs_block_t b) { + InternalFS._lockFS(); + int r = base(c).erase(c, b); + InternalFS._unlockFS(); + return r; + } + + static int sync(const lfs_config* c) { + InternalFS._lockFS(); + int r = base(c).sync(c); + InternalFS._unlockFS(); + return r; + } + +public: + LockedLFS(uint32_t addr, uint32_t size, uint32_t block) + : CustomLFS(addr, size, block), orig(_lfs_config) { + _lfs_config.read = read; + _lfs_config.prog = prog; + _lfs_config.erase = erase; + _lfs_config.sync = sync; + } +}; diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 89f0e6cb9f..375d9d7708 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -72,8 +72,8 @@ MultiSerialInterface interface_manager; DataStore store(InternalFS, QSPIFlash, rtc_clock); #else #if defined(EXTRAFS) - #include - CustomLFS ExtraFS(0xD4000, 0x19000, 128); + #include "LockedLFS.h" + LockedLFS ExtraFS(0xD4000, 0x19000, 128); DataStore store(InternalFS, ExtraFS, rtc_clock); #else DataStore store(InternalFS, rtc_clock);