xref: /btstack/platform/embedded/btstack_tlv_flash_bank.c (revision 372a35564ca2f7b9ab4a619788764314302613a2)
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  *
1717ae5bc4SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD 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
2017ae5bc4SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
2117ae5bc4SMatthias Ringwald  * RINGWALD 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 
3217ae5bc4SMatthias 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>
4117ae5bc4SMatthias Ringwald 
4217ae5bc4SMatthias Ringwald // Header:
4317ae5bc4SMatthias Ringwald // - Magic: 'BTstack'
4417ae5bc4SMatthias Ringwald // - Status:
4517ae5bc4SMatthias Ringwald //   - bits 765432: reserved
4617ae5bc4SMatthias Ringwald //	 - bits 10:     epoch
4717ae5bc4SMatthias Ringwald 
4817ae5bc4SMatthias Ringwald // Entries
4917ae5bc4SMatthias Ringwald // - Tag: 32 bit
5017ae5bc4SMatthias Ringwald // - Len: 32 bit
5117ae5bc4SMatthias Ringwald // - Value: Len in bytes
5217ae5bc4SMatthias Ringwald 
5317ae5bc4SMatthias Ringwald #define BTSTACK_TLV_HEADER_LEN 8
54e8efffc8SMatthias Ringwald 
55*372a3556SMatthias Ringwald #ifndef BTSTACK_FLASH_ALIGNMENT_MAX
56*372a3556SMatthias Ringwald #define BTSTACK_FLASH_ALIGNMENT_MAX 8
57*372a3556SMatthias Ringwald #endif
58e8efffc8SMatthias Ringwald 
5917ae5bc4SMatthias Ringwald static const char * btstack_tlv_header_magic = "BTstack";
6017ae5bc4SMatthias Ringwald 
6117ae5bc4SMatthias Ringwald // TLV Iterator
6217ae5bc4SMatthias Ringwald typedef struct {
6317ae5bc4SMatthias Ringwald 	int 	 bank;
6417ae5bc4SMatthias Ringwald 	uint32_t offset;
6517ae5bc4SMatthias Ringwald 	uint32_t tag;
6617ae5bc4SMatthias Ringwald 	uint32_t len;
6717ae5bc4SMatthias Ringwald } tlv_iterator_t;
6817ae5bc4SMatthias Ringwald 
69*372a3556SMatthias Ringwald static uint32_t btstack_tlv_flash_bank_align_size(btstack_tlv_flash_bank_t * self, uint32_t size){
70*372a3556SMatthias Ringwald 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
71*372a3556SMatthias Ringwald 	return (size + aligment - 1) & ~(aligment - 1);
72*372a3556SMatthias Ringwald }
73*372a3556SMatthias Ringwald 
74*372a3556SMatthias Ringwald // support unaligned flash read/writes
75*372a3556SMatthias Ringwald // strategy: increase size to meet alignment, perform unaligned read/write of last chunk with helper buffer
76*372a3556SMatthias Ringwald 
77e8efffc8SMatthias 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){
78*372a3556SMatthias Ringwald 
79*372a3556SMatthias Ringwald 	// read main data
80*372a3556SMatthias Ringwald 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
81*372a3556SMatthias Ringwald 	uint32_t lower_bits = size & (aligment - 1);
82*372a3556SMatthias Ringwald 	uint32_t size_aligned = size - lower_bits;
83*372a3556SMatthias Ringwald 	if (size_aligned){
84*372a3556SMatthias Ringwald 		self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, size_aligned);
85*372a3556SMatthias Ringwald 		buffer += size_aligned;
86*372a3556SMatthias Ringwald 		offset += size_aligned;
87*372a3556SMatthias Ringwald 		size   -= size_aligned;
88*372a3556SMatthias Ringwald 	}
89*372a3556SMatthias Ringwald 
90*372a3556SMatthias Ringwald 	// read last part
91*372a3556SMatthias Ringwald 	if (size == 0) return;
92*372a3556SMatthias Ringwald 	uint8_t aligment_block[BTSTACK_FLASH_ALIGNMENT_MAX];
93*372a3556SMatthias Ringwald 	self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, aligment_block, aligment);
94*372a3556SMatthias Ringwald 	uint32_t bytes_to_copy = btstack_min(aligment - lower_bits, size);
95*372a3556SMatthias Ringwald 	memcpy(buffer, aligment_block, bytes_to_copy);
96e8efffc8SMatthias Ringwald }
97e8efffc8SMatthias Ringwald 
98e8efffc8SMatthias 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){
99*372a3556SMatthias Ringwald 
100*372a3556SMatthias Ringwald 	// write main data
101*372a3556SMatthias Ringwald 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
102*372a3556SMatthias Ringwald 	uint32_t lower_bits = size & (aligment - 1);
103*372a3556SMatthias Ringwald 	uint32_t size_aligned = size - lower_bits;
104*372a3556SMatthias Ringwald 	if (size_aligned){
105*372a3556SMatthias Ringwald 		self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, buffer, size_aligned);
106*372a3556SMatthias Ringwald 		buffer += size_aligned;
107*372a3556SMatthias Ringwald 		offset += size_aligned;
108*372a3556SMatthias Ringwald 		size   -= size_aligned;
109e8efffc8SMatthias Ringwald 	}
110e8efffc8SMatthias Ringwald 
111*372a3556SMatthias Ringwald 	// write last part
112*372a3556SMatthias Ringwald 	if (size == 0) return;
113*372a3556SMatthias Ringwald 	uint8_t aligment_block[BTSTACK_FLASH_ALIGNMENT_MAX];
114*372a3556SMatthias Ringwald 	memset(aligment_block, 0xff, aligment);
115*372a3556SMatthias Ringwald 	memcpy(aligment_block, buffer, lower_bits);
116*372a3556SMatthias Ringwald 	self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, aligment_block, aligment);
117*372a3556SMatthias Ringwald }
118*372a3556SMatthias Ringwald 
119e8efffc8SMatthias Ringwald 
120e8efffc8SMatthias Ringwald // iterator
121e8efffc8SMatthias Ringwald 
12217ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
12317ae5bc4SMatthias Ringwald 	uint8_t entry[8];
124e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, it->bank, it->offset, entry, 8);
12517ae5bc4SMatthias Ringwald 	it->tag = big_endian_read_32(entry, 0);
12617ae5bc4SMatthias Ringwald 	it->len = big_endian_read_32(entry, 4);
12717ae5bc4SMatthias Ringwald }
12817ae5bc4SMatthias Ringwald 
12917ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it, int bank){
13017ae5bc4SMatthias Ringwald 	memset(it, 0, sizeof(tlv_iterator_t));
13117ae5bc4SMatthias Ringwald 	it->bank = bank;
13217ae5bc4SMatthias Ringwald 	it->offset = BTSTACK_TLV_HEADER_LEN;
13317ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
13417ae5bc4SMatthias Ringwald }
13517ae5bc4SMatthias Ringwald 
13617ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
13717ae5bc4SMatthias Ringwald 	if (it->tag == 0xffffffff) return 0;
13817ae5bc4SMatthias Ringwald 	return 1;
13917ae5bc4SMatthias Ringwald }
14017ae5bc4SMatthias Ringwald 
14117ae5bc4SMatthias Ringwald static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
142*372a3556SMatthias Ringwald 	it->offset += 8 + btstack_tlv_flash_bank_align_size(self, it->len);
14317ae5bc4SMatthias Ringwald 	if (it->offset >= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)) {
14417ae5bc4SMatthias Ringwald 		it->tag = 0xffffffff;
14517ae5bc4SMatthias Ringwald 		it->len = 0;
14617ae5bc4SMatthias Ringwald 		return;
14717ae5bc4SMatthias Ringwald 	}
14817ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
14917ae5bc4SMatthias Ringwald }
15017ae5bc4SMatthias Ringwald 
15117ae5bc4SMatthias Ringwald //
15217ae5bc4SMatthias Ringwald 
15317ae5bc4SMatthias Ringwald // check both banks for headers and pick the one with the higher epoch % 4
15417ae5bc4SMatthias Ringwald // @returns bank or -1 if something is invalid
15517ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){
15617ae5bc4SMatthias Ringwald  	uint8_t header0[BTSTACK_TLV_HEADER_LEN];
15717ae5bc4SMatthias Ringwald  	uint8_t header1[BTSTACK_TLV_HEADER_LEN];
158e8efffc8SMatthias Ringwald  	btstack_tlv_flash_bank_read(self, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN);
159e8efffc8SMatthias Ringwald  	btstack_tlv_flash_bank_read(self, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN);
16017ae5bc4SMatthias Ringwald  	int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
16117ae5bc4SMatthias Ringwald  	int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
16217ae5bc4SMatthias Ringwald 	if (!valid0 && !valid1) return -1;
16317ae5bc4SMatthias Ringwald 	if ( valid0 && !valid1) return 0;
16417ae5bc4SMatthias Ringwald 	if (!valid0 &&  valid1) return 1;
16517ae5bc4SMatthias Ringwald 	int epoch0 = header0[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
16617ae5bc4SMatthias Ringwald 	int epoch1 = header1[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
16717ae5bc4SMatthias Ringwald 	if (epoch0 == ((epoch1 + 1) & 0x03)) return 0;
16817ae5bc4SMatthias Ringwald 	if (epoch1 == ((epoch0 + 1) & 0x03)) return 1;
16917ae5bc4SMatthias Ringwald 	return -1;	// invalid, must not happen
17017ae5bc4SMatthias Ringwald }
17117ae5bc4SMatthias Ringwald 
17217ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self, int bank, int epoch){
17317ae5bc4SMatthias Ringwald 	uint8_t header[BTSTACK_TLV_HEADER_LEN];
17417ae5bc4SMatthias Ringwald 	memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1);
17517ae5bc4SMatthias Ringwald 	header[BTSTACK_TLV_HEADER_LEN-1] = epoch;
176e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, bank, 0, header, BTSTACK_TLV_HEADER_LEN);
17717ae5bc4SMatthias Ringwald }
17817ae5bc4SMatthias Ringwald 
17917ae5bc4SMatthias Ringwald /**
18017ae5bc4SMatthias Ringwald  * @brief Check if erased from offset
18117ae5bc4SMatthias Ringwald  */
18217ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset){
18317ae5bc4SMatthias Ringwald 	log_info("test erased: bank %u, offset %u", bank, offset);
18417ae5bc4SMatthias Ringwald 	uint32_t size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context);
18517ae5bc4SMatthias Ringwald 	uint8_t buffer[16];
18617ae5bc4SMatthias Ringwald 	uint8_t empty16[16];
18717ae5bc4SMatthias Ringwald 	memset(empty16, 0xff, sizeof(empty16));
18817ae5bc4SMatthias Ringwald 	while (offset < size){
18917ae5bc4SMatthias Ringwald 		uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset);
190e8efffc8SMatthias Ringwald 		btstack_tlv_flash_bank_read(self, bank, offset, buffer, copy_size);
19117ae5bc4SMatthias Ringwald 		if (memcmp(buffer, empty16, copy_size)) {
19217ae5bc4SMatthias Ringwald 			log_info("not erased %x - %x", offset, offset + copy_size);
19317ae5bc4SMatthias Ringwald 			return 0;
19417ae5bc4SMatthias Ringwald 		}
19517ae5bc4SMatthias Ringwald 		offset += copy_size;
19617ae5bc4SMatthias Ringwald 	}
19717ae5bc4SMatthias Ringwald 	return 1;
19817ae5bc4SMatthias Ringwald }
19917ae5bc4SMatthias Ringwald 
20017ae5bc4SMatthias Ringwald /**
20117ae5bc4SMatthias Ringwald  * @brief erase bank (only if not already erased)
20217ae5bc4SMatthias Ringwald  */
20317ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self, int bank){
20417ae5bc4SMatthias Ringwald 	if (btstack_tlv_flash_bank_test_erased(self, bank, 0)){
20517ae5bc4SMatthias Ringwald 		log_info("bank %u already erased", bank);
20617ae5bc4SMatthias Ringwald 	} else {
20717ae5bc4SMatthias Ringwald 		log_info("bank %u not empty, erase bank", bank);
20817ae5bc4SMatthias Ringwald 		self->hal_flash_bank_impl->erase(self->hal_flash_bank_context, bank);
20917ae5bc4SMatthias Ringwald 	}
21017ae5bc4SMatthias Ringwald }
21117ae5bc4SMatthias Ringwald 
21217ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){
21317ae5bc4SMatthias Ringwald 
21417ae5bc4SMatthias Ringwald 	int next_bank = 1 - self->current_bank;
21517ae5bc4SMatthias Ringwald 	log_info("migrate bank %u -> bank %u", self->current_bank, next_bank);
21617ae5bc4SMatthias Ringwald 	// erase bank (if needed)
21717ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_erase_bank(self, next_bank);
21817ae5bc4SMatthias Ringwald 	int next_write_pos = 8;
21917ae5bc4SMatthias Ringwald 
22017ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
22117ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
22217ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
22317ae5bc4SMatthias Ringwald 		// skip deleted entries
22417ae5bc4SMatthias Ringwald 		if (it.tag) {
22517ae5bc4SMatthias Ringwald 			uint32_t tag_len = it.len;
22617ae5bc4SMatthias Ringwald 			uint32_t tag_index = it.offset;
22717ae5bc4SMatthias Ringwald 
22817ae5bc4SMatthias Ringwald 			// copy
22917ae5bc4SMatthias Ringwald 			int bytes_to_copy = 8 + tag_len;
23017ae5bc4SMatthias Ringwald 			log_info("migrate pos %u, tag '%x' len %u -> new pos %u", tag_index, it.tag, bytes_to_copy, next_write_pos);
23117ae5bc4SMatthias Ringwald 			uint8_t copy_buffer[32];
23217ae5bc4SMatthias Ringwald 			while (bytes_to_copy){
23317ae5bc4SMatthias Ringwald 				int bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer));
234e8efffc8SMatthias Ringwald 				btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
235e8efffc8SMatthias Ringwald 				btstack_tlv_flash_bank_write(self, next_bank, next_write_pos, copy_buffer, bytes_this_iteration);
23617ae5bc4SMatthias Ringwald 				tag_index      += bytes_this_iteration;
23717ae5bc4SMatthias Ringwald 				next_write_pos += bytes_this_iteration;
23817ae5bc4SMatthias Ringwald 				bytes_to_copy  -= bytes_this_iteration;
23917ae5bc4SMatthias Ringwald 			}
24017ae5bc4SMatthias Ringwald 		}
24117ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
24217ae5bc4SMatthias Ringwald 	}
24317ae5bc4SMatthias Ringwald 
24417ae5bc4SMatthias Ringwald 	// prepare new one
24517ae5bc4SMatthias Ringwald 	uint8_t epoch_buffer;
246e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1);
24717ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3);
24817ae5bc4SMatthias Ringwald 	self->current_bank = next_bank;
24917ae5bc4SMatthias Ringwald 	self->write_offset = next_write_pos;
25017ae5bc4SMatthias Ringwald }
25117ae5bc4SMatthias Ringwald 
25217ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self, uint32_t tag, uint32_t offset){
25317ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
25417ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
25517ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it) && it.offset < offset){
25617ae5bc4SMatthias Ringwald 		if (it.tag == tag){
25717ae5bc4SMatthias Ringwald 			log_info("Erase tag '%x' at position %u", tag, it.offset);
25817ae5bc4SMatthias Ringwald 			// overwrite tag with invalid tag
25917ae5bc4SMatthias Ringwald 			uint32_t zero_tag = 0;
260e8efffc8SMatthias Ringwald 			btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_tag, sizeof(zero_tag));
26117ae5bc4SMatthias Ringwald 		}
26217ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
26317ae5bc4SMatthias Ringwald 	}
26417ae5bc4SMatthias Ringwald }
26517ae5bc4SMatthias Ringwald 
26617ae5bc4SMatthias Ringwald /**
26717ae5bc4SMatthias Ringwald  * Get Value for Tag
26817ae5bc4SMatthias Ringwald  * @param tag
26917ae5bc4SMatthias Ringwald  * @param buffer
27017ae5bc4SMatthias Ringwald  * @param buffer_size
27117ae5bc4SMatthias Ringwald  * @returns size of value
27217ae5bc4SMatthias Ringwald  */
27317ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
27417ae5bc4SMatthias Ringwald 
27517ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
27617ae5bc4SMatthias Ringwald 
27717ae5bc4SMatthias Ringwald 	uint32_t tag_index = 0;
27817ae5bc4SMatthias Ringwald 	uint32_t tag_len   = 0;
27917ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
28017ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
28117ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
28217ae5bc4SMatthias Ringwald 		if (it.tag == tag){
28317ae5bc4SMatthias Ringwald 			log_info("Found tag '%x' at position %u", tag, it.offset);
28417ae5bc4SMatthias Ringwald 			tag_index = it.offset;
28517ae5bc4SMatthias Ringwald 			tag_len   = it.len;
28617ae5bc4SMatthias Ringwald 			break;
28717ae5bc4SMatthias Ringwald 		}
28817ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
28917ae5bc4SMatthias Ringwald 	}
29017ae5bc4SMatthias Ringwald 	if (tag_index == 0) return 0;
29117ae5bc4SMatthias Ringwald 	if (!buffer) return tag_len;
29217ae5bc4SMatthias Ringwald 	int copy_size = btstack_min(buffer_size, tag_len);
293e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, self->current_bank, tag_index + 8, buffer, copy_size);
29417ae5bc4SMatthias Ringwald 	return copy_size;
29517ae5bc4SMatthias Ringwald }
29617ae5bc4SMatthias Ringwald 
29717ae5bc4SMatthias Ringwald /**
29817ae5bc4SMatthias Ringwald  * Store Tag
29917ae5bc4SMatthias Ringwald  * @param tag
30017ae5bc4SMatthias Ringwald  * @param data
30117ae5bc4SMatthias Ringwald  * @param data_size
30217ae5bc4SMatthias Ringwald  */
30317ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
30417ae5bc4SMatthias Ringwald 
30517ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
30617ae5bc4SMatthias Ringwald 
30717ae5bc4SMatthias Ringwald 	// trigger migration if not enough space
30817ae5bc4SMatthias Ringwald 	if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
30917ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_migrate(self);
31017ae5bc4SMatthias Ringwald 	}
31117ae5bc4SMatthias Ringwald 
31217ae5bc4SMatthias Ringwald 	if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
31317ae5bc4SMatthias Ringwald 		log_error("couldn't write entry, not enough space left");
31417ae5bc4SMatthias Ringwald 		return 2;
31517ae5bc4SMatthias Ringwald 	}
31617ae5bc4SMatthias Ringwald 
31717ae5bc4SMatthias Ringwald 	// prepare entry
31817ae5bc4SMatthias Ringwald 	uint8_t entry[8];
31917ae5bc4SMatthias Ringwald 	big_endian_store_32(entry, 0, tag);
32017ae5bc4SMatthias Ringwald 	big_endian_store_32(entry, 4, data_size);
32117ae5bc4SMatthias Ringwald 
32217ae5bc4SMatthias Ringwald 	log_info("write '%x', len %u at %u", tag, data_size, self->write_offset);
32317ae5bc4SMatthias Ringwald 
32417ae5bc4SMatthias Ringwald 	// write value first
325e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset + 8, data, data_size);
32617ae5bc4SMatthias Ringwald 
32717ae5bc4SMatthias Ringwald 	// then entry
328e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset, entry, sizeof(entry));
32917ae5bc4SMatthias Ringwald 
33017ae5bc4SMatthias Ringwald 	// overwrite old entries (if exists)
33117ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
33217ae5bc4SMatthias Ringwald 
33317ae5bc4SMatthias Ringwald 	// done
334*372a3556SMatthias Ringwald 	self->write_offset += sizeof(entry) + btstack_tlv_flash_bank_align_size(self, data_size);
33517ae5bc4SMatthias Ringwald 
33617ae5bc4SMatthias Ringwald 	return 0;
33717ae5bc4SMatthias Ringwald }
33817ae5bc4SMatthias Ringwald 
33917ae5bc4SMatthias Ringwald /**
34017ae5bc4SMatthias Ringwald  * Delete Tag
34117ae5bc4SMatthias Ringwald  * @param tag
34217ae5bc4SMatthias Ringwald  */
34317ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag(void * context, uint32_t tag){
34417ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
34517ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
34617ae5bc4SMatthias Ringwald }
34717ae5bc4SMatthias Ringwald 
34817ae5bc4SMatthias Ringwald static const btstack_tlv_t btstack_tlv_flash_bank = {
34917ae5bc4SMatthias Ringwald 	/* int  (*get_tag)(..);     */ &btstack_tlv_flash_bank_get_tag,
35017ae5bc4SMatthias Ringwald 	/* int (*store_tag)(..);    */ &btstack_tlv_flash_bank_store_tag,
35117ae5bc4SMatthias Ringwald 	/* void (*delete_tag)(v..); */ &btstack_tlv_flash_bank_delete_tag,
35217ae5bc4SMatthias Ringwald };
35317ae5bc4SMatthias Ringwald 
35417ae5bc4SMatthias Ringwald /**
35517ae5bc4SMatthias Ringwald  * Init Tag Length Value Store
35617ae5bc4SMatthias Ringwald  */
35717ae5bc4SMatthias 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){
35817ae5bc4SMatthias Ringwald 
35917ae5bc4SMatthias Ringwald 	self->hal_flash_bank_impl    = hal_flash_bank_impl;
36017ae5bc4SMatthias Ringwald 	self->hal_flash_bank_context = hal_flash_bank_context;
36117ae5bc4SMatthias Ringwald 
36217ae5bc4SMatthias Ringwald 	// try to find current bank
36317ae5bc4SMatthias Ringwald 	self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self);
36417ae5bc4SMatthias Ringwald 	log_info("found bank %d", self->current_bank);
36517ae5bc4SMatthias Ringwald 	if (self->current_bank >= 0){
36617ae5bc4SMatthias Ringwald 
36717ae5bc4SMatthias Ringwald 		// find last entry and write offset
36817ae5bc4SMatthias Ringwald 		tlv_iterator_t it;
36917ae5bc4SMatthias Ringwald 		uint32_t last_tag = 0;
37017ae5bc4SMatthias Ringwald 		uint32_t last_offset = 0;
37117ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
37217ae5bc4SMatthias Ringwald 		while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
37317ae5bc4SMatthias Ringwald 			last_tag = it.tag;
37417ae5bc4SMatthias Ringwald 			last_offset = it.offset;
37517ae5bc4SMatthias Ringwald 			tlv_iterator_fetch_next(self, &it);
37617ae5bc4SMatthias Ringwald 		}
37717ae5bc4SMatthias Ringwald 		self->write_offset = it.offset;
37817ae5bc4SMatthias Ringwald 
37917ae5bc4SMatthias Ringwald 		if (self->write_offset < self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
38017ae5bc4SMatthias Ringwald 
38117ae5bc4SMatthias Ringwald 			// delete older instances of last_tag
38217ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete
38317ae5bc4SMatthias Ringwald 			if (last_tag){
38417ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset);
38517ae5bc4SMatthias Ringwald 			}
38617ae5bc4SMatthias Ringwald 
38717ae5bc4SMatthias Ringwald 			// verify that rest of bank is empty
38817ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value was written, but not the tag
38917ae5bc4SMatthias Ringwald 			if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){
39017ae5bc4SMatthias Ringwald 				log_info("Flash not empty after last found tag -> migrate");
39117ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_migrate(self);
39217ae5bc4SMatthias Ringwald 			} else {
39317ae5bc4SMatthias Ringwald 				log_info("Flash clean after last found tag");
39417ae5bc4SMatthias Ringwald 			}
39517ae5bc4SMatthias Ringwald 		} else {
39617ae5bc4SMatthias Ringwald 			// failure!
39717ae5bc4SMatthias Ringwald 			self->current_bank = -1;
39817ae5bc4SMatthias Ringwald 		}
39917ae5bc4SMatthias Ringwald 	}
40017ae5bc4SMatthias Ringwald 
40117ae5bc4SMatthias Ringwald 	if (self->current_bank < 0) {
40217ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_erase_bank(self, 0);
40317ae5bc4SMatthias Ringwald 		self->current_bank = 0;
40417ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_write_header(self, self->current_bank, 0);	// epoch = 0;
40517ae5bc4SMatthias Ringwald 		self->write_offset = 8;
40617ae5bc4SMatthias Ringwald 	}
40717ae5bc4SMatthias Ringwald 
40817ae5bc4SMatthias Ringwald 	log_info("write offset %u", self->write_offset);
40917ae5bc4SMatthias Ringwald 	return &btstack_tlv_flash_bank;
41017ae5bc4SMatthias Ringwald }
41117ae5bc4SMatthias Ringwald 
412