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