storage: flash map: Add flash_area_sectors

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 <dominik.ermel@nordicsemi.no>
This commit is contained in:
Dominik Ermel 2024-11-07 17:16:45 +01:00 committed by Anas Nashif
commit b50dc43a26
2 changed files with 25 additions and 7 deletions

View file

@ -254,6 +254,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
*

View file

@ -78,8 +78,6 @@ 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);
@ -87,7 +85,17 @@ int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
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;
@ -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);