1 /*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
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
14 #include "yaffs_guts.h"
15 #include "yaffs_tagscompat.h"
16 #include "yaffs_ecc.h"
17 #include "yaffs_getblockinfo.h"
18
19 static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND);
20 #ifdef NOTYET
21 static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND);
22 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
23 const __u8 *data,
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);
28 #endif
29
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
47 };
48
yaffs_CountBits(__u8 x)49 int yaffs_CountBits(__u8 x)
50 {
51 int retVal;
52 retVal = yaffs_countBitsTable[x];
53 return retVal;
54 }
55
56 /********** Tags ECC calculations *********/
57
yaffs_CalcECC(const __u8 * data,yaffs_Spare * spare)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
yaffs_CalcTagsECC(yaffs_Tags * tags)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 for (j = 1; j & 0xff; j <<= 1) {
77 bit++;
78 if (b[i] & j)
79 ecc ^= bit;
80 }
81 }
82
83 tags->ecc = ecc;
84
85 }
86
yaffs_CheckECCOnTags(yaffs_Tags * tags)87 int yaffs_CheckECCOnTags(yaffs_Tags *tags)
88 {
89 unsigned ecc = tags->ecc;
90
91 yaffs_CalcTagsECC(tags);
92
93 ecc ^= tags->ecc;
94
95 if (ecc && ecc <= 64) {
96 /* TODO: Handle the failure better. Retire? */
97 unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
98
99 ecc--;
100
101 b[ecc / 8] ^= (1 << (ecc & 7));
102
103 /* Now recvalc the ecc */
104 yaffs_CalcTagsECC(tags);
105
106 return 1; /* recovered error */
107 } else if (ecc) {
108 /* Wierd ecc failure value */
109 /* TODO Need to do somethiong here */
110 return -1; /* unrecovered error */
111 }
112
113 return 0;
114 }
115
116 /********** Tags **********/
117
yaffs_LoadTagsIntoSpare(yaffs_Spare * sparePtr,yaffs_Tags * tagsPtr)118 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr,
119 yaffs_Tags *tagsPtr)
120 {
121 yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
122
123 yaffs_CalcTagsECC(tagsPtr);
124
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];
133 }
134
yaffs_GetTagsFromSpare(yaffs_Device * dev,yaffs_Spare * sparePtr,yaffs_Tags * tagsPtr)135 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,
136 yaffs_Tags *tagsPtr)
137 {
138 yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
139 int result;
140
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;
149
150 result = yaffs_CheckECCOnTags(tagsPtr);
151 if (result > 0)
152 dev->tagsEccFixed++;
153 else if (result < 0)
154 dev->tagsEccUnfixed++;
155 }
156
yaffs_SpareInitialise(yaffs_Spare * spare)157 static void yaffs_SpareInitialise(yaffs_Spare *spare)
158 {
159 memset(spare, 0xFF, sizeof(yaffs_Spare));
160 }
161
yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct * dev,int chunkInNAND,const __u8 * data,yaffs_Spare * spare)162 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
163 int chunkInNAND, const __u8 *data,
164 yaffs_Spare *spare)
165 {
166 if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
167 T(YAFFS_TRACE_ERROR,
168 (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
169 chunkInNAND));
170 return YAFFS_FAIL;
171 }
172
173 return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
174 }
175
yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct * dev,int chunkInNAND,__u8 * data,yaffs_Spare * spare,yaffs_ECCResult * eccResult,int doErrorCorrection)176 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
177 int chunkInNAND,
178 __u8 *data,
179 yaffs_Spare *spare,
180 yaffs_ECCResult *eccResult,
181 int doErrorCorrection)
182 {
183 int retVal;
184 yaffs_Spare localSpare;
185
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 */
189 spare = &localSpare;
190 }
191
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;
198 __u8 calcEcc[3];
199
200 yaffs_ECCCalculate(data, calcEcc);
201 eccResult1 =
202 yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
203 yaffs_ECCCalculate(&data[256], calcEcc);
204 eccResult2 =
205 yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
206
207 if (eccResult1 > 0) {
208 T(YAFFS_TRACE_ERROR,
209 (TSTR
210 ("**>>yaffs ecc error fix performed on chunk %d:0"
211 TENDSTR), chunkInNAND));
212 dev->eccFixed++;
213 } else if (eccResult1 < 0) {
214 T(YAFFS_TRACE_ERROR,
215 (TSTR
216 ("**>>yaffs ecc error unfixed on chunk %d:0"
217 TENDSTR), chunkInNAND));
218 dev->eccUnfixed++;
219 }
220
221 if (eccResult2 > 0) {
222 T(YAFFS_TRACE_ERROR,
223 (TSTR
224 ("**>>yaffs ecc error fix performed on chunk %d:1"
225 TENDSTR), chunkInNAND));
226 dev->eccFixed++;
227 } else if (eccResult2 < 0) {
228 T(YAFFS_TRACE_ERROR,
229 (TSTR
230 ("**>>yaffs ecc error unfixed on chunk %d:1"
231 TENDSTR), chunkInNAND));
232 dev->eccUnfixed++;
233 }
234
235 if (eccResult1 || eccResult2) {
236 /* We had a data problem on this page */
237 yaffs_HandleReadDataError(dev, chunkInNAND);
238 }
239
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;
244 else
245 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
246 }
247 } else {
248 /* Must allocate enough memory for spare+2*sizeof(int) */
249 /* for ecc results from device. */
250 struct yaffs_NANDSpare nspare;
251
252 memset(&nspare, 0, sizeof(nspare));
253
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) {
259 T(YAFFS_TRACE_ERROR,
260 (TSTR
261 ("**>>mtd ecc error fix performed on chunk %d:0"
262 TENDSTR), chunkInNAND));
263 } else if (nspare.eccres1 < 0) {
264 T(YAFFS_TRACE_ERROR,
265 (TSTR
266 ("**>>mtd ecc error unfixed on chunk %d:0"
267 TENDSTR), chunkInNAND));
268 }
269
270 if (nspare.eccres2 > 0) {
271 T(YAFFS_TRACE_ERROR,
272 (TSTR
273 ("**>>mtd ecc error fix performed on chunk %d:1"
274 TENDSTR), chunkInNAND));
275 } else if (nspare.eccres2 < 0) {
276 T(YAFFS_TRACE_ERROR,
277 (TSTR
278 ("**>>mtd ecc error unfixed on chunk %d:1"
279 TENDSTR), chunkInNAND));
280 }
281
282 if (nspare.eccres1 || nspare.eccres2) {
283 /* We had a data problem on this page */
284 yaffs_HandleReadDataError(dev, chunkInNAND);
285 }
286
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;
291 else
292 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
293
294 }
295 }
296 return retVal;
297 }
298
299 #ifdef NOTYET
yaffs_CheckChunkErased(struct yaffs_DeviceStruct * dev,int chunkInNAND)300 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
301 int chunkInNAND)
302 {
303 static int init;
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)];
309
310 dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
311
312 if (!init) {
313 memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
314 init = 1;
315 }
316
317 if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
318 return YAFFS_FAIL;
319 if (memcmp(cmpbuf, spare, 16))
320 return YAFFS_FAIL;
321
322 return YAFFS_OK;
323
324 }
325 #endif
326
327 /*
328 * Functions for robustisizing
329 */
330
yaffs_HandleReadDataError(yaffs_Device * dev,int chunkInNAND)331 static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND)
332 {
333 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
334
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));
339
340 /* TODO:
341 * Just do a garbage collection on the affected block
342 * then retire the block
343 * NB recursion
344 */
345 }
346
347 #ifdef NOTYET
yaffs_CheckWrittenBlock(yaffs_Device * dev,int chunkInNAND)348 static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND)
349 {
350 }
351
yaffs_HandleWriteChunkOk(yaffs_Device * dev,int chunkInNAND,const __u8 * data,const yaffs_Spare * spare)352 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
353 const __u8 *data,
354 const yaffs_Spare *spare)
355 {
356 }
357
yaffs_HandleUpdateChunk(yaffs_Device * dev,int chunkInNAND,const yaffs_Spare * spare)358 static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
359 const yaffs_Spare *spare)
360 {
361 }
362
yaffs_HandleWriteChunkError(yaffs_Device * dev,int chunkInNAND)363 static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND)
364 {
365 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
366
367 /* Mark the block for retirement */
368 yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
369 /* Delete the chunk */
370 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
371 }
372
yaffs_VerifyCompare(const __u8 * d0,const __u8 * d1,const yaffs_Spare * s0,const yaffs_Spare * s1)373 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 *d1,
374 const yaffs_Spare *s0, const yaffs_Spare *s1)
375 {
376
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]) {
391 return 0;
392 }
393
394 return 1;
395 }
396 #endif /* NOTYET */
397
yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,int chunkInNAND,const __u8 * data,const yaffs_ExtendedTags * eTags)398 int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
399 int chunkInNAND,
400 const __u8 *data,
401 const yaffs_ExtendedTags *eTags)
402 {
403 yaffs_Spare spare;
404 yaffs_Tags tags;
405
406 yaffs_SpareInitialise(&spare);
407
408 if (eTags->chunkDeleted)
409 spare.pageStatus = 0;
410 else {
411 tags.objectId = eTags->objectId;
412 tags.chunkId = eTags->chunkId;
413
414 tags.byteCountLSB = eTags->byteCount & 0x3ff;
415
416 if (dev->nDataBytesPerChunk >= 1024)
417 tags.byteCountMSB = (eTags->byteCount >> 10) & 3;
418 else
419 tags.byteCountMSB = 3;
420
421
422 tags.serialNumber = eTags->serialNumber;
423
424 if (!dev->useNANDECC && data)
425 yaffs_CalcECC(data, &spare);
426
427 yaffs_LoadTagsIntoSpare(&spare, &tags);
428
429 }
430
431 return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
432 }
433
yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,int chunkInNAND,__u8 * data,yaffs_ExtendedTags * eTags)434 int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
435 int chunkInNAND,
436 __u8 *data,
437 yaffs_ExtendedTags *eTags)
438 {
439
440 yaffs_Spare spare;
441 yaffs_Tags tags;
442 yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_UNKNOWN;
443
444 static yaffs_Spare spareFF;
445 static int init;
446
447 if (!init) {
448 memset(&spareFF, 0xFF, sizeof(spareFF));
449 init = 1;
450 }
451
452 if (yaffs_ReadChunkFromNAND
453 (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
454 /* eTags may be NULL */
455 if (eTags) {
456
457 int deleted =
458 (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
459
460 eTags->chunkDeleted = deleted;
461 eTags->eccResult = eccResult;
462 eTags->blockBad = 0; /* We're reading it */
463 /* therefore it is not a bad block */
464 eTags->chunkUsed =
465 (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
466 0) ? 1 : 0;
467
468 if (eTags->chunkUsed) {
469 yaffs_GetTagsFromSpare(dev, &spare, &tags);
470
471 eTags->objectId = tags.objectId;
472 eTags->chunkId = tags.chunkId;
473 eTags->byteCount = tags.byteCountLSB;
474
475 if (dev->nDataBytesPerChunk >= 1024)
476 eTags->byteCount |= (((unsigned) tags.byteCountMSB) << 10);
477
478 eTags->serialNumber = tags.serialNumber;
479 }
480 }
481
482 return YAFFS_OK;
483 } else {
484 return YAFFS_FAIL;
485 }
486 }
487
yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct * dev,int blockInNAND)488 int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
489 int blockInNAND)
490 {
491
492 yaffs_Spare spare;
493
494 memset(&spare, 0xff, sizeof(yaffs_Spare));
495
496 spare.blockStatus = 'Y';
497
498 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
499 &spare);
500 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
501 NULL, &spare);
502
503 return YAFFS_OK;
504
505 }
506
yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct * dev,int blockNo,yaffs_BlockState * state,__u32 * sequenceNumber)507 int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
508 int blockNo,
509 yaffs_BlockState *state,
510 __u32 *sequenceNumber)
511 {
512
513 yaffs_Spare spare0, spare1;
514 static yaffs_Spare spareFF;
515 static int init;
516 yaffs_ECCResult dummy;
517
518 if (!init) {
519 memset(&spareFF, 0xFF, sizeof(spareFF));
520 init = 1;
521 }
522
523 *sequenceNumber = 0;
524
525 yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
526 &spare0, &dummy, 1);
527 yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
528 &spare1, &dummy, 1);
529
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;
534 else
535 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
536
537 return YAFFS_OK;
538 }
539