-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskio.c
71 lines (61 loc) · 2.06 KB
/
diskio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include "fatfs/ff.h"
#include "fatfs/diskio.h"
FILE *image_file = NULL;
size_t image_size = 0;
#define BLOCK_SIZE 512
DSTATUS disk_status(BYTE pdrv) {
return 0;
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize(BYTE pdrv) {
return 0;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_read(BYTE pdrv, BYTE *buf, LBA_t sector, UINT count) {
if (fseek(image_file, sector * BLOCK_SIZE, SEEK_SET)) {
return RES_ERROR;
}
if (fread(buf, BLOCK_SIZE, count, image_file) != count) {
return RES_ERROR;
}
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_write(BYTE pdrv, const BYTE *buf, LBA_t sector, UINT count) {
if (fseek(image_file, sector * BLOCK_SIZE, SEEK_SET)) {
return RES_ERROR;
}
if (fwrite(buf, BLOCK_SIZE, count, image_file) != count) {
return RES_ERROR;
}
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buf) {
switch (cmd) {
case GET_SECTOR_SIZE:
*(DWORD *)buf = BLOCK_SIZE;
break;
case GET_BLOCK_SIZE:
*(DWORD *)buf = BLOCK_SIZE;
break;
case GET_SECTOR_COUNT:
*(DWORD *)buf = image_size / BLOCK_SIZE;
break;
case CTRL_TRIM:
case CTRL_SYNC:
break;
default:
return RES_PARERR;
}
return RES_OK;
}