diff --git a/include/storage/flash_map.h b/include/storage/flash_map.h index 0f209036294..2d3a720eabc 100644 --- a/include/storage/flash_map.h +++ b/include/storage/flash_map.h @@ -245,6 +245,17 @@ int flash_area_has_driver(const struct flash_area *fa); */ const struct device *flash_area_get_device(const struct flash_area *fa); +/** + * Get the value expected to be read when accessing any erased + * flash byte. + * This API is compatible with the MCUBoot's porting layer. + * + * @param fa Flash area. + * + * @return Byte value of erase memory. + */ +uint8_t flash_area_erased_val(const struct flash_area *fa); + #define FLASH_AREA_LABEL_EXISTS(label) \ DT_HAS_FIXED_PARTITION_LABEL(label) diff --git a/subsys/dfu/boot/mcuboot.c b/subsys/dfu/boot/mcuboot.c index 0b12a84ca25..b38a0aa6ad8 100644 --- a/subsys/dfu/boot/mcuboot.c +++ b/subsys/dfu/boot/mcuboot.c @@ -205,15 +205,6 @@ static int boot_flag_decode(uint8_t flag) return BOOT_FLAG_SET; } -/* TODO: this function should be moved to flash_area api in future */ -uint8_t flash_area_erased_val(const struct flash_area *fa) -{ - #define ERASED_VAL 0xff - - (void)fa; - return ERASED_VAL; -} - /* TODO: this function should be moved to flash_area api in future */ int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off, void *dst, uint32_t len) diff --git a/subsys/storage/flash_map/flash_map.c b/subsys/storage/flash_map/flash_map.c index 6bb678a00ec..926592595cb 100644 --- a/subsys/storage/flash_map/flash_map.c +++ b/subsys/storage/flash_map/flash_map.c @@ -270,6 +270,15 @@ const struct device *flash_area_get_device(const struct flash_area *fa) return device_get_binding(fa->fa_dev_name); } +uint8_t flash_area_erased_val(const struct flash_area *fa) +{ + const struct flash_parameters *param; + + param = flash_get_parameters(device_get_binding(fa->fa_dev_name)); + + return param->erase_value; +} + #if defined(CONFIG_FLASH_AREA_CHECK_INTEGRITY) int flash_area_check_int_sha256(const struct flash_area *fa, const struct flash_area_check *fac) diff --git a/tests/subsys/storage/flash_map/src/main.c b/tests/subsys/storage/flash_map/src/main.c index 5a0290e8169..fc01348f07d 100644 --- a/tests/subsys/storage/flash_map/src/main.c +++ b/tests/subsys/storage/flash_map/src/main.c @@ -158,9 +158,28 @@ void test_flash_area_check_int_sha256(void) flash_area_close(fa); } +void test_flash_area_erased_val(void) +{ + const struct flash_parameters *param; + const struct flash_area *fa; + uint8_t val; + int rc; + + rc = flash_area_open(FLASH_AREA_ID(image_1), &fa); + zassert_true(rc == 0, "flash_area_open() fail"); + + val = flash_area_erased_val(fa); + + param = flash_get_parameters(device_get_binding(fa->fa_dev_name)); + + zassert_equal(param->erase_value, val, + "value different than the flash erase value"); +} + void test_main(void) { ztest_test_suite(test_flash_map, + ztest_unit_test(test_flash_area_erased_val), ztest_unit_test(test_flash_area_get_sectors), ztest_unit_test(test_flash_area_check_int_sha256) );