]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/ff14/source/diskio.c
bebe6253c8491e937f4b8dd426975ea21a6562c3
[Ventoy.git] / Ventoy2Disk / Ventoy2Disk / ff14 / source / diskio.c
1 /*-----------------------------------------------------------------------*/
2 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2019 */
3 /*-----------------------------------------------------------------------*/
4 /* If a working storage control module is available, it should be */
5 /* attached to the FatFs via a glue function rather than modifying it. */
6 /* This is an example of glue functions to attach various exsisting */
7 /* storage control modules to the FatFs module with a defined API. */
8 /*-----------------------------------------------------------------------*/
9
10 #include <Windows.h>
11
12 #include "ff.h" /* Obtains integer types */
13 #include "diskio.h" /* Declarations of disk functions */
14
15 /* Definitions of physical drive number for each drive */
16 #define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */
17 #define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
18 #define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */
19
20 void Log(const char *fmt, ...);
21
22 static UINT8 g_MbrSector[512];
23 HANDLE g_hPhyDrive;
24 UINT64 g_SectorCount;
25
26 void disk_io_set_param(HANDLE Handle, UINT64 SectorCount)
27 {
28 g_hPhyDrive = Handle;
29 g_SectorCount = SectorCount;
30 }
31
32 /*-----------------------------------------------------------------------*/
33 /* Get Drive Status */
34 /*-----------------------------------------------------------------------*/
35
36 DSTATUS disk_status (
37 BYTE pdrv /* Physical drive nmuber to identify the drive */
38 )
39 {
40 return RES_OK;
41 #if 0
42 DSTATUS stat;
43 int result;
44
45 switch (pdrv) {
46 case DEV_RAM :
47 result = RAM_disk_status();
48
49 // translate the reslut code here
50
51 return stat;
52
53 case DEV_MMC :
54 result = MMC_disk_status();
55
56 // translate the reslut code here
57
58 return stat;
59
60 case DEV_USB :
61 result = USB_disk_status();
62
63 // translate the reslut code here
64
65 return stat;
66 }
67 return STA_NOINIT;
68 #endif
69 }
70
71
72
73 /*-----------------------------------------------------------------------*/
74 /* Inidialize a Drive */
75 /*-----------------------------------------------------------------------*/
76
77 DSTATUS disk_initialize (
78 BYTE pdrv /* Physical drive nmuber to identify the drive */
79 )
80 {
81 return RES_OK;
82 #if 0
83 DSTATUS stat;
84 int result;
85
86 switch (pdrv) {
87 case DEV_RAM :
88 result = RAM_disk_initialize();
89
90 // translate the reslut code here
91
92 return stat;
93
94 case DEV_MMC :
95 result = MMC_disk_initialize();
96
97 // translate the reslut code here
98
99 return stat;
100
101 case DEV_USB :
102 result = USB_disk_initialize();
103
104 // translate the reslut code here
105
106 return stat;
107 }
108 return STA_NOINIT;
109 #endif
110 }
111
112
113
114 /*-----------------------------------------------------------------------*/
115 /* Read Sector(s) */
116 /*-----------------------------------------------------------------------*/
117
118 DRESULT disk_read (
119 BYTE pdrv, /* Physical drive nmuber to identify the drive */
120 BYTE *buff, /* Data buffer to store read data */
121 LBA_t sector, /* Start sector in LBA */
122 UINT count /* Number of sectors to read */
123 )
124 {
125 DWORD dwSize;
126 BOOL bRet;
127 LARGE_INTEGER liCurrentPosition;
128
129 liCurrentPosition.QuadPart = sector * 512;
130 SetFilePointerEx(g_hPhyDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN);
131
132 bRet = ReadFile(g_hPhyDrive, buff, count * 512, &dwSize, NULL);
133
134 if (dwSize != count * 512)
135 {
136 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u", bRet, count * 512, dwSize, GetLastError());
137 }
138
139 if (sector == 0)
140 {
141 memcpy(buff, g_MbrSector, sizeof(g_MbrSector));
142 }
143
144 return RES_OK;
145 }
146
147
148
149 /*-----------------------------------------------------------------------*/
150 /* Write Sector(s) */
151 /*-----------------------------------------------------------------------*/
152
153 #if FF_FS_READONLY == 0
154
155 DRESULT disk_write (
156 BYTE pdrv, /* Physical drive nmuber to identify the drive */
157 const BYTE *buff, /* Data to be written */
158 LBA_t sector, /* Start sector in LBA */
159 UINT count /* Number of sectors to write */
160 )
161 {
162 DWORD dwSize;
163 BOOL bRet;
164 LARGE_INTEGER liCurrentPosition;
165
166 // skip MBR
167 if (sector == 0)
168 {
169 memcpy(g_MbrSector, buff, sizeof(g_MbrSector));
170
171 if (count == 1)
172 {
173 return RES_OK;
174 }
175
176 sector++;
177 count--;
178 }
179
180 liCurrentPosition.QuadPart = sector * 512;
181 SetFilePointerEx(g_hPhyDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN);
182
183 bRet = WriteFile(g_hPhyDrive, buff, count * 512, &dwSize, NULL);
184
185 if (dwSize != count * 512)
186 {
187 Log("WriteFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u", bRet, count * 512, dwSize, GetLastError());
188 }
189
190 return RES_OK;
191 }
192
193 #endif
194
195
196 /*-----------------------------------------------------------------------*/
197 /* Miscellaneous Functions */
198 /*-----------------------------------------------------------------------*/
199
200 DRESULT disk_ioctl (
201 BYTE pdrv, /* Physical drive nmuber (0..) */
202 BYTE cmd, /* Control code */
203 void *buff /* Buffer to send/receive control data */
204 )
205 {
206 switch (cmd)
207 {
208 case CTRL_SYNC:
209 {
210 //FILE_FLAG_NO_BUFFERING & FILE_FLAG_WRITE_THROUGH was set, no need to sync
211 break;
212 }
213 case GET_SECTOR_COUNT:
214 {
215 *(LBA_t *)buff = g_SectorCount;
216 break;
217 }
218 case GET_SECTOR_SIZE:
219 {
220 *(WORD *)buff = 512;
221 break;
222 }
223 case GET_BLOCK_SIZE:
224 {
225 *(DWORD *)buff = 8;
226 break;
227 }
228 }
229
230 return RES_OK;
231
232 #if 0
233 DRESULT res;
234 int result;
235
236 switch (pdrv) {
237 case DEV_RAM :
238
239 // Process of the command for the RAM drive
240
241 return res;
242
243 case DEV_MMC :
244
245 // Process of the command for the MMC/SD card
246
247 return res;
248
249 case DEV_USB :
250
251 // Process of the command the USB drive
252
253 return res;
254 }
255
256 return RES_PARERR;
257 #endif
258 }
259