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_flashif2_c_version = "$Id: yaffs_fileem2k.c,v 1.16 2009-01-12 00:49:01 charles Exp $";
24 #include "yaffs_flashif2.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
39 #define REPORT_ERROR 0
43 __u8 data[PAGE_SIZE]; // Data + spare
48 yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
54 #define MAX_HANDLES 20
55 #define BLOCKS_PER_HANDLE 8000
59 int handle[MAX_HANDLES];
63 static yflash_Device filedisk;
65 int yaffs_testPartialWrite = 0;
70 static __u8 localBuffer[PAGE_SIZE];
72 static char *NToName(char *buf,int n)
74 sprintf(buf,"emfile-2k-%d",n);
78 static char dummyBuffer[BLOCK_SIZE];
80 static int GetBlockFileHandle(int n)
90 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
92 fSize = lseek(h,0,SEEK_END);
93 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
94 if(fSize < requiredSize){
95 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
96 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
106 static int CheckInit(void)
108 static int initialised = 0;
121 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
124 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
126 for(i = 0; i < MAX_HANDLES; i++)
127 filedisk.handle[i] = -1;
129 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
130 filedisk.handle[i] = GetBlockFileHandle(i);
137 int yflash2_GetNumberOfBlocks(void)
141 return filedisk.nBlocks;
144 int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
153 T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
160 yaffs_PackedTags2TagsPart * pt2tp;
161 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
162 yaffs_PackTags2TagsPart(pt2tp,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 written = write(h,data,dev->totalBytesPerChunk);
171 if(yaffs_testPartialWrite){
176 if(written != dev->totalBytesPerChunk) return YAFFS_FAIL;
185 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
186 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
188 lseek(h,pos,SEEK_SET);
189 nRead = read(h, localBuffer,dev->nDataBytesPerChunk);
190 for(i = error = 0; i < dev->nDataBytesPerChunk && !error; i++){
191 if(REPORT_ERROR && localBuffer[i] != 0xFF){
192 printf("nand simulation: chunk %d data byte %d was %0x2\n",
193 chunkInNAND,i,localBuffer[i]);
198 for(i = 0; i < dev->nDataBytesPerChunk; i++)
199 localBuffer[i] &= data[i];
201 if(REPORT_ERROR && memcmp(localBuffer,data,dev->nDataBytesPerChunk))
202 printf("nand simulator: data does not match\n");
204 lseek(h,pos,SEEK_SET);
205 written = write(h,localBuffer,dev->nDataBytesPerChunk);
207 if(yaffs_testPartialWrite){
212 #ifdef SIMULATE_FAILURES
213 if((chunkInNAND >> 6) == 100)
216 if((chunkInNAND >> 6) == 110)
221 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
226 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
227 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
229 lseek(h,pos,SEEK_SET);
231 if( 0 && dev->isYaffs2)
234 written = write(h,tags,sizeof(yaffs_ExtendedTags));
235 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
239 yaffs_PackedTags2 pt;
240 yaffs_PackTags2(&pt,tags);
241 __u8 * ptab = (__u8 *)&pt;
243 nRead = read(h,localBuffer,sizeof(pt));
244 for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
245 if(localBuffer[i] != 0xFF){
246 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
247 chunkInNAND,i,localBuffer[i]);
252 for(i = 0; i < sizeof(pt); i++)
253 localBuffer[i] &= ptab[i];
255 if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
256 printf("nand sim: tags corruption\n");
258 lseek(h,pos,SEEK_SET);
260 written = write(h,localBuffer,sizeof(pt));
261 if(written != sizeof(pt)) return YAFFS_FAIL;
270 int yaffs_CheckAllFF(const __u8 *ptr, int n)
275 if(*ptr!=0xFF) return 0;
282 static int fail300 = 1;
283 static int fail320 = 1;
285 static int failRead10 = 2;
287 int yflash2_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
293 int retval = YAFFS_OK;
296 T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
304 /* Got to suck the tags out of the data area */
307 data = yaffs_GetTempBuffer(dev,__LINE__);
311 yaffs_PackedTags2TagsPart * pt2tp;
312 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
315 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
316 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
318 lseek(h,pos,SEEK_SET);
320 nRead = read(h, data,dev->totalBytesPerChunk);
322 yaffs_UnpackTags2TagsPart(tags,pt2tp);
324 if(nread != dev->totalBytesPerChunk)
328 yaffs_ReleaseTempBuffer(dev,data,__LINE__);
339 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
340 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
341 lseek(h,pos,SEEK_SET);
342 nread = read(h,data,dev->nDataBytesPerChunk);
345 if(nread != dev->nDataBytesPerChunk)
351 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
352 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
353 lseek(h,pos,SEEK_SET);
355 if(0 && dev->isYaffs2)
357 nread= read(h,tags,sizeof(yaffs_ExtendedTags));
358 if(nread != sizeof(yaffs_ExtendedTags))
360 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
362 yaffs_InitialiseTags(tags);
371 yaffs_PackedTags2 pt;
372 nread= read(h,&pt,sizeof(pt));
373 yaffs_UnpackTags2(tags,&pt);
374 #ifdef SIMULATE_FAILURES
375 if((chunkInNAND >> 6) == 100) {
376 if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
377 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
382 if((chunkInNAND >> 6) == 110) {
383 if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
384 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
389 if(failRead10>0 && chunkInNAND == 10){
394 if(nread != sizeof(pt))
407 int yflash2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
412 yaffs_PackedTags2 pt;
416 memset(&pt,0,sizeof(pt));
417 h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
418 lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
419 written = write(h,&pt,sizeof(pt));
421 if(written != sizeof(pt)) return YAFFS_FAIL;
428 int yflash2_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
436 printf("erase block %d\n",blockNumber);
438 if(blockNumber == 320)
441 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
443 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
456 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
457 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);
458 for(i = 0; i < dev->nChunksPerBlock; i++)
460 write(h,pg,PAGE_SIZE);
462 pos = lseek(h, 0,SEEK_CUR);
469 int yflash2_InitialiseNAND(yaffs_Device *dev)
479 int yflash2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, __u32 *sequenceNumber)
481 yaffs_ExtendedTags tags;
486 chunkNo = blockNo * dev->nChunksPerBlock;
488 yflash2_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
491 *state = YAFFS_BLOCK_STATE_DEAD;
493 else if(!tags.chunkUsed)
495 *state = YAFFS_BLOCK_STATE_EMPTY;
497 else if(tags.chunkUsed)
499 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
500 *sequenceNumber = tags.sequenceNumber;