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.
19 const char *yaffs_flashif_c_version = "$Id: yaffs_fileem.c,v 1.6 2010-01-11 04:06:47 charles Exp $";
23 #include "yaffs_trace.h"
25 #include "yaffs_flashif.h"
26 #include "yaffs_guts.h"
28 #include "devextras.h"
30 #include <sys/types.h>
39 #define BLOCK_SIZE (32 * 528)
40 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
46 __u8 data[528]; // Data + spare
51 yflash_Page page[32]; // The pages in the block
63 static yflash_Device filedisk;
65 static int CheckInit(yaffs_Device *dev)
67 static int initialised = 0;
85 filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
87 filedisk.handle = open("emfile-512-0", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
89 if(filedisk.handle < 0)
91 perror("Failed to open yaffs emulation file");
96 fSize = lseek(filedisk.handle,0,SEEK_END);
98 if(fSize < SIZE_IN_MB * 1024 * 1024)
100 printf("Creating yaffs emulation file\n");
102 lseek(filedisk.handle,0,SEEK_SET);
104 memset(&p,0xff,sizeof(yflash_Page));
106 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
108 written = write(filedisk.handle,&p,sizeof(yflash_Page));
110 if(written != sizeof(yflash_Page))
112 printf("Write failed\n");
121 int yflash_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
131 lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
132 written = write(filedisk.handle,data,512);
134 if(written != 512) return YAFFS_FAIL;
139 lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
140 written = write(filedisk.handle,spare,16);
142 if(written != 16) return YAFFS_FAIL;
151 int yflash_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
161 lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
162 nread = read(filedisk.handle,data,512);
164 if(nread != 512) return YAFFS_FAIL;
169 lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
170 nread= read(filedisk.handle,spare,16);
172 if(nread != 16) return YAFFS_FAIL;
181 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
188 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
190 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
198 memset(&pg,0xff,sizeof(yflash_Page));
200 lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
202 for(i = 0; i < 32; i++)
204 write(filedisk.handle,&pg,528);
211 int yflash_InitialiseNAND(yaffs_Device *dev)
213 dev->useNANDECC = 1; // force on useNANDECC which gets faked.
214 // This saves us doing ECC checks.