SimpleKernel 1.17.0
Loading...
Searching...
No Matches
diskio.cpp File Reference
#include <ff.h>
#include <diskio.h>
#include "fatfs.hpp"
#include "kernel_log.hpp"
Include dependency graph for diskio.cpp:

Go to the source code of this file.

Functions

auto get_fattime () -> DWORD
 返回 FAT 时间戳。无 RTC 时返回 0(epoch)。
 
auto disk_status (BYTE pdrv) -> DSTATUS
 查询磁盘驱动器状态。
 
auto disk_initialize (BYTE pdrv) -> DSTATUS
 初始化磁盘驱动器。
 
auto disk_read (BYTE pdrv, BYTE *buff, LBA_t sector, UINT count) -> DRESULT
 从磁盘读取扇区。
 
auto disk_write (BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count) -> DRESULT
 向磁盘写入扇区。
 
auto disk_ioctl (BYTE pdrv, BYTE cmd, void *buff) -> DRESULT
 执行磁盘 I/O 控制操作。
 

Function Documentation

◆ disk_initialize()

auto disk_initialize ( BYTE  pdrv) -> DSTATUS

初始化磁盘驱动器。

Note
BlockDevice 已由调用方在注册时完成初始化,此函数仅检查设备是否存在。
Parameters
pdrv物理驱动器编号。
Returns
DSTATUS 驱动器状态标志;如果设备未注册则返回 STA_NOINIT。

Definition at line 44 of file diskio.cpp.

44 {
45 if (fatfs::FatFsFileSystem::GetBlockDevice(pdrv) == nullptr) {
46 return STA_NOINIT;
47 }
48 return 0;
49}
static auto GetBlockDevice(uint8_t pdrv) -> vfs::BlockDevice *
获取指定驱动器的块设备
Definition fatfs.cpp:85
Here is the call graph for this function:

◆ disk_ioctl()

auto disk_ioctl ( BYTE  pdrv,
BYTE  cmd,
void *  buff 
) -> DRESULT

执行磁盘 I/O 控制操作。

Parameters
pdrv物理驱动器编号。
cmd控制命令(CTRL_SYNC、GET_SECTOR_COUNT、GET_SECTOR_SIZE 等)。
buff命令参数/结果缓冲区,含义取决于 cmd。
Returns
DRESULT 操作结果;不支持的命令返回 RES_PARERR。

Definition at line 108 of file diskio.cpp.

108 {
110 if (dev == nullptr) {
111 return RES_NOTRDY;
112 }
113 switch (cmd) {
114 case CTRL_SYNC: {
115 auto r = dev->Flush();
116 return r ? RES_OK : RES_ERROR;
117 }
118 case GET_SECTOR_COUNT:
119 if (buff == nullptr) {
120 return RES_PARERR;
121 }
122 static_cast<LBA_t*>(buff)[0] = static_cast<LBA_t>(dev->GetSectorCount());
123 return RES_OK;
124 case GET_SECTOR_SIZE:
125 if (buff == nullptr) {
126 return RES_PARERR;
127 }
128 static_cast<WORD*>(buff)[0] = static_cast<WORD>(dev->GetSectorSize());
129 return RES_OK;
130 case GET_BLOCK_SIZE:
131 if (buff == nullptr) {
132 return RES_PARERR;
133 }
134 static_cast<DWORD*>(buff)[0] = 1;
135 return RES_OK;
136 case CTRL_TRIM:
137 return RES_OK; // TRIM 不是必需的
138 default:
139 return RES_PARERR;
140 }
141}
Here is the call graph for this function:

◆ disk_read()

auto disk_read ( BYTE  pdrv,
BYTE *  buff,
LBA_t  sector,
UINT  count 
) -> DRESULT

从磁盘读取扇区。

Parameters
pdrv物理驱动器编号。
buff目标缓冲区指针。
sector起始逻辑块地址。
count要读取的扇区数。
Returns
DRESULT 操作结果;RES_OK 表示成功。

Definition at line 60 of file diskio.cpp.

60 {
62 if (dev == nullptr) {
63 return RES_NOTRDY;
64 }
65 auto result = dev->ReadSectors(sector, static_cast<uint32_t>(count), buff);
66 if (!result) {
67 klog::Err("disk_read: pdrv={} sector={} count={} failed",
68 static_cast<unsigned>(pdrv), static_cast<uint64_t>(sector),
69 static_cast<unsigned>(count));
70 return RES_ERROR;
71 }
72 return RES_OK;
73}
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志
Here is the call graph for this function:

◆ disk_status()

auto disk_status ( BYTE  pdrv) -> DSTATUS

查询磁盘驱动器状态。

Parameters
pdrv物理驱动器编号。
Returns
DSTATUS 驱动器状态标志;如果设备未注册则返回 STA_NOINIT。

Definition at line 30 of file diskio.cpp.

30 {
31 if (fatfs::FatFsFileSystem::GetBlockDevice(pdrv) == nullptr) {
32 return STA_NOINIT;
33 }
34 return 0;
35}
Here is the call graph for this function:

◆ disk_write()

auto disk_write ( BYTE  pdrv,
const BYTE *  buff,
LBA_t  sector,
UINT  count 
) -> DRESULT

向磁盘写入扇区。

Parameters
pdrv物理驱动器编号。
buff源数据缓冲区指针。
sector起始逻辑块地址。
count要写入的扇区数。
Returns
DRESULT 操作结果;RES_OK 表示成功。

Definition at line 84 of file diskio.cpp.

85 {
87 if (dev == nullptr) {
88 return RES_NOTRDY;
89 }
90 auto result = dev->WriteSectors(sector, static_cast<uint32_t>(count), buff);
91 if (!result) {
92 klog::Err("disk_write: pdrv={} sector={} count={} failed",
93 static_cast<unsigned>(pdrv), static_cast<uint64_t>(sector),
94 static_cast<unsigned>(count));
95 return RES_ERROR;
96 }
97 return RES_OK;
98}
Here is the call graph for this function:

◆ get_fattime()

auto get_fattime ( ) -> DWORD

返回 FAT 时间戳。无 RTC 时返回 0(epoch)。

Note
FF_FS_READONLY == 0 时 FatFS 要求此函数存在。
Returns
DWORD 始终返回 0。

Definition at line 22 of file diskio.cpp.

22{ return 0; }