forked from comex/formatter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
diskio.c
161 lines (134 loc) · 2.96 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
/*
diskio.c -- glue interface to ElmChan FAT FS driver. Part of the
BootMii project.
Copyright (C) 2008, 2009 Haxx Enterprises <[email protected]>
Copyright (C) 2008, 2009 Sven Peter <[email protected]>
# This code is licensed to you under the terms of the GNU GPL, version 2;
# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
#include "bootmii_ppc.h"
#include "ipc.h"
#include "mini_ipc.h"
#include "diskio.h"
#include "string.h"
static u8 *buffer[512] __attribute__((aligned(32)));
#define MAX_SECTORS_PER_READ 512
#define MAX_SECTORS_PER_WRITE 512
DSTATUS disk_initialize (BYTE drv)
{
(void) drv;
int state = sd_get_state();
switch (state) {
case SDMMC_NO_CARD:
return STA_NODISK;
case SDMMC_NEW_CARD:
if (sd_mount())
return STA_NOINIT;
else
return 0;
default:
return 0;
}
}
DSTATUS disk_status (BYTE drv)
{
(void) drv;
int state = sd_get_state();
switch (state) {
case SDMMC_NO_CARD:
return STA_NODISK;
case SDMMC_NEW_CARD:
return STA_NOINIT;
default:
return 0;
}
}
DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, u32 count)
{
u32 i;
DRESULT res;
(void) drv;
// printf("%s(%d,%p,%u,%u)\n", __FUNCTION__, drv, buff, sector, count);
if (count > 1 && ((u32) buff % 64) == 0) {
while (count != 0) {
u32 sectors = (count > MAX_SECTORS_PER_READ)? MAX_SECTORS_PER_READ : count;
res = sd_read(sector, sectors, buff);
if (res != 0) {
printf("error %d reading sector %d\n", res, sector);
return RES_ERROR;
}
buff += sectors * 512;
count -= sectors;
sector += sectors;
}
return RES_OK;
}
res = RES_OK;
for (i = 0; i < count; i++) {
if (sd_read(sector + i, 1, buffer) != 0) {
res = RES_ERROR;
break;
}
memcpy(buff + i * 512, buffer, 512);
}
return res;
}
#if _READONLY == 0
DRESULT disk_write (BYTE drv, const BYTE *buff, DWORD sector, u32 count)
{
u32 i;
DRESULT res;
(void) drv;
// printf("%s(%d,%p,%u,%u)\n", __FUNCTION__, drv, buff, sector, count);
res = RES_OK;
if (count > 1 && ((u32) buff % 64) == 0) {
while (count != 0) {
u32 sectors = (count > MAX_SECTORS_PER_WRITE)? MAX_SECTORS_PER_WRITE : count;
res = sd_write(sector, sectors, buff);
if (res != 0) {
printf("error %d writing sector %d\n", res, sector);
return RES_ERROR;
}
buff += sectors * 512;
count -= sectors;
sector += sectors;
}
return RES_OK;
}
for (i = 0; i < count; i++) {
memcpy(buffer, buff + i * 512, 512);
if (sd_write(sector + i, 1, buffer) != 0) {
res = RES_ERROR;
break;
}
}
return res;
}
#endif /* _READONLY */
DRESULT disk_ioctl (BYTE drv, BYTE ctrl, void *buff)
{
(void) drv;
u32 *buff_u32 = (u32 *) buff;
DRESULT res = RES_OK;
switch (ctrl) {
case CTRL_SYNC:
break;
case GET_SECTOR_COUNT:
*buff_u32 = sd_getsize();
break;
case GET_SECTOR_SIZE:
*buff_u32 = 512;
break;
case GET_BLOCK_SIZE:
*buff_u32 = 512;
break;
default:
res = RES_PARERR;
break;
}
return res;
}
DWORD get_fattime(void)
{
return 0; // TODO
}