*** empty log message ***
[yaffs/.git] / yaffs_ramem.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  * yaffs_ramem.c  NAND emulation on top of a chunk of RAM
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
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.
13  *
14  */
15  //yaffs_ramem.c
16  // Since this creates the RAM block at start up it is pretty useless for testing the scanner.
17
18 #ifndef __KERNEL__
19 #define CONFIG_YAFFS_RAM_ENABLED
20 #endif
21
22 #ifdef CONFIG_YAFFS_RAM_ENABLED
23
24 #include "yportenv.h"
25
26 #include "yaffs_nandemul.h"
27 #include "yaffs_guts.h"
28 #include "yaffsinterface.h"
29 #include "devextras.h"
30
31
32 #define EM_SIZE_IN_MEG 2
33
34 #define BLOCK_SIZE (32 * 528)
35 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
36 #define FILE_SIZE_IN_BLOCKS (FILE_SIZE_IN_MEG * BLOCKS_PER_MEG)
37 #define FILE_SIZE_IN_BYTES (FILE_SIZE_IN_BLOCKS * BLOCK_SIZE)
38
39 #define T(x) YPRINTF(x)
40
41 #define DEFAULT_SIZE_IN_MB 2
42
43 typedef struct 
44 {
45         __u8 data[528]; // Data + spare
46         int count[3];   // The programming count for each area of
47                         // the page (0..255,256..511,512..527
48         int empty;      // is this empty?
49 } nandemul_Page;
50
51 typedef struct
52 {
53         nandemul_Page page[32]; // The pages in the block
54         __u8 damaged;           // Is the block damaged?
55         
56 } nandemul_Block;
57
58
59
60 typedef struct
61 {
62         nandemul_Block **block;
63         int nBlocks;
64 } nandemul_Device;
65
66 static nandemul_Device ned;
67
68 int sizeInMB = DEFAULT_SIZE_IN_MB;
69
70
71 static void nandemul_ReallyEraseBlock(int blockNumber)
72 {
73         int i;
74         
75         nandemul_Block *theBlock = ned.block[blockNumber];
76         
77         for(i = 0; i < 32; i++)
78         {
79                 memset(theBlock->page[i].data,0xff,528);
80                 theBlock->page[i].count[0] = 0;
81                 theBlock->page[i].count[1] = 0;
82                 theBlock->page[i].count[2] = 0;
83                 theBlock->page[i].empty = 1;
84         }
85
86 }
87
88
89 int nandemul_CalcNBlocks(void)
90 {
91         switch(sizeInMB)
92         {
93                 case 8:
94                 case 16:
95                 case 32:
96                 case 64:
97                 case 128:
98                 case 256:
99                 case 512:
100                         break;
101                 default:
102                         sizeInMB = DEFAULT_SIZE_IN_MB;
103         }
104         return sizeInMB * 64;
105 }
106
107
108
109 static int  CheckInit(void)
110 {
111         static int initialised = 0;
112         
113         int i;
114         int fail = 0;
115         int nBlocks; 
116         int nAllocated = 0;
117         
118         if(initialised) 
119         {
120                 return YAFFS_OK;
121         }
122         
123         
124         nBlocks = nandemul_CalcNBlocks();
125         
126         ned.block = YMALLOC(sizeof(nandemul_Block *) * nBlocks);
127         
128         if(!ned.block) return 0;
129         
130         for(i=0; i <nBlocks; i++)
131         {
132                 ned.block[i] = NULL;
133         }
134         
135         for(i=0; i <nBlocks && !fail; i++)
136         {
137                 if((ned.block[i] = YMALLOC(sizeof(nandemul_Block))) == 0)
138                 {
139                         fail = 1;
140                 }
141                 else
142                 {
143                         nandemul_ReallyEraseBlock(i);
144                         ned.block[i]->damaged = 0;
145                         nAllocated++;
146                 }
147         }
148         
149         if(fail)
150         {
151                 for(i = 0; i < nAllocated; i++)
152                 {
153                         YFREE(ned.block[i]);
154                 }
155                 YFREE(ned.block);
156                 
157                 T(("Allocation failed, could only allocate %dMB of %dMB requested.\n",
158                    nAllocated/64,sizeInMB));
159                 return 0;
160         }
161         
162         ned.nBlocks = nBlocks;
163         
164         initialised = 1;
165         
166         return 1;
167 }
168
169 int nandemul_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare)
170 {
171         int blk;
172         int pg;
173         int i;
174         
175         __u8 *x;
176         
177         __u8 *spareAsBytes = (__u8 *)spare;
178
179         
180         CheckInit();
181         
182         blk = chunkInNAND/32;
183         pg = chunkInNAND%32;
184         
185         
186         if(data)
187         {
188                 x = ned.block[blk]->page[pg].data;
189                 
190                 for(i = 0; i < 512; i++)
191                 {
192                         x[i] &=data[i];
193                 }
194                 
195                 ned.block[blk]->page[pg].count[0]++;
196                 ned.block[blk]->page[pg].count[1]++;
197                 ned.block[blk]->page[pg].empty = 0;
198         }
199         
200         
201         if(spare)
202         {
203                 x = &ned.block[blk]->page[pg].data[512];
204                         
205                 for(i = 0; i < 16; i++)
206                 {
207                         x[i] &=spareAsBytes[i];
208                 }
209                 ned.block[blk]->page[pg].count[2]++;
210         }
211
212         return YAFFS_OK;
213 }
214
215
216 int nandemul_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare)
217 {
218         int blk;
219         int pg;
220
221         
222         CheckInit();
223         
224         blk = chunkInNAND/32;
225         pg = chunkInNAND%32;
226         
227         
228         if(data)
229         {
230                 memcpy(data,ned.block[blk]->page[pg].data,512);
231         }
232         
233         
234         if(spare)
235         {
236                 memcpy(spare,&ned.block[blk]->page[pg].data[512],16);
237         }
238
239         return YAFFS_OK;
240 }
241
242
243 int nandemul_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
244 {
245         int blk;
246         int pg;
247         int i;
248
249         
250         CheckInit();
251         
252         blk = chunkInNAND/32;
253         pg = chunkInNAND%32;
254         
255         
256         for(i = 0; i < 528; i++)
257         {
258                 if(ned.block[blk]->page[pg].data[i] != 0xFF)
259                 {
260                         return YAFFS_FAIL;
261                 }
262         }
263
264         return YAFFS_OK;
265
266 }
267
268 int nandemul_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
269 {
270         
271         CheckInit();
272         
273         if(blockNumber < 0 || blockNumber >= ned.nBlocks)
274         {
275                 T(("Attempt to erase non-existant block %d\n",blockNumber));
276         }
277         else if(ned.block[blockNumber]->damaged)
278         {
279                 T(("Attempt to erase damaged block %d\n",blockNumber));
280         }
281         else
282         {
283                 nandemul_ReallyEraseBlock(blockNumber);
284         }
285         
286         return YAFFS_OK;
287 }
288
289 int nandemul_InitialiseNAND(yaffs_Device *dev)
290 {
291         return YAFFS_OK;
292 }
293
294 #endif //YAFFS_RAM_ENABLED
295