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