Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/nyx/nyx_gui/libs/fatfs/diskio.c
1476 views
1
/*-----------------------------------------------------------------------*/
2
/* Low level disk I/O module skeleton for FatFs */
3
/* (C) ChaN, 2016 */
4
/* (C) CTCaer, 2018-2020 */
5
/*-----------------------------------------------------------------------*/
6
/* If a working storage control module is available, it should be */
7
/* attached to the FatFs via a glue function rather than modifying it. */
8
/* This is an example of glue functions to attach various exsisting */
9
/* storage control modules to the FatFs module with a defined API. */
10
/*-----------------------------------------------------------------------*/
11
12
#include <string.h>
13
14
#include <bdk.h>
15
16
#include <libs/fatfs/diskio.h> /* FatFs lower layer API */
17
18
static u32 sd_rsvd_sectors = 0;
19
static u32 ramdisk_sectors = 0;
20
static u32 emummc_sectors = 0;
21
22
/*-----------------------------------------------------------------------*/
23
/* Get Drive Status */
24
/*-----------------------------------------------------------------------*/
25
DSTATUS disk_status (
26
BYTE pdrv /* Physical drive nmuber to identify the drive */
27
)
28
{
29
return 0;
30
}
31
32
/*-----------------------------------------------------------------------*/
33
/* Inidialize a Drive */
34
/*-----------------------------------------------------------------------*/
35
DSTATUS disk_initialize (
36
BYTE pdrv /* Physical drive nmuber to identify the drive */
37
)
38
{
39
return 0;
40
}
41
42
/*-----------------------------------------------------------------------*/
43
/* Read Sector(s) */
44
/*-----------------------------------------------------------------------*/
45
DRESULT disk_read (
46
BYTE pdrv, /* Physical drive nmuber to identify the drive */
47
BYTE *buff, /* Data buffer to store read data */
48
DWORD sector, /* Start sector in LBA */
49
UINT count /* Number of sectors to read */
50
)
51
{
52
switch (pdrv)
53
{
54
case DRIVE_SD:
55
return sdmmc_storage_read(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;
56
case DRIVE_RAM:
57
return ram_disk_read(sector, count, (void *)buff);
58
case DRIVE_EMMC:
59
return sdmmc_storage_read(&emmc_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;
60
case DRIVE_BIS:
61
case DRIVE_EMU:
62
return nx_emmc_bis_read(sector, count, (void *)buff) ? RES_OK : RES_ERROR;
63
}
64
65
return RES_ERROR;
66
}
67
68
/*-----------------------------------------------------------------------*/
69
/* Write Sector(s) */
70
/*-----------------------------------------------------------------------*/
71
DRESULT disk_write (
72
BYTE pdrv, /* Physical drive nmuber to identify the drive */
73
const BYTE *buff, /* Data to be written */
74
DWORD sector, /* Start sector in LBA */
75
UINT count /* Number of sectors to write */
76
)
77
{
78
switch (pdrv)
79
{
80
case DRIVE_SD:
81
return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;
82
case DRIVE_RAM:
83
return ram_disk_write(sector, count, (void *)buff);
84
case DRIVE_EMMC:
85
case DRIVE_BIS:
86
return RES_WRPRT;
87
case DRIVE_EMU:
88
return nx_emmc_bis_write(sector, count, (void *)buff) ? RES_OK : RES_ERROR;
89
}
90
91
return RES_ERROR;
92
}
93
94
/*-----------------------------------------------------------------------*/
95
/* Miscellaneous Functions */
96
/*-----------------------------------------------------------------------*/
97
DRESULT disk_ioctl (
98
BYTE pdrv, /* Physical drive nmuber (0..) */
99
BYTE cmd, /* Control code */
100
void *buff /* Buffer to send/receive control data */
101
)
102
{
103
DWORD *buf = (DWORD *)buff;
104
105
if (pdrv == DRIVE_SD)
106
{
107
switch (cmd)
108
{
109
case GET_SECTOR_COUNT:
110
*buf = sd_storage.sec_cnt - sd_rsvd_sectors;
111
break;
112
case GET_BLOCK_SIZE:
113
*buf = 32768; // Align to 16MB.
114
break;
115
}
116
}
117
else if (pdrv == DRIVE_RAM)
118
{
119
switch (cmd)
120
{
121
case GET_SECTOR_COUNT:
122
*buf = ramdisk_sectors;
123
break;
124
case GET_BLOCK_SIZE:
125
*buf = 2048; // Align to 1MB.
126
break;
127
}
128
}
129
else if (pdrv == DRIVE_EMU)
130
{
131
switch (cmd)
132
{
133
case GET_SECTOR_COUNT:
134
*buf = emummc_sectors;
135
break;
136
case GET_BLOCK_SIZE:
137
*buf = 32768; // Align to 16MB.
138
break;
139
}
140
}
141
else // Catch all for unknown devices.
142
{
143
switch (cmd)
144
{
145
case CTRL_SYNC:
146
break;
147
case GET_SECTOR_COUNT:
148
case GET_BLOCK_SIZE:
149
*buf = 0; // Zero value to force default or abort.
150
break;
151
}
152
}
153
154
return RES_OK;
155
}
156
157
DRESULT disk_set_info (
158
BYTE pdrv, /* Physical drive nmuber (0..) */
159
BYTE cmd, /* Control code */
160
void *buff /* Buffer to send/receive control data */
161
)
162
{
163
DWORD *buf = (DWORD *)buff;
164
165
if (cmd == SET_SECTOR_COUNT)
166
{
167
switch (pdrv)
168
{
169
case DRIVE_SD:
170
sd_rsvd_sectors = *buf;
171
break;
172
case DRIVE_RAM:
173
ramdisk_sectors = *buf;
174
break;
175
case DRIVE_EMU:
176
emummc_sectors = *buf;
177
break;
178
}
179
}
180
181
return RES_OK;
182
}
183
184