mbed: Fix calling to BlockDevice::size before initialization

These changes fixes a bug that can cause an uninitialized BlockDevice
to be queried for its size. In some cases, this can return unexpected
results (eg: 0). Move setting block device size after its
initialization.
Co-created: @AGlass0fMilk - George Beckstein

Signed-off-by: Artur Tynecki <artur.tynecki@mobica.com>
Signed-off-by: George Beckstein <george.beckstein@gmail.com>
This commit is contained in:
Artur Tynecki 2022-02-08 12:04:52 +01:00 committed by David Brown
parent 6822365ecb
commit a3cf3f8041
1 changed files with 9 additions and 2 deletions

View File

@ -91,15 +91,22 @@ int flash_area_open(uint8_t id, const struct flash_area** fapp) {
fap->fa_device_id = 0; // not relevant
mbed::BlockDevice* bd = flash_map_bd[id];
fap->fa_size = (uint32_t) bd->size();
/* Only initialize if this isn't a nested call to open the flash area */
if (open_count[id] == 1) {
MCUBOOT_LOG_DBG("initializing flash area %d...", id);
return bd->init();
int ret = bd->init();
if (ret)
{
MCUBOOT_LOG_ERR("initializing flash area failed %d", ret);
return ret;
}
} else {
return 0;
}
fap->fa_size = (uint32_t) bd->size();
return 0;
}
void flash_area_close(const struct flash_area* fap) {