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)
65 return 3;// ding two bits
71 case 3007: return 1;// ding one bit
78 static void yaffs_ModifyWriteData(int chunkInNAND,__u8 *data)
82 *data ^= yaffs_WriteFailCorruption(chunkInNAND);
86 static __u8 yaffs_ReadFailCorruption(int chunkInNAND)
91 return 3;// ding two bits
94 return 1;// ding one bit
101 static void yaffs_ModifyReadData(int chunkInNAND,__u8 *data)
105 *data ^= yaffs_ReadFailCorruption(chunkInNAND);
113 static void CheckInit(yaffs_Device *dev)
115 static int initialised = 0;
122 memset(ffChunk,0xFF,528);
124 h = open("yaffs-em-file" , O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
127 perror("Fatal error opening yaffs emulation file");
132 length = lseek(h,0,SEEK_END);
133 if(length != FILE_SIZE_IN_BYTES)
135 // Create file contents
138 printf("Creating emulation file...\n");
139 for(i = 0; i < FILE_SIZE_IN_BLOCKS; i++)
141 yaffs_FEEraseBlockInNAND(dev,i);
143 if(IsAMarkedBadBlock(i))
146 memset(&spare,0xff,sizeof(spare));
147 spare.blockStatus = 1;
149 yaffs_FEWriteChunkToNAND(dev, i * 32,NULL,&spare);
153 eraseDisplayEnabled = 1;
157 int yaffs_FEWriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
163 pos = chunkInNAND * 528;
170 memcpy(localData,data,512);
171 yaffs_ModifyWriteData(chunkInNAND,localData);
172 lseek(h,pos,SEEK_SET);
173 write(h,localData,512);
180 lseek(h,pos,SEEK_SET);
188 int yaffs_FEReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
192 pos = chunkInNAND * 528;
199 lseek(h,pos,SEEK_SET);
201 yaffs_ModifyReadData(chunkInNAND,data);
208 lseek(h,pos,SEEK_SET);
216 int yaffs_FEEraseBlockInNAND(yaffs_Device *dev,int blockInNAND)
222 if(eraseDisplayEnabled)
224 printf("Erasing block %d\n",blockInNAND);
227 lseek(h,blockInNAND * BLOCK_SIZE,SEEK_SET);
228 for(i = 0; i < 32; i++)
230 write(h,ffChunk,528);
236 case 15: return YAFFS_FAIL;
242 int yaffs_FEInitialiseNAND(yaffs_Device *dev)