2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
3 * yaffs_fileem.c NAND emulation on top of files
5 * Copyright (C) 2002 Aleph One Ltd.
6 * for Toby Churchill Ltd and Brightstar Engineering
8 * Created by Charles Manning <charles@aleph1.co.uk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
17 #include "yaffs_fileem.h"
18 #include "yaffs_guts.h"
19 #include "yaffsinterface.h"
24 #include <sys/types.h>
29 #define FILE_SIZE_IN_MEG 32
31 #define BLOCK_SIZE (32 * 528)
32 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
33 #define FILE_SIZE_IN_BLOCKS (FILE_SIZE_IN_MEG * BLOCKS_PER_MEG)
34 #define FILE_SIZE_IN_BYTES (FILE_SIZE_IN_BLOCKS * BLOCK_SIZE)
38 static __u8 ffChunk[528];
40 static int eraseDisplayEnabled;
42 static int markedBadBlocks[] = { 1, 4, -1};
44 static int IsAMarkedBadBlock(int blk)
46 int *m = markedBadBlocks;
50 if(*m == blk) return 1;
57 static void CheckInit(yaffs_Device *dev)
59 static int initialised = 0;
66 memset(ffChunk,0xFF,528);
68 h = open("yaffs-em-file" , O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
71 perror("Fatal error opening yaffs emulation file");
76 length = lseek(h,0,SEEK_END);
77 if(length != FILE_SIZE_IN_BYTES)
79 // Create file contents
82 printf("Creating emulation file...\n");
83 for(i = 0; i < FILE_SIZE_IN_BLOCKS; i++)
85 yaffs_FEEraseBlockInNAND(dev,i);
87 if(IsAMarkedBadBlock(i))
90 memset(&spare,0xff,sizeof(spare));
91 spare.blockStatus = 1;
93 yaffs_FEWriteChunkToNAND(dev, i * 32,NULL,&spare);
97 eraseDisplayEnabled = 1;
101 int yaffs_FEWriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
105 pos = chunkInNAND * 528;
112 lseek(h,pos,SEEK_SET);
120 lseek(h,pos,SEEK_SET);
128 int yaffs_FEReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
132 pos = chunkInNAND * 528;
139 lseek(h,pos,SEEK_SET);
147 lseek(h,pos,SEEK_SET);
155 int yaffs_FEEraseBlockInNAND(yaffs_Device *dev,int blockInNAND)
161 if(eraseDisplayEnabled)
163 printf("Erasing block %d\n",blockInNAND);
166 lseek(h,blockInNAND * BLOCK_SIZE,SEEK_SET);
167 for(i = 0; i < 32; i++)
169 write(h,ffChunk,528);
174 int yaffs_FEInitialiseNAND(yaffs_Device *dev)