xref: /btstack/platform/embedded/btstack_tlv_flash_bank.c (revision 6681346093ef1432c3011d0b31a532d2c10b2a90)
117ae5bc4SMatthias Ringwald /*
217ae5bc4SMatthias Ringwald  * Copyright (C) 2017 BlueKitchen GmbH
317ae5bc4SMatthias Ringwald  *
417ae5bc4SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
517ae5bc4SMatthias Ringwald  * modification, are permitted provided that the following conditions
617ae5bc4SMatthias Ringwald  * are met:
717ae5bc4SMatthias Ringwald  *
817ae5bc4SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
917ae5bc4SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
1017ae5bc4SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
1117ae5bc4SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
1217ae5bc4SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
1317ae5bc4SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
1417ae5bc4SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
1517ae5bc4SMatthias Ringwald  *    from this software without specific prior written permission.
1617ae5bc4SMatthias Ringwald  *
172fca4dadSMilanka Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
1817ae5bc4SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1917ae5bc4SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
202fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
212fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2217ae5bc4SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2317ae5bc4SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2417ae5bc4SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2517ae5bc4SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2617ae5bc4SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
2717ae5bc4SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2817ae5bc4SMatthias Ringwald  * SUCH DAMAGE.
2917ae5bc4SMatthias Ringwald  *
3017ae5bc4SMatthias Ringwald  */
3117ae5bc4SMatthias Ringwald 
32e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "btstack_tlv_flash_bank.c"
3317ae5bc4SMatthias Ringwald 
3417ae5bc4SMatthias Ringwald #include "btstack_tlv.h"
3517ae5bc4SMatthias Ringwald #include "btstack_tlv_flash_bank.h"
3617ae5bc4SMatthias Ringwald #include "btstack_debug.h"
3717ae5bc4SMatthias Ringwald #include "btstack_util.h"
3817ae5bc4SMatthias Ringwald #include "btstack_debug.h"
3917ae5bc4SMatthias Ringwald 
4017ae5bc4SMatthias Ringwald #include <string.h>
4179b7220dSMatthias Ringwald #include <inttypes.h>
4217ae5bc4SMatthias Ringwald 
4317ae5bc4SMatthias Ringwald // Header:
4417ae5bc4SMatthias Ringwald // - Magic: 'BTstack'
4517ae5bc4SMatthias Ringwald // - Status:
4617ae5bc4SMatthias Ringwald //   - bits 765432: reserved
4717ae5bc4SMatthias Ringwald //	 - bits 10:     epoch
4817ae5bc4SMatthias Ringwald 
4917ae5bc4SMatthias Ringwald // Entries
5017ae5bc4SMatthias Ringwald // - Tag: 32 bit
5117ae5bc4SMatthias Ringwald // - Len: 32 bit
52ffded216SMatthias Ringwald // - if ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD is used
53ffded216SMatthias Ringwald //    - padding to align delete field
54ffded216SMatthias Ringwald //    - Delete: 32 bit
55ffded216SMatthias Ringwald //    - padding to align value field
5617ae5bc4SMatthias Ringwald // - Value: Len in bytes
5717ae5bc4SMatthias Ringwald 
582ea4a509SMatthias Ringwald // Alignment
59ffded216SMatthias Ringwald // Tag is aligned to the alignment from hal_flash_bank_t
60ffded216SMatthias Ringwald // If ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD is used, both Delete and Value are aligned, too
612ea4a509SMatthias Ringwald 
620d5520a9SMatthias Ringwald // ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
630d5520a9SMatthias Ringwald //
640d5520a9SMatthias Ringwald // Most Flash implementations allow to:
650d5520a9SMatthias Ringwald // - erase sector -> all values are 0xff
660d5520a9SMatthias Ringwald // - write value (1s -> 0s)
67052e5e42SMatthias Ringwald // - overwrite value with zero (remaining 1s -> 0s)
680d5520a9SMatthias Ringwald //
69ffded216SMatthias Ringwald // We use the ability to overwrite a value with zeros to mark deleted entries (by writing zero into the tag field).
70ffded216SMatthias Ringwald // Some targets, E.g. Kinetix K64F, do not allow for that.
710d5520a9SMatthias Ringwald //
720d5520a9SMatthias Ringwald // With ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD an extra field is reserved to indicate a deleted tag, while keeping main logic
73052e5e42SMatthias Ringwald //
74052e5e42SMatthias Ringwald // With ENABLE_TLV_FLASH_WRITE_ONCE, tags are never marked as deleted. Instead, an emtpy tag will be written instead.
75052e5e42SMatthias Ringwald //     Also, lookup and migrate requires to always search until the end of the valid bank
76052e5e42SMatthias Ringwald 
77052e5e42SMatthias Ringwald #if defined (ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD) && defined (ENABLE_TLV_FLASH_WRITE_ONCE)
78052e5e42SMatthias Ringwald #error "Please define either ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD or ENABLE_TLV_FLASH_WRITE_ONCE"
79052e5e42SMatthias Ringwald #endif
800d5520a9SMatthias Ringwald 
81b6c12bd6SMatthias Ringwald #define BTSTACK_TLV_BANK_HEADER_LEN  8
829b112178SMatthias Ringwald #define BTSTACK_TLV_ENTRY_HEADER_LEN 8
83e8efffc8SMatthias Ringwald 
84372a3556SMatthias Ringwald #ifndef BTSTACK_FLASH_ALIGNMENT_MAX
85ffded216SMatthias Ringwald #define BTSTACK_FLASH_ALIGNMENT_MAX 16
86372a3556SMatthias Ringwald #endif
87e8efffc8SMatthias Ringwald 
8817ae5bc4SMatthias Ringwald static const char * btstack_tlv_header_magic = "BTstack";
8917ae5bc4SMatthias Ringwald 
9017ae5bc4SMatthias Ringwald // TLV Iterator
9117ae5bc4SMatthias Ringwald typedef struct {
9217ae5bc4SMatthias Ringwald 	int 	 bank;
9317ae5bc4SMatthias Ringwald 	uint32_t offset;
944c7f5275SMatthias Ringwald     uint32_t size;
9517ae5bc4SMatthias Ringwald 	uint32_t tag;
9617ae5bc4SMatthias Ringwald 	uint32_t len;
9717ae5bc4SMatthias Ringwald } tlv_iterator_t;
9817ae5bc4SMatthias Ringwald 
btstack_tlv_flash_bank_align_size(btstack_tlv_flash_bank_t * self,uint32_t size)99372a3556SMatthias Ringwald static uint32_t btstack_tlv_flash_bank_align_size(btstack_tlv_flash_bank_t * self, uint32_t size){
1004c7f5275SMatthias Ringwald 	uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
1014c7f5275SMatthias Ringwald 	return (size + alignment - 1) & ~(alignment - 1);
102372a3556SMatthias Ringwald }
103372a3556SMatthias Ringwald 
btstack_tlv_flash_bank_aligned_entry_size(btstack_tlv_flash_bank_t * self,uint32_t size)1042ea4a509SMatthias Ringwald static uint32_t btstack_tlv_flash_bank_aligned_entry_size(btstack_tlv_flash_bank_t * self, uint32_t size) {
1052ea4a509SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
1064a1addd5SMatthias Ringwald     // entry header and delete fields are already padded
1074a1addd5SMatthias Ringwald     return self->entry_header_len + self->delete_tag_len + btstack_tlv_flash_bank_align_size(self, size);
1084a1addd5SMatthias Ringwald #else
1094a1addd5SMatthias Ringwald     // otherwise, data starts right after entry header
1104a1addd5SMatthias Ringwald     return btstack_tlv_flash_bank_align_size(self, self->entry_header_len + size);
1112ea4a509SMatthias Ringwald #endif
1122ea4a509SMatthias Ringwald }
1132ea4a509SMatthias Ringwald 
114372a3556SMatthias Ringwald // support unaligned flash read/writes
115372a3556SMatthias Ringwald // strategy: increase size to meet alignment, perform unaligned read/write of last chunk with helper buffer
116372a3556SMatthias Ringwald 
btstack_tlv_flash_bank_read(btstack_tlv_flash_bank_t * self,int bank,uint32_t offset,uint8_t * buffer,uint32_t size)117e8efffc8SMatthias Ringwald static void btstack_tlv_flash_bank_read(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
118372a3556SMatthias Ringwald 
119372a3556SMatthias Ringwald 	// read main data
1201295b091SMatthias Ringwald 	uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
1211295b091SMatthias Ringwald 	uint32_t lower_bits = size & (alignment - 1);
122372a3556SMatthias Ringwald 	uint32_t size_aligned = size - lower_bits;
123372a3556SMatthias Ringwald 	if (size_aligned){
124372a3556SMatthias Ringwald 		self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, size_aligned);
125372a3556SMatthias Ringwald 		buffer += size_aligned;
126372a3556SMatthias Ringwald 		offset += size_aligned;
127372a3556SMatthias Ringwald 		size   -= size_aligned;
128372a3556SMatthias Ringwald 	}
129372a3556SMatthias Ringwald 
130372a3556SMatthias Ringwald 	// read last part
131372a3556SMatthias Ringwald 	if (size == 0) return;
1321295b091SMatthias Ringwald 	uint8_t alignment_block[BTSTACK_FLASH_ALIGNMENT_MAX];
1331295b091SMatthias Ringwald 	self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, alignment_block, alignment);
134d2d1d053SMatthias Ringwald 	memcpy(buffer, alignment_block, size);
135e8efffc8SMatthias Ringwald }
136e8efffc8SMatthias Ringwald 
btstack_tlv_flash_bank_write(btstack_tlv_flash_bank_t * self,int bank,uint32_t offset,const uint8_t * buffer,uint32_t size)137e8efffc8SMatthias Ringwald static void btstack_tlv_flash_bank_write(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset, const uint8_t * buffer, uint32_t size){
138372a3556SMatthias Ringwald 
139372a3556SMatthias Ringwald 	// write main data
1401295b091SMatthias Ringwald 	uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
1411295b091SMatthias Ringwald 	uint32_t lower_bits = size & (alignment - 1);
142372a3556SMatthias Ringwald 	uint32_t size_aligned = size - lower_bits;
143372a3556SMatthias Ringwald 	if (size_aligned){
144372a3556SMatthias Ringwald 		self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, buffer, size_aligned);
145372a3556SMatthias Ringwald 		buffer += size_aligned;
146372a3556SMatthias Ringwald 		offset += size_aligned;
147372a3556SMatthias Ringwald 		size   -= size_aligned;
148e8efffc8SMatthias Ringwald 	}
149e8efffc8SMatthias Ringwald 
150372a3556SMatthias Ringwald 	// write last part
151372a3556SMatthias Ringwald 	if (size == 0) return;
1521295b091SMatthias Ringwald 	uint8_t alignment_block[BTSTACK_FLASH_ALIGNMENT_MAX];
1531295b091SMatthias Ringwald 	memset(alignment_block, 0xff, alignment);
1541295b091SMatthias Ringwald 	memcpy(alignment_block, buffer, lower_bits);
1551295b091SMatthias Ringwald 	self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, alignment_block, alignment);
156372a3556SMatthias Ringwald }
157372a3556SMatthias Ringwald 
158e8efffc8SMatthias Ringwald 
159e8efffc8SMatthias Ringwald // iterator
160e8efffc8SMatthias Ringwald 
btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self,tlv_iterator_t * it)16117ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
1624c7f5275SMatthias Ringwald     // abort if header doesn't fit into remaining space
1632ea4a509SMatthias Ringwald     if (it->offset + self->entry_header_len + self->delete_tag_len >= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
16426c447f6SMatthias Ringwald         it->tag = 0xffffffff;
16526c447f6SMatthias Ringwald         return;
16626c447f6SMatthias Ringwald     }
16726c447f6SMatthias Ringwald 
1689b112178SMatthias Ringwald 	uint8_t entry[BTSTACK_TLV_ENTRY_HEADER_LEN];
1699b112178SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, it->bank, it->offset, entry, BTSTACK_TLV_ENTRY_HEADER_LEN);
17017ae5bc4SMatthias Ringwald 	it->tag = big_endian_read_32(entry, 0);
17117ae5bc4SMatthias Ringwald 	it->len = big_endian_read_32(entry, 4);
1720d5520a9SMatthias Ringwald 
1730d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
1740d5520a9SMatthias Ringwald 	// clear tag, if delete field is set
1750d5520a9SMatthias Ringwald 	uint32_t delete_tag;
1762ea4a509SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, it->bank, it->offset + self->entry_header_len, (uint8_t *) &delete_tag, 4);
1770d5520a9SMatthias Ringwald 	if (delete_tag == 0){
1780d5520a9SMatthias Ringwald 		it->tag = 0;
1790d5520a9SMatthias Ringwald 	}
1800d5520a9SMatthias Ringwald #endif
18117ae5bc4SMatthias Ringwald }
18217ae5bc4SMatthias Ringwald 
btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self,tlv_iterator_t * it,int bank)18317ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it, int bank){
18417ae5bc4SMatthias Ringwald 	memset(it, 0, sizeof(tlv_iterator_t));
18517ae5bc4SMatthias Ringwald 	it->bank = bank;
186ffded216SMatthias Ringwald 	it->offset = btstack_tlv_flash_bank_align_size(self, BTSTACK_TLV_BANK_HEADER_LEN);
1874c7f5275SMatthias Ringwald     it->size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context);
18817ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
18917ae5bc4SMatthias Ringwald }
19017ae5bc4SMatthias Ringwald 
btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self,tlv_iterator_t * it)1914c7f5275SMatthias Ringwald static bool btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
19279b7220dSMatthias Ringwald 	UNUSED(self);
1934c7f5275SMatthias Ringwald 	return it->tag != 0xffffffff;
19417ae5bc4SMatthias Ringwald }
19517ae5bc4SMatthias Ringwald 
tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self,tlv_iterator_t * it)19617ae5bc4SMatthias Ringwald static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
1972ea4a509SMatthias Ringwald     it->offset += btstack_tlv_flash_bank_aligned_entry_size(self, it->len);
1980d5520a9SMatthias Ringwald 
1994c7f5275SMatthias Ringwald 	if (it->offset >= it->size) {
20017ae5bc4SMatthias Ringwald 		it->tag = 0xffffffff;
20117ae5bc4SMatthias Ringwald 		it->len = 0;
20217ae5bc4SMatthias Ringwald 		return;
20317ae5bc4SMatthias Ringwald 	}
20417ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
20517ae5bc4SMatthias Ringwald }
20617ae5bc4SMatthias Ringwald 
20717ae5bc4SMatthias Ringwald //
20817ae5bc4SMatthias Ringwald 
20917ae5bc4SMatthias Ringwald // check both banks for headers and pick the one with the higher epoch % 4
21017ae5bc4SMatthias Ringwald // @returns bank or -1 if something is invalid
btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self)21117ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){
212b6c12bd6SMatthias Ringwald  	uint8_t header0[BTSTACK_TLV_BANK_HEADER_LEN];
213b6c12bd6SMatthias Ringwald  	uint8_t header1[BTSTACK_TLV_BANK_HEADER_LEN];
214b6c12bd6SMatthias Ringwald  	btstack_tlv_flash_bank_read(self, 0, 0, &header0[0], BTSTACK_TLV_BANK_HEADER_LEN);
215b6c12bd6SMatthias Ringwald  	btstack_tlv_flash_bank_read(self, 1, 0, &header1[0], BTSTACK_TLV_BANK_HEADER_LEN);
216b6c12bd6SMatthias Ringwald  	int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_BANK_HEADER_LEN-1) == 0;
217b6c12bd6SMatthias Ringwald  	int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_BANK_HEADER_LEN-1) == 0;
21817ae5bc4SMatthias Ringwald 	if (!valid0 && !valid1) return -1;
21917ae5bc4SMatthias Ringwald 	if ( valid0 && !valid1) return 0;
22017ae5bc4SMatthias Ringwald 	if (!valid0 &&  valid1) return 1;
221b6c12bd6SMatthias Ringwald 	int epoch0 = header0[BTSTACK_TLV_BANK_HEADER_LEN-1] & 0x03;
222b6c12bd6SMatthias Ringwald 	int epoch1 = header1[BTSTACK_TLV_BANK_HEADER_LEN-1] & 0x03;
22317ae5bc4SMatthias Ringwald 	if (epoch0 == ((epoch1 + 1) & 0x03)) return 0;
22417ae5bc4SMatthias Ringwald 	if (epoch1 == ((epoch0 + 1) & 0x03)) return 1;
22517ae5bc4SMatthias Ringwald 	return -1;	// invalid, must not happen
22617ae5bc4SMatthias Ringwald }
22717ae5bc4SMatthias Ringwald 
btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self,int bank,int epoch)22817ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self, int bank, int epoch){
229b6c12bd6SMatthias Ringwald 	uint8_t header[BTSTACK_TLV_BANK_HEADER_LEN];
230b6c12bd6SMatthias Ringwald 	memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_BANK_HEADER_LEN-1);
231b6c12bd6SMatthias Ringwald 	header[BTSTACK_TLV_BANK_HEADER_LEN-1] = epoch;
232b6c12bd6SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, bank, 0, header, BTSTACK_TLV_BANK_HEADER_LEN);
23317ae5bc4SMatthias Ringwald }
23417ae5bc4SMatthias Ringwald 
23517ae5bc4SMatthias Ringwald /**
23617ae5bc4SMatthias Ringwald  * @brief Check if erased from offset
23717ae5bc4SMatthias Ringwald  */
btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self,int bank,uint32_t offset)23817ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset){
23997d2cfbcSMatthias Ringwald 	log_info("test erased: bank %u, offset %u", bank, (unsigned int) offset);
24017ae5bc4SMatthias Ringwald 	uint32_t size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context);
24117ae5bc4SMatthias Ringwald 	uint8_t buffer[16];
24217ae5bc4SMatthias Ringwald 	uint8_t empty16[16];
24317ae5bc4SMatthias Ringwald 	memset(empty16, 0xff, sizeof(empty16));
24417ae5bc4SMatthias Ringwald 	while (offset < size){
24517ae5bc4SMatthias Ringwald 		uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset);
246e8efffc8SMatthias Ringwald 		btstack_tlv_flash_bank_read(self, bank, offset, buffer, copy_size);
2474c7f5275SMatthias Ringwald 		if (memcmp(buffer, empty16, copy_size) != 0) {
24897d2cfbcSMatthias Ringwald 			log_info("not erased %x - %x", (unsigned int) offset, (unsigned int) (offset + copy_size));
24917ae5bc4SMatthias Ringwald 			return 0;
25017ae5bc4SMatthias Ringwald 		}
25117ae5bc4SMatthias Ringwald 		offset += copy_size;
25217ae5bc4SMatthias Ringwald 	}
25317ae5bc4SMatthias Ringwald 	return 1;
25417ae5bc4SMatthias Ringwald }
25517ae5bc4SMatthias Ringwald 
25617ae5bc4SMatthias Ringwald /**
25717ae5bc4SMatthias Ringwald  * @brief erase bank (only if not already erased)
25817ae5bc4SMatthias Ringwald  */
btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self,int bank)25917ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self, int bank){
26017ae5bc4SMatthias Ringwald 	if (btstack_tlv_flash_bank_test_erased(self, bank, 0)){
26117ae5bc4SMatthias Ringwald 		log_info("bank %u already erased", bank);
26217ae5bc4SMatthias Ringwald 	} else {
26317ae5bc4SMatthias Ringwald 		log_info("bank %u not empty, erase bank", bank);
26417ae5bc4SMatthias Ringwald 		self->hal_flash_bank_impl->erase(self->hal_flash_bank_context, bank);
26517ae5bc4SMatthias Ringwald 	}
26617ae5bc4SMatthias Ringwald }
26717ae5bc4SMatthias Ringwald 
btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self)26817ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){
26917ae5bc4SMatthias Ringwald 
27017ae5bc4SMatthias Ringwald 	int next_bank = 1 - self->current_bank;
27117ae5bc4SMatthias Ringwald 	log_info("migrate bank %u -> bank %u", self->current_bank, next_bank);
27217ae5bc4SMatthias Ringwald 	// erase bank (if needed)
27317ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_erase_bank(self, next_bank);
274d2d1d053SMatthias Ringwald 	uint32_t next_write_pos = btstack_tlv_flash_bank_align_size (self, BTSTACK_TLV_BANK_HEADER_LEN);;
27517ae5bc4SMatthias Ringwald 
27617ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
27717ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
27817ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
27917ae5bc4SMatthias Ringwald 		// skip deleted entries
28017ae5bc4SMatthias Ringwald 		if (it.tag) {
28117ae5bc4SMatthias Ringwald 			uint32_t tag_len = it.len;
28217ae5bc4SMatthias Ringwald 			uint32_t tag_index = it.offset;
28317ae5bc4SMatthias Ringwald 
284052e5e42SMatthias Ringwald             bool tag_valid = true;
285052e5e42SMatthias Ringwald 
286052e5e42SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_WRITE_ONCE
287052e5e42SMatthias Ringwald             // search until end for newer entry of same tag
288052e5e42SMatthias Ringwald             tlv_iterator_t it2;
289052e5e42SMatthias Ringwald             memcpy(&it2, &it, sizeof(tlv_iterator_t));
290052e5e42SMatthias Ringwald             while (btstack_tlv_flash_bank_iterator_has_next(self, &it2)){
291052e5e42SMatthias Ringwald                 if ((it2.offset != it.offset) && (it2.tag == it.tag)){
292052e5e42SMatthias Ringwald                     tag_valid = false;
293052e5e42SMatthias Ringwald                     break;
294052e5e42SMatthias Ringwald                 }
295052e5e42SMatthias Ringwald                 tlv_iterator_fetch_next(self, &it2);
296052e5e42SMatthias Ringwald             }
297052e5e42SMatthias Ringwald             if (tag_valid == false){
298052e5e42SMatthias Ringwald 			    log_info("skip pos %u, tag '%x' as newer entry found at %u", (unsigned int) tag_index, (unsigned int) it.tag,
299052e5e42SMatthias Ringwald                     (unsigned int) it2.offset);
300052e5e42SMatthias Ringwald             }
301052e5e42SMatthias Ringwald #endif
302052e5e42SMatthias Ringwald 
303052e5e42SMatthias Ringwald             if (tag_valid) {
304052e5e42SMatthias Ringwald 
30597d2cfbcSMatthias Ringwald                 log_info("migrate pos %u, tag '%x' len %u -> new pos %u",
306*ae14e166Sshuffle2                          (unsigned int) tag_index, (unsigned int) it.tag, (unsigned int) tag_len,
307*ae14e166Sshuffle2                          (unsigned int) next_write_pos);
3080d5520a9SMatthias Ringwald 
3094a1addd5SMatthias Ringwald                 uint32_t write_offset = next_write_pos;
3104a1addd5SMatthias Ringwald                 uint32_t bytes_to_copy;
3114a1addd5SMatthias Ringwald                 uint32_t entry_size = btstack_tlv_flash_bank_aligned_entry_size(self, tag_len);
3124a1addd5SMatthias Ringwald 
3134a1addd5SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
3144a1addd5SMatthias Ringwald                 // copy in two steps to skip delete field
3154a1addd5SMatthias Ringwald 
3160d5520a9SMatthias Ringwald                 // copy header
3179b112178SMatthias Ringwald                 uint8_t header_buffer[BTSTACK_TLV_ENTRY_HEADER_LEN];
3189b112178SMatthias Ringwald                 btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, header_buffer, BTSTACK_TLV_ENTRY_HEADER_LEN);
3199b112178SMatthias Ringwald                 btstack_tlv_flash_bank_write(self, next_bank, next_write_pos,    header_buffer, BTSTACK_TLV_ENTRY_HEADER_LEN);
3204a1addd5SMatthias Ringwald                 tag_index    += self->entry_header_len + self->delete_tag_len;
3214a1addd5SMatthias Ringwald                 write_offset += self->entry_header_len + self->delete_tag_len;
3224a1addd5SMatthias Ringwald 
3234a1addd5SMatthias Ringwald                 // preparee copy value
3244a1addd5SMatthias Ringwald                 bytes_to_copy = tag_len;
3254a1addd5SMatthias Ringwald #else
3264a1addd5SMatthias Ringwald                 // copy everything as one block
3274a1addd5SMatthias Ringwald                 bytes_to_copy = entry_size;
3280d5520a9SMatthias Ringwald #endif
3294a1addd5SMatthias Ringwald 
3300d5520a9SMatthias Ringwald                 // copy value
33117ae5bc4SMatthias Ringwald                 uint8_t copy_buffer[32];
3324a1addd5SMatthias Ringwald                 while (bytes_to_copy > 0) {
3334a1addd5SMatthias Ringwald                     uint32_t bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer));
334e8efffc8SMatthias Ringwald                     btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
3352ea4a509SMatthias Ringwald                     btstack_tlv_flash_bank_write(self, next_bank, write_offset, copy_buffer, bytes_this_iteration);
33617ae5bc4SMatthias Ringwald                     tag_index     += bytes_this_iteration;
3372ea4a509SMatthias Ringwald                     write_offset  += bytes_this_iteration;
33817ae5bc4SMatthias Ringwald                     bytes_to_copy -= bytes_this_iteration;
33917ae5bc4SMatthias Ringwald                 }
3404a1addd5SMatthias Ringwald                 next_write_pos += entry_size;
34117ae5bc4SMatthias Ringwald             }
342052e5e42SMatthias Ringwald 		}
34317ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
34417ae5bc4SMatthias Ringwald 	}
34517ae5bc4SMatthias Ringwald 
34617ae5bc4SMatthias Ringwald 	// prepare new one
34717ae5bc4SMatthias Ringwald 	uint8_t epoch_buffer;
348b6c12bd6SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, self->current_bank, BTSTACK_TLV_BANK_HEADER_LEN-1, &epoch_buffer, 1);
34917ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3);
35017ae5bc4SMatthias Ringwald 	self->current_bank = next_bank;
35117ae5bc4SMatthias Ringwald 	self->write_offset = next_write_pos;
35217ae5bc4SMatthias Ringwald }
35317ae5bc4SMatthias Ringwald 
354052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self,uint32_t tag,uint32_t offset)35517ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self, uint32_t tag, uint32_t offset){
35617ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
35717ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
35817ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it) && it.offset < offset){
35917ae5bc4SMatthias Ringwald 		if (it.tag == tag){
36097d2cfbcSMatthias Ringwald 			log_info("Erase tag '%x' at position %u", (unsigned int) tag, (unsigned int) it.offset);
3610d5520a9SMatthias Ringwald 
3620d5520a9SMatthias Ringwald 			// mark entry as invalid
3630d5520a9SMatthias Ringwald 			uint32_t zero_value = 0;
3640d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
3659b112178SMatthias Ringwald 			// write delete field after entry header
36660a3ef13SMatthias Ringwald 			btstack_tlv_flash_bank_write(self, self->current_bank, it.offset+self->entry_header_len, (uint8_t*) &zero_value, sizeof(zero_value));
3670d5520a9SMatthias Ringwald #else
3685574c439SMatthias Ringwald             uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
3695574c439SMatthias Ringwald             if (alignment <= 4){
3705574c439SMatthias Ringwald                 // if alignment < 4, overwrite only tag with zero value
3710d5520a9SMatthias Ringwald                 btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_value, sizeof(zero_value));
3725574c439SMatthias Ringwald             } else {
3735574c439SMatthias Ringwald                 // otherwise, overwrite complete entry. This results in a sequence of { tag: 0, len: 0 } entries
3745574c439SMatthias Ringwald                 uint8_t zero_buffer[32];
3755574c439SMatthias Ringwald                 memset(zero_buffer, 0, sizeof(zero_buffer));
3765574c439SMatthias Ringwald                 uint32_t entry_offset = 0;
3775574c439SMatthias Ringwald                 uint32_t entry_size = btstack_tlv_flash_bank_aligned_entry_size(self, it.len);
3785574c439SMatthias Ringwald                 while (entry_offset < entry_size) {
3795574c439SMatthias Ringwald                     uint32_t bytes_to_write = btstack_min(entry_size - entry_offset, sizeof(zero_buffer));
3805574c439SMatthias Ringwald                     btstack_tlv_flash_bank_write(self, self->current_bank, it.offset + entry_offset, zero_buffer, bytes_to_write);
3815574c439SMatthias Ringwald                     entry_offset += bytes_to_write;
3825574c439SMatthias Ringwald                 }
3835574c439SMatthias Ringwald             }
3840d5520a9SMatthias Ringwald #endif
38517ae5bc4SMatthias Ringwald 		}
38617ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
38717ae5bc4SMatthias Ringwald 	}
38817ae5bc4SMatthias Ringwald }
389052e5e42SMatthias Ringwald #endif
39017ae5bc4SMatthias Ringwald 
39117ae5bc4SMatthias Ringwald /**
39217ae5bc4SMatthias Ringwald  * Get Value for Tag
39317ae5bc4SMatthias Ringwald  * @param tag
39417ae5bc4SMatthias Ringwald  * @param buffer
39517ae5bc4SMatthias Ringwald  * @param buffer_size
39617ae5bc4SMatthias Ringwald  * @returns size of value
39717ae5bc4SMatthias Ringwald  */
btstack_tlv_flash_bank_get_tag(void * context,uint32_t tag,uint8_t * buffer,uint32_t buffer_size)39817ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
39917ae5bc4SMatthias Ringwald 
40017ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
40117ae5bc4SMatthias Ringwald 
40217ae5bc4SMatthias Ringwald 	uint32_t tag_index = 0;
40317ae5bc4SMatthias Ringwald 	uint32_t tag_len   = 0;
40417ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
40517ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
40617ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
40717ae5bc4SMatthias Ringwald 		if (it.tag == tag){
40897d2cfbcSMatthias Ringwald 			log_info("Found tag '%x' at position %u", (unsigned int) tag, (unsigned int) it.offset);
40917ae5bc4SMatthias Ringwald 			tag_index = it.offset;
41017ae5bc4SMatthias Ringwald 			tag_len   = it.len;
411052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
41217ae5bc4SMatthias Ringwald 			break;
413052e5e42SMatthias Ringwald #endif
41417ae5bc4SMatthias Ringwald 		}
41517ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
41617ae5bc4SMatthias Ringwald 	}
41717ae5bc4SMatthias Ringwald 	if (tag_index == 0) return 0;
41817ae5bc4SMatthias Ringwald 	if (!buffer) return tag_len;
41917ae5bc4SMatthias Ringwald 	int copy_size = btstack_min(buffer_size, tag_len);
42060a3ef13SMatthias Ringwald 	uint32_t value_offset = tag_index + self->entry_header_len;
4210d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
4220d5520a9SMatthias Ringwald 	// skip delete field
4235ff6a55bSMatthias Ringwald 	value_offset += self->delete_tag_len;
4240d5520a9SMatthias Ringwald #endif
4250d5520a9SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, self->current_bank, value_offset, buffer, copy_size);
42617ae5bc4SMatthias Ringwald 	return copy_size;
42717ae5bc4SMatthias Ringwald }
42817ae5bc4SMatthias Ringwald 
42917ae5bc4SMatthias Ringwald /**
43017ae5bc4SMatthias Ringwald  * Store Tag
43117ae5bc4SMatthias Ringwald  * @param tag
43217ae5bc4SMatthias Ringwald  * @param data
43317ae5bc4SMatthias Ringwald  * @param data_size
43417ae5bc4SMatthias Ringwald  */
btstack_tlv_flash_bank_store_tag(void * context,uint32_t tag,const uint8_t * data,uint32_t data_size)43517ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
43617ae5bc4SMatthias Ringwald 
43717ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
43817ae5bc4SMatthias Ringwald 
43917ae5bc4SMatthias Ringwald 	// trigger migration if not enough space
44060a3ef13SMatthias Ringwald 	uint32_t required_space = self->entry_header_len + self->delete_tag_len + data_size;
4415ff6a55bSMatthias Ringwald 	if (self->write_offset + required_space > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
44217ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_migrate(self);
44317ae5bc4SMatthias Ringwald 	}
44417ae5bc4SMatthias Ringwald 
4455ff6a55bSMatthias Ringwald 	if (self->write_offset + required_space > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
44617ae5bc4SMatthias Ringwald 		log_error("couldn't write entry, not enough space left");
44717ae5bc4SMatthias Ringwald 		return 2;
44817ae5bc4SMatthias Ringwald 	}
44917ae5bc4SMatthias Ringwald 
45017ae5bc4SMatthias Ringwald     // prepare entry
45179b7220dSMatthias Ringwald     log_info("write '%" PRIx32 "', len %" PRIu32 " at %" PRIx32, tag, data_size, self->write_offset);
45217ae5bc4SMatthias Ringwald 
4534a1addd5SMatthias Ringwald     uint8_t alignment_buffer[BTSTACK_FLASH_ALIGNMENT_MAX];
4544a1addd5SMatthias Ringwald     memset(alignment_buffer, 0, sizeof(alignment_buffer));
4554a1addd5SMatthias Ringwald     big_endian_store_32(alignment_buffer, 0, tag);
4564a1addd5SMatthias Ringwald     big_endian_store_32(alignment_buffer, 4, data_size);
4574a1addd5SMatthias Ringwald 
4584a1addd5SMatthias Ringwald     uint32_t header_len = BTSTACK_TLV_ENTRY_HEADER_LEN;
4594a1addd5SMatthias Ringwald     uint32_t value_size = data_size;
46060a3ef13SMatthias Ringwald     uint32_t value_offset = self->write_offset + self->entry_header_len;
4610d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
4620d5520a9SMatthias Ringwald     // skip delete field
4635ff6a55bSMatthias Ringwald     value_offset += self->delete_tag_len;
4644a1addd5SMatthias Ringwald #else
4654a1addd5SMatthias Ringwald     uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
4664a1addd5SMatthias Ringwald 
4674a1addd5SMatthias Ringwald     // if alignment is larger than the entry header, store parts from value in alignment buffer
4684a1addd5SMatthias Ringwald     if (alignment > BTSTACK_TLV_ENTRY_HEADER_LEN){
4694a1addd5SMatthias Ringwald         // calculated number of value bytes to store in alignment buffer
4704a1addd5SMatthias Ringwald         uint32_t bytes_from_value = btstack_min(alignment - BTSTACK_TLV_ENTRY_HEADER_LEN, value_size);
4714a1addd5SMatthias Ringwald 
4724a1addd5SMatthias Ringwald         // store parts from value in alignment buffer and update remaining value
4734a1addd5SMatthias Ringwald         memcpy(&alignment_buffer[header_len], data, bytes_from_value);
4744a1addd5SMatthias Ringwald         header_len += bytes_from_value;
4754a1addd5SMatthias Ringwald         data       += bytes_from_value;
4764a1addd5SMatthias Ringwald         value_size -= bytes_from_value;
4774a1addd5SMatthias Ringwald         value_offset = self->write_offset + alignment;
4784a1addd5SMatthias Ringwald     }
4790d5520a9SMatthias Ringwald #endif
4800d5520a9SMatthias Ringwald 
48117ae5bc4SMatthias Ringwald     // write value first
4824a1addd5SMatthias Ringwald     if (value_size > 0){
4834a1addd5SMatthias Ringwald         btstack_tlv_flash_bank_write(self, self->current_bank, value_offset, data, value_size);
4844a1addd5SMatthias Ringwald     }
48517ae5bc4SMatthias Ringwald 
48617ae5bc4SMatthias Ringwald     // then entry
4874a1addd5SMatthias Ringwald     btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset, alignment_buffer, header_len);
48817ae5bc4SMatthias Ringwald 
489052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
49017ae5bc4SMatthias Ringwald 	// overwrite old entries (if exists)
49117ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
492052e5e42SMatthias Ringwald #endif
49317ae5bc4SMatthias Ringwald 
49417ae5bc4SMatthias Ringwald 	// done
4954a1addd5SMatthias Ringwald 	self->write_offset += btstack_tlv_flash_bank_aligned_entry_size(self, data_size);
4960d5520a9SMatthias Ringwald 
49717ae5bc4SMatthias Ringwald 	return 0;
49817ae5bc4SMatthias Ringwald }
49917ae5bc4SMatthias Ringwald 
50017ae5bc4SMatthias Ringwald /**
50117ae5bc4SMatthias Ringwald  * Delete Tag
50217ae5bc4SMatthias Ringwald  * @param tag
50317ae5bc4SMatthias Ringwald  */
btstack_tlv_flash_bank_delete_tag(void * context,uint32_t tag)50417ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag(void * context, uint32_t tag){
505052e5e42SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_WRITE_ONCE
506052e5e42SMatthias Ringwald     btstack_tlv_flash_bank_store_tag(context, tag, NULL, 0);
507052e5e42SMatthias Ringwald #else
50817ae5bc4SMatthias Ringwald     btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
50917ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
510052e5e42SMatthias Ringwald #endif
51117ae5bc4SMatthias Ringwald }
51217ae5bc4SMatthias Ringwald 
51317ae5bc4SMatthias Ringwald static const btstack_tlv_t btstack_tlv_flash_bank = {
51417ae5bc4SMatthias Ringwald 	/* int  (*get_tag)(..);     */ &btstack_tlv_flash_bank_get_tag,
51517ae5bc4SMatthias Ringwald 	/* int (*store_tag)(..);    */ &btstack_tlv_flash_bank_store_tag,
51617ae5bc4SMatthias Ringwald 	/* void (*delete_tag)(v..); */ &btstack_tlv_flash_bank_delete_tag,
51717ae5bc4SMatthias Ringwald };
51817ae5bc4SMatthias Ringwald 
51917ae5bc4SMatthias Ringwald /**
52017ae5bc4SMatthias Ringwald  * Init Tag Length Value Store
52117ae5bc4SMatthias Ringwald  */
btstack_tlv_flash_bank_init_instance(btstack_tlv_flash_bank_t * self,const hal_flash_bank_t * hal_flash_bank_impl,void * hal_flash_bank_context)52217ae5bc4SMatthias Ringwald const btstack_tlv_t * btstack_tlv_flash_bank_init_instance(btstack_tlv_flash_bank_t * self, const hal_flash_bank_t * hal_flash_bank_impl, void * hal_flash_bank_context){
52317ae5bc4SMatthias Ringwald 
52417ae5bc4SMatthias Ringwald     self->hal_flash_bank_impl    = hal_flash_bank_impl;
52517ae5bc4SMatthias Ringwald     self->hal_flash_bank_context = hal_flash_bank_context;
5265ff6a55bSMatthias Ringwald     self->delete_tag_len = 0;
5275ff6a55bSMatthias Ringwald 
528ffded216SMatthias Ringwald     // BTSTACK_FLASH_ALIGNMENT_MAX must be larger than alignment
5291295b091SMatthias Ringwald     uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
530ffded216SMatthias Ringwald     btstack_assert(BTSTACK_FLASH_ALIGNMENT_MAX >= alignment);
531ffded216SMatthias Ringwald 
532ffded216SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
533ffded216SMatthias Ringwald 	// set delete tag len
5341295b091SMatthias Ringwald 	self->delete_tag_len = (uint8_t) btstack_max(4, alignment);
5355ff6a55bSMatthias Ringwald 	log_info("delete tag len %u", self->delete_tag_len);
53617ae5bc4SMatthias Ringwald 
537f268b03aSMatthias Ringwald     // set aligned entry header len
538f268b03aSMatthias Ringwald     self->entry_header_len = btstack_tlv_flash_bank_align_size(self, BTSTACK_TLV_ENTRY_HEADER_LEN);
539ffded216SMatthias Ringwald 	log_info("entry header len %u", self->entry_header_len);
5404a1addd5SMatthias Ringwald #else
541ffded216SMatthias Ringwald     UNUSED(alignment);
5424a1addd5SMatthias Ringwald     // data starts right after entry header
5434a1addd5SMatthias Ringwald     self->entry_header_len = BTSTACK_TLV_ENTRY_HEADER_LEN;
5444a1addd5SMatthias Ringwald #endif
545f268b03aSMatthias Ringwald 
54617ae5bc4SMatthias Ringwald 	// try to find current bank
54717ae5bc4SMatthias Ringwald 	self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self);
54817ae5bc4SMatthias Ringwald 	log_info("found bank %d", self->current_bank);
54917ae5bc4SMatthias Ringwald 	if (self->current_bank >= 0){
55017ae5bc4SMatthias Ringwald 
55117ae5bc4SMatthias Ringwald 		// find last entry and write offset
55217ae5bc4SMatthias Ringwald 		tlv_iterator_t it;
553052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
55417ae5bc4SMatthias Ringwald 		uint32_t last_tag = 0;
55517ae5bc4SMatthias Ringwald 		uint32_t last_offset = 0;
556052e5e42SMatthias Ringwald #endif
55717ae5bc4SMatthias Ringwald         btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
55817ae5bc4SMatthias Ringwald 		while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
559052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
56017ae5bc4SMatthias Ringwald 			last_tag = it.tag;
56117ae5bc4SMatthias Ringwald 			last_offset = it.offset;
562052e5e42SMatthias Ringwald #endif
56317ae5bc4SMatthias Ringwald 			tlv_iterator_fetch_next(self, &it);
56417ae5bc4SMatthias Ringwald 		}
56517ae5bc4SMatthias Ringwald 		self->write_offset = it.offset;
56617ae5bc4SMatthias Ringwald 
567e195acfcSMatthias Ringwald 		if (self->write_offset <= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
56817ae5bc4SMatthias Ringwald 
569052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
57017ae5bc4SMatthias Ringwald 			// delete older instances of last_tag
57117ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete
57217ae5bc4SMatthias Ringwald 			if (last_tag){
57317ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset);
57417ae5bc4SMatthias Ringwald 			}
575052e5e42SMatthias Ringwald #endif
57617ae5bc4SMatthias Ringwald 
57717ae5bc4SMatthias Ringwald 			// verify that rest of bank is empty
57817ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value was written, but not the tag
57917ae5bc4SMatthias Ringwald 			if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){
58017ae5bc4SMatthias Ringwald 				log_info("Flash not empty after last found tag -> migrate");
58117ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_migrate(self);
58217ae5bc4SMatthias Ringwald 			} else {
58317ae5bc4SMatthias Ringwald 				log_info("Flash clean after last found tag");
58417ae5bc4SMatthias Ringwald 			}
58517ae5bc4SMatthias Ringwald 		} else {
58617ae5bc4SMatthias Ringwald 			// failure!
58717ae5bc4SMatthias Ringwald 			self->current_bank = -1;
58817ae5bc4SMatthias Ringwald 		}
58917ae5bc4SMatthias Ringwald 	}
59017ae5bc4SMatthias Ringwald 
59117ae5bc4SMatthias Ringwald 	if (self->current_bank < 0) {
59217ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_erase_bank(self, 0);
59317ae5bc4SMatthias Ringwald 		self->current_bank = 0;
59417ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_write_header(self, self->current_bank, 0);	// epoch = 0;
595ffded216SMatthias Ringwald         self->write_offset = btstack_tlv_flash_bank_align_size (self, BTSTACK_TLV_BANK_HEADER_LEN);
59617ae5bc4SMatthias Ringwald 	}
59717ae5bc4SMatthias Ringwald 
59879b7220dSMatthias Ringwald 	log_info("write offset %" PRIx32, self->write_offset);
59917ae5bc4SMatthias Ringwald 	return &btstack_tlv_flash_bank;
60017ae5bc4SMatthias Ringwald }
60117ae5bc4SMatthias Ringwald 
602