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 /*-----------------------------------------------------------------------*/
12 #include "ff.h" /* Obtains integer types */
13 #include "diskio.h" /* Declarations of disk functions */
14 #include "../Ventoy2Disk.h"
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 */
22 void Log(const char *fmt
, ...);
24 static UINT8 g_MbrSector
[512];
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;
34 void disk_io_set_param(HANDLE Handle
, UINT64 SectorCount
)
37 g_SectorCount
= SectorCount
;
40 void disk_io_set_imghook(FILE *fp
, VTSI_SEGMENT
*segment
, int maxseg
, UINT64 data_offset
)
43 g_VentoySegment
= segment
;
44 g_VentoyMaxSeg
= maxseg
;
46 memset(segment
, 0, maxseg
* sizeof(VTSI_SEGMENT
));
48 g_VentoyDataOffset
= data_offset
;
51 void disk_io_reset_imghook(int *psegnum
, UINT64
*pDataOffset
)
53 *psegnum
= g_VentoyCurSeg
+ 1;
54 *pDataOffset
= g_VentoyDataOffset
;
57 g_VentoySegment
= NULL
;
60 g_VentoyDataOffset
= 0;
63 /*-----------------------------------------------------------------------*/
64 /* Get Drive Status */
65 /*-----------------------------------------------------------------------*/
68 BYTE pdrv
/* Physical drive nmuber to identify the drive */
78 result
= RAM_disk_status();
80 // translate the reslut code here
85 result
= MMC_disk_status();
87 // translate the reslut code here
92 result
= USB_disk_status();
94 // translate the reslut code here
104 /*-----------------------------------------------------------------------*/
105 /* Inidialize a Drive */
106 /*-----------------------------------------------------------------------*/
108 DSTATUS
disk_initialize (
109 BYTE pdrv
/* Physical drive nmuber to identify the drive */
119 result
= RAM_disk_initialize();
121 // translate the reslut code here
126 result
= MMC_disk_initialize();
128 // translate the reslut code here
133 result
= USB_disk_initialize();
135 // translate the reslut code here
145 /*-----------------------------------------------------------------------*/
147 /*-----------------------------------------------------------------------*/
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 */
158 LARGE_INTEGER liCurrentPosition
;
160 //Log("xxx disk_read: sector:%ld count:%ld", (long)sector, (long)count);
167 liCurrentPosition
.QuadPart
= sector
* 512;
168 SetFilePointerEx(g_hPhyDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
170 bRet
= ReadFile(g_hPhyDrive
, buff
, count
* 512, &dwSize
, NULL
);
172 if (dwSize
!= count
* 512)
174 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u", bRet
, count
* 512, dwSize
, GetLastError());
179 memcpy(buff
, g_MbrSector
, sizeof(g_MbrSector
));
187 /*-----------------------------------------------------------------------*/
188 /* Write Sector(s) */
189 /*-----------------------------------------------------------------------*/
191 #if FF_FS_READONLY == 0
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 */
202 LARGE_INTEGER liCurrentPosition
;
203 VTSI_SEGMENT
*CurSeg
= NULL
;
205 //Log("==== disk_write: sector:%ld count:%ld", (long)sector, (long)count);
210 memcpy(g_MbrSector
, buff
, sizeof(g_MbrSector
));
223 CurSeg
= g_VentoySegment
+ g_VentoyCurSeg
;
225 if (g_VentoyCurSeg
>= 0 && CurSeg
->sector_num
> 0 && sector
== CurSeg
->disk_start_sector
+ CurSeg
->sector_num
)
227 CurSeg
->sector_num
+= count
; //merge
234 CurSeg
->disk_start_sector
= sector
;
235 CurSeg
->data_offset
= g_VentoyDataOffset
;
236 CurSeg
->sector_num
= count
;
239 g_VentoyDataOffset
+= count
* 512;
241 fwrite(buff
, 1, count
* 512, g_VentoyImgFp
);
246 liCurrentPosition
.QuadPart
= sector
* 512;
247 SetFilePointerEx(g_hPhyDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
249 bRet
= WriteFile(g_hPhyDrive
, buff
, count
* 512, &dwSize
, NULL
);
251 if (dwSize
!= count
* 512)
253 Log("WriteFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u", bRet
, count
* 512, dwSize
, GetLastError());
262 /*-----------------------------------------------------------------------*/
263 /* Miscellaneous Functions */
264 /*-----------------------------------------------------------------------*/
267 BYTE pdrv
, /* Physical drive nmuber (0..) */
268 BYTE cmd
, /* Control code */
269 void *buff
/* Buffer to send/receive control data */
276 //FILE_FLAG_NO_BUFFERING & FILE_FLAG_WRITE_THROUGH was set, no need to sync
279 case GET_SECTOR_COUNT
:
281 *(LBA_t
*)buff
= g_SectorCount
;
284 case GET_SECTOR_SIZE
:
305 // Process of the command for the RAM drive
311 // Process of the command for the MMC/SD card
317 // Process of the command the USB drive