1 #include "nor_stress.h"
12 #define FSX_INIT(mount_pt) do{ if(interleave_fsx) yaffs_fsx_init(mount_pt); } while(0)
13 #define FSX_COMPLETE() do { if(interleave_fsx) yaffs_fsx_complete(); } while (0)
17 if((myrand() & 0x1) == 0){\
24 #define FSX_INIT(mount_point) do { } while(0)
25 #define FSX_COMPLETE() do { } while(0)
26 #define FSX() do { } while(0)
30 static unsigned powerUps;
31 static unsigned cycleStarts;
32 static unsigned cycleEnds;
34 static int interleave_fsx;
36 char fullPathName[100];
37 char fullPowerUpName[100];
38 char fullStartName[100];
39 char fullEndName[100];
40 char fullMainName[100];
41 char fullTempMainName[100];
42 char fullTempCounterName[100];
45 extern int random_seed;
48 random_seed = random_seed * 1103515245 + 12345;
49 return((unsigned)(random_seed/65536) % 32768);
52 void MakeName(char *fullName,const char *prefix, const char *name)
54 strcpy(fullName,prefix);
56 strcat(fullName,name);
60 void MakeFullNames(const char *prefix)
62 MakeName(fullPathName,prefix,"");
63 MakeName(fullPowerUpName,prefix,"powerUps");
64 MakeName(fullStartName,prefix,"starts");
65 MakeName(fullEndName,prefix,"ends");
66 MakeName(fullMainName,prefix,"main");
67 MakeName(fullTempCounterName,prefix,"tmp-counter");
68 MakeName(fullTempMainName,prefix,"tmp-main");
71 static void FatalError(void)
73 printf("Integrity error\n");
79 static void UpdateCounter(const char *name, unsigned *val, int initialise)
93 inh = yaffs_open(name,O_RDONLY, S_IREAD | S_IWRITE);
95 nread = yaffs_read(inh,x,sizeof(x));
99 if(nread != sizeof(x) ||
101 printf("Error reading counter %s handle %d, x[0] %u x[1] %u last error %d\n",
102 name, inh, x[0], x[1],yaffsfs_GetLastError());
111 outh = yaffs_open(fullTempCounterName, O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
114 nwritten = yaffs_write(outh,x,sizeof(x));
118 yaffs_rename(fullTempCounterName,name);
122 if(nwritten != sizeof(x)){
123 printf("Error writing counter %s handle %d, x[0] %u x[1] %u\n",
124 name, inh, x[0], x[1]);
131 "## Set counter %s to %u\n"
136 static void dump_directory_tree_worker(const char *dname,int recursive)
143 d = yaffs_opendir(dname);
147 printf("opendir failed\n");
151 while((de = yaffs_readdir(d)) != NULL)
155 strcat(str,de->d_name);
159 printf("%s inode %d obj %x length %d mode %X ",str,s.st_ino,de->d_dont_use,(int)s.st_size,s.st_mode);
160 switch(s.st_mode & S_IFMT)
162 case S_IFREG: printf("data file"); break;
163 case S_IFDIR: printf("directory"); break;
164 case S_IFLNK: printf("symlink -->");
165 if(yaffs_readlink(str,str,100) < 0)
168 printf("\"%s\"",str);
170 default: printf("unknown"); break;
175 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
176 dump_directory_tree_worker(str,1);
188 static void dump_directory_tree(const char *dname)
190 dump_directory_tree_worker(dname,1);
192 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
200 static unsigned xx[XX_SIZE];
202 static int yWriteFile(const char *fname, unsigned sz32)
207 unsigned checksum = 0;
209 printf("Writing file %s\n",fname);
212 h = yaffs_open(fname,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
216 printf("could not open file %s\n",fname);
223 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
228 for(i = 0; i < XX_SIZE; i++){
234 if((r = yaffs_write(h,xx,sizeof(xx))) != sizeof(xx)){
242 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
251 printf("ywrite error at position %d\n",(int)yaffs_lseek(h,0,SEEK_END));
257 static int yVerifyFile(const char *fName)
259 unsigned checksum = 0;
262 unsigned recordedSize = 0;
269 printf("Verifying file %s\n",fName);
271 h = yaffs_open(fName, O_RDONLY,S_IREAD | S_IWRITE);
274 printf("could not open file %s\n",fName);
278 totalSize = yaffs_lseek(h,0,SEEK_END);
279 yaffs_lseek(h,0,SEEK_SET);
281 r = yaffs_read(h,&sz32,sizeof(sz32));
283 if(r != sizeof(sz32)){
284 printf("reading size failed ... returned %d\n",r);
289 recordedSize = sz32 * sizeof(xx) + 8;
291 printf("verify %s: file size is %d, recorded size is %d\n", fName, totalSize, recordedSize);
292 if(totalSize != recordedSize){
293 printf("!!!!!!!!!!!!!!!!!!!!!!!!file size is wrong, should be %d, is %d\n", recordedSize,totalSize);
302 r = yaffs_read(h,xx,sizeof(xx));
304 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
308 for(i = 0; i < XX_SIZE; i++)
312 r = yaffs_read(h,xx,sizeof(xx[0]));
313 if(r != sizeof(xx[0])){
314 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
322 printf("!!!!!!!!!!!!!!!!!!!!! checksum failed\n");
325 printf("verified ok\n");
332 static void DoUpdateMainFile(void)
336 sz32 = (myrand() % 1000) + 20;
338 result = yWriteFile(fullTempMainName,sz32);
342 yaffs_rename(fullTempMainName,fullMainName);
346 static void DoVerifyMainFile(void)
349 result = yVerifyFile(fullMainName);
356 void NorStressTestInitialise(const char *prefix)
358 MakeFullNames(prefix);
360 UpdateCounter(fullPowerUpName,&powerUps,1);
361 UpdateCounter(fullStartName,&cycleStarts,1);
362 UpdateCounter(fullEndName,&cycleEnds,1);
363 UpdateCounter(fullPowerUpName,&powerUps,1);
369 void NorStressTestRun(const char *prefix, int n_cycles, int do_fsx)
371 interleave_fsx = do_fsx;
372 MakeFullNames(prefix);
375 dump_directory_tree(fullPathName);
377 UpdateCounter(fullPowerUpName,&powerUps,0);
378 dump_directory_tree(fullPathName);
380 while(n_cycles < 0 || n_cycles > 0){
383 UpdateCounter(fullStartName, &cycleStarts,0);
384 dump_directory_tree(fullPathName);
387 dump_directory_tree(fullPathName);
389 UpdateCounter(fullEndName,&cycleEnds,0);
390 dump_directory_tree(fullPathName);