diff --git a/include/zephyr/storage/flash_map.h b/include/zephyr/storage/flash_map.h index a0d528acfb7..d8ba414abd3 100644 --- a/include/zephyr/storage/flash_map.h +++ b/include/zephyr/storage/flash_map.h @@ -188,6 +188,28 @@ int flash_area_read(const struct flash_area *fa, off_t off, void *dst, int flash_area_write(const struct flash_area *fa, off_t off, const void *src, size_t len); +/** + * @brief Copy flash memory from one flash area to another. + * + * Copy data to flash area. Area boundaries are asserted before copy + * request. + * + * For more information, see flash_copy(). + * + * @param[in] src_fa Source Flash area + * @param[in] src_off Offset relative from beginning of source flash area. + * @param[in] dst_fa Destination Flash area + * @param[in] dst_off Offset relative from beginning of destination flash area. + * @param[in] len Number of bytes to copy, in bytes. + * @param[out] buf Pointer to a buffer of size @a buf_size. + * @param[in] buf_size Size of the buffer pointed to by @a buf. + * + * @return 0 on success, negative errno code on fail. + */ +int flash_area_copy(const struct flash_area *src_fa, off_t src_off, + const struct flash_area *dst_fa, off_t dst_off, + off_t len, uint8_t *buf, size_t buf_size); + /** * @brief Erase flash area * diff --git a/subsys/storage/flash_map/flash_map.c b/subsys/storage/flash_map/flash_map.c index 7b66825c442..d5b7b20bb0c 100644 --- a/subsys/storage/flash_map/flash_map.c +++ b/subsys/storage/flash_map/flash_map.c @@ -82,6 +82,20 @@ int flash_area_erase(const struct flash_area *fa, off_t off, size_t len) return flash_erase(fa->fa_dev, fa->fa_off + off, len); } +int flash_area_copy(const struct flash_area *src_fa, off_t src_off, + const struct flash_area *dst_fa, off_t dst_off, + off_t len, uint8_t *buf, size_t buf_size) +{ + if (!(is_in_flash_area_bounds(src_fa, src_off, len) && + is_in_flash_area_bounds(dst_fa, dst_off, len))) { + return -EINVAL; + } + + return flash_copy(src_fa->fa_dev, src_fa->fa_off + src_off, + dst_fa->fa_dev, dst_fa->fa_off + dst_off, len, buf, + buf_size); +} + int flash_area_flatten(const struct flash_area *fa, off_t off, size_t len) { if (!is_in_flash_area_bounds(fa, off, len)) {