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.5 2006-09-21 08:13:59 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 int yaffs_testPartialWrite = 0;
60 static int CheckInit(void)
62 static int initialised = 0;
80 filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
82 filedisk.handle = open("yaffsemfile2k", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
84 if(filedisk.handle < 0)
86 perror("Failed to open yaffs emulation file");
91 fSize = lseek(filedisk.handle,0,SEEK_END);
93 if(fSize < filedisk.nBlocks * BLOCK_SIZE)
95 printf("Creating yaffs emulation file\n");
97 lseek(filedisk.handle,0,SEEK_SET);
99 memset(&p,0xff,sizeof(yflash_Page));
101 for(i = 0; i < filedisk.nBlocks * BLOCK_SIZE; i+= PAGE_SIZE)
103 written = write(filedisk.handle,&p,sizeof(yflash_Page));
105 if(written != sizeof(yflash_Page))
107 printf("Write failed\n");
114 filedisk.nBlocks = fSize/(BLOCK_SIZE);
121 int yflash_GetNumberOfBlocks(void)
125 return filedisk.nBlocks;
128 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
139 pos = chunkInNAND * PAGE_SIZE;
140 lseek(filedisk.handle,pos,SEEK_SET);
141 written = write(filedisk.handle,data,dev->nBytesPerChunk);
143 if(yaffs_testPartialWrite){
144 close(filedisk.handle);
148 if(written != dev->nBytesPerChunk) return YAFFS_FAIL;
153 pos = chunkInNAND * PAGE_SIZE + PAGE_DATA_SIZE;
154 lseek(filedisk.handle,pos,SEEK_SET);
155 if( 0 && dev->isYaffs2)
158 written = write(filedisk.handle,tags,sizeof(yaffs_ExtendedTags));
159 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
163 yaffs_PackedTags2 pt;
164 yaffs_PackTags2(&pt,tags);
166 written = write(filedisk.handle,&pt,sizeof(pt));
167 if(written != sizeof(pt)) return YAFFS_FAIL;
176 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
180 yaffs_PackedTags2 pt;
184 memset(&pt,0,sizeof(pt));
185 lseek(filedisk.handle,(blockNo * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
186 written = write(filedisk.handle,&pt,sizeof(pt));
188 if(written != sizeof(pt)) return YAFFS_FAIL;
195 int yaffs_CheckAllFF(const __u8 *ptr, int n)
200 if(*ptr!=0xFF) return 0;
207 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
218 pos = chunkInNAND * PAGE_SIZE;
219 lseek(filedisk.handle,pos,SEEK_SET);
220 nread = read(filedisk.handle,data,dev->nBytesPerChunk);
222 if(nread != dev->nBytesPerChunk) return YAFFS_FAIL;
227 pos = chunkInNAND * PAGE_SIZE + PAGE_DATA_SIZE;
228 lseek(filedisk.handle,pos,SEEK_SET);
229 if(0 && dev->isYaffs2)
231 nread= read(filedisk.handle,tags,sizeof(yaffs_ExtendedTags));
232 if(nread != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
233 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
235 yaffs_InitialiseTags(tags);
244 yaffs_PackedTags2 pt;
245 nread= read(filedisk.handle,&pt,sizeof(pt));
246 yaffs_UnpackTags2(tags,&pt);
247 if(nread != sizeof(pt)) return YAFFS_FAIL;
257 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
264 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
266 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
278 pos = lseek(filedisk.handle, blockNumber * dev->nChunksPerBlock * PAGE_SIZE, SEEK_SET);
280 for(i = 0; i < dev->nChunksPerBlock; i++)
282 write(filedisk.handle,pg,PAGE_SIZE);
284 pos = lseek(filedisk.handle, 0,SEEK_CUR);
291 int yflash_InitialiseNAND(yaffs_Device *dev)
301 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
303 yaffs_ExtendedTags tags;
308 chunkNo = blockNo * dev->nChunksPerBlock;
310 yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
313 *state = YAFFS_BLOCK_STATE_DEAD;
315 else if(!tags.chunkUsed)
317 *state = YAFFS_BLOCK_STATE_EMPTY;
319 else if(tags.chunkUsed)
321 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
322 *sequenceNumber = tags.sequenceNumber;