88fc3a4f3f952242a3ed32d59e468df9d40f0df9
[yaffs2.git] / direct / basic-test / yaffs_ramdisk.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 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->use_nand_ecc enabled, then ECC overheads are not required.
19  */
20
21 const char *yaffs_ramdisk_c_version = "$Id: yaffs_ramdisk.c,v 1.6 2010-01-11 04:06:47 charles Exp $";
22
23
24 #include "yportenv.h"
25 #include "yaffs_trace.h"
26
27 #include "yaffs_ramdisk.h"
28 #include "yaffs_guts.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(struct yaffs_dev *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 = malloc(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] = malloc(sizeof(yramdisk_block))) == 0)
94                 {
95                         fail = 1;
96                 }
97                 else
98                 {
99                         yramdisk_erase(dev,i);
100                         nAllocated++;
101                 }
102         }
103         
104         if(fail)
105         {
106                 for(i = 0; i < nAllocated; i++)
107                 {
108                         kfree(ramdisk.block[i]);
109                 }
110                 kfree(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_wr_chunk(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_ext_tags *tags)
122 {
123         int blk;
124         int pg;
125         
126
127         CheckInit(dev);
128         
129         blk = nand_chunk/32;
130         pg = nand_chunk%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                 struct yaffs_packed_tags1 pt;
142                 
143                 yaffs_pack_tags1(&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_rd_chunk(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_ext_tags *tags)
153 {
154         int blk;
155         int pg;
156
157         
158         CheckInit(dev);
159         
160         blk = nand_chunk/32;
161         pg = nand_chunk%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                 struct yaffs_packed_tags1 pt;
173                 
174                 memcpy(&pt,&ramdisk.block[blk]->page[pg].data[512],sizeof(pt));
175                 yaffs_unpack_tags1(tags,&pt);
176                 
177         }
178
179         return YAFFS_OK;
180 }
181
182
183 int yramdisk_check_chunk_erased(struct yaffs_dev *dev,int nand_chunk)
184 {
185         int blk;
186         int pg;
187         int i;
188
189         
190         CheckInit(dev);
191         
192         blk = nand_chunk/32;
193         pg = nand_chunk%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_erase(struct yaffs_dev *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_initialise(struct yaffs_dev *dev)
227 {
228         //dev->use_nand_ecc = 1; // force on use_nand_ecc which gets faked. 
229                                                  // This saves us doing ECC checks.
230         
231         return YAFFS_OK;
232 }
233
234