From 053857e09aec24228dffa891aed3ac14ed91f688 Mon Sep 17 00:00:00 2001 From: Andrzej Puzdrowski Date: Fri, 18 Sep 2020 18:19:39 +0200 Subject: [PATCH] storage/flash_map: Added function for get erased byte value Added flash_area_erased_val() function for get value of erased byte of memory which is under flash area. This function already exist in MCUBoot and zephyr dfu subsystem which makes simultaneous usage of both impossible. Signed-off-by: Andrzej Puzdrowski --- include/storage/flash_map.h | 11 +++++++++++ subsys/dfu/boot/mcuboot.c | 9 --------- subsys/storage/flash_map/flash_map.c | 9 +++++++++ tests/subsys/storage/flash_map/src/main.c | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 9 deletions(-) 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) );