1e2cd6b3ef59156d9b4fbb26824414415ebf1a2e
[yaffs2.git] / direct / yaffs_fileem2k.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2007 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 const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.12 2007-02-14 01:09:06 wookey Exp $";
20
21
22 #include "yportenv.h"
23
24 #include "yaffs_flashif.h"
25 #include "yaffs_guts.h"
26 #include "devextras.h"
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h> 
32
33 #include "yaffs_fileem2k.h"
34 #include "yaffs_packedtags2.h"
35
36 //#define SIMULATE_FAILURES
37
38 typedef struct 
39 {
40         __u8 data[PAGE_SIZE]; // Data + spare
41 } yflash_Page;
42
43 typedef struct
44 {
45         yflash_Page page[PAGES_PER_BLOCK]; // The pages in the block
46         
47 } yflash_Block;
48
49
50
51 #define MAX_HANDLES 20
52 #define BLOCKS_PER_HANDLE 8000
53
54 typedef struct
55 {
56         int handle[MAX_HANDLES];
57         int nBlocks;
58 } yflash_Device;
59
60 static yflash_Device filedisk;
61
62 int yaffs_testPartialWrite = 0;
63
64
65
66
67 static __u8 localBuffer[PAGE_SIZE];
68
69 static char *NToName(char *buf,int n)
70 {
71         sprintf(buf,"emfile%d",n);
72         return buf;
73 }
74
75 static char dummyBuffer[BLOCK_SIZE];
76
77 static int GetBlockFileHandle(int n)
78 {
79         int h;
80         int requiredSize;
81         
82         char name[40];
83         NToName(name,n);
84         int fSize;
85         int i;
86         
87         h =  open(name, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
88         if(h >= 0){
89                 fSize = lseek(h,0,SEEK_END);
90                 requiredSize = BLOCKS_PER_HANDLE * BLOCK_SIZE;
91                 if(fSize < requiredSize){
92                    for(i = 0; i < BLOCKS_PER_HANDLE; i++)
93                         if(write(h,dummyBuffer,BLOCK_SIZE) != BLOCK_SIZE)
94                                 return -1;
95                         
96                 }
97         }
98         
99         return h;
100
101 }
102
103 static int  CheckInit(void)
104 {
105         static int initialised = 0;
106         int h;
107         int i;
108
109         
110         off_t fSize;
111         off_t requiredSize;
112         int written;
113         int blk;
114         
115         yflash_Page p;
116         
117         if(initialised) 
118         {
119                 return YAFFS_OK;
120         }
121
122         initialised = 1;
123         
124         memset(dummyBuffer,0xff,sizeof(dummyBuffer));
125         
126         
127         filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
128
129         for(i = 0; i <  MAX_HANDLES; i++)
130                 filedisk.handle[i] = -1;
131         
132         for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
133                 filedisk.handle[i] = GetBlockFileHandle(i);
134         
135         
136         return 1;
137 }
138
139
140 int yflash_GetNumberOfBlocks(void)
141 {
142         CheckInit();
143         
144         return filedisk.nBlocks;
145 }
146
147 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
148 {
149         int written;
150         int pos;
151         int h;
152         int i;
153         int nRead;
154         int error;
155         
156         T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
157
158         CheckInit();
159         
160         
161         
162         if(data)
163         {
164                 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
165                 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
166                 
167                 lseek(h,pos,SEEK_SET);
168                 nRead =  read(h, localBuffer,dev->nDataBytesPerChunk);
169                 for(i = error = 0; i < dev->nDataBytesPerChunk && !error; i++){
170                         if(localBuffer[i] != 0xFF){
171                                 printf("nand simulation: chunk %d data byte %d was %0x2\n",
172                                         chunkInNAND,i,localBuffer[i]);
173                                 error = 1;
174                         }
175                 }
176                 
177                 for(i = 0; i < dev->nDataBytesPerChunk; i++)
178                   localBuffer[i] &= data[i];
179                   
180                 if(memcmp(localBuffer,data,dev->nDataBytesPerChunk))
181                         printf("nand simulator: data does not match\n");
182                         
183                 lseek(h,pos,SEEK_SET);
184                 written = write(h,localBuffer,dev->nDataBytesPerChunk);
185                 
186                 if(yaffs_testPartialWrite){
187                         close(h);
188                         exit(1);
189                 }
190                 
191 #ifdef SIMULATE_FAILURES
192                         if((chunkInNAND >> 6) == 100) 
193                           written = 0;
194
195                         if((chunkInNAND >> 6) == 110) 
196                           written = 0;
197 #endif
198
199
200                 if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
201         }
202         
203         if(tags)
204         {
205                 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
206                 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
207                 
208                 lseek(h,pos,SEEK_SET);
209
210                 if( 0 && dev->isYaffs2)
211                 {
212                         
213                         written = write(h,tags,sizeof(yaffs_ExtendedTags));
214                         if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
215                 }
216                 else
217                 {
218                         yaffs_PackedTags2 pt;
219                         yaffs_PackTags2(&pt,tags);
220                         __u8 * ptab = (__u8 *)&pt;
221
222                         nRead = read(h,localBuffer,sizeof(pt));
223                         for(i = error = 0; i < sizeof(pt) && !error; i++){
224                                 if(localBuffer[i] != 0xFF){
225                                         printf("nand simulation: chunk %d oob byte %d was %0x2\n",
226                                                 chunkInNAND,i,localBuffer[i]);
227                                                 error = 1;
228                                 }
229                         }
230                 
231                         for(i = 0; i < sizeof(pt); i++)
232                           localBuffer[i] &= ptab[i];
233                          
234                         if(memcmp(localBuffer,&pt,sizeof(pt)))
235                                 printf("nand sim: tags corruption\n");
236                                 
237                         lseek(h,pos,SEEK_SET);
238                         
239                         written = write(h,localBuffer,sizeof(pt));
240                         if(written != sizeof(pt)) return YAFFS_FAIL;
241                 }
242         }
243         
244
245         return YAFFS_OK;        
246
247 }
248
249 int yaffs_CheckAllFF(const __u8 *ptr, int n)
250 {
251         while(n)
252         {
253                 n--;
254                 if(*ptr!=0xFF) return 0;
255                 ptr++;
256         }
257         return 1;
258 }
259
260
261 static int fail300 = 1;
262 static int fail320 = 1;
263
264 static int failRead10 = 2;
265
266 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
267 {
268         int nread;
269         int pos;
270         int h;
271         
272         T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
273         
274         CheckInit();
275         
276         
277         
278         if(data)
279         {
280
281                 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
282                 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];             
283                 lseek(h,pos,SEEK_SET);
284                 nread = read(h,data,dev->nDataBytesPerChunk);
285                 
286                 
287                 if(nread != dev->nDataBytesPerChunk) return YAFFS_FAIL;
288         }
289         
290         if(tags)
291         {
292                 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
293                 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];             
294                 lseek(h,pos,SEEK_SET);
295
296                 if(0 && dev->isYaffs2)
297                 {
298                         nread= read(h,tags,sizeof(yaffs_ExtendedTags));
299                         if(nread != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
300                         if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
301                         {
302                                 yaffs_InitialiseTags(tags);
303                         }
304                         else
305                         {
306                                 tags->chunkUsed = 1;
307                         }
308                 }
309                 else
310                 {
311                         yaffs_PackedTags2 pt;
312                         nread= read(h,&pt,sizeof(pt));
313                         yaffs_UnpackTags2(tags,&pt);
314 #ifdef SIMULATE_FAILURES
315                         if((chunkInNAND >> 6) == 100) {
316                             if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
317                                tags->eccResult = YAFFS_ECC_RESULT_FIXED;
318                                fail300 = 0;
319                             }
320                             
321                         }
322                         if((chunkInNAND >> 6) == 110) {
323                             if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
324                                tags->eccResult = YAFFS_ECC_RESULT_FIXED;
325                                fail320 = 0;
326                             }
327                         }
328 #endif
329                         if(failRead10>0 && chunkInNAND == 10){
330                                 failRead10--;
331                                 nread = 0;
332                         }
333                         
334                         if(nread != sizeof(pt)) return YAFFS_FAIL;
335                 }
336         }
337         
338
339         return YAFFS_OK;        
340
341 }
342
343
344 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
345 {
346         int written;
347         int h;
348         
349         yaffs_PackedTags2 pt;
350
351         CheckInit();
352         
353         memset(&pt,0,sizeof(pt));
354         h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
355         lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
356         written = write(h,&pt,sizeof(pt));
357                 
358         if(written != sizeof(pt)) return YAFFS_FAIL;
359         
360         
361         return YAFFS_OK;
362         
363 }
364
365 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
366 {
367
368         int i;
369         int h;
370                 
371         CheckInit();
372         
373         printf("erase block %d\n",blockNumber);
374         
375         if(blockNumber == 320)
376                 fail320 = 1;
377         
378         if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
379         {
380                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
381                 return YAFFS_FAIL;
382         }
383         else
384         {
385         
386                 __u8 pg[PAGE_SIZE];
387                 int syz = PAGE_SIZE;
388                 int pos;
389                 
390                 memset(pg,0xff,syz);
391                 
392
393                 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
394                 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);               
395                 for(i = 0; i < dev->nChunksPerBlock; i++)
396                 {
397                         write(h,pg,PAGE_SIZE);
398                 }
399                 pos = lseek(h, 0,SEEK_CUR);
400                 
401                 return YAFFS_OK;
402         }
403         
404 }
405
406 int yflash_InitialiseNAND(yaffs_Device *dev)
407 {
408         CheckInit();
409         
410         return YAFFS_OK;
411 }
412
413
414
415
416 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
417 {
418         yaffs_ExtendedTags tags;
419         int chunkNo;
420
421         *sequenceNumber = 0;
422         
423         chunkNo = blockNo * dev->nChunksPerBlock;
424         
425         yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
426         if(tags.blockBad)
427         {
428                 *state = YAFFS_BLOCK_STATE_DEAD;
429         }
430         else if(!tags.chunkUsed)
431         {
432                 *state = YAFFS_BLOCK_STATE_EMPTY;
433         }
434         else if(tags.chunkUsed)
435         {
436                 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
437                 *sequenceNumber = tags.sequenceNumber;
438         }
439         return YAFFS_OK;
440 }
441