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.4 2005-07-18 23:12:00 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
56 static yflash_Device filedisk;
58 static int CheckInit(void)
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");
112 filedisk.nBlocks = fSize/(BLOCK_SIZE);
119 int yflash_GetNumberOfBlocks(void)
123 return filedisk.nBlocks;
126 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
137 pos = chunkInNAND * PAGE_SIZE;
138 lseek(filedisk.handle,pos,SEEK_SET);
139 written = write(filedisk.handle,data,dev->nBytesPerChunk);
141 if(written != dev->nBytesPerChunk) return YAFFS_FAIL;
146 pos = chunkInNAND * PAGE_SIZE + PAGE_DATA_SIZE;
147 lseek(filedisk.handle,pos,SEEK_SET);
148 if( 0 && dev->isYaffs2)
151 written = write(filedisk.handle,tags,sizeof(yaffs_ExtendedTags));
152 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
156 yaffs_PackedTags2 pt;
157 yaffs_PackTags2(&pt,tags);
159 written = write(filedisk.handle,&pt,sizeof(pt));
160 if(written != sizeof(pt)) return YAFFS_FAIL;
169 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
173 yaffs_PackedTags2 pt;
177 memset(&pt,0,sizeof(pt));
178 lseek(filedisk.handle,(blockNo * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
179 written = write(filedisk.handle,&pt,sizeof(pt));
181 if(written != sizeof(pt)) return YAFFS_FAIL;
188 int yaffs_CheckAllFF(const __u8 *ptr, int n)
193 if(*ptr!=0xFF) return 0;
200 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
211 pos = chunkInNAND * PAGE_SIZE;
212 lseek(filedisk.handle,pos,SEEK_SET);
213 nread = read(filedisk.handle,data,dev->nBytesPerChunk);
215 if(nread != dev->nBytesPerChunk) return YAFFS_FAIL;
220 pos = chunkInNAND * PAGE_SIZE + PAGE_DATA_SIZE;
221 lseek(filedisk.handle,pos,SEEK_SET);
222 if(0 && dev->isYaffs2)
224 nread= read(filedisk.handle,tags,sizeof(yaffs_ExtendedTags));
225 if(nread != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
226 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
228 yaffs_InitialiseTags(tags);
237 yaffs_PackedTags2 pt;
238 nread= read(filedisk.handle,&pt,sizeof(pt));
239 yaffs_UnpackTags2(tags,&pt);
240 if(nread != sizeof(pt)) return YAFFS_FAIL;
250 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
257 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
259 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
271 pos = lseek(filedisk.handle, blockNumber * dev->nChunksPerBlock * PAGE_SIZE, SEEK_SET);
273 for(i = 0; i < dev->nChunksPerBlock; i++)
275 write(filedisk.handle,pg,PAGE_SIZE);
277 pos = lseek(filedisk.handle, 0,SEEK_CUR);
284 int yflash_InitialiseNAND(yaffs_Device *dev)
294 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
296 yaffs_ExtendedTags tags;
301 chunkNo = blockNo * dev->nChunksPerBlock;
303 yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
306 *state = YAFFS_BLOCK_STATE_DEAD;
308 else if(!tags.chunkUsed)
310 *state = YAFFS_BLOCK_STATE_EMPTY;
312 else if(tags.chunkUsed)
314 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
315 *sequenceNumber = tags.sequenceNumber;