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.18 2009-01-16 02:26:56 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"
38 #define REPORT_ERROR 0
42 __u8 data[PAGE_SIZE]; // Data + spare
47 yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
53 #define MAX_HANDLES 20
54 #define BLOCKS_PER_HANDLE (32*8)
58 int handle[MAX_HANDLES];
62 static yflash_Device filedisk;
64 int yaffs_testPartialWrite = 0;
66 extern int random_seed;
67 extern int simulate_power_failure;
68 static int initialised = 0;
69 static int remaining_ops;
70 static int nops_so_far;
75 static void yflash2_MaybePowerFail(void)
82 if(simulate_power_failure &&
84 printf("Simulated power failure after %d operations\n",nops_so_far);
93 static __u8 localBuffer[PAGE_SIZE];
95 static char *NToName(char *buf,int n)
97 sprintf(buf,"emfile-2k-%d",n);
101 static char dummyBuffer[BLOCK_SIZE];
103 static int GetBlockFileHandle(int n)
113 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
115 fSize = lseek(h,0,SEEK_END);
116 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
117 if(fSize < requiredSize){
118 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
119 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
129 static int CheckInit(void)
131 static int initialised = 0;
146 remaining_ops = (rand() % 1000) * 5;
147 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
151 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
153 for(i = 0; i < MAX_HANDLES; i++)
154 filedisk.handle[i] = -1;
156 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
157 filedisk.handle[i] = GetBlockFileHandle(i);
164 int yflash2_GetNumberOfBlocks(void)
168 return filedisk.nBlocks;
171 int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
180 T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
187 yaffs_PackedTags2TagsPart * pt2tp;
188 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
189 yaffs_PackTags2TagsPart(pt2tp,tags);
191 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
192 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
194 lseek(h,pos,SEEK_SET);
195 written = write(h,data,dev->totalBytesPerChunk);
198 if(yaffs_testPartialWrite){
203 if(written != dev->totalBytesPerChunk) return YAFFS_FAIL;
209 /* First do a write of a partial page */
215 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
216 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
219 memcpy(localBuffer,data, dev->nDataBytesPerChunk);
221 n_partials = rand()%20;
223 for(i = 0; i < n_partials; i++){
224 bpos = rand() % dev->nDataBytesPerChunk;
226 localBuffer[bpos] |= (1 << (rand() & 7));
229 if(REPORT_ERROR && memcmp(localBuffer,data,dev->nDataBytesPerChunk))
230 printf("nand simulator: data does not match\n");
232 lseek(h,pos,SEEK_SET);
233 written = write(h,localBuffer,dev->nDataBytesPerChunk);
235 if(yaffs_testPartialWrite){
241 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
243 yflash2_MaybePowerFail();
247 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
248 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
250 lseek(h,pos,SEEK_SET);
252 if( 0 && dev->isYaffs2)
255 written = write(h,tags,sizeof(yaffs_ExtendedTags));
256 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
260 yaffs_PackedTags2 pt;
261 yaffs_PackTags2(&pt,tags);
262 __u8 * ptab = (__u8 *)&pt;
264 nRead = read(h,localBuffer,sizeof(pt));
265 for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
266 if(localBuffer[i] != 0xFF){
267 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
268 chunkInNAND,i,localBuffer[i]);
273 for(i = 0; i < sizeof(pt); i++)
274 localBuffer[i] &= ptab[i];
276 n_partials = rand()% sizeof(pt);
278 for(i = 0; i < n_partials; i++){
279 bpos = rand() % sizeof(pt);
281 localBuffer[bpos] |= (1 << (rand() & 7));
284 if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
285 printf("nand sim: tags corruption\n");
287 lseek(h,pos,SEEK_SET);
289 written = write(h,localBuffer,sizeof(pt));
290 if(written != sizeof(pt)) return YAFFS_FAIL;
294 //yflash2_MaybePowerFail();
296 /* Next do the whole write */
299 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
300 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
303 memset(localBuffer,0xFF, PAGE_SIZE);
304 for(i = 0; i < dev->nDataBytesPerChunk; i++){
305 localBuffer[i] &= data[i];
308 if(REPORT_ERROR && memcmp(localBuffer,data,dev->nDataBytesPerChunk))
309 printf("nand simulator: data does not match\n");
311 lseek(h,pos,SEEK_SET);
312 written = write(h,localBuffer,dev->nDataBytesPerChunk);
314 if(yaffs_testPartialWrite){
320 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
325 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
326 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
328 lseek(h,pos,SEEK_SET);
330 if( 0 && dev->isYaffs2)
333 written = write(h,tags,sizeof(yaffs_ExtendedTags));
334 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
338 yaffs_PackedTags2 pt;
339 yaffs_PackTags2(&pt,tags);
340 __u8 * ptab = (__u8 *)&pt;
342 nRead = read(h,localBuffer,sizeof(pt));
343 for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
344 if(localBuffer[i] != 0xFF){
345 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
346 chunkInNAND,i,localBuffer[i]);
351 for(i = 0; i < sizeof(pt); i++)
352 localBuffer[i] &= ptab[i];
354 if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
355 printf("nand sim: tags corruption\n");
357 lseek(h,pos,SEEK_SET);
359 written = write(h,localBuffer,sizeof(pt));
360 if(written != sizeof(pt)) return YAFFS_FAIL;
364 yflash2_MaybePowerFail();
371 int yaffs_CheckAllFF(const __u8 *ptr, int n)
376 if(*ptr!=0xFF) return 0;
383 static int fail300 = 1;
384 static int fail320 = 1;
386 static int failRead10 = 2;
388 int yflash2_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
394 int retval = YAFFS_OK;
397 T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
405 /* Got to suck the tags out of the data area */
408 data = yaffs_GetTempBuffer(dev,__LINE__);
412 yaffs_PackedTags2TagsPart * pt2tp;
413 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
416 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
417 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
419 lseek(h,pos,SEEK_SET);
421 nRead = read(h, data,dev->totalBytesPerChunk);
423 yaffs_UnpackTags2TagsPart(tags,pt2tp);
425 if(nread != dev->totalBytesPerChunk)
429 yaffs_ReleaseTempBuffer(dev,data,__LINE__);
440 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
441 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
442 lseek(h,pos,SEEK_SET);
443 nread = read(h,data,dev->nDataBytesPerChunk);
446 if(nread != dev->nDataBytesPerChunk)
452 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
453 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
454 lseek(h,pos,SEEK_SET);
456 if(0 && dev->isYaffs2)
458 nread= read(h,tags,sizeof(yaffs_ExtendedTags));
459 if(nread != sizeof(yaffs_ExtendedTags))
461 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
463 yaffs_InitialiseTags(tags);
472 yaffs_PackedTags2 pt;
473 nread= read(h,&pt,sizeof(pt));
474 yaffs_UnpackTags2(tags,&pt);
475 #ifdef SIMULATE_FAILURES
476 if((chunkInNAND >> 6) == 100) {
477 if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
478 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
483 if((chunkInNAND >> 6) == 110) {
484 if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
485 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
490 if(failRead10>0 && chunkInNAND == 10){
495 if(nread != sizeof(pt))
508 int yflash2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
513 yaffs_PackedTags2 pt;
517 memset(&pt,0,sizeof(pt));
518 h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
519 lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
520 written = write(h,&pt,sizeof(pt));
522 if(written != sizeof(pt)) return YAFFS_FAIL;
529 int yflash2_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
537 printf("erase block %d\n",blockNumber);
539 if(blockNumber == 320)
542 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
544 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
557 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
558 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);
559 for(i = 0; i < dev->nChunksPerBlock; i++)
561 write(h,pg,PAGE_SIZE);
563 pos = lseek(h, 0,SEEK_CUR);
570 int yflash2_InitialiseNAND(yaffs_Device *dev)
580 int yflash2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, __u32 *sequenceNumber)
582 yaffs_ExtendedTags tags;
587 chunkNo = blockNo * dev->nChunksPerBlock;
589 yflash2_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
592 *state = YAFFS_BLOCK_STATE_DEAD;
594 else if(!tags.chunkUsed)
596 *state = YAFFS_BLOCK_STATE_EMPTY;
598 else if(tags.chunkUsed)
600 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
601 *sequenceNumber = tags.sequenceNumber;