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 // - Value: Len in bytes 52 53 #define BTSTACK_TLV_HEADER_LEN 8 54 55 #ifndef BTSTACK_FLASH_ALIGNMENT_MAX 56 #define BTSTACK_FLASH_ALIGNMENT_MAX 8 57 #endif 58 59 static const char * btstack_tlv_header_magic = "BTstack"; 60 61 // TLV Iterator 62 typedef struct { 63 int bank; 64 uint32_t offset; 65 uint32_t tag; 66 uint32_t len; 67 } tlv_iterator_t; 68 69 static uint32_t btstack_tlv_flash_bank_align_size(btstack_tlv_flash_bank_t * self, uint32_t size){ 70 uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context); 71 return (size + aligment - 1) & ~(aligment - 1); 72 } 73 74 // support unaligned flash read/writes 75 // strategy: increase size to meet alignment, perform unaligned read/write of last chunk with helper buffer 76 77 static void btstack_tlv_flash_bank_read(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){ 78 79 // read main data 80 uint32_t aligment = self->hal_flash_bank_impl->get_alignment(self->hal_flash_bank_context); 81 uint32_t lower_bits = size & (aligment - 1); 82 uint32_t size_aligned = size - lower_bits; 83 if (size_aligned){ 84 self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, buffer, size_aligned); 85 buffer += size_aligned; 86 offset += size_aligned; 87 size -= size_aligned; 88 } 89 90 // read last part 91 if (size == 0) return; 92 uint8_t aligment_block[BTSTACK_FLASH_ALIGNMENT_MAX]; 93 self->hal_flash_bank_impl->read(self->hal_flash_bank_context, bank, offset, aligment_block, aligment); 94 uint32_t bytes_to_copy = btstack_min(aligment - lower_bits, size); 95 memcpy(buffer, aligment_block, bytes_to_copy); 96 } 97 98 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){ 99 100 // write 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->write(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 // write last part 112 if (size == 0) return; 113 uint8_t aligment_block[BTSTACK_FLASH_ALIGNMENT_MAX]; 114 memset(aligment_block, 0xff, aligment); 115 memcpy(aligment_block, buffer, lower_bits); 116 self->hal_flash_bank_impl->write(self->hal_flash_bank_context, bank, offset, aligment_block, aligment); 117 } 118 119 120 // iterator 121 122 static void btstack_tlv_flash_bank_iterator_fetch_tag_len(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){ 123 uint8_t entry[8]; 124 btstack_tlv_flash_bank_read(self, it->bank, it->offset, entry, 8); 125 it->tag = big_endian_read_32(entry, 0); 126 it->len = big_endian_read_32(entry, 4); 127 } 128 129 static void btstack_tlv_flash_bank_iterator_init(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it, int bank){ 130 memset(it, 0, sizeof(tlv_iterator_t)); 131 it->bank = bank; 132 it->offset = BTSTACK_TLV_HEADER_LEN; 133 btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it); 134 } 135 136 static int btstack_tlv_flash_bank_iterator_has_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){ 137 if (it->tag == 0xffffffff) return 0; 138 return 1; 139 } 140 141 static void tlv_iterator_fetch_next(btstack_tlv_flash_bank_t * self, tlv_iterator_t * it){ 142 it->offset += 8 + btstack_tlv_flash_bank_align_size(self, it->len); 143 if (it->offset >= self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)) { 144 it->tag = 0xffffffff; 145 it->len = 0; 146 return; 147 } 148 btstack_tlv_flash_bank_iterator_fetch_tag_len(self, it); 149 } 150 151 // 152 153 // check both banks for headers and pick the one with the higher epoch % 4 154 // @returns bank or -1 if something is invalid 155 static int btstack_tlv_flash_bank_get_latest_bank(btstack_tlv_flash_bank_t * self){ 156 uint8_t header0[BTSTACK_TLV_HEADER_LEN]; 157 uint8_t header1[BTSTACK_TLV_HEADER_LEN]; 158 btstack_tlv_flash_bank_read(self, 0, 0, &header0[0], BTSTACK_TLV_HEADER_LEN); 159 btstack_tlv_flash_bank_read(self, 1, 0, &header1[0], BTSTACK_TLV_HEADER_LEN); 160 int valid0 = memcmp(header0, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0; 161 int valid1 = memcmp(header1, btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1) == 0; 162 if (!valid0 && !valid1) return -1; 163 if ( valid0 && !valid1) return 0; 164 if (!valid0 && valid1) return 1; 165 int epoch0 = header0[BTSTACK_TLV_HEADER_LEN-1] & 0x03; 166 int epoch1 = header1[BTSTACK_TLV_HEADER_LEN-1] & 0x03; 167 if (epoch0 == ((epoch1 + 1) & 0x03)) return 0; 168 if (epoch1 == ((epoch0 + 1) & 0x03)) return 1; 169 return -1; // invalid, must not happen 170 } 171 172 static void btstack_tlv_flash_bank_write_header(btstack_tlv_flash_bank_t * self, int bank, int epoch){ 173 uint8_t header[BTSTACK_TLV_HEADER_LEN]; 174 memcpy(&header[0], btstack_tlv_header_magic, BTSTACK_TLV_HEADER_LEN-1); 175 header[BTSTACK_TLV_HEADER_LEN-1] = epoch; 176 btstack_tlv_flash_bank_write(self, bank, 0, header, BTSTACK_TLV_HEADER_LEN); 177 } 178 179 /** 180 * @brief Check if erased from offset 181 */ 182 static int btstack_tlv_flash_bank_test_erased(btstack_tlv_flash_bank_t * self, int bank, uint32_t offset){ 183 log_info("test erased: bank %u, offset %u", bank, offset); 184 uint32_t size = self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context); 185 uint8_t buffer[16]; 186 uint8_t empty16[16]; 187 memset(empty16, 0xff, sizeof(empty16)); 188 while (offset < size){ 189 uint32_t copy_size = (offset + sizeof(empty16) < size) ? sizeof(empty16) : (size - offset); 190 btstack_tlv_flash_bank_read(self, bank, offset, buffer, copy_size); 191 if (memcmp(buffer, empty16, copy_size)) { 192 log_info("not erased %x - %x", offset, offset + copy_size); 193 return 0; 194 } 195 offset += copy_size; 196 } 197 return 1; 198 } 199 200 /** 201 * @brief erase bank (only if not already erased) 202 */ 203 static void btstack_tlv_flash_bank_erase_bank(btstack_tlv_flash_bank_t * self, int bank){ 204 if (btstack_tlv_flash_bank_test_erased(self, bank, 0)){ 205 log_info("bank %u already erased", bank); 206 } else { 207 log_info("bank %u not empty, erase bank", bank); 208 self->hal_flash_bank_impl->erase(self->hal_flash_bank_context, bank); 209 } 210 } 211 212 static void btstack_tlv_flash_bank_migrate(btstack_tlv_flash_bank_t * self){ 213 214 int next_bank = 1 - self->current_bank; 215 log_info("migrate bank %u -> bank %u", self->current_bank, next_bank); 216 // erase bank (if needed) 217 btstack_tlv_flash_bank_erase_bank(self, next_bank); 218 int next_write_pos = 8; 219 220 tlv_iterator_t it; 221 btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank); 222 while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){ 223 // skip deleted entries 224 if (it.tag) { 225 uint32_t tag_len = it.len; 226 uint32_t tag_index = it.offset; 227 228 // copy 229 int bytes_to_copy = 8 + tag_len; 230 log_info("migrate pos %u, tag '%x' len %u -> new pos %u", tag_index, it.tag, bytes_to_copy, next_write_pos); 231 uint8_t copy_buffer[32]; 232 while (bytes_to_copy){ 233 int bytes_this_iteration = btstack_min(bytes_to_copy, sizeof(copy_buffer)); 234 btstack_tlv_flash_bank_read(self, self->current_bank, tag_index, copy_buffer, bytes_this_iteration); 235 btstack_tlv_flash_bank_write(self, next_bank, next_write_pos, copy_buffer, bytes_this_iteration); 236 tag_index += bytes_this_iteration; 237 next_write_pos += bytes_this_iteration; 238 bytes_to_copy -= bytes_this_iteration; 239 } 240 } 241 tlv_iterator_fetch_next(self, &it); 242 } 243 244 // prepare new one 245 uint8_t epoch_buffer; 246 btstack_tlv_flash_bank_read(self, self->current_bank, BTSTACK_TLV_HEADER_LEN-1, &epoch_buffer, 1); 247 btstack_tlv_flash_bank_write_header(self, next_bank, (epoch_buffer + 1) & 3); 248 self->current_bank = next_bank; 249 self->write_offset = next_write_pos; 250 } 251 252 static void btstack_tlv_flash_bank_delete_tag_until_offset(btstack_tlv_flash_bank_t * self, uint32_t tag, uint32_t offset){ 253 tlv_iterator_t it; 254 btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank); 255 while (btstack_tlv_flash_bank_iterator_has_next(self, &it) && it.offset < offset){ 256 if (it.tag == tag){ 257 log_info("Erase tag '%x' at position %u", tag, it.offset); 258 // overwrite tag with invalid tag 259 uint32_t zero_tag = 0; 260 btstack_tlv_flash_bank_write(self, self->current_bank, it.offset, (uint8_t*) &zero_tag, sizeof(zero_tag)); 261 } 262 tlv_iterator_fetch_next(self, &it); 263 } 264 } 265 266 /** 267 * Get Value for Tag 268 * @param tag 269 * @param buffer 270 * @param buffer_size 271 * @returns size of value 272 */ 273 static int btstack_tlv_flash_bank_get_tag(void * context, uint32_t tag, uint8_t * buffer, uint32_t buffer_size){ 274 275 btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context; 276 277 uint32_t tag_index = 0; 278 uint32_t tag_len = 0; 279 tlv_iterator_t it; 280 btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank); 281 while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){ 282 if (it.tag == tag){ 283 log_info("Found tag '%x' at position %u", tag, it.offset); 284 tag_index = it.offset; 285 tag_len = it.len; 286 break; 287 } 288 tlv_iterator_fetch_next(self, &it); 289 } 290 if (tag_index == 0) return 0; 291 if (!buffer) return tag_len; 292 int copy_size = btstack_min(buffer_size, tag_len); 293 btstack_tlv_flash_bank_read(self, self->current_bank, tag_index + 8, buffer, copy_size); 294 return copy_size; 295 } 296 297 /** 298 * Store Tag 299 * @param tag 300 * @param data 301 * @param data_size 302 */ 303 static int btstack_tlv_flash_bank_store_tag(void * context, uint32_t tag, const uint8_t * data, uint32_t data_size){ 304 305 btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context; 306 307 // trigger migration if not enough space 308 if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){ 309 btstack_tlv_flash_bank_migrate(self); 310 } 311 312 if (self->write_offset + 8 + data_size > self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){ 313 log_error("couldn't write entry, not enough space left"); 314 return 2; 315 } 316 317 // prepare entry 318 uint8_t entry[8]; 319 big_endian_store_32(entry, 0, tag); 320 big_endian_store_32(entry, 4, data_size); 321 322 log_info("write '%x', len %u at %u", tag, data_size, self->write_offset); 323 324 // write value first 325 btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset + 8, data, data_size); 326 327 // then entry 328 btstack_tlv_flash_bank_write(self, self->current_bank, self->write_offset, entry, sizeof(entry)); 329 330 // overwrite old entries (if exists) 331 btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset); 332 333 // done 334 self->write_offset += sizeof(entry) + btstack_tlv_flash_bank_align_size(self, data_size); 335 336 return 0; 337 } 338 339 /** 340 * Delete Tag 341 * @param tag 342 */ 343 static void btstack_tlv_flash_bank_delete_tag(void * context, uint32_t tag){ 344 btstack_tlv_flash_bank_t * self = (btstack_tlv_flash_bank_t *) context; 345 btstack_tlv_flash_bank_delete_tag_until_offset(self, tag, self->write_offset); 346 } 347 348 static const btstack_tlv_t btstack_tlv_flash_bank = { 349 /* int (*get_tag)(..); */ &btstack_tlv_flash_bank_get_tag, 350 /* int (*store_tag)(..); */ &btstack_tlv_flash_bank_store_tag, 351 /* void (*delete_tag)(v..); */ &btstack_tlv_flash_bank_delete_tag, 352 }; 353 354 /** 355 * Init Tag Length Value Store 356 */ 357 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){ 358 359 self->hal_flash_bank_impl = hal_flash_bank_impl; 360 self->hal_flash_bank_context = hal_flash_bank_context; 361 362 // try to find current bank 363 self->current_bank = btstack_tlv_flash_bank_get_latest_bank(self); 364 log_info("found bank %d", self->current_bank); 365 if (self->current_bank >= 0){ 366 367 // find last entry and write offset 368 tlv_iterator_t it; 369 uint32_t last_tag = 0; 370 uint32_t last_offset = 0; 371 btstack_tlv_flash_bank_iterator_init(self, &it, self->current_bank); 372 while (btstack_tlv_flash_bank_iterator_has_next(self, &it)){ 373 last_tag = it.tag; 374 last_offset = it.offset; 375 tlv_iterator_fetch_next(self, &it); 376 } 377 self->write_offset = it.offset; 378 379 if (self->write_offset < self->hal_flash_bank_impl->get_size(self->hal_flash_bank_context)){ 380 381 // delete older instances of last_tag 382 // this handles the unlikely case where MCU did reset after new value + header was written but before delete did complete 383 if (last_tag){ 384 btstack_tlv_flash_bank_delete_tag_until_offset(self, last_tag, last_offset); 385 } 386 387 // verify that rest of bank is empty 388 // this handles the unlikely case where MCU did reset after new value was written, but not the tag 389 if (!btstack_tlv_flash_bank_test_erased(self, self->current_bank, self->write_offset)){ 390 log_info("Flash not empty after last found tag -> migrate"); 391 btstack_tlv_flash_bank_migrate(self); 392 } else { 393 log_info("Flash clean after last found tag"); 394 } 395 } else { 396 // failure! 397 self->current_bank = -1; 398 } 399 } 400 401 if (self->current_bank < 0) { 402 btstack_tlv_flash_bank_erase_bank(self, 0); 403 self->current_bank = 0; 404 btstack_tlv_flash_bank_write_header(self, self->current_bank, 0); // epoch = 0; 405 self->write_offset = 8; 406 } 407 408 log_info("write offset %u", self->write_offset); 409 return &btstack_tlv_flash_bank; 410 } 411 412