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.
16 // THis is only intended as test code to test persistence etc.
18 const char *yaffs_flashif_c_version = "$Id: yaffs_fileem.c,v 1.1 2003-01-21 03:32:17 charles Exp $";
23 #include "yaffs_flashif.h"
24 #include "yaffs_guts.h"
25 #include "devextras.h"
27 #include <sys/types.h>
36 #define BLOCK_SIZE (32 * 528)
37 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
43 __u8 data[528]; // Data + spare
48 yflash_Page page[32]; // The pages in the block
60 static yflash_Device filedisk;
62 static int CheckInit(yaffs_Device *dev)
64 static int initialised = 0;
82 filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
84 filedisk.handle = open("yaffsemfile", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
86 if(filedisk.handle < 0)
88 perror("Failed to open yaffs emulation file");
93 fSize = lseek(filedisk.handle,0,SEEK_END);
95 if(fSize < SIZE_IN_MB * 1024 * 1024)
97 printf("Creating yaffs emulation file\n");
99 lseek(filedisk.handle,0,SEEK_SET);
101 memset(&p,0xff,sizeof(yflash_Page));
103 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
105 written = write(filedisk.handle,&p,sizeof(yflash_Page));
107 if(written != sizeof(yflash_Page))
109 printf("Write failed\n");
118 int yflash_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
128 lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
129 written = write(filedisk.handle,data,512);
131 if(written != 512) return YAFFS_FAIL;
136 lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
137 written = write(filedisk.handle,spare,16);
139 if(written != 16) return YAFFS_FAIL;
148 int yflash_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
158 lseek(filedisk.handle,chunkInNAND * 528,SEEK_SET);
159 nread = read(filedisk.handle,data,512);
161 if(nread != 512) return YAFFS_FAIL;
166 lseek(filedisk.handle,chunkInNAND * 528 + 512,SEEK_SET);
167 nread= read(filedisk.handle,spare,16);
169 if(nread != 16) return YAFFS_FAIL;
178 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
185 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
187 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
195 memset(&pg,0xff,sizeof(yflash_Page));
197 lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
199 for(i = 0; i < 32; i++)
201 write(filedisk.handle,&pg,528);
208 int yflash_InitialiseNAND(yaffs_Device *dev)
210 dev->useNANDECC = 1; // force on useNANDECC which gets faked.
211 // This saves us doing ECC checks.