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");
77 static void UpdateCounter(const char *name, unsigned *val, int initialise)
91 inh = yaffs_open(name,O_RDONLY, S_IREAD | S_IWRITE);
93 nread = yaffs_read(inh,x,sizeof(x));
97 if(nread != sizeof(x) ||
99 printf("Error reading counter %s handle %d, x[0] %u x[1] %u last error %d\n",
100 name, inh, x[0], x[1],yaffsfs_GetLastError());
109 outh = yaffs_open(fullTempCounterName, O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
112 nwritten = yaffs_write(outh,x,sizeof(x));
116 yaffs_rename(fullTempCounterName,name);
120 if(nwritten != sizeof(x)){
121 printf("Error writing counter %s handle %d, x[0] %u x[1] %u\n",
122 name, inh, x[0], x[1]);
129 "## Set counter %s to %u\n"
134 static void dump_directory_tree_worker(const char *dname,int recursive)
141 d = yaffs_opendir(dname);
145 printf("opendir failed\n");
149 while((de = yaffs_readdir(d)) != NULL)
153 strcat(str,de->d_name);
157 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);
158 switch(s.st_mode & S_IFMT)
160 case S_IFREG: printf("data file"); break;
161 case S_IFDIR: printf("directory"); break;
162 case S_IFLNK: printf("symlink -->");
163 if(yaffs_readlink(str,str,100) < 0)
166 printf("\"%s\"",str);
168 default: printf("unknown"); break;
173 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
174 dump_directory_tree_worker(str,1);
186 static void dump_directory_tree(const char *dname)
188 dump_directory_tree_worker(dname,1);
190 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
198 static unsigned xx[XX_SIZE];
200 static int yWriteFile(const char *fname, unsigned sz32)
205 unsigned checksum = 0;
207 printf("Writing file %s\n",fname);
210 h = yaffs_open(fname,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
214 printf("could not open file %s\n",fname);
221 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
226 for(i = 0; i < XX_SIZE; i++){
232 if((r = yaffs_write(h,xx,sizeof(xx))) != sizeof(xx)){
240 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
249 printf("ywrite error at position %d\n",(int)yaffs_lseek(h,0,SEEK_END));
255 static int yVerifyFile(const char *fName)
257 unsigned checksum = 0;
260 unsigned recordedSize = 0;
267 printf("Verifying file %s\n",fName);
269 h = yaffs_open(fName, O_RDONLY,S_IREAD | S_IWRITE);
272 printf("could not open file %s\n",fName);
276 totalSize = yaffs_lseek(h,0,SEEK_END);
277 yaffs_lseek(h,0,SEEK_SET);
279 r = yaffs_read(h,&sz32,sizeof(sz32));
281 if(r != sizeof(sz32)){
282 printf("reading size failed ... returned %d\n",r);
287 recordedSize = sz32 * sizeof(xx) + 8;
289 printf("verify %s: file size is %d, recorded size is %d\n", fName, totalSize, recordedSize);
290 if(totalSize != recordedSize){
291 printf("!!!!!!!!!!!!!!!!!!!!!!!!file size is wrong, should be %d, is %d\n", recordedSize,totalSize);
300 r = yaffs_read(h,xx,sizeof(xx));
302 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
306 for(i = 0; i < XX_SIZE; i++)
310 r = yaffs_read(h,xx,sizeof(xx[0]));
311 if(r != sizeof(xx[0])){
312 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
320 printf("!!!!!!!!!!!!!!!!!!!!! checksum failed\n");
323 printf("verified ok\n");
330 static void DoUpdateMainFile(void)
334 sz32 = (myrand() % 1000) + 20;
336 result = yWriteFile(fullTempMainName,sz32);
340 yaffs_rename(fullTempMainName,fullMainName);
344 static void DoVerifyMainFile(void)
347 result = yVerifyFile(fullMainName);
354 void NorStressTestInitialise(const char *prefix)
356 MakeFullNames(prefix);
358 UpdateCounter(fullPowerUpName,&powerUps,1);
359 UpdateCounter(fullStartName,&cycleStarts,1);
360 UpdateCounter(fullEndName,&cycleEnds,1);
361 UpdateCounter(fullPowerUpName,&powerUps,1);
367 void NorStressTestRun(const char *prefix, int n_cycles, int do_fsx)
369 interleave_fsx = do_fsx;
370 MakeFullNames(prefix);
373 dump_directory_tree(fullPathName);
375 UpdateCounter(fullPowerUpName,&powerUps,0);
376 dump_directory_tree(fullPathName);
378 while(n_cycles < 0 || n_cycles > 0){
381 UpdateCounter(fullStartName, &cycleStarts,0);
382 dump_directory_tree(fullPathName);
385 dump_directory_tree(fullPathName);
387 UpdateCounter(fullEndName,&cycleEnds,0);
388 dump_directory_tree(fullPathName);