xref: /btstack/platform/embedded/btstack_tlv_flash_bank.c (revision 046b44372d25c7bd6fe78e5cf6e5176a1979f437)
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 #endif
149 }
150 
151 static void btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it, int bank){
152 	memset(it, 0, sizeof(tlv_iterator_t));
153 	it->bank = bank;
154 	it->offset = BTSTACK_TLV_HEADER_LEN;
155 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
156 }
157 
158 static int btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
159 	if (it->tag == 0xffffffff) return 0;
160 	return 1;
161 }
162 
163 static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){
164 	it->offset += 8 + btstack_tlv_flash_bank_align_size(self, it->len);
165 
166 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
167 	// skip delete field
168 	it->offset += self->delete_tag_len;
169 #endif
170 
171 	if (it->offset >= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)) {
172 		it->tag = 0xffffffff;
173 		it->len = 0;
174 		return;
175 	}
176 	btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it);
177 }
178 
179 //
180 
181 // check both banks for headers and pick the one with the higher epoch % 4
182 // @returns bank or -1 if something is invalid
183 static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){
184  	uint8_t header0[BTSTACK_TLV_HEADER_LEN];
185  	uint8_t header1[BTSTACK_TLV_HEADER_LEN];
186  	btstack_tlv_flash_bank_read(self, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN);
187  	btstack_tlv_flash_bank_read(self, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN);
188  	int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
189  	int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0;
190 	if (!valid0 && !valid1) return -1;
191 	if ( valid0 && !valid1) return 0;
192 	if (!valid0 &&  valid1) return 1;
193 	int epoch0 = header0[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
194 	int epoch1 = header1[BTSTACK_TLV_HEADER_LEN-1] & 0x03;
195 	if (epoch0 == ((epoch1 + 1) & 0x03)) return 0;
196 	if (epoch1 == ((epoch0 + 1) & 0x03)) return 1;
197 	return -1;	// invalid, must not happen
198 }
199 
200 static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self, int bank, int epoch){
201 	uint8_t header[BTSTACK_TLV_HEADER_LEN];
202 	memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1);
203 	header[BTSTACK_TLV_HEADER_LEN-1] = epoch;
204 	btstack_tlv_flash_bank_write(self, bank, 0, header, BTSTACK_TLV_HEADER_LEN);
205 }
206 
207 /**
208  * @brief Check if erased from offset
209  */
210 static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset){
211 	log_info("test erased: bank %u, offset %u", bank, offset);
212 	uint32_t size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context);
213 	uint8_t buffer[16];
214 	uint8_t empty16[16];
215 	memset(empty16, 0xff, sizeof(empty16));
216 	while (offset < size){
217 		uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset);
218 		btstack_tlv_flash_bank_read(self, bank, offset, buffer, copy_size);
219 		if (memcmp(buffer, empty16, copy_size)) {
220 			log_info("not erased %x - %x", offset, offset + copy_size);
221 			return 0;
222 		}
223 		offset += copy_size;
224 	}
225 	return 1;
226 }
227 
228 /**
229  * @brief erase bank (only if not already erased)
230  */
231 static void btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self, int bank){
232 	if (btstack_tlv_flash_bank_test_erased(self, bank, 0)){
233 		log_info("bank %u already erased", bank);
234 	} else {
235 		log_info("bank %u not empty, erase bank", bank);
236 		self->hal_flash_bank_impl->erase(self->hal_flash_bank_context, bank);
237 	}
238 }
239 
240 static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){
241 
242 	int next_bank = 1 - self->current_bank;
243 	log_info("migrate bank %u -> bank %u", self->current_bank, next_bank);
244 	// erase bank (if needed)
245 	btstack_tlv_flash_bank_erase_bank(self, next_bank);
246 	int next_write_pos = 8;
247 
248 	tlv_iterator_t it;
249 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
250 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
251 		// skip deleted entries
252 		if (it.tag) {
253 			uint32_t tag_len = it.len;
254 			uint32_t tag_index = it.offset;
255 
256 			log_info("migrate pos %u, tag '%x' len %u -> new pos %u", tag_index, it.tag, tag_len, next_write_pos);
257 
258 			// copy header
259 			uint8_t header_buffer[8];
260 			btstack_tlv_flash_bank_read(self, self->current_bank, tag_index,      header_buffer, 8);
261 			btstack_tlv_flash_bank_write(self, next_bank,         next_write_pos, header_buffer, 8);
262 			tag_index      += 8;
263 			next_write_pos += 8;
264 
265 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
266 			// skip delete field
267 			tag_index      += self->delete_tag_len;
268 			next_write_pos += self->delete_tag_len;
269 #endif
270 			// copy value
271 			int bytes_to_copy = tag_len;
272 			uint8_t copy_buffer[32];
273 			while (bytes_to_copy){
274 				int bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer));
275 				btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, copy_buffer, bytes_this_iteration);
276 				btstack_tlv_flash_bank_write(self, next_bank, next_write_pos, copy_buffer, bytes_this_iteration);
277 				tag_index      += bytes_this_iteration;
278 				next_write_pos += bytes_this_iteration;
279 				bytes_to_copy  -= bytes_this_iteration;
280 			}
281 		}
282 		tlv_iterator_fetch_next(self, &it);
283 	}
284 
285 	// prepare new one
286 	uint8_t epoch_buffer;
287 	btstack_tlv_flash_bank_read(self, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1);
288 	btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3);
289 	self->current_bank = next_bank;
290 	self->write_offset = next_write_pos;
291 }
292 
293 static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self, uint32_t tag, uint32_t offset){
294 	tlv_iterator_t it;
295 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
296 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it) && it.offset < offset){
297 		if (it.tag == tag){
298 			log_info("Erase tag '%x' at position %u", tag, it.offset);
299 
300 			// mark entry as invalid
301 			uint32_t zero_value = 0;
302 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
303 			// write delete field at offset 8
304 			btstack_tlv_flash_bank_write(self, self->current_bank, it.offset+8, (uint8_t*) &zero_value, sizeof(zero_value));
305 #else
306 			// overwrite tag with zero value
307 			btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_value, sizeof(zero_value));
308 #endif
309 
310 		}
311 		tlv_iterator_fetch_next(self, &it);
312 	}
313 }
314 
315 /**
316  * Get Value for Tag
317  * @param tag
318  * @param buffer
319  * @param buffer_size
320  * @returns size of value
321  */
322 static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){
323 
324 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
325 
326 	uint32_t tag_index = 0;
327 	uint32_t tag_len   = 0;
328 	tlv_iterator_t it;
329 	btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
330 	while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
331 		if (it.tag == tag){
332 			log_info("Found tag '%x' at position %u", tag, it.offset);
333 			tag_index = it.offset;
334 			tag_len   = it.len;
335 			break;
336 		}
337 		tlv_iterator_fetch_next(self, &it);
338 	}
339 	if (tag_index == 0) return 0;
340 	if (!buffer) return tag_len;
341 	int copy_size = btstack_min(buffer_size, tag_len);
342 	uint32_t value_offset = tag_index + 8;
343 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
344 	// skip delete field
345 	value_offset += self->delete_tag_len;
346 #endif
347 	btstack_tlv_flash_bank_read(self, self->current_bank, value_offset, buffer, copy_size);
348 	return copy_size;
349 }
350 
351 /**
352  * Store Tag
353  * @param tag
354  * @param data
355  * @param data_size
356  */
357 static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){
358 
359 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
360 
361 	// trigger migration if not enough space
362 	uint32_t required_space = 8 + self->delete_tag_len + data_size;
363 	if (self->write_offset + required_space > 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 + required_space > 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 += self->delete_tag_len;
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 += self->delete_tag_len;
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 	self->hal_flash_bank_impl    = hal_flash_bank_impl;
426 	self->hal_flash_bank_context = hal_flash_bank_context;
427 	self->delete_tag_len = 0;
428 
429 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
430 	if (hal_flash_bank_impl->get_alignment(hal_flash_bank_context) > 8){
431 		log_error("Flash alignment > 8 with ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD not supported");
432 		return NULL;
433 	}
434 	// set delete tag len
435 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
436 	self->delete_tag_len = (uint8_t) btstack_max(4, aligment);
437 	log_info("delete tag len %u", self->delete_tag_len);
438 #endif
439 
440 	// try to find current bank
441 	self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self);
442 	log_info("found bank %d", self->current_bank);
443 	if (self->current_bank >= 0){
444 
445 		// find last entry and write offset
446 		tlv_iterator_t it;
447 		uint32_t last_tag = 0;
448 		uint32_t last_offset = 0;
449 		btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
450 		while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
451 			last_tag = it.tag;
452 			last_offset = it.offset;
453 			tlv_iterator_fetch_next(self, &it);
454 		}
455 		self->write_offset = it.offset;
456 
457 		if (self->write_offset < self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
458 
459 			// delete older instances of last_tag
460 			// this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete
461 			if (last_tag){
462 				btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset);
463 			}
464 
465 			// verify that rest of bank is empty
466 			// this handles the unlikely case where MCU did reset after new value was written, but not the tag
467 			if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){
468 				log_info("Flash not empty after last found tag -> migrate");
469 				btstack_tlv_flash_bank_migrate(self);
470 			} else {
471 				log_info("Flash clean after last found tag");
472 			}
473 		} else {
474 			// failure!
475 			self->current_bank = -1;
476 		}
477 	}
478 
479 	if (self->current_bank < 0) {
480 		btstack_tlv_flash_bank_erase_bank(self, 0);
481 		self->current_bank = 0;
482 		btstack_tlv_flash_bank_write_header(self, self->current_bank, 0);	// epoch = 0;
483 		self->write_offset = 8;
484 	}
485 
486 	log_info("write offset %u", self->write_offset);
487 	return &btstack_tlv_flash_bank;
488 }
489 
490