2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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.
20 #include "yaffs_trace.h"
22 #include "yaffs_flashif.h"
23 #include "yaffs_guts.h"
25 #include <sys/types.h>
34 #define BLOCK_SIZE (32 * 528)
35 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
41 u8 data[528]; // Data + spare
46 yflash_Page page[32]; // The pages in the block
58 static yflash_Device filedisk;
60 static int CheckInit(struct yaffs_dev *dev)
62 static int initialised = 0;
80 filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
82 filedisk.handle = open("emfile-512-0", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
84 if(filedisk.handle < 0)
86 perror("Failed to open yaffs emulation file");
91 fSize = lseek(filedisk.handle,0,SEEK_END);
93 if(fSize < SIZE_IN_MB * 1024 * 1024)
95 printf("Creating yaffs emulation file\n");
97 lseek(filedisk.handle,0,SEEK_SET);
99 memset(&p,0xff,sizeof(yflash_Page));
101 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
103 written = write(filedisk.handle,&p,sizeof(yflash_Page));
105 if(written != sizeof(yflash_Page))
107 printf("Write failed\n");
116 int yflash_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_spare *spare)
126 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
127 written = write(filedisk.handle,data,512);
129 if(written != 512) return YAFFS_FAIL;
134 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
135 written = write(filedisk.handle,spare,16);
137 if(written != 16) return YAFFS_FAIL;
146 int yflash_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_spare *spare)
156 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
157 nread = read(filedisk.handle,data,512);
159 if(nread != 512) return YAFFS_FAIL;
164 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
165 nread= read(filedisk.handle,spare,16);
167 if(nread != 16) return YAFFS_FAIL;
176 int yflash_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
183 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
185 yaffs_trace(YAFFS_TRACE_ALWAYS,
186 "Attempt to erase non-existant block %d\n",
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(struct yaffs_dev *dev)