xref: /btstack/platform/embedded/btstack_tlv_flash_bank.c (revision e195acfc185b7844c375a5433e7ddeeb9a5fb309)
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 
550d5520a9SMatthias Ringwald // ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
560d5520a9SMatthias Ringwald //
570d5520a9SMatthias Ringwald // Most Flash implementations allow to:
580d5520a9SMatthias Ringwald // - erase sector -> all values are 0xff
590d5520a9SMatthias Ringwald // - write value (1s -> 0s)
60052e5e42SMatthias Ringwald // - overwrite value with zero (remaining 1s -> 0s)
610d5520a9SMatthias Ringwald //
620d5520a9SMatthias Ringwald // We use the ability to overwrite a value with zeros to mark deleted enttries (by writing zero into the tag field).
63052e5e42SMatthias Ringwald // Some targets, E.g. Kinetix K64F, do enot allow for that.
640d5520a9SMatthias Ringwald //
650d5520a9SMatthias Ringwald // With ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD an extra field is reserved to indicate a deleted tag, while keeping main logic
66052e5e42SMatthias Ringwald //
67052e5e42SMatthias Ringwald // With ENABLE_TLV_FLASH_WRITE_ONCE, tags are never marked as deleted. Instead, an emtpy tag will be written instead.
68052e5e42SMatthias Ringwald //     Also, lookup and migrate requires to always search until the end of the valid bank
69052e5e42SMatthias Ringwald 
70052e5e42SMatthias Ringwald #if defined (ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD) && defined (ENABLE_TLV_FLASH_WRITE_ONCE)
71052e5e42SMatthias Ringwald #error "Please define either ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD or ENABLE_TLV_FLASH_WRITE_ONCE"
72052e5e42SMatthias Ringwald #endif
730d5520a9SMatthias Ringwald 
7417ae5bc4SMatthias Ringwald #define BTSTACK_TLV_HEADER_LEN 8
75e8efffc8SMatthias Ringwald 
76372a3556SMatthias Ringwald #ifndef BTSTACK_FLASH_ALIGNMENT_MAX
77372a3556SMatthias Ringwald #define BTSTACK_FLASH_ALIGNMENT_MAX 8
78372a3556SMatthias Ringwald #endif
79e8efffc8SMatthias Ringwald 
8017ae5bc4SMatthias Ringwald static const char * btstack_tlv_header_magic = "BTstack";
8117ae5bc4SMatthias Ringwald 
8217ae5bc4SMatthias Ringwald // TLV Iterator
8317ae5bc4SMatthias Ringwald typedef struct {
8417ae5bc4SMatthias Ringwald 	int 	 bank;
8517ae5bc4SMatthias Ringwald 	uint32_t offset;
8617ae5bc4SMatthias Ringwald 	uint32_t tag;
8717ae5bc4SMatthias Ringwald 	uint32_t len;
8817ae5bc4SMatthias Ringwald } tlv_iterator_t;
8917ae5bc4SMatthias Ringwald 
90372a3556SMatthias Ringwald static uint32_t btstack_tlv_flash_bank_align_size(btstack_tlv_flash_bank_t * self, uint32_t size){
91372a3556SMatthias Ringwald 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
92372a3556SMatthias Ringwald 	return (size + aligment - 1) & ~(aligment - 1);
93372a3556SMatthias Ringwald }
94372a3556SMatthias Ringwald 
95372a3556SMatthias Ringwald // support unaligned flash read/writes
96372a3556SMatthias Ringwald // strategy: increase size to meet alignment, perform unaligned read/write of last chunk with helper buffer
97372a3556SMatthias Ringwald 
98e8efffc8SMatthias 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){
99372a3556SMatthias Ringwald 
100372a3556SMatthias Ringwald 	// read main data
101372a3556SMatthias Ringwald 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
102372a3556SMatthias Ringwald 	uint32_t lower_bits = size & (aligment - 1);
103372a3556SMatthias Ringwald 	uint32_t size_aligned = size - lower_bits;
104372a3556SMatthias Ringwald 	if (size_aligned){
105372a3556SMatthias Ringwald 		self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, size_aligned);
106372a3556SMatthias Ringwald 		buffer += size_aligned;
107372a3556SMatthias Ringwald 		offset += size_aligned;
108372a3556SMatthias Ringwald 		size   -= size_aligned;
109372a3556SMatthias Ringwald 	}
110372a3556SMatthias Ringwald 
111372a3556SMatthias Ringwald 	// read last part
112372a3556SMatthias Ringwald 	if (size == 0) return;
113372a3556SMatthias Ringwald 	uint8_t aligment_block[BTSTACK_FLASH_ALIGNMENT_MAX];
114372a3556SMatthias Ringwald 	self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, aligment_block, aligment);
115372a3556SMatthias Ringwald 	uint32_t bytes_to_copy = btstack_min(aligment - lower_bits, size);
116372a3556SMatthias Ringwald 	memcpy(buffer, aligment_block, bytes_to_copy);
117e8efffc8SMatthias Ringwald }
118e8efffc8SMatthias Ringwald 
119e8efffc8SMatthias 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){
120372a3556SMatthias Ringwald 
121372a3556SMatthias Ringwald 	// write main data
122372a3556SMatthias Ringwald 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
123372a3556SMatthias Ringwald 	uint32_t lower_bits = size & (aligment - 1);
124372a3556SMatthias Ringwald 	uint32_t size_aligned = size - lower_bits;
125372a3556SMatthias Ringwald 	if (size_aligned){
126372a3556SMatthias Ringwald 		self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, buffer, size_aligned);
127372a3556SMatthias Ringwald 		buffer += size_aligned;
128372a3556SMatthias Ringwald 		offset += size_aligned;
129372a3556SMatthias Ringwald 		size   -= size_aligned;
130e8efffc8SMatthias Ringwald 	}
131e8efffc8SMatthias Ringwald 
132372a3556SMatthias Ringwald 	// write last part
133372a3556SMatthias Ringwald 	if (size == 0) return;
134372a3556SMatthias Ringwald 	uint8_t aligment_block[BTSTACK_FLASH_ALIGNMENT_MAX];
135372a3556SMatthias Ringwald 	memset(aligment_block, 0xff, aligment);
136372a3556SMatthias Ringwald 	memcpy(aligment_block, buffer, lower_bits);
137372a3556SMatthias Ringwald 	self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, aligment_block, aligment);
138372a3556SMatthias Ringwald }
139372a3556SMatthias Ringwald 
140e8efffc8SMatthias Ringwald 
141e8efffc8SMatthias Ringwald // iterator
142e8efffc8SMatthias Ringwald 
14317ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
14417ae5bc4SMatthias Ringwald 	uint8_t entry[8];
145e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, it->bank, it->offset, entry, 8);
14617ae5bc4SMatthias Ringwald 	it->tag = big_endian_read_32(entry, 0);
14717ae5bc4SMatthias Ringwald 	it->len = big_endian_read_32(entry, 4);
1480d5520a9SMatthias Ringwald 
1490d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
1500d5520a9SMatthias Ringwald 	// clear tag, if delete field is set
1510d5520a9SMatthias Ringwald 	uint32_t delete_tag;
1520d5520a9SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, it->bank, it->offset + 8, (uint8_t *) &delete_tag, 4);
1530d5520a9SMatthias Ringwald 	if (delete_tag == 0){
1540d5520a9SMatthias Ringwald 		it->tag = 0;
1550d5520a9SMatthias Ringwald 	}
1560d5520a9SMatthias Ringwald #endif
15717ae5bc4SMatthias Ringwald }
15817ae5bc4SMatthias Ringwald 
15917ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it, int bank){
16017ae5bc4SMatthias Ringwald 	memset(it, 0, sizeof(tlv_iterator_t));
16117ae5bc4SMatthias Ringwald 	it->bank = bank;
16217ae5bc4SMatthias Ringwald 	it->offset = BTSTACK_TLV_HEADER_LEN;
16317ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
16417ae5bc4SMatthias Ringwald }
16517ae5bc4SMatthias Ringwald 
16617ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
16779b7220dSMatthias Ringwald 	UNUSED(self);
16817ae5bc4SMatthias Ringwald 	if (it->tag == 0xffffffff) return 0;
16917ae5bc4SMatthias Ringwald 	return 1;
17017ae5bc4SMatthias Ringwald }
17117ae5bc4SMatthias Ringwald 
17217ae5bc4SMatthias Ringwald static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
173372a3556SMatthias Ringwald 	it->offset += 8 + btstack_tlv_flash_bank_align_size(self, it->len);
1740d5520a9SMatthias Ringwald 
1750d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
1760d5520a9SMatthias Ringwald 	// skip delete field
1775ff6a55bSMatthias Ringwald 	it->offset += self->delete_tag_len;
1780d5520a9SMatthias Ringwald #endif
1790d5520a9SMatthias Ringwald 
18017ae5bc4SMatthias Ringwald 	if (it->offset >= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)) {
18117ae5bc4SMatthias Ringwald 		it->tag = 0xffffffff;
18217ae5bc4SMatthias Ringwald 		it->len = 0;
18317ae5bc4SMatthias Ringwald 		return;
18417ae5bc4SMatthias Ringwald 	}
18517ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
18617ae5bc4SMatthias Ringwald }
18717ae5bc4SMatthias Ringwald 
18817ae5bc4SMatthias Ringwald //
18917ae5bc4SMatthias Ringwald 
19017ae5bc4SMatthias Ringwald // check both banks for headers and pick the one with the higher epoch % 4
19117ae5bc4SMatthias Ringwald // @returns bank or -1 if something is invalid
19217ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){
19317ae5bc4SMatthias Ringwald  	uint8_t header0[BTSTACK_TLV_HEADER_LEN];
19417ae5bc4SMatthias Ringwald  	uint8_t header1[BTSTACK_TLV_HEADER_LEN];
195e8efffc8SMatthias Ringwald  	btstack_tlv_flash_bank_read(self, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN);
196e8efffc8SMatthias Ringwald  	btstack_tlv_flash_bank_read(self, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN);
19717ae5bc4SMatthias Ringwald  	int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
19817ae5bc4SMatthias Ringwald  	int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
19917ae5bc4SMatthias Ringwald 	if (!valid0 && !valid1) return -1;
20017ae5bc4SMatthias Ringwald 	if ( valid0 && !valid1) return 0;
20117ae5bc4SMatthias Ringwald 	if (!valid0 &&  valid1) return 1;
20217ae5bc4SMatthias Ringwald 	int epoch0 = header0[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
20317ae5bc4SMatthias Ringwald 	int epoch1 = header1[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
20417ae5bc4SMatthias Ringwald 	if (epoch0 == ((epoch1 + 1) & 0x03)) return 0;
20517ae5bc4SMatthias Ringwald 	if (epoch1 == ((epoch0 + 1) & 0x03)) return 1;
20617ae5bc4SMatthias Ringwald 	return -1;	// invalid, must not happen
20717ae5bc4SMatthias Ringwald }
20817ae5bc4SMatthias Ringwald 
20917ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self, int bank, int epoch){
21017ae5bc4SMatthias Ringwald 	uint8_t header[BTSTACK_TLV_HEADER_LEN];
21117ae5bc4SMatthias Ringwald 	memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1);
21217ae5bc4SMatthias Ringwald 	header[BTSTACK_TLV_HEADER_LEN-1] = epoch;
213e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, bank, 0, header, BTSTACK_TLV_HEADER_LEN);
21417ae5bc4SMatthias Ringwald }
21517ae5bc4SMatthias Ringwald 
21617ae5bc4SMatthias Ringwald /**
21717ae5bc4SMatthias Ringwald  * @brief Check if erased from offset
21817ae5bc4SMatthias Ringwald  */
21917ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset){
22097d2cfbcSMatthias Ringwald 	log_info("test erased: bank %u, offset %u", bank, (unsigned int) offset);
22117ae5bc4SMatthias Ringwald 	uint32_t size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context);
22217ae5bc4SMatthias Ringwald 	uint8_t buffer[16];
22317ae5bc4SMatthias Ringwald 	uint8_t empty16[16];
22417ae5bc4SMatthias Ringwald 	memset(empty16, 0xff, sizeof(empty16));
22517ae5bc4SMatthias Ringwald 	while (offset < size){
22617ae5bc4SMatthias Ringwald 		uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset);
227e8efffc8SMatthias Ringwald 		btstack_tlv_flash_bank_read(self, bank, offset, buffer, copy_size);
22817ae5bc4SMatthias Ringwald 		if (memcmp(buffer, empty16, copy_size)) {
22997d2cfbcSMatthias Ringwald 			log_info("not erased %x - %x", (unsigned int) offset, (unsigned int) (offset + copy_size));
23017ae5bc4SMatthias Ringwald 			return 0;
23117ae5bc4SMatthias Ringwald 		}
23217ae5bc4SMatthias Ringwald 		offset += copy_size;
23317ae5bc4SMatthias Ringwald 	}
23417ae5bc4SMatthias Ringwald 	return 1;
23517ae5bc4SMatthias Ringwald }
23617ae5bc4SMatthias Ringwald 
23717ae5bc4SMatthias Ringwald /**
23817ae5bc4SMatthias Ringwald  * @brief erase bank (only if not already erased)
23917ae5bc4SMatthias Ringwald  */
24017ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self, int bank){
24117ae5bc4SMatthias Ringwald 	if (btstack_tlv_flash_bank_test_erased(self, bank, 0)){
24217ae5bc4SMatthias Ringwald 		log_info("bank %u already erased", bank);
24317ae5bc4SMatthias Ringwald 	} else {
24417ae5bc4SMatthias Ringwald 		log_info("bank %u not empty, erase bank", bank);
24517ae5bc4SMatthias Ringwald 		self->hal_flash_bank_impl->erase(self->hal_flash_bank_context, bank);
24617ae5bc4SMatthias Ringwald 	}
24717ae5bc4SMatthias Ringwald }
24817ae5bc4SMatthias Ringwald 
24917ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){
25017ae5bc4SMatthias Ringwald 
25117ae5bc4SMatthias Ringwald 	int next_bank = 1 - self->current_bank;
25217ae5bc4SMatthias Ringwald 	log_info("migrate bank %u -> bank %u", self->current_bank, next_bank);
25317ae5bc4SMatthias Ringwald 	// erase bank (if needed)
25417ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_erase_bank(self, next_bank);
25517ae5bc4SMatthias Ringwald 	int next_write_pos = 8;
25617ae5bc4SMatthias Ringwald 
25717ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
25817ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
25917ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
26017ae5bc4SMatthias Ringwald 		// skip deleted entries
26117ae5bc4SMatthias Ringwald 		if (it.tag) {
26217ae5bc4SMatthias Ringwald 			uint32_t tag_len = it.len;
26317ae5bc4SMatthias Ringwald 			uint32_t tag_index = it.offset;
26417ae5bc4SMatthias Ringwald 
265052e5e42SMatthias Ringwald             bool tag_valid = true;
266052e5e42SMatthias Ringwald 
267052e5e42SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_WRITE_ONCE
268052e5e42SMatthias Ringwald             // search until end for newer entry of same tag
269052e5e42SMatthias Ringwald             tlv_iterator_t it2;
270052e5e42SMatthias Ringwald             memcpy(&it2, &it, sizeof(tlv_iterator_t));
271052e5e42SMatthias Ringwald             while (btstack_tlv_flash_bank_iterator_has_next(self, &it2)){
272052e5e42SMatthias Ringwald                 if ((it2.offset != it.offset) && (it2.tag == it.tag)){
273052e5e42SMatthias Ringwald                     tag_valid = false;
274052e5e42SMatthias Ringwald                     break;
275052e5e42SMatthias Ringwald                 }
276052e5e42SMatthias Ringwald                 tlv_iterator_fetch_next(self, &it2);
277052e5e42SMatthias Ringwald             }
278052e5e42SMatthias Ringwald             if (tag_valid == false){
279052e5e42SMatthias Ringwald 			    log_info("skip pos %u, tag '%x' as newer entry found at %u", (unsigned int) tag_index, (unsigned int) it.tag,
280052e5e42SMatthias Ringwald                     (unsigned int) it2.offset);
281052e5e42SMatthias Ringwald             }
282052e5e42SMatthias Ringwald #endif
283052e5e42SMatthias Ringwald 
284052e5e42SMatthias Ringwald             if (tag_valid) {
285052e5e42SMatthias Ringwald 
28697d2cfbcSMatthias Ringwald                 log_info("migrate pos %u, tag '%x' len %u -> new pos %u",
28797d2cfbcSMatthias Ringwald                          (unsigned int) tag_index, (unsigned int) it.tag, (unsigned int) tag_len, next_write_pos);
2880d5520a9SMatthias Ringwald 
2890d5520a9SMatthias Ringwald                 // copy header
2900d5520a9SMatthias Ringwald                 uint8_t header_buffer[8];
2910d5520a9SMatthias Ringwald                 btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, header_buffer, 8);
2920d5520a9SMatthias Ringwald                 btstack_tlv_flash_bank_write(self, next_bank, next_write_pos, header_buffer, 8);
2930d5520a9SMatthias Ringwald                 tag_index += 8;
2940d5520a9SMatthias Ringwald                 next_write_pos += 8;
2950d5520a9SMatthias Ringwald 
2960d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
2970d5520a9SMatthias Ringwald                 // skip delete field
2985ff6a55bSMatthias Ringwald                 tag_index      += self->delete_tag_len;
2995ff6a55bSMatthias Ringwald                 next_write_pos += self->delete_tag_len;
3000d5520a9SMatthias Ringwald #endif
3010d5520a9SMatthias Ringwald                 // copy value
3020d5520a9SMatthias Ringwald                 int bytes_to_copy = tag_len;
30317ae5bc4SMatthias Ringwald                 uint8_t copy_buffer[32];
30417ae5bc4SMatthias Ringwald                 while (bytes_to_copy) {
30517ae5bc4SMatthias Ringwald                     int bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer));
306e8efffc8SMatthias Ringwald                     btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
307e8efffc8SMatthias Ringwald                     btstack_tlv_flash_bank_write(self, next_bank, next_write_pos, copy_buffer, bytes_this_iteration);
30817ae5bc4SMatthias Ringwald                     tag_index += bytes_this_iteration;
30917ae5bc4SMatthias Ringwald                     next_write_pos += bytes_this_iteration;
31017ae5bc4SMatthias Ringwald                     bytes_to_copy -= bytes_this_iteration;
31117ae5bc4SMatthias Ringwald                 }
31217ae5bc4SMatthias Ringwald             }
313052e5e42SMatthias Ringwald 		}
31417ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
31517ae5bc4SMatthias Ringwald 	}
31617ae5bc4SMatthias Ringwald 
31717ae5bc4SMatthias Ringwald 	// prepare new one
31817ae5bc4SMatthias Ringwald 	uint8_t epoch_buffer;
319e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1);
32017ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3);
32117ae5bc4SMatthias Ringwald 	self->current_bank = next_bank;
32217ae5bc4SMatthias Ringwald 	self->write_offset = next_write_pos;
32317ae5bc4SMatthias Ringwald }
32417ae5bc4SMatthias Ringwald 
325052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
32617ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self, uint32_t tag, uint32_t offset){
32717ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
32817ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
32917ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it) && it.offset < offset){
33017ae5bc4SMatthias Ringwald 		if (it.tag == tag){
33197d2cfbcSMatthias Ringwald 			log_info("Erase tag '%x' at position %u", (unsigned int) tag, (unsigned int) it.offset);
3320d5520a9SMatthias Ringwald 
3330d5520a9SMatthias Ringwald 			// mark entry as invalid
3340d5520a9SMatthias Ringwald 			uint32_t zero_value = 0;
3350d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
3360d5520a9SMatthias Ringwald 			// write delete field at offset 8
3370d5520a9SMatthias Ringwald 			btstack_tlv_flash_bank_write(self, self->current_bank, it.offset+8, (uint8_t*) &zero_value, sizeof(zero_value));
3380d5520a9SMatthias Ringwald #else
3390d5520a9SMatthias Ringwald 			// overwrite tag with zero value
3400d5520a9SMatthias Ringwald 			btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_value, sizeof(zero_value));
3410d5520a9SMatthias Ringwald #endif
3420d5520a9SMatthias Ringwald 
34317ae5bc4SMatthias Ringwald 		}
34417ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
34517ae5bc4SMatthias Ringwald 	}
34617ae5bc4SMatthias Ringwald }
347052e5e42SMatthias Ringwald #endif
34817ae5bc4SMatthias Ringwald 
34917ae5bc4SMatthias Ringwald /**
35017ae5bc4SMatthias Ringwald  * Get Value for Tag
35117ae5bc4SMatthias Ringwald  * @param tag
35217ae5bc4SMatthias Ringwald  * @param buffer
35317ae5bc4SMatthias Ringwald  * @param buffer_size
35417ae5bc4SMatthias Ringwald  * @returns size of value
35517ae5bc4SMatthias Ringwald  */
35617ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
35717ae5bc4SMatthias Ringwald 
35817ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
35917ae5bc4SMatthias Ringwald 
36017ae5bc4SMatthias Ringwald 	uint32_t tag_index = 0;
36117ae5bc4SMatthias Ringwald 	uint32_t tag_len   = 0;
36217ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
36317ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
36417ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
36517ae5bc4SMatthias Ringwald 		if (it.tag == tag){
36697d2cfbcSMatthias Ringwald 			log_info("Found tag '%x' at position %u", (unsigned int) tag, (unsigned int) it.offset);
36717ae5bc4SMatthias Ringwald 			tag_index = it.offset;
36817ae5bc4SMatthias Ringwald 			tag_len   = it.len;
369052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
37017ae5bc4SMatthias Ringwald 			break;
371052e5e42SMatthias Ringwald #endif
37217ae5bc4SMatthias Ringwald 		}
37317ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
37417ae5bc4SMatthias Ringwald 	}
37517ae5bc4SMatthias Ringwald 	if (tag_index == 0) return 0;
37617ae5bc4SMatthias Ringwald 	if (!buffer) return tag_len;
37717ae5bc4SMatthias Ringwald 	int copy_size = btstack_min(buffer_size, tag_len);
3780d5520a9SMatthias Ringwald 	uint32_t value_offset = tag_index + 8;
3790d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
3800d5520a9SMatthias Ringwald 	// skip delete field
3815ff6a55bSMatthias Ringwald 	value_offset += self->delete_tag_len;
3820d5520a9SMatthias Ringwald #endif
3830d5520a9SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, self->current_bank, value_offset, buffer, copy_size);
38417ae5bc4SMatthias Ringwald 	return copy_size;
38517ae5bc4SMatthias Ringwald }
38617ae5bc4SMatthias Ringwald 
38717ae5bc4SMatthias Ringwald /**
38817ae5bc4SMatthias Ringwald  * Store Tag
38917ae5bc4SMatthias Ringwald  * @param tag
39017ae5bc4SMatthias Ringwald  * @param data
39117ae5bc4SMatthias Ringwald  * @param data_size
39217ae5bc4SMatthias Ringwald  */
39317ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
39417ae5bc4SMatthias Ringwald 
39517ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
39617ae5bc4SMatthias Ringwald 
39717ae5bc4SMatthias Ringwald 	// trigger migration if not enough space
3985ff6a55bSMatthias Ringwald 	uint32_t required_space = 8 + self->delete_tag_len + data_size;
3995ff6a55bSMatthias Ringwald 	if (self->write_offset + required_space > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
40017ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_migrate(self);
40117ae5bc4SMatthias Ringwald 	}
40217ae5bc4SMatthias Ringwald 
4035ff6a55bSMatthias Ringwald 	if (self->write_offset + required_space > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
40417ae5bc4SMatthias Ringwald 		log_error("couldn't write entry, not enough space left");
40517ae5bc4SMatthias Ringwald 		return 2;
40617ae5bc4SMatthias Ringwald 	}
40717ae5bc4SMatthias Ringwald 
40817ae5bc4SMatthias Ringwald 	// prepare entry
40917ae5bc4SMatthias Ringwald 	uint8_t entry[8];
41017ae5bc4SMatthias Ringwald 	big_endian_store_32(entry, 0, tag);
41117ae5bc4SMatthias Ringwald 	big_endian_store_32(entry, 4, data_size);
41217ae5bc4SMatthias Ringwald 
41379b7220dSMatthias Ringwald 	log_info("write '%" PRIx32 "', len %" PRIu32 " at %" PRIx32, tag, data_size, self->write_offset);
41417ae5bc4SMatthias Ringwald 
4150d5520a9SMatthias Ringwald 	uint32_t value_offset = self->write_offset + 8;
4160d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
4170d5520a9SMatthias Ringwald 	// skip delete field
4185ff6a55bSMatthias Ringwald 	value_offset += self->delete_tag_len;
4190d5520a9SMatthias Ringwald #endif
4200d5520a9SMatthias Ringwald 
42117ae5bc4SMatthias Ringwald 	// write value first
4220d5520a9SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, self->current_bank, value_offset, data, data_size);
42317ae5bc4SMatthias Ringwald 
42417ae5bc4SMatthias Ringwald 	// then entry
425e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset, entry, sizeof(entry));
42617ae5bc4SMatthias Ringwald 
427052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
42817ae5bc4SMatthias Ringwald 	// overwrite old entries (if exists)
42917ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
430052e5e42SMatthias Ringwald #endif
43117ae5bc4SMatthias Ringwald 
43217ae5bc4SMatthias Ringwald 	// done
433372a3556SMatthias Ringwald 	self->write_offset += sizeof(entry) + btstack_tlv_flash_bank_align_size(self, data_size);
43417ae5bc4SMatthias Ringwald 
4350d5520a9SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
4360d5520a9SMatthias Ringwald 	// skip delete field
4375ff6a55bSMatthias Ringwald 	self->write_offset += self->delete_tag_len;
4380d5520a9SMatthias Ringwald #endif
4390d5520a9SMatthias Ringwald 
44017ae5bc4SMatthias Ringwald 	return 0;
44117ae5bc4SMatthias Ringwald }
44217ae5bc4SMatthias Ringwald 
44317ae5bc4SMatthias Ringwald /**
44417ae5bc4SMatthias Ringwald  * Delete Tag
44517ae5bc4SMatthias Ringwald  * @param tag
44617ae5bc4SMatthias Ringwald  */
44717ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag(void * context, uint32_t tag){
448052e5e42SMatthias Ringwald #ifdef ENABLE_TLV_FLASH_WRITE_ONCE
449052e5e42SMatthias Ringwald     btstack_tlv_flash_bank_store_tag(context, tag, NULL, 0);
450052e5e42SMatthias Ringwald #else
45117ae5bc4SMatthias Ringwald     btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
45217ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
453052e5e42SMatthias Ringwald #endif
45417ae5bc4SMatthias Ringwald }
45517ae5bc4SMatthias Ringwald 
45617ae5bc4SMatthias Ringwald static const btstack_tlv_t btstack_tlv_flash_bank = {
45717ae5bc4SMatthias Ringwald 	/* int  (*get_tag)(..);     */ &btstack_tlv_flash_bank_get_tag,
45817ae5bc4SMatthias Ringwald 	/* int (*store_tag)(..);    */ &btstack_tlv_flash_bank_store_tag,
45917ae5bc4SMatthias Ringwald 	/* void (*delete_tag)(v..); */ &btstack_tlv_flash_bank_delete_tag,
46017ae5bc4SMatthias Ringwald };
46117ae5bc4SMatthias Ringwald 
46217ae5bc4SMatthias Ringwald /**
46317ae5bc4SMatthias Ringwald  * Init Tag Length Value Store
46417ae5bc4SMatthias Ringwald  */
46517ae5bc4SMatthias 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){
46617ae5bc4SMatthias Ringwald 
46717ae5bc4SMatthias Ringwald 	self->hal_flash_bank_impl    = hal_flash_bank_impl;
46817ae5bc4SMatthias Ringwald 	self->hal_flash_bank_context = hal_flash_bank_context;
4695ff6a55bSMatthias Ringwald 	self->delete_tag_len = 0;
4705ff6a55bSMatthias Ringwald 
4715ff6a55bSMatthias Ringwald #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
4725ff6a55bSMatthias Ringwald 	if (hal_flash_bank_impl->get_alignment(hal_flash_bank_context) > 8){
4735ff6a55bSMatthias Ringwald 		log_error("Flash alignment > 8 with ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD not supported");
4745ff6a55bSMatthias Ringwald 		return NULL;
4755ff6a55bSMatthias Ringwald 	}
4765ff6a55bSMatthias Ringwald 	// set delete tag len
4775ff6a55bSMatthias Ringwald 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
4785ff6a55bSMatthias Ringwald 	self->delete_tag_len = (uint8_t) btstack_max(4, aligment);
4795ff6a55bSMatthias Ringwald 	log_info("delete tag len %u", self->delete_tag_len);
4805ff6a55bSMatthias Ringwald #endif
48117ae5bc4SMatthias Ringwald 
48217ae5bc4SMatthias Ringwald 	// try to find current bank
48317ae5bc4SMatthias Ringwald 	self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self);
48417ae5bc4SMatthias Ringwald 	log_info("found bank %d", self->current_bank);
48517ae5bc4SMatthias Ringwald 	if (self->current_bank >= 0){
48617ae5bc4SMatthias Ringwald 
48717ae5bc4SMatthias Ringwald 		// find last entry and write offset
48817ae5bc4SMatthias Ringwald 		tlv_iterator_t it;
489052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
49017ae5bc4SMatthias Ringwald 		uint32_t last_tag = 0;
49117ae5bc4SMatthias Ringwald 		uint32_t last_offset = 0;
492052e5e42SMatthias Ringwald #endif
49317ae5bc4SMatthias Ringwald         btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
49417ae5bc4SMatthias Ringwald 		while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
495052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
49617ae5bc4SMatthias Ringwald 			last_tag = it.tag;
49717ae5bc4SMatthias Ringwald 			last_offset = it.offset;
498052e5e42SMatthias Ringwald #endif
49917ae5bc4SMatthias Ringwald 			tlv_iterator_fetch_next(self, &it);
50017ae5bc4SMatthias Ringwald 		}
50117ae5bc4SMatthias Ringwald 		self->write_offset = it.offset;
50217ae5bc4SMatthias Ringwald 
503*e195acfcSMatthias Ringwald 		if (self->write_offset <= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
50417ae5bc4SMatthias Ringwald 
505052e5e42SMatthias Ringwald #ifndef ENABLE_TLV_FLASH_WRITE_ONCE
50617ae5bc4SMatthias Ringwald 			// delete older instances of last_tag
50717ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete
50817ae5bc4SMatthias Ringwald 			if (last_tag){
50917ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset);
51017ae5bc4SMatthias Ringwald 			}
511052e5e42SMatthias Ringwald #endif
51217ae5bc4SMatthias Ringwald 
51317ae5bc4SMatthias Ringwald 			// verify that rest of bank is empty
51417ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value was written, but not the tag
51517ae5bc4SMatthias Ringwald 			if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){
51617ae5bc4SMatthias Ringwald 				log_info("Flash not empty after last found tag -> migrate");
51717ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_migrate(self);
51817ae5bc4SMatthias Ringwald 			} else {
51917ae5bc4SMatthias Ringwald 				log_info("Flash clean after last found tag");
52017ae5bc4SMatthias Ringwald 			}
52117ae5bc4SMatthias Ringwald 		} else {
52217ae5bc4SMatthias Ringwald 			// failure!
52317ae5bc4SMatthias Ringwald 			self->current_bank = -1;
52417ae5bc4SMatthias Ringwald 		}
52517ae5bc4SMatthias Ringwald 	}
52617ae5bc4SMatthias Ringwald 
52717ae5bc4SMatthias Ringwald 	if (self->current_bank < 0) {
52817ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_erase_bank(self, 0);
52917ae5bc4SMatthias Ringwald 		self->current_bank = 0;
53017ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_write_header(self, self->current_bank, 0);	// epoch = 0;
53117ae5bc4SMatthias Ringwald 		self->write_offset = 8;
53217ae5bc4SMatthias Ringwald 	}
53317ae5bc4SMatthias Ringwald 
53479b7220dSMatthias Ringwald 	log_info("write offset %" PRIx32, self->write_offset);
53517ae5bc4SMatthias Ringwald 	return &btstack_tlv_flash_bank;
53617ae5bc4SMatthias Ringwald }
53717ae5bc4SMatthias Ringwald 
538