chore: migrate project into clean repository

This commit is contained in:
yuuux
2026-08-13 16:50:52 +08:00
commit d1d25a09e7
27405 changed files with 9422808 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
add_library(disk STATIC "")
target_include_directories(disk PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_sources(disk PRIVATE disk_access.c)
if(CONFIG_DISK_DRIVER_FLASH)
target_sources(disk PRIVATE flashdisk.c)
endif()
if (CONFIG_DISK_DRIVER_RAM)
target_sources(disk PRIVATE ramdisk.c)
endif()
if (CONFIG_DISK_DRIVER_SDMMC)
target_sources(disk PRIVATE sdmmc.c)
endif()
if (CONFIG_DISK_IOCACHE_ENABLE)
target_sources(disk PRIVATE iocache.c)
endif()
target_link_libraries(disk PUBLIC arcs-hal fs_env)
if(DEFINED LISTENAI_CMAKE_PATH)
listenai_link_libraries(disk)
endif()

View File

@@ -0,0 +1,22 @@
# SPDX-License-Identifier: Apache-2.0
menu "disk driver"
config DISK_DRIVER
bool "Disk driver support"
help
Enables support for disk driver.
if DISK_DRIVER
config DISK_IOCACHE_ENABLE
bool "Enable IO cache"
default n
rsource "Kconfig.flash"
rsource "Kconfig.ram"
rsource "Kconfig.sdmmc"
endif # FILE_SYSTEM
endmenu

View File

@@ -0,0 +1,66 @@
# SPDX-License-Identifier: Apache-2.0
config DISK_DRIVER_FLASH
bool "Flash"
select LISA_DEVICE
select LISA_FLASH
help
Flash device is used for the file system.
if DISK_DRIVER_FLASH
config DISK_FLASH_VOLUME_NAME
string "Flash mount point or drive name"
default "NAND"
help
Disk name as per file system naming guidelines.
config DISK_FLASH_CHIP_BASE_ADDR
hex "Flash device chip base address in hex"
default 0x30000000
help
This is base logic address of the flash.
config DISK_FLASH_CHIP_SIZE
hex "Flash device chip size in hex"
default 0x2000000
help
This is chip size of the flash.
config DISK_FLASH_START_ADDR
hex "Flash device start address in hex"
help
This is start address of the flash to be used as storage backend.
config DISK_FLASH_MAX_RW_SIZE
int "Flash device max read-write size in decimal"
help
This is the maximum number of bytes that the
flash_write API can accept per invocation.
API.
config DISK_FLASH_ERASE_ALIGNMENT
int "Flash device erase alignment in decimal"
help
This is the start address alignment required by
the flash component.
config DISK_FLASH_ERASE_BLOCK_SIZE
int "Flash device erasable block size in decimal"
help
This is typically the minimum block size that
is erased at one time in flash storage.
Typically it is equal to the flash memory page size.
config DISK_FLASH_SECTOR_SIZE
int "Flash device sector size in decimal"
default 512
help
This is the file system sector size in bytes.
config DISK_FLASH_VOLUME_SIZE
int "Flash device volume size in decimal"
help
This is the file system volume size in bytes.
endif # DISK_DRIVER_FLASH

View File

@@ -0,0 +1,37 @@
# SPDX-License-Identifier: Apache-2.0
config DISK_DRIVER_RAM
bool "RAM Disk"
default n
help
RAM buffer used to emulate storage disk.
This option can be used to test the file
system.
if DISK_DRIVER_RAM
config DISK_RAM_VOLUME_NAME
string "RAM mount point or drive name"
default "RAM"
help
Disk name as per file system naming guidelines.
config DISK_RAM_SECTOR_SIZE
int "RAM device sector size in decimal"
default 512
help
This is the file system sector size in bytes.
config DISK_RAM_SECTOR_COUNT
int "RAM device sector size in decimal"
default 128
help
This is the file system sector count.
config DISK_RAM_MEMORY_SECTION_NAME
string "RAM device used memory segment name"
default ".bss"
help
This is the name memory segment used.
endif # DISK_DRIVER_RAM

View File

@@ -0,0 +1,59 @@
# SPDX-License-Identifier: Apache-2.0
config DISK_DRIVER_SDMMC
bool "SDMMC disk driver"
default n
select LISA_DEVICE
select LISA_SDMMC_DEVICE
help
Support using sdmmc devices as storage media for file systems.
Automatically enables LISA SDMMC device driver.
if DISK_DRIVER_SDMMC
config DISK_SDMMC_VOLUME_NAME
string "SDMMC mount point or drive name"
default "SD"
help
Disk name as per file system naming guidelines.
config DISK_SDMMC_START_SECTOR_NUMBER
int "SDMMC disk start sector number"
default 0x0
help
The starting sector number of the file system in the SD/MMC device.
config DISK_SDMMC_ACCESS_BUFFER_ALIGN_ENABLE
bool "Enable SDMMC access buffer alignment"
default y
if DISK_SDMMC_ACCESS_BUFFER_ALIGN_ENABLE
config DISK_SDMMC_ACCESS_BUFFER_ALIGN_SIZE
int "SDMMC access buffer alignment size"
default 32
config DISK_SDMMC_ACCESS_WRAP_BUFFER_SIZE
int "SDMMC access wrap buffer size"
default 4096
endif
config DISK_SDMMC_IOCACHE_ENABLE
bool "Enable SDMMC IO cache"
default n
depends on DISK_IOCACHE_ENABLE
if DISK_SDMMC_IOCACHE_ENABLE
config DISK_SDMMC_IOCACHE_ENTRY_COUNT
int "SDMMC IO cache entry count"
default 1024
config DISK_SDMMC_IOCACHE_MAX_BLOCK_SIZE
int "SDMMC IO cache max block size"
default 512
config DISK_SDMMC_IOCACHE_SECTOR_SIZE
int "SDMMC IO cache sector size"
default 512
endif
endif # DISK_DRIVER_SDMMC

View File

@@ -0,0 +1,196 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include "fs_env/fs_env.h"
#include "disk/disk.h"
/* list of mounted file systems */
static sys_dlist_t disk_access_list;
static fs_env_mutex_handle_t list_mutex_handle = {
.mutex = NULL,
};
struct disk_info *disk_access_get_di(const char *name)
{
struct disk_info *disk = NULL, *itr;
size_t name_len = strlen(name);
sys_dnode_t *node;
fs_env_mutex_lock(&list_mutex_handle,FS_ENV_MAX_DELAY);;
SYS_DLIST_FOR_EACH_NODE(&disk_access_list, node)
{
itr = CONTAINER_OF(node, struct disk_info, node);
/*
* Move to next node if mount point length is
* shorter than longest_match match or if path
* name is shorter than the mount point name.
*/
if (strlen(itr->name) != name_len)
{
continue;
}
/* Check for disk name match */
if (strncmp(name, itr->name, name_len) == 0)
{
disk = itr;
break;
}
}
fs_env_mutex_unlock(&list_mutex_handle);
return disk;
}
int disk_access_init(const char *pdrv)
{
struct disk_info *disk = disk_access_get_di(pdrv);
int rc = -EINVAL;
if ((disk != NULL) && (disk->ops != NULL) &&
(disk->ops->init != NULL))
{
rc = disk->ops->init(disk);
}
return rc;
}
int disk_access_status(const char *pdrv)
{
struct disk_info *disk = disk_access_get_di(pdrv);
int rc = -EINVAL;
if ((disk != NULL) && (disk->ops != NULL) &&
(disk->ops->status != NULL))
{
rc = disk->ops->status(disk);
}
return rc;
}
int disk_access_read(const char *pdrv, uint8_t *data_buf,
uint32_t start_sector, uint32_t num_sector)
{
struct disk_info *disk = disk_access_get_di(pdrv);
int rc = -EINVAL;
if ((disk != NULL) && (disk->ops != NULL) &&
(disk->ops->read != NULL))
{
rc = disk->ops->read(disk, data_buf, start_sector, num_sector);
}
return rc;
}
int disk_access_write(const char *pdrv, const uint8_t *data_buf,
uint32_t start_sector, uint32_t num_sector)
{
struct disk_info *disk = disk_access_get_di(pdrv);
int rc = -EINVAL;
if ((disk != NULL) && (disk->ops != NULL) &&
(disk->ops->write != NULL))
{
rc = disk->ops->write(disk, data_buf, 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);
int rc = -EINVAL;
if ((disk != NULL) && (disk->ops != NULL) &&
(disk->ops->ioctl != NULL))
{
rc = disk->ops->ioctl(disk, cmd, buf);
}
return rc;
}
int disk_access_register(struct disk_info *disk)
{
if ((disk == NULL) || (disk->name == NULL))
{
DISK_LOG("invalid disk interface!!");
return -EINVAL;
}
if (disk_access_get_di(disk->name) != NULL)
{
DISK_LOG("disk interface already registered!!");
return -EINVAL;
}
/* append to the disk list */
fs_env_mutex_lock(&list_mutex_handle,FS_ENV_MAX_DELAY);
sys_dlist_append(&disk_access_list, &disk->node);
fs_env_mutex_unlock(&list_mutex_handle);
DISK_LOG("disk interface(%s) registered", disk->name);
return 0;
}
int disk_access_unregister(struct disk_info *disk)
{
if ((disk == NULL) || (disk->name == NULL))
{
DISK_LOG("invalid disk interface!!");
return -EINVAL;
}
if (disk_access_get_di(disk->name) == NULL)
{
DISK_LOG("disk interface not registered!!");
return -EINVAL;
}
/* remove disk node from the list */
fs_env_mutex_lock(&list_mutex_handle,FS_ENV_MAX_DELAY);
sys_dlist_remove(&disk->node);
fs_env_mutex_unlock(&list_mutex_handle);
DISK_LOG("disk interface(%s) unregistered", disk->name);
return 0;
}
int disk_init(const void *dev)
{
ARG_UNUSED(dev);
fs_env_mutex_create(&list_mutex_handle);
sys_dlist_init(&disk_access_list);
#if (CONFIG_DISK_DRIVER_FLASH)
extern int disk_flash_init(void *dev);
disk_flash_init(NULL);
#endif
#if (CONFIG_DISK_DRIVER_RAM)
extern int disk_ram_init(void *dev);
disk_ram_init(NULL);
#endif
#if (CONFIG_DISK_DRIVER_SDMMC)
extern int disk_sdmmc_init(void *dev);
disk_sdmmc_init(NULL);
#endif
return 0;
}

View File

@@ -0,0 +1,73 @@
#include "lisa_flash.h"
#include "arcs_ap.h"
#include "cache.h"
#include <stdint.h>
#define FLASH_DEVICE "flash0"
#define SECTOR_SIZE CONFIG_DISK_FLASH_SECTOR_SIZE
static uint8_t _s_flash_wrap_buf[CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE] __attribute__((section(".psram.data"),aligned(4))) ;
#define IS_IN_FLASH_REGION(addr) \
((uintptr_t)(addr) >= (uintptr_t)(CONFIG_DISK_FLASH_CHIP_BASE_ADDR) && \
(uintptr_t)(addr) < (uintptr_t)(CONFIG_DISK_FLASH_CHIP_BASE_ADDR) + (uintptr_t)(CONFIG_DISK_FLASH_CHIP_SIZE))
typedef struct
{
void *flash_dev;
} sflash_dev_t;
static inline int sflash_init(sflash_dev_t *dev, unsigned char ud0, unsigned char ud1)
{
int ret;
dev->flash_dev = lisa_device_get(FLASH_DEVICE);
return 0;
}
static inline int sflash_write(sflash_dev_t *dev, off_t offset, const void *data, size_t len)
{
int ret;
size_t num;
size_t remain = len;
/*如果原始数据在flash中,需要将数据搬运到内存中再进行写入*/
if (!IS_IN_FLASH_REGION(data))
{
do{
num = remain > CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE ? CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE : remain;
memcpy(_s_flash_wrap_buf, data, num);
ret = lisa_flash_write(dev->flash_dev, offset, _s_flash_wrap_buf, num);
if(ret != 0){
break;
}
offset += num;
data += num;
remain -= num;
}while(remain > 0);
}
else{
ret = lisa_flash_write(dev->flash_dev, offset, data, len);
}
return ret;
}
static inline int sflash_read(sflash_dev_t *dev, off_t offset, void *buffer, size_t size)
{
int ret;
ret = lisa_flash_read(dev->flash_dev, offset, buffer, size);
return ret;
}
static int sflash_erase(sflash_dev_t *dev, off_t offset, size_t size)
{
int ret;
ret = lisa_flash_erase(dev->flash_dev, offset, size);
return ret;
}

View File

@@ -0,0 +1,282 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <errno.h>
#include "disk/disk.h"
#include "fs_env/fs_env.h"
#include "flash_arcs.h"
static sflash_dev_t sflash_dev = {
.flash_dev = NULL,
};
/* flash read-copy-erase-write operation */
static uint8_t __aligned(4) read_copy_buf[CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE];
static uint8_t *fs_buff = read_copy_buf;
/* calculate number of blocks required for a given size */
#define GET_NUM_BLOCK(total_size, block_size) \
((total_size + block_size - 1) / block_size)
#define GET_SIZE_TO_BOUNDARY(start, block_size) \
(block_size - (start & (block_size - 1)))
static uint32_t lba_to_address(uint32_t sector_num)
{
uint32_t flash_addr;
flash_addr = CONFIG_DISK_FLASH_START_ADDR + sector_num * SECTOR_SIZE;
return flash_addr;
}
static int disk_flash_access_status(struct disk_info *disk)
{
if (!sflash_dev.flash_dev)
{
return DISK_STATUS_NOMEDIA;
}
return DISK_STATUS_OK;
}
static int disk_flash_access_init(struct disk_info *disk)
{
if (sflash_dev.flash_dev)
{
return 0;
}
sflash_init(&sflash_dev, 0, 0);
return 0;
}
static int disk_flash_access_read(struct disk_info *disk, uint8_t *buff,
uint32_t start_sector, uint32_t sector_count)
{
off_t fl_addr;
uint8_t* src_addr;
fl_addr = lba_to_address(start_sector);
src_addr = (uint8_t*)CONFIG_DISK_FLASH_CHIP_BASE_ADDR + fl_addr;
memcpy(buff, src_addr, sector_count*SECTOR_SIZE);
return 0;
}
/* This performs read-copy into an output buffer */
static int read_copy_flash_block(off_t start_addr, uint32_t size,
const void *src_buff,
uint8_t *dest_buff)
{
off_t fl_addr;
uint32_t num_read;
uint32_t offset = 0U;
/* adjust offset if starting address is not erase-aligned address */
if (start_addr & (CONFIG_DISK_FLASH_ERASE_ALIGNMENT - 1))
{
offset = start_addr & (CONFIG_DISK_FLASH_ERASE_ALIGNMENT - 1);
}
/* align starting address to an aligned address for flash erase-write */
fl_addr = ROUND_DOWN(start_addr, CONFIG_DISK_FLASH_ERASE_ALIGNMENT);
num_read = GET_NUM_BLOCK(CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE,
CONFIG_DISK_FLASH_MAX_RW_SIZE);
/* read one block from flash */
for (uint32_t i = 0; i < num_read; i++)
{
int rc;
rc = sflash_read(&sflash_dev,
fl_addr + (CONFIG_DISK_FLASH_MAX_RW_SIZE * i),
dest_buff + (CONFIG_DISK_FLASH_MAX_RW_SIZE * i),
CONFIG_DISK_FLASH_MAX_RW_SIZE);
if (rc != 0)
{
return -EIO;
}
}
/* overwrite with user data */
memcpy(dest_buff + offset, src_buff, size);
return 0;
}
/* input size is either less or equal to a block size,
* CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE.
*/
static int update_flash_block(off_t start_addr, uint32_t size, const void *buff)
{
off_t fl_addr;
uint8_t *src = (uint8_t *)buff;
uint32_t num_write;
/* if size is a partial block, perform read-copy with user data */
if (size < CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE)
{
int rc;
rc = read_copy_flash_block(start_addr, size, buff, fs_buff);
if (rc != 0)
{
return -EIO;
}
/* now use the local buffer as the source */
src = (uint8_t *)fs_buff;
}
/* always align starting address for flash write operation */
fl_addr = ROUND_DOWN(start_addr, CONFIG_DISK_FLASH_ERASE_ALIGNMENT);
if (sflash_erase(&sflash_dev, fl_addr, CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE) != 0)
{
return -EIO;
}
/* write data to flash */
num_write = GET_NUM_BLOCK(CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE,
CONFIG_DISK_FLASH_MAX_RW_SIZE);
for (uint32_t i = 0; i < num_write; i++)
{
if (sflash_write(&sflash_dev, fl_addr, src,
CONFIG_DISK_FLASH_MAX_RW_SIZE) != 0)
{
return -EIO;
}
fl_addr += CONFIG_DISK_FLASH_MAX_RW_SIZE;
src += CONFIG_DISK_FLASH_MAX_RW_SIZE;
}
return 0;
}
static int disk_flash_access_write(struct disk_info *disk, const uint8_t *buff,
uint32_t start_sector, uint32_t sector_count)
{
off_t fl_addr;
uint32_t remaining;
uint32_t size;
fl_addr = lba_to_address(start_sector);
remaining = (sector_count * SECTOR_SIZE);
/* check if start address is erased-aligned address */
if (fl_addr & (CONFIG_DISK_FLASH_ERASE_ALIGNMENT - 1))
{
off_t block_bnd;
/* not aligned */
/* check if the size goes over flash block boundary */
block_bnd = fl_addr + CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE;
block_bnd = block_bnd & ~(CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE - 1);
if ((fl_addr + remaining) < block_bnd)
{
/* not over block boundary (a partial block also) */
if (update_flash_block(fl_addr, remaining, buff) != 0)
{
return -EIO;
}
return 0;
}
/* write goes over block boundary */
size = GET_SIZE_TO_BOUNDARY(fl_addr,
CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE);
/* write first partial block */
if (update_flash_block(fl_addr, size, buff) != 0)
{
return -EIO;
}
fl_addr += size;
remaining -= size;
buff += size;
}
/* start is an erase-aligned address */
while (remaining)
{
int rc;
if (remaining < CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE)
{
break;
}
rc = update_flash_block(fl_addr, CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE,
buff);
if (rc != 0)
{
return -EIO;
}
fl_addr += CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE;
remaining -= CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE;
buff += CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE;
}
/* remaining partial block */
if (remaining)
{
if (update_flash_block(fl_addr, remaining, buff) != 0)
{
return -EIO;
}
}
return 0;
}
static int disk_flash_access_ioctl(struct disk_info *disk, uint8_t cmd, void *buff)
{
switch (cmd)
{
case DISK_IOCTL_CTRL_SYNC:
return 0;
case DISK_IOCTL_GET_SECTOR_COUNT:
*(uint32_t *)buff = CONFIG_DISK_FLASH_VOLUME_SIZE / SECTOR_SIZE;
return 0;
case DISK_IOCTL_GET_SECTOR_SIZE:
*(uint32_t *)buff = SECTOR_SIZE;
return 0;
case DISK_IOCTL_GET_ERASE_BLOCK_SZ: /* in sectors */
*(uint32_t *)buff = CONFIG_DISK_FLASH_ERASE_BLOCK_SIZE / SECTOR_SIZE;
return 0;
default:
break;
}
return -EINVAL;
}
static const struct disk_operations flash_disk_ops = {
.init = disk_flash_access_init,
.status = disk_flash_access_status,
.read = disk_flash_access_read,
.write = disk_flash_access_write,
.ioctl = disk_flash_access_ioctl,
};
static struct disk_info flash_disk = {
.name = CONFIG_DISK_FLASH_VOLUME_NAME,
.ops = &flash_disk_ops,
};
int disk_flash_init(void *dev)
{
ARG_UNUSED(dev);
return disk_access_register(&flash_disk);
}

View File

@@ -0,0 +1,142 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Disk Driver Interface
*
* This file contains interface for disk access. Apart from disks, various
* other storage media like Flash and RAM disks may implement this interface to
* be used by various higher layers(consumers) like USB Mass storage
* and Filesystems.
*/
#ifndef __DISK_H_
#define __DISK_H_
/**
* @brief Disk Driver Interface
* @defgroup disk_driver_interface Disk Driver Interface
* @{
*/
#include <stdint.h>
#include "fs_env/dlist.h"
#ifdef __cplusplus
extern "C"
{
#endif
#ifndef DISK_LOG
#define DISK_LOG(fmt, ...)
#endif
#ifndef ROUND_UP
#define ROUND_UP(x, align) \
(((unsigned long)(x) + ((unsigned long)(align) - 1)) & \
~((unsigned long)(align) - 1))
#endif
#ifndef ROUND_DOWN
#define ROUND_DOWN(x, align) \
((unsigned long)(x) & ~((unsigned long)(align) - 1))
#endif
/**
* @brief Possible Cmd Codes for disk_ioctl()
*/
/** Get the number of sectors in the disk */
#define DISK_IOCTL_GET_SECTOR_COUNT 1
/** Get the size of a disk SECTOR in bytes */
#define DISK_IOCTL_GET_SECTOR_SIZE 2
/** reserved. It used to be DISK_IOCTL_GET_DISK_SIZE */
#define DISK_IOCTL_RESERVED 3
/** How many sectors constitute a FLASH Erase block */
#define DISK_IOCTL_GET_ERASE_BLOCK_SZ 4
/** Commit any cached read/writes to disk */
#define DISK_IOCTL_CTRL_SYNC 5
#define DISK_IOCTL_BYTE_READ 6
struct disk_ioctl_byte_read
{
void *addr;
void *datas;
uint32_t size;
struct k_sem *break_sem;
};
/**
* @brief Possible return bitmasks for disk_status()
*/
/** Disk status okay */
#define DISK_STATUS_OK 0x00
/** Disk status uninitialized */
#define DISK_STATUS_UNINIT 0x01
/** Disk status no media */
#define DISK_STATUS_NOMEDIA 0x02
/** Disk status write protected */
#define DISK_STATUS_WR_PROTECT 0x04
struct disk_operations;
/**
* @brief Disk info
*/
struct disk_info
{
/** Internally used list node */
sys_dnode_t node;
/** Disk name */
char *name;
/** Disk operations */
const struct disk_operations *ops;
void *cache_instance; /* Pointer to cache instance */
};
/**
* @brief Disk operations
*/
struct disk_operations
{
int (*init)(struct disk_info *disk);
int (*status)(struct disk_info *disk);
int (*read)(struct disk_info *disk, uint8_t *data_buf,
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 (*ioctl)(struct disk_info *disk, uint8_t cmd, void *buff);
};
/**
* @brief Register disk
*
* @param[in] disk Pointer to the disk info structure
*
* @return 0 on success, negative errno code on fail
*/
int disk_access_register(struct disk_info *disk);
/**
* @brief Unregister disk
*
* @param[in] disk Pointer to the disk info structure
*
* @return 0 on success, negative errno code on fail
*/
int disk_access_unregister(struct disk_info *disk);
#ifdef __cplusplus
}
#endif
/**
* @}
*/
#endif /* __DISK_H_ */

View File

@@ -0,0 +1,100 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Disk Access layer API
*
* This file contains APIs for disk access.
*/
#ifndef _DISK_ACCESS_H_
#define _DISK_ACCESS_H_
/**
* @brief Disk Access APIs
* @defgroup disk_access_interface Disk Access Interface
* @{
*/
#include "disk.h"
#ifdef __cplusplus
extern "C"
{
#endif
int disk_init(void *dev);
/**
* @brief perform any initialization
*
* This call is made by the consumer before doing any IO calls so that the
* disk or the backing device can do any initialization.
*
* @param[in] pdrv Disk name
*
* @return 0 on success, negative errno code on fail
*/
int disk_access_init(const char *pdrv);
/**
* @brief Get the status of disk
*
* This call is used to get the status of the disk
*
* @param[in] pdrv Disk name
*
* @return DISK_STATUS_OK or other DISK_STATUS_*s
*/
int disk_access_status(const char *pdrv);
/**
* @brief read data from disk
*
* Function to read data from disk to a memory buffer.
*
* @param[in] pdrv Disk name
* @param[in] data_buf Pointer to the memory buffer to put data.
* @param[in] start_sector Start disk sector to read from
* @param[in] num_sector Number of disk sectors to read
*
* @return 0 on success, negative errno code on fail
*/
int disk_access_read(const char *pdrv, uint8_t *data_buf,
uint32_t start_sector, uint32_t num_sector);
/**
* @brief write data to disk
*
* Function write data from memory buffer to disk.
*
* @param[in] pdrv Disk name
* @param[in] data_buf Pointer to the memory buffer
* @param[in] start_sector Start disk sector to write to
* @param[in] num_sector Number of disk sectors to write
*
* @return 0 on success, negative errno code on fail
*/
int disk_access_write(const char *pdrv, const uint8_t *data_buf,
uint32_t start_sector, uint32_t num_sector);
/**
* @brief Get/Configure disk parameters
*
* Function to get disk parameters and make any special device requests.
*
* @param[in] pdrv Disk name
* @param[in] cmd DISK_IOCTL_* code describing the request
* @param[in] buff Command data buffer
*
* @return 0 on success, negative errno code on fail
*/
int disk_access_ioctl(const char *pdrv, uint8_t cmd, void *buff);
#ifdef __cplusplus
}
#endif
#endif /* _DISK_ACCESS_H_ */

View File

@@ -0,0 +1,107 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file iocache.h
* @brief IO Cache module header for disk read operations
*
* This module provides a configurable cache layer to reduce physical disk reads
* by caching frequently accessed sectors in memory using LRU replacement policy.
*/
#ifndef _IO_CACHE_H_
#define _IO_CACHE_H_
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Forward declaration */
struct disk_info;
/* Cache handle type for multi-instance support */
typedef struct io_cache_instance* io_cache_handle_t;
/**
* @brief IO Cache configuration structure
*/
struct io_cache_config {
uint32_t entry_count; /* Number of cache entries */
uint32_t max_block_size; /* Maximum block size in bytes */
uint32_t sector_size; /* Sector size in bytes */
};
/**
* @brief IO Cache statistics structure
*/
struct io_cache_stats {
uint32_t total_entries; /* Total cache entries */
uint32_t cached_entries; /* Currently cached entries */
uint32_t max_block_size; /* Maximum block size in bytes */
uint32_t sector_size; /* Sector size in bytes */
uint32_t cache_hits; /* Number of cache hits */
uint32_t cache_misses; /* Number of cache misses */
};
/**
* @brief Create and initialize IO cache instance
*
* @param disk Pointer to disk info structure
* @param config Cache configuration (NULL for defaults)
* @return Cache handle on success, NULL on failure
*/
io_cache_handle_t io_cache_create(struct disk_info *disk, const struct io_cache_config *config);
/**
* @brief Enable cache for a disk
*
* @param handle Cache handle
* @param disk Pointer to disk info structure
* @return 0 on success, negative errno on failure
*/
int io_cache_enable(io_cache_handle_t handle, struct disk_info *disk);
/**
* @brief Disable cache for a disk
*
* @param handle Cache handle
* @param disk Pointer to disk info structure
* @return 0 on success, negative errno on failure
*/
int io_cache_disable(io_cache_handle_t handle, struct disk_info *disk);
/**
* @brief Get cache statistics
*
* @param handle Cache handle
* @param stats Pointer to statistics structure
* @return 0 on success, negative errno on failure
*/
int io_cache_get_stats(io_cache_handle_t handle, struct io_cache_stats *stats);
/**
* @brief Flush all cache entries (mark as invalid)
*
* @param handle Cache handle
* @return 0 on success, negative errno on failure
*/
int io_cache_flush(io_cache_handle_t handle);
/**
* @brief Destroy cache instance and cleanup resources
*
* @param handle Cache handle
* @return 0 on success, negative errno on failure
*/
int io_cache_destroy(io_cache_handle_t handle);
#ifdef __cplusplus
}
#endif
#endif /* __IOCACHE_H_ */

View File

@@ -0,0 +1,533 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file iocache.c
* @brief IO Cache module for disk read operations
*
* This module provides a configurable cache layer to reduce physical disk reads
* by caching frequently accessed sectors in memory using LRU replacement policy.
*/
#include "disk/iocache.h"
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <stdbool.h>
#include "disk/disk.h"
#include "fs_env/fs_env.h"
/* Cache entry structure */
struct io_cache_entry {
uint32_t start_sector; /* Starting sector number */
uint32_t sector_count; /* Number of sectors cached */
uint8_t *data; /* Cached data buffer */
uint32_t access_time; /* LRU timestamp */
bool valid; /* Entry validity flag */
struct io_cache_entry *next;
struct io_cache_entry *prev;
};
/* Cache management structure */
struct io_cache {
struct io_cache_entry *entries; /* Cache entry pool */
struct io_cache_entry *lru_head; /* LRU list head (most recent) */
struct io_cache_entry *lru_tail; /* LRU list tail (least recent) */
uint8_t *data_pool; /* Data buffer pool */
uint32_t entry_count; /* Total cache entries */
uint32_t max_block_size; /* Maximum block size in bytes */
uint32_t sector_size; /* Sector size in bytes */
uint32_t access_counter; /* Global access counter for LRU */
const struct disk_operations *orig_ops; /* Original disk operations */
struct disk_info *disk; /* Associated disk */
fs_env_mutex_handle_t mutex; /* Thread safety mutex */
uint32_t cache_hits; /* Cache hit counter */
uint32_t cache_misses; /* Cache miss counter */
};
/* Cache instance management */
struct io_cache_instance {
struct io_cache cache;
struct disk_operations cached_ops; /* Modified operations for this instance */
bool initialized;
};
/**
* Initialize LRU list management
*/
static void lru_init(struct io_cache *cache)
{
cache->lru_head = NULL;
cache->lru_tail = NULL;
}
/**
* Move entry to head of LRU list (mark as most recently used)
*/
static void lru_move_to_head(struct io_cache *cache, struct io_cache_entry *entry)
{
if (cache->lru_head == entry) {
return; /* Already at head */
}
/* Remove from current position */
if (entry->prev) {
entry->prev->next = entry->next;
}
if (entry->next) {
entry->next->prev = entry->prev;
}
if (cache->lru_tail == entry) {
cache->lru_tail = entry->prev;
}
/* Insert at head */
entry->prev = NULL;
entry->next = cache->lru_head;
if (cache->lru_head) {
cache->lru_head->prev = entry;
}
cache->lru_head = entry;
if (!cache->lru_tail) {
cache->lru_tail = entry;
}
}
/**
* Get least recently used entry for eviction
*/
static struct io_cache_entry *lru_get_victim(struct io_cache *cache)
{
return cache->lru_tail;
}
/**
* Search for cache entry that contains the requested sector range
*/
static struct io_cache_entry *cache_find_entry(struct io_cache *cache,
uint32_t start_sector,
uint32_t sector_count)
{
for (uint32_t i = 0; i < cache->entry_count; i++) {
struct io_cache_entry *entry = &cache->entries[i];
if (!entry->valid) {
continue;
}
/* Check if requested range is fully contained in cached range */
if (start_sector >= entry->start_sector &&
(start_sector + sector_count) <= (entry->start_sector + entry->sector_count)) {
return entry;
}
}
return NULL;
}
/**
* Find an available cache entry (invalid or LRU victim)
*/
static struct io_cache_entry *cache_get_free_entry(struct io_cache *cache)
{
/* First try to find an invalid entry */
for (uint32_t i = 0; i < cache->entry_count; i++) {
if (!cache->entries[i].valid) {
return &cache->entries[i];
}
}
/* No free entry, use LRU victim */
struct io_cache_entry *victim = lru_get_victim(cache);
return victim;
}
/**
* Load data into cache entry from disk
*/
static int cache_load_entry(struct io_cache *cache,
struct io_cache_entry *entry,
uint32_t start_sector,
uint32_t sector_count)
{
int ret;
/* Calculate optimal read size (don't exceed max block size) */
uint32_t max_sectors = cache->max_block_size / cache->sector_size;
if (sector_count > max_sectors) {
sector_count = max_sectors;
}
/* Perform actual disk read using original operations */
ret = cache->orig_ops->read(cache->disk, entry->data, start_sector, sector_count);
if (ret != 0) {
return ret;
}
/* Update entry metadata */
entry->start_sector = start_sector;
entry->sector_count = sector_count;
entry->access_time = ++cache->access_counter;
entry->valid = true;
/* Update LRU list */
lru_move_to_head(cache, entry);
return 0;
}
/**
* Cached disk read operation
*/
static int io_cache_read(struct disk_info *disk, uint8_t *data_buf,
uint32_t start_sector, uint32_t sector_count)
{
struct io_cache_instance *instance = (struct io_cache_instance *)disk->cache_instance;
struct io_cache *cache;
struct io_cache_entry *entry;
int ret;
if (!instance || !instance->initialized || (sector_count > instance->cache.max_block_size / instance->cache.sector_size)) {
/* Cache not initialized, fall back to original operation */
return instance ? instance->cache.orig_ops->read(disk, data_buf, start_sector, sector_count) : -EINVAL;
}
cache = &instance->cache;
/* Lock for thread safety */
fs_env_mutex_lock(&cache->mutex, FS_ENV_MAX_DELAY);
/* Try to find data in cache */
entry = cache_find_entry(cache, start_sector, sector_count);
if (entry) {
/* Cache hit - copy data from cache */
uint32_t offset = (start_sector - entry->start_sector) * cache->sector_size;
memcpy(data_buf, entry->data + offset, sector_count * cache->sector_size);
/* Update LRU */
entry->access_time = ++cache->access_counter;
lru_move_to_head(cache, entry);
/* Update hit statistics */
cache->cache_hits++;
/* Unlock and return */
fs_env_mutex_unlock(&cache->mutex);
return 0;
}
/* Cache miss - need to load from disk */
cache->cache_misses++;
entry = cache_get_free_entry(cache);
if (!entry) {
/* Should not happen, but fall back to direct read */
fs_env_mutex_unlock(&cache->mutex);
return cache->orig_ops->read(disk, data_buf, start_sector, sector_count);
}
/* Load data into cache */
ret = cache_load_entry(cache, entry, start_sector, sector_count);
if (ret != 0) {
fs_env_mutex_unlock(&cache->mutex);
return ret;
}
/* Copy requested data to output buffer */
memcpy(data_buf, entry->data, sector_count * cache->sector_size);
/* Unlock */
fs_env_mutex_unlock(&cache->mutex);
return 0;
}
/**
* Cached disk write operation
* Write operations are performed directly to disk, but we need to handle
* cache coherency by invalidating overlapping cache entries.
*/
static int io_cache_write(struct disk_info *disk, const uint8_t *data_buf,
uint32_t start_sector, uint32_t sector_count)
{
struct io_cache_instance *instance = (struct io_cache_instance *)disk->cache_instance;
struct io_cache *cache;
int ret;
if (!instance || !instance->initialized) {
/* Cache not initialized, fall back to original operation */
return instance ? instance->cache.orig_ops->write(disk, data_buf, start_sector, sector_count) : -EINVAL;
}
cache = &instance->cache;
/* First, perform the actual write to disk */
ret = cache->orig_ops->write(disk, data_buf, start_sector, sector_count);
if (ret != 0) {
return ret;
}
/* Lock for thread safety before modifying cache */
fs_env_mutex_lock(&cache->mutex, FS_ENV_MAX_DELAY);
/* After successful write, update any overlapping cache entries */
uint32_t write_end = start_sector + sector_count;
for (uint32_t i = 0; i < cache->entry_count; i++) {
struct io_cache_entry *entry = &cache->entries[i];
if (!entry->valid) {
continue;
}
uint32_t entry_end = entry->start_sector + entry->sector_count;
/* Check if write range overlaps with cache entry */
if (start_sector < entry_end && write_end > entry->start_sector) {
/* Check if the write completely covers the cache entry */
if (start_sector <= entry->start_sector && write_end >= entry_end) {
/* Write completely covers cache entry - update entire entry */
uint32_t offset_in_write = (entry->start_sector - start_sector) * cache->sector_size;
uint32_t copy_size = entry->sector_count * cache->sector_size;
memcpy(entry->data, data_buf + offset_in_write, copy_size);
/* Update LRU - move to head since it was just written */
entry->access_time = ++cache->access_counter;
lru_move_to_head(cache, entry);
}
/* Check if the write is completely within the cache entry */
else if (start_sector >= entry->start_sector && write_end <= entry_end) {
/* Write is completely within cache entry - update partial data */
uint32_t offset_in_entry = (start_sector - entry->start_sector) * cache->sector_size;
uint32_t copy_size = sector_count * cache->sector_size;
memcpy(entry->data + offset_in_entry, data_buf, copy_size);
/* Update LRU - move to head since it was just written */
entry->access_time = ++cache->access_counter;
lru_move_to_head(cache, entry);
}
else {
/* Partial overlap - invalidate the cache entry to avoid inconsistency */
entry->valid = false;
/* Remove from LRU list */
if (cache->lru_head == entry) {
cache->lru_head = entry->next;
if (cache->lru_head) {
cache->lru_head->prev = NULL;
}
}
if (cache->lru_tail == entry) {
cache->lru_tail = entry->prev;
if (cache->lru_tail) {
cache->lru_tail->next = NULL;
}
}
if (entry->prev) {
entry->prev->next = entry->next;
}
if (entry->next) {
entry->next->prev = entry->prev;
}
entry->next = NULL;
entry->prev = NULL;
}
}
}
/* Unlock */
fs_env_mutex_unlock(&cache->mutex);
return 0;
}
/**
* Create and initialize IO cache instance
*/
io_cache_handle_t io_cache_create(struct disk_info *disk, const struct io_cache_config *config)
{
struct io_cache_instance *instance;
struct io_cache *cache;
uint32_t total_data_size;
if (!disk || !disk->ops || !disk->ops->read) {
return NULL;
}
/* Allocate cache instance */
instance = (struct io_cache_instance *)FS_ENV_MEM_MALLOC(sizeof(struct io_cache_instance));
if (!instance) {
return NULL;
}
cache = &instance->cache;
/* Initialize mutex for thread safety */
if (fs_env_mutex_create(&cache->mutex) != 0) {
FS_ENV_MEM_FREE(instance);
return NULL;
}
/* Use provided config or defaults */
cache->entry_count = config->entry_count;
cache->max_block_size = config->max_block_size;
cache->sector_size = config->sector_size;
/* Allocate cache entries */
cache->entries = (struct io_cache_entry *)FS_ENV_MEM_MALLOC(cache->entry_count * sizeof(struct io_cache_entry));
if (!cache->entries) {
fs_env_mutex_destroy(&cache->mutex);
FS_ENV_MEM_FREE(instance);
return NULL;
}
/* Allocate data pool */
total_data_size = cache->entry_count * cache->max_block_size;
cache->data_pool = (uint8_t *)FS_ENV_MEM_MALLOC(total_data_size);
if (!cache->data_pool) {
FS_ENV_MEM_FREE(cache->entries);
fs_env_mutex_destroy(&cache->mutex);
FS_ENV_MEM_FREE(instance);
return NULL;
}
/* Initialize cache entries */
for (uint32_t i = 0; i < cache->entry_count; i++) {
struct io_cache_entry *entry = &cache->entries[i];
entry->data = cache->data_pool + (i * cache->max_block_size);
entry->valid = false;
entry->access_time = 0;
entry->next = NULL;
entry->prev = NULL;
}
/* Initialize LRU list */
lru_init(cache);
/* Store original operations and disk reference */
cache->orig_ops = disk->ops;
cache->disk = disk;
cache->access_counter = 0;
/* Initialize statistics */
cache->cache_hits = 0;
cache->cache_misses = 0;
instance->initialized = true;
return instance;
}
/**
* Enable cache for a disk by replacing its read operation
*/
int io_cache_enable(io_cache_handle_t handle, struct disk_info *disk)
{
struct io_cache_instance *instance = (struct io_cache_instance *)handle;
if (!instance || !instance->initialized || !disk || !disk->ops) {
return -EINVAL;
}
/* Create new operations structure with cached read and write */
instance->cached_ops = *disk->ops;
instance->cached_ops.read = io_cache_read;
instance->cached_ops.write = io_cache_write;
/* Store cache instance in disk and replace operations */
disk->cache_instance = instance;
disk->ops = &instance->cached_ops;
return 0;
}
/**
* Disable cache and restore original operations
*/
int io_cache_disable(io_cache_handle_t handle, struct disk_info *disk)
{
struct io_cache_instance *instance = (struct io_cache_instance *)handle;
if (!instance || !instance->initialized || !disk) {
return -EINVAL;
}
/* Restore original operations */
disk->ops = instance->cache.orig_ops;
disk->cache_instance = NULL;
return 0;
}
/**
* Get cache statistics
*/
int io_cache_get_stats(io_cache_handle_t handle, struct io_cache_stats *stats)
{
struct io_cache_instance *instance = (struct io_cache_instance *)handle;
struct io_cache *cache;
if (!instance || !instance->initialized || !stats) {
return -EINVAL;
}
cache = &instance->cache;
memset(stats, 0, sizeof(*stats));
/* Lock for thread safety */
fs_env_mutex_lock(&cache->mutex, FS_ENV_MAX_DELAY);
/* Count valid entries */
for (uint32_t i = 0; i < cache->entry_count; i++) {
if (cache->entries[i].valid) {
stats->cached_entries++;
}
}
stats->total_entries = cache->entry_count;
stats->max_block_size = cache->max_block_size;
stats->sector_size = cache->sector_size;
stats->cache_hits = cache->cache_hits;
stats->cache_misses = cache->cache_misses;
/* Unlock */
fs_env_mutex_unlock(&cache->mutex);
return 0;
}
/**
* Destroy cache instance and cleanup resources
*/
int io_cache_destroy(io_cache_handle_t handle)
{
struct io_cache_instance *instance = (struct io_cache_instance *)handle;
struct io_cache *cache;
if (!instance) {
return 0;
}
cache = &instance->cache;
/* Free allocated memory */
if (cache->data_pool) {
FS_ENV_MEM_FREE(cache->data_pool);
}
if (cache->entries) {
FS_ENV_MEM_FREE(cache->entries);
}
/* Destroy mutex */
if (instance->initialized) {
fs_env_mutex_destroy(&cache->mutex);
}
FS_ENV_MEM_FREE(instance);
return 0;
}

View File

@@ -0,0 +1,208 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file iocache_example.c
* @brief Example usage of IO Cache module
*
* This file demonstrates how to integrate and use the IO cache module
* with existing disk operations to improve read performance.
*/
#include <stdio.h>
#include <string.h>
#include "iocache.h"
/* Example disk read implementation (simulates EMMC read) */
static int example_disk_read(struct disk_info *disk, uint8_t *data_buf,
uint32_t start_sector, uint32_t num_sector)
{
LS_ENV_LOG("Physical disk read: sector %u, count %u\n", start_sector, num_sector);
/* Simulate disk read delay */
for (volatile int i = 0; i < 1000000; i++);
/* Fill buffer with test data */
for (uint32_t i = 0; i < num_sector * 512; i++) {
data_buf[i] = (uint8_t)(start_sector + i);
}
return 0;
}
/* Example disk operations */
static const struct disk_operations example_disk_ops = {
.read = example_disk_read,
/* Other operations can be NULL for this example */
};
/* Example disk info */
static struct disk_info example_disk = {
.name = "emmc0",
.ops = &example_disk_ops,
};
/**
* Example function showing how to initialize and use IO cache
*/
int iocache_usage_example(void)
{
int ret;
uint8_t read_buffer[512];
struct io_cache_config config;
struct io_cache_stats stats;
io_cache_handle_t cache_handle;
LS_ENV_LOG("=== IO Cache Usage Example ===\n");
/* Configure cache parameters */
config.entry_count = 8; /* 8 cache entries */
config.max_block_size = 2048; /* 4 sectors max per entry */
config.sector_size = 512; /* Standard sector size */
/* Create cache instance with custom configuration */
cache_handle = io_cache_create(&example_disk, &config);
if (!cache_handle) {
LS_ENV_LOG("Failed to create cache instance\n");
return -1;
}
/* Enable cache for the disk */
ret = io_cache_enable(cache_handle, &example_disk);
if (ret != 0) {
LS_ENV_LOG("Failed to enable cache: %d\n", ret);
io_cache_destroy(cache_handle);
return ret;
}
LS_ENV_LOG("Cache initialized and enabled\n");
/* Demonstrate cache behavior */
LS_ENV_LOG("\n--- First read (cache miss) ---\n");
ret = example_disk.ops->read(&example_disk, read_buffer, 100, 1);
if (ret != 0) {
LS_ENV_LOG("Read failed: %d\n", ret);
return ret;
}
LS_ENV_LOG("\n--- Second read (cache hit) ---\n");
ret = example_disk.ops->read(&example_disk, read_buffer, 100, 1);
if (ret != 0) {
LS_ENV_LOG("Read failed: %d\n", ret);
return ret;
}
LS_ENV_LOG("\n--- Write to cached sector (creates dirty data) ---\n");
uint8_t write_data[512];
memset(write_data, 0xAA, sizeof(write_data));
ret = example_disk.ops->write(&example_disk, write_data, 100, 1);
if (ret != 0) {
LS_ENV_LOG("Write failed: %d\n", ret);
return ret;
}
LS_ENV_LOG("\n--- Read modified data (cache hit, dirty data) ---\n");
ret = example_disk.ops->read(&example_disk, read_buffer, 100, 1);
if (ret != 0) {
LS_ENV_LOG("Read failed: %d\n", ret);
return ret;
}
LS_ENV_LOG("First byte of read data: 0x%02X (should be 0xAA)\n", read_buffer[0]);
LS_ENV_LOG("\n--- Third read (different sector, cache miss) ---\n");
ret = example_disk.ops->read(&example_disk, read_buffer, 200, 1);
if (ret != 0) {
LS_ENV_LOG("Read failed: %d\n", ret);
return ret;
}
/* Get cache statistics */
ret = io_cache_get_stats(cache_handle, &stats);
if (ret == 0) {
LS_ENV_LOG("\n--- Cache Statistics ---\n");
LS_ENV_LOG("Total entries: %u\n", stats.total_entries);
LS_ENV_LOG("Cached entries: %u\n", stats.cached_entries);
LS_ENV_LOG("Max block size: %u bytes\n", stats.max_block_size);
LS_ENV_LOG("Sector size: %u bytes\n", stats.sector_size);
LS_ENV_LOG("Cache hits: %u\n", stats.cache_hits);
LS_ENV_LOG("Cache misses: %u\n", stats.cache_misses);
if (stats.cache_hits + stats.cache_misses > 0) {
uint32_t hit_rate = (stats.cache_hits * 100) / (stats.cache_hits + stats.cache_misses);
LS_ENV_LOG("Hit rate: %u%%\n", hit_rate);
}
}
/* Flush cache */
LS_ENV_LOG("\n--- Flushing cache ---\n");
io_cache_flush(cache_handle);
/* Disable cache */
ret = io_cache_disable(cache_handle, &example_disk);
if (ret != 0) {
LS_ENV_LOG("Failed to disable cache: %d\n", ret);
io_cache_destroy(cache_handle);
return ret;
}
LS_ENV_LOG("\n--- Read after cache disabled (direct disk access) ---\n");
ret = example_disk.ops->read(&example_disk, read_buffer, 100, 1);
if (ret != 0) {
LS_ENV_LOG("Read failed: %d\n", ret);
io_cache_destroy(cache_handle);
return ret;
}
/* Cleanup */
io_cache_destroy(cache_handle);
LS_ENV_LOG("\nCache cleanup completed\n");
return 0;
}
/**
* Integration example with existing SDMMC disk
*/
int integrate_with_sdmmc_example(void)
{
/* This function shows how to integrate with existing SDMMC implementation */
LS_ENV_LOG("\n=== SDMMC Integration Example ===\n");
LS_ENV_LOG("To integrate with existing SDMMC disk:\n\n");
LS_ENV_LOG("1. In sdmmc initialization code:\n");
LS_ENV_LOG(" struct io_cache_config cache_config = {\n");
LS_ENV_LOG(" .entry_count = 16,\n");
LS_ENV_LOG(" .max_block_size = 4096, // 8 sectors\n");
LS_ENV_LOG(" .sector_size = 512\n");
LS_ENV_LOG(" };\n");
LS_ENV_LOG(" io_cache_init(&sdmmc_disk, &cache_config);\n");
LS_ENV_LOG(" io_cache_enable(&sdmmc_disk);\n\n");
LS_ENV_LOG("2. The cache will automatically intercept disk reads\n");
LS_ENV_LOG("3. File system operations will benefit from caching\n");
LS_ENV_LOG("4. No changes needed to existing file system code\n\n");
LS_ENV_LOG("Configuration recommendations:\n");
LS_ENV_LOG("- entry_count: 8-32 (depends on available memory)\n");
LS_ENV_LOG("- max_block_size: 2048-8192 bytes (4-16 sectors)\n");
LS_ENV_LOG("- sector_size: 512 bytes (standard)\n\n");
return 0;
}
/* Main function for testing */
#ifdef IOCACHE_STANDALONE_TEST
int main(void)
{
int ret;
ret = iocache_usage_example();
if (ret != 0) {
return ret;
}
ret = integrate_with_sdmmc_example();
return ret;
}
#endif

View File

@@ -0,0 +1,102 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <errno.h>
#include "disk/disk.h"
#include "fs_env/fs_env.h"
static uint8_t __attribute__((section(CONFIG_DISK_RAM_MEMORY_SECTION_NAME)))
memory_pool[CONFIG_DISK_RAM_SECTOR_SIZE * CONFIG_DISK_RAM_SECTOR_COUNT];
static void *lba_to_address(uint32_t lba)
{
return &memory_pool[lba * CONFIG_DISK_RAM_SECTOR_SIZE];
}
static int disk_ram_access_status(struct disk_info *disk)
{
return DISK_STATUS_OK;
}
static int disk_ram_access_read(struct disk_info *disk, uint8_t *buff,
uint32_t sector, uint32_t count)
{
uint32_t last_sector = sector + count;
if (last_sector < sector || last_sector > CONFIG_DISK_RAM_SECTOR_COUNT)
{
DISK_LOG("Sector %" PRIu32 " is outside the range %zu",
last_sector, CONFIG_DISK_RAM_SECTOR_COUNT);
return -EIO;
}
memcpy(buff, lba_to_address(sector), count * CONFIG_DISK_RAM_SECTOR_SIZE);
return 0;
}
static int disk_ram_access_write(struct disk_info *disk, const uint8_t *buff,
uint32_t sector, uint32_t count)
{
uint32_t last_sector = sector + count;
if (last_sector < sector || last_sector > CONFIG_DISK_RAM_SECTOR_COUNT)
{
DISK_LOG("Sector %" PRIu32 " is outside the range %zu",
last_sector, CONFIG_DISK_RAM_SECTOR_COUNT);
return -EIO;
}
memcpy(lba_to_address(sector), buff, count * CONFIG_DISK_RAM_SECTOR_SIZE);
return 0;
}
static int disk_ram_access_ioctl(struct disk_info *disk, uint8_t cmd, void *buff)
{
switch (cmd)
{
case DISK_IOCTL_CTRL_SYNC:
return 0;
case DISK_IOCTL_GET_SECTOR_COUNT:
*(uint32_t *)buff = CONFIG_DISK_RAM_SECTOR_COUNT;
return 0;
case DISK_IOCTL_GET_SECTOR_SIZE:
*(uint32_t *)buff = CONFIG_DISK_RAM_SECTOR_SIZE;
return 0;
case DISK_IOCTL_GET_ERASE_BLOCK_SZ: /* in sectors */
*(uint32_t *)buff = 1U;
return 0;
default:
break;
}
return -EINVAL;
}
static int disk_ram_access_init(struct disk_info *disk)
{
return 0;
}
static const struct disk_operations ram_disk_ops = {
.init = disk_ram_access_init,
.status = disk_ram_access_status,
.read = disk_ram_access_read,
.write = disk_ram_access_write,
.ioctl = disk_ram_access_ioctl,
};
static struct disk_info ram_disk = {
.name = CONFIG_DISK_RAM_VOLUME_NAME,
.ops = &ram_disk_ops,
};
int disk_ram_init(const void *dev)
{
ARG_UNUSED(dev);
return disk_access_register(&ram_disk);
}

View File

@@ -0,0 +1,226 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file sdmmc.c
* @brief SD/MMC disk 适配层 - 基于 lisa_sdmmc 驱动接口
*
* 本文件将 lisa_sdmmc 驱动接口适配到 FS 层的 disk_operations 接口,
* 使得文件系统可以通过统一的 disk_access API 访问 SD/MMC 存储设备。
*/
#include <string.h>
#include <errno.h>
#include "fs_env/fs_env.h"
#include "disk/disk.h"
#include "disk/iocache.h"
#include "lisa_device.h"
#include "lisa_sdmmc.h"
/* lisa_sdmmc 设备指针 */
static lisa_device_t *lisa_sdmmc_dev = NULL;
/* FS 层 disk_info 结构体 */
static struct disk_info sdmmc_disk_raw;
static struct disk_info sdmmc_disk;
/**
* @brief 获取 lisa_sdmmc 设备
*
* 延迟获取设备,确保设备已注册
*/
static lisa_device_t *get_lisa_sdmmc_dev(void)
{
if (lisa_sdmmc_dev == NULL) {
lisa_sdmmc_dev = lisa_device_get("sdmmc0");
}
return lisa_sdmmc_dev;
}
static int disk_sdmmc_access_init(struct disk_info *disk)
{
ARG_UNUSED(disk);
// Don't do any initialization in this function.
// There are issues if sdmmc initialized called by file system.
return 0;
}
static int disk_sdmmc_access_status(struct disk_info *disk)
{
ARG_UNUSED(disk);
lisa_device_t *dev = get_lisa_sdmmc_dev();
if (dev == NULL) {
return DISK_STATUS_NOMEDIA;
}
int status = lisa_sdmmc_status(dev);
/* 转换 lisa_sdmmc 状态到 FS 层状态 */
switch (status) {
case LISA_SDMMC_STATUS_OK:
return DISK_STATUS_OK;
case LISA_SDMMC_STATUS_UNINIT:
return DISK_STATUS_UNINIT;
case LISA_SDMMC_STATUS_NO_MEDIA:
return DISK_STATUS_NOMEDIA;
case LISA_SDMMC_STATUS_BUSY:
default:
return DISK_STATUS_OK;
}
}
static int disk_sdmmc_access_read(struct disk_info *disk, uint8_t *buff,
uint32_t sector, uint32_t count)
{
lisa_device_t *dev = get_lisa_sdmmc_dev();
if (dev == NULL) {
return -EIO;
}
/* RAW 模式不偏移扇区,普通模式从配置的起始扇区开始 */
if (disk != &sdmmc_disk_raw) {
sector += CONFIG_DISK_SDMMC_START_SECTOR_NUMBER;
}
int ret = lisa_sdmmc_read(dev, buff, sector, count);
if (ret != 0) {
return -EIO;
}
return 0;
}
static int disk_sdmmc_access_write(struct disk_info *disk, const uint8_t *buff,
uint32_t sector, uint32_t count)
{
lisa_device_t *dev = get_lisa_sdmmc_dev();
if (dev == NULL) {
return -EIO;
}
/* RAW 模式不偏移扇区,普通模式从配置的起始扇区开始 */
if (disk != &sdmmc_disk_raw) {
sector += CONFIG_DISK_SDMMC_START_SECTOR_NUMBER;
}
int ret = lisa_sdmmc_write(dev, buff, sector, count);
if (ret != 0) {
return -EIO;
}
return 0;
}
static int disk_sdmmc_access_ioctl(struct disk_info *disk, uint8_t cmd, void *buff)
{
lisa_device_t *dev = get_lisa_sdmmc_dev();
uint32_t value;
int ret;
/* SYNC 命令不需要设备 */
if (cmd == DISK_IOCTL_CTRL_SYNC) {
if (dev != NULL) {
lisa_sdmmc_sync(dev);
}
return 0;
}
if (dev == NULL) {
return -EIO;
}
switch (cmd) {
case DISK_IOCTL_GET_SECTOR_COUNT:
ret = lisa_sdmmc_ioctl(dev, LISA_SDMMC_IOCTL_GET_SECTOR_COUNT, &value);
if (ret != 0) {
return -EIO;
}
/* 普通模式需要减去起始扇区偏移 */
if (disk != &sdmmc_disk_raw) {
value -= CONFIG_DISK_SDMMC_START_SECTOR_NUMBER;
}
*(uint32_t *)buff = value;
return 0;
case DISK_IOCTL_GET_SECTOR_SIZE:
ret = lisa_sdmmc_ioctl(dev, LISA_SDMMC_IOCTL_GET_SECTOR_SIZE, &value);
if (ret != 0) {
return -EIO;
}
*(uint32_t *)buff = value;
return 0;
case DISK_IOCTL_GET_ERASE_BLOCK_SZ:
ret = lisa_sdmmc_ioctl(dev, LISA_SDMMC_IOCTL_GET_ERASE_BLOCK_SZ, &value);
if (ret != 0) {
return -EIO;
}
*(uint32_t *)buff = value;
return 0;
default:
break;
}
return -EINVAL;
}
static const struct disk_operations sdmmc_disk_ops = {
.init = disk_sdmmc_access_init,
.status = disk_sdmmc_access_status,
.read = disk_sdmmc_access_read,
.write = disk_sdmmc_access_write,
.ioctl = disk_sdmmc_access_ioctl,
};
static struct disk_info sdmmc_disk = {
.name = CONFIG_DISK_SDMMC_VOLUME_NAME,
.ops = &sdmmc_disk_ops,
};
static const struct disk_operations sdmmc_disk_raw_ops = {
.init = disk_sdmmc_access_init,
.status = disk_sdmmc_access_status,
.read = disk_sdmmc_access_read,
.write = disk_sdmmc_access_write,
.ioctl = disk_sdmmc_access_ioctl,
};
static struct disk_info sdmmc_disk_raw = {
.name = "SDRAW",
.ops = &sdmmc_disk_raw_ops,
};
int disk_sdmmc_init(const void *dev)
{
int r;
ARG_UNUSED(dev);
r = disk_access_register(&sdmmc_disk);
if (r) {
return r;
}
r = disk_access_register(&sdmmc_disk_raw);
if (r) {
return r;
}
#if CONFIG_DISK_SDMMC_IOCACHE_ENABLE
struct io_cache_config cache_config;
io_cache_handle_t cache_handle;
cache_config.entry_count = CONFIG_DISK_SDMMC_IOCACHE_ENTRY_COUNT;
cache_config.max_block_size = CONFIG_DISK_SDMMC_IOCACHE_MAX_BLOCK_SIZE;
cache_config.sector_size = CONFIG_DISK_SDMMC_IOCACHE_SECTOR_SIZE;
cache_handle = io_cache_create(&sdmmc_disk, &cache_config);
io_cache_enable(cache_handle, &sdmmc_disk);
#endif
return 0;
}

View File

@@ -0,0 +1,8 @@
#include <stdint.h>
#include "lib_sdc.h"
#include "drv_sdc.h"
static inline int sdmmc_init(void)
{
return 0;
}