*** empty log message ***
[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.1 2004-11-03 08:14:07 charles 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 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND);
22 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare);
23 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare);
24 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
25
26
27
28 static const char yaffs_countBitsTable[256] =
29 {
30 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
31 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
32 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
33 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
34 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
35 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
36 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
37 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
38 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
39 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
40 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
41 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
42 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
43 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
44 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
45 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
46 };
47
48 static int yaffs_CountBits(__u8 x)
49 {
50         int retVal;
51         retVal = yaffs_countBitsTable[x];
52         return retVal;
53 }
54
55
56 /////////////// Tags ECC calculations ///////////////////
57
58 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
59 {
60         yaffs_ECCCalculate(data , spare->ecc1);
61         yaffs_ECCCalculate(&data[256] , spare->ecc2);
62 }
63
64 void yaffs_CalcTagsECC(yaffs_Tags *tags)
65 {
66         // Calculate an ecc
67
68         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
69         unsigned  i,j;
70         unsigned  ecc = 0;
71         unsigned bit = 0;
72
73         tags->ecc = 0;
74
75         for(i = 0; i < 8; i++)
76         {
77                 for(j = 1; j &0xff; j<<=1)
78                 {
79                         bit++;
80                         if(b[i] & j)
81                         {
82                                 ecc ^= bit;
83                         }
84                 }
85         }
86
87         tags->ecc = ecc;
88
89
90 }
91
92 int  yaffs_CheckECCOnTags(yaffs_Tags *tags)
93 {
94         unsigned ecc = tags->ecc;
95
96         yaffs_CalcTagsECC(tags);
97
98         ecc ^= tags->ecc;
99         
100         if(ecc && ecc <= 64)
101         {
102                 // TODO: Handle the failure better. Retire?
103                 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
104
105                 ecc--;
106
107                 b[ecc / 8] ^= (1 << (ecc & 7));
108
109                 // Now recvalc the ecc
110                 yaffs_CalcTagsECC(tags);
111
112                 return 1; // recovered error
113         }
114         else if(ecc)
115         {
116                 // Wierd ecc failure value
117                 // TODO Need to do somethiong here
118                 return -1; //unrecovered error
119         }
120
121         return 0;
122 }
123
124 //////////////////////////// Tags ///////////////////////////////////////
125
126 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
127 {
128         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
129
130         yaffs_CalcTagsECC(tagsPtr);
131         
132         sparePtr->tagByte0 = tu->asBytes[0];
133         sparePtr->tagByte1 = tu->asBytes[1];
134         sparePtr->tagByte2 = tu->asBytes[2];
135         sparePtr->tagByte3 = tu->asBytes[3];
136         sparePtr->tagByte4 = tu->asBytes[4];
137         sparePtr->tagByte5 = tu->asBytes[5];
138         sparePtr->tagByte6 = tu->asBytes[6];
139         sparePtr->tagByte7 = tu->asBytes[7];
140 }
141
142 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
143 {
144         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
145         int result;
146
147         tu->asBytes[0]= sparePtr->tagByte0;
148         tu->asBytes[1]= sparePtr->tagByte1;
149         tu->asBytes[2]= sparePtr->tagByte2;
150         tu->asBytes[3]= sparePtr->tagByte3;
151         tu->asBytes[4]= sparePtr->tagByte4;
152         tu->asBytes[5]= sparePtr->tagByte5;
153         tu->asBytes[6]= sparePtr->tagByte6;
154         tu->asBytes[7]= sparePtr->tagByte7;
155         
156         result =  yaffs_CheckECCOnTags(tagsPtr);
157         if(result> 0)
158         {
159                 dev->tagsEccFixed++;
160         }
161         else if(result <0)
162         {
163                 dev->tagsEccUnfixed++;
164         }
165 }
166
167 static void yaffs_SpareInitialise(yaffs_Spare *spare)
168 {
169         memset(spare,0xFF,sizeof(yaffs_Spare));
170 }
171
172
173
174
175 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
176 {
177         if(chunkInNAND < dev->startBlock * dev->nChunksPerBlock)
178         {
179                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));
180                 return YAFFS_FAIL;
181         }
182
183         dev->nPageWrites++;
184         return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);
185 }
186
187
188
189 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
190                                                         int chunkInNAND, 
191                                                         __u8 *data, 
192                                                         yaffs_Spare *spare,
193                                                         yaffs_ECCResult *eccResult,
194                                                         int doErrorCorrection)
195 {
196         int retVal;
197         yaffs_Spare localSpare;
198
199         dev->nPageReads++;
200         
201         
202
203         
204         if(!spare && data)
205         {
206                 // If we don't have a real spare, then we use a local one.
207                 // Need this for the calculation of the ecc
208                 spare = &localSpare;
209         }
210         
211
212         if(!dev->useNANDECC)
213         {
214                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,spare);
215                 if(data && doErrorCorrection)
216                 {
217                         // Do ECC correction
218                         //Todo handle any errors
219                 int eccResult1,eccResult2;
220                 __u8 calcEcc[3];
221                 
222                         yaffs_ECCCalculate(data,calcEcc);
223                         eccResult1 = yaffs_ECCCorrect (data,spare->ecc1, calcEcc);
224                         yaffs_ECCCalculate(&data[256],calcEcc);
225                         eccResult2 = yaffs_ECCCorrect(&data[256],spare->ecc2, calcEcc);
226
227                         if(eccResult1>0)
228                         {
229                                 T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
230                                 dev->eccFixed++;
231                         }
232                         else if(eccResult1<0)
233                         {
234                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
235                                 dev->eccUnfixed++;
236                         }
237
238                         if(eccResult2>0)
239                         {
240                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
241                                 dev->eccFixed++;
242                         }
243                         else if(eccResult2<0)
244                         {
245                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
246                                 dev->eccUnfixed++;
247                         }
248
249                         if(eccResult1 || eccResult2)
250                         {
251                                 // Hoosterman, we had a data problem on this page
252                                 yaffs_HandleReadDataError(dev,chunkInNAND);
253                         }
254                         
255                         if(eccResult1 < 0 || eccResult2 < 0) 
256                                 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
257                         else if(eccResult1 > 0 || eccResult2 > 0)
258                                 *eccResult = YAFFS_ECC_RESULT_FIXED;
259                         else
260                                 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
261                 }
262         }
263         else
264         {
265         // Must allocate enough memory for spare+2*sizeof(int) for ecc results from device.
266         struct yaffs_NANDSpare nspare;
267                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare*)&nspare);
268                 memcpy (spare, &nspare, sizeof(yaffs_Spare));
269                 if(data && doErrorCorrection)
270                 {
271                         if(nspare.eccres1>0)
272                         {
273                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
274                         }
275                         else if(nspare.eccres1<0)
276                         {
277                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
278                         }
279
280                         if(nspare.eccres2>0)
281                         {
282                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
283                         }
284                         else if(nspare.eccres2<0)
285                         {
286                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
287                         }
288
289                         if(nspare.eccres1 || nspare.eccres2)
290                         {
291                                 // Hoosterman, we had a data problem on this page
292                                 yaffs_HandleReadDataError(dev,chunkInNAND);
293                         }
294                         
295                         if(nspare.eccres1 < 0 || nspare.eccres2 < 0) 
296                                 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
297                         else if(nspare.eccres1 > 0 || nspare.eccres2 > 0)
298                                 *eccResult = YAFFS_ECC_RESULT_FIXED;
299                         else
300                                 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
301
302
303                 }
304         }
305         return retVal;
306 }
307
308
309
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
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 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
484 {
485 }
486
487 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
488 {
489 }
490
491 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare)
492 {
493 }
494
495 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
496 {
497         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
498
499         // Mark the block for retirement
500         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
501         // Delete the chunk
502         yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
503 }
504
505
506
507
508 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1)
509 {
510
511
512         if( memcmp(d0,d1,YAFFS_BYTES_PER_CHUNK) != 0 ||
513                 s0->tagByte0 != s1->tagByte0 ||
514                 s0->tagByte1 != s1->tagByte1 ||
515                 s0->tagByte2 != s1->tagByte2 ||
516                 s0->tagByte3 != s1->tagByte3 ||
517                 s0->tagByte4 != s1->tagByte4 ||
518                 s0->tagByte5 != s1->tagByte5 ||
519                 s0->tagByte6 != s1->tagByte6 ||
520                 s0->tagByte7 != s1->tagByte7 ||
521                 s0->ecc1[0]  != s1->ecc1[0]  ||
522                 s0->ecc1[1]  != s1->ecc1[1]  ||
523                 s0->ecc1[2]  != s1->ecc1[2]  ||
524                 s0->ecc2[0]  != s1->ecc2[0]  ||
525                 s0->ecc2[1]  != s1->ecc2[1]  ||
526                 s0->ecc2[2]  != s1->ecc2[2] )
527                 {
528                         return 0;
529                 }
530
531         return 1;
532 }
533
534 #if 0
535 typedef struct
536 {
537
538         unsigned validMarker0;
539         unsigned chunkUsed;                 //  Status of the chunk: used or unused
540         unsigned objectId;                      // If 0 then this is not part of an object (unused)
541         unsigned chunkId;                       // If 0 then this is a header
542         unsigned byteCount;                 // Only valid for data chunks
543         // The following stuff only has meaning when we read
544         yaffs_ECCResult eccResult;  // Only valid when we read.
545         unsigned blockBad;                      // Only valid on reading
546
547         // YAFFS 1 stuff        
548         unsigned chunkDeleted;          // The chunk is marked deleted
549         unsigned serialNumber;          // Yaffs1 2-bit serial number
550         
551         // YAFFS2 stuff
552         unsigned sequenceNumber;        // The sequence number of this block
553
554         unsigned validMarker1;
555
556 } yaffs_ExtendedTags;
557
558
559 typedef struct
560 {   
561     unsigned chunkId:20;
562     unsigned serialNumber:2;
563     unsigned byteCount:10;
564     unsigned objectId:18;
565     unsigned ecc:12;
566     unsigned unusedStuff:2;
567 } yaffs_Tags;
568
569
570 #endif
571
572 int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *eTags)
573 {
574         yaffs_Spare spare;
575         yaffs_Tags tags;        
576         
577         yaffs_SpareInitialise(&spare);
578         
579         if(eTags->chunkDeleted)
580         {
581                 spare.pageStatus = 0;
582         }
583         else
584         {
585                 tags.objectId = eTags->objectId;
586                 tags.chunkId = eTags->chunkId;
587                 tags.byteCount = eTags->byteCount;
588                 tags.serialNumber = eTags->serialNumber;
589                 
590                  yaffs_LoadTagsIntoSpare(&spare,&tags);
591                 
592         }
593         
594         return yaffs_WriteChunkToNAND(dev,chunkInNAND,data,&spare);
595 }
596
597
598 int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *eTags)
599 {
600
601         yaffs_Spare spare;
602         yaffs_Tags tags;
603         yaffs_ECCResult eccResult;
604         
605         if(yaffs_ReadChunkFromNAND(dev,chunkInNAND,data,&spare,&eccResult,1))
606         {
607                  int deleted = (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
608                         
609                  yaffs_GetTagsFromSpare(dev,&spare,&tags);
610                  
611                  eTags->chunkDeleted = deleted;
612                  eTags->objectId = tags.objectId;
613                  eTags->chunkId = tags.chunkId;
614                  eTags->byteCount = tags.byteCount;
615                  eTags->serialNumber = tags.serialNumber;
616                  eTags->eccResult = eccResult;
617                  eTags->blockBad = 0; // We're reading it therefore it is not a bad block
618                  
619                  return YAFFS_OK;
620         }
621         else
622         { 
623                 return YAFFS_FAIL;
624         }
625 }
626
627 int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockInNAND)
628 {
629
630         yaffs_Spare spare;
631
632         memset(&spare, 0xff,sizeof(yaffs_Spare));
633
634         spare.blockStatus = 0;
635
636         yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL , &spare);
637         yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1, NULL , &spare);
638         
639         return YAFFS_OK;
640         
641 }
642
643
644 int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
645 {
646      
647      yaffs_Spare spare0,spare1;
648      static yaffs_Spare spareFF;
649      static int init;
650      yaffs_ECCResult dummy;
651      
652      if(!init)
653      {
654              memset(&spareFF,0xFF,sizeof(spareFF));
655              init = 1;
656      }
657      
658      *sequenceNumber = 0;
659      
660      yaffs_ReadChunkFromNAND(dev,blockNo * dev->nChunksPerBlock,NULL,&spare0,&dummy,1);
661      yaffs_ReadChunkFromNAND(dev,blockNo * dev->nChunksPerBlock + 1,NULL,&spare1,&dummy,1);
662      
663      if(yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
664         *state = YAFFS_BLOCK_STATE_DEAD;
665      else if(memcmp(&spareFF,&spare0,sizeof(spareFF)) == 0)
666          *state = YAFFS_BLOCK_STATE_EMPTY;
667      else
668          *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;  
669
670      return YAFFS_OK;
671 }
672