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) {
88 int yaffs_CheckECCOnTags(yaffs_Tags * tags)
90 unsigned ecc = tags->ecc;
92 yaffs_CalcTagsECC(tags);
96 if (ecc && ecc <= 64) {
97 /* TODO: Handle the failure better. Retire? */
98 unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
102 b[ecc / 8] ^= (1 << (ecc & 7));
104 /* Now recvalc the ecc */
105 yaffs_CalcTagsECC(tags);
107 return 1; /* recovered error */
109 /* Wierd ecc failure value */
110 /* TODO Need to do somethiong here */
111 return -1; /* unrecovered error */
117 /********** Tags **********/
119 static void yaffs_LoadTagsIntoSpare(yaffs_Spare * sparePtr,
120 yaffs_Tags * tagsPtr)
122 yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
124 yaffs_CalcTagsECC(tagsPtr);
126 sparePtr->tagByte0 = tu->asBytes[0];
127 sparePtr->tagByte1 = tu->asBytes[1];
128 sparePtr->tagByte2 = tu->asBytes[2];
129 sparePtr->tagByte3 = tu->asBytes[3];
130 sparePtr->tagByte4 = tu->asBytes[4];
131 sparePtr->tagByte5 = tu->asBytes[5];
132 sparePtr->tagByte6 = tu->asBytes[6];
133 sparePtr->tagByte7 = tu->asBytes[7];
136 static void yaffs_GetTagsFromSpare(yaffs_Device * dev, yaffs_Spare * sparePtr,
137 yaffs_Tags * tagsPtr)
139 yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
142 tu->asBytes[0] = sparePtr->tagByte0;
143 tu->asBytes[1] = sparePtr->tagByte1;
144 tu->asBytes[2] = sparePtr->tagByte2;
145 tu->asBytes[3] = sparePtr->tagByte3;
146 tu->asBytes[4] = sparePtr->tagByte4;
147 tu->asBytes[5] = sparePtr->tagByte5;
148 tu->asBytes[6] = sparePtr->tagByte6;
149 tu->asBytes[7] = sparePtr->tagByte7;
151 result = yaffs_CheckECCOnTags(tagsPtr);
154 } else if (result < 0) {
155 dev->tagsEccUnfixed++;
159 static void yaffs_SpareInitialise(yaffs_Spare * spare)
161 memset(spare, 0xFF, sizeof(yaffs_Spare));
164 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
165 int chunkInNAND, const __u8 * data,
168 if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
170 (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
176 return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
179 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
183 yaffs_ECCResult * eccResult,
184 int doErrorCorrection)
187 yaffs_Spare localSpare;
191 if (!spare && data) {
192 /* If we don't have a real spare, then we use a local one. */
193 /* Need this for the calculation of the ecc */
197 if (!dev->useNANDECC) {
198 retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
199 if (data && doErrorCorrection) {
200 /* Do ECC correction */
201 /* Todo handle any errors */
202 int eccResult1, eccResult2;
205 yaffs_ECCCalculate(data, calcEcc);
207 yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
208 yaffs_ECCCalculate(&data[256], calcEcc);
210 yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
212 if (eccResult1 > 0) {
215 ("**>>yaffs ecc error fix performed on chunk %d:0"
216 TENDSTR), chunkInNAND));
218 } else if (eccResult1 < 0) {
221 ("**>>yaffs ecc error unfixed on chunk %d:0"
222 TENDSTR), chunkInNAND));
226 if (eccResult2 > 0) {
229 ("**>>yaffs ecc error fix performed on chunk %d:1"
230 TENDSTR), chunkInNAND));
232 } else if (eccResult2 < 0) {
235 ("**>>yaffs ecc error unfixed on chunk %d:1"
236 TENDSTR), chunkInNAND));
240 if (eccResult1 || eccResult2) {
241 /* We had a data problem on this page */
242 yaffs_HandleReadDataError(dev, chunkInNAND);
245 if (eccResult1 < 0 || eccResult2 < 0)
246 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
247 else if (eccResult1 > 0 || eccResult2 > 0)
248 *eccResult = YAFFS_ECC_RESULT_FIXED;
250 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
253 /* Must allocate enough memory for spare+2*sizeof(int) */
254 /* for ecc results from device. */
255 struct yaffs_NANDSpare nspare;
257 memset(&nspare,0,sizeof(nspare));
260 dev->readChunkFromNAND(dev, chunkInNAND, data,
261 (yaffs_Spare *) & nspare);
262 memcpy(spare, &nspare, sizeof(yaffs_Spare));
263 if (data && doErrorCorrection) {
264 if (nspare.eccres1 > 0) {
267 ("**>>mtd ecc error fix performed on chunk %d:0"
268 TENDSTR), chunkInNAND));
269 } else if (nspare.eccres1 < 0) {
272 ("**>>mtd ecc error unfixed on chunk %d:0"
273 TENDSTR), chunkInNAND));
276 if (nspare.eccres2 > 0) {
279 ("**>>mtd ecc error fix performed on chunk %d:1"
280 TENDSTR), chunkInNAND));
281 } else if (nspare.eccres2 < 0) {
284 ("**>>mtd ecc error unfixed on chunk %d:1"
285 TENDSTR), chunkInNAND));
288 if (nspare.eccres1 || nspare.eccres2) {
289 /* We had a data problem on this page */
290 yaffs_HandleReadDataError(dev, chunkInNAND);
293 if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
294 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
295 else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
296 *eccResult = YAFFS_ECC_RESULT_FIXED;
298 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
306 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
311 static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
312 static __u8 data[YAFFS_BYTES_PER_CHUNK];
313 /* Might as well always allocate the larger size for */
314 /* dev->useNANDECC == true; */
315 static __u8 spare[sizeof(struct yaffs_NANDSpare)];
317 dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
320 memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
324 if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
326 if (memcmp(cmpbuf, spare, 16))
335 * Functions for robustisizing
338 static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND)
340 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
342 /* Mark the block for retirement */
343 yaffs_GetBlockInfo(dev, blockInNAND + dev->blockOffset)->needsRetiring = 1;
344 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
345 (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
348 * Just do a garbage collection on the affected block
349 * then retire the block
355 static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND)
359 static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
361 const yaffs_Spare * spare)
365 static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
366 const yaffs_Spare * spare)
370 static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND)
372 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
374 /* Mark the block for retirement */
375 yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
376 /* Delete the chunk */
377 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
380 static int yaffs_VerifyCompare(const __u8 * d0, const __u8 * d1,
381 const yaffs_Spare * s0, const yaffs_Spare * s1)
384 if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
385 s0->tagByte0 != s1->tagByte0 ||
386 s0->tagByte1 != s1->tagByte1 ||
387 s0->tagByte2 != s1->tagByte2 ||
388 s0->tagByte3 != s1->tagByte3 ||
389 s0->tagByte4 != s1->tagByte4 ||
390 s0->tagByte5 != s1->tagByte5 ||
391 s0->tagByte6 != s1->tagByte6 ||
392 s0->tagByte7 != s1->tagByte7 ||
393 s0->ecc1[0] != s1->ecc1[0] ||
394 s0->ecc1[1] != s1->ecc1[1] ||
395 s0->ecc1[2] != s1->ecc1[2] ||
396 s0->ecc2[0] != s1->ecc2[0] ||
397 s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
405 int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
408 const yaffs_ExtendedTags *
414 yaffs_SpareInitialise(&spare);
416 if (eTags->chunkDeleted) {
417 spare.pageStatus = 0;
419 tags.objectId = eTags->objectId;
420 tags.chunkId = eTags->chunkId;
422 tags.byteCountLSB = eTags->byteCount & 0x3ff;
424 if(dev->nDataBytesPerChunk >= 1024){
425 tags.byteCountMSB = (eTags->byteCount >> 10) & 3;
427 tags.byteCountMSB = 3;
431 tags.serialNumber = eTags->serialNumber;
433 if (!dev->useNANDECC && data) {
434 yaffs_CalcECC(data, &spare);
436 yaffs_LoadTagsIntoSpare(&spare, &tags);
440 return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
443 int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
446 yaffs_ExtendedTags * eTags)
451 yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_UNKNOWN;
453 static yaffs_Spare spareFF;
457 memset(&spareFF, 0xFF, sizeof(spareFF));
461 if (yaffs_ReadChunkFromNAND
462 (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
463 /* eTags may be NULL */
467 (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
469 eTags->chunkDeleted = deleted;
470 eTags->eccResult = eccResult;
471 eTags->blockBad = 0; /* We're reading it */
472 /* therefore it is not a bad block */
474 (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
477 if (eTags->chunkUsed) {
478 yaffs_GetTagsFromSpare(dev, &spare, &tags);
480 eTags->objectId = tags.objectId;
481 eTags->chunkId = tags.chunkId;
482 eTags->byteCount = tags.byteCountLSB;
484 if(dev->nDataBytesPerChunk >= 1024)
485 eTags->byteCount |= (((unsigned) tags.byteCountMSB) << 10);
487 eTags->serialNumber = tags.serialNumber;
497 int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
503 memset(&spare, 0xff, sizeof(yaffs_Spare));
505 spare.blockStatus = 'Y';
507 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
509 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
516 int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
518 yaffs_BlockState *state,
519 __u32 *sequenceNumber)
522 yaffs_Spare spare0, spare1;
523 static yaffs_Spare spareFF;
525 yaffs_ECCResult dummy;
528 memset(&spareFF, 0xFF, sizeof(spareFF));
534 yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
536 yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
539 if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
540 *state = YAFFS_BLOCK_STATE_DEAD;
541 else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
542 *state = YAFFS_BLOCK_STATE_EMPTY;
544 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;