]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Ventoy2Disk/Ventoy2Disk/ff14/source/diskio.c
2a8c5b3844d70d33bd761e8471e9a7eaf59b6b15
[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 #include "../Ventoy2Disk.h"
15
16
17 /* Definitions of physical drive number for each drive */
18 #define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */
19 #define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
20 #define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */
21
22 void Log(const char *fmt, ...);
23
24 static UINT8 g_MbrSector[512];
25 HANDLE g_hPhyDrive;
26 UINT64 g_SectorCount;
27
28 FILE *g_VentoyImgFp = NULL;
29 VTSI_SEGMENT *g_VentoySegment = NULL;
30 int g_VentoyMaxSeg = 0;
31 int g_VentoyCurSeg = -1;
32 UINT64 g_VentoyDataOffset = 0;
33
34 void disk_io_set_param(HANDLE Handle, UINT64 SectorCount)
35 {
36 g_hPhyDrive = Handle;
37 g_SectorCount = SectorCount;
38 }
39
40 void disk_io_set_imghook(FILE *fp, VTSI_SEGMENT *segment, int maxseg, UINT64 data_offset)
41 {
42 g_VentoyImgFp = fp;
43 g_VentoySegment = segment;
44 g_VentoyMaxSeg = maxseg;
45
46 memset(segment, 0, maxseg * sizeof(VTSI_SEGMENT));
47 g_VentoyCurSeg = -1;
48 g_VentoyDataOffset = data_offset;
49 }
50
51 void disk_io_reset_imghook(int *psegnum, UINT64 *pDataOffset)
52 {
53 *psegnum = g_VentoyCurSeg + 1;
54 *pDataOffset = g_VentoyDataOffset;
55
56 g_VentoyImgFp = NULL;
57 g_VentoySegment = NULL;
58 g_VentoyMaxSeg = 0;
59 g_VentoyCurSeg = -1;
60 g_VentoyDataOffset = 0;
61 }
62
63 /*-----------------------------------------------------------------------*/
64 /* Get Drive Status */
65 /*-----------------------------------------------------------------------*/
66
67 DSTATUS disk_status (
68 BYTE pdrv /* Physical drive nmuber to identify the drive */
69 )
70 {
71 return RES_OK;
72 #if 0
73 DSTATUS stat;
74 int result;
75
76 switch (pdrv) {
77 case DEV_RAM :
78 result = RAM_disk_status();
79
80 // translate the reslut code here
81
82 return stat;
83
84 case DEV_MMC :
85 result = MMC_disk_status();
86
87 // translate the reslut code here
88
89 return stat;
90
91 case DEV_USB :
92 result = USB_disk_status();
93
94 // translate the reslut code here
95
96 return stat;
97 }
98 return STA_NOINIT;
99 #endif
100 }
101
102
103
104 /*-----------------------------------------------------------------------*/
105 /* Inidialize a Drive */
106 /*-----------------------------------------------------------------------*/
107
108 DSTATUS disk_initialize (
109 BYTE pdrv /* Physical drive nmuber to identify the drive */
110 )
111 {
112 return RES_OK;
113 #if 0
114 DSTATUS stat;
115 int result;
116
117 switch (pdrv) {
118 case DEV_RAM :
119 result = RAM_disk_initialize();
120
121 // translate the reslut code here
122
123 return stat;
124
125 case DEV_MMC :
126 result = MMC_disk_initialize();
127
128 // translate the reslut code here
129
130 return stat;
131
132 case DEV_USB :
133 result = USB_disk_initialize();
134
135 // translate the reslut code here
136
137 return stat;
138 }
139 return STA_NOINIT;
140 #endif
141 }
142
143
144
145 /*-----------------------------------------------------------------------*/
146 /* Read Sector(s) */
147 /*-----------------------------------------------------------------------*/
148
149 DRESULT disk_read (
150 BYTE pdrv, /* Physical drive nmuber to identify the drive */
151 BYTE *buff, /* Data buffer to store read data */
152 LBA_t sector, /* Start sector in LBA */
153 UINT count /* Number of sectors to read */
154 )
155 {
156 DWORD dwSize;
157 BOOL bRet;
158 LARGE_INTEGER liCurrentPosition;
159
160 //Log("xxx disk_read: sector:%ld count:%ld", (long)sector, (long)count);
161
162 if (g_VentoyImgFp)
163 {
164 return RES_OK;
165 }
166
167 liCurrentPosition.QuadPart = sector * 512;
168 SetFilePointerEx(g_hPhyDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN);
169
170 bRet = ReadFile(g_hPhyDrive, buff, count * 512, &dwSize, NULL);
171
172 if (dwSize != count * 512)
173 {
174 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u", bRet, count * 512, dwSize, GetLastError());
175 }
176
177 if (sector == 0)
178 {
179 memcpy(buff, g_MbrSector, sizeof(g_MbrSector));
180 }
181
182 return RES_OK;
183 }
184
185
186
187 /*-----------------------------------------------------------------------*/
188 /* Write Sector(s) */
189 /*-----------------------------------------------------------------------*/
190
191 #if FF_FS_READONLY == 0
192
193 DRESULT disk_write (
194 BYTE pdrv, /* Physical drive nmuber to identify the drive */
195 const BYTE *buff, /* Data to be written */
196 LBA_t sector, /* Start sector in LBA */
197 UINT count /* Number of sectors to write */
198 )
199 {
200 DWORD dwSize;
201 BOOL bRet;
202 LARGE_INTEGER liCurrentPosition;
203 VTSI_SEGMENT *CurSeg = NULL;
204
205 //Log("==== disk_write: sector:%ld count:%ld", (long)sector, (long)count);
206
207 // skip MBR
208 if (sector == 0)
209 {
210 memcpy(g_MbrSector, buff, sizeof(g_MbrSector));
211
212 if (count == 1)
213 {
214 return RES_OK;
215 }
216
217 sector++;
218 count--;
219 }
220
221 if (g_VentoyImgFp)
222 {
223 CurSeg = g_VentoySegment + g_VentoyCurSeg;
224
225 if (g_VentoyCurSeg >= 0 && CurSeg->sector_num > 0 && sector == CurSeg->disk_start_sector + CurSeg->sector_num)
226 {
227 CurSeg->sector_num += count; //merge
228 }
229 else
230 {
231 g_VentoyCurSeg++;
232 CurSeg++;
233
234 CurSeg->disk_start_sector = sector;
235 CurSeg->data_offset = g_VentoyDataOffset;
236 CurSeg->sector_num = count;
237 }
238
239 g_VentoyDataOffset += count * 512;
240
241 fwrite(buff, 1, count * 512, g_VentoyImgFp);
242 return RES_OK;
243 }
244
245
246 liCurrentPosition.QuadPart = sector * 512;
247 SetFilePointerEx(g_hPhyDrive, liCurrentPosition, &liCurrentPosition, FILE_BEGIN);
248
249 bRet = WriteFile(g_hPhyDrive, buff, count * 512, &dwSize, NULL);
250
251 if (dwSize != count * 512)
252 {
253 Log("WriteFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u", bRet, count * 512, dwSize, GetLastError());
254 }
255
256 return RES_OK;
257 }
258
259 #endif
260
261
262 /*-----------------------------------------------------------------------*/
263 /* Miscellaneous Functions */
264 /*-----------------------------------------------------------------------*/
265
266 DRESULT disk_ioctl (
267 BYTE pdrv, /* Physical drive nmuber (0..) */
268 BYTE cmd, /* Control code */
269 void *buff /* Buffer to send/receive control data */
270 )
271 {
272 switch (cmd)
273 {
274 case CTRL_SYNC:
275 {
276 //FILE_FLAG_NO_BUFFERING & FILE_FLAG_WRITE_THROUGH was set, no need to sync
277 break;
278 }
279 case GET_SECTOR_COUNT:
280 {
281 *(LBA_t *)buff = g_SectorCount;
282 break;
283 }
284 case GET_SECTOR_SIZE:
285 {
286 *(WORD *)buff = 512;
287 break;
288 }
289 case GET_BLOCK_SIZE:
290 {
291 *(DWORD *)buff = 8;
292 break;
293 }
294 }
295
296 return RES_OK;
297
298 #if 0
299 DRESULT res;
300 int result;
301
302 switch (pdrv) {
303 case DEV_RAM :
304
305 // Process of the command for the RAM drive
306
307 return res;
308
309 case DEV_MMC :
310
311 // Process of the command for the MMC/SD card
312
313 return res;
314
315 case DEV_USB :
316
317 // Process of the command the USB drive
318
319 return res;
320 }
321
322 return RES_PARERR;
323 #endif
324 }
325