Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/nyx/nyx_gui/libs/fatfs/diskio.c
2598 views
1
/*-----------------------------------------------------------------------*/
2
/* Low level disk I/O module skeleton for FatFs */
3
/* (C) ChaN, 2016 */
4
/* (C) CTCaer, 2018-2025 */
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 bis_sectors = 0;
21
static u32 emummc_sectors = 0;
22
23
static bool bis_write_allowed = false;
24
25
/*-----------------------------------------------------------------------*/
26
/* Get Drive Status */
27
/*-----------------------------------------------------------------------*/
28
DSTATUS disk_status (
29
BYTE pdrv /* Physical drive nmuber to identify the drive */
30
)
31
{
32
return 0;
33
}
34
35
/*-----------------------------------------------------------------------*/
36
/* Inidialize a Drive */
37
/*-----------------------------------------------------------------------*/
38
DSTATUS disk_initialize (
39
BYTE pdrv /* Physical drive nmuber to identify the drive */
40
)
41
{
42
return 0;
43
}
44
45
/*-----------------------------------------------------------------------*/
46
/* Read Sector(s) */
47
/*-----------------------------------------------------------------------*/
48
DRESULT disk_read (
49
BYTE pdrv, /* Physical drive nmuber to identify the drive */
50
BYTE *buff, /* Data buffer to store read data */
51
DWORD sector, /* Start sector in LBA */
52
UINT count /* Number of sectors to read */
53
)
54
{
55
switch (pdrv)
56
{
57
case DRIVE_SD:
58
return sdmmc_storage_read(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;
59
case DRIVE_RAM:
60
return ram_disk_read(sector, count, (void *)buff);
61
case DRIVE_EMMC:
62
return sdmmc_storage_read(&emmc_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;
63
case DRIVE_BIS:
64
case DRIVE_EMU:
65
return nx_emmc_bis_read(sector, count, (void *)buff) ? RES_OK : RES_ERROR;
66
}
67
68
return RES_ERROR;
69
}
70
71
/*-----------------------------------------------------------------------*/
72
/* Write Sector(s) */
73
/*-----------------------------------------------------------------------*/
74
DRESULT disk_write (
75
BYTE pdrv, /* Physical drive nmuber to identify the drive */
76
const BYTE *buff, /* Data to be written */
77
DWORD sector, /* Start sector in LBA */
78
UINT count /* Number of sectors to write */
79
)
80
{
81
switch (pdrv)
82
{
83
case DRIVE_SD:
84
return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;
85
case DRIVE_RAM:
86
return ram_disk_write(sector, count, (void *)buff);
87
case DRIVE_EMMC:
88
return RES_WRPRT;
89
case DRIVE_BIS:
90
case DRIVE_EMU:
91
if (pdrv == DRIVE_BIS && !bis_write_allowed)
92
return RES_WRPRT;
93
return nx_emmc_bis_write(sector, count, (void *)buff) ? RES_OK : RES_ERROR;
94
}
95
96
return RES_ERROR;
97
}
98
99
/*-----------------------------------------------------------------------*/
100
/* Miscellaneous Functions */
101
/*-----------------------------------------------------------------------*/
102
DRESULT disk_ioctl (
103
BYTE pdrv, /* Physical drive nmuber (0..) */
104
BYTE cmd, /* Control code */
105
void *buff /* Buffer to send/receive control data */
106
)
107
{
108
DWORD *buf = (DWORD *)buff;
109
110
switch (pdrv)
111
{
112
case DRIVE_SD:
113
switch (cmd)
114
{
115
case GET_SECTOR_COUNT:
116
*buf = sd_storage.sec_cnt - sd_rsvd_sectors;
117
break;
118
case GET_BLOCK_SIZE:
119
*buf = 32768; // Align to 16MB.
120
break;
121
}
122
break;
123
124
case DRIVE_RAM:
125
switch (cmd)
126
{
127
case GET_SECTOR_COUNT:
128
*buf = ramdisk_sectors;
129
break;
130
case GET_BLOCK_SIZE:
131
*buf = 2048; // Align to 1MB.
132
break;
133
}
134
break;
135
136
case DRIVE_BIS:
137
switch (cmd)
138
{
139
case GET_SECTOR_COUNT:
140
*buf = bis_sectors;
141
break;
142
case GET_BLOCK_SIZE:
143
*buf = 32768; // Align to 16MB.
144
break;
145
}
146
break;
147
148
case DRIVE_EMU:
149
switch (cmd)
150
{
151
case GET_SECTOR_COUNT:
152
*buf = emummc_sectors;
153
break;
154
case GET_BLOCK_SIZE:
155
*buf = 16384; // Align to 8MB (With BOOT0/1 data will be at 16MB BU).
156
break;
157
}
158
break;
159
160
default: // Catch all for unknown devices.
161
switch (cmd)
162
{
163
case CTRL_SYNC:
164
break;
165
case GET_SECTOR_COUNT:
166
case GET_BLOCK_SIZE:
167
*buf = 0; // Zero value to force default or abort.
168
break;
169
}
170
break;
171
}
172
173
return RES_OK;
174
}
175
176
DRESULT disk_set_info (
177
BYTE pdrv, /* Physical drive nmuber (0..) */
178
BYTE cmd, /* Control code */
179
void *buff /* Buffer to send/receive control data */
180
)
181
{
182
DWORD *buf = (DWORD *)buff;
183
184
if (cmd == SET_SECTOR_COUNT)
185
{
186
switch (pdrv)
187
{
188
case DRIVE_SD:
189
sd_rsvd_sectors = *buf;
190
break;
191
192
case DRIVE_RAM:
193
ramdisk_sectors = *buf;
194
break;
195
196
case DRIVE_BIS:
197
bis_sectors = *buf;
198
break;
199
200
case DRIVE_EMU:
201
emummc_sectors = *buf;
202
break;
203
}
204
}
205
else if (cmd == SET_WRITE_PROTECT && pdrv == DRIVE_BIS)
206
bis_write_allowed = *(bool *)buff;
207
208
return RES_OK;
209
}
210
211