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 <jordan@embeint.com>
This commit is contained in:
Jordan Yates
2024-07-12 15:05:24 +10:00
committed by Benjamin Cabé
parent 2f47985a9e
commit 3050daaecc
3 changed files with 52 additions and 1 deletions

View File

@@ -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);
};

View File

@@ -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
*

View File

@@ -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);