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.21 2009-11-07 22:35:15 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(unsigned int chunkInNAND, int failPoint)
82 if(simulate_power_failure &&
84 printf("Simulated power failure after %d operations\n",nops_so_far);
85 printf(" power failed on chunkInNAND %d, at fail point %d\n",
86 chunkInNAND, failPoint);
95 static __u8 localBuffer[PAGE_SIZE];
97 static char *NToName(char *buf,int n)
99 sprintf(buf,"emfile-2k-%d",n);
103 static char dummyBuffer[BLOCK_SIZE];
105 static int GetBlockFileHandle(int n)
115 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
117 fSize = lseek(h,0,SEEK_END);
118 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
119 if(fSize < requiredSize){
120 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
121 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
131 static int CheckInit(void)
133 static int initialised = 0;
148 remaining_ops = (rand() % 1000) * 5;
149 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
153 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
155 for(i = 0; i < MAX_HANDLES; i++)
156 filedisk.handle[i] = -1;
158 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
159 filedisk.handle[i] = GetBlockFileHandle(i);
166 int yflash2_GetNumberOfBlocks(void)
170 return filedisk.nBlocks;
173 int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
182 T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
189 yaffs_PackedTags2TagsPart * pt2tp;
190 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
191 yaffs_PackTags2TagsPart(pt2tp,tags);
193 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
194 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
196 lseek(h,pos,SEEK_SET);
197 written = write(h,data,dev->totalBytesPerChunk);
200 if(yaffs_testPartialWrite){
205 if(written != dev->totalBytesPerChunk) return YAFFS_FAIL;
211 /* First do a write of a partial page */
217 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
218 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
221 memcpy(localBuffer,data, dev->nDataBytesPerChunk);
223 n_partials = rand()%20;
225 for(i = 0; i < n_partials; i++){
226 bpos = rand() % dev->nDataBytesPerChunk;
228 localBuffer[bpos] |= (1 << (rand() & 7));
231 if(REPORT_ERROR && memcmp(localBuffer,data,dev->nDataBytesPerChunk))
232 printf("nand simulator: data does not match\n");
234 lseek(h,pos,SEEK_SET);
235 written = write(h,localBuffer,dev->nDataBytesPerChunk);
237 if(yaffs_testPartialWrite){
243 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
245 // yflash2_MaybePowerFail(chunkInNAND,1);
249 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
250 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
252 lseek(h,pos,SEEK_SET);
254 if( 0 && dev->isYaffs2)
257 written = write(h,tags,sizeof(yaffs_ExtendedTags));
258 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
262 yaffs_PackedTags2 pt;
263 yaffs_PackTags2(dev,&pt,tags);
264 __u8 * ptab = (__u8 *)&pt;
266 nRead = read(h,localBuffer,sizeof(pt));
267 for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
268 if(localBuffer[i] != 0xFF){
269 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
270 chunkInNAND,i,localBuffer[i]);
275 for(i = 0; i < sizeof(pt); i++)
276 localBuffer[i] &= ptab[i];
278 n_partials = rand()% sizeof(pt);
280 for(i = 0; i < n_partials; i++){
281 bpos = rand() % sizeof(pt);
283 localBuffer[bpos] |= (1 << (rand() & 7));
286 if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
287 printf("nand sim: tags corruption\n");
289 lseek(h,pos,SEEK_SET);
291 written = write(h,localBuffer,sizeof(pt));
292 if(written != sizeof(pt)) return YAFFS_FAIL;
296 //yflash2_MaybePowerFail(chunkInNAND,2);
298 /* Next do the whole write */
301 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
302 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
305 memset(localBuffer,0xFF, PAGE_SIZE);
306 for(i = 0; i < dev->nDataBytesPerChunk; i++){
307 localBuffer[i] &= data[i];
310 if(REPORT_ERROR && memcmp(localBuffer,data,dev->nDataBytesPerChunk))
311 printf("nand simulator: data does not match\n");
313 lseek(h,pos,SEEK_SET);
314 written = write(h,localBuffer,dev->nDataBytesPerChunk);
316 if(yaffs_testPartialWrite){
322 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
327 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
328 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
330 lseek(h,pos,SEEK_SET);
332 if( 0 && dev->isYaffs2)
335 written = write(h,tags,sizeof(yaffs_ExtendedTags));
336 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
340 yaffs_PackedTags2 pt;
341 yaffs_PackTags2(dev,&pt,tags);
342 __u8 * ptab = (__u8 *)&pt;
344 nRead = read(h,localBuffer,sizeof(pt));
345 for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
346 if(localBuffer[i] != 0xFF){
347 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
348 chunkInNAND,i,localBuffer[i]);
353 for(i = 0; i < sizeof(pt); i++)
354 localBuffer[i] &= ptab[i];
356 if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
357 printf("nand sim: tags corruption\n");
359 lseek(h,pos,SEEK_SET);
361 written = write(h,localBuffer,sizeof(pt));
362 if(written != sizeof(pt)) return YAFFS_FAIL;
366 yflash2_MaybePowerFail(chunkInNAND,3);
373 int yaffs_CheckAllFF(const __u8 *ptr, int n)
378 if(*ptr!=0xFF) return 0;
385 static int fail300 = 1;
386 static int fail320 = 1;
388 static int failRead10 = 2;
390 int yflash2_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
396 int retval = YAFFS_OK;
399 T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
407 /* Got to suck the tags out of the data area */
410 data = yaffs_GetTempBuffer(dev,__LINE__);
414 yaffs_PackedTags2TagsPart * pt2tp;
415 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
418 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
419 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
421 lseek(h,pos,SEEK_SET);
423 nRead = read(h, data,dev->totalBytesPerChunk);
425 yaffs_UnpackTags2TagsPart(tags,pt2tp);
427 if(nread != dev->totalBytesPerChunk)
431 yaffs_ReleaseTempBuffer(dev,data,__LINE__);
442 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
443 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
444 lseek(h,pos,SEEK_SET);
445 nread = read(h,data,dev->nDataBytesPerChunk);
448 if(nread != dev->nDataBytesPerChunk)
454 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
455 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
456 lseek(h,pos,SEEK_SET);
458 if(0 && dev->isYaffs2)
460 nread= read(h,tags,sizeof(yaffs_ExtendedTags));
461 if(nread != sizeof(yaffs_ExtendedTags))
463 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
465 yaffs_InitialiseTags(tags);
474 yaffs_PackedTags2 pt;
475 nread= read(h,&pt,sizeof(pt));
476 yaffs_UnpackTags2(dev,tags,&pt);
477 #ifdef SIMULATE_FAILURES
478 if((chunkInNAND >> 6) == 100) {
479 if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
480 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
485 if((chunkInNAND >> 6) == 110) {
486 if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
487 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
492 if(failRead10>0 && chunkInNAND == 10){
497 if(nread != sizeof(pt))
510 int yflash2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
515 yaffs_PackedTags2 pt;
519 memset(&pt,0,sizeof(pt));
520 h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
521 lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
522 written = write(h,&pt,sizeof(pt));
524 if(written != sizeof(pt)) return YAFFS_FAIL;
531 int yflash2_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
539 printf("erase block %d\n",blockNumber);
541 if(blockNumber == 320)
544 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
546 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
559 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
560 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);
561 for(i = 0; i < dev->nChunksPerBlock; i++)
563 write(h,pg,PAGE_SIZE);
565 pos = lseek(h, 0,SEEK_CUR);
572 int yflash2_InitialiseNAND(yaffs_Device *dev)
582 int yflash2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, __u32 *sequenceNumber)
584 yaffs_ExtendedTags tags;
589 chunkNo = blockNo * dev->nChunksPerBlock;
591 yflash2_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
594 *state = YAFFS_BLOCK_STATE_DEAD;
596 else if(!tags.chunkUsed)
598 *state = YAFFS_BLOCK_STATE_EMPTY;
600 else if(tags.chunkUsed)
602 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
603 *sequenceNumber = tags.sequenceNumber;