Check in inband tags, some extra yaffs direct functions and some other changes
[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.13 2008-05-05 07:58:58 charles 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 i;
107
108         int blk;
109
110         
111         if(initialised) 
112         {
113                 return YAFFS_OK;
114         }
115
116         initialised = 1;
117         
118         memset(dummyBuffer,0xff,sizeof(dummyBuffer));
119         
120         
121         filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
122
123         for(i = 0; i <  MAX_HANDLES; i++)
124                 filedisk.handle[i] = -1;
125         
126         for(i = 0,blk = 0; blk < filedisk.nBlocks; blk+=BLOCKS_PER_HANDLE,i++)
127                 filedisk.handle[i] = GetBlockFileHandle(i);
128         
129         
130         return 1;
131 }
132
133
134 int yflash_GetNumberOfBlocks(void)
135 {
136         CheckInit();
137         
138         return filedisk.nBlocks;
139 }
140
141 int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
142 {
143         int written;
144         int pos;
145         int h;
146         int i;
147         int nRead;
148         int error;
149         
150         T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
151
152         CheckInit();
153         
154         
155         if(dev->inbandTags){
156                 
157                 yaffs_PackedTags2TagsPart * pt2tp;
158                 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
159                 yaffs_PackTags2TagsPart(pt2tp,tags);
160                 
161                 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
162                 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
163                                                         
164                 lseek(h,pos,SEEK_SET);
165                 written = write(h,data,dev->totalBytesPerChunk);
166
167                 
168                 if(yaffs_testPartialWrite){
169                         close(h);
170                         exit(1);
171                 }
172                 
173                 if(written != dev->totalBytesPerChunk) return YAFFS_FAIL;
174
175
176         }
177         
178         else {
179         
180                 if(data)
181                 {
182                         pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
183                         h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
184                 
185                         lseek(h,pos,SEEK_SET);
186                         nRead =  read(h, localBuffer,dev->nDataBytesPerChunk);
187                         for(i = error = 0; i < dev->nDataBytesPerChunk && !error; i++){
188                                 if(localBuffer[i] != 0xFF){
189                                         printf("nand simulation: chunk %d data byte %d was %0x2\n",
190                                                 chunkInNAND,i,localBuffer[i]);
191                                         error = 1;
192                                 }
193                         }
194                 
195                         for(i = 0; i < dev->nDataBytesPerChunk; i++)
196                         localBuffer[i] &= data[i];
197                   
198                         if(memcmp(localBuffer,data,dev->nDataBytesPerChunk))
199                                 printf("nand simulator: data does not match\n");
200                         
201                         lseek(h,pos,SEEK_SET);
202                         written = write(h,localBuffer,dev->nDataBytesPerChunk);
203                 
204                         if(yaffs_testPartialWrite){
205                                 close(h);
206                                 exit(1);
207                         }
208                 
209 #ifdef SIMULATE_FAILURES
210                                 if((chunkInNAND >> 6) == 100) 
211                                 written = 0;
212
213                                 if((chunkInNAND >> 6) == 110) 
214                                 written = 0;
215 #endif
216
217
218                         if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
219                 }
220         
221                 if(tags)
222                 {
223                         pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
224                         h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
225                 
226                         lseek(h,pos,SEEK_SET);
227
228                         if( 0 && dev->isYaffs2)
229                         {
230                         
231                                 written = write(h,tags,sizeof(yaffs_ExtendedTags));
232                                 if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
233                         }
234                         else
235                         {
236                                 yaffs_PackedTags2 pt;
237                                 yaffs_PackTags2(&pt,tags);
238                                 __u8 * ptab = (__u8 *)&pt;
239
240                                 nRead = read(h,localBuffer,sizeof(pt));
241                                 for(i = error = 0; i < sizeof(pt) && !error; i++){
242                                         if(localBuffer[i] != 0xFF){
243                                                 printf("nand simulation: chunk %d oob byte %d was %0x2\n",
244                                                         chunkInNAND,i,localBuffer[i]);
245                                                         error = 1;
246                                         }
247                                 }
248                 
249                                 for(i = 0; i < sizeof(pt); i++)
250                                 localBuffer[i] &= ptab[i];
251                          
252                                 if(memcmp(localBuffer,&pt,sizeof(pt)))
253                                         printf("nand sim: tags corruption\n");
254                                 
255                                 lseek(h,pos,SEEK_SET);
256                         
257                                 written = write(h,localBuffer,sizeof(pt));
258                                 if(written != sizeof(pt)) return YAFFS_FAIL;
259                         }
260                 }
261         
262         }
263         return YAFFS_OK;        
264
265 }
266
267 int yaffs_CheckAllFF(const __u8 *ptr, int n)
268 {
269         while(n)
270         {
271                 n--;
272                 if(*ptr!=0xFF) return 0;
273                 ptr++;
274         }
275         return 1;
276 }
277
278
279 static int fail300 = 1;
280 static int fail320 = 1;
281
282 static int failRead10 = 2;
283
284 int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
285 {
286         int nread;
287         int pos;
288         int h;
289         int localData = 0;
290         int retval = YAFFS_OK;
291         int nRead;
292         
293         T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
294         
295         CheckInit();
296         
297         
298         
299         
300         if(dev->inbandTags){
301                 /* Got to suck the tags out of the data area */
302                 if(!data) {
303                         localData=1;
304                         data = yaffs_GetTempBuffer(dev,__LINE__);
305                 }
306
307                 
308                 yaffs_PackedTags2TagsPart * pt2tp;
309                 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
310
311                 
312                 pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
313                 h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
314                 
315                 lseek(h,pos,SEEK_SET);
316
317                 nRead = read(h, data,dev->totalBytesPerChunk);
318
319                 yaffs_UnpackTags2TagsPart(tags,pt2tp);
320                 
321                 if(nread != dev->totalBytesPerChunk)
322                         retval = YAFFS_FAIL;
323                         
324                 if(localData)
325                         yaffs_ReleaseTempBuffer(dev,data,__LINE__);
326
327
328
329         }
330         
331         else {
332         
333                 if(data)
334                 {
335
336                         pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
337                         h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];             
338                         lseek(h,pos,SEEK_SET);
339                         nread = read(h,data,dev->nDataBytesPerChunk);
340                 
341                 
342                         if(nread != dev->nDataBytesPerChunk) 
343                                 retval = YAFFS_FAIL;
344                 }
345         
346                 if(tags)
347                 {
348                         pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE;
349                         h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];             
350                         lseek(h,pos,SEEK_SET);
351
352                         if(0 && dev->isYaffs2)
353                         {
354                                 nread= read(h,tags,sizeof(yaffs_ExtendedTags));
355                                 if(nread != sizeof(yaffs_ExtendedTags))
356                                          retval =  YAFFS_FAIL;
357                                 if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
358                                 {
359                                         yaffs_InitialiseTags(tags);
360                                 }
361                                 else
362                                 {
363                                         tags->chunkUsed = 1;
364                                 }
365                         }
366                         else
367                         {
368                                 yaffs_PackedTags2 pt;
369                                 nread= read(h,&pt,sizeof(pt));
370                                 yaffs_UnpackTags2(tags,&pt);
371 #ifdef SIMULATE_FAILURES
372                                 if((chunkInNAND >> 6) == 100) {
373                                         if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
374                                                 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
375                                                 fail300 = 0;
376                                         }
377                                 }
378                                 
379                                 if((chunkInNAND >> 6) == 110) {
380                                         if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
381                                                 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
382                                                 fail320 = 0;
383                                         }
384                                 }
385 #endif
386                                 if(failRead10>0 && chunkInNAND == 10){
387                                         failRead10--;
388                                         nread = 0;
389                                 }
390                         
391                                 if(nread != sizeof(pt))
392                                         retval = YAFFS_FAIL;
393                         }
394                 }
395         }
396         
397
398
399         return retval;  
400
401 }
402
403
404 int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
405 {
406         int written;
407         int h;
408         
409         yaffs_PackedTags2 pt;
410
411         CheckInit();
412         
413         memset(&pt,0,sizeof(pt));
414         h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
415         lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
416         written = write(h,&pt,sizeof(pt));
417                 
418         if(written != sizeof(pt)) return YAFFS_FAIL;
419         
420         
421         return YAFFS_OK;
422         
423 }
424
425 int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
426 {
427
428         int i;
429         int h;
430                 
431         CheckInit();
432         
433         printf("erase block %d\n",blockNumber);
434         
435         if(blockNumber == 320)
436                 fail320 = 1;
437         
438         if(blockNumber < 0 || blockNumber >= filedisk.nBlocks)
439         {
440                 T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber));
441                 return YAFFS_FAIL;
442         }
443         else
444         {
445         
446                 __u8 pg[PAGE_SIZE];
447                 int syz = PAGE_SIZE;
448                 int pos;
449                 
450                 memset(pg,0xff,syz);
451                 
452
453                 h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
454                 lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);               
455                 for(i = 0; i < dev->nChunksPerBlock; i++)
456                 {
457                         write(h,pg,PAGE_SIZE);
458                 }
459                 pos = lseek(h, 0,SEEK_CUR);
460                 
461                 return YAFFS_OK;
462         }
463         
464 }
465
466 int yflash_InitialiseNAND(yaffs_Device *dev)
467 {
468         CheckInit();
469         
470         return YAFFS_OK;
471 }
472
473
474
475
476 int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, __u32 *sequenceNumber)
477 {
478         yaffs_ExtendedTags tags;
479         int chunkNo;
480
481         *sequenceNumber = 0;
482         
483         chunkNo = blockNo * dev->nChunksPerBlock;
484         
485         yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
486         if(tags.blockBad)
487         {
488                 *state = YAFFS_BLOCK_STATE_DEAD;
489         }
490         else if(!tags.chunkUsed)
491         {
492                 *state = YAFFS_BLOCK_STATE_EMPTY;
493         }
494         else if(tags.chunkUsed)
495         {
496                 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
497                 *sequenceNumber = tags.sequenceNumber;
498         }
499         return YAFFS_OK;
500 }
501