-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskio.c
169 lines (145 loc) · 4.9 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
158
159
160
161
162
163
164
165
166
167
168
169
/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2019 */
/* Customized for fusefatfs 2020-2023 Renzo Davoli */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be */
/* attached to the FatFs via a glue function rather than modifying it. */
/* This is an example of glue functions to attach various exsisting */
/* storage control modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/
#include "ff.h" /* Obtains integer types */
#include "diskio.h" /* Declarations of disk functions */
#include "fftable.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
(void) pdrv;
return 0;
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
struct fftab *drv = fftab_get(pdrv);
if (!drv) return STA_NOINIT;
if (drv->flags & FFFF_RDONLY)
drv->fd = open(drv->path, O_RDONLY);
else
drv->fd = open(drv->path, O_SYNC|O_RDWR);
if (drv->fd < 0)
return STA_NOINIT;
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
)
{
DRESULT res;
struct fftab *drv = fftab_get(pdrv);
WORD ssize;
//printf("disk_read %d %p\n", pdrv, drv);
if (!drv) return RES_PARERR;
#if FF_MAX_SS != FF_MIN_SS
ssize = drv.fs->ssize;
#else
ssize = FF_MIN_SS;
#endif
ssize_t size = count * ssize;
if (pread(drv->fd, buff, size, sector * ssize) != size)
return RES_ERROR;
res = RES_OK;
return res;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
#if FF_FS_READONLY == 0
DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
)
{
DRESULT res;
struct fftab *drv = fftab_get(pdrv);
WORD ssize;
if (!drv) return RES_PARERR;
#if FF_MAX_SS != FF_MIN_SS
ssize = drv.fs->ssize;
#else
ssize = FF_MIN_SS;
#endif
if (drv->flags & FFFF_RDONLY)
return RES_WRPRT;
ssize_t size = count * ssize;
if (pwrite(drv->fd, buff, size, sector * ssize) != size)
return RES_ERROR;
res = RES_OK;
return res;
}
#endif
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
struct fftab *drv = fftab_get(pdrv);
if (!drv) return RES_PARERR;
switch (cmd) {
case CTRL_SYNC:
return RES_OK;
case GET_SECTOR_SIZE:
#if FF_MAX_SS != FF_MIN_SS
*((WORD*)buff) = drv.fs->ssize;
#else
*((WORD*)buff) = FF_MIN_SS;
#endif
return RES_OK;
}
return RES_PARERR;
}
DWORD get_fattime(void)
{
struct tm tm;
time_t now = time(NULL);
if (localtime_r(&now, &tm) != NULL) {
DWORD fattime =
/* bit31:25: Year origin from the 1980 (0..127, e.g. 37 for 2017) */
(((tm.tm_year - 80) & 0x7f) << 25) |
/* bit24:21: Month (1..12) */
(((tm.tm_mon + 1) & 0xf) << 21) |
/* bit20:16: Day of the month (1..31) */
((tm.tm_mday & 0x1f) << 16) |
/* bit15:11: Hour (0..23)) */
((tm.tm_hour & 0x1f) << 11) |
/* bit10:5: Minute (0..59) */
((tm.tm_min & 0x3f) << 5) |
/* bit4:0 Second / 2 (0..29, e.g. 25 for 50) */
((tm.tm_sec & 0x3f) / 2);
//printf("get_fattime %x\n", fattime);
return fattime;
} else
return 1;
}