@@ -267,51 +267,48 @@ FileSystem *_get_filesystem_default(const char *mount)
267
267
}
268
268
269
269
// 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
272
271
int _get_flashiap_bd_default_addresses_tdb_internal (bd_addr_t *start_address, bd_size_t *size)
273
272
{
273
+ int ret = MBED_SUCCESS;
274
+
274
275
#if COMPONENT_FLASHIAP
275
276
276
277
FlashIAP flash;
278
+ static const int STORE_SECTORS = 2 ;
277
279
278
- if (*start_address != 0 || *size != 0 ) {
280
+ if (*start_address || *size) {
279
281
return MBED_ERROR_INVALID_ARGUMENT;
280
282
}
281
283
282
- // If default values are set, we should get the maximum available size of internal bd.
283
284
if (flash.init () != 0 ) {
284
285
return MBED_ERROR_FAILED_OPERATION;
285
286
}
286
287
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 ();
288
290
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
+ }
293
295
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)));
303
299
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;
307
305
}
308
- *start_address += spare_size_for_app;
309
306
310
307
flash.deinit ();
311
308
312
309
#endif
313
310
314
- return MBED_SUCCESS ;
311
+ return ret ;
315
312
}
316
313
317
314
// Calculates address and size for FLASHIAP block device in TDB_EXTERNAL and FILESYSTEM profiles.
@@ -697,11 +694,13 @@ int _storage_config_TDB_INTERNAL()
697
694
#if COMPONENT_FLASHIAP
698
695
bd_size_t internal_size = MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_SIZE;
699
696
bd_addr_t internal_start_address = MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_BASE_ADDRESS;
697
+ int ret;
700
698
701
699
if (internal_size == 0 && internal_start_address == 0 ) {
702
700
// 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;
705
704
}
706
705
}
707
706
@@ -713,7 +712,7 @@ int _storage_config_TDB_INTERNAL()
713
712
}
714
713
715
714
716
- int ret = kvstore_config.internal_bd ->init ();
715
+ ret = kvstore_config.internal_bd ->init ();
717
716
if (ret != MBED_SUCCESS) {
718
717
tr_error (" KV Config: Fail to init internal BlockDevice." );
719
718
return MBED_ERROR_FAILED_OPERATION;
0 commit comments