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