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