xref: /btstack/platform/embedded/btstack_tlv_flash_bank.c (revision 17ae5bc4f48b8b6c46a2a11fb6395ec461fe225d)
1*17ae5bc4SMatthias Ringwald /*
2*17ae5bc4SMatthias Ringwald  * Copyright (C) 2017 BlueKitchen GmbH
3*17ae5bc4SMatthias Ringwald  *
4*17ae5bc4SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*17ae5bc4SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*17ae5bc4SMatthias Ringwald  * are met:
7*17ae5bc4SMatthias Ringwald  *
8*17ae5bc4SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*17ae5bc4SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*17ae5bc4SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*17ae5bc4SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*17ae5bc4SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*17ae5bc4SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*17ae5bc4SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*17ae5bc4SMatthias Ringwald  *    from this software without specific prior written permission.
16*17ae5bc4SMatthias Ringwald  *
17*17ae5bc4SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
18*17ae5bc4SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*17ae5bc4SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20*17ae5bc4SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
21*17ae5bc4SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22*17ae5bc4SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23*17ae5bc4SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24*17ae5bc4SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25*17ae5bc4SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26*17ae5bc4SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27*17ae5bc4SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*17ae5bc4SMatthias Ringwald  * SUCH DAMAGE.
29*17ae5bc4SMatthias Ringwald  *
30*17ae5bc4SMatthias Ringwald  */
31*17ae5bc4SMatthias Ringwald 
32*17ae5bc4SMatthias Ringwald #define __BTSTACK_FILE__ "btstack_tlv_flash_bank.c"
33*17ae5bc4SMatthias Ringwald 
34*17ae5bc4SMatthias Ringwald #include "btstack_tlv.h"
35*17ae5bc4SMatthias Ringwald #include "btstack_tlv_flash_bank.h"
36*17ae5bc4SMatthias Ringwald #include "btstack_debug.h"
37*17ae5bc4SMatthias Ringwald #include "btstack_util.h"
38*17ae5bc4SMatthias Ringwald #include "btstack_debug.h"
39*17ae5bc4SMatthias Ringwald 
40*17ae5bc4SMatthias Ringwald #include <string.h>
41*17ae5bc4SMatthias Ringwald 
42*17ae5bc4SMatthias Ringwald // Header:
43*17ae5bc4SMatthias Ringwald // - Magic: 'BTstack'
44*17ae5bc4SMatthias Ringwald // - Status:
45*17ae5bc4SMatthias Ringwald //   - bits 765432: reserved
46*17ae5bc4SMatthias Ringwald //	 - bits 10:     epoch
47*17ae5bc4SMatthias Ringwald 
48*17ae5bc4SMatthias Ringwald // Entries
49*17ae5bc4SMatthias Ringwald // - Tag: 32 bit
50*17ae5bc4SMatthias Ringwald // - Len: 32 bit
51*17ae5bc4SMatthias Ringwald // - Value: Len in bytes
52*17ae5bc4SMatthias Ringwald 
53*17ae5bc4SMatthias Ringwald #define BTSTACK_TLV_HEADER_LEN 8
54*17ae5bc4SMatthias Ringwald static const char * btstack_tlv_header_magic = "BTstack";
55*17ae5bc4SMatthias Ringwald 
56*17ae5bc4SMatthias Ringwald // TLV Iterator
57*17ae5bc4SMatthias Ringwald 
58*17ae5bc4SMatthias Ringwald typedef struct {
59*17ae5bc4SMatthias Ringwald 	int 	 bank;
60*17ae5bc4SMatthias Ringwald 	uint32_t offset;
61*17ae5bc4SMatthias Ringwald 	uint32_t tag;
62*17ae5bc4SMatthias Ringwald 	uint32_t len;
63*17ae5bc4SMatthias Ringwald } tlv_iterator_t;
64*17ae5bc4SMatthias Ringwald 
65*17ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
66*17ae5bc4SMatthias Ringwald 	uint8_t entry[8];
67*17ae5bc4SMatthias Ringwald 	self->hal_flash_bank_impl->read(self->hal_flash_bank_context, it->bank, it->offset, entry, 8);
68*17ae5bc4SMatthias Ringwald 	it->tag = big_endian_read_32(entry, 0);
69*17ae5bc4SMatthias Ringwald 	it->len = big_endian_read_32(entry, 4);
70*17ae5bc4SMatthias Ringwald }
71*17ae5bc4SMatthias Ringwald 
72*17ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it, int bank){
73*17ae5bc4SMatthias Ringwald 	memset(it, 0, sizeof(tlv_iterator_t));
74*17ae5bc4SMatthias Ringwald 	it->bank = bank;
75*17ae5bc4SMatthias Ringwald 	it->offset = BTSTACK_TLV_HEADER_LEN;
76*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
77*17ae5bc4SMatthias Ringwald }
78*17ae5bc4SMatthias Ringwald 
79*17ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
80*17ae5bc4SMatthias Ringwald 	if (it->tag == 0xffffffff) return 0;
81*17ae5bc4SMatthias Ringwald 	return 1;
82*17ae5bc4SMatthias Ringwald }
83*17ae5bc4SMatthias Ringwald 
84*17ae5bc4SMatthias Ringwald static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
85*17ae5bc4SMatthias Ringwald 	it->offset += 8 + it->len;
86*17ae5bc4SMatthias Ringwald 	if (it->offset >= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)) {
87*17ae5bc4SMatthias Ringwald 		it->tag = 0xffffffff;
88*17ae5bc4SMatthias Ringwald 		it->len = 0;
89*17ae5bc4SMatthias Ringwald 		return;
90*17ae5bc4SMatthias Ringwald 	}
91*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
92*17ae5bc4SMatthias Ringwald }
93*17ae5bc4SMatthias Ringwald 
94*17ae5bc4SMatthias Ringwald //
95*17ae5bc4SMatthias Ringwald 
96*17ae5bc4SMatthias Ringwald // check both banks for headers and pick the one with the higher epoch % 4
97*17ae5bc4SMatthias Ringwald // @returns bank or -1 if something is invalid
98*17ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){
99*17ae5bc4SMatthias Ringwald  	uint8_t header0[BTSTACK_TLV_HEADER_LEN];
100*17ae5bc4SMatthias Ringwald  	uint8_t header1[BTSTACK_TLV_HEADER_LEN];
101*17ae5bc4SMatthias Ringwald  	self->hal_flash_bank_impl->read(self->hal_flash_bank_context, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN);
102*17ae5bc4SMatthias Ringwald  	self->hal_flash_bank_impl->read(self->hal_flash_bank_context, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN);
103*17ae5bc4SMatthias Ringwald  	int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
104*17ae5bc4SMatthias Ringwald  	int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
105*17ae5bc4SMatthias Ringwald 	if (!valid0 && !valid1) return -1;
106*17ae5bc4SMatthias Ringwald 	if ( valid0 && !valid1) return 0;
107*17ae5bc4SMatthias Ringwald 	if (!valid0 &&  valid1) return 1;
108*17ae5bc4SMatthias Ringwald 	int epoch0 = header0[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
109*17ae5bc4SMatthias Ringwald 	int epoch1 = header1[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
110*17ae5bc4SMatthias Ringwald 	if (epoch0 == ((epoch1 + 1) & 0x03)) return 0;
111*17ae5bc4SMatthias Ringwald 	if (epoch1 == ((epoch0 + 1) & 0x03)) return 1;
112*17ae5bc4SMatthias Ringwald 	return -1;	// invalid, must not happen
113*17ae5bc4SMatthias Ringwald }
114*17ae5bc4SMatthias Ringwald 
115*17ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self, int bank, int epoch){
116*17ae5bc4SMatthias Ringwald 	uint8_t header[BTSTACK_TLV_HEADER_LEN];
117*17ae5bc4SMatthias Ringwald 	memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1);
118*17ae5bc4SMatthias Ringwald 	header[BTSTACK_TLV_HEADER_LEN-1] = epoch;
119*17ae5bc4SMatthias Ringwald 	self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, 0, header, BTSTACK_TLV_HEADER_LEN);
120*17ae5bc4SMatthias Ringwald }
121*17ae5bc4SMatthias Ringwald 
122*17ae5bc4SMatthias Ringwald /**
123*17ae5bc4SMatthias Ringwald  * @brief Check if erased from offset
124*17ae5bc4SMatthias Ringwald  */
125*17ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset){
126*17ae5bc4SMatthias Ringwald 	log_info("test erased: bank %u, offset %u", bank, offset);
127*17ae5bc4SMatthias Ringwald 	uint32_t size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context);
128*17ae5bc4SMatthias Ringwald 	uint8_t buffer[16];
129*17ae5bc4SMatthias Ringwald 	uint8_t empty16[16];
130*17ae5bc4SMatthias Ringwald 	memset(empty16, 0xff, sizeof(empty16));
131*17ae5bc4SMatthias Ringwald 	while (offset < size){
132*17ae5bc4SMatthias Ringwald 		uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset);
133*17ae5bc4SMatthias Ringwald 		self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, copy_size);
134*17ae5bc4SMatthias Ringwald 		if (memcmp(buffer, empty16, copy_size)) {
135*17ae5bc4SMatthias Ringwald 			log_info("not erased %x - %x", offset, offset + copy_size);
136*17ae5bc4SMatthias Ringwald 			return 0;
137*17ae5bc4SMatthias Ringwald 		}
138*17ae5bc4SMatthias Ringwald 		offset += copy_size;
139*17ae5bc4SMatthias Ringwald 	}
140*17ae5bc4SMatthias Ringwald 	return 1;
141*17ae5bc4SMatthias Ringwald }
142*17ae5bc4SMatthias Ringwald 
143*17ae5bc4SMatthias Ringwald /**
144*17ae5bc4SMatthias Ringwald  * @brief erase bank (only if not already erased)
145*17ae5bc4SMatthias Ringwald  */
146*17ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self, int bank){
147*17ae5bc4SMatthias Ringwald 	if (btstack_tlv_flash_bank_test_erased(self, bank, 0)){
148*17ae5bc4SMatthias Ringwald 		log_info("bank %u already erased", bank);
149*17ae5bc4SMatthias Ringwald 	} else {
150*17ae5bc4SMatthias Ringwald 		log_info("bank %u not empty, erase bank", bank);
151*17ae5bc4SMatthias Ringwald 		self->hal_flash_bank_impl->erase(self->hal_flash_bank_context, bank);
152*17ae5bc4SMatthias Ringwald 	}
153*17ae5bc4SMatthias Ringwald }
154*17ae5bc4SMatthias Ringwald 
155*17ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){
156*17ae5bc4SMatthias Ringwald 
157*17ae5bc4SMatthias Ringwald 	int next_bank = 1 - self->current_bank;
158*17ae5bc4SMatthias Ringwald 	log_info("migrate bank %u -> bank %u", self->current_bank, next_bank);
159*17ae5bc4SMatthias Ringwald 	// erase bank (if needed)
160*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_erase_bank(self, next_bank);
161*17ae5bc4SMatthias Ringwald 	int next_write_pos = 8;
162*17ae5bc4SMatthias Ringwald 
163*17ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
164*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
165*17ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
166*17ae5bc4SMatthias Ringwald 		// skip deleted entries
167*17ae5bc4SMatthias Ringwald 		if (it.tag) {
168*17ae5bc4SMatthias Ringwald 			uint32_t tag_len = it.len;
169*17ae5bc4SMatthias Ringwald 			uint32_t tag_index = it.offset;
170*17ae5bc4SMatthias Ringwald 
171*17ae5bc4SMatthias Ringwald 			// copy
172*17ae5bc4SMatthias Ringwald 			int bytes_to_copy = 8 + tag_len;
173*17ae5bc4SMatthias Ringwald 			log_info("migrate pos %u, tag '%x' len %u -> new pos %u", tag_index, it.tag, bytes_to_copy, next_write_pos);
174*17ae5bc4SMatthias Ringwald 			uint8_t copy_buffer[32];
175*17ae5bc4SMatthias Ringwald 			while (bytes_to_copy){
176*17ae5bc4SMatthias Ringwald 				int bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer));
177*17ae5bc4SMatthias Ringwald 				self->hal_flash_bank_impl->read(self->hal_flash_bank_context, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
178*17ae5bc4SMatthias Ringwald 				self->hal_flash_bank_impl->write(self->hal_flash_bank_context, next_bank, next_write_pos, copy_buffer, bytes_this_iteration);
179*17ae5bc4SMatthias Ringwald 				tag_index      += bytes_this_iteration;
180*17ae5bc4SMatthias Ringwald 				next_write_pos += bytes_this_iteration;
181*17ae5bc4SMatthias Ringwald 				bytes_to_copy  -= bytes_this_iteration;
182*17ae5bc4SMatthias Ringwald 			}
183*17ae5bc4SMatthias Ringwald 		}
184*17ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
185*17ae5bc4SMatthias Ringwald 	}
186*17ae5bc4SMatthias Ringwald 
187*17ae5bc4SMatthias Ringwald 	// prepare new one
188*17ae5bc4SMatthias Ringwald 	uint8_t epoch_buffer;
189*17ae5bc4SMatthias Ringwald 	self->hal_flash_bank_impl->read(self->hal_flash_bank_context, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1);
190*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3);
191*17ae5bc4SMatthias Ringwald 	self->current_bank = next_bank;
192*17ae5bc4SMatthias Ringwald 	self->write_offset = next_write_pos;
193*17ae5bc4SMatthias Ringwald }
194*17ae5bc4SMatthias Ringwald 
195*17ae5bc4SMatthias Ringwald // returns 1 == ok
196*17ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_verify_alignment(btstack_tlv_flash_bank_t * self, uint32_t value_size){
197*17ae5bc4SMatthias Ringwald 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
198*17ae5bc4SMatthias Ringwald 	if (value_size % aligment){
199*17ae5bc4SMatthias Ringwald 		log_error("Value size %u not a multiply of flash alignment %u", value_size, aligment);
200*17ae5bc4SMatthias Ringwald 		return 0;
201*17ae5bc4SMatthias Ringwald 	};
202*17ae5bc4SMatthias Ringwald 	return 1;
203*17ae5bc4SMatthias Ringwald }
204*17ae5bc4SMatthias Ringwald 
205*17ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self, uint32_t tag, uint32_t offset){
206*17ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
207*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
208*17ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it) && it.offset < offset){
209*17ae5bc4SMatthias Ringwald 		if (it.tag == tag){
210*17ae5bc4SMatthias Ringwald 			log_info("Erase tag '%x' at position %u", tag, it.offset);
211*17ae5bc4SMatthias Ringwald 			// overwrite tag with invalid tag
212*17ae5bc4SMatthias Ringwald 			uint32_t zero_tag = 0;
213*17ae5bc4SMatthias Ringwald 			self->hal_flash_bank_impl->write(self->hal_flash_bank_context, self->current_bank, it.offset, (uint8_t*) &zero_tag, sizeof(zero_tag));
214*17ae5bc4SMatthias Ringwald 		}
215*17ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
216*17ae5bc4SMatthias Ringwald 	}
217*17ae5bc4SMatthias Ringwald }
218*17ae5bc4SMatthias Ringwald 
219*17ae5bc4SMatthias Ringwald /**
220*17ae5bc4SMatthias Ringwald  * Get Value for Tag
221*17ae5bc4SMatthias Ringwald  * @param tag
222*17ae5bc4SMatthias Ringwald  * @param buffer
223*17ae5bc4SMatthias Ringwald  * @param buffer_size
224*17ae5bc4SMatthias Ringwald  * @returns size of value
225*17ae5bc4SMatthias Ringwald  */
226*17ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
227*17ae5bc4SMatthias Ringwald 
228*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
229*17ae5bc4SMatthias Ringwald 
230*17ae5bc4SMatthias Ringwald 	// abort if data size not aligned with flash requirements
231*17ae5bc4SMatthias Ringwald 	if (!btstack_tlv_flash_bank_verify_alignment(self, buffer_size)) return 0;
232*17ae5bc4SMatthias Ringwald 
233*17ae5bc4SMatthias Ringwald 	uint32_t tag_index = 0;
234*17ae5bc4SMatthias Ringwald 	uint32_t tag_len   = 0;
235*17ae5bc4SMatthias Ringwald 	tlv_iterator_t it;
236*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
237*17ae5bc4SMatthias Ringwald 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
238*17ae5bc4SMatthias Ringwald 		if (it.tag == tag){
239*17ae5bc4SMatthias Ringwald 			log_info("Found tag '%x' at position %u", tag, it.offset);
240*17ae5bc4SMatthias Ringwald 			tag_index = it.offset;
241*17ae5bc4SMatthias Ringwald 			tag_len   = it.len;
242*17ae5bc4SMatthias Ringwald 			break;
243*17ae5bc4SMatthias Ringwald 		}
244*17ae5bc4SMatthias Ringwald 		tlv_iterator_fetch_next(self, &it);
245*17ae5bc4SMatthias Ringwald 	}
246*17ae5bc4SMatthias Ringwald 	if (tag_index == 0) return 0;
247*17ae5bc4SMatthias Ringwald 	if (!buffer) return tag_len;
248*17ae5bc4SMatthias Ringwald 	int copy_size = btstack_min(buffer_size, tag_len);
249*17ae5bc4SMatthias Ringwald 	self->hal_flash_bank_impl->read(self->hal_flash_bank_context, self->current_bank, tag_index + 8, buffer, copy_size);
250*17ae5bc4SMatthias Ringwald 	return copy_size;
251*17ae5bc4SMatthias Ringwald }
252*17ae5bc4SMatthias Ringwald 
253*17ae5bc4SMatthias Ringwald /**
254*17ae5bc4SMatthias Ringwald  * Store Tag
255*17ae5bc4SMatthias Ringwald  * @param tag
256*17ae5bc4SMatthias Ringwald  * @param data
257*17ae5bc4SMatthias Ringwald  * @param data_size
258*17ae5bc4SMatthias Ringwald  */
259*17ae5bc4SMatthias Ringwald static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
260*17ae5bc4SMatthias Ringwald 
261*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
262*17ae5bc4SMatthias Ringwald 
263*17ae5bc4SMatthias Ringwald 	// abort if data size not aligned with flash requirements
264*17ae5bc4SMatthias Ringwald 	if (!btstack_tlv_flash_bank_verify_alignment(self, data_size)) return 1;
265*17ae5bc4SMatthias Ringwald 
266*17ae5bc4SMatthias Ringwald 	// trigger migration if not enough space
267*17ae5bc4SMatthias Ringwald 	if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
268*17ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_migrate(self);
269*17ae5bc4SMatthias Ringwald 	}
270*17ae5bc4SMatthias Ringwald 
271*17ae5bc4SMatthias Ringwald 	if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
272*17ae5bc4SMatthias Ringwald 		log_error("couldn't write entry, not enough space left");
273*17ae5bc4SMatthias Ringwald 		return 2;
274*17ae5bc4SMatthias Ringwald 	}
275*17ae5bc4SMatthias Ringwald 
276*17ae5bc4SMatthias Ringwald 	// prepare entry
277*17ae5bc4SMatthias Ringwald 	uint8_t entry[8];
278*17ae5bc4SMatthias Ringwald 	big_endian_store_32(entry, 0, tag);
279*17ae5bc4SMatthias Ringwald 	big_endian_store_32(entry, 4, data_size);
280*17ae5bc4SMatthias Ringwald 
281*17ae5bc4SMatthias Ringwald 	log_info("write '%x', len %u at %u", tag, data_size, self->write_offset);
282*17ae5bc4SMatthias Ringwald 
283*17ae5bc4SMatthias Ringwald 	// write value first
284*17ae5bc4SMatthias Ringwald 	self->hal_flash_bank_impl->write(self->hal_flash_bank_context, self->current_bank, self->write_offset + 8, data, data_size);
285*17ae5bc4SMatthias Ringwald 
286*17ae5bc4SMatthias Ringwald 	// then entry
287*17ae5bc4SMatthias Ringwald 	self->hal_flash_bank_impl->write(self->hal_flash_bank_context, self->current_bank, self->write_offset, entry, sizeof(entry));
288*17ae5bc4SMatthias Ringwald 
289*17ae5bc4SMatthias Ringwald 	// overwrite old entries (if exists)
290*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
291*17ae5bc4SMatthias Ringwald 
292*17ae5bc4SMatthias Ringwald 	// done
293*17ae5bc4SMatthias Ringwald 	self->write_offset += sizeof(entry) + data_size;
294*17ae5bc4SMatthias Ringwald 
295*17ae5bc4SMatthias Ringwald 	return 0;
296*17ae5bc4SMatthias Ringwald }
297*17ae5bc4SMatthias Ringwald 
298*17ae5bc4SMatthias Ringwald /**
299*17ae5bc4SMatthias Ringwald  * Delete Tag
300*17ae5bc4SMatthias Ringwald  * @param tag
301*17ae5bc4SMatthias Ringwald  */
302*17ae5bc4SMatthias Ringwald static void btstack_tlv_flash_bank_delete_tag(void * context, uint32_t tag){
303*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
304*17ae5bc4SMatthias Ringwald 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
305*17ae5bc4SMatthias Ringwald }
306*17ae5bc4SMatthias Ringwald 
307*17ae5bc4SMatthias Ringwald static const btstack_tlv_t btstack_tlv_flash_bank = {
308*17ae5bc4SMatthias Ringwald 	/* int  (*get_tag)(..);     */ &btstack_tlv_flash_bank_get_tag,
309*17ae5bc4SMatthias Ringwald 	/* int (*store_tag)(..);    */ &btstack_tlv_flash_bank_store_tag,
310*17ae5bc4SMatthias Ringwald 	/* void (*delete_tag)(v..); */ &btstack_tlv_flash_bank_delete_tag,
311*17ae5bc4SMatthias Ringwald };
312*17ae5bc4SMatthias Ringwald 
313*17ae5bc4SMatthias Ringwald /**
314*17ae5bc4SMatthias Ringwald  * Init Tag Length Value Store
315*17ae5bc4SMatthias Ringwald  */
316*17ae5bc4SMatthias 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){
317*17ae5bc4SMatthias Ringwald 
318*17ae5bc4SMatthias Ringwald 	self->hal_flash_bank_impl    = hal_flash_bank_impl;
319*17ae5bc4SMatthias Ringwald 	self->hal_flash_bank_context = hal_flash_bank_context;
320*17ae5bc4SMatthias Ringwald 
321*17ae5bc4SMatthias Ringwald 	// try to find current bank
322*17ae5bc4SMatthias Ringwald 	self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self);
323*17ae5bc4SMatthias Ringwald 	log_info("found bank %d", self->current_bank);
324*17ae5bc4SMatthias Ringwald 	if (self->current_bank >= 0){
325*17ae5bc4SMatthias Ringwald 
326*17ae5bc4SMatthias Ringwald 		// find last entry and write offset
327*17ae5bc4SMatthias Ringwald 		tlv_iterator_t it;
328*17ae5bc4SMatthias Ringwald 		uint32_t last_tag = 0;
329*17ae5bc4SMatthias Ringwald 		uint32_t last_offset = 0;
330*17ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
331*17ae5bc4SMatthias Ringwald 		while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
332*17ae5bc4SMatthias Ringwald 			last_tag = it.tag;
333*17ae5bc4SMatthias Ringwald 			last_offset = it.offset;
334*17ae5bc4SMatthias Ringwald 			tlv_iterator_fetch_next(self, &it);
335*17ae5bc4SMatthias Ringwald 		}
336*17ae5bc4SMatthias Ringwald 		self->write_offset = it.offset;
337*17ae5bc4SMatthias Ringwald 
338*17ae5bc4SMatthias Ringwald 		if (self->write_offset < self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
339*17ae5bc4SMatthias Ringwald 
340*17ae5bc4SMatthias Ringwald 			// delete older instances of last_tag
341*17ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete
342*17ae5bc4SMatthias Ringwald 			if (last_tag){
343*17ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset);
344*17ae5bc4SMatthias Ringwald 			}
345*17ae5bc4SMatthias Ringwald 
346*17ae5bc4SMatthias Ringwald 			// verify that rest of bank is empty
347*17ae5bc4SMatthias Ringwald 			// this handles the unlikely case where MCU did reset after new value was written, but not the tag
348*17ae5bc4SMatthias Ringwald 			if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){
349*17ae5bc4SMatthias Ringwald 				log_info("Flash not empty after last found tag -> migrate");
350*17ae5bc4SMatthias Ringwald 				btstack_tlv_flash_bank_migrate(self);
351*17ae5bc4SMatthias Ringwald 			} else {
352*17ae5bc4SMatthias Ringwald 				log_info("Flash clean after last found tag");
353*17ae5bc4SMatthias Ringwald 			}
354*17ae5bc4SMatthias Ringwald 		} else {
355*17ae5bc4SMatthias Ringwald 			// failure!
356*17ae5bc4SMatthias Ringwald 			self->current_bank = -1;
357*17ae5bc4SMatthias Ringwald 		}
358*17ae5bc4SMatthias Ringwald 	}
359*17ae5bc4SMatthias Ringwald 
360*17ae5bc4SMatthias Ringwald 	if (self->current_bank < 0) {
361*17ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_erase_bank(self, 0);
362*17ae5bc4SMatthias Ringwald 		self->current_bank = 0;
363*17ae5bc4SMatthias Ringwald 		btstack_tlv_flash_bank_write_header(self, self->current_bank, 0);	// epoch = 0;
364*17ae5bc4SMatthias Ringwald 		self->write_offset = 8;
365*17ae5bc4SMatthias Ringwald 	}
366*17ae5bc4SMatthias Ringwald 
367*17ae5bc4SMatthias Ringwald 	log_info("write offset %u", self->write_offset);
368*17ae5bc4SMatthias Ringwald 	return &btstack_tlv_flash_bank;
369*17ae5bc4SMatthias Ringwald }
370*17ae5bc4SMatthias Ringwald 
371