Fix all Makefile headers to be the same format
[yaffs2.git] / direct / yaffs_fileem2k.c
index dcb3e5fc7e85a73c1f6b28473f2987bb9a3e9723..1e2cd6b3ef59156d9b4fbb26824414415ebf1a2e 100644 (file)
@@ -1,21 +1,22 @@
 /*
- * YAFFS: Yet another FFS. A NAND-flash specific file system. 
- * yaffs_ramdisk.c: yaffs ram disk component
+ * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  *
- * Copyright (C) 2002 Aleph One Ltd.
+ * Copyright (C) 2002-2007 Aleph One Ltd.
+ *   for Toby Churchill Ltd and Brightstar Engineering
  *
  * Created by Charles Manning <charles@aleph1.co.uk>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
- *
  */
 
-// This provides a YAFFS nand emulation on a file for emulating 2kB pages.
-// THis is only intended as test code to test persistence etc.
+/*
+ * This provides a YAFFS nand emulation on a file for emulating 2kB pages.
+ * This is only intended as test code to test persistence etc.
+ */
 
-const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.6 2006-10-03 10:13:03 charles Exp $";
+const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.12 2007-02-14 01:09:06 wookey Exp $";
 
 
 #include "yportenv.h"
@@ -32,7 +33,7 @@ const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.6 2006-10-03 10
 #include "yaffs_fileem2k.h"
 #include "yaffs_packedtags2.h"
 
-
+//#define SIMULATE_FAILURES
 
 typedef struct 
 {
@@ -61,6 +62,10 @@ static yflash_Device filedisk;
 int yaffs_testPartialWrite = 0;
 
 
+
+
+static __u8 localBuffer[PAGE_SIZE];
+
 static char *NToName(char *buf,int n)
 {
        sprintf(buf,"emfile%d",n);
@@ -144,7 +149,12 @@ int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8
        int written;
        int pos;
        int h;
+       int i;
+       int nRead;
+       int error;
        
+       T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
+
        CheckInit();
        
        
@@ -155,13 +165,38 @@ int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8
                h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
                
                lseek(h,pos,SEEK_SET);
-               written = write(h,data,dev->nDataBytesPerChunk);
+               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;
+                       }
+               }
+               
+               for(i = 0; i < dev->nDataBytesPerChunk; i++)
+                 localBuffer[i] &= data[i];
+                 
+               if(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);
                }
                
+#ifdef SIMULATE_FAILURES
+                       if((chunkInNAND >> 6) == 100) 
+                         written = 0;
+
+                       if((chunkInNAND >> 6) == 110) 
+                         written = 0;
+#endif
+
+
                if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
        }
        
@@ -171,6 +206,7 @@ int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8
                h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
                
                lseek(h,pos,SEEK_SET);
+
                if( 0 && dev->isYaffs2)
                {
                        
@@ -181,8 +217,26 @@ int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8
                {
                        yaffs_PackedTags2 pt;
                        yaffs_PackTags2(&pt,tags);
-
-                       written = write(h,&pt,sizeof(pt));
+                       __u8 * ptab = (__u8 *)&pt;
+
+                       nRead = read(h,localBuffer,sizeof(pt));
+                       for(i = error = 0; 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];
+                        
+                       if(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;
                }
        }
@@ -204,12 +258,19 @@ int yaffs_CheckAllFF(const __u8 *ptr, int n)
 }
 
 
+static int fail300 = 1;
+static int fail320 = 1;
+
+static int failRead10 = 2;
+
 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
 {
        int nread;
        int pos;
        int h;
        
+       T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
+       
        CheckInit();
        
        
@@ -222,6 +283,7 @@ int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *da
                lseek(h,pos,SEEK_SET);
                nread = read(h,data,dev->nDataBytesPerChunk);
                
+               
                if(nread != dev->nDataBytesPerChunk) return YAFFS_FAIL;
        }
        
@@ -249,6 +311,26 @@ int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *da
                        yaffs_PackedTags2 pt;
                        nread= read(h,&pt,sizeof(pt));
                        yaffs_UnpackTags2(tags,&pt);
+#ifdef SIMULATE_FAILURES
+                       if((chunkInNAND >> 6) == 100) {
+                           if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
+                              tags->eccResult = YAFFS_ECC_RESULT_FIXED;
+                              fail300 = 0;
+                           }
+                           
+                       }
+                       if((chunkInNAND >> 6) == 110) {
+                           if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
+                              tags->eccResult = YAFFS_ECC_RESULT_FIXED;
+                              fail320 = 0;
+                           }
+                       }
+#endif
+                       if(failRead10>0 && chunkInNAND == 10){
+                               failRead10--;
+                               nread = 0;
+                       }
+                       
                        if(nread != sizeof(pt)) return YAFFS_FAIL;
                }
        }
@@ -288,6 +370,11 @@ int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
                
        CheckInit();
        
+       printf("erase block %d\n",blockNumber);
+       
+       if(blockNumber == 320)
+               fail320 = 1;
+       
        if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
        {
                T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));