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);
153 static void dump_directory_tree(const char *dname)
155 dump_directory_tree_worker(dname,1);
157 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
165 static unsigned xx[XX_SIZE];
167 static int yWriteFile(const char *fname, unsigned sz32)
172 unsigned checksum = 0;
174 printf("Writing file %s\n",fname);
176 h = yaffs_open(fname,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
179 printf("could not open file %s\n",fname);
186 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
191 for(i = 0; i < XX_SIZE; i++){
196 if((r = yaffs_write(h,xx,sizeof(xx))) != sizeof(xx)){
204 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
213 printf("ywrite error at position %d\n",(int)yaffs_lseek(h,0,SEEK_END));
219 static int yVerifyFile(const char *fName)
221 unsigned checksum = 0;
224 unsigned recordedSize = 0;
231 printf("Verifying file %s\n",fName);
233 h = yaffs_open(fName, O_RDONLY,S_IREAD | S_IWRITE);
236 printf("could not open file %s\n",fName);
240 totalSize = yaffs_lseek(h,0,SEEK_END);
241 yaffs_lseek(h,0,SEEK_SET);
243 r = yaffs_read(h,&sz32,sizeof(sz32));
245 if(r != sizeof(sz32)){
246 printf("reading size failed ... returned %d\n",r);
251 recordedSize = sz32 * sizeof(xx) + 8;
253 printf("verify %s: file size is %d, recorded size is %d\n", fName, totalSize, recordedSize);
254 if(totalSize != recordedSize){
255 printf("!!!!!!!!!!!!!!!!!!!!!!!!file size is wrong, should be %d, is %d\n", recordedSize,totalSize);
264 r = yaffs_read(h,xx,sizeof(xx));
266 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
270 for(i = 0; i < XX_SIZE; i++)
274 r = yaffs_read(h,xx,sizeof(xx[0]));
275 if(r != sizeof(xx[0])){
276 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
284 printf("!!!!!!!!!!!!!!!!!!!!! checksum failed\n");
287 printf("verified ok\n");
293 extern int random_seed;
296 random_seed = random_seed * 1103515245 + 12345;
297 return((unsigned)(random_seed/65536) % 32768);
300 static void DoUpdateMainFile(void)
304 sz32 = (myrand() % 1000) + 20;
306 result = yWriteFile(fullTempMainName,sz32);
309 yaffs_rename(fullTempMainName,fullMainName);
312 static void DoVerifyMainFile(void)
315 result = yVerifyFile(fullMainName);
322 void NorStressTestInitialise(const char *prefix)
324 MakeFullNames(prefix);
327 yaffs_mount(fullPathName);
328 UpdateCounter(fullPowerUpName,&powerUps,1);
329 UpdateCounter(fullStartName,&cycleStarts,1);
330 UpdateCounter(fullEndName,&cycleEnds,1);
331 UpdateCounter(fullPowerUpName,&powerUps,1);
334 yaffs_unmount(fullPathName);
338 void NorStressTestRun(const char *prefix)
340 MakeFullNames(prefix);
343 yaffs_mount(fullPathName);
345 dump_directory_tree(fullPathName);
347 UpdateCounter(fullPowerUpName,&powerUps,0);
348 dump_directory_tree(fullPathName);
351 UpdateCounter(fullStartName, &cycleStarts,0);
352 dump_directory_tree(fullPathName);
355 dump_directory_tree(fullPathName);
357 UpdateCounter(fullEndName,&cycleEnds,0);
358 dump_directory_tree(fullPathName);