xref: /btstack/platform/embedded/btstack_tlv_flash_bank.c (revision 60a3ef1384d36a605cdbdf12a02df326b71b22b6)
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
520d5520a9SMatthias Ringwald // - Delete: 32 delete field - only used with ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
5317ae5bc4SMatthias Ringwald // - Value: Len in bytes
5417ae5bc4SMatthias Ringwald 
552ea4a509SMatthias Ringwald // Alignment
562ea4a509SMatthias Ringwald // Tag and Delete (if it exists) are aligned to the alignment from hal_flash_bank_t
572ea4a509SMatthias Ringwald 
580d5520a9SMatthias Ringwald // ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
590d5520a9SMatthias Ringwald //
600d5520a9SMatthias Ringwald // Most Flash implementations allow to:
610d5520a9SMatthias Ringwald // - erase sector -> all values are 0xff
620d5520a9SMatthias Ringwald // - write value (1s -> 0s)
63052e5e42SMatthias Ringwald // - overwrite value with zero (remaining 1s -> 0s)
640d5520a9SMatthias Ringwald //
650d5520a9SMatthias Ringwald // We use the ability to overwrite a value with zeros to mark deleted enttries (by writing zero into the tag field).
66052e5e42SMatthias Ringwald // Some targets, E.g. Kinetix K64F, do enot allow for that.
670d5520a9SMatthias Ringwald //
680d5520a9SMatthias Ringwald // With ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD an extra field is reserved to indicate a deleted tag, while keeping main logic
69052e5e42SMatthias Ringwald //
70052e5e42SMatthias Ringwald // With ENABLE_TLV_FLASH_WRITE_ONCE, tags are never marked as deleted. Instead, an emtpy tag will be written instead.
71052e5e42SMatthias Ringwald //     Also, lookup and migrate requires to always search until the end of the valid bank
72052e5e42SMatthias Ringwald 
73052e5e42SMatthias Ringwald #if defined (ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD) && defined (ENABLE_TLV_FLASH_WRITE_ONCE)
74052e5e42SMatthias Ringwald #error "Please define either ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD or ENABLE_TLV_FLASH_WRITE_ONCE"
75052e5e42SMatthias Ringwald #endif
760d5520a9SMatthias Ringwald 
77b6c12bd6SMatthias Ringwald #define BTSTACK_TLV_BANK_HEADER_LEN  8
789b112178SMatthias Ringwald #define BTSTACK_TLV_ENTRY_HEADER_LEN 8
79e8efffc8SMatthias Ringwald 
80372a3556SMatthias Ringwald #ifndef BTSTACK_FLASH_ALIGNMENT_MAX
81372a3556SMatthias Ringwald #define BTSTACK_FLASH_ALIGNMENT_MAX 8
82372a3556SMatthias Ringwald #endif
83e8efffc8SMatthias Ringwald 
8417ae5bc4SMatthias Ringwald static const char * btstack_tlv_header_magic = "BTstack";
8517ae5bc4SMatthias Ringwald 
8617ae5bc4SMatthias Ringwald // TLV Iterator
8717ae5bc4SMatthias Ringwald typedef struct {
8817ae5bc4SMatthias Ringwald 	int 	 bank;
8917ae5bc4SMatthias Ringwald 	uint32_t offset;
904c7f5275SMatthias Ringwald     uint32_t size;
9117ae5bc4SMatthias Ringwald 	uint32_t tag;
9217ae5bc4SMatthias Ringwald 	uint32_t len;
9317ae5bc4SMatthias Ringwald } tlv_iterator_t;
9417ae5bc4SMatthias Ringwald 
95372a3556SMatthias Ringwald static uint32_t btstack_tlv_flash_bank_align_size(btstack_tlv_flash_bank_t * self, uint32_t size){
964c7f5275SMatthias Ringwald 	uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
974c7f5275SMatthias Ringwald 	return (size + alignment - 1) & ~(alignment - 1);
98372a3556SMatthias Ringwald }
99372a3556SMatthias Ringwald 
1002ea4a509SMatthias Ringwald static uint32_t btstack_tlv_flash_bank_aligned_entry_size(btstack_tlv_flash_bank_t * self, uint32_t size){
1012ea4a509SMatthias Ringwald     // entry_header_len is already padded
1022ea4a509SMatthias Ringwald     uint32_t aligned_size = self->entry_header_len + btstack_tlv_flash_bank_align_size(self, size);
1032ea4a509SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
1042ea4a509SMatthias Ringwald     // delete_tag_len is already padded
1052ea4a509SMatthias Ringwald     aligned_size += self->delete_tag_len;
1062ea4a509SMatthias Ringwald #endif
1072ea4a509SMatthias Ringwald     return aligned_size;
1082ea4a509SMatthias Ringwald }
1092ea4a509SMatthias Ringwald 
110372a3556SMatthias Ringwald // support unaligned flash read/writes
111372a3556SMatthias Ringwald // strategy: increase size to meet alignment, perform unaligned read/write of last chunk with helper buffer
112372a3556SMatthias Ringwald 
113e8efffc8SMatthias 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){
114372a3556SMatthias Ringwald 
115372a3556SMatthias Ringwald 	// read main data
1161295b091SMatthias Ringwald 	uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
1171295b091SMatthias Ringwald 	uint32_t lower_bits = size & (alignment - 1);
118372a3556SMatthias Ringwald 	uint32_t size_aligned = size - lower_bits;
119372a3556SMatthias Ringwald 	if (size_aligned){
120372a3556SMatthias Ringwald 		self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, size_aligned);
121372a3556SMatthias Ringwald 		buffer += size_aligned;
122372a3556SMatthias Ringwald 		offset += size_aligned;
123372a3556SMatthias Ringwald 		size   -= size_aligned;
124372a3556SMatthias Ringwald 	}
125372a3556SMatthias Ringwald 
126372a3556SMatthias Ringwald 	// read last part
127372a3556SMatthias Ringwald 	if (size == 0) return;
1281295b091SMatthias Ringwald 	uint8_t alignment_block[BTSTACK_FLASH_ALIGNMENT_MAX];
1291295b091SMatthias Ringwald 	self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, alignment_block, alignment);
1301295b091SMatthias Ringwald 	uint32_t bytes_to_copy = btstack_min(alignment - lower_bits, size);
1311295b091SMatthias Ringwald 	memcpy(buffer, alignment_block, bytes_to_copy);
132e8efffc8SMatthias Ringwald }
133e8efffc8SMatthias Ringwald 
134e8efffc8SMatthias 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){
135372a3556SMatthias Ringwald 
136372a3556SMatthias Ringwald 	// write main data
1371295b091SMatthias Ringwald 	uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
1381295b091SMatthias Ringwald 	uint32_t lower_bits = size & (alignment - 1);
139372a3556SMatthias Ringwald 	uint32_t size_aligned = size - lower_bits;
140372a3556SMatthias Ringwald 	if (size_aligned){
141372a3556SMatthias Ringwald 		self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, buffer, size_aligned);
142372a3556SMatthias Ringwald 		buffer += size_aligned;
143372a3556SMatthias Ringwald 		offset += size_aligned;
144372a3556SMatthias Ringwald 		size   -= size_aligned;
145e8efffc8SMatthias Ringwald 	}
146e8efffc8SMatthias Ringwald 
147372a3556SMatthias Ringwald 	// write last part
148372a3556SMatthias Ringwald 	if (size == 0) return;
1491295b091SMatthias Ringwald 	uint8_t alignment_block[BTSTACK_FLASH_ALIGNMENT_MAX];
1501295b091SMatthias Ringwald 	memset(alignment_block, 0xff, alignment);
1511295b091SMatthias Ringwald 	memcpy(alignment_block, buffer, lower_bits);
1521295b091SMatthias Ringwald 	self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, alignment_block, alignment);
153372a3556SMatthias Ringwald }
154372a3556SMatthias Ringwald 
155e8efffc8SMatthias Ringwald 
156e8efffc8SMatthias Ringwald // iterator
157e8efffc8SMatthias Ringwald 
15817ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
1594c7f5275SMatthias Ringwald     // abort if header doesn't fit into remaining space
1602ea4a509SMatthias Ringwald     if (it->offset + self->entry_header_len + self->delete_tag_len >= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
16126c447f6SMatthias Ringwald         it->tag = 0xffffffff;
16226c447f6SMatthias Ringwald         return;
16326c447f6SMatthias Ringwald     }
16426c447f6SMatthias Ringwald 
1659b112178SMatthias Ringwald 	uint8_t entry[BTSTACK_TLV_ENTRY_HEADER_LEN];
1669b112178SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, it->bank, it->offset, entry, BTSTACK_TLV_ENTRY_HEADER_LEN);
16717ae5bc4SMatthias Ringwald 	it->tag = big_endian_read_32(entry, 0);
16817ae5bc4SMatthias Ringwald 	it->len = big_endian_read_32(entry, 4);
1690d5520a9SMatthias Ringwald 
1700d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
1710d5520a9SMatthias Ringwald 	// clear tag, if delete field is set
1720d5520a9SMatthias Ringwald 	uint32_t delete_tag;
1732ea4a509SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, it->bank, it->offset + self->entry_header_len, (uint8_t *) &delete_tag, 4);
1740d5520a9SMatthias Ringwald 	if (delete_tag == 0){
1750d5520a9SMatthias Ringwald 		it->tag = 0;
1760d5520a9SMatthias Ringwald 	}
1770d5520a9SMatthias Ringwald #endif
17817ae5bc4SMatthias Ringwald }
17917ae5bc4SMatthias Ringwald 
18017ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it, int bank){
18117ae5bc4SMatthias Ringwald 	memset(it, 0, sizeof(tlv_iterator_t));
18217ae5bc4SMatthias Ringwald 	it->bank = bank;
183b6c12bd6SMatthias Ringwald 	it->offset = BTSTACK_TLV_BANK_HEADER_LEN;
1844c7f5275SMatthias Ringwald     it->size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context);
18517ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
18617ae5bc4SMatthias Ringwald }
18717ae5bc4SMatthias Ringwald 
1884c7f5275SMatthias Ringwald static bool btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
18979b7220dSMatthias Ringwald 	UNUSED(self);
1904c7f5275SMatthias Ringwald 	return it->tag != 0xffffffff;
19117ae5bc4SMatthias Ringwald }
19217ae5bc4SMatthias Ringwald 
19317ae5bc4SMatthias Ringwald static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
1942ea4a509SMatthias Ringwald     it->offset += btstack_tlv_flash_bank_aligned_entry_size(self, it->len);
1950d5520a9SMatthias Ringwald 
1960d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
1970d5520a9SMatthias Ringwald 	// skip delete field
1985ff6a55bSMatthias Ringwald 	it->offset += self->delete_tag_len;
1990d5520a9SMatthias Ringwald #endif
2000d5520a9SMatthias Ringwald 
2014c7f5275SMatthias Ringwald 	if (it->offset >= it->size) {
20217ae5bc4SMatthias Ringwald 		it->tag = 0xffffffff;
20317ae5bc4SMatthias Ringwald 		it->len = 0;
20417ae5bc4SMatthias Ringwald 		return;
20517ae5bc4SMatthias Ringwald 	}
20617ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
20717ae5bc4SMatthias Ringwald }
20817ae5bc4SMatthias Ringwald 
20917ae5bc4SMatthias Ringwald //
21017ae5bc4SMatthias Ringwald 
21117ae5bc4SMatthias Ringwald // check both banks for headers and pick the one with the higher epoch % 4
21217ae5bc4SMatthias Ringwald // @returns bank or -1 if something is invalid
21317ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){
214b6c12bd6SMatthias Ringwald  	uint8_t header0[BTSTACK_TLV_BANK_HEADER_LEN];
215b6c12bd6SMatthias Ringwald  	uint8_t header1[BTSTACK_TLV_BANK_HEADER_LEN];
216b6c12bd6SMatthias Ringwald  	btstack_tlv_flash_bank_read(self, 0, 0, &header0[0], BTSTACK_TLV_BANK_HEADER_LEN);
217b6c12bd6SMatthias Ringwald  	btstack_tlv_flash_bank_read(self, 1, 0, &header1[0], BTSTACK_TLV_BANK_HEADER_LEN);
218b6c12bd6SMatthias Ringwald  	int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_BANK_HEADER_LEN-1) == 0;
219b6c12bd6SMatthias Ringwald  	int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_BANK_HEADER_LEN-1) == 0;
22017ae5bc4SMatthias Ringwald 	if (!valid0 && !valid1) return -1;
22117ae5bc4SMatthias Ringwald 	if ( valid0 && !valid1) return 0;
22217ae5bc4SMatthias Ringwald 	if (!valid0 &&  valid1) return 1;
223b6c12bd6SMatthias Ringwald 	int epoch0 = header0[BTSTACK_TLV_BANK_HEADER_LEN-1] & 0x03;
224b6c12bd6SMatthias Ringwald 	int epoch1 = header1[BTSTACK_TLV_BANK_HEADER_LEN-1] & 0x03;
22517ae5bc4SMatthias Ringwald 	if (epoch0 == ((epoch1 + 1) & 0x03)) return 0;
22617ae5bc4SMatthias Ringwald 	if (epoch1 == ((epoch0 + 1) & 0x03)) return 1;
22717ae5bc4SMatthias Ringwald 	return -1;	// invalid, must not happen
22817ae5bc4SMatthias Ringwald }
22917ae5bc4SMatthias Ringwald 
23017ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self, int bank, int epoch){
231b6c12bd6SMatthias Ringwald 	uint8_t header[BTSTACK_TLV_BANK_HEADER_LEN];
232b6c12bd6SMatthias Ringwald 	memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_BANK_HEADER_LEN-1);
233b6c12bd6SMatthias Ringwald 	header[BTSTACK_TLV_BANK_HEADER_LEN-1] = epoch;
234b6c12bd6SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, bank, 0, header, BTSTACK_TLV_BANK_HEADER_LEN);
23517ae5bc4SMatthias Ringwald }
23617ae5bc4SMatthias Ringwald 
23717ae5bc4SMatthias Ringwald /**
23817ae5bc4SMatthias Ringwald  * @brief Check if erased from offset
23917ae5bc4SMatthias Ringwald  */
24017ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset){
24197d2cfbcSMatthias Ringwald 	log_info("test erased: bank %u, offset %u", bank, (unsigned int) offset);
24217ae5bc4SMatthias Ringwald 	uint32_t size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context);
24317ae5bc4SMatthias Ringwald 	uint8_t buffer[16];
24417ae5bc4SMatthias Ringwald 	uint8_t empty16[16];
24517ae5bc4SMatthias Ringwald 	memset(empty16, 0xff, sizeof(empty16));
24617ae5bc4SMatthias Ringwald 	while (offset < size){
24717ae5bc4SMatthias Ringwald 		uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset);
248e8efffc8SMatthias Ringwald 		btstack_tlv_flash_bank_read(self, bank, offset, buffer, copy_size);
2494c7f5275SMatthias Ringwald 		if (memcmp(buffer, empty16, copy_size) != 0) {
25097d2cfbcSMatthias Ringwald 			log_info("not erased %x - %x", (unsigned int) offset, (unsigned int) (offset + copy_size));
25117ae5bc4SMatthias Ringwald 			return 0;
25217ae5bc4SMatthias Ringwald 		}
25317ae5bc4SMatthias Ringwald 		offset += copy_size;
25417ae5bc4SMatthias Ringwald 	}
25517ae5bc4SMatthias Ringwald 	return 1;
25617ae5bc4SMatthias Ringwald }
25717ae5bc4SMatthias Ringwald 
25817ae5bc4SMatthias Ringwald /**
25917ae5bc4SMatthias Ringwald  * @brief erase bank (only if not already erased)
26017ae5bc4SMatthias Ringwald  */
26117ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self, int bank){
26217ae5bc4SMatthias Ringwald 	if (btstack_tlv_flash_bank_test_erased(self, bank, 0)){
26317ae5bc4SMatthias Ringwald 		log_info("bank %u already erased", bank);
26417ae5bc4SMatthias Ringwald 	} else {
26517ae5bc4SMatthias Ringwald 		log_info("bank %u not empty, erase bank", bank);
26617ae5bc4SMatthias Ringwald 		self->hal_flash_bank_impl->erase(self->hal_flash_bank_context, bank);
26717ae5bc4SMatthias Ringwald 	}
26817ae5bc4SMatthias Ringwald }
26917ae5bc4SMatthias Ringwald 
27017ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){
27117ae5bc4SMatthias Ringwald 
27217ae5bc4SMatthias Ringwald 	int next_bank = 1 - self->current_bank;
27317ae5bc4SMatthias Ringwald 	log_info("migrate bank %u -> bank %u", self->current_bank, next_bank);
27417ae5bc4SMatthias Ringwald 	// erase bank (if needed)
27517ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_erase_bank(self, next_bank);
276b6c12bd6SMatthias Ringwald 	int next_write_pos = BTSTACK_TLV_BANK_HEADER_LEN;
27717ae5bc4SMatthias Ringwald 
27817ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
27917ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
28017ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
28117ae5bc4SMatthias Ringwald 		// skip deleted entries
28217ae5bc4SMatthias Ringwald 		if (it.tag) {
28317ae5bc4SMatthias Ringwald 			uint32_t tag_len = it.len;
28417ae5bc4SMatthias Ringwald 			uint32_t tag_index = it.offset;
28517ae5bc4SMatthias Ringwald 
286052e5e42SMatthias Ringwald             bool tag_valid = true;
287052e5e42SMatthias Ringwald 
288052e5e42SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_WRITE_ONCE
289052e5e42SMatthias Ringwald             // search until end for newer entry of same tag
290052e5e42SMatthias Ringwald             tlv_iterator_t it2;
291052e5e42SMatthias Ringwald             memcpy(&it2, &it, sizeof(tlv_iterator_t));
292052e5e42SMatthias Ringwald             while (btstack_tlv_flash_bank_iterator_has_next(self, &it2)){
293052e5e42SMatthias Ringwald                 if ((it2.offset != it.offset) && (it2.tag == it.tag)){
294052e5e42SMatthias Ringwald                     tag_valid = false;
295052e5e42SMatthias Ringwald                     break;
296052e5e42SMatthias Ringwald                 }
297052e5e42SMatthias Ringwald                 tlv_iterator_fetch_next(self, &it2);
298052e5e42SMatthias Ringwald             }
299052e5e42SMatthias Ringwald             if (tag_valid == false){
300052e5e42SMatthias Ringwald 			    log_info("skip pos %u, tag '%x' as newer entry found at %u", (unsigned int) tag_index, (unsigned int) it.tag,
301052e5e42SMatthias Ringwald                     (unsigned int) it2.offset);
302052e5e42SMatthias Ringwald             }
303052e5e42SMatthias Ringwald #endif
304052e5e42SMatthias Ringwald 
305052e5e42SMatthias Ringwald             if (tag_valid) {
306052e5e42SMatthias Ringwald 
30797d2cfbcSMatthias Ringwald                 log_info("migrate pos %u, tag '%x' len %u -> new pos %u",
30897d2cfbcSMatthias Ringwald                          (unsigned int) tag_index, (unsigned int) it.tag, (unsigned int) tag_len, next_write_pos);
3090d5520a9SMatthias Ringwald 
3100d5520a9SMatthias Ringwald                 // copy header
3119b112178SMatthias Ringwald                 uint8_t header_buffer[BTSTACK_TLV_ENTRY_HEADER_LEN];
3129b112178SMatthias Ringwald                 btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, header_buffer, BTSTACK_TLV_ENTRY_HEADER_LEN);
3139b112178SMatthias Ringwald                 btstack_tlv_flash_bank_write(self, next_bank, next_write_pos, header_buffer, BTSTACK_TLV_ENTRY_HEADER_LEN);
3142ea4a509SMatthias Ringwald                 tag_index      += self->entry_header_len;
3152ea4a509SMatthias Ringwald                 uint32_t write_offset = next_write_pos + self->entry_header_len;
3160d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
3170d5520a9SMatthias Ringwald                 // skip delete field
3185ff6a55bSMatthias Ringwald                 tag_index      += self->delete_tag_len;
3190d5520a9SMatthias Ringwald #endif
3200d5520a9SMatthias Ringwald                 // copy value
3210d5520a9SMatthias Ringwald                 int bytes_to_copy = tag_len;
32217ae5bc4SMatthias Ringwald                 uint8_t copy_buffer[32];
32317ae5bc4SMatthias Ringwald                 while (bytes_to_copy) {
32417ae5bc4SMatthias Ringwald                     int bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer));
325e8efffc8SMatthias Ringwald                     btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
3262ea4a509SMatthias Ringwald                     btstack_tlv_flash_bank_write(self, next_bank, write_offset, copy_buffer, bytes_this_iteration);
32717ae5bc4SMatthias Ringwald                     tag_index     += bytes_this_iteration;
3282ea4a509SMatthias Ringwald                     write_offset  += bytes_this_iteration;
32917ae5bc4SMatthias Ringwald                     bytes_to_copy -= bytes_this_iteration;
33017ae5bc4SMatthias Ringwald                 }
3312ea4a509SMatthias Ringwald                 next_write_pos += btstack_tlv_flash_bank_aligned_entry_size(self, tag_len);
33217ae5bc4SMatthias Ringwald             }
333052e5e42SMatthias Ringwald 		}
33417ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
33517ae5bc4SMatthias Ringwald 	}
33617ae5bc4SMatthias Ringwald 
33717ae5bc4SMatthias Ringwald 	// prepare new one
33817ae5bc4SMatthias Ringwald 	uint8_t epoch_buffer;
339b6c12bd6SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, self->current_bank, BTSTACK_TLV_BANK_HEADER_LEN-1, &epoch_buffer, 1);
34017ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3);
34117ae5bc4SMatthias Ringwald 	self->current_bank = next_bank;
34217ae5bc4SMatthias Ringwald 	self->write_offset = next_write_pos;
34317ae5bc4SMatthias Ringwald }
34417ae5bc4SMatthias Ringwald 
345052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
34617ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self, uint32_t tag, uint32_t offset){
34717ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
34817ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
34917ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it) && it.offset < offset){
35017ae5bc4SMatthias Ringwald 		if (it.tag == tag){
35197d2cfbcSMatthias Ringwald 			log_info("Erase tag '%x' at position %u", (unsigned int) tag, (unsigned int) it.offset);
3520d5520a9SMatthias Ringwald 
3530d5520a9SMatthias Ringwald 			// mark entry as invalid
3540d5520a9SMatthias Ringwald 			uint32_t zero_value = 0;
3550d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
3569b112178SMatthias Ringwald 			// write delete field after entry header
357*60a3ef13SMatthias Ringwald 			btstack_tlv_flash_bank_write(self, self->current_bank, it.offset+self->entry_header_len, (uint8_t*) &zero_value, sizeof(zero_value));
3580d5520a9SMatthias Ringwald #else
3595574c439SMatthias Ringwald             uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
3605574c439SMatthias Ringwald             if (alignment <= 4){
3615574c439SMatthias Ringwald                 // if alignment < 4, overwrite only tag with zero value
3620d5520a9SMatthias Ringwald                 btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_value, sizeof(zero_value));
3635574c439SMatthias Ringwald             } else {
3645574c439SMatthias Ringwald                 // otherwise, overwrite complete entry. This results in a sequence of { tag: 0, len: 0 } entries
3655574c439SMatthias Ringwald                 uint8_t zero_buffer[32];
3665574c439SMatthias Ringwald                 memset(zero_buffer, 0, sizeof(zero_buffer));
3675574c439SMatthias Ringwald                 uint32_t entry_offset = 0;
3685574c439SMatthias Ringwald                 uint32_t entry_size = btstack_tlv_flash_bank_aligned_entry_size(self, it.len);
3695574c439SMatthias Ringwald                 while (entry_offset < entry_size) {
3705574c439SMatthias Ringwald                     uint32_t bytes_to_write = btstack_min(entry_size - entry_offset, sizeof(zero_buffer));
3715574c439SMatthias Ringwald                     btstack_tlv_flash_bank_write(self, self->current_bank, it.offset + entry_offset, zero_buffer, bytes_to_write);
3725574c439SMatthias Ringwald                     entry_offset += bytes_to_write;
3735574c439SMatthias Ringwald                 }
3745574c439SMatthias Ringwald             }
3750d5520a9SMatthias Ringwald #endif
37617ae5bc4SMatthias Ringwald 		}
37717ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
37817ae5bc4SMatthias Ringwald 	}
37917ae5bc4SMatthias Ringwald }
380052e5e42SMatthias Ringwald #endif
38117ae5bc4SMatthias Ringwald 
38217ae5bc4SMatthias Ringwald /**
38317ae5bc4SMatthias Ringwald  * Get Value for Tag
38417ae5bc4SMatthias Ringwald  * @param tag
38517ae5bc4SMatthias Ringwald  * @param buffer
38617ae5bc4SMatthias Ringwald  * @param buffer_size
38717ae5bc4SMatthias Ringwald  * @returns size of value
38817ae5bc4SMatthias Ringwald  */
38917ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
39017ae5bc4SMatthias Ringwald 
39117ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
39217ae5bc4SMatthias Ringwald 
39317ae5bc4SMatthias Ringwald 	uint32_t tag_index = 0;
39417ae5bc4SMatthias Ringwald 	uint32_t tag_len   = 0;
39517ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
39617ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
39717ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
39817ae5bc4SMatthias Ringwald 		if (it.tag == tag){
39997d2cfbcSMatthias Ringwald 			log_info("Found tag '%x' at position %u", (unsigned int) tag, (unsigned int) it.offset);
40017ae5bc4SMatthias Ringwald 			tag_index = it.offset;
40117ae5bc4SMatthias Ringwald 			tag_len   = it.len;
402052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
40317ae5bc4SMatthias Ringwald 			break;
404052e5e42SMatthias Ringwald #endif
40517ae5bc4SMatthias Ringwald 		}
40617ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
40717ae5bc4SMatthias Ringwald 	}
40817ae5bc4SMatthias Ringwald 	if (tag_index == 0) return 0;
40917ae5bc4SMatthias Ringwald 	if (!buffer) return tag_len;
41017ae5bc4SMatthias Ringwald 	int copy_size = btstack_min(buffer_size, tag_len);
411*60a3ef13SMatthias Ringwald 	uint32_t value_offset = tag_index + self->entry_header_len;
4120d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
4130d5520a9SMatthias Ringwald 	// skip delete field
4145ff6a55bSMatthias Ringwald 	value_offset += self->delete_tag_len;
4150d5520a9SMatthias Ringwald #endif
4160d5520a9SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, self->current_bank, value_offset, buffer, copy_size);
41717ae5bc4SMatthias Ringwald 	return copy_size;
41817ae5bc4SMatthias Ringwald }
41917ae5bc4SMatthias Ringwald 
42017ae5bc4SMatthias Ringwald /**
42117ae5bc4SMatthias Ringwald  * Store Tag
42217ae5bc4SMatthias Ringwald  * @param tag
42317ae5bc4SMatthias Ringwald  * @param data
42417ae5bc4SMatthias Ringwald  * @param data_size
42517ae5bc4SMatthias Ringwald  */
42617ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
42717ae5bc4SMatthias Ringwald 
42817ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
42917ae5bc4SMatthias Ringwald 
43017ae5bc4SMatthias Ringwald 	// trigger migration if not enough space
431*60a3ef13SMatthias Ringwald 	uint32_t required_space = self->entry_header_len + self->delete_tag_len + data_size;
4325ff6a55bSMatthias Ringwald 	if (self->write_offset + required_space > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
43317ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_migrate(self);
43417ae5bc4SMatthias Ringwald 	}
43517ae5bc4SMatthias Ringwald 
4365ff6a55bSMatthias Ringwald 	if (self->write_offset + required_space > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
43717ae5bc4SMatthias Ringwald 		log_error("couldn't write entry, not enough space left");
43817ae5bc4SMatthias Ringwald 		return 2;
43917ae5bc4SMatthias Ringwald 	}
44017ae5bc4SMatthias Ringwald 
44117ae5bc4SMatthias Ringwald 	// prepare entry
4429b112178SMatthias Ringwald 	uint8_t entry[BTSTACK_TLV_ENTRY_HEADER_LEN];
44317ae5bc4SMatthias Ringwald 	big_endian_store_32(entry, 0, tag);
44417ae5bc4SMatthias Ringwald 	big_endian_store_32(entry, 4, data_size);
44517ae5bc4SMatthias Ringwald 
44679b7220dSMatthias Ringwald 	log_info("write '%" PRIx32 "', len %" PRIu32 " at %" PRIx32, tag, data_size, self->write_offset);
44717ae5bc4SMatthias Ringwald 
448*60a3ef13SMatthias Ringwald 	uint32_t value_offset = self->write_offset + self->entry_header_len;
4490d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
4500d5520a9SMatthias Ringwald 	// skip delete field
4515ff6a55bSMatthias Ringwald 	value_offset += self->delete_tag_len;
4520d5520a9SMatthias Ringwald #endif
4530d5520a9SMatthias Ringwald 
45417ae5bc4SMatthias Ringwald 	// write value first
4550d5520a9SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, self->current_bank, value_offset, data, data_size);
45617ae5bc4SMatthias Ringwald 
45717ae5bc4SMatthias Ringwald 	// then entry
458e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset, entry, sizeof(entry));
45917ae5bc4SMatthias Ringwald 
460052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
46117ae5bc4SMatthias Ringwald 	// overwrite old entries (if exists)
46217ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
463052e5e42SMatthias Ringwald #endif
46417ae5bc4SMatthias Ringwald 
46517ae5bc4SMatthias Ringwald 	// done
466372a3556SMatthias Ringwald 	self->write_offset += sizeof(entry) + btstack_tlv_flash_bank_align_size(self, data_size);
46717ae5bc4SMatthias Ringwald 
4680d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
4690d5520a9SMatthias Ringwald 	// skip delete field
4705ff6a55bSMatthias Ringwald 	self->write_offset += self->delete_tag_len;
4710d5520a9SMatthias Ringwald #endif
4720d5520a9SMatthias Ringwald 
47317ae5bc4SMatthias Ringwald 	return 0;
47417ae5bc4SMatthias Ringwald }
47517ae5bc4SMatthias Ringwald 
47617ae5bc4SMatthias Ringwald /**
47717ae5bc4SMatthias Ringwald  * Delete Tag
47817ae5bc4SMatthias Ringwald  * @param tag
47917ae5bc4SMatthias Ringwald  */
48017ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag(void * context, uint32_t tag){
481052e5e42SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_WRITE_ONCE
482052e5e42SMatthias Ringwald     btstack_tlv_flash_bank_store_tag(context, tag, NULL, 0);
483052e5e42SMatthias Ringwald #else
48417ae5bc4SMatthias Ringwald     btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
48517ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
486052e5e42SMatthias Ringwald #endif
48717ae5bc4SMatthias Ringwald }
48817ae5bc4SMatthias Ringwald 
48917ae5bc4SMatthias Ringwald static const btstack_tlv_t btstack_tlv_flash_bank = {
49017ae5bc4SMatthias Ringwald 	/* int  (*get_tag)(..);     */ &btstack_tlv_flash_bank_get_tag,
49117ae5bc4SMatthias Ringwald 	/* int (*store_tag)(..);    */ &btstack_tlv_flash_bank_store_tag,
49217ae5bc4SMatthias Ringwald 	/* void (*delete_tag)(v..); */ &btstack_tlv_flash_bank_delete_tag,
49317ae5bc4SMatthias Ringwald };
49417ae5bc4SMatthias Ringwald 
49517ae5bc4SMatthias Ringwald /**
49617ae5bc4SMatthias Ringwald  * Init Tag Length Value Store
49717ae5bc4SMatthias Ringwald  */
49817ae5bc4SMatthias 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){
49917ae5bc4SMatthias Ringwald 
50017ae5bc4SMatthias Ringwald 	self->hal_flash_bank_impl    = hal_flash_bank_impl;
50117ae5bc4SMatthias Ringwald 	self->hal_flash_bank_context = hal_flash_bank_context;
5025ff6a55bSMatthias Ringwald 	self->delete_tag_len = 0;
5035ff6a55bSMatthias Ringwald 
5045ff6a55bSMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
5055ff6a55bSMatthias Ringwald 	if (hal_flash_bank_impl->get_alignment(hal_flash_bank_context) > 8){
5065ff6a55bSMatthias Ringwald 		log_error("Flash alignment > 8 with ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD not supported");
5075ff6a55bSMatthias Ringwald 		return NULL;
5085ff6a55bSMatthias Ringwald 	}
5095ff6a55bSMatthias Ringwald 	// set delete tag len
5101295b091SMatthias Ringwald 	uint32_t alignment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
5111295b091SMatthias Ringwald 	self->delete_tag_len = (uint8_t) btstack_max(4, alignment);
5125ff6a55bSMatthias Ringwald 	log_info("delete tag len %u", self->delete_tag_len);
5135ff6a55bSMatthias Ringwald #endif
51417ae5bc4SMatthias Ringwald 
515f268b03aSMatthias Ringwald     // set aligned entry header len
516f268b03aSMatthias Ringwald     self->entry_header_len = btstack_tlv_flash_bank_align_size(self, BTSTACK_TLV_ENTRY_HEADER_LEN);
517f268b03aSMatthias Ringwald 
51817ae5bc4SMatthias Ringwald 	// try to find current bank
51917ae5bc4SMatthias Ringwald 	self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self);
52017ae5bc4SMatthias Ringwald 	log_info("found bank %d", self->current_bank);
52117ae5bc4SMatthias Ringwald 	if (self->current_bank >= 0){
52217ae5bc4SMatthias Ringwald 
52317ae5bc4SMatthias Ringwald 		// find last entry and write offset
52417ae5bc4SMatthias Ringwald 		tlv_iterator_t it;
525052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
52617ae5bc4SMatthias Ringwald 		uint32_t last_tag = 0;
52717ae5bc4SMatthias Ringwald 		uint32_t last_offset = 0;
528052e5e42SMatthias Ringwald #endif
52917ae5bc4SMatthias Ringwald         btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
53017ae5bc4SMatthias Ringwald 		while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
531052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
53217ae5bc4SMatthias Ringwald 			last_tag = it.tag;
53317ae5bc4SMatthias Ringwald 			last_offset = it.offset;
534052e5e42SMatthias Ringwald #endif
53517ae5bc4SMatthias Ringwald 			tlv_iterator_fetch_next(self, &it);
53617ae5bc4SMatthias Ringwald 		}
53717ae5bc4SMatthias Ringwald 		self->write_offset = it.offset;
53817ae5bc4SMatthias Ringwald 
539e195acfcSMatthias Ringwald 		if (self->write_offset <= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
54017ae5bc4SMatthias Ringwald 
541052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
54217ae5bc4SMatthias Ringwald 			// delete older instances of last_tag
54317ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete
54417ae5bc4SMatthias Ringwald 			if (last_tag){
54517ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset);
54617ae5bc4SMatthias Ringwald 			}
547052e5e42SMatthias Ringwald #endif
54817ae5bc4SMatthias Ringwald 
54917ae5bc4SMatthias Ringwald 			// verify that rest of bank is empty
55017ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value was written, but not the tag
55117ae5bc4SMatthias Ringwald 			if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){
55217ae5bc4SMatthias Ringwald 				log_info("Flash not empty after last found tag -> migrate");
55317ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_migrate(self);
55417ae5bc4SMatthias Ringwald 			} else {
55517ae5bc4SMatthias Ringwald 				log_info("Flash clean after last found tag");
55617ae5bc4SMatthias Ringwald 			}
55717ae5bc4SMatthias Ringwald 		} else {
55817ae5bc4SMatthias Ringwald 			// failure!
55917ae5bc4SMatthias Ringwald 			self->current_bank = -1;
56017ae5bc4SMatthias Ringwald 		}
56117ae5bc4SMatthias Ringwald 	}
56217ae5bc4SMatthias Ringwald 
56317ae5bc4SMatthias Ringwald 	if (self->current_bank < 0) {
56417ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_erase_bank(self, 0);
56517ae5bc4SMatthias Ringwald 		self->current_bank = 0;
56617ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_write_header(self, self->current_bank, 0);	// epoch = 0;
567b6c12bd6SMatthias Ringwald 		self->write_offset = BTSTACK_TLV_BANK_HEADER_LEN;
56817ae5bc4SMatthias Ringwald 	}
56917ae5bc4SMatthias Ringwald 
57079b7220dSMatthias Ringwald 	log_info("write offset %" PRIx32, self->write_offset);
57117ae5bc4SMatthias Ringwald 	return &btstack_tlv_flash_bank;
57217ae5bc4SMatthias Ringwald }
57317ae5bc4SMatthias Ringwald 
574