Skip to content

Commit

Permalink
storage: flash map: Add flash_area_sectors
Browse files Browse the repository at this point in the history
The commit adds flash_area_sectors function that allows to get information
on sector/erase page layout by flash_area object pointer instead of
index.
The only difference between flash_area_sectors and flash_area_get_sectors
is that the later calls flash_area_open internally and as such requires
flash map to be compiled in.

Signed-off-by: Dominik Ermel <[email protected]>
  • Loading branch information
de-nordic authored and kartben committed Dec 2, 2024
1 parent 984be5e commit 55c12f2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
28 changes: 21 additions & 7 deletions include/zephyr/storage/flash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,10 @@ void flash_area_close(const struct flash_area *fa);
* Indicates whether the provided flash area has a device known to be
* in a state where it can be used with Flash Map API.
*
* This can be used with struct flash_area pointers captured from
* FIXED_PARTITION().
* This can be used with struct flash_area pointers captured from FIXED_PARTITION().
* At minimum this means that the device has been successfully initialized.
*
* @param dev pointer to flash_area object to check.
* @param fa pointer to flash_area object to check.
*
* @retval true If the device is ready for use.
* @retval false If the device is not ready for use or if a NULL pointer is
Expand Down Expand Up @@ -254,6 +253,20 @@ uint32_t flash_area_align(const struct flash_area *fa);
int flash_area_get_sectors(int fa_id, uint32_t *count,
struct flash_sector *sectors);

/**
* Retrieve info about sectors within the area.
*
* @param[in] fa pointer to flash area object.
* @param[out] sectors buffer for sectors data
* @param[in,out] count On input Capacity of @p sectors, on output number of
* sectors retrieved.
*
* @return 0 on success, negative errno code on fail. Especially returns
* -ENOMEM if There are too many flash pages on the flash_area to fit in the
* array.
*/
int flash_area_sectors(const struct flash_area *fa, uint32_t *count, struct flash_sector *sectors);

/**
* Flash map iteration callback
*
Expand Down Expand Up @@ -407,12 +420,13 @@ uint8_t flash_area_erased_val(const struct flash_area *fa);
*/
#define FIXED_PARTITION(label) FIXED_PARTITION_1(DT_NODELABEL(label))
#define FIXED_PARTITION_1(node) FIXED_PARTITION_0(DT_DEP_ORD(node))
#define FIXED_PARTITION_0(ord) (const struct flash_area *)&DT_CAT(global_fixed_partition_ORD_, part)
#define FIXED_PARTITION_0(ord) \
((const struct flash_area *)&DT_CAT(global_fixed_partition_ORD_, ord))

/** @cond INTERNAL_HIDDEN */
#define DECLARE_PARTITION(part) DECLARE_PARTITION_0(DT_DEP_ORD(part))
#define DECLARE_PARTITION_0(part) \
extern const struct flash_area DT_CAT(global_fixed_partition_ORD_, part);
#define DECLARE_PARTITION(node) DECLARE_PARTITION_0(DT_DEP_ORD(node))
#define DECLARE_PARTITION_0(ord) \
extern const struct flash_area DT_CAT(global_fixed_partition_ORD_, ord);
#define FOR_EACH_PARTITION_TABLE(table) DT_FOREACH_CHILD(table, DECLARE_PARTITION)

/* Generate declarations */
Expand Down
18 changes: 11 additions & 7 deletions subsys/storage/flash_map/flash_map_layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,24 @@ static bool get_sectors_cb(const struct flash_pages_info *info, void *datav)

int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
{
struct layout_data data;
const struct device *flash_dev;
const struct flash_area *fa;
int rc = flash_area_open(idx, &fa);

if (rc < 0 || fa == NULL) {
return -EINVAL;
}

data.area_idx = idx;
rc = flash_area_sectors(fa, cnt, ret);
flash_area_close(fa);

return rc;
}

int flash_area_sectors(const struct flash_area *fa, uint32_t *cnt, struct flash_sector *ret)
{
struct layout_data data;
const struct device *flash_dev;

data.area_off = fa->fa_off;
data.area_len = fa->fa_size;

Expand All @@ -97,10 +105,6 @@ int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
data.status = 0;

flash_dev = fa->fa_dev;
flash_area_close(fa);
if (flash_dev == NULL) {
return -ENODEV;
}

flash_page_foreach(flash_dev, get_sectors_cb, &data);

Expand Down

0 comments on commit 55c12f2

Please sign in to comment.