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