diff --git a/include/zephyr/storage/flash_map.h b/include/zephyr/storage/flash_map.h index b389eacc6fb..cdd9ae00759 100644 --- a/include/zephyr/storage/flash_map.h +++ b/include/zephyr/storage/flash_map.h @@ -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 @@ -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 * @@ -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 */ diff --git a/subsys/storage/flash_map/flash_map_layout.c b/subsys/storage/flash_map/flash_map_layout.c index 27d100183e1..d1ee845d469 100644 --- a/subsys/storage/flash_map/flash_map_layout.c +++ b/subsys/storage/flash_map/flash_map_layout.c @@ -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);