Skip to content

Commit 722628b

Browse files
author
Veijo Pesonen
committed
[TDBStore] changes the default TDBStore location
Thus far the default position has been after the application plus two spare sectors. For simplicity and to have a predictable location for the TDBStore with the default configuration the location is now switched to the end of the flash. Two last sectors to be exact.
1 parent 9222e15 commit 722628b

File tree

2 files changed

+39
-54
lines changed

2 files changed

+39
-54
lines changed

features/storage/TESTS/kvstore/direct_access_devicekey_test/main.cpp

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ static inline uint32_t align_down(uint64_t val, uint64_t size)
5353
int get_virtual_TDBStore_position(uint32_t conf_start_address, uint32_t conf_size, bool is_conf_tdb_internal,
5454
uint32_t *tdb_start_address, uint32_t *tdb_end_address)
5555
{
56-
uint32_t bd_final_size = conf_size;;
56+
uint32_t bd_final_size = conf_size;
5757
uint32_t flash_end_address;
5858
uint32_t flash_start_address;
5959
uint32_t aligned_start_address;
6060
FlashIAP flash;
61+
static const int STORE_SECTORS = 2;
6162

6263
int ret = flash.init();
6364
if (ret != 0) {
@@ -92,34 +93,19 @@ int get_virtual_TDBStore_position(uint32_t conf_start_address, uint32_t conf_si
9293
}
9394
}
9495
} else {
95-
if (is_conf_tdb_internal == true) {
96-
aligned_start_address = flash_first_writable_sector_address;
97-
bd_size_t spare_size_for_app = 0;
98-
bd_addr_t curr_addr = aligned_start_address;
99-
int spare_sectors_for_app = 2;
100-
int min_sectors_for_storage = 2;
101-
for (int i = 0; i < spare_sectors_for_app + min_sectors_for_storage - 1; i++) {
102-
bd_size_t sector_size = flash.get_sector_size(curr_addr);
103-
curr_addr += sector_size;
104-
if (curr_addr >= flash_end_address) {
105-
spare_size_for_app = 0;
106-
break;
107-
}
108-
109-
if (i < spare_sectors_for_app) {
110-
spare_size_for_app += sector_size;
111-
}
112-
}
113-
aligned_start_address += spare_size_for_app;
114-
bd_final_size = (flash_end_address - aligned_start_address);
115-
} else {
116-
aligned_start_address = flash_end_address - (flash.get_sector_size(flash_end_address - 1) * 2);
117-
if (aligned_start_address < flash_first_writable_sector_address) {
118-
flash.deinit();
119-
return -2;
120-
}
121-
bd_final_size = (flash_end_address - aligned_start_address);
96+
// Assumption is that last two sectors are reserved for the TDBStore
97+
aligned_start_address = flash.get_flash_start() + flash.get_flash_size();
98+
99+
for (int i = STORE_SECTORS; i; i--) {
100+
bd_size_t sector_size = flash.get_sector_size(aligned_start_address - 1);
101+
aligned_start_address -= sector_size;
102+
}
103+
104+
if (aligned_start_address < flash_first_writable_sector_address) {
105+
flash.deinit();
106+
return -2;
122107
}
108+
bd_final_size = (flash_end_address - aligned_start_address);
123109
}
124110

125111
(*tdb_start_address) = aligned_start_address;

features/storage/kvstore/conf/kv_config.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -267,51 +267,48 @@ FileSystem *_get_filesystem_default(const char *mount)
267267
}
268268

269269
//Calculates the start address of FLASHIAP block device for TDB_INTERNAL profile.
270-
//If possible, the address will start 2 sectors after the end of code sector allowing
271-
//some space for an application update.
270+
//Last two sectors to have a predictable location for the TDBStore
272271
int _get_flashiap_bd_default_addresses_tdb_internal(bd_addr_t *start_address, bd_size_t *size)
273272
{
273+
int ret = MBED_SUCCESS;
274+
274275
#if COMPONENT_FLASHIAP
275276

276277
FlashIAP flash;
278+
static const int STORE_SECTORS = 2;
277279

278-
if (*start_address != 0 || *size != 0) {
280+
if (*start_address || *size) {
279281
return MBED_ERROR_INVALID_ARGUMENT;
280282
}
281283

282-
//If default values are set, we should get the maximum available size of internal bd.
283284
if (flash.init() != 0) {
284285
return MBED_ERROR_FAILED_OPERATION;
285286
}
286287

287-
*start_address = align_up(FLASHIAP_APP_ROM_END_ADDR, flash.get_sector_size(FLASHIAP_APP_ROM_END_ADDR));
288+
// Lets work from end of the flash backwards
289+
bd_addr_t curr_addr = flash.get_flash_start() + flash.get_flash_size();
288290

289-
// Give the application a couple of spare sectors to grow (if there are such)
290-
bd_size_t spare_size_for_app = 0;
291-
bd_addr_t curr_addr = *start_address;
292-
bd_addr_t flash_end_address = flash.get_flash_start() + flash.get_flash_size();
291+
for (int i = STORE_SECTORS; i; i--) {
292+
bd_size_t sector_size = flash.get_sector_size(curr_addr - 1);
293+
curr_addr -= sector_size;
294+
}
293295

294-
int spare_sectors_for_app = 2;
295-
int min_sectors_for_storage = 2;
296-
for (int i = 0; i < spare_sectors_for_app + min_sectors_for_storage - 1; i++) {
297-
bd_size_t sector_size = flash.get_sector_size(curr_addr);
298-
curr_addr += sector_size;
299-
if (curr_addr >= flash_end_address) {
300-
spare_size_for_app = 0;
301-
break;
302-
}
296+
// Store- and application-sectors mustn't overlap
297+
uint32_t first_wrtbl_sector_addr =
298+
(uint32_t)(align_up(FLASHIAP_APP_ROM_END_ADDR, flash.get_sector_size(FLASHIAP_APP_ROM_END_ADDR)));
303299

304-
if (i < spare_sectors_for_app) {
305-
spare_size_for_app += sector_size;
306-
}
300+
MBED_ASSERT(curr_addr >= first_wrtbl_sector_addr);
301+
if (curr_addr < first_wrtbl_sector_addr) {
302+
ret = MBED_ERROR_MEDIA_FULL;
303+
} else {
304+
*start_address = curr_addr;
307305
}
308-
*start_address += spare_size_for_app;
309306

310307
flash.deinit();
311308

312309
#endif
313310

314-
return MBED_SUCCESS;
311+
return ret;
315312
}
316313

317314
//Calculates address and size for FLASHIAP block device in TDB_EXTERNAL and FILESYSTEM profiles.
@@ -697,11 +694,13 @@ int _storage_config_TDB_INTERNAL()
697694
#if COMPONENT_FLASHIAP
698695
bd_size_t internal_size = MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_SIZE;
699696
bd_addr_t internal_start_address = MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_BASE_ADDRESS;
697+
int ret;
700698

701699
if (internal_size == 0 && internal_start_address == 0) {
702700
//Calculate the block device size and start address in case default values are used.
703-
if (_get_flashiap_bd_default_addresses_tdb_internal(&internal_start_address, &internal_size) != MBED_SUCCESS) {
704-
return MBED_ERROR_FAILED_OPERATION;
701+
ret = _get_flashiap_bd_default_addresses_tdb_internal(&internal_start_address, &internal_size);
702+
if (ret != MBED_SUCCESS) {
703+
return ret;
705704
}
706705
}
707706

@@ -713,7 +712,7 @@ int _storage_config_TDB_INTERNAL()
713712
}
714713

715714

716-
int ret = kvstore_config.internal_bd->init();
715+
ret = kvstore_config.internal_bd->init();
717716
if (ret != MBED_SUCCESS) {
718717
tr_error("KV Config: Fail to init internal BlockDevice.");
719718
return MBED_ERROR_FAILED_OPERATION;

0 commit comments

Comments
 (0)