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 BLUEKITCHEN GMBH 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 BLUEKITCHEN 21 * GMBH 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 #include <inttypes.h> 42 43 // Header: 44 // - Magic: 'BTstack' 45 // - Status: 46 // - bits 765432: reserved 47 // - bits 10: epoch 48 49 // Entries 50 // - Tag: 32 bit 51 // - Len: 32 bit 52 // - Delete: 32 delete field - only used with ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD 53 // - Value: Len in bytes 54 55 // ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD 56 // 57 // Most Flash implementations allow to: 58 // - erase sector -> all values are 0xff 59 // - write value (1s -> 0s) 60 // - overwrite value with zero (remaining 1s -> 0s) 61 // 62 // We use the ability to overwrite a value with zeros to mark deleted enttries (by writing zero into the tag field). 63 // Some targets, E.g. Kinetix K64F, do enot allow for that. 64 // 65 // With ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD an extra field is reserved to indicate a deleted tag, while keeping main logic 66 // 67 // With ENABLE_TLV_FLASH_WRITE_ONCE, tags are never marked as deleted. Instead, an emtpy tag will be written instead. 68 // Also, lookup and migrate requires to always search until the end of the valid bank 69 70 #if defined (ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD) && defined (ENABLE_TLV_FLASH_WRITE_ONCE) 71 #error "Please define either ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD or ENABLE_TLV_FLASH_WRITE_ONCE" 72 #endif 73 74 #define BTSTACK_TLV_HEADER_LEN 8 75 76 #ifndef BTSTACK_FLASH_ALIGNMENT_MAX 77 #define BTSTACK_FLASH_ALIGNMENT_MAX 8 78 #endif 79 80 static const char * btstack_tlv_header_magic = "BTstack"; 81 82 // TLV Iterator 83 typedef struct { 84 int bank; 85 uint32_t offset; 86 uint32_t tag; 87 uint32_t len; 88 } tlv_iterator_t; 89 90 static uint32_t btstack_tlv_flash_bank_align_size(btstack_tlv_flash_bank_t * self, uint32_t size){ 91 uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context); 92 return (size + aligment - 1) & ~(aligment - 1); 93 } 94 95 // support unaligned flash read/writes 96 // strategy: increase size to meet alignment, perform unaligned read/write of last chunk with helper buffer 97 98 static void btstack_tlv_flash_bank_read(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){ 99 100 // read main data 101 uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context); 102 uint32_t lower_bits = size & (aligment - 1); 103 uint32_t size_aligned = size - lower_bits; 104 if (size_aligned){ 105 self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, size_aligned); 106 buffer += size_aligned; 107 offset += size_aligned; 108 size -= size_aligned; 109 } 110 111 // read last part 112 if (size == 0) return; 113 uint8_t aligment_block[BTSTACK_FLASH_ALIGNMENT_MAX]; 114 self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, aligment_block, aligment); 115 uint32_t bytes_to_copy = btstack_min(aligment - lower_bits, size); 116 memcpy(buffer, aligment_block, bytes_to_copy); 117 } 118 119 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){ 120 121 // write main data 122 uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context); 123 uint32_t lower_bits = size & (aligment - 1); 124 uint32_t size_aligned = size - lower_bits; 125 if (size_aligned){ 126 self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, buffer, size_aligned); 127 buffer += size_aligned; 128 offset += size_aligned; 129 size -= size_aligned; 130 } 131 132 // write last part 133 if (size == 0) return; 134 uint8_t aligment_block[BTSTACK_FLASH_ALIGNMENT_MAX]; 135 memset(aligment_block, 0xff, aligment); 136 memcpy(aligment_block, buffer, lower_bits); 137 self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, aligment_block, aligment); 138 } 139 140 141 // iterator 142 143 static void btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){ 144 uint8_t entry[8]; 145 btstack_tlv_flash_bank_read(self, it->bank, it->offset, entry, 8); 146 it->tag = big_endian_read_32(entry, 0); 147 it->len = big_endian_read_32(entry, 4); 148 149 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD 150 // clear tag, if delete field is set 151 uint32_t delete_tag; 152 btstack_tlv_flash_bank_read(self, it->bank, it->offset + 8, (uint8_t *) &delete_tag, 4); 153 if (delete_tag == 0){ 154 it->tag = 0; 155 } 156 #endif 157 } 158 159 static void btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it, int bank){ 160 memset(it, 0, sizeof(tlv_iterator_t)); 161 it->bank = bank; 162 it->offset = BTSTACK_TLV_HEADER_LEN; 163 btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it); 164 } 165 166 static int btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){ 167 UNUSED(self); 168 if (it->tag == 0xffffffff) return 0; 169 return 1; 170 } 171 172 static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){ 173 it->offset += 8 + btstack_tlv_flash_bank_align_size(self, it->len); 174 175 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD 176 // skip delete field 177 it->offset += self->delete_tag_len; 178 #endif 179 180 if (it->offset >= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)) { 181 it->tag = 0xffffffff; 182 it->len = 0; 183 return; 184 } 185 btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it); 186 } 187 188 // 189 190 // check both banks for headers and pick the one with the higher epoch % 4 191 // @returns bank or -1 if something is invalid 192 static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){ 193 uint8_t header0[BTSTACK_TLV_HEADER_LEN]; 194 uint8_t header1[BTSTACK_TLV_HEADER_LEN]; 195 btstack_tlv_flash_bank_read(self, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN); 196 btstack_tlv_flash_bank_read(self, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN); 197 int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0; 198 int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0; 199 if (!valid0 && !valid1) return -1; 200 if ( valid0 && !valid1) return 0; 201 if (!valid0 && valid1) return 1; 202 int epoch0 = header0[BTSTACK_TLV_HEADER_LEN-1] & 0x03; 203 int epoch1 = header1[BTSTACK_TLV_HEADER_LEN-1] & 0x03; 204 if (epoch0 == ((epoch1 + 1) & 0x03)) return 0; 205 if (epoch1 == ((epoch0 + 1) & 0x03)) return 1; 206 return -1; // invalid, must not happen 207 } 208 209 static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self, int bank, int epoch){ 210 uint8_t header[BTSTACK_TLV_HEADER_LEN]; 211 memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1); 212 header[BTSTACK_TLV_HEADER_LEN-1] = epoch; 213 btstack_tlv_flash_bank_write(self, bank, 0, header, BTSTACK_TLV_HEADER_LEN); 214 } 215 216 /** 217 * @brief Check if erased from offset 218 */ 219 static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset){ 220 log_info("test erased: bank %u, offset %u", bank, (unsigned int) offset); 221 uint32_t size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context); 222 uint8_t buffer[16]; 223 uint8_t empty16[16]; 224 memset(empty16, 0xff, sizeof(empty16)); 225 while (offset < size){ 226 uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset); 227 btstack_tlv_flash_bank_read(self, bank, offset, buffer, copy_size); 228 if (memcmp(buffer, empty16, copy_size)) { 229 log_info("not erased %x - %x", (unsigned int) offset, (unsigned int) (offset + copy_size)); 230 return 0; 231 } 232 offset += copy_size; 233 } 234 return 1; 235 } 236 237 /** 238 * @brief erase bank (only if not already erased) 239 */ 240 static void btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self, int bank){ 241 if (btstack_tlv_flash_bank_test_erased(self, bank, 0)){ 242 log_info("bank %u already erased", bank); 243 } else { 244 log_info("bank %u not empty, erase bank", bank); 245 self->hal_flash_bank_impl->erase(self->hal_flash_bank_context, bank); 246 } 247 } 248 249 static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){ 250 251 int next_bank = 1 - self->current_bank; 252 log_info("migrate bank %u -> bank %u", self->current_bank, next_bank); 253 // erase bank (if needed) 254 btstack_tlv_flash_bank_erase_bank(self, next_bank); 255 int next_write_pos = 8; 256 257 tlv_iterator_t it; 258 btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank); 259 while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){ 260 // skip deleted entries 261 if (it.tag) { 262 uint32_t tag_len = it.len; 263 uint32_t tag_index = it.offset; 264 265 bool tag_valid = true; 266 267 #ifdef ENABLE_TLV_FLASH_WRITE_ONCE 268 // search until end for newer entry of same tag 269 tlv_iterator_t it2; 270 memcpy(&it2, &it, sizeof(tlv_iterator_t)); 271 while (btstack_tlv_flash_bank_iterator_has_next(self, &it2)){ 272 if ((it2.offset != it.offset) && (it2.tag == it.tag)){ 273 tag_valid = false; 274 break; 275 } 276 tlv_iterator_fetch_next(self, &it2); 277 } 278 if (tag_valid == false){ 279 log_info("skip pos %u, tag '%x' as newer entry found at %u", (unsigned int) tag_index, (unsigned int) it.tag, 280 (unsigned int) it2.offset); 281 } 282 #endif 283 284 if (tag_valid) { 285 286 log_info("migrate pos %u, tag '%x' len %u -> new pos %u", 287 (unsigned int) tag_index, (unsigned int) it.tag, (unsigned int) tag_len, next_write_pos); 288 289 // copy header 290 uint8_t header_buffer[8]; 291 btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, header_buffer, 8); 292 btstack_tlv_flash_bank_write(self, next_bank, next_write_pos, header_buffer, 8); 293 tag_index += 8; 294 next_write_pos += 8; 295 296 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD 297 // skip delete field 298 tag_index += self->delete_tag_len; 299 next_write_pos += self->delete_tag_len; 300 #endif 301 // copy value 302 int bytes_to_copy = tag_len; 303 uint8_t copy_buffer[32]; 304 while (bytes_to_copy) { 305 int bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer)); 306 btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, copy_buffer, bytes_this_iteration); 307 btstack_tlv_flash_bank_write(self, next_bank, next_write_pos, copy_buffer, bytes_this_iteration); 308 tag_index += bytes_this_iteration; 309 next_write_pos += bytes_this_iteration; 310 bytes_to_copy -= bytes_this_iteration; 311 } 312 } 313 } 314 tlv_iterator_fetch_next(self, &it); 315 } 316 317 // prepare new one 318 uint8_t epoch_buffer; 319 btstack_tlv_flash_bank_read(self, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1); 320 btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3); 321 self->current_bank = next_bank; 322 self->write_offset = next_write_pos; 323 } 324 325 #ifndef ENABLE_TLV_FLASH_WRITE_ONCE 326 static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self, uint32_t tag, uint32_t offset){ 327 tlv_iterator_t it; 328 btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank); 329 while (btstack_tlv_flash_bank_iterator_has_next(self, &it) && it.offset < offset){ 330 if (it.tag == tag){ 331 log_info("Erase tag '%x' at position %u", (unsigned int) tag, (unsigned int) it.offset); 332 333 // mark entry as invalid 334 uint32_t zero_value = 0; 335 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD 336 // write delete field at offset 8 337 btstack_tlv_flash_bank_write(self, self->current_bank, it.offset+8, (uint8_t*) &zero_value, sizeof(zero_value)); 338 #else 339 // overwrite tag with zero value 340 btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_value, sizeof(zero_value)); 341 #endif 342 343 } 344 tlv_iterator_fetch_next(self, &it); 345 } 346 } 347 #endif 348 349 /** 350 * Get Value for Tag 351 * @param tag 352 * @param buffer 353 * @param buffer_size 354 * @returns size of value 355 */ 356 static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){ 357 358 btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context; 359 360 uint32_t tag_index = 0; 361 uint32_t tag_len = 0; 362 tlv_iterator_t it; 363 btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank); 364 while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){ 365 if (it.tag == tag){ 366 log_info("Found tag '%x' at position %u", (unsigned int) tag, (unsigned int) it.offset); 367 tag_index = it.offset; 368 tag_len = it.len; 369 #ifndef ENABLE_TLV_FLASH_WRITE_ONCE 370 break; 371 #endif 372 } 373 tlv_iterator_fetch_next(self, &it); 374 } 375 if (tag_index == 0) return 0; 376 if (!buffer) return tag_len; 377 int copy_size = btstack_min(buffer_size, tag_len); 378 uint32_t value_offset = tag_index + 8; 379 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD 380 // skip delete field 381 value_offset += self->delete_tag_len; 382 #endif 383 btstack_tlv_flash_bank_read(self, self->current_bank, value_offset, buffer, copy_size); 384 return copy_size; 385 } 386 387 /** 388 * Store Tag 389 * @param tag 390 * @param data 391 * @param data_size 392 */ 393 static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){ 394 395 btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context; 396 397 // trigger migration if not enough space 398 uint32_t required_space = 8 + self->delete_tag_len + data_size; 399 if (self->write_offset + required_space > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){ 400 btstack_tlv_flash_bank_migrate(self); 401 } 402 403 if (self->write_offset + required_space > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){ 404 log_error("couldn't write entry, not enough space left"); 405 return 2; 406 } 407 408 // prepare entry 409 uint8_t entry[8]; 410 big_endian_store_32(entry, 0, tag); 411 big_endian_store_32(entry, 4, data_size); 412 413 log_info("write '%" PRIx32 "', len %" PRIu32 " at %" PRIx32, tag, data_size, self->write_offset); 414 415 uint32_t value_offset = self->write_offset + 8; 416 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD 417 // skip delete field 418 value_offset += self->delete_tag_len; 419 #endif 420 421 // write value first 422 btstack_tlv_flash_bank_write(self, self->current_bank, value_offset, data, data_size); 423 424 // then entry 425 btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset, entry, sizeof(entry)); 426 427 #ifndef ENABLE_TLV_FLASH_WRITE_ONCE 428 // overwrite old entries (if exists) 429 btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset); 430 #endif 431 432 // done 433 self->write_offset += sizeof(entry) + btstack_tlv_flash_bank_align_size(self, data_size); 434 435 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD 436 // skip delete field 437 self->write_offset += self->delete_tag_len; 438 #endif 439 440 return 0; 441 } 442 443 /** 444 * Delete Tag 445 * @param tag 446 */ 447 static void btstack_tlv_flash_bank_delete_tag(void * context, uint32_t tag){ 448 #ifdef ENABLE_TLV_FLASH_WRITE_ONCE 449 btstack_tlv_flash_bank_store_tag(context, tag, NULL, 0); 450 #else 451 btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context; 452 btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset); 453 #endif 454 } 455 456 static const btstack_tlv_t btstack_tlv_flash_bank = { 457 /* int (*get_tag)(..); */ &btstack_tlv_flash_bank_get_tag, 458 /* int (*store_tag)(..); */ &btstack_tlv_flash_bank_store_tag, 459 /* void (*delete_tag)(v..); */ &btstack_tlv_flash_bank_delete_tag, 460 }; 461 462 /** 463 * Init Tag Length Value Store 464 */ 465 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){ 466 467 self->hal_flash_bank_impl = hal_flash_bank_impl; 468 self->hal_flash_bank_context = hal_flash_bank_context; 469 self->delete_tag_len = 0; 470 471 #ifdef ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD 472 if (hal_flash_bank_impl->get_alignment(hal_flash_bank_context) > 8){ 473 log_error("Flash alignment > 8 with ENABLE_TLV_FLASH_EXPLICIT_DELETE_FIELD not supported"); 474 return NULL; 475 } 476 // set delete tag len 477 uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context); 478 self->delete_tag_len = (uint8_t) btstack_max(4, aligment); 479 log_info("delete tag len %u", self->delete_tag_len); 480 #endif 481 482 // try to find current bank 483 self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self); 484 log_info("found bank %d", self->current_bank); 485 if (self->current_bank >= 0){ 486 487 // find last entry and write offset 488 tlv_iterator_t it; 489 #ifndef ENABLE_TLV_FLASH_WRITE_ONCE 490 uint32_t last_tag = 0; 491 uint32_t last_offset = 0; 492 #endif 493 btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank); 494 while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){ 495 #ifndef ENABLE_TLV_FLASH_WRITE_ONCE 496 last_tag = it.tag; 497 last_offset = it.offset; 498 #endif 499 tlv_iterator_fetch_next(self, &it); 500 } 501 self->write_offset = it.offset; 502 503 if (self->write_offset < self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){ 504 505 #ifndef ENABLE_TLV_FLASH_WRITE_ONCE 506 // delete older instances of last_tag 507 // this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete 508 if (last_tag){ 509 btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset); 510 } 511 #endif 512 513 // verify that rest of bank is empty 514 // this handles the unlikely case where MCU did reset after new value was written, but not the tag 515 if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){ 516 log_info("Flash not empty after last found tag -> migrate"); 517 btstack_tlv_flash_bank_migrate(self); 518 } else { 519 log_info("Flash clean after last found tag"); 520 } 521 } else { 522 // failure! 523 self->current_bank = -1; 524 } 525 } 526 527 if (self->current_bank < 0) { 528 btstack_tlv_flash_bank_erase_bank(self, 0); 529 self->current_bank = 0; 530 btstack_tlv_flash_bank_write_header(self, self->current_bank, 0); // epoch = 0; 531 self->write_offset = 8; 532 } 533 534 log_info("write offset %" PRIx32, self->write_offset); 535 return &btstack_tlv_flash_bank; 536 } 537 538