xref: /btstack/platform/embedded/btstack_tlv_flash_bank.c (revision cd5f23a3250874824c01a2b3326a9522fea3f99f)
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, (unsigned int) 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", (unsigned int) offset, (unsigned int) (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",
257                 (unsigned  int)  tag_index, (unsigned int)  it.tag, (unsigned int) 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      += self->delete_tag_len;
269 			next_write_pos += self->delete_tag_len;
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", (unsigned int) tag, (unsigned int) 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", (unsigned int) tag, (unsigned int) 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 += self->delete_tag_len;
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 	uint32_t required_space = 8 + self->delete_tag_len + data_size;
364 	if (self->write_offset + required_space > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
365 		btstack_tlv_flash_bank_migrate(self);
366 	}
367 
368 	if (self->write_offset + required_space > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
369 		log_error("couldn't write entry, not enough space left");
370 		return 2;
371 	}
372 
373 	// prepare entry
374 	uint8_t entry[8];
375 	big_endian_store_32(entry, 0, tag);
376 	big_endian_store_32(entry, 4, data_size);
377 
378 	log_info("write '%x', len %u at %u", (unsigned int) tag, (unsigned int) data_size, self->write_offset);
379 
380 	uint32_t value_offset = self->write_offset + 8;
381 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
382 	// skip delete field
383 	value_offset += self->delete_tag_len;
384 #endif
385 
386 	// write value first
387 	btstack_tlv_flash_bank_write(self, self->current_bank, value_offset, data, data_size);
388 
389 	// then entry
390 	btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset, entry, sizeof(entry));
391 
392 	// overwrite old entries (if exists)
393 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
394 
395 	// done
396 	self->write_offset += sizeof(entry) + btstack_tlv_flash_bank_align_size(self, data_size);
397 
398 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
399 	// skip delete field
400 	self->write_offset += self->delete_tag_len;
401 #endif
402 
403 	return 0;
404 }
405 
406 /**
407  * Delete Tag
408  * @param tag
409  */
410 static void btstack_tlv_flash_bank_delete_tag(void * context, uint32_t tag){
411 	btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context;
412 	btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset);
413 }
414 
415 static const btstack_tlv_t btstack_tlv_flash_bank = {
416 	/* int  (*get_tag)(..);     */ &btstack_tlv_flash_bank_get_tag,
417 	/* int (*store_tag)(..);    */ &btstack_tlv_flash_bank_store_tag,
418 	/* void (*delete_tag)(v..); */ &btstack_tlv_flash_bank_delete_tag,
419 };
420 
421 /**
422  * Init Tag Length Value Store
423  */
424 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){
425 
426 	self->hal_flash_bank_impl    = hal_flash_bank_impl;
427 	self->hal_flash_bank_context = hal_flash_bank_context;
428 	self->delete_tag_len = 0;
429 
430 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD
431 	if (hal_flash_bank_impl->get_alignment(hal_flash_bank_context) > 8){
432 		log_error("Flash alignment > 8 with ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD not supported");
433 		return NULL;
434 	}
435 	// set delete tag len
436 	uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context);
437 	self->delete_tag_len = (uint8_t) btstack_max(4, aligment);
438 	log_info("delete tag len %u", self->delete_tag_len);
439 #endif
440 
441 	// try to find current bank
442 	self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self);
443 	log_info("found bank %d", self->current_bank);
444 	if (self->current_bank >= 0){
445 
446 		// find last entry and write offset
447 		tlv_iterator_t it;
448 		uint32_t last_tag = 0;
449 		uint32_t last_offset = 0;
450 		btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank);
451 		while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){
452 			last_tag = it.tag;
453 			last_offset = it.offset;
454 			tlv_iterator_fetch_next(self, &it);
455 		}
456 		self->write_offset = it.offset;
457 
458 		if (self->write_offset < self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){
459 
460 			// delete older instances of last_tag
461 			// this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete
462 			if (last_tag){
463 				btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset);
464 			}
465 
466 			// verify that rest of bank is empty
467 			// this handles the unlikely case where MCU did reset after new value was written, but not the tag
468 			if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){
469 				log_info("Flash not empty after last found tag -> migrate");
470 				btstack_tlv_flash_bank_migrate(self);
471 			} else {
472 				log_info("Flash clean after last found tag");
473 			}
474 		} else {
475 			// failure!
476 			self->current_bank = -1;
477 		}
478 	}
479 
480 	if (self->current_bank < 0) {
481 		btstack_tlv_flash_bank_erase_bank(self, 0);
482 		self->current_bank = 0;
483 		btstack_tlv_flash_bank_write_header(self, self->current_bank, 0);	// epoch = 0;
484 		self->write_offset = 8;
485 	}
486 
487 	log_info("write offset %u", self->write_offset);
488 	return &btstack_tlv_flash_bank;
489 }
490 
491