1 #include "nor_stress.h"
10 static unsigned powerUps;
11 static unsigned cycleStarts;
12 static unsigned cycleEnds;
15 char fullPathName[100];
16 char fullPowerUpName[100];
17 char fullStartName[100];
18 char fullEndName[100];
19 char fullMainName[100];
20 char fullTempMainName[100];
21 char fullTempCounterName[100];
24 void MakeName(char *fullName,const char *prefix, const char *name)
26 strcpy(fullName,prefix);
28 strcat(fullName,name);
32 void MakeFullNames(const char *prefix)
34 MakeName(fullPathName,prefix,"");
35 MakeName(fullPowerUpName,prefix,"powerUps");
36 MakeName(fullStartName,prefix,"starts");
37 MakeName(fullEndName,prefix,"ends");
38 MakeName(fullMainName,prefix,"main");
39 MakeName(fullTempCounterName,prefix,"tmp-counter");
40 MakeName(fullTempMainName,prefix,"tmp-main");
43 static void FatalError(void)
45 printf("Integrity error\n");
49 static void UpdateCounter(const char *name, unsigned *val, int initialise)
63 inh = yaffs_open(name,O_RDONLY, S_IREAD | S_IWRITE);
65 nread = yaffs_read(inh,x,sizeof(x));
69 if(nread != sizeof(x) ||
71 printf("Error reading counter %s handle %d, x[0] %u x[1] %u last error %d\n",
72 name, inh, x[0], x[1],yaffsfs_GetLastError());
80 outh = yaffs_open(fullTempCounterName, O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
82 nwritten = yaffs_write(outh,x,sizeof(x));
84 yaffs_rename(fullTempCounterName,name);
87 if(nwritten != sizeof(x)){
88 printf("Error writing counter %s handle %d, x[0] %u x[1] %u\n",
89 name, inh, x[0], x[1]);
96 "## Set counter %s to %u\n"
101 static void dump_directory_tree_worker(const char *dname,int recursive)
108 d = yaffs_opendir(dname);
112 printf("opendir failed\n");
116 while((de = yaffs_readdir(d)) != NULL)
120 strcat(str,de->d_name);
124 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);
125 switch(s.st_mode & S_IFMT)
127 case S_IFREG: printf("data file"); break;
128 case S_IFDIR: printf("directory"); break;
129 case S_IFLNK: printf("symlink -->");
130 if(yaffs_readlink(str,str,100) < 0)
133 printf("\"%s\"",str);
135 default: printf("unknown"); break;
140 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
141 dump_directory_tree_worker(str,1);
150 static void dump_directory_tree(const char *dname)
152 dump_directory_tree_worker(dname,1);
154 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
162 static unsigned xx[XX_SIZE];
164 static int yWriteFile(const char *fname, unsigned sz32)
169 unsigned checksum = 0;
171 printf("Writing file %s\n",fname);
173 h = yaffs_open(fname,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
176 printf("could not open file %s\n",fname);
183 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
188 for(i = 0; i < XX_SIZE; i++){
193 if((r = yaffs_write(h,xx,sizeof(xx))) != sizeof(xx)){
201 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
210 printf("ywrite error at position %d\n",(int)yaffs_lseek(h,0,SEEK_END));
216 static int yVerifyFile(const char *fName)
218 unsigned checksum = 0;
221 unsigned recordedSize = 0;
228 printf("Verifying file %s\n",fName);
230 h = yaffs_open(fName, O_RDONLY,S_IREAD | S_IWRITE);
233 printf("could not open file %s\n",fName);
237 totalSize = yaffs_lseek(h,0,SEEK_END);
238 yaffs_lseek(h,0,SEEK_SET);
240 r = yaffs_read(h,&sz32,sizeof(sz32));
242 if(r != sizeof(sz32)){
243 printf("reading size failed ... returned %d\n",r);
248 recordedSize = sz32 * sizeof(xx) + 8;
250 printf("verify %s: file size is %d, recorded size is %d\n", fName, totalSize, recordedSize);
251 if(totalSize != recordedSize){
252 printf("!!!!!!!!!!!!!!!!!!!!!!!!file size is wrong, should be %d, is %d\n", recordedSize,totalSize);
261 r = yaffs_read(h,xx,sizeof(xx));
263 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
267 for(i = 0; i < XX_SIZE; i++)
271 r = yaffs_read(h,xx,sizeof(xx[0]));
272 if(r != sizeof(xx[0])){
273 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
281 printf("!!!!!!!!!!!!!!!!!!!!! checksum failed\n");
284 printf("verified ok\n");
290 static unsigned long next = 1;
292 next = next * 1103515245 + 12345;
293 return((unsigned)(next/65536) % 32768);
296 static void DoUpdateMainFile(void)
300 sz32 = (myrand() % 1000) + 20;
302 result = yWriteFile(fullTempMainName,sz32);
305 yaffs_rename(fullTempMainName,fullMainName);
308 static void DoVerifyMainFile(void)
311 result = yVerifyFile(fullMainName);
318 void NorStressTestInitialise(const char *prefix)
320 MakeFullNames(prefix);
323 yaffs_mount(fullPathName);
324 UpdateCounter(fullPowerUpName,&powerUps,1);
325 UpdateCounter(fullStartName,&cycleStarts,1);
326 UpdateCounter(fullEndName,&cycleEnds,1);
327 UpdateCounter(fullPowerUpName,&powerUps,1);
330 yaffs_unmount(fullPathName);
334 void NorStressTestRun(const char *prefix)
336 MakeFullNames(prefix);
339 yaffs_mount(fullPathName);
341 dump_directory_tree(fullPathName);
343 UpdateCounter(fullPowerUpName,&powerUps,0);
346 UpdateCounter(fullStartName, &cycleStarts,0);
349 dump_directory_tree(fullPathName);
351 UpdateCounter(fullEndName,&cycleEnds,0);