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>
diff --git a/boot/mbed/src/flash_map_backend.cpp b/boot/mbed/src/flash_map_backend.cpp
index 8f5137d..7a1908a 100644
--- a/boot/mbed/src/flash_map_backend.cpp
+++ b/boot/mbed/src/flash_map_backend.cpp
@@ -91,15 +91,22 @@
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) {