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;
79 filedisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
81 filedisk.handle = open("emfile-512-0", O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
83 if(filedisk.handle < 0)
85 perror("Failed to open yaffs emulation file");
90 fSize = lseek(filedisk.handle,0,SEEK_END);
92 if(fSize < SIZE_IN_MB * 1024 * 1024)
94 printf("Creating yaffs emulation file\n");
96 lseek(filedisk.handle,0,SEEK_SET);
98 memset(&p,0xff,sizeof(yflash_Page));
100 for(i = 0; i < SIZE_IN_MB * 1024 * 1024; i+= 512)
102 written = write(filedisk.handle,&p,sizeof(yflash_Page));
104 if(written != sizeof(yflash_Page))
106 printf("Write failed\n");
115 int yflash_WriteChunkToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_spare *spare)
123 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
124 written = write(filedisk.handle,data,512);
126 if(written != 512) return YAFFS_FAIL;
131 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
132 written = write(filedisk.handle,spare,16);
134 if(written != 16) return YAFFS_FAIL;
143 int yflash_ReadChunkFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_spare *spare)
151 lseek(filedisk.handle,nand_chunk * 528,SEEK_SET);
152 nread = read(filedisk.handle,data,512);
154 if(nread != 512) return YAFFS_FAIL;
159 lseek(filedisk.handle,nand_chunk * 528 + 512,SEEK_SET);
160 nread= read(filedisk.handle,spare,16);
162 if(nread != 16) return YAFFS_FAIL;
171 int yflash_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
177 if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
179 yaffs_trace(YAFFS_TRACE_ALWAYS,
180 "Attempt to erase non-existant block %d\n",
189 memset(&pg,0xff,sizeof(yflash_Page));
191 lseek(filedisk.handle, blockNumber * 32 * 528, SEEK_SET);
193 for(i = 0; i < 32; i++)
195 write(filedisk.handle,&pg,528);
202 int yflash_InitialiseNAND(struct yaffs_dev *dev)