Allow the disabling of tags ECC
[yaffs2.git] / direct / yaffs_fileem2k.c
index cb944869d5445c605cdc7aa1ffdd52702dd66562..cfe992e0de674d0165dd9e31f139473f81b60ea2 100644 (file)
  * This is only intended as test code to test persistence etc.
  */
 
  * This is only intended as test code to test persistence etc.
  */
 
-const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.13 2008-05-05 07:58:58 charles Exp $";
+const char *yaffs_flashif2_c_version = "$Id: yaffs_fileem2k.c,v 1.19 2009-10-14 00:01:57 charles Exp $";
 
 
 #include "yportenv.h"
 
 
 
 #include "yportenv.h"
 
-#include "yaffs_flashif.h"
+#include "yaffs_flashif2.h"
 #include "yaffs_guts.h"
 #include "devextras.h"
 
 #include "yaffs_guts.h"
 #include "devextras.h"
 
@@ -33,7 +33,9 @@ const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.13 2008-05-05 0
 #include "yaffs_fileem2k.h"
 #include "yaffs_packedtags2.h"
 
 #include "yaffs_fileem2k.h"
 #include "yaffs_packedtags2.h"
 
-//#define SIMULATE_FAILURES
+
+
+#define REPORT_ERROR 0
 
 typedef struct 
 {
 
 typedef struct 
 {
@@ -49,7 +51,7 @@ typedef struct
 
 
 #define MAX_HANDLES 20
 
 
 #define MAX_HANDLES 20
-#define BLOCKS_PER_HANDLE 8000
+#define BLOCKS_PER_HANDLE (32*8)
 
 typedef struct
 {
 
 typedef struct
 {
@@ -61,6 +63,30 @@ static yflash_Device filedisk;
 
 int yaffs_testPartialWrite = 0;
 
 
 int yaffs_testPartialWrite = 0;
 
+extern int random_seed;
+extern int simulate_power_failure;
+static int initialised = 0;
+static int remaining_ops;
+static int nops_so_far;
+
+int ops_multiplier;
+
+
+static void yflash2_MaybePowerFail(void)
+{
+
+   nops_so_far++;
+   
+   
+   remaining_ops--;
+   if(simulate_power_failure &&
+      remaining_ops < 1){
+       printf("Simulated power failure after %d operations\n",nops_so_far);
+       exit(0);
+  }
+}
+
+
 
 
 
 
 
 
@@ -68,7 +94,7 @@ static __u8 localBuffer[PAGE_SIZE];
 
 static char *NToName(char *buf,int n)
 {
 
 static char *NToName(char *buf,int n)
 {
-       sprintf(buf,"emfile%d",n);
+       sprintf(buf,"emfile-2k-%d",n);
        return buf;
 }
 
        return buf;
 }
 
@@ -115,7 +141,11 @@ static int  CheckInit(void)
 
        initialised = 1;
        
 
        initialised = 1;
        
-       memset(dummyBuffer,0xff,sizeof(dummyBuffer));
+
+       srand(random_seed);
+       remaining_ops = (rand() % 1000) * 5;
+       memset(dummyBuffer,0xff,sizeof(dummyBuffer));
+
        
        
        filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
        
        
        filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
@@ -131,14 +161,14 @@ static int  CheckInit(void)
 }
 
 
 }
 
 
-int yflash_GetNumberOfBlocks(void)
+int yflash2_GetNumberOfBlocks(void)
 {
        CheckInit();
        
        return filedisk.nBlocks;
 }
 
 {
        CheckInit();
        
        return filedisk.nBlocks;
 }
 
-int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
+int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
 {
        int written;
        int pos;
 {
        int written;
        int pos;
@@ -176,26 +206,106 @@ int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8
        }
        
        else {
        }
        
        else {
-       
+               /* First do a write of a partial page */
+               int n_partials;
+               int bpos;
+
                if(data)
                {
                        pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
                        h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
                
                if(data)
                {
                        pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
                        h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
                
+                       
+                       memcpy(localBuffer,data, dev->nDataBytesPerChunk);
+                       
+                       n_partials = rand()%20;
+                       
+                       for(i = 0; i < n_partials; i++){
+                               bpos = rand() % dev->nDataBytesPerChunk;
+                               
+                               localBuffer[bpos] |= (1 << (rand() & 7));
+                       }
+                 
+                       if(REPORT_ERROR && memcmp(localBuffer,data,dev->nDataBytesPerChunk))
+                               printf("nand simulator: data does not match\n");
+                       
+                       lseek(h,pos,SEEK_SET);
+                       written = write(h,localBuffer,dev->nDataBytesPerChunk);
+               
+                       if(yaffs_testPartialWrite){
+                               close(h);
+                               exit(1);
+                       }
+
+
+                       if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
+               }
+               yflash2_MaybePowerFail();
+       
+               if(tags)
+               {
+                       pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
+                       h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
+               
                        lseek(h,pos,SEEK_SET);
                        lseek(h,pos,SEEK_SET);
-                       nRead =  read(h, localBuffer,dev->nDataBytesPerChunk);
-                       for(i = error = 0; i < dev->nDataBytesPerChunk && !error; i++){
-                               if(localBuffer[i] != 0xFF){
-                                       printf("nand simulation: chunk %d data byte %d was %0x2\n",
-                                               chunkInNAND,i,localBuffer[i]);
-                                       error = 1;
+
+                       if( 0 && dev->isYaffs2)
+                       {
+                       
+                               written = write(h,tags,sizeof(yaffs_ExtendedTags));
+                               if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
+                       }
+                       else
+                       {
+                               yaffs_PackedTags2 pt;
+                               yaffs_PackTags2(dev,&pt,tags);
+                               __u8 * ptab = (__u8 *)&pt;
+
+                               nRead = read(h,localBuffer,sizeof(pt));
+                               for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
+                                       if(localBuffer[i] != 0xFF){
+                                               printf("nand simulation: chunk %d oob byte %d was %0x2\n",
+                                                       chunkInNAND,i,localBuffer[i]);
+                                                       error = 1;
+                                       }
                                }
                                }
+               
+                               for(i = 0; i < sizeof(pt); i++)
+                               localBuffer[i] &= ptab[i];
+                               
+                               n_partials = rand()% sizeof(pt);
+                       
+                               for(i = 0; i < n_partials; i++){
+                                       bpos = rand() % sizeof(pt);
+                               
+                                       localBuffer[bpos] |= (1 << (rand() & 7));
+                               }                       
+                        
+                               if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
+                                       printf("nand sim: tags corruption\n");
+                               
+                               lseek(h,pos,SEEK_SET);
+                       
+                               written = write(h,localBuffer,sizeof(pt));
+                               if(written != sizeof(pt)) return YAFFS_FAIL;
                        }
                        }
+               }
+               
+               //yflash2_MaybePowerFail();
                
                
-                       for(i = 0; i < dev->nDataBytesPerChunk; i++)
-                       localBuffer[i] &= data[i];
+               /* Next do the whole write */
+               if(data)
+               {
+                       pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
+                       h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
+               
+                       
+                       memset(localBuffer,0xFF, PAGE_SIZE);            
+                       for(i = 0; i < dev->nDataBytesPerChunk; i++){
+                               localBuffer[i] &= data[i];
+                       }
                  
                  
-                       if(memcmp(localBuffer,data,dev->nDataBytesPerChunk))
+                       if(REPORT_ERROR && memcmp(localBuffer,data,dev->nDataBytesPerChunk))
                                printf("nand simulator: data does not match\n");
                        
                        lseek(h,pos,SEEK_SET);
                                printf("nand simulator: data does not match\n");
                        
                        lseek(h,pos,SEEK_SET);
@@ -205,14 +315,6 @@ int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8
                                close(h);
                                exit(1);
                        }
                                close(h);
                                exit(1);
                        }
-               
-#ifdef SIMULATE_FAILURES
-                               if((chunkInNAND >> 6) == 100) 
-                               written = 0;
-
-                               if((chunkInNAND >> 6) == 110) 
-                               written = 0;
-#endif
 
 
                        if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
 
 
                        if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
@@ -234,11 +336,11 @@ int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8
                        else
                        {
                                yaffs_PackedTags2 pt;
                        else
                        {
                                yaffs_PackedTags2 pt;
-                               yaffs_PackTags2(&pt,tags);
+                               yaffs_PackTags2(dev,&pt,tags);
                                __u8 * ptab = (__u8 *)&pt;
 
                                nRead = read(h,localBuffer,sizeof(pt));
                                __u8 * ptab = (__u8 *)&pt;
 
                                nRead = read(h,localBuffer,sizeof(pt));
-                               for(i = error = 0; i < sizeof(pt) && !error; i++){
+                               for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
                                        if(localBuffer[i] != 0xFF){
                                                printf("nand simulation: chunk %d oob byte %d was %0x2\n",
                                                        chunkInNAND,i,localBuffer[i]);
                                        if(localBuffer[i] != 0xFF){
                                                printf("nand simulation: chunk %d oob byte %d was %0x2\n",
                                                        chunkInNAND,i,localBuffer[i]);
@@ -249,7 +351,7 @@ int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8
                                for(i = 0; i < sizeof(pt); i++)
                                localBuffer[i] &= ptab[i];
                         
                                for(i = 0; i < sizeof(pt); i++)
                                localBuffer[i] &= ptab[i];
                         
-                               if(memcmp(localBuffer,&pt,sizeof(pt)))
+                               if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
                                        printf("nand sim: tags corruption\n");
                                
                                lseek(h,pos,SEEK_SET);
                                        printf("nand sim: tags corruption\n");
                                
                                lseek(h,pos,SEEK_SET);
@@ -258,6 +360,8 @@ int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8
                                if(written != sizeof(pt)) return YAFFS_FAIL;
                        }
                }
                                if(written != sizeof(pt)) return YAFFS_FAIL;
                        }
                }
+               
+               yflash2_MaybePowerFail();
        
        }
        return YAFFS_OK;        
        
        }
        return YAFFS_OK;        
@@ -281,7 +385,7 @@ static int fail320 = 1;
 
 static int failRead10 = 2;
 
 
 static int failRead10 = 2;
 
-int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
+int yflash2_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
 {
        int nread;
        int pos;
 {
        int nread;
        int pos;
@@ -367,7 +471,7 @@ int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *da
                        {
                                yaffs_PackedTags2 pt;
                                nread= read(h,&pt,sizeof(pt));
                        {
                                yaffs_PackedTags2 pt;
                                nread= read(h,&pt,sizeof(pt));
-                               yaffs_UnpackTags2(tags,&pt);
+                               yaffs_UnpackTags2(dev,tags,&pt);
 #ifdef SIMULATE_FAILURES
                                if((chunkInNAND >> 6) == 100) {
                                        if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
 #ifdef SIMULATE_FAILURES
                                if((chunkInNAND >> 6) == 100) {
                                        if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
@@ -401,7 +505,7 @@ int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *da
 }
 
 
 }
 
 
-int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
+int yflash2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
 {
        int written;
        int h;
 {
        int written;
        int h;
@@ -422,7 +526,7 @@ int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
        
 }
 
        
 }
 
-int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
+int yflash2_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
 {
 
        int i;
 {
 
        int i;
@@ -463,7 +567,7 @@ int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
        
 }
 
        
 }
 
-int yflash_InitialiseNAND(yaffs_Device *dev)
+int yflash2_InitialiseNAND(yaffs_Device *dev)
 {
        CheckInit();
        
 {
        CheckInit();
        
@@ -473,7 +577,7 @@ int yflash_InitialiseNAND(yaffs_Device *dev)
 
 
 
 
 
 
-int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, __u32 *sequenceNumber)
+int yflash2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, __u32 *sequenceNumber)
 {
        yaffs_ExtendedTags tags;
        int chunkNo;
 {
        yaffs_ExtendedTags tags;
        int chunkNo;
@@ -482,7 +586,7 @@ int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_Blo
        
        chunkNo = blockNo * dev->nChunksPerBlock;
        
        
        chunkNo = blockNo * dev->nChunksPerBlock;
        
-       yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
+       yflash2_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
        if(tags.blockBad)
        {
                *state = YAFFS_BLOCK_STATE_DEAD;
        if(tags.blockBad)
        {
                *state = YAFFS_BLOCK_STATE_DEAD;