More stuff to fix shrinkHeader handling problem
[yaffs2.git] / yaffs_tagscompat.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  * yaffs_tagscompat.h: Tags compatability layer to use YAFFS1 formatted NAND.
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
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  * $Id: yaffs_tagscompat.c,v 1.3 2005-07-31 06:47:12 marty Exp $
14  */
15
16 #include "yaffs_guts.h"
17 #include "yaffs_tagscompat.h"
18 #include "yaffs_ecc.h"
19
20 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
21 #ifdef NOTYET
22 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND);
23 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare);
24 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare);
25 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
26 #endif
27
28
29 static const char yaffs_countBitsTable[256] =
30 {
31 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
32 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
33 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
34 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
35 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
36 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
37 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
38 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
39 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
40 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
41 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
42 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
43 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
44 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
45 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
46 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
47 };
48
49 static int yaffs_CountBits(__u8 x)
50 {
51         int retVal;
52         retVal = yaffs_countBitsTable[x];
53         return retVal;
54 }
55
56
57 /////////////// Tags ECC calculations ///////////////////
58
59 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
60 {
61         yaffs_ECCCalculate(data , spare->ecc1);
62         yaffs_ECCCalculate(&data[256] , spare->ecc2);
63 }
64
65 void yaffs_CalcTagsECC(yaffs_Tags *tags)
66 {
67         // Calculate an ecc
68
69         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
70         unsigned  i,j;
71         unsigned  ecc = 0;
72         unsigned bit = 0;
73
74         tags->ecc = 0;
75
76         for(i = 0; i < 8; i++)
77         {
78                 for(j = 1; j &0xff; j<<=1)
79                 {
80                         bit++;
81                         if(b[i] & j)
82                         {
83                                 ecc ^= bit;
84                         }
85                 }
86         }
87
88         tags->ecc = ecc;
89
90
91 }
92
93 int  yaffs_CheckECCOnTags(yaffs_Tags *tags)
94 {
95         unsigned ecc = tags->ecc;
96
97         yaffs_CalcTagsECC(tags);
98
99         ecc ^= tags->ecc;
100         
101         if(ecc && ecc <= 64)
102         {
103                 // TODO: Handle the failure better. Retire?
104                 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
105
106                 ecc--;
107
108                 b[ecc / 8] ^= (1 << (ecc & 7));
109
110                 // Now recvalc the ecc
111                 yaffs_CalcTagsECC(tags);
112
113                 return 1; // recovered error
114         }
115         else if(ecc)
116         {
117                 // Wierd ecc failure value
118                 // TODO Need to do somethiong here
119                 return -1; //unrecovered error
120         }
121
122         return 0;
123 }
124
125 //////////////////////////// Tags ///////////////////////////////////////
126
127 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
128 {
129         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
130
131         yaffs_CalcTagsECC(tagsPtr);
132         
133         sparePtr->tagByte0 = tu->asBytes[0];
134         sparePtr->tagByte1 = tu->asBytes[1];
135         sparePtr->tagByte2 = tu->asBytes[2];
136         sparePtr->tagByte3 = tu->asBytes[3];
137         sparePtr->tagByte4 = tu->asBytes[4];
138         sparePtr->tagByte5 = tu->asBytes[5];
139         sparePtr->tagByte6 = tu->asBytes[6];
140         sparePtr->tagByte7 = tu->asBytes[7];
141 }
142
143 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
144 {
145         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
146         int result;
147
148         tu->asBytes[0]= sparePtr->tagByte0;
149         tu->asBytes[1]= sparePtr->tagByte1;
150         tu->asBytes[2]= sparePtr->tagByte2;
151         tu->asBytes[3]= sparePtr->tagByte3;
152         tu->asBytes[4]= sparePtr->tagByte4;
153         tu->asBytes[5]= sparePtr->tagByte5;
154         tu->asBytes[6]= sparePtr->tagByte6;
155         tu->asBytes[7]= sparePtr->tagByte7;
156         
157         result =  yaffs_CheckECCOnTags(tagsPtr);
158         if(result> 0)
159         {
160                 dev->tagsEccFixed++;
161         }
162         else if(result <0)
163         {
164                 dev->tagsEccUnfixed++;
165         }
166 }
167
168 static void yaffs_SpareInitialise(yaffs_Spare *spare)
169 {
170         memset(spare,0xFF,sizeof(yaffs_Spare));
171 }
172
173
174
175
176 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
177 {
178         if(chunkInNAND < dev->startBlock * dev->nChunksPerBlock)
179         {
180                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));
181                 return YAFFS_FAIL;
182         }
183
184         dev->nPageWrites++;
185         return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);
186 }
187
188
189
190 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
191                                                         int chunkInNAND, 
192                                                         __u8 *data, 
193                                                         yaffs_Spare *spare,
194                                                         yaffs_ECCResult *eccResult,
195                                                         int doErrorCorrection)
196 {
197         int retVal;
198         yaffs_Spare localSpare;
199
200         dev->nPageReads++;
201         
202         
203
204         
205         if(!spare && data)
206         {
207                 // If we don't have a real spare, then we use a local one.
208                 // Need this for the calculation of the ecc
209                 spare = &localSpare;
210         }
211         
212
213         if(!dev->useNANDECC)
214         {
215                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,spare);
216                 if(data && doErrorCorrection)
217                 {
218                         // Do ECC correction
219                         //Todo handle any errors
220                 int eccResult1,eccResult2;
221                 __u8 calcEcc[3];
222                 
223                         yaffs_ECCCalculate(data,calcEcc);
224                         eccResult1 = yaffs_ECCCorrect (data,spare->ecc1, calcEcc);
225                         yaffs_ECCCalculate(&data[256],calcEcc);
226                         eccResult2 = yaffs_ECCCorrect(&data[256],spare->ecc2, calcEcc);
227
228                         if(eccResult1>0)
229                         {
230                                 T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
231                                 dev->eccFixed++;
232                         }
233                         else if(eccResult1<0)
234                         {
235                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
236                                 dev->eccUnfixed++;
237                         }
238
239                         if(eccResult2>0)
240                         {
241                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
242                                 dev->eccFixed++;
243                         }
244                         else if(eccResult2<0)
245                         {
246                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
247                                 dev->eccUnfixed++;
248                         }
249
250                         if(eccResult1 || eccResult2)
251                         {
252                                 // Hoosterman, we had a data problem on this page
253                                 yaffs_HandleReadDataError(dev,chunkInNAND);
254                         }
255                         
256                         if(eccResult1 < 0 || eccResult2 < 0) 
257                                 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
258                         else if(eccResult1 > 0 || eccResult2 > 0)
259                                 *eccResult = YAFFS_ECC_RESULT_FIXED;
260                         else
261                                 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
262                 }
263         }
264         else
265         {
266         // Must allocate enough memory for spare+2*sizeof(int) for ecc results from device.
267         struct yaffs_NANDSpare nspare;
268                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare*)&nspare);
269                 memcpy (spare, &nspare, sizeof(yaffs_Spare));
270                 if(data && doErrorCorrection)
271                 {
272                         if(nspare.eccres1>0)
273                         {
274                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
275                         }
276                         else if(nspare.eccres1<0)
277                         {
278                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
279                         }
280
281                         if(nspare.eccres2>0)
282                         {
283                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
284                         }
285                         else if(nspare.eccres2<0)
286                         {
287                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
288                         }
289
290                         if(nspare.eccres1 || nspare.eccres2)
291                         {
292                                 // Hoosterman, we had a data problem on this page
293                                 yaffs_HandleReadDataError(dev,chunkInNAND);
294                         }
295                         
296                         if(nspare.eccres1 < 0 || nspare.eccres2 < 0) 
297                                 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
298                         else if(nspare.eccres1 > 0 || nspare.eccres2 > 0)
299                                 *eccResult = YAFFS_ECC_RESULT_FIXED;
300                         else
301                                 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
302
303
304                 }
305         }
306         return retVal;
307 }
308
309 #ifdef NOTYET
310 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
311 {
312
313         static int init = 0;
314         static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
315         static __u8 data[YAFFS_BYTES_PER_CHUNK];
316     // Might as well always allocate the larger size for dev->useNANDECC == true;
317         static __u8 spare[sizeof(struct yaffs_NANDSpare)];
318
319         dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare *)spare);
320
321         if(!init)
322         {
323                 memset(cmpbuf,0xff,YAFFS_BYTES_PER_CHUNK);
324                 init = 1;
325         }
326
327         if(memcmp(cmpbuf,data,YAFFS_BYTES_PER_CHUNK)) return  YAFFS_FAIL;
328         if(memcmp(cmpbuf,spare,16)) return YAFFS_FAIL;
329
330
331         return YAFFS_OK;
332
333 }
334 #endif
335
336 #if 0
337 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
338 {
339         dev->nBlockErasures++;
340         return dev->eraseBlockInNAND(dev,blockInNAND);
341 }
342
343 int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
344 {
345         return dev->initialiseNAND(dev);
346 }
347
348 #endif
349
350 #if 0
351 static int yaffs_WriteNewChunkToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_Spare *spare,int useReserve)
352 {
353         int chunk;
354
355         int writeOk = 1;
356         int attempts = 0;
357
358         unsigned char rbData[YAFFS_BYTES_PER_CHUNK];
359         yaffs_Spare rbSpare;
360
361         do{
362                 chunk = yaffs_AllocateChunk(dev,useReserve);
363
364                 if(chunk >= 0)
365                 {
366
367                         // First check this chunk is erased...
368 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
369                         writeOk = yaffs_CheckChunkErased(dev,chunk);
370 #endif
371                         if(!writeOk)
372                         {
373                                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
374                         }
375                         else
376                         {
377                                 writeOk =  yaffs_WriteChunkToNAND(dev,chunk,data,spare);
378                         }
379                         attempts++;
380                         if(writeOk)
381                         {
382                                 // Readback & verify
383                                 // If verify fails, then delete this chunk and try again
384                                 // To verify we compare everything except the block and
385                                 // page status bytes.
386                                 // NB We check a raw read without ECC correction applied
387                                 yaffs_ReadChunkFromNAND(dev,chunk,rbData,&rbSpare,0);
388
389 #ifndef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
390                                 if(!yaffs_VerifyCompare(data,rbData,spare,&rbSpare))
391                                 {
392                                         // Didn't verify
393                                         T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write verify failed on chunk %d" TENDSTR), chunk));
394
395                                         writeOk = 0;
396                                 }
397 #endif
398
399                         }
400                         if(writeOk)
401                         {
402                                 // Copy the data into the write buffer.
403                                 // NB We do this at the end to prevent duplicates in the case of a write error.
404                                 //Todo
405                                 yaffs_HandleWriteChunkOk(dev,chunk,data,spare);
406                         }
407                         else
408                         {
409                                 yaffs_HandleWriteChunkError(dev,chunk);
410                         }
411                 }
412
413         } while(chunk >= 0 && ! writeOk);
414
415         if(attempts > 1)
416         {
417                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
418                 dev->nRetriedWrites+= (attempts - 1);
419         }
420
421         return chunk;
422 }
423
424 #endif
425
426 ///
427 // Functions for robustisizing
428 //
429 //
430 #if 0
431
432 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
433 {
434         // Ding the blockStatus in the first two pages of the block.
435
436         yaffs_Spare spare;
437
438         memset(&spare, 0xff,sizeof(yaffs_Spare));
439
440         spare.blockStatus = 0;
441
442         // TODO change this retirement marking for other NAND types
443         yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL , &spare);
444         yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1, NULL , &spare);
445
446         yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
447         dev->nRetiredBlocks++;
448 }
449
450 #endif
451
452 #if 0
453 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
454 {
455         dev->doingBufferedBlockRewrite = 1;
456         //
457         //      Remove erased chunks
458         //  Rewrite existing chunks to a new block
459         //      Set current write block to the new block
460
461         dev->doingBufferedBlockRewrite = 0;
462
463         return 1;
464 }
465
466 #endif
467
468 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
469 {
470         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
471
472         // Mark the block for retirement
473         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
474         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
475
476
477         //TODO
478         // Just do a garbage collection on the affected block then retire the block
479         // NB recursion
480 }
481
482
483 #ifdef NOTYET
484 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
485 {
486 }
487
488 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
489 {
490 }
491
492 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare)
493 {
494 }
495
496 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
497 {
498         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
499
500         // Mark the block for retirement
501         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
502         // Delete the chunk
503         yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
504 }
505
506
507
508
509 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1)
510 {
511
512
513         if( memcmp(d0,d1,YAFFS_BYTES_PER_CHUNK) != 0 ||
514                 s0->tagByte0 != s1->tagByte0 ||
515                 s0->tagByte1 != s1->tagByte1 ||
516                 s0->tagByte2 != s1->tagByte2 ||
517                 s0->tagByte3 != s1->tagByte3 ||
518                 s0->tagByte4 != s1->tagByte4 ||
519                 s0->tagByte5 != s1->tagByte5 ||
520                 s0->tagByte6 != s1->tagByte6 ||
521                 s0->tagByte7 != s1->tagByte7 ||
522                 s0->ecc1[0]  != s1->ecc1[0]  ||
523                 s0->ecc1[1]  != s1->ecc1[1]  ||
524                 s0->ecc1[2]  != s1->ecc1[2]  ||
525                 s0->ecc2[0]  != s1->ecc2[0]  ||
526                 s0->ecc2[1]  != s1->ecc2[1]  ||
527                 s0->ecc2[2]  != s1->ecc2[2] )
528                 {
529                         return 0;
530                 }
531
532         return 1;
533 }
534 #endif /* NOTYET */
535
536 #if 0
537 typedef struct
538 {
539
540         unsigned validMarker0;
541         unsigned chunkUsed;                 //  Status of the chunk: used or unused
542         unsigned objectId;                      // If 0 then this is not part of an object (unused)
543         unsigned chunkId;                       // If 0 then this is a header
544         unsigned byteCount;                 // Only valid for data chunks
545         // The following stuff only has meaning when we read
546         yaffs_ECCResult eccResult;  // Only valid when we read.
547         unsigned blockBad;                      // Only valid on reading
548
549         // YAFFS 1 stuff        
550         unsigned chunkDeleted;          // The chunk is marked deleted
551         unsigned serialNumber;          // Yaffs1 2-bit serial number
552         
553         // YAFFS2 stuff
554         unsigned sequenceNumber;        // The sequence number of this block
555
556         unsigned validMarker1;
557
558 } yaffs_ExtendedTags;
559
560
561 typedef struct
562 {   
563     unsigned chunkId:20;
564     unsigned serialNumber:2;
565     unsigned byteCount:10;
566     unsigned objectId:18;
567     unsigned ecc:12;
568     unsigned unusedStuff:2;
569 } yaffs_Tags;
570
571
572 #endif
573
574 int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *eTags)
575 {
576         yaffs_Spare spare;
577         yaffs_Tags tags;        
578         
579         yaffs_SpareInitialise(&spare);
580         
581         if(eTags->chunkDeleted)
582         {
583                 spare.pageStatus = 0;
584         }
585         else
586         {
587                 tags.objectId = eTags->objectId;
588                 tags.chunkId = eTags->chunkId;
589                 tags.byteCount = eTags->byteCount;
590                 tags.serialNumber = eTags->serialNumber;
591                 
592 // NCB
593                 if (!dev->useNANDECC && data)
594                 {
595                     yaffs_CalcECC(data,&spare);
596                 }
597
598 // /NCB
599                  yaffs_LoadTagsIntoSpare(&spare,&tags);
600                 
601         }
602         
603         return yaffs_WriteChunkToNAND(dev,chunkInNAND,data,&spare);
604 }
605
606
607 int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *eTags)
608 {
609
610         yaffs_Spare spare;
611         yaffs_Tags tags;
612         yaffs_ECCResult eccResult;
613         
614 // NCB
615      static yaffs_Spare spareFF;
616      static int init;
617      
618      if(!init)
619      {
620              memset(&spareFF,0xFF,sizeof(spareFF));
621              init = 1;
622      }
623 // /NCB
624         if(yaffs_ReadChunkFromNAND(dev,chunkInNAND,data,&spare,&eccResult,1))
625         {
626 // added NCB - eTags may be NULL
627                 if (eTags) {
628
629                  int deleted = (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
630                         
631                  yaffs_GetTagsFromSpare(dev,&spare,&tags);
632                  
633                  eTags->chunkDeleted = deleted;
634                  eTags->objectId = tags.objectId;
635                  eTags->chunkId = tags.chunkId;
636                  eTags->byteCount = tags.byteCount;
637                  eTags->serialNumber = tags.serialNumber;
638                  eTags->eccResult = eccResult;
639                  eTags->blockBad = 0; // We're reading it therefore it is not a bad block
640                  
641 // NCB added 18/2/2005
642                  eTags->chunkUsed = (memcmp(&spareFF,&spare,sizeof(spareFF)) != 0) ? 1:0;
643                 }
644                  
645                  return YAFFS_OK;
646         }
647         else
648         { 
649                 return YAFFS_FAIL;
650         }
651 }
652
653 int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockInNAND)
654 {
655
656         yaffs_Spare spare;
657
658         memset(&spare, 0xff,sizeof(yaffs_Spare));
659
660         spare.blockStatus = 0;
661
662         yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL , &spare);
663         yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1, NULL , &spare);
664         
665         return YAFFS_OK;
666         
667 }
668
669
670 int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
671 {
672      
673      yaffs_Spare spare0,spare1;
674      static yaffs_Spare spareFF;
675      static int init;
676      yaffs_ECCResult dummy;
677      
678      if(!init)
679      {
680              memset(&spareFF,0xFF,sizeof(spareFF));
681              init = 1;
682      }
683      
684      *sequenceNumber = 0;
685      
686      yaffs_ReadChunkFromNAND(dev,blockNo * dev->nChunksPerBlock,NULL,&spare0,&dummy,1);
687      yaffs_ReadChunkFromNAND(dev,blockNo * dev->nChunksPerBlock + 1,NULL,&spare1,&dummy,1);
688      
689      if(yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
690         *state = YAFFS_BLOCK_STATE_DEAD;
691      else if(memcmp(&spareFF,&spare0,sizeof(spareFF)) == 0)
692          *state = YAFFS_BLOCK_STATE_EMPTY;
693      else
694          *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;  
695
696      return YAFFS_OK;
697 }
698