2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * This provides a YAFFS nand emulation on a file for emulating 2kB pages.
16 * This is only intended as test code to test persistence etc.
19 const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.12 2007-02-14 01:09:06 wookey Exp $";
24 #include "yaffs_flashif.h"
25 #include "yaffs_guts.h"
26 #include "devextras.h"
28 #include <sys/types.h>
33 #include "yaffs_fileem2k.h"
34 #include "yaffs_packedtags2.h"
36 //#define SIMULATE_FAILURES
40 __u8 data[PAGE_SIZE]; // Data + spare
45 yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
51 #define MAX_HANDLES 20
52 #define BLOCKS_PER_HANDLE 8000
56 int handle[MAX_HANDLES];
60 static yflash_Device filedisk;
62 int yaffs_testPartialWrite = 0;
67 static __u8 localBuffer[PAGE_SIZE];
69 static char *NToName(char *buf,int n)
71 sprintf(buf,"emfile%d",n);
75 static char dummyBuffer[BLOCK_SIZE];
77 static int GetBlockFileHandle(int n)
87 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
89 fSize = lseek(h,0,SEEK_END);
90 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
91 if(fSize < requiredSize){
92 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
93 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
103 static int CheckInit(void)
105 static int initialised = 0;
124 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
127 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
129 for(i = 0; i < MAX_HANDLES; i++)
130 filedisk.handle[i] = -1;
132 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
133 filedisk.handle[i] = GetBlockFileHandle(i);
140 int yflash_GetNumberOfBlocks(void)
144 return filedisk.nBlocks;
147 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
156 T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
164 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
165 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
167 lseek(h,pos,SEEK_SET);
168 nRead = read(h, localBuffer,dev->nDataBytesPerChunk);
169 for(i = error = 0; i < dev->nDataBytesPerChunk && !error; i++){
170 if(localBuffer[i] != 0xFF){
171 printf("nand simulation: chunk %d data byte %d was %0x2\n",
172 chunkInNAND,i,localBuffer[i]);
177 for(i = 0; i < dev->nDataBytesPerChunk; i++)
178 localBuffer[i] &= data[i];
180 if(memcmp(localBuffer,data,dev->nDataBytesPerChunk))
181 printf("nand simulator: data does not match\n");
183 lseek(h,pos,SEEK_SET);
184 written = write(h,localBuffer,dev->nDataBytesPerChunk);
186 if(yaffs_testPartialWrite){
191 #ifdef SIMULATE_FAILURES
192 if((chunkInNAND >> 6) == 100)
195 if((chunkInNAND >> 6) == 110)
200 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
205 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
206 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
208 lseek(h,pos,SEEK_SET);
210 if( 0 && dev->isYaffs2)
213 written = write(h,tags,sizeof(yaffs_ExtendedTags));
214 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
218 yaffs_PackedTags2 pt;
219 yaffs_PackTags2(&pt,tags);
220 __u8 * ptab = (__u8 *)&pt;
222 nRead = read(h,localBuffer,sizeof(pt));
223 for(i = error = 0; i < sizeof(pt) && !error; i++){
224 if(localBuffer[i] != 0xFF){
225 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
226 chunkInNAND,i,localBuffer[i]);
231 for(i = 0; i < sizeof(pt); i++)
232 localBuffer[i] &= ptab[i];
234 if(memcmp(localBuffer,&pt,sizeof(pt)))
235 printf("nand sim: tags corruption\n");
237 lseek(h,pos,SEEK_SET);
239 written = write(h,localBuffer,sizeof(pt));
240 if(written != sizeof(pt)) return YAFFS_FAIL;
249 int yaffs_CheckAllFF(const __u8 *ptr, int n)
254 if(*ptr!=0xFF) return 0;
261 static int fail300 = 1;
262 static int fail320 = 1;
264 static int failRead10 = 2;
266 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
272 T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
281 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
282 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
283 lseek(h,pos,SEEK_SET);
284 nread = read(h,data,dev->nDataBytesPerChunk);
287 if(nread != dev->nDataBytesPerChunk) return YAFFS_FAIL;
292 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
293 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
294 lseek(h,pos,SEEK_SET);
296 if(0 && dev->isYaffs2)
298 nread= read(h,tags,sizeof(yaffs_ExtendedTags));
299 if(nread != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
300 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
302 yaffs_InitialiseTags(tags);
311 yaffs_PackedTags2 pt;
312 nread= read(h,&pt,sizeof(pt));
313 yaffs_UnpackTags2(tags,&pt);
314 #ifdef SIMULATE_FAILURES
315 if((chunkInNAND >> 6) == 100) {
316 if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
317 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
322 if((chunkInNAND >> 6) == 110) {
323 if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
324 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
329 if(failRead10>0 && chunkInNAND == 10){
334 if(nread != sizeof(pt)) return YAFFS_FAIL;
344 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
349 yaffs_PackedTags2 pt;
353 memset(&pt,0,sizeof(pt));
354 h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
355 lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
356 written = write(h,&pt,sizeof(pt));
358 if(written != sizeof(pt)) return YAFFS_FAIL;
365 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
373 printf("erase block %d\n",blockNumber);
375 if(blockNumber == 320)
378 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
380 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
393 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
394 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);
395 for(i = 0; i < dev->nChunksPerBlock; i++)
397 write(h,pg,PAGE_SIZE);
399 pos = lseek(h, 0,SEEK_CUR);
406 int yflash_InitialiseNAND(yaffs_Device *dev)
416 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
418 yaffs_ExtendedTags tags;
423 chunkNo = blockNo * dev->nChunksPerBlock;
425 yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
428 *state = YAFFS_BLOCK_STATE_DEAD;
430 else if(!tags.chunkUsed)
432 *state = YAFFS_BLOCK_STATE_EMPTY;
434 else if(tags.chunkUsed)
436 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
437 *sequenceNumber = tags.sequenceNumber;