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.7 2006-10-13 08:52:49 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"
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;
64 static char *NToName(char *buf,int n)
66 sprintf(buf,"emfile%d",n);
70 static char dummyBuffer[BLOCK_SIZE];
72 static int GetBlockFileHandle(int n)
82 h = open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
84 fSize = lseek(h,0,SEEK_END);
85 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
86 if(fSize < requiredSize){
87 for(i = 0; i < BLOCKS_PER_HANDLE; i++)
88 if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
98 static int CheckInit(void)
100 static int initialised = 0;
119 memset(dummyBuffer,0xff,sizeof(dummyBuffer));
122 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
124 for(i = 0; i < MAX_HANDLES; i++)
125 filedisk.handle[i] = -1;
127 for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
128 filedisk.handle[i] = GetBlockFileHandle(i);
135 int yflash_GetNumberOfBlocks(void)
139 return filedisk.nBlocks;
142 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
154 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
155 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
157 lseek(h,pos,SEEK_SET);
158 written = write(h,data,dev->nDataBytesPerChunk);
160 if(yaffs_testPartialWrite){
165 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
170 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
171 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
173 lseek(h,pos,SEEK_SET);
174 if( 0 && dev->isYaffs2)
177 written = write(h,tags,sizeof(yaffs_ExtendedTags));
178 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
182 yaffs_PackedTags2 pt;
183 yaffs_PackTags2(&pt,tags);
185 written = write(h,&pt,sizeof(pt));
186 if(written != sizeof(pt)) return YAFFS_FAIL;
195 int yaffs_CheckAllFF(const __u8 *ptr, int n)
200 if(*ptr!=0xFF) return 0;
207 static int fail300 = 1;
208 static int fail320 = 1;
210 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
223 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
224 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
225 lseek(h,pos,SEEK_SET);
226 nread = read(h,data,dev->nDataBytesPerChunk);
228 if(nread != dev->nDataBytesPerChunk) return YAFFS_FAIL;
233 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
234 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
235 lseek(h,pos,SEEK_SET);
237 if(0 && dev->isYaffs2)
239 nread= read(h,tags,sizeof(yaffs_ExtendedTags));
240 if(nread != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
241 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
243 yaffs_InitialiseTags(tags);
252 yaffs_PackedTags2 pt;
253 nread= read(h,&pt,sizeof(pt));
254 yaffs_UnpackTags2(tags,&pt);
255 if((chunkInNAND >> 6) == 300) {
256 if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
257 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
262 if((chunkInNAND >> 6) == 320) {
263 if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
264 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
268 if(nread != sizeof(pt)) return YAFFS_FAIL;
278 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
283 yaffs_PackedTags2 pt;
287 memset(&pt,0,sizeof(pt));
288 h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
289 lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
290 written = write(h,&pt,sizeof(pt));
292 if(written != sizeof(pt)) return YAFFS_FAIL;
299 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
307 if(blockNumber == 320)
310 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
312 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
325 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
326 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);
327 for(i = 0; i < dev->nChunksPerBlock; i++)
329 write(h,pg,PAGE_SIZE);
331 pos = lseek(h, 0,SEEK_CUR);
338 int yflash_InitialiseNAND(yaffs_Device *dev)
348 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
350 yaffs_ExtendedTags tags;
355 chunkNo = blockNo * dev->nChunksPerBlock;
357 yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
360 *state = YAFFS_BLOCK_STATE_DEAD;
362 else if(!tags.chunkUsed)
364 *state = YAFFS_BLOCK_STATE_EMPTY;
366 else if(tags.chunkUsed)
368 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
369 *sequenceNumber = tags.sequenceNumber;