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