bcb44f8b99899249377453677fe515d00721d270
[yaffs2.git] / direct / yaffs_ramdisk.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2007 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  *
13  */
14
15 /*
16  * yaffs_ramdisk.c: yaffs ram disk component
17  * This provides a ram disk under yaffs.
18  * NB this is not intended for NAND emulation.
19  * Use this with dev->useNANDECC enabled, then ECC overheads are not required.
20  */
21
22 const char *yaffs_ramdisk_c_version = "$Id: yaffs_ramdisk.c,v 1.3 2007-02-12 16:49:50 wookey Exp $";
23
24
25 #include "yportenv.h"
26
27 #include "yaffs_ramdisk.h"
28 #include "yaffs_guts.h"
29 #include "devextras.h"
30 #include "yaffs_packedtags1.h"
31
32
33
34 #define SIZE_IN_MB 2
35
36 #define BLOCK_SIZE (32 * 528)
37 #define BLOCKS_PER_MEG ((1024*1024)/(32 * 512))
38
39
40
41
42
43 typedef struct 
44 {
45         __u8 data[528]; // Data + spare
46 } yramdisk_Page;
47
48 typedef struct
49 {
50         yramdisk_Page page[32]; // The pages in the block
51         
52 } yramdisk_Block;
53
54
55
56 typedef struct
57 {
58         yramdisk_Block **block;
59         int nBlocks;
60 } yramdisk_Device;
61
62 static yramdisk_Device ramdisk;
63
64 static int  CheckInit(yaffs_Device *dev)
65 {
66         static int initialised = 0;
67         
68         int i;
69         int fail = 0;
70         //int nBlocks; 
71         int nAllocated = 0;
72         
73         if(initialised) 
74         {
75                 return YAFFS_OK;
76         }
77
78         initialised = 1;
79         
80         
81         ramdisk.nBlocks = (SIZE_IN_MB * 1024 * 1024)/(16 * 1024);
82         
83         ramdisk.block = YMALLOC(sizeof(yramdisk_Block *) * ramdisk.nBlocks);
84         
85         if(!ramdisk.block) return 0;
86         
87         for(i=0; i <ramdisk.nBlocks; i++)
88         {
89                 ramdisk.block[i] = NULL;
90         }
91         
92         for(i=0; i <ramdisk.nBlocks && !fail; i++)
93         {
94                 if((ramdisk.block[i] = YMALLOC(sizeof(yramdisk_Block))) == 0)
95                 {
96                         fail = 1;
97                 }
98                 else
99                 {
100                         yramdisk_EraseBlockInNAND(dev,i);
101                         nAllocated++;
102                 }
103         }
104         
105         if(fail)
106         {
107                 for(i = 0; i < nAllocated; i++)
108                 {
109                         YFREE(ramdisk.block[i]);
110                 }
111                 YFREE(ramdisk.block);
112                 
113                 T(YAFFS_TRACE_ALWAYS,("Allocation failed, could only allocate %dMB of %dMB requested.\n",
114                    nAllocated/64,ramdisk.nBlocks * 528));
115                 return 0;
116         }
117         
118         
119         return 1;
120 }
121
122 int yramdisk_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
123 {
124         int blk;
125         int pg;
126         
127
128         CheckInit(dev);
129         
130         blk = chunkInNAND/32;
131         pg = chunkInNAND%32;
132         
133         
134         if(data)
135         {
136                 memcpy(ramdisk.block[blk]->page[pg].data,data,512);
137         }
138         
139         
140         if(tags)
141         {
142                 yaffs_PackedTags1 pt;
143                 
144                 yaffs_PackTags1(&pt,tags);
145                 memcpy(&ramdisk.block[blk]->page[pg].data[512],&pt,sizeof(pt));
146         }
147
148         return YAFFS_OK;        
149
150 }
151
152
153 int yramdisk_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
154 {
155         int blk;
156         int pg;
157
158         
159         CheckInit(dev);
160         
161         blk = chunkInNAND/32;
162         pg = chunkInNAND%32;
163         
164         
165         if(data)
166         {
167                 memcpy(data,ramdisk.block[blk]->page[pg].data,512);
168         }
169         
170         
171         if(tags)
172         {
173                 yaffs_PackedTags1 pt;
174                 
175                 memcpy(&pt,&ramdisk.block[blk]->page[pg].data[512],sizeof(pt));
176                 yaffs_UnpackTags1(tags,&pt);
177                 
178         }
179
180         return YAFFS_OK;
181 }
182
183
184 int yramdisk_CheckChunkErased(yaffs_Device *dev,int chunkInNAND)
185 {
186         int blk;
187         int pg;
188         int i;
189
190         
191         CheckInit(dev);
192         
193         blk = chunkInNAND/32;
194         pg = chunkInNAND%32;
195         
196         
197         for(i = 0; i < 528; i++)
198         {
199                 if(ramdisk.block[blk]->page[pg].data[i] != 0xFF)
200                 {
201                         return YAFFS_FAIL;
202                 }
203         }
204
205         return YAFFS_OK;
206
207 }
208
209 int yramdisk_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
210 {
211         
212         CheckInit(dev);
213         
214         if(blockNumber < 0 || blockNumber >= ramdisk.nBlocks)
215         {
216                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
217                 return YAFFS_FAIL;
218         }
219         else
220         {
221                 memset(ramdisk.block[blockNumber],0xFF,sizeof(yramdisk_Block));
222                 return YAFFS_OK;
223         }
224         
225 }
226
227 int yramdisk_InitialiseNAND(yaffs_Device *dev)
228 {
229         //dev->useNANDECC = 1; // force on useNANDECC which gets faked. 
230                                                  // This saves us doing ECC checks.
231         
232         return YAFFS_OK;
233 }
234
235