2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
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.
16 * This is only intended as test code to test persistence etc.
22 const char *yaffs_flashif_c_version = "$Id: yaffs_fileem.c,v 1.3 2007/02/14 01:09:06 wookey Exp $";
27 #include "yaffs_flashif.h"
28 #include "yaffs_guts.h"
30 #include "devextras.h"
32 #include <sys/types.h>
41 #define BLOCK_SIZE (32 * 528)
42 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
48 __u8 data[528]; // Data + spare
53 yflash_Page page[32]; // The pages in the block
65 static yflash_Device filedisk;
67 static int CheckInit(yaffs_Device *dev)
69 static int initialised = 0;
87 filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
89 filedisk.handle = open("yaffsemfile", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
91 if(filedisk.handle < 0)
93 perror("Failed to open yaffs emulation file");
98 fSize = lseek(filedisk.handle,0,SEEK_END);
100 if(fSize < SIZE_IN_MB * 1024 * 1024)
102 printf("Creating yaffs emulation file\n");
104 lseek(filedisk.handle,0,SEEK_SET);
106 memset(&p,0xff,sizeof(yflash_Page));
108 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
110 written = write(filedisk.handle,&p,sizeof(yflash_Page));
112 if(written != sizeof(yflash_Page))
114 printf("Write failed\n");
123 int yflash_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
133 lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
134 written = write(filedisk.handle,data,512);
136 if(written != 512) return YAFFS_FAIL;
141 lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
142 written = write(filedisk.handle,spare,16);
144 if(written != 16) return YAFFS_FAIL;
153 int yflash_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
163 lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
164 nread = read(filedisk.handle,data,512);
166 if(nread != 512) return YAFFS_FAIL;
171 lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
172 nread= read(filedisk.handle,spare,16);
174 if(nread != 16) return YAFFS_FAIL;
183 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
190 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
192 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
200 memset(&pg,0xff,sizeof(yflash_Page));
202 lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
204 for(i = 0; i < 32; i++)
206 write(filedisk.handle,&pg,528);
213 int yflash_InitialiseNAND(yaffs_Device *dev)
215 dev->useNANDECC = 1; // force on useNANDECC which gets faked.
216 // This saves us doing ECC checks.