yaffs: Refactor drivers WIP - stress tests passing
[yaffs2.git] / direct / basic-test / yaffs_fileem2k.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 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  * This provides a YAFFS nand emulation on a file for emulating 2kB pages.
16  * This is only intended as test code to test persistence etc.
17  */
18
19 #include "yportenv.h"
20 #include "yaffs_trace.h"
21
22 #include "yaffs_flashif2.h"
23 #include "yaffs_guts.h"
24 #include "yaffs_fileem2k.h"
25 #include "yaffs_packedtags2.h"
26
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32
33 #define REPORT_ERROR 0
34
35 typedef struct
36 {
37         u8 data[PAGE_SIZE]; // Data + spare
38 } yflash_Page;
39
40 typedef struct
41 {
42         yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
43
44 } yflash_Block;
45
46
47
48 #define MAX_HANDLES 20
49 #define BLOCKS_PER_HANDLE (32*8)
50
51 typedef struct
52 {
53         int handle[MAX_HANDLES];
54         int nBlocks;
55 } yflash_Device;
56
57 static yflash_Device filedisk;
58
59 int yaffs_test_partial_write = 0;
60
61 extern int random_seed;
62 extern int simulate_power_failure;
63 static int remaining_ops;
64 static int nops_so_far;
65
66 int ops_multiplier;
67
68
69 static void yflash2_MaybePowerFail(unsigned int nand_chunk, int failPoint)
70 {
71
72    nops_so_far++;
73
74
75    remaining_ops--;
76    if(simulate_power_failure &&
77       remaining_ops < 1){
78        printf("Simulated power failure after %d operations\n",nops_so_far);
79        printf("  power failed on nand_chunk %d, at fail point %d\n",
80                         nand_chunk, failPoint);
81         exit(0);
82   }
83 }
84
85
86
87
88
89 static u8 localBuffer[PAGE_SIZE];
90
91 static char *NToName(char *buf,int n)
92 {
93         sprintf(buf,"emfile-2k-%d",n);
94         return buf;
95 }
96
97 static char dummyBuffer[BLOCK_SIZE];
98
99 static int GetBlockFileHandle(int n)
100 {
101         int h;
102         int requiredSize;
103
104         char name[40];
105         NToName(name,n);
106         int fSize;
107         int i;
108
109         h =  open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
110         if(h >= 0){
111                 fSize = lseek(h,0,SEEK_END);
112                 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
113                 if(fSize < requiredSize){
114                    for(i = 0; i < BLOCKS_PER_HANDLE; i++)
115                         if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
116                                 return -1;
117
118                 }
119         }
120
121         return h;
122
123 }
124
125 static int  CheckInit(void)
126 {
127         static int initialised = 0;
128         int i;
129
130         int blk;
131
132
133         if(initialised)
134         {
135                 return YAFFS_OK;
136         }
137
138         initialised = 1;
139
140
141         srand(random_seed);
142         remaining_ops = (rand() % 1000) * 5;
143         memset(dummyBuffer,0xff,sizeof(dummyBuffer));
144
145
146
147         filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
148
149         for(i = 0; i <  MAX_HANDLES; i++)
150                 filedisk.handle[i] = -1;
151
152         for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
153                 filedisk.handle[i] = GetBlockFileHandle(i);
154
155
156         return 1;
157 }
158
159
160 int yflash2_GetNumberOfBlocks(void)
161 {
162         CheckInit();
163
164         return filedisk.nBlocks;
165 }
166
167
168 int yaffs_check_all_ff(const u8 *ptr, int n)
169 {
170         while(n)
171         {
172                 n--;
173                 if(*ptr!=0xFF) return 0;
174                 ptr++;
175         }
176         return 1;
177 }
178
179
180 static int yflash2_WriteChunk(struct yaffs_dev *dev, int nand_chunk,
181                                    const u8 *data, int data_len,
182                                    const u8 *oob, int oob_len)
183 {
184         int retval = YAFFS_OK;
185         int pos;
186         int h;
187         int nwritten;
188
189         (void) dev;
190
191         if (data && data_len) {
192                 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
193                 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
194                 lseek(h,pos,SEEK_SET);
195                 nwritten = write(h,data,data_len);
196                 if(nwritten != data_len)
197                         retval = YAFFS_FAIL;
198         }
199
200         if (oob && oob_len) {
201                 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
202                 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
203                 lseek(h,pos,SEEK_SET);
204                 nwritten = write(h,oob,oob_len);
205                 if(nwritten != oob_len)
206                         retval = YAFFS_FAIL;
207         }
208
209         yflash2_MaybePowerFail(nand_chunk,3);
210
211         return retval;
212 }
213
214 static int yflash2_ReadChunk(struct yaffs_dev *dev, int nand_chunk,
215                                    u8 *data, int data_len,
216                                    u8 *oob, int oob_len,
217                                    enum yaffs_ecc_result *ecc_result)
218 {
219         int retval = YAFFS_OK;
220         int pos;
221         int h;
222         int nread;
223
224         (void) dev;
225
226         if (data && data_len) {
227                 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
228                 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
229                 lseek(h,pos,SEEK_SET);
230                 nread = read(h,data,data_len);
231                 if(nread != data_len)
232                         retval = YAFFS_FAIL;
233         }
234
235         if (oob && oob_len) {
236                 pos = (nand_chunk % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
237                 h = filedisk.handle[(nand_chunk / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
238                 lseek(h,pos,SEEK_SET);
239                 nread = read(h,oob,oob_len);
240                 if(nread != oob_len)
241                         retval = YAFFS_FAIL;
242         }
243
244         if (ecc_result)
245                 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
246
247         return retval;
248 }
249
250 static int yflash2_EraseBlock(struct yaffs_dev *dev, int block_no)
251 {
252         int i;
253         int h;
254
255         CheckInit();
256
257         if(block_no < 0 || block_no >= filedisk.nBlocks)
258         {
259                 yaffs_trace(YAFFS_TRACE_ALWAYS,"Attempt to erase non-existant block %d",block_no);
260                 return YAFFS_FAIL;
261         }
262         else
263         {
264
265                 u8 pg[PAGE_SIZE];
266                 int syz = PAGE_SIZE;
267                 int pos;
268
269                 memset(pg,0xff,syz);
270
271
272                 h = filedisk.handle[(block_no / ( BLOCKS_PER_HANDLE))];
273                 lseek(h,((block_no % BLOCKS_PER_HANDLE) * dev->param.chunks_per_block) * PAGE_SIZE,SEEK_SET);
274                 for(i = 0; i < dev->param.chunks_per_block; i++)
275                 {
276                         write(h,pg,PAGE_SIZE);
277                 }
278                 pos = lseek(h, 0,SEEK_CUR);
279
280                 return YAFFS_OK;
281         }
282 }
283
284 static int yflash2_MarkBad(struct yaffs_dev *dev, int block_no)
285 {
286         int written;
287         int h;
288
289         struct yaffs_packed_tags2 pt;
290
291         CheckInit();
292
293         memset(&pt,0,sizeof(pt));
294         h = filedisk.handle[(block_no / ( BLOCKS_PER_HANDLE))];
295         lseek(h,((block_no % BLOCKS_PER_HANDLE) * dev->param.chunks_per_block) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
296         written = write(h,&pt,sizeof(pt));
297
298         if(written != sizeof(pt))
299                 return YAFFS_FAIL;
300
301         return YAFFS_OK;
302
303 }
304
305 static int yflash2_CheckBad(struct yaffs_dev *dev, int block_no)
306 {
307         (void) dev;
308         (void) block_no;
309
310         return YAFFS_OK;
311 }
312
313 static int yflash2_Initialise(struct yaffs_dev *dev)
314 {
315         (void) dev;
316
317         CheckInit();
318
319         return YAFFS_OK;
320 }
321
322 void yflash2_install_drv(struct yaffs_dev *dev)
323 {
324         struct yaffs_param *param = &dev->param;
325
326         param->drv_write_chunk_fn = yflash2_WriteChunk;
327         param->drv_read_chunk_fn = yflash2_ReadChunk;
328         param->drv_erase_fn = yflash2_EraseBlock;
329         param->drv_mark_bad_fn = yflash2_MarkBad;
330         param->drv_check_bad_fn = yflash2_CheckBad;
331         param->drv_initialise_fn = yflash2_Initialise;
332
333
334         param->total_bytes_per_chunk = 2048;
335         param->chunks_per_block = 64;
336         param->start_block = 0;
337         param->end_block = yflash2_GetNumberOfBlocks()-1;
338         param->is_yaffs2 = 1;
339         param->use_nand_ecc=1;
340 }