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