1 /*-----------------------------------------------------------------------*/
2 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2013 */
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 module to the FatFs module with a defined API. */
8 /*-----------------------------------------------------------------------*/
12 /*-----------------------------------------------------------------------*/
13 /* Correspondence between physical drive number and image file handles. */
15 UINT sectorCount
[1] = { 0 };
16 FILE* driveHandle
[1] = { NULL
};
17 const int driveHandleCount
= sizeof(driveHandle
) / sizeof(FILE*);
19 /*-----------------------------------------------------------------------*/
20 /* Open an image file a Drive */
21 /*-----------------------------------------------------------------------*/
23 DSTATUS
disk_openimage(BYTE pdrv
, const char* imageFileName
)
25 if (pdrv
< driveHandleCount
)
27 if (driveHandle
[0] != NULL
)
30 driveHandle
[0] = fopen(imageFileName
, "r+b");
33 driveHandle
[0] = fopen(imageFileName
, "w+");
36 if (driveHandle
[0] != NULL
)
43 /*-----------------------------------------------------------------------*/
45 /*-----------------------------------------------------------------------*/
48 BYTE pdrv
/* Physical drive nmuber (0..) */
51 if (pdrv
< driveHandleCount
)
53 if (driveHandle
[pdrv
] != NULL
)
55 fclose(driveHandle
[pdrv
]);
56 driveHandle
[pdrv
] = NULL
;
61 /*-----------------------------------------------------------------------*/
62 /* Inidialize a Drive */
63 /*-----------------------------------------------------------------------*/
65 DSTATUS
disk_initialize(
66 BYTE pdrv
/* Physical drive nmuber (0..) */
69 if (pdrv
== 0) /* only one drive (image file) supported atm. */
78 /*-----------------------------------------------------------------------*/
80 /*-----------------------------------------------------------------------*/
83 BYTE pdrv
/* Physical drive nmuber (0..) */
86 if (pdrv
< driveHandleCount
)
88 if (driveHandle
[pdrv
] != NULL
)
94 /*-----------------------------------------------------------------------*/
96 /*-----------------------------------------------------------------------*/
99 BYTE pdrv
, /* Physical drive nmuber (0..) */
100 BYTE
*buff
, /* Data buffer to store read data */
101 DWORD sector
, /* Sector address (LBA) */
102 UINT count
/* Number of sectors to read (1..128) */
107 if (pdrv
< driveHandleCount
)
109 if (driveHandle
[pdrv
] != NULL
)
111 if (fseek(driveHandle
[pdrv
], sector
* 512, SEEK_SET
))
114 result
= fread(buff
, 512, count
, driveHandle
[pdrv
]);
128 /*-----------------------------------------------------------------------*/
129 /* Write Sector(s) */
130 /*-----------------------------------------------------------------------*/
134 BYTE pdrv
, /* Physical drive nmuber (0..) */
135 const BYTE
*buff
, /* Data to be written */
136 DWORD sector
, /* Sector address (LBA) */
137 UINT count
/* Number of sectors to write (1..128) */
142 if (pdrv
< driveHandleCount
)
144 if (driveHandle
[pdrv
] != NULL
)
146 if (fseek(driveHandle
[pdrv
], sector
* 512, SEEK_SET
))
149 result
= fwrite(buff
, 512, count
, driveHandle
[pdrv
]);
163 /*-----------------------------------------------------------------------*/
164 /* Miscellaneous Functions */
165 /*-----------------------------------------------------------------------*/
169 BYTE pdrv
, /* Physical drive nmuber (0..) */
170 BYTE cmd
, /* Control code */
171 void *buff
/* Buffer to send/receive control data */
174 if (pdrv
< driveHandleCount
)
176 if (driveHandle
[pdrv
] != NULL
)
181 fflush(driveHandle
[pdrv
]);
183 case GET_SECTOR_SIZE
:
189 case GET_SECTOR_COUNT
:
191 if (sectorCount
[pdrv
] <= 0)
193 if (fseek(driveHandle
[pdrv
], 0, SEEK_END
))
194 printf("fseek failed!\n");
196 sectorCount
[pdrv
] = ftell(driveHandle
[pdrv
]) / 512;
199 *(DWORD
*)buff
= sectorCount
[pdrv
];
202 case SET_SECTOR_COUNT
:
204 int count
= *(DWORD
*)buff
;
207 sectorCount
[pdrv
] = count
;
209 fseek(driveHandle
[pdrv
], 0, SEEK_END
);
210 size
= ftell(driveHandle
[pdrv
]) / 512;
214 if (fseek(driveHandle
[pdrv
], count
* 512 - 1, SEEK_SET
))
217 fwrite(buff
, 1, 1, driveHandle
[pdrv
]);
223 // SHRINKING NOT IMPLEMENTED