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.5 2009-01-12 00:49:01 charles Exp $";
24 #include "yaffs_flashif.h"
25 #include "yaffs_guts.h"
27 #include "devextras.h"
29 #include <sys/types.h>
38 #define BLOCK_SIZE (32 * 528)
39 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
45 __u8 data[528]; // Data + spare
50 yflash_Page page[32]; // The pages in the block
62 static yflash_Device filedisk;
64 static int CheckInit(yaffs_Device *dev)
66 static int initialised = 0;
84 filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
86 filedisk.handle = open("emfile-512-0", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
88 if(filedisk.handle < 0)
90 perror("Failed to open yaffs emulation file");
95 fSize = lseek(filedisk.handle,0,SEEK_END);
97 if(fSize < SIZE_IN_MB * 1024 * 1024)
99 printf("Creating yaffs emulation file\n");
101 lseek(filedisk.handle,0,SEEK_SET);
103 memset(&p,0xff,sizeof(yflash_Page));
105 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
107 written = write(filedisk.handle,&p,sizeof(yflash_Page));
109 if(written != sizeof(yflash_Page))
111 printf("Write failed\n");
120 int yflash_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
130 lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
131 written = write(filedisk.handle,data,512);
133 if(written != 512) return YAFFS_FAIL;
138 lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
139 written = write(filedisk.handle,spare,16);
141 if(written != 16) return YAFFS_FAIL;
150 int yflash_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
160 lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
161 nread = read(filedisk.handle,data,512);
163 if(nread != 512) return YAFFS_FAIL;
168 lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
169 nread= read(filedisk.handle,spare,16);
171 if(nread != 16) return YAFFS_FAIL;
180 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
187 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
189 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
197 memset(&pg,0xff,sizeof(yflash_Page));
199 lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
201 for(i = 0; i < 32; i++)
203 write(filedisk.handle,&pg,528);
210 int yflash_InitialiseNAND(yaffs_Device *dev)
212 dev->useNANDECC = 1; // force on useNANDECC which gets faked.
213 // This saves us doing ECC checks.