xref: /btstack/platform/embedded/btstack_tlv_flash_bank.c (revision e8efffc8d1878464c1ad464324098a8887d81639)
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
54*e8efffc8SMatthias Ringwald 
55*e8efffc8SMatthias Ringwald // #ifdef BTSTACK_FLASH_ALIGNMENT_MAX
56*e8efffc8SMatthias Ringwald // #define BTSTACK_FLASH_ALIGNMENT 16
57*e8efffc8SMatthias Ringwald // #endif
58*e8efffc8SMatthias Ringwald 
5917ae5bc4SMatthias Ringwald static const char * btstack_tlv_header_magic = "BTstack";
6017ae5bc4SMatthias Ringwald 
6117ae5bc4SMatthias Ringwald // TLV Iterator
6217ae5bc4SMatthias Ringwald 
6317ae5bc4SMatthias Ringwald typedef struct {
6417ae5bc4SMatthias Ringwald 	int 	 bank;
6517ae5bc4SMatthias Ringwald 	uint32_t offset;
6617ae5bc4SMatthias Ringwald 	uint32_t tag;
6717ae5bc4SMatthias Ringwald 	uint32_t len;
6817ae5bc4SMatthias Ringwald } tlv_iterator_t;
6917ae5bc4SMatthias Ringwald 
70*e8efffc8SMatthias Ringwald // support unaligned flash read/write
71*e8efffc8SMatthias 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){
72*e8efffc8SMatthias Ringwald  	self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, size);
73*e8efffc8SMatthias Ringwald }
74*e8efffc8SMatthias Ringwald 
75*e8efffc8SMatthias 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){
76*e8efffc8SMatthias Ringwald 	self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, buffer, size);
77*e8efffc8SMatthias Ringwald }
78*e8efffc8SMatthias Ringwald 
79*e8efffc8SMatthias Ringwald // static uint32_t btstack_tlv_flash_bank_align_size(btstack_tlv_flash_bank * self, uint32_t size){
80*e8efffc8SMatthias Ringwald // 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
81*e8efffc8SMatthias Ringwald // 	return (size + aligment-1) & ~(alignement - 1);
82*e8efffc8SMatthias Ringwald // }
83*e8efffc8SMatthias Ringwald 
84*e8efffc8SMatthias Ringwald // iterator
85*e8efffc8SMatthias Ringwald 
8617ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
8717ae5bc4SMatthias Ringwald 	uint8_t entry[8];
88*e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, it->bank, it->offset, entry, 8);
8917ae5bc4SMatthias Ringwald 	it->tag = big_endian_read_32(entry, 0);
9017ae5bc4SMatthias Ringwald 	it->len = big_endian_read_32(entry, 4);
9117ae5bc4SMatthias Ringwald }
9217ae5bc4SMatthias Ringwald 
9317ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it, int bank){
9417ae5bc4SMatthias Ringwald 	memset(it, 0, sizeof(tlv_iterator_t));
9517ae5bc4SMatthias Ringwald 	it->bank = bank;
9617ae5bc4SMatthias Ringwald 	it->offset = BTSTACK_TLV_HEADER_LEN;
9717ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
9817ae5bc4SMatthias Ringwald }
9917ae5bc4SMatthias Ringwald 
10017ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
10117ae5bc4SMatthias Ringwald 	if (it->tag == 0xffffffff) return 0;
10217ae5bc4SMatthias Ringwald 	return 1;
10317ae5bc4SMatthias Ringwald }
10417ae5bc4SMatthias Ringwald 
10517ae5bc4SMatthias Ringwald static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
10617ae5bc4SMatthias Ringwald 	it->offset += 8 + it->len;
10717ae5bc4SMatthias Ringwald 	if (it->offset >= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)) {
10817ae5bc4SMatthias Ringwald 		it->tag = 0xffffffff;
10917ae5bc4SMatthias Ringwald 		it->len = 0;
11017ae5bc4SMatthias Ringwald 		return;
11117ae5bc4SMatthias Ringwald 	}
11217ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
11317ae5bc4SMatthias Ringwald }
11417ae5bc4SMatthias Ringwald 
11517ae5bc4SMatthias Ringwald //
11617ae5bc4SMatthias Ringwald 
11717ae5bc4SMatthias Ringwald // check both banks for headers and pick the one with the higher epoch % 4
11817ae5bc4SMatthias Ringwald // @returns bank or -1 if something is invalid
11917ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){
12017ae5bc4SMatthias Ringwald  	uint8_t header0[BTSTACK_TLV_HEADER_LEN];
12117ae5bc4SMatthias Ringwald  	uint8_t header1[BTSTACK_TLV_HEADER_LEN];
122*e8efffc8SMatthias Ringwald  	btstack_tlv_flash_bank_read(self, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN);
123*e8efffc8SMatthias Ringwald  	btstack_tlv_flash_bank_read(self, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN);
12417ae5bc4SMatthias Ringwald  	int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
12517ae5bc4SMatthias Ringwald  	int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
12617ae5bc4SMatthias Ringwald 	if (!valid0 && !valid1) return -1;
12717ae5bc4SMatthias Ringwald 	if ( valid0 && !valid1) return 0;
12817ae5bc4SMatthias Ringwald 	if (!valid0 &&  valid1) return 1;
12917ae5bc4SMatthias Ringwald 	int epoch0 = header0[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
13017ae5bc4SMatthias Ringwald 	int epoch1 = header1[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
13117ae5bc4SMatthias Ringwald 	if (epoch0 == ((epoch1 + 1) & 0x03)) return 0;
13217ae5bc4SMatthias Ringwald 	if (epoch1 == ((epoch0 + 1) & 0x03)) return 1;
13317ae5bc4SMatthias Ringwald 	return -1;	// invalid, must not happen
13417ae5bc4SMatthias Ringwald }
13517ae5bc4SMatthias Ringwald 
13617ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self, int bank, int epoch){
13717ae5bc4SMatthias Ringwald 	uint8_t header[BTSTACK_TLV_HEADER_LEN];
13817ae5bc4SMatthias Ringwald 	memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1);
13917ae5bc4SMatthias Ringwald 	header[BTSTACK_TLV_HEADER_LEN-1] = epoch;
140*e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, bank, 0, header, BTSTACK_TLV_HEADER_LEN);
14117ae5bc4SMatthias Ringwald }
14217ae5bc4SMatthias Ringwald 
14317ae5bc4SMatthias Ringwald /**
14417ae5bc4SMatthias Ringwald  * @brief Check if erased from offset
14517ae5bc4SMatthias Ringwald  */
14617ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset){
14717ae5bc4SMatthias Ringwald 	log_info("test erased: bank %u, offset %u", bank, offset);
14817ae5bc4SMatthias Ringwald 	uint32_t size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context);
14917ae5bc4SMatthias Ringwald 	uint8_t buffer[16];
15017ae5bc4SMatthias Ringwald 	uint8_t empty16[16];
15117ae5bc4SMatthias Ringwald 	memset(empty16, 0xff, sizeof(empty16));
15217ae5bc4SMatthias Ringwald 	while (offset < size){
15317ae5bc4SMatthias Ringwald 		uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset);
154*e8efffc8SMatthias Ringwald 		btstack_tlv_flash_bank_read(self, bank, offset, buffer, copy_size);
15517ae5bc4SMatthias Ringwald 		if (memcmp(buffer, empty16, copy_size)) {
15617ae5bc4SMatthias Ringwald 			log_info("not erased %x - %x", offset, offset + copy_size);
15717ae5bc4SMatthias Ringwald 			return 0;
15817ae5bc4SMatthias Ringwald 		}
15917ae5bc4SMatthias Ringwald 		offset += copy_size;
16017ae5bc4SMatthias Ringwald 	}
16117ae5bc4SMatthias Ringwald 	return 1;
16217ae5bc4SMatthias Ringwald }
16317ae5bc4SMatthias Ringwald 
16417ae5bc4SMatthias Ringwald /**
16517ae5bc4SMatthias Ringwald  * @brief erase bank (only if not already erased)
16617ae5bc4SMatthias Ringwald  */
16717ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self, int bank){
16817ae5bc4SMatthias Ringwald 	if (btstack_tlv_flash_bank_test_erased(self, bank, 0)){
16917ae5bc4SMatthias Ringwald 		log_info("bank %u already erased", bank);
17017ae5bc4SMatthias Ringwald 	} else {
17117ae5bc4SMatthias Ringwald 		log_info("bank %u not empty, erase bank", bank);
17217ae5bc4SMatthias Ringwald 		self->hal_flash_bank_impl->erase(self->hal_flash_bank_context, bank);
17317ae5bc4SMatthias Ringwald 	}
17417ae5bc4SMatthias Ringwald }
17517ae5bc4SMatthias Ringwald 
17617ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){
17717ae5bc4SMatthias Ringwald 
17817ae5bc4SMatthias Ringwald 	int next_bank = 1 - self->current_bank;
17917ae5bc4SMatthias Ringwald 	log_info("migrate bank %u -> bank %u", self->current_bank, next_bank);
18017ae5bc4SMatthias Ringwald 	// erase bank (if needed)
18117ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_erase_bank(self, next_bank);
18217ae5bc4SMatthias Ringwald 	int next_write_pos = 8;
18317ae5bc4SMatthias Ringwald 
18417ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
18517ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
18617ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
18717ae5bc4SMatthias Ringwald 		// skip deleted entries
18817ae5bc4SMatthias Ringwald 		if (it.tag) {
18917ae5bc4SMatthias Ringwald 			uint32_t tag_len = it.len;
19017ae5bc4SMatthias Ringwald 			uint32_t tag_index = it.offset;
19117ae5bc4SMatthias Ringwald 
19217ae5bc4SMatthias Ringwald 			// copy
19317ae5bc4SMatthias Ringwald 			int bytes_to_copy = 8 + tag_len;
19417ae5bc4SMatthias Ringwald 			log_info("migrate pos %u, tag '%x' len %u -> new pos %u", tag_index, it.tag, bytes_to_copy, next_write_pos);
19517ae5bc4SMatthias Ringwald 			uint8_t copy_buffer[32];
19617ae5bc4SMatthias Ringwald 			while (bytes_to_copy){
19717ae5bc4SMatthias Ringwald 				int bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer));
198*e8efffc8SMatthias Ringwald 				btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
199*e8efffc8SMatthias Ringwald 				btstack_tlv_flash_bank_write(self, next_bank, next_write_pos, copy_buffer, bytes_this_iteration);
20017ae5bc4SMatthias Ringwald 				tag_index      += bytes_this_iteration;
20117ae5bc4SMatthias Ringwald 				next_write_pos += bytes_this_iteration;
20217ae5bc4SMatthias Ringwald 				bytes_to_copy  -= bytes_this_iteration;
20317ae5bc4SMatthias Ringwald 			}
20417ae5bc4SMatthias Ringwald 		}
20517ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
20617ae5bc4SMatthias Ringwald 	}
20717ae5bc4SMatthias Ringwald 
20817ae5bc4SMatthias Ringwald 	// prepare new one
20917ae5bc4SMatthias Ringwald 	uint8_t epoch_buffer;
210*e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1);
21117ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3);
21217ae5bc4SMatthias Ringwald 	self->current_bank = next_bank;
21317ae5bc4SMatthias Ringwald 	self->write_offset = next_write_pos;
21417ae5bc4SMatthias Ringwald }
21517ae5bc4SMatthias Ringwald 
21617ae5bc4SMatthias Ringwald // returns 1 == ok
21717ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_verify_alignment(btstack_tlv_flash_bank_t * self, uint32_t value_size){
21817ae5bc4SMatthias Ringwald 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
21917ae5bc4SMatthias Ringwald 	if (value_size % aligment){
22017ae5bc4SMatthias Ringwald 		log_error("Value size %u not a multiply of flash alignment %u", value_size, aligment);
22117ae5bc4SMatthias Ringwald 		return 0;
22217ae5bc4SMatthias Ringwald 	};
22317ae5bc4SMatthias Ringwald 	return 1;
22417ae5bc4SMatthias Ringwald }
22517ae5bc4SMatthias Ringwald 
22617ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self, uint32_t tag, uint32_t offset){
22717ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
22817ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
22917ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it) && it.offset < offset){
23017ae5bc4SMatthias Ringwald 		if (it.tag == tag){
23117ae5bc4SMatthias Ringwald 			log_info("Erase tag '%x' at position %u", tag, it.offset);
23217ae5bc4SMatthias Ringwald 			// overwrite tag with invalid tag
23317ae5bc4SMatthias Ringwald 			uint32_t zero_tag = 0;
234*e8efffc8SMatthias Ringwald 			btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_tag, sizeof(zero_tag));
23517ae5bc4SMatthias Ringwald 		}
23617ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
23717ae5bc4SMatthias Ringwald 	}
23817ae5bc4SMatthias Ringwald }
23917ae5bc4SMatthias Ringwald 
24017ae5bc4SMatthias Ringwald /**
24117ae5bc4SMatthias Ringwald  * Get Value for Tag
24217ae5bc4SMatthias Ringwald  * @param tag
24317ae5bc4SMatthias Ringwald  * @param buffer
24417ae5bc4SMatthias Ringwald  * @param buffer_size
24517ae5bc4SMatthias Ringwald  * @returns size of value
24617ae5bc4SMatthias Ringwald  */
24717ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
24817ae5bc4SMatthias Ringwald 
24917ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
25017ae5bc4SMatthias Ringwald 
25117ae5bc4SMatthias Ringwald 	// abort if data size not aligned with flash requirements
25217ae5bc4SMatthias Ringwald 	if (!btstack_tlv_flash_bank_verify_alignment(self, buffer_size)) return 0;
25317ae5bc4SMatthias Ringwald 
25417ae5bc4SMatthias Ringwald 	uint32_t tag_index = 0;
25517ae5bc4SMatthias Ringwald 	uint32_t tag_len   = 0;
25617ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
25717ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
25817ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
25917ae5bc4SMatthias Ringwald 		if (it.tag == tag){
26017ae5bc4SMatthias Ringwald 			log_info("Found tag '%x' at position %u", tag, it.offset);
26117ae5bc4SMatthias Ringwald 			tag_index = it.offset;
26217ae5bc4SMatthias Ringwald 			tag_len   = it.len;
26317ae5bc4SMatthias Ringwald 			break;
26417ae5bc4SMatthias Ringwald 		}
26517ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
26617ae5bc4SMatthias Ringwald 	}
26717ae5bc4SMatthias Ringwald 	if (tag_index == 0) return 0;
26817ae5bc4SMatthias Ringwald 	if (!buffer) return tag_len;
26917ae5bc4SMatthias Ringwald 	int copy_size = btstack_min(buffer_size, tag_len);
270*e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_read(self, self->current_bank, tag_index + 8, buffer, copy_size);
27117ae5bc4SMatthias Ringwald 	return copy_size;
27217ae5bc4SMatthias Ringwald }
27317ae5bc4SMatthias Ringwald 
27417ae5bc4SMatthias Ringwald /**
27517ae5bc4SMatthias Ringwald  * Store Tag
27617ae5bc4SMatthias Ringwald  * @param tag
27717ae5bc4SMatthias Ringwald  * @param data
27817ae5bc4SMatthias Ringwald  * @param data_size
27917ae5bc4SMatthias Ringwald  */
28017ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
28117ae5bc4SMatthias Ringwald 
28217ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
28317ae5bc4SMatthias Ringwald 
28417ae5bc4SMatthias Ringwald 	// abort if data size not aligned with flash requirements
28517ae5bc4SMatthias Ringwald 	if (!btstack_tlv_flash_bank_verify_alignment(self, data_size)) return 1;
28617ae5bc4SMatthias Ringwald 
28717ae5bc4SMatthias Ringwald 	// trigger migration if not enough space
28817ae5bc4SMatthias Ringwald 	if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
28917ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_migrate(self);
29017ae5bc4SMatthias Ringwald 	}
29117ae5bc4SMatthias Ringwald 
29217ae5bc4SMatthias Ringwald 	if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
29317ae5bc4SMatthias Ringwald 		log_error("couldn't write entry, not enough space left");
29417ae5bc4SMatthias Ringwald 		return 2;
29517ae5bc4SMatthias Ringwald 	}
29617ae5bc4SMatthias Ringwald 
29717ae5bc4SMatthias Ringwald 	// prepare entry
29817ae5bc4SMatthias Ringwald 	uint8_t entry[8];
29917ae5bc4SMatthias Ringwald 	big_endian_store_32(entry, 0, tag);
30017ae5bc4SMatthias Ringwald 	big_endian_store_32(entry, 4, data_size);
30117ae5bc4SMatthias Ringwald 
30217ae5bc4SMatthias Ringwald 	log_info("write '%x', len %u at %u", tag, data_size, self->write_offset);
30317ae5bc4SMatthias Ringwald 
30417ae5bc4SMatthias Ringwald 	// write value first
305*e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset + 8, data, data_size);
30617ae5bc4SMatthias Ringwald 
30717ae5bc4SMatthias Ringwald 	// then entry
308*e8efffc8SMatthias Ringwald 	btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset, entry, sizeof(entry));
30917ae5bc4SMatthias Ringwald 
31017ae5bc4SMatthias Ringwald 	// overwrite old entries (if exists)
31117ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
31217ae5bc4SMatthias Ringwald 
31317ae5bc4SMatthias Ringwald 	// done
31417ae5bc4SMatthias Ringwald 	self->write_offset += sizeof(entry) + data_size;
31517ae5bc4SMatthias Ringwald 
31617ae5bc4SMatthias Ringwald 	return 0;
31717ae5bc4SMatthias Ringwald }
31817ae5bc4SMatthias Ringwald 
31917ae5bc4SMatthias Ringwald /**
32017ae5bc4SMatthias Ringwald  * Delete Tag
32117ae5bc4SMatthias Ringwald  * @param tag
32217ae5bc4SMatthias Ringwald  */
32317ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag(void * context, uint32_t tag){
32417ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
32517ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
32617ae5bc4SMatthias Ringwald }
32717ae5bc4SMatthias Ringwald 
32817ae5bc4SMatthias Ringwald static const btstack_tlv_t btstack_tlv_flash_bank = {
32917ae5bc4SMatthias Ringwald 	/* int  (*get_tag)(..);     */ &btstack_tlv_flash_bank_get_tag,
33017ae5bc4SMatthias Ringwald 	/* int (*store_tag)(..);    */ &btstack_tlv_flash_bank_store_tag,
33117ae5bc4SMatthias Ringwald 	/* void (*delete_tag)(v..); */ &btstack_tlv_flash_bank_delete_tag,
33217ae5bc4SMatthias Ringwald };
33317ae5bc4SMatthias Ringwald 
33417ae5bc4SMatthias Ringwald /**
33517ae5bc4SMatthias Ringwald  * Init Tag Length Value Store
33617ae5bc4SMatthias Ringwald  */
33717ae5bc4SMatthias 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){
33817ae5bc4SMatthias Ringwald 
33917ae5bc4SMatthias Ringwald 	self->hal_flash_bank_impl    = hal_flash_bank_impl;
34017ae5bc4SMatthias Ringwald 	self->hal_flash_bank_context = hal_flash_bank_context;
34117ae5bc4SMatthias Ringwald 
34217ae5bc4SMatthias Ringwald 	// try to find current bank
34317ae5bc4SMatthias Ringwald 	self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self);
34417ae5bc4SMatthias Ringwald 	log_info("found bank %d", self->current_bank);
34517ae5bc4SMatthias Ringwald 	if (self->current_bank >= 0){
34617ae5bc4SMatthias Ringwald 
34717ae5bc4SMatthias Ringwald 		// find last entry and write offset
34817ae5bc4SMatthias Ringwald 		tlv_iterator_t it;
34917ae5bc4SMatthias Ringwald 		uint32_t last_tag = 0;
35017ae5bc4SMatthias Ringwald 		uint32_t last_offset = 0;
35117ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
35217ae5bc4SMatthias Ringwald 		while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
35317ae5bc4SMatthias Ringwald 			last_tag = it.tag;
35417ae5bc4SMatthias Ringwald 			last_offset = it.offset;
35517ae5bc4SMatthias Ringwald 			tlv_iterator_fetch_next(self, &it);
35617ae5bc4SMatthias Ringwald 		}
35717ae5bc4SMatthias Ringwald 		self->write_offset = it.offset;
35817ae5bc4SMatthias Ringwald 
35917ae5bc4SMatthias Ringwald 		if (self->write_offset < self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
36017ae5bc4SMatthias Ringwald 
36117ae5bc4SMatthias Ringwald 			// delete older instances of last_tag
36217ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete
36317ae5bc4SMatthias Ringwald 			if (last_tag){
36417ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset);
36517ae5bc4SMatthias Ringwald 			}
36617ae5bc4SMatthias Ringwald 
36717ae5bc4SMatthias Ringwald 			// verify that rest of bank is empty
36817ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value was written, but not the tag
36917ae5bc4SMatthias Ringwald 			if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){
37017ae5bc4SMatthias Ringwald 				log_info("Flash not empty after last found tag -> migrate");
37117ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_migrate(self);
37217ae5bc4SMatthias Ringwald 			} else {
37317ae5bc4SMatthias Ringwald 				log_info("Flash clean after last found tag");
37417ae5bc4SMatthias Ringwald 			}
37517ae5bc4SMatthias Ringwald 		} else {
37617ae5bc4SMatthias Ringwald 			// failure!
37717ae5bc4SMatthias Ringwald 			self->current_bank = -1;
37817ae5bc4SMatthias Ringwald 		}
37917ae5bc4SMatthias Ringwald 	}
38017ae5bc4SMatthias Ringwald 
38117ae5bc4SMatthias Ringwald 	if (self->current_bank < 0) {
38217ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_erase_bank(self, 0);
38317ae5bc4SMatthias Ringwald 		self->current_bank = 0;
38417ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_write_header(self, self->current_bank, 0);	// epoch = 0;
38517ae5bc4SMatthias Ringwald 		self->write_offset = 8;
38617ae5bc4SMatthias Ringwald 	}
38717ae5bc4SMatthias Ringwald 
38817ae5bc4SMatthias Ringwald 	log_info("write offset %u", self->write_offset);
38917ae5bc4SMatthias Ringwald 	return &btstack_tlv_flash_bank;
39017ae5bc4SMatthias Ringwald }
39117ae5bc4SMatthias Ringwald 
392