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.13 2008-05-05 07:58:58 charles 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;
118 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
121 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
123 for(i = 0; i < MAX_HANDLES; i++)
124 filedisk.handle[i] = -1;
126 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
127 filedisk.handle[i] = GetBlockFileHandle(i);
134 int yflash_GetNumberOfBlocks(void)
138 return filedisk.nBlocks;
141 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
150 T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
157 yaffs_PackedTags2TagsPart * pt2tp;
158 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
159 yaffs_PackTags2TagsPart(pt2tp,tags);
161 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
162 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
164 lseek(h,pos,SEEK_SET);
165 written = write(h,data,dev->totalBytesPerChunk);
168 if(yaffs_testPartialWrite){
173 if(written != dev->totalBytesPerChunk) return YAFFS_FAIL;
182 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
183 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
185 lseek(h,pos,SEEK_SET);
186 nRead = read(h, localBuffer,dev->nDataBytesPerChunk);
187 for(i = error = 0; i < dev->nDataBytesPerChunk && !error; i++){
188 if(localBuffer[i] != 0xFF){
189 printf("nand simulation: chunk %d data byte %d was %0x2\n",
190 chunkInNAND,i,localBuffer[i]);
195 for(i = 0; i < dev->nDataBytesPerChunk; i++)
196 localBuffer[i] &= data[i];
198 if(memcmp(localBuffer,data,dev->nDataBytesPerChunk))
199 printf("nand simulator: data does not match\n");
201 lseek(h,pos,SEEK_SET);
202 written = write(h,localBuffer,dev->nDataBytesPerChunk);
204 if(yaffs_testPartialWrite){
209 #ifdef SIMULATE_FAILURES
210 if((chunkInNAND >> 6) == 100)
213 if((chunkInNAND >> 6) == 110)
218 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
223 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
224 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
226 lseek(h,pos,SEEK_SET);
228 if( 0 && dev->isYaffs2)
231 written = write(h,tags,sizeof(yaffs_ExtendedTags));
232 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
236 yaffs_PackedTags2 pt;
237 yaffs_PackTags2(&pt,tags);
238 __u8 * ptab = (__u8 *)&pt;
240 nRead = read(h,localBuffer,sizeof(pt));
241 for(i = error = 0; i < sizeof(pt) && !error; i++){
242 if(localBuffer[i] != 0xFF){
243 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
244 chunkInNAND,i,localBuffer[i]);
249 for(i = 0; i < sizeof(pt); i++)
250 localBuffer[i] &= ptab[i];
252 if(memcmp(localBuffer,&pt,sizeof(pt)))
253 printf("nand sim: tags corruption\n");
255 lseek(h,pos,SEEK_SET);
257 written = write(h,localBuffer,sizeof(pt));
258 if(written != sizeof(pt)) return YAFFS_FAIL;
267 int yaffs_CheckAllFF(const __u8 *ptr, int n)
272 if(*ptr!=0xFF) return 0;
279 static int fail300 = 1;
280 static int fail320 = 1;
282 static int failRead10 = 2;
284 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
290 int retval = YAFFS_OK;
293 T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
301 /* Got to suck the tags out of the data area */
304 data = yaffs_GetTempBuffer(dev,__LINE__);
308 yaffs_PackedTags2TagsPart * pt2tp;
309 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
312 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
313 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
315 lseek(h,pos,SEEK_SET);
317 nRead = read(h, data,dev->totalBytesPerChunk);
319 yaffs_UnpackTags2TagsPart(tags,pt2tp);
321 if(nread != dev->totalBytesPerChunk)
325 yaffs_ReleaseTempBuffer(dev,data,__LINE__);
336 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
337 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
338 lseek(h,pos,SEEK_SET);
339 nread = read(h,data,dev->nDataBytesPerChunk);
342 if(nread != dev->nDataBytesPerChunk)
348 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
349 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
350 lseek(h,pos,SEEK_SET);
352 if(0 && dev->isYaffs2)
354 nread= read(h,tags,sizeof(yaffs_ExtendedTags));
355 if(nread != sizeof(yaffs_ExtendedTags))
357 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
359 yaffs_InitialiseTags(tags);
368 yaffs_PackedTags2 pt;
369 nread= read(h,&pt,sizeof(pt));
370 yaffs_UnpackTags2(tags,&pt);
371 #ifdef SIMULATE_FAILURES
372 if((chunkInNAND >> 6) == 100) {
373 if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
374 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
379 if((chunkInNAND >> 6) == 110) {
380 if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
381 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
386 if(failRead10>0 && chunkInNAND == 10){
391 if(nread != sizeof(pt))
404 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
409 yaffs_PackedTags2 pt;
413 memset(&pt,0,sizeof(pt));
414 h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
415 lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
416 written = write(h,&pt,sizeof(pt));
418 if(written != sizeof(pt)) return YAFFS_FAIL;
425 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
433 printf("erase block %d\n",blockNumber);
435 if(blockNumber == 320)
438 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
440 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
453 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
454 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);
455 for(i = 0; i < dev->nChunksPerBlock; i++)
457 write(h,pg,PAGE_SIZE);
459 pos = lseek(h, 0,SEEK_CUR);
466 int yflash_InitialiseNAND(yaffs_Device *dev)
476 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, __u32 *sequenceNumber)
478 yaffs_ExtendedTags tags;
483 chunkNo = blockNo * dev->nChunksPerBlock;
485 yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
488 *state = YAFFS_BLOCK_STATE_DEAD;
490 else if(!tags.chunkUsed)
492 *state = YAFFS_BLOCK_STATE_EMPTY;
494 else if(tags.chunkUsed)
496 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
497 *sequenceNumber = tags.sequenceNumber;