@@ -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
272271int _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