2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
3 * yaffs_fileem.c NAND emulation on top of files
5 * Copyright (C) 2002 Aleph One Ltd.
6 * for Toby Churchill Ltd and Brightstar Engineering
8 * Created by Charles Manning <charles@aleph1.co.uk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
17 #include "yaffs_fileem.h"
18 #include "yaffs_guts.h"
19 #include "yaffsinterface.h"
24 #include <sys/types.h>
29 #define FILE_SIZE_IN_MEG 32
31 #define BLOCK_SIZE (32 * 528)
32 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
33 #define FILE_SIZE_IN_BLOCKS (FILE_SIZE_IN_MEG * BLOCKS_PER_MEG)
34 #define FILE_SIZE_IN_BYTES (FILE_SIZE_IN_BLOCKS * BLOCK_SIZE)
38 static __u8 ffChunk[528];
40 static int eraseDisplayEnabled;
42 static int markedBadBlocks[] = { 1, 4, -1};
44 static int IsAMarkedBadBlock(int blk)
46 int *m = markedBadBlocks;
50 if(*m == blk) return 1;
57 static __u8 yaffs_WriteFailCorruption(int chunkInNAND)
60 // Whole blocks that fail
61 switch(chunkInNAND/YAFFS_CHUNKS_PER_BLOCK)
68 // Single blocks that fail
75 return 3;// ding two bits
81 case 3007: return 1;// ding one bit
89 static void yaffs_ModifyWriteData(int chunkInNAND,__u8 *data)
93 *data ^= yaffs_WriteFailCorruption(chunkInNAND);
97 static __u8 yaffs_ReadFailCorruption(int chunkInNAND)
102 return 3;// ding two bits
105 return 1;// ding one bit
112 static void yaffs_ModifyReadData(int chunkInNAND,__u8 *data)
116 *data ^= yaffs_ReadFailCorruption(chunkInNAND);
124 static void CheckInit(yaffs_Device *dev)
126 static int initialised = 0;
133 memset(ffChunk,0xFF,528);
135 h = open("yaffs-em-file" , O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
138 perror("Fatal error opening yaffs emulation file");
143 length = lseek(h,0,SEEK_END);
144 if(length != FILE_SIZE_IN_BYTES)
146 // Create file contents
149 printf("Creating emulation file...\n");
150 for(i = 0; i < FILE_SIZE_IN_BLOCKS; i++)
152 yaffs_FEEraseBlockInNAND(dev,i);
154 if(IsAMarkedBadBlock(i))
157 memset(&spare,0xff,sizeof(spare));
158 spare.blockStatus = 1;
160 yaffs_FEWriteChunkToNAND(dev, i * 32,NULL,&spare);
164 eraseDisplayEnabled = 1;
168 int yaffs_FEWriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
174 pos = chunkInNAND * 528;
181 memcpy(localData,data,512);
182 yaffs_ModifyWriteData(chunkInNAND,localData);
183 lseek(h,pos,SEEK_SET);
184 write(h,localData,512);
191 lseek(h,pos,SEEK_SET);
199 int yaffs_FEReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
203 pos = chunkInNAND * 528;
210 lseek(h,pos,SEEK_SET);
212 yaffs_ModifyReadData(chunkInNAND,data);
219 lseek(h,pos,SEEK_SET);
227 int yaffs_FEEraseBlockInNAND(yaffs_Device *dev,int blockInNAND)
233 if(eraseDisplayEnabled)
235 printf("Erasing block %d\n",blockInNAND);
238 lseek(h,blockInNAND * BLOCK_SIZE,SEEK_SET);
239 for(i = 0; i < 32; i++)
241 write(h,ffChunk,528);
247 case 15: return YAFFS_FAIL;
253 int yaffs_FEInitialiseNAND(yaffs_Device *dev)