From aedc6009ea36ed9913360d71e2e130141820df1d Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Tue, 6 Apr 2021 16:06:29 +0100 Subject: [PATCH] TDBStore: call `BlockDevice::erase()` regarless of erase value From the documentations of `BlockDevice::get_erase_value()`: -1 if you can't rely on the value of the erased storage and `BlockDevice::program()`: The blocks must have been erased prior to being programmed So, `BlockDevice::erase()` should always be called regardless of erase value. --- storage/kvstore/tdbstore/source/TDBStore.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/storage/kvstore/tdbstore/source/TDBStore.cpp b/storage/kvstore/tdbstore/source/TDBStore.cpp index 7fe44ed021..7d8d8822f3 100644 --- a/storage/kvstore/tdbstore/source/TDBStore.cpp +++ b/storage/kvstore/tdbstore/source/TDBStore.cpp @@ -174,9 +174,12 @@ int TDBStore::erase_area(uint8_t area, uint32_t offset, uint32_t size) { uint32_t bd_offset = _area_params[area].address + offset; - if (_buff_bd->get_erase_value() != -1) { - return _buff_bd->erase(bd_offset, size); - } else { + int ret = _buff_bd->erase(bd_offset, size); + if (ret) { + return ret; + } + + if (_buff_bd->get_erase_value() == -1) { // We need to simulate erase to wipe records, as our block device // may not do it. Program in chunks of _work_buf_size if the minimum // program size is too small (e.g. one-byte) to avoid performance @@ -186,7 +189,7 @@ int TDBStore::erase_area(uint8_t area, uint32_t offset, uint32_t size) memset(_work_buf, 0xFF, _work_buf_size); while (size) { uint32_t chunk = std::min(_work_buf_size, size); - int ret = _buff_bd->program(_work_buf, bd_offset, chunk); + ret = _buff_bd->program(_work_buf, bd_offset, chunk); if (ret) { return ret; }