2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 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"
18 #include "yaffs_trace.h"
20 static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND);
22 static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND);
23 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
25 const yaffs_Spare *spare);
26 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
27 const yaffs_Spare *spare);
28 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND);
31 static const char yaffs_countBitsTable[256] = {
32 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
33 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
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 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
37 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
38 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
39 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
40 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
41 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
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 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
45 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
46 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
47 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
50 int yaffs_CountBits(__u8 x)
53 retVal = yaffs_countBitsTable[x];
57 /********** Tags ECC calculations *********/
59 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
61 yaffs_ECCCalculate(data, spare->ecc1);
62 yaffs_ECCCalculate(&data[256], spare->ecc2);
65 void yaffs_CalcTagsECC(yaffs_Tags *tags)
67 /* Calculate an ecc */
69 unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
76 for (i = 0; i < 8; i++) {
77 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,
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,
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);
155 dev->tagsEccUnfixed++;
158 static void yaffs_SpareInitialise(yaffs_Spare *spare)
160 memset(spare, 0xFF, sizeof(yaffs_Spare));
163 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
164 int chunkInNAND, const __u8 *data,
167 if (chunkInNAND < dev->param.startBlock * dev->param.nChunksPerBlock) {
169 (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
174 return dev->param.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;
187 if (!spare && data) {
188 /* If we don't have a real spare, then we use a local one. */
189 /* Need this for the calculation of the ecc */
193 if (!dev->param.useNANDECC) {
194 retVal = dev->param.readChunkFromNAND(dev, chunkInNAND, data, spare);
195 if (data && doErrorCorrection) {
196 /* Do ECC correction */
197 /* Todo handle any errors */
198 int eccResult1, eccResult2;
201 yaffs_ECCCalculate(data, calcEcc);
203 yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
204 yaffs_ECCCalculate(&data[256], calcEcc);
206 yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
208 if (eccResult1 > 0) {
211 ("**>>yaffs ecc error fix performed on chunk %d:0"
212 TENDSTR), chunkInNAND));
214 } else if (eccResult1 < 0) {
217 ("**>>yaffs ecc error unfixed on chunk %d:0"
218 TENDSTR), chunkInNAND));
222 if (eccResult2 > 0) {
225 ("**>>yaffs ecc error fix performed on chunk %d:1"
226 TENDSTR), chunkInNAND));
228 } else if (eccResult2 < 0) {
231 ("**>>yaffs ecc error unfixed on chunk %d:1"
232 TENDSTR), chunkInNAND));
236 if (eccResult1 || eccResult2) {
237 /* We had a data problem on this page */
238 yaffs_HandleReadDataError(dev, chunkInNAND);
241 if (eccResult1 < 0 || eccResult2 < 0)
242 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
243 else if (eccResult1 > 0 || eccResult2 > 0)
244 *eccResult = YAFFS_ECC_RESULT_FIXED;
246 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
249 /* Must allocate enough memory for spare+2*sizeof(int) */
250 /* for ecc results from device. */
251 struct yaffs_NANDSpare nspare;
253 memset(&nspare, 0, sizeof(nspare));
255 retVal = dev->param.readChunkFromNAND(dev, chunkInNAND, data,
256 (yaffs_Spare *) &nspare);
257 memcpy(spare, &nspare, sizeof(yaffs_Spare));
258 if (data && doErrorCorrection) {
259 if (nspare.eccres1 > 0) {
262 ("**>>mtd ecc error fix performed on chunk %d:0"
263 TENDSTR), chunkInNAND));
264 } else if (nspare.eccres1 < 0) {
267 ("**>>mtd ecc error unfixed on chunk %d:0"
268 TENDSTR), chunkInNAND));
271 if (nspare.eccres2 > 0) {
274 ("**>>mtd ecc error fix performed on chunk %d:1"
275 TENDSTR), chunkInNAND));
276 } else if (nspare.eccres2 < 0) {
279 ("**>>mtd ecc error unfixed on chunk %d:1"
280 TENDSTR), chunkInNAND));
283 if (nspare.eccres1 || nspare.eccres2) {
284 /* We had a data problem on this page */
285 yaffs_HandleReadDataError(dev, chunkInNAND);
288 if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
289 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
290 else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
291 *eccResult = YAFFS_ECC_RESULT_FIXED;
293 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
301 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
305 static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
306 static __u8 data[YAFFS_BYTES_PER_CHUNK];
307 /* Might as well always allocate the larger size for */
308 /* dev->param.useNANDECC == true; */
309 static __u8 spare[sizeof(struct yaffs_NANDSpare)];
311 dev->param.readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
314 memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
318 if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
320 if (memcmp(cmpbuf, spare, 16))
329 * Functions for robustisizing
332 static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND)
334 int blockInNAND = chunkInNAND / dev->param.nChunksPerBlock;
336 /* Mark the block for retirement */
337 yaffs_GetBlockInfo(dev, blockInNAND + dev->blockOffset)->needsRetiring = 1;
338 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
339 (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
342 * Just do a garbage collection on the affected block
343 * then retire the block
349 static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND)
353 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
355 const yaffs_Spare *spare)
359 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
360 const yaffs_Spare *spare)
364 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND)
366 int blockInNAND = chunkInNAND / dev->param.nChunksPerBlock;
368 /* Mark the block for retirement */
369 yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
370 /* Delete the chunk */
371 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
374 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 *d1,
375 const yaffs_Spare *s0, const yaffs_Spare *s1)
378 if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
379 s0->tagByte0 != s1->tagByte0 ||
380 s0->tagByte1 != s1->tagByte1 ||
381 s0->tagByte2 != s1->tagByte2 ||
382 s0->tagByte3 != s1->tagByte3 ||
383 s0->tagByte4 != s1->tagByte4 ||
384 s0->tagByte5 != s1->tagByte5 ||
385 s0->tagByte6 != s1->tagByte6 ||
386 s0->tagByte7 != s1->tagByte7 ||
387 s0->ecc1[0] != s1->ecc1[0] ||
388 s0->ecc1[1] != s1->ecc1[1] ||
389 s0->ecc1[2] != s1->ecc1[2] ||
390 s0->ecc2[0] != s1->ecc2[0] ||
391 s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
399 int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
402 const yaffs_ExtendedTags *eTags)
407 yaffs_SpareInitialise(&spare);
409 if (eTags->chunkDeleted)
410 spare.pageStatus = 0;
412 tags.objectId = eTags->objectId;
413 tags.chunkId = eTags->chunkId;
415 tags.byteCountLSB = eTags->byteCount & 0x3ff;
417 if (dev->nDataBytesPerChunk >= 1024)
418 tags.byteCountMSB = (eTags->byteCount >> 10) & 3;
420 tags.byteCountMSB = 3;
423 tags.serialNumber = eTags->serialNumber;
425 if (!dev->param.useNANDECC && data)
426 yaffs_CalcECC(data, &spare);
428 yaffs_LoadTagsIntoSpare(&spare, &tags);
432 return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
435 int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
438 yaffs_ExtendedTags *eTags)
443 yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_UNKNOWN;
445 static yaffs_Spare spareFF;
449 memset(&spareFF, 0xFF, sizeof(spareFF));
453 if (yaffs_ReadChunkFromNAND
454 (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
455 /* eTags may be NULL */
459 (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
461 eTags->chunkDeleted = deleted;
462 eTags->eccResult = eccResult;
463 eTags->blockBad = 0; /* We're reading it */
464 /* therefore it is not a bad block */
466 (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
469 if (eTags->chunkUsed) {
470 yaffs_GetTagsFromSpare(dev, &spare, &tags);
472 eTags->objectId = tags.objectId;
473 eTags->chunkId = tags.chunkId;
474 eTags->byteCount = tags.byteCountLSB;
476 if (dev->nDataBytesPerChunk >= 1024)
477 eTags->byteCount |= (((unsigned) tags.byteCountMSB) << 10);
479 eTags->serialNumber = tags.serialNumber;
489 int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
495 memset(&spare, 0xff, sizeof(yaffs_Spare));
497 spare.blockStatus = 'Y';
499 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->param.nChunksPerBlock, NULL,
501 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->param.nChunksPerBlock + 1,
508 int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
510 yaffs_BlockState *state,
511 __u32 *sequenceNumber)
514 yaffs_Spare spare0, spare1;
515 static yaffs_Spare spareFF;
517 yaffs_ECCResult dummy;
520 memset(&spareFF, 0xFF, sizeof(spareFF));
526 yaffs_ReadChunkFromNAND(dev, blockNo * dev->param.nChunksPerBlock, NULL,
528 yaffs_ReadChunkFromNAND(dev, blockNo * dev->param.nChunksPerBlock + 1, NULL,
531 if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
532 *state = YAFFS_BLOCK_STATE_DEAD;
533 else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
534 *state = YAFFS_BLOCK_STATE_EMPTY;
536 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;