00001
00002
00003
00004
00005
00006
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include "ff.h"
00010 #include "diskio.h"
00011
00012
00013 static
00014 DWORD pn (
00015 DWORD pns
00016 )
00017 {
00018 static DWORD lfsr;
00019 UINT n;
00020
00021
00022 if (pns) {
00023 lfsr = pns;
00024 for (n = 0; n < 32; n++) pn(0);
00025 }
00026 if (lfsr & 1) {
00027 lfsr >>= 1;
00028 lfsr ^= 0x80200003;
00029 } else {
00030 lfsr >>= 1;
00031 }
00032 return lfsr;
00033 }
00034
00035
00036 int test_diskio (
00037 BYTE pdrv,
00038 UINT ncyc,
00039 DWORD* buff,
00040 UINT sz_buff
00041 )
00042 {
00043 UINT n, cc, ns;
00044 DWORD sz_drv, lba, lba2, pns = 1;
00045 WORD sz_sect, sz_eblk;
00046 BYTE *pbuff = (BYTE*)buff;
00047 DSTATUS ds;
00048 DRESULT dr;
00049
00050
00051
00052 printf("test_diskio(%u, %u, 0x%08X, 0x%08X)\n", pdrv, ncyc, (UINT)buff, sz_buff);
00053
00054 if (sz_buff < _MAX_SS + 4) {
00055 printf("Insufficient work area to test.\n");
00056 return 1;
00057 }
00058
00059 for (cc = 1; cc <= ncyc; cc++) {
00060 printf("**** Test cycle %u of %u start ****\n", cc, ncyc);
00061
00062
00063 printf(" disk_initalize(%u)", pdrv);
00064 ds = disk_initialize(pdrv);
00065 if (ds & STA_NOINIT) {
00066 printf(" - failed.\n");
00067 return 2;
00068 } else {
00069 printf(" - ok.\n");
00070 }
00071
00072
00073 printf("**** Get drive size ****\n");
00074 printf(" disk_ioctl(%u, GET_SECTOR_COUNT, 0x%08X)", pdrv, (UINT)&sz_drv);
00075 sz_drv = 0;
00076 dr = disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_drv);
00077 if (dr == RES_OK) {
00078 printf(" - ok.\n");
00079 } else {
00080 printf(" - failed.\n");
00081 return 3;
00082 }
00083 if (sz_drv < 128) {
00084 printf("Failed: Insufficient drive size to test.\n");
00085 return 4;
00086 }
00087 printf(" Number of sectors on the drive %u is %lu.\n", pdrv, sz_drv);
00088
00089 #if _MAX_SS != _MIN_SS
00090
00091 printf("**** Get sector size ****\n");
00092 printf(" disk_ioctl(%u, GET_SECTOR_SIZE, 0x%X)", pdrv, (UINT)&sz_sect);
00093 sz_sect = 0;
00094 dr = disk_ioctl(pdrv, GET_SECTOR_SIZE, &sz_sect);
00095 if (dr == RES_OK) {
00096 printf(" - ok.\n");
00097 } else {
00098 printf(" - failed.\n");
00099 return 5;
00100 }
00101 printf(" Size of sector is %u bytes.\n", sz_sect);
00102 #else
00103 sz_sect = _MAX_SS;
00104 #endif
00105
00106
00107 printf("**** Get block size ****\n");
00108 printf(" disk_ioctl(%u, GET_BLOCK_SIZE, 0x%X)", pdrv, (UINT)&sz_eblk);
00109 sz_eblk = 0;
00110 dr = disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_eblk);
00111 if (dr == RES_OK) {
00112 printf(" - ok.\n");
00113 } else {
00114 printf(" - failed.\n");
00115 }
00116 if (dr == RES_OK || sz_eblk >= 2) {
00117 printf(" Size of the erase block is %u sectors.\n", sz_eblk);
00118 } else {
00119 printf(" Size of the erase block is unknown.\n");
00120 }
00121
00122
00123 printf("**** Single sector write test 1 ****\n");
00124 lba = 0;
00125 for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n] = (BYTE)pn(0);
00126 printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba);
00127 dr = disk_write(pdrv, pbuff, lba, 1);
00128 if (dr == RES_OK) {
00129 printf(" - ok.\n");
00130 } else {
00131 printf(" - failed.\n");
00132 return 6;
00133 }
00134 printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
00135 dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
00136 if (dr == RES_OK) {
00137 printf(" - ok.\n");
00138 } else {
00139 printf(" - failed.\n");
00140 return 7;
00141 }
00142 memset(pbuff, 0, sz_sect);
00143 printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba);
00144 dr = disk_read(pdrv, pbuff, lba, 1);
00145 if (dr == RES_OK) {
00146 printf(" - ok.\n");
00147 } else {
00148 printf(" - failed.\n");
00149 return 8;
00150 }
00151 for (n = 0, pn(pns); n < sz_sect && pbuff[n] == (BYTE)pn(0); n++) ;
00152 if (n == sz_sect) {
00153 printf(" Data matched.\n");
00154 } else {
00155 printf("Failed: Read data differs from the data written.\n");
00156 return 10;
00157 }
00158 pns++;
00159
00160
00161 printf("**** Multiple sector write test ****\n");
00162 lba = 1; ns = sz_buff / sz_sect;
00163 if (ns > 4) ns = 4;
00164 for (n = 0, pn(pns); n < (UINT)(sz_sect * ns); n++) pbuff[n] = (BYTE)pn(0);
00165 printf(" disk_write(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns);
00166 dr = disk_write(pdrv, pbuff, lba, ns);
00167 if (dr == RES_OK) {
00168 printf(" - ok.\n");
00169 } else {
00170 printf(" - failed.\n");
00171 return 11;
00172 }
00173 printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
00174 dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
00175 if (dr == RES_OK) {
00176 printf(" - ok.\n");
00177 } else {
00178 printf(" - failed.\n");
00179 return 12;
00180 }
00181 memset(pbuff, 0, sz_sect * ns);
00182 printf(" disk_read(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns);
00183 dr = disk_read(pdrv, pbuff, lba, ns);
00184 if (dr == RES_OK) {
00185 printf(" - ok.\n");
00186 } else {
00187 printf(" - failed.\n");
00188 return 13;
00189 }
00190 for (n = 0, pn(pns); n < (UINT)(sz_sect * ns) && pbuff[n] == (BYTE)pn(0); n++) ;
00191 if (n == (UINT)(sz_sect * ns)) {
00192 printf(" Data matched.\n");
00193 } else {
00194 printf("Failed: Read data differs from the data written.\n");
00195 return 14;
00196 }
00197 pns++;
00198
00199
00200 printf("**** Single sector write test 2 ****\n");
00201 lba = 5;
00202 for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n+3] = (BYTE)pn(0);
00203 printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+3), lba);
00204 dr = disk_write(pdrv, pbuff+3, lba, 1);
00205 if (dr == RES_OK) {
00206 printf(" - ok.\n");
00207 } else {
00208 printf(" - failed.\n");
00209 return 15;
00210 }
00211 printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
00212 dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
00213 if (dr == RES_OK) {
00214 printf(" - ok.\n");
00215 } else {
00216 printf(" - failed.\n");
00217 return 16;
00218 }
00219 memset(pbuff+5, 0, sz_sect);
00220 printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+5), lba);
00221 dr = disk_read(pdrv, pbuff+5, lba, 1);
00222 if (dr == RES_OK) {
00223 printf(" - ok.\n");
00224 } else {
00225 printf(" - failed.\n");
00226 return 17;
00227 }
00228 for (n = 0, pn(pns); n < sz_sect && pbuff[n+5] == (BYTE)pn(0); n++) ;
00229 if (n == sz_sect) {
00230 printf(" Data matched.\n");
00231 } else {
00232 printf("Failed: Read data differs from the data written.\n");
00233 return 18;
00234 }
00235 pns++;
00236
00237
00238 printf("**** 4GB barrier test ****\n");
00239 if (sz_drv >= 128 + 0x80000000 / (sz_sect / 2)) {
00240 lba = 6; lba2 = lba + 0x80000000 / (sz_sect / 2);
00241 for (n = 0, pn(pns); n < (UINT)(sz_sect * 2); n++) pbuff[n] = (BYTE)pn(0);
00242 printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba);
00243 dr = disk_write(pdrv, pbuff, lba, 1);
00244 if (dr == RES_OK) {
00245 printf(" - ok.\n");
00246 } else {
00247 printf(" - failed.\n");
00248 return 19;
00249 }
00250 printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+sz_sect), lba2);
00251 dr = disk_write(pdrv, pbuff+sz_sect, lba2, 1);
00252 if (dr == RES_OK) {
00253 printf(" - ok.\n");
00254 } else {
00255 printf(" - failed.\n");
00256 return 20;
00257 }
00258 printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv);
00259 dr = disk_ioctl(pdrv, CTRL_SYNC, 0);
00260 if (dr == RES_OK) {
00261 printf(" - ok.\n");
00262 } else {
00263 printf(" - failed.\n");
00264 return 21;
00265 }
00266 memset(pbuff, 0, sz_sect * 2);
00267 printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba);
00268 dr = disk_read(pdrv, pbuff, lba, 1);
00269 if (dr == RES_OK) {
00270 printf(" - ok.\n");
00271 } else {
00272 printf(" - failed.\n");
00273 return 22;
00274 }
00275 printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+sz_sect), lba2);
00276 dr = disk_read(pdrv, pbuff+sz_sect, lba2, 1);
00277 if (dr == RES_OK) {
00278 printf(" - ok.\n");
00279 } else {
00280 printf(" - failed.\n");
00281 return 23;
00282 }
00283 for (n = 0, pn(pns); pbuff[n] == (BYTE)pn(0) && n < (UINT)(sz_sect * 2); n++) ;
00284 if (n == (UINT)(sz_sect * 2)) {
00285 printf(" Data matched.\n");
00286 } else {
00287 printf("Failed: Read data differs from the data written.\n");
00288 return 24;
00289 }
00290 } else {
00291 printf(" Test skipped.\n");
00292 }
00293 pns++;
00294
00295 printf("**** Test cycle %u of %u completed ****\n\n", cc, ncyc);
00296 }
00297
00298 return 0;
00299 }
00300
00301
00302
00303 int main (int argc, char* argv[])
00304 {
00305 int rc;
00306 DWORD buff[512];
00307
00308
00309 rc = test_diskio(0, 1, buff, sizeof buff);
00310 if (res) {
00311 printf("Sorry the function/compatibility test failed.\nFatFs will not work on this disk driver.\n");
00312 } else {
00313 printf("Congratulations! The disk I/O layer works well.\n");
00314 }
00315
00316 return rc;
00317 }
00318