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),
174 return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
177 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
181 yaffs_ECCResult *eccResult,
182 int doErrorCorrection)
185 yaffs_Spare localSpare;
189 if (!spare && data) {
190 /* If we don't have a real spare, then we use a local one. */
191 /* Need this for the calculation of the ecc */
195 if (!dev->useNANDECC) {
196 retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
197 if (data && doErrorCorrection) {
198 /* Do ECC correction */
199 /* Todo handle any errors */
200 int eccResult1, eccResult2;
203 yaffs_ECCCalculate(data, calcEcc);
205 yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
206 yaffs_ECCCalculate(&data[256], calcEcc);
208 yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
210 if (eccResult1 > 0) {
213 ("**>>yaffs ecc error fix performed on chunk %d:0"
214 TENDSTR), chunkInNAND));
216 } else if (eccResult1 < 0) {
219 ("**>>yaffs ecc error unfixed on chunk %d:0"
220 TENDSTR), chunkInNAND));
224 if (eccResult2 > 0) {
227 ("**>>yaffs ecc error fix performed on chunk %d:1"
228 TENDSTR), chunkInNAND));
230 } else if (eccResult2 < 0) {
233 ("**>>yaffs ecc error unfixed on chunk %d:1"
234 TENDSTR), chunkInNAND));
238 if (eccResult1 || eccResult2) {
239 /* We had a data problem on this page */
240 yaffs_HandleReadDataError(dev, chunkInNAND);
243 if (eccResult1 < 0 || eccResult2 < 0)
244 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
245 else if (eccResult1 > 0 || eccResult2 > 0)
246 *eccResult = YAFFS_ECC_RESULT_FIXED;
248 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
251 /* Must allocate enough memory for spare+2*sizeof(int) */
252 /* for ecc results from device. */
253 struct yaffs_NANDSpare nspare;
255 memset(&nspare, 0, sizeof(nspare));
257 retVal = dev->readChunkFromNAND(dev, chunkInNAND, data,
258 (yaffs_Spare *) &nspare);
259 memcpy(spare, &nspare, sizeof(yaffs_Spare));
260 if (data && doErrorCorrection) {
261 if (nspare.eccres1 > 0) {
264 ("**>>mtd ecc error fix performed on chunk %d:0"
265 TENDSTR), chunkInNAND));
266 } else if (nspare.eccres1 < 0) {
269 ("**>>mtd ecc error unfixed on chunk %d:0"
270 TENDSTR), chunkInNAND));
273 if (nspare.eccres2 > 0) {
276 ("**>>mtd ecc error fix performed on chunk %d:1"
277 TENDSTR), chunkInNAND));
278 } else if (nspare.eccres2 < 0) {
281 ("**>>mtd ecc error unfixed on chunk %d:1"
282 TENDSTR), chunkInNAND));
285 if (nspare.eccres1 || nspare.eccres2) {
286 /* We had a data problem on this page */
287 yaffs_HandleReadDataError(dev, chunkInNAND);
290 if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
291 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
292 else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
293 *eccResult = YAFFS_ECC_RESULT_FIXED;
295 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
303 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
307 static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
308 static __u8 data[YAFFS_BYTES_PER_CHUNK];
309 /* Might as well always allocate the larger size for */
310 /* dev->useNANDECC == true; */
311 static __u8 spare[sizeof(struct yaffs_NANDSpare)];
313 dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
316 memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
320 if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
322 if (memcmp(cmpbuf, spare, 16))
331 * Functions for robustisizing
334 static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND)
336 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
338 /* Mark the block for retirement */
339 yaffs_GetBlockInfo(dev, blockInNAND + dev->blockOffset)->needsRetiring = 1;
340 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
341 (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
344 * Just do a garbage collection on the affected block
345 * then retire the block
351 static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND)
355 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
357 const yaffs_Spare *spare)
361 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
362 const yaffs_Spare *spare)
366 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND)
368 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
370 /* Mark the block for retirement */
371 yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
372 /* Delete the chunk */
373 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
376 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 *d1,
377 const yaffs_Spare *s0, const yaffs_Spare *s1)
380 if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
381 s0->tagByte0 != s1->tagByte0 ||
382 s0->tagByte1 != s1->tagByte1 ||
383 s0->tagByte2 != s1->tagByte2 ||
384 s0->tagByte3 != s1->tagByte3 ||
385 s0->tagByte4 != s1->tagByte4 ||
386 s0->tagByte5 != s1->tagByte5 ||
387 s0->tagByte6 != s1->tagByte6 ||
388 s0->tagByte7 != s1->tagByte7 ||
389 s0->ecc1[0] != s1->ecc1[0] ||
390 s0->ecc1[1] != s1->ecc1[1] ||
391 s0->ecc1[2] != s1->ecc1[2] ||
392 s0->ecc2[0] != s1->ecc2[0] ||
393 s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
401 int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
404 const yaffs_ExtendedTags *eTags)
409 yaffs_SpareInitialise(&spare);
411 if (eTags->chunkDeleted)
412 spare.pageStatus = 0;
414 tags.objectId = eTags->objectId;
415 tags.chunkId = eTags->chunkId;
417 tags.byteCountLSB = eTags->byteCount & 0x3ff;
419 if (dev->nDataBytesPerChunk >= 1024)
420 tags.byteCountMSB = (eTags->byteCount >> 10) & 3;
422 tags.byteCountMSB = 3;
425 tags.serialNumber = eTags->serialNumber;
427 if (!dev->useNANDECC && data)
428 yaffs_CalcECC(data, &spare);
430 yaffs_LoadTagsIntoSpare(&spare, &tags);
434 return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
437 int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
440 yaffs_ExtendedTags *eTags)
445 yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_UNKNOWN;
447 static yaffs_Spare spareFF;
451 memset(&spareFF, 0xFF, sizeof(spareFF));
455 if (yaffs_ReadChunkFromNAND
456 (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
457 /* eTags may be NULL */
461 (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
463 eTags->chunkDeleted = deleted;
464 eTags->eccResult = eccResult;
465 eTags->blockBad = 0; /* We're reading it */
466 /* therefore it is not a bad block */
468 (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
471 if (eTags->chunkUsed) {
472 yaffs_GetTagsFromSpare(dev, &spare, &tags);
474 eTags->objectId = tags.objectId;
475 eTags->chunkId = tags.chunkId;
476 eTags->byteCount = tags.byteCountLSB;
478 if (dev->nDataBytesPerChunk >= 1024)
479 eTags->byteCount |= (((unsigned) tags.byteCountMSB) << 10);
481 eTags->serialNumber = tags.serialNumber;
491 int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
497 memset(&spare, 0xff, sizeof(yaffs_Spare));
499 spare.blockStatus = 'Y';
501 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
503 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
510 int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
512 yaffs_BlockState *state,
513 __u32 *sequenceNumber)
516 yaffs_Spare spare0, spare1;
517 static yaffs_Spare spareFF;
519 yaffs_ECCResult dummy;
522 memset(&spareFF, 0xFF, sizeof(spareFF));
528 yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
530 yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
533 if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
534 *state = YAFFS_BLOCK_STATE_DEAD;
535 else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
536 *state = YAFFS_BLOCK_STATE_EMPTY;
538 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;