yaffs Change tracing to yaffs_trace
[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                 yaffs_trace(YAFFS_TRACE_ALWAYS,
113                         "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_wr_chunk(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_ext_tags *tags)
123 {
124         int blk;
125         int pg;
126         
127
128         CheckInit(dev);
129         
130         blk = nand_chunk/32;
131         pg = nand_chunk%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                 struct yaffs_packed_tags1 pt;
143                 
144                 yaffs_pack_tags1(&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_rd_chunk(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_ext_tags *tags)
154 {
155         int blk;
156         int pg;
157
158         
159         CheckInit(dev);
160         
161         blk = nand_chunk/32;
162         pg = nand_chunk%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                 struct yaffs_packed_tags1 pt;
174                 
175                 memcpy(&pt,&ramdisk.block[blk]->page[pg].data[512],sizeof(pt));
176                 yaffs_unpack_tags1(tags,&pt);
177                 
178         }
179
180         return YAFFS_OK;
181 }
182
183
184 int yramdisk_check_chunk_erased(struct yaffs_dev *dev,int nand_chunk)
185 {
186         int blk;
187         int pg;
188         int i;
189
190         
191         CheckInit(dev);
192         
193         blk = nand_chunk/32;
194         pg = nand_chunk%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_erase(struct yaffs_dev *dev, int blockNumber)
210 {
211         
212         CheckInit(dev);
213         
214         if(blockNumber < 0 || blockNumber >= ramdisk.nBlocks)
215         {
216                 yaffs_trace(YAFFS_TRACE_ALWAYS,
217                         "Attempt to erase non-existant block %d\n",
218                         blockNumber);
219                 return YAFFS_FAIL;
220         }
221         else
222         {
223                 memset(ramdisk.block[blockNumber],0xFF,sizeof(yramdisk_block));
224                 return YAFFS_OK;
225         }
226         
227 }
228
229 int yramdisk_initialise(struct yaffs_dev *dev)
230 {
231         //dev->use_nand_ecc = 1; // force on use_nand_ecc which gets faked. 
232                                                  // This saves us doing ECC checks.
233         
234         return YAFFS_OK;
235 }
236