Skip to content
30 changes: 30 additions & 0 deletions boot/zephyr/flash_map_extended.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,33 @@ __weak uint8_t flash_area_erased_val(const struct flash_area *fap)
(void)fap;
return ERASED_VAL;
}

int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *count,
struct flash_sector *ret)
{
off_t fa_off = fa->fa_off;
off_t offset = 0;
size_t fa_size = fa->fa_size;
Comment on lines +153 to +155
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fa_off and fa_size only seem to be used for reading, can't the fa structure be used directly instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it can.

int max_sectors = *count;
int sector_idx = 0;
int rc = 0;

while (rc == 0 && sector_idx < max_sectors && offset < fa_size) {
struct flash_pages_info fpi;

rc = flash_get_page_info_by_offs(fa->fa_dev, fa_off + offset, &fpi);

if (rc == 0) {
ret[sector_idx].fs_off = fpi.start_offset - fa_off;
ret[sector_idx].fs_size = fpi.size;
++sector_idx;
offset += fpi.size;
}
}

if (sector_idx >= max_sectors && offset >= fa_size) {
return -ENOENT;
}

return rc;
}
15 changes: 15 additions & 0 deletions boot/zephyr/include/flash_map_backend/flash_map_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ int flash_area_id_to_multi_image_slot(int image_index, int area_id);
*/
int flash_area_sector_from_off(off_t off, struct flash_sector *sector);

/* Get sectors for given flash_area object. This function is similar to
* flash_area_get_sectors but takes flash_area object pointer instead
* of flash area identifier.
*
* @param fa pointer to flash_area object.
* @param count in: size of array for returned sectors, out: number
* of sectors filled in.
* @param ret array of sectors.
*
* Returns 0 on success, -ERANGE when there is not enough space in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERANGE looks wrong

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERANGE seems appropriate here.

* @p ret for sector information; other negative errno code.
*/
int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *count,
struct flash_sector *ret);

static inline uint32_t flash_area_get_off(const struct flash_area *fa)
{
return (uint32_t)fa->fa_off;
Expand Down