2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
14 #include "yaffs_guts.h"
15 #include "yaffs_tagscompat.h"
16 #include "yaffs_ecc.h"
17 #include "yaffs_getblockinfo.h"
19 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,
24 const yaffs_Spare *spare);
25 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
26 const yaffs_Spare *spare);
27 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND);
30 static const char yaffs_countBitsTable[256] = {
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
49 int yaffs_CountBits(__u8 x)
52 retVal = yaffs_countBitsTable[x];
56 /********** Tags ECC calculations *********/
58 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
60 yaffs_ECCCalculate(data, spare->ecc1);
61 yaffs_ECCCalculate(&data[256], spare->ecc2);
64 void yaffs_CalcTagsECC(yaffs_Tags *tags)
66 /* Calculate an ecc */
68 unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
75 for (i = 0; i < 8; i++) {
76 for (j = 1; j & 0xff; j <<= 1) {
87 int yaffs_CheckECCOnTags(yaffs_Tags *tags)
89 unsigned ecc = tags->ecc;
91 yaffs_CalcTagsECC(tags);
95 if (ecc && ecc <= 64) {
96 /* TODO: Handle the failure better. Retire? */
97 unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
101 b[ecc / 8] ^= (1 << (ecc & 7));
103 /* Now recvalc the ecc */
104 yaffs_CalcTagsECC(tags);
106 return 1; /* recovered error */
108 /* Wierd ecc failure value */
109 /* TODO Need to do somethiong here */
110 return -1; /* unrecovered error */
116 /********** Tags **********/
118 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr,
121 yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
123 yaffs_CalcTagsECC(tagsPtr);
125 sparePtr->tagByte0 = tu->asBytes[0];
126 sparePtr->tagByte1 = tu->asBytes[1];
127 sparePtr->tagByte2 = tu->asBytes[2];
128 sparePtr->tagByte3 = tu->asBytes[3];
129 sparePtr->tagByte4 = tu->asBytes[4];
130 sparePtr->tagByte5 = tu->asBytes[5];
131 sparePtr->tagByte6 = tu->asBytes[6];
132 sparePtr->tagByte7 = tu->asBytes[7];
135 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,
138 yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
141 tu->asBytes[0] = sparePtr->tagByte0;
142 tu->asBytes[1] = sparePtr->tagByte1;
143 tu->asBytes[2] = sparePtr->tagByte2;
144 tu->asBytes[3] = sparePtr->tagByte3;
145 tu->asBytes[4] = sparePtr->tagByte4;
146 tu->asBytes[5] = sparePtr->tagByte5;
147 tu->asBytes[6] = sparePtr->tagByte6;
148 tu->asBytes[7] = sparePtr->tagByte7;
150 result = yaffs_CheckECCOnTags(tagsPtr);
154 dev->tagsEccUnfixed++;
157 static void yaffs_SpareInitialise(yaffs_Spare *spare)
159 memset(spare, 0xFF, sizeof(yaffs_Spare));
162 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
163 int chunkInNAND, const __u8 *data,
166 if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
168 (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
173 return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
176 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
180 yaffs_ECCResult *eccResult,
181 int doErrorCorrection)
184 yaffs_Spare localSpare;
186 if (!spare && data) {
187 /* If we don't have a real spare, then we use a local one. */
188 /* Need this for the calculation of the ecc */
192 if (!dev->useNANDECC) {
193 retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
194 if (data && doErrorCorrection) {
195 /* Do ECC correction */
196 /* Todo handle any errors */
197 int eccResult1, eccResult2;
200 yaffs_ECCCalculate(data, calcEcc);
202 yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
203 yaffs_ECCCalculate(&data[256], calcEcc);
205 yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
207 if (eccResult1 > 0) {
210 ("**>>yaffs ecc error fix performed on chunk %d:0"
211 TENDSTR), chunkInNAND));
213 } else if (eccResult1 < 0) {
216 ("**>>yaffs ecc error unfixed on chunk %d:0"
217 TENDSTR), chunkInNAND));
221 if (eccResult2 > 0) {
224 ("**>>yaffs ecc error fix performed on chunk %d:1"
225 TENDSTR), chunkInNAND));
227 } else if (eccResult2 < 0) {
230 ("**>>yaffs ecc error unfixed on chunk %d:1"
231 TENDSTR), chunkInNAND));
235 if (eccResult1 || eccResult2) {
236 /* We had a data problem on this page */
237 yaffs_HandleReadDataError(dev, chunkInNAND);
240 if (eccResult1 < 0 || eccResult2 < 0)
241 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
242 else if (eccResult1 > 0 || eccResult2 > 0)
243 *eccResult = YAFFS_ECC_RESULT_FIXED;
245 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
248 /* Must allocate enough memory for spare+2*sizeof(int) */
249 /* for ecc results from device. */
250 struct yaffs_NANDSpare nspare;
252 memset(&nspare, 0, sizeof(nspare));
254 retVal = dev->readChunkFromNAND(dev, chunkInNAND, data,
255 (yaffs_Spare *) &nspare);
256 memcpy(spare, &nspare, sizeof(yaffs_Spare));
257 if (data && doErrorCorrection) {
258 if (nspare.eccres1 > 0) {
261 ("**>>mtd ecc error fix performed on chunk %d:0"
262 TENDSTR), chunkInNAND));
263 } else if (nspare.eccres1 < 0) {
266 ("**>>mtd ecc error unfixed on chunk %d:0"
267 TENDSTR), chunkInNAND));
270 if (nspare.eccres2 > 0) {
273 ("**>>mtd ecc error fix performed on chunk %d:1"
274 TENDSTR), chunkInNAND));
275 } else if (nspare.eccres2 < 0) {
278 ("**>>mtd ecc error unfixed on chunk %d:1"
279 TENDSTR), chunkInNAND));
282 if (nspare.eccres1 || nspare.eccres2) {
283 /* We had a data problem on this page */
284 yaffs_HandleReadDataError(dev, chunkInNAND);
287 if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
288 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
289 else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
290 *eccResult = YAFFS_ECC_RESULT_FIXED;
292 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
300 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
304 static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
305 static __u8 data[YAFFS_BYTES_PER_CHUNK];
306 /* Might as well always allocate the larger size for */
307 /* dev->useNANDECC == true; */
308 static __u8 spare[sizeof(struct yaffs_NANDSpare)];
310 dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
313 memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
317 if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
319 if (memcmp(cmpbuf, spare, 16))
328 * Functions for robustisizing
331 static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND)
333 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
335 /* Mark the block for retirement */
336 yaffs_GetBlockInfo(dev, blockInNAND + dev->blockOffset)->needsRetiring = 1;
337 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
338 (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
341 * Just do a garbage collection on the affected block
342 * then retire the block
348 static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND)
352 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
354 const yaffs_Spare *spare)
358 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
359 const yaffs_Spare *spare)
363 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND)
365 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
367 /* Mark the block for retirement */
368 yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
369 /* Delete the chunk */
370 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
373 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 *d1,
374 const yaffs_Spare *s0, const yaffs_Spare *s1)
377 if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
378 s0->tagByte0 != s1->tagByte0 ||
379 s0->tagByte1 != s1->tagByte1 ||
380 s0->tagByte2 != s1->tagByte2 ||
381 s0->tagByte3 != s1->tagByte3 ||
382 s0->tagByte4 != s1->tagByte4 ||
383 s0->tagByte5 != s1->tagByte5 ||
384 s0->tagByte6 != s1->tagByte6 ||
385 s0->tagByte7 != s1->tagByte7 ||
386 s0->ecc1[0] != s1->ecc1[0] ||
387 s0->ecc1[1] != s1->ecc1[1] ||
388 s0->ecc1[2] != s1->ecc1[2] ||
389 s0->ecc2[0] != s1->ecc2[0] ||
390 s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
398 int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
401 const yaffs_ExtendedTags *eTags)
406 yaffs_SpareInitialise(&spare);
408 if (eTags->chunkDeleted)
409 spare.pageStatus = 0;
411 tags.objectId = eTags->objectId;
412 tags.chunkId = eTags->chunkId;
414 tags.byteCountLSB = eTags->byteCount & 0x3ff;
416 if (dev->nDataBytesPerChunk >= 1024)
417 tags.byteCountMSB = (eTags->byteCount >> 10) & 3;
419 tags.byteCountMSB = 3;
422 tags.serialNumber = eTags->serialNumber;
424 if (!dev->useNANDECC && data)
425 yaffs_CalcECC(data, &spare);
427 yaffs_LoadTagsIntoSpare(&spare, &tags);
431 return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
434 int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
437 yaffs_ExtendedTags *eTags)
442 yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_UNKNOWN;
444 static yaffs_Spare spareFF;
448 memset(&spareFF, 0xFF, sizeof(spareFF));
452 if (yaffs_ReadChunkFromNAND
453 (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
454 /* eTags may be NULL */
458 (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
460 eTags->chunkDeleted = deleted;
461 eTags->eccResult = eccResult;
462 eTags->blockBad = 0; /* We're reading it */
463 /* therefore it is not a bad block */
465 (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
468 if (eTags->chunkUsed) {
469 yaffs_GetTagsFromSpare(dev, &spare, &tags);
471 eTags->objectId = tags.objectId;
472 eTags->chunkId = tags.chunkId;
473 eTags->byteCount = tags.byteCountLSB;
475 if (dev->nDataBytesPerChunk >= 1024)
476 eTags->byteCount |= (((unsigned) tags.byteCountMSB) << 10);
478 eTags->serialNumber = tags.serialNumber;
488 int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
494 memset(&spare, 0xff, sizeof(yaffs_Spare));
496 spare.blockStatus = 'Y';
498 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
500 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
507 int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
509 yaffs_BlockState *state,
510 __u32 *sequenceNumber)
513 yaffs_Spare spare0, spare1;
514 static yaffs_Spare spareFF;
516 yaffs_ECCResult dummy;
519 memset(&spareFF, 0xFF, sizeof(spareFF));
525 yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
527 yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
530 if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
531 *state = YAFFS_BLOCK_STATE_DEAD;
532 else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
533 *state = YAFFS_BLOCK_STATE_EMPTY;
535 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;