storage/flash_map: Macros for accessing fixed-partitions

The commit adds FIXED_PARTITION_* macros that take DTS node,
in contrary to FLASH_AREA_* macros that take label property,
to access properties of fixed-partition compatible DTS partition
definitions.
There is FIXED_PARTITION_NODE macro provided for translating
DTS labels to nodes.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
This commit is contained in:
Dominik Ermel 2022-07-22 13:53:16 +00:00 committed by Carles Cufí
parent a3d7de7628
commit 1b29f056b9
2 changed files with 62 additions and 10 deletions

View file

@ -34,6 +34,8 @@
#include <zephyr/types.h>
#include <stddef.h>
#include <sys/types.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#ifdef __cplusplus
extern "C" {
@ -269,16 +271,63 @@ uint8_t flash_area_erased_val(const struct flash_area *fa);
#define FLASH_AREA_SIZE(label) \
DT_REG_SIZE(DT_NODE_BY_FIXED_PARTITION_LABEL(label))
/**
* Returns non-0 value if fixed-partition of given DTS node label exists.
*
* @param label DTS node label
*
* @return non-0 if fixed-partition node exists and is enabled;
* 0 if node does not exist, is not enabled or is not fixed-partition.
*/
#define FIXED_PARTITION_EXISTS(label) DT_FIXED_PARTITION_EXISTS(DT_NODELABEL(label))
/**
* Get flash area ID from fixed-partition DTS node label
*
* @param label DTS node label of a partition
*
* @return flash area ID
*/
#define FIXED_PARTITION_ID(label) DT_FIXED_PARTITION_ID(DT_NODELABEL(label))
/**
* Get fixed-partition offset from DTS node label
*
* @param label DTS node label of a partition
*
* @return fixed-partition offset, as defined for the partition in DTS.
*/
#define FIXED_PARTITION_OFFSET(label) DT_REG_ADDR(DT_NODELABEL(label))
/**
* Get fixed-partition size for DTS node label
*
* @param label DTS node label
*
* @return fixed-partition offset, as defined for the partition in DTS.
*/
#define FIXED_PARTITION_SIZE(label) DT_REG_SIZE(DT_NODELABEL(label))
/**
* Get device pointer for device the area/partition resides on
*
* @param label partition label
* @param label DTS node label of a partition
*
* @return const struct device type pointer
*/
#define FLASH_AREA_DEVICE(label) \
DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(DT_NODE_BY_FIXED_PARTITION_LABEL(label)))
/**
* Get device pointer for device the area/partition resides on
*
* @param label DTS node label of a partition
*
* @return Pointer to a device.
*/
#define FIXED_PARTITION_DEVICE(label) \
DEVICE_DT_GET(DT_MTD_FROM_FIXED_PARTITION(DT_NODELABEL(label)))
#ifdef __cplusplus
}
#endif

View file

@ -10,6 +10,10 @@
#include <zephyr/drivers/flash.h>
#include <zephyr/storage/flash_map.h>
#define SLOT1_PARTITION slot1_partition
#define SLOT1_PARTITION_ID FIXED_PARTITION_ID(SLOT1_PARTITION)
#define SLOT1_PARTITION_DEV FIXED_PARTITION_DEVICE(SLOT1_PARTITION)
extern int flash_map_entries;
struct flash_sector fs_sectors[256];
@ -19,9 +23,9 @@ ZTEST(flash_map, test_flash_area_disabled_device)
int rc;
/* Test that attempting to open a disabled flash area fails */
rc = flash_area_open(FLASH_AREA_ID(disabled_a), &fa);
rc = flash_area_open(FIXED_PARTITION_ID(disabled_a), &fa);
zassert_equal(rc, -ENODEV, "Open did not fail");
rc = flash_area_open(FLASH_AREA_ID(disabled_b), &fa);
rc = flash_area_open(FIXED_PARTITION_ID(disabled_b), &fa);
zassert_equal(rc, -ENODEV, "Open did not fail");
}
@ -38,16 +42,16 @@ ZTEST(flash_map, test_flash_area_get_sectors)
uint8_t wd[256];
uint8_t rd[256];
const struct device *flash_dev;
const struct device *flash_dev_a = FLASH_AREA_DEVICE(image_1);
const struct device *flash_dev_a = SLOT1_PARTITION_DEV;
rc = flash_area_open(FLASH_AREA_ID(image_1), &fa);
rc = flash_area_open(SLOT1_PARTITION_ID, &fa);
zassert_true(rc == 0, "flash_area_open() fail");
/* First erase the area so it's ready for use. */
flash_dev = flash_area_get_device(fa);
/* Device obtained by label should match the one from fa object */
zassert_equal(flash_dev, flash_dev_a, "Device for image_1 do not match");
zassert_equal(flash_dev, flash_dev_a, "Device for slot1_partition do not match");
rc = flash_erase(flash_dev, fa->fa_off, fa->fa_size);
zassert_true(rc == 0, "flash area erase fail");
@ -55,8 +59,7 @@ ZTEST(flash_map, test_flash_area_get_sectors)
(void)memset(wd, 0xa5, sizeof(wd));
sec_cnt = ARRAY_SIZE(fs_sectors);
rc = flash_area_get_sectors(FLASH_AREA_ID(image_1), &sec_cnt,
fs_sectors);
rc = flash_area_get_sectors(SLOT1_PARTITION_ID, &sec_cnt, fs_sectors);
zassert_true(rc == 0, "flash_area_get_sectors failed");
/* write stuff to beginning of every sector */
@ -128,7 +131,7 @@ ZTEST(flash_map, test_flash_area_check_int_sha256)
uint8_t buffer[16];
int rc;
rc = flash_area_open(FLASH_AREA_ID(image_1), &fa);
rc = flash_area_open(SLOT1_PARTITION_ID, &fa);
zassert_true(rc == 0, "flash_area_open() fail, error %d\n", rc);
rc = flash_area_erase(fa, 0, fa->fa_size);
zassert_true(rc == 0, "Flash erase failure (%d), error %d\n", rc);
@ -174,7 +177,7 @@ ZTEST(flash_map, test_flash_area_erased_val)
uint8_t val;
int rc;
rc = flash_area_open(FLASH_AREA_ID(image_1), &fa);
rc = flash_area_open(SLOT1_PARTITION_ID, &fa);
zassert_true(rc == 0, "flash_area_open() fail");
val = flash_area_erased_val(fa);