-
Notifications
You must be signed in to change notification settings - Fork 1
/
diskio.c
159 lines (127 loc) · 2.5 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <diskio.h>
#include <ffconf.h>
#if 0
#define IO_TRACE
#endif
static FILE * fp = NULL;
void disk_fileimage_init(FILE * file)
{
fp = file;
}
DSTATUS disk_initialize(BYTE drive)
{
#ifdef IO_TRACE
printf("disk_initialize\n");
#endif
if (drive)
return STA_NOINIT; /* Supports only single drive */
if (fp == NULL)
{
return STA_NOINIT;
}
return 0;
}
DSTATUS disk_status(BYTE drive)
{
#ifdef IO_TRACE
printf("disk_status\n");
#endif
return 0;
}
DRESULT disk_read(BYTE drive, BYTE* buffer, DWORD sectorNumber, BYTE sectorCount)
{
long new_ofs = sectorNumber * _MAX_SS;
#ifdef IO_TRACE
printf("disk_read(%d, %d) = %ld: ", (int)sectorNumber, sectorCount, new_ofs);
#endif
// Goto the correct offset
if (fseek(fp, new_ofs, SEEK_SET) != 0)
{
#ifdef IO_TRACE
printf("ERROR! (1)\n");
#endif
return RES_ERROR;
}
// Read the requested number of sectors
if (fread(buffer, _MAX_SS, sectorCount, fp) != sectorCount)
{
#ifdef IO_TRACE
printf("ERROR! (2)\n");
#endif
return RES_ERROR;
}
#ifdef IO_TRACE
printf("OK\n");
#endif
return RES_OK;
}
DRESULT disk_write(BYTE drive, const BYTE* buffer, DWORD sectorNumber, BYTE sectorCount)
{
long new_ofs = sectorNumber * _MAX_SS;
#ifdef IO_TRACE
printf("disk_write: ");
#endif
// Goto the correct offset
if (fseek(fp, new_ofs, SEEK_SET) != 0)
{
#ifdef IO_TRACE
printf("ERROR! (1)\n");
#endif
return RES_ERROR;
}
// Write the requested number of sectors
if (fwrite(buffer, _MAX_SS, sectorCount, fp) != sectorCount)
{
#ifdef IO_TRACE
printf("ERROR! (2)\n");
#endif
return RES_ERROR;
}
#ifdef IO_TRACE
printf("OK\n");
#endif
return RES_OK;
}
DRESULT disk_ioctl(BYTE drive, BYTE command, void* buffer)
{
DRESULT rv = RES_ERROR;
switch(command)
{
case (CTRL_SYNC):
if (fsync(fileno(fp)) == 0)
{
rv = RES_OK;
}
break;
case (GET_BLOCK_SIZE):
{
WORD * pW = (WORD *)buffer;
*pW = 1;
rv = RES_OK;
}
break;
case (GET_SECTOR_COUNT):
{
struct stat stbuf;
if (fstat(fileno(fp), &stbuf) == 0)
{
DWORD nrSectors = stbuf.st_size / _MAX_SS;
DWORD * pW = (DWORD *)buffer;
*pW = nrSectors;
rv = RES_OK;
}
}
break;
default:
printf("disk_ioctl: unsupported command! (%d)\n", command);
rv = RES_PARERR;
break;
}
#ifdef IO_TRACE
printf("disk_ioctl(%d): %d\n", command, rv);
#endif
return rv;
}