From 3050daaecc1fff63d1095230e0e212e2ba459856 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Fri, 12 Jul 2024 15:05:24 +1000 Subject: [PATCH] storage: disk_access: add `disk_access_erase` Add the `disk_access_erase` command to complement the read and write commands. As a backwards compatible new feature, this increments the API version from `1.0.0` to `1.1.0`. Signed-off-by: Jordan Yates --- include/zephyr/drivers/disk.h | 3 ++- include/zephyr/storage/disk_access.h | 20 +++++++++++++++++++ subsys/disk/disk_access.c | 30 ++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/include/zephyr/drivers/disk.h b/include/zephyr/drivers/disk.h index 18b1c0f3ede..899a3e25be5 100644 --- a/include/zephyr/drivers/disk.h +++ b/include/zephyr/drivers/disk.h @@ -23,7 +23,7 @@ * @brief Interfaces for disks. * @defgroup disk_driver_interface Disk Access * @since 1.6 - * @version 1.0.0 + * @version 1.1.0 * @ingroup io_interfaces * @{ */ @@ -110,6 +110,7 @@ struct disk_operations { uint32_t start_sector, uint32_t num_sector); int (*write)(struct disk_info *disk, const uint8_t *data_buf, uint32_t start_sector, uint32_t num_sector); + int (*erase)(struct disk_info *disk, uint32_t start_sector, uint32_t num_sector); int (*ioctl)(struct disk_info *disk, uint8_t cmd, void *buff); }; diff --git a/include/zephyr/storage/disk_access.h b/include/zephyr/storage/disk_access.h index c06d7d1baf2..4608cb7bb67 100644 --- a/include/zephyr/storage/disk_access.h +++ b/include/zephyr/storage/disk_access.h @@ -100,6 +100,26 @@ int disk_access_read(const char *pdrv, uint8_t *data_buf, int disk_access_write(const char *pdrv, const uint8_t *data_buf, uint32_t start_sector, uint32_t num_sector); +enum disk_access_erase_type { + /** Erase the physical bytes on the disk to their natural erase value (0x00 or 0xFF) */ + DISK_ACCESS_ERASE_PHYSICAL = 0, +}; + +/** + * @brief erase data from disk + * + * The result of this operation depends on the type of erase, as specified by @a erase_type. + * + * @param[in] pdrv Disk name + * @param[in] start_sector Start disk sector to erase + * @param[in] num_sector Number of disk sectors to erase + * @param[in] erase_type Type of erase to perform + * + * @return 0 on success, negative errno code on fail + */ +int disk_access_erase(const char *pdrv, uint32_t start_sector, uint32_t num_sector, + enum disk_access_erase_type erase_type); + /** * @brief Get/Configure disk parameters * diff --git a/subsys/disk/disk_access.c b/subsys/disk/disk_access.c index 313c937c15d..b9512a788ab 100644 --- a/subsys/disk/disk_access.c +++ b/subsys/disk/disk_access.c @@ -118,6 +118,36 @@ int disk_access_write(const char *pdrv, const uint8_t *data_buf, return rc; } +int disk_access_erase(const char *pdrv, uint32_t start_sector, uint32_t num_sector, + enum disk_access_erase_type erase_type) +{ + struct disk_info *disk = disk_access_get_di(pdrv); + uint32_t erase_sector_size; + int rc = -EINVAL; + + /* Only support physical erase for now. + * This parameter is not passed through to the underlying disk to leave the design + * space open for future erase types (Other erase types may be dedicated functions). + */ + if (erase_type != DISK_ACCESS_ERASE_PHYSICAL) { + return -EINVAL; + } + + /* Validate sector sizes, if underlying driver exposes a way to query it */ + if (disk_access_ioctl(pdrv, DISK_IOCTL_GET_ERASE_BLOCK_SZ, &erase_sector_size) == 0) { + /* Alignment check on both start and range of erase request */ + if ((start_sector % erase_sector_size) || (num_sector % erase_sector_size)) { + return -EINVAL; + } + } + + if ((disk != NULL) && (disk->ops != NULL) && (disk->ops->erase != NULL)) { + rc = disk->ops->erase(disk, start_sector, num_sector); + } + + return rc; +} + int disk_access_ioctl(const char *pdrv, uint8_t cmd, void *buf) { struct disk_info *disk = disk_access_get_di(pdrv);