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