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