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.1 2004-11-03 08:29:28 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_packedtags.h"
39 __u8 data[PAGE_SIZE]; // Data + spare
44 yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
56 static yflash_Device filedisk;
58 static int CheckInit(yaffs_Device *dev)
60 static int initialised = 0;
78 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
80 filedisk.handle = open("yaffsemfile2k", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
82 if(filedisk.handle < 0)
84 perror("Failed to open yaffs emulation file");
89 fSize = lseek(filedisk.handle,0,SEEK_END);
91 if(fSize < filedisk.nBlocks * BLOCK_SIZE)
93 printf("Creating yaffs emulation file\n");
95 lseek(filedisk.handle,0,SEEK_SET);
97 memset(&p,0xff,sizeof(yflash_Page));
99 for(i = 0; i < filedisk.nBlocks * BLOCK_SIZE; i+= PAGE_SIZE)
101 written = write(filedisk.handle,&p,sizeof(yflash_Page));
103 if(written != sizeof(yflash_Page))
105 printf("Write failed\n");
114 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
125 pos = chunkInNAND * PAGE_SIZE;
126 lseek(filedisk.handle,pos,SEEK_SET);
127 written = write(filedisk.handle,data,dev->nBytesPerChunk);
129 if(written != dev->nBytesPerChunk) return YAFFS_FAIL;
134 pos = chunkInNAND * PAGE_SIZE + PAGE_DATA_SIZE;
135 lseek(filedisk.handle,pos,SEEK_SET);
139 written = write(filedisk.handle,tags,sizeof(yaffs_ExtendedTags));
140 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
145 yaffs_PackTags(&pt,tags);
147 written = write(filedisk.handle,&pt,sizeof(pt));
148 if(written != sizeof(pt)) return YAFFS_FAIL;
157 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
165 memset(&pt,0,sizeof(pt));
166 lseek(filedisk.handle,(blockNo * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
167 written = write(filedisk.handle,&pt,sizeof(pt));
169 if(written != sizeof(pt)) return YAFFS_FAIL;
176 int yaffs_CheckAllFF(const __u8 *ptr, int n)
181 if(*ptr!=0xFF) return 0;
188 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
199 pos = chunkInNAND * PAGE_SIZE;
200 lseek(filedisk.handle,pos,SEEK_SET);
201 nread = read(filedisk.handle,data,dev->nBytesPerChunk);
203 if(nread != dev->nBytesPerChunk) return YAFFS_FAIL;
208 pos = chunkInNAND * PAGE_SIZE + PAGE_DATA_SIZE;
209 lseek(filedisk.handle,pos,SEEK_SET);
212 nread= read(filedisk.handle,tags,sizeof(yaffs_ExtendedTags));
213 if(nread != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
214 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
216 yaffs_InitialiseTags(tags);
226 nread= read(filedisk.handle,&pt,sizeof(pt));
227 yaffs_UnpackTags(tags,&pt);
228 if(nread != sizeof(pt)) return YAFFS_FAIL;
238 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
245 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
247 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
259 pos = lseek(filedisk.handle, blockNumber * dev->nChunksPerBlock * PAGE_SIZE, SEEK_SET);
261 for(i = 0; i < dev->nChunksPerBlock; i++)
263 write(filedisk.handle,pg,PAGE_SIZE);
265 pos = lseek(filedisk.handle, 0,SEEK_CUR);
272 int yflash_InitialiseNAND(yaffs_Device *dev)
282 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
284 yaffs_ExtendedTags tags;
289 chunkNo = blockNo * dev->nChunksPerBlock;
291 yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
294 *state = YAFFS_BLOCK_STATE_DEAD;
296 else if(!tags.chunkUsed)
298 *state = YAFFS_BLOCK_STATE_EMPTY;
300 else if(tags.chunkUsed)
302 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
303 *sequenceNumber = tags.sequenceNumber;