xref: /btstack/platform/embedded/btstack_tlv_flash_bank.c (revision 0d5520a9a5e886171fe21d71d9d335c6608e68c0)
1 /*
2  * Copyright (C) 2017 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
21  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #define BTSTACK_FILE__ "btstack_tlv_flash_bank.c"
33 
34 #include "btstack_tlv.h"
35 #include "btstack_tlv_flash_bank.h"
36 #include "btstack_debug.h"
37 #include "btstack_util.h"
38 #include "btstack_debug.h"
39 
40 #include <string.h>
41 
42 // Header:
43 // - Magic: 'BTstack'
44 // - Status:
45 //   - bits 765432: reserved
46 //	 - bits 10:     epoch
47 
48 // Entries
49 // - Tag: 32 bit
50 // - Len: 32 bit
51 // - Delete: 32 delete field - only used with ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
52 // - Value: Len in bytes
53 
54 // ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
55 //
56 // Most Flash implementations allow to:
57 // - erase sector -> all values are 0xff
58 // - write value (1s -> 0s)
59 // - overwrite value with zero (remaininig 1s -> 0s)
60 //
61 // We use the ability to overwrite a value with zeros to mark deleted enttries (by writing zero into the tag field).
62 // Some targetes, E.g. Kinetix K64F, do enot allow for that.
63 //
64 // With ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD an extra field is reserved to indicate a deleted tag, while keeping main logic
65 
66 #define BTSTACK_TLV_HEADER_LEN 8
67 
68 #ifndef BTSTACK_FLASH_ALIGNMENT_MAX
69 #define BTSTACK_FLASH_ALIGNMENT_MAX 8
70 #endif
71 
72 static const char * btstack_tlv_header_magic = "BTstack";
73 
74 // TLV Iterator
75 typedef struct {
76 	int 	 bank;
77 	uint32_t offset;
78 	uint32_t tag;
79 	uint32_t len;
80 } tlv_iterator_t;
81 
82 static uint32_t btstack_tlv_flash_bank_align_size(btstack_tlv_flash_bank_t * self, uint32_t size){
83 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
84 	return (size + aligment - 1) & ~(aligment - 1);
85 }
86 
87 // support unaligned flash read/writes
88 // strategy: increase size to meet alignment, perform unaligned read/write of last chunk with helper buffer
89 
90 static void btstack_tlv_flash_bank_read(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
91 
92 	// read main data
93 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
94 	uint32_t lower_bits = size & (aligment - 1);
95 	uint32_t size_aligned = size - lower_bits;
96 	if (size_aligned){
97 		self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, size_aligned);
98 		buffer += size_aligned;
99 		offset += size_aligned;
100 		size   -= size_aligned;
101 	}
102 
103 	// read last part
104 	if (size == 0) return;
105 	uint8_t aligment_block[BTSTACK_FLASH_ALIGNMENT_MAX];
106 	self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, aligment_block, aligment);
107 	uint32_t bytes_to_copy = btstack_min(aligment - lower_bits, size);
108 	memcpy(buffer, aligment_block, bytes_to_copy);
109 }
110 
111 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){
112 
113 	// write main data
114 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
115 	uint32_t lower_bits = size & (aligment - 1);
116 	uint32_t size_aligned = size - lower_bits;
117 	if (size_aligned){
118 		self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, buffer, size_aligned);
119 		buffer += size_aligned;
120 		offset += size_aligned;
121 		size   -= size_aligned;
122 	}
123 
124 	// write last part
125 	if (size == 0) return;
126 	uint8_t aligment_block[BTSTACK_FLASH_ALIGNMENT_MAX];
127 	memset(aligment_block, 0xff, aligment);
128 	memcpy(aligment_block, buffer, lower_bits);
129 	self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, aligment_block, aligment);
130 }
131 
132 
133 // iterator
134 
135 static void btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
136 	uint8_t entry[8];
137 	btstack_tlv_flash_bank_read(self, it->bank, it->offset, entry, 8);
138 	it->tag = big_endian_read_32(entry, 0);
139 	it->len = big_endian_read_32(entry, 4);
140 
141 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
142 	// clear tag, if delete field is set
143 	uint32_t delete_tag;
144 	btstack_tlv_flash_bank_read(self, it->bank, it->offset + 8, (uint8_t *) &delete_tag, 4);
145 	if (delete_tag == 0){
146 		it->tag = 0;
147 	}
148 	log_info("fetch tag: %x, len %u, delete %x (offset %u)", it->tag, it->len, delete_tag, it->offset);
149 #endif
150 }
151 
152 static void btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it, int bank){
153 	memset(it, 0, sizeof(tlv_iterator_t));
154 	it->bank = bank;
155 	it->offset = BTSTACK_TLV_HEADER_LEN;
156 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
157 }
158 
159 static int btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
160 	if (it->tag == 0xffffffff) return 0;
161 	return 1;
162 }
163 
164 static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
165 	it->offset += 8 + btstack_tlv_flash_bank_align_size(self, it->len);
166 
167 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
168 	// skip delete field
169 	it->offset += 4;
170 #endif
171 
172 	if (it->offset >= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)) {
173 		it->tag = 0xffffffff;
174 		it->len = 0;
175 		return;
176 	}
177 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
178 }
179 
180 //
181 
182 // check both banks for headers and pick the one with the higher epoch % 4
183 // @returns bank or -1 if something is invalid
184 static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){
185  	uint8_t header0[BTSTACK_TLV_HEADER_LEN];
186  	uint8_t header1[BTSTACK_TLV_HEADER_LEN];
187  	btstack_tlv_flash_bank_read(self, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN);
188  	btstack_tlv_flash_bank_read(self, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN);
189  	int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
190  	int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
191 	if (!valid0 && !valid1) return -1;
192 	if ( valid0 && !valid1) return 0;
193 	if (!valid0 &&  valid1) return 1;
194 	int epoch0 = header0[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
195 	int epoch1 = header1[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
196 	if (epoch0 == ((epoch1 + 1) & 0x03)) return 0;
197 	if (epoch1 == ((epoch0 + 1) & 0x03)) return 1;
198 	return -1;	// invalid, must not happen
199 }
200 
201 static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self, int bank, int epoch){
202 	uint8_t header[BTSTACK_TLV_HEADER_LEN];
203 	memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1);
204 	header[BTSTACK_TLV_HEADER_LEN-1] = epoch;
205 	btstack_tlv_flash_bank_write(self, bank, 0, header, BTSTACK_TLV_HEADER_LEN);
206 }
207 
208 /**
209  * @brief Check if erased from offset
210  */
211 static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset){
212 	log_info("test erased: bank %u, offset %u", bank, offset);
213 	uint32_t size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context);
214 	uint8_t buffer[16];
215 	uint8_t empty16[16];
216 	memset(empty16, 0xff, sizeof(empty16));
217 	while (offset < size){
218 		uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset);
219 		btstack_tlv_flash_bank_read(self, bank, offset, buffer, copy_size);
220 		if (memcmp(buffer, empty16, copy_size)) {
221 			log_info("not erased %x - %x", offset, offset + copy_size);
222 			return 0;
223 		}
224 		offset += copy_size;
225 	}
226 	return 1;
227 }
228 
229 /**
230  * @brief erase bank (only if not already erased)
231  */
232 static void btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self, int bank){
233 	if (btstack_tlv_flash_bank_test_erased(self, bank, 0)){
234 		log_info("bank %u already erased", bank);
235 	} else {
236 		log_info("bank %u not empty, erase bank", bank);
237 		self->hal_flash_bank_impl->erase(self->hal_flash_bank_context, bank);
238 	}
239 }
240 
241 static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){
242 
243 	int next_bank = 1 - self->current_bank;
244 	log_info("migrate bank %u -> bank %u", self->current_bank, next_bank);
245 	// erase bank (if needed)
246 	btstack_tlv_flash_bank_erase_bank(self, next_bank);
247 	int next_write_pos = 8;
248 
249 	tlv_iterator_t it;
250 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
251 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
252 		// skip deleted entries
253 		if (it.tag) {
254 			uint32_t tag_len = it.len;
255 			uint32_t tag_index = it.offset;
256 
257 			log_info("migrate pos %u, tag '%x' len %u -> new pos %u", tag_index, it.tag, tag_len, next_write_pos);
258 
259 			// copy header
260 			uint8_t header_buffer[8];
261 			btstack_tlv_flash_bank_read(self, self->current_bank, tag_index,      header_buffer, 8);
262 			btstack_tlv_flash_bank_write(self, next_bank,         next_write_pos, header_buffer, 8);
263 			tag_index      += 8;
264 			next_write_pos += 8;
265 
266 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
267 			// skip delete field
268 			tag_index      += 4;
269 			next_write_pos += 4;
270 #endif
271 			// copy value
272 			int bytes_to_copy = tag_len;
273 			uint8_t copy_buffer[32];
274 			while (bytes_to_copy){
275 				int bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer));
276 				btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
277 				btstack_tlv_flash_bank_write(self, next_bank, next_write_pos, copy_buffer, bytes_this_iteration);
278 				tag_index      += bytes_this_iteration;
279 				next_write_pos += bytes_this_iteration;
280 				bytes_to_copy  -= bytes_this_iteration;
281 			}
282 		}
283 		tlv_iterator_fetch_next(self, &it);
284 	}
285 
286 	// prepare new one
287 	uint8_t epoch_buffer;
288 	btstack_tlv_flash_bank_read(self, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1);
289 	btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3);
290 	self->current_bank = next_bank;
291 	self->write_offset = next_write_pos;
292 }
293 
294 static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self, uint32_t tag, uint32_t offset){
295 	tlv_iterator_t it;
296 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
297 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it) && it.offset < offset){
298 		if (it.tag == tag){
299 			log_info("Erase tag '%x' at position %u", tag, it.offset);
300 
301 			// mark entry as invalid
302 			uint32_t zero_value = 0;
303 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
304 			// write delete field at offset 8
305 			btstack_tlv_flash_bank_write(self, self->current_bank, it.offset+8, (uint8_t*) &zero_value, sizeof(zero_value));
306 #else
307 			// overwrite tag with zero value
308 			btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_value, sizeof(zero_value));
309 #endif
310 
311 		}
312 		tlv_iterator_fetch_next(self, &it);
313 	}
314 }
315 
316 /**
317  * Get Value for Tag
318  * @param tag
319  * @param buffer
320  * @param buffer_size
321  * @returns size of value
322  */
323 static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
324 
325 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
326 
327 	uint32_t tag_index = 0;
328 	uint32_t tag_len   = 0;
329 	tlv_iterator_t it;
330 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
331 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
332 		if (it.tag == tag){
333 			log_info("Found tag '%x' at position %u", tag, it.offset);
334 			tag_index = it.offset;
335 			tag_len   = it.len;
336 			break;
337 		}
338 		tlv_iterator_fetch_next(self, &it);
339 	}
340 	if (tag_index == 0) return 0;
341 	if (!buffer) return tag_len;
342 	int copy_size = btstack_min(buffer_size, tag_len);
343 	uint32_t value_offset = tag_index + 8;
344 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
345 	// skip delete field
346 	value_offset += 4;
347 #endif
348 	btstack_tlv_flash_bank_read(self, self->current_bank, value_offset, buffer, copy_size);
349 	return copy_size;
350 }
351 
352 /**
353  * Store Tag
354  * @param tag
355  * @param data
356  * @param data_size
357  */
358 static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
359 
360 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
361 
362 	// trigger migration if not enough space
363 	if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
364 		btstack_tlv_flash_bank_migrate(self);
365 	}
366 
367 	if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
368 		log_error("couldn't write entry, not enough space left");
369 		return 2;
370 	}
371 
372 	// prepare entry
373 	uint8_t entry[8];
374 	big_endian_store_32(entry, 0, tag);
375 	big_endian_store_32(entry, 4, data_size);
376 
377 	log_info("write '%x', len %u at %u", tag, data_size, self->write_offset);
378 
379 	uint32_t value_offset = self->write_offset + 8;
380 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
381 	// skip delete field
382 	value_offset += 4;
383 #endif
384 
385 	// write value first
386 	btstack_tlv_flash_bank_write(self, self->current_bank, value_offset, data, data_size);
387 
388 	// then entry
389 	btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset, entry, sizeof(entry));
390 
391 	// overwrite old entries (if exists)
392 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
393 
394 	// done
395 	self->write_offset += sizeof(entry) + btstack_tlv_flash_bank_align_size(self, data_size);
396 
397 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
398 	// skip delete field
399 	self->write_offset += 4;
400 #endif
401 
402 	return 0;
403 }
404 
405 /**
406  * Delete Tag
407  * @param tag
408  */
409 static void btstack_tlv_flash_bank_delete_tag(void * context, uint32_t tag){
410 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
411 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
412 }
413 
414 static const btstack_tlv_t btstack_tlv_flash_bank = {
415 	/* int  (*get_tag)(..);     */ &btstack_tlv_flash_bank_get_tag,
416 	/* int (*store_tag)(..);    */ &btstack_tlv_flash_bank_store_tag,
417 	/* void (*delete_tag)(v..); */ &btstack_tlv_flash_bank_delete_tag,
418 };
419 
420 /**
421  * Init Tag Length Value Store
422  */
423 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){
424 
425 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
426 	if (hal_flash_bank_impl->get_alignment(hal_flash_bank_context) > 4){
427 		log_error("Flash alignment > 4 with ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD not supported");
428 		return NULL;
429 	}
430 #endif
431 
432 	self->hal_flash_bank_impl    = hal_flash_bank_impl;
433 	self->hal_flash_bank_context = hal_flash_bank_context;
434 
435 	// try to find current bank
436 	self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self);
437 	log_info("found bank %d", self->current_bank);
438 	if (self->current_bank >= 0){
439 
440 		// find last entry and write offset
441 		tlv_iterator_t it;
442 		uint32_t last_tag = 0;
443 		uint32_t last_offset = 0;
444 		btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
445 		while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
446 			last_tag = it.tag;
447 			last_offset = it.offset;
448 			tlv_iterator_fetch_next(self, &it);
449 		}
450 		self->write_offset = it.offset;
451 
452 		if (self->write_offset < self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
453 
454 			// delete older instances of last_tag
455 			// this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete
456 			if (last_tag){
457 				btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset);
458 			}
459 
460 			// verify that rest of bank is empty
461 			// this handles the unlikely case where MCU did reset after new value was written, but not the tag
462 			if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){
463 				log_info("Flash not empty after last found tag -> migrate");
464 				btstack_tlv_flash_bank_migrate(self);
465 			} else {
466 				log_info("Flash clean after last found tag");
467 			}
468 		} else {
469 			// failure!
470 			self->current_bank = -1;
471 		}
472 	}
473 
474 	if (self->current_bank < 0) {
475 		btstack_tlv_flash_bank_erase_bank(self, 0);
476 		self->current_bank = 0;
477 		btstack_tlv_flash_bank_write_header(self, self->current_bank, 0);	// epoch = 0;
478 		self->write_offset = 8;
479 	}
480 
481 	log_info("write offset %u", self->write_offset);
482 	return &btstack_tlv_flash_bank;
483 }
484 
485