2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 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.7 2010-02-18 01:18:04 charles Exp $";
23 #include "yaffs_trace.h"
25 #include "yaffs_flashif.h"
26 #include "yaffs_guts.h"
28 #include <sys/types.h>
37 #define BLOCK_SIZE (32 * 528)
38 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
44 u8 data[528]; // Data + spare
49 yflash_Page page[32]; // The pages in the block
61 static yflash_Device filedisk;
63 static int CheckInit(struct yaffs_dev *dev)
65 static int initialised = 0;
83 filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
85 filedisk.handle = open("emfile-512-0", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
87 if(filedisk.handle < 0)
89 perror("Failed to open yaffs emulation file");
94 fSize = lseek(filedisk.handle,0,SEEK_END);
96 if(fSize < SIZE_IN_MB * 1024 * 1024)
98 printf("Creating yaffs emulation file\n");
100 lseek(filedisk.handle,0,SEEK_SET);
102 memset(&p,0xff,sizeof(yflash_Page));
104 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
106 written = write(filedisk.handle,&p,sizeof(yflash_Page));
108 if(written != sizeof(yflash_Page))
110 printf("Write failed\n");
119 int yflash_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_spare *spare)
129 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
130 written = write(filedisk.handle,data,512);
132 if(written != 512) return YAFFS_FAIL;
137 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
138 written = write(filedisk.handle,spare,16);
140 if(written != 16) return YAFFS_FAIL;
149 int yflash_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_spare *spare)
159 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
160 nread = read(filedisk.handle,data,512);
162 if(nread != 512) return YAFFS_FAIL;
167 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
168 nread= read(filedisk.handle,spare,16);
170 if(nread != 16) return YAFFS_FAIL;
179 int yflash_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
186 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
188 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
196 memset(&pg,0xff,sizeof(yflash_Page));
198 lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
200 for(i = 0; i < 32; i++)
202 write(filedisk.handle,&pg,528);
209 int yflash_InitialiseNAND(struct yaffs_dev *dev)