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.
16 * This provides a YAFFS nand emulation on a file for emulating 2kB pages.
17 * This is only intended as test code to test persistence etc.
20 const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.11 2007-02-12 16:49:50 wookey Exp $";
25 #include "yaffs_flashif.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"
37 //#define SIMULATE_FAILURES
41 __u8 data[PAGE_SIZE]; // Data + spare
46 yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
52 #define MAX_HANDLES 20
53 #define BLOCKS_PER_HANDLE 8000
57 int handle[MAX_HANDLES];
61 static yflash_Device filedisk;
63 int yaffs_testPartialWrite = 0;
68 static __u8 localBuffer[PAGE_SIZE];
70 static char *NToName(char *buf,int n)
72 sprintf(buf,"emfile%d",n);
76 static char dummyBuffer[BLOCK_SIZE];
78 static int GetBlockFileHandle(int n)
88 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
90 fSize = lseek(h,0,SEEK_END);
91 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
92 if(fSize < requiredSize){
93 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
94 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
104 static int CheckInit(void)
106 static int initialised = 0;
125 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
128 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
130 for(i = 0; i < MAX_HANDLES; i++)
131 filedisk.handle[i] = -1;
133 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
134 filedisk.handle[i] = GetBlockFileHandle(i);
141 int yflash_GetNumberOfBlocks(void)
145 return filedisk.nBlocks;
148 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
157 T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
165 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
166 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
168 lseek(h,pos,SEEK_SET);
169 nRead = read(h, localBuffer,dev->nDataBytesPerChunk);
170 for(i = error = 0; i < dev->nDataBytesPerChunk && !error; i++){
171 if(localBuffer[i] != 0xFF){
172 printf("nand simulation: chunk %d data byte %d was %0x2\n",
173 chunkInNAND,i,localBuffer[i]);
178 for(i = 0; i < dev->nDataBytesPerChunk; i++)
179 localBuffer[i] &= data[i];
181 if(memcmp(localBuffer,data,dev->nDataBytesPerChunk))
182 printf("nand simulator: data does not match\n");
184 lseek(h,pos,SEEK_SET);
185 written = write(h,localBuffer,dev->nDataBytesPerChunk);
187 if(yaffs_testPartialWrite){
192 #ifdef SIMULATE_FAILURES
193 if((chunkInNAND >> 6) == 100)
196 if((chunkInNAND >> 6) == 110)
201 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
206 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
207 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
209 lseek(h,pos,SEEK_SET);
211 if( 0 && dev->isYaffs2)
214 written = write(h,tags,sizeof(yaffs_ExtendedTags));
215 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
219 yaffs_PackedTags2 pt;
220 yaffs_PackTags2(&pt,tags);
221 __u8 * ptab = (__u8 *)&pt;
223 nRead = read(h,localBuffer,sizeof(pt));
224 for(i = error = 0; i < sizeof(pt) && !error; i++){
225 if(localBuffer[i] != 0xFF){
226 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
227 chunkInNAND,i,localBuffer[i]);
232 for(i = 0; i < sizeof(pt); i++)
233 localBuffer[i] &= ptab[i];
235 if(memcmp(localBuffer,&pt,sizeof(pt)))
236 printf("nand sim: tags corruption\n");
238 lseek(h,pos,SEEK_SET);
240 written = write(h,localBuffer,sizeof(pt));
241 if(written != sizeof(pt)) return YAFFS_FAIL;
250 int yaffs_CheckAllFF(const __u8 *ptr, int n)
255 if(*ptr!=0xFF) return 0;
262 static int fail300 = 1;
263 static int fail320 = 1;
265 static int failRead10 = 2;
267 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
273 T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
282 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
283 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
284 lseek(h,pos,SEEK_SET);
285 nread = read(h,data,dev->nDataBytesPerChunk);
288 if(nread != dev->nDataBytesPerChunk) return YAFFS_FAIL;
293 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
294 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
295 lseek(h,pos,SEEK_SET);
297 if(0 && dev->isYaffs2)
299 nread= read(h,tags,sizeof(yaffs_ExtendedTags));
300 if(nread != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
301 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
303 yaffs_InitialiseTags(tags);
312 yaffs_PackedTags2 pt;
313 nread= read(h,&pt,sizeof(pt));
314 yaffs_UnpackTags2(tags,&pt);
315 #ifdef SIMULATE_FAILURES
316 if((chunkInNAND >> 6) == 100) {
317 if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
318 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
323 if((chunkInNAND >> 6) == 110) {
324 if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
325 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
330 if(failRead10>0 && chunkInNAND == 10){
335 if(nread != sizeof(pt)) return YAFFS_FAIL;
345 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
350 yaffs_PackedTags2 pt;
354 memset(&pt,0,sizeof(pt));
355 h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
356 lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
357 written = write(h,&pt,sizeof(pt));
359 if(written != sizeof(pt)) return YAFFS_FAIL;
366 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
374 printf("erase block %d\n",blockNumber);
376 if(blockNumber == 320)
379 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
381 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
394 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
395 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);
396 for(i = 0; i < dev->nChunksPerBlock; i++)
398 write(h,pg,PAGE_SIZE);
400 pos = lseek(h, 0,SEEK_CUR);
407 int yflash_InitialiseNAND(yaffs_Device *dev)
417 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
419 yaffs_ExtendedTags tags;
424 chunkNo = blockNo * dev->nChunksPerBlock;
426 yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
429 *state = YAFFS_BLOCK_STATE_DEAD;
431 else if(!tags.chunkUsed)
433 *state = YAFFS_BLOCK_STATE_EMPTY;
435 else if(tags.chunkUsed)
437 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
438 *sequenceNumber = tags.sequenceNumber;