6bd3a0e73ec17c65b6be410aa05e97e84f059ca3
[yaffs2.git] / direct / test-framework / yaffs_ramem2k.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2018 Aleph One Ltd.
5  *
6  * Created by Charles Manning <charles@aleph1.co.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 /*
14  * yaffs_ramem2k.c: RAM emulation in-kernel for 2K pages (YAFFS2)
15  */
16
17
18 #ifndef __KERNEL__
19 #define CONFIG_YAFFS_RAM_ENABLED
20 #else
21 #include <linux/config.h>
22 #endif
23
24 #ifdef CONFIG_YAFFS_RAM_ENABLED
25
26 #include "yportenv.h"
27 #include "yaffs_trace.h"
28
29 #include "yaffs_nandemul2k.h"
30 #include "yaffs_guts.h"
31 #include "yaffs_packedtags2.h"
32
33
34
35 #define EM_SIZE_IN_MEG (32)
36 #define PAGE_DATA_SIZE  (2048)
37 #define PAGE_SPARE_SIZE (64)
38 #define PAGES_PER_BLOCK (64)
39
40
41
42 #define EM_SIZE_IN_BYTES (EM_SIZE_IN_MEG * (1<<20))
43
44 #define PAGE_TOTAL_SIZE (PAGE_DATA_SIZE+PAGE_SPARE_SIZE)
45
46 #define BLOCK_TOTAL_SIZE (PAGES_PER_BLOCK * PAGE_TOTAL_SIZE)
47
48 #define BLOCKS_PER_MEG ((1<<20)/(PAGES_PER_BLOCK * PAGE_DATA_SIZE))
49
50
51 typedef struct
52 {
53         u8 data[PAGE_TOTAL_SIZE]; // Data + spare
54         int empty;      // is this empty?
55 } nandemul_Page;
56
57
58 typedef struct
59 {
60         nandemul_Page *page[PAGES_PER_BLOCK];
61         int damaged;
62 } nandemul_Block;
63
64
65
66 typedef struct
67 {
68         nandemul_Block**block;
69         int nBlocks;
70 } nandemul_Device;
71
72 static nandemul_Device ned;
73
74 static int sizeInMB = EM_SIZE_IN_MEG;
75
76
77 static void nandemul_yield(int n)
78 {
79         (void)n;
80 #ifdef __KERNEL__
81         if(n > 0) schedule_timeout(n);
82 #endif
83
84 }
85
86
87 static void nandemul_ReallyEraseBlock(int blockNumber)
88 {
89         int i;
90
91         nandemul_Block *blk;
92
93         if(blockNumber < 0 || blockNumber >= ned.nBlocks)
94         {
95                 return;
96         }
97
98         blk = ned.block[blockNumber];
99
100         for(i = 0; i < PAGES_PER_BLOCK; i++)
101         {
102                 memset(blk->page[i],0xff,sizeof(nandemul_Page));
103                 blk->page[i]->empty = 1;
104         }
105         nandemul_yield(2);
106 }
107
108
109 static int nandemul2k_CalcNBlocks(void)
110 {
111         return EM_SIZE_IN_MEG * BLOCKS_PER_MEG;
112 }
113
114
115
116 static int  CheckInit(void)
117 {
118         static int initialised = 0;
119
120         int i,j;
121
122         int fail = 0;
123         int nBlocks;
124
125         int nAllocated = 0;
126
127         if(initialised)
128         {
129                 return YAFFS_OK;
130         }
131
132
133         ned.nBlocks = nBlocks = nandemul2k_CalcNBlocks();
134
135
136         ned.block = malloc(sizeof(nandemul_Block*) * nBlocks );
137
138         if(!ned.block) return YAFFS_FAIL;
139
140
141
142
143
144         for(i=fail=0; i <nBlocks; i++)
145         {
146
147                 nandemul_Block *blk;
148
149                 if(!(blk = ned.block[i] = malloc(sizeof(nandemul_Block))))
150                 {
151                  fail = 1;
152                 }
153                 else
154                 {
155                         for(j = 0; j < PAGES_PER_BLOCK; j++)
156                         {
157                                 if((blk->page[j] = malloc(sizeof(nandemul_Page))) == 0)
158                                 {
159                                         fail = 1;
160                                 }
161                         }
162                         nandemul_ReallyEraseBlock(i);
163                         ned.block[i]->damaged = 0;
164                         nAllocated++;
165                 }
166         }
167
168         if(fail)
169         {
170                 //Todo thump pages
171
172                 for(i = 0; i < nAllocated; i++)
173                 {
174                         kfree(ned.block[i]);
175                 }
176                 kfree(ned.block);
177
178                 yaffs_trace(YAFFS_TRACE_ALWAYS,
179                         "Allocation failed, could only allocate %dMB of %dMB requested.\n",
180                         nAllocated/64,sizeInMB);
181                 return 0;
182         }
183
184         ned.nBlocks = nBlocks;
185
186         initialised = 1;
187
188         return 1;
189 }
190
191 int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_dev *dev,int nand_chunk,const u8 *data, const struct yaffs_ext_tags *tags)
192 {
193         int blk;
194         int pg;
195         int i;
196
197         u8 *x;
198
199
200         blk = nand_chunk/PAGES_PER_BLOCK;
201         pg = nand_chunk%PAGES_PER_BLOCK;
202
203
204         if(data)
205         {
206                 x = ned.block[blk]->page[pg]->data;
207
208                 for(i = 0; i < PAGE_DATA_SIZE; i++)
209                 {
210                         x[i] &=data[i];
211                 }
212
213                 ned.block[blk]->page[pg]->empty = 0;
214         }
215
216
217         if(tags)
218         {
219                 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
220
221                 yaffs_pack_tags2(dev, (struct yaffs_packed_tags2 *)x,tags, !dev->param.no_tags_ecc);
222
223         }
224
225         if(tags || data)
226         {
227                 nandemul_yield(1);
228         }
229
230         return YAFFS_OK;
231 }
232
233
234 int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_dev *dev,int nand_chunk, u8 *data, struct yaffs_ext_tags *tags)
235 {
236         int blk;
237         int pg;
238
239         u8 *x;
240
241
242
243         blk = nand_chunk/PAGES_PER_BLOCK;
244         pg = nand_chunk%PAGES_PER_BLOCK;
245
246
247         if(data)
248         {
249                 memcpy(data,ned.block[blk]->page[pg]->data,PAGE_DATA_SIZE);
250         }
251
252
253         if(tags)
254         {
255                 x = &ned.block[blk]->page[pg]->data[PAGE_DATA_SIZE];
256
257                 yaffs_unpack_tags2(dev, tags,(struct yaffs_packed_tags2 *)x, !dev->param.no_tags_ecc);
258         }
259
260         return YAFFS_OK;
261 }
262
263 int nandemul2k_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
264 {
265         (void) dev;
266
267         if(blockNumber < 0 || blockNumber >= ned.nBlocks)
268         {
269                 yaffs_trace(YAFFS_TRACE_ALWAYS,
270                         "Attempt to erase non-existant block %d\n",
271                         blockNumber);
272         }
273         else if(ned.block[blockNumber]->damaged)
274         {
275                 yaffs_trace(YAFFS_TRACE_ALWAYS,
276                         "Attempt to erase damaged block %d\n",
277                         blockNumber);
278         }
279         else
280         {
281                 nandemul_ReallyEraseBlock(blockNumber);
282         }
283
284         return YAFFS_OK;
285 }
286
287 int nandemul2k_InitialiseNAND(struct yaffs_dev *dev)
288 {
289         (void) dev;
290
291         CheckInit();
292         return YAFFS_OK;
293 }
294
295 int nandemul2k_MarkNANDBlockBad(struct yaffs_dev *dev, int block_no)
296 {
297
298         u8 *x;
299
300         (void) dev;
301
302         x = &ned.block[block_no]->page[0]->data[PAGE_DATA_SIZE];
303
304         memset(x,0,sizeof(struct yaffs_packed_tags2));
305
306
307         return YAFFS_OK;
308
309 }
310
311 int nandemul2k_QueryNANDBlock(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, u32  *seq_number)
312 {
313         struct yaffs_ext_tags tags;
314         int chunkNo;
315
316         *seq_number = 0;
317
318         chunkNo = block_no * dev->param.chunks_per_block;
319
320         nandemul2k_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
321         if(tags.block_bad)
322         {
323                 *state = YAFFS_BLOCK_STATE_DEAD;
324         }
325         else if(!tags.chunk_used)
326         {
327                 *state = YAFFS_BLOCK_STATE_EMPTY;
328         }
329         else if(tags.chunk_used)
330         {
331                 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
332                 *seq_number = tags.seq_number;
333         }
334         return YAFFS_OK;
335 }
336
337 int nandemul2k_GetBytesPerChunk(void) { return PAGE_DATA_SIZE;}
338
339 int nandemul2k_GetChunksPerBlock(void) { return PAGES_PER_BLOCK; }
340 int nandemul2k_GetNumberOfBlocks(void) {return nandemul2k_CalcNBlocks();}
341
342
343 #endif //YAFFS_RAM_ENABLED
344