2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 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.24 2010-02-18 01:18:04 charles Exp $";
23 #include "yaffs_trace.h"
25 #include "yaffs_flashif2.h"
26 #include "yaffs_guts.h"
27 #include "devextras.h"
29 #include <sys/types.h>
34 #include "yaffs_fileem2k.h"
35 #include "yaffs_packedtags2.h"
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 (32*8)
59 int handle[MAX_HANDLES];
63 static yflash_Device filedisk;
65 int yaffs_testPartialWrite = 0;
67 extern int random_seed;
68 extern int simulate_power_failure;
69 static int initialised = 0;
70 static int remaining_ops;
71 static int nops_so_far;
76 static void yflash2_MaybePowerFail(unsigned int chunkInNAND, int failPoint)
83 if(simulate_power_failure &&
85 printf("Simulated power failure after %d operations\n",nops_so_far);
86 printf(" power failed on chunkInNAND %d, at fail point %d\n",
87 chunkInNAND, failPoint);
96 static __u8 localBuffer[PAGE_SIZE];
98 static char *NToName(char *buf,int n)
100 sprintf(buf,"emfile-2k-%d",n);
104 static char dummyBuffer[BLOCK_SIZE];
106 static int GetBlockFileHandle(int n)
116 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
118 fSize = lseek(h,0,SEEK_END);
119 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
120 if(fSize < requiredSize){
121 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
122 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
132 static int CheckInit(void)
134 static int initialised = 0;
149 remaining_ops = (rand() % 1000) * 5;
150 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
154 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
156 for(i = 0; i < MAX_HANDLES; i++)
157 filedisk.handle[i] = -1;
159 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
160 filedisk.handle[i] = GetBlockFileHandle(i);
167 int yflash2_GetNumberOfBlocks(void)
171 return filedisk.nBlocks;
174 int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
183 T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
188 if(dev->param.inbandTags){
190 yaffs_PackedTags2TagsPart * pt2tp;
191 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
192 yaffs_PackTags2TagsPart(pt2tp,tags);
194 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
195 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
197 lseek(h,pos,SEEK_SET);
198 written = write(h,data,dev->param.totalBytesPerChunk);
201 if(yaffs_testPartialWrite){
206 if(written != dev->param.totalBytesPerChunk) return YAFFS_FAIL;
212 /* First do a write of a partial page */
218 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
219 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
222 memcpy(localBuffer,data, dev->nDataBytesPerChunk);
224 n_partials = rand()%20;
226 for(i = 0; i < n_partials; i++){
227 bpos = rand() % dev->nDataBytesPerChunk;
229 localBuffer[bpos] |= (1 << (rand() & 7));
232 if(REPORT_ERROR && memcmp(localBuffer,data,dev->nDataBytesPerChunk))
233 printf("nand simulator: data does not match\n");
235 lseek(h,pos,SEEK_SET);
236 written = write(h,localBuffer,dev->nDataBytesPerChunk);
238 if(yaffs_testPartialWrite){
244 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
246 // yflash2_MaybePowerFail(chunkInNAND,1);
250 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
251 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
253 lseek(h,pos,SEEK_SET);
255 if( 0 && dev->param.isYaffs2)
258 written = write(h,tags,sizeof(yaffs_ExtendedTags));
259 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
263 yaffs_PackedTags2 pt;
264 yaffs_PackTags2(&pt,tags, !dev->param.noTagsECC);
265 __u8 * ptab = (__u8 *)&pt;
267 nRead = read(h,localBuffer,sizeof(pt));
268 for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
269 if(localBuffer[i] != 0xFF){
270 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
271 chunkInNAND,i,localBuffer[i]);
276 for(i = 0; i < sizeof(pt); i++)
277 localBuffer[i] &= ptab[i];
279 n_partials = rand()% sizeof(pt);
281 for(i = 0; i < n_partials; i++){
282 bpos = rand() % sizeof(pt);
284 localBuffer[bpos] |= (1 << (rand() & 7));
287 if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
288 printf("nand sim: tags corruption\n");
290 lseek(h,pos,SEEK_SET);
292 written = write(h,localBuffer,sizeof(pt));
293 if(written != sizeof(pt)) return YAFFS_FAIL;
297 //yflash2_MaybePowerFail(chunkInNAND,2);
299 /* Next do the whole write */
302 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
303 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
306 memset(localBuffer,0xFF, PAGE_SIZE);
307 for(i = 0; i < dev->nDataBytesPerChunk; i++){
308 localBuffer[i] &= data[i];
311 if(REPORT_ERROR && memcmp(localBuffer,data,dev->nDataBytesPerChunk))
312 printf("nand simulator: data does not match\n");
314 lseek(h,pos,SEEK_SET);
315 written = write(h,localBuffer,dev->nDataBytesPerChunk);
317 if(yaffs_testPartialWrite){
323 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
328 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
329 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
331 lseek(h,pos,SEEK_SET);
333 if( 0 && dev->param.isYaffs2)
336 written = write(h,tags,sizeof(yaffs_ExtendedTags));
337 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
341 yaffs_PackedTags2 pt;
342 yaffs_PackTags2(&pt,tags,!dev->param.noTagsECC);
343 __u8 * ptab = (__u8 *)&pt;
345 nRead = read(h,localBuffer,sizeof(pt));
346 for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
347 if(localBuffer[i] != 0xFF){
348 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
349 chunkInNAND,i,localBuffer[i]);
354 for(i = 0; i < sizeof(pt); i++)
355 localBuffer[i] &= ptab[i];
357 if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
358 printf("nand sim: tags corruption\n");
360 lseek(h,pos,SEEK_SET);
362 written = write(h,localBuffer,sizeof(pt));
363 if(written != sizeof(pt)) return YAFFS_FAIL;
367 yflash2_MaybePowerFail(chunkInNAND,3);
374 int yaffs_CheckAllFF(const __u8 *ptr, int n)
379 if(*ptr!=0xFF) return 0;
386 static int fail300 = 1;
387 static int fail320 = 1;
389 static int failRead10 = 2;
391 int yflash2_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
397 int retval = YAFFS_OK;
400 T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
407 if(dev->param.inbandTags){
408 /* Got to suck the tags out of the data area */
411 data = yaffs_GetTempBuffer(dev,__LINE__);
415 yaffs_PackedTags2TagsPart * pt2tp;
416 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
419 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
420 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
422 lseek(h,pos,SEEK_SET);
424 nRead = read(h, data,dev->param.totalBytesPerChunk);
426 yaffs_UnpackTags2TagsPart(tags,pt2tp);
428 if(nread != dev->param.totalBytesPerChunk)
432 yaffs_ReleaseTempBuffer(dev,data,__LINE__);
443 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
444 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
445 lseek(h,pos,SEEK_SET);
446 nread = read(h,data,dev->nDataBytesPerChunk);
449 if(nread != dev->nDataBytesPerChunk)
455 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
456 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
457 lseek(h,pos,SEEK_SET);
459 if(0 && dev->param.isYaffs2)
461 nread= read(h,tags,sizeof(yaffs_ExtendedTags));
462 if(nread != sizeof(yaffs_ExtendedTags))
464 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
466 yaffs_InitialiseTags(tags);
475 yaffs_PackedTags2 pt;
476 nread= read(h,&pt,sizeof(pt));
477 yaffs_UnpackTags2(tags,&pt, !dev->param.noTagsECC);
478 #ifdef SIMULATE_FAILURES
479 if((chunkInNAND >> 6) == 100) {
480 if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
481 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
486 if((chunkInNAND >> 6) == 110) {
487 if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
488 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
493 if(failRead10>0 && chunkInNAND == 10){
498 if(nread != sizeof(pt))
511 int yflash2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
516 yaffs_PackedTags2 pt;
520 memset(&pt,0,sizeof(pt));
521 h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
522 lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->param.nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
523 written = write(h,&pt,sizeof(pt));
525 if(written != sizeof(pt)) return YAFFS_FAIL;
532 int yflash2_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
540 printf("erase block %d\n",blockNumber);
542 if(blockNumber == 320)
545 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
547 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
560 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
561 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->param.nChunksPerBlock) * PAGE_SIZE,SEEK_SET);
562 for(i = 0; i < dev->param.nChunksPerBlock; i++)
564 write(h,pg,PAGE_SIZE);
566 pos = lseek(h, 0,SEEK_CUR);
573 int yflash2_InitialiseNAND(yaffs_Device *dev)
583 int yflash2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, __u32 *sequenceNumber)
585 yaffs_ExtendedTags tags;
590 chunkNo = blockNo * dev->param.nChunksPerBlock;
592 yflash2_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
595 *state = YAFFS_BLOCK_STATE_DEAD;
597 else if(!tags.chunkUsed)
599 *state = YAFFS_BLOCK_STATE_EMPTY;
601 else if(tags.chunkUsed)
603 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
604 *sequenceNumber = tags.sequenceNumber;