From 5574c439d5f5b0de86abf265966d2cdc2803428c Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Tue, 29 Aug 2023 10:13:51 +0200 Subject: [PATCH] btstack_tlv_flash_bank: overwrite complete entry with zeroes if we cannot just delete tag due to alignment --- platform/embedded/btstack_tlv_flash_bank.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/platform/embedded/btstack_tlv_flash_bank.c b/platform/embedded/btstack_tlv_flash_bank.c index b8e25178e..48b384f98 100644 --- a/platform/embedded/btstack_tlv_flash_bank.c +++ b/platform/embedded/btstack_tlv_flash_bank.c @@ -356,10 +356,23 @@ static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_ban // write delete field after entry header btstack_tlv_flash_bank_write(self, self->current_bank, it.offset+BTSTACK_TLV_ENTRY_HEADER_LEN, (uint8_t*) &zero_value, sizeof(zero_value)); #else - // overwrite tag with zero value - btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_value, sizeof(zero_value)); + uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context); + if (alignment <= 4){ + // if alignment < 4, overwrite only tag with zero value + btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_value, sizeof(zero_value)); + } else { + // otherwise, overwrite complete entry. This results in a sequence of { tag: 0, len: 0 } entries + uint8_t zero_buffer[32]; + memset(zero_buffer, 0, sizeof(zero_buffer)); + uint32_t entry_offset = 0; + uint32_t entry_size = btstack_tlv_flash_bank_aligned_entry_size(self, it.len); + while (entry_offset < entry_size) { + uint32_t bytes_to_write = btstack_min(entry_size - entry_offset, sizeof(zero_buffer)); + btstack_tlv_flash_bank_write(self, self->current_bank, it.offset + entry_offset, zero_buffer, bytes_to_write); + entry_offset += bytes_to_write; + } + } #endif - } tlv_iterator_fetch_next(self, &it); }