2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
3 * yaffs_ramdisk.c: yaffs ram disk component
5 * Copyright (C) 2002 Aleph One Ltd.
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.
18 const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.10 2006-11-13 07:16:37 charles Exp $";
23 #include "yaffs_flashif.h"
24 #include "yaffs_guts.h"
25 #include "devextras.h"
27 #include <sys/types.h>
32 #include "yaffs_fileem2k.h"
33 #include "yaffs_packedtags2.h"
35 //#define SIMULATE_FAILURES
39 __u8 data[PAGE_SIZE]; // Data + spare
44 yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
50 #define MAX_HANDLES 20
51 #define BLOCKS_PER_HANDLE 8000
55 int handle[MAX_HANDLES];
59 static yflash_Device filedisk;
61 int yaffs_testPartialWrite = 0;
66 static __u8 localBuffer[PAGE_SIZE];
68 static char *NToName(char *buf,int n)
70 sprintf(buf,"emfile%d",n);
74 static char dummyBuffer[BLOCK_SIZE];
76 static int GetBlockFileHandle(int n)
86 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
88 fSize = lseek(h,0,SEEK_END);
89 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
90 if(fSize < requiredSize){
91 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
92 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
102 static int CheckInit(void)
104 static int initialised = 0;
123 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
126 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
128 for(i = 0; i < MAX_HANDLES; i++)
129 filedisk.handle[i] = -1;
131 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
132 filedisk.handle[i] = GetBlockFileHandle(i);
139 int yflash_GetNumberOfBlocks(void)
143 return filedisk.nBlocks;
146 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
155 T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
163 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
164 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
166 lseek(h,pos,SEEK_SET);
167 nRead = read(h, localBuffer,dev->nDataBytesPerChunk);
168 for(i = error = 0; i < dev->nDataBytesPerChunk && !error; i++){
169 if(localBuffer[i] != 0xFF){
170 printf("nand simulation: chunk %d data byte %d was %0x2\n",
171 chunkInNAND,i,localBuffer[i]);
176 for(i = 0; i < dev->nDataBytesPerChunk; i++)
177 localBuffer[i] &= data[i];
179 if(memcmp(localBuffer,data,dev->nDataBytesPerChunk))
180 printf("nand simulator: data does not match\n");
182 lseek(h,pos,SEEK_SET);
183 written = write(h,localBuffer,dev->nDataBytesPerChunk);
185 if(yaffs_testPartialWrite){
190 #ifdef SIMULATE_FAILURES
191 if((chunkInNAND >> 6) == 100)
194 if((chunkInNAND >> 6) == 110)
199 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
204 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
205 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
207 lseek(h,pos,SEEK_SET);
209 if( 0 && dev->isYaffs2)
212 written = write(h,tags,sizeof(yaffs_ExtendedTags));
213 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
217 yaffs_PackedTags2 pt;
218 yaffs_PackTags2(&pt,tags);
219 __u8 * ptab = (__u8 *)&pt;
221 nRead = read(h,localBuffer,sizeof(pt));
222 for(i = error = 0; i < sizeof(pt) && !error; i++){
223 if(localBuffer[i] != 0xFF){
224 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
225 chunkInNAND,i,localBuffer[i]);
230 for(i = 0; i < sizeof(pt); i++)
231 localBuffer[i] &= ptab[i];
233 if(memcmp(localBuffer,&pt,sizeof(pt)))
234 printf("nand sim: tags corruption\n");
236 lseek(h,pos,SEEK_SET);
238 written = write(h,localBuffer,sizeof(pt));
239 if(written != sizeof(pt)) return YAFFS_FAIL;
248 int yaffs_CheckAllFF(const __u8 *ptr, int n)
253 if(*ptr!=0xFF) return 0;
260 static int fail300 = 1;
261 static int fail320 = 1;
263 static int failRead10 = 2;
265 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
271 T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
280 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
281 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
282 lseek(h,pos,SEEK_SET);
283 nread = read(h,data,dev->nDataBytesPerChunk);
286 if(nread != dev->nDataBytesPerChunk) return YAFFS_FAIL;
291 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
292 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
293 lseek(h,pos,SEEK_SET);
295 if(0 && dev->isYaffs2)
297 nread= read(h,tags,sizeof(yaffs_ExtendedTags));
298 if(nread != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
299 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
301 yaffs_InitialiseTags(tags);
310 yaffs_PackedTags2 pt;
311 nread= read(h,&pt,sizeof(pt));
312 yaffs_UnpackTags2(tags,&pt);
313 #ifdef SIMULATE_FAILURES
314 if((chunkInNAND >> 6) == 100) {
315 if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
316 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
321 if((chunkInNAND >> 6) == 110) {
322 if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
323 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
328 if(failRead10>0 && chunkInNAND == 10){
333 if(nread != sizeof(pt)) return YAFFS_FAIL;
343 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
348 yaffs_PackedTags2 pt;
352 memset(&pt,0,sizeof(pt));
353 h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
354 lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
355 written = write(h,&pt,sizeof(pt));
357 if(written != sizeof(pt)) return YAFFS_FAIL;
364 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
372 printf("erase block %d\n",blockNumber);
374 if(blockNumber == 320)
377 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
379 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
392 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
393 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);
394 for(i = 0; i < dev->nChunksPerBlock; i++)
396 write(h,pg,PAGE_SIZE);
398 pos = lseek(h, 0,SEEK_CUR);
405 int yflash_InitialiseNAND(yaffs_Device *dev)
415 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
417 yaffs_ExtendedTags tags;
422 chunkNo = blockNo * dev->nChunksPerBlock;
424 yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
427 *state = YAFFS_BLOCK_STATE_DEAD;
429 else if(!tags.chunkUsed)
431 *state = YAFFS_BLOCK_STATE_EMPTY;
433 else if(tags.chunkUsed)
435 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
436 *sequenceNumber = tags.sequenceNumber;