]> cloudbase.mooo.com Git - z180-stamp.git/blob - fatfs/doc/res/app4.c
Import fatfs R0.12b
[z180-stamp.git] / fatfs / doc / res / app4.c
1 /*----------------------------------------------------------------------/
2 / Low level disk I/O module function checker
3 /-----------------------------------------------------------------------/
4 / WARNING: The data on the target drive will be lost!
5 */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include "ff.h"
10 #include "diskio.h"
11
12
13 static
14 DWORD pn (
15 DWORD pns
16 )
17 {
18 static DWORD lfsr;
19 UINT n;
20
21
22 if (pns) {
23 lfsr = pns;
24 for (n = 0; n < 32; n++) pn(0);
25 }
26 if (lfsr & 1) {
27 lfsr >>= 1;
28 lfsr ^= 0x80200003;
29 } else {
30 lfsr >>= 1;
31 }
32 return lfsr;
33 }
34
35
36 int test_diskio (
37 BYTE pdrv, /* Physical drive number to be checked (all data on the drive will be lost) */
38 UINT ncyc, /* Number of test cycles */
39 DWORD* buff, /* Pointer to the working buffer */
40 UINT sz_buff /* Size of the working buffer in unit of byte */
41 )
42 {
43 UINT n, cc, ns;
44 DWORD sz_drv, lba, lba2, pns = 1;
45 WORD sz_sect, sz_eblk;
46 BYTE *pbuff = (BYTE*)buff;
47 DSTATUS ds;
48 DRESULT dr;
49
50
51 printf("test_diskio(%u, %u, 0x%08X, 0x%08X)\n", pdrv, ncyc, (UINT)buff, sz_buff);
52
53 if (sz_buff < _MAX_SS + 4) {
54 printf("Insufficient work area to test.\n");
55 return 1;
56 }
57
58 for (cc = 1; cc <= ncyc; cc++) {
59 printf("**** Test cycle %u of %u start ****\n", cc, ncyc);
60
61 /* Initialization */
62 printf(" disk_initalize(%u)", pdrv);
63 ds = disk_initialize(pdrv);
64 if (ds & STA_NOINIT) {
65 printf(" - failed.\n");
66 return 2;
67 } else {
68 printf(" - ok.\n");
69 }
70
71 /* Get drive size */
72 printf("**** Get drive size ****\n");
73 printf(" disk_ioctl(%u, GET_SECTOR_COUNT, 0x%08X)", pdrv, (UINT)&sz_drv);
74 sz_drv = 0;
75 dr = disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_drv);
76 if (dr == RES_OK) {
77 printf(" - ok.\n");
78 } else {
79 printf(" - failed.\n");
80 return 3;
81 }
82 if (sz_drv < 128) {
83 printf("Failed: Insufficient drive size to test.\n");
84 return 4;
85 }
86 printf(" Number of sectors on the drive %u is %lu.\n", pdrv, sz_drv);
87
88 #if _MAX_SS != _MIN_SS
89 /* Get sector size */
90 printf("**** Get sector size ****\n");
91 printf(" disk_ioctl(%u, GET_SECTOR_SIZE, 0x%X)", pdrv, (UINT)&sz_sect);
92 sz_sect = 0;
93 dr = disk_ioctl(pdrv, GET_SECTOR_SIZE, &sz_sect);
94 if (dr == RES_OK) {
95 printf(" - ok.\n");
96 } else {
97 printf(" - failed.\n");
98 return 5;
99 }
100 printf(" Size of sector is %u bytes.\n", sz_sect);
101 #else
102 sz_sect = _MAX_SS;
103 #endif
104
105 /* Get erase block size */
106 printf("**** Get block size ****\n");
107 printf(" disk_ioctl(%u, GET_BLOCK_SIZE, 0x%X)", pdrv, (UINT)&sz_eblk);
108 sz_eblk = 0;
109 dr = disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_eblk);
110 if (dr == RES_OK) {
111 printf(" - ok.\n");
112 } else {
113 printf(" - failed.\n");
114 }
115 if (dr == RES_OK || sz_eblk >= 2) {
116 printf(" Size of the erase block is %u sectors.\n", sz_eblk);
117 } else {
118 printf(" Size of the erase block is unknown.\n");
119 }
120
121 /* Single sector write test */
122 printf("**** Single sector write test 1 ****\n");
123 lba = 0;
124 for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n] = (BYTE)pn(0);
125 printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba);
126 dr = disk_write(pdrv, pbuff, lba, 1);
127 if (dr == RES_OK) {
128 printf(" - ok.\n");
129 } else {
130 printf(" - failed.\n");
131 return 6;
132 }
133 printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
134 dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
135 if (dr == RES_OK) {
136 printf(" - ok.\n");
137 } else {
138 printf(" - failed.\n");
139 return 7;
140 }
141 memset(pbuff, 0, sz_sect);
142 printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba);
143 dr = disk_read(pdrv, pbuff, lba, 1);
144 if (dr == RES_OK) {
145 printf(" - ok.\n");
146 } else {
147 printf(" - failed.\n");
148 return 8;
149 }
150 for (n = 0, pn(pns); n < sz_sect && pbuff[n] == (BYTE)pn(0); n++) ;
151 if (n == sz_sect) {
152 printf(" Data matched.\n");
153 } else {
154 printf("Failed: Read data differs from the data written.\n");
155 return 10;
156 }
157 pns++;
158
159 /* Multiple sector write test */
160 printf("**** Multiple sector write test ****\n");
161 lba = 1; ns = sz_buff / sz_sect;
162 if (ns > 4) ns = 4;
163 for (n = 0, pn(pns); n < (UINT)(sz_sect * ns); n++) pbuff[n] = (BYTE)pn(0);
164 printf(" disk_write(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns);
165 dr = disk_write(pdrv, pbuff, lba, ns);
166 if (dr == RES_OK) {
167 printf(" - ok.\n");
168 } else {
169 printf(" - failed.\n");
170 return 11;
171 }
172 printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
173 dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
174 if (dr == RES_OK) {
175 printf(" - ok.\n");
176 } else {
177 printf(" - failed.\n");
178 return 12;
179 }
180 memset(pbuff, 0, sz_sect * ns);
181 printf(" disk_read(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns);
182 dr = disk_read(pdrv, pbuff, lba, ns);
183 if (dr == RES_OK) {
184 printf(" - ok.\n");
185 } else {
186 printf(" - failed.\n");
187 return 13;
188 }
189 for (n = 0, pn(pns); n < (UINT)(sz_sect * ns) && pbuff[n] == (BYTE)pn(0); n++) ;
190 if (n == (UINT)(sz_sect * ns)) {
191 printf(" Data matched.\n");
192 } else {
193 printf("Failed: Read data differs from the data written.\n");
194 return 14;
195 }
196 pns++;
197
198 /* Single sector write test (misaligned memory address) */
199 printf("**** Single sector write test 2 ****\n");
200 lba = 5;
201 for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n+3] = (BYTE)pn(0);
202 printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+3), lba);
203 dr = disk_write(pdrv, pbuff+3, lba, 1);
204 if (dr == RES_OK) {
205 printf(" - ok.\n");
206 } else {
207 printf(" - failed.\n");
208 return 15;
209 }
210 printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
211 dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
212 if (dr == RES_OK) {
213 printf(" - ok.\n");
214 } else {
215 printf(" - failed.\n");
216 return 16;
217 }
218 memset(pbuff+5, 0, sz_sect);
219 printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+5), lba);
220 dr = disk_read(pdrv, pbuff+5, lba, 1);
221 if (dr == RES_OK) {
222 printf(" - ok.\n");
223 } else {
224 printf(" - failed.\n");
225 return 17;
226 }
227 for (n = 0, pn(pns); n < sz_sect && pbuff[n+5] == (BYTE)pn(0); n++) ;
228 if (n == sz_sect) {
229 printf(" Data matched.\n");
230 } else {
231 printf("Failed: Read data differs from the data written.\n");
232 return 18;
233 }
234 pns++;
235
236 /* 4GB barrier test */
237 printf("**** 4GB barrier test ****\n");
238 if (sz_drv >= 128 + 0x80000000 / (sz_sect / 2)) {
239 lba = 6; lba2 = lba + 0x80000000 / (sz_sect / 2);
240 for (n = 0, pn(pns); n < (UINT)(sz_sect * 2); n++) pbuff[n] = (BYTE)pn(0);
241 printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba);
242 dr = disk_write(pdrv, pbuff, lba, 1);
243 if (dr == RES_OK) {
244 printf(" - ok.\n");
245 } else {
246 printf(" - failed.\n");
247 return 19;
248 }
249 printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+sz_sect), lba2);
250 dr = disk_write(pdrv, pbuff+sz_sect, lba2, 1);
251 if (dr == RES_OK) {
252 printf(" - ok.\n");
253 } else {
254 printf(" - failed.\n");
255 return 20;
256 }
257 printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
258 dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
259 if (dr == RES_OK) {
260 printf(" - ok.\n");
261 } else {
262 printf(" - failed.\n");
263 return 21;
264 }
265 memset(pbuff, 0, sz_sect * 2);
266 printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba);
267 dr = disk_read(pdrv, pbuff, lba, 1);
268 if (dr == RES_OK) {
269 printf(" - ok.\n");
270 } else {
271 printf(" - failed.\n");
272 return 22;
273 }
274 printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+sz_sect), lba2);
275 dr = disk_read(pdrv, pbuff+sz_sect, lba2, 1);
276 if (dr == RES_OK) {
277 printf(" - ok.\n");
278 } else {
279 printf(" - failed.\n");
280 return 23;
281 }
282 for (n = 0, pn(pns); pbuff[n] == (BYTE)pn(0) && n < (UINT)(sz_sect * 2); n++) ;
283 if (n == (UINT)(sz_sect * 2)) {
284 printf(" Data matched.\n");
285 } else {
286 printf("Failed: Read data differs from the data written.\n");
287 return 24;
288 }
289 } else {
290 printf(" Test skipped.\n");
291 }
292 pns++;
293
294 printf("**** Test cycle %u of %u completed ****\n\n", cc, ncyc);
295 }
296
297 return 0;
298 }
299
300
301
302 int main (int argc, char* argv[])
303 {
304 int rc;
305 DWORD buff[512]; /* 2048 byte working buffer */
306
307 /* Check function/compatibility of the physical drive #0 */
308 rc = test_diskio(0, 3, buff, sizeof buff);
309 if (rc) {
310 printf("Sorry the function/compatibility test failed. (rc=%d)\nFatFs will not work on this disk driver.\n", rc);
311 } else {
312 printf("Congratulations! The disk driver works well.\n");
313 }
314
315 return rc;
316 }
317